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> 18630d9c47SPaul Gortmaker #include <linux/export.h> 1944696908SDavid S. Miller #include <linux/kernel.h> 201da177e4SLinus Torvalds #include <linux/slab.h> 211da177e4SLinus Torvalds #include <linux/fs.h> 221da177e4SLinus Torvalds #include <linux/namei.h> 231da177e4SLinus Torvalds #include <linux/pagemap.h> 240eeca283SRobert Love #include <linux/fsnotify.h> 251da177e4SLinus Torvalds #include <linux/personality.h> 261da177e4SLinus Torvalds #include <linux/security.h> 276146f0d5SMimi Zohar #include <linux/ima.h> 281da177e4SLinus Torvalds #include <linux/syscalls.h> 291da177e4SLinus Torvalds #include <linux/mount.h> 301da177e4SLinus Torvalds #include <linux/audit.h> 3116f7e0feSRandy Dunlap #include <linux/capability.h> 32834f2a4aSTrond Myklebust #include <linux/file.h> 335590ff0dSUlrich Drepper #include <linux/fcntl.h> 3408ce5f16SSerge E. Hallyn #include <linux/device_cgroup.h> 355ad4e53bSAl Viro #include <linux/fs_struct.h> 36e77819e5SLinus Torvalds #include <linux/posix_acl.h> 371da177e4SLinus Torvalds #include <asm/uaccess.h> 381da177e4SLinus Torvalds 39e81e3f4dSEric Paris #include "internal.h" 40c7105365SAl Viro #include "mount.h" 41e81e3f4dSEric Paris 421da177e4SLinus Torvalds /* [Feb-1997 T. Schoebel-Theuer] 431da177e4SLinus Torvalds * Fundamental changes in the pathname lookup mechanisms (namei) 441da177e4SLinus Torvalds * were necessary because of omirr. The reason is that omirr needs 451da177e4SLinus Torvalds * to know the _real_ pathname, not the user-supplied one, in case 461da177e4SLinus Torvalds * of symlinks (and also when transname replacements occur). 471da177e4SLinus Torvalds * 481da177e4SLinus Torvalds * The new code replaces the old recursive symlink resolution with 491da177e4SLinus Torvalds * an iterative one (in case of non-nested symlink chains). It does 501da177e4SLinus Torvalds * this with calls to <fs>_follow_link(). 511da177e4SLinus Torvalds * As a side effect, dir_namei(), _namei() and follow_link() are now 521da177e4SLinus Torvalds * replaced with a single function lookup_dentry() that can handle all 531da177e4SLinus Torvalds * the special cases of the former code. 541da177e4SLinus Torvalds * 551da177e4SLinus Torvalds * With the new dcache, the pathname is stored at each inode, at least as 561da177e4SLinus Torvalds * long as the refcount of the inode is positive. As a side effect, the 571da177e4SLinus Torvalds * size of the dcache depends on the inode cache and thus is dynamic. 581da177e4SLinus Torvalds * 591da177e4SLinus Torvalds * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink 601da177e4SLinus Torvalds * resolution to correspond with current state of the code. 611da177e4SLinus Torvalds * 621da177e4SLinus Torvalds * Note that the symlink resolution is not *completely* iterative. 631da177e4SLinus Torvalds * There is still a significant amount of tail- and mid- recursion in 641da177e4SLinus Torvalds * the algorithm. Also, note that <fs>_readlink() is not used in 651da177e4SLinus Torvalds * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink() 661da177e4SLinus Torvalds * may return different results than <fs>_follow_link(). Many virtual 671da177e4SLinus Torvalds * filesystems (including /proc) exhibit this behavior. 681da177e4SLinus Torvalds */ 691da177e4SLinus Torvalds 701da177e4SLinus Torvalds /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation: 711da177e4SLinus Torvalds * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL 721da177e4SLinus Torvalds * and the name already exists in form of a symlink, try to create the new 731da177e4SLinus Torvalds * name indicated by the symlink. The old code always complained that the 741da177e4SLinus Torvalds * name already exists, due to not following the symlink even if its target 751da177e4SLinus Torvalds * is nonexistent. The new semantics affects also mknod() and link() when 7625985edcSLucas De Marchi * the name is a symlink pointing to a non-existent name. 771da177e4SLinus Torvalds * 781da177e4SLinus Torvalds * I don't know which semantics is the right one, since I have no access 791da177e4SLinus Torvalds * to standards. But I found by trial that HP-UX 9.0 has the full "new" 801da177e4SLinus Torvalds * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the 811da177e4SLinus Torvalds * "old" one. Personally, I think the new semantics is much more logical. 821da177e4SLinus Torvalds * Note that "ln old new" where "new" is a symlink pointing to a non-existing 831da177e4SLinus Torvalds * file does succeed in both HP-UX and SunOs, but not in Solaris 841da177e4SLinus Torvalds * and in the old Linux semantics. 851da177e4SLinus Torvalds */ 861da177e4SLinus Torvalds 871da177e4SLinus Torvalds /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink 881da177e4SLinus Torvalds * semantics. See the comments in "open_namei" and "do_link" below. 891da177e4SLinus Torvalds * 901da177e4SLinus Torvalds * [10-Sep-98 Alan Modra] Another symlink change. 911da177e4SLinus Torvalds */ 921da177e4SLinus Torvalds 931da177e4SLinus Torvalds /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks: 941da177e4SLinus Torvalds * inside the path - always follow. 951da177e4SLinus Torvalds * in the last component in creation/removal/renaming - never follow. 961da177e4SLinus Torvalds * if LOOKUP_FOLLOW passed - follow. 971da177e4SLinus Torvalds * if the pathname has trailing slashes - follow. 981da177e4SLinus Torvalds * otherwise - don't follow. 991da177e4SLinus Torvalds * (applied in that order). 1001da177e4SLinus Torvalds * 1011da177e4SLinus Torvalds * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT 1021da177e4SLinus Torvalds * restored for 2.4. This is the last surviving part of old 4.2BSD bug. 1031da177e4SLinus Torvalds * During the 2.4 we need to fix the userland stuff depending on it - 1041da177e4SLinus Torvalds * hopefully we will be able to get rid of that wart in 2.5. So far only 1051da177e4SLinus Torvalds * XEmacs seems to be relying on it... 1061da177e4SLinus Torvalds */ 1071da177e4SLinus Torvalds /* 1081da177e4SLinus Torvalds * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland) 109a11f3a05SArjan van de Ven * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives 1101da177e4SLinus Torvalds * any extra contention... 1111da177e4SLinus Torvalds */ 1121da177e4SLinus Torvalds 1131da177e4SLinus Torvalds /* In order to reduce some races, while at the same time doing additional 1141da177e4SLinus Torvalds * checking and hopefully speeding things up, we copy filenames to the 1151da177e4SLinus Torvalds * kernel data space before using them.. 1161da177e4SLinus Torvalds * 1171da177e4SLinus Torvalds * POSIX.1 2.4: an empty pathname is invalid (ENOENT). 1181da177e4SLinus Torvalds * PATH_MAX includes the nul terminator --RR. 1191da177e4SLinus Torvalds */ 1201fa1e7f6SAndy Whitcroft static char *getname_flags(const char __user *filename, int flags, int *empty) 1211da177e4SLinus Torvalds { 1223f9f0aa6SLinus Torvalds char *result = __getname(), *err; 1233f9f0aa6SLinus Torvalds int len; 1241da177e4SLinus Torvalds 1253f9f0aa6SLinus Torvalds if (unlikely(!result)) 1264043cde8SEric Paris return ERR_PTR(-ENOMEM); 1271da177e4SLinus Torvalds 1283f9f0aa6SLinus Torvalds len = strncpy_from_user(result, filename, PATH_MAX); 1293f9f0aa6SLinus Torvalds err = ERR_PTR(len); 1303f9f0aa6SLinus Torvalds if (unlikely(len < 0)) 1313f9f0aa6SLinus Torvalds goto error; 1323f9f0aa6SLinus Torvalds 1333f9f0aa6SLinus Torvalds /* The empty path is special. */ 1343f9f0aa6SLinus Torvalds if (unlikely(!len)) { 1353f9f0aa6SLinus Torvalds if (empty) 1361fa1e7f6SAndy Whitcroft *empty = 1; 1373f9f0aa6SLinus Torvalds err = ERR_PTR(-ENOENT); 1383f9f0aa6SLinus Torvalds if (!(flags & LOOKUP_EMPTY)) 1393f9f0aa6SLinus Torvalds goto error; 1401da177e4SLinus Torvalds } 1413f9f0aa6SLinus Torvalds 1423f9f0aa6SLinus Torvalds err = ERR_PTR(-ENAMETOOLONG); 1433f9f0aa6SLinus Torvalds if (likely(len < PATH_MAX)) { 1441da177e4SLinus Torvalds audit_getname(result); 1451da177e4SLinus Torvalds return result; 1461da177e4SLinus Torvalds } 1471da177e4SLinus Torvalds 1483f9f0aa6SLinus Torvalds error: 1493f9f0aa6SLinus Torvalds __putname(result); 1503f9f0aa6SLinus Torvalds return err; 1513f9f0aa6SLinus Torvalds } 1523f9f0aa6SLinus Torvalds 153f52e0c11SAl Viro char *getname(const char __user * filename) 154f52e0c11SAl Viro { 155f7493e5dSLinus Torvalds return getname_flags(filename, 0, NULL); 156f52e0c11SAl Viro } 157f52e0c11SAl Viro 1581da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL 1591da177e4SLinus Torvalds void putname(const char *name) 1601da177e4SLinus Torvalds { 1615ac3a9c2SAl Viro if (unlikely(!audit_dummy_context())) 1621da177e4SLinus Torvalds audit_putname(name); 1631da177e4SLinus Torvalds else 1641da177e4SLinus Torvalds __putname(name); 1651da177e4SLinus Torvalds } 1661da177e4SLinus Torvalds EXPORT_SYMBOL(putname); 1671da177e4SLinus Torvalds #endif 1681da177e4SLinus Torvalds 169e77819e5SLinus Torvalds static int check_acl(struct inode *inode, int mask) 170e77819e5SLinus Torvalds { 17184635d68SLinus Torvalds #ifdef CONFIG_FS_POSIX_ACL 172e77819e5SLinus Torvalds struct posix_acl *acl; 173e77819e5SLinus Torvalds 174e77819e5SLinus Torvalds if (mask & MAY_NOT_BLOCK) { 1753567866bSAl Viro acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS); 1763567866bSAl Viro if (!acl) 177e77819e5SLinus Torvalds return -EAGAIN; 1783567866bSAl Viro /* no ->get_acl() calls in RCU mode... */ 1793567866bSAl Viro if (acl == ACL_NOT_CACHED) 180e77819e5SLinus Torvalds return -ECHILD; 181206b1d09SAri Savolainen return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK); 182e77819e5SLinus Torvalds } 183e77819e5SLinus Torvalds 184e77819e5SLinus Torvalds acl = get_cached_acl(inode, ACL_TYPE_ACCESS); 185e77819e5SLinus Torvalds 186e77819e5SLinus Torvalds /* 1874e34e719SChristoph Hellwig * A filesystem can force a ACL callback by just never filling the 1884e34e719SChristoph Hellwig * ACL cache. But normally you'd fill the cache either at inode 1894e34e719SChristoph Hellwig * instantiation time, or on the first ->get_acl call. 190e77819e5SLinus Torvalds * 1914e34e719SChristoph Hellwig * If the filesystem doesn't have a get_acl() function at all, we'll 1924e34e719SChristoph Hellwig * just create the negative cache entry. 193e77819e5SLinus Torvalds */ 194e77819e5SLinus Torvalds if (acl == ACL_NOT_CACHED) { 1954e34e719SChristoph Hellwig if (inode->i_op->get_acl) { 1964e34e719SChristoph Hellwig acl = inode->i_op->get_acl(inode, ACL_TYPE_ACCESS); 1974e34e719SChristoph Hellwig if (IS_ERR(acl)) 1984e34e719SChristoph Hellwig return PTR_ERR(acl); 1994e34e719SChristoph Hellwig } else { 200e77819e5SLinus Torvalds set_cached_acl(inode, ACL_TYPE_ACCESS, NULL); 201e77819e5SLinus Torvalds return -EAGAIN; 202e77819e5SLinus Torvalds } 2034e34e719SChristoph Hellwig } 204e77819e5SLinus Torvalds 205e77819e5SLinus Torvalds if (acl) { 206e77819e5SLinus Torvalds int error = posix_acl_permission(inode, acl, mask); 207e77819e5SLinus Torvalds posix_acl_release(acl); 208e77819e5SLinus Torvalds return error; 209e77819e5SLinus Torvalds } 21084635d68SLinus Torvalds #endif 211e77819e5SLinus Torvalds 212e77819e5SLinus Torvalds return -EAGAIN; 213e77819e5SLinus Torvalds } 214e77819e5SLinus Torvalds 2155909ccaaSLinus Torvalds /* 216948409c7SAndreas Gruenbacher * This does the basic permission checking 2175909ccaaSLinus Torvalds */ 2187e40145eSAl Viro static int acl_permission_check(struct inode *inode, int mask) 2195909ccaaSLinus Torvalds { 22026cf46beSLinus Torvalds unsigned int mode = inode->i_mode; 2215909ccaaSLinus Torvalds 2228e96e3b7SEric W. Biederman if (likely(uid_eq(current_fsuid(), inode->i_uid))) 2235909ccaaSLinus Torvalds mode >>= 6; 2245909ccaaSLinus Torvalds else { 225e77819e5SLinus Torvalds if (IS_POSIXACL(inode) && (mode & S_IRWXG)) { 2267e40145eSAl Viro int error = check_acl(inode, mask); 2275909ccaaSLinus Torvalds if (error != -EAGAIN) 2285909ccaaSLinus Torvalds return error; 2295909ccaaSLinus Torvalds } 2305909ccaaSLinus Torvalds 2315909ccaaSLinus Torvalds if (in_group_p(inode->i_gid)) 2325909ccaaSLinus Torvalds mode >>= 3; 2335909ccaaSLinus Torvalds } 2345909ccaaSLinus Torvalds 2355909ccaaSLinus Torvalds /* 2365909ccaaSLinus Torvalds * If the DACs are ok we don't need any capability check. 2375909ccaaSLinus Torvalds */ 2389c2c7039SAl Viro if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0) 2395909ccaaSLinus Torvalds return 0; 2405909ccaaSLinus Torvalds return -EACCES; 2415909ccaaSLinus Torvalds } 2421da177e4SLinus Torvalds 2431da177e4SLinus Torvalds /** 2441da177e4SLinus Torvalds * generic_permission - check for access rights on a Posix-like filesystem 2451da177e4SLinus Torvalds * @inode: inode to check access rights for 2468fd90c8dSAndreas Gruenbacher * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...) 2471da177e4SLinus Torvalds * 2481da177e4SLinus Torvalds * Used to check for read/write/execute permissions on a file. 2491da177e4SLinus Torvalds * We use "fsuid" for this, letting us set arbitrary permissions 2501da177e4SLinus Torvalds * for filesystem access without changing the "normal" uids which 251b74c79e9SNick Piggin * are used for other things. 252b74c79e9SNick Piggin * 253b74c79e9SNick Piggin * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk 254b74c79e9SNick Piggin * request cannot be satisfied (eg. requires blocking or too much complexity). 255b74c79e9SNick Piggin * It would then be called again in ref-walk mode. 2561da177e4SLinus Torvalds */ 2572830ba7fSAl Viro int generic_permission(struct inode *inode, int mask) 2581da177e4SLinus Torvalds { 2595909ccaaSLinus Torvalds int ret; 2601da177e4SLinus Torvalds 2611da177e4SLinus Torvalds /* 262948409c7SAndreas Gruenbacher * Do the basic permission checks. 2631da177e4SLinus Torvalds */ 2647e40145eSAl Viro ret = acl_permission_check(inode, mask); 2655909ccaaSLinus Torvalds if (ret != -EACCES) 2665909ccaaSLinus Torvalds return ret; 2671da177e4SLinus Torvalds 268d594e7ecSAl Viro if (S_ISDIR(inode->i_mode)) { 269d594e7ecSAl Viro /* DACs are overridable for directories */ 2701a48e2acSEric W. Biederman if (inode_capable(inode, CAP_DAC_OVERRIDE)) 271d594e7ecSAl Viro return 0; 272d594e7ecSAl Viro if (!(mask & MAY_WRITE)) 2731a48e2acSEric W. Biederman if (inode_capable(inode, CAP_DAC_READ_SEARCH)) 274d594e7ecSAl Viro return 0; 275d594e7ecSAl Viro return -EACCES; 276d594e7ecSAl Viro } 2771da177e4SLinus Torvalds /* 2781da177e4SLinus Torvalds * Read/write DACs are always overridable. 279d594e7ecSAl Viro * Executable DACs are overridable when there is 280d594e7ecSAl Viro * at least one exec bit set. 2811da177e4SLinus Torvalds */ 282d594e7ecSAl Viro if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO)) 2831a48e2acSEric W. Biederman if (inode_capable(inode, CAP_DAC_OVERRIDE)) 2841da177e4SLinus Torvalds return 0; 2851da177e4SLinus Torvalds 2861da177e4SLinus Torvalds /* 2871da177e4SLinus Torvalds * Searching includes executable on directories, else just read. 2881da177e4SLinus Torvalds */ 2897ea66001SSerge E. Hallyn mask &= MAY_READ | MAY_WRITE | MAY_EXEC; 290d594e7ecSAl Viro if (mask == MAY_READ) 2911a48e2acSEric W. Biederman if (inode_capable(inode, CAP_DAC_READ_SEARCH)) 2921da177e4SLinus Torvalds return 0; 2931da177e4SLinus Torvalds 2941da177e4SLinus Torvalds return -EACCES; 2951da177e4SLinus Torvalds } 2961da177e4SLinus Torvalds 2973ddcd056SLinus Torvalds /* 2983ddcd056SLinus Torvalds * We _really_ want to just do "generic_permission()" without 2993ddcd056SLinus Torvalds * even looking at the inode->i_op values. So we keep a cache 3003ddcd056SLinus Torvalds * flag in inode->i_opflags, that says "this has not special 3013ddcd056SLinus Torvalds * permission function, use the fast case". 3023ddcd056SLinus Torvalds */ 3033ddcd056SLinus Torvalds static inline int do_inode_permission(struct inode *inode, int mask) 3043ddcd056SLinus Torvalds { 3053ddcd056SLinus Torvalds if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) { 3063ddcd056SLinus Torvalds if (likely(inode->i_op->permission)) 3073ddcd056SLinus Torvalds return inode->i_op->permission(inode, mask); 3083ddcd056SLinus Torvalds 3093ddcd056SLinus Torvalds /* This gets set once for the inode lifetime */ 3103ddcd056SLinus Torvalds spin_lock(&inode->i_lock); 3113ddcd056SLinus Torvalds inode->i_opflags |= IOP_FASTPERM; 3123ddcd056SLinus Torvalds spin_unlock(&inode->i_lock); 3133ddcd056SLinus Torvalds } 3143ddcd056SLinus Torvalds return generic_permission(inode, mask); 3153ddcd056SLinus Torvalds } 3163ddcd056SLinus Torvalds 317cb23beb5SChristoph Hellwig /** 318cb23beb5SChristoph Hellwig * inode_permission - check for access rights to a given inode 319cb23beb5SChristoph Hellwig * @inode: inode to check permission on 3208fd90c8dSAndreas Gruenbacher * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...) 321cb23beb5SChristoph Hellwig * 322cb23beb5SChristoph Hellwig * Used to check for read/write/execute permissions on an inode. 323cb23beb5SChristoph Hellwig * We use "fsuid" for this, letting us set arbitrary permissions 324cb23beb5SChristoph Hellwig * for filesystem access without changing the "normal" uids which 325cb23beb5SChristoph Hellwig * are used for other things. 326948409c7SAndreas Gruenbacher * 327948409c7SAndreas Gruenbacher * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask. 328cb23beb5SChristoph Hellwig */ 329f419a2e3SAl Viro int inode_permission(struct inode *inode, int mask) 3301da177e4SLinus Torvalds { 331e6305c43SAl Viro int retval; 3321da177e4SLinus Torvalds 3333ddcd056SLinus Torvalds if (unlikely(mask & MAY_WRITE)) { 33422590e41SMiklos Szeredi umode_t mode = inode->i_mode; 3351da177e4SLinus Torvalds 3361da177e4SLinus Torvalds /* 3371da177e4SLinus Torvalds * Nobody gets write access to a read-only fs. 3381da177e4SLinus Torvalds */ 3391da177e4SLinus Torvalds if (IS_RDONLY(inode) && 3401da177e4SLinus Torvalds (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) 3411da177e4SLinus Torvalds return -EROFS; 3421da177e4SLinus Torvalds 3431da177e4SLinus Torvalds /* 3441da177e4SLinus Torvalds * Nobody gets write access to an immutable file. 3451da177e4SLinus Torvalds */ 3461da177e4SLinus Torvalds if (IS_IMMUTABLE(inode)) 3471da177e4SLinus Torvalds return -EACCES; 3481da177e4SLinus Torvalds } 3491da177e4SLinus Torvalds 3503ddcd056SLinus Torvalds retval = do_inode_permission(inode, mask); 3511da177e4SLinus Torvalds if (retval) 3521da177e4SLinus Torvalds return retval; 3531da177e4SLinus Torvalds 35408ce5f16SSerge E. Hallyn retval = devcgroup_inode_permission(inode, mask); 35508ce5f16SSerge E. Hallyn if (retval) 35608ce5f16SSerge E. Hallyn return retval; 35708ce5f16SSerge E. Hallyn 358d09ca739SEric Paris return security_inode_permission(inode, mask); 3591da177e4SLinus Torvalds } 3601da177e4SLinus Torvalds 361f4d6ff89SAl Viro /** 3625dd784d0SJan Blunck * path_get - get a reference to a path 3635dd784d0SJan Blunck * @path: path to get the reference to 3645dd784d0SJan Blunck * 3655dd784d0SJan Blunck * Given a path increment the reference count to the dentry and the vfsmount. 3665dd784d0SJan Blunck */ 3675dd784d0SJan Blunck void path_get(struct path *path) 3685dd784d0SJan Blunck { 3695dd784d0SJan Blunck mntget(path->mnt); 3705dd784d0SJan Blunck dget(path->dentry); 3715dd784d0SJan Blunck } 3725dd784d0SJan Blunck EXPORT_SYMBOL(path_get); 3735dd784d0SJan Blunck 3745dd784d0SJan Blunck /** 3751d957f9bSJan Blunck * path_put - put a reference to a path 3761d957f9bSJan Blunck * @path: path to put the reference to 3771d957f9bSJan Blunck * 3781d957f9bSJan Blunck * Given a path decrement the reference count to the dentry and the vfsmount. 3791d957f9bSJan Blunck */ 3801d957f9bSJan Blunck void path_put(struct path *path) 3811da177e4SLinus Torvalds { 3821d957f9bSJan Blunck dput(path->dentry); 3831d957f9bSJan Blunck mntput(path->mnt); 3841da177e4SLinus Torvalds } 3851d957f9bSJan Blunck EXPORT_SYMBOL(path_put); 3861da177e4SLinus Torvalds 38719660af7SAl Viro /* 38831e6b01fSNick Piggin * Path walking has 2 modes, rcu-walk and ref-walk (see 38919660af7SAl Viro * Documentation/filesystems/path-lookup.txt). In situations when we can't 39019660af7SAl Viro * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab 39119660af7SAl Viro * normal reference counts on dentries and vfsmounts to transition to rcu-walk 39219660af7SAl Viro * mode. Refcounts are grabbed at the last known good point before rcu-walk 39319660af7SAl Viro * got stuck, so ref-walk may continue from there. If this is not successful 39419660af7SAl Viro * (eg. a seqcount has changed), then failure is returned and it's up to caller 39519660af7SAl Viro * to restart the path walk from the beginning in ref-walk mode. 39631e6b01fSNick Piggin */ 39731e6b01fSNick Piggin 39831e6b01fSNick Piggin /** 39919660af7SAl Viro * unlazy_walk - try to switch to ref-walk mode. 40019660af7SAl Viro * @nd: nameidata pathwalk data 40119660af7SAl Viro * @dentry: child of nd->path.dentry or NULL 40239191628SRandy Dunlap * Returns: 0 on success, -ECHILD on failure 40331e6b01fSNick Piggin * 40419660af7SAl Viro * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry 40519660af7SAl Viro * for ref-walk mode. @dentry must be a path found by a do_lookup call on 40619660af7SAl Viro * @nd or NULL. Must be called from rcu-walk context. 40731e6b01fSNick Piggin */ 40819660af7SAl Viro static int unlazy_walk(struct nameidata *nd, struct dentry *dentry) 40931e6b01fSNick Piggin { 41031e6b01fSNick Piggin struct fs_struct *fs = current->fs; 41131e6b01fSNick Piggin struct dentry *parent = nd->path.dentry; 4125b6ca027SAl Viro int want_root = 0; 41331e6b01fSNick Piggin 41431e6b01fSNick Piggin BUG_ON(!(nd->flags & LOOKUP_RCU)); 4155b6ca027SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) { 4165b6ca027SAl Viro want_root = 1; 41731e6b01fSNick Piggin spin_lock(&fs->lock); 41831e6b01fSNick Piggin if (nd->root.mnt != fs->root.mnt || 41931e6b01fSNick Piggin nd->root.dentry != fs->root.dentry) 42031e6b01fSNick Piggin goto err_root; 42131e6b01fSNick Piggin } 42231e6b01fSNick Piggin spin_lock(&parent->d_lock); 42319660af7SAl Viro if (!dentry) { 42419660af7SAl Viro if (!__d_rcu_to_refcount(parent, nd->seq)) 42519660af7SAl Viro goto err_parent; 42619660af7SAl Viro BUG_ON(nd->inode != parent->d_inode); 42719660af7SAl Viro } else { 42894c0d4ecSAl Viro if (dentry->d_parent != parent) 42994c0d4ecSAl Viro goto err_parent; 43031e6b01fSNick Piggin spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); 43131e6b01fSNick Piggin if (!__d_rcu_to_refcount(dentry, nd->seq)) 43219660af7SAl Viro goto err_child; 43331e6b01fSNick Piggin /* 43419660af7SAl Viro * If the sequence check on the child dentry passed, then 43519660af7SAl Viro * the child has not been removed from its parent. This 43619660af7SAl Viro * means the parent dentry must be valid and able to take 43719660af7SAl Viro * a reference at this point. 43831e6b01fSNick Piggin */ 43931e6b01fSNick Piggin BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent); 44031e6b01fSNick Piggin BUG_ON(!parent->d_count); 44131e6b01fSNick Piggin parent->d_count++; 44231e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 44319660af7SAl Viro } 44431e6b01fSNick Piggin spin_unlock(&parent->d_lock); 4455b6ca027SAl Viro if (want_root) { 44631e6b01fSNick Piggin path_get(&nd->root); 44731e6b01fSNick Piggin spin_unlock(&fs->lock); 44831e6b01fSNick Piggin } 44931e6b01fSNick Piggin mntget(nd->path.mnt); 45031e6b01fSNick Piggin 45131e6b01fSNick Piggin rcu_read_unlock(); 45231e6b01fSNick Piggin br_read_unlock(vfsmount_lock); 45331e6b01fSNick Piggin nd->flags &= ~LOOKUP_RCU; 45431e6b01fSNick Piggin return 0; 45519660af7SAl Viro 45619660af7SAl Viro err_child: 45731e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 45819660af7SAl Viro err_parent: 45931e6b01fSNick Piggin spin_unlock(&parent->d_lock); 46031e6b01fSNick Piggin err_root: 4615b6ca027SAl Viro if (want_root) 46231e6b01fSNick Piggin spin_unlock(&fs->lock); 46331e6b01fSNick Piggin return -ECHILD; 46431e6b01fSNick Piggin } 46531e6b01fSNick Piggin 46631e6b01fSNick Piggin /** 467834f2a4aSTrond Myklebust * release_open_intent - free up open intent resources 468834f2a4aSTrond Myklebust * @nd: pointer to nameidata 469834f2a4aSTrond Myklebust */ 470834f2a4aSTrond Myklebust void release_open_intent(struct nameidata *nd) 471834f2a4aSTrond Myklebust { 4722dab5974SLinus Torvalds struct file *file = nd->intent.open.file; 4732dab5974SLinus Torvalds 4742dab5974SLinus Torvalds if (file && !IS_ERR(file)) { 4752dab5974SLinus Torvalds if (file->f_path.dentry == NULL) 4762dab5974SLinus Torvalds put_filp(file); 477834f2a4aSTrond Myklebust else 4782dab5974SLinus Torvalds fput(file); 4792dab5974SLinus Torvalds } 480834f2a4aSTrond Myklebust } 481834f2a4aSTrond Myklebust 482f60aef7eSAl Viro static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd) 48334286d66SNick Piggin { 484f60aef7eSAl Viro return dentry->d_op->d_revalidate(dentry, nd); 48534286d66SNick Piggin } 48634286d66SNick Piggin 4879f1fafeeSAl Viro /** 4889f1fafeeSAl Viro * complete_walk - successful completion of path walk 4899f1fafeeSAl Viro * @nd: pointer nameidata 49039159de2SJeff Layton * 4919f1fafeeSAl Viro * If we had been in RCU mode, drop out of it and legitimize nd->path. 4929f1fafeeSAl Viro * Revalidate the final result, unless we'd already done that during 4939f1fafeeSAl Viro * the path walk or the filesystem doesn't ask for it. Return 0 on 4949f1fafeeSAl Viro * success, -error on failure. In case of failure caller does not 4959f1fafeeSAl Viro * need to drop nd->path. 49639159de2SJeff Layton */ 4979f1fafeeSAl Viro static int complete_walk(struct nameidata *nd) 49839159de2SJeff Layton { 49916c2cd71SAl Viro struct dentry *dentry = nd->path.dentry; 50039159de2SJeff Layton int status; 50139159de2SJeff Layton 5029f1fafeeSAl Viro if (nd->flags & LOOKUP_RCU) { 5039f1fafeeSAl Viro nd->flags &= ~LOOKUP_RCU; 5049f1fafeeSAl Viro if (!(nd->flags & LOOKUP_ROOT)) 5059f1fafeeSAl Viro nd->root.mnt = NULL; 5069f1fafeeSAl Viro spin_lock(&dentry->d_lock); 5079f1fafeeSAl Viro if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) { 5089f1fafeeSAl Viro spin_unlock(&dentry->d_lock); 5099f1fafeeSAl Viro rcu_read_unlock(); 5109f1fafeeSAl Viro br_read_unlock(vfsmount_lock); 5119f1fafeeSAl Viro return -ECHILD; 5129f1fafeeSAl Viro } 5139f1fafeeSAl Viro BUG_ON(nd->inode != dentry->d_inode); 5149f1fafeeSAl Viro spin_unlock(&dentry->d_lock); 5159f1fafeeSAl Viro mntget(nd->path.mnt); 5169f1fafeeSAl Viro rcu_read_unlock(); 5179f1fafeeSAl Viro br_read_unlock(vfsmount_lock); 5189f1fafeeSAl Viro } 5199f1fafeeSAl Viro 52016c2cd71SAl Viro if (likely(!(nd->flags & LOOKUP_JUMPED))) 52139159de2SJeff Layton return 0; 52239159de2SJeff Layton 52316c2cd71SAl Viro if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE))) 52416c2cd71SAl Viro return 0; 52516c2cd71SAl Viro 52616c2cd71SAl Viro if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT))) 52716c2cd71SAl Viro return 0; 52816c2cd71SAl Viro 52916c2cd71SAl Viro /* Note: we do not d_invalidate() */ 53034286d66SNick Piggin status = d_revalidate(dentry, nd); 53139159de2SJeff Layton if (status > 0) 53239159de2SJeff Layton return 0; 53339159de2SJeff Layton 53416c2cd71SAl Viro if (!status) 53539159de2SJeff Layton status = -ESTALE; 53616c2cd71SAl Viro 5379f1fafeeSAl Viro path_put(&nd->path); 53839159de2SJeff Layton return status; 53939159de2SJeff Layton } 54039159de2SJeff Layton 5412a737871SAl Viro static __always_inline void set_root(struct nameidata *nd) 5422a737871SAl Viro { 543f7ad3c6bSMiklos Szeredi if (!nd->root.mnt) 544f7ad3c6bSMiklos Szeredi get_fs_root(current->fs, &nd->root); 5452a737871SAl Viro } 5462a737871SAl Viro 5476de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *); 5486de88d72SAl Viro 54931e6b01fSNick Piggin static __always_inline void set_root_rcu(struct nameidata *nd) 55031e6b01fSNick Piggin { 55131e6b01fSNick Piggin if (!nd->root.mnt) { 55231e6b01fSNick Piggin struct fs_struct *fs = current->fs; 553c28cc364SNick Piggin unsigned seq; 554c28cc364SNick Piggin 555c28cc364SNick Piggin do { 556c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 55731e6b01fSNick Piggin nd->root = fs->root; 558c1530019STim Chen nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq); 559c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 56031e6b01fSNick Piggin } 56131e6b01fSNick Piggin } 56231e6b01fSNick Piggin 563f1662356SArjan van de Ven static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link) 5641da177e4SLinus Torvalds { 56531e6b01fSNick Piggin int ret; 56631e6b01fSNick Piggin 5671da177e4SLinus Torvalds if (IS_ERR(link)) 5681da177e4SLinus Torvalds goto fail; 5691da177e4SLinus Torvalds 5701da177e4SLinus Torvalds if (*link == '/') { 5712a737871SAl Viro set_root(nd); 5721d957f9bSJan Blunck path_put(&nd->path); 5732a737871SAl Viro nd->path = nd->root; 5742a737871SAl Viro path_get(&nd->root); 57516c2cd71SAl Viro nd->flags |= LOOKUP_JUMPED; 5761da177e4SLinus Torvalds } 57731e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 578b4091d5fSChristoph Hellwig 57931e6b01fSNick Piggin ret = link_path_walk(link, nd); 58031e6b01fSNick Piggin return ret; 5811da177e4SLinus Torvalds fail: 5821d957f9bSJan Blunck path_put(&nd->path); 5831da177e4SLinus Torvalds return PTR_ERR(link); 5841da177e4SLinus Torvalds } 5851da177e4SLinus Torvalds 5861d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd) 587051d3812SIan Kent { 588051d3812SIan Kent dput(path->dentry); 5894ac91378SJan Blunck if (path->mnt != nd->path.mnt) 590051d3812SIan Kent mntput(path->mnt); 591051d3812SIan Kent } 592051d3812SIan Kent 5937b9337aaSNick Piggin static inline void path_to_nameidata(const struct path *path, 5947b9337aaSNick Piggin struct nameidata *nd) 595051d3812SIan Kent { 59631e6b01fSNick Piggin if (!(nd->flags & LOOKUP_RCU)) { 5974ac91378SJan Blunck dput(nd->path.dentry); 59831e6b01fSNick Piggin if (nd->path.mnt != path->mnt) 5994ac91378SJan Blunck mntput(nd->path.mnt); 6009a229683SHuang Shijie } 60131e6b01fSNick Piggin nd->path.mnt = path->mnt; 6024ac91378SJan Blunck nd->path.dentry = path->dentry; 603051d3812SIan Kent } 604051d3812SIan Kent 605574197e0SAl Viro static inline void put_link(struct nameidata *nd, struct path *link, void *cookie) 606574197e0SAl Viro { 607574197e0SAl Viro struct inode *inode = link->dentry->d_inode; 608574197e0SAl Viro if (!IS_ERR(cookie) && inode->i_op->put_link) 609574197e0SAl Viro inode->i_op->put_link(link->dentry, nd, cookie); 610574197e0SAl Viro path_put(link); 611574197e0SAl Viro } 612574197e0SAl Viro 613def4af30SAl Viro static __always_inline int 614574197e0SAl Viro follow_link(struct path *link, struct nameidata *nd, void **p) 6151da177e4SLinus Torvalds { 6161da177e4SLinus Torvalds int error; 6177b9337aaSNick Piggin struct dentry *dentry = link->dentry; 6181da177e4SLinus Torvalds 619844a3917SAl Viro BUG_ON(nd->flags & LOOKUP_RCU); 620844a3917SAl Viro 6210e794589SAl Viro if (link->mnt == nd->path.mnt) 6220e794589SAl Viro mntget(link->mnt); 6230e794589SAl Viro 624574197e0SAl Viro if (unlikely(current->total_link_count >= 40)) { 625574197e0SAl Viro *p = ERR_PTR(-ELOOP); /* no ->put_link(), please */ 626574197e0SAl Viro path_put(&nd->path); 627574197e0SAl Viro return -ELOOP; 628574197e0SAl Viro } 629574197e0SAl Viro cond_resched(); 630574197e0SAl Viro current->total_link_count++; 631574197e0SAl Viro 63268ac1234SAl Viro touch_atime(link); 6331da177e4SLinus Torvalds nd_set_link(nd, NULL); 634cd4e91d3SAl Viro 63536f3b4f6SAl Viro error = security_inode_follow_link(link->dentry, nd); 63636f3b4f6SAl Viro if (error) { 63736f3b4f6SAl Viro *p = ERR_PTR(error); /* no ->put_link(), please */ 63836f3b4f6SAl Viro path_put(&nd->path); 63936f3b4f6SAl Viro return error; 64036f3b4f6SAl Viro } 64136f3b4f6SAl Viro 64286acdca1SAl Viro nd->last_type = LAST_BIND; 643def4af30SAl Viro *p = dentry->d_inode->i_op->follow_link(dentry, nd); 644def4af30SAl Viro error = PTR_ERR(*p); 645def4af30SAl Viro if (!IS_ERR(*p)) { 6461da177e4SLinus Torvalds char *s = nd_get_link(nd); 647cc314eefSLinus Torvalds error = 0; 6481da177e4SLinus Torvalds if (s) 6491da177e4SLinus Torvalds error = __vfs_follow_link(nd, s); 650bcda7652SAl Viro else if (nd->last_type == LAST_BIND) { 65116c2cd71SAl Viro nd->flags |= LOOKUP_JUMPED; 652b21041d0SAl Viro nd->inode = nd->path.dentry->d_inode; 653b21041d0SAl Viro if (nd->inode->i_op->follow_link) { 654bcda7652SAl Viro /* stepped on a _really_ weird one */ 655bcda7652SAl Viro path_put(&nd->path); 656bcda7652SAl Viro error = -ELOOP; 657bcda7652SAl Viro } 658bcda7652SAl Viro } 6591da177e4SLinus Torvalds } 6601da177e4SLinus Torvalds return error; 6611da177e4SLinus Torvalds } 6621da177e4SLinus Torvalds 66331e6b01fSNick Piggin static int follow_up_rcu(struct path *path) 66431e6b01fSNick Piggin { 6650714a533SAl Viro struct mount *mnt = real_mount(path->mnt); 6660714a533SAl Viro struct mount *parent; 66731e6b01fSNick Piggin struct dentry *mountpoint; 66831e6b01fSNick Piggin 6690714a533SAl Viro parent = mnt->mnt_parent; 6700714a533SAl Viro if (&parent->mnt == path->mnt) 67131e6b01fSNick Piggin return 0; 672a73324daSAl Viro mountpoint = mnt->mnt_mountpoint; 67331e6b01fSNick Piggin path->dentry = mountpoint; 6740714a533SAl Viro path->mnt = &parent->mnt; 67531e6b01fSNick Piggin return 1; 67631e6b01fSNick Piggin } 67731e6b01fSNick Piggin 678bab77ebfSAl Viro int follow_up(struct path *path) 6791da177e4SLinus Torvalds { 6800714a533SAl Viro struct mount *mnt = real_mount(path->mnt); 6810714a533SAl Viro struct mount *parent; 6821da177e4SLinus Torvalds struct dentry *mountpoint; 68399b7db7bSNick Piggin 68499b7db7bSNick Piggin br_read_lock(vfsmount_lock); 6850714a533SAl Viro parent = mnt->mnt_parent; 6860714a533SAl Viro if (&parent->mnt == path->mnt) { 68799b7db7bSNick Piggin br_read_unlock(vfsmount_lock); 6881da177e4SLinus Torvalds return 0; 6891da177e4SLinus Torvalds } 6900714a533SAl Viro mntget(&parent->mnt); 691a73324daSAl Viro mountpoint = dget(mnt->mnt_mountpoint); 69299b7db7bSNick Piggin br_read_unlock(vfsmount_lock); 693bab77ebfSAl Viro dput(path->dentry); 694bab77ebfSAl Viro path->dentry = mountpoint; 695bab77ebfSAl Viro mntput(path->mnt); 6960714a533SAl Viro path->mnt = &parent->mnt; 6971da177e4SLinus Torvalds return 1; 6981da177e4SLinus Torvalds } 6991da177e4SLinus Torvalds 700b5c84bf6SNick Piggin /* 7019875cf80SDavid Howells * Perform an automount 7029875cf80SDavid Howells * - return -EISDIR to tell follow_managed() to stop and return the path we 7039875cf80SDavid Howells * were called with. 7041da177e4SLinus Torvalds */ 7059875cf80SDavid Howells static int follow_automount(struct path *path, unsigned flags, 7069875cf80SDavid Howells bool *need_mntput) 70731e6b01fSNick Piggin { 7089875cf80SDavid Howells struct vfsmount *mnt; 709ea5b778aSDavid Howells int err; 7109875cf80SDavid Howells 7119875cf80SDavid Howells if (!path->dentry->d_op || !path->dentry->d_op->d_automount) 7129875cf80SDavid Howells return -EREMOTE; 7139875cf80SDavid Howells 7140ec26fd0SMiklos Szeredi /* We don't want to mount if someone's just doing a stat - 7150ec26fd0SMiklos Szeredi * unless they're stat'ing a directory and appended a '/' to 7160ec26fd0SMiklos Szeredi * the name. 7170ec26fd0SMiklos Szeredi * 7180ec26fd0SMiklos Szeredi * We do, however, want to mount if someone wants to open or 7195a30d8a2SDavid Howells * create a file of any type under the mountpoint, wants to 7200ec26fd0SMiklos Szeredi * traverse through the mountpoint or wants to open the 7210ec26fd0SMiklos Szeredi * mounted directory. Also, autofs may mark negative dentries 7220ec26fd0SMiklos Szeredi * as being automount points. These will need the attentions 7230ec26fd0SMiklos Szeredi * of the daemon to instantiate them before they can be used. 7245a30d8a2SDavid Howells */ 7255a30d8a2SDavid Howells if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY | 726d94c177bSLinus Torvalds LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) && 7275a30d8a2SDavid Howells path->dentry->d_inode) 7289875cf80SDavid Howells return -EISDIR; 7290ec26fd0SMiklos Szeredi 7309875cf80SDavid Howells current->total_link_count++; 7319875cf80SDavid Howells if (current->total_link_count >= 40) 7329875cf80SDavid Howells return -ELOOP; 7339875cf80SDavid Howells 7349875cf80SDavid Howells mnt = path->dentry->d_op->d_automount(path); 7359875cf80SDavid Howells if (IS_ERR(mnt)) { 7369875cf80SDavid Howells /* 7379875cf80SDavid Howells * The filesystem is allowed to return -EISDIR here to indicate 7389875cf80SDavid Howells * it doesn't want to automount. For instance, autofs would do 7399875cf80SDavid Howells * this so that its userspace daemon can mount on this dentry. 7409875cf80SDavid Howells * 7419875cf80SDavid Howells * However, we can only permit this if it's a terminal point in 7429875cf80SDavid Howells * the path being looked up; if it wasn't then the remainder of 7439875cf80SDavid Howells * the path is inaccessible and we should say so. 7449875cf80SDavid Howells */ 74549084c3bSAl Viro if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT)) 7469875cf80SDavid Howells return -EREMOTE; 7479875cf80SDavid Howells return PTR_ERR(mnt); 74831e6b01fSNick Piggin } 749ea5b778aSDavid Howells 7509875cf80SDavid Howells if (!mnt) /* mount collision */ 7519875cf80SDavid Howells return 0; 7529875cf80SDavid Howells 7538aef1884SAl Viro if (!*need_mntput) { 7548aef1884SAl Viro /* lock_mount() may release path->mnt on error */ 7558aef1884SAl Viro mntget(path->mnt); 7568aef1884SAl Viro *need_mntput = true; 7578aef1884SAl Viro } 75819a167afSAl Viro err = finish_automount(mnt, path); 759ea5b778aSDavid Howells 760ea5b778aSDavid Howells switch (err) { 761ea5b778aSDavid Howells case -EBUSY: 762ea5b778aSDavid Howells /* Someone else made a mount here whilst we were busy */ 76319a167afSAl Viro return 0; 764ea5b778aSDavid Howells case 0: 7658aef1884SAl Viro path_put(path); 7669875cf80SDavid Howells path->mnt = mnt; 7679875cf80SDavid Howells path->dentry = dget(mnt->mnt_root); 7689875cf80SDavid Howells return 0; 76919a167afSAl Viro default: 77019a167afSAl Viro return err; 7719875cf80SDavid Howells } 77219a167afSAl Viro 773ea5b778aSDavid Howells } 7749875cf80SDavid Howells 7759875cf80SDavid Howells /* 7769875cf80SDavid Howells * Handle a dentry that is managed in some way. 777cc53ce53SDavid Howells * - Flagged for transit management (autofs) 7789875cf80SDavid Howells * - Flagged as mountpoint 7799875cf80SDavid Howells * - Flagged as automount point 7809875cf80SDavid Howells * 7819875cf80SDavid Howells * This may only be called in refwalk mode. 7829875cf80SDavid Howells * 7839875cf80SDavid Howells * Serialization is taken care of in namespace.c 7849875cf80SDavid Howells */ 7859875cf80SDavid Howells static int follow_managed(struct path *path, unsigned flags) 7869875cf80SDavid Howells { 7878aef1884SAl Viro struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */ 7889875cf80SDavid Howells unsigned managed; 7899875cf80SDavid Howells bool need_mntput = false; 7908aef1884SAl Viro int ret = 0; 7919875cf80SDavid Howells 7929875cf80SDavid Howells /* Given that we're not holding a lock here, we retain the value in a 7939875cf80SDavid Howells * local variable for each dentry as we look at it so that we don't see 7949875cf80SDavid Howells * the components of that value change under us */ 7959875cf80SDavid Howells while (managed = ACCESS_ONCE(path->dentry->d_flags), 7969875cf80SDavid Howells managed &= DCACHE_MANAGED_DENTRY, 7979875cf80SDavid Howells unlikely(managed != 0)) { 798cc53ce53SDavid Howells /* Allow the filesystem to manage the transit without i_mutex 799cc53ce53SDavid Howells * being held. */ 800cc53ce53SDavid Howells if (managed & DCACHE_MANAGE_TRANSIT) { 801cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op); 802cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op->d_manage); 8031aed3e42SAl Viro ret = path->dentry->d_op->d_manage(path->dentry, false); 804cc53ce53SDavid Howells if (ret < 0) 8058aef1884SAl Viro break; 806cc53ce53SDavid Howells } 807cc53ce53SDavid Howells 8089875cf80SDavid Howells /* Transit to a mounted filesystem. */ 8099875cf80SDavid Howells if (managed & DCACHE_MOUNTED) { 8109875cf80SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 8119875cf80SDavid Howells if (mounted) { 8129875cf80SDavid Howells dput(path->dentry); 8139875cf80SDavid Howells if (need_mntput) 814463ffb2eSAl Viro mntput(path->mnt); 815463ffb2eSAl Viro path->mnt = mounted; 816463ffb2eSAl Viro path->dentry = dget(mounted->mnt_root); 8179875cf80SDavid Howells need_mntput = true; 8189875cf80SDavid Howells continue; 819463ffb2eSAl Viro } 820463ffb2eSAl Viro 8219875cf80SDavid Howells /* Something is mounted on this dentry in another 8229875cf80SDavid Howells * namespace and/or whatever was mounted there in this 8239875cf80SDavid Howells * namespace got unmounted before we managed to get the 8249875cf80SDavid Howells * vfsmount_lock */ 8251da177e4SLinus Torvalds } 8269875cf80SDavid Howells 8279875cf80SDavid Howells /* Handle an automount point */ 8289875cf80SDavid Howells if (managed & DCACHE_NEED_AUTOMOUNT) { 8299875cf80SDavid Howells ret = follow_automount(path, flags, &need_mntput); 8309875cf80SDavid Howells if (ret < 0) 8318aef1884SAl Viro break; 8329875cf80SDavid Howells continue; 8339875cf80SDavid Howells } 8349875cf80SDavid Howells 8359875cf80SDavid Howells /* We didn't change the current path point */ 8369875cf80SDavid Howells break; 8379875cf80SDavid Howells } 8388aef1884SAl Viro 8398aef1884SAl Viro if (need_mntput && path->mnt == mnt) 8408aef1884SAl Viro mntput(path->mnt); 8418aef1884SAl Viro if (ret == -EISDIR) 8428aef1884SAl Viro ret = 0; 843a3fbbde7SAl Viro return ret < 0 ? ret : need_mntput; 8441da177e4SLinus Torvalds } 8451da177e4SLinus Torvalds 846cc53ce53SDavid Howells int follow_down_one(struct path *path) 8471da177e4SLinus Torvalds { 8481da177e4SLinus Torvalds struct vfsmount *mounted; 8491da177e4SLinus Torvalds 8501c755af4SAl Viro mounted = lookup_mnt(path); 8511da177e4SLinus Torvalds if (mounted) { 8529393bd07SAl Viro dput(path->dentry); 8539393bd07SAl Viro mntput(path->mnt); 8549393bd07SAl Viro path->mnt = mounted; 8559393bd07SAl Viro path->dentry = dget(mounted->mnt_root); 8561da177e4SLinus Torvalds return 1; 8571da177e4SLinus Torvalds } 8581da177e4SLinus Torvalds return 0; 8591da177e4SLinus Torvalds } 8601da177e4SLinus Torvalds 86162a7375eSIan Kent static inline bool managed_dentry_might_block(struct dentry *dentry) 86262a7375eSIan Kent { 86362a7375eSIan Kent return (dentry->d_flags & DCACHE_MANAGE_TRANSIT && 86462a7375eSIan Kent dentry->d_op->d_manage(dentry, true) < 0); 86562a7375eSIan Kent } 86662a7375eSIan Kent 8679875cf80SDavid Howells /* 868287548e4SAl Viro * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if 869287548e4SAl Viro * we meet a managed dentry that would need blocking. 8709875cf80SDavid Howells */ 8719875cf80SDavid Howells static bool __follow_mount_rcu(struct nameidata *nd, struct path *path, 872287548e4SAl Viro struct inode **inode) 8739875cf80SDavid Howells { 87462a7375eSIan Kent for (;;) { 875c7105365SAl Viro struct mount *mounted; 87662a7375eSIan Kent /* 87762a7375eSIan Kent * Don't forget we might have a non-mountpoint managed dentry 87862a7375eSIan Kent * that wants to block transit. 87962a7375eSIan Kent */ 880287548e4SAl Viro if (unlikely(managed_dentry_might_block(path->dentry))) 881ab90911fSDavid Howells return false; 88262a7375eSIan Kent 88362a7375eSIan Kent if (!d_mountpoint(path->dentry)) 88462a7375eSIan Kent break; 88562a7375eSIan Kent 8869875cf80SDavid Howells mounted = __lookup_mnt(path->mnt, path->dentry, 1); 8879875cf80SDavid Howells if (!mounted) 8889875cf80SDavid Howells break; 889c7105365SAl Viro path->mnt = &mounted->mnt; 890c7105365SAl Viro path->dentry = mounted->mnt.mnt_root; 891a3fbbde7SAl Viro nd->flags |= LOOKUP_JUMPED; 8929875cf80SDavid Howells nd->seq = read_seqcount_begin(&path->dentry->d_seq); 89359430262SLinus Torvalds /* 89459430262SLinus Torvalds * Update the inode too. We don't need to re-check the 89559430262SLinus Torvalds * dentry sequence number here after this d_inode read, 89659430262SLinus Torvalds * because a mount-point is always pinned. 89759430262SLinus Torvalds */ 89859430262SLinus Torvalds *inode = path->dentry->d_inode; 8999875cf80SDavid Howells } 9009875cf80SDavid Howells return true; 9019875cf80SDavid Howells } 9029875cf80SDavid Howells 903dea39376SAl Viro static void follow_mount_rcu(struct nameidata *nd) 904287548e4SAl Viro { 905dea39376SAl Viro while (d_mountpoint(nd->path.dentry)) { 906c7105365SAl Viro struct mount *mounted; 907dea39376SAl Viro mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1); 908287548e4SAl Viro if (!mounted) 909287548e4SAl Viro break; 910c7105365SAl Viro nd->path.mnt = &mounted->mnt; 911c7105365SAl Viro nd->path.dentry = mounted->mnt.mnt_root; 912dea39376SAl Viro nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); 913287548e4SAl Viro } 914287548e4SAl Viro } 915287548e4SAl Viro 91631e6b01fSNick Piggin static int follow_dotdot_rcu(struct nameidata *nd) 91731e6b01fSNick Piggin { 91831e6b01fSNick Piggin set_root_rcu(nd); 91931e6b01fSNick Piggin 92031e6b01fSNick Piggin while (1) { 92131e6b01fSNick Piggin if (nd->path.dentry == nd->root.dentry && 92231e6b01fSNick Piggin nd->path.mnt == nd->root.mnt) { 92331e6b01fSNick Piggin break; 92431e6b01fSNick Piggin } 92531e6b01fSNick Piggin if (nd->path.dentry != nd->path.mnt->mnt_root) { 92631e6b01fSNick Piggin struct dentry *old = nd->path.dentry; 92731e6b01fSNick Piggin struct dentry *parent = old->d_parent; 92831e6b01fSNick Piggin unsigned seq; 92931e6b01fSNick Piggin 93031e6b01fSNick Piggin seq = read_seqcount_begin(&parent->d_seq); 93131e6b01fSNick Piggin if (read_seqcount_retry(&old->d_seq, nd->seq)) 932ef7562d5SAl Viro goto failed; 93331e6b01fSNick Piggin nd->path.dentry = parent; 93431e6b01fSNick Piggin nd->seq = seq; 93531e6b01fSNick Piggin break; 93631e6b01fSNick Piggin } 93731e6b01fSNick Piggin if (!follow_up_rcu(&nd->path)) 93831e6b01fSNick Piggin break; 93931e6b01fSNick Piggin nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); 94031e6b01fSNick Piggin } 941dea39376SAl Viro follow_mount_rcu(nd); 942dea39376SAl Viro nd->inode = nd->path.dentry->d_inode; 94331e6b01fSNick Piggin return 0; 944ef7562d5SAl Viro 945ef7562d5SAl Viro failed: 946ef7562d5SAl Viro nd->flags &= ~LOOKUP_RCU; 9475b6ca027SAl Viro if (!(nd->flags & LOOKUP_ROOT)) 948ef7562d5SAl Viro nd->root.mnt = NULL; 949ef7562d5SAl Viro rcu_read_unlock(); 950ef7562d5SAl Viro br_read_unlock(vfsmount_lock); 951ef7562d5SAl Viro return -ECHILD; 95231e6b01fSNick Piggin } 95331e6b01fSNick Piggin 9549875cf80SDavid Howells /* 955cc53ce53SDavid Howells * Follow down to the covering mount currently visible to userspace. At each 956cc53ce53SDavid Howells * point, the filesystem owning that dentry may be queried as to whether the 957cc53ce53SDavid Howells * caller is permitted to proceed or not. 958cc53ce53SDavid Howells */ 9597cc90cc3SAl Viro int follow_down(struct path *path) 960cc53ce53SDavid Howells { 961cc53ce53SDavid Howells unsigned managed; 962cc53ce53SDavid Howells int ret; 963cc53ce53SDavid Howells 964cc53ce53SDavid Howells while (managed = ACCESS_ONCE(path->dentry->d_flags), 965cc53ce53SDavid Howells unlikely(managed & DCACHE_MANAGED_DENTRY)) { 966cc53ce53SDavid Howells /* Allow the filesystem to manage the transit without i_mutex 967cc53ce53SDavid Howells * being held. 968cc53ce53SDavid Howells * 969cc53ce53SDavid Howells * We indicate to the filesystem if someone is trying to mount 970cc53ce53SDavid Howells * something here. This gives autofs the chance to deny anyone 971cc53ce53SDavid Howells * other than its daemon the right to mount on its 972cc53ce53SDavid Howells * superstructure. 973cc53ce53SDavid Howells * 974cc53ce53SDavid Howells * The filesystem may sleep at this point. 975cc53ce53SDavid Howells */ 976cc53ce53SDavid Howells if (managed & DCACHE_MANAGE_TRANSIT) { 977cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op); 978cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op->d_manage); 979ab90911fSDavid Howells ret = path->dentry->d_op->d_manage( 9801aed3e42SAl Viro path->dentry, false); 981cc53ce53SDavid Howells if (ret < 0) 982cc53ce53SDavid Howells return ret == -EISDIR ? 0 : ret; 983cc53ce53SDavid Howells } 984cc53ce53SDavid Howells 985cc53ce53SDavid Howells /* Transit to a mounted filesystem. */ 986cc53ce53SDavid Howells if (managed & DCACHE_MOUNTED) { 987cc53ce53SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 988cc53ce53SDavid Howells if (!mounted) 989cc53ce53SDavid Howells break; 990cc53ce53SDavid Howells dput(path->dentry); 991cc53ce53SDavid Howells mntput(path->mnt); 992cc53ce53SDavid Howells path->mnt = mounted; 993cc53ce53SDavid Howells path->dentry = dget(mounted->mnt_root); 994cc53ce53SDavid Howells continue; 995cc53ce53SDavid Howells } 996cc53ce53SDavid Howells 997cc53ce53SDavid Howells /* Don't handle automount points here */ 998cc53ce53SDavid Howells break; 999cc53ce53SDavid Howells } 1000cc53ce53SDavid Howells return 0; 1001cc53ce53SDavid Howells } 1002cc53ce53SDavid Howells 1003cc53ce53SDavid Howells /* 10049875cf80SDavid Howells * Skip to top of mountpoint pile in refwalk mode for follow_dotdot() 10059875cf80SDavid Howells */ 10069875cf80SDavid Howells static void follow_mount(struct path *path) 10079875cf80SDavid Howells { 10089875cf80SDavid Howells while (d_mountpoint(path->dentry)) { 10099875cf80SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 10109875cf80SDavid Howells if (!mounted) 10119875cf80SDavid Howells break; 10129875cf80SDavid Howells dput(path->dentry); 10139875cf80SDavid Howells mntput(path->mnt); 10149875cf80SDavid Howells path->mnt = mounted; 10159875cf80SDavid Howells path->dentry = dget(mounted->mnt_root); 10169875cf80SDavid Howells } 10179875cf80SDavid Howells } 10189875cf80SDavid Howells 101931e6b01fSNick Piggin static void follow_dotdot(struct nameidata *nd) 10201da177e4SLinus Torvalds { 10212a737871SAl Viro set_root(nd); 1022e518ddb7SAndreas Mohr 10231da177e4SLinus Torvalds while(1) { 10244ac91378SJan Blunck struct dentry *old = nd->path.dentry; 10251da177e4SLinus Torvalds 10262a737871SAl Viro if (nd->path.dentry == nd->root.dentry && 10272a737871SAl Viro nd->path.mnt == nd->root.mnt) { 10281da177e4SLinus Torvalds break; 10291da177e4SLinus Torvalds } 10304ac91378SJan Blunck if (nd->path.dentry != nd->path.mnt->mnt_root) { 10313088dd70SAl Viro /* rare case of legitimate dget_parent()... */ 10323088dd70SAl Viro nd->path.dentry = dget_parent(nd->path.dentry); 10331da177e4SLinus Torvalds dput(old); 10341da177e4SLinus Torvalds break; 10351da177e4SLinus Torvalds } 10363088dd70SAl Viro if (!follow_up(&nd->path)) 10371da177e4SLinus Torvalds break; 10381da177e4SLinus Torvalds } 103979ed0226SAl Viro follow_mount(&nd->path); 104031e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 10411da177e4SLinus Torvalds } 10421da177e4SLinus Torvalds 10431da177e4SLinus Torvalds /* 1044bad61189SMiklos Szeredi * This looks up the name in dcache, possibly revalidates the old dentry and 1045bad61189SMiklos Szeredi * allocates a new one if not found or not valid. In the need_lookup argument 1046bad61189SMiklos Szeredi * returns whether i_op->lookup is necessary. 1047bad61189SMiklos Szeredi * 1048bad61189SMiklos Szeredi * dir->d_inode->i_mutex must be held 1049baa03890SNick Piggin */ 1050bad61189SMiklos Szeredi static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir, 1051bad61189SMiklos Szeredi struct nameidata *nd, bool *need_lookup) 1052baa03890SNick Piggin { 1053baa03890SNick Piggin struct dentry *dentry; 1054bad61189SMiklos Szeredi int error; 1055baa03890SNick Piggin 1056bad61189SMiklos Szeredi *need_lookup = false; 1057bad61189SMiklos Szeredi dentry = d_lookup(dir, name); 1058bad61189SMiklos Szeredi if (dentry) { 1059bad61189SMiklos Szeredi if (d_need_lookup(dentry)) { 1060bad61189SMiklos Szeredi *need_lookup = true; 1061bad61189SMiklos Szeredi } else if (dentry->d_flags & DCACHE_OP_REVALIDATE) { 1062bad61189SMiklos Szeredi error = d_revalidate(dentry, nd); 1063bad61189SMiklos Szeredi if (unlikely(error <= 0)) { 1064bad61189SMiklos Szeredi if (error < 0) { 1065bad61189SMiklos Szeredi dput(dentry); 1066bad61189SMiklos Szeredi return ERR_PTR(error); 1067bad61189SMiklos Szeredi } else if (!d_invalidate(dentry)) { 1068bad61189SMiklos Szeredi dput(dentry); 1069bad61189SMiklos Szeredi dentry = NULL; 1070bad61189SMiklos Szeredi } 1071bad61189SMiklos Szeredi } 1072bad61189SMiklos Szeredi } 1073bad61189SMiklos Szeredi } 1074baa03890SNick Piggin 1075bad61189SMiklos Szeredi if (!dentry) { 1076bad61189SMiklos Szeredi dentry = d_alloc(dir, name); 1077baa03890SNick Piggin if (unlikely(!dentry)) 1078baa03890SNick Piggin return ERR_PTR(-ENOMEM); 1079baa03890SNick Piggin 1080bad61189SMiklos Szeredi *need_lookup = true; 1081baa03890SNick Piggin } 1082baa03890SNick Piggin return dentry; 1083baa03890SNick Piggin } 1084baa03890SNick Piggin 1085baa03890SNick Piggin /* 1086bad61189SMiklos Szeredi * Call i_op->lookup on the dentry. The dentry must be negative but may be 1087bad61189SMiklos Szeredi * hashed if it was pouplated with DCACHE_NEED_LOOKUP. 1088bad61189SMiklos Szeredi * 1089bad61189SMiklos Szeredi * dir->d_inode->i_mutex must be held 109044396f4bSJosef Bacik */ 1091bad61189SMiklos Szeredi static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry, 109244396f4bSJosef Bacik struct nameidata *nd) 109344396f4bSJosef Bacik { 109444396f4bSJosef Bacik struct dentry *old; 109544396f4bSJosef Bacik 109644396f4bSJosef Bacik /* Don't create child dentry for a dead directory. */ 1097bad61189SMiklos Szeredi if (unlikely(IS_DEADDIR(dir))) { 1098e188dc02SMiklos Szeredi dput(dentry); 109944396f4bSJosef Bacik return ERR_PTR(-ENOENT); 1100e188dc02SMiklos Szeredi } 110144396f4bSJosef Bacik 1102bad61189SMiklos Szeredi old = dir->i_op->lookup(dir, dentry, nd); 110344396f4bSJosef Bacik if (unlikely(old)) { 110444396f4bSJosef Bacik dput(dentry); 110544396f4bSJosef Bacik dentry = old; 110644396f4bSJosef Bacik } 110744396f4bSJosef Bacik return dentry; 110844396f4bSJosef Bacik } 110944396f4bSJosef Bacik 1110a3255546SAl Viro static struct dentry *__lookup_hash(struct qstr *name, 1111a3255546SAl Viro struct dentry *base, struct nameidata *nd) 1112a3255546SAl Viro { 1113bad61189SMiklos Szeredi bool need_lookup; 1114a3255546SAl Viro struct dentry *dentry; 1115a3255546SAl Viro 1116bad61189SMiklos Szeredi dentry = lookup_dcache(name, base, nd, &need_lookup); 1117bad61189SMiklos Szeredi if (!need_lookup) 1118a3255546SAl Viro return dentry; 1119bad61189SMiklos Szeredi 1120bad61189SMiklos Szeredi return lookup_real(base->d_inode, dentry, nd); 1121a3255546SAl Viro } 1122a3255546SAl Viro 112344396f4bSJosef Bacik /* 11241da177e4SLinus Torvalds * It's more convoluted than I'd like it to be, but... it's still fairly 11251da177e4SLinus Torvalds * small and for now I'd prefer to have fast path as straight as possible. 11261da177e4SLinus Torvalds * It _is_ time-critical. 11271da177e4SLinus Torvalds */ 11281da177e4SLinus Torvalds static int do_lookup(struct nameidata *nd, struct qstr *name, 112931e6b01fSNick Piggin struct path *path, struct inode **inode) 11301da177e4SLinus Torvalds { 11314ac91378SJan Blunck struct vfsmount *mnt = nd->path.mnt; 113231e6b01fSNick Piggin struct dentry *dentry, *parent = nd->path.dentry; 11335a18fff2SAl Viro int need_reval = 1; 11345a18fff2SAl Viro int status = 1; 11359875cf80SDavid Howells int err; 11369875cf80SDavid Howells 11373cac260aSAl Viro /* 1138b04f784eSNick Piggin * Rename seqlock is not required here because in the off chance 1139b04f784eSNick Piggin * of a false negative due to a concurrent rename, we're going to 1140b04f784eSNick Piggin * do the non-racy lookup, below. 1141b04f784eSNick Piggin */ 114231e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) { 114331e6b01fSNick Piggin unsigned seq; 114412f8ad4bSLinus Torvalds dentry = __d_lookup_rcu(parent, name, &seq, nd->inode); 11455a18fff2SAl Viro if (!dentry) 11465a18fff2SAl Viro goto unlazy; 11475a18fff2SAl Viro 114812f8ad4bSLinus Torvalds /* 114912f8ad4bSLinus Torvalds * This sequence count validates that the inode matches 115012f8ad4bSLinus Torvalds * the dentry name information from lookup. 115112f8ad4bSLinus Torvalds */ 115212f8ad4bSLinus Torvalds *inode = dentry->d_inode; 115312f8ad4bSLinus Torvalds if (read_seqcount_retry(&dentry->d_seq, seq)) 115412f8ad4bSLinus Torvalds return -ECHILD; 115512f8ad4bSLinus Torvalds 115612f8ad4bSLinus Torvalds /* 115712f8ad4bSLinus Torvalds * This sequence count validates that the parent had no 115812f8ad4bSLinus Torvalds * changes while we did the lookup of the dentry above. 115912f8ad4bSLinus Torvalds * 116012f8ad4bSLinus Torvalds * The memory barrier in read_seqcount_begin of child is 116112f8ad4bSLinus Torvalds * enough, we can use __read_seqcount_retry here. 116212f8ad4bSLinus Torvalds */ 116331e6b01fSNick Piggin if (__read_seqcount_retry(&parent->d_seq, nd->seq)) 116431e6b01fSNick Piggin return -ECHILD; 116531e6b01fSNick Piggin nd->seq = seq; 11665a18fff2SAl Viro 1167fa4ee159SMiklos Szeredi if (unlikely(d_need_lookup(dentry))) 1168fa4ee159SMiklos Szeredi goto unlazy; 116924643087SAl Viro if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) { 11705a18fff2SAl Viro status = d_revalidate(dentry, nd); 11715a18fff2SAl Viro if (unlikely(status <= 0)) { 11725a18fff2SAl Viro if (status != -ECHILD) 11735a18fff2SAl Viro need_reval = 0; 11745a18fff2SAl Viro goto unlazy; 11755a18fff2SAl Viro } 117624643087SAl Viro } 117731e6b01fSNick Piggin path->mnt = mnt; 117831e6b01fSNick Piggin path->dentry = dentry; 1179d6e9bd25SAl Viro if (unlikely(!__follow_mount_rcu(nd, path, inode))) 1180d6e9bd25SAl Viro goto unlazy; 1181d6e9bd25SAl Viro if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT)) 1182d6e9bd25SAl Viro goto unlazy; 11839875cf80SDavid Howells return 0; 11845a18fff2SAl Viro unlazy: 118519660af7SAl Viro if (unlazy_walk(nd, dentry)) 11865a18fff2SAl Viro return -ECHILD; 11875a18fff2SAl Viro } else { 118831e6b01fSNick Piggin dentry = __d_lookup(parent, name); 118924643087SAl Viro } 11905a18fff2SAl Viro 119181e6f520SAl Viro if (unlikely(!dentry)) 119281e6f520SAl Viro goto need_lookup; 11935a18fff2SAl Viro 119481e6f520SAl Viro if (unlikely(d_need_lookup(dentry))) { 119581e6f520SAl Viro dput(dentry); 119681e6f520SAl Viro goto need_lookup; 11975a18fff2SAl Viro } 119881e6f520SAl Viro 11995a18fff2SAl Viro if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval) 12005a18fff2SAl Viro status = d_revalidate(dentry, nd); 12015a18fff2SAl Viro if (unlikely(status <= 0)) { 12025a18fff2SAl Viro if (status < 0) { 12035a18fff2SAl Viro dput(dentry); 12045a18fff2SAl Viro return status; 12055a18fff2SAl Viro } 12065a18fff2SAl Viro if (!d_invalidate(dentry)) { 12075a18fff2SAl Viro dput(dentry); 120881e6f520SAl Viro goto need_lookup; 12095a18fff2SAl Viro } 12105a18fff2SAl Viro } 12113f6c7c71SAl Viro done: 12121da177e4SLinus Torvalds path->mnt = mnt; 12131da177e4SLinus Torvalds path->dentry = dentry; 12149875cf80SDavid Howells err = follow_managed(path, nd->flags); 121589312214SIan Kent if (unlikely(err < 0)) { 121689312214SIan Kent path_put_conditional(path, nd); 12179875cf80SDavid Howells return err; 121889312214SIan Kent } 1219a3fbbde7SAl Viro if (err) 1220a3fbbde7SAl Viro nd->flags |= LOOKUP_JUMPED; 122131e6b01fSNick Piggin *inode = path->dentry->d_inode; 12221da177e4SLinus Torvalds return 0; 122381e6f520SAl Viro 122481e6f520SAl Viro need_lookup: 122581e6f520SAl Viro BUG_ON(nd->inode != parent->d_inode); 122681e6f520SAl Viro 122781e6f520SAl Viro mutex_lock(&parent->d_inode->i_mutex); 122881e6f520SAl Viro dentry = __lookup_hash(name, parent, nd); 122981e6f520SAl Viro mutex_unlock(&parent->d_inode->i_mutex); 123081e6f520SAl Viro if (IS_ERR(dentry)) 123181e6f520SAl Viro return PTR_ERR(dentry); 123281e6f520SAl Viro goto done; 12331da177e4SLinus Torvalds } 12341da177e4SLinus Torvalds 123552094c8aSAl Viro static inline int may_lookup(struct nameidata *nd) 123652094c8aSAl Viro { 123752094c8aSAl Viro if (nd->flags & LOOKUP_RCU) { 12384ad5abb3SAl Viro int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK); 123952094c8aSAl Viro if (err != -ECHILD) 124052094c8aSAl Viro return err; 124119660af7SAl Viro if (unlazy_walk(nd, NULL)) 124252094c8aSAl Viro return -ECHILD; 124352094c8aSAl Viro } 12444ad5abb3SAl Viro return inode_permission(nd->inode, MAY_EXEC); 124552094c8aSAl Viro } 124652094c8aSAl Viro 12479856fa1bSAl Viro static inline int handle_dots(struct nameidata *nd, int type) 12489856fa1bSAl Viro { 12499856fa1bSAl Viro if (type == LAST_DOTDOT) { 12509856fa1bSAl Viro if (nd->flags & LOOKUP_RCU) { 12519856fa1bSAl Viro if (follow_dotdot_rcu(nd)) 12529856fa1bSAl Viro return -ECHILD; 12539856fa1bSAl Viro } else 12549856fa1bSAl Viro follow_dotdot(nd); 12559856fa1bSAl Viro } 12569856fa1bSAl Viro return 0; 12579856fa1bSAl Viro } 12589856fa1bSAl Viro 1259951361f9SAl Viro static void terminate_walk(struct nameidata *nd) 1260951361f9SAl Viro { 1261951361f9SAl Viro if (!(nd->flags & LOOKUP_RCU)) { 1262951361f9SAl Viro path_put(&nd->path); 1263951361f9SAl Viro } else { 1264951361f9SAl Viro nd->flags &= ~LOOKUP_RCU; 12655b6ca027SAl Viro if (!(nd->flags & LOOKUP_ROOT)) 1266951361f9SAl Viro nd->root.mnt = NULL; 1267951361f9SAl Viro rcu_read_unlock(); 1268951361f9SAl Viro br_read_unlock(vfsmount_lock); 1269951361f9SAl Viro } 1270951361f9SAl Viro } 1271951361f9SAl Viro 12723ddcd056SLinus Torvalds /* 12733ddcd056SLinus Torvalds * Do we need to follow links? We _really_ want to be able 12743ddcd056SLinus Torvalds * to do this check without having to look at inode->i_op, 12753ddcd056SLinus Torvalds * so we keep a cache of "no, this doesn't need follow_link" 12763ddcd056SLinus Torvalds * for the common case. 12773ddcd056SLinus Torvalds */ 12787813b94aSLinus Torvalds static inline int should_follow_link(struct inode *inode, int follow) 12793ddcd056SLinus Torvalds { 12803ddcd056SLinus Torvalds if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) { 12813ddcd056SLinus Torvalds if (likely(inode->i_op->follow_link)) 12823ddcd056SLinus Torvalds return follow; 12833ddcd056SLinus Torvalds 12843ddcd056SLinus Torvalds /* This gets set once for the inode lifetime */ 12853ddcd056SLinus Torvalds spin_lock(&inode->i_lock); 12863ddcd056SLinus Torvalds inode->i_opflags |= IOP_NOFOLLOW; 12873ddcd056SLinus Torvalds spin_unlock(&inode->i_lock); 12883ddcd056SLinus Torvalds } 12893ddcd056SLinus Torvalds return 0; 12903ddcd056SLinus Torvalds } 12913ddcd056SLinus Torvalds 1292ce57dfc1SAl Viro static inline int walk_component(struct nameidata *nd, struct path *path, 1293ce57dfc1SAl Viro struct qstr *name, int type, int follow) 1294ce57dfc1SAl Viro { 1295ce57dfc1SAl Viro struct inode *inode; 1296ce57dfc1SAl Viro int err; 1297ce57dfc1SAl Viro /* 1298ce57dfc1SAl Viro * "." and ".." are special - ".." especially so because it has 1299ce57dfc1SAl Viro * to be able to know about the current root directory and 1300ce57dfc1SAl Viro * parent relationships. 1301ce57dfc1SAl Viro */ 1302ce57dfc1SAl Viro if (unlikely(type != LAST_NORM)) 1303ce57dfc1SAl Viro return handle_dots(nd, type); 1304ce57dfc1SAl Viro err = do_lookup(nd, name, path, &inode); 1305ce57dfc1SAl Viro if (unlikely(err)) { 1306ce57dfc1SAl Viro terminate_walk(nd); 1307ce57dfc1SAl Viro return err; 1308ce57dfc1SAl Viro } 1309ce57dfc1SAl Viro if (!inode) { 1310ce57dfc1SAl Viro path_to_nameidata(path, nd); 1311ce57dfc1SAl Viro terminate_walk(nd); 1312ce57dfc1SAl Viro return -ENOENT; 1313ce57dfc1SAl Viro } 13147813b94aSLinus Torvalds if (should_follow_link(inode, follow)) { 131519660af7SAl Viro if (nd->flags & LOOKUP_RCU) { 131619660af7SAl Viro if (unlikely(unlazy_walk(nd, path->dentry))) { 131719660af7SAl Viro terminate_walk(nd); 1318ce57dfc1SAl Viro return -ECHILD; 131919660af7SAl Viro } 132019660af7SAl Viro } 1321ce57dfc1SAl Viro BUG_ON(inode != path->dentry->d_inode); 1322ce57dfc1SAl Viro return 1; 1323ce57dfc1SAl Viro } 1324ce57dfc1SAl Viro path_to_nameidata(path, nd); 1325ce57dfc1SAl Viro nd->inode = inode; 1326ce57dfc1SAl Viro return 0; 1327ce57dfc1SAl Viro } 1328ce57dfc1SAl Viro 13291da177e4SLinus Torvalds /* 1330b356379aSAl Viro * This limits recursive symlink follows to 8, while 1331b356379aSAl Viro * limiting consecutive symlinks to 40. 1332b356379aSAl Viro * 1333b356379aSAl Viro * Without that kind of total limit, nasty chains of consecutive 1334b356379aSAl Viro * symlinks can cause almost arbitrarily long lookups. 1335b356379aSAl Viro */ 1336b356379aSAl Viro static inline int nested_symlink(struct path *path, struct nameidata *nd) 1337b356379aSAl Viro { 1338b356379aSAl Viro int res; 1339b356379aSAl Viro 1340b356379aSAl Viro if (unlikely(current->link_count >= MAX_NESTED_LINKS)) { 1341b356379aSAl Viro path_put_conditional(path, nd); 1342b356379aSAl Viro path_put(&nd->path); 1343b356379aSAl Viro return -ELOOP; 1344b356379aSAl Viro } 13451a4022f8SErez Zadok BUG_ON(nd->depth >= MAX_NESTED_LINKS); 1346b356379aSAl Viro 1347b356379aSAl Viro nd->depth++; 1348b356379aSAl Viro current->link_count++; 1349b356379aSAl Viro 1350b356379aSAl Viro do { 1351b356379aSAl Viro struct path link = *path; 1352b356379aSAl Viro void *cookie; 1353574197e0SAl Viro 1354574197e0SAl Viro res = follow_link(&link, nd, &cookie); 1355b356379aSAl Viro if (!res) 1356b356379aSAl Viro res = walk_component(nd, path, &nd->last, 1357b356379aSAl Viro nd->last_type, LOOKUP_FOLLOW); 1358574197e0SAl Viro put_link(nd, &link, cookie); 1359b356379aSAl Viro } while (res > 0); 1360b356379aSAl Viro 1361b356379aSAl Viro current->link_count--; 1362b356379aSAl Viro nd->depth--; 1363b356379aSAl Viro return res; 1364b356379aSAl Viro } 1365b356379aSAl Viro 1366b356379aSAl Viro /* 13673ddcd056SLinus Torvalds * We really don't want to look at inode->i_op->lookup 13683ddcd056SLinus Torvalds * when we don't have to. So we keep a cache bit in 13693ddcd056SLinus Torvalds * the inode ->i_opflags field that says "yes, we can 13703ddcd056SLinus Torvalds * do lookup on this inode". 13713ddcd056SLinus Torvalds */ 13723ddcd056SLinus Torvalds static inline int can_lookup(struct inode *inode) 13733ddcd056SLinus Torvalds { 13743ddcd056SLinus Torvalds if (likely(inode->i_opflags & IOP_LOOKUP)) 13753ddcd056SLinus Torvalds return 1; 13763ddcd056SLinus Torvalds if (likely(!inode->i_op->lookup)) 13773ddcd056SLinus Torvalds return 0; 13783ddcd056SLinus Torvalds 13793ddcd056SLinus Torvalds /* We do this once for the lifetime of the inode */ 13803ddcd056SLinus Torvalds spin_lock(&inode->i_lock); 13813ddcd056SLinus Torvalds inode->i_opflags |= IOP_LOOKUP; 13823ddcd056SLinus Torvalds spin_unlock(&inode->i_lock); 13833ddcd056SLinus Torvalds return 1; 13843ddcd056SLinus Torvalds } 13853ddcd056SLinus Torvalds 1386bfcfaa77SLinus Torvalds /* 1387bfcfaa77SLinus Torvalds * We can do the critical dentry name comparison and hashing 1388bfcfaa77SLinus Torvalds * operations one word at a time, but we are limited to: 1389bfcfaa77SLinus Torvalds * 1390bfcfaa77SLinus Torvalds * - Architectures with fast unaligned word accesses. We could 1391bfcfaa77SLinus Torvalds * do a "get_unaligned()" if this helps and is sufficiently 1392bfcfaa77SLinus Torvalds * fast. 1393bfcfaa77SLinus Torvalds * 1394bfcfaa77SLinus Torvalds * - Little-endian machines (so that we can generate the mask 1395bfcfaa77SLinus Torvalds * of low bytes efficiently). Again, we *could* do a byte 1396bfcfaa77SLinus Torvalds * swapping load on big-endian architectures if that is not 1397bfcfaa77SLinus Torvalds * expensive enough to make the optimization worthless. 1398bfcfaa77SLinus Torvalds * 1399bfcfaa77SLinus Torvalds * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we 1400bfcfaa77SLinus Torvalds * do not trap on the (extremely unlikely) case of a page 1401bfcfaa77SLinus Torvalds * crossing operation. 1402bfcfaa77SLinus Torvalds * 1403bfcfaa77SLinus Torvalds * - Furthermore, we need an efficient 64-bit compile for the 1404bfcfaa77SLinus Torvalds * 64-bit case in order to generate the "number of bytes in 1405bfcfaa77SLinus Torvalds * the final mask". Again, that could be replaced with a 1406bfcfaa77SLinus Torvalds * efficient population count instruction or similar. 1407bfcfaa77SLinus Torvalds */ 1408bfcfaa77SLinus Torvalds #ifdef CONFIG_DCACHE_WORD_ACCESS 1409bfcfaa77SLinus Torvalds 1410f68e556eSLinus Torvalds #include <asm/word-at-a-time.h> 1411bfcfaa77SLinus Torvalds 1412f68e556eSLinus Torvalds #ifdef CONFIG_64BIT 1413bfcfaa77SLinus Torvalds 1414bfcfaa77SLinus Torvalds static inline unsigned int fold_hash(unsigned long hash) 1415bfcfaa77SLinus Torvalds { 1416bfcfaa77SLinus Torvalds hash += hash >> (8*sizeof(int)); 1417bfcfaa77SLinus Torvalds return hash; 1418bfcfaa77SLinus Torvalds } 1419bfcfaa77SLinus Torvalds 1420bfcfaa77SLinus Torvalds #else /* 32-bit case */ 1421bfcfaa77SLinus Torvalds 1422bfcfaa77SLinus Torvalds #define fold_hash(x) (x) 1423bfcfaa77SLinus Torvalds 1424bfcfaa77SLinus Torvalds #endif 1425bfcfaa77SLinus Torvalds 1426bfcfaa77SLinus Torvalds unsigned int full_name_hash(const unsigned char *name, unsigned int len) 1427bfcfaa77SLinus Torvalds { 1428bfcfaa77SLinus Torvalds unsigned long a, mask; 1429bfcfaa77SLinus Torvalds unsigned long hash = 0; 1430bfcfaa77SLinus Torvalds 1431bfcfaa77SLinus Torvalds for (;;) { 1432e419b4ccSLinus Torvalds a = load_unaligned_zeropad(name); 1433bfcfaa77SLinus Torvalds if (len < sizeof(unsigned long)) 1434bfcfaa77SLinus Torvalds break; 1435bfcfaa77SLinus Torvalds hash += a; 1436f132c5beSAl Viro hash *= 9; 1437bfcfaa77SLinus Torvalds name += sizeof(unsigned long); 1438bfcfaa77SLinus Torvalds len -= sizeof(unsigned long); 1439bfcfaa77SLinus Torvalds if (!len) 1440bfcfaa77SLinus Torvalds goto done; 1441bfcfaa77SLinus Torvalds } 1442bfcfaa77SLinus Torvalds mask = ~(~0ul << len*8); 1443bfcfaa77SLinus Torvalds hash += mask & a; 1444bfcfaa77SLinus Torvalds done: 1445bfcfaa77SLinus Torvalds return fold_hash(hash); 1446bfcfaa77SLinus Torvalds } 1447bfcfaa77SLinus Torvalds EXPORT_SYMBOL(full_name_hash); 1448bfcfaa77SLinus Torvalds 1449bfcfaa77SLinus Torvalds /* 1450bfcfaa77SLinus Torvalds * Calculate the length and hash of the path component, and 1451bfcfaa77SLinus Torvalds * return the length of the component; 1452bfcfaa77SLinus Torvalds */ 1453bfcfaa77SLinus Torvalds static inline unsigned long hash_name(const char *name, unsigned int *hashp) 1454bfcfaa77SLinus Torvalds { 145536126f8fSLinus Torvalds unsigned long a, b, adata, bdata, mask, hash, len; 145636126f8fSLinus Torvalds const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS; 1457bfcfaa77SLinus Torvalds 1458bfcfaa77SLinus Torvalds hash = a = 0; 1459bfcfaa77SLinus Torvalds len = -sizeof(unsigned long); 1460bfcfaa77SLinus Torvalds do { 1461bfcfaa77SLinus Torvalds hash = (hash + a) * 9; 1462bfcfaa77SLinus Torvalds len += sizeof(unsigned long); 1463e419b4ccSLinus Torvalds a = load_unaligned_zeropad(name+len); 146436126f8fSLinus Torvalds b = a ^ REPEAT_BYTE('/'); 146536126f8fSLinus Torvalds } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants))); 1466bfcfaa77SLinus Torvalds 146736126f8fSLinus Torvalds adata = prep_zero_mask(a, adata, &constants); 146836126f8fSLinus Torvalds bdata = prep_zero_mask(b, bdata, &constants); 146936126f8fSLinus Torvalds 147036126f8fSLinus Torvalds mask = create_zero_mask(adata | bdata); 147136126f8fSLinus Torvalds 147236126f8fSLinus Torvalds hash += a & zero_bytemask(mask); 1473bfcfaa77SLinus Torvalds *hashp = fold_hash(hash); 1474bfcfaa77SLinus Torvalds 147536126f8fSLinus Torvalds return len + find_zero(mask); 1476bfcfaa77SLinus Torvalds } 1477bfcfaa77SLinus Torvalds 1478bfcfaa77SLinus Torvalds #else 1479bfcfaa77SLinus Torvalds 14800145acc2SLinus Torvalds unsigned int full_name_hash(const unsigned char *name, unsigned int len) 14810145acc2SLinus Torvalds { 14820145acc2SLinus Torvalds unsigned long hash = init_name_hash(); 14830145acc2SLinus Torvalds while (len--) 14840145acc2SLinus Torvalds hash = partial_name_hash(*name++, hash); 14850145acc2SLinus Torvalds return end_name_hash(hash); 14860145acc2SLinus Torvalds } 1487ae942ae7SLinus Torvalds EXPORT_SYMBOL(full_name_hash); 14880145acc2SLinus Torvalds 14893ddcd056SLinus Torvalds /* 1490200e9ef7SLinus Torvalds * We know there's a real path component here of at least 1491200e9ef7SLinus Torvalds * one character. 1492200e9ef7SLinus Torvalds */ 1493200e9ef7SLinus Torvalds static inline unsigned long hash_name(const char *name, unsigned int *hashp) 1494200e9ef7SLinus Torvalds { 1495200e9ef7SLinus Torvalds unsigned long hash = init_name_hash(); 1496200e9ef7SLinus Torvalds unsigned long len = 0, c; 1497200e9ef7SLinus Torvalds 1498200e9ef7SLinus Torvalds c = (unsigned char)*name; 1499200e9ef7SLinus Torvalds do { 1500200e9ef7SLinus Torvalds len++; 1501200e9ef7SLinus Torvalds hash = partial_name_hash(c, hash); 1502200e9ef7SLinus Torvalds c = (unsigned char)name[len]; 1503200e9ef7SLinus Torvalds } while (c && c != '/'); 1504200e9ef7SLinus Torvalds *hashp = end_name_hash(hash); 1505200e9ef7SLinus Torvalds return len; 1506200e9ef7SLinus Torvalds } 1507200e9ef7SLinus Torvalds 1508bfcfaa77SLinus Torvalds #endif 1509bfcfaa77SLinus Torvalds 1510200e9ef7SLinus Torvalds /* 15111da177e4SLinus Torvalds * Name resolution. 1512ea3834d9SPrasanna Meda * This is the basic name resolution function, turning a pathname into 1513ea3834d9SPrasanna Meda * the final dentry. We expect 'base' to be positive and a directory. 15141da177e4SLinus Torvalds * 1515ea3834d9SPrasanna Meda * Returns 0 and nd will have valid dentry and mnt on success. 1516ea3834d9SPrasanna Meda * Returns error and drops reference to input namei data on failure. 15171da177e4SLinus Torvalds */ 15186de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd) 15191da177e4SLinus Torvalds { 15201da177e4SLinus Torvalds struct path next; 15211da177e4SLinus Torvalds int err; 15221da177e4SLinus Torvalds 15231da177e4SLinus Torvalds while (*name=='/') 15241da177e4SLinus Torvalds name++; 15251da177e4SLinus Torvalds if (!*name) 1526086e183aSAl Viro return 0; 15271da177e4SLinus Torvalds 15281da177e4SLinus Torvalds /* At this point we know we have a real path component. */ 15291da177e4SLinus Torvalds for(;;) { 15301da177e4SLinus Torvalds struct qstr this; 1531200e9ef7SLinus Torvalds long len; 1532fe479a58SAl Viro int type; 15331da177e4SLinus Torvalds 153452094c8aSAl Viro err = may_lookup(nd); 15351da177e4SLinus Torvalds if (err) 15361da177e4SLinus Torvalds break; 15371da177e4SLinus Torvalds 1538200e9ef7SLinus Torvalds len = hash_name(name, &this.hash); 15391da177e4SLinus Torvalds this.name = name; 1540200e9ef7SLinus Torvalds this.len = len; 15411da177e4SLinus Torvalds 1542fe479a58SAl Viro type = LAST_NORM; 1543200e9ef7SLinus Torvalds if (name[0] == '.') switch (len) { 1544fe479a58SAl Viro case 2: 1545200e9ef7SLinus Torvalds if (name[1] == '.') { 1546fe479a58SAl Viro type = LAST_DOTDOT; 154716c2cd71SAl Viro nd->flags |= LOOKUP_JUMPED; 154816c2cd71SAl Viro } 1549fe479a58SAl Viro break; 1550fe479a58SAl Viro case 1: 1551fe479a58SAl Viro type = LAST_DOT; 1552fe479a58SAl Viro } 15535a202bcdSAl Viro if (likely(type == LAST_NORM)) { 15545a202bcdSAl Viro struct dentry *parent = nd->path.dentry; 155516c2cd71SAl Viro nd->flags &= ~LOOKUP_JUMPED; 15565a202bcdSAl Viro if (unlikely(parent->d_flags & DCACHE_OP_HASH)) { 15575a202bcdSAl Viro err = parent->d_op->d_hash(parent, nd->inode, 15585a202bcdSAl Viro &this); 15595a202bcdSAl Viro if (err < 0) 15605a202bcdSAl Viro break; 15615a202bcdSAl Viro } 15625a202bcdSAl Viro } 1563fe479a58SAl Viro 1564200e9ef7SLinus Torvalds if (!name[len]) 15651da177e4SLinus Torvalds goto last_component; 1566200e9ef7SLinus Torvalds /* 1567200e9ef7SLinus Torvalds * If it wasn't NUL, we know it was '/'. Skip that 1568200e9ef7SLinus Torvalds * slash, and continue until no more slashes. 1569200e9ef7SLinus Torvalds */ 1570200e9ef7SLinus Torvalds do { 1571200e9ef7SLinus Torvalds len++; 1572200e9ef7SLinus Torvalds } while (unlikely(name[len] == '/')); 1573200e9ef7SLinus Torvalds if (!name[len]) 1574b356379aSAl Viro goto last_component; 1575200e9ef7SLinus Torvalds name += len; 15761da177e4SLinus Torvalds 1577ce57dfc1SAl Viro err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW); 1578ce57dfc1SAl Viro if (err < 0) 1579ce57dfc1SAl Viro return err; 1580fe479a58SAl Viro 1581ce57dfc1SAl Viro if (err) { 1582b356379aSAl Viro err = nested_symlink(&next, nd); 15831da177e4SLinus Torvalds if (err) 1584a7472babSAl Viro return err; 158531e6b01fSNick Piggin } 15863ddcd056SLinus Torvalds if (can_lookup(nd->inode)) 15871da177e4SLinus Torvalds continue; 15883ddcd056SLinus Torvalds err = -ENOTDIR; 15893ddcd056SLinus Torvalds break; 15901da177e4SLinus Torvalds /* here ends the main loop */ 15911da177e4SLinus Torvalds 15921da177e4SLinus Torvalds last_component: 1593ce57dfc1SAl Viro nd->last = this; 1594ce57dfc1SAl Viro nd->last_type = type; 1595ce57dfc1SAl Viro return 0; 1596ce57dfc1SAl Viro } 1597951361f9SAl Viro terminate_walk(nd); 15981da177e4SLinus Torvalds return err; 15991da177e4SLinus Torvalds } 16001da177e4SLinus Torvalds 160170e9b357SAl Viro static int path_init(int dfd, const char *name, unsigned int flags, 160270e9b357SAl Viro struct nameidata *nd, struct file **fp) 160331e6b01fSNick Piggin { 160431e6b01fSNick Piggin int retval = 0; 160531e6b01fSNick Piggin int fput_needed; 160631e6b01fSNick Piggin struct file *file; 160731e6b01fSNick Piggin 160831e6b01fSNick Piggin nd->last_type = LAST_ROOT; /* if there are only slashes... */ 160916c2cd71SAl Viro nd->flags = flags | LOOKUP_JUMPED; 161031e6b01fSNick Piggin nd->depth = 0; 16115b6ca027SAl Viro if (flags & LOOKUP_ROOT) { 16125b6ca027SAl Viro struct inode *inode = nd->root.dentry->d_inode; 161373d049a4SAl Viro if (*name) { 16145b6ca027SAl Viro if (!inode->i_op->lookup) 16155b6ca027SAl Viro return -ENOTDIR; 16165b6ca027SAl Viro retval = inode_permission(inode, MAY_EXEC); 16175b6ca027SAl Viro if (retval) 16185b6ca027SAl Viro return retval; 161973d049a4SAl Viro } 16205b6ca027SAl Viro nd->path = nd->root; 16215b6ca027SAl Viro nd->inode = inode; 16225b6ca027SAl Viro if (flags & LOOKUP_RCU) { 16235b6ca027SAl Viro br_read_lock(vfsmount_lock); 16245b6ca027SAl Viro rcu_read_lock(); 16255b6ca027SAl Viro nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 16265b6ca027SAl Viro } else { 16275b6ca027SAl Viro path_get(&nd->path); 16285b6ca027SAl Viro } 16295b6ca027SAl Viro return 0; 16305b6ca027SAl Viro } 16315b6ca027SAl Viro 163231e6b01fSNick Piggin nd->root.mnt = NULL; 163331e6b01fSNick Piggin 163431e6b01fSNick Piggin if (*name=='/') { 1635e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 163631e6b01fSNick Piggin br_read_lock(vfsmount_lock); 163731e6b01fSNick Piggin rcu_read_lock(); 1638e41f7d4eSAl Viro set_root_rcu(nd); 1639e41f7d4eSAl Viro } else { 1640e41f7d4eSAl Viro set_root(nd); 1641e41f7d4eSAl Viro path_get(&nd->root); 1642e41f7d4eSAl Viro } 164331e6b01fSNick Piggin nd->path = nd->root; 164431e6b01fSNick Piggin } else if (dfd == AT_FDCWD) { 1645e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 164631e6b01fSNick Piggin struct fs_struct *fs = current->fs; 1647c28cc364SNick Piggin unsigned seq; 164831e6b01fSNick Piggin 164931e6b01fSNick Piggin br_read_lock(vfsmount_lock); 165031e6b01fSNick Piggin rcu_read_lock(); 165131e6b01fSNick Piggin 1652c28cc364SNick Piggin do { 1653c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 165431e6b01fSNick Piggin nd->path = fs->pwd; 1655c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 1656c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 1657e41f7d4eSAl Viro } else { 1658e41f7d4eSAl Viro get_fs_pwd(current->fs, &nd->path); 1659e41f7d4eSAl Viro } 166031e6b01fSNick Piggin } else { 166131e6b01fSNick Piggin struct dentry *dentry; 166231e6b01fSNick Piggin 16631abf0c71SAl Viro file = fget_raw_light(dfd, &fput_needed); 166431e6b01fSNick Piggin retval = -EBADF; 166531e6b01fSNick Piggin if (!file) 166631e6b01fSNick Piggin goto out_fail; 166731e6b01fSNick Piggin 166831e6b01fSNick Piggin dentry = file->f_path.dentry; 166931e6b01fSNick Piggin 1670f52e0c11SAl Viro if (*name) { 167131e6b01fSNick Piggin retval = -ENOTDIR; 167231e6b01fSNick Piggin if (!S_ISDIR(dentry->d_inode->i_mode)) 167331e6b01fSNick Piggin goto fput_fail; 167431e6b01fSNick Piggin 16754ad5abb3SAl Viro retval = inode_permission(dentry->d_inode, MAY_EXEC); 167631e6b01fSNick Piggin if (retval) 167731e6b01fSNick Piggin goto fput_fail; 1678f52e0c11SAl Viro } 167931e6b01fSNick Piggin 168031e6b01fSNick Piggin nd->path = file->f_path; 1681e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 168231e6b01fSNick Piggin if (fput_needed) 168370e9b357SAl Viro *fp = file; 1684c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 168531e6b01fSNick Piggin br_read_lock(vfsmount_lock); 168631e6b01fSNick Piggin rcu_read_lock(); 16875590ff0dSUlrich Drepper } else { 16885dd784d0SJan Blunck path_get(&file->f_path); 16895590ff0dSUlrich Drepper fput_light(file, fput_needed); 16901da177e4SLinus Torvalds } 1691e41f7d4eSAl Viro } 1692e41f7d4eSAl Viro 169331e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 16949b4a9b14SAl Viro return 0; 16952dfdd266SJosef 'Jeff' Sipek 16969b4a9b14SAl Viro fput_fail: 16979b4a9b14SAl Viro fput_light(file, fput_needed); 16989b4a9b14SAl Viro out_fail: 16999b4a9b14SAl Viro return retval; 17009b4a9b14SAl Viro } 17019b4a9b14SAl Viro 1702bd92d7feSAl Viro static inline int lookup_last(struct nameidata *nd, struct path *path) 1703bd92d7feSAl Viro { 1704bd92d7feSAl Viro if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len]) 1705bd92d7feSAl Viro nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 1706bd92d7feSAl Viro 1707bd92d7feSAl Viro nd->flags &= ~LOOKUP_PARENT; 1708bd92d7feSAl Viro return walk_component(nd, path, &nd->last, nd->last_type, 1709bd92d7feSAl Viro nd->flags & LOOKUP_FOLLOW); 1710bd92d7feSAl Viro } 1711bd92d7feSAl Viro 17129b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */ 1713ee0827cdSAl Viro static int path_lookupat(int dfd, const char *name, 17149b4a9b14SAl Viro unsigned int flags, struct nameidata *nd) 17159b4a9b14SAl Viro { 171670e9b357SAl Viro struct file *base = NULL; 1717bd92d7feSAl Viro struct path path; 1718bd92d7feSAl Viro int err; 171931e6b01fSNick Piggin 172031e6b01fSNick Piggin /* 172131e6b01fSNick Piggin * Path walking is largely split up into 2 different synchronisation 172231e6b01fSNick Piggin * schemes, rcu-walk and ref-walk (explained in 172331e6b01fSNick Piggin * Documentation/filesystems/path-lookup.txt). These share much of the 172431e6b01fSNick Piggin * path walk code, but some things particularly setup, cleanup, and 172531e6b01fSNick Piggin * following mounts are sufficiently divergent that functions are 172631e6b01fSNick Piggin * duplicated. Typically there is a function foo(), and its RCU 172731e6b01fSNick Piggin * analogue, foo_rcu(). 172831e6b01fSNick Piggin * 172931e6b01fSNick Piggin * -ECHILD is the error number of choice (just to avoid clashes) that 173031e6b01fSNick Piggin * is returned if some aspect of an rcu-walk fails. Such an error must 173131e6b01fSNick Piggin * be handled by restarting a traditional ref-walk (which will always 173231e6b01fSNick Piggin * be able to complete). 173331e6b01fSNick Piggin */ 1734bd92d7feSAl Viro err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base); 1735ee0827cdSAl Viro 1736bd92d7feSAl Viro if (unlikely(err)) 1737bd92d7feSAl Viro return err; 1738ee0827cdSAl Viro 1739ee0827cdSAl Viro current->total_link_count = 0; 1740bd92d7feSAl Viro err = link_path_walk(name, nd); 1741bd92d7feSAl Viro 1742bd92d7feSAl Viro if (!err && !(flags & LOOKUP_PARENT)) { 1743bd92d7feSAl Viro err = lookup_last(nd, &path); 1744bd92d7feSAl Viro while (err > 0) { 1745bd92d7feSAl Viro void *cookie; 1746bd92d7feSAl Viro struct path link = path; 1747bd92d7feSAl Viro nd->flags |= LOOKUP_PARENT; 1748574197e0SAl Viro err = follow_link(&link, nd, &cookie); 1749bd92d7feSAl Viro if (!err) 1750bd92d7feSAl Viro err = lookup_last(nd, &path); 1751574197e0SAl Viro put_link(nd, &link, cookie); 1752bd92d7feSAl Viro } 1753bd92d7feSAl Viro } 1754ee0827cdSAl Viro 17559f1fafeeSAl Viro if (!err) 17569f1fafeeSAl Viro err = complete_walk(nd); 1757bd92d7feSAl Viro 1758bd92d7feSAl Viro if (!err && nd->flags & LOOKUP_DIRECTORY) { 1759bd92d7feSAl Viro if (!nd->inode->i_op->lookup) { 1760bd92d7feSAl Viro path_put(&nd->path); 1761bd23a539SAl Viro err = -ENOTDIR; 1762bd92d7feSAl Viro } 1763bd92d7feSAl Viro } 176416c2cd71SAl Viro 176570e9b357SAl Viro if (base) 176670e9b357SAl Viro fput(base); 1767ee0827cdSAl Viro 17685b6ca027SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) { 176931e6b01fSNick Piggin path_put(&nd->root); 177031e6b01fSNick Piggin nd->root.mnt = NULL; 177131e6b01fSNick Piggin } 1772bd92d7feSAl Viro return err; 177331e6b01fSNick Piggin } 177431e6b01fSNick Piggin 1775ee0827cdSAl Viro static int do_path_lookup(int dfd, const char *name, 1776ee0827cdSAl Viro unsigned int flags, struct nameidata *nd) 1777ee0827cdSAl Viro { 1778ee0827cdSAl Viro int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd); 1779ee0827cdSAl Viro if (unlikely(retval == -ECHILD)) 1780ee0827cdSAl Viro retval = path_lookupat(dfd, name, flags, nd); 1781ee0827cdSAl Viro if (unlikely(retval == -ESTALE)) 1782ee0827cdSAl Viro retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd); 1783ee0827cdSAl Viro 178431e6b01fSNick Piggin if (likely(!retval)) { 178531e6b01fSNick Piggin if (unlikely(!audit_dummy_context())) { 178631e6b01fSNick Piggin if (nd->path.dentry && nd->inode) 178731e6b01fSNick Piggin audit_inode(name, nd->path.dentry); 178831e6b01fSNick Piggin } 178931e6b01fSNick Piggin } 1790170aa3d0SUlrich Drepper return retval; 17911da177e4SLinus Torvalds } 17921da177e4SLinus Torvalds 1793c9c6cac0SAl Viro int kern_path_parent(const char *name, struct nameidata *nd) 17945590ff0dSUlrich Drepper { 1795c9c6cac0SAl Viro return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd); 17965590ff0dSUlrich Drepper } 17975590ff0dSUlrich Drepper 1798d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path) 1799d1811465SAl Viro { 1800d1811465SAl Viro struct nameidata nd; 1801d1811465SAl Viro int res = do_path_lookup(AT_FDCWD, name, flags, &nd); 1802d1811465SAl Viro if (!res) 1803d1811465SAl Viro *path = nd.path; 1804d1811465SAl Viro return res; 1805d1811465SAl Viro } 1806d1811465SAl Viro 180716f18200SJosef 'Jeff' Sipek /** 180816f18200SJosef 'Jeff' Sipek * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair 180916f18200SJosef 'Jeff' Sipek * @dentry: pointer to dentry of the base directory 181016f18200SJosef 'Jeff' Sipek * @mnt: pointer to vfs mount of the base directory 181116f18200SJosef 'Jeff' Sipek * @name: pointer to file name 181216f18200SJosef 'Jeff' Sipek * @flags: lookup flags 1813e0a01249SAl Viro * @path: pointer to struct path to fill 181416f18200SJosef 'Jeff' Sipek */ 181516f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt, 181616f18200SJosef 'Jeff' Sipek const char *name, unsigned int flags, 1817e0a01249SAl Viro struct path *path) 181816f18200SJosef 'Jeff' Sipek { 1819e0a01249SAl Viro struct nameidata nd; 1820e0a01249SAl Viro int err; 1821e0a01249SAl Viro nd.root.dentry = dentry; 1822e0a01249SAl Viro nd.root.mnt = mnt; 1823e0a01249SAl Viro BUG_ON(flags & LOOKUP_PARENT); 18245b6ca027SAl Viro /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */ 1825e0a01249SAl Viro err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd); 1826e0a01249SAl Viro if (!err) 1827e0a01249SAl Viro *path = nd.path; 1828e0a01249SAl Viro return err; 182916f18200SJosef 'Jeff' Sipek } 183016f18200SJosef 'Jeff' Sipek 1831057f6c01SJames Morris /* 1832057f6c01SJames Morris * Restricted form of lookup. Doesn't follow links, single-component only, 1833057f6c01SJames Morris * needs parent already locked. Doesn't follow mounts. 1834057f6c01SJames Morris * SMP-safe. 1835057f6c01SJames Morris */ 1836a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd) 18371da177e4SLinus Torvalds { 18384ac91378SJan Blunck return __lookup_hash(&nd->last, nd->path.dentry, nd); 18391da177e4SLinus Torvalds } 18401da177e4SLinus Torvalds 1841eead1911SChristoph Hellwig /** 1842a6b91919SRandy Dunlap * lookup_one_len - filesystem helper to lookup single pathname component 1843eead1911SChristoph Hellwig * @name: pathname component to lookup 1844eead1911SChristoph Hellwig * @base: base directory to lookup from 1845eead1911SChristoph Hellwig * @len: maximum length @len should be interpreted to 1846eead1911SChristoph Hellwig * 1847a6b91919SRandy Dunlap * Note that this routine is purely a helper for filesystem usage and should 1848a6b91919SRandy Dunlap * not be called by generic code. Also note that by using this function the 1849eead1911SChristoph Hellwig * nameidata argument is passed to the filesystem methods and a filesystem 1850eead1911SChristoph Hellwig * using this helper needs to be prepared for that. 1851eead1911SChristoph Hellwig */ 1852057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len) 1853057f6c01SJames Morris { 1854057f6c01SJames Morris struct qstr this; 18556a96ba54SAl Viro unsigned int c; 1856cda309deSMiklos Szeredi int err; 1857057f6c01SJames Morris 18582f9092e1SDavid Woodhouse WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex)); 18592f9092e1SDavid Woodhouse 18606a96ba54SAl Viro this.name = name; 18616a96ba54SAl Viro this.len = len; 18620145acc2SLinus Torvalds this.hash = full_name_hash(name, len); 18636a96ba54SAl Viro if (!len) 18646a96ba54SAl Viro return ERR_PTR(-EACCES); 18656a96ba54SAl Viro 18666a96ba54SAl Viro while (len--) { 18676a96ba54SAl Viro c = *(const unsigned char *)name++; 18686a96ba54SAl Viro if (c == '/' || c == '\0') 18696a96ba54SAl Viro return ERR_PTR(-EACCES); 18706a96ba54SAl Viro } 18715a202bcdSAl Viro /* 18725a202bcdSAl Viro * See if the low-level filesystem might want 18735a202bcdSAl Viro * to use its own hash.. 18745a202bcdSAl Viro */ 18755a202bcdSAl Viro if (base->d_flags & DCACHE_OP_HASH) { 18765a202bcdSAl Viro int err = base->d_op->d_hash(base, base->d_inode, &this); 18775a202bcdSAl Viro if (err < 0) 18785a202bcdSAl Viro return ERR_PTR(err); 18795a202bcdSAl Viro } 1880eead1911SChristoph Hellwig 1881cda309deSMiklos Szeredi err = inode_permission(base->d_inode, MAY_EXEC); 1882cda309deSMiklos Szeredi if (err) 1883cda309deSMiklos Szeredi return ERR_PTR(err); 1884cda309deSMiklos Szeredi 188549705b77SChristoph Hellwig return __lookup_hash(&this, base, NULL); 1886057f6c01SJames Morris } 1887057f6c01SJames Morris 18881fa1e7f6SAndy Whitcroft int user_path_at_empty(int dfd, const char __user *name, unsigned flags, 18891fa1e7f6SAndy Whitcroft struct path *path, int *empty) 18901da177e4SLinus Torvalds { 18912d8f3038SAl Viro struct nameidata nd; 18921fa1e7f6SAndy Whitcroft char *tmp = getname_flags(name, flags, empty); 18931da177e4SLinus Torvalds int err = PTR_ERR(tmp); 18941da177e4SLinus Torvalds if (!IS_ERR(tmp)) { 18952d8f3038SAl Viro 18962d8f3038SAl Viro BUG_ON(flags & LOOKUP_PARENT); 18972d8f3038SAl Viro 18982d8f3038SAl Viro err = do_path_lookup(dfd, tmp, flags, &nd); 18991da177e4SLinus Torvalds putname(tmp); 19002d8f3038SAl Viro if (!err) 19012d8f3038SAl Viro *path = nd.path; 19021da177e4SLinus Torvalds } 19031da177e4SLinus Torvalds return err; 19041da177e4SLinus Torvalds } 19051da177e4SLinus Torvalds 19061fa1e7f6SAndy Whitcroft int user_path_at(int dfd, const char __user *name, unsigned flags, 19071fa1e7f6SAndy Whitcroft struct path *path) 19081fa1e7f6SAndy Whitcroft { 1909f7493e5dSLinus Torvalds return user_path_at_empty(dfd, name, flags, path, NULL); 19101fa1e7f6SAndy Whitcroft } 19111fa1e7f6SAndy Whitcroft 19122ad94ae6SAl Viro static int user_path_parent(int dfd, const char __user *path, 19132ad94ae6SAl Viro struct nameidata *nd, char **name) 19142ad94ae6SAl Viro { 19152ad94ae6SAl Viro char *s = getname(path); 19162ad94ae6SAl Viro int error; 19172ad94ae6SAl Viro 19182ad94ae6SAl Viro if (IS_ERR(s)) 19192ad94ae6SAl Viro return PTR_ERR(s); 19202ad94ae6SAl Viro 19212ad94ae6SAl Viro error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd); 19222ad94ae6SAl Viro if (error) 19232ad94ae6SAl Viro putname(s); 19242ad94ae6SAl Viro else 19252ad94ae6SAl Viro *name = s; 19262ad94ae6SAl Viro 19272ad94ae6SAl Viro return error; 19282ad94ae6SAl Viro } 19292ad94ae6SAl Viro 19301da177e4SLinus Torvalds /* 19311da177e4SLinus Torvalds * It's inline, so penalty for filesystems that don't use sticky bit is 19321da177e4SLinus Torvalds * minimal. 19331da177e4SLinus Torvalds */ 19341da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode) 19351da177e4SLinus Torvalds { 19368e96e3b7SEric W. Biederman kuid_t fsuid = current_fsuid(); 1937da9592edSDavid Howells 19381da177e4SLinus Torvalds if (!(dir->i_mode & S_ISVTX)) 19391da177e4SLinus Torvalds return 0; 19408e96e3b7SEric W. Biederman if (uid_eq(inode->i_uid, fsuid)) 19411da177e4SLinus Torvalds return 0; 19428e96e3b7SEric W. Biederman if (uid_eq(dir->i_uid, fsuid)) 19431da177e4SLinus Torvalds return 0; 19441a48e2acSEric W. Biederman return !inode_capable(inode, CAP_FOWNER); 19451da177e4SLinus Torvalds } 19461da177e4SLinus Torvalds 19471da177e4SLinus Torvalds /* 19481da177e4SLinus Torvalds * Check whether we can remove a link victim from directory dir, check 19491da177e4SLinus Torvalds * whether the type of victim is right. 19501da177e4SLinus Torvalds * 1. We can't do it if dir is read-only (done in permission()) 19511da177e4SLinus Torvalds * 2. We should have write and exec permissions on dir 19521da177e4SLinus Torvalds * 3. We can't remove anything from append-only dir 19531da177e4SLinus Torvalds * 4. We can't do anything with immutable dir (done in permission()) 19541da177e4SLinus Torvalds * 5. If the sticky bit on dir is set we should either 19551da177e4SLinus Torvalds * a. be owner of dir, or 19561da177e4SLinus Torvalds * b. be owner of victim, or 19571da177e4SLinus Torvalds * c. have CAP_FOWNER capability 19581da177e4SLinus Torvalds * 6. If the victim is append-only or immutable we can't do antyhing with 19591da177e4SLinus Torvalds * links pointing to it. 19601da177e4SLinus Torvalds * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. 19611da177e4SLinus Torvalds * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. 19621da177e4SLinus Torvalds * 9. We can't remove a root or mountpoint. 19631da177e4SLinus Torvalds * 10. We don't allow removal of NFS sillyrenamed files; it's handled by 19641da177e4SLinus Torvalds * nfs_async_unlink(). 19651da177e4SLinus Torvalds */ 1966858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir) 19671da177e4SLinus Torvalds { 19681da177e4SLinus Torvalds int error; 19691da177e4SLinus Torvalds 19701da177e4SLinus Torvalds if (!victim->d_inode) 19711da177e4SLinus Torvalds return -ENOENT; 19721da177e4SLinus Torvalds 19731da177e4SLinus Torvalds BUG_ON(victim->d_parent->d_inode != dir); 1974cccc6bbaSAl Viro audit_inode_child(victim, dir); 19751da177e4SLinus Torvalds 1976f419a2e3SAl Viro error = inode_permission(dir, MAY_WRITE | MAY_EXEC); 19771da177e4SLinus Torvalds if (error) 19781da177e4SLinus Torvalds return error; 19791da177e4SLinus Torvalds if (IS_APPEND(dir)) 19801da177e4SLinus Torvalds return -EPERM; 19811da177e4SLinus Torvalds if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)|| 1982f9454548SHugh Dickins IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode)) 19831da177e4SLinus Torvalds return -EPERM; 19841da177e4SLinus Torvalds if (isdir) { 19851da177e4SLinus Torvalds if (!S_ISDIR(victim->d_inode->i_mode)) 19861da177e4SLinus Torvalds return -ENOTDIR; 19871da177e4SLinus Torvalds if (IS_ROOT(victim)) 19881da177e4SLinus Torvalds return -EBUSY; 19891da177e4SLinus Torvalds } else if (S_ISDIR(victim->d_inode->i_mode)) 19901da177e4SLinus Torvalds return -EISDIR; 19911da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 19921da177e4SLinus Torvalds return -ENOENT; 19931da177e4SLinus Torvalds if (victim->d_flags & DCACHE_NFSFS_RENAMED) 19941da177e4SLinus Torvalds return -EBUSY; 19951da177e4SLinus Torvalds return 0; 19961da177e4SLinus Torvalds } 19971da177e4SLinus Torvalds 19981da177e4SLinus Torvalds /* Check whether we can create an object with dentry child in directory 19991da177e4SLinus Torvalds * dir. 20001da177e4SLinus Torvalds * 1. We can't do it if child already exists (open has special treatment for 20011da177e4SLinus Torvalds * this case, but since we are inlined it's OK) 20021da177e4SLinus Torvalds * 2. We can't do it if dir is read-only (done in permission()) 20031da177e4SLinus Torvalds * 3. We should have write and exec permissions on dir 20041da177e4SLinus Torvalds * 4. We can't do it if dir is immutable (done in permission()) 20051da177e4SLinus Torvalds */ 2006a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child) 20071da177e4SLinus Torvalds { 20081da177e4SLinus Torvalds if (child->d_inode) 20091da177e4SLinus Torvalds return -EEXIST; 20101da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 20111da177e4SLinus Torvalds return -ENOENT; 2012f419a2e3SAl Viro return inode_permission(dir, MAY_WRITE | MAY_EXEC); 20131da177e4SLinus Torvalds } 20141da177e4SLinus Torvalds 20151da177e4SLinus Torvalds /* 20161da177e4SLinus Torvalds * p1 and p2 should be directories on the same fs. 20171da177e4SLinus Torvalds */ 20181da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2) 20191da177e4SLinus Torvalds { 20201da177e4SLinus Torvalds struct dentry *p; 20211da177e4SLinus Torvalds 20221da177e4SLinus Torvalds if (p1 == p2) { 2023f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 20241da177e4SLinus Torvalds return NULL; 20251da177e4SLinus Torvalds } 20261da177e4SLinus Torvalds 2027a11f3a05SArjan van de Ven mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 20281da177e4SLinus Torvalds 2029e2761a11SOGAWA Hirofumi p = d_ancestor(p2, p1); 2030e2761a11SOGAWA Hirofumi if (p) { 2031f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT); 2032f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD); 20331da177e4SLinus Torvalds return p; 20341da177e4SLinus Torvalds } 20351da177e4SLinus Torvalds 2036e2761a11SOGAWA Hirofumi p = d_ancestor(p1, p2); 2037e2761a11SOGAWA Hirofumi if (p) { 2038f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 2039f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 20401da177e4SLinus Torvalds return p; 20411da177e4SLinus Torvalds } 20421da177e4SLinus Torvalds 2043f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 2044f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 20451da177e4SLinus Torvalds return NULL; 20461da177e4SLinus Torvalds } 20471da177e4SLinus Torvalds 20481da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2) 20491da177e4SLinus Torvalds { 20501b1dcc1bSJes Sorensen mutex_unlock(&p1->d_inode->i_mutex); 20511da177e4SLinus Torvalds if (p1 != p2) { 20521b1dcc1bSJes Sorensen mutex_unlock(&p2->d_inode->i_mutex); 2053a11f3a05SArjan van de Ven mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 20541da177e4SLinus Torvalds } 20551da177e4SLinus Torvalds } 20561da177e4SLinus Torvalds 20574acdaf27SAl Viro int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode, 20581da177e4SLinus Torvalds struct nameidata *nd) 20591da177e4SLinus Torvalds { 2060a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 20611da177e4SLinus Torvalds 20621da177e4SLinus Torvalds if (error) 20631da177e4SLinus Torvalds return error; 20641da177e4SLinus Torvalds 2065acfa4380SAl Viro if (!dir->i_op->create) 20661da177e4SLinus Torvalds return -EACCES; /* shouldn't it be ENOSYS? */ 20671da177e4SLinus Torvalds mode &= S_IALLUGO; 20681da177e4SLinus Torvalds mode |= S_IFREG; 20691da177e4SLinus Torvalds error = security_inode_create(dir, dentry, mode); 20701da177e4SLinus Torvalds if (error) 20711da177e4SLinus Torvalds return error; 20721da177e4SLinus Torvalds error = dir->i_op->create(dir, dentry, mode, nd); 2073a74574aaSStephen Smalley if (!error) 2074f38aa942SAmy Griffis fsnotify_create(dir, dentry); 20751da177e4SLinus Torvalds return error; 20761da177e4SLinus Torvalds } 20771da177e4SLinus Torvalds 207873d049a4SAl Viro static int may_open(struct path *path, int acc_mode, int flag) 20791da177e4SLinus Torvalds { 20803fb64190SChristoph Hellwig struct dentry *dentry = path->dentry; 20811da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 20821da177e4SLinus Torvalds int error; 20831da177e4SLinus Torvalds 2084bcda7652SAl Viro /* O_PATH? */ 2085bcda7652SAl Viro if (!acc_mode) 2086bcda7652SAl Viro return 0; 2087bcda7652SAl Viro 20881da177e4SLinus Torvalds if (!inode) 20891da177e4SLinus Torvalds return -ENOENT; 20901da177e4SLinus Torvalds 2091c8fe8f30SChristoph Hellwig switch (inode->i_mode & S_IFMT) { 2092c8fe8f30SChristoph Hellwig case S_IFLNK: 20931da177e4SLinus Torvalds return -ELOOP; 2094c8fe8f30SChristoph Hellwig case S_IFDIR: 2095c8fe8f30SChristoph Hellwig if (acc_mode & MAY_WRITE) 20961da177e4SLinus Torvalds return -EISDIR; 2097c8fe8f30SChristoph Hellwig break; 2098c8fe8f30SChristoph Hellwig case S_IFBLK: 2099c8fe8f30SChristoph Hellwig case S_IFCHR: 21003fb64190SChristoph Hellwig if (path->mnt->mnt_flags & MNT_NODEV) 21011da177e4SLinus Torvalds return -EACCES; 2102c8fe8f30SChristoph Hellwig /*FALLTHRU*/ 2103c8fe8f30SChristoph Hellwig case S_IFIFO: 2104c8fe8f30SChristoph Hellwig case S_IFSOCK: 21051da177e4SLinus Torvalds flag &= ~O_TRUNC; 2106c8fe8f30SChristoph Hellwig break; 21074a3fd211SDave Hansen } 2108b41572e9SDave Hansen 21093fb64190SChristoph Hellwig error = inode_permission(inode, acc_mode); 2110b41572e9SDave Hansen if (error) 2111b41572e9SDave Hansen return error; 21126146f0d5SMimi Zohar 21131da177e4SLinus Torvalds /* 21141da177e4SLinus Torvalds * An append-only file must be opened in append mode for writing. 21151da177e4SLinus Torvalds */ 21161da177e4SLinus Torvalds if (IS_APPEND(inode)) { 21178737c930SAl Viro if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND)) 21187715b521SAl Viro return -EPERM; 21191da177e4SLinus Torvalds if (flag & O_TRUNC) 21207715b521SAl Viro return -EPERM; 21211da177e4SLinus Torvalds } 21221da177e4SLinus Torvalds 21231da177e4SLinus Torvalds /* O_NOATIME can only be set by the owner or superuser */ 21242e149670SSerge E. Hallyn if (flag & O_NOATIME && !inode_owner_or_capable(inode)) 21257715b521SAl Viro return -EPERM; 21261da177e4SLinus Torvalds 2127f3c7691eSJ. Bruce Fields return 0; 21287715b521SAl Viro } 21297715b521SAl Viro 2130e1181ee6SJeff Layton static int handle_truncate(struct file *filp) 21317715b521SAl Viro { 2132e1181ee6SJeff Layton struct path *path = &filp->f_path; 21337715b521SAl Viro struct inode *inode = path->dentry->d_inode; 21347715b521SAl Viro int error = get_write_access(inode); 21351da177e4SLinus Torvalds if (error) 21367715b521SAl Viro return error; 21371da177e4SLinus Torvalds /* 21381da177e4SLinus Torvalds * Refuse to truncate files with mandatory locks held on them. 21391da177e4SLinus Torvalds */ 21401da177e4SLinus Torvalds error = locks_verify_locked(inode); 2141be6d3e56SKentaro Takeda if (!error) 2142ea0d3ab2STetsuo Handa error = security_path_truncate(path); 21431da177e4SLinus Torvalds if (!error) { 21447715b521SAl Viro error = do_truncate(path->dentry, 0, 2145d139d7ffSMiklos Szeredi ATTR_MTIME|ATTR_CTIME|ATTR_OPEN, 2146e1181ee6SJeff Layton filp); 21471da177e4SLinus Torvalds } 21481da177e4SLinus Torvalds put_write_access(inode); 2149acd0c935SMimi Zohar return error; 21501da177e4SLinus Torvalds } 21511da177e4SLinus Torvalds 2152d57999e1SDave Hansen static inline int open_to_namei_flags(int flag) 2153d57999e1SDave Hansen { 21548a5e929dSAl Viro if ((flag & O_ACCMODE) == 3) 21558a5e929dSAl Viro flag--; 2156d57999e1SDave Hansen return flag; 2157d57999e1SDave Hansen } 2158d57999e1SDave Hansen 215931e6b01fSNick Piggin /* 2160fe2d35ffSAl Viro * Handle the last step of open() 216131e6b01fSNick Piggin */ 2162fb1cc555SAl Viro static struct file *do_last(struct nameidata *nd, struct path *path, 2163c3e380b0SAl Viro const struct open_flags *op, const char *pathname) 2164fb1cc555SAl Viro { 2165a1e28038SAl Viro struct dentry *dir = nd->path.dentry; 21666c0d46c4SAl Viro struct dentry *dentry; 2167ca344a89SAl Viro int open_flag = op->open_flag; 21686c0d46c4SAl Viro int will_truncate = open_flag & O_TRUNC; 2169ca344a89SAl Viro int want_write = 0; 2170bcda7652SAl Viro int acc_mode = op->acc_mode; 2171fb1cc555SAl Viro struct file *filp; 217216c2cd71SAl Viro int error; 2173fb1cc555SAl Viro 2174c3e380b0SAl Viro nd->flags &= ~LOOKUP_PARENT; 2175c3e380b0SAl Viro nd->flags |= op->intent; 2176c3e380b0SAl Viro 21771f36f774SAl Viro switch (nd->last_type) { 21781f36f774SAl Viro case LAST_DOTDOT: 2179176306f5SNeil Brown case LAST_DOT: 2180fe2d35ffSAl Viro error = handle_dots(nd, nd->last_type); 2181fe2d35ffSAl Viro if (error) 2182fe2d35ffSAl Viro return ERR_PTR(error); 21831f36f774SAl Viro /* fallthrough */ 21841f36f774SAl Viro case LAST_ROOT: 21859f1fafeeSAl Viro error = complete_walk(nd); 218616c2cd71SAl Viro if (error) 21879f1fafeeSAl Viro return ERR_PTR(error); 2188fe2d35ffSAl Viro audit_inode(pathname, nd->path.dentry); 2189ca344a89SAl Viro if (open_flag & O_CREAT) { 219016c2cd71SAl Viro error = -EISDIR; 21911f36f774SAl Viro goto exit; 2192fe2d35ffSAl Viro } 2193fe2d35ffSAl Viro goto ok; 21941f36f774SAl Viro case LAST_BIND: 21959f1fafeeSAl Viro error = complete_walk(nd); 219616c2cd71SAl Viro if (error) 21979f1fafeeSAl Viro return ERR_PTR(error); 21981f36f774SAl Viro audit_inode(pathname, dir); 21991f36f774SAl Viro goto ok; 22001f36f774SAl Viro } 2201a2c36b45SAl Viro 2202ca344a89SAl Viro if (!(open_flag & O_CREAT)) { 2203bcda7652SAl Viro int symlink_ok = 0; 2204fe2d35ffSAl Viro if (nd->last.name[nd->last.len]) 2205fe2d35ffSAl Viro nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 2206bcda7652SAl Viro if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW)) 2207bcda7652SAl Viro symlink_ok = 1; 2208fe2d35ffSAl Viro /* we _can_ be in RCU mode here */ 2209ce57dfc1SAl Viro error = walk_component(nd, path, &nd->last, LAST_NORM, 2210ce57dfc1SAl Viro !symlink_ok); 2211ce57dfc1SAl Viro if (error < 0) 2212fe2d35ffSAl Viro return ERR_PTR(error); 2213ce57dfc1SAl Viro if (error) /* symlink */ 2214fe2d35ffSAl Viro return NULL; 2215fe2d35ffSAl Viro /* sayonara */ 22169f1fafeeSAl Viro error = complete_walk(nd); 22179f1fafeeSAl Viro if (error) 22187f6c7e62SMiklos Szeredi return ERR_PTR(error); 2219fe2d35ffSAl Viro 2220fe2d35ffSAl Viro error = -ENOTDIR; 2221fe2d35ffSAl Viro if (nd->flags & LOOKUP_DIRECTORY) { 2222ce57dfc1SAl Viro if (!nd->inode->i_op->lookup) 2223fe2d35ffSAl Viro goto exit; 2224fe2d35ffSAl Viro } 2225fe2d35ffSAl Viro audit_inode(pathname, nd->path.dentry); 2226fe2d35ffSAl Viro goto ok; 2227fe2d35ffSAl Viro } 2228fe2d35ffSAl Viro 2229fe2d35ffSAl Viro /* create side of things */ 2230a3fbbde7SAl Viro /* 2231a3fbbde7SAl Viro * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED has been 2232a3fbbde7SAl Viro * cleared when we got to the last component we are about to look up 2233a3fbbde7SAl Viro */ 22349f1fafeeSAl Viro error = complete_walk(nd); 22359f1fafeeSAl Viro if (error) 22369f1fafeeSAl Viro return ERR_PTR(error); 2237fe2d35ffSAl Viro 2238fe2d35ffSAl Viro audit_inode(pathname, dir); 223916c2cd71SAl Viro error = -EISDIR; 22401f36f774SAl Viro /* trailing slashes? */ 224131e6b01fSNick Piggin if (nd->last.name[nd->last.len]) 22421f36f774SAl Viro goto exit; 22431f36f774SAl Viro 2244a1e28038SAl Viro mutex_lock(&dir->d_inode->i_mutex); 2245a1e28038SAl Viro 22466c0d46c4SAl Viro dentry = lookup_hash(nd); 22476c0d46c4SAl Viro error = PTR_ERR(dentry); 22486c0d46c4SAl Viro if (IS_ERR(dentry)) { 2249fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2250fb1cc555SAl Viro goto exit; 2251fb1cc555SAl Viro } 2252fb1cc555SAl Viro 22536c0d46c4SAl Viro path->dentry = dentry; 22546c0d46c4SAl Viro path->mnt = nd->path.mnt; 22556c0d46c4SAl Viro 2256fb1cc555SAl Viro /* Negative dentry, just create the file */ 22576c0d46c4SAl Viro if (!dentry->d_inode) { 2258a218d0fdSAl Viro umode_t mode = op->mode; 22596c0d46c4SAl Viro if (!IS_POSIXACL(dir->d_inode)) 22606c0d46c4SAl Viro mode &= ~current_umask(); 2261fb1cc555SAl Viro /* 2262fb1cc555SAl Viro * This write is needed to ensure that a 22636c0d46c4SAl Viro * rw->ro transition does not occur between 2264fb1cc555SAl Viro * the time when the file is created and when 2265fb1cc555SAl Viro * a permanent write count is taken through 2266fb1cc555SAl Viro * the 'struct file' in nameidata_to_filp(). 2267fb1cc555SAl Viro */ 2268fb1cc555SAl Viro error = mnt_want_write(nd->path.mnt); 2269fb1cc555SAl Viro if (error) 2270fb1cc555SAl Viro goto exit_mutex_unlock; 2271ca344a89SAl Viro want_write = 1; 22729b44f1b3SAl Viro /* Don't check for write permission, don't truncate */ 2273ca344a89SAl Viro open_flag &= ~O_TRUNC; 22746c0d46c4SAl Viro will_truncate = 0; 2275bcda7652SAl Viro acc_mode = MAY_OPEN; 22766c0d46c4SAl Viro error = security_path_mknod(&nd->path, dentry, mode, 0); 22776c0d46c4SAl Viro if (error) 22786c0d46c4SAl Viro goto exit_mutex_unlock; 22796c0d46c4SAl Viro error = vfs_create(dir->d_inode, dentry, mode, nd); 22806c0d46c4SAl Viro if (error) 22816c0d46c4SAl Viro goto exit_mutex_unlock; 22826c0d46c4SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 22836c0d46c4SAl Viro dput(nd->path.dentry); 22846c0d46c4SAl Viro nd->path.dentry = dentry; 2285ca344a89SAl Viro goto common; 2286fb1cc555SAl Viro } 2287fb1cc555SAl Viro 2288fb1cc555SAl Viro /* 2289fb1cc555SAl Viro * It already exists. 2290fb1cc555SAl Viro */ 2291fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2292fb1cc555SAl Viro audit_inode(pathname, path->dentry); 2293fb1cc555SAl Viro 2294fb1cc555SAl Viro error = -EEXIST; 2295ca344a89SAl Viro if (open_flag & O_EXCL) 2296fb1cc555SAl Viro goto exit_dput; 2297fb1cc555SAl Viro 22989875cf80SDavid Howells error = follow_managed(path, nd->flags); 22999875cf80SDavid Howells if (error < 0) 2300fb1cc555SAl Viro goto exit_dput; 2301fb1cc555SAl Viro 2302a3fbbde7SAl Viro if (error) 2303a3fbbde7SAl Viro nd->flags |= LOOKUP_JUMPED; 2304a3fbbde7SAl Viro 2305fb1cc555SAl Viro error = -ENOENT; 2306fb1cc555SAl Viro if (!path->dentry->d_inode) 2307fb1cc555SAl Viro goto exit_dput; 23089e67f361SAl Viro 23099e67f361SAl Viro if (path->dentry->d_inode->i_op->follow_link) 2310fb1cc555SAl Viro return NULL; 2311fb1cc555SAl Viro 2312fb1cc555SAl Viro path_to_nameidata(path, nd); 231331e6b01fSNick Piggin nd->inode = path->dentry->d_inode; 2314a3fbbde7SAl Viro /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */ 2315a3fbbde7SAl Viro error = complete_walk(nd); 2316a3fbbde7SAl Viro if (error) 2317097b180cSMiklos Szeredi return ERR_PTR(error); 2318fb1cc555SAl Viro error = -EISDIR; 231931e6b01fSNick Piggin if (S_ISDIR(nd->inode->i_mode)) 2320fb1cc555SAl Viro goto exit; 232167ee3ad2SAl Viro ok: 23226c0d46c4SAl Viro if (!S_ISREG(nd->inode->i_mode)) 23236c0d46c4SAl Viro will_truncate = 0; 23246c0d46c4SAl Viro 23250f9d1a10SAl Viro if (will_truncate) { 23260f9d1a10SAl Viro error = mnt_want_write(nd->path.mnt); 23270f9d1a10SAl Viro if (error) 23280f9d1a10SAl Viro goto exit; 2329ca344a89SAl Viro want_write = 1; 23300f9d1a10SAl Viro } 2331ca344a89SAl Viro common: 2332bcda7652SAl Viro error = may_open(&nd->path, acc_mode, open_flag); 2333ca344a89SAl Viro if (error) 23340f9d1a10SAl Viro goto exit; 23350f9d1a10SAl Viro filp = nameidata_to_filp(nd); 23360f9d1a10SAl Viro if (!IS_ERR(filp)) { 23370f9d1a10SAl Viro error = ima_file_check(filp, op->acc_mode); 23380f9d1a10SAl Viro if (error) { 23390f9d1a10SAl Viro fput(filp); 23400f9d1a10SAl Viro filp = ERR_PTR(error); 23410f9d1a10SAl Viro } 23420f9d1a10SAl Viro } 23430f9d1a10SAl Viro if (!IS_ERR(filp)) { 23440f9d1a10SAl Viro if (will_truncate) { 23450f9d1a10SAl Viro error = handle_truncate(filp); 23460f9d1a10SAl Viro if (error) { 23470f9d1a10SAl Viro fput(filp); 23480f9d1a10SAl Viro filp = ERR_PTR(error); 23490f9d1a10SAl Viro } 23500f9d1a10SAl Viro } 23510f9d1a10SAl Viro } 2352ca344a89SAl Viro out: 2353ca344a89SAl Viro if (want_write) 23540f9d1a10SAl Viro mnt_drop_write(nd->path.mnt); 23550f9d1a10SAl Viro path_put(&nd->path); 2356fb1cc555SAl Viro return filp; 2357fb1cc555SAl Viro 2358fb1cc555SAl Viro exit_mutex_unlock: 2359fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2360fb1cc555SAl Viro exit_dput: 2361fb1cc555SAl Viro path_put_conditional(path, nd); 2362fb1cc555SAl Viro exit: 2363ca344a89SAl Viro filp = ERR_PTR(error); 2364ca344a89SAl Viro goto out; 2365fb1cc555SAl Viro } 2366fb1cc555SAl Viro 236713aab428SAl Viro static struct file *path_openat(int dfd, const char *pathname, 236873d049a4SAl Viro struct nameidata *nd, const struct open_flags *op, int flags) 23691da177e4SLinus Torvalds { 2370fe2d35ffSAl Viro struct file *base = NULL; 23714a3fd211SDave Hansen struct file *filp; 23729850c056SAl Viro struct path path; 237313aab428SAl Viro int error; 237431e6b01fSNick Piggin 237531e6b01fSNick Piggin filp = get_empty_filp(); 237631e6b01fSNick Piggin if (!filp) 237731e6b01fSNick Piggin return ERR_PTR(-ENFILE); 237831e6b01fSNick Piggin 237947c805dcSAl Viro filp->f_flags = op->open_flag; 238073d049a4SAl Viro nd->intent.open.file = filp; 238173d049a4SAl Viro nd->intent.open.flags = open_to_namei_flags(op->open_flag); 238273d049a4SAl Viro nd->intent.open.create_mode = op->mode; 238331e6b01fSNick Piggin 238473d049a4SAl Viro error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base); 238531e6b01fSNick Piggin if (unlikely(error)) 238613aab428SAl Viro goto out_filp; 238731e6b01fSNick Piggin 2388fe2d35ffSAl Viro current->total_link_count = 0; 238973d049a4SAl Viro error = link_path_walk(pathname, nd); 239031e6b01fSNick Piggin if (unlikely(error)) 239131e6b01fSNick Piggin goto out_filp; 23921da177e4SLinus Torvalds 239373d049a4SAl Viro filp = do_last(nd, &path, op, pathname); 2394806b681cSAl Viro while (unlikely(!filp)) { /* trailing symlink */ 23957b9337aaSNick Piggin struct path link = path; 2396def4af30SAl Viro void *cookie; 2397574197e0SAl Viro if (!(nd->flags & LOOKUP_FOLLOW)) { 239873d049a4SAl Viro path_put_conditional(&path, nd); 239973d049a4SAl Viro path_put(&nd->path); 240040b39136SAl Viro filp = ERR_PTR(-ELOOP); 240140b39136SAl Viro break; 240240b39136SAl Viro } 240373d049a4SAl Viro nd->flags |= LOOKUP_PARENT; 240473d049a4SAl Viro nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL); 2405574197e0SAl Viro error = follow_link(&link, nd, &cookie); 2406c3e380b0SAl Viro if (unlikely(error)) 2407f1afe9efSAl Viro filp = ERR_PTR(error); 2408c3e380b0SAl Viro else 240973d049a4SAl Viro filp = do_last(nd, &path, op, pathname); 2410574197e0SAl Viro put_link(nd, &link, cookie); 2411806b681cSAl Viro } 241210fa8e62SAl Viro out: 241373d049a4SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) 241473d049a4SAl Viro path_put(&nd->root); 2415fe2d35ffSAl Viro if (base) 2416fe2d35ffSAl Viro fput(base); 241773d049a4SAl Viro release_open_intent(nd); 241810fa8e62SAl Viro return filp; 24191da177e4SLinus Torvalds 242031e6b01fSNick Piggin out_filp: 242110fa8e62SAl Viro filp = ERR_PTR(error); 242210fa8e62SAl Viro goto out; 2423de459215SKirill Korotaev } 24241da177e4SLinus Torvalds 242513aab428SAl Viro struct file *do_filp_open(int dfd, const char *pathname, 242613aab428SAl Viro const struct open_flags *op, int flags) 242713aab428SAl Viro { 242873d049a4SAl Viro struct nameidata nd; 242913aab428SAl Viro struct file *filp; 243013aab428SAl Viro 243173d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU); 243213aab428SAl Viro if (unlikely(filp == ERR_PTR(-ECHILD))) 243373d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags); 243413aab428SAl Viro if (unlikely(filp == ERR_PTR(-ESTALE))) 243573d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL); 243613aab428SAl Viro return filp; 243713aab428SAl Viro } 243813aab428SAl Viro 243973d049a4SAl Viro struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt, 244073d049a4SAl Viro const char *name, const struct open_flags *op, int flags) 244173d049a4SAl Viro { 244273d049a4SAl Viro struct nameidata nd; 244373d049a4SAl Viro struct file *file; 244473d049a4SAl Viro 244573d049a4SAl Viro nd.root.mnt = mnt; 244673d049a4SAl Viro nd.root.dentry = dentry; 244773d049a4SAl Viro 244873d049a4SAl Viro flags |= LOOKUP_ROOT; 244973d049a4SAl Viro 2450bcda7652SAl Viro if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN) 245173d049a4SAl Viro return ERR_PTR(-ELOOP); 245273d049a4SAl Viro 245373d049a4SAl Viro file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU); 245473d049a4SAl Viro if (unlikely(file == ERR_PTR(-ECHILD))) 245573d049a4SAl Viro file = path_openat(-1, name, &nd, op, flags); 245673d049a4SAl Viro if (unlikely(file == ERR_PTR(-ESTALE))) 245773d049a4SAl Viro file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL); 245873d049a4SAl Viro return file; 245973d049a4SAl Viro } 246073d049a4SAl Viro 2461ed75e95dSAl Viro struct dentry *kern_path_create(int dfd, const char *pathname, struct path *path, int is_dir) 24621da177e4SLinus Torvalds { 2463c663e5d8SChristoph Hellwig struct dentry *dentry = ERR_PTR(-EEXIST); 2464ed75e95dSAl Viro struct nameidata nd; 2465ed75e95dSAl Viro int error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd); 2466ed75e95dSAl Viro if (error) 2467ed75e95dSAl Viro return ERR_PTR(error); 24681da177e4SLinus Torvalds 2469c663e5d8SChristoph Hellwig /* 2470c663e5d8SChristoph Hellwig * Yucky last component or no last component at all? 2471c663e5d8SChristoph Hellwig * (foo/., foo/.., /////) 2472c663e5d8SChristoph Hellwig */ 2473ed75e95dSAl Viro if (nd.last_type != LAST_NORM) 2474ed75e95dSAl Viro goto out; 2475ed75e95dSAl Viro nd.flags &= ~LOOKUP_PARENT; 2476ed75e95dSAl Viro nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL; 2477ed75e95dSAl Viro nd.intent.open.flags = O_EXCL; 2478c663e5d8SChristoph Hellwig 2479c663e5d8SChristoph Hellwig /* 2480c663e5d8SChristoph Hellwig * Do the final lookup. 2481c663e5d8SChristoph Hellwig */ 2482ed75e95dSAl Viro mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 2483ed75e95dSAl Viro dentry = lookup_hash(&nd); 24841da177e4SLinus Torvalds if (IS_ERR(dentry)) 24851da177e4SLinus Torvalds goto fail; 2486c663e5d8SChristoph Hellwig 2487e9baf6e5SAl Viro if (dentry->d_inode) 2488e9baf6e5SAl Viro goto eexist; 2489c663e5d8SChristoph Hellwig /* 2490c663e5d8SChristoph Hellwig * Special case - lookup gave negative, but... we had foo/bar/ 2491c663e5d8SChristoph Hellwig * From the vfs_mknod() POV we just have a negative dentry - 2492c663e5d8SChristoph Hellwig * all is fine. Let's be bastards - you had / on the end, you've 2493c663e5d8SChristoph Hellwig * been asking for (non-existent) directory. -ENOENT for you. 2494c663e5d8SChristoph Hellwig */ 2495ed75e95dSAl Viro if (unlikely(!is_dir && nd.last.name[nd.last.len])) { 24961da177e4SLinus Torvalds dput(dentry); 24971da177e4SLinus Torvalds dentry = ERR_PTR(-ENOENT); 2498ed75e95dSAl Viro goto fail; 2499e9baf6e5SAl Viro } 2500ed75e95dSAl Viro *path = nd.path; 2501e9baf6e5SAl Viro return dentry; 2502e9baf6e5SAl Viro eexist: 2503e9baf6e5SAl Viro dput(dentry); 2504e9baf6e5SAl Viro dentry = ERR_PTR(-EEXIST); 25051da177e4SLinus Torvalds fail: 2506dae6ad8fSAl Viro mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 2507ed75e95dSAl Viro out: 2508dae6ad8fSAl Viro path_put(&nd.path); 2509ed75e95dSAl Viro return dentry; 2510dae6ad8fSAl Viro } 2511dae6ad8fSAl Viro EXPORT_SYMBOL(kern_path_create); 2512dae6ad8fSAl Viro 2513dae6ad8fSAl Viro struct dentry *user_path_create(int dfd, const char __user *pathname, struct path *path, int is_dir) 2514dae6ad8fSAl Viro { 2515dae6ad8fSAl Viro char *tmp = getname(pathname); 2516dae6ad8fSAl Viro struct dentry *res; 2517dae6ad8fSAl Viro if (IS_ERR(tmp)) 2518dae6ad8fSAl Viro return ERR_CAST(tmp); 2519dae6ad8fSAl Viro res = kern_path_create(dfd, tmp, path, is_dir); 2520dae6ad8fSAl Viro putname(tmp); 2521dae6ad8fSAl Viro return res; 2522dae6ad8fSAl Viro } 2523dae6ad8fSAl Viro EXPORT_SYMBOL(user_path_create); 2524dae6ad8fSAl Viro 25251a67aafbSAl Viro int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev) 25261da177e4SLinus Torvalds { 2527a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 25281da177e4SLinus Torvalds 25291da177e4SLinus Torvalds if (error) 25301da177e4SLinus Torvalds return error; 25311da177e4SLinus Torvalds 2532975d6b39SEric W. Biederman if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD)) 25331da177e4SLinus Torvalds return -EPERM; 25341da177e4SLinus Torvalds 2535acfa4380SAl Viro if (!dir->i_op->mknod) 25361da177e4SLinus Torvalds return -EPERM; 25371da177e4SLinus Torvalds 253808ce5f16SSerge E. Hallyn error = devcgroup_inode_mknod(mode, dev); 253908ce5f16SSerge E. Hallyn if (error) 254008ce5f16SSerge E. Hallyn return error; 254108ce5f16SSerge E. Hallyn 25421da177e4SLinus Torvalds error = security_inode_mknod(dir, dentry, mode, dev); 25431da177e4SLinus Torvalds if (error) 25441da177e4SLinus Torvalds return error; 25451da177e4SLinus Torvalds 25461da177e4SLinus Torvalds error = dir->i_op->mknod(dir, dentry, mode, dev); 2547a74574aaSStephen Smalley if (!error) 2548f38aa942SAmy Griffis fsnotify_create(dir, dentry); 25491da177e4SLinus Torvalds return error; 25501da177e4SLinus Torvalds } 25511da177e4SLinus Torvalds 2552f69aac00SAl Viro static int may_mknod(umode_t mode) 2553463c3197SDave Hansen { 2554463c3197SDave Hansen switch (mode & S_IFMT) { 2555463c3197SDave Hansen case S_IFREG: 2556463c3197SDave Hansen case S_IFCHR: 2557463c3197SDave Hansen case S_IFBLK: 2558463c3197SDave Hansen case S_IFIFO: 2559463c3197SDave Hansen case S_IFSOCK: 2560463c3197SDave Hansen case 0: /* zero mode translates to S_IFREG */ 2561463c3197SDave Hansen return 0; 2562463c3197SDave Hansen case S_IFDIR: 2563463c3197SDave Hansen return -EPERM; 2564463c3197SDave Hansen default: 2565463c3197SDave Hansen return -EINVAL; 2566463c3197SDave Hansen } 2567463c3197SDave Hansen } 2568463c3197SDave Hansen 25698208a22bSAl Viro SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode, 25702e4d0924SHeiko Carstens unsigned, dev) 25711da177e4SLinus Torvalds { 25721da177e4SLinus Torvalds struct dentry *dentry; 2573dae6ad8fSAl Viro struct path path; 2574dae6ad8fSAl Viro int error; 25751da177e4SLinus Torvalds 25761da177e4SLinus Torvalds if (S_ISDIR(mode)) 25771da177e4SLinus Torvalds return -EPERM; 25781da177e4SLinus Torvalds 2579dae6ad8fSAl Viro dentry = user_path_create(dfd, filename, &path, 0); 2580dae6ad8fSAl Viro if (IS_ERR(dentry)) 2581dae6ad8fSAl Viro return PTR_ERR(dentry); 25822ad94ae6SAl Viro 2583dae6ad8fSAl Viro if (!IS_POSIXACL(path.dentry->d_inode)) 2584ce3b0f8dSAl Viro mode &= ~current_umask(); 2585463c3197SDave Hansen error = may_mknod(mode); 2586463c3197SDave Hansen if (error) 2587463c3197SDave Hansen goto out_dput; 2588dae6ad8fSAl Viro error = mnt_want_write(path.mnt); 2589463c3197SDave Hansen if (error) 2590463c3197SDave Hansen goto out_dput; 2591dae6ad8fSAl Viro error = security_path_mknod(&path, dentry, mode, dev); 2592be6d3e56SKentaro Takeda if (error) 2593be6d3e56SKentaro Takeda goto out_drop_write; 25941da177e4SLinus Torvalds switch (mode & S_IFMT) { 25951da177e4SLinus Torvalds case 0: case S_IFREG: 2596dae6ad8fSAl Viro error = vfs_create(path.dentry->d_inode,dentry,mode,NULL); 25971da177e4SLinus Torvalds break; 25981da177e4SLinus Torvalds case S_IFCHR: case S_IFBLK: 2599dae6ad8fSAl Viro error = vfs_mknod(path.dentry->d_inode,dentry,mode, 26001da177e4SLinus Torvalds new_decode_dev(dev)); 26011da177e4SLinus Torvalds break; 26021da177e4SLinus Torvalds case S_IFIFO: case S_IFSOCK: 2603dae6ad8fSAl Viro error = vfs_mknod(path.dentry->d_inode,dentry,mode,0); 26041da177e4SLinus Torvalds break; 26051da177e4SLinus Torvalds } 2606be6d3e56SKentaro Takeda out_drop_write: 2607dae6ad8fSAl Viro mnt_drop_write(path.mnt); 2608463c3197SDave Hansen out_dput: 26091da177e4SLinus Torvalds dput(dentry); 2610dae6ad8fSAl Viro mutex_unlock(&path.dentry->d_inode->i_mutex); 2611dae6ad8fSAl Viro path_put(&path); 26121da177e4SLinus Torvalds 26131da177e4SLinus Torvalds return error; 26141da177e4SLinus Torvalds } 26151da177e4SLinus Torvalds 26168208a22bSAl Viro SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev) 26175590ff0dSUlrich Drepper { 26185590ff0dSUlrich Drepper return sys_mknodat(AT_FDCWD, filename, mode, dev); 26195590ff0dSUlrich Drepper } 26205590ff0dSUlrich Drepper 262118bb1db3SAl Viro int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) 26221da177e4SLinus Torvalds { 2623a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 26248de52778SAl Viro unsigned max_links = dir->i_sb->s_max_links; 26251da177e4SLinus Torvalds 26261da177e4SLinus Torvalds if (error) 26271da177e4SLinus Torvalds return error; 26281da177e4SLinus Torvalds 2629acfa4380SAl Viro if (!dir->i_op->mkdir) 26301da177e4SLinus Torvalds return -EPERM; 26311da177e4SLinus Torvalds 26321da177e4SLinus Torvalds mode &= (S_IRWXUGO|S_ISVTX); 26331da177e4SLinus Torvalds error = security_inode_mkdir(dir, dentry, mode); 26341da177e4SLinus Torvalds if (error) 26351da177e4SLinus Torvalds return error; 26361da177e4SLinus Torvalds 26378de52778SAl Viro if (max_links && dir->i_nlink >= max_links) 26388de52778SAl Viro return -EMLINK; 26398de52778SAl Viro 26401da177e4SLinus Torvalds error = dir->i_op->mkdir(dir, dentry, mode); 2641a74574aaSStephen Smalley if (!error) 2642f38aa942SAmy Griffis fsnotify_mkdir(dir, dentry); 26431da177e4SLinus Torvalds return error; 26441da177e4SLinus Torvalds } 26451da177e4SLinus Torvalds 2646a218d0fdSAl Viro SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode) 26471da177e4SLinus Torvalds { 26486902d925SDave Hansen struct dentry *dentry; 2649dae6ad8fSAl Viro struct path path; 2650dae6ad8fSAl Viro int error; 26511da177e4SLinus Torvalds 2652dae6ad8fSAl Viro dentry = user_path_create(dfd, pathname, &path, 1); 26536902d925SDave Hansen if (IS_ERR(dentry)) 2654dae6ad8fSAl Viro return PTR_ERR(dentry); 26556902d925SDave Hansen 2656dae6ad8fSAl Viro if (!IS_POSIXACL(path.dentry->d_inode)) 2657ce3b0f8dSAl Viro mode &= ~current_umask(); 2658dae6ad8fSAl Viro error = mnt_want_write(path.mnt); 2659463c3197SDave Hansen if (error) 2660463c3197SDave Hansen goto out_dput; 2661dae6ad8fSAl Viro error = security_path_mkdir(&path, dentry, mode); 2662be6d3e56SKentaro Takeda if (error) 2663be6d3e56SKentaro Takeda goto out_drop_write; 2664dae6ad8fSAl Viro error = vfs_mkdir(path.dentry->d_inode, dentry, mode); 2665be6d3e56SKentaro Takeda out_drop_write: 2666dae6ad8fSAl Viro mnt_drop_write(path.mnt); 2667463c3197SDave Hansen out_dput: 26681da177e4SLinus Torvalds dput(dentry); 2669dae6ad8fSAl Viro mutex_unlock(&path.dentry->d_inode->i_mutex); 2670dae6ad8fSAl Viro path_put(&path); 26711da177e4SLinus Torvalds return error; 26721da177e4SLinus Torvalds } 26731da177e4SLinus Torvalds 2674a218d0fdSAl Viro SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode) 26755590ff0dSUlrich Drepper { 26765590ff0dSUlrich Drepper return sys_mkdirat(AT_FDCWD, pathname, mode); 26775590ff0dSUlrich Drepper } 26785590ff0dSUlrich Drepper 26791da177e4SLinus Torvalds /* 2680a71905f0SSage Weil * The dentry_unhash() helper will try to drop the dentry early: we 2681c0d02594SJ. Bruce Fields * should have a usage count of 1 if we're the only user of this 2682a71905f0SSage Weil * dentry, and if that is true (possibly after pruning the dcache), 2683a71905f0SSage Weil * then we drop the dentry now. 26841da177e4SLinus Torvalds * 26851da177e4SLinus Torvalds * A low-level filesystem can, if it choses, legally 26861da177e4SLinus Torvalds * do a 26871da177e4SLinus Torvalds * 26881da177e4SLinus Torvalds * if (!d_unhashed(dentry)) 26891da177e4SLinus Torvalds * return -EBUSY; 26901da177e4SLinus Torvalds * 26911da177e4SLinus Torvalds * if it cannot handle the case of removing a directory 26921da177e4SLinus Torvalds * that is still in use by something else.. 26931da177e4SLinus Torvalds */ 26941da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry) 26951da177e4SLinus Torvalds { 26961da177e4SLinus Torvalds shrink_dcache_parent(dentry); 26971da177e4SLinus Torvalds spin_lock(&dentry->d_lock); 269864252c75SSage Weil if (dentry->d_count == 1) 26991da177e4SLinus Torvalds __d_drop(dentry); 27001da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 27011da177e4SLinus Torvalds } 27021da177e4SLinus Torvalds 27031da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry) 27041da177e4SLinus Torvalds { 27051da177e4SLinus Torvalds int error = may_delete(dir, dentry, 1); 27061da177e4SLinus Torvalds 27071da177e4SLinus Torvalds if (error) 27081da177e4SLinus Torvalds return error; 27091da177e4SLinus Torvalds 2710acfa4380SAl Viro if (!dir->i_op->rmdir) 27111da177e4SLinus Torvalds return -EPERM; 27121da177e4SLinus Torvalds 27131d2ef590SAl Viro dget(dentry); 27141b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 2715912dbc15SSage Weil 27161da177e4SLinus Torvalds error = -EBUSY; 2717912dbc15SSage Weil if (d_mountpoint(dentry)) 2718912dbc15SSage Weil goto out; 2719912dbc15SSage Weil 27201da177e4SLinus Torvalds error = security_inode_rmdir(dir, dentry); 2721912dbc15SSage Weil if (error) 2722912dbc15SSage Weil goto out; 2723912dbc15SSage Weil 27243cebde24SSage Weil shrink_dcache_parent(dentry); 27251da177e4SLinus Torvalds error = dir->i_op->rmdir(dir, dentry); 2726912dbc15SSage Weil if (error) 2727912dbc15SSage Weil goto out; 2728912dbc15SSage Weil 27291da177e4SLinus Torvalds dentry->d_inode->i_flags |= S_DEAD; 2730d83c49f3SAl Viro dont_mount(dentry); 27311da177e4SLinus Torvalds 2732912dbc15SSage Weil out: 2733912dbc15SSage Weil mutex_unlock(&dentry->d_inode->i_mutex); 27341d2ef590SAl Viro dput(dentry); 2735912dbc15SSage Weil if (!error) 2736912dbc15SSage Weil d_delete(dentry); 27371da177e4SLinus Torvalds return error; 27381da177e4SLinus Torvalds } 27391da177e4SLinus Torvalds 27405590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname) 27411da177e4SLinus Torvalds { 27421da177e4SLinus Torvalds int error = 0; 27431da177e4SLinus Torvalds char * name; 27441da177e4SLinus Torvalds struct dentry *dentry; 27451da177e4SLinus Torvalds struct nameidata nd; 27461da177e4SLinus Torvalds 27472ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &name); 27481da177e4SLinus Torvalds if (error) 27492ad94ae6SAl Viro return error; 27501da177e4SLinus Torvalds 27511da177e4SLinus Torvalds switch(nd.last_type) { 27521da177e4SLinus Torvalds case LAST_DOTDOT: 27531da177e4SLinus Torvalds error = -ENOTEMPTY; 27541da177e4SLinus Torvalds goto exit1; 27551da177e4SLinus Torvalds case LAST_DOT: 27561da177e4SLinus Torvalds error = -EINVAL; 27571da177e4SLinus Torvalds goto exit1; 27581da177e4SLinus Torvalds case LAST_ROOT: 27591da177e4SLinus Torvalds error = -EBUSY; 27601da177e4SLinus Torvalds goto exit1; 27611da177e4SLinus Torvalds } 27620612d9fbSOGAWA Hirofumi 27630612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 27640612d9fbSOGAWA Hirofumi 27654ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 276649705b77SChristoph Hellwig dentry = lookup_hash(&nd); 27671da177e4SLinus Torvalds error = PTR_ERR(dentry); 27686902d925SDave Hansen if (IS_ERR(dentry)) 27696902d925SDave Hansen goto exit2; 2770e6bc45d6STheodore Ts'o if (!dentry->d_inode) { 2771e6bc45d6STheodore Ts'o error = -ENOENT; 2772e6bc45d6STheodore Ts'o goto exit3; 2773e6bc45d6STheodore Ts'o } 27740622753bSDave Hansen error = mnt_want_write(nd.path.mnt); 27750622753bSDave Hansen if (error) 27760622753bSDave Hansen goto exit3; 2777be6d3e56SKentaro Takeda error = security_path_rmdir(&nd.path, dentry); 2778be6d3e56SKentaro Takeda if (error) 2779be6d3e56SKentaro Takeda goto exit4; 27804ac91378SJan Blunck error = vfs_rmdir(nd.path.dentry->d_inode, dentry); 2781be6d3e56SKentaro Takeda exit4: 27820622753bSDave Hansen mnt_drop_write(nd.path.mnt); 27830622753bSDave Hansen exit3: 27841da177e4SLinus Torvalds dput(dentry); 27856902d925SDave Hansen exit2: 27864ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 27871da177e4SLinus Torvalds exit1: 27881d957f9bSJan Blunck path_put(&nd.path); 27891da177e4SLinus Torvalds putname(name); 27901da177e4SLinus Torvalds return error; 27911da177e4SLinus Torvalds } 27921da177e4SLinus Torvalds 27933cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname) 27945590ff0dSUlrich Drepper { 27955590ff0dSUlrich Drepper return do_rmdir(AT_FDCWD, pathname); 27965590ff0dSUlrich Drepper } 27975590ff0dSUlrich Drepper 27981da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry) 27991da177e4SLinus Torvalds { 28001da177e4SLinus Torvalds int error = may_delete(dir, dentry, 0); 28011da177e4SLinus Torvalds 28021da177e4SLinus Torvalds if (error) 28031da177e4SLinus Torvalds return error; 28041da177e4SLinus Torvalds 2805acfa4380SAl Viro if (!dir->i_op->unlink) 28061da177e4SLinus Torvalds return -EPERM; 28071da177e4SLinus Torvalds 28081b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 28091da177e4SLinus Torvalds if (d_mountpoint(dentry)) 28101da177e4SLinus Torvalds error = -EBUSY; 28111da177e4SLinus Torvalds else { 28121da177e4SLinus Torvalds error = security_inode_unlink(dir, dentry); 2813bec1052eSAl Viro if (!error) { 28141da177e4SLinus Torvalds error = dir->i_op->unlink(dir, dentry); 2815bec1052eSAl Viro if (!error) 2816d83c49f3SAl Viro dont_mount(dentry); 2817bec1052eSAl Viro } 28181da177e4SLinus Torvalds } 28191b1dcc1bSJes Sorensen mutex_unlock(&dentry->d_inode->i_mutex); 28201da177e4SLinus Torvalds 28211da177e4SLinus Torvalds /* We don't d_delete() NFS sillyrenamed files--they still exist. */ 28221da177e4SLinus Torvalds if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) { 2823ece95912SJan Kara fsnotify_link_count(dentry->d_inode); 28241da177e4SLinus Torvalds d_delete(dentry); 28251da177e4SLinus Torvalds } 28260eeca283SRobert Love 28271da177e4SLinus Torvalds return error; 28281da177e4SLinus Torvalds } 28291da177e4SLinus Torvalds 28301da177e4SLinus Torvalds /* 28311da177e4SLinus Torvalds * Make sure that the actual truncation of the file will occur outside its 28321b1dcc1bSJes Sorensen * directory's i_mutex. Truncate can take a long time if there is a lot of 28331da177e4SLinus Torvalds * writeout happening, and we don't want to prevent access to the directory 28341da177e4SLinus Torvalds * while waiting on the I/O. 28351da177e4SLinus Torvalds */ 28365590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname) 28371da177e4SLinus Torvalds { 28382ad94ae6SAl Viro int error; 28391da177e4SLinus Torvalds char *name; 28401da177e4SLinus Torvalds struct dentry *dentry; 28411da177e4SLinus Torvalds struct nameidata nd; 28421da177e4SLinus Torvalds struct inode *inode = NULL; 28431da177e4SLinus Torvalds 28442ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &name); 28451da177e4SLinus Torvalds if (error) 28462ad94ae6SAl Viro return error; 28472ad94ae6SAl Viro 28481da177e4SLinus Torvalds error = -EISDIR; 28491da177e4SLinus Torvalds if (nd.last_type != LAST_NORM) 28501da177e4SLinus Torvalds goto exit1; 28510612d9fbSOGAWA Hirofumi 28520612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 28530612d9fbSOGAWA Hirofumi 28544ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 285549705b77SChristoph Hellwig dentry = lookup_hash(&nd); 28561da177e4SLinus Torvalds error = PTR_ERR(dentry); 28571da177e4SLinus Torvalds if (!IS_ERR(dentry)) { 28581da177e4SLinus Torvalds /* Why not before? Because we want correct error value */ 285950338b88STörök Edwin if (nd.last.name[nd.last.len]) 286050338b88STörök Edwin goto slashes; 28611da177e4SLinus Torvalds inode = dentry->d_inode; 286250338b88STörök Edwin if (!inode) 2863e6bc45d6STheodore Ts'o goto slashes; 28647de9c6eeSAl Viro ihold(inode); 28650622753bSDave Hansen error = mnt_want_write(nd.path.mnt); 28660622753bSDave Hansen if (error) 28670622753bSDave Hansen goto exit2; 2868be6d3e56SKentaro Takeda error = security_path_unlink(&nd.path, dentry); 2869be6d3e56SKentaro Takeda if (error) 2870be6d3e56SKentaro Takeda goto exit3; 28714ac91378SJan Blunck error = vfs_unlink(nd.path.dentry->d_inode, dentry); 2872be6d3e56SKentaro Takeda exit3: 28730622753bSDave Hansen mnt_drop_write(nd.path.mnt); 28741da177e4SLinus Torvalds exit2: 28751da177e4SLinus Torvalds dput(dentry); 28761da177e4SLinus Torvalds } 28774ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 28781da177e4SLinus Torvalds if (inode) 28791da177e4SLinus Torvalds iput(inode); /* truncate the inode here */ 28801da177e4SLinus Torvalds exit1: 28811d957f9bSJan Blunck path_put(&nd.path); 28821da177e4SLinus Torvalds putname(name); 28831da177e4SLinus Torvalds return error; 28841da177e4SLinus Torvalds 28851da177e4SLinus Torvalds slashes: 28861da177e4SLinus Torvalds error = !dentry->d_inode ? -ENOENT : 28871da177e4SLinus Torvalds S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR; 28881da177e4SLinus Torvalds goto exit2; 28891da177e4SLinus Torvalds } 28901da177e4SLinus Torvalds 28912e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag) 28925590ff0dSUlrich Drepper { 28935590ff0dSUlrich Drepper if ((flag & ~AT_REMOVEDIR) != 0) 28945590ff0dSUlrich Drepper return -EINVAL; 28955590ff0dSUlrich Drepper 28965590ff0dSUlrich Drepper if (flag & AT_REMOVEDIR) 28975590ff0dSUlrich Drepper return do_rmdir(dfd, pathname); 28985590ff0dSUlrich Drepper 28995590ff0dSUlrich Drepper return do_unlinkat(dfd, pathname); 29005590ff0dSUlrich Drepper } 29015590ff0dSUlrich Drepper 29023480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname) 29035590ff0dSUlrich Drepper { 29045590ff0dSUlrich Drepper return do_unlinkat(AT_FDCWD, pathname); 29055590ff0dSUlrich Drepper } 29065590ff0dSUlrich Drepper 2907db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname) 29081da177e4SLinus Torvalds { 2909a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 29101da177e4SLinus Torvalds 29111da177e4SLinus Torvalds if (error) 29121da177e4SLinus Torvalds return error; 29131da177e4SLinus Torvalds 2914acfa4380SAl Viro if (!dir->i_op->symlink) 29151da177e4SLinus Torvalds return -EPERM; 29161da177e4SLinus Torvalds 29171da177e4SLinus Torvalds error = security_inode_symlink(dir, dentry, oldname); 29181da177e4SLinus Torvalds if (error) 29191da177e4SLinus Torvalds return error; 29201da177e4SLinus Torvalds 29211da177e4SLinus Torvalds error = dir->i_op->symlink(dir, dentry, oldname); 2922a74574aaSStephen Smalley if (!error) 2923f38aa942SAmy Griffis fsnotify_create(dir, dentry); 29241da177e4SLinus Torvalds return error; 29251da177e4SLinus Torvalds } 29261da177e4SLinus Torvalds 29272e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname, 29282e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 29291da177e4SLinus Torvalds { 29302ad94ae6SAl Viro int error; 29311da177e4SLinus Torvalds char *from; 29326902d925SDave Hansen struct dentry *dentry; 2933dae6ad8fSAl Viro struct path path; 29341da177e4SLinus Torvalds 29351da177e4SLinus Torvalds from = getname(oldname); 29361da177e4SLinus Torvalds if (IS_ERR(from)) 29371da177e4SLinus Torvalds return PTR_ERR(from); 29382ad94ae6SAl Viro 2939dae6ad8fSAl Viro dentry = user_path_create(newdfd, newname, &path, 0); 29401da177e4SLinus Torvalds error = PTR_ERR(dentry); 29416902d925SDave Hansen if (IS_ERR(dentry)) 2942dae6ad8fSAl Viro goto out_putname; 29436902d925SDave Hansen 2944dae6ad8fSAl Viro error = mnt_want_write(path.mnt); 294575c3f29dSDave Hansen if (error) 294675c3f29dSDave Hansen goto out_dput; 2947dae6ad8fSAl Viro error = security_path_symlink(&path, dentry, from); 2948be6d3e56SKentaro Takeda if (error) 2949be6d3e56SKentaro Takeda goto out_drop_write; 2950dae6ad8fSAl Viro error = vfs_symlink(path.dentry->d_inode, dentry, from); 2951be6d3e56SKentaro Takeda out_drop_write: 2952dae6ad8fSAl Viro mnt_drop_write(path.mnt); 295375c3f29dSDave Hansen out_dput: 29541da177e4SLinus Torvalds dput(dentry); 2955dae6ad8fSAl Viro mutex_unlock(&path.dentry->d_inode->i_mutex); 2956dae6ad8fSAl Viro path_put(&path); 29576902d925SDave Hansen out_putname: 29581da177e4SLinus Torvalds putname(from); 29591da177e4SLinus Torvalds return error; 29601da177e4SLinus Torvalds } 29611da177e4SLinus Torvalds 29623480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname) 29635590ff0dSUlrich Drepper { 29645590ff0dSUlrich Drepper return sys_symlinkat(oldname, AT_FDCWD, newname); 29655590ff0dSUlrich Drepper } 29665590ff0dSUlrich Drepper 29671da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) 29681da177e4SLinus Torvalds { 29691da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 29708de52778SAl Viro unsigned max_links = dir->i_sb->s_max_links; 29711da177e4SLinus Torvalds int error; 29721da177e4SLinus Torvalds 29731da177e4SLinus Torvalds if (!inode) 29741da177e4SLinus Torvalds return -ENOENT; 29751da177e4SLinus Torvalds 2976a95164d9SMiklos Szeredi error = may_create(dir, new_dentry); 29771da177e4SLinus Torvalds if (error) 29781da177e4SLinus Torvalds return error; 29791da177e4SLinus Torvalds 29801da177e4SLinus Torvalds if (dir->i_sb != inode->i_sb) 29811da177e4SLinus Torvalds return -EXDEV; 29821da177e4SLinus Torvalds 29831da177e4SLinus Torvalds /* 29841da177e4SLinus Torvalds * A link to an append-only or immutable file cannot be created. 29851da177e4SLinus Torvalds */ 29861da177e4SLinus Torvalds if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) 29871da177e4SLinus Torvalds return -EPERM; 2988acfa4380SAl Viro if (!dir->i_op->link) 29891da177e4SLinus Torvalds return -EPERM; 29907e79eedbSTetsuo Handa if (S_ISDIR(inode->i_mode)) 29911da177e4SLinus Torvalds return -EPERM; 29921da177e4SLinus Torvalds 29931da177e4SLinus Torvalds error = security_inode_link(old_dentry, dir, new_dentry); 29941da177e4SLinus Torvalds if (error) 29951da177e4SLinus Torvalds return error; 29961da177e4SLinus Torvalds 29977e79eedbSTetsuo Handa mutex_lock(&inode->i_mutex); 2998aae8a97dSAneesh Kumar K.V /* Make sure we don't allow creating hardlink to an unlinked file */ 2999aae8a97dSAneesh Kumar K.V if (inode->i_nlink == 0) 3000aae8a97dSAneesh Kumar K.V error = -ENOENT; 30018de52778SAl Viro else if (max_links && inode->i_nlink >= max_links) 30028de52778SAl Viro error = -EMLINK; 3003aae8a97dSAneesh Kumar K.V else 30041da177e4SLinus Torvalds error = dir->i_op->link(old_dentry, dir, new_dentry); 30057e79eedbSTetsuo Handa mutex_unlock(&inode->i_mutex); 3006e31e14ecSStephen Smalley if (!error) 30077e79eedbSTetsuo Handa fsnotify_link(dir, inode, new_dentry); 30081da177e4SLinus Torvalds return error; 30091da177e4SLinus Torvalds } 30101da177e4SLinus Torvalds 30111da177e4SLinus Torvalds /* 30121da177e4SLinus Torvalds * Hardlinks are often used in delicate situations. We avoid 30131da177e4SLinus Torvalds * security-related surprises by not following symlinks on the 30141da177e4SLinus Torvalds * newname. --KAB 30151da177e4SLinus Torvalds * 30161da177e4SLinus Torvalds * We don't follow them on the oldname either to be compatible 30171da177e4SLinus Torvalds * with linux 2.0, and to avoid hard-linking to directories 30181da177e4SLinus Torvalds * and other special files. --ADM 30191da177e4SLinus Torvalds */ 30202e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname, 30212e4d0924SHeiko Carstens int, newdfd, const char __user *, newname, int, flags) 30221da177e4SLinus Torvalds { 30231da177e4SLinus Torvalds struct dentry *new_dentry; 3024dae6ad8fSAl Viro struct path old_path, new_path; 302511a7b371SAneesh Kumar K.V int how = 0; 30261da177e4SLinus Torvalds int error; 30271da177e4SLinus Torvalds 302811a7b371SAneesh Kumar K.V if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0) 3029c04030e1SUlrich Drepper return -EINVAL; 303011a7b371SAneesh Kumar K.V /* 303111a7b371SAneesh Kumar K.V * To use null names we require CAP_DAC_READ_SEARCH 303211a7b371SAneesh Kumar K.V * This ensures that not everyone will be able to create 303311a7b371SAneesh Kumar K.V * handlink using the passed filedescriptor. 303411a7b371SAneesh Kumar K.V */ 303511a7b371SAneesh Kumar K.V if (flags & AT_EMPTY_PATH) { 303611a7b371SAneesh Kumar K.V if (!capable(CAP_DAC_READ_SEARCH)) 303711a7b371SAneesh Kumar K.V return -ENOENT; 303811a7b371SAneesh Kumar K.V how = LOOKUP_EMPTY; 303911a7b371SAneesh Kumar K.V } 3040c04030e1SUlrich Drepper 304111a7b371SAneesh Kumar K.V if (flags & AT_SYMLINK_FOLLOW) 304211a7b371SAneesh Kumar K.V how |= LOOKUP_FOLLOW; 304311a7b371SAneesh Kumar K.V 304411a7b371SAneesh Kumar K.V error = user_path_at(olddfd, oldname, how, &old_path); 30451da177e4SLinus Torvalds if (error) 30462ad94ae6SAl Viro return error; 30472ad94ae6SAl Viro 3048dae6ad8fSAl Viro new_dentry = user_path_create(newdfd, newname, &new_path, 0); 30491da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 30506902d925SDave Hansen if (IS_ERR(new_dentry)) 3051dae6ad8fSAl Viro goto out; 3052dae6ad8fSAl Viro 3053dae6ad8fSAl Viro error = -EXDEV; 3054dae6ad8fSAl Viro if (old_path.mnt != new_path.mnt) 3055dae6ad8fSAl Viro goto out_dput; 3056dae6ad8fSAl Viro error = mnt_want_write(new_path.mnt); 305775c3f29dSDave Hansen if (error) 305875c3f29dSDave Hansen goto out_dput; 3059dae6ad8fSAl Viro error = security_path_link(old_path.dentry, &new_path, new_dentry); 3060be6d3e56SKentaro Takeda if (error) 3061be6d3e56SKentaro Takeda goto out_drop_write; 3062dae6ad8fSAl Viro error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry); 3063be6d3e56SKentaro Takeda out_drop_write: 3064dae6ad8fSAl Viro mnt_drop_write(new_path.mnt); 306575c3f29dSDave Hansen out_dput: 30661da177e4SLinus Torvalds dput(new_dentry); 3067dae6ad8fSAl Viro mutex_unlock(&new_path.dentry->d_inode->i_mutex); 3068dae6ad8fSAl Viro path_put(&new_path); 30691da177e4SLinus Torvalds out: 30702d8f3038SAl Viro path_put(&old_path); 30711da177e4SLinus Torvalds 30721da177e4SLinus Torvalds return error; 30731da177e4SLinus Torvalds } 30741da177e4SLinus Torvalds 30753480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname) 30765590ff0dSUlrich Drepper { 3077c04030e1SUlrich Drepper return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0); 30785590ff0dSUlrich Drepper } 30795590ff0dSUlrich Drepper 30801da177e4SLinus Torvalds /* 30811da177e4SLinus Torvalds * The worst of all namespace operations - renaming directory. "Perverted" 30821da177e4SLinus Torvalds * doesn't even start to describe it. Somebody in UCB had a heck of a trip... 30831da177e4SLinus Torvalds * Problems: 30841da177e4SLinus Torvalds * a) we can get into loop creation. Check is done in is_subdir(). 30851da177e4SLinus Torvalds * b) race potential - two innocent renames can create a loop together. 30861da177e4SLinus Torvalds * That's where 4.4 screws up. Current fix: serialization on 3087a11f3a05SArjan van de Ven * sb->s_vfs_rename_mutex. We might be more accurate, but that's another 30881da177e4SLinus Torvalds * story. 30891da177e4SLinus Torvalds * c) we have to lock _three_ objects - parents and victim (if it exists). 30901b1dcc1bSJes Sorensen * And that - after we got ->i_mutex on parents (until then we don't know 30911da177e4SLinus Torvalds * whether the target exists). Solution: try to be smart with locking 30921da177e4SLinus Torvalds * order for inodes. We rely on the fact that tree topology may change 3093a11f3a05SArjan van de Ven * only under ->s_vfs_rename_mutex _and_ that parent of the object we 30941da177e4SLinus Torvalds * move will be locked. Thus we can rank directories by the tree 30951da177e4SLinus Torvalds * (ancestors first) and rank all non-directories after them. 30961da177e4SLinus Torvalds * That works since everybody except rename does "lock parent, lookup, 3097a11f3a05SArjan van de Ven * lock child" and rename is under ->s_vfs_rename_mutex. 30981da177e4SLinus Torvalds * HOWEVER, it relies on the assumption that any object with ->lookup() 30991da177e4SLinus Torvalds * has no more than 1 dentry. If "hybrid" objects will ever appear, 31001da177e4SLinus Torvalds * we'd better make sure that there's no link(2) for them. 3101e4eaac06SSage Weil * d) conversion from fhandle to dentry may come in the wrong moment - when 31021b1dcc1bSJes Sorensen * we are removing the target. Solution: we will have to grab ->i_mutex 31031da177e4SLinus Torvalds * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on 3104c41b20e7SAdam Buchbinder * ->i_mutex on parents, which works but leads to some truly excessive 31051da177e4SLinus Torvalds * locking]. 31061da177e4SLinus Torvalds */ 310775c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry, 31081da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 31091da177e4SLinus Torvalds { 31101da177e4SLinus Torvalds int error = 0; 31119055cba7SSage Weil struct inode *target = new_dentry->d_inode; 31128de52778SAl Viro unsigned max_links = new_dir->i_sb->s_max_links; 31131da177e4SLinus Torvalds 31141da177e4SLinus Torvalds /* 31151da177e4SLinus Torvalds * If we are going to change the parent - check write permissions, 31161da177e4SLinus Torvalds * we'll need to flip '..'. 31171da177e4SLinus Torvalds */ 31181da177e4SLinus Torvalds if (new_dir != old_dir) { 3119f419a2e3SAl Viro error = inode_permission(old_dentry->d_inode, MAY_WRITE); 31201da177e4SLinus Torvalds if (error) 31211da177e4SLinus Torvalds return error; 31221da177e4SLinus Torvalds } 31231da177e4SLinus Torvalds 31241da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 31251da177e4SLinus Torvalds if (error) 31261da177e4SLinus Torvalds return error; 31271da177e4SLinus Torvalds 31281d2ef590SAl Viro dget(new_dentry); 3129d83c49f3SAl Viro if (target) 31301b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 31319055cba7SSage Weil 31321da177e4SLinus Torvalds error = -EBUSY; 31339055cba7SSage Weil if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry)) 31349055cba7SSage Weil goto out; 31359055cba7SSage Weil 31368de52778SAl Viro error = -EMLINK; 31378de52778SAl Viro if (max_links && !target && new_dir != old_dir && 31388de52778SAl Viro new_dir->i_nlink >= max_links) 31398de52778SAl Viro goto out; 31408de52778SAl Viro 31413cebde24SSage Weil if (target) 31423cebde24SSage Weil shrink_dcache_parent(new_dentry); 31431da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 31449055cba7SSage Weil if (error) 31459055cba7SSage Weil goto out; 31469055cba7SSage Weil 31471da177e4SLinus Torvalds if (target) { 31481da177e4SLinus Torvalds target->i_flags |= S_DEAD; 3149d83c49f3SAl Viro dont_mount(new_dentry); 3150d83c49f3SAl Viro } 31519055cba7SSage Weil out: 31529055cba7SSage Weil if (target) 31531b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 31541d2ef590SAl Viro dput(new_dentry); 3155e31e14ecSStephen Smalley if (!error) 3156349457ccSMark Fasheh if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) 31571da177e4SLinus Torvalds d_move(old_dentry,new_dentry); 31581da177e4SLinus Torvalds return error; 31591da177e4SLinus Torvalds } 31601da177e4SLinus Torvalds 316175c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry, 31621da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 31631da177e4SLinus Torvalds { 316451892bbbSSage Weil struct inode *target = new_dentry->d_inode; 31651da177e4SLinus Torvalds int error; 31661da177e4SLinus Torvalds 31671da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 31681da177e4SLinus Torvalds if (error) 31691da177e4SLinus Torvalds return error; 31701da177e4SLinus Torvalds 31711da177e4SLinus Torvalds dget(new_dentry); 31721da177e4SLinus Torvalds if (target) 31731b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 317451892bbbSSage Weil 31751da177e4SLinus Torvalds error = -EBUSY; 317651892bbbSSage Weil if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry)) 317751892bbbSSage Weil goto out; 317851892bbbSSage Weil 31791da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 318051892bbbSSage Weil if (error) 318151892bbbSSage Weil goto out; 318251892bbbSSage Weil 3183bec1052eSAl Viro if (target) 3184d83c49f3SAl Viro dont_mount(new_dentry); 3185349457ccSMark Fasheh if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) 31861da177e4SLinus Torvalds d_move(old_dentry, new_dentry); 318751892bbbSSage Weil out: 31881da177e4SLinus Torvalds if (target) 31891b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 31901da177e4SLinus Torvalds dput(new_dentry); 31911da177e4SLinus Torvalds return error; 31921da177e4SLinus Torvalds } 31931da177e4SLinus Torvalds 31941da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry, 31951da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 31961da177e4SLinus Torvalds { 31971da177e4SLinus Torvalds int error; 31981da177e4SLinus Torvalds int is_dir = S_ISDIR(old_dentry->d_inode->i_mode); 319959b0df21SEric Paris const unsigned char *old_name; 32001da177e4SLinus Torvalds 32011da177e4SLinus Torvalds if (old_dentry->d_inode == new_dentry->d_inode) 32021da177e4SLinus Torvalds return 0; 32031da177e4SLinus Torvalds 32041da177e4SLinus Torvalds error = may_delete(old_dir, old_dentry, is_dir); 32051da177e4SLinus Torvalds if (error) 32061da177e4SLinus Torvalds return error; 32071da177e4SLinus Torvalds 32081da177e4SLinus Torvalds if (!new_dentry->d_inode) 3209a95164d9SMiklos Szeredi error = may_create(new_dir, new_dentry); 32101da177e4SLinus Torvalds else 32111da177e4SLinus Torvalds error = may_delete(new_dir, new_dentry, is_dir); 32121da177e4SLinus Torvalds if (error) 32131da177e4SLinus Torvalds return error; 32141da177e4SLinus Torvalds 3215acfa4380SAl Viro if (!old_dir->i_op->rename) 32161da177e4SLinus Torvalds return -EPERM; 32171da177e4SLinus Torvalds 32180eeca283SRobert Love old_name = fsnotify_oldname_init(old_dentry->d_name.name); 32190eeca283SRobert Love 32201da177e4SLinus Torvalds if (is_dir) 32211da177e4SLinus Torvalds error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry); 32221da177e4SLinus Torvalds else 32231da177e4SLinus Torvalds error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry); 3224123df294SAl Viro if (!error) 3225123df294SAl Viro fsnotify_move(old_dir, new_dir, old_name, is_dir, 32265a190ae6SAl Viro new_dentry->d_inode, old_dentry); 32270eeca283SRobert Love fsnotify_oldname_free(old_name); 32280eeca283SRobert Love 32291da177e4SLinus Torvalds return error; 32301da177e4SLinus Torvalds } 32311da177e4SLinus Torvalds 32322e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname, 32332e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 32341da177e4SLinus Torvalds { 32351da177e4SLinus Torvalds struct dentry *old_dir, *new_dir; 32361da177e4SLinus Torvalds struct dentry *old_dentry, *new_dentry; 32371da177e4SLinus Torvalds struct dentry *trap; 32381da177e4SLinus Torvalds struct nameidata oldnd, newnd; 32392ad94ae6SAl Viro char *from; 32402ad94ae6SAl Viro char *to; 32412ad94ae6SAl Viro int error; 32421da177e4SLinus Torvalds 32432ad94ae6SAl Viro error = user_path_parent(olddfd, oldname, &oldnd, &from); 32441da177e4SLinus Torvalds if (error) 32451da177e4SLinus Torvalds goto exit; 32461da177e4SLinus Torvalds 32472ad94ae6SAl Viro error = user_path_parent(newdfd, newname, &newnd, &to); 32481da177e4SLinus Torvalds if (error) 32491da177e4SLinus Torvalds goto exit1; 32501da177e4SLinus Torvalds 32511da177e4SLinus Torvalds error = -EXDEV; 32524ac91378SJan Blunck if (oldnd.path.mnt != newnd.path.mnt) 32531da177e4SLinus Torvalds goto exit2; 32541da177e4SLinus Torvalds 32554ac91378SJan Blunck old_dir = oldnd.path.dentry; 32561da177e4SLinus Torvalds error = -EBUSY; 32571da177e4SLinus Torvalds if (oldnd.last_type != LAST_NORM) 32581da177e4SLinus Torvalds goto exit2; 32591da177e4SLinus Torvalds 32604ac91378SJan Blunck new_dir = newnd.path.dentry; 32611da177e4SLinus Torvalds if (newnd.last_type != LAST_NORM) 32621da177e4SLinus Torvalds goto exit2; 32631da177e4SLinus Torvalds 32640612d9fbSOGAWA Hirofumi oldnd.flags &= ~LOOKUP_PARENT; 32650612d9fbSOGAWA Hirofumi newnd.flags &= ~LOOKUP_PARENT; 32664e9ed2f8SOGAWA Hirofumi newnd.flags |= LOOKUP_RENAME_TARGET; 32670612d9fbSOGAWA Hirofumi 32681da177e4SLinus Torvalds trap = lock_rename(new_dir, old_dir); 32691da177e4SLinus Torvalds 327049705b77SChristoph Hellwig old_dentry = lookup_hash(&oldnd); 32711da177e4SLinus Torvalds error = PTR_ERR(old_dentry); 32721da177e4SLinus Torvalds if (IS_ERR(old_dentry)) 32731da177e4SLinus Torvalds goto exit3; 32741da177e4SLinus Torvalds /* source must exist */ 32751da177e4SLinus Torvalds error = -ENOENT; 32761da177e4SLinus Torvalds if (!old_dentry->d_inode) 32771da177e4SLinus Torvalds goto exit4; 32781da177e4SLinus Torvalds /* unless the source is a directory trailing slashes give -ENOTDIR */ 32791da177e4SLinus Torvalds if (!S_ISDIR(old_dentry->d_inode->i_mode)) { 32801da177e4SLinus Torvalds error = -ENOTDIR; 32811da177e4SLinus Torvalds if (oldnd.last.name[oldnd.last.len]) 32821da177e4SLinus Torvalds goto exit4; 32831da177e4SLinus Torvalds if (newnd.last.name[newnd.last.len]) 32841da177e4SLinus Torvalds goto exit4; 32851da177e4SLinus Torvalds } 32861da177e4SLinus Torvalds /* source should not be ancestor of target */ 32871da177e4SLinus Torvalds error = -EINVAL; 32881da177e4SLinus Torvalds if (old_dentry == trap) 32891da177e4SLinus Torvalds goto exit4; 329049705b77SChristoph Hellwig new_dentry = lookup_hash(&newnd); 32911da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 32921da177e4SLinus Torvalds if (IS_ERR(new_dentry)) 32931da177e4SLinus Torvalds goto exit4; 32941da177e4SLinus Torvalds /* target should not be an ancestor of source */ 32951da177e4SLinus Torvalds error = -ENOTEMPTY; 32961da177e4SLinus Torvalds if (new_dentry == trap) 32971da177e4SLinus Torvalds goto exit5; 32981da177e4SLinus Torvalds 32999079b1ebSDave Hansen error = mnt_want_write(oldnd.path.mnt); 33009079b1ebSDave Hansen if (error) 33019079b1ebSDave Hansen goto exit5; 3302be6d3e56SKentaro Takeda error = security_path_rename(&oldnd.path, old_dentry, 3303be6d3e56SKentaro Takeda &newnd.path, new_dentry); 3304be6d3e56SKentaro Takeda if (error) 3305be6d3e56SKentaro Takeda goto exit6; 33061da177e4SLinus Torvalds error = vfs_rename(old_dir->d_inode, old_dentry, 33071da177e4SLinus Torvalds new_dir->d_inode, new_dentry); 3308be6d3e56SKentaro Takeda exit6: 33099079b1ebSDave Hansen mnt_drop_write(oldnd.path.mnt); 33101da177e4SLinus Torvalds exit5: 33111da177e4SLinus Torvalds dput(new_dentry); 33121da177e4SLinus Torvalds exit4: 33131da177e4SLinus Torvalds dput(old_dentry); 33141da177e4SLinus Torvalds exit3: 33151da177e4SLinus Torvalds unlock_rename(new_dir, old_dir); 33161da177e4SLinus Torvalds exit2: 33171d957f9bSJan Blunck path_put(&newnd.path); 33182ad94ae6SAl Viro putname(to); 33191da177e4SLinus Torvalds exit1: 33201d957f9bSJan Blunck path_put(&oldnd.path); 33211da177e4SLinus Torvalds putname(from); 33222ad94ae6SAl Viro exit: 33231da177e4SLinus Torvalds return error; 33241da177e4SLinus Torvalds } 33251da177e4SLinus Torvalds 3326a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname) 33275590ff0dSUlrich Drepper { 33285590ff0dSUlrich Drepper return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname); 33295590ff0dSUlrich Drepper } 33305590ff0dSUlrich Drepper 33311da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link) 33321da177e4SLinus Torvalds { 33331da177e4SLinus Torvalds int len; 33341da177e4SLinus Torvalds 33351da177e4SLinus Torvalds len = PTR_ERR(link); 33361da177e4SLinus Torvalds if (IS_ERR(link)) 33371da177e4SLinus Torvalds goto out; 33381da177e4SLinus Torvalds 33391da177e4SLinus Torvalds len = strlen(link); 33401da177e4SLinus Torvalds if (len > (unsigned) buflen) 33411da177e4SLinus Torvalds len = buflen; 33421da177e4SLinus Torvalds if (copy_to_user(buffer, link, len)) 33431da177e4SLinus Torvalds len = -EFAULT; 33441da177e4SLinus Torvalds out: 33451da177e4SLinus Torvalds return len; 33461da177e4SLinus Torvalds } 33471da177e4SLinus Torvalds 33481da177e4SLinus Torvalds /* 33491da177e4SLinus Torvalds * A helper for ->readlink(). This should be used *ONLY* for symlinks that 33501da177e4SLinus Torvalds * have ->follow_link() touching nd only in nd_set_link(). Using (or not 33511da177e4SLinus Torvalds * using) it for any given inode is up to filesystem. 33521da177e4SLinus Torvalds */ 33531da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen) 33541da177e4SLinus Torvalds { 33551da177e4SLinus Torvalds struct nameidata nd; 3356cc314eefSLinus Torvalds void *cookie; 3357694a1764SMarcin Slusarz int res; 3358cc314eefSLinus Torvalds 33591da177e4SLinus Torvalds nd.depth = 0; 3360cc314eefSLinus Torvalds cookie = dentry->d_inode->i_op->follow_link(dentry, &nd); 3361694a1764SMarcin Slusarz if (IS_ERR(cookie)) 3362694a1764SMarcin Slusarz return PTR_ERR(cookie); 3363694a1764SMarcin Slusarz 3364694a1764SMarcin Slusarz res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd)); 33651da177e4SLinus Torvalds if (dentry->d_inode->i_op->put_link) 3366cc314eefSLinus Torvalds dentry->d_inode->i_op->put_link(dentry, &nd, cookie); 3367694a1764SMarcin Slusarz return res; 33681da177e4SLinus Torvalds } 33691da177e4SLinus Torvalds 33701da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link) 33711da177e4SLinus Torvalds { 33721da177e4SLinus Torvalds return __vfs_follow_link(nd, link); 33731da177e4SLinus Torvalds } 33741da177e4SLinus Torvalds 33751da177e4SLinus Torvalds /* get the link contents into pagecache */ 33761da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage) 33771da177e4SLinus Torvalds { 3378ebd09abbSDuane Griffin char *kaddr; 33791da177e4SLinus Torvalds struct page *page; 33801da177e4SLinus Torvalds struct address_space *mapping = dentry->d_inode->i_mapping; 3381090d2b18SPekka Enberg page = read_mapping_page(mapping, 0, NULL); 33821da177e4SLinus Torvalds if (IS_ERR(page)) 33836fe6900eSNick Piggin return (char*)page; 33841da177e4SLinus Torvalds *ppage = page; 3385ebd09abbSDuane Griffin kaddr = kmap(page); 3386ebd09abbSDuane Griffin nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1); 3387ebd09abbSDuane Griffin return kaddr; 33881da177e4SLinus Torvalds } 33891da177e4SLinus Torvalds 33901da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen) 33911da177e4SLinus Torvalds { 33921da177e4SLinus Torvalds struct page *page = NULL; 33931da177e4SLinus Torvalds char *s = page_getlink(dentry, &page); 33941da177e4SLinus Torvalds int res = vfs_readlink(dentry,buffer,buflen,s); 33951da177e4SLinus Torvalds if (page) { 33961da177e4SLinus Torvalds kunmap(page); 33971da177e4SLinus Torvalds page_cache_release(page); 33981da177e4SLinus Torvalds } 33991da177e4SLinus Torvalds return res; 34001da177e4SLinus Torvalds } 34011da177e4SLinus Torvalds 3402cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd) 34031da177e4SLinus Torvalds { 3404cc314eefSLinus Torvalds struct page *page = NULL; 34051da177e4SLinus Torvalds nd_set_link(nd, page_getlink(dentry, &page)); 3406cc314eefSLinus Torvalds return page; 34071da177e4SLinus Torvalds } 34081da177e4SLinus Torvalds 3409cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie) 34101da177e4SLinus Torvalds { 3411cc314eefSLinus Torvalds struct page *page = cookie; 3412cc314eefSLinus Torvalds 3413cc314eefSLinus Torvalds if (page) { 34141da177e4SLinus Torvalds kunmap(page); 34151da177e4SLinus Torvalds page_cache_release(page); 34161da177e4SLinus Torvalds } 34171da177e4SLinus Torvalds } 34181da177e4SLinus Torvalds 341954566b2cSNick Piggin /* 342054566b2cSNick Piggin * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS 342154566b2cSNick Piggin */ 342254566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs) 34231da177e4SLinus Torvalds { 34241da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 34250adb25d2SKirill Korotaev struct page *page; 3426afddba49SNick Piggin void *fsdata; 3427beb497abSDmitriy Monakhov int err; 34281da177e4SLinus Torvalds char *kaddr; 342954566b2cSNick Piggin unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE; 343054566b2cSNick Piggin if (nofs) 343154566b2cSNick Piggin flags |= AOP_FLAG_NOFS; 34321da177e4SLinus Torvalds 34337e53cac4SNeilBrown retry: 3434afddba49SNick Piggin err = pagecache_write_begin(NULL, mapping, 0, len-1, 343554566b2cSNick Piggin flags, &page, &fsdata); 34361da177e4SLinus Torvalds if (err) 3437afddba49SNick Piggin goto fail; 3438afddba49SNick Piggin 3439e8e3c3d6SCong Wang kaddr = kmap_atomic(page); 34401da177e4SLinus Torvalds memcpy(kaddr, symname, len-1); 3441e8e3c3d6SCong Wang kunmap_atomic(kaddr); 3442afddba49SNick Piggin 3443afddba49SNick Piggin err = pagecache_write_end(NULL, mapping, 0, len-1, len-1, 3444afddba49SNick Piggin page, fsdata); 34451da177e4SLinus Torvalds if (err < 0) 34461da177e4SLinus Torvalds goto fail; 3447afddba49SNick Piggin if (err < len-1) 3448afddba49SNick Piggin goto retry; 3449afddba49SNick Piggin 34501da177e4SLinus Torvalds mark_inode_dirty(inode); 34511da177e4SLinus Torvalds return 0; 34521da177e4SLinus Torvalds fail: 34531da177e4SLinus Torvalds return err; 34541da177e4SLinus Torvalds } 34551da177e4SLinus Torvalds 34560adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len) 34570adb25d2SKirill Korotaev { 34580adb25d2SKirill Korotaev return __page_symlink(inode, symname, len, 345954566b2cSNick Piggin !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS)); 34600adb25d2SKirill Korotaev } 34610adb25d2SKirill Korotaev 346292e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = { 34631da177e4SLinus Torvalds .readlink = generic_readlink, 34641da177e4SLinus Torvalds .follow_link = page_follow_link_light, 34651da177e4SLinus Torvalds .put_link = page_put_link, 34661da177e4SLinus Torvalds }; 34671da177e4SLinus Torvalds 34682d8f3038SAl Viro EXPORT_SYMBOL(user_path_at); 3469cc53ce53SDavid Howells EXPORT_SYMBOL(follow_down_one); 34701da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down); 34711da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up); 34721da177e4SLinus Torvalds EXPORT_SYMBOL(get_write_access); /* binfmt_aout */ 34731da177e4SLinus Torvalds EXPORT_SYMBOL(getname); 34741da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename); 34751da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len); 34761da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light); 34771da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link); 34781da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink); 34790adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink); 34801da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink); 34811da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations); 3482d1811465SAl Viro EXPORT_SYMBOL(kern_path); 348316f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup); 3484f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission); 34851da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename); 34861da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create); 34871da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link); 34881da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link); 34891da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir); 34901da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod); 34911da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission); 34921da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink); 34931da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename); 34941da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir); 34951da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink); 34961da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink); 34971da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash); 34981da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink); 3499