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 1401fa1e7f6SAndy Whitcroft static char *getname_flags(const char __user *filename, int flags, int *empty) 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) { 1511fa1e7f6SAndy Whitcroft if (retval == -ENOENT && empty) 1521fa1e7f6SAndy Whitcroft *empty = 1; 153f52e0c11SAl Viro if (retval != -ENOENT || !(flags & LOOKUP_EMPTY)) { 1541da177e4SLinus Torvalds __putname(tmp); 1551da177e4SLinus Torvalds result = ERR_PTR(retval); 1561da177e4SLinus Torvalds } 1571da177e4SLinus Torvalds } 158f52e0c11SAl Viro } 1591da177e4SLinus Torvalds audit_getname(result); 1601da177e4SLinus Torvalds return result; 1611da177e4SLinus Torvalds } 1621da177e4SLinus Torvalds 163f52e0c11SAl Viro char *getname(const char __user * filename) 164f52e0c11SAl Viro { 1651fa1e7f6SAndy Whitcroft return getname_flags(filename, 0, 0); 166f52e0c11SAl Viro } 167f52e0c11SAl Viro 1681da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL 1691da177e4SLinus Torvalds void putname(const char *name) 1701da177e4SLinus Torvalds { 1715ac3a9c2SAl Viro if (unlikely(!audit_dummy_context())) 1721da177e4SLinus Torvalds audit_putname(name); 1731da177e4SLinus Torvalds else 1741da177e4SLinus Torvalds __putname(name); 1751da177e4SLinus Torvalds } 1761da177e4SLinus Torvalds EXPORT_SYMBOL(putname); 1771da177e4SLinus Torvalds #endif 1781da177e4SLinus Torvalds 179e77819e5SLinus Torvalds static int check_acl(struct inode *inode, int mask) 180e77819e5SLinus Torvalds { 18184635d68SLinus Torvalds #ifdef CONFIG_FS_POSIX_ACL 182e77819e5SLinus Torvalds struct posix_acl *acl; 183e77819e5SLinus Torvalds 184e77819e5SLinus Torvalds if (mask & MAY_NOT_BLOCK) { 1853567866bSAl Viro acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS); 1863567866bSAl Viro if (!acl) 187e77819e5SLinus Torvalds return -EAGAIN; 1883567866bSAl Viro /* no ->get_acl() calls in RCU mode... */ 1893567866bSAl Viro if (acl == ACL_NOT_CACHED) 190e77819e5SLinus Torvalds return -ECHILD; 191206b1d09SAri Savolainen return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK); 192e77819e5SLinus Torvalds } 193e77819e5SLinus Torvalds 194e77819e5SLinus Torvalds acl = get_cached_acl(inode, ACL_TYPE_ACCESS); 195e77819e5SLinus Torvalds 196e77819e5SLinus Torvalds /* 1974e34e719SChristoph Hellwig * A filesystem can force a ACL callback by just never filling the 1984e34e719SChristoph Hellwig * ACL cache. But normally you'd fill the cache either at inode 1994e34e719SChristoph Hellwig * instantiation time, or on the first ->get_acl call. 200e77819e5SLinus Torvalds * 2014e34e719SChristoph Hellwig * If the filesystem doesn't have a get_acl() function at all, we'll 2024e34e719SChristoph Hellwig * just create the negative cache entry. 203e77819e5SLinus Torvalds */ 204e77819e5SLinus Torvalds if (acl == ACL_NOT_CACHED) { 2054e34e719SChristoph Hellwig if (inode->i_op->get_acl) { 2064e34e719SChristoph Hellwig acl = inode->i_op->get_acl(inode, ACL_TYPE_ACCESS); 2074e34e719SChristoph Hellwig if (IS_ERR(acl)) 2084e34e719SChristoph Hellwig return PTR_ERR(acl); 2094e34e719SChristoph Hellwig } else { 210e77819e5SLinus Torvalds set_cached_acl(inode, ACL_TYPE_ACCESS, NULL); 211e77819e5SLinus Torvalds return -EAGAIN; 212e77819e5SLinus Torvalds } 2134e34e719SChristoph Hellwig } 214e77819e5SLinus Torvalds 215e77819e5SLinus Torvalds if (acl) { 216e77819e5SLinus Torvalds int error = posix_acl_permission(inode, acl, mask); 217e77819e5SLinus Torvalds posix_acl_release(acl); 218e77819e5SLinus Torvalds return error; 219e77819e5SLinus Torvalds } 22084635d68SLinus Torvalds #endif 221e77819e5SLinus Torvalds 222e77819e5SLinus Torvalds return -EAGAIN; 223e77819e5SLinus Torvalds } 224e77819e5SLinus Torvalds 2255909ccaaSLinus Torvalds /* 226948409c7SAndreas Gruenbacher * This does the basic permission checking 2275909ccaaSLinus Torvalds */ 2287e40145eSAl Viro static int acl_permission_check(struct inode *inode, int mask) 2295909ccaaSLinus Torvalds { 23026cf46beSLinus Torvalds unsigned int mode = inode->i_mode; 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 2608fd90c8dSAndreas Gruenbacher * @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 /* 276948409c7SAndreas Gruenbacher * Do the basic 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 3348fd90c8dSAndreas Gruenbacher * @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. 340948409c7SAndreas Gruenbacher * 341948409c7SAndreas Gruenbacher * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask. 342cb23beb5SChristoph Hellwig */ 343f419a2e3SAl Viro int inode_permission(struct inode *inode, int mask) 3441da177e4SLinus Torvalds { 345e6305c43SAl Viro int retval; 3461da177e4SLinus Torvalds 3473ddcd056SLinus Torvalds if (unlikely(mask & MAY_WRITE)) { 34822590e41SMiklos Szeredi umode_t mode = inode->i_mode; 3491da177e4SLinus Torvalds 3501da177e4SLinus Torvalds /* 3511da177e4SLinus Torvalds * Nobody gets write access to a read-only fs. 3521da177e4SLinus Torvalds */ 3531da177e4SLinus Torvalds if (IS_RDONLY(inode) && 3541da177e4SLinus Torvalds (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) 3551da177e4SLinus Torvalds return -EROFS; 3561da177e4SLinus Torvalds 3571da177e4SLinus Torvalds /* 3581da177e4SLinus Torvalds * Nobody gets write access to an immutable file. 3591da177e4SLinus Torvalds */ 3601da177e4SLinus Torvalds if (IS_IMMUTABLE(inode)) 3611da177e4SLinus Torvalds return -EACCES; 3621da177e4SLinus Torvalds } 3631da177e4SLinus Torvalds 3643ddcd056SLinus Torvalds retval = do_inode_permission(inode, mask); 3651da177e4SLinus Torvalds if (retval) 3661da177e4SLinus Torvalds return retval; 3671da177e4SLinus Torvalds 36808ce5f16SSerge E. Hallyn retval = devcgroup_inode_permission(inode, mask); 36908ce5f16SSerge E. Hallyn if (retval) 37008ce5f16SSerge E. Hallyn return retval; 37108ce5f16SSerge E. Hallyn 372d09ca739SEric Paris return security_inode_permission(inode, mask); 3731da177e4SLinus Torvalds } 3741da177e4SLinus Torvalds 375f4d6ff89SAl Viro /** 3765dd784d0SJan Blunck * path_get - get a reference to a path 3775dd784d0SJan Blunck * @path: path to get the reference to 3785dd784d0SJan Blunck * 3795dd784d0SJan Blunck * Given a path increment the reference count to the dentry and the vfsmount. 3805dd784d0SJan Blunck */ 3815dd784d0SJan Blunck void path_get(struct path *path) 3825dd784d0SJan Blunck { 3835dd784d0SJan Blunck mntget(path->mnt); 3845dd784d0SJan Blunck dget(path->dentry); 3855dd784d0SJan Blunck } 3865dd784d0SJan Blunck EXPORT_SYMBOL(path_get); 3875dd784d0SJan Blunck 3885dd784d0SJan Blunck /** 3891d957f9bSJan Blunck * path_put - put a reference to a path 3901d957f9bSJan Blunck * @path: path to put the reference to 3911d957f9bSJan Blunck * 3921d957f9bSJan Blunck * Given a path decrement the reference count to the dentry and the vfsmount. 3931d957f9bSJan Blunck */ 3941d957f9bSJan Blunck void path_put(struct path *path) 3951da177e4SLinus Torvalds { 3961d957f9bSJan Blunck dput(path->dentry); 3971d957f9bSJan Blunck mntput(path->mnt); 3981da177e4SLinus Torvalds } 3991d957f9bSJan Blunck EXPORT_SYMBOL(path_put); 4001da177e4SLinus Torvalds 40119660af7SAl Viro /* 40231e6b01fSNick Piggin * Path walking has 2 modes, rcu-walk and ref-walk (see 40319660af7SAl Viro * Documentation/filesystems/path-lookup.txt). In situations when we can't 40419660af7SAl Viro * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab 40519660af7SAl Viro * normal reference counts on dentries and vfsmounts to transition to rcu-walk 40619660af7SAl Viro * mode. Refcounts are grabbed at the last known good point before rcu-walk 40719660af7SAl Viro * got stuck, so ref-walk may continue from there. If this is not successful 40819660af7SAl Viro * (eg. a seqcount has changed), then failure is returned and it's up to caller 40919660af7SAl Viro * to restart the path walk from the beginning in ref-walk mode. 41031e6b01fSNick Piggin */ 41131e6b01fSNick Piggin 41231e6b01fSNick Piggin /** 41319660af7SAl Viro * unlazy_walk - try to switch to ref-walk mode. 41419660af7SAl Viro * @nd: nameidata pathwalk data 41519660af7SAl Viro * @dentry: child of nd->path.dentry or NULL 41639191628SRandy Dunlap * Returns: 0 on success, -ECHILD on failure 41731e6b01fSNick Piggin * 41819660af7SAl Viro * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry 41919660af7SAl Viro * for ref-walk mode. @dentry must be a path found by a do_lookup call on 42019660af7SAl Viro * @nd or NULL. Must be called from rcu-walk context. 42131e6b01fSNick Piggin */ 42219660af7SAl Viro static int unlazy_walk(struct nameidata *nd, struct dentry *dentry) 42331e6b01fSNick Piggin { 42431e6b01fSNick Piggin struct fs_struct *fs = current->fs; 42531e6b01fSNick Piggin struct dentry *parent = nd->path.dentry; 4265b6ca027SAl Viro int want_root = 0; 42731e6b01fSNick Piggin 42831e6b01fSNick Piggin BUG_ON(!(nd->flags & LOOKUP_RCU)); 4295b6ca027SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) { 4305b6ca027SAl Viro want_root = 1; 43131e6b01fSNick Piggin spin_lock(&fs->lock); 43231e6b01fSNick Piggin if (nd->root.mnt != fs->root.mnt || 43331e6b01fSNick Piggin nd->root.dentry != fs->root.dentry) 43431e6b01fSNick Piggin goto err_root; 43531e6b01fSNick Piggin } 43631e6b01fSNick Piggin spin_lock(&parent->d_lock); 43719660af7SAl Viro if (!dentry) { 43819660af7SAl Viro if (!__d_rcu_to_refcount(parent, nd->seq)) 43919660af7SAl Viro goto err_parent; 44019660af7SAl Viro BUG_ON(nd->inode != parent->d_inode); 44119660af7SAl Viro } else { 44294c0d4ecSAl Viro if (dentry->d_parent != parent) 44394c0d4ecSAl Viro goto err_parent; 44431e6b01fSNick Piggin spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); 44531e6b01fSNick Piggin if (!__d_rcu_to_refcount(dentry, nd->seq)) 44619660af7SAl Viro goto err_child; 44731e6b01fSNick Piggin /* 44819660af7SAl Viro * If the sequence check on the child dentry passed, then 44919660af7SAl Viro * the child has not been removed from its parent. This 45019660af7SAl Viro * means the parent dentry must be valid and able to take 45119660af7SAl Viro * a reference at this point. 45231e6b01fSNick Piggin */ 45331e6b01fSNick Piggin BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent); 45431e6b01fSNick Piggin BUG_ON(!parent->d_count); 45531e6b01fSNick Piggin parent->d_count++; 45631e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 45719660af7SAl Viro } 45831e6b01fSNick Piggin spin_unlock(&parent->d_lock); 4595b6ca027SAl Viro if (want_root) { 46031e6b01fSNick Piggin path_get(&nd->root); 46131e6b01fSNick Piggin spin_unlock(&fs->lock); 46231e6b01fSNick Piggin } 46331e6b01fSNick Piggin mntget(nd->path.mnt); 46431e6b01fSNick Piggin 46531e6b01fSNick Piggin rcu_read_unlock(); 46631e6b01fSNick Piggin br_read_unlock(vfsmount_lock); 46731e6b01fSNick Piggin nd->flags &= ~LOOKUP_RCU; 46831e6b01fSNick Piggin return 0; 46919660af7SAl Viro 47019660af7SAl Viro err_child: 47131e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 47219660af7SAl Viro err_parent: 47331e6b01fSNick Piggin spin_unlock(&parent->d_lock); 47431e6b01fSNick Piggin err_root: 4755b6ca027SAl Viro if (want_root) 47631e6b01fSNick Piggin spin_unlock(&fs->lock); 47731e6b01fSNick Piggin return -ECHILD; 47831e6b01fSNick Piggin } 47931e6b01fSNick Piggin 48031e6b01fSNick Piggin /** 481834f2a4aSTrond Myklebust * release_open_intent - free up open intent resources 482834f2a4aSTrond Myklebust * @nd: pointer to nameidata 483834f2a4aSTrond Myklebust */ 484834f2a4aSTrond Myklebust void release_open_intent(struct nameidata *nd) 485834f2a4aSTrond Myklebust { 4862dab5974SLinus Torvalds struct file *file = nd->intent.open.file; 4872dab5974SLinus Torvalds 4882dab5974SLinus Torvalds if (file && !IS_ERR(file)) { 4892dab5974SLinus Torvalds if (file->f_path.dentry == NULL) 4902dab5974SLinus Torvalds put_filp(file); 491834f2a4aSTrond Myklebust else 4922dab5974SLinus Torvalds fput(file); 4932dab5974SLinus Torvalds } 494834f2a4aSTrond Myklebust } 495834f2a4aSTrond Myklebust 496f60aef7eSAl Viro static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd) 49734286d66SNick Piggin { 498f60aef7eSAl Viro return dentry->d_op->d_revalidate(dentry, nd); 49934286d66SNick Piggin } 50034286d66SNick Piggin 5019f1fafeeSAl Viro /** 5029f1fafeeSAl Viro * complete_walk - successful completion of path walk 5039f1fafeeSAl Viro * @nd: pointer nameidata 50439159de2SJeff Layton * 5059f1fafeeSAl Viro * If we had been in RCU mode, drop out of it and legitimize nd->path. 5069f1fafeeSAl Viro * Revalidate the final result, unless we'd already done that during 5079f1fafeeSAl Viro * the path walk or the filesystem doesn't ask for it. Return 0 on 5089f1fafeeSAl Viro * success, -error on failure. In case of failure caller does not 5099f1fafeeSAl Viro * need to drop nd->path. 51039159de2SJeff Layton */ 5119f1fafeeSAl Viro static int complete_walk(struct nameidata *nd) 51239159de2SJeff Layton { 51316c2cd71SAl Viro struct dentry *dentry = nd->path.dentry; 51439159de2SJeff Layton int status; 51539159de2SJeff Layton 5169f1fafeeSAl Viro if (nd->flags & LOOKUP_RCU) { 5179f1fafeeSAl Viro nd->flags &= ~LOOKUP_RCU; 5189f1fafeeSAl Viro if (!(nd->flags & LOOKUP_ROOT)) 5199f1fafeeSAl Viro nd->root.mnt = NULL; 5209f1fafeeSAl Viro spin_lock(&dentry->d_lock); 5219f1fafeeSAl Viro if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) { 5229f1fafeeSAl Viro spin_unlock(&dentry->d_lock); 5239f1fafeeSAl Viro rcu_read_unlock(); 5249f1fafeeSAl Viro br_read_unlock(vfsmount_lock); 5259f1fafeeSAl Viro return -ECHILD; 5269f1fafeeSAl Viro } 5279f1fafeeSAl Viro BUG_ON(nd->inode != dentry->d_inode); 5289f1fafeeSAl Viro spin_unlock(&dentry->d_lock); 5299f1fafeeSAl Viro mntget(nd->path.mnt); 5309f1fafeeSAl Viro rcu_read_unlock(); 5319f1fafeeSAl Viro br_read_unlock(vfsmount_lock); 5329f1fafeeSAl Viro } 5339f1fafeeSAl Viro 53416c2cd71SAl Viro if (likely(!(nd->flags & LOOKUP_JUMPED))) 53539159de2SJeff Layton return 0; 53639159de2SJeff Layton 53716c2cd71SAl Viro if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE))) 53816c2cd71SAl Viro return 0; 53916c2cd71SAl Viro 54016c2cd71SAl Viro if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT))) 54116c2cd71SAl Viro return 0; 54216c2cd71SAl Viro 54316c2cd71SAl Viro /* Note: we do not d_invalidate() */ 54434286d66SNick Piggin status = d_revalidate(dentry, nd); 54539159de2SJeff Layton if (status > 0) 54639159de2SJeff Layton return 0; 54739159de2SJeff Layton 54816c2cd71SAl Viro if (!status) 54939159de2SJeff Layton status = -ESTALE; 55016c2cd71SAl Viro 5519f1fafeeSAl Viro path_put(&nd->path); 55239159de2SJeff Layton return status; 55339159de2SJeff Layton } 55439159de2SJeff Layton 5552a737871SAl Viro static __always_inline void set_root(struct nameidata *nd) 5562a737871SAl Viro { 557f7ad3c6bSMiklos Szeredi if (!nd->root.mnt) 558f7ad3c6bSMiklos Szeredi get_fs_root(current->fs, &nd->root); 5592a737871SAl Viro } 5602a737871SAl Viro 5616de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *); 5626de88d72SAl Viro 56331e6b01fSNick Piggin static __always_inline void set_root_rcu(struct nameidata *nd) 56431e6b01fSNick Piggin { 56531e6b01fSNick Piggin if (!nd->root.mnt) { 56631e6b01fSNick Piggin struct fs_struct *fs = current->fs; 567c28cc364SNick Piggin unsigned seq; 568c28cc364SNick Piggin 569c28cc364SNick Piggin do { 570c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 57131e6b01fSNick Piggin nd->root = fs->root; 572c1530019STim Chen nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq); 573c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 57431e6b01fSNick Piggin } 57531e6b01fSNick Piggin } 57631e6b01fSNick Piggin 577f1662356SArjan van de Ven static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link) 5781da177e4SLinus Torvalds { 57931e6b01fSNick Piggin int ret; 58031e6b01fSNick Piggin 5811da177e4SLinus Torvalds if (IS_ERR(link)) 5821da177e4SLinus Torvalds goto fail; 5831da177e4SLinus Torvalds 5841da177e4SLinus Torvalds if (*link == '/') { 5852a737871SAl Viro set_root(nd); 5861d957f9bSJan Blunck path_put(&nd->path); 5872a737871SAl Viro nd->path = nd->root; 5882a737871SAl Viro path_get(&nd->root); 58916c2cd71SAl Viro nd->flags |= LOOKUP_JUMPED; 5901da177e4SLinus Torvalds } 59131e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 592b4091d5fSChristoph Hellwig 59331e6b01fSNick Piggin ret = link_path_walk(link, nd); 59431e6b01fSNick Piggin return ret; 5951da177e4SLinus Torvalds fail: 5961d957f9bSJan Blunck path_put(&nd->path); 5971da177e4SLinus Torvalds return PTR_ERR(link); 5981da177e4SLinus Torvalds } 5991da177e4SLinus Torvalds 6001d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd) 601051d3812SIan Kent { 602051d3812SIan Kent dput(path->dentry); 6034ac91378SJan Blunck if (path->mnt != nd->path.mnt) 604051d3812SIan Kent mntput(path->mnt); 605051d3812SIan Kent } 606051d3812SIan Kent 6077b9337aaSNick Piggin static inline void path_to_nameidata(const struct path *path, 6087b9337aaSNick Piggin struct nameidata *nd) 609051d3812SIan Kent { 61031e6b01fSNick Piggin if (!(nd->flags & LOOKUP_RCU)) { 6114ac91378SJan Blunck dput(nd->path.dentry); 61231e6b01fSNick Piggin if (nd->path.mnt != path->mnt) 6134ac91378SJan Blunck mntput(nd->path.mnt); 6149a229683SHuang Shijie } 61531e6b01fSNick Piggin nd->path.mnt = path->mnt; 6164ac91378SJan Blunck nd->path.dentry = path->dentry; 617051d3812SIan Kent } 618051d3812SIan Kent 619574197e0SAl Viro static inline void put_link(struct nameidata *nd, struct path *link, void *cookie) 620574197e0SAl Viro { 621574197e0SAl Viro struct inode *inode = link->dentry->d_inode; 622574197e0SAl Viro if (!IS_ERR(cookie) && inode->i_op->put_link) 623574197e0SAl Viro inode->i_op->put_link(link->dentry, nd, cookie); 624574197e0SAl Viro path_put(link); 625574197e0SAl Viro } 626574197e0SAl Viro 627def4af30SAl Viro static __always_inline int 628574197e0SAl Viro follow_link(struct path *link, struct nameidata *nd, void **p) 6291da177e4SLinus Torvalds { 6301da177e4SLinus Torvalds int error; 6317b9337aaSNick Piggin struct dentry *dentry = link->dentry; 6321da177e4SLinus Torvalds 633844a3917SAl Viro BUG_ON(nd->flags & LOOKUP_RCU); 634844a3917SAl Viro 6350e794589SAl Viro if (link->mnt == nd->path.mnt) 6360e794589SAl Viro mntget(link->mnt); 6370e794589SAl Viro 638574197e0SAl Viro if (unlikely(current->total_link_count >= 40)) { 639574197e0SAl Viro *p = ERR_PTR(-ELOOP); /* no ->put_link(), please */ 640574197e0SAl Viro path_put(&nd->path); 641574197e0SAl Viro return -ELOOP; 642574197e0SAl Viro } 643574197e0SAl Viro cond_resched(); 644574197e0SAl Viro current->total_link_count++; 645574197e0SAl Viro 6467b9337aaSNick Piggin touch_atime(link->mnt, dentry); 6471da177e4SLinus Torvalds nd_set_link(nd, NULL); 648cd4e91d3SAl Viro 64936f3b4f6SAl Viro error = security_inode_follow_link(link->dentry, nd); 65036f3b4f6SAl Viro if (error) { 65136f3b4f6SAl Viro *p = ERR_PTR(error); /* no ->put_link(), please */ 65236f3b4f6SAl Viro path_put(&nd->path); 65336f3b4f6SAl Viro return error; 65436f3b4f6SAl Viro } 65536f3b4f6SAl Viro 65686acdca1SAl Viro nd->last_type = LAST_BIND; 657def4af30SAl Viro *p = dentry->d_inode->i_op->follow_link(dentry, nd); 658def4af30SAl Viro error = PTR_ERR(*p); 659def4af30SAl Viro if (!IS_ERR(*p)) { 6601da177e4SLinus Torvalds char *s = nd_get_link(nd); 661cc314eefSLinus Torvalds error = 0; 6621da177e4SLinus Torvalds if (s) 6631da177e4SLinus Torvalds error = __vfs_follow_link(nd, s); 664bcda7652SAl Viro else if (nd->last_type == LAST_BIND) { 66516c2cd71SAl Viro nd->flags |= LOOKUP_JUMPED; 666b21041d0SAl Viro nd->inode = nd->path.dentry->d_inode; 667b21041d0SAl Viro if (nd->inode->i_op->follow_link) { 668bcda7652SAl Viro /* stepped on a _really_ weird one */ 669bcda7652SAl Viro path_put(&nd->path); 670bcda7652SAl Viro error = -ELOOP; 671bcda7652SAl Viro } 672bcda7652SAl Viro } 6731da177e4SLinus Torvalds } 6741da177e4SLinus Torvalds return error; 6751da177e4SLinus Torvalds } 6761da177e4SLinus Torvalds 67731e6b01fSNick Piggin static int follow_up_rcu(struct path *path) 67831e6b01fSNick Piggin { 67931e6b01fSNick Piggin struct vfsmount *parent; 68031e6b01fSNick Piggin struct dentry *mountpoint; 68131e6b01fSNick Piggin 68231e6b01fSNick Piggin parent = path->mnt->mnt_parent; 68331e6b01fSNick Piggin if (parent == path->mnt) 68431e6b01fSNick Piggin return 0; 68531e6b01fSNick Piggin mountpoint = path->mnt->mnt_mountpoint; 68631e6b01fSNick Piggin path->dentry = mountpoint; 68731e6b01fSNick Piggin path->mnt = parent; 68831e6b01fSNick Piggin return 1; 68931e6b01fSNick Piggin } 69031e6b01fSNick Piggin 691bab77ebfSAl Viro int follow_up(struct path *path) 6921da177e4SLinus Torvalds { 6931da177e4SLinus Torvalds struct vfsmount *parent; 6941da177e4SLinus Torvalds struct dentry *mountpoint; 69599b7db7bSNick Piggin 69699b7db7bSNick Piggin br_read_lock(vfsmount_lock); 697bab77ebfSAl Viro parent = path->mnt->mnt_parent; 698bab77ebfSAl Viro if (parent == path->mnt) { 69999b7db7bSNick Piggin br_read_unlock(vfsmount_lock); 7001da177e4SLinus Torvalds return 0; 7011da177e4SLinus Torvalds } 7021da177e4SLinus Torvalds mntget(parent); 703bab77ebfSAl Viro mountpoint = dget(path->mnt->mnt_mountpoint); 70499b7db7bSNick Piggin br_read_unlock(vfsmount_lock); 705bab77ebfSAl Viro dput(path->dentry); 706bab77ebfSAl Viro path->dentry = mountpoint; 707bab77ebfSAl Viro mntput(path->mnt); 708bab77ebfSAl Viro path->mnt = parent; 7091da177e4SLinus Torvalds return 1; 7101da177e4SLinus Torvalds } 7111da177e4SLinus Torvalds 712b5c84bf6SNick Piggin /* 7139875cf80SDavid Howells * Perform an automount 7149875cf80SDavid Howells * - return -EISDIR to tell follow_managed() to stop and return the path we 7159875cf80SDavid Howells * were called with. 7161da177e4SLinus Torvalds */ 7179875cf80SDavid Howells static int follow_automount(struct path *path, unsigned flags, 7189875cf80SDavid Howells bool *need_mntput) 71931e6b01fSNick Piggin { 7209875cf80SDavid Howells struct vfsmount *mnt; 721ea5b778aSDavid Howells int err; 7229875cf80SDavid Howells 7239875cf80SDavid Howells if (!path->dentry->d_op || !path->dentry->d_op->d_automount) 7249875cf80SDavid Howells return -EREMOTE; 7259875cf80SDavid Howells 7260ec26fd0SMiklos Szeredi /* We don't want to mount if someone's just doing a stat - 7270ec26fd0SMiklos Szeredi * unless they're stat'ing a directory and appended a '/' to 7280ec26fd0SMiklos Szeredi * the name. 7290ec26fd0SMiklos Szeredi * 7300ec26fd0SMiklos Szeredi * We do, however, want to mount if someone wants to open or 7315a30d8a2SDavid Howells * create a file of any type under the mountpoint, wants to 7320ec26fd0SMiklos Szeredi * traverse through the mountpoint or wants to open the 7330ec26fd0SMiklos Szeredi * mounted directory. Also, autofs may mark negative dentries 7340ec26fd0SMiklos Szeredi * as being automount points. These will need the attentions 7350ec26fd0SMiklos Szeredi * of the daemon to instantiate them before they can be used. 7365a30d8a2SDavid Howells */ 7375a30d8a2SDavid Howells if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY | 738d94c177bSLinus Torvalds LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) && 7395a30d8a2SDavid Howells path->dentry->d_inode) 7409875cf80SDavid Howells return -EISDIR; 7410ec26fd0SMiklos Szeredi 7429875cf80SDavid Howells current->total_link_count++; 7439875cf80SDavid Howells if (current->total_link_count >= 40) 7449875cf80SDavid Howells return -ELOOP; 7459875cf80SDavid Howells 7469875cf80SDavid Howells mnt = path->dentry->d_op->d_automount(path); 7479875cf80SDavid Howells if (IS_ERR(mnt)) { 7489875cf80SDavid Howells /* 7499875cf80SDavid Howells * The filesystem is allowed to return -EISDIR here to indicate 7509875cf80SDavid Howells * it doesn't want to automount. For instance, autofs would do 7519875cf80SDavid Howells * this so that its userspace daemon can mount on this dentry. 7529875cf80SDavid Howells * 7539875cf80SDavid Howells * However, we can only permit this if it's a terminal point in 7549875cf80SDavid Howells * the path being looked up; if it wasn't then the remainder of 7559875cf80SDavid Howells * the path is inaccessible and we should say so. 7569875cf80SDavid Howells */ 75749084c3bSAl Viro if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT)) 7589875cf80SDavid Howells return -EREMOTE; 7599875cf80SDavid Howells return PTR_ERR(mnt); 76031e6b01fSNick Piggin } 761ea5b778aSDavid Howells 7629875cf80SDavid Howells if (!mnt) /* mount collision */ 7639875cf80SDavid Howells return 0; 7649875cf80SDavid Howells 7658aef1884SAl Viro if (!*need_mntput) { 7668aef1884SAl Viro /* lock_mount() may release path->mnt on error */ 7678aef1884SAl Viro mntget(path->mnt); 7688aef1884SAl Viro *need_mntput = true; 7698aef1884SAl Viro } 77019a167afSAl Viro err = finish_automount(mnt, path); 771ea5b778aSDavid Howells 772ea5b778aSDavid Howells switch (err) { 773ea5b778aSDavid Howells case -EBUSY: 774ea5b778aSDavid Howells /* Someone else made a mount here whilst we were busy */ 77519a167afSAl Viro return 0; 776ea5b778aSDavid Howells case 0: 7778aef1884SAl Viro path_put(path); 7789875cf80SDavid Howells path->mnt = mnt; 7799875cf80SDavid Howells path->dentry = dget(mnt->mnt_root); 7809875cf80SDavid Howells return 0; 78119a167afSAl Viro default: 78219a167afSAl Viro return err; 7839875cf80SDavid Howells } 78419a167afSAl Viro 785ea5b778aSDavid Howells } 7869875cf80SDavid Howells 7879875cf80SDavid Howells /* 7889875cf80SDavid Howells * Handle a dentry that is managed in some way. 789cc53ce53SDavid Howells * - Flagged for transit management (autofs) 7909875cf80SDavid Howells * - Flagged as mountpoint 7919875cf80SDavid Howells * - Flagged as automount point 7929875cf80SDavid Howells * 7939875cf80SDavid Howells * This may only be called in refwalk mode. 7949875cf80SDavid Howells * 7959875cf80SDavid Howells * Serialization is taken care of in namespace.c 7969875cf80SDavid Howells */ 7979875cf80SDavid Howells static int follow_managed(struct path *path, unsigned flags) 7989875cf80SDavid Howells { 7998aef1884SAl Viro struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */ 8009875cf80SDavid Howells unsigned managed; 8019875cf80SDavid Howells bool need_mntput = false; 8028aef1884SAl Viro int ret = 0; 8039875cf80SDavid Howells 8049875cf80SDavid Howells /* Given that we're not holding a lock here, we retain the value in a 8059875cf80SDavid Howells * local variable for each dentry as we look at it so that we don't see 8069875cf80SDavid Howells * the components of that value change under us */ 8079875cf80SDavid Howells while (managed = ACCESS_ONCE(path->dentry->d_flags), 8089875cf80SDavid Howells managed &= DCACHE_MANAGED_DENTRY, 8099875cf80SDavid Howells unlikely(managed != 0)) { 810cc53ce53SDavid Howells /* Allow the filesystem to manage the transit without i_mutex 811cc53ce53SDavid Howells * being held. */ 812cc53ce53SDavid Howells if (managed & DCACHE_MANAGE_TRANSIT) { 813cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op); 814cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op->d_manage); 8151aed3e42SAl Viro ret = path->dentry->d_op->d_manage(path->dentry, false); 816cc53ce53SDavid Howells if (ret < 0) 8178aef1884SAl Viro break; 818cc53ce53SDavid Howells } 819cc53ce53SDavid Howells 8209875cf80SDavid Howells /* Transit to a mounted filesystem. */ 8219875cf80SDavid Howells if (managed & DCACHE_MOUNTED) { 8229875cf80SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 8239875cf80SDavid Howells if (mounted) { 8249875cf80SDavid Howells dput(path->dentry); 8259875cf80SDavid Howells if (need_mntput) 826463ffb2eSAl Viro mntput(path->mnt); 827463ffb2eSAl Viro path->mnt = mounted; 828463ffb2eSAl Viro path->dentry = dget(mounted->mnt_root); 8299875cf80SDavid Howells need_mntput = true; 8309875cf80SDavid Howells continue; 831463ffb2eSAl Viro } 832463ffb2eSAl Viro 8339875cf80SDavid Howells /* Something is mounted on this dentry in another 8349875cf80SDavid Howells * namespace and/or whatever was mounted there in this 8359875cf80SDavid Howells * namespace got unmounted before we managed to get the 8369875cf80SDavid Howells * vfsmount_lock */ 8371da177e4SLinus Torvalds } 8389875cf80SDavid Howells 8399875cf80SDavid Howells /* Handle an automount point */ 8409875cf80SDavid Howells if (managed & DCACHE_NEED_AUTOMOUNT) { 8419875cf80SDavid Howells ret = follow_automount(path, flags, &need_mntput); 8429875cf80SDavid Howells if (ret < 0) 8438aef1884SAl Viro break; 8449875cf80SDavid Howells continue; 8459875cf80SDavid Howells } 8469875cf80SDavid Howells 8479875cf80SDavid Howells /* We didn't change the current path point */ 8489875cf80SDavid Howells break; 8499875cf80SDavid Howells } 8508aef1884SAl Viro 8518aef1884SAl Viro if (need_mntput && path->mnt == mnt) 8528aef1884SAl Viro mntput(path->mnt); 8538aef1884SAl Viro if (ret == -EISDIR) 8548aef1884SAl Viro ret = 0; 8558aef1884SAl Viro return ret; 8561da177e4SLinus Torvalds } 8571da177e4SLinus Torvalds 858cc53ce53SDavid Howells int follow_down_one(struct path *path) 8591da177e4SLinus Torvalds { 8601da177e4SLinus Torvalds struct vfsmount *mounted; 8611da177e4SLinus Torvalds 8621c755af4SAl Viro mounted = lookup_mnt(path); 8631da177e4SLinus Torvalds if (mounted) { 8649393bd07SAl Viro dput(path->dentry); 8659393bd07SAl Viro mntput(path->mnt); 8669393bd07SAl Viro path->mnt = mounted; 8679393bd07SAl Viro path->dentry = dget(mounted->mnt_root); 8681da177e4SLinus Torvalds return 1; 8691da177e4SLinus Torvalds } 8701da177e4SLinus Torvalds return 0; 8711da177e4SLinus Torvalds } 8721da177e4SLinus Torvalds 87362a7375eSIan Kent static inline bool managed_dentry_might_block(struct dentry *dentry) 87462a7375eSIan Kent { 87562a7375eSIan Kent return (dentry->d_flags & DCACHE_MANAGE_TRANSIT && 87662a7375eSIan Kent dentry->d_op->d_manage(dentry, true) < 0); 87762a7375eSIan Kent } 87862a7375eSIan Kent 8799875cf80SDavid Howells /* 880287548e4SAl Viro * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if 881287548e4SAl Viro * we meet a managed dentry that would need blocking. 8829875cf80SDavid Howells */ 8839875cf80SDavid Howells static bool __follow_mount_rcu(struct nameidata *nd, struct path *path, 884287548e4SAl Viro struct inode **inode) 8859875cf80SDavid Howells { 88662a7375eSIan Kent for (;;) { 8879875cf80SDavid Howells struct vfsmount *mounted; 88862a7375eSIan Kent /* 88962a7375eSIan Kent * Don't forget we might have a non-mountpoint managed dentry 89062a7375eSIan Kent * that wants to block transit. 89162a7375eSIan Kent */ 892287548e4SAl Viro if (unlikely(managed_dentry_might_block(path->dentry))) 893ab90911fSDavid Howells return false; 89462a7375eSIan Kent 89562a7375eSIan Kent if (!d_mountpoint(path->dentry)) 89662a7375eSIan Kent break; 89762a7375eSIan Kent 8989875cf80SDavid Howells mounted = __lookup_mnt(path->mnt, path->dentry, 1); 8999875cf80SDavid Howells if (!mounted) 9009875cf80SDavid Howells break; 9019875cf80SDavid Howells path->mnt = mounted; 9029875cf80SDavid Howells path->dentry = mounted->mnt_root; 9039875cf80SDavid Howells nd->seq = read_seqcount_begin(&path->dentry->d_seq); 90459430262SLinus Torvalds /* 90559430262SLinus Torvalds * Update the inode too. We don't need to re-check the 90659430262SLinus Torvalds * dentry sequence number here after this d_inode read, 90759430262SLinus Torvalds * because a mount-point is always pinned. 90859430262SLinus Torvalds */ 90959430262SLinus Torvalds *inode = path->dentry->d_inode; 9109875cf80SDavid Howells } 9119875cf80SDavid Howells return true; 9129875cf80SDavid Howells } 9139875cf80SDavid Howells 914dea39376SAl Viro static void follow_mount_rcu(struct nameidata *nd) 915287548e4SAl Viro { 916dea39376SAl Viro while (d_mountpoint(nd->path.dentry)) { 917287548e4SAl Viro struct vfsmount *mounted; 918dea39376SAl Viro mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1); 919287548e4SAl Viro if (!mounted) 920287548e4SAl Viro break; 921dea39376SAl Viro nd->path.mnt = mounted; 922dea39376SAl Viro nd->path.dentry = mounted->mnt_root; 923dea39376SAl Viro nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); 924287548e4SAl Viro } 925287548e4SAl Viro } 926287548e4SAl Viro 92731e6b01fSNick Piggin static int follow_dotdot_rcu(struct nameidata *nd) 92831e6b01fSNick Piggin { 92931e6b01fSNick Piggin set_root_rcu(nd); 93031e6b01fSNick Piggin 93131e6b01fSNick Piggin while (1) { 93231e6b01fSNick Piggin if (nd->path.dentry == nd->root.dentry && 93331e6b01fSNick Piggin nd->path.mnt == nd->root.mnt) { 93431e6b01fSNick Piggin break; 93531e6b01fSNick Piggin } 93631e6b01fSNick Piggin if (nd->path.dentry != nd->path.mnt->mnt_root) { 93731e6b01fSNick Piggin struct dentry *old = nd->path.dentry; 93831e6b01fSNick Piggin struct dentry *parent = old->d_parent; 93931e6b01fSNick Piggin unsigned seq; 94031e6b01fSNick Piggin 94131e6b01fSNick Piggin seq = read_seqcount_begin(&parent->d_seq); 94231e6b01fSNick Piggin if (read_seqcount_retry(&old->d_seq, nd->seq)) 943ef7562d5SAl Viro goto failed; 94431e6b01fSNick Piggin nd->path.dentry = parent; 94531e6b01fSNick Piggin nd->seq = seq; 94631e6b01fSNick Piggin break; 94731e6b01fSNick Piggin } 94831e6b01fSNick Piggin if (!follow_up_rcu(&nd->path)) 94931e6b01fSNick Piggin break; 95031e6b01fSNick Piggin nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); 95131e6b01fSNick Piggin } 952dea39376SAl Viro follow_mount_rcu(nd); 953dea39376SAl Viro nd->inode = nd->path.dentry->d_inode; 95431e6b01fSNick Piggin return 0; 955ef7562d5SAl Viro 956ef7562d5SAl Viro failed: 957ef7562d5SAl Viro nd->flags &= ~LOOKUP_RCU; 9585b6ca027SAl Viro if (!(nd->flags & LOOKUP_ROOT)) 959ef7562d5SAl Viro nd->root.mnt = NULL; 960ef7562d5SAl Viro rcu_read_unlock(); 961ef7562d5SAl Viro br_read_unlock(vfsmount_lock); 962ef7562d5SAl Viro return -ECHILD; 96331e6b01fSNick Piggin } 96431e6b01fSNick Piggin 9659875cf80SDavid Howells /* 966cc53ce53SDavid Howells * Follow down to the covering mount currently visible to userspace. At each 967cc53ce53SDavid Howells * point, the filesystem owning that dentry may be queried as to whether the 968cc53ce53SDavid Howells * caller is permitted to proceed or not. 969cc53ce53SDavid Howells */ 9707cc90cc3SAl Viro int follow_down(struct path *path) 971cc53ce53SDavid Howells { 972cc53ce53SDavid Howells unsigned managed; 973cc53ce53SDavid Howells int ret; 974cc53ce53SDavid Howells 975cc53ce53SDavid Howells while (managed = ACCESS_ONCE(path->dentry->d_flags), 976cc53ce53SDavid Howells unlikely(managed & DCACHE_MANAGED_DENTRY)) { 977cc53ce53SDavid Howells /* Allow the filesystem to manage the transit without i_mutex 978cc53ce53SDavid Howells * being held. 979cc53ce53SDavid Howells * 980cc53ce53SDavid Howells * We indicate to the filesystem if someone is trying to mount 981cc53ce53SDavid Howells * something here. This gives autofs the chance to deny anyone 982cc53ce53SDavid Howells * other than its daemon the right to mount on its 983cc53ce53SDavid Howells * superstructure. 984cc53ce53SDavid Howells * 985cc53ce53SDavid Howells * The filesystem may sleep at this point. 986cc53ce53SDavid Howells */ 987cc53ce53SDavid Howells if (managed & DCACHE_MANAGE_TRANSIT) { 988cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op); 989cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op->d_manage); 990ab90911fSDavid Howells ret = path->dentry->d_op->d_manage( 9911aed3e42SAl Viro path->dentry, false); 992cc53ce53SDavid Howells if (ret < 0) 993cc53ce53SDavid Howells return ret == -EISDIR ? 0 : ret; 994cc53ce53SDavid Howells } 995cc53ce53SDavid Howells 996cc53ce53SDavid Howells /* Transit to a mounted filesystem. */ 997cc53ce53SDavid Howells if (managed & DCACHE_MOUNTED) { 998cc53ce53SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 999cc53ce53SDavid Howells if (!mounted) 1000cc53ce53SDavid Howells break; 1001cc53ce53SDavid Howells dput(path->dentry); 1002cc53ce53SDavid Howells mntput(path->mnt); 1003cc53ce53SDavid Howells path->mnt = mounted; 1004cc53ce53SDavid Howells path->dentry = dget(mounted->mnt_root); 1005cc53ce53SDavid Howells continue; 1006cc53ce53SDavid Howells } 1007cc53ce53SDavid Howells 1008cc53ce53SDavid Howells /* Don't handle automount points here */ 1009cc53ce53SDavid Howells break; 1010cc53ce53SDavid Howells } 1011cc53ce53SDavid Howells return 0; 1012cc53ce53SDavid Howells } 1013cc53ce53SDavid Howells 1014cc53ce53SDavid Howells /* 10159875cf80SDavid Howells * Skip to top of mountpoint pile in refwalk mode for follow_dotdot() 10169875cf80SDavid Howells */ 10179875cf80SDavid Howells static void follow_mount(struct path *path) 10189875cf80SDavid Howells { 10199875cf80SDavid Howells while (d_mountpoint(path->dentry)) { 10209875cf80SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 10219875cf80SDavid Howells if (!mounted) 10229875cf80SDavid Howells break; 10239875cf80SDavid Howells dput(path->dentry); 10249875cf80SDavid Howells mntput(path->mnt); 10259875cf80SDavid Howells path->mnt = mounted; 10269875cf80SDavid Howells path->dentry = dget(mounted->mnt_root); 10279875cf80SDavid Howells } 10289875cf80SDavid Howells } 10299875cf80SDavid Howells 103031e6b01fSNick Piggin static void follow_dotdot(struct nameidata *nd) 10311da177e4SLinus Torvalds { 10322a737871SAl Viro set_root(nd); 1033e518ddb7SAndreas Mohr 10341da177e4SLinus Torvalds while(1) { 10354ac91378SJan Blunck struct dentry *old = nd->path.dentry; 10361da177e4SLinus Torvalds 10372a737871SAl Viro if (nd->path.dentry == nd->root.dentry && 10382a737871SAl Viro nd->path.mnt == nd->root.mnt) { 10391da177e4SLinus Torvalds break; 10401da177e4SLinus Torvalds } 10414ac91378SJan Blunck if (nd->path.dentry != nd->path.mnt->mnt_root) { 10423088dd70SAl Viro /* rare case of legitimate dget_parent()... */ 10433088dd70SAl Viro nd->path.dentry = dget_parent(nd->path.dentry); 10441da177e4SLinus Torvalds dput(old); 10451da177e4SLinus Torvalds break; 10461da177e4SLinus Torvalds } 10473088dd70SAl Viro if (!follow_up(&nd->path)) 10481da177e4SLinus Torvalds break; 10491da177e4SLinus Torvalds } 105079ed0226SAl Viro follow_mount(&nd->path); 105131e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 10521da177e4SLinus Torvalds } 10531da177e4SLinus Torvalds 10541da177e4SLinus Torvalds /* 1055baa03890SNick Piggin * Allocate a dentry with name and parent, and perform a parent 1056baa03890SNick Piggin * directory ->lookup on it. Returns the new dentry, or ERR_PTR 1057baa03890SNick Piggin * on error. parent->d_inode->i_mutex must be held. d_lookup must 1058baa03890SNick Piggin * have verified that no child exists while under i_mutex. 1059baa03890SNick Piggin */ 1060baa03890SNick Piggin static struct dentry *d_alloc_and_lookup(struct dentry *parent, 1061baa03890SNick Piggin struct qstr *name, struct nameidata *nd) 1062baa03890SNick Piggin { 1063baa03890SNick Piggin struct inode *inode = parent->d_inode; 1064baa03890SNick Piggin struct dentry *dentry; 1065baa03890SNick Piggin struct dentry *old; 1066baa03890SNick Piggin 1067baa03890SNick Piggin /* Don't create child dentry for a dead directory. */ 1068baa03890SNick Piggin if (unlikely(IS_DEADDIR(inode))) 1069baa03890SNick Piggin return ERR_PTR(-ENOENT); 1070baa03890SNick Piggin 1071baa03890SNick Piggin dentry = d_alloc(parent, name); 1072baa03890SNick Piggin if (unlikely(!dentry)) 1073baa03890SNick Piggin return ERR_PTR(-ENOMEM); 1074baa03890SNick Piggin 1075baa03890SNick Piggin old = inode->i_op->lookup(inode, dentry, nd); 1076baa03890SNick Piggin if (unlikely(old)) { 1077baa03890SNick Piggin dput(dentry); 1078baa03890SNick Piggin dentry = old; 1079baa03890SNick Piggin } 1080baa03890SNick Piggin return dentry; 1081baa03890SNick Piggin } 1082baa03890SNick Piggin 1083baa03890SNick Piggin /* 108444396f4bSJosef Bacik * We already have a dentry, but require a lookup to be performed on the parent 108544396f4bSJosef Bacik * directory to fill in d_inode. Returns the new dentry, or ERR_PTR on error. 108644396f4bSJosef Bacik * parent->d_inode->i_mutex must be held. d_lookup must have verified that no 108744396f4bSJosef Bacik * child exists while under i_mutex. 108844396f4bSJosef Bacik */ 108944396f4bSJosef Bacik static struct dentry *d_inode_lookup(struct dentry *parent, struct dentry *dentry, 109044396f4bSJosef Bacik struct nameidata *nd) 109144396f4bSJosef Bacik { 109244396f4bSJosef Bacik struct inode *inode = parent->d_inode; 109344396f4bSJosef Bacik struct dentry *old; 109444396f4bSJosef Bacik 109544396f4bSJosef Bacik /* Don't create child dentry for a dead directory. */ 109644396f4bSJosef Bacik if (unlikely(IS_DEADDIR(inode))) 109744396f4bSJosef Bacik return ERR_PTR(-ENOENT); 109844396f4bSJosef Bacik 109944396f4bSJosef Bacik old = inode->i_op->lookup(inode, dentry, nd); 110044396f4bSJosef Bacik if (unlikely(old)) { 110144396f4bSJosef Bacik dput(dentry); 110244396f4bSJosef Bacik dentry = old; 110344396f4bSJosef Bacik } 110444396f4bSJosef Bacik return dentry; 110544396f4bSJosef Bacik } 110644396f4bSJosef Bacik 110744396f4bSJosef Bacik /* 11081da177e4SLinus Torvalds * It's more convoluted than I'd like it to be, but... it's still fairly 11091da177e4SLinus Torvalds * small and for now I'd prefer to have fast path as straight as possible. 11101da177e4SLinus Torvalds * It _is_ time-critical. 11111da177e4SLinus Torvalds */ 11121da177e4SLinus Torvalds static int do_lookup(struct nameidata *nd, struct qstr *name, 111331e6b01fSNick Piggin struct path *path, struct inode **inode) 11141da177e4SLinus Torvalds { 11154ac91378SJan Blunck struct vfsmount *mnt = nd->path.mnt; 111631e6b01fSNick Piggin struct dentry *dentry, *parent = nd->path.dentry; 11175a18fff2SAl Viro int need_reval = 1; 11185a18fff2SAl Viro int status = 1; 11199875cf80SDavid Howells int err; 11209875cf80SDavid Howells 11213cac260aSAl Viro /* 1122b04f784eSNick Piggin * Rename seqlock is not required here because in the off chance 1123b04f784eSNick Piggin * of a false negative due to a concurrent rename, we're going to 1124b04f784eSNick Piggin * do the non-racy lookup, below. 1125b04f784eSNick Piggin */ 112631e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) { 112731e6b01fSNick Piggin unsigned seq; 112831e6b01fSNick Piggin *inode = nd->inode; 112931e6b01fSNick Piggin dentry = __d_lookup_rcu(parent, name, &seq, inode); 11305a18fff2SAl Viro if (!dentry) 11315a18fff2SAl Viro goto unlazy; 11325a18fff2SAl Viro 113331e6b01fSNick Piggin /* Memory barrier in read_seqcount_begin of child is enough */ 113431e6b01fSNick Piggin if (__read_seqcount_retry(&parent->d_seq, nd->seq)) 113531e6b01fSNick Piggin return -ECHILD; 113631e6b01fSNick Piggin nd->seq = seq; 11375a18fff2SAl Viro 113824643087SAl Viro if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) { 11395a18fff2SAl Viro status = d_revalidate(dentry, nd); 11405a18fff2SAl Viro if (unlikely(status <= 0)) { 11415a18fff2SAl Viro if (status != -ECHILD) 11425a18fff2SAl Viro need_reval = 0; 11435a18fff2SAl Viro goto unlazy; 11445a18fff2SAl Viro } 114524643087SAl Viro } 114644396f4bSJosef Bacik if (unlikely(d_need_lookup(dentry))) 114744396f4bSJosef Bacik goto unlazy; 114831e6b01fSNick Piggin path->mnt = mnt; 114931e6b01fSNick Piggin path->dentry = dentry; 1150d6e9bd25SAl Viro if (unlikely(!__follow_mount_rcu(nd, path, inode))) 1151d6e9bd25SAl Viro goto unlazy; 1152d6e9bd25SAl Viro if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT)) 1153d6e9bd25SAl Viro goto unlazy; 11549875cf80SDavid Howells return 0; 11555a18fff2SAl Viro unlazy: 115619660af7SAl Viro if (unlazy_walk(nd, dentry)) 11575a18fff2SAl Viro return -ECHILD; 11585a18fff2SAl Viro } else { 115931e6b01fSNick Piggin dentry = __d_lookup(parent, name); 116024643087SAl Viro } 11615a18fff2SAl Viro 116244396f4bSJosef Bacik if (dentry && unlikely(d_need_lookup(dentry))) { 116344396f4bSJosef Bacik dput(dentry); 116444396f4bSJosef Bacik dentry = NULL; 116544396f4bSJosef Bacik } 11665a18fff2SAl Viro retry: 11675a18fff2SAl Viro if (unlikely(!dentry)) { 11685a18fff2SAl Viro struct inode *dir = parent->d_inode; 11695a18fff2SAl Viro BUG_ON(nd->inode != dir); 11705a18fff2SAl Viro 11715a18fff2SAl Viro mutex_lock(&dir->i_mutex); 11725a18fff2SAl Viro dentry = d_lookup(parent, name); 11735a18fff2SAl Viro if (likely(!dentry)) { 11745a18fff2SAl Viro dentry = d_alloc_and_lookup(parent, name, nd); 11755a18fff2SAl Viro if (IS_ERR(dentry)) { 11765a18fff2SAl Viro mutex_unlock(&dir->i_mutex); 11775a18fff2SAl Viro return PTR_ERR(dentry); 11785a18fff2SAl Viro } 11795a18fff2SAl Viro /* known good */ 11805a18fff2SAl Viro need_reval = 0; 11815a18fff2SAl Viro status = 1; 118244396f4bSJosef Bacik } else if (unlikely(d_need_lookup(dentry))) { 118344396f4bSJosef Bacik dentry = d_inode_lookup(parent, dentry, nd); 118444396f4bSJosef Bacik if (IS_ERR(dentry)) { 118544396f4bSJosef Bacik mutex_unlock(&dir->i_mutex); 118644396f4bSJosef Bacik return PTR_ERR(dentry); 118744396f4bSJosef Bacik } 118844396f4bSJosef Bacik /* known good */ 118944396f4bSJosef Bacik need_reval = 0; 119044396f4bSJosef Bacik status = 1; 11915a18fff2SAl Viro } 11925a18fff2SAl Viro mutex_unlock(&dir->i_mutex); 11935a18fff2SAl Viro } 11945a18fff2SAl Viro if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval) 11955a18fff2SAl Viro status = d_revalidate(dentry, nd); 11965a18fff2SAl Viro if (unlikely(status <= 0)) { 11975a18fff2SAl Viro if (status < 0) { 11985a18fff2SAl Viro dput(dentry); 11995a18fff2SAl Viro return status; 12005a18fff2SAl Viro } 12015a18fff2SAl Viro if (!d_invalidate(dentry)) { 12025a18fff2SAl Viro dput(dentry); 12035a18fff2SAl Viro dentry = NULL; 12045a18fff2SAl Viro need_reval = 1; 12055a18fff2SAl Viro goto retry; 12065a18fff2SAl Viro } 12075a18fff2SAl Viro } 12085a18fff2SAl Viro 12091da177e4SLinus Torvalds path->mnt = mnt; 12101da177e4SLinus Torvalds path->dentry = dentry; 12119875cf80SDavid Howells err = follow_managed(path, nd->flags); 121289312214SIan Kent if (unlikely(err < 0)) { 121389312214SIan Kent path_put_conditional(path, nd); 12149875cf80SDavid Howells return err; 121589312214SIan Kent } 121631e6b01fSNick Piggin *inode = path->dentry->d_inode; 12171da177e4SLinus Torvalds return 0; 12181da177e4SLinus Torvalds } 12191da177e4SLinus Torvalds 122052094c8aSAl Viro static inline int may_lookup(struct nameidata *nd) 122152094c8aSAl Viro { 122252094c8aSAl Viro if (nd->flags & LOOKUP_RCU) { 12234ad5abb3SAl Viro int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK); 122452094c8aSAl Viro if (err != -ECHILD) 122552094c8aSAl Viro return err; 122619660af7SAl Viro if (unlazy_walk(nd, NULL)) 122752094c8aSAl Viro return -ECHILD; 122852094c8aSAl Viro } 12294ad5abb3SAl Viro return inode_permission(nd->inode, MAY_EXEC); 123052094c8aSAl Viro } 123152094c8aSAl Viro 12329856fa1bSAl Viro static inline int handle_dots(struct nameidata *nd, int type) 12339856fa1bSAl Viro { 12349856fa1bSAl Viro if (type == LAST_DOTDOT) { 12359856fa1bSAl Viro if (nd->flags & LOOKUP_RCU) { 12369856fa1bSAl Viro if (follow_dotdot_rcu(nd)) 12379856fa1bSAl Viro return -ECHILD; 12389856fa1bSAl Viro } else 12399856fa1bSAl Viro follow_dotdot(nd); 12409856fa1bSAl Viro } 12419856fa1bSAl Viro return 0; 12429856fa1bSAl Viro } 12439856fa1bSAl Viro 1244951361f9SAl Viro static void terminate_walk(struct nameidata *nd) 1245951361f9SAl Viro { 1246951361f9SAl Viro if (!(nd->flags & LOOKUP_RCU)) { 1247951361f9SAl Viro path_put(&nd->path); 1248951361f9SAl Viro } else { 1249951361f9SAl Viro nd->flags &= ~LOOKUP_RCU; 12505b6ca027SAl Viro if (!(nd->flags & LOOKUP_ROOT)) 1251951361f9SAl Viro nd->root.mnt = NULL; 1252951361f9SAl Viro rcu_read_unlock(); 1253951361f9SAl Viro br_read_unlock(vfsmount_lock); 1254951361f9SAl Viro } 1255951361f9SAl Viro } 1256951361f9SAl Viro 12573ddcd056SLinus Torvalds /* 12583ddcd056SLinus Torvalds * Do we need to follow links? We _really_ want to be able 12593ddcd056SLinus Torvalds * to do this check without having to look at inode->i_op, 12603ddcd056SLinus Torvalds * so we keep a cache of "no, this doesn't need follow_link" 12613ddcd056SLinus Torvalds * for the common case. 12623ddcd056SLinus Torvalds */ 12637813b94aSLinus Torvalds static inline int should_follow_link(struct inode *inode, int follow) 12643ddcd056SLinus Torvalds { 12653ddcd056SLinus Torvalds if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) { 12663ddcd056SLinus Torvalds if (likely(inode->i_op->follow_link)) 12673ddcd056SLinus Torvalds return follow; 12683ddcd056SLinus Torvalds 12693ddcd056SLinus Torvalds /* This gets set once for the inode lifetime */ 12703ddcd056SLinus Torvalds spin_lock(&inode->i_lock); 12713ddcd056SLinus Torvalds inode->i_opflags |= IOP_NOFOLLOW; 12723ddcd056SLinus Torvalds spin_unlock(&inode->i_lock); 12733ddcd056SLinus Torvalds } 12743ddcd056SLinus Torvalds return 0; 12753ddcd056SLinus Torvalds } 12763ddcd056SLinus Torvalds 1277ce57dfc1SAl Viro static inline int walk_component(struct nameidata *nd, struct path *path, 1278ce57dfc1SAl Viro struct qstr *name, int type, int follow) 1279ce57dfc1SAl Viro { 1280ce57dfc1SAl Viro struct inode *inode; 1281ce57dfc1SAl Viro int err; 1282ce57dfc1SAl Viro /* 1283ce57dfc1SAl Viro * "." and ".." are special - ".." especially so because it has 1284ce57dfc1SAl Viro * to be able to know about the current root directory and 1285ce57dfc1SAl Viro * parent relationships. 1286ce57dfc1SAl Viro */ 1287ce57dfc1SAl Viro if (unlikely(type != LAST_NORM)) 1288ce57dfc1SAl Viro return handle_dots(nd, type); 1289ce57dfc1SAl Viro err = do_lookup(nd, name, path, &inode); 1290ce57dfc1SAl Viro if (unlikely(err)) { 1291ce57dfc1SAl Viro terminate_walk(nd); 1292ce57dfc1SAl Viro return err; 1293ce57dfc1SAl Viro } 1294ce57dfc1SAl Viro if (!inode) { 1295ce57dfc1SAl Viro path_to_nameidata(path, nd); 1296ce57dfc1SAl Viro terminate_walk(nd); 1297ce57dfc1SAl Viro return -ENOENT; 1298ce57dfc1SAl Viro } 12997813b94aSLinus Torvalds if (should_follow_link(inode, follow)) { 130019660af7SAl Viro if (nd->flags & LOOKUP_RCU) { 130119660af7SAl Viro if (unlikely(unlazy_walk(nd, path->dentry))) { 130219660af7SAl Viro terminate_walk(nd); 1303ce57dfc1SAl Viro return -ECHILD; 130419660af7SAl Viro } 130519660af7SAl Viro } 1306ce57dfc1SAl Viro BUG_ON(inode != path->dentry->d_inode); 1307ce57dfc1SAl Viro return 1; 1308ce57dfc1SAl Viro } 1309ce57dfc1SAl Viro path_to_nameidata(path, nd); 1310ce57dfc1SAl Viro nd->inode = inode; 1311ce57dfc1SAl Viro return 0; 1312ce57dfc1SAl Viro } 1313ce57dfc1SAl Viro 13141da177e4SLinus Torvalds /* 1315b356379aSAl Viro * This limits recursive symlink follows to 8, while 1316b356379aSAl Viro * limiting consecutive symlinks to 40. 1317b356379aSAl Viro * 1318b356379aSAl Viro * Without that kind of total limit, nasty chains of consecutive 1319b356379aSAl Viro * symlinks can cause almost arbitrarily long lookups. 1320b356379aSAl Viro */ 1321b356379aSAl Viro static inline int nested_symlink(struct path *path, struct nameidata *nd) 1322b356379aSAl Viro { 1323b356379aSAl Viro int res; 1324b356379aSAl Viro 1325b356379aSAl Viro if (unlikely(current->link_count >= MAX_NESTED_LINKS)) { 1326b356379aSAl Viro path_put_conditional(path, nd); 1327b356379aSAl Viro path_put(&nd->path); 1328b356379aSAl Viro return -ELOOP; 1329b356379aSAl Viro } 13301a4022f8SErez Zadok BUG_ON(nd->depth >= MAX_NESTED_LINKS); 1331b356379aSAl Viro 1332b356379aSAl Viro nd->depth++; 1333b356379aSAl Viro current->link_count++; 1334b356379aSAl Viro 1335b356379aSAl Viro do { 1336b356379aSAl Viro struct path link = *path; 1337b356379aSAl Viro void *cookie; 1338574197e0SAl Viro 1339574197e0SAl Viro res = follow_link(&link, nd, &cookie); 1340b356379aSAl Viro if (!res) 1341b356379aSAl Viro res = walk_component(nd, path, &nd->last, 1342b356379aSAl Viro nd->last_type, LOOKUP_FOLLOW); 1343574197e0SAl Viro put_link(nd, &link, cookie); 1344b356379aSAl Viro } while (res > 0); 1345b356379aSAl Viro 1346b356379aSAl Viro current->link_count--; 1347b356379aSAl Viro nd->depth--; 1348b356379aSAl Viro return res; 1349b356379aSAl Viro } 1350b356379aSAl Viro 1351b356379aSAl Viro /* 13523ddcd056SLinus Torvalds * We really don't want to look at inode->i_op->lookup 13533ddcd056SLinus Torvalds * when we don't have to. So we keep a cache bit in 13543ddcd056SLinus Torvalds * the inode ->i_opflags field that says "yes, we can 13553ddcd056SLinus Torvalds * do lookup on this inode". 13563ddcd056SLinus Torvalds */ 13573ddcd056SLinus Torvalds static inline int can_lookup(struct inode *inode) 13583ddcd056SLinus Torvalds { 13593ddcd056SLinus Torvalds if (likely(inode->i_opflags & IOP_LOOKUP)) 13603ddcd056SLinus Torvalds return 1; 13613ddcd056SLinus Torvalds if (likely(!inode->i_op->lookup)) 13623ddcd056SLinus Torvalds return 0; 13633ddcd056SLinus Torvalds 13643ddcd056SLinus Torvalds /* We do this once for the lifetime of the inode */ 13653ddcd056SLinus Torvalds spin_lock(&inode->i_lock); 13663ddcd056SLinus Torvalds inode->i_opflags |= IOP_LOOKUP; 13673ddcd056SLinus Torvalds spin_unlock(&inode->i_lock); 13683ddcd056SLinus Torvalds return 1; 13693ddcd056SLinus Torvalds } 13703ddcd056SLinus Torvalds 13713ddcd056SLinus Torvalds /* 13721da177e4SLinus Torvalds * Name resolution. 1373ea3834d9SPrasanna Meda * This is the basic name resolution function, turning a pathname into 1374ea3834d9SPrasanna Meda * the final dentry. We expect 'base' to be positive and a directory. 13751da177e4SLinus Torvalds * 1376ea3834d9SPrasanna Meda * Returns 0 and nd will have valid dentry and mnt on success. 1377ea3834d9SPrasanna Meda * Returns error and drops reference to input namei data on failure. 13781da177e4SLinus Torvalds */ 13796de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd) 13801da177e4SLinus Torvalds { 13811da177e4SLinus Torvalds struct path next; 13821da177e4SLinus Torvalds int err; 13831da177e4SLinus Torvalds 13841da177e4SLinus Torvalds while (*name=='/') 13851da177e4SLinus Torvalds name++; 13861da177e4SLinus Torvalds if (!*name) 1387086e183aSAl Viro return 0; 13881da177e4SLinus Torvalds 13891da177e4SLinus Torvalds /* At this point we know we have a real path component. */ 13901da177e4SLinus Torvalds for(;;) { 13911da177e4SLinus Torvalds unsigned long hash; 13921da177e4SLinus Torvalds struct qstr this; 13931da177e4SLinus Torvalds unsigned int c; 1394fe479a58SAl Viro int type; 13951da177e4SLinus Torvalds 139652094c8aSAl Viro err = may_lookup(nd); 13971da177e4SLinus Torvalds if (err) 13981da177e4SLinus Torvalds break; 13991da177e4SLinus Torvalds 14001da177e4SLinus Torvalds this.name = name; 14011da177e4SLinus Torvalds c = *(const unsigned char *)name; 14021da177e4SLinus Torvalds 14031da177e4SLinus Torvalds hash = init_name_hash(); 14041da177e4SLinus Torvalds do { 14051da177e4SLinus Torvalds name++; 14061da177e4SLinus Torvalds hash = partial_name_hash(c, hash); 14071da177e4SLinus Torvalds c = *(const unsigned char *)name; 14081da177e4SLinus Torvalds } while (c && (c != '/')); 14091da177e4SLinus Torvalds this.len = name - (const char *) this.name; 14101da177e4SLinus Torvalds this.hash = end_name_hash(hash); 14111da177e4SLinus Torvalds 1412fe479a58SAl Viro type = LAST_NORM; 1413fe479a58SAl Viro if (this.name[0] == '.') switch (this.len) { 1414fe479a58SAl Viro case 2: 141516c2cd71SAl Viro if (this.name[1] == '.') { 1416fe479a58SAl Viro type = LAST_DOTDOT; 141716c2cd71SAl Viro nd->flags |= LOOKUP_JUMPED; 141816c2cd71SAl Viro } 1419fe479a58SAl Viro break; 1420fe479a58SAl Viro case 1: 1421fe479a58SAl Viro type = LAST_DOT; 1422fe479a58SAl Viro } 14235a202bcdSAl Viro if (likely(type == LAST_NORM)) { 14245a202bcdSAl Viro struct dentry *parent = nd->path.dentry; 142516c2cd71SAl Viro nd->flags &= ~LOOKUP_JUMPED; 14265a202bcdSAl Viro if (unlikely(parent->d_flags & DCACHE_OP_HASH)) { 14275a202bcdSAl Viro err = parent->d_op->d_hash(parent, nd->inode, 14285a202bcdSAl Viro &this); 14295a202bcdSAl Viro if (err < 0) 14305a202bcdSAl Viro break; 14315a202bcdSAl Viro } 14325a202bcdSAl Viro } 1433fe479a58SAl Viro 14341da177e4SLinus Torvalds /* remove trailing slashes? */ 14351da177e4SLinus Torvalds if (!c) 14361da177e4SLinus Torvalds goto last_component; 14371da177e4SLinus Torvalds while (*++name == '/'); 14381da177e4SLinus Torvalds if (!*name) 1439b356379aSAl Viro goto last_component; 14401da177e4SLinus Torvalds 1441ce57dfc1SAl Viro err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW); 1442ce57dfc1SAl Viro if (err < 0) 1443ce57dfc1SAl Viro return err; 1444fe479a58SAl Viro 1445ce57dfc1SAl Viro if (err) { 1446b356379aSAl Viro err = nested_symlink(&next, nd); 14471da177e4SLinus Torvalds if (err) 1448a7472babSAl Viro return err; 144931e6b01fSNick Piggin } 14503ddcd056SLinus Torvalds if (can_lookup(nd->inode)) 14511da177e4SLinus Torvalds continue; 14523ddcd056SLinus Torvalds err = -ENOTDIR; 14533ddcd056SLinus Torvalds break; 14541da177e4SLinus Torvalds /* here ends the main loop */ 14551da177e4SLinus Torvalds 14561da177e4SLinus Torvalds last_component: 1457ce57dfc1SAl Viro nd->last = this; 1458ce57dfc1SAl Viro nd->last_type = type; 1459ce57dfc1SAl Viro return 0; 1460ce57dfc1SAl Viro } 1461951361f9SAl Viro terminate_walk(nd); 14621da177e4SLinus Torvalds return err; 14631da177e4SLinus Torvalds } 14641da177e4SLinus Torvalds 146570e9b357SAl Viro static int path_init(int dfd, const char *name, unsigned int flags, 146670e9b357SAl Viro struct nameidata *nd, struct file **fp) 146731e6b01fSNick Piggin { 146831e6b01fSNick Piggin int retval = 0; 146931e6b01fSNick Piggin int fput_needed; 147031e6b01fSNick Piggin struct file *file; 147131e6b01fSNick Piggin 147231e6b01fSNick Piggin nd->last_type = LAST_ROOT; /* if there are only slashes... */ 147316c2cd71SAl Viro nd->flags = flags | LOOKUP_JUMPED; 147431e6b01fSNick Piggin nd->depth = 0; 14755b6ca027SAl Viro if (flags & LOOKUP_ROOT) { 14765b6ca027SAl Viro struct inode *inode = nd->root.dentry->d_inode; 147773d049a4SAl Viro if (*name) { 14785b6ca027SAl Viro if (!inode->i_op->lookup) 14795b6ca027SAl Viro return -ENOTDIR; 14805b6ca027SAl Viro retval = inode_permission(inode, MAY_EXEC); 14815b6ca027SAl Viro if (retval) 14825b6ca027SAl Viro return retval; 148373d049a4SAl Viro } 14845b6ca027SAl Viro nd->path = nd->root; 14855b6ca027SAl Viro nd->inode = inode; 14865b6ca027SAl Viro if (flags & LOOKUP_RCU) { 14875b6ca027SAl Viro br_read_lock(vfsmount_lock); 14885b6ca027SAl Viro rcu_read_lock(); 14895b6ca027SAl Viro nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 14905b6ca027SAl Viro } else { 14915b6ca027SAl Viro path_get(&nd->path); 14925b6ca027SAl Viro } 14935b6ca027SAl Viro return 0; 14945b6ca027SAl Viro } 14955b6ca027SAl Viro 149631e6b01fSNick Piggin nd->root.mnt = NULL; 149731e6b01fSNick Piggin 149831e6b01fSNick Piggin if (*name=='/') { 1499e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 150031e6b01fSNick Piggin br_read_lock(vfsmount_lock); 150131e6b01fSNick Piggin rcu_read_lock(); 1502e41f7d4eSAl Viro set_root_rcu(nd); 1503e41f7d4eSAl Viro } else { 1504e41f7d4eSAl Viro set_root(nd); 1505e41f7d4eSAl Viro path_get(&nd->root); 1506e41f7d4eSAl Viro } 150731e6b01fSNick Piggin nd->path = nd->root; 150831e6b01fSNick Piggin } else if (dfd == AT_FDCWD) { 1509e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 151031e6b01fSNick Piggin struct fs_struct *fs = current->fs; 1511c28cc364SNick Piggin unsigned seq; 151231e6b01fSNick Piggin 151331e6b01fSNick Piggin br_read_lock(vfsmount_lock); 151431e6b01fSNick Piggin rcu_read_lock(); 151531e6b01fSNick Piggin 1516c28cc364SNick Piggin do { 1517c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 151831e6b01fSNick Piggin nd->path = fs->pwd; 1519c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 1520c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 1521e41f7d4eSAl Viro } else { 1522e41f7d4eSAl Viro get_fs_pwd(current->fs, &nd->path); 1523e41f7d4eSAl Viro } 152431e6b01fSNick Piggin } else { 152531e6b01fSNick Piggin struct dentry *dentry; 152631e6b01fSNick Piggin 15271abf0c71SAl Viro file = fget_raw_light(dfd, &fput_needed); 152831e6b01fSNick Piggin retval = -EBADF; 152931e6b01fSNick Piggin if (!file) 153031e6b01fSNick Piggin goto out_fail; 153131e6b01fSNick Piggin 153231e6b01fSNick Piggin dentry = file->f_path.dentry; 153331e6b01fSNick Piggin 1534f52e0c11SAl Viro if (*name) { 153531e6b01fSNick Piggin retval = -ENOTDIR; 153631e6b01fSNick Piggin if (!S_ISDIR(dentry->d_inode->i_mode)) 153731e6b01fSNick Piggin goto fput_fail; 153831e6b01fSNick Piggin 15394ad5abb3SAl Viro retval = inode_permission(dentry->d_inode, MAY_EXEC); 154031e6b01fSNick Piggin if (retval) 154131e6b01fSNick Piggin goto fput_fail; 1542f52e0c11SAl Viro } 154331e6b01fSNick Piggin 154431e6b01fSNick Piggin nd->path = file->f_path; 1545e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 154631e6b01fSNick Piggin if (fput_needed) 154770e9b357SAl Viro *fp = file; 1548c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 154931e6b01fSNick Piggin br_read_lock(vfsmount_lock); 155031e6b01fSNick Piggin rcu_read_lock(); 15515590ff0dSUlrich Drepper } else { 15525dd784d0SJan Blunck path_get(&file->f_path); 15535590ff0dSUlrich Drepper fput_light(file, fput_needed); 15541da177e4SLinus Torvalds } 1555e41f7d4eSAl Viro } 1556e41f7d4eSAl Viro 155731e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 15589b4a9b14SAl Viro return 0; 15592dfdd266SJosef 'Jeff' Sipek 15609b4a9b14SAl Viro fput_fail: 15619b4a9b14SAl Viro fput_light(file, fput_needed); 15629b4a9b14SAl Viro out_fail: 15639b4a9b14SAl Viro return retval; 15649b4a9b14SAl Viro } 15659b4a9b14SAl Viro 1566bd92d7feSAl Viro static inline int lookup_last(struct nameidata *nd, struct path *path) 1567bd92d7feSAl Viro { 1568bd92d7feSAl Viro if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len]) 1569bd92d7feSAl Viro nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 1570bd92d7feSAl Viro 1571bd92d7feSAl Viro nd->flags &= ~LOOKUP_PARENT; 1572bd92d7feSAl Viro return walk_component(nd, path, &nd->last, nd->last_type, 1573bd92d7feSAl Viro nd->flags & LOOKUP_FOLLOW); 1574bd92d7feSAl Viro } 1575bd92d7feSAl Viro 15769b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */ 1577ee0827cdSAl Viro static int path_lookupat(int dfd, const char *name, 15789b4a9b14SAl Viro unsigned int flags, struct nameidata *nd) 15799b4a9b14SAl Viro { 158070e9b357SAl Viro struct file *base = NULL; 1581bd92d7feSAl Viro struct path path; 1582bd92d7feSAl Viro int err; 158331e6b01fSNick Piggin 158431e6b01fSNick Piggin /* 158531e6b01fSNick Piggin * Path walking is largely split up into 2 different synchronisation 158631e6b01fSNick Piggin * schemes, rcu-walk and ref-walk (explained in 158731e6b01fSNick Piggin * Documentation/filesystems/path-lookup.txt). These share much of the 158831e6b01fSNick Piggin * path walk code, but some things particularly setup, cleanup, and 158931e6b01fSNick Piggin * following mounts are sufficiently divergent that functions are 159031e6b01fSNick Piggin * duplicated. Typically there is a function foo(), and its RCU 159131e6b01fSNick Piggin * analogue, foo_rcu(). 159231e6b01fSNick Piggin * 159331e6b01fSNick Piggin * -ECHILD is the error number of choice (just to avoid clashes) that 159431e6b01fSNick Piggin * is returned if some aspect of an rcu-walk fails. Such an error must 159531e6b01fSNick Piggin * be handled by restarting a traditional ref-walk (which will always 159631e6b01fSNick Piggin * be able to complete). 159731e6b01fSNick Piggin */ 1598bd92d7feSAl Viro err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base); 1599ee0827cdSAl Viro 1600bd92d7feSAl Viro if (unlikely(err)) 1601bd92d7feSAl Viro return err; 1602ee0827cdSAl Viro 1603ee0827cdSAl Viro current->total_link_count = 0; 1604bd92d7feSAl Viro err = link_path_walk(name, nd); 1605bd92d7feSAl Viro 1606bd92d7feSAl Viro if (!err && !(flags & LOOKUP_PARENT)) { 1607bd92d7feSAl Viro err = lookup_last(nd, &path); 1608bd92d7feSAl Viro while (err > 0) { 1609bd92d7feSAl Viro void *cookie; 1610bd92d7feSAl Viro struct path link = path; 1611bd92d7feSAl Viro nd->flags |= LOOKUP_PARENT; 1612574197e0SAl Viro err = follow_link(&link, nd, &cookie); 1613bd92d7feSAl Viro if (!err) 1614bd92d7feSAl Viro err = lookup_last(nd, &path); 1615574197e0SAl Viro put_link(nd, &link, cookie); 1616bd92d7feSAl Viro } 1617bd92d7feSAl Viro } 1618ee0827cdSAl Viro 16199f1fafeeSAl Viro if (!err) 16209f1fafeeSAl Viro err = complete_walk(nd); 1621bd92d7feSAl Viro 1622bd92d7feSAl Viro if (!err && nd->flags & LOOKUP_DIRECTORY) { 1623bd92d7feSAl Viro if (!nd->inode->i_op->lookup) { 1624bd92d7feSAl Viro path_put(&nd->path); 1625bd23a539SAl Viro err = -ENOTDIR; 1626bd92d7feSAl Viro } 1627bd92d7feSAl Viro } 162816c2cd71SAl Viro 162970e9b357SAl Viro if (base) 163070e9b357SAl Viro fput(base); 1631ee0827cdSAl Viro 16325b6ca027SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) { 163331e6b01fSNick Piggin path_put(&nd->root); 163431e6b01fSNick Piggin nd->root.mnt = NULL; 163531e6b01fSNick Piggin } 1636bd92d7feSAl Viro return err; 163731e6b01fSNick Piggin } 163831e6b01fSNick Piggin 1639ee0827cdSAl Viro static int do_path_lookup(int dfd, const char *name, 1640ee0827cdSAl Viro unsigned int flags, struct nameidata *nd) 1641ee0827cdSAl Viro { 1642ee0827cdSAl Viro int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd); 1643ee0827cdSAl Viro if (unlikely(retval == -ECHILD)) 1644ee0827cdSAl Viro retval = path_lookupat(dfd, name, flags, nd); 1645ee0827cdSAl Viro if (unlikely(retval == -ESTALE)) 1646ee0827cdSAl Viro retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd); 1647ee0827cdSAl Viro 164831e6b01fSNick Piggin if (likely(!retval)) { 164931e6b01fSNick Piggin if (unlikely(!audit_dummy_context())) { 165031e6b01fSNick Piggin if (nd->path.dentry && nd->inode) 165131e6b01fSNick Piggin audit_inode(name, nd->path.dentry); 165231e6b01fSNick Piggin } 165331e6b01fSNick Piggin } 1654170aa3d0SUlrich Drepper return retval; 16551da177e4SLinus Torvalds } 16561da177e4SLinus Torvalds 1657c9c6cac0SAl Viro int kern_path_parent(const char *name, struct nameidata *nd) 16585590ff0dSUlrich Drepper { 1659c9c6cac0SAl Viro return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd); 16605590ff0dSUlrich Drepper } 16615590ff0dSUlrich Drepper 1662d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path) 1663d1811465SAl Viro { 1664d1811465SAl Viro struct nameidata nd; 1665d1811465SAl Viro int res = do_path_lookup(AT_FDCWD, name, flags, &nd); 1666d1811465SAl Viro if (!res) 1667d1811465SAl Viro *path = nd.path; 1668d1811465SAl Viro return res; 1669d1811465SAl Viro } 1670d1811465SAl Viro 167116f18200SJosef 'Jeff' Sipek /** 167216f18200SJosef 'Jeff' Sipek * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair 167316f18200SJosef 'Jeff' Sipek * @dentry: pointer to dentry of the base directory 167416f18200SJosef 'Jeff' Sipek * @mnt: pointer to vfs mount of the base directory 167516f18200SJosef 'Jeff' Sipek * @name: pointer to file name 167616f18200SJosef 'Jeff' Sipek * @flags: lookup flags 1677e0a01249SAl Viro * @path: pointer to struct path to fill 167816f18200SJosef 'Jeff' Sipek */ 167916f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt, 168016f18200SJosef 'Jeff' Sipek const char *name, unsigned int flags, 1681e0a01249SAl Viro struct path *path) 168216f18200SJosef 'Jeff' Sipek { 1683e0a01249SAl Viro struct nameidata nd; 1684e0a01249SAl Viro int err; 1685e0a01249SAl Viro nd.root.dentry = dentry; 1686e0a01249SAl Viro nd.root.mnt = mnt; 1687e0a01249SAl Viro BUG_ON(flags & LOOKUP_PARENT); 16885b6ca027SAl Viro /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */ 1689e0a01249SAl Viro err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd); 1690e0a01249SAl Viro if (!err) 1691e0a01249SAl Viro *path = nd.path; 1692e0a01249SAl Viro return err; 169316f18200SJosef 'Jeff' Sipek } 169416f18200SJosef 'Jeff' Sipek 1695eead1911SChristoph Hellwig static struct dentry *__lookup_hash(struct qstr *name, 1696eead1911SChristoph Hellwig struct dentry *base, struct nameidata *nd) 16971da177e4SLinus Torvalds { 169881fca444SChristoph Hellwig struct inode *inode = base->d_inode; 16991da177e4SLinus Torvalds struct dentry *dentry; 17001da177e4SLinus Torvalds int err; 17011da177e4SLinus Torvalds 17024ad5abb3SAl Viro err = inode_permission(inode, MAY_EXEC); 170381fca444SChristoph Hellwig if (err) 170481fca444SChristoph Hellwig return ERR_PTR(err); 17051da177e4SLinus Torvalds 17061da177e4SLinus Torvalds /* 1707b04f784eSNick Piggin * Don't bother with __d_lookup: callers are for creat as 1708b04f784eSNick Piggin * well as unlink, so a lot of the time it would cost 1709b04f784eSNick Piggin * a double lookup. 17106e6b1bd1SAl Viro */ 17116e6b1bd1SAl Viro dentry = d_lookup(base, name); 17126e6b1bd1SAl Viro 171344396f4bSJosef Bacik if (dentry && d_need_lookup(dentry)) { 171444396f4bSJosef Bacik /* 171544396f4bSJosef Bacik * __lookup_hash is called with the parent dir's i_mutex already 171644396f4bSJosef Bacik * held, so we are good to go here. 171744396f4bSJosef Bacik */ 171844396f4bSJosef Bacik dentry = d_inode_lookup(base, dentry, nd); 171944396f4bSJosef Bacik if (IS_ERR(dentry)) 172044396f4bSJosef Bacik return dentry; 172144396f4bSJosef Bacik } 172244396f4bSJosef Bacik 1723d2d9e9fbSAl Viro if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE)) { 1724d2d9e9fbSAl Viro int status = d_revalidate(dentry, nd); 1725d2d9e9fbSAl Viro if (unlikely(status <= 0)) { 1726d2d9e9fbSAl Viro /* 1727d2d9e9fbSAl Viro * The dentry failed validation. 1728d2d9e9fbSAl Viro * If d_revalidate returned 0 attempt to invalidate 1729d2d9e9fbSAl Viro * the dentry otherwise d_revalidate is asking us 1730d2d9e9fbSAl Viro * to return a fail status. 1731d2d9e9fbSAl Viro */ 1732d2d9e9fbSAl Viro if (status < 0) { 1733d2d9e9fbSAl Viro dput(dentry); 1734d2d9e9fbSAl Viro return ERR_PTR(status); 1735d2d9e9fbSAl Viro } else if (!d_invalidate(dentry)) { 1736d2d9e9fbSAl Viro dput(dentry); 1737d2d9e9fbSAl Viro dentry = NULL; 1738d2d9e9fbSAl Viro } 1739d2d9e9fbSAl Viro } 1740d2d9e9fbSAl Viro } 17416e6b1bd1SAl Viro 17421da177e4SLinus Torvalds if (!dentry) 1743baa03890SNick Piggin dentry = d_alloc_and_lookup(base, name, nd); 17445a202bcdSAl Viro 17451da177e4SLinus Torvalds return dentry; 17461da177e4SLinus Torvalds } 17471da177e4SLinus Torvalds 1748057f6c01SJames Morris /* 1749057f6c01SJames Morris * Restricted form of lookup. Doesn't follow links, single-component only, 1750057f6c01SJames Morris * needs parent already locked. Doesn't follow mounts. 1751057f6c01SJames Morris * SMP-safe. 1752057f6c01SJames Morris */ 1753a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd) 17541da177e4SLinus Torvalds { 17554ac91378SJan Blunck return __lookup_hash(&nd->last, nd->path.dentry, nd); 17561da177e4SLinus Torvalds } 17571da177e4SLinus Torvalds 1758eead1911SChristoph Hellwig /** 1759a6b91919SRandy Dunlap * lookup_one_len - filesystem helper to lookup single pathname component 1760eead1911SChristoph Hellwig * @name: pathname component to lookup 1761eead1911SChristoph Hellwig * @base: base directory to lookup from 1762eead1911SChristoph Hellwig * @len: maximum length @len should be interpreted to 1763eead1911SChristoph Hellwig * 1764a6b91919SRandy Dunlap * Note that this routine is purely a helper for filesystem usage and should 1765a6b91919SRandy Dunlap * not be called by generic code. Also note that by using this function the 1766eead1911SChristoph Hellwig * nameidata argument is passed to the filesystem methods and a filesystem 1767eead1911SChristoph Hellwig * using this helper needs to be prepared for that. 1768eead1911SChristoph Hellwig */ 1769057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len) 1770057f6c01SJames Morris { 1771057f6c01SJames Morris struct qstr this; 17726a96ba54SAl Viro unsigned long hash; 17736a96ba54SAl Viro unsigned int c; 1774057f6c01SJames Morris 17752f9092e1SDavid Woodhouse WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex)); 17762f9092e1SDavid Woodhouse 17776a96ba54SAl Viro this.name = name; 17786a96ba54SAl Viro this.len = len; 17796a96ba54SAl Viro if (!len) 17806a96ba54SAl Viro return ERR_PTR(-EACCES); 17816a96ba54SAl Viro 17826a96ba54SAl Viro hash = init_name_hash(); 17836a96ba54SAl Viro while (len--) { 17846a96ba54SAl Viro c = *(const unsigned char *)name++; 17856a96ba54SAl Viro if (c == '/' || c == '\0') 17866a96ba54SAl Viro return ERR_PTR(-EACCES); 17876a96ba54SAl Viro hash = partial_name_hash(c, hash); 17886a96ba54SAl Viro } 17896a96ba54SAl Viro this.hash = end_name_hash(hash); 17905a202bcdSAl Viro /* 17915a202bcdSAl Viro * See if the low-level filesystem might want 17925a202bcdSAl Viro * to use its own hash.. 17935a202bcdSAl Viro */ 17945a202bcdSAl Viro if (base->d_flags & DCACHE_OP_HASH) { 17955a202bcdSAl Viro int err = base->d_op->d_hash(base, base->d_inode, &this); 17965a202bcdSAl Viro if (err < 0) 17975a202bcdSAl Viro return ERR_PTR(err); 17985a202bcdSAl Viro } 1799eead1911SChristoph Hellwig 180049705b77SChristoph Hellwig return __lookup_hash(&this, base, NULL); 1801057f6c01SJames Morris } 1802057f6c01SJames Morris 18031fa1e7f6SAndy Whitcroft int user_path_at_empty(int dfd, const char __user *name, unsigned flags, 18041fa1e7f6SAndy Whitcroft struct path *path, int *empty) 18051da177e4SLinus Torvalds { 18062d8f3038SAl Viro struct nameidata nd; 18071fa1e7f6SAndy Whitcroft char *tmp = getname_flags(name, flags, empty); 18081da177e4SLinus Torvalds int err = PTR_ERR(tmp); 18091da177e4SLinus Torvalds if (!IS_ERR(tmp)) { 18102d8f3038SAl Viro 18112d8f3038SAl Viro BUG_ON(flags & LOOKUP_PARENT); 18122d8f3038SAl Viro 18132d8f3038SAl Viro err = do_path_lookup(dfd, tmp, flags, &nd); 18141da177e4SLinus Torvalds putname(tmp); 18152d8f3038SAl Viro if (!err) 18162d8f3038SAl Viro *path = nd.path; 18171da177e4SLinus Torvalds } 18181da177e4SLinus Torvalds return err; 18191da177e4SLinus Torvalds } 18201da177e4SLinus Torvalds 18211fa1e7f6SAndy Whitcroft int user_path_at(int dfd, const char __user *name, unsigned flags, 18221fa1e7f6SAndy Whitcroft struct path *path) 18231fa1e7f6SAndy Whitcroft { 18241fa1e7f6SAndy Whitcroft return user_path_at_empty(dfd, name, flags, path, 0); 18251fa1e7f6SAndy Whitcroft } 18261fa1e7f6SAndy Whitcroft 18272ad94ae6SAl Viro static int user_path_parent(int dfd, const char __user *path, 18282ad94ae6SAl Viro struct nameidata *nd, char **name) 18292ad94ae6SAl Viro { 18302ad94ae6SAl Viro char *s = getname(path); 18312ad94ae6SAl Viro int error; 18322ad94ae6SAl Viro 18332ad94ae6SAl Viro if (IS_ERR(s)) 18342ad94ae6SAl Viro return PTR_ERR(s); 18352ad94ae6SAl Viro 18362ad94ae6SAl Viro error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd); 18372ad94ae6SAl Viro if (error) 18382ad94ae6SAl Viro putname(s); 18392ad94ae6SAl Viro else 18402ad94ae6SAl Viro *name = s; 18412ad94ae6SAl Viro 18422ad94ae6SAl Viro return error; 18432ad94ae6SAl Viro } 18442ad94ae6SAl Viro 18451da177e4SLinus Torvalds /* 18461da177e4SLinus Torvalds * It's inline, so penalty for filesystems that don't use sticky bit is 18471da177e4SLinus Torvalds * minimal. 18481da177e4SLinus Torvalds */ 18491da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode) 18501da177e4SLinus Torvalds { 1851da9592edSDavid Howells uid_t fsuid = current_fsuid(); 1852da9592edSDavid Howells 18531da177e4SLinus Torvalds if (!(dir->i_mode & S_ISVTX)) 18541da177e4SLinus Torvalds return 0; 1855e795b717SSerge E. Hallyn if (current_user_ns() != inode_userns(inode)) 1856e795b717SSerge E. Hallyn goto other_userns; 1857da9592edSDavid Howells if (inode->i_uid == fsuid) 18581da177e4SLinus Torvalds return 0; 1859da9592edSDavid Howells if (dir->i_uid == fsuid) 18601da177e4SLinus Torvalds return 0; 1861e795b717SSerge E. Hallyn 1862e795b717SSerge E. Hallyn other_userns: 1863e795b717SSerge E. Hallyn return !ns_capable(inode_userns(inode), CAP_FOWNER); 18641da177e4SLinus Torvalds } 18651da177e4SLinus Torvalds 18661da177e4SLinus Torvalds /* 18671da177e4SLinus Torvalds * Check whether we can remove a link victim from directory dir, check 18681da177e4SLinus Torvalds * whether the type of victim is right. 18691da177e4SLinus Torvalds * 1. We can't do it if dir is read-only (done in permission()) 18701da177e4SLinus Torvalds * 2. We should have write and exec permissions on dir 18711da177e4SLinus Torvalds * 3. We can't remove anything from append-only dir 18721da177e4SLinus Torvalds * 4. We can't do anything with immutable dir (done in permission()) 18731da177e4SLinus Torvalds * 5. If the sticky bit on dir is set we should either 18741da177e4SLinus Torvalds * a. be owner of dir, or 18751da177e4SLinus Torvalds * b. be owner of victim, or 18761da177e4SLinus Torvalds * c. have CAP_FOWNER capability 18771da177e4SLinus Torvalds * 6. If the victim is append-only or immutable we can't do antyhing with 18781da177e4SLinus Torvalds * links pointing to it. 18791da177e4SLinus Torvalds * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. 18801da177e4SLinus Torvalds * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. 18811da177e4SLinus Torvalds * 9. We can't remove a root or mountpoint. 18821da177e4SLinus Torvalds * 10. We don't allow removal of NFS sillyrenamed files; it's handled by 18831da177e4SLinus Torvalds * nfs_async_unlink(). 18841da177e4SLinus Torvalds */ 1885858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir) 18861da177e4SLinus Torvalds { 18871da177e4SLinus Torvalds int error; 18881da177e4SLinus Torvalds 18891da177e4SLinus Torvalds if (!victim->d_inode) 18901da177e4SLinus Torvalds return -ENOENT; 18911da177e4SLinus Torvalds 18921da177e4SLinus Torvalds BUG_ON(victim->d_parent->d_inode != dir); 1893cccc6bbaSAl Viro audit_inode_child(victim, dir); 18941da177e4SLinus Torvalds 1895f419a2e3SAl Viro error = inode_permission(dir, MAY_WRITE | MAY_EXEC); 18961da177e4SLinus Torvalds if (error) 18971da177e4SLinus Torvalds return error; 18981da177e4SLinus Torvalds if (IS_APPEND(dir)) 18991da177e4SLinus Torvalds return -EPERM; 19001da177e4SLinus Torvalds if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)|| 1901f9454548SHugh Dickins IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode)) 19021da177e4SLinus Torvalds return -EPERM; 19031da177e4SLinus Torvalds if (isdir) { 19041da177e4SLinus Torvalds if (!S_ISDIR(victim->d_inode->i_mode)) 19051da177e4SLinus Torvalds return -ENOTDIR; 19061da177e4SLinus Torvalds if (IS_ROOT(victim)) 19071da177e4SLinus Torvalds return -EBUSY; 19081da177e4SLinus Torvalds } else if (S_ISDIR(victim->d_inode->i_mode)) 19091da177e4SLinus Torvalds return -EISDIR; 19101da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 19111da177e4SLinus Torvalds return -ENOENT; 19121da177e4SLinus Torvalds if (victim->d_flags & DCACHE_NFSFS_RENAMED) 19131da177e4SLinus Torvalds return -EBUSY; 19141da177e4SLinus Torvalds return 0; 19151da177e4SLinus Torvalds } 19161da177e4SLinus Torvalds 19171da177e4SLinus Torvalds /* Check whether we can create an object with dentry child in directory 19181da177e4SLinus Torvalds * dir. 19191da177e4SLinus Torvalds * 1. We can't do it if child already exists (open has special treatment for 19201da177e4SLinus Torvalds * this case, but since we are inlined it's OK) 19211da177e4SLinus Torvalds * 2. We can't do it if dir is read-only (done in permission()) 19221da177e4SLinus Torvalds * 3. We should have write and exec permissions on dir 19231da177e4SLinus Torvalds * 4. We can't do it if dir is immutable (done in permission()) 19241da177e4SLinus Torvalds */ 1925a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child) 19261da177e4SLinus Torvalds { 19271da177e4SLinus Torvalds if (child->d_inode) 19281da177e4SLinus Torvalds return -EEXIST; 19291da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 19301da177e4SLinus Torvalds return -ENOENT; 1931f419a2e3SAl Viro return inode_permission(dir, MAY_WRITE | MAY_EXEC); 19321da177e4SLinus Torvalds } 19331da177e4SLinus Torvalds 19341da177e4SLinus Torvalds /* 19351da177e4SLinus Torvalds * p1 and p2 should be directories on the same fs. 19361da177e4SLinus Torvalds */ 19371da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2) 19381da177e4SLinus Torvalds { 19391da177e4SLinus Torvalds struct dentry *p; 19401da177e4SLinus Torvalds 19411da177e4SLinus Torvalds if (p1 == p2) { 1942f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 19431da177e4SLinus Torvalds return NULL; 19441da177e4SLinus Torvalds } 19451da177e4SLinus Torvalds 1946a11f3a05SArjan van de Ven mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 19471da177e4SLinus Torvalds 1948e2761a11SOGAWA Hirofumi p = d_ancestor(p2, p1); 1949e2761a11SOGAWA Hirofumi if (p) { 1950f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT); 1951f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD); 19521da177e4SLinus Torvalds return p; 19531da177e4SLinus Torvalds } 19541da177e4SLinus Torvalds 1955e2761a11SOGAWA Hirofumi p = d_ancestor(p1, p2); 1956e2761a11SOGAWA Hirofumi if (p) { 1957f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 1958f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 19591da177e4SLinus Torvalds return p; 19601da177e4SLinus Torvalds } 19611da177e4SLinus Torvalds 1962f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 1963f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 19641da177e4SLinus Torvalds return NULL; 19651da177e4SLinus Torvalds } 19661da177e4SLinus Torvalds 19671da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2) 19681da177e4SLinus Torvalds { 19691b1dcc1bSJes Sorensen mutex_unlock(&p1->d_inode->i_mutex); 19701da177e4SLinus Torvalds if (p1 != p2) { 19711b1dcc1bSJes Sorensen mutex_unlock(&p2->d_inode->i_mutex); 1972a11f3a05SArjan van de Ven mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 19731da177e4SLinus Torvalds } 19741da177e4SLinus Torvalds } 19751da177e4SLinus Torvalds 19761da177e4SLinus Torvalds int vfs_create(struct inode *dir, struct dentry *dentry, int mode, 19771da177e4SLinus Torvalds struct nameidata *nd) 19781da177e4SLinus Torvalds { 1979a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 19801da177e4SLinus Torvalds 19811da177e4SLinus Torvalds if (error) 19821da177e4SLinus Torvalds return error; 19831da177e4SLinus Torvalds 1984acfa4380SAl Viro if (!dir->i_op->create) 19851da177e4SLinus Torvalds return -EACCES; /* shouldn't it be ENOSYS? */ 19861da177e4SLinus Torvalds mode &= S_IALLUGO; 19871da177e4SLinus Torvalds mode |= S_IFREG; 19881da177e4SLinus Torvalds error = security_inode_create(dir, dentry, mode); 19891da177e4SLinus Torvalds if (error) 19901da177e4SLinus Torvalds return error; 19911da177e4SLinus Torvalds error = dir->i_op->create(dir, dentry, mode, nd); 1992a74574aaSStephen Smalley if (!error) 1993f38aa942SAmy Griffis fsnotify_create(dir, dentry); 19941da177e4SLinus Torvalds return error; 19951da177e4SLinus Torvalds } 19961da177e4SLinus Torvalds 199773d049a4SAl Viro static int may_open(struct path *path, int acc_mode, int flag) 19981da177e4SLinus Torvalds { 19993fb64190SChristoph Hellwig struct dentry *dentry = path->dentry; 20001da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 20011da177e4SLinus Torvalds int error; 20021da177e4SLinus Torvalds 2003bcda7652SAl Viro /* O_PATH? */ 2004bcda7652SAl Viro if (!acc_mode) 2005bcda7652SAl Viro return 0; 2006bcda7652SAl Viro 20071da177e4SLinus Torvalds if (!inode) 20081da177e4SLinus Torvalds return -ENOENT; 20091da177e4SLinus Torvalds 2010c8fe8f30SChristoph Hellwig switch (inode->i_mode & S_IFMT) { 2011c8fe8f30SChristoph Hellwig case S_IFLNK: 20121da177e4SLinus Torvalds return -ELOOP; 2013c8fe8f30SChristoph Hellwig case S_IFDIR: 2014c8fe8f30SChristoph Hellwig if (acc_mode & MAY_WRITE) 20151da177e4SLinus Torvalds return -EISDIR; 2016c8fe8f30SChristoph Hellwig break; 2017c8fe8f30SChristoph Hellwig case S_IFBLK: 2018c8fe8f30SChristoph Hellwig case S_IFCHR: 20193fb64190SChristoph Hellwig if (path->mnt->mnt_flags & MNT_NODEV) 20201da177e4SLinus Torvalds return -EACCES; 2021c8fe8f30SChristoph Hellwig /*FALLTHRU*/ 2022c8fe8f30SChristoph Hellwig case S_IFIFO: 2023c8fe8f30SChristoph Hellwig case S_IFSOCK: 20241da177e4SLinus Torvalds flag &= ~O_TRUNC; 2025c8fe8f30SChristoph Hellwig break; 20264a3fd211SDave Hansen } 2027b41572e9SDave Hansen 20283fb64190SChristoph Hellwig error = inode_permission(inode, acc_mode); 2029b41572e9SDave Hansen if (error) 2030b41572e9SDave Hansen return error; 20316146f0d5SMimi Zohar 20321da177e4SLinus Torvalds /* 20331da177e4SLinus Torvalds * An append-only file must be opened in append mode for writing. 20341da177e4SLinus Torvalds */ 20351da177e4SLinus Torvalds if (IS_APPEND(inode)) { 20368737c930SAl Viro if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND)) 20377715b521SAl Viro return -EPERM; 20381da177e4SLinus Torvalds if (flag & O_TRUNC) 20397715b521SAl Viro return -EPERM; 20401da177e4SLinus Torvalds } 20411da177e4SLinus Torvalds 20421da177e4SLinus Torvalds /* O_NOATIME can only be set by the owner or superuser */ 20432e149670SSerge E. Hallyn if (flag & O_NOATIME && !inode_owner_or_capable(inode)) 20447715b521SAl Viro return -EPERM; 20451da177e4SLinus Torvalds 2046f3c7691eSJ. Bruce Fields return 0; 20477715b521SAl Viro } 20487715b521SAl Viro 2049e1181ee6SJeff Layton static int handle_truncate(struct file *filp) 20507715b521SAl Viro { 2051e1181ee6SJeff Layton struct path *path = &filp->f_path; 20527715b521SAl Viro struct inode *inode = path->dentry->d_inode; 20537715b521SAl Viro int error = get_write_access(inode); 20541da177e4SLinus Torvalds if (error) 20557715b521SAl Viro return error; 20561da177e4SLinus Torvalds /* 20571da177e4SLinus Torvalds * Refuse to truncate files with mandatory locks held on them. 20581da177e4SLinus Torvalds */ 20591da177e4SLinus Torvalds error = locks_verify_locked(inode); 2060be6d3e56SKentaro Takeda if (!error) 2061ea0d3ab2STetsuo Handa error = security_path_truncate(path); 20621da177e4SLinus Torvalds if (!error) { 20637715b521SAl Viro error = do_truncate(path->dentry, 0, 2064d139d7ffSMiklos Szeredi ATTR_MTIME|ATTR_CTIME|ATTR_OPEN, 2065e1181ee6SJeff Layton filp); 20661da177e4SLinus Torvalds } 20671da177e4SLinus Torvalds put_write_access(inode); 2068acd0c935SMimi Zohar return error; 20691da177e4SLinus Torvalds } 20701da177e4SLinus Torvalds 2071d57999e1SDave Hansen static inline int open_to_namei_flags(int flag) 2072d57999e1SDave Hansen { 20738a5e929dSAl Viro if ((flag & O_ACCMODE) == 3) 20748a5e929dSAl Viro flag--; 2075d57999e1SDave Hansen return flag; 2076d57999e1SDave Hansen } 2077d57999e1SDave Hansen 207831e6b01fSNick Piggin /* 2079fe2d35ffSAl Viro * Handle the last step of open() 208031e6b01fSNick Piggin */ 2081fb1cc555SAl Viro static struct file *do_last(struct nameidata *nd, struct path *path, 2082c3e380b0SAl Viro const struct open_flags *op, const char *pathname) 2083fb1cc555SAl Viro { 2084a1e28038SAl Viro struct dentry *dir = nd->path.dentry; 20856c0d46c4SAl Viro struct dentry *dentry; 2086ca344a89SAl Viro int open_flag = op->open_flag; 20876c0d46c4SAl Viro int will_truncate = open_flag & O_TRUNC; 2088ca344a89SAl Viro int want_write = 0; 2089bcda7652SAl Viro int acc_mode = op->acc_mode; 2090fb1cc555SAl Viro struct file *filp; 209116c2cd71SAl Viro int error; 2092fb1cc555SAl Viro 2093c3e380b0SAl Viro nd->flags &= ~LOOKUP_PARENT; 2094c3e380b0SAl Viro nd->flags |= op->intent; 2095c3e380b0SAl Viro 20961f36f774SAl Viro switch (nd->last_type) { 20971f36f774SAl Viro case LAST_DOTDOT: 2098176306f5SNeil Brown case LAST_DOT: 2099fe2d35ffSAl Viro error = handle_dots(nd, nd->last_type); 2100fe2d35ffSAl Viro if (error) 2101fe2d35ffSAl Viro return ERR_PTR(error); 21021f36f774SAl Viro /* fallthrough */ 21031f36f774SAl Viro case LAST_ROOT: 21049f1fafeeSAl Viro error = complete_walk(nd); 210516c2cd71SAl Viro if (error) 21069f1fafeeSAl Viro return ERR_PTR(error); 2107fe2d35ffSAl Viro audit_inode(pathname, nd->path.dentry); 2108ca344a89SAl Viro if (open_flag & O_CREAT) { 210916c2cd71SAl Viro error = -EISDIR; 21101f36f774SAl Viro goto exit; 2111fe2d35ffSAl Viro } 2112fe2d35ffSAl Viro goto ok; 21131f36f774SAl Viro case LAST_BIND: 21149f1fafeeSAl Viro error = complete_walk(nd); 211516c2cd71SAl Viro if (error) 21169f1fafeeSAl Viro return ERR_PTR(error); 21171f36f774SAl Viro audit_inode(pathname, dir); 21181f36f774SAl Viro goto ok; 21191f36f774SAl Viro } 2120a2c36b45SAl Viro 2121ca344a89SAl Viro if (!(open_flag & O_CREAT)) { 2122bcda7652SAl Viro int symlink_ok = 0; 2123fe2d35ffSAl Viro if (nd->last.name[nd->last.len]) 2124fe2d35ffSAl Viro nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 2125bcda7652SAl Viro if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW)) 2126bcda7652SAl Viro symlink_ok = 1; 2127fe2d35ffSAl Viro /* we _can_ be in RCU mode here */ 2128ce57dfc1SAl Viro error = walk_component(nd, path, &nd->last, LAST_NORM, 2129ce57dfc1SAl Viro !symlink_ok); 2130ce57dfc1SAl Viro if (error < 0) 2131fe2d35ffSAl Viro return ERR_PTR(error); 2132ce57dfc1SAl Viro if (error) /* symlink */ 2133fe2d35ffSAl Viro return NULL; 2134fe2d35ffSAl Viro /* sayonara */ 21359f1fafeeSAl Viro error = complete_walk(nd); 21369f1fafeeSAl Viro if (error) 2137fe2d35ffSAl Viro return ERR_PTR(-ECHILD); 2138fe2d35ffSAl Viro 2139fe2d35ffSAl Viro error = -ENOTDIR; 2140fe2d35ffSAl Viro if (nd->flags & LOOKUP_DIRECTORY) { 2141ce57dfc1SAl Viro if (!nd->inode->i_op->lookup) 2142fe2d35ffSAl Viro goto exit; 2143fe2d35ffSAl Viro } 2144fe2d35ffSAl Viro audit_inode(pathname, nd->path.dentry); 2145fe2d35ffSAl Viro goto ok; 2146fe2d35ffSAl Viro } 2147fe2d35ffSAl Viro 2148fe2d35ffSAl Viro /* create side of things */ 21499f1fafeeSAl Viro error = complete_walk(nd); 21509f1fafeeSAl Viro if (error) 21519f1fafeeSAl Viro return ERR_PTR(error); 2152fe2d35ffSAl Viro 2153fe2d35ffSAl Viro audit_inode(pathname, dir); 215416c2cd71SAl Viro error = -EISDIR; 21551f36f774SAl Viro /* trailing slashes? */ 215631e6b01fSNick Piggin if (nd->last.name[nd->last.len]) 21571f36f774SAl Viro goto exit; 21581f36f774SAl Viro 2159a1e28038SAl Viro mutex_lock(&dir->d_inode->i_mutex); 2160a1e28038SAl Viro 21616c0d46c4SAl Viro dentry = lookup_hash(nd); 21626c0d46c4SAl Viro error = PTR_ERR(dentry); 21636c0d46c4SAl Viro if (IS_ERR(dentry)) { 2164fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2165fb1cc555SAl Viro goto exit; 2166fb1cc555SAl Viro } 2167fb1cc555SAl Viro 21686c0d46c4SAl Viro path->dentry = dentry; 21696c0d46c4SAl Viro path->mnt = nd->path.mnt; 21706c0d46c4SAl Viro 2171fb1cc555SAl Viro /* Negative dentry, just create the file */ 21726c0d46c4SAl Viro if (!dentry->d_inode) { 21736c0d46c4SAl Viro int mode = op->mode; 21746c0d46c4SAl Viro if (!IS_POSIXACL(dir->d_inode)) 21756c0d46c4SAl Viro mode &= ~current_umask(); 2176fb1cc555SAl Viro /* 2177fb1cc555SAl Viro * This write is needed to ensure that a 21786c0d46c4SAl Viro * rw->ro transition does not occur between 2179fb1cc555SAl Viro * the time when the file is created and when 2180fb1cc555SAl Viro * a permanent write count is taken through 2181fb1cc555SAl Viro * the 'struct file' in nameidata_to_filp(). 2182fb1cc555SAl Viro */ 2183fb1cc555SAl Viro error = mnt_want_write(nd->path.mnt); 2184fb1cc555SAl Viro if (error) 2185fb1cc555SAl Viro goto exit_mutex_unlock; 2186ca344a89SAl Viro want_write = 1; 21879b44f1b3SAl Viro /* Don't check for write permission, don't truncate */ 2188ca344a89SAl Viro open_flag &= ~O_TRUNC; 21896c0d46c4SAl Viro will_truncate = 0; 2190bcda7652SAl Viro acc_mode = MAY_OPEN; 21916c0d46c4SAl Viro error = security_path_mknod(&nd->path, dentry, mode, 0); 21926c0d46c4SAl Viro if (error) 21936c0d46c4SAl Viro goto exit_mutex_unlock; 21946c0d46c4SAl Viro error = vfs_create(dir->d_inode, dentry, mode, nd); 21956c0d46c4SAl Viro if (error) 21966c0d46c4SAl Viro goto exit_mutex_unlock; 21976c0d46c4SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 21986c0d46c4SAl Viro dput(nd->path.dentry); 21996c0d46c4SAl Viro nd->path.dentry = dentry; 2200ca344a89SAl Viro goto common; 2201fb1cc555SAl Viro } 2202fb1cc555SAl Viro 2203fb1cc555SAl Viro /* 2204fb1cc555SAl Viro * It already exists. 2205fb1cc555SAl Viro */ 2206fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2207fb1cc555SAl Viro audit_inode(pathname, path->dentry); 2208fb1cc555SAl Viro 2209fb1cc555SAl Viro error = -EEXIST; 2210ca344a89SAl Viro if (open_flag & O_EXCL) 2211fb1cc555SAl Viro goto exit_dput; 2212fb1cc555SAl Viro 22139875cf80SDavid Howells error = follow_managed(path, nd->flags); 22149875cf80SDavid Howells if (error < 0) 2215fb1cc555SAl Viro goto exit_dput; 2216fb1cc555SAl Viro 2217fb1cc555SAl Viro error = -ENOENT; 2218fb1cc555SAl Viro if (!path->dentry->d_inode) 2219fb1cc555SAl Viro goto exit_dput; 22209e67f361SAl Viro 22219e67f361SAl Viro if (path->dentry->d_inode->i_op->follow_link) 2222fb1cc555SAl Viro return NULL; 2223fb1cc555SAl Viro 2224fb1cc555SAl Viro path_to_nameidata(path, nd); 222531e6b01fSNick Piggin nd->inode = path->dentry->d_inode; 2226fb1cc555SAl Viro error = -EISDIR; 222731e6b01fSNick Piggin if (S_ISDIR(nd->inode->i_mode)) 2228fb1cc555SAl Viro goto exit; 222967ee3ad2SAl Viro ok: 22306c0d46c4SAl Viro if (!S_ISREG(nd->inode->i_mode)) 22316c0d46c4SAl Viro will_truncate = 0; 22326c0d46c4SAl Viro 22330f9d1a10SAl Viro if (will_truncate) { 22340f9d1a10SAl Viro error = mnt_want_write(nd->path.mnt); 22350f9d1a10SAl Viro if (error) 22360f9d1a10SAl Viro goto exit; 2237ca344a89SAl Viro want_write = 1; 22380f9d1a10SAl Viro } 2239ca344a89SAl Viro common: 2240bcda7652SAl Viro error = may_open(&nd->path, acc_mode, open_flag); 2241ca344a89SAl Viro if (error) 22420f9d1a10SAl Viro goto exit; 22430f9d1a10SAl Viro filp = nameidata_to_filp(nd); 22440f9d1a10SAl Viro if (!IS_ERR(filp)) { 22450f9d1a10SAl Viro error = ima_file_check(filp, op->acc_mode); 22460f9d1a10SAl Viro if (error) { 22470f9d1a10SAl Viro fput(filp); 22480f9d1a10SAl Viro filp = ERR_PTR(error); 22490f9d1a10SAl Viro } 22500f9d1a10SAl Viro } 22510f9d1a10SAl Viro if (!IS_ERR(filp)) { 22520f9d1a10SAl Viro if (will_truncate) { 22530f9d1a10SAl Viro error = handle_truncate(filp); 22540f9d1a10SAl Viro if (error) { 22550f9d1a10SAl Viro fput(filp); 22560f9d1a10SAl Viro filp = ERR_PTR(error); 22570f9d1a10SAl Viro } 22580f9d1a10SAl Viro } 22590f9d1a10SAl Viro } 2260ca344a89SAl Viro out: 2261ca344a89SAl Viro if (want_write) 22620f9d1a10SAl Viro mnt_drop_write(nd->path.mnt); 22630f9d1a10SAl Viro path_put(&nd->path); 2264fb1cc555SAl Viro return filp; 2265fb1cc555SAl Viro 2266fb1cc555SAl Viro exit_mutex_unlock: 2267fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2268fb1cc555SAl Viro exit_dput: 2269fb1cc555SAl Viro path_put_conditional(path, nd); 2270fb1cc555SAl Viro exit: 2271ca344a89SAl Viro filp = ERR_PTR(error); 2272ca344a89SAl Viro goto out; 2273fb1cc555SAl Viro } 2274fb1cc555SAl Viro 227513aab428SAl Viro static struct file *path_openat(int dfd, const char *pathname, 227673d049a4SAl Viro struct nameidata *nd, const struct open_flags *op, int flags) 22771da177e4SLinus Torvalds { 2278fe2d35ffSAl Viro struct file *base = NULL; 22794a3fd211SDave Hansen struct file *filp; 22809850c056SAl Viro struct path path; 228113aab428SAl Viro int error; 228231e6b01fSNick Piggin 228331e6b01fSNick Piggin filp = get_empty_filp(); 228431e6b01fSNick Piggin if (!filp) 228531e6b01fSNick Piggin return ERR_PTR(-ENFILE); 228631e6b01fSNick Piggin 228747c805dcSAl Viro filp->f_flags = op->open_flag; 228873d049a4SAl Viro nd->intent.open.file = filp; 228973d049a4SAl Viro nd->intent.open.flags = open_to_namei_flags(op->open_flag); 229073d049a4SAl Viro nd->intent.open.create_mode = op->mode; 229131e6b01fSNick Piggin 229273d049a4SAl Viro error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base); 229331e6b01fSNick Piggin if (unlikely(error)) 229413aab428SAl Viro goto out_filp; 229531e6b01fSNick Piggin 2296fe2d35ffSAl Viro current->total_link_count = 0; 229773d049a4SAl Viro error = link_path_walk(pathname, nd); 229831e6b01fSNick Piggin if (unlikely(error)) 229931e6b01fSNick Piggin goto out_filp; 23001da177e4SLinus Torvalds 230173d049a4SAl Viro filp = do_last(nd, &path, op, pathname); 2302806b681cSAl Viro while (unlikely(!filp)) { /* trailing symlink */ 23037b9337aaSNick Piggin struct path link = path; 2304def4af30SAl Viro void *cookie; 2305574197e0SAl Viro if (!(nd->flags & LOOKUP_FOLLOW)) { 230673d049a4SAl Viro path_put_conditional(&path, nd); 230773d049a4SAl Viro path_put(&nd->path); 230840b39136SAl Viro filp = ERR_PTR(-ELOOP); 230940b39136SAl Viro break; 231040b39136SAl Viro } 231173d049a4SAl Viro nd->flags |= LOOKUP_PARENT; 231273d049a4SAl Viro nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL); 2313574197e0SAl Viro error = follow_link(&link, nd, &cookie); 2314c3e380b0SAl Viro if (unlikely(error)) 2315f1afe9efSAl Viro filp = ERR_PTR(error); 2316c3e380b0SAl Viro else 231773d049a4SAl Viro filp = do_last(nd, &path, op, pathname); 2318574197e0SAl Viro put_link(nd, &link, cookie); 2319806b681cSAl Viro } 232010fa8e62SAl Viro out: 232173d049a4SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) 232273d049a4SAl Viro path_put(&nd->root); 2323fe2d35ffSAl Viro if (base) 2324fe2d35ffSAl Viro fput(base); 232573d049a4SAl Viro release_open_intent(nd); 232610fa8e62SAl Viro return filp; 23271da177e4SLinus Torvalds 232831e6b01fSNick Piggin out_filp: 232910fa8e62SAl Viro filp = ERR_PTR(error); 233010fa8e62SAl Viro goto out; 2331de459215SKirill Korotaev } 23321da177e4SLinus Torvalds 233313aab428SAl Viro struct file *do_filp_open(int dfd, const char *pathname, 233413aab428SAl Viro const struct open_flags *op, int flags) 233513aab428SAl Viro { 233673d049a4SAl Viro struct nameidata nd; 233713aab428SAl Viro struct file *filp; 233813aab428SAl Viro 233973d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU); 234013aab428SAl Viro if (unlikely(filp == ERR_PTR(-ECHILD))) 234173d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags); 234213aab428SAl Viro if (unlikely(filp == ERR_PTR(-ESTALE))) 234373d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL); 234413aab428SAl Viro return filp; 234513aab428SAl Viro } 234613aab428SAl Viro 234773d049a4SAl Viro struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt, 234873d049a4SAl Viro const char *name, const struct open_flags *op, int flags) 234973d049a4SAl Viro { 235073d049a4SAl Viro struct nameidata nd; 235173d049a4SAl Viro struct file *file; 235273d049a4SAl Viro 235373d049a4SAl Viro nd.root.mnt = mnt; 235473d049a4SAl Viro nd.root.dentry = dentry; 235573d049a4SAl Viro 235673d049a4SAl Viro flags |= LOOKUP_ROOT; 235773d049a4SAl Viro 2358bcda7652SAl Viro if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN) 235973d049a4SAl Viro return ERR_PTR(-ELOOP); 236073d049a4SAl Viro 236173d049a4SAl Viro file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU); 236273d049a4SAl Viro if (unlikely(file == ERR_PTR(-ECHILD))) 236373d049a4SAl Viro file = path_openat(-1, name, &nd, op, flags); 236473d049a4SAl Viro if (unlikely(file == ERR_PTR(-ESTALE))) 236573d049a4SAl Viro file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL); 236673d049a4SAl Viro return file; 236773d049a4SAl Viro } 236873d049a4SAl Viro 2369ed75e95dSAl Viro struct dentry *kern_path_create(int dfd, const char *pathname, struct path *path, int is_dir) 23701da177e4SLinus Torvalds { 2371c663e5d8SChristoph Hellwig struct dentry *dentry = ERR_PTR(-EEXIST); 2372ed75e95dSAl Viro struct nameidata nd; 2373ed75e95dSAl Viro int error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd); 2374ed75e95dSAl Viro if (error) 2375ed75e95dSAl Viro return ERR_PTR(error); 23761da177e4SLinus Torvalds 2377c663e5d8SChristoph Hellwig /* 2378c663e5d8SChristoph Hellwig * Yucky last component or no last component at all? 2379c663e5d8SChristoph Hellwig * (foo/., foo/.., /////) 2380c663e5d8SChristoph Hellwig */ 2381ed75e95dSAl Viro if (nd.last_type != LAST_NORM) 2382ed75e95dSAl Viro goto out; 2383ed75e95dSAl Viro nd.flags &= ~LOOKUP_PARENT; 2384ed75e95dSAl Viro nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL; 2385ed75e95dSAl Viro nd.intent.open.flags = O_EXCL; 2386c663e5d8SChristoph Hellwig 2387c663e5d8SChristoph Hellwig /* 2388c663e5d8SChristoph Hellwig * Do the final lookup. 2389c663e5d8SChristoph Hellwig */ 2390ed75e95dSAl Viro mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 2391ed75e95dSAl Viro dentry = lookup_hash(&nd); 23921da177e4SLinus Torvalds if (IS_ERR(dentry)) 23931da177e4SLinus Torvalds goto fail; 2394c663e5d8SChristoph Hellwig 2395e9baf6e5SAl Viro if (dentry->d_inode) 2396e9baf6e5SAl Viro goto eexist; 2397c663e5d8SChristoph Hellwig /* 2398c663e5d8SChristoph Hellwig * Special case - lookup gave negative, but... we had foo/bar/ 2399c663e5d8SChristoph Hellwig * From the vfs_mknod() POV we just have a negative dentry - 2400c663e5d8SChristoph Hellwig * all is fine. Let's be bastards - you had / on the end, you've 2401c663e5d8SChristoph Hellwig * been asking for (non-existent) directory. -ENOENT for you. 2402c663e5d8SChristoph Hellwig */ 2403ed75e95dSAl Viro if (unlikely(!is_dir && nd.last.name[nd.last.len])) { 24041da177e4SLinus Torvalds dput(dentry); 24051da177e4SLinus Torvalds dentry = ERR_PTR(-ENOENT); 2406ed75e95dSAl Viro goto fail; 2407e9baf6e5SAl Viro } 2408ed75e95dSAl Viro *path = nd.path; 2409e9baf6e5SAl Viro return dentry; 2410e9baf6e5SAl Viro eexist: 2411e9baf6e5SAl Viro dput(dentry); 2412e9baf6e5SAl Viro dentry = ERR_PTR(-EEXIST); 24131da177e4SLinus Torvalds fail: 2414dae6ad8fSAl Viro mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 2415ed75e95dSAl Viro out: 2416dae6ad8fSAl Viro path_put(&nd.path); 2417ed75e95dSAl Viro return dentry; 2418dae6ad8fSAl Viro } 2419dae6ad8fSAl Viro EXPORT_SYMBOL(kern_path_create); 2420dae6ad8fSAl Viro 2421dae6ad8fSAl Viro struct dentry *user_path_create(int dfd, const char __user *pathname, struct path *path, int is_dir) 2422dae6ad8fSAl Viro { 2423dae6ad8fSAl Viro char *tmp = getname(pathname); 2424dae6ad8fSAl Viro struct dentry *res; 2425dae6ad8fSAl Viro if (IS_ERR(tmp)) 2426dae6ad8fSAl Viro return ERR_CAST(tmp); 2427dae6ad8fSAl Viro res = kern_path_create(dfd, tmp, path, is_dir); 2428dae6ad8fSAl Viro putname(tmp); 2429dae6ad8fSAl Viro return res; 2430dae6ad8fSAl Viro } 2431dae6ad8fSAl Viro EXPORT_SYMBOL(user_path_create); 2432dae6ad8fSAl Viro 24331da177e4SLinus Torvalds int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev) 24341da177e4SLinus Torvalds { 2435a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 24361da177e4SLinus Torvalds 24371da177e4SLinus Torvalds if (error) 24381da177e4SLinus Torvalds return error; 24391da177e4SLinus Torvalds 2440e795b717SSerge E. Hallyn if ((S_ISCHR(mode) || S_ISBLK(mode)) && 2441e795b717SSerge E. Hallyn !ns_capable(inode_userns(dir), CAP_MKNOD)) 24421da177e4SLinus Torvalds return -EPERM; 24431da177e4SLinus Torvalds 2444acfa4380SAl Viro if (!dir->i_op->mknod) 24451da177e4SLinus Torvalds return -EPERM; 24461da177e4SLinus Torvalds 244708ce5f16SSerge E. Hallyn error = devcgroup_inode_mknod(mode, dev); 244808ce5f16SSerge E. Hallyn if (error) 244908ce5f16SSerge E. Hallyn return error; 245008ce5f16SSerge E. Hallyn 24511da177e4SLinus Torvalds error = security_inode_mknod(dir, dentry, mode, dev); 24521da177e4SLinus Torvalds if (error) 24531da177e4SLinus Torvalds return error; 24541da177e4SLinus Torvalds 24551da177e4SLinus Torvalds error = dir->i_op->mknod(dir, dentry, mode, dev); 2456a74574aaSStephen Smalley if (!error) 2457f38aa942SAmy Griffis fsnotify_create(dir, dentry); 24581da177e4SLinus Torvalds return error; 24591da177e4SLinus Torvalds } 24601da177e4SLinus Torvalds 2461463c3197SDave Hansen static int may_mknod(mode_t mode) 2462463c3197SDave Hansen { 2463463c3197SDave Hansen switch (mode & S_IFMT) { 2464463c3197SDave Hansen case S_IFREG: 2465463c3197SDave Hansen case S_IFCHR: 2466463c3197SDave Hansen case S_IFBLK: 2467463c3197SDave Hansen case S_IFIFO: 2468463c3197SDave Hansen case S_IFSOCK: 2469463c3197SDave Hansen case 0: /* zero mode translates to S_IFREG */ 2470463c3197SDave Hansen return 0; 2471463c3197SDave Hansen case S_IFDIR: 2472463c3197SDave Hansen return -EPERM; 2473463c3197SDave Hansen default: 2474463c3197SDave Hansen return -EINVAL; 2475463c3197SDave Hansen } 2476463c3197SDave Hansen } 2477463c3197SDave Hansen 24782e4d0924SHeiko Carstens SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode, 24792e4d0924SHeiko Carstens unsigned, dev) 24801da177e4SLinus Torvalds { 24811da177e4SLinus Torvalds struct dentry *dentry; 2482dae6ad8fSAl Viro struct path path; 2483dae6ad8fSAl Viro int error; 24841da177e4SLinus Torvalds 24851da177e4SLinus Torvalds if (S_ISDIR(mode)) 24861da177e4SLinus Torvalds return -EPERM; 24871da177e4SLinus Torvalds 2488dae6ad8fSAl Viro dentry = user_path_create(dfd, filename, &path, 0); 2489dae6ad8fSAl Viro if (IS_ERR(dentry)) 2490dae6ad8fSAl Viro return PTR_ERR(dentry); 24912ad94ae6SAl Viro 2492dae6ad8fSAl Viro if (!IS_POSIXACL(path.dentry->d_inode)) 2493ce3b0f8dSAl Viro mode &= ~current_umask(); 2494463c3197SDave Hansen error = may_mknod(mode); 2495463c3197SDave Hansen if (error) 2496463c3197SDave Hansen goto out_dput; 2497dae6ad8fSAl Viro error = mnt_want_write(path.mnt); 2498463c3197SDave Hansen if (error) 2499463c3197SDave Hansen goto out_dput; 2500dae6ad8fSAl Viro error = security_path_mknod(&path, dentry, mode, dev); 2501be6d3e56SKentaro Takeda if (error) 2502be6d3e56SKentaro Takeda goto out_drop_write; 25031da177e4SLinus Torvalds switch (mode & S_IFMT) { 25041da177e4SLinus Torvalds case 0: case S_IFREG: 2505dae6ad8fSAl Viro error = vfs_create(path.dentry->d_inode,dentry,mode,NULL); 25061da177e4SLinus Torvalds break; 25071da177e4SLinus Torvalds case S_IFCHR: case S_IFBLK: 2508dae6ad8fSAl Viro error = vfs_mknod(path.dentry->d_inode,dentry,mode, 25091da177e4SLinus Torvalds new_decode_dev(dev)); 25101da177e4SLinus Torvalds break; 25111da177e4SLinus Torvalds case S_IFIFO: case S_IFSOCK: 2512dae6ad8fSAl Viro error = vfs_mknod(path.dentry->d_inode,dentry,mode,0); 25131da177e4SLinus Torvalds break; 25141da177e4SLinus Torvalds } 2515be6d3e56SKentaro Takeda out_drop_write: 2516dae6ad8fSAl Viro mnt_drop_write(path.mnt); 2517463c3197SDave Hansen out_dput: 25181da177e4SLinus Torvalds dput(dentry); 2519dae6ad8fSAl Viro mutex_unlock(&path.dentry->d_inode->i_mutex); 2520dae6ad8fSAl Viro path_put(&path); 25211da177e4SLinus Torvalds 25221da177e4SLinus Torvalds return error; 25231da177e4SLinus Torvalds } 25241da177e4SLinus Torvalds 25253480b257SHeiko Carstens SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev) 25265590ff0dSUlrich Drepper { 25275590ff0dSUlrich Drepper return sys_mknodat(AT_FDCWD, filename, mode, dev); 25285590ff0dSUlrich Drepper } 25295590ff0dSUlrich Drepper 25301da177e4SLinus Torvalds int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) 25311da177e4SLinus Torvalds { 2532a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 25331da177e4SLinus Torvalds 25341da177e4SLinus Torvalds if (error) 25351da177e4SLinus Torvalds return error; 25361da177e4SLinus Torvalds 2537acfa4380SAl Viro if (!dir->i_op->mkdir) 25381da177e4SLinus Torvalds return -EPERM; 25391da177e4SLinus Torvalds 25401da177e4SLinus Torvalds mode &= (S_IRWXUGO|S_ISVTX); 25411da177e4SLinus Torvalds error = security_inode_mkdir(dir, dentry, mode); 25421da177e4SLinus Torvalds if (error) 25431da177e4SLinus Torvalds return error; 25441da177e4SLinus Torvalds 25451da177e4SLinus Torvalds error = dir->i_op->mkdir(dir, dentry, mode); 2546a74574aaSStephen Smalley if (!error) 2547f38aa942SAmy Griffis fsnotify_mkdir(dir, dentry); 25481da177e4SLinus Torvalds return error; 25491da177e4SLinus Torvalds } 25501da177e4SLinus Torvalds 25512e4d0924SHeiko Carstens SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode) 25521da177e4SLinus Torvalds { 25536902d925SDave Hansen struct dentry *dentry; 2554dae6ad8fSAl Viro struct path path; 2555dae6ad8fSAl Viro int error; 25561da177e4SLinus Torvalds 2557dae6ad8fSAl Viro dentry = user_path_create(dfd, pathname, &path, 1); 25586902d925SDave Hansen if (IS_ERR(dentry)) 2559dae6ad8fSAl Viro return PTR_ERR(dentry); 25606902d925SDave Hansen 2561dae6ad8fSAl Viro if (!IS_POSIXACL(path.dentry->d_inode)) 2562ce3b0f8dSAl Viro mode &= ~current_umask(); 2563dae6ad8fSAl Viro error = mnt_want_write(path.mnt); 2564463c3197SDave Hansen if (error) 2565463c3197SDave Hansen goto out_dput; 2566dae6ad8fSAl Viro error = security_path_mkdir(&path, dentry, mode); 2567be6d3e56SKentaro Takeda if (error) 2568be6d3e56SKentaro Takeda goto out_drop_write; 2569dae6ad8fSAl Viro error = vfs_mkdir(path.dentry->d_inode, dentry, mode); 2570be6d3e56SKentaro Takeda out_drop_write: 2571dae6ad8fSAl Viro mnt_drop_write(path.mnt); 2572463c3197SDave Hansen out_dput: 25731da177e4SLinus Torvalds dput(dentry); 2574dae6ad8fSAl Viro mutex_unlock(&path.dentry->d_inode->i_mutex); 2575dae6ad8fSAl Viro path_put(&path); 25761da177e4SLinus Torvalds return error; 25771da177e4SLinus Torvalds } 25781da177e4SLinus Torvalds 25793cdad428SHeiko Carstens SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode) 25805590ff0dSUlrich Drepper { 25815590ff0dSUlrich Drepper return sys_mkdirat(AT_FDCWD, pathname, mode); 25825590ff0dSUlrich Drepper } 25835590ff0dSUlrich Drepper 25841da177e4SLinus Torvalds /* 2585a71905f0SSage Weil * The dentry_unhash() helper will try to drop the dentry early: we 2586a71905f0SSage Weil * should have a usage count of 2 if we're the only user of this 2587a71905f0SSage Weil * dentry, and if that is true (possibly after pruning the dcache), 2588a71905f0SSage Weil * then we drop the dentry now. 25891da177e4SLinus Torvalds * 25901da177e4SLinus Torvalds * A low-level filesystem can, if it choses, legally 25911da177e4SLinus Torvalds * do a 25921da177e4SLinus Torvalds * 25931da177e4SLinus Torvalds * if (!d_unhashed(dentry)) 25941da177e4SLinus Torvalds * return -EBUSY; 25951da177e4SLinus Torvalds * 25961da177e4SLinus Torvalds * if it cannot handle the case of removing a directory 25971da177e4SLinus Torvalds * that is still in use by something else.. 25981da177e4SLinus Torvalds */ 25991da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry) 26001da177e4SLinus Torvalds { 26011da177e4SLinus Torvalds shrink_dcache_parent(dentry); 26021da177e4SLinus Torvalds spin_lock(&dentry->d_lock); 260364252c75SSage Weil if (dentry->d_count == 1) 26041da177e4SLinus Torvalds __d_drop(dentry); 26051da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 26061da177e4SLinus Torvalds } 26071da177e4SLinus Torvalds 26081da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry) 26091da177e4SLinus Torvalds { 26101da177e4SLinus Torvalds int error = may_delete(dir, dentry, 1); 26111da177e4SLinus Torvalds 26121da177e4SLinus Torvalds if (error) 26131da177e4SLinus Torvalds return error; 26141da177e4SLinus Torvalds 2615acfa4380SAl Viro if (!dir->i_op->rmdir) 26161da177e4SLinus Torvalds return -EPERM; 26171da177e4SLinus Torvalds 26181d2ef590SAl Viro dget(dentry); 26191b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 2620912dbc15SSage Weil 26211da177e4SLinus Torvalds error = -EBUSY; 2622912dbc15SSage Weil if (d_mountpoint(dentry)) 2623912dbc15SSage Weil goto out; 2624912dbc15SSage Weil 26251da177e4SLinus Torvalds error = security_inode_rmdir(dir, dentry); 2626912dbc15SSage Weil if (error) 2627912dbc15SSage Weil goto out; 2628912dbc15SSage Weil 26293cebde24SSage Weil shrink_dcache_parent(dentry); 26301da177e4SLinus Torvalds error = dir->i_op->rmdir(dir, dentry); 2631912dbc15SSage Weil if (error) 2632912dbc15SSage Weil goto out; 2633912dbc15SSage Weil 26341da177e4SLinus Torvalds dentry->d_inode->i_flags |= S_DEAD; 2635d83c49f3SAl Viro dont_mount(dentry); 26361da177e4SLinus Torvalds 2637912dbc15SSage Weil out: 2638912dbc15SSage Weil mutex_unlock(&dentry->d_inode->i_mutex); 26391d2ef590SAl Viro dput(dentry); 2640912dbc15SSage Weil if (!error) 2641912dbc15SSage Weil d_delete(dentry); 26421da177e4SLinus Torvalds return error; 26431da177e4SLinus Torvalds } 26441da177e4SLinus Torvalds 26455590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname) 26461da177e4SLinus Torvalds { 26471da177e4SLinus Torvalds int error = 0; 26481da177e4SLinus Torvalds char * name; 26491da177e4SLinus Torvalds struct dentry *dentry; 26501da177e4SLinus Torvalds struct nameidata nd; 26511da177e4SLinus Torvalds 26522ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &name); 26531da177e4SLinus Torvalds if (error) 26542ad94ae6SAl Viro return error; 26551da177e4SLinus Torvalds 26561da177e4SLinus Torvalds switch(nd.last_type) { 26571da177e4SLinus Torvalds case LAST_DOTDOT: 26581da177e4SLinus Torvalds error = -ENOTEMPTY; 26591da177e4SLinus Torvalds goto exit1; 26601da177e4SLinus Torvalds case LAST_DOT: 26611da177e4SLinus Torvalds error = -EINVAL; 26621da177e4SLinus Torvalds goto exit1; 26631da177e4SLinus Torvalds case LAST_ROOT: 26641da177e4SLinus Torvalds error = -EBUSY; 26651da177e4SLinus Torvalds goto exit1; 26661da177e4SLinus Torvalds } 26670612d9fbSOGAWA Hirofumi 26680612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 26690612d9fbSOGAWA Hirofumi 26704ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 267149705b77SChristoph Hellwig dentry = lookup_hash(&nd); 26721da177e4SLinus Torvalds error = PTR_ERR(dentry); 26736902d925SDave Hansen if (IS_ERR(dentry)) 26746902d925SDave Hansen goto exit2; 2675e6bc45d6STheodore Ts'o if (!dentry->d_inode) { 2676e6bc45d6STheodore Ts'o error = -ENOENT; 2677e6bc45d6STheodore Ts'o goto exit3; 2678e6bc45d6STheodore Ts'o } 26790622753bSDave Hansen error = mnt_want_write(nd.path.mnt); 26800622753bSDave Hansen if (error) 26810622753bSDave Hansen goto exit3; 2682be6d3e56SKentaro Takeda error = security_path_rmdir(&nd.path, dentry); 2683be6d3e56SKentaro Takeda if (error) 2684be6d3e56SKentaro Takeda goto exit4; 26854ac91378SJan Blunck error = vfs_rmdir(nd.path.dentry->d_inode, dentry); 2686be6d3e56SKentaro Takeda exit4: 26870622753bSDave Hansen mnt_drop_write(nd.path.mnt); 26880622753bSDave Hansen exit3: 26891da177e4SLinus Torvalds dput(dentry); 26906902d925SDave Hansen exit2: 26914ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 26921da177e4SLinus Torvalds exit1: 26931d957f9bSJan Blunck path_put(&nd.path); 26941da177e4SLinus Torvalds putname(name); 26951da177e4SLinus Torvalds return error; 26961da177e4SLinus Torvalds } 26971da177e4SLinus Torvalds 26983cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname) 26995590ff0dSUlrich Drepper { 27005590ff0dSUlrich Drepper return do_rmdir(AT_FDCWD, pathname); 27015590ff0dSUlrich Drepper } 27025590ff0dSUlrich Drepper 27031da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry) 27041da177e4SLinus Torvalds { 27051da177e4SLinus Torvalds int error = may_delete(dir, dentry, 0); 27061da177e4SLinus Torvalds 27071da177e4SLinus Torvalds if (error) 27081da177e4SLinus Torvalds return error; 27091da177e4SLinus Torvalds 2710acfa4380SAl Viro if (!dir->i_op->unlink) 27111da177e4SLinus Torvalds return -EPERM; 27121da177e4SLinus Torvalds 27131b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 27141da177e4SLinus Torvalds if (d_mountpoint(dentry)) 27151da177e4SLinus Torvalds error = -EBUSY; 27161da177e4SLinus Torvalds else { 27171da177e4SLinus Torvalds error = security_inode_unlink(dir, dentry); 2718bec1052eSAl Viro if (!error) { 27191da177e4SLinus Torvalds error = dir->i_op->unlink(dir, dentry); 2720bec1052eSAl Viro if (!error) 2721d83c49f3SAl Viro dont_mount(dentry); 2722bec1052eSAl Viro } 27231da177e4SLinus Torvalds } 27241b1dcc1bSJes Sorensen mutex_unlock(&dentry->d_inode->i_mutex); 27251da177e4SLinus Torvalds 27261da177e4SLinus Torvalds /* We don't d_delete() NFS sillyrenamed files--they still exist. */ 27271da177e4SLinus Torvalds if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) { 2728ece95912SJan Kara fsnotify_link_count(dentry->d_inode); 27291da177e4SLinus Torvalds d_delete(dentry); 27301da177e4SLinus Torvalds } 27310eeca283SRobert Love 27321da177e4SLinus Torvalds return error; 27331da177e4SLinus Torvalds } 27341da177e4SLinus Torvalds 27351da177e4SLinus Torvalds /* 27361da177e4SLinus Torvalds * Make sure that the actual truncation of the file will occur outside its 27371b1dcc1bSJes Sorensen * directory's i_mutex. Truncate can take a long time if there is a lot of 27381da177e4SLinus Torvalds * writeout happening, and we don't want to prevent access to the directory 27391da177e4SLinus Torvalds * while waiting on the I/O. 27401da177e4SLinus Torvalds */ 27415590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname) 27421da177e4SLinus Torvalds { 27432ad94ae6SAl Viro int error; 27441da177e4SLinus Torvalds char *name; 27451da177e4SLinus Torvalds struct dentry *dentry; 27461da177e4SLinus Torvalds struct nameidata nd; 27471da177e4SLinus Torvalds struct inode *inode = NULL; 27481da177e4SLinus Torvalds 27492ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &name); 27501da177e4SLinus Torvalds if (error) 27512ad94ae6SAl Viro return error; 27522ad94ae6SAl Viro 27531da177e4SLinus Torvalds error = -EISDIR; 27541da177e4SLinus Torvalds if (nd.last_type != LAST_NORM) 27551da177e4SLinus Torvalds goto exit1; 27560612d9fbSOGAWA Hirofumi 27570612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 27580612d9fbSOGAWA Hirofumi 27594ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 276049705b77SChristoph Hellwig dentry = lookup_hash(&nd); 27611da177e4SLinus Torvalds error = PTR_ERR(dentry); 27621da177e4SLinus Torvalds if (!IS_ERR(dentry)) { 27631da177e4SLinus Torvalds /* Why not before? Because we want correct error value */ 276450338b88STörök Edwin if (nd.last.name[nd.last.len]) 276550338b88STörök Edwin goto slashes; 27661da177e4SLinus Torvalds inode = dentry->d_inode; 276750338b88STörök Edwin if (!inode) 2768e6bc45d6STheodore Ts'o goto slashes; 27697de9c6eeSAl Viro ihold(inode); 27700622753bSDave Hansen error = mnt_want_write(nd.path.mnt); 27710622753bSDave Hansen if (error) 27720622753bSDave Hansen goto exit2; 2773be6d3e56SKentaro Takeda error = security_path_unlink(&nd.path, dentry); 2774be6d3e56SKentaro Takeda if (error) 2775be6d3e56SKentaro Takeda goto exit3; 27764ac91378SJan Blunck error = vfs_unlink(nd.path.dentry->d_inode, dentry); 2777be6d3e56SKentaro Takeda exit3: 27780622753bSDave Hansen mnt_drop_write(nd.path.mnt); 27791da177e4SLinus Torvalds exit2: 27801da177e4SLinus Torvalds dput(dentry); 27811da177e4SLinus Torvalds } 27824ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 27831da177e4SLinus Torvalds if (inode) 27841da177e4SLinus Torvalds iput(inode); /* truncate the inode here */ 27851da177e4SLinus Torvalds exit1: 27861d957f9bSJan Blunck path_put(&nd.path); 27871da177e4SLinus Torvalds putname(name); 27881da177e4SLinus Torvalds return error; 27891da177e4SLinus Torvalds 27901da177e4SLinus Torvalds slashes: 27911da177e4SLinus Torvalds error = !dentry->d_inode ? -ENOENT : 27921da177e4SLinus Torvalds S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR; 27931da177e4SLinus Torvalds goto exit2; 27941da177e4SLinus Torvalds } 27951da177e4SLinus Torvalds 27962e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag) 27975590ff0dSUlrich Drepper { 27985590ff0dSUlrich Drepper if ((flag & ~AT_REMOVEDIR) != 0) 27995590ff0dSUlrich Drepper return -EINVAL; 28005590ff0dSUlrich Drepper 28015590ff0dSUlrich Drepper if (flag & AT_REMOVEDIR) 28025590ff0dSUlrich Drepper return do_rmdir(dfd, pathname); 28035590ff0dSUlrich Drepper 28045590ff0dSUlrich Drepper return do_unlinkat(dfd, pathname); 28055590ff0dSUlrich Drepper } 28065590ff0dSUlrich Drepper 28073480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname) 28085590ff0dSUlrich Drepper { 28095590ff0dSUlrich Drepper return do_unlinkat(AT_FDCWD, pathname); 28105590ff0dSUlrich Drepper } 28115590ff0dSUlrich Drepper 2812db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname) 28131da177e4SLinus Torvalds { 2814a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 28151da177e4SLinus Torvalds 28161da177e4SLinus Torvalds if (error) 28171da177e4SLinus Torvalds return error; 28181da177e4SLinus Torvalds 2819acfa4380SAl Viro if (!dir->i_op->symlink) 28201da177e4SLinus Torvalds return -EPERM; 28211da177e4SLinus Torvalds 28221da177e4SLinus Torvalds error = security_inode_symlink(dir, dentry, oldname); 28231da177e4SLinus Torvalds if (error) 28241da177e4SLinus Torvalds return error; 28251da177e4SLinus Torvalds 28261da177e4SLinus Torvalds error = dir->i_op->symlink(dir, dentry, oldname); 2827a74574aaSStephen Smalley if (!error) 2828f38aa942SAmy Griffis fsnotify_create(dir, dentry); 28291da177e4SLinus Torvalds return error; 28301da177e4SLinus Torvalds } 28311da177e4SLinus Torvalds 28322e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname, 28332e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 28341da177e4SLinus Torvalds { 28352ad94ae6SAl Viro int error; 28361da177e4SLinus Torvalds char *from; 28376902d925SDave Hansen struct dentry *dentry; 2838dae6ad8fSAl Viro struct path path; 28391da177e4SLinus Torvalds 28401da177e4SLinus Torvalds from = getname(oldname); 28411da177e4SLinus Torvalds if (IS_ERR(from)) 28421da177e4SLinus Torvalds return PTR_ERR(from); 28432ad94ae6SAl Viro 2844dae6ad8fSAl Viro dentry = user_path_create(newdfd, newname, &path, 0); 28451da177e4SLinus Torvalds error = PTR_ERR(dentry); 28466902d925SDave Hansen if (IS_ERR(dentry)) 2847dae6ad8fSAl Viro goto out_putname; 28486902d925SDave Hansen 2849dae6ad8fSAl Viro error = mnt_want_write(path.mnt); 285075c3f29dSDave Hansen if (error) 285175c3f29dSDave Hansen goto out_dput; 2852dae6ad8fSAl Viro error = security_path_symlink(&path, dentry, from); 2853be6d3e56SKentaro Takeda if (error) 2854be6d3e56SKentaro Takeda goto out_drop_write; 2855dae6ad8fSAl Viro error = vfs_symlink(path.dentry->d_inode, dentry, from); 2856be6d3e56SKentaro Takeda out_drop_write: 2857dae6ad8fSAl Viro mnt_drop_write(path.mnt); 285875c3f29dSDave Hansen out_dput: 28591da177e4SLinus Torvalds dput(dentry); 2860dae6ad8fSAl Viro mutex_unlock(&path.dentry->d_inode->i_mutex); 2861dae6ad8fSAl Viro path_put(&path); 28626902d925SDave Hansen out_putname: 28631da177e4SLinus Torvalds putname(from); 28641da177e4SLinus Torvalds return error; 28651da177e4SLinus Torvalds } 28661da177e4SLinus Torvalds 28673480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname) 28685590ff0dSUlrich Drepper { 28695590ff0dSUlrich Drepper return sys_symlinkat(oldname, AT_FDCWD, newname); 28705590ff0dSUlrich Drepper } 28715590ff0dSUlrich Drepper 28721da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) 28731da177e4SLinus Torvalds { 28741da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 28751da177e4SLinus Torvalds int error; 28761da177e4SLinus Torvalds 28771da177e4SLinus Torvalds if (!inode) 28781da177e4SLinus Torvalds return -ENOENT; 28791da177e4SLinus Torvalds 2880a95164d9SMiklos Szeredi error = may_create(dir, new_dentry); 28811da177e4SLinus Torvalds if (error) 28821da177e4SLinus Torvalds return error; 28831da177e4SLinus Torvalds 28841da177e4SLinus Torvalds if (dir->i_sb != inode->i_sb) 28851da177e4SLinus Torvalds return -EXDEV; 28861da177e4SLinus Torvalds 28871da177e4SLinus Torvalds /* 28881da177e4SLinus Torvalds * A link to an append-only or immutable file cannot be created. 28891da177e4SLinus Torvalds */ 28901da177e4SLinus Torvalds if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) 28911da177e4SLinus Torvalds return -EPERM; 2892acfa4380SAl Viro if (!dir->i_op->link) 28931da177e4SLinus Torvalds return -EPERM; 28947e79eedbSTetsuo Handa if (S_ISDIR(inode->i_mode)) 28951da177e4SLinus Torvalds return -EPERM; 28961da177e4SLinus Torvalds 28971da177e4SLinus Torvalds error = security_inode_link(old_dentry, dir, new_dentry); 28981da177e4SLinus Torvalds if (error) 28991da177e4SLinus Torvalds return error; 29001da177e4SLinus Torvalds 29017e79eedbSTetsuo Handa mutex_lock(&inode->i_mutex); 2902aae8a97dSAneesh Kumar K.V /* Make sure we don't allow creating hardlink to an unlinked file */ 2903aae8a97dSAneesh Kumar K.V if (inode->i_nlink == 0) 2904aae8a97dSAneesh Kumar K.V error = -ENOENT; 2905aae8a97dSAneesh Kumar K.V else 29061da177e4SLinus Torvalds error = dir->i_op->link(old_dentry, dir, new_dentry); 29077e79eedbSTetsuo Handa mutex_unlock(&inode->i_mutex); 2908e31e14ecSStephen Smalley if (!error) 29097e79eedbSTetsuo Handa fsnotify_link(dir, inode, new_dentry); 29101da177e4SLinus Torvalds return error; 29111da177e4SLinus Torvalds } 29121da177e4SLinus Torvalds 29131da177e4SLinus Torvalds /* 29141da177e4SLinus Torvalds * Hardlinks are often used in delicate situations. We avoid 29151da177e4SLinus Torvalds * security-related surprises by not following symlinks on the 29161da177e4SLinus Torvalds * newname. --KAB 29171da177e4SLinus Torvalds * 29181da177e4SLinus Torvalds * We don't follow them on the oldname either to be compatible 29191da177e4SLinus Torvalds * with linux 2.0, and to avoid hard-linking to directories 29201da177e4SLinus Torvalds * and other special files. --ADM 29211da177e4SLinus Torvalds */ 29222e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname, 29232e4d0924SHeiko Carstens int, newdfd, const char __user *, newname, int, flags) 29241da177e4SLinus Torvalds { 29251da177e4SLinus Torvalds struct dentry *new_dentry; 2926dae6ad8fSAl Viro struct path old_path, new_path; 292711a7b371SAneesh Kumar K.V int how = 0; 29281da177e4SLinus Torvalds int error; 29291da177e4SLinus Torvalds 293011a7b371SAneesh Kumar K.V if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0) 2931c04030e1SUlrich Drepper return -EINVAL; 293211a7b371SAneesh Kumar K.V /* 293311a7b371SAneesh Kumar K.V * To use null names we require CAP_DAC_READ_SEARCH 293411a7b371SAneesh Kumar K.V * This ensures that not everyone will be able to create 293511a7b371SAneesh Kumar K.V * handlink using the passed filedescriptor. 293611a7b371SAneesh Kumar K.V */ 293711a7b371SAneesh Kumar K.V if (flags & AT_EMPTY_PATH) { 293811a7b371SAneesh Kumar K.V if (!capable(CAP_DAC_READ_SEARCH)) 293911a7b371SAneesh Kumar K.V return -ENOENT; 294011a7b371SAneesh Kumar K.V how = LOOKUP_EMPTY; 294111a7b371SAneesh Kumar K.V } 2942c04030e1SUlrich Drepper 294311a7b371SAneesh Kumar K.V if (flags & AT_SYMLINK_FOLLOW) 294411a7b371SAneesh Kumar K.V how |= LOOKUP_FOLLOW; 294511a7b371SAneesh Kumar K.V 294611a7b371SAneesh Kumar K.V error = user_path_at(olddfd, oldname, how, &old_path); 29471da177e4SLinus Torvalds if (error) 29482ad94ae6SAl Viro return error; 29492ad94ae6SAl Viro 2950dae6ad8fSAl Viro new_dentry = user_path_create(newdfd, newname, &new_path, 0); 29511da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 29526902d925SDave Hansen if (IS_ERR(new_dentry)) 2953dae6ad8fSAl Viro goto out; 2954dae6ad8fSAl Viro 2955dae6ad8fSAl Viro error = -EXDEV; 2956dae6ad8fSAl Viro if (old_path.mnt != new_path.mnt) 2957dae6ad8fSAl Viro goto out_dput; 2958dae6ad8fSAl Viro error = mnt_want_write(new_path.mnt); 295975c3f29dSDave Hansen if (error) 296075c3f29dSDave Hansen goto out_dput; 2961dae6ad8fSAl Viro error = security_path_link(old_path.dentry, &new_path, new_dentry); 2962be6d3e56SKentaro Takeda if (error) 2963be6d3e56SKentaro Takeda goto out_drop_write; 2964dae6ad8fSAl Viro error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry); 2965be6d3e56SKentaro Takeda out_drop_write: 2966dae6ad8fSAl Viro mnt_drop_write(new_path.mnt); 296775c3f29dSDave Hansen out_dput: 29681da177e4SLinus Torvalds dput(new_dentry); 2969dae6ad8fSAl Viro mutex_unlock(&new_path.dentry->d_inode->i_mutex); 2970dae6ad8fSAl Viro path_put(&new_path); 29711da177e4SLinus Torvalds out: 29722d8f3038SAl Viro path_put(&old_path); 29731da177e4SLinus Torvalds 29741da177e4SLinus Torvalds return error; 29751da177e4SLinus Torvalds } 29761da177e4SLinus Torvalds 29773480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname) 29785590ff0dSUlrich Drepper { 2979c04030e1SUlrich Drepper return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0); 29805590ff0dSUlrich Drepper } 29815590ff0dSUlrich Drepper 29821da177e4SLinus Torvalds /* 29831da177e4SLinus Torvalds * The worst of all namespace operations - renaming directory. "Perverted" 29841da177e4SLinus Torvalds * doesn't even start to describe it. Somebody in UCB had a heck of a trip... 29851da177e4SLinus Torvalds * Problems: 29861da177e4SLinus Torvalds * a) we can get into loop creation. Check is done in is_subdir(). 29871da177e4SLinus Torvalds * b) race potential - two innocent renames can create a loop together. 29881da177e4SLinus Torvalds * That's where 4.4 screws up. Current fix: serialization on 2989a11f3a05SArjan van de Ven * sb->s_vfs_rename_mutex. We might be more accurate, but that's another 29901da177e4SLinus Torvalds * story. 29911da177e4SLinus Torvalds * c) we have to lock _three_ objects - parents and victim (if it exists). 29921b1dcc1bSJes Sorensen * And that - after we got ->i_mutex on parents (until then we don't know 29931da177e4SLinus Torvalds * whether the target exists). Solution: try to be smart with locking 29941da177e4SLinus Torvalds * order for inodes. We rely on the fact that tree topology may change 2995a11f3a05SArjan van de Ven * only under ->s_vfs_rename_mutex _and_ that parent of the object we 29961da177e4SLinus Torvalds * move will be locked. Thus we can rank directories by the tree 29971da177e4SLinus Torvalds * (ancestors first) and rank all non-directories after them. 29981da177e4SLinus Torvalds * That works since everybody except rename does "lock parent, lookup, 2999a11f3a05SArjan van de Ven * lock child" and rename is under ->s_vfs_rename_mutex. 30001da177e4SLinus Torvalds * HOWEVER, it relies on the assumption that any object with ->lookup() 30011da177e4SLinus Torvalds * has no more than 1 dentry. If "hybrid" objects will ever appear, 30021da177e4SLinus Torvalds * we'd better make sure that there's no link(2) for them. 3003e4eaac06SSage Weil * d) conversion from fhandle to dentry may come in the wrong moment - when 30041b1dcc1bSJes Sorensen * we are removing the target. Solution: we will have to grab ->i_mutex 30051da177e4SLinus Torvalds * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on 3006c41b20e7SAdam Buchbinder * ->i_mutex on parents, which works but leads to some truly excessive 30071da177e4SLinus Torvalds * locking]. 30081da177e4SLinus Torvalds */ 300975c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry, 30101da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 30111da177e4SLinus Torvalds { 30121da177e4SLinus Torvalds int error = 0; 30139055cba7SSage Weil struct inode *target = new_dentry->d_inode; 30141da177e4SLinus Torvalds 30151da177e4SLinus Torvalds /* 30161da177e4SLinus Torvalds * If we are going to change the parent - check write permissions, 30171da177e4SLinus Torvalds * we'll need to flip '..'. 30181da177e4SLinus Torvalds */ 30191da177e4SLinus Torvalds if (new_dir != old_dir) { 3020f419a2e3SAl Viro error = inode_permission(old_dentry->d_inode, MAY_WRITE); 30211da177e4SLinus Torvalds if (error) 30221da177e4SLinus Torvalds return error; 30231da177e4SLinus Torvalds } 30241da177e4SLinus Torvalds 30251da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 30261da177e4SLinus Torvalds if (error) 30271da177e4SLinus Torvalds return error; 30281da177e4SLinus Torvalds 30291d2ef590SAl Viro dget(new_dentry); 3030d83c49f3SAl Viro if (target) 30311b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 30329055cba7SSage Weil 30331da177e4SLinus Torvalds error = -EBUSY; 30349055cba7SSage Weil if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry)) 30359055cba7SSage Weil goto out; 30369055cba7SSage Weil 30373cebde24SSage Weil if (target) 30383cebde24SSage Weil shrink_dcache_parent(new_dentry); 30391da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 30409055cba7SSage Weil if (error) 30419055cba7SSage Weil goto out; 30429055cba7SSage Weil 30431da177e4SLinus Torvalds if (target) { 30441da177e4SLinus Torvalds target->i_flags |= S_DEAD; 3045d83c49f3SAl Viro dont_mount(new_dentry); 3046d83c49f3SAl Viro } 30479055cba7SSage Weil out: 30489055cba7SSage Weil if (target) 30491b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 30501d2ef590SAl Viro dput(new_dentry); 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