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(); 452962830dfSAndi Kleen 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 4664ce16ef3SAl Viro static inline int d_revalidate(struct dentry *dentry, unsigned int flags) 46734286d66SNick Piggin { 4684ce16ef3SAl Viro return dentry->d_op->d_revalidate(dentry, flags); 46934286d66SNick Piggin } 47034286d66SNick Piggin 4719f1fafeeSAl Viro /** 4729f1fafeeSAl Viro * complete_walk - successful completion of path walk 4739f1fafeeSAl Viro * @nd: pointer nameidata 47439159de2SJeff Layton * 4759f1fafeeSAl Viro * If we had been in RCU mode, drop out of it and legitimize nd->path. 4769f1fafeeSAl Viro * Revalidate the final result, unless we'd already done that during 4779f1fafeeSAl Viro * the path walk or the filesystem doesn't ask for it. Return 0 on 4789f1fafeeSAl Viro * success, -error on failure. In case of failure caller does not 4799f1fafeeSAl Viro * need to drop nd->path. 48039159de2SJeff Layton */ 4819f1fafeeSAl Viro static int complete_walk(struct nameidata *nd) 48239159de2SJeff Layton { 48316c2cd71SAl Viro struct dentry *dentry = nd->path.dentry; 48439159de2SJeff Layton int status; 48539159de2SJeff Layton 4869f1fafeeSAl Viro if (nd->flags & LOOKUP_RCU) { 4879f1fafeeSAl Viro nd->flags &= ~LOOKUP_RCU; 4889f1fafeeSAl Viro if (!(nd->flags & LOOKUP_ROOT)) 4899f1fafeeSAl Viro nd->root.mnt = NULL; 4909f1fafeeSAl Viro spin_lock(&dentry->d_lock); 4919f1fafeeSAl Viro if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) { 4929f1fafeeSAl Viro spin_unlock(&dentry->d_lock); 4939f1fafeeSAl Viro rcu_read_unlock(); 494962830dfSAndi Kleen br_read_unlock(&vfsmount_lock); 4959f1fafeeSAl Viro return -ECHILD; 4969f1fafeeSAl Viro } 4979f1fafeeSAl Viro BUG_ON(nd->inode != dentry->d_inode); 4989f1fafeeSAl Viro spin_unlock(&dentry->d_lock); 4999f1fafeeSAl Viro mntget(nd->path.mnt); 5009f1fafeeSAl Viro rcu_read_unlock(); 501962830dfSAndi Kleen br_read_unlock(&vfsmount_lock); 5029f1fafeeSAl Viro } 5039f1fafeeSAl Viro 50416c2cd71SAl Viro if (likely(!(nd->flags & LOOKUP_JUMPED))) 50539159de2SJeff Layton return 0; 50639159de2SJeff Layton 50716c2cd71SAl Viro if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE))) 50816c2cd71SAl Viro return 0; 50916c2cd71SAl Viro 51016c2cd71SAl Viro if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT))) 51116c2cd71SAl Viro return 0; 51216c2cd71SAl Viro 51316c2cd71SAl Viro /* Note: we do not d_invalidate() */ 5144ce16ef3SAl Viro status = d_revalidate(dentry, nd->flags); 51539159de2SJeff Layton if (status > 0) 51639159de2SJeff Layton return 0; 51739159de2SJeff Layton 51816c2cd71SAl Viro if (!status) 51939159de2SJeff Layton status = -ESTALE; 52016c2cd71SAl Viro 5219f1fafeeSAl Viro path_put(&nd->path); 52239159de2SJeff Layton return status; 52339159de2SJeff Layton } 52439159de2SJeff Layton 5252a737871SAl Viro static __always_inline void set_root(struct nameidata *nd) 5262a737871SAl Viro { 527f7ad3c6bSMiklos Szeredi if (!nd->root.mnt) 528f7ad3c6bSMiklos Szeredi get_fs_root(current->fs, &nd->root); 5292a737871SAl Viro } 5302a737871SAl Viro 5316de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *); 5326de88d72SAl Viro 53331e6b01fSNick Piggin static __always_inline void set_root_rcu(struct nameidata *nd) 53431e6b01fSNick Piggin { 53531e6b01fSNick Piggin if (!nd->root.mnt) { 53631e6b01fSNick Piggin struct fs_struct *fs = current->fs; 537c28cc364SNick Piggin unsigned seq; 538c28cc364SNick Piggin 539c28cc364SNick Piggin do { 540c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 54131e6b01fSNick Piggin nd->root = fs->root; 542c1530019STim Chen nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq); 543c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 54431e6b01fSNick Piggin } 54531e6b01fSNick Piggin } 54631e6b01fSNick Piggin 547f1662356SArjan van de Ven static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link) 5481da177e4SLinus Torvalds { 54931e6b01fSNick Piggin int ret; 55031e6b01fSNick Piggin 5511da177e4SLinus Torvalds if (IS_ERR(link)) 5521da177e4SLinus Torvalds goto fail; 5531da177e4SLinus Torvalds 5541da177e4SLinus Torvalds if (*link == '/') { 5552a737871SAl Viro set_root(nd); 5561d957f9bSJan Blunck path_put(&nd->path); 5572a737871SAl Viro nd->path = nd->root; 5582a737871SAl Viro path_get(&nd->root); 55916c2cd71SAl Viro nd->flags |= LOOKUP_JUMPED; 5601da177e4SLinus Torvalds } 56131e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 562b4091d5fSChristoph Hellwig 56331e6b01fSNick Piggin ret = link_path_walk(link, nd); 56431e6b01fSNick Piggin return ret; 5651da177e4SLinus Torvalds fail: 5661d957f9bSJan Blunck path_put(&nd->path); 5671da177e4SLinus Torvalds return PTR_ERR(link); 5681da177e4SLinus Torvalds } 5691da177e4SLinus Torvalds 5701d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd) 571051d3812SIan Kent { 572051d3812SIan Kent dput(path->dentry); 5734ac91378SJan Blunck if (path->mnt != nd->path.mnt) 574051d3812SIan Kent mntput(path->mnt); 575051d3812SIan Kent } 576051d3812SIan Kent 5777b9337aaSNick Piggin static inline void path_to_nameidata(const struct path *path, 5787b9337aaSNick Piggin struct nameidata *nd) 579051d3812SIan Kent { 58031e6b01fSNick Piggin if (!(nd->flags & LOOKUP_RCU)) { 5814ac91378SJan Blunck dput(nd->path.dentry); 58231e6b01fSNick Piggin if (nd->path.mnt != path->mnt) 5834ac91378SJan Blunck mntput(nd->path.mnt); 5849a229683SHuang Shijie } 58531e6b01fSNick Piggin nd->path.mnt = path->mnt; 5864ac91378SJan Blunck nd->path.dentry = path->dentry; 587051d3812SIan Kent } 588051d3812SIan Kent 589574197e0SAl Viro static inline void put_link(struct nameidata *nd, struct path *link, void *cookie) 590574197e0SAl Viro { 591574197e0SAl Viro struct inode *inode = link->dentry->d_inode; 5926d7b5aaeSAl Viro if (inode->i_op->put_link) 593574197e0SAl Viro inode->i_op->put_link(link->dentry, nd, cookie); 594574197e0SAl Viro path_put(link); 595574197e0SAl Viro } 596574197e0SAl Viro 597def4af30SAl Viro static __always_inline int 598574197e0SAl Viro follow_link(struct path *link, struct nameidata *nd, void **p) 5991da177e4SLinus Torvalds { 6007b9337aaSNick Piggin struct dentry *dentry = link->dentry; 6016d7b5aaeSAl Viro int error; 6026d7b5aaeSAl Viro char *s; 6031da177e4SLinus Torvalds 604844a3917SAl Viro BUG_ON(nd->flags & LOOKUP_RCU); 605844a3917SAl Viro 6060e794589SAl Viro if (link->mnt == nd->path.mnt) 6070e794589SAl Viro mntget(link->mnt); 6080e794589SAl Viro 6096d7b5aaeSAl Viro error = -ELOOP; 6106d7b5aaeSAl Viro if (unlikely(current->total_link_count >= 40)) 6116d7b5aaeSAl Viro goto out_put_nd_path; 6126d7b5aaeSAl Viro 613574197e0SAl Viro cond_resched(); 614574197e0SAl Viro current->total_link_count++; 615574197e0SAl Viro 61668ac1234SAl Viro touch_atime(link); 6171da177e4SLinus Torvalds nd_set_link(nd, NULL); 618cd4e91d3SAl Viro 61936f3b4f6SAl Viro error = security_inode_follow_link(link->dentry, nd); 6206d7b5aaeSAl Viro if (error) 6216d7b5aaeSAl Viro goto out_put_nd_path; 62236f3b4f6SAl Viro 62386acdca1SAl Viro nd->last_type = LAST_BIND; 624def4af30SAl Viro *p = dentry->d_inode->i_op->follow_link(dentry, nd); 625def4af30SAl Viro error = PTR_ERR(*p); 6266d7b5aaeSAl Viro if (IS_ERR(*p)) 6276d7b5aaeSAl Viro goto out_put_link; 6286d7b5aaeSAl Viro 629cc314eefSLinus Torvalds error = 0; 6306d7b5aaeSAl Viro s = nd_get_link(nd); 6316d7b5aaeSAl Viro if (s) { 6321da177e4SLinus Torvalds error = __vfs_follow_link(nd, s); 6336d7b5aaeSAl Viro } else if (nd->last_type == LAST_BIND) { 63416c2cd71SAl Viro nd->flags |= LOOKUP_JUMPED; 635b21041d0SAl Viro nd->inode = nd->path.dentry->d_inode; 636b21041d0SAl Viro if (nd->inode->i_op->follow_link) { 637bcda7652SAl Viro /* stepped on a _really_ weird one */ 638bcda7652SAl Viro path_put(&nd->path); 639bcda7652SAl Viro error = -ELOOP; 640bcda7652SAl Viro } 641bcda7652SAl Viro } 6426d7b5aaeSAl Viro if (unlikely(error)) 6436d7b5aaeSAl Viro put_link(nd, link, *p); 6446d7b5aaeSAl Viro 6456d7b5aaeSAl Viro return error; 6466d7b5aaeSAl Viro 6476d7b5aaeSAl Viro out_put_nd_path: 6486d7b5aaeSAl Viro path_put(&nd->path); 6496d7b5aaeSAl Viro out_put_link: 6506d7b5aaeSAl Viro path_put(link); 6511da177e4SLinus Torvalds return error; 6521da177e4SLinus Torvalds } 6531da177e4SLinus Torvalds 65431e6b01fSNick Piggin static int follow_up_rcu(struct path *path) 65531e6b01fSNick Piggin { 6560714a533SAl Viro struct mount *mnt = real_mount(path->mnt); 6570714a533SAl Viro struct mount *parent; 65831e6b01fSNick Piggin struct dentry *mountpoint; 65931e6b01fSNick Piggin 6600714a533SAl Viro parent = mnt->mnt_parent; 6610714a533SAl Viro if (&parent->mnt == path->mnt) 66231e6b01fSNick Piggin return 0; 663a73324daSAl Viro mountpoint = mnt->mnt_mountpoint; 66431e6b01fSNick Piggin path->dentry = mountpoint; 6650714a533SAl Viro path->mnt = &parent->mnt; 66631e6b01fSNick Piggin return 1; 66731e6b01fSNick Piggin } 66831e6b01fSNick Piggin 669bab77ebfSAl Viro int follow_up(struct path *path) 6701da177e4SLinus Torvalds { 6710714a533SAl Viro struct mount *mnt = real_mount(path->mnt); 6720714a533SAl Viro struct mount *parent; 6731da177e4SLinus Torvalds struct dentry *mountpoint; 67499b7db7bSNick Piggin 675962830dfSAndi Kleen br_read_lock(&vfsmount_lock); 6760714a533SAl Viro parent = mnt->mnt_parent; 6770714a533SAl Viro if (&parent->mnt == path->mnt) { 678962830dfSAndi Kleen br_read_unlock(&vfsmount_lock); 6791da177e4SLinus Torvalds return 0; 6801da177e4SLinus Torvalds } 6810714a533SAl Viro mntget(&parent->mnt); 682a73324daSAl Viro mountpoint = dget(mnt->mnt_mountpoint); 683962830dfSAndi Kleen br_read_unlock(&vfsmount_lock); 684bab77ebfSAl Viro dput(path->dentry); 685bab77ebfSAl Viro path->dentry = mountpoint; 686bab77ebfSAl Viro mntput(path->mnt); 6870714a533SAl Viro path->mnt = &parent->mnt; 6881da177e4SLinus Torvalds return 1; 6891da177e4SLinus Torvalds } 6901da177e4SLinus Torvalds 691b5c84bf6SNick Piggin /* 6929875cf80SDavid Howells * Perform an automount 6939875cf80SDavid Howells * - return -EISDIR to tell follow_managed() to stop and return the path we 6949875cf80SDavid Howells * were called with. 6951da177e4SLinus Torvalds */ 6969875cf80SDavid Howells static int follow_automount(struct path *path, unsigned flags, 6979875cf80SDavid Howells bool *need_mntput) 69831e6b01fSNick Piggin { 6999875cf80SDavid Howells struct vfsmount *mnt; 700ea5b778aSDavid Howells int err; 7019875cf80SDavid Howells 7029875cf80SDavid Howells if (!path->dentry->d_op || !path->dentry->d_op->d_automount) 7039875cf80SDavid Howells return -EREMOTE; 7049875cf80SDavid Howells 7050ec26fd0SMiklos Szeredi /* We don't want to mount if someone's just doing a stat - 7060ec26fd0SMiklos Szeredi * unless they're stat'ing a directory and appended a '/' to 7070ec26fd0SMiklos Szeredi * the name. 7080ec26fd0SMiklos Szeredi * 7090ec26fd0SMiklos Szeredi * We do, however, want to mount if someone wants to open or 7105a30d8a2SDavid Howells * create a file of any type under the mountpoint, wants to 7110ec26fd0SMiklos Szeredi * traverse through the mountpoint or wants to open the 7120ec26fd0SMiklos Szeredi * mounted directory. Also, autofs may mark negative dentries 7130ec26fd0SMiklos Szeredi * as being automount points. These will need the attentions 7140ec26fd0SMiklos Szeredi * of the daemon to instantiate them before they can be used. 7155a30d8a2SDavid Howells */ 7165a30d8a2SDavid Howells if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY | 717d94c177bSLinus Torvalds LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) && 7185a30d8a2SDavid Howells path->dentry->d_inode) 7199875cf80SDavid Howells return -EISDIR; 7200ec26fd0SMiklos Szeredi 7219875cf80SDavid Howells current->total_link_count++; 7229875cf80SDavid Howells if (current->total_link_count >= 40) 7239875cf80SDavid Howells return -ELOOP; 7249875cf80SDavid Howells 7259875cf80SDavid Howells mnt = path->dentry->d_op->d_automount(path); 7269875cf80SDavid Howells if (IS_ERR(mnt)) { 7279875cf80SDavid Howells /* 7289875cf80SDavid Howells * The filesystem is allowed to return -EISDIR here to indicate 7299875cf80SDavid Howells * it doesn't want to automount. For instance, autofs would do 7309875cf80SDavid Howells * this so that its userspace daemon can mount on this dentry. 7319875cf80SDavid Howells * 7329875cf80SDavid Howells * However, we can only permit this if it's a terminal point in 7339875cf80SDavid Howells * the path being looked up; if it wasn't then the remainder of 7349875cf80SDavid Howells * the path is inaccessible and we should say so. 7359875cf80SDavid Howells */ 73649084c3bSAl Viro if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT)) 7379875cf80SDavid Howells return -EREMOTE; 7389875cf80SDavid Howells return PTR_ERR(mnt); 73931e6b01fSNick Piggin } 740ea5b778aSDavid Howells 7419875cf80SDavid Howells if (!mnt) /* mount collision */ 7429875cf80SDavid Howells return 0; 7439875cf80SDavid Howells 7448aef1884SAl Viro if (!*need_mntput) { 7458aef1884SAl Viro /* lock_mount() may release path->mnt on error */ 7468aef1884SAl Viro mntget(path->mnt); 7478aef1884SAl Viro *need_mntput = true; 7488aef1884SAl Viro } 74919a167afSAl Viro err = finish_automount(mnt, path); 750ea5b778aSDavid Howells 751ea5b778aSDavid Howells switch (err) { 752ea5b778aSDavid Howells case -EBUSY: 753ea5b778aSDavid Howells /* Someone else made a mount here whilst we were busy */ 75419a167afSAl Viro return 0; 755ea5b778aSDavid Howells case 0: 7568aef1884SAl Viro path_put(path); 7579875cf80SDavid Howells path->mnt = mnt; 7589875cf80SDavid Howells path->dentry = dget(mnt->mnt_root); 7599875cf80SDavid Howells return 0; 76019a167afSAl Viro default: 76119a167afSAl Viro return err; 7629875cf80SDavid Howells } 76319a167afSAl Viro 764ea5b778aSDavid Howells } 7659875cf80SDavid Howells 7669875cf80SDavid Howells /* 7679875cf80SDavid Howells * Handle a dentry that is managed in some way. 768cc53ce53SDavid Howells * - Flagged for transit management (autofs) 7699875cf80SDavid Howells * - Flagged as mountpoint 7709875cf80SDavid Howells * - Flagged as automount point 7719875cf80SDavid Howells * 7729875cf80SDavid Howells * This may only be called in refwalk mode. 7739875cf80SDavid Howells * 7749875cf80SDavid Howells * Serialization is taken care of in namespace.c 7759875cf80SDavid Howells */ 7769875cf80SDavid Howells static int follow_managed(struct path *path, unsigned flags) 7779875cf80SDavid Howells { 7788aef1884SAl Viro struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */ 7799875cf80SDavid Howells unsigned managed; 7809875cf80SDavid Howells bool need_mntput = false; 7818aef1884SAl Viro int ret = 0; 7829875cf80SDavid Howells 7839875cf80SDavid Howells /* Given that we're not holding a lock here, we retain the value in a 7849875cf80SDavid Howells * local variable for each dentry as we look at it so that we don't see 7859875cf80SDavid Howells * the components of that value change under us */ 7869875cf80SDavid Howells while (managed = ACCESS_ONCE(path->dentry->d_flags), 7879875cf80SDavid Howells managed &= DCACHE_MANAGED_DENTRY, 7889875cf80SDavid Howells unlikely(managed != 0)) { 789cc53ce53SDavid Howells /* Allow the filesystem to manage the transit without i_mutex 790cc53ce53SDavid Howells * being held. */ 791cc53ce53SDavid Howells if (managed & DCACHE_MANAGE_TRANSIT) { 792cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op); 793cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op->d_manage); 7941aed3e42SAl Viro ret = path->dentry->d_op->d_manage(path->dentry, false); 795cc53ce53SDavid Howells if (ret < 0) 7968aef1884SAl Viro break; 797cc53ce53SDavid Howells } 798cc53ce53SDavid Howells 7999875cf80SDavid Howells /* Transit to a mounted filesystem. */ 8009875cf80SDavid Howells if (managed & DCACHE_MOUNTED) { 8019875cf80SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 8029875cf80SDavid Howells if (mounted) { 8039875cf80SDavid Howells dput(path->dentry); 8049875cf80SDavid Howells if (need_mntput) 805463ffb2eSAl Viro mntput(path->mnt); 806463ffb2eSAl Viro path->mnt = mounted; 807463ffb2eSAl Viro path->dentry = dget(mounted->mnt_root); 8089875cf80SDavid Howells need_mntput = true; 8099875cf80SDavid Howells continue; 810463ffb2eSAl Viro } 811463ffb2eSAl Viro 8129875cf80SDavid Howells /* Something is mounted on this dentry in another 8139875cf80SDavid Howells * namespace and/or whatever was mounted there in this 8149875cf80SDavid Howells * namespace got unmounted before we managed to get the 8159875cf80SDavid Howells * vfsmount_lock */ 8161da177e4SLinus Torvalds } 8179875cf80SDavid Howells 8189875cf80SDavid Howells /* Handle an automount point */ 8199875cf80SDavid Howells if (managed & DCACHE_NEED_AUTOMOUNT) { 8209875cf80SDavid Howells ret = follow_automount(path, flags, &need_mntput); 8219875cf80SDavid Howells if (ret < 0) 8228aef1884SAl Viro break; 8239875cf80SDavid Howells continue; 8249875cf80SDavid Howells } 8259875cf80SDavid Howells 8269875cf80SDavid Howells /* We didn't change the current path point */ 8279875cf80SDavid Howells break; 8289875cf80SDavid Howells } 8298aef1884SAl Viro 8308aef1884SAl Viro if (need_mntput && path->mnt == mnt) 8318aef1884SAl Viro mntput(path->mnt); 8328aef1884SAl Viro if (ret == -EISDIR) 8338aef1884SAl Viro ret = 0; 834a3fbbde7SAl Viro return ret < 0 ? ret : need_mntput; 8351da177e4SLinus Torvalds } 8361da177e4SLinus Torvalds 837cc53ce53SDavid Howells int follow_down_one(struct path *path) 8381da177e4SLinus Torvalds { 8391da177e4SLinus Torvalds struct vfsmount *mounted; 8401da177e4SLinus Torvalds 8411c755af4SAl Viro mounted = lookup_mnt(path); 8421da177e4SLinus Torvalds if (mounted) { 8439393bd07SAl Viro dput(path->dentry); 8449393bd07SAl Viro mntput(path->mnt); 8459393bd07SAl Viro path->mnt = mounted; 8469393bd07SAl Viro path->dentry = dget(mounted->mnt_root); 8471da177e4SLinus Torvalds return 1; 8481da177e4SLinus Torvalds } 8491da177e4SLinus Torvalds return 0; 8501da177e4SLinus Torvalds } 8511da177e4SLinus Torvalds 85262a7375eSIan Kent static inline bool managed_dentry_might_block(struct dentry *dentry) 85362a7375eSIan Kent { 85462a7375eSIan Kent return (dentry->d_flags & DCACHE_MANAGE_TRANSIT && 85562a7375eSIan Kent dentry->d_op->d_manage(dentry, true) < 0); 85662a7375eSIan Kent } 85762a7375eSIan Kent 8589875cf80SDavid Howells /* 859287548e4SAl Viro * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if 860287548e4SAl Viro * we meet a managed dentry that would need blocking. 8619875cf80SDavid Howells */ 8629875cf80SDavid Howells static bool __follow_mount_rcu(struct nameidata *nd, struct path *path, 863287548e4SAl Viro struct inode **inode) 8649875cf80SDavid Howells { 86562a7375eSIan Kent for (;;) { 866c7105365SAl Viro struct mount *mounted; 86762a7375eSIan Kent /* 86862a7375eSIan Kent * Don't forget we might have a non-mountpoint managed dentry 86962a7375eSIan Kent * that wants to block transit. 87062a7375eSIan Kent */ 871287548e4SAl Viro if (unlikely(managed_dentry_might_block(path->dentry))) 872ab90911fSDavid Howells return false; 87362a7375eSIan Kent 87462a7375eSIan Kent if (!d_mountpoint(path->dentry)) 87562a7375eSIan Kent break; 87662a7375eSIan Kent 8779875cf80SDavid Howells mounted = __lookup_mnt(path->mnt, path->dentry, 1); 8789875cf80SDavid Howells if (!mounted) 8799875cf80SDavid Howells break; 880c7105365SAl Viro path->mnt = &mounted->mnt; 881c7105365SAl Viro path->dentry = mounted->mnt.mnt_root; 882a3fbbde7SAl Viro nd->flags |= LOOKUP_JUMPED; 8839875cf80SDavid Howells nd->seq = read_seqcount_begin(&path->dentry->d_seq); 88459430262SLinus Torvalds /* 88559430262SLinus Torvalds * Update the inode too. We don't need to re-check the 88659430262SLinus Torvalds * dentry sequence number here after this d_inode read, 88759430262SLinus Torvalds * because a mount-point is always pinned. 88859430262SLinus Torvalds */ 88959430262SLinus Torvalds *inode = path->dentry->d_inode; 8909875cf80SDavid Howells } 8919875cf80SDavid Howells return true; 8929875cf80SDavid Howells } 8939875cf80SDavid Howells 894dea39376SAl Viro static void follow_mount_rcu(struct nameidata *nd) 895287548e4SAl Viro { 896dea39376SAl Viro while (d_mountpoint(nd->path.dentry)) { 897c7105365SAl Viro struct mount *mounted; 898dea39376SAl Viro mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1); 899287548e4SAl Viro if (!mounted) 900287548e4SAl Viro break; 901c7105365SAl Viro nd->path.mnt = &mounted->mnt; 902c7105365SAl Viro nd->path.dentry = mounted->mnt.mnt_root; 903dea39376SAl Viro nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); 904287548e4SAl Viro } 905287548e4SAl Viro } 906287548e4SAl Viro 90731e6b01fSNick Piggin static int follow_dotdot_rcu(struct nameidata *nd) 90831e6b01fSNick Piggin { 90931e6b01fSNick Piggin set_root_rcu(nd); 91031e6b01fSNick Piggin 91131e6b01fSNick Piggin while (1) { 91231e6b01fSNick Piggin if (nd->path.dentry == nd->root.dentry && 91331e6b01fSNick Piggin nd->path.mnt == nd->root.mnt) { 91431e6b01fSNick Piggin break; 91531e6b01fSNick Piggin } 91631e6b01fSNick Piggin if (nd->path.dentry != nd->path.mnt->mnt_root) { 91731e6b01fSNick Piggin struct dentry *old = nd->path.dentry; 91831e6b01fSNick Piggin struct dentry *parent = old->d_parent; 91931e6b01fSNick Piggin unsigned seq; 92031e6b01fSNick Piggin 92131e6b01fSNick Piggin seq = read_seqcount_begin(&parent->d_seq); 92231e6b01fSNick Piggin if (read_seqcount_retry(&old->d_seq, nd->seq)) 923ef7562d5SAl Viro goto failed; 92431e6b01fSNick Piggin nd->path.dentry = parent; 92531e6b01fSNick Piggin nd->seq = seq; 92631e6b01fSNick Piggin break; 92731e6b01fSNick Piggin } 92831e6b01fSNick Piggin if (!follow_up_rcu(&nd->path)) 92931e6b01fSNick Piggin break; 93031e6b01fSNick Piggin nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); 93131e6b01fSNick Piggin } 932dea39376SAl Viro follow_mount_rcu(nd); 933dea39376SAl Viro nd->inode = nd->path.dentry->d_inode; 93431e6b01fSNick Piggin return 0; 935ef7562d5SAl Viro 936ef7562d5SAl Viro failed: 937ef7562d5SAl Viro nd->flags &= ~LOOKUP_RCU; 9385b6ca027SAl Viro if (!(nd->flags & LOOKUP_ROOT)) 939ef7562d5SAl Viro nd->root.mnt = NULL; 940ef7562d5SAl Viro rcu_read_unlock(); 941962830dfSAndi Kleen br_read_unlock(&vfsmount_lock); 942ef7562d5SAl Viro return -ECHILD; 94331e6b01fSNick Piggin } 94431e6b01fSNick Piggin 9459875cf80SDavid Howells /* 946cc53ce53SDavid Howells * Follow down to the covering mount currently visible to userspace. At each 947cc53ce53SDavid Howells * point, the filesystem owning that dentry may be queried as to whether the 948cc53ce53SDavid Howells * caller is permitted to proceed or not. 949cc53ce53SDavid Howells */ 9507cc90cc3SAl Viro int follow_down(struct path *path) 951cc53ce53SDavid Howells { 952cc53ce53SDavid Howells unsigned managed; 953cc53ce53SDavid Howells int ret; 954cc53ce53SDavid Howells 955cc53ce53SDavid Howells while (managed = ACCESS_ONCE(path->dentry->d_flags), 956cc53ce53SDavid Howells unlikely(managed & DCACHE_MANAGED_DENTRY)) { 957cc53ce53SDavid Howells /* Allow the filesystem to manage the transit without i_mutex 958cc53ce53SDavid Howells * being held. 959cc53ce53SDavid Howells * 960cc53ce53SDavid Howells * We indicate to the filesystem if someone is trying to mount 961cc53ce53SDavid Howells * something here. This gives autofs the chance to deny anyone 962cc53ce53SDavid Howells * other than its daemon the right to mount on its 963cc53ce53SDavid Howells * superstructure. 964cc53ce53SDavid Howells * 965cc53ce53SDavid Howells * The filesystem may sleep at this point. 966cc53ce53SDavid Howells */ 967cc53ce53SDavid Howells if (managed & DCACHE_MANAGE_TRANSIT) { 968cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op); 969cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op->d_manage); 970ab90911fSDavid Howells ret = path->dentry->d_op->d_manage( 9711aed3e42SAl Viro path->dentry, false); 972cc53ce53SDavid Howells if (ret < 0) 973cc53ce53SDavid Howells return ret == -EISDIR ? 0 : ret; 974cc53ce53SDavid Howells } 975cc53ce53SDavid Howells 976cc53ce53SDavid Howells /* Transit to a mounted filesystem. */ 977cc53ce53SDavid Howells if (managed & DCACHE_MOUNTED) { 978cc53ce53SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 979cc53ce53SDavid Howells if (!mounted) 980cc53ce53SDavid Howells break; 981cc53ce53SDavid Howells dput(path->dentry); 982cc53ce53SDavid Howells mntput(path->mnt); 983cc53ce53SDavid Howells path->mnt = mounted; 984cc53ce53SDavid Howells path->dentry = dget(mounted->mnt_root); 985cc53ce53SDavid Howells continue; 986cc53ce53SDavid Howells } 987cc53ce53SDavid Howells 988cc53ce53SDavid Howells /* Don't handle automount points here */ 989cc53ce53SDavid Howells break; 990cc53ce53SDavid Howells } 991cc53ce53SDavid Howells return 0; 992cc53ce53SDavid Howells } 993cc53ce53SDavid Howells 994cc53ce53SDavid Howells /* 9959875cf80SDavid Howells * Skip to top of mountpoint pile in refwalk mode for follow_dotdot() 9969875cf80SDavid Howells */ 9979875cf80SDavid Howells static void follow_mount(struct path *path) 9989875cf80SDavid Howells { 9999875cf80SDavid Howells while (d_mountpoint(path->dentry)) { 10009875cf80SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 10019875cf80SDavid Howells if (!mounted) 10029875cf80SDavid Howells break; 10039875cf80SDavid Howells dput(path->dentry); 10049875cf80SDavid Howells mntput(path->mnt); 10059875cf80SDavid Howells path->mnt = mounted; 10069875cf80SDavid Howells path->dentry = dget(mounted->mnt_root); 10079875cf80SDavid Howells } 10089875cf80SDavid Howells } 10099875cf80SDavid Howells 101031e6b01fSNick Piggin static void follow_dotdot(struct nameidata *nd) 10111da177e4SLinus Torvalds { 10122a737871SAl Viro set_root(nd); 1013e518ddb7SAndreas Mohr 10141da177e4SLinus Torvalds while(1) { 10154ac91378SJan Blunck struct dentry *old = nd->path.dentry; 10161da177e4SLinus Torvalds 10172a737871SAl Viro if (nd->path.dentry == nd->root.dentry && 10182a737871SAl Viro nd->path.mnt == nd->root.mnt) { 10191da177e4SLinus Torvalds break; 10201da177e4SLinus Torvalds } 10214ac91378SJan Blunck if (nd->path.dentry != nd->path.mnt->mnt_root) { 10223088dd70SAl Viro /* rare case of legitimate dget_parent()... */ 10233088dd70SAl Viro nd->path.dentry = dget_parent(nd->path.dentry); 10241da177e4SLinus Torvalds dput(old); 10251da177e4SLinus Torvalds break; 10261da177e4SLinus Torvalds } 10273088dd70SAl Viro if (!follow_up(&nd->path)) 10281da177e4SLinus Torvalds break; 10291da177e4SLinus Torvalds } 103079ed0226SAl Viro follow_mount(&nd->path); 103131e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 10321da177e4SLinus Torvalds } 10331da177e4SLinus Torvalds 10341da177e4SLinus Torvalds /* 1035bad61189SMiklos Szeredi * This looks up the name in dcache, possibly revalidates the old dentry and 1036bad61189SMiklos Szeredi * allocates a new one if not found or not valid. In the need_lookup argument 1037bad61189SMiklos Szeredi * returns whether i_op->lookup is necessary. 1038bad61189SMiklos Szeredi * 1039bad61189SMiklos Szeredi * dir->d_inode->i_mutex must be held 1040baa03890SNick Piggin */ 1041bad61189SMiklos Szeredi static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir, 1042201f956eSAl Viro unsigned int flags, bool *need_lookup) 1043baa03890SNick Piggin { 1044baa03890SNick Piggin struct dentry *dentry; 1045bad61189SMiklos Szeredi int error; 1046baa03890SNick Piggin 1047bad61189SMiklos Szeredi *need_lookup = false; 1048bad61189SMiklos Szeredi dentry = d_lookup(dir, name); 1049bad61189SMiklos Szeredi if (dentry) { 1050bad61189SMiklos Szeredi if (d_need_lookup(dentry)) { 1051bad61189SMiklos Szeredi *need_lookup = true; 1052bad61189SMiklos Szeredi } else if (dentry->d_flags & DCACHE_OP_REVALIDATE) { 1053201f956eSAl Viro error = d_revalidate(dentry, flags); 1054bad61189SMiklos Szeredi if (unlikely(error <= 0)) { 1055bad61189SMiklos Szeredi if (error < 0) { 1056bad61189SMiklos Szeredi dput(dentry); 1057bad61189SMiklos Szeredi return ERR_PTR(error); 1058bad61189SMiklos Szeredi } else if (!d_invalidate(dentry)) { 1059bad61189SMiklos Szeredi dput(dentry); 1060bad61189SMiklos Szeredi dentry = NULL; 1061bad61189SMiklos Szeredi } 1062bad61189SMiklos Szeredi } 1063bad61189SMiklos Szeredi } 1064bad61189SMiklos Szeredi } 1065baa03890SNick Piggin 1066bad61189SMiklos Szeredi if (!dentry) { 1067bad61189SMiklos Szeredi dentry = d_alloc(dir, name); 1068baa03890SNick Piggin if (unlikely(!dentry)) 1069baa03890SNick Piggin return ERR_PTR(-ENOMEM); 1070baa03890SNick Piggin 1071bad61189SMiklos Szeredi *need_lookup = true; 1072baa03890SNick Piggin } 1073baa03890SNick Piggin return dentry; 1074baa03890SNick Piggin } 1075baa03890SNick Piggin 1076baa03890SNick Piggin /* 1077bad61189SMiklos Szeredi * Call i_op->lookup on the dentry. The dentry must be negative but may be 1078bad61189SMiklos Szeredi * hashed if it was pouplated with DCACHE_NEED_LOOKUP. 1079bad61189SMiklos Szeredi * 1080bad61189SMiklos Szeredi * dir->d_inode->i_mutex must be held 108144396f4bSJosef Bacik */ 1082bad61189SMiklos Szeredi static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry, 108372bd866aSAl Viro unsigned int flags) 108444396f4bSJosef Bacik { 108544396f4bSJosef Bacik struct dentry *old; 108644396f4bSJosef Bacik 108744396f4bSJosef Bacik /* Don't create child dentry for a dead directory. */ 1088bad61189SMiklos Szeredi if (unlikely(IS_DEADDIR(dir))) { 1089e188dc02SMiklos Szeredi dput(dentry); 109044396f4bSJosef Bacik return ERR_PTR(-ENOENT); 1091e188dc02SMiklos Szeredi } 109244396f4bSJosef Bacik 109372bd866aSAl Viro old = dir->i_op->lookup(dir, dentry, flags); 109444396f4bSJosef Bacik if (unlikely(old)) { 109544396f4bSJosef Bacik dput(dentry); 109644396f4bSJosef Bacik dentry = old; 109744396f4bSJosef Bacik } 109844396f4bSJosef Bacik return dentry; 109944396f4bSJosef Bacik } 110044396f4bSJosef Bacik 1101a3255546SAl Viro static struct dentry *__lookup_hash(struct qstr *name, 110272bd866aSAl Viro struct dentry *base, unsigned int flags) 1103a3255546SAl Viro { 1104bad61189SMiklos Szeredi bool need_lookup; 1105a3255546SAl Viro struct dentry *dentry; 1106a3255546SAl Viro 110772bd866aSAl Viro dentry = lookup_dcache(name, base, flags, &need_lookup); 1108bad61189SMiklos Szeredi if (!need_lookup) 1109a3255546SAl Viro return dentry; 1110bad61189SMiklos Szeredi 111172bd866aSAl Viro return lookup_real(base->d_inode, dentry, flags); 1112a3255546SAl Viro } 1113a3255546SAl Viro 111444396f4bSJosef Bacik /* 11151da177e4SLinus Torvalds * It's more convoluted than I'd like it to be, but... it's still fairly 11161da177e4SLinus Torvalds * small and for now I'd prefer to have fast path as straight as possible. 11171da177e4SLinus Torvalds * It _is_ time-critical. 11181da177e4SLinus Torvalds */ 1119697f514dSMiklos Szeredi static int lookup_fast(struct nameidata *nd, struct qstr *name, 112031e6b01fSNick Piggin struct path *path, struct inode **inode) 11211da177e4SLinus Torvalds { 11224ac91378SJan Blunck struct vfsmount *mnt = nd->path.mnt; 112331e6b01fSNick Piggin struct dentry *dentry, *parent = nd->path.dentry; 11245a18fff2SAl Viro int need_reval = 1; 11255a18fff2SAl Viro int status = 1; 11269875cf80SDavid Howells int err; 11279875cf80SDavid Howells 11283cac260aSAl Viro /* 1129b04f784eSNick Piggin * Rename seqlock is not required here because in the off chance 1130b04f784eSNick Piggin * of a false negative due to a concurrent rename, we're going to 1131b04f784eSNick Piggin * do the non-racy lookup, below. 1132b04f784eSNick Piggin */ 113331e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) { 113431e6b01fSNick Piggin unsigned seq; 113512f8ad4bSLinus Torvalds dentry = __d_lookup_rcu(parent, name, &seq, nd->inode); 11365a18fff2SAl Viro if (!dentry) 11375a18fff2SAl Viro goto unlazy; 11385a18fff2SAl Viro 113912f8ad4bSLinus Torvalds /* 114012f8ad4bSLinus Torvalds * This sequence count validates that the inode matches 114112f8ad4bSLinus Torvalds * the dentry name information from lookup. 114212f8ad4bSLinus Torvalds */ 114312f8ad4bSLinus Torvalds *inode = dentry->d_inode; 114412f8ad4bSLinus Torvalds if (read_seqcount_retry(&dentry->d_seq, seq)) 114512f8ad4bSLinus Torvalds return -ECHILD; 114612f8ad4bSLinus Torvalds 114712f8ad4bSLinus Torvalds /* 114812f8ad4bSLinus Torvalds * This sequence count validates that the parent had no 114912f8ad4bSLinus Torvalds * changes while we did the lookup of the dentry above. 115012f8ad4bSLinus Torvalds * 115112f8ad4bSLinus Torvalds * The memory barrier in read_seqcount_begin of child is 115212f8ad4bSLinus Torvalds * enough, we can use __read_seqcount_retry here. 115312f8ad4bSLinus Torvalds */ 115431e6b01fSNick Piggin if (__read_seqcount_retry(&parent->d_seq, nd->seq)) 115531e6b01fSNick Piggin return -ECHILD; 115631e6b01fSNick Piggin nd->seq = seq; 11575a18fff2SAl Viro 1158fa4ee159SMiklos Szeredi if (unlikely(d_need_lookup(dentry))) 1159fa4ee159SMiklos Szeredi goto unlazy; 116024643087SAl Viro if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) { 11614ce16ef3SAl Viro status = d_revalidate(dentry, nd->flags); 11625a18fff2SAl Viro if (unlikely(status <= 0)) { 11635a18fff2SAl Viro if (status != -ECHILD) 11645a18fff2SAl Viro need_reval = 0; 11655a18fff2SAl Viro goto unlazy; 11665a18fff2SAl Viro } 116724643087SAl Viro } 116831e6b01fSNick Piggin path->mnt = mnt; 116931e6b01fSNick Piggin path->dentry = dentry; 1170d6e9bd25SAl Viro if (unlikely(!__follow_mount_rcu(nd, path, inode))) 1171d6e9bd25SAl Viro goto unlazy; 1172d6e9bd25SAl Viro if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT)) 1173d6e9bd25SAl Viro goto unlazy; 11749875cf80SDavid Howells return 0; 11755a18fff2SAl Viro unlazy: 117619660af7SAl Viro if (unlazy_walk(nd, dentry)) 11775a18fff2SAl Viro return -ECHILD; 11785a18fff2SAl Viro } else { 117931e6b01fSNick Piggin dentry = __d_lookup(parent, name); 118024643087SAl Viro } 11815a18fff2SAl Viro 118281e6f520SAl Viro if (unlikely(!dentry)) 118381e6f520SAl Viro goto need_lookup; 11845a18fff2SAl Viro 118581e6f520SAl Viro if (unlikely(d_need_lookup(dentry))) { 118681e6f520SAl Viro dput(dentry); 118781e6f520SAl Viro goto need_lookup; 11885a18fff2SAl Viro } 118981e6f520SAl Viro 11905a18fff2SAl Viro if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval) 11914ce16ef3SAl Viro status = d_revalidate(dentry, nd->flags); 11925a18fff2SAl Viro if (unlikely(status <= 0)) { 11935a18fff2SAl Viro if (status < 0) { 11945a18fff2SAl Viro dput(dentry); 11955a18fff2SAl Viro return status; 11965a18fff2SAl Viro } 11975a18fff2SAl Viro if (!d_invalidate(dentry)) { 11985a18fff2SAl Viro dput(dentry); 119981e6f520SAl Viro goto need_lookup; 12005a18fff2SAl Viro } 12015a18fff2SAl Viro } 1202697f514dSMiklos Szeredi 12031da177e4SLinus Torvalds path->mnt = mnt; 12041da177e4SLinus Torvalds path->dentry = dentry; 12059875cf80SDavid Howells err = follow_managed(path, nd->flags); 120689312214SIan Kent if (unlikely(err < 0)) { 120789312214SIan Kent path_put_conditional(path, nd); 12089875cf80SDavid Howells return err; 120989312214SIan Kent } 1210a3fbbde7SAl Viro if (err) 1211a3fbbde7SAl Viro nd->flags |= LOOKUP_JUMPED; 121231e6b01fSNick Piggin *inode = path->dentry->d_inode; 12131da177e4SLinus Torvalds return 0; 121481e6f520SAl Viro 121581e6f520SAl Viro need_lookup: 1216697f514dSMiklos Szeredi return 1; 1217697f514dSMiklos Szeredi } 1218697f514dSMiklos Szeredi 1219697f514dSMiklos Szeredi /* Fast lookup failed, do it the slow way */ 1220697f514dSMiklos Szeredi static int lookup_slow(struct nameidata *nd, struct qstr *name, 1221697f514dSMiklos Szeredi struct path *path) 1222697f514dSMiklos Szeredi { 1223697f514dSMiklos Szeredi struct dentry *dentry, *parent; 1224697f514dSMiklos Szeredi int err; 1225697f514dSMiklos Szeredi 1226697f514dSMiklos Szeredi parent = nd->path.dentry; 122781e6f520SAl Viro BUG_ON(nd->inode != parent->d_inode); 122881e6f520SAl Viro 122981e6f520SAl Viro mutex_lock(&parent->d_inode->i_mutex); 123072bd866aSAl Viro dentry = __lookup_hash(name, parent, nd->flags); 123181e6f520SAl Viro mutex_unlock(&parent->d_inode->i_mutex); 123281e6f520SAl Viro if (IS_ERR(dentry)) 123381e6f520SAl Viro return PTR_ERR(dentry); 1234697f514dSMiklos Szeredi path->mnt = nd->path.mnt; 1235697f514dSMiklos Szeredi path->dentry = dentry; 1236697f514dSMiklos Szeredi err = follow_managed(path, nd->flags); 1237697f514dSMiklos Szeredi if (unlikely(err < 0)) { 1238697f514dSMiklos Szeredi path_put_conditional(path, nd); 1239697f514dSMiklos Szeredi return err; 1240697f514dSMiklos Szeredi } 1241697f514dSMiklos Szeredi if (err) 1242697f514dSMiklos Szeredi nd->flags |= LOOKUP_JUMPED; 1243697f514dSMiklos Szeredi return 0; 12441da177e4SLinus Torvalds } 12451da177e4SLinus Torvalds 124652094c8aSAl Viro static inline int may_lookup(struct nameidata *nd) 124752094c8aSAl Viro { 124852094c8aSAl Viro if (nd->flags & LOOKUP_RCU) { 12494ad5abb3SAl Viro int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK); 125052094c8aSAl Viro if (err != -ECHILD) 125152094c8aSAl Viro return err; 125219660af7SAl Viro if (unlazy_walk(nd, NULL)) 125352094c8aSAl Viro return -ECHILD; 125452094c8aSAl Viro } 12554ad5abb3SAl Viro return inode_permission(nd->inode, MAY_EXEC); 125652094c8aSAl Viro } 125752094c8aSAl Viro 12589856fa1bSAl Viro static inline int handle_dots(struct nameidata *nd, int type) 12599856fa1bSAl Viro { 12609856fa1bSAl Viro if (type == LAST_DOTDOT) { 12619856fa1bSAl Viro if (nd->flags & LOOKUP_RCU) { 12629856fa1bSAl Viro if (follow_dotdot_rcu(nd)) 12639856fa1bSAl Viro return -ECHILD; 12649856fa1bSAl Viro } else 12659856fa1bSAl Viro follow_dotdot(nd); 12669856fa1bSAl Viro } 12679856fa1bSAl Viro return 0; 12689856fa1bSAl Viro } 12699856fa1bSAl Viro 1270951361f9SAl Viro static void terminate_walk(struct nameidata *nd) 1271951361f9SAl Viro { 1272951361f9SAl Viro if (!(nd->flags & LOOKUP_RCU)) { 1273951361f9SAl Viro path_put(&nd->path); 1274951361f9SAl Viro } else { 1275951361f9SAl Viro nd->flags &= ~LOOKUP_RCU; 12765b6ca027SAl Viro if (!(nd->flags & LOOKUP_ROOT)) 1277951361f9SAl Viro nd->root.mnt = NULL; 1278951361f9SAl Viro rcu_read_unlock(); 1279962830dfSAndi Kleen br_read_unlock(&vfsmount_lock); 1280951361f9SAl Viro } 1281951361f9SAl Viro } 1282951361f9SAl Viro 12833ddcd056SLinus Torvalds /* 12843ddcd056SLinus Torvalds * Do we need to follow links? We _really_ want to be able 12853ddcd056SLinus Torvalds * to do this check without having to look at inode->i_op, 12863ddcd056SLinus Torvalds * so we keep a cache of "no, this doesn't need follow_link" 12873ddcd056SLinus Torvalds * for the common case. 12883ddcd056SLinus Torvalds */ 12897813b94aSLinus Torvalds static inline int should_follow_link(struct inode *inode, int follow) 12903ddcd056SLinus Torvalds { 12913ddcd056SLinus Torvalds if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) { 12923ddcd056SLinus Torvalds if (likely(inode->i_op->follow_link)) 12933ddcd056SLinus Torvalds return follow; 12943ddcd056SLinus Torvalds 12953ddcd056SLinus Torvalds /* This gets set once for the inode lifetime */ 12963ddcd056SLinus Torvalds spin_lock(&inode->i_lock); 12973ddcd056SLinus Torvalds inode->i_opflags |= IOP_NOFOLLOW; 12983ddcd056SLinus Torvalds spin_unlock(&inode->i_lock); 12993ddcd056SLinus Torvalds } 13003ddcd056SLinus Torvalds return 0; 13013ddcd056SLinus Torvalds } 13023ddcd056SLinus Torvalds 1303ce57dfc1SAl Viro static inline int walk_component(struct nameidata *nd, struct path *path, 1304ce57dfc1SAl Viro struct qstr *name, int type, int follow) 1305ce57dfc1SAl Viro { 1306ce57dfc1SAl Viro struct inode *inode; 1307ce57dfc1SAl Viro int err; 1308ce57dfc1SAl Viro /* 1309ce57dfc1SAl Viro * "." and ".." are special - ".." especially so because it has 1310ce57dfc1SAl Viro * to be able to know about the current root directory and 1311ce57dfc1SAl Viro * parent relationships. 1312ce57dfc1SAl Viro */ 1313ce57dfc1SAl Viro if (unlikely(type != LAST_NORM)) 1314ce57dfc1SAl Viro return handle_dots(nd, type); 1315697f514dSMiklos Szeredi err = lookup_fast(nd, name, path, &inode); 1316ce57dfc1SAl Viro if (unlikely(err)) { 1317697f514dSMiklos Szeredi if (err < 0) 1318697f514dSMiklos Szeredi goto out_err; 1319697f514dSMiklos Szeredi 1320697f514dSMiklos Szeredi err = lookup_slow(nd, name, path); 1321697f514dSMiklos Szeredi if (err < 0) 1322697f514dSMiklos Szeredi goto out_err; 1323697f514dSMiklos Szeredi 1324697f514dSMiklos Szeredi inode = path->dentry->d_inode; 1325ce57dfc1SAl Viro } 1326697f514dSMiklos Szeredi err = -ENOENT; 1327697f514dSMiklos Szeredi if (!inode) 1328697f514dSMiklos Szeredi goto out_path_put; 1329697f514dSMiklos Szeredi 13307813b94aSLinus Torvalds if (should_follow_link(inode, follow)) { 133119660af7SAl Viro if (nd->flags & LOOKUP_RCU) { 133219660af7SAl Viro if (unlikely(unlazy_walk(nd, path->dentry))) { 1333697f514dSMiklos Szeredi err = -ECHILD; 1334697f514dSMiklos Szeredi goto out_err; 133519660af7SAl Viro } 133619660af7SAl Viro } 1337ce57dfc1SAl Viro BUG_ON(inode != path->dentry->d_inode); 1338ce57dfc1SAl Viro return 1; 1339ce57dfc1SAl Viro } 1340ce57dfc1SAl Viro path_to_nameidata(path, nd); 1341ce57dfc1SAl Viro nd->inode = inode; 1342ce57dfc1SAl Viro return 0; 1343697f514dSMiklos Szeredi 1344697f514dSMiklos Szeredi out_path_put: 1345697f514dSMiklos Szeredi path_to_nameidata(path, nd); 1346697f514dSMiklos Szeredi out_err: 1347697f514dSMiklos Szeredi terminate_walk(nd); 1348697f514dSMiklos Szeredi return err; 1349ce57dfc1SAl Viro } 1350ce57dfc1SAl Viro 13511da177e4SLinus Torvalds /* 1352b356379aSAl Viro * This limits recursive symlink follows to 8, while 1353b356379aSAl Viro * limiting consecutive symlinks to 40. 1354b356379aSAl Viro * 1355b356379aSAl Viro * Without that kind of total limit, nasty chains of consecutive 1356b356379aSAl Viro * symlinks can cause almost arbitrarily long lookups. 1357b356379aSAl Viro */ 1358b356379aSAl Viro static inline int nested_symlink(struct path *path, struct nameidata *nd) 1359b356379aSAl Viro { 1360b356379aSAl Viro int res; 1361b356379aSAl Viro 1362b356379aSAl Viro if (unlikely(current->link_count >= MAX_NESTED_LINKS)) { 1363b356379aSAl Viro path_put_conditional(path, nd); 1364b356379aSAl Viro path_put(&nd->path); 1365b356379aSAl Viro return -ELOOP; 1366b356379aSAl Viro } 13671a4022f8SErez Zadok BUG_ON(nd->depth >= MAX_NESTED_LINKS); 1368b356379aSAl Viro 1369b356379aSAl Viro nd->depth++; 1370b356379aSAl Viro current->link_count++; 1371b356379aSAl Viro 1372b356379aSAl Viro do { 1373b356379aSAl Viro struct path link = *path; 1374b356379aSAl Viro void *cookie; 1375574197e0SAl Viro 1376574197e0SAl Viro res = follow_link(&link, nd, &cookie); 13776d7b5aaeSAl Viro if (res) 13786d7b5aaeSAl Viro break; 1379b356379aSAl Viro res = walk_component(nd, path, &nd->last, 1380b356379aSAl Viro nd->last_type, LOOKUP_FOLLOW); 1381574197e0SAl Viro put_link(nd, &link, cookie); 1382b356379aSAl Viro } while (res > 0); 1383b356379aSAl Viro 1384b356379aSAl Viro current->link_count--; 1385b356379aSAl Viro nd->depth--; 1386b356379aSAl Viro return res; 1387b356379aSAl Viro } 1388b356379aSAl Viro 1389b356379aSAl Viro /* 13903ddcd056SLinus Torvalds * We really don't want to look at inode->i_op->lookup 13913ddcd056SLinus Torvalds * when we don't have to. So we keep a cache bit in 13923ddcd056SLinus Torvalds * the inode ->i_opflags field that says "yes, we can 13933ddcd056SLinus Torvalds * do lookup on this inode". 13943ddcd056SLinus Torvalds */ 13953ddcd056SLinus Torvalds static inline int can_lookup(struct inode *inode) 13963ddcd056SLinus Torvalds { 13973ddcd056SLinus Torvalds if (likely(inode->i_opflags & IOP_LOOKUP)) 13983ddcd056SLinus Torvalds return 1; 13993ddcd056SLinus Torvalds if (likely(!inode->i_op->lookup)) 14003ddcd056SLinus Torvalds return 0; 14013ddcd056SLinus Torvalds 14023ddcd056SLinus Torvalds /* We do this once for the lifetime of the inode */ 14033ddcd056SLinus Torvalds spin_lock(&inode->i_lock); 14043ddcd056SLinus Torvalds inode->i_opflags |= IOP_LOOKUP; 14053ddcd056SLinus Torvalds spin_unlock(&inode->i_lock); 14063ddcd056SLinus Torvalds return 1; 14073ddcd056SLinus Torvalds } 14083ddcd056SLinus Torvalds 1409bfcfaa77SLinus Torvalds /* 1410bfcfaa77SLinus Torvalds * We can do the critical dentry name comparison and hashing 1411bfcfaa77SLinus Torvalds * operations one word at a time, but we are limited to: 1412bfcfaa77SLinus Torvalds * 1413bfcfaa77SLinus Torvalds * - Architectures with fast unaligned word accesses. We could 1414bfcfaa77SLinus Torvalds * do a "get_unaligned()" if this helps and is sufficiently 1415bfcfaa77SLinus Torvalds * fast. 1416bfcfaa77SLinus Torvalds * 1417bfcfaa77SLinus Torvalds * - Little-endian machines (so that we can generate the mask 1418bfcfaa77SLinus Torvalds * of low bytes efficiently). Again, we *could* do a byte 1419bfcfaa77SLinus Torvalds * swapping load on big-endian architectures if that is not 1420bfcfaa77SLinus Torvalds * expensive enough to make the optimization worthless. 1421bfcfaa77SLinus Torvalds * 1422bfcfaa77SLinus Torvalds * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we 1423bfcfaa77SLinus Torvalds * do not trap on the (extremely unlikely) case of a page 1424bfcfaa77SLinus Torvalds * crossing operation. 1425bfcfaa77SLinus Torvalds * 1426bfcfaa77SLinus Torvalds * - Furthermore, we need an efficient 64-bit compile for the 1427bfcfaa77SLinus Torvalds * 64-bit case in order to generate the "number of bytes in 1428bfcfaa77SLinus Torvalds * the final mask". Again, that could be replaced with a 1429bfcfaa77SLinus Torvalds * efficient population count instruction or similar. 1430bfcfaa77SLinus Torvalds */ 1431bfcfaa77SLinus Torvalds #ifdef CONFIG_DCACHE_WORD_ACCESS 1432bfcfaa77SLinus Torvalds 1433f68e556eSLinus Torvalds #include <asm/word-at-a-time.h> 1434bfcfaa77SLinus Torvalds 1435f68e556eSLinus Torvalds #ifdef CONFIG_64BIT 1436bfcfaa77SLinus Torvalds 1437bfcfaa77SLinus Torvalds static inline unsigned int fold_hash(unsigned long hash) 1438bfcfaa77SLinus Torvalds { 1439bfcfaa77SLinus Torvalds hash += hash >> (8*sizeof(int)); 1440bfcfaa77SLinus Torvalds return hash; 1441bfcfaa77SLinus Torvalds } 1442bfcfaa77SLinus Torvalds 1443bfcfaa77SLinus Torvalds #else /* 32-bit case */ 1444bfcfaa77SLinus Torvalds 1445bfcfaa77SLinus Torvalds #define fold_hash(x) (x) 1446bfcfaa77SLinus Torvalds 1447bfcfaa77SLinus Torvalds #endif 1448bfcfaa77SLinus Torvalds 1449bfcfaa77SLinus Torvalds unsigned int full_name_hash(const unsigned char *name, unsigned int len) 1450bfcfaa77SLinus Torvalds { 1451bfcfaa77SLinus Torvalds unsigned long a, mask; 1452bfcfaa77SLinus Torvalds unsigned long hash = 0; 1453bfcfaa77SLinus Torvalds 1454bfcfaa77SLinus Torvalds for (;;) { 1455e419b4ccSLinus Torvalds a = load_unaligned_zeropad(name); 1456bfcfaa77SLinus Torvalds if (len < sizeof(unsigned long)) 1457bfcfaa77SLinus Torvalds break; 1458bfcfaa77SLinus Torvalds hash += a; 1459f132c5beSAl Viro hash *= 9; 1460bfcfaa77SLinus Torvalds name += sizeof(unsigned long); 1461bfcfaa77SLinus Torvalds len -= sizeof(unsigned long); 1462bfcfaa77SLinus Torvalds if (!len) 1463bfcfaa77SLinus Torvalds goto done; 1464bfcfaa77SLinus Torvalds } 1465bfcfaa77SLinus Torvalds mask = ~(~0ul << len*8); 1466bfcfaa77SLinus Torvalds hash += mask & a; 1467bfcfaa77SLinus Torvalds done: 1468bfcfaa77SLinus Torvalds return fold_hash(hash); 1469bfcfaa77SLinus Torvalds } 1470bfcfaa77SLinus Torvalds EXPORT_SYMBOL(full_name_hash); 1471bfcfaa77SLinus Torvalds 1472bfcfaa77SLinus Torvalds /* 1473bfcfaa77SLinus Torvalds * Calculate the length and hash of the path component, and 1474bfcfaa77SLinus Torvalds * return the length of the component; 1475bfcfaa77SLinus Torvalds */ 1476bfcfaa77SLinus Torvalds static inline unsigned long hash_name(const char *name, unsigned int *hashp) 1477bfcfaa77SLinus Torvalds { 147836126f8fSLinus Torvalds unsigned long a, b, adata, bdata, mask, hash, len; 147936126f8fSLinus Torvalds const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS; 1480bfcfaa77SLinus Torvalds 1481bfcfaa77SLinus Torvalds hash = a = 0; 1482bfcfaa77SLinus Torvalds len = -sizeof(unsigned long); 1483bfcfaa77SLinus Torvalds do { 1484bfcfaa77SLinus Torvalds hash = (hash + a) * 9; 1485bfcfaa77SLinus Torvalds len += sizeof(unsigned long); 1486e419b4ccSLinus Torvalds a = load_unaligned_zeropad(name+len); 148736126f8fSLinus Torvalds b = a ^ REPEAT_BYTE('/'); 148836126f8fSLinus Torvalds } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants))); 1489bfcfaa77SLinus Torvalds 149036126f8fSLinus Torvalds adata = prep_zero_mask(a, adata, &constants); 149136126f8fSLinus Torvalds bdata = prep_zero_mask(b, bdata, &constants); 149236126f8fSLinus Torvalds 149336126f8fSLinus Torvalds mask = create_zero_mask(adata | bdata); 149436126f8fSLinus Torvalds 149536126f8fSLinus Torvalds hash += a & zero_bytemask(mask); 1496bfcfaa77SLinus Torvalds *hashp = fold_hash(hash); 1497bfcfaa77SLinus Torvalds 149836126f8fSLinus Torvalds return len + find_zero(mask); 1499bfcfaa77SLinus Torvalds } 1500bfcfaa77SLinus Torvalds 1501bfcfaa77SLinus Torvalds #else 1502bfcfaa77SLinus Torvalds 15030145acc2SLinus Torvalds unsigned int full_name_hash(const unsigned char *name, unsigned int len) 15040145acc2SLinus Torvalds { 15050145acc2SLinus Torvalds unsigned long hash = init_name_hash(); 15060145acc2SLinus Torvalds while (len--) 15070145acc2SLinus Torvalds hash = partial_name_hash(*name++, hash); 15080145acc2SLinus Torvalds return end_name_hash(hash); 15090145acc2SLinus Torvalds } 1510ae942ae7SLinus Torvalds EXPORT_SYMBOL(full_name_hash); 15110145acc2SLinus Torvalds 15123ddcd056SLinus Torvalds /* 1513200e9ef7SLinus Torvalds * We know there's a real path component here of at least 1514200e9ef7SLinus Torvalds * one character. 1515200e9ef7SLinus Torvalds */ 1516200e9ef7SLinus Torvalds static inline unsigned long hash_name(const char *name, unsigned int *hashp) 1517200e9ef7SLinus Torvalds { 1518200e9ef7SLinus Torvalds unsigned long hash = init_name_hash(); 1519200e9ef7SLinus Torvalds unsigned long len = 0, c; 1520200e9ef7SLinus Torvalds 1521200e9ef7SLinus Torvalds c = (unsigned char)*name; 1522200e9ef7SLinus Torvalds do { 1523200e9ef7SLinus Torvalds len++; 1524200e9ef7SLinus Torvalds hash = partial_name_hash(c, hash); 1525200e9ef7SLinus Torvalds c = (unsigned char)name[len]; 1526200e9ef7SLinus Torvalds } while (c && c != '/'); 1527200e9ef7SLinus Torvalds *hashp = end_name_hash(hash); 1528200e9ef7SLinus Torvalds return len; 1529200e9ef7SLinus Torvalds } 1530200e9ef7SLinus Torvalds 1531bfcfaa77SLinus Torvalds #endif 1532bfcfaa77SLinus Torvalds 1533200e9ef7SLinus Torvalds /* 15341da177e4SLinus Torvalds * Name resolution. 1535ea3834d9SPrasanna Meda * This is the basic name resolution function, turning a pathname into 1536ea3834d9SPrasanna Meda * the final dentry. We expect 'base' to be positive and a directory. 15371da177e4SLinus Torvalds * 1538ea3834d9SPrasanna Meda * Returns 0 and nd will have valid dentry and mnt on success. 1539ea3834d9SPrasanna Meda * Returns error and drops reference to input namei data on failure. 15401da177e4SLinus Torvalds */ 15416de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd) 15421da177e4SLinus Torvalds { 15431da177e4SLinus Torvalds struct path next; 15441da177e4SLinus Torvalds int err; 15451da177e4SLinus Torvalds 15461da177e4SLinus Torvalds while (*name=='/') 15471da177e4SLinus Torvalds name++; 15481da177e4SLinus Torvalds if (!*name) 1549086e183aSAl Viro return 0; 15501da177e4SLinus Torvalds 15511da177e4SLinus Torvalds /* At this point we know we have a real path component. */ 15521da177e4SLinus Torvalds for(;;) { 15531da177e4SLinus Torvalds struct qstr this; 1554200e9ef7SLinus Torvalds long len; 1555fe479a58SAl Viro int type; 15561da177e4SLinus Torvalds 155752094c8aSAl Viro err = may_lookup(nd); 15581da177e4SLinus Torvalds if (err) 15591da177e4SLinus Torvalds break; 15601da177e4SLinus Torvalds 1561200e9ef7SLinus Torvalds len = hash_name(name, &this.hash); 15621da177e4SLinus Torvalds this.name = name; 1563200e9ef7SLinus Torvalds this.len = len; 15641da177e4SLinus Torvalds 1565fe479a58SAl Viro type = LAST_NORM; 1566200e9ef7SLinus Torvalds if (name[0] == '.') switch (len) { 1567fe479a58SAl Viro case 2: 1568200e9ef7SLinus Torvalds if (name[1] == '.') { 1569fe479a58SAl Viro type = LAST_DOTDOT; 157016c2cd71SAl Viro nd->flags |= LOOKUP_JUMPED; 157116c2cd71SAl Viro } 1572fe479a58SAl Viro break; 1573fe479a58SAl Viro case 1: 1574fe479a58SAl Viro type = LAST_DOT; 1575fe479a58SAl Viro } 15765a202bcdSAl Viro if (likely(type == LAST_NORM)) { 15775a202bcdSAl Viro struct dentry *parent = nd->path.dentry; 157816c2cd71SAl Viro nd->flags &= ~LOOKUP_JUMPED; 15795a202bcdSAl Viro if (unlikely(parent->d_flags & DCACHE_OP_HASH)) { 15805a202bcdSAl Viro err = parent->d_op->d_hash(parent, nd->inode, 15815a202bcdSAl Viro &this); 15825a202bcdSAl Viro if (err < 0) 15835a202bcdSAl Viro break; 15845a202bcdSAl Viro } 15855a202bcdSAl Viro } 1586fe479a58SAl Viro 1587200e9ef7SLinus Torvalds if (!name[len]) 15881da177e4SLinus Torvalds goto last_component; 1589200e9ef7SLinus Torvalds /* 1590200e9ef7SLinus Torvalds * If it wasn't NUL, we know it was '/'. Skip that 1591200e9ef7SLinus Torvalds * slash, and continue until no more slashes. 1592200e9ef7SLinus Torvalds */ 1593200e9ef7SLinus Torvalds do { 1594200e9ef7SLinus Torvalds len++; 1595200e9ef7SLinus Torvalds } while (unlikely(name[len] == '/')); 1596200e9ef7SLinus Torvalds if (!name[len]) 1597b356379aSAl Viro goto last_component; 1598200e9ef7SLinus Torvalds name += len; 15991da177e4SLinus Torvalds 1600ce57dfc1SAl Viro err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW); 1601ce57dfc1SAl Viro if (err < 0) 1602ce57dfc1SAl Viro return err; 1603fe479a58SAl Viro 1604ce57dfc1SAl Viro if (err) { 1605b356379aSAl Viro err = nested_symlink(&next, nd); 16061da177e4SLinus Torvalds if (err) 1607a7472babSAl Viro return err; 160831e6b01fSNick Piggin } 16093ddcd056SLinus Torvalds if (can_lookup(nd->inode)) 16101da177e4SLinus Torvalds continue; 16113ddcd056SLinus Torvalds err = -ENOTDIR; 16123ddcd056SLinus Torvalds break; 16131da177e4SLinus Torvalds /* here ends the main loop */ 16141da177e4SLinus Torvalds 16151da177e4SLinus Torvalds last_component: 1616ce57dfc1SAl Viro nd->last = this; 1617ce57dfc1SAl Viro nd->last_type = type; 1618ce57dfc1SAl Viro return 0; 1619ce57dfc1SAl Viro } 1620951361f9SAl Viro terminate_walk(nd); 16211da177e4SLinus Torvalds return err; 16221da177e4SLinus Torvalds } 16231da177e4SLinus Torvalds 162470e9b357SAl Viro static int path_init(int dfd, const char *name, unsigned int flags, 162570e9b357SAl Viro struct nameidata *nd, struct file **fp) 162631e6b01fSNick Piggin { 162731e6b01fSNick Piggin int retval = 0; 162831e6b01fSNick Piggin int fput_needed; 162931e6b01fSNick Piggin struct file *file; 163031e6b01fSNick Piggin 163131e6b01fSNick Piggin nd->last_type = LAST_ROOT; /* if there are only slashes... */ 163216c2cd71SAl Viro nd->flags = flags | LOOKUP_JUMPED; 163331e6b01fSNick Piggin nd->depth = 0; 16345b6ca027SAl Viro if (flags & LOOKUP_ROOT) { 16355b6ca027SAl Viro struct inode *inode = nd->root.dentry->d_inode; 163673d049a4SAl Viro if (*name) { 16375b6ca027SAl Viro if (!inode->i_op->lookup) 16385b6ca027SAl Viro return -ENOTDIR; 16395b6ca027SAl Viro retval = inode_permission(inode, MAY_EXEC); 16405b6ca027SAl Viro if (retval) 16415b6ca027SAl Viro return retval; 164273d049a4SAl Viro } 16435b6ca027SAl Viro nd->path = nd->root; 16445b6ca027SAl Viro nd->inode = inode; 16455b6ca027SAl Viro if (flags & LOOKUP_RCU) { 1646962830dfSAndi Kleen br_read_lock(&vfsmount_lock); 16475b6ca027SAl Viro rcu_read_lock(); 16485b6ca027SAl Viro nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 16495b6ca027SAl Viro } else { 16505b6ca027SAl Viro path_get(&nd->path); 16515b6ca027SAl Viro } 16525b6ca027SAl Viro return 0; 16535b6ca027SAl Viro } 16545b6ca027SAl Viro 165531e6b01fSNick Piggin nd->root.mnt = NULL; 165631e6b01fSNick Piggin 165731e6b01fSNick Piggin if (*name=='/') { 1658e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 1659962830dfSAndi Kleen br_read_lock(&vfsmount_lock); 166031e6b01fSNick Piggin rcu_read_lock(); 1661e41f7d4eSAl Viro set_root_rcu(nd); 1662e41f7d4eSAl Viro } else { 1663e41f7d4eSAl Viro set_root(nd); 1664e41f7d4eSAl Viro path_get(&nd->root); 1665e41f7d4eSAl Viro } 166631e6b01fSNick Piggin nd->path = nd->root; 166731e6b01fSNick Piggin } else if (dfd == AT_FDCWD) { 1668e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 166931e6b01fSNick Piggin struct fs_struct *fs = current->fs; 1670c28cc364SNick Piggin unsigned seq; 167131e6b01fSNick Piggin 1672962830dfSAndi Kleen br_read_lock(&vfsmount_lock); 167331e6b01fSNick Piggin rcu_read_lock(); 167431e6b01fSNick Piggin 1675c28cc364SNick Piggin do { 1676c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 167731e6b01fSNick Piggin nd->path = fs->pwd; 1678c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 1679c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 1680e41f7d4eSAl Viro } else { 1681e41f7d4eSAl Viro get_fs_pwd(current->fs, &nd->path); 1682e41f7d4eSAl Viro } 168331e6b01fSNick Piggin } else { 168431e6b01fSNick Piggin struct dentry *dentry; 168531e6b01fSNick Piggin 16861abf0c71SAl Viro file = fget_raw_light(dfd, &fput_needed); 168731e6b01fSNick Piggin retval = -EBADF; 168831e6b01fSNick Piggin if (!file) 168931e6b01fSNick Piggin goto out_fail; 169031e6b01fSNick Piggin 169131e6b01fSNick Piggin dentry = file->f_path.dentry; 169231e6b01fSNick Piggin 1693f52e0c11SAl Viro if (*name) { 169431e6b01fSNick Piggin retval = -ENOTDIR; 169531e6b01fSNick Piggin if (!S_ISDIR(dentry->d_inode->i_mode)) 169631e6b01fSNick Piggin goto fput_fail; 169731e6b01fSNick Piggin 16984ad5abb3SAl Viro retval = inode_permission(dentry->d_inode, MAY_EXEC); 169931e6b01fSNick Piggin if (retval) 170031e6b01fSNick Piggin goto fput_fail; 1701f52e0c11SAl Viro } 170231e6b01fSNick Piggin 170331e6b01fSNick Piggin nd->path = file->f_path; 1704e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 170531e6b01fSNick Piggin if (fput_needed) 170670e9b357SAl Viro *fp = file; 1707c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 1708962830dfSAndi Kleen br_read_lock(&vfsmount_lock); 170931e6b01fSNick Piggin rcu_read_lock(); 17105590ff0dSUlrich Drepper } else { 17115dd784d0SJan Blunck path_get(&file->f_path); 17125590ff0dSUlrich Drepper fput_light(file, fput_needed); 17131da177e4SLinus Torvalds } 1714e41f7d4eSAl Viro } 1715e41f7d4eSAl Viro 171631e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 17179b4a9b14SAl Viro return 0; 17182dfdd266SJosef 'Jeff' Sipek 17199b4a9b14SAl Viro fput_fail: 17209b4a9b14SAl Viro fput_light(file, fput_needed); 17219b4a9b14SAl Viro out_fail: 17229b4a9b14SAl Viro return retval; 17239b4a9b14SAl Viro } 17249b4a9b14SAl Viro 1725bd92d7feSAl Viro static inline int lookup_last(struct nameidata *nd, struct path *path) 1726bd92d7feSAl Viro { 1727bd92d7feSAl Viro if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len]) 1728bd92d7feSAl Viro nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 1729bd92d7feSAl Viro 1730bd92d7feSAl Viro nd->flags &= ~LOOKUP_PARENT; 1731bd92d7feSAl Viro return walk_component(nd, path, &nd->last, nd->last_type, 1732bd92d7feSAl Viro nd->flags & LOOKUP_FOLLOW); 1733bd92d7feSAl Viro } 1734bd92d7feSAl Viro 17359b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */ 1736ee0827cdSAl Viro static int path_lookupat(int dfd, const char *name, 17379b4a9b14SAl Viro unsigned int flags, struct nameidata *nd) 17389b4a9b14SAl Viro { 173970e9b357SAl Viro struct file *base = NULL; 1740bd92d7feSAl Viro struct path path; 1741bd92d7feSAl Viro int err; 174231e6b01fSNick Piggin 174331e6b01fSNick Piggin /* 174431e6b01fSNick Piggin * Path walking is largely split up into 2 different synchronisation 174531e6b01fSNick Piggin * schemes, rcu-walk and ref-walk (explained in 174631e6b01fSNick Piggin * Documentation/filesystems/path-lookup.txt). These share much of the 174731e6b01fSNick Piggin * path walk code, but some things particularly setup, cleanup, and 174831e6b01fSNick Piggin * following mounts are sufficiently divergent that functions are 174931e6b01fSNick Piggin * duplicated. Typically there is a function foo(), and its RCU 175031e6b01fSNick Piggin * analogue, foo_rcu(). 175131e6b01fSNick Piggin * 175231e6b01fSNick Piggin * -ECHILD is the error number of choice (just to avoid clashes) that 175331e6b01fSNick Piggin * is returned if some aspect of an rcu-walk fails. Such an error must 175431e6b01fSNick Piggin * be handled by restarting a traditional ref-walk (which will always 175531e6b01fSNick Piggin * be able to complete). 175631e6b01fSNick Piggin */ 1757bd92d7feSAl Viro err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base); 1758ee0827cdSAl Viro 1759bd92d7feSAl Viro if (unlikely(err)) 1760bd92d7feSAl Viro return err; 1761ee0827cdSAl Viro 1762ee0827cdSAl Viro current->total_link_count = 0; 1763bd92d7feSAl Viro err = link_path_walk(name, nd); 1764bd92d7feSAl Viro 1765bd92d7feSAl Viro if (!err && !(flags & LOOKUP_PARENT)) { 1766bd92d7feSAl Viro err = lookup_last(nd, &path); 1767bd92d7feSAl Viro while (err > 0) { 1768bd92d7feSAl Viro void *cookie; 1769bd92d7feSAl Viro struct path link = path; 1770bd92d7feSAl Viro nd->flags |= LOOKUP_PARENT; 1771574197e0SAl Viro err = follow_link(&link, nd, &cookie); 17726d7b5aaeSAl Viro if (err) 17736d7b5aaeSAl Viro break; 1774bd92d7feSAl Viro err = lookup_last(nd, &path); 1775574197e0SAl Viro put_link(nd, &link, cookie); 1776bd92d7feSAl Viro } 1777bd92d7feSAl Viro } 1778ee0827cdSAl Viro 17799f1fafeeSAl Viro if (!err) 17809f1fafeeSAl Viro err = complete_walk(nd); 1781bd92d7feSAl Viro 1782bd92d7feSAl Viro if (!err && nd->flags & LOOKUP_DIRECTORY) { 1783bd92d7feSAl Viro if (!nd->inode->i_op->lookup) { 1784bd92d7feSAl Viro path_put(&nd->path); 1785bd23a539SAl Viro err = -ENOTDIR; 1786bd92d7feSAl Viro } 1787bd92d7feSAl Viro } 178816c2cd71SAl Viro 178970e9b357SAl Viro if (base) 179070e9b357SAl Viro fput(base); 1791ee0827cdSAl Viro 17925b6ca027SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) { 179331e6b01fSNick Piggin path_put(&nd->root); 179431e6b01fSNick Piggin nd->root.mnt = NULL; 179531e6b01fSNick Piggin } 1796bd92d7feSAl Viro return err; 179731e6b01fSNick Piggin } 179831e6b01fSNick Piggin 1799ee0827cdSAl Viro static int do_path_lookup(int dfd, const char *name, 1800ee0827cdSAl Viro unsigned int flags, struct nameidata *nd) 1801ee0827cdSAl Viro { 1802ee0827cdSAl Viro int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd); 1803ee0827cdSAl Viro if (unlikely(retval == -ECHILD)) 1804ee0827cdSAl Viro retval = path_lookupat(dfd, name, flags, nd); 1805ee0827cdSAl Viro if (unlikely(retval == -ESTALE)) 1806ee0827cdSAl Viro retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd); 1807ee0827cdSAl Viro 180831e6b01fSNick Piggin if (likely(!retval)) { 180931e6b01fSNick Piggin if (unlikely(!audit_dummy_context())) { 181031e6b01fSNick Piggin if (nd->path.dentry && nd->inode) 181131e6b01fSNick Piggin audit_inode(name, nd->path.dentry); 181231e6b01fSNick Piggin } 181331e6b01fSNick Piggin } 1814170aa3d0SUlrich Drepper return retval; 18151da177e4SLinus Torvalds } 18161da177e4SLinus Torvalds 181779714f72SAl Viro /* does lookup, returns the object with parent locked */ 181879714f72SAl Viro struct dentry *kern_path_locked(const char *name, struct path *path) 18195590ff0dSUlrich Drepper { 182079714f72SAl Viro struct nameidata nd; 182179714f72SAl Viro struct dentry *d; 182279714f72SAl Viro int err = do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, &nd); 182379714f72SAl Viro if (err) 182479714f72SAl Viro return ERR_PTR(err); 182579714f72SAl Viro if (nd.last_type != LAST_NORM) { 182679714f72SAl Viro path_put(&nd.path); 182779714f72SAl Viro return ERR_PTR(-EINVAL); 182879714f72SAl Viro } 182979714f72SAl Viro mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 183079714f72SAl Viro d = lookup_one_len(nd.last.name, nd.path.dentry, nd.last.len); 183179714f72SAl Viro if (IS_ERR(d)) { 183279714f72SAl Viro mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 183379714f72SAl Viro path_put(&nd.path); 183479714f72SAl Viro return d; 183579714f72SAl Viro } 183679714f72SAl Viro *path = nd.path; 183779714f72SAl Viro return d; 18385590ff0dSUlrich Drepper } 18395590ff0dSUlrich Drepper 1840d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path) 1841d1811465SAl Viro { 1842d1811465SAl Viro struct nameidata nd; 1843d1811465SAl Viro int res = do_path_lookup(AT_FDCWD, name, flags, &nd); 1844d1811465SAl Viro if (!res) 1845d1811465SAl Viro *path = nd.path; 1846d1811465SAl Viro return res; 1847d1811465SAl Viro } 1848d1811465SAl Viro 184916f18200SJosef 'Jeff' Sipek /** 185016f18200SJosef 'Jeff' Sipek * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair 185116f18200SJosef 'Jeff' Sipek * @dentry: pointer to dentry of the base directory 185216f18200SJosef 'Jeff' Sipek * @mnt: pointer to vfs mount of the base directory 185316f18200SJosef 'Jeff' Sipek * @name: pointer to file name 185416f18200SJosef 'Jeff' Sipek * @flags: lookup flags 1855e0a01249SAl Viro * @path: pointer to struct path to fill 185616f18200SJosef 'Jeff' Sipek */ 185716f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt, 185816f18200SJosef 'Jeff' Sipek const char *name, unsigned int flags, 1859e0a01249SAl Viro struct path *path) 186016f18200SJosef 'Jeff' Sipek { 1861e0a01249SAl Viro struct nameidata nd; 1862e0a01249SAl Viro int err; 1863e0a01249SAl Viro nd.root.dentry = dentry; 1864e0a01249SAl Viro nd.root.mnt = mnt; 1865e0a01249SAl Viro BUG_ON(flags & LOOKUP_PARENT); 18665b6ca027SAl Viro /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */ 1867e0a01249SAl Viro err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd); 1868e0a01249SAl Viro if (!err) 1869e0a01249SAl Viro *path = nd.path; 1870e0a01249SAl Viro return err; 187116f18200SJosef 'Jeff' Sipek } 187216f18200SJosef 'Jeff' Sipek 1873057f6c01SJames Morris /* 1874057f6c01SJames Morris * Restricted form of lookup. Doesn't follow links, single-component only, 1875057f6c01SJames Morris * needs parent already locked. Doesn't follow mounts. 1876057f6c01SJames Morris * SMP-safe. 1877057f6c01SJames Morris */ 1878a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd) 18791da177e4SLinus Torvalds { 188072bd866aSAl Viro return __lookup_hash(&nd->last, nd->path.dentry, nd->flags); 18811da177e4SLinus Torvalds } 18821da177e4SLinus Torvalds 1883eead1911SChristoph Hellwig /** 1884a6b91919SRandy Dunlap * lookup_one_len - filesystem helper to lookup single pathname component 1885eead1911SChristoph Hellwig * @name: pathname component to lookup 1886eead1911SChristoph Hellwig * @base: base directory to lookup from 1887eead1911SChristoph Hellwig * @len: maximum length @len should be interpreted to 1888eead1911SChristoph Hellwig * 1889a6b91919SRandy Dunlap * Note that this routine is purely a helper for filesystem usage and should 1890a6b91919SRandy Dunlap * not be called by generic code. Also note that by using this function the 1891eead1911SChristoph Hellwig * nameidata argument is passed to the filesystem methods and a filesystem 1892eead1911SChristoph Hellwig * using this helper needs to be prepared for that. 1893eead1911SChristoph Hellwig */ 1894057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len) 1895057f6c01SJames Morris { 1896057f6c01SJames Morris struct qstr this; 18976a96ba54SAl Viro unsigned int c; 1898cda309deSMiklos Szeredi int err; 1899057f6c01SJames Morris 19002f9092e1SDavid Woodhouse WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex)); 19012f9092e1SDavid Woodhouse 19026a96ba54SAl Viro this.name = name; 19036a96ba54SAl Viro this.len = len; 19040145acc2SLinus Torvalds this.hash = full_name_hash(name, len); 19056a96ba54SAl Viro if (!len) 19066a96ba54SAl Viro return ERR_PTR(-EACCES); 19076a96ba54SAl Viro 19086a96ba54SAl Viro while (len--) { 19096a96ba54SAl Viro c = *(const unsigned char *)name++; 19106a96ba54SAl Viro if (c == '/' || c == '\0') 19116a96ba54SAl Viro return ERR_PTR(-EACCES); 19126a96ba54SAl Viro } 19135a202bcdSAl Viro /* 19145a202bcdSAl Viro * See if the low-level filesystem might want 19155a202bcdSAl Viro * to use its own hash.. 19165a202bcdSAl Viro */ 19175a202bcdSAl Viro if (base->d_flags & DCACHE_OP_HASH) { 19185a202bcdSAl Viro int err = base->d_op->d_hash(base, base->d_inode, &this); 19195a202bcdSAl Viro if (err < 0) 19205a202bcdSAl Viro return ERR_PTR(err); 19215a202bcdSAl Viro } 1922eead1911SChristoph Hellwig 1923cda309deSMiklos Szeredi err = inode_permission(base->d_inode, MAY_EXEC); 1924cda309deSMiklos Szeredi if (err) 1925cda309deSMiklos Szeredi return ERR_PTR(err); 1926cda309deSMiklos Szeredi 192772bd866aSAl Viro return __lookup_hash(&this, base, 0); 1928057f6c01SJames Morris } 1929057f6c01SJames Morris 19301fa1e7f6SAndy Whitcroft int user_path_at_empty(int dfd, const char __user *name, unsigned flags, 19311fa1e7f6SAndy Whitcroft struct path *path, int *empty) 19321da177e4SLinus Torvalds { 19332d8f3038SAl Viro struct nameidata nd; 19341fa1e7f6SAndy Whitcroft char *tmp = getname_flags(name, flags, empty); 19351da177e4SLinus Torvalds int err = PTR_ERR(tmp); 19361da177e4SLinus Torvalds if (!IS_ERR(tmp)) { 19372d8f3038SAl Viro 19382d8f3038SAl Viro BUG_ON(flags & LOOKUP_PARENT); 19392d8f3038SAl Viro 19402d8f3038SAl Viro err = do_path_lookup(dfd, tmp, flags, &nd); 19411da177e4SLinus Torvalds putname(tmp); 19422d8f3038SAl Viro if (!err) 19432d8f3038SAl Viro *path = nd.path; 19441da177e4SLinus Torvalds } 19451da177e4SLinus Torvalds return err; 19461da177e4SLinus Torvalds } 19471da177e4SLinus Torvalds 19481fa1e7f6SAndy Whitcroft int user_path_at(int dfd, const char __user *name, unsigned flags, 19491fa1e7f6SAndy Whitcroft struct path *path) 19501fa1e7f6SAndy Whitcroft { 1951f7493e5dSLinus Torvalds return user_path_at_empty(dfd, name, flags, path, NULL); 19521fa1e7f6SAndy Whitcroft } 19531fa1e7f6SAndy Whitcroft 19542ad94ae6SAl Viro static int user_path_parent(int dfd, const char __user *path, 19552ad94ae6SAl Viro struct nameidata *nd, char **name) 19562ad94ae6SAl Viro { 19572ad94ae6SAl Viro char *s = getname(path); 19582ad94ae6SAl Viro int error; 19592ad94ae6SAl Viro 19602ad94ae6SAl Viro if (IS_ERR(s)) 19612ad94ae6SAl Viro return PTR_ERR(s); 19622ad94ae6SAl Viro 19632ad94ae6SAl Viro error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd); 19642ad94ae6SAl Viro if (error) 19652ad94ae6SAl Viro putname(s); 19662ad94ae6SAl Viro else 19672ad94ae6SAl Viro *name = s; 19682ad94ae6SAl Viro 19692ad94ae6SAl Viro return error; 19702ad94ae6SAl Viro } 19712ad94ae6SAl Viro 19721da177e4SLinus Torvalds /* 19731da177e4SLinus Torvalds * It's inline, so penalty for filesystems that don't use sticky bit is 19741da177e4SLinus Torvalds * minimal. 19751da177e4SLinus Torvalds */ 19761da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode) 19771da177e4SLinus Torvalds { 19788e96e3b7SEric W. Biederman kuid_t fsuid = current_fsuid(); 1979da9592edSDavid Howells 19801da177e4SLinus Torvalds if (!(dir->i_mode & S_ISVTX)) 19811da177e4SLinus Torvalds return 0; 19828e96e3b7SEric W. Biederman if (uid_eq(inode->i_uid, fsuid)) 19831da177e4SLinus Torvalds return 0; 19848e96e3b7SEric W. Biederman if (uid_eq(dir->i_uid, fsuid)) 19851da177e4SLinus Torvalds return 0; 19861a48e2acSEric W. Biederman return !inode_capable(inode, CAP_FOWNER); 19871da177e4SLinus Torvalds } 19881da177e4SLinus Torvalds 19891da177e4SLinus Torvalds /* 19901da177e4SLinus Torvalds * Check whether we can remove a link victim from directory dir, check 19911da177e4SLinus Torvalds * whether the type of victim is right. 19921da177e4SLinus Torvalds * 1. We can't do it if dir is read-only (done in permission()) 19931da177e4SLinus Torvalds * 2. We should have write and exec permissions on dir 19941da177e4SLinus Torvalds * 3. We can't remove anything from append-only dir 19951da177e4SLinus Torvalds * 4. We can't do anything with immutable dir (done in permission()) 19961da177e4SLinus Torvalds * 5. If the sticky bit on dir is set we should either 19971da177e4SLinus Torvalds * a. be owner of dir, or 19981da177e4SLinus Torvalds * b. be owner of victim, or 19991da177e4SLinus Torvalds * c. have CAP_FOWNER capability 20001da177e4SLinus Torvalds * 6. If the victim is append-only or immutable we can't do antyhing with 20011da177e4SLinus Torvalds * links pointing to it. 20021da177e4SLinus Torvalds * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. 20031da177e4SLinus Torvalds * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. 20041da177e4SLinus Torvalds * 9. We can't remove a root or mountpoint. 20051da177e4SLinus Torvalds * 10. We don't allow removal of NFS sillyrenamed files; it's handled by 20061da177e4SLinus Torvalds * nfs_async_unlink(). 20071da177e4SLinus Torvalds */ 2008858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir) 20091da177e4SLinus Torvalds { 20101da177e4SLinus Torvalds int error; 20111da177e4SLinus Torvalds 20121da177e4SLinus Torvalds if (!victim->d_inode) 20131da177e4SLinus Torvalds return -ENOENT; 20141da177e4SLinus Torvalds 20151da177e4SLinus Torvalds BUG_ON(victim->d_parent->d_inode != dir); 2016cccc6bbaSAl Viro audit_inode_child(victim, dir); 20171da177e4SLinus Torvalds 2018f419a2e3SAl Viro error = inode_permission(dir, MAY_WRITE | MAY_EXEC); 20191da177e4SLinus Torvalds if (error) 20201da177e4SLinus Torvalds return error; 20211da177e4SLinus Torvalds if (IS_APPEND(dir)) 20221da177e4SLinus Torvalds return -EPERM; 20231da177e4SLinus Torvalds if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)|| 2024f9454548SHugh Dickins IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode)) 20251da177e4SLinus Torvalds return -EPERM; 20261da177e4SLinus Torvalds if (isdir) { 20271da177e4SLinus Torvalds if (!S_ISDIR(victim->d_inode->i_mode)) 20281da177e4SLinus Torvalds return -ENOTDIR; 20291da177e4SLinus Torvalds if (IS_ROOT(victim)) 20301da177e4SLinus Torvalds return -EBUSY; 20311da177e4SLinus Torvalds } else if (S_ISDIR(victim->d_inode->i_mode)) 20321da177e4SLinus Torvalds return -EISDIR; 20331da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 20341da177e4SLinus Torvalds return -ENOENT; 20351da177e4SLinus Torvalds if (victim->d_flags & DCACHE_NFSFS_RENAMED) 20361da177e4SLinus Torvalds return -EBUSY; 20371da177e4SLinus Torvalds return 0; 20381da177e4SLinus Torvalds } 20391da177e4SLinus Torvalds 20401da177e4SLinus Torvalds /* Check whether we can create an object with dentry child in directory 20411da177e4SLinus Torvalds * dir. 20421da177e4SLinus Torvalds * 1. We can't do it if child already exists (open has special treatment for 20431da177e4SLinus Torvalds * this case, but since we are inlined it's OK) 20441da177e4SLinus Torvalds * 2. We can't do it if dir is read-only (done in permission()) 20451da177e4SLinus Torvalds * 3. We should have write and exec permissions on dir 20461da177e4SLinus Torvalds * 4. We can't do it if dir is immutable (done in permission()) 20471da177e4SLinus Torvalds */ 2048a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child) 20491da177e4SLinus Torvalds { 20501da177e4SLinus Torvalds if (child->d_inode) 20511da177e4SLinus Torvalds return -EEXIST; 20521da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 20531da177e4SLinus Torvalds return -ENOENT; 2054f419a2e3SAl Viro return inode_permission(dir, MAY_WRITE | MAY_EXEC); 20551da177e4SLinus Torvalds } 20561da177e4SLinus Torvalds 20571da177e4SLinus Torvalds /* 20581da177e4SLinus Torvalds * p1 and p2 should be directories on the same fs. 20591da177e4SLinus Torvalds */ 20601da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2) 20611da177e4SLinus Torvalds { 20621da177e4SLinus Torvalds struct dentry *p; 20631da177e4SLinus Torvalds 20641da177e4SLinus Torvalds if (p1 == p2) { 2065f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 20661da177e4SLinus Torvalds return NULL; 20671da177e4SLinus Torvalds } 20681da177e4SLinus Torvalds 2069a11f3a05SArjan van de Ven mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 20701da177e4SLinus Torvalds 2071e2761a11SOGAWA Hirofumi p = d_ancestor(p2, p1); 2072e2761a11SOGAWA Hirofumi if (p) { 2073f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT); 2074f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD); 20751da177e4SLinus Torvalds return p; 20761da177e4SLinus Torvalds } 20771da177e4SLinus Torvalds 2078e2761a11SOGAWA Hirofumi p = d_ancestor(p1, p2); 2079e2761a11SOGAWA Hirofumi if (p) { 2080f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 2081f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 20821da177e4SLinus Torvalds return p; 20831da177e4SLinus Torvalds } 20841da177e4SLinus Torvalds 2085f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 2086f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 20871da177e4SLinus Torvalds return NULL; 20881da177e4SLinus Torvalds } 20891da177e4SLinus Torvalds 20901da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2) 20911da177e4SLinus Torvalds { 20921b1dcc1bSJes Sorensen mutex_unlock(&p1->d_inode->i_mutex); 20931da177e4SLinus Torvalds if (p1 != p2) { 20941b1dcc1bSJes Sorensen mutex_unlock(&p2->d_inode->i_mutex); 2095a11f3a05SArjan van de Ven mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 20961da177e4SLinus Torvalds } 20971da177e4SLinus Torvalds } 20981da177e4SLinus Torvalds 20994acdaf27SAl Viro int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode, 2100312b63fbSAl Viro bool want_excl) 21011da177e4SLinus Torvalds { 2102a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 21031da177e4SLinus Torvalds if (error) 21041da177e4SLinus Torvalds return error; 21051da177e4SLinus Torvalds 2106acfa4380SAl Viro if (!dir->i_op->create) 21071da177e4SLinus Torvalds return -EACCES; /* shouldn't it be ENOSYS? */ 21081da177e4SLinus Torvalds mode &= S_IALLUGO; 21091da177e4SLinus Torvalds mode |= S_IFREG; 21101da177e4SLinus Torvalds error = security_inode_create(dir, dentry, mode); 21111da177e4SLinus Torvalds if (error) 21121da177e4SLinus Torvalds return error; 2113312b63fbSAl Viro error = dir->i_op->create(dir, dentry, mode, want_excl); 2114a74574aaSStephen Smalley if (!error) 2115f38aa942SAmy Griffis fsnotify_create(dir, dentry); 21161da177e4SLinus Torvalds return error; 21171da177e4SLinus Torvalds } 21181da177e4SLinus Torvalds 211973d049a4SAl Viro static int may_open(struct path *path, int acc_mode, int flag) 21201da177e4SLinus Torvalds { 21213fb64190SChristoph Hellwig struct dentry *dentry = path->dentry; 21221da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 21231da177e4SLinus Torvalds int error; 21241da177e4SLinus Torvalds 2125bcda7652SAl Viro /* O_PATH? */ 2126bcda7652SAl Viro if (!acc_mode) 2127bcda7652SAl Viro return 0; 2128bcda7652SAl Viro 21291da177e4SLinus Torvalds if (!inode) 21301da177e4SLinus Torvalds return -ENOENT; 21311da177e4SLinus Torvalds 2132c8fe8f30SChristoph Hellwig switch (inode->i_mode & S_IFMT) { 2133c8fe8f30SChristoph Hellwig case S_IFLNK: 21341da177e4SLinus Torvalds return -ELOOP; 2135c8fe8f30SChristoph Hellwig case S_IFDIR: 2136c8fe8f30SChristoph Hellwig if (acc_mode & MAY_WRITE) 21371da177e4SLinus Torvalds return -EISDIR; 2138c8fe8f30SChristoph Hellwig break; 2139c8fe8f30SChristoph Hellwig case S_IFBLK: 2140c8fe8f30SChristoph Hellwig case S_IFCHR: 21413fb64190SChristoph Hellwig if (path->mnt->mnt_flags & MNT_NODEV) 21421da177e4SLinus Torvalds return -EACCES; 2143c8fe8f30SChristoph Hellwig /*FALLTHRU*/ 2144c8fe8f30SChristoph Hellwig case S_IFIFO: 2145c8fe8f30SChristoph Hellwig case S_IFSOCK: 21461da177e4SLinus Torvalds flag &= ~O_TRUNC; 2147c8fe8f30SChristoph Hellwig break; 21484a3fd211SDave Hansen } 2149b41572e9SDave Hansen 21503fb64190SChristoph Hellwig error = inode_permission(inode, acc_mode); 2151b41572e9SDave Hansen if (error) 2152b41572e9SDave Hansen return error; 21536146f0d5SMimi Zohar 21541da177e4SLinus Torvalds /* 21551da177e4SLinus Torvalds * An append-only file must be opened in append mode for writing. 21561da177e4SLinus Torvalds */ 21571da177e4SLinus Torvalds if (IS_APPEND(inode)) { 21588737c930SAl Viro if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND)) 21597715b521SAl Viro return -EPERM; 21601da177e4SLinus Torvalds if (flag & O_TRUNC) 21617715b521SAl Viro return -EPERM; 21621da177e4SLinus Torvalds } 21631da177e4SLinus Torvalds 21641da177e4SLinus Torvalds /* O_NOATIME can only be set by the owner or superuser */ 21652e149670SSerge E. Hallyn if (flag & O_NOATIME && !inode_owner_or_capable(inode)) 21667715b521SAl Viro return -EPERM; 21671da177e4SLinus Torvalds 2168f3c7691eSJ. Bruce Fields return 0; 21697715b521SAl Viro } 21707715b521SAl Viro 2171e1181ee6SJeff Layton static int handle_truncate(struct file *filp) 21727715b521SAl Viro { 2173e1181ee6SJeff Layton struct path *path = &filp->f_path; 21747715b521SAl Viro struct inode *inode = path->dentry->d_inode; 21757715b521SAl Viro int error = get_write_access(inode); 21761da177e4SLinus Torvalds if (error) 21777715b521SAl Viro return error; 21781da177e4SLinus Torvalds /* 21791da177e4SLinus Torvalds * Refuse to truncate files with mandatory locks held on them. 21801da177e4SLinus Torvalds */ 21811da177e4SLinus Torvalds error = locks_verify_locked(inode); 2182be6d3e56SKentaro Takeda if (!error) 2183ea0d3ab2STetsuo Handa error = security_path_truncate(path); 21841da177e4SLinus Torvalds if (!error) { 21857715b521SAl Viro error = do_truncate(path->dentry, 0, 2186d139d7ffSMiklos Szeredi ATTR_MTIME|ATTR_CTIME|ATTR_OPEN, 2187e1181ee6SJeff Layton filp); 21881da177e4SLinus Torvalds } 21891da177e4SLinus Torvalds put_write_access(inode); 2190acd0c935SMimi Zohar return error; 21911da177e4SLinus Torvalds } 21921da177e4SLinus Torvalds 2193d57999e1SDave Hansen static inline int open_to_namei_flags(int flag) 2194d57999e1SDave Hansen { 21958a5e929dSAl Viro if ((flag & O_ACCMODE) == 3) 21968a5e929dSAl Viro flag--; 2197d57999e1SDave Hansen return flag; 2198d57999e1SDave Hansen } 2199d57999e1SDave Hansen 2200d18e9008SMiklos Szeredi static int may_o_create(struct path *dir, struct dentry *dentry, umode_t mode) 2201d18e9008SMiklos Szeredi { 2202d18e9008SMiklos Szeredi int error = security_path_mknod(dir, dentry, mode, 0); 2203d18e9008SMiklos Szeredi if (error) 2204d18e9008SMiklos Szeredi return error; 2205d18e9008SMiklos Szeredi 2206d18e9008SMiklos Szeredi error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC); 2207d18e9008SMiklos Szeredi if (error) 2208d18e9008SMiklos Szeredi return error; 2209d18e9008SMiklos Szeredi 2210d18e9008SMiklos Szeredi return security_inode_create(dir->dentry->d_inode, dentry, mode); 2211d18e9008SMiklos Szeredi } 2212d18e9008SMiklos Szeredi 22131acf0af9SDavid Howells /* 22141acf0af9SDavid Howells * Attempt to atomically look up, create and open a file from a negative 22151acf0af9SDavid Howells * dentry. 22161acf0af9SDavid Howells * 22171acf0af9SDavid Howells * Returns 0 if successful. The file will have been created and attached to 22181acf0af9SDavid Howells * @file by the filesystem calling finish_open(). 22191acf0af9SDavid Howells * 22201acf0af9SDavid Howells * Returns 1 if the file was looked up only or didn't need creating. The 22211acf0af9SDavid Howells * caller will need to perform the open themselves. @path will have been 22221acf0af9SDavid Howells * updated to point to the new dentry. This may be negative. 22231acf0af9SDavid Howells * 22241acf0af9SDavid Howells * Returns an error code otherwise. 22251acf0af9SDavid Howells */ 22262675a4ebSAl Viro static int atomic_open(struct nameidata *nd, struct dentry *dentry, 222730d90494SAl Viro struct path *path, struct file *file, 2228015c3bbcSMiklos Szeredi const struct open_flags *op, 222977d660a8SMiklos Szeredi bool *want_write, bool need_lookup, 223047237687SAl Viro int *opened) 2231d18e9008SMiklos Szeredi { 2232d18e9008SMiklos Szeredi struct inode *dir = nd->path.dentry->d_inode; 2233d18e9008SMiklos Szeredi unsigned open_flag = open_to_namei_flags(op->open_flag); 2234d18e9008SMiklos Szeredi umode_t mode; 2235d18e9008SMiklos Szeredi int error; 2236d18e9008SMiklos Szeredi int acc_mode; 2237d18e9008SMiklos Szeredi int create_error = 0; 2238d18e9008SMiklos Szeredi struct dentry *const DENTRY_NOT_SET = (void *) -1UL; 2239d18e9008SMiklos Szeredi 2240d18e9008SMiklos Szeredi BUG_ON(dentry->d_inode); 2241d18e9008SMiklos Szeredi 2242d18e9008SMiklos Szeredi /* Don't create child dentry for a dead directory. */ 2243d18e9008SMiklos Szeredi if (unlikely(IS_DEADDIR(dir))) { 22442675a4ebSAl Viro error = -ENOENT; 2245d18e9008SMiklos Szeredi goto out; 2246d18e9008SMiklos Szeredi } 2247d18e9008SMiklos Szeredi 2248d18e9008SMiklos Szeredi mode = op->mode & S_IALLUGO; 2249d18e9008SMiklos Szeredi if ((open_flag & O_CREAT) && !IS_POSIXACL(dir)) 2250d18e9008SMiklos Szeredi mode &= ~current_umask(); 2251d18e9008SMiklos Szeredi 2252d18e9008SMiklos Szeredi if (open_flag & O_EXCL) { 2253d18e9008SMiklos Szeredi open_flag &= ~O_TRUNC; 225447237687SAl Viro *opened |= FILE_CREATED; 2255d18e9008SMiklos Szeredi } 2256d18e9008SMiklos Szeredi 2257d18e9008SMiklos Szeredi /* 2258d18e9008SMiklos Szeredi * Checking write permission is tricky, bacuse we don't know if we are 2259d18e9008SMiklos Szeredi * going to actually need it: O_CREAT opens should work as long as the 2260d18e9008SMiklos Szeredi * file exists. But checking existence breaks atomicity. The trick is 2261d18e9008SMiklos Szeredi * to check access and if not granted clear O_CREAT from the flags. 2262d18e9008SMiklos Szeredi * 2263d18e9008SMiklos Szeredi * Another problem is returing the "right" error value (e.g. for an 2264d18e9008SMiklos Szeredi * O_EXCL open we want to return EEXIST not EROFS). 2265d18e9008SMiklos Szeredi */ 2266d18e9008SMiklos Szeredi if ((open_flag & (O_CREAT | O_TRUNC)) || 2267d18e9008SMiklos Szeredi (open_flag & O_ACCMODE) != O_RDONLY) { 2268d18e9008SMiklos Szeredi error = mnt_want_write(nd->path.mnt); 2269d18e9008SMiklos Szeredi if (!error) { 227077d660a8SMiklos Szeredi *want_write = true; 2271d18e9008SMiklos Szeredi } else if (!(open_flag & O_CREAT)) { 2272d18e9008SMiklos Szeredi /* 2273d18e9008SMiklos Szeredi * No O_CREATE -> atomicity not a requirement -> fall 2274d18e9008SMiklos Szeredi * back to lookup + open 2275d18e9008SMiklos Szeredi */ 2276d18e9008SMiklos Szeredi goto no_open; 2277d18e9008SMiklos Szeredi } else if (open_flag & (O_EXCL | O_TRUNC)) { 2278d18e9008SMiklos Szeredi /* Fall back and fail with the right error */ 2279d18e9008SMiklos Szeredi create_error = error; 2280d18e9008SMiklos Szeredi goto no_open; 2281d18e9008SMiklos Szeredi } else { 2282d18e9008SMiklos Szeredi /* No side effects, safe to clear O_CREAT */ 2283d18e9008SMiklos Szeredi create_error = error; 2284d18e9008SMiklos Szeredi open_flag &= ~O_CREAT; 2285d18e9008SMiklos Szeredi } 2286d18e9008SMiklos Szeredi } 2287d18e9008SMiklos Szeredi 2288d18e9008SMiklos Szeredi if (open_flag & O_CREAT) { 2289d18e9008SMiklos Szeredi error = may_o_create(&nd->path, dentry, op->mode); 2290d18e9008SMiklos Szeredi if (error) { 2291d18e9008SMiklos Szeredi create_error = error; 2292d18e9008SMiklos Szeredi if (open_flag & O_EXCL) 2293d18e9008SMiklos Szeredi goto no_open; 2294d18e9008SMiklos Szeredi open_flag &= ~O_CREAT; 2295d18e9008SMiklos Szeredi } 2296d18e9008SMiklos Szeredi } 2297d18e9008SMiklos Szeredi 2298d18e9008SMiklos Szeredi if (nd->flags & LOOKUP_DIRECTORY) 2299d18e9008SMiklos Szeredi open_flag |= O_DIRECTORY; 2300d18e9008SMiklos Szeredi 230130d90494SAl Viro file->f_path.dentry = DENTRY_NOT_SET; 230230d90494SAl Viro file->f_path.mnt = nd->path.mnt; 230330d90494SAl Viro error = dir->i_op->atomic_open(dir, dentry, file, open_flag, mode, 230447237687SAl Viro opened); 2305d9585277SAl Viro if (error < 0) { 2306d9585277SAl Viro if (create_error && error == -ENOENT) 2307d9585277SAl Viro error = create_error; 2308d18e9008SMiklos Szeredi goto out; 2309d18e9008SMiklos Szeredi } 2310d18e9008SMiklos Szeredi 2311d18e9008SMiklos Szeredi acc_mode = op->acc_mode; 231247237687SAl Viro if (*opened & FILE_CREATED) { 2313d18e9008SMiklos Szeredi fsnotify_create(dir, dentry); 2314d18e9008SMiklos Szeredi acc_mode = MAY_OPEN; 2315d18e9008SMiklos Szeredi } 2316d18e9008SMiklos Szeredi 2317d9585277SAl Viro if (error) { /* returned 1, that is */ 231830d90494SAl Viro if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) { 23192675a4ebSAl Viro error = -EIO; 2320d18e9008SMiklos Szeredi goto out; 2321d18e9008SMiklos Szeredi } 232230d90494SAl Viro if (file->f_path.dentry) { 2323d18e9008SMiklos Szeredi dput(dentry); 232430d90494SAl Viro dentry = file->f_path.dentry; 2325d18e9008SMiklos Szeredi } 2326d18e9008SMiklos Szeredi goto looked_up; 2327d18e9008SMiklos Szeredi } 2328d18e9008SMiklos Szeredi 2329d18e9008SMiklos Szeredi /* 2330d18e9008SMiklos Szeredi * We didn't have the inode before the open, so check open permission 2331d18e9008SMiklos Szeredi * here. 2332d18e9008SMiklos Szeredi */ 23332675a4ebSAl Viro error = may_open(&file->f_path, acc_mode, open_flag); 23342675a4ebSAl Viro if (error) 23352675a4ebSAl Viro fput(file); 2336d18e9008SMiklos Szeredi 2337d18e9008SMiklos Szeredi out: 2338d18e9008SMiklos Szeredi dput(dentry); 23392675a4ebSAl Viro return error; 2340d18e9008SMiklos Szeredi 2341d18e9008SMiklos Szeredi no_open: 2342d18e9008SMiklos Szeredi if (need_lookup) { 234372bd866aSAl Viro dentry = lookup_real(dir, dentry, nd->flags); 2344d18e9008SMiklos Szeredi if (IS_ERR(dentry)) 23452675a4ebSAl Viro return PTR_ERR(dentry); 2346d18e9008SMiklos Szeredi 2347d18e9008SMiklos Szeredi if (create_error) { 2348d18e9008SMiklos Szeredi int open_flag = op->open_flag; 2349d18e9008SMiklos Szeredi 23502675a4ebSAl Viro error = create_error; 2351d18e9008SMiklos Szeredi if ((open_flag & O_EXCL)) { 2352d18e9008SMiklos Szeredi if (!dentry->d_inode) 2353d18e9008SMiklos Szeredi goto out; 2354d18e9008SMiklos Szeredi } else if (!dentry->d_inode) { 2355d18e9008SMiklos Szeredi goto out; 2356d18e9008SMiklos Szeredi } else if ((open_flag & O_TRUNC) && 2357d18e9008SMiklos Szeredi S_ISREG(dentry->d_inode->i_mode)) { 2358d18e9008SMiklos Szeredi goto out; 2359d18e9008SMiklos Szeredi } 2360d18e9008SMiklos Szeredi /* will fail later, go on to get the right error */ 2361d18e9008SMiklos Szeredi } 2362d18e9008SMiklos Szeredi } 2363d18e9008SMiklos Szeredi looked_up: 2364d18e9008SMiklos Szeredi path->dentry = dentry; 2365d18e9008SMiklos Szeredi path->mnt = nd->path.mnt; 23662675a4ebSAl Viro return 1; 2367d18e9008SMiklos Szeredi } 2368d18e9008SMiklos Szeredi 236931e6b01fSNick Piggin /* 23701acf0af9SDavid Howells * Look up and maybe create and open the last component. 2371d58ffd35SMiklos Szeredi * 2372d58ffd35SMiklos Szeredi * Must be called with i_mutex held on parent. 2373d58ffd35SMiklos Szeredi * 23741acf0af9SDavid Howells * Returns 0 if the file was successfully atomically created (if necessary) and 23751acf0af9SDavid Howells * opened. In this case the file will be returned attached to @file. 23761acf0af9SDavid Howells * 23771acf0af9SDavid Howells * Returns 1 if the file was not completely opened at this time, though lookups 23781acf0af9SDavid Howells * and creations will have been performed and the dentry returned in @path will 23791acf0af9SDavid Howells * be positive upon return if O_CREAT was specified. If O_CREAT wasn't 23801acf0af9SDavid Howells * specified then a negative dentry may be returned. 23811acf0af9SDavid Howells * 23821acf0af9SDavid Howells * An error code is returned otherwise. 23831acf0af9SDavid Howells * 23841acf0af9SDavid Howells * FILE_CREATE will be set in @*opened if the dentry was created and will be 23851acf0af9SDavid Howells * cleared otherwise prior to returning. 2386d58ffd35SMiklos Szeredi */ 23872675a4ebSAl Viro static int lookup_open(struct nameidata *nd, struct path *path, 238830d90494SAl Viro struct file *file, 2389d58ffd35SMiklos Szeredi const struct open_flags *op, 239047237687SAl Viro bool *want_write, int *opened) 2391d58ffd35SMiklos Szeredi { 2392d58ffd35SMiklos Szeredi struct dentry *dir = nd->path.dentry; 239354ef4872SMiklos Szeredi struct inode *dir_inode = dir->d_inode; 2394d58ffd35SMiklos Szeredi struct dentry *dentry; 2395d58ffd35SMiklos Szeredi int error; 239654ef4872SMiklos Szeredi bool need_lookup; 2397d58ffd35SMiklos Szeredi 239847237687SAl Viro *opened &= ~FILE_CREATED; 2399201f956eSAl Viro dentry = lookup_dcache(&nd->last, dir, nd->flags, &need_lookup); 2400d58ffd35SMiklos Szeredi if (IS_ERR(dentry)) 24012675a4ebSAl Viro return PTR_ERR(dentry); 2402d58ffd35SMiklos Szeredi 2403d18e9008SMiklos Szeredi /* Cached positive dentry: will open in f_op->open */ 2404d18e9008SMiklos Szeredi if (!need_lookup && dentry->d_inode) 2405d18e9008SMiklos Szeredi goto out_no_open; 2406d18e9008SMiklos Szeredi 2407d18e9008SMiklos Szeredi if ((nd->flags & LOOKUP_OPEN) && dir_inode->i_op->atomic_open) { 240830d90494SAl Viro return atomic_open(nd, dentry, path, file, op, want_write, 240947237687SAl Viro need_lookup, opened); 2410d18e9008SMiklos Szeredi } 2411d18e9008SMiklos Szeredi 241254ef4872SMiklos Szeredi if (need_lookup) { 241354ef4872SMiklos Szeredi BUG_ON(dentry->d_inode); 241454ef4872SMiklos Szeredi 241572bd866aSAl Viro dentry = lookup_real(dir_inode, dentry, nd->flags); 241654ef4872SMiklos Szeredi if (IS_ERR(dentry)) 24172675a4ebSAl Viro return PTR_ERR(dentry); 241854ef4872SMiklos Szeredi } 241954ef4872SMiklos Szeredi 2420d58ffd35SMiklos Szeredi /* Negative dentry, just create the file */ 2421d58ffd35SMiklos Szeredi if (!dentry->d_inode && (op->open_flag & O_CREAT)) { 2422d58ffd35SMiklos Szeredi umode_t mode = op->mode; 2423d58ffd35SMiklos Szeredi if (!IS_POSIXACL(dir->d_inode)) 2424d58ffd35SMiklos Szeredi mode &= ~current_umask(); 2425d58ffd35SMiklos Szeredi /* 2426d58ffd35SMiklos Szeredi * This write is needed to ensure that a 2427d58ffd35SMiklos Szeredi * rw->ro transition does not occur between 2428d58ffd35SMiklos Szeredi * the time when the file is created and when 2429d58ffd35SMiklos Szeredi * a permanent write count is taken through 2430015c3bbcSMiklos Szeredi * the 'struct file' in finish_open(). 2431d58ffd35SMiklos Szeredi */ 2432d58ffd35SMiklos Szeredi error = mnt_want_write(nd->path.mnt); 2433d58ffd35SMiklos Szeredi if (error) 2434d58ffd35SMiklos Szeredi goto out_dput; 243577d660a8SMiklos Szeredi *want_write = true; 243647237687SAl Viro *opened |= FILE_CREATED; 2437d58ffd35SMiklos Szeredi error = security_path_mknod(&nd->path, dentry, mode, 0); 2438d58ffd35SMiklos Szeredi if (error) 2439d58ffd35SMiklos Szeredi goto out_dput; 2440312b63fbSAl Viro error = vfs_create(dir->d_inode, dentry, mode, 2441312b63fbSAl Viro nd->flags & LOOKUP_EXCL); 2442d58ffd35SMiklos Szeredi if (error) 2443d58ffd35SMiklos Szeredi goto out_dput; 2444d58ffd35SMiklos Szeredi } 2445d18e9008SMiklos Szeredi out_no_open: 2446d58ffd35SMiklos Szeredi path->dentry = dentry; 2447d58ffd35SMiklos Szeredi path->mnt = nd->path.mnt; 24482675a4ebSAl Viro return 1; 2449d58ffd35SMiklos Szeredi 2450d58ffd35SMiklos Szeredi out_dput: 2451d58ffd35SMiklos Szeredi dput(dentry); 24522675a4ebSAl Viro return error; 2453d58ffd35SMiklos Szeredi } 2454d58ffd35SMiklos Szeredi 2455d58ffd35SMiklos Szeredi /* 2456fe2d35ffSAl Viro * Handle the last step of open() 245731e6b01fSNick Piggin */ 24582675a4ebSAl Viro static int do_last(struct nameidata *nd, struct path *path, 245930d90494SAl Viro struct file *file, const struct open_flags *op, 246047237687SAl Viro int *opened, const char *pathname) 2461fb1cc555SAl Viro { 2462a1e28038SAl Viro struct dentry *dir = nd->path.dentry; 2463ca344a89SAl Viro int open_flag = op->open_flag; 246477d660a8SMiklos Szeredi bool will_truncate = (open_flag & O_TRUNC) != 0; 246577d660a8SMiklos Szeredi bool want_write = false; 2466bcda7652SAl Viro int acc_mode = op->acc_mode; 2467a1eb3315SMiklos Szeredi struct inode *inode; 246877d660a8SMiklos Szeredi bool symlink_ok = false; 246916b1c1cdSMiklos Szeredi struct path save_parent = { .dentry = NULL, .mnt = NULL }; 247016b1c1cdSMiklos Szeredi bool retried = false; 247116c2cd71SAl Viro int error; 2472fb1cc555SAl Viro 2473c3e380b0SAl Viro nd->flags &= ~LOOKUP_PARENT; 2474c3e380b0SAl Viro nd->flags |= op->intent; 2475c3e380b0SAl Viro 24761f36f774SAl Viro switch (nd->last_type) { 24771f36f774SAl Viro case LAST_DOTDOT: 2478176306f5SNeil Brown case LAST_DOT: 2479fe2d35ffSAl Viro error = handle_dots(nd, nd->last_type); 2480fe2d35ffSAl Viro if (error) 24812675a4ebSAl Viro return error; 24821f36f774SAl Viro /* fallthrough */ 24831f36f774SAl Viro case LAST_ROOT: 24849f1fafeeSAl Viro error = complete_walk(nd); 248516c2cd71SAl Viro if (error) 24862675a4ebSAl Viro return error; 2487fe2d35ffSAl Viro audit_inode(pathname, nd->path.dentry); 2488ca344a89SAl Viro if (open_flag & O_CREAT) { 248916c2cd71SAl Viro error = -EISDIR; 24902675a4ebSAl Viro goto out; 2491fe2d35ffSAl Viro } 2492e83db167SMiklos Szeredi goto finish_open; 24931f36f774SAl Viro case LAST_BIND: 24949f1fafeeSAl Viro error = complete_walk(nd); 249516c2cd71SAl Viro if (error) 24962675a4ebSAl Viro return error; 24971f36f774SAl Viro audit_inode(pathname, dir); 2498e83db167SMiklos Szeredi goto finish_open; 24991f36f774SAl Viro } 2500a2c36b45SAl Viro 2501ca344a89SAl Viro if (!(open_flag & O_CREAT)) { 2502fe2d35ffSAl Viro if (nd->last.name[nd->last.len]) 2503fe2d35ffSAl Viro nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 2504bcda7652SAl Viro if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW)) 250577d660a8SMiklos Szeredi symlink_ok = true; 2506fe2d35ffSAl Viro /* we _can_ be in RCU mode here */ 2507a1eb3315SMiklos Szeredi error = lookup_fast(nd, &nd->last, path, &inode); 250871574865SMiklos Szeredi if (likely(!error)) 250971574865SMiklos Szeredi goto finish_lookup; 251071574865SMiklos Szeredi 2511ce57dfc1SAl Viro if (error < 0) 25122675a4ebSAl Viro goto out; 2513a1eb3315SMiklos Szeredi 251437d7fffcSMiklos Szeredi BUG_ON(nd->inode != dir->d_inode); 2515b6183df7SMiklos Szeredi } else { 2516fe2d35ffSAl Viro /* create side of things */ 2517a3fbbde7SAl Viro /* 2518b6183df7SMiklos Szeredi * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED 2519b6183df7SMiklos Szeredi * has been cleared when we got to the last component we are 2520b6183df7SMiklos Szeredi * about to look up 2521a3fbbde7SAl Viro */ 25229f1fafeeSAl Viro error = complete_walk(nd); 25239f1fafeeSAl Viro if (error) 25242675a4ebSAl Viro return error; 2525fe2d35ffSAl Viro 2526fe2d35ffSAl Viro audit_inode(pathname, dir); 252716c2cd71SAl Viro error = -EISDIR; 25281f36f774SAl Viro /* trailing slashes? */ 252931e6b01fSNick Piggin if (nd->last.name[nd->last.len]) 25302675a4ebSAl Viro goto out; 2531b6183df7SMiklos Szeredi } 25321f36f774SAl Viro 253316b1c1cdSMiklos Szeredi retry_lookup: 2534a1e28038SAl Viro mutex_lock(&dir->d_inode->i_mutex); 25352675a4ebSAl Viro error = lookup_open(nd, path, file, op, &want_write, opened); 2536fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2537fb1cc555SAl Viro 25382675a4ebSAl Viro if (error <= 0) { 25392675a4ebSAl Viro if (error) 2540d58ffd35SMiklos Szeredi goto out; 25416c0d46c4SAl Viro 254247237687SAl Viro if ((*opened & FILE_CREATED) || 25432675a4ebSAl Viro !S_ISREG(file->f_path.dentry->d_inode->i_mode)) 254477d660a8SMiklos Szeredi will_truncate = false; 2545d18e9008SMiklos Szeredi 25462675a4ebSAl Viro audit_inode(pathname, file->f_path.dentry); 2547d18e9008SMiklos Szeredi goto opened; 2548d18e9008SMiklos Szeredi } 2549d18e9008SMiklos Szeredi 255047237687SAl Viro if (*opened & FILE_CREATED) { 25519b44f1b3SAl Viro /* Don't check for write permission, don't truncate */ 2552ca344a89SAl Viro open_flag &= ~O_TRUNC; 255377d660a8SMiklos Szeredi will_truncate = false; 2554bcda7652SAl Viro acc_mode = MAY_OPEN; 2555d58ffd35SMiklos Szeredi path_to_nameidata(path, nd); 2556e83db167SMiklos Szeredi goto finish_open_created; 2557fb1cc555SAl Viro } 2558fb1cc555SAl Viro 2559fb1cc555SAl Viro /* 2560fb1cc555SAl Viro * It already exists. 2561fb1cc555SAl Viro */ 2562fb1cc555SAl Viro audit_inode(pathname, path->dentry); 2563fb1cc555SAl Viro 2564d18e9008SMiklos Szeredi /* 2565d18e9008SMiklos Szeredi * If atomic_open() acquired write access it is dropped now due to 2566d18e9008SMiklos Szeredi * possible mount and symlink following (this might be optimized away if 2567d18e9008SMiklos Szeredi * necessary...) 2568d18e9008SMiklos Szeredi */ 2569d18e9008SMiklos Szeredi if (want_write) { 2570d18e9008SMiklos Szeredi mnt_drop_write(nd->path.mnt); 257177d660a8SMiklos Szeredi want_write = false; 2572d18e9008SMiklos Szeredi } 2573d18e9008SMiklos Szeredi 2574fb1cc555SAl Viro error = -EEXIST; 2575ca344a89SAl Viro if (open_flag & O_EXCL) 2576fb1cc555SAl Viro goto exit_dput; 2577fb1cc555SAl Viro 25789875cf80SDavid Howells error = follow_managed(path, nd->flags); 25799875cf80SDavid Howells if (error < 0) 2580fb1cc555SAl Viro goto exit_dput; 2581fb1cc555SAl Viro 2582a3fbbde7SAl Viro if (error) 2583a3fbbde7SAl Viro nd->flags |= LOOKUP_JUMPED; 2584a3fbbde7SAl Viro 2585decf3400SMiklos Szeredi BUG_ON(nd->flags & LOOKUP_RCU); 2586decf3400SMiklos Szeredi inode = path->dentry->d_inode; 25875f5daac1SMiklos Szeredi finish_lookup: 25885f5daac1SMiklos Szeredi /* we _can_ be in RCU mode here */ 2589fb1cc555SAl Viro error = -ENOENT; 259054c33e7fSMiklos Szeredi if (!inode) { 259154c33e7fSMiklos Szeredi path_to_nameidata(path, nd); 25922675a4ebSAl Viro goto out; 259354c33e7fSMiklos Szeredi } 25949e67f361SAl Viro 2595d45ea867SMiklos Szeredi if (should_follow_link(inode, !symlink_ok)) { 2596d45ea867SMiklos Szeredi if (nd->flags & LOOKUP_RCU) { 2597d45ea867SMiklos Szeredi if (unlikely(unlazy_walk(nd, path->dentry))) { 2598d45ea867SMiklos Szeredi error = -ECHILD; 25992675a4ebSAl Viro goto out; 2600d45ea867SMiklos Szeredi } 2601d45ea867SMiklos Szeredi } 2602d45ea867SMiklos Szeredi BUG_ON(inode != path->dentry->d_inode); 26032675a4ebSAl Viro return 1; 2604d45ea867SMiklos Szeredi } 2605fb1cc555SAl Viro 260616b1c1cdSMiklos Szeredi if ((nd->flags & LOOKUP_RCU) || nd->path.mnt != path->mnt) { 2607fb1cc555SAl Viro path_to_nameidata(path, nd); 260816b1c1cdSMiklos Szeredi } else { 260916b1c1cdSMiklos Szeredi save_parent.dentry = nd->path.dentry; 261016b1c1cdSMiklos Szeredi save_parent.mnt = mntget(path->mnt); 261116b1c1cdSMiklos Szeredi nd->path.dentry = path->dentry; 261216b1c1cdSMiklos Szeredi 261316b1c1cdSMiklos Szeredi } 2614decf3400SMiklos Szeredi nd->inode = inode; 2615a3fbbde7SAl Viro /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */ 2616a3fbbde7SAl Viro error = complete_walk(nd); 261716b1c1cdSMiklos Szeredi if (error) { 261816b1c1cdSMiklos Szeredi path_put(&save_parent); 26192675a4ebSAl Viro return error; 262016b1c1cdSMiklos Szeredi } 2621fb1cc555SAl Viro error = -EISDIR; 2622050ac841SMiklos Szeredi if ((open_flag & O_CREAT) && S_ISDIR(nd->inode->i_mode)) 26232675a4ebSAl Viro goto out; 2624af2f5542SMiklos Szeredi error = -ENOTDIR; 2625af2f5542SMiklos Szeredi if ((nd->flags & LOOKUP_DIRECTORY) && !nd->inode->i_op->lookup) 26262675a4ebSAl Viro goto out; 2627d7fdd7f6SMiklos Szeredi audit_inode(pathname, nd->path.dentry); 2628e83db167SMiklos Szeredi finish_open: 26296c0d46c4SAl Viro if (!S_ISREG(nd->inode->i_mode)) 263077d660a8SMiklos Szeredi will_truncate = false; 26316c0d46c4SAl Viro 26320f9d1a10SAl Viro if (will_truncate) { 26330f9d1a10SAl Viro error = mnt_want_write(nd->path.mnt); 26340f9d1a10SAl Viro if (error) 26352675a4ebSAl Viro goto out; 263677d660a8SMiklos Szeredi want_write = true; 26370f9d1a10SAl Viro } 2638e83db167SMiklos Szeredi finish_open_created: 2639bcda7652SAl Viro error = may_open(&nd->path, acc_mode, open_flag); 2640ca344a89SAl Viro if (error) 26412675a4ebSAl Viro goto out; 264230d90494SAl Viro file->f_path.mnt = nd->path.mnt; 264330d90494SAl Viro error = finish_open(file, nd->path.dentry, NULL, opened); 264430d90494SAl Viro if (error) { 264530d90494SAl Viro if (error == -EOPENSTALE) 2646f60dc3dbSMiklos Szeredi goto stale_open; 2647015c3bbcSMiklos Szeredi goto out; 2648f60dc3dbSMiklos Szeredi } 2649a8277b9bSMiklos Szeredi opened: 26502675a4ebSAl Viro error = open_check_o_direct(file); 2651015c3bbcSMiklos Szeredi if (error) 2652015c3bbcSMiklos Szeredi goto exit_fput; 26532675a4ebSAl Viro error = ima_file_check(file, op->acc_mode); 2654aa4caadbSMiklos Szeredi if (error) 2655aa4caadbSMiklos Szeredi goto exit_fput; 2656aa4caadbSMiklos Szeredi 26570f9d1a10SAl Viro if (will_truncate) { 26582675a4ebSAl Viro error = handle_truncate(file); 2659aa4caadbSMiklos Szeredi if (error) 2660aa4caadbSMiklos Szeredi goto exit_fput; 26610f9d1a10SAl Viro } 2662ca344a89SAl Viro out: 2663ca344a89SAl Viro if (want_write) 26640f9d1a10SAl Viro mnt_drop_write(nd->path.mnt); 266516b1c1cdSMiklos Szeredi path_put(&save_parent); 2666e276ae67SMiklos Szeredi terminate_walk(nd); 26672675a4ebSAl Viro return error; 2668fb1cc555SAl Viro 2669fb1cc555SAl Viro exit_dput: 2670fb1cc555SAl Viro path_put_conditional(path, nd); 2671ca344a89SAl Viro goto out; 2672015c3bbcSMiklos Szeredi exit_fput: 26732675a4ebSAl Viro fput(file); 26742675a4ebSAl Viro goto out; 2675015c3bbcSMiklos Szeredi 2676f60dc3dbSMiklos Szeredi stale_open: 2677f60dc3dbSMiklos Szeredi /* If no saved parent or already retried then can't retry */ 2678f60dc3dbSMiklos Szeredi if (!save_parent.dentry || retried) 2679f60dc3dbSMiklos Szeredi goto out; 2680f60dc3dbSMiklos Szeredi 2681f60dc3dbSMiklos Szeredi BUG_ON(save_parent.dentry != dir); 2682f60dc3dbSMiklos Szeredi path_put(&nd->path); 2683f60dc3dbSMiklos Szeredi nd->path = save_parent; 2684f60dc3dbSMiklos Szeredi nd->inode = dir->d_inode; 2685f60dc3dbSMiklos Szeredi save_parent.mnt = NULL; 2686f60dc3dbSMiklos Szeredi save_parent.dentry = NULL; 2687f60dc3dbSMiklos Szeredi if (want_write) { 2688f60dc3dbSMiklos Szeredi mnt_drop_write(nd->path.mnt); 2689f60dc3dbSMiklos Szeredi want_write = false; 2690f60dc3dbSMiklos Szeredi } 2691f60dc3dbSMiklos Szeredi retried = true; 2692f60dc3dbSMiklos Szeredi goto retry_lookup; 2693fb1cc555SAl Viro } 2694fb1cc555SAl Viro 269513aab428SAl Viro static struct file *path_openat(int dfd, const char *pathname, 269673d049a4SAl Viro struct nameidata *nd, const struct open_flags *op, int flags) 26971da177e4SLinus Torvalds { 2698fe2d35ffSAl Viro struct file *base = NULL; 269930d90494SAl Viro struct file *file; 27009850c056SAl Viro struct path path; 270147237687SAl Viro int opened = 0; 270213aab428SAl Viro int error; 270331e6b01fSNick Piggin 270430d90494SAl Viro file = get_empty_filp(); 270530d90494SAl Viro if (!file) 270631e6b01fSNick Piggin return ERR_PTR(-ENFILE); 270731e6b01fSNick Piggin 270830d90494SAl Viro file->f_flags = op->open_flag; 270931e6b01fSNick Piggin 271073d049a4SAl Viro error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base); 271131e6b01fSNick Piggin if (unlikely(error)) 27122675a4ebSAl Viro goto out; 271331e6b01fSNick Piggin 2714fe2d35ffSAl Viro current->total_link_count = 0; 271573d049a4SAl Viro error = link_path_walk(pathname, nd); 271631e6b01fSNick Piggin if (unlikely(error)) 27172675a4ebSAl Viro goto out; 27181da177e4SLinus Torvalds 27192675a4ebSAl Viro error = do_last(nd, &path, file, op, &opened, pathname); 27202675a4ebSAl Viro while (unlikely(error > 0)) { /* trailing symlink */ 27217b9337aaSNick Piggin struct path link = path; 2722def4af30SAl Viro void *cookie; 2723574197e0SAl Viro if (!(nd->flags & LOOKUP_FOLLOW)) { 272473d049a4SAl Viro path_put_conditional(&path, nd); 272573d049a4SAl Viro path_put(&nd->path); 27262675a4ebSAl Viro error = -ELOOP; 272740b39136SAl Viro break; 272840b39136SAl Viro } 272973d049a4SAl Viro nd->flags |= LOOKUP_PARENT; 273073d049a4SAl Viro nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL); 2731574197e0SAl Viro error = follow_link(&link, nd, &cookie); 2732c3e380b0SAl Viro if (unlikely(error)) 27332675a4ebSAl Viro break; 27342675a4ebSAl Viro error = do_last(nd, &path, file, op, &opened, pathname); 2735574197e0SAl Viro put_link(nd, &link, cookie); 2736806b681cSAl Viro } 273710fa8e62SAl Viro out: 273873d049a4SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) 273973d049a4SAl Viro path_put(&nd->root); 2740fe2d35ffSAl Viro if (base) 2741fe2d35ffSAl Viro fput(base); 27422675a4ebSAl Viro if (!(opened & FILE_OPENED)) { 27432675a4ebSAl Viro BUG_ON(!error); 274430d90494SAl Viro put_filp(file); 2745015c3bbcSMiklos Szeredi } 27462675a4ebSAl Viro if (unlikely(error)) { 27472675a4ebSAl Viro if (error == -EOPENSTALE) { 27482675a4ebSAl Viro if (flags & LOOKUP_RCU) 27492675a4ebSAl Viro error = -ECHILD; 27502675a4ebSAl Viro else 27512675a4ebSAl Viro error = -ESTALE; 27522675a4ebSAl Viro } 27532675a4ebSAl Viro file = ERR_PTR(error); 27542675a4ebSAl Viro } 27552675a4ebSAl Viro return file; 2756de459215SKirill Korotaev } 27571da177e4SLinus Torvalds 275813aab428SAl Viro struct file *do_filp_open(int dfd, const char *pathname, 275913aab428SAl Viro const struct open_flags *op, int flags) 276013aab428SAl Viro { 276173d049a4SAl Viro struct nameidata nd; 276213aab428SAl Viro struct file *filp; 276313aab428SAl Viro 276473d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU); 276513aab428SAl Viro if (unlikely(filp == ERR_PTR(-ECHILD))) 276673d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags); 276713aab428SAl Viro if (unlikely(filp == ERR_PTR(-ESTALE))) 276873d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL); 276913aab428SAl Viro return filp; 277013aab428SAl Viro } 277113aab428SAl Viro 277273d049a4SAl Viro struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt, 277373d049a4SAl Viro const char *name, const struct open_flags *op, int flags) 277473d049a4SAl Viro { 277573d049a4SAl Viro struct nameidata nd; 277673d049a4SAl Viro struct file *file; 277773d049a4SAl Viro 277873d049a4SAl Viro nd.root.mnt = mnt; 277973d049a4SAl Viro nd.root.dentry = dentry; 278073d049a4SAl Viro 278173d049a4SAl Viro flags |= LOOKUP_ROOT; 278273d049a4SAl Viro 2783bcda7652SAl Viro if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN) 278473d049a4SAl Viro return ERR_PTR(-ELOOP); 278573d049a4SAl Viro 278673d049a4SAl Viro file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU); 278773d049a4SAl Viro if (unlikely(file == ERR_PTR(-ECHILD))) 278873d049a4SAl Viro file = path_openat(-1, name, &nd, op, flags); 278973d049a4SAl Viro if (unlikely(file == ERR_PTR(-ESTALE))) 279073d049a4SAl Viro file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL); 279173d049a4SAl Viro return file; 279273d049a4SAl Viro } 279373d049a4SAl Viro 2794ed75e95dSAl Viro struct dentry *kern_path_create(int dfd, const char *pathname, struct path *path, int is_dir) 27951da177e4SLinus Torvalds { 2796c663e5d8SChristoph Hellwig struct dentry *dentry = ERR_PTR(-EEXIST); 2797ed75e95dSAl Viro struct nameidata nd; 2798ed75e95dSAl Viro int error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd); 2799ed75e95dSAl Viro if (error) 2800ed75e95dSAl Viro return ERR_PTR(error); 28011da177e4SLinus Torvalds 2802c663e5d8SChristoph Hellwig /* 2803c663e5d8SChristoph Hellwig * Yucky last component or no last component at all? 2804c663e5d8SChristoph Hellwig * (foo/., foo/.., /////) 2805c663e5d8SChristoph Hellwig */ 2806ed75e95dSAl Viro if (nd.last_type != LAST_NORM) 2807ed75e95dSAl Viro goto out; 2808ed75e95dSAl Viro nd.flags &= ~LOOKUP_PARENT; 2809ed75e95dSAl Viro nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL; 2810c663e5d8SChristoph Hellwig 2811c663e5d8SChristoph Hellwig /* 2812c663e5d8SChristoph Hellwig * Do the final lookup. 2813c663e5d8SChristoph Hellwig */ 2814ed75e95dSAl Viro mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 2815ed75e95dSAl Viro dentry = lookup_hash(&nd); 28161da177e4SLinus Torvalds if (IS_ERR(dentry)) 28171da177e4SLinus Torvalds goto fail; 2818c663e5d8SChristoph Hellwig 2819e9baf6e5SAl Viro if (dentry->d_inode) 2820e9baf6e5SAl Viro goto eexist; 2821c663e5d8SChristoph Hellwig /* 2822c663e5d8SChristoph Hellwig * Special case - lookup gave negative, but... we had foo/bar/ 2823c663e5d8SChristoph Hellwig * From the vfs_mknod() POV we just have a negative dentry - 2824c663e5d8SChristoph Hellwig * all is fine. Let's be bastards - you had / on the end, you've 2825c663e5d8SChristoph Hellwig * been asking for (non-existent) directory. -ENOENT for you. 2826c663e5d8SChristoph Hellwig */ 2827ed75e95dSAl Viro if (unlikely(!is_dir && nd.last.name[nd.last.len])) { 28281da177e4SLinus Torvalds dput(dentry); 28291da177e4SLinus Torvalds dentry = ERR_PTR(-ENOENT); 2830ed75e95dSAl Viro goto fail; 2831e9baf6e5SAl Viro } 2832ed75e95dSAl Viro *path = nd.path; 2833e9baf6e5SAl Viro return dentry; 2834e9baf6e5SAl Viro eexist: 2835e9baf6e5SAl Viro dput(dentry); 2836e9baf6e5SAl Viro dentry = ERR_PTR(-EEXIST); 28371da177e4SLinus Torvalds fail: 2838dae6ad8fSAl Viro mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 2839ed75e95dSAl Viro out: 2840dae6ad8fSAl Viro path_put(&nd.path); 2841ed75e95dSAl Viro return dentry; 2842dae6ad8fSAl Viro } 2843dae6ad8fSAl Viro EXPORT_SYMBOL(kern_path_create); 2844dae6ad8fSAl Viro 2845dae6ad8fSAl Viro struct dentry *user_path_create(int dfd, const char __user *pathname, struct path *path, int is_dir) 2846dae6ad8fSAl Viro { 2847dae6ad8fSAl Viro char *tmp = getname(pathname); 2848dae6ad8fSAl Viro struct dentry *res; 2849dae6ad8fSAl Viro if (IS_ERR(tmp)) 2850dae6ad8fSAl Viro return ERR_CAST(tmp); 2851dae6ad8fSAl Viro res = kern_path_create(dfd, tmp, path, is_dir); 2852dae6ad8fSAl Viro putname(tmp); 2853dae6ad8fSAl Viro return res; 2854dae6ad8fSAl Viro } 2855dae6ad8fSAl Viro EXPORT_SYMBOL(user_path_create); 2856dae6ad8fSAl Viro 28571a67aafbSAl Viro int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev) 28581da177e4SLinus Torvalds { 2859a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 28601da177e4SLinus Torvalds 28611da177e4SLinus Torvalds if (error) 28621da177e4SLinus Torvalds return error; 28631da177e4SLinus Torvalds 2864975d6b39SEric W. Biederman if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD)) 28651da177e4SLinus Torvalds return -EPERM; 28661da177e4SLinus Torvalds 2867acfa4380SAl Viro if (!dir->i_op->mknod) 28681da177e4SLinus Torvalds return -EPERM; 28691da177e4SLinus Torvalds 287008ce5f16SSerge E. Hallyn error = devcgroup_inode_mknod(mode, dev); 287108ce5f16SSerge E. Hallyn if (error) 287208ce5f16SSerge E. Hallyn return error; 287308ce5f16SSerge E. Hallyn 28741da177e4SLinus Torvalds error = security_inode_mknod(dir, dentry, mode, dev); 28751da177e4SLinus Torvalds if (error) 28761da177e4SLinus Torvalds return error; 28771da177e4SLinus Torvalds 28781da177e4SLinus Torvalds error = dir->i_op->mknod(dir, dentry, mode, dev); 2879a74574aaSStephen Smalley if (!error) 2880f38aa942SAmy Griffis fsnotify_create(dir, dentry); 28811da177e4SLinus Torvalds return error; 28821da177e4SLinus Torvalds } 28831da177e4SLinus Torvalds 2884f69aac00SAl Viro static int may_mknod(umode_t mode) 2885463c3197SDave Hansen { 2886463c3197SDave Hansen switch (mode & S_IFMT) { 2887463c3197SDave Hansen case S_IFREG: 2888463c3197SDave Hansen case S_IFCHR: 2889463c3197SDave Hansen case S_IFBLK: 2890463c3197SDave Hansen case S_IFIFO: 2891463c3197SDave Hansen case S_IFSOCK: 2892463c3197SDave Hansen case 0: /* zero mode translates to S_IFREG */ 2893463c3197SDave Hansen return 0; 2894463c3197SDave Hansen case S_IFDIR: 2895463c3197SDave Hansen return -EPERM; 2896463c3197SDave Hansen default: 2897463c3197SDave Hansen return -EINVAL; 2898463c3197SDave Hansen } 2899463c3197SDave Hansen } 2900463c3197SDave Hansen 29018208a22bSAl Viro SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode, 29022e4d0924SHeiko Carstens unsigned, dev) 29031da177e4SLinus Torvalds { 29041da177e4SLinus Torvalds struct dentry *dentry; 2905dae6ad8fSAl Viro struct path path; 2906dae6ad8fSAl Viro int error; 29071da177e4SLinus Torvalds 29081da177e4SLinus Torvalds if (S_ISDIR(mode)) 29091da177e4SLinus Torvalds return -EPERM; 29101da177e4SLinus Torvalds 2911dae6ad8fSAl Viro dentry = user_path_create(dfd, filename, &path, 0); 2912dae6ad8fSAl Viro if (IS_ERR(dentry)) 2913dae6ad8fSAl Viro return PTR_ERR(dentry); 29142ad94ae6SAl Viro 2915dae6ad8fSAl Viro if (!IS_POSIXACL(path.dentry->d_inode)) 2916ce3b0f8dSAl Viro mode &= ~current_umask(); 2917463c3197SDave Hansen error = may_mknod(mode); 2918463c3197SDave Hansen if (error) 2919463c3197SDave Hansen goto out_dput; 2920dae6ad8fSAl Viro error = mnt_want_write(path.mnt); 2921463c3197SDave Hansen if (error) 2922463c3197SDave Hansen goto out_dput; 2923dae6ad8fSAl Viro error = security_path_mknod(&path, dentry, mode, dev); 2924be6d3e56SKentaro Takeda if (error) 2925be6d3e56SKentaro Takeda goto out_drop_write; 29261da177e4SLinus Torvalds switch (mode & S_IFMT) { 29271da177e4SLinus Torvalds case 0: case S_IFREG: 2928312b63fbSAl Viro error = vfs_create(path.dentry->d_inode,dentry,mode,true); 29291da177e4SLinus Torvalds break; 29301da177e4SLinus Torvalds case S_IFCHR: case S_IFBLK: 2931dae6ad8fSAl Viro error = vfs_mknod(path.dentry->d_inode,dentry,mode, 29321da177e4SLinus Torvalds new_decode_dev(dev)); 29331da177e4SLinus Torvalds break; 29341da177e4SLinus Torvalds case S_IFIFO: case S_IFSOCK: 2935dae6ad8fSAl Viro error = vfs_mknod(path.dentry->d_inode,dentry,mode,0); 29361da177e4SLinus Torvalds break; 29371da177e4SLinus Torvalds } 2938be6d3e56SKentaro Takeda out_drop_write: 2939dae6ad8fSAl Viro mnt_drop_write(path.mnt); 2940463c3197SDave Hansen out_dput: 29411da177e4SLinus Torvalds dput(dentry); 2942dae6ad8fSAl Viro mutex_unlock(&path.dentry->d_inode->i_mutex); 2943dae6ad8fSAl Viro path_put(&path); 29441da177e4SLinus Torvalds 29451da177e4SLinus Torvalds return error; 29461da177e4SLinus Torvalds } 29471da177e4SLinus Torvalds 29488208a22bSAl Viro SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev) 29495590ff0dSUlrich Drepper { 29505590ff0dSUlrich Drepper return sys_mknodat(AT_FDCWD, filename, mode, dev); 29515590ff0dSUlrich Drepper } 29525590ff0dSUlrich Drepper 295318bb1db3SAl Viro int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) 29541da177e4SLinus Torvalds { 2955a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 29568de52778SAl Viro unsigned max_links = dir->i_sb->s_max_links; 29571da177e4SLinus Torvalds 29581da177e4SLinus Torvalds if (error) 29591da177e4SLinus Torvalds return error; 29601da177e4SLinus Torvalds 2961acfa4380SAl Viro if (!dir->i_op->mkdir) 29621da177e4SLinus Torvalds return -EPERM; 29631da177e4SLinus Torvalds 29641da177e4SLinus Torvalds mode &= (S_IRWXUGO|S_ISVTX); 29651da177e4SLinus Torvalds error = security_inode_mkdir(dir, dentry, mode); 29661da177e4SLinus Torvalds if (error) 29671da177e4SLinus Torvalds return error; 29681da177e4SLinus Torvalds 29698de52778SAl Viro if (max_links && dir->i_nlink >= max_links) 29708de52778SAl Viro return -EMLINK; 29718de52778SAl Viro 29721da177e4SLinus Torvalds error = dir->i_op->mkdir(dir, dentry, mode); 2973a74574aaSStephen Smalley if (!error) 2974f38aa942SAmy Griffis fsnotify_mkdir(dir, dentry); 29751da177e4SLinus Torvalds return error; 29761da177e4SLinus Torvalds } 29771da177e4SLinus Torvalds 2978a218d0fdSAl Viro SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode) 29791da177e4SLinus Torvalds { 29806902d925SDave Hansen struct dentry *dentry; 2981dae6ad8fSAl Viro struct path path; 2982dae6ad8fSAl Viro int error; 29831da177e4SLinus Torvalds 2984dae6ad8fSAl Viro dentry = user_path_create(dfd, pathname, &path, 1); 29856902d925SDave Hansen if (IS_ERR(dentry)) 2986dae6ad8fSAl Viro return PTR_ERR(dentry); 29876902d925SDave Hansen 2988dae6ad8fSAl Viro if (!IS_POSIXACL(path.dentry->d_inode)) 2989ce3b0f8dSAl Viro mode &= ~current_umask(); 2990dae6ad8fSAl Viro error = mnt_want_write(path.mnt); 2991463c3197SDave Hansen if (error) 2992463c3197SDave Hansen goto out_dput; 2993dae6ad8fSAl Viro error = security_path_mkdir(&path, dentry, mode); 2994be6d3e56SKentaro Takeda if (error) 2995be6d3e56SKentaro Takeda goto out_drop_write; 2996dae6ad8fSAl Viro error = vfs_mkdir(path.dentry->d_inode, dentry, mode); 2997be6d3e56SKentaro Takeda out_drop_write: 2998dae6ad8fSAl Viro mnt_drop_write(path.mnt); 2999463c3197SDave Hansen out_dput: 30001da177e4SLinus Torvalds dput(dentry); 3001dae6ad8fSAl Viro mutex_unlock(&path.dentry->d_inode->i_mutex); 3002dae6ad8fSAl Viro path_put(&path); 30031da177e4SLinus Torvalds return error; 30041da177e4SLinus Torvalds } 30051da177e4SLinus Torvalds 3006a218d0fdSAl Viro SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode) 30075590ff0dSUlrich Drepper { 30085590ff0dSUlrich Drepper return sys_mkdirat(AT_FDCWD, pathname, mode); 30095590ff0dSUlrich Drepper } 30105590ff0dSUlrich Drepper 30111da177e4SLinus Torvalds /* 3012a71905f0SSage Weil * The dentry_unhash() helper will try to drop the dentry early: we 3013c0d02594SJ. Bruce Fields * should have a usage count of 1 if we're the only user of this 3014a71905f0SSage Weil * dentry, and if that is true (possibly after pruning the dcache), 3015a71905f0SSage Weil * then we drop the dentry now. 30161da177e4SLinus Torvalds * 30171da177e4SLinus Torvalds * A low-level filesystem can, if it choses, legally 30181da177e4SLinus Torvalds * do a 30191da177e4SLinus Torvalds * 30201da177e4SLinus Torvalds * if (!d_unhashed(dentry)) 30211da177e4SLinus Torvalds * return -EBUSY; 30221da177e4SLinus Torvalds * 30231da177e4SLinus Torvalds * if it cannot handle the case of removing a directory 30241da177e4SLinus Torvalds * that is still in use by something else.. 30251da177e4SLinus Torvalds */ 30261da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry) 30271da177e4SLinus Torvalds { 30281da177e4SLinus Torvalds shrink_dcache_parent(dentry); 30291da177e4SLinus Torvalds spin_lock(&dentry->d_lock); 303064252c75SSage Weil if (dentry->d_count == 1) 30311da177e4SLinus Torvalds __d_drop(dentry); 30321da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 30331da177e4SLinus Torvalds } 30341da177e4SLinus Torvalds 30351da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry) 30361da177e4SLinus Torvalds { 30371da177e4SLinus Torvalds int error = may_delete(dir, dentry, 1); 30381da177e4SLinus Torvalds 30391da177e4SLinus Torvalds if (error) 30401da177e4SLinus Torvalds return error; 30411da177e4SLinus Torvalds 3042acfa4380SAl Viro if (!dir->i_op->rmdir) 30431da177e4SLinus Torvalds return -EPERM; 30441da177e4SLinus Torvalds 30451d2ef590SAl Viro dget(dentry); 30461b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 3047912dbc15SSage Weil 30481da177e4SLinus Torvalds error = -EBUSY; 3049912dbc15SSage Weil if (d_mountpoint(dentry)) 3050912dbc15SSage Weil goto out; 3051912dbc15SSage Weil 30521da177e4SLinus Torvalds error = security_inode_rmdir(dir, dentry); 3053912dbc15SSage Weil if (error) 3054912dbc15SSage Weil goto out; 3055912dbc15SSage Weil 30563cebde24SSage Weil shrink_dcache_parent(dentry); 30571da177e4SLinus Torvalds error = dir->i_op->rmdir(dir, dentry); 3058912dbc15SSage Weil if (error) 3059912dbc15SSage Weil goto out; 3060912dbc15SSage Weil 30611da177e4SLinus Torvalds dentry->d_inode->i_flags |= S_DEAD; 3062d83c49f3SAl Viro dont_mount(dentry); 30631da177e4SLinus Torvalds 3064912dbc15SSage Weil out: 3065912dbc15SSage Weil mutex_unlock(&dentry->d_inode->i_mutex); 30661d2ef590SAl Viro dput(dentry); 3067912dbc15SSage Weil if (!error) 3068912dbc15SSage Weil d_delete(dentry); 30691da177e4SLinus Torvalds return error; 30701da177e4SLinus Torvalds } 30711da177e4SLinus Torvalds 30725590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname) 30731da177e4SLinus Torvalds { 30741da177e4SLinus Torvalds int error = 0; 30751da177e4SLinus Torvalds char * name; 30761da177e4SLinus Torvalds struct dentry *dentry; 30771da177e4SLinus Torvalds struct nameidata nd; 30781da177e4SLinus Torvalds 30792ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &name); 30801da177e4SLinus Torvalds if (error) 30812ad94ae6SAl Viro return error; 30821da177e4SLinus Torvalds 30831da177e4SLinus Torvalds switch(nd.last_type) { 30841da177e4SLinus Torvalds case LAST_DOTDOT: 30851da177e4SLinus Torvalds error = -ENOTEMPTY; 30861da177e4SLinus Torvalds goto exit1; 30871da177e4SLinus Torvalds case LAST_DOT: 30881da177e4SLinus Torvalds error = -EINVAL; 30891da177e4SLinus Torvalds goto exit1; 30901da177e4SLinus Torvalds case LAST_ROOT: 30911da177e4SLinus Torvalds error = -EBUSY; 30921da177e4SLinus Torvalds goto exit1; 30931da177e4SLinus Torvalds } 30940612d9fbSOGAWA Hirofumi 30950612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 30960612d9fbSOGAWA Hirofumi 30974ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 309849705b77SChristoph Hellwig dentry = lookup_hash(&nd); 30991da177e4SLinus Torvalds error = PTR_ERR(dentry); 31006902d925SDave Hansen if (IS_ERR(dentry)) 31016902d925SDave Hansen goto exit2; 3102e6bc45d6STheodore Ts'o if (!dentry->d_inode) { 3103e6bc45d6STheodore Ts'o error = -ENOENT; 3104e6bc45d6STheodore Ts'o goto exit3; 3105e6bc45d6STheodore Ts'o } 31060622753bSDave Hansen error = mnt_want_write(nd.path.mnt); 31070622753bSDave Hansen if (error) 31080622753bSDave Hansen goto exit3; 3109be6d3e56SKentaro Takeda error = security_path_rmdir(&nd.path, dentry); 3110be6d3e56SKentaro Takeda if (error) 3111be6d3e56SKentaro Takeda goto exit4; 31124ac91378SJan Blunck error = vfs_rmdir(nd.path.dentry->d_inode, dentry); 3113be6d3e56SKentaro Takeda exit4: 31140622753bSDave Hansen mnt_drop_write(nd.path.mnt); 31150622753bSDave Hansen exit3: 31161da177e4SLinus Torvalds dput(dentry); 31176902d925SDave Hansen exit2: 31184ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 31191da177e4SLinus Torvalds exit1: 31201d957f9bSJan Blunck path_put(&nd.path); 31211da177e4SLinus Torvalds putname(name); 31221da177e4SLinus Torvalds return error; 31231da177e4SLinus Torvalds } 31241da177e4SLinus Torvalds 31253cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname) 31265590ff0dSUlrich Drepper { 31275590ff0dSUlrich Drepper return do_rmdir(AT_FDCWD, pathname); 31285590ff0dSUlrich Drepper } 31295590ff0dSUlrich Drepper 31301da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry) 31311da177e4SLinus Torvalds { 31321da177e4SLinus Torvalds int error = may_delete(dir, dentry, 0); 31331da177e4SLinus Torvalds 31341da177e4SLinus Torvalds if (error) 31351da177e4SLinus Torvalds return error; 31361da177e4SLinus Torvalds 3137acfa4380SAl Viro if (!dir->i_op->unlink) 31381da177e4SLinus Torvalds return -EPERM; 31391da177e4SLinus Torvalds 31401b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 31411da177e4SLinus Torvalds if (d_mountpoint(dentry)) 31421da177e4SLinus Torvalds error = -EBUSY; 31431da177e4SLinus Torvalds else { 31441da177e4SLinus Torvalds error = security_inode_unlink(dir, dentry); 3145bec1052eSAl Viro if (!error) { 31461da177e4SLinus Torvalds error = dir->i_op->unlink(dir, dentry); 3147bec1052eSAl Viro if (!error) 3148d83c49f3SAl Viro dont_mount(dentry); 3149bec1052eSAl Viro } 31501da177e4SLinus Torvalds } 31511b1dcc1bSJes Sorensen mutex_unlock(&dentry->d_inode->i_mutex); 31521da177e4SLinus Torvalds 31531da177e4SLinus Torvalds /* We don't d_delete() NFS sillyrenamed files--they still exist. */ 31541da177e4SLinus Torvalds if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) { 3155ece95912SJan Kara fsnotify_link_count(dentry->d_inode); 31561da177e4SLinus Torvalds d_delete(dentry); 31571da177e4SLinus Torvalds } 31580eeca283SRobert Love 31591da177e4SLinus Torvalds return error; 31601da177e4SLinus Torvalds } 31611da177e4SLinus Torvalds 31621da177e4SLinus Torvalds /* 31631da177e4SLinus Torvalds * Make sure that the actual truncation of the file will occur outside its 31641b1dcc1bSJes Sorensen * directory's i_mutex. Truncate can take a long time if there is a lot of 31651da177e4SLinus Torvalds * writeout happening, and we don't want to prevent access to the directory 31661da177e4SLinus Torvalds * while waiting on the I/O. 31671da177e4SLinus Torvalds */ 31685590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname) 31691da177e4SLinus Torvalds { 31702ad94ae6SAl Viro int error; 31711da177e4SLinus Torvalds char *name; 31721da177e4SLinus Torvalds struct dentry *dentry; 31731da177e4SLinus Torvalds struct nameidata nd; 31741da177e4SLinus Torvalds struct inode *inode = NULL; 31751da177e4SLinus Torvalds 31762ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &name); 31771da177e4SLinus Torvalds if (error) 31782ad94ae6SAl Viro return error; 31792ad94ae6SAl Viro 31801da177e4SLinus Torvalds error = -EISDIR; 31811da177e4SLinus Torvalds if (nd.last_type != LAST_NORM) 31821da177e4SLinus Torvalds goto exit1; 31830612d9fbSOGAWA Hirofumi 31840612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 31850612d9fbSOGAWA Hirofumi 31864ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 318749705b77SChristoph Hellwig dentry = lookup_hash(&nd); 31881da177e4SLinus Torvalds error = PTR_ERR(dentry); 31891da177e4SLinus Torvalds if (!IS_ERR(dentry)) { 31901da177e4SLinus Torvalds /* Why not before? Because we want correct error value */ 319150338b88STörök Edwin if (nd.last.name[nd.last.len]) 319250338b88STörök Edwin goto slashes; 31931da177e4SLinus Torvalds inode = dentry->d_inode; 319450338b88STörök Edwin if (!inode) 3195e6bc45d6STheodore Ts'o goto slashes; 31967de9c6eeSAl Viro ihold(inode); 31970622753bSDave Hansen error = mnt_want_write(nd.path.mnt); 31980622753bSDave Hansen if (error) 31990622753bSDave Hansen goto exit2; 3200be6d3e56SKentaro Takeda error = security_path_unlink(&nd.path, dentry); 3201be6d3e56SKentaro Takeda if (error) 3202be6d3e56SKentaro Takeda goto exit3; 32034ac91378SJan Blunck error = vfs_unlink(nd.path.dentry->d_inode, dentry); 3204be6d3e56SKentaro Takeda exit3: 32050622753bSDave Hansen mnt_drop_write(nd.path.mnt); 32061da177e4SLinus Torvalds exit2: 32071da177e4SLinus Torvalds dput(dentry); 32081da177e4SLinus Torvalds } 32094ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 32101da177e4SLinus Torvalds if (inode) 32111da177e4SLinus Torvalds iput(inode); /* truncate the inode here */ 32121da177e4SLinus Torvalds exit1: 32131d957f9bSJan Blunck path_put(&nd.path); 32141da177e4SLinus Torvalds putname(name); 32151da177e4SLinus Torvalds return error; 32161da177e4SLinus Torvalds 32171da177e4SLinus Torvalds slashes: 32181da177e4SLinus Torvalds error = !dentry->d_inode ? -ENOENT : 32191da177e4SLinus Torvalds S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR; 32201da177e4SLinus Torvalds goto exit2; 32211da177e4SLinus Torvalds } 32221da177e4SLinus Torvalds 32232e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag) 32245590ff0dSUlrich Drepper { 32255590ff0dSUlrich Drepper if ((flag & ~AT_REMOVEDIR) != 0) 32265590ff0dSUlrich Drepper return -EINVAL; 32275590ff0dSUlrich Drepper 32285590ff0dSUlrich Drepper if (flag & AT_REMOVEDIR) 32295590ff0dSUlrich Drepper return do_rmdir(dfd, pathname); 32305590ff0dSUlrich Drepper 32315590ff0dSUlrich Drepper return do_unlinkat(dfd, pathname); 32325590ff0dSUlrich Drepper } 32335590ff0dSUlrich Drepper 32343480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname) 32355590ff0dSUlrich Drepper { 32365590ff0dSUlrich Drepper return do_unlinkat(AT_FDCWD, pathname); 32375590ff0dSUlrich Drepper } 32385590ff0dSUlrich Drepper 3239db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname) 32401da177e4SLinus Torvalds { 3241a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 32421da177e4SLinus Torvalds 32431da177e4SLinus Torvalds if (error) 32441da177e4SLinus Torvalds return error; 32451da177e4SLinus Torvalds 3246acfa4380SAl Viro if (!dir->i_op->symlink) 32471da177e4SLinus Torvalds return -EPERM; 32481da177e4SLinus Torvalds 32491da177e4SLinus Torvalds error = security_inode_symlink(dir, dentry, oldname); 32501da177e4SLinus Torvalds if (error) 32511da177e4SLinus Torvalds return error; 32521da177e4SLinus Torvalds 32531da177e4SLinus Torvalds error = dir->i_op->symlink(dir, dentry, oldname); 3254a74574aaSStephen Smalley if (!error) 3255f38aa942SAmy Griffis fsnotify_create(dir, dentry); 32561da177e4SLinus Torvalds return error; 32571da177e4SLinus Torvalds } 32581da177e4SLinus Torvalds 32592e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname, 32602e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 32611da177e4SLinus Torvalds { 32622ad94ae6SAl Viro int error; 32631da177e4SLinus Torvalds char *from; 32646902d925SDave Hansen struct dentry *dentry; 3265dae6ad8fSAl Viro struct path path; 32661da177e4SLinus Torvalds 32671da177e4SLinus Torvalds from = getname(oldname); 32681da177e4SLinus Torvalds if (IS_ERR(from)) 32691da177e4SLinus Torvalds return PTR_ERR(from); 32702ad94ae6SAl Viro 3271dae6ad8fSAl Viro dentry = user_path_create(newdfd, newname, &path, 0); 32721da177e4SLinus Torvalds error = PTR_ERR(dentry); 32736902d925SDave Hansen if (IS_ERR(dentry)) 3274dae6ad8fSAl Viro goto out_putname; 32756902d925SDave Hansen 3276dae6ad8fSAl Viro error = mnt_want_write(path.mnt); 327775c3f29dSDave Hansen if (error) 327875c3f29dSDave Hansen goto out_dput; 3279dae6ad8fSAl Viro error = security_path_symlink(&path, dentry, from); 3280be6d3e56SKentaro Takeda if (error) 3281be6d3e56SKentaro Takeda goto out_drop_write; 3282dae6ad8fSAl Viro error = vfs_symlink(path.dentry->d_inode, dentry, from); 3283be6d3e56SKentaro Takeda out_drop_write: 3284dae6ad8fSAl Viro mnt_drop_write(path.mnt); 328575c3f29dSDave Hansen out_dput: 32861da177e4SLinus Torvalds dput(dentry); 3287dae6ad8fSAl Viro mutex_unlock(&path.dentry->d_inode->i_mutex); 3288dae6ad8fSAl Viro path_put(&path); 32896902d925SDave Hansen out_putname: 32901da177e4SLinus Torvalds putname(from); 32911da177e4SLinus Torvalds return error; 32921da177e4SLinus Torvalds } 32931da177e4SLinus Torvalds 32943480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname) 32955590ff0dSUlrich Drepper { 32965590ff0dSUlrich Drepper return sys_symlinkat(oldname, AT_FDCWD, newname); 32975590ff0dSUlrich Drepper } 32985590ff0dSUlrich Drepper 32991da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) 33001da177e4SLinus Torvalds { 33011da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 33028de52778SAl Viro unsigned max_links = dir->i_sb->s_max_links; 33031da177e4SLinus Torvalds int error; 33041da177e4SLinus Torvalds 33051da177e4SLinus Torvalds if (!inode) 33061da177e4SLinus Torvalds return -ENOENT; 33071da177e4SLinus Torvalds 3308a95164d9SMiklos Szeredi error = may_create(dir, new_dentry); 33091da177e4SLinus Torvalds if (error) 33101da177e4SLinus Torvalds return error; 33111da177e4SLinus Torvalds 33121da177e4SLinus Torvalds if (dir->i_sb != inode->i_sb) 33131da177e4SLinus Torvalds return -EXDEV; 33141da177e4SLinus Torvalds 33151da177e4SLinus Torvalds /* 33161da177e4SLinus Torvalds * A link to an append-only or immutable file cannot be created. 33171da177e4SLinus Torvalds */ 33181da177e4SLinus Torvalds if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) 33191da177e4SLinus Torvalds return -EPERM; 3320acfa4380SAl Viro if (!dir->i_op->link) 33211da177e4SLinus Torvalds return -EPERM; 33227e79eedbSTetsuo Handa if (S_ISDIR(inode->i_mode)) 33231da177e4SLinus Torvalds return -EPERM; 33241da177e4SLinus Torvalds 33251da177e4SLinus Torvalds error = security_inode_link(old_dentry, dir, new_dentry); 33261da177e4SLinus Torvalds if (error) 33271da177e4SLinus Torvalds return error; 33281da177e4SLinus Torvalds 33297e79eedbSTetsuo Handa mutex_lock(&inode->i_mutex); 3330aae8a97dSAneesh Kumar K.V /* Make sure we don't allow creating hardlink to an unlinked file */ 3331aae8a97dSAneesh Kumar K.V if (inode->i_nlink == 0) 3332aae8a97dSAneesh Kumar K.V error = -ENOENT; 33338de52778SAl Viro else if (max_links && inode->i_nlink >= max_links) 33348de52778SAl Viro error = -EMLINK; 3335aae8a97dSAneesh Kumar K.V else 33361da177e4SLinus Torvalds error = dir->i_op->link(old_dentry, dir, new_dentry); 33377e79eedbSTetsuo Handa mutex_unlock(&inode->i_mutex); 3338e31e14ecSStephen Smalley if (!error) 33397e79eedbSTetsuo Handa fsnotify_link(dir, inode, new_dentry); 33401da177e4SLinus Torvalds return error; 33411da177e4SLinus Torvalds } 33421da177e4SLinus Torvalds 33431da177e4SLinus Torvalds /* 33441da177e4SLinus Torvalds * Hardlinks are often used in delicate situations. We avoid 33451da177e4SLinus Torvalds * security-related surprises by not following symlinks on the 33461da177e4SLinus Torvalds * newname. --KAB 33471da177e4SLinus Torvalds * 33481da177e4SLinus Torvalds * We don't follow them on the oldname either to be compatible 33491da177e4SLinus Torvalds * with linux 2.0, and to avoid hard-linking to directories 33501da177e4SLinus Torvalds * and other special files. --ADM 33511da177e4SLinus Torvalds */ 33522e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname, 33532e4d0924SHeiko Carstens int, newdfd, const char __user *, newname, int, flags) 33541da177e4SLinus Torvalds { 33551da177e4SLinus Torvalds struct dentry *new_dentry; 3356dae6ad8fSAl Viro struct path old_path, new_path; 335711a7b371SAneesh Kumar K.V int how = 0; 33581da177e4SLinus Torvalds int error; 33591da177e4SLinus Torvalds 336011a7b371SAneesh Kumar K.V if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0) 3361c04030e1SUlrich Drepper return -EINVAL; 336211a7b371SAneesh Kumar K.V /* 336311a7b371SAneesh Kumar K.V * To use null names we require CAP_DAC_READ_SEARCH 336411a7b371SAneesh Kumar K.V * This ensures that not everyone will be able to create 336511a7b371SAneesh Kumar K.V * handlink using the passed filedescriptor. 336611a7b371SAneesh Kumar K.V */ 336711a7b371SAneesh Kumar K.V if (flags & AT_EMPTY_PATH) { 336811a7b371SAneesh Kumar K.V if (!capable(CAP_DAC_READ_SEARCH)) 336911a7b371SAneesh Kumar K.V return -ENOENT; 337011a7b371SAneesh Kumar K.V how = LOOKUP_EMPTY; 337111a7b371SAneesh Kumar K.V } 3372c04030e1SUlrich Drepper 337311a7b371SAneesh Kumar K.V if (flags & AT_SYMLINK_FOLLOW) 337411a7b371SAneesh Kumar K.V how |= LOOKUP_FOLLOW; 337511a7b371SAneesh Kumar K.V 337611a7b371SAneesh Kumar K.V error = user_path_at(olddfd, oldname, how, &old_path); 33771da177e4SLinus Torvalds if (error) 33782ad94ae6SAl Viro return error; 33792ad94ae6SAl Viro 3380dae6ad8fSAl Viro new_dentry = user_path_create(newdfd, newname, &new_path, 0); 33811da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 33826902d925SDave Hansen if (IS_ERR(new_dentry)) 3383dae6ad8fSAl Viro goto out; 3384dae6ad8fSAl Viro 3385dae6ad8fSAl Viro error = -EXDEV; 3386dae6ad8fSAl Viro if (old_path.mnt != new_path.mnt) 3387dae6ad8fSAl Viro goto out_dput; 3388dae6ad8fSAl Viro error = mnt_want_write(new_path.mnt); 338975c3f29dSDave Hansen if (error) 339075c3f29dSDave Hansen goto out_dput; 3391dae6ad8fSAl Viro error = security_path_link(old_path.dentry, &new_path, new_dentry); 3392be6d3e56SKentaro Takeda if (error) 3393be6d3e56SKentaro Takeda goto out_drop_write; 3394dae6ad8fSAl Viro error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry); 3395be6d3e56SKentaro Takeda out_drop_write: 3396dae6ad8fSAl Viro mnt_drop_write(new_path.mnt); 339775c3f29dSDave Hansen out_dput: 33981da177e4SLinus Torvalds dput(new_dentry); 3399dae6ad8fSAl Viro mutex_unlock(&new_path.dentry->d_inode->i_mutex); 3400dae6ad8fSAl Viro path_put(&new_path); 34011da177e4SLinus Torvalds out: 34022d8f3038SAl Viro path_put(&old_path); 34031da177e4SLinus Torvalds 34041da177e4SLinus Torvalds return error; 34051da177e4SLinus Torvalds } 34061da177e4SLinus Torvalds 34073480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname) 34085590ff0dSUlrich Drepper { 3409c04030e1SUlrich Drepper return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0); 34105590ff0dSUlrich Drepper } 34115590ff0dSUlrich Drepper 34121da177e4SLinus Torvalds /* 34131da177e4SLinus Torvalds * The worst of all namespace operations - renaming directory. "Perverted" 34141da177e4SLinus Torvalds * doesn't even start to describe it. Somebody in UCB had a heck of a trip... 34151da177e4SLinus Torvalds * Problems: 34161da177e4SLinus Torvalds * a) we can get into loop creation. Check is done in is_subdir(). 34171da177e4SLinus Torvalds * b) race potential - two innocent renames can create a loop together. 34181da177e4SLinus Torvalds * That's where 4.4 screws up. Current fix: serialization on 3419a11f3a05SArjan van de Ven * sb->s_vfs_rename_mutex. We might be more accurate, but that's another 34201da177e4SLinus Torvalds * story. 34211da177e4SLinus Torvalds * c) we have to lock _three_ objects - parents and victim (if it exists). 34221b1dcc1bSJes Sorensen * And that - after we got ->i_mutex on parents (until then we don't know 34231da177e4SLinus Torvalds * whether the target exists). Solution: try to be smart with locking 34241da177e4SLinus Torvalds * order for inodes. We rely on the fact that tree topology may change 3425a11f3a05SArjan van de Ven * only under ->s_vfs_rename_mutex _and_ that parent of the object we 34261da177e4SLinus Torvalds * move will be locked. Thus we can rank directories by the tree 34271da177e4SLinus Torvalds * (ancestors first) and rank all non-directories after them. 34281da177e4SLinus Torvalds * That works since everybody except rename does "lock parent, lookup, 3429a11f3a05SArjan van de Ven * lock child" and rename is under ->s_vfs_rename_mutex. 34301da177e4SLinus Torvalds * HOWEVER, it relies on the assumption that any object with ->lookup() 34311da177e4SLinus Torvalds * has no more than 1 dentry. If "hybrid" objects will ever appear, 34321da177e4SLinus Torvalds * we'd better make sure that there's no link(2) for them. 3433e4eaac06SSage Weil * d) conversion from fhandle to dentry may come in the wrong moment - when 34341b1dcc1bSJes Sorensen * we are removing the target. Solution: we will have to grab ->i_mutex 34351da177e4SLinus Torvalds * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on 3436c41b20e7SAdam Buchbinder * ->i_mutex on parents, which works but leads to some truly excessive 34371da177e4SLinus Torvalds * locking]. 34381da177e4SLinus Torvalds */ 343975c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry, 34401da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 34411da177e4SLinus Torvalds { 34421da177e4SLinus Torvalds int error = 0; 34439055cba7SSage Weil struct inode *target = new_dentry->d_inode; 34448de52778SAl Viro unsigned max_links = new_dir->i_sb->s_max_links; 34451da177e4SLinus Torvalds 34461da177e4SLinus Torvalds /* 34471da177e4SLinus Torvalds * If we are going to change the parent - check write permissions, 34481da177e4SLinus Torvalds * we'll need to flip '..'. 34491da177e4SLinus Torvalds */ 34501da177e4SLinus Torvalds if (new_dir != old_dir) { 3451f419a2e3SAl Viro error = inode_permission(old_dentry->d_inode, MAY_WRITE); 34521da177e4SLinus Torvalds if (error) 34531da177e4SLinus Torvalds return error; 34541da177e4SLinus Torvalds } 34551da177e4SLinus Torvalds 34561da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 34571da177e4SLinus Torvalds if (error) 34581da177e4SLinus Torvalds return error; 34591da177e4SLinus Torvalds 34601d2ef590SAl Viro dget(new_dentry); 3461d83c49f3SAl Viro if (target) 34621b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 34639055cba7SSage Weil 34641da177e4SLinus Torvalds error = -EBUSY; 34659055cba7SSage Weil if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry)) 34669055cba7SSage Weil goto out; 34679055cba7SSage Weil 34688de52778SAl Viro error = -EMLINK; 34698de52778SAl Viro if (max_links && !target && new_dir != old_dir && 34708de52778SAl Viro new_dir->i_nlink >= max_links) 34718de52778SAl Viro goto out; 34728de52778SAl Viro 34733cebde24SSage Weil if (target) 34743cebde24SSage Weil shrink_dcache_parent(new_dentry); 34751da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 34769055cba7SSage Weil if (error) 34779055cba7SSage Weil goto out; 34789055cba7SSage Weil 34791da177e4SLinus Torvalds if (target) { 34801da177e4SLinus Torvalds target->i_flags |= S_DEAD; 3481d83c49f3SAl Viro dont_mount(new_dentry); 3482d83c49f3SAl Viro } 34839055cba7SSage Weil out: 34849055cba7SSage Weil if (target) 34851b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 34861d2ef590SAl Viro dput(new_dentry); 3487e31e14ecSStephen Smalley if (!error) 3488349457ccSMark Fasheh if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) 34891da177e4SLinus Torvalds d_move(old_dentry,new_dentry); 34901da177e4SLinus Torvalds return error; 34911da177e4SLinus Torvalds } 34921da177e4SLinus Torvalds 349375c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry, 34941da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 34951da177e4SLinus Torvalds { 349651892bbbSSage Weil struct inode *target = new_dentry->d_inode; 34971da177e4SLinus Torvalds int error; 34981da177e4SLinus Torvalds 34991da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 35001da177e4SLinus Torvalds if (error) 35011da177e4SLinus Torvalds return error; 35021da177e4SLinus Torvalds 35031da177e4SLinus Torvalds dget(new_dentry); 35041da177e4SLinus Torvalds if (target) 35051b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 350651892bbbSSage Weil 35071da177e4SLinus Torvalds error = -EBUSY; 350851892bbbSSage Weil if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry)) 350951892bbbSSage Weil goto out; 351051892bbbSSage Weil 35111da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 351251892bbbSSage Weil if (error) 351351892bbbSSage Weil goto out; 351451892bbbSSage Weil 3515bec1052eSAl Viro if (target) 3516d83c49f3SAl Viro dont_mount(new_dentry); 3517349457ccSMark Fasheh if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) 35181da177e4SLinus Torvalds d_move(old_dentry, new_dentry); 351951892bbbSSage Weil out: 35201da177e4SLinus Torvalds if (target) 35211b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 35221da177e4SLinus Torvalds dput(new_dentry); 35231da177e4SLinus Torvalds return error; 35241da177e4SLinus Torvalds } 35251da177e4SLinus Torvalds 35261da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry, 35271da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 35281da177e4SLinus Torvalds { 35291da177e4SLinus Torvalds int error; 35301da177e4SLinus Torvalds int is_dir = S_ISDIR(old_dentry->d_inode->i_mode); 353159b0df21SEric Paris const unsigned char *old_name; 35321da177e4SLinus Torvalds 35331da177e4SLinus Torvalds if (old_dentry->d_inode == new_dentry->d_inode) 35341da177e4SLinus Torvalds return 0; 35351da177e4SLinus Torvalds 35361da177e4SLinus Torvalds error = may_delete(old_dir, old_dentry, is_dir); 35371da177e4SLinus Torvalds if (error) 35381da177e4SLinus Torvalds return error; 35391da177e4SLinus Torvalds 35401da177e4SLinus Torvalds if (!new_dentry->d_inode) 3541a95164d9SMiklos Szeredi error = may_create(new_dir, new_dentry); 35421da177e4SLinus Torvalds else 35431da177e4SLinus Torvalds error = may_delete(new_dir, new_dentry, is_dir); 35441da177e4SLinus Torvalds if (error) 35451da177e4SLinus Torvalds return error; 35461da177e4SLinus Torvalds 3547acfa4380SAl Viro if (!old_dir->i_op->rename) 35481da177e4SLinus Torvalds return -EPERM; 35491da177e4SLinus Torvalds 35500eeca283SRobert Love old_name = fsnotify_oldname_init(old_dentry->d_name.name); 35510eeca283SRobert Love 35521da177e4SLinus Torvalds if (is_dir) 35531da177e4SLinus Torvalds error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry); 35541da177e4SLinus Torvalds else 35551da177e4SLinus Torvalds error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry); 3556123df294SAl Viro if (!error) 3557123df294SAl Viro fsnotify_move(old_dir, new_dir, old_name, is_dir, 35585a190ae6SAl Viro new_dentry->d_inode, old_dentry); 35590eeca283SRobert Love fsnotify_oldname_free(old_name); 35600eeca283SRobert Love 35611da177e4SLinus Torvalds return error; 35621da177e4SLinus Torvalds } 35631da177e4SLinus Torvalds 35642e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname, 35652e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 35661da177e4SLinus Torvalds { 35671da177e4SLinus Torvalds struct dentry *old_dir, *new_dir; 35681da177e4SLinus Torvalds struct dentry *old_dentry, *new_dentry; 35691da177e4SLinus Torvalds struct dentry *trap; 35701da177e4SLinus Torvalds struct nameidata oldnd, newnd; 35712ad94ae6SAl Viro char *from; 35722ad94ae6SAl Viro char *to; 35732ad94ae6SAl Viro int error; 35741da177e4SLinus Torvalds 35752ad94ae6SAl Viro error = user_path_parent(olddfd, oldname, &oldnd, &from); 35761da177e4SLinus Torvalds if (error) 35771da177e4SLinus Torvalds goto exit; 35781da177e4SLinus Torvalds 35792ad94ae6SAl Viro error = user_path_parent(newdfd, newname, &newnd, &to); 35801da177e4SLinus Torvalds if (error) 35811da177e4SLinus Torvalds goto exit1; 35821da177e4SLinus Torvalds 35831da177e4SLinus Torvalds error = -EXDEV; 35844ac91378SJan Blunck if (oldnd.path.mnt != newnd.path.mnt) 35851da177e4SLinus Torvalds goto exit2; 35861da177e4SLinus Torvalds 35874ac91378SJan Blunck old_dir = oldnd.path.dentry; 35881da177e4SLinus Torvalds error = -EBUSY; 35891da177e4SLinus Torvalds if (oldnd.last_type != LAST_NORM) 35901da177e4SLinus Torvalds goto exit2; 35911da177e4SLinus Torvalds 35924ac91378SJan Blunck new_dir = newnd.path.dentry; 35931da177e4SLinus Torvalds if (newnd.last_type != LAST_NORM) 35941da177e4SLinus Torvalds goto exit2; 35951da177e4SLinus Torvalds 35960612d9fbSOGAWA Hirofumi oldnd.flags &= ~LOOKUP_PARENT; 35970612d9fbSOGAWA Hirofumi newnd.flags &= ~LOOKUP_PARENT; 35984e9ed2f8SOGAWA Hirofumi newnd.flags |= LOOKUP_RENAME_TARGET; 35990612d9fbSOGAWA Hirofumi 36001da177e4SLinus Torvalds trap = lock_rename(new_dir, old_dir); 36011da177e4SLinus Torvalds 360249705b77SChristoph Hellwig old_dentry = lookup_hash(&oldnd); 36031da177e4SLinus Torvalds error = PTR_ERR(old_dentry); 36041da177e4SLinus Torvalds if (IS_ERR(old_dentry)) 36051da177e4SLinus Torvalds goto exit3; 36061da177e4SLinus Torvalds /* source must exist */ 36071da177e4SLinus Torvalds error = -ENOENT; 36081da177e4SLinus Torvalds if (!old_dentry->d_inode) 36091da177e4SLinus Torvalds goto exit4; 36101da177e4SLinus Torvalds /* unless the source is a directory trailing slashes give -ENOTDIR */ 36111da177e4SLinus Torvalds if (!S_ISDIR(old_dentry->d_inode->i_mode)) { 36121da177e4SLinus Torvalds error = -ENOTDIR; 36131da177e4SLinus Torvalds if (oldnd.last.name[oldnd.last.len]) 36141da177e4SLinus Torvalds goto exit4; 36151da177e4SLinus Torvalds if (newnd.last.name[newnd.last.len]) 36161da177e4SLinus Torvalds goto exit4; 36171da177e4SLinus Torvalds } 36181da177e4SLinus Torvalds /* source should not be ancestor of target */ 36191da177e4SLinus Torvalds error = -EINVAL; 36201da177e4SLinus Torvalds if (old_dentry == trap) 36211da177e4SLinus Torvalds goto exit4; 362249705b77SChristoph Hellwig new_dentry = lookup_hash(&newnd); 36231da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 36241da177e4SLinus Torvalds if (IS_ERR(new_dentry)) 36251da177e4SLinus Torvalds goto exit4; 36261da177e4SLinus Torvalds /* target should not be an ancestor of source */ 36271da177e4SLinus Torvalds error = -ENOTEMPTY; 36281da177e4SLinus Torvalds if (new_dentry == trap) 36291da177e4SLinus Torvalds goto exit5; 36301da177e4SLinus Torvalds 36319079b1ebSDave Hansen error = mnt_want_write(oldnd.path.mnt); 36329079b1ebSDave Hansen if (error) 36339079b1ebSDave Hansen goto exit5; 3634be6d3e56SKentaro Takeda error = security_path_rename(&oldnd.path, old_dentry, 3635be6d3e56SKentaro Takeda &newnd.path, new_dentry); 3636be6d3e56SKentaro Takeda if (error) 3637be6d3e56SKentaro Takeda goto exit6; 36381da177e4SLinus Torvalds error = vfs_rename(old_dir->d_inode, old_dentry, 36391da177e4SLinus Torvalds new_dir->d_inode, new_dentry); 3640be6d3e56SKentaro Takeda exit6: 36419079b1ebSDave Hansen mnt_drop_write(oldnd.path.mnt); 36421da177e4SLinus Torvalds exit5: 36431da177e4SLinus Torvalds dput(new_dentry); 36441da177e4SLinus Torvalds exit4: 36451da177e4SLinus Torvalds dput(old_dentry); 36461da177e4SLinus Torvalds exit3: 36471da177e4SLinus Torvalds unlock_rename(new_dir, old_dir); 36481da177e4SLinus Torvalds exit2: 36491d957f9bSJan Blunck path_put(&newnd.path); 36502ad94ae6SAl Viro putname(to); 36511da177e4SLinus Torvalds exit1: 36521d957f9bSJan Blunck path_put(&oldnd.path); 36531da177e4SLinus Torvalds putname(from); 36542ad94ae6SAl Viro exit: 36551da177e4SLinus Torvalds return error; 36561da177e4SLinus Torvalds } 36571da177e4SLinus Torvalds 3658a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname) 36595590ff0dSUlrich Drepper { 36605590ff0dSUlrich Drepper return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname); 36615590ff0dSUlrich Drepper } 36625590ff0dSUlrich Drepper 36631da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link) 36641da177e4SLinus Torvalds { 36651da177e4SLinus Torvalds int len; 36661da177e4SLinus Torvalds 36671da177e4SLinus Torvalds len = PTR_ERR(link); 36681da177e4SLinus Torvalds if (IS_ERR(link)) 36691da177e4SLinus Torvalds goto out; 36701da177e4SLinus Torvalds 36711da177e4SLinus Torvalds len = strlen(link); 36721da177e4SLinus Torvalds if (len > (unsigned) buflen) 36731da177e4SLinus Torvalds len = buflen; 36741da177e4SLinus Torvalds if (copy_to_user(buffer, link, len)) 36751da177e4SLinus Torvalds len = -EFAULT; 36761da177e4SLinus Torvalds out: 36771da177e4SLinus Torvalds return len; 36781da177e4SLinus Torvalds } 36791da177e4SLinus Torvalds 36801da177e4SLinus Torvalds /* 36811da177e4SLinus Torvalds * A helper for ->readlink(). This should be used *ONLY* for symlinks that 36821da177e4SLinus Torvalds * have ->follow_link() touching nd only in nd_set_link(). Using (or not 36831da177e4SLinus Torvalds * using) it for any given inode is up to filesystem. 36841da177e4SLinus Torvalds */ 36851da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen) 36861da177e4SLinus Torvalds { 36871da177e4SLinus Torvalds struct nameidata nd; 3688cc314eefSLinus Torvalds void *cookie; 3689694a1764SMarcin Slusarz int res; 3690cc314eefSLinus Torvalds 36911da177e4SLinus Torvalds nd.depth = 0; 3692cc314eefSLinus Torvalds cookie = dentry->d_inode->i_op->follow_link(dentry, &nd); 3693694a1764SMarcin Slusarz if (IS_ERR(cookie)) 3694694a1764SMarcin Slusarz return PTR_ERR(cookie); 3695694a1764SMarcin Slusarz 3696694a1764SMarcin Slusarz res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd)); 36971da177e4SLinus Torvalds if (dentry->d_inode->i_op->put_link) 3698cc314eefSLinus Torvalds dentry->d_inode->i_op->put_link(dentry, &nd, cookie); 3699694a1764SMarcin Slusarz return res; 37001da177e4SLinus Torvalds } 37011da177e4SLinus Torvalds 37021da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link) 37031da177e4SLinus Torvalds { 37041da177e4SLinus Torvalds return __vfs_follow_link(nd, link); 37051da177e4SLinus Torvalds } 37061da177e4SLinus Torvalds 37071da177e4SLinus Torvalds /* get the link contents into pagecache */ 37081da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage) 37091da177e4SLinus Torvalds { 3710ebd09abbSDuane Griffin char *kaddr; 37111da177e4SLinus Torvalds struct page *page; 37121da177e4SLinus Torvalds struct address_space *mapping = dentry->d_inode->i_mapping; 3713090d2b18SPekka Enberg page = read_mapping_page(mapping, 0, NULL); 37141da177e4SLinus Torvalds if (IS_ERR(page)) 37156fe6900eSNick Piggin return (char*)page; 37161da177e4SLinus Torvalds *ppage = page; 3717ebd09abbSDuane Griffin kaddr = kmap(page); 3718ebd09abbSDuane Griffin nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1); 3719ebd09abbSDuane Griffin return kaddr; 37201da177e4SLinus Torvalds } 37211da177e4SLinus Torvalds 37221da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen) 37231da177e4SLinus Torvalds { 37241da177e4SLinus Torvalds struct page *page = NULL; 37251da177e4SLinus Torvalds char *s = page_getlink(dentry, &page); 37261da177e4SLinus Torvalds int res = vfs_readlink(dentry,buffer,buflen,s); 37271da177e4SLinus Torvalds if (page) { 37281da177e4SLinus Torvalds kunmap(page); 37291da177e4SLinus Torvalds page_cache_release(page); 37301da177e4SLinus Torvalds } 37311da177e4SLinus Torvalds return res; 37321da177e4SLinus Torvalds } 37331da177e4SLinus Torvalds 3734cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd) 37351da177e4SLinus Torvalds { 3736cc314eefSLinus Torvalds struct page *page = NULL; 37371da177e4SLinus Torvalds nd_set_link(nd, page_getlink(dentry, &page)); 3738cc314eefSLinus Torvalds return page; 37391da177e4SLinus Torvalds } 37401da177e4SLinus Torvalds 3741cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie) 37421da177e4SLinus Torvalds { 3743cc314eefSLinus Torvalds struct page *page = cookie; 3744cc314eefSLinus Torvalds 3745cc314eefSLinus Torvalds if (page) { 37461da177e4SLinus Torvalds kunmap(page); 37471da177e4SLinus Torvalds page_cache_release(page); 37481da177e4SLinus Torvalds } 37491da177e4SLinus Torvalds } 37501da177e4SLinus Torvalds 375154566b2cSNick Piggin /* 375254566b2cSNick Piggin * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS 375354566b2cSNick Piggin */ 375454566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs) 37551da177e4SLinus Torvalds { 37561da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 37570adb25d2SKirill Korotaev struct page *page; 3758afddba49SNick Piggin void *fsdata; 3759beb497abSDmitriy Monakhov int err; 37601da177e4SLinus Torvalds char *kaddr; 376154566b2cSNick Piggin unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE; 376254566b2cSNick Piggin if (nofs) 376354566b2cSNick Piggin flags |= AOP_FLAG_NOFS; 37641da177e4SLinus Torvalds 37657e53cac4SNeilBrown retry: 3766afddba49SNick Piggin err = pagecache_write_begin(NULL, mapping, 0, len-1, 376754566b2cSNick Piggin flags, &page, &fsdata); 37681da177e4SLinus Torvalds if (err) 3769afddba49SNick Piggin goto fail; 3770afddba49SNick Piggin 3771e8e3c3d6SCong Wang kaddr = kmap_atomic(page); 37721da177e4SLinus Torvalds memcpy(kaddr, symname, len-1); 3773e8e3c3d6SCong Wang kunmap_atomic(kaddr); 3774afddba49SNick Piggin 3775afddba49SNick Piggin err = pagecache_write_end(NULL, mapping, 0, len-1, len-1, 3776afddba49SNick Piggin page, fsdata); 37771da177e4SLinus Torvalds if (err < 0) 37781da177e4SLinus Torvalds goto fail; 3779afddba49SNick Piggin if (err < len-1) 3780afddba49SNick Piggin goto retry; 3781afddba49SNick Piggin 37821da177e4SLinus Torvalds mark_inode_dirty(inode); 37831da177e4SLinus Torvalds return 0; 37841da177e4SLinus Torvalds fail: 37851da177e4SLinus Torvalds return err; 37861da177e4SLinus Torvalds } 37871da177e4SLinus Torvalds 37880adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len) 37890adb25d2SKirill Korotaev { 37900adb25d2SKirill Korotaev return __page_symlink(inode, symname, len, 379154566b2cSNick Piggin !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS)); 37920adb25d2SKirill Korotaev } 37930adb25d2SKirill Korotaev 379492e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = { 37951da177e4SLinus Torvalds .readlink = generic_readlink, 37961da177e4SLinus Torvalds .follow_link = page_follow_link_light, 37971da177e4SLinus Torvalds .put_link = page_put_link, 37981da177e4SLinus Torvalds }; 37991da177e4SLinus Torvalds 38002d8f3038SAl Viro EXPORT_SYMBOL(user_path_at); 3801cc53ce53SDavid Howells EXPORT_SYMBOL(follow_down_one); 38021da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down); 38031da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up); 38041da177e4SLinus Torvalds EXPORT_SYMBOL(get_write_access); /* binfmt_aout */ 38051da177e4SLinus Torvalds EXPORT_SYMBOL(getname); 38061da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename); 38071da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len); 38081da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light); 38091da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link); 38101da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink); 38110adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink); 38121da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink); 38131da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations); 3814d1811465SAl Viro EXPORT_SYMBOL(kern_path); 381516f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup); 3816f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission); 38171da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename); 38181da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create); 38191da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link); 38201da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link); 38211da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir); 38221da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod); 38231da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission); 38241da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink); 38251da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename); 38261da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir); 38271da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink); 38281da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink); 38291da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash); 38301da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink); 3831