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 589b5fb63c1SChristoph Hellwig /* 590b5fb63c1SChristoph Hellwig * Helper to directly jump to a known parsed path from ->follow_link, 591b5fb63c1SChristoph Hellwig * caller must have taken a reference to path beforehand. 592b5fb63c1SChristoph Hellwig */ 593b5fb63c1SChristoph Hellwig void nd_jump_link(struct nameidata *nd, struct path *path) 594b5fb63c1SChristoph Hellwig { 595b5fb63c1SChristoph Hellwig path_put(&nd->path); 596b5fb63c1SChristoph Hellwig 597b5fb63c1SChristoph Hellwig nd->path = *path; 598b5fb63c1SChristoph Hellwig nd->inode = nd->path.dentry->d_inode; 599b5fb63c1SChristoph Hellwig nd->flags |= LOOKUP_JUMPED; 600b5fb63c1SChristoph Hellwig 601b5fb63c1SChristoph Hellwig BUG_ON(nd->inode->i_op->follow_link); 602b5fb63c1SChristoph Hellwig } 603b5fb63c1SChristoph Hellwig 604574197e0SAl Viro static inline void put_link(struct nameidata *nd, struct path *link, void *cookie) 605574197e0SAl Viro { 606574197e0SAl Viro struct inode *inode = link->dentry->d_inode; 6076d7b5aaeSAl Viro if (inode->i_op->put_link) 608574197e0SAl Viro inode->i_op->put_link(link->dentry, nd, cookie); 609574197e0SAl Viro path_put(link); 610574197e0SAl Viro } 611574197e0SAl Viro 612def4af30SAl Viro static __always_inline int 613574197e0SAl Viro follow_link(struct path *link, struct nameidata *nd, void **p) 6141da177e4SLinus Torvalds { 6157b9337aaSNick Piggin struct dentry *dentry = link->dentry; 6166d7b5aaeSAl Viro int error; 6176d7b5aaeSAl Viro char *s; 6181da177e4SLinus Torvalds 619844a3917SAl Viro BUG_ON(nd->flags & LOOKUP_RCU); 620844a3917SAl Viro 6210e794589SAl Viro if (link->mnt == nd->path.mnt) 6220e794589SAl Viro mntget(link->mnt); 6230e794589SAl Viro 6246d7b5aaeSAl Viro error = -ELOOP; 6256d7b5aaeSAl Viro if (unlikely(current->total_link_count >= 40)) 6266d7b5aaeSAl Viro goto out_put_nd_path; 6276d7b5aaeSAl Viro 628574197e0SAl Viro cond_resched(); 629574197e0SAl Viro current->total_link_count++; 630574197e0SAl Viro 63168ac1234SAl Viro touch_atime(link); 6321da177e4SLinus Torvalds nd_set_link(nd, NULL); 633cd4e91d3SAl Viro 63436f3b4f6SAl Viro error = security_inode_follow_link(link->dentry, nd); 6356d7b5aaeSAl Viro if (error) 6366d7b5aaeSAl Viro goto out_put_nd_path; 63736f3b4f6SAl Viro 63886acdca1SAl Viro nd->last_type = LAST_BIND; 639def4af30SAl Viro *p = dentry->d_inode->i_op->follow_link(dentry, nd); 640def4af30SAl Viro error = PTR_ERR(*p); 6416d7b5aaeSAl Viro if (IS_ERR(*p)) 642408ef013SChristoph Hellwig goto out_put_nd_path; 6436d7b5aaeSAl Viro 644cc314eefSLinus Torvalds error = 0; 6456d7b5aaeSAl Viro s = nd_get_link(nd); 6466d7b5aaeSAl Viro if (s) { 6471da177e4SLinus Torvalds error = __vfs_follow_link(nd, s); 6486d7b5aaeSAl Viro if (unlikely(error)) 6496d7b5aaeSAl Viro put_link(nd, link, *p); 650b5fb63c1SChristoph Hellwig } 6516d7b5aaeSAl Viro 6526d7b5aaeSAl Viro return error; 6536d7b5aaeSAl Viro 6546d7b5aaeSAl Viro out_put_nd_path: 6556d7b5aaeSAl Viro path_put(&nd->path); 6566d7b5aaeSAl Viro path_put(link); 6571da177e4SLinus Torvalds return error; 6581da177e4SLinus Torvalds } 6591da177e4SLinus Torvalds 66031e6b01fSNick Piggin static int follow_up_rcu(struct path *path) 66131e6b01fSNick Piggin { 6620714a533SAl Viro struct mount *mnt = real_mount(path->mnt); 6630714a533SAl Viro struct mount *parent; 66431e6b01fSNick Piggin struct dentry *mountpoint; 66531e6b01fSNick Piggin 6660714a533SAl Viro parent = mnt->mnt_parent; 6670714a533SAl Viro if (&parent->mnt == path->mnt) 66831e6b01fSNick Piggin return 0; 669a73324daSAl Viro mountpoint = mnt->mnt_mountpoint; 67031e6b01fSNick Piggin path->dentry = mountpoint; 6710714a533SAl Viro path->mnt = &parent->mnt; 67231e6b01fSNick Piggin return 1; 67331e6b01fSNick Piggin } 67431e6b01fSNick Piggin 675bab77ebfSAl Viro int follow_up(struct path *path) 6761da177e4SLinus Torvalds { 6770714a533SAl Viro struct mount *mnt = real_mount(path->mnt); 6780714a533SAl Viro struct mount *parent; 6791da177e4SLinus Torvalds struct dentry *mountpoint; 68099b7db7bSNick Piggin 681962830dfSAndi Kleen br_read_lock(&vfsmount_lock); 6820714a533SAl Viro parent = mnt->mnt_parent; 6830714a533SAl Viro if (&parent->mnt == path->mnt) { 684962830dfSAndi Kleen br_read_unlock(&vfsmount_lock); 6851da177e4SLinus Torvalds return 0; 6861da177e4SLinus Torvalds } 6870714a533SAl Viro mntget(&parent->mnt); 688a73324daSAl Viro mountpoint = dget(mnt->mnt_mountpoint); 689962830dfSAndi Kleen br_read_unlock(&vfsmount_lock); 690bab77ebfSAl Viro dput(path->dentry); 691bab77ebfSAl Viro path->dentry = mountpoint; 692bab77ebfSAl Viro mntput(path->mnt); 6930714a533SAl Viro path->mnt = &parent->mnt; 6941da177e4SLinus Torvalds return 1; 6951da177e4SLinus Torvalds } 6961da177e4SLinus Torvalds 697b5c84bf6SNick Piggin /* 6989875cf80SDavid Howells * Perform an automount 6999875cf80SDavid Howells * - return -EISDIR to tell follow_managed() to stop and return the path we 7009875cf80SDavid Howells * were called with. 7011da177e4SLinus Torvalds */ 7029875cf80SDavid Howells static int follow_automount(struct path *path, unsigned flags, 7039875cf80SDavid Howells bool *need_mntput) 70431e6b01fSNick Piggin { 7059875cf80SDavid Howells struct vfsmount *mnt; 706ea5b778aSDavid Howells int err; 7079875cf80SDavid Howells 7089875cf80SDavid Howells if (!path->dentry->d_op || !path->dentry->d_op->d_automount) 7099875cf80SDavid Howells return -EREMOTE; 7109875cf80SDavid Howells 7110ec26fd0SMiklos Szeredi /* We don't want to mount if someone's just doing a stat - 7120ec26fd0SMiklos Szeredi * unless they're stat'ing a directory and appended a '/' to 7130ec26fd0SMiklos Szeredi * the name. 7140ec26fd0SMiklos Szeredi * 7150ec26fd0SMiklos Szeredi * We do, however, want to mount if someone wants to open or 7165a30d8a2SDavid Howells * create a file of any type under the mountpoint, wants to 7170ec26fd0SMiklos Szeredi * traverse through the mountpoint or wants to open the 7180ec26fd0SMiklos Szeredi * mounted directory. Also, autofs may mark negative dentries 7190ec26fd0SMiklos Szeredi * as being automount points. These will need the attentions 7200ec26fd0SMiklos Szeredi * of the daemon to instantiate them before they can be used. 7215a30d8a2SDavid Howells */ 7225a30d8a2SDavid Howells if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY | 723d94c177bSLinus Torvalds LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) && 7245a30d8a2SDavid Howells path->dentry->d_inode) 7259875cf80SDavid Howells return -EISDIR; 7260ec26fd0SMiklos Szeredi 7279875cf80SDavid Howells current->total_link_count++; 7289875cf80SDavid Howells if (current->total_link_count >= 40) 7299875cf80SDavid Howells return -ELOOP; 7309875cf80SDavid Howells 7319875cf80SDavid Howells mnt = path->dentry->d_op->d_automount(path); 7329875cf80SDavid Howells if (IS_ERR(mnt)) { 7339875cf80SDavid Howells /* 7349875cf80SDavid Howells * The filesystem is allowed to return -EISDIR here to indicate 7359875cf80SDavid Howells * it doesn't want to automount. For instance, autofs would do 7369875cf80SDavid Howells * this so that its userspace daemon can mount on this dentry. 7379875cf80SDavid Howells * 7389875cf80SDavid Howells * However, we can only permit this if it's a terminal point in 7399875cf80SDavid Howells * the path being looked up; if it wasn't then the remainder of 7409875cf80SDavid Howells * the path is inaccessible and we should say so. 7419875cf80SDavid Howells */ 74249084c3bSAl Viro if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT)) 7439875cf80SDavid Howells return -EREMOTE; 7449875cf80SDavid Howells return PTR_ERR(mnt); 74531e6b01fSNick Piggin } 746ea5b778aSDavid Howells 7479875cf80SDavid Howells if (!mnt) /* mount collision */ 7489875cf80SDavid Howells return 0; 7499875cf80SDavid Howells 7508aef1884SAl Viro if (!*need_mntput) { 7518aef1884SAl Viro /* lock_mount() may release path->mnt on error */ 7528aef1884SAl Viro mntget(path->mnt); 7538aef1884SAl Viro *need_mntput = true; 7548aef1884SAl Viro } 75519a167afSAl Viro err = finish_automount(mnt, path); 756ea5b778aSDavid Howells 757ea5b778aSDavid Howells switch (err) { 758ea5b778aSDavid Howells case -EBUSY: 759ea5b778aSDavid Howells /* Someone else made a mount here whilst we were busy */ 76019a167afSAl Viro return 0; 761ea5b778aSDavid Howells case 0: 7628aef1884SAl Viro path_put(path); 7639875cf80SDavid Howells path->mnt = mnt; 7649875cf80SDavid Howells path->dentry = dget(mnt->mnt_root); 7659875cf80SDavid Howells return 0; 76619a167afSAl Viro default: 76719a167afSAl Viro return err; 7689875cf80SDavid Howells } 76919a167afSAl Viro 770ea5b778aSDavid Howells } 7719875cf80SDavid Howells 7729875cf80SDavid Howells /* 7739875cf80SDavid Howells * Handle a dentry that is managed in some way. 774cc53ce53SDavid Howells * - Flagged for transit management (autofs) 7759875cf80SDavid Howells * - Flagged as mountpoint 7769875cf80SDavid Howells * - Flagged as automount point 7779875cf80SDavid Howells * 7789875cf80SDavid Howells * This may only be called in refwalk mode. 7799875cf80SDavid Howells * 7809875cf80SDavid Howells * Serialization is taken care of in namespace.c 7819875cf80SDavid Howells */ 7829875cf80SDavid Howells static int follow_managed(struct path *path, unsigned flags) 7839875cf80SDavid Howells { 7848aef1884SAl Viro struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */ 7859875cf80SDavid Howells unsigned managed; 7869875cf80SDavid Howells bool need_mntput = false; 7878aef1884SAl Viro int ret = 0; 7889875cf80SDavid Howells 7899875cf80SDavid Howells /* Given that we're not holding a lock here, we retain the value in a 7909875cf80SDavid Howells * local variable for each dentry as we look at it so that we don't see 7919875cf80SDavid Howells * the components of that value change under us */ 7929875cf80SDavid Howells while (managed = ACCESS_ONCE(path->dentry->d_flags), 7939875cf80SDavid Howells managed &= DCACHE_MANAGED_DENTRY, 7949875cf80SDavid Howells unlikely(managed != 0)) { 795cc53ce53SDavid Howells /* Allow the filesystem to manage the transit without i_mutex 796cc53ce53SDavid Howells * being held. */ 797cc53ce53SDavid Howells if (managed & DCACHE_MANAGE_TRANSIT) { 798cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op); 799cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op->d_manage); 8001aed3e42SAl Viro ret = path->dentry->d_op->d_manage(path->dentry, false); 801cc53ce53SDavid Howells if (ret < 0) 8028aef1884SAl Viro break; 803cc53ce53SDavid Howells } 804cc53ce53SDavid Howells 8059875cf80SDavid Howells /* Transit to a mounted filesystem. */ 8069875cf80SDavid Howells if (managed & DCACHE_MOUNTED) { 8079875cf80SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 8089875cf80SDavid Howells if (mounted) { 8099875cf80SDavid Howells dput(path->dentry); 8109875cf80SDavid Howells if (need_mntput) 811463ffb2eSAl Viro mntput(path->mnt); 812463ffb2eSAl Viro path->mnt = mounted; 813463ffb2eSAl Viro path->dentry = dget(mounted->mnt_root); 8149875cf80SDavid Howells need_mntput = true; 8159875cf80SDavid Howells continue; 816463ffb2eSAl Viro } 817463ffb2eSAl Viro 8189875cf80SDavid Howells /* Something is mounted on this dentry in another 8199875cf80SDavid Howells * namespace and/or whatever was mounted there in this 8209875cf80SDavid Howells * namespace got unmounted before we managed to get the 8219875cf80SDavid Howells * vfsmount_lock */ 8221da177e4SLinus Torvalds } 8239875cf80SDavid Howells 8249875cf80SDavid Howells /* Handle an automount point */ 8259875cf80SDavid Howells if (managed & DCACHE_NEED_AUTOMOUNT) { 8269875cf80SDavid Howells ret = follow_automount(path, flags, &need_mntput); 8279875cf80SDavid Howells if (ret < 0) 8288aef1884SAl Viro break; 8299875cf80SDavid Howells continue; 8309875cf80SDavid Howells } 8319875cf80SDavid Howells 8329875cf80SDavid Howells /* We didn't change the current path point */ 8339875cf80SDavid Howells break; 8349875cf80SDavid Howells } 8358aef1884SAl Viro 8368aef1884SAl Viro if (need_mntput && path->mnt == mnt) 8378aef1884SAl Viro mntput(path->mnt); 8388aef1884SAl Viro if (ret == -EISDIR) 8398aef1884SAl Viro ret = 0; 840a3fbbde7SAl Viro return ret < 0 ? ret : need_mntput; 8411da177e4SLinus Torvalds } 8421da177e4SLinus Torvalds 843cc53ce53SDavid Howells int follow_down_one(struct path *path) 8441da177e4SLinus Torvalds { 8451da177e4SLinus Torvalds struct vfsmount *mounted; 8461da177e4SLinus Torvalds 8471c755af4SAl Viro mounted = lookup_mnt(path); 8481da177e4SLinus Torvalds if (mounted) { 8499393bd07SAl Viro dput(path->dentry); 8509393bd07SAl Viro mntput(path->mnt); 8519393bd07SAl Viro path->mnt = mounted; 8529393bd07SAl Viro path->dentry = dget(mounted->mnt_root); 8531da177e4SLinus Torvalds return 1; 8541da177e4SLinus Torvalds } 8551da177e4SLinus Torvalds return 0; 8561da177e4SLinus Torvalds } 8571da177e4SLinus Torvalds 85862a7375eSIan Kent static inline bool managed_dentry_might_block(struct dentry *dentry) 85962a7375eSIan Kent { 86062a7375eSIan Kent return (dentry->d_flags & DCACHE_MANAGE_TRANSIT && 86162a7375eSIan Kent dentry->d_op->d_manage(dentry, true) < 0); 86262a7375eSIan Kent } 86362a7375eSIan Kent 8649875cf80SDavid Howells /* 865287548e4SAl Viro * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if 866287548e4SAl Viro * we meet a managed dentry that would need blocking. 8679875cf80SDavid Howells */ 8689875cf80SDavid Howells static bool __follow_mount_rcu(struct nameidata *nd, struct path *path, 869287548e4SAl Viro struct inode **inode) 8709875cf80SDavid Howells { 87162a7375eSIan Kent for (;;) { 872c7105365SAl Viro struct mount *mounted; 87362a7375eSIan Kent /* 87462a7375eSIan Kent * Don't forget we might have a non-mountpoint managed dentry 87562a7375eSIan Kent * that wants to block transit. 87662a7375eSIan Kent */ 877287548e4SAl Viro if (unlikely(managed_dentry_might_block(path->dentry))) 878ab90911fSDavid Howells return false; 87962a7375eSIan Kent 88062a7375eSIan Kent if (!d_mountpoint(path->dentry)) 88162a7375eSIan Kent break; 88262a7375eSIan Kent 8839875cf80SDavid Howells mounted = __lookup_mnt(path->mnt, path->dentry, 1); 8849875cf80SDavid Howells if (!mounted) 8859875cf80SDavid Howells break; 886c7105365SAl Viro path->mnt = &mounted->mnt; 887c7105365SAl Viro path->dentry = mounted->mnt.mnt_root; 888a3fbbde7SAl Viro nd->flags |= LOOKUP_JUMPED; 8899875cf80SDavid Howells nd->seq = read_seqcount_begin(&path->dentry->d_seq); 89059430262SLinus Torvalds /* 89159430262SLinus Torvalds * Update the inode too. We don't need to re-check the 89259430262SLinus Torvalds * dentry sequence number here after this d_inode read, 89359430262SLinus Torvalds * because a mount-point is always pinned. 89459430262SLinus Torvalds */ 89559430262SLinus Torvalds *inode = path->dentry->d_inode; 8969875cf80SDavid Howells } 8979875cf80SDavid Howells return true; 8989875cf80SDavid Howells } 8999875cf80SDavid Howells 900dea39376SAl Viro static void follow_mount_rcu(struct nameidata *nd) 901287548e4SAl Viro { 902dea39376SAl Viro while (d_mountpoint(nd->path.dentry)) { 903c7105365SAl Viro struct mount *mounted; 904dea39376SAl Viro mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1); 905287548e4SAl Viro if (!mounted) 906287548e4SAl Viro break; 907c7105365SAl Viro nd->path.mnt = &mounted->mnt; 908c7105365SAl Viro nd->path.dentry = mounted->mnt.mnt_root; 909dea39376SAl Viro nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); 910287548e4SAl Viro } 911287548e4SAl Viro } 912287548e4SAl Viro 91331e6b01fSNick Piggin static int follow_dotdot_rcu(struct nameidata *nd) 91431e6b01fSNick Piggin { 91531e6b01fSNick Piggin set_root_rcu(nd); 91631e6b01fSNick Piggin 91731e6b01fSNick Piggin while (1) { 91831e6b01fSNick Piggin if (nd->path.dentry == nd->root.dentry && 91931e6b01fSNick Piggin nd->path.mnt == nd->root.mnt) { 92031e6b01fSNick Piggin break; 92131e6b01fSNick Piggin } 92231e6b01fSNick Piggin if (nd->path.dentry != nd->path.mnt->mnt_root) { 92331e6b01fSNick Piggin struct dentry *old = nd->path.dentry; 92431e6b01fSNick Piggin struct dentry *parent = old->d_parent; 92531e6b01fSNick Piggin unsigned seq; 92631e6b01fSNick Piggin 92731e6b01fSNick Piggin seq = read_seqcount_begin(&parent->d_seq); 92831e6b01fSNick Piggin if (read_seqcount_retry(&old->d_seq, nd->seq)) 929ef7562d5SAl Viro goto failed; 93031e6b01fSNick Piggin nd->path.dentry = parent; 93131e6b01fSNick Piggin nd->seq = seq; 93231e6b01fSNick Piggin break; 93331e6b01fSNick Piggin } 93431e6b01fSNick Piggin if (!follow_up_rcu(&nd->path)) 93531e6b01fSNick Piggin break; 93631e6b01fSNick Piggin nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); 93731e6b01fSNick Piggin } 938dea39376SAl Viro follow_mount_rcu(nd); 939dea39376SAl Viro nd->inode = nd->path.dentry->d_inode; 94031e6b01fSNick Piggin return 0; 941ef7562d5SAl Viro 942ef7562d5SAl Viro failed: 943ef7562d5SAl Viro nd->flags &= ~LOOKUP_RCU; 9445b6ca027SAl Viro if (!(nd->flags & LOOKUP_ROOT)) 945ef7562d5SAl Viro nd->root.mnt = NULL; 946ef7562d5SAl Viro rcu_read_unlock(); 947962830dfSAndi Kleen br_read_unlock(&vfsmount_lock); 948ef7562d5SAl Viro return -ECHILD; 94931e6b01fSNick Piggin } 95031e6b01fSNick Piggin 9519875cf80SDavid Howells /* 952cc53ce53SDavid Howells * Follow down to the covering mount currently visible to userspace. At each 953cc53ce53SDavid Howells * point, the filesystem owning that dentry may be queried as to whether the 954cc53ce53SDavid Howells * caller is permitted to proceed or not. 955cc53ce53SDavid Howells */ 9567cc90cc3SAl Viro int follow_down(struct path *path) 957cc53ce53SDavid Howells { 958cc53ce53SDavid Howells unsigned managed; 959cc53ce53SDavid Howells int ret; 960cc53ce53SDavid Howells 961cc53ce53SDavid Howells while (managed = ACCESS_ONCE(path->dentry->d_flags), 962cc53ce53SDavid Howells unlikely(managed & DCACHE_MANAGED_DENTRY)) { 963cc53ce53SDavid Howells /* Allow the filesystem to manage the transit without i_mutex 964cc53ce53SDavid Howells * being held. 965cc53ce53SDavid Howells * 966cc53ce53SDavid Howells * We indicate to the filesystem if someone is trying to mount 967cc53ce53SDavid Howells * something here. This gives autofs the chance to deny anyone 968cc53ce53SDavid Howells * other than its daemon the right to mount on its 969cc53ce53SDavid Howells * superstructure. 970cc53ce53SDavid Howells * 971cc53ce53SDavid Howells * The filesystem may sleep at this point. 972cc53ce53SDavid Howells */ 973cc53ce53SDavid Howells if (managed & DCACHE_MANAGE_TRANSIT) { 974cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op); 975cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op->d_manage); 976ab90911fSDavid Howells ret = path->dentry->d_op->d_manage( 9771aed3e42SAl Viro path->dentry, false); 978cc53ce53SDavid Howells if (ret < 0) 979cc53ce53SDavid Howells return ret == -EISDIR ? 0 : ret; 980cc53ce53SDavid Howells } 981cc53ce53SDavid Howells 982cc53ce53SDavid Howells /* Transit to a mounted filesystem. */ 983cc53ce53SDavid Howells if (managed & DCACHE_MOUNTED) { 984cc53ce53SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 985cc53ce53SDavid Howells if (!mounted) 986cc53ce53SDavid Howells break; 987cc53ce53SDavid Howells dput(path->dentry); 988cc53ce53SDavid Howells mntput(path->mnt); 989cc53ce53SDavid Howells path->mnt = mounted; 990cc53ce53SDavid Howells path->dentry = dget(mounted->mnt_root); 991cc53ce53SDavid Howells continue; 992cc53ce53SDavid Howells } 993cc53ce53SDavid Howells 994cc53ce53SDavid Howells /* Don't handle automount points here */ 995cc53ce53SDavid Howells break; 996cc53ce53SDavid Howells } 997cc53ce53SDavid Howells return 0; 998cc53ce53SDavid Howells } 999cc53ce53SDavid Howells 1000cc53ce53SDavid Howells /* 10019875cf80SDavid Howells * Skip to top of mountpoint pile in refwalk mode for follow_dotdot() 10029875cf80SDavid Howells */ 10039875cf80SDavid Howells static void follow_mount(struct path *path) 10049875cf80SDavid Howells { 10059875cf80SDavid Howells while (d_mountpoint(path->dentry)) { 10069875cf80SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 10079875cf80SDavid Howells if (!mounted) 10089875cf80SDavid Howells break; 10099875cf80SDavid Howells dput(path->dentry); 10109875cf80SDavid Howells mntput(path->mnt); 10119875cf80SDavid Howells path->mnt = mounted; 10129875cf80SDavid Howells path->dentry = dget(mounted->mnt_root); 10139875cf80SDavid Howells } 10149875cf80SDavid Howells } 10159875cf80SDavid Howells 101631e6b01fSNick Piggin static void follow_dotdot(struct nameidata *nd) 10171da177e4SLinus Torvalds { 10182a737871SAl Viro set_root(nd); 1019e518ddb7SAndreas Mohr 10201da177e4SLinus Torvalds while(1) { 10214ac91378SJan Blunck struct dentry *old = nd->path.dentry; 10221da177e4SLinus Torvalds 10232a737871SAl Viro if (nd->path.dentry == nd->root.dentry && 10242a737871SAl Viro nd->path.mnt == nd->root.mnt) { 10251da177e4SLinus Torvalds break; 10261da177e4SLinus Torvalds } 10274ac91378SJan Blunck if (nd->path.dentry != nd->path.mnt->mnt_root) { 10283088dd70SAl Viro /* rare case of legitimate dget_parent()... */ 10293088dd70SAl Viro nd->path.dentry = dget_parent(nd->path.dentry); 10301da177e4SLinus Torvalds dput(old); 10311da177e4SLinus Torvalds break; 10321da177e4SLinus Torvalds } 10333088dd70SAl Viro if (!follow_up(&nd->path)) 10341da177e4SLinus Torvalds break; 10351da177e4SLinus Torvalds } 103679ed0226SAl Viro follow_mount(&nd->path); 103731e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 10381da177e4SLinus Torvalds } 10391da177e4SLinus Torvalds 10401da177e4SLinus Torvalds /* 1041bad61189SMiklos Szeredi * This looks up the name in dcache, possibly revalidates the old dentry and 1042bad61189SMiklos Szeredi * allocates a new one if not found or not valid. In the need_lookup argument 1043bad61189SMiklos Szeredi * returns whether i_op->lookup is necessary. 1044bad61189SMiklos Szeredi * 1045bad61189SMiklos Szeredi * dir->d_inode->i_mutex must be held 1046baa03890SNick Piggin */ 1047bad61189SMiklos Szeredi static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir, 1048201f956eSAl Viro unsigned int flags, bool *need_lookup) 1049baa03890SNick Piggin { 1050baa03890SNick Piggin struct dentry *dentry; 1051bad61189SMiklos Szeredi int error; 1052baa03890SNick Piggin 1053bad61189SMiklos Szeredi *need_lookup = false; 1054bad61189SMiklos Szeredi dentry = d_lookup(dir, name); 1055bad61189SMiklos Szeredi if (dentry) { 1056bad61189SMiklos Szeredi if (d_need_lookup(dentry)) { 1057bad61189SMiklos Szeredi *need_lookup = true; 1058bad61189SMiklos Szeredi } else if (dentry->d_flags & DCACHE_OP_REVALIDATE) { 1059201f956eSAl Viro error = d_revalidate(dentry, flags); 1060bad61189SMiklos Szeredi if (unlikely(error <= 0)) { 1061bad61189SMiklos Szeredi if (error < 0) { 1062bad61189SMiklos Szeredi dput(dentry); 1063bad61189SMiklos Szeredi return ERR_PTR(error); 1064bad61189SMiklos Szeredi } else if (!d_invalidate(dentry)) { 1065bad61189SMiklos Szeredi dput(dentry); 1066bad61189SMiklos Szeredi dentry = NULL; 1067bad61189SMiklos Szeredi } 1068bad61189SMiklos Szeredi } 1069bad61189SMiklos Szeredi } 1070bad61189SMiklos Szeredi } 1071baa03890SNick Piggin 1072bad61189SMiklos Szeredi if (!dentry) { 1073bad61189SMiklos Szeredi dentry = d_alloc(dir, name); 1074baa03890SNick Piggin if (unlikely(!dentry)) 1075baa03890SNick Piggin return ERR_PTR(-ENOMEM); 1076baa03890SNick Piggin 1077bad61189SMiklos Szeredi *need_lookup = true; 1078baa03890SNick Piggin } 1079baa03890SNick Piggin return dentry; 1080baa03890SNick Piggin } 1081baa03890SNick Piggin 1082baa03890SNick Piggin /* 1083bad61189SMiklos Szeredi * Call i_op->lookup on the dentry. The dentry must be negative but may be 1084bad61189SMiklos Szeredi * hashed if it was pouplated with DCACHE_NEED_LOOKUP. 1085bad61189SMiklos Szeredi * 1086bad61189SMiklos Szeredi * dir->d_inode->i_mutex must be held 108744396f4bSJosef Bacik */ 1088bad61189SMiklos Szeredi static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry, 108972bd866aSAl Viro unsigned int flags) 109044396f4bSJosef Bacik { 109144396f4bSJosef Bacik struct dentry *old; 109244396f4bSJosef Bacik 109344396f4bSJosef Bacik /* Don't create child dentry for a dead directory. */ 1094bad61189SMiklos Szeredi if (unlikely(IS_DEADDIR(dir))) { 1095e188dc02SMiklos Szeredi dput(dentry); 109644396f4bSJosef Bacik return ERR_PTR(-ENOENT); 1097e188dc02SMiklos Szeredi } 109844396f4bSJosef Bacik 109972bd866aSAl Viro old = dir->i_op->lookup(dir, dentry, flags); 110044396f4bSJosef Bacik if (unlikely(old)) { 110144396f4bSJosef Bacik dput(dentry); 110244396f4bSJosef Bacik dentry = old; 110344396f4bSJosef Bacik } 110444396f4bSJosef Bacik return dentry; 110544396f4bSJosef Bacik } 110644396f4bSJosef Bacik 1107a3255546SAl Viro static struct dentry *__lookup_hash(struct qstr *name, 110872bd866aSAl Viro struct dentry *base, unsigned int flags) 1109a3255546SAl Viro { 1110bad61189SMiklos Szeredi bool need_lookup; 1111a3255546SAl Viro struct dentry *dentry; 1112a3255546SAl Viro 111372bd866aSAl Viro dentry = lookup_dcache(name, base, flags, &need_lookup); 1114bad61189SMiklos Szeredi if (!need_lookup) 1115a3255546SAl Viro return dentry; 1116bad61189SMiklos Szeredi 111772bd866aSAl Viro return lookup_real(base->d_inode, dentry, flags); 1118a3255546SAl Viro } 1119a3255546SAl Viro 112044396f4bSJosef Bacik /* 11211da177e4SLinus Torvalds * It's more convoluted than I'd like it to be, but... it's still fairly 11221da177e4SLinus Torvalds * small and for now I'd prefer to have fast path as straight as possible. 11231da177e4SLinus Torvalds * It _is_ time-critical. 11241da177e4SLinus Torvalds */ 1125697f514dSMiklos Szeredi static int lookup_fast(struct nameidata *nd, struct qstr *name, 112631e6b01fSNick Piggin struct path *path, struct inode **inode) 11271da177e4SLinus Torvalds { 11284ac91378SJan Blunck struct vfsmount *mnt = nd->path.mnt; 112931e6b01fSNick Piggin struct dentry *dentry, *parent = nd->path.dentry; 11305a18fff2SAl Viro int need_reval = 1; 11315a18fff2SAl Viro int status = 1; 11329875cf80SDavid Howells int err; 11339875cf80SDavid Howells 11343cac260aSAl Viro /* 1135b04f784eSNick Piggin * Rename seqlock is not required here because in the off chance 1136b04f784eSNick Piggin * of a false negative due to a concurrent rename, we're going to 1137b04f784eSNick Piggin * do the non-racy lookup, below. 1138b04f784eSNick Piggin */ 113931e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) { 114031e6b01fSNick Piggin unsigned seq; 114112f8ad4bSLinus Torvalds dentry = __d_lookup_rcu(parent, name, &seq, nd->inode); 11425a18fff2SAl Viro if (!dentry) 11435a18fff2SAl Viro goto unlazy; 11445a18fff2SAl Viro 114512f8ad4bSLinus Torvalds /* 114612f8ad4bSLinus Torvalds * This sequence count validates that the inode matches 114712f8ad4bSLinus Torvalds * the dentry name information from lookup. 114812f8ad4bSLinus Torvalds */ 114912f8ad4bSLinus Torvalds *inode = dentry->d_inode; 115012f8ad4bSLinus Torvalds if (read_seqcount_retry(&dentry->d_seq, seq)) 115112f8ad4bSLinus Torvalds return -ECHILD; 115212f8ad4bSLinus Torvalds 115312f8ad4bSLinus Torvalds /* 115412f8ad4bSLinus Torvalds * This sequence count validates that the parent had no 115512f8ad4bSLinus Torvalds * changes while we did the lookup of the dentry above. 115612f8ad4bSLinus Torvalds * 115712f8ad4bSLinus Torvalds * The memory barrier in read_seqcount_begin of child is 115812f8ad4bSLinus Torvalds * enough, we can use __read_seqcount_retry here. 115912f8ad4bSLinus Torvalds */ 116031e6b01fSNick Piggin if (__read_seqcount_retry(&parent->d_seq, nd->seq)) 116131e6b01fSNick Piggin return -ECHILD; 116231e6b01fSNick Piggin nd->seq = seq; 11635a18fff2SAl Viro 1164fa4ee159SMiklos Szeredi if (unlikely(d_need_lookup(dentry))) 1165fa4ee159SMiklos Szeredi goto unlazy; 116624643087SAl Viro if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) { 11674ce16ef3SAl Viro status = d_revalidate(dentry, nd->flags); 11685a18fff2SAl Viro if (unlikely(status <= 0)) { 11695a18fff2SAl Viro if (status != -ECHILD) 11705a18fff2SAl Viro need_reval = 0; 11715a18fff2SAl Viro goto unlazy; 11725a18fff2SAl Viro } 117324643087SAl Viro } 117431e6b01fSNick Piggin path->mnt = mnt; 117531e6b01fSNick Piggin path->dentry = dentry; 1176d6e9bd25SAl Viro if (unlikely(!__follow_mount_rcu(nd, path, inode))) 1177d6e9bd25SAl Viro goto unlazy; 1178d6e9bd25SAl Viro if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT)) 1179d6e9bd25SAl Viro goto unlazy; 11809875cf80SDavid Howells return 0; 11815a18fff2SAl Viro unlazy: 118219660af7SAl Viro if (unlazy_walk(nd, dentry)) 11835a18fff2SAl Viro return -ECHILD; 11845a18fff2SAl Viro } else { 118531e6b01fSNick Piggin dentry = __d_lookup(parent, name); 118624643087SAl Viro } 11875a18fff2SAl Viro 118881e6f520SAl Viro if (unlikely(!dentry)) 118981e6f520SAl Viro goto need_lookup; 11905a18fff2SAl Viro 119181e6f520SAl Viro if (unlikely(d_need_lookup(dentry))) { 119281e6f520SAl Viro dput(dentry); 119381e6f520SAl Viro goto need_lookup; 11945a18fff2SAl Viro } 119581e6f520SAl Viro 11965a18fff2SAl Viro if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval) 11974ce16ef3SAl Viro status = d_revalidate(dentry, nd->flags); 11985a18fff2SAl Viro if (unlikely(status <= 0)) { 11995a18fff2SAl Viro if (status < 0) { 12005a18fff2SAl Viro dput(dentry); 12015a18fff2SAl Viro return status; 12025a18fff2SAl Viro } 12035a18fff2SAl Viro if (!d_invalidate(dentry)) { 12045a18fff2SAl Viro dput(dentry); 120581e6f520SAl Viro goto need_lookup; 12065a18fff2SAl Viro } 12075a18fff2SAl Viro } 1208697f514dSMiklos Szeredi 12091da177e4SLinus Torvalds path->mnt = mnt; 12101da177e4SLinus Torvalds path->dentry = dentry; 12119875cf80SDavid Howells err = follow_managed(path, nd->flags); 121289312214SIan Kent if (unlikely(err < 0)) { 121389312214SIan Kent path_put_conditional(path, nd); 12149875cf80SDavid Howells return err; 121589312214SIan Kent } 1216a3fbbde7SAl Viro if (err) 1217a3fbbde7SAl Viro nd->flags |= LOOKUP_JUMPED; 121831e6b01fSNick Piggin *inode = path->dentry->d_inode; 12191da177e4SLinus Torvalds return 0; 122081e6f520SAl Viro 122181e6f520SAl Viro need_lookup: 1222697f514dSMiklos Szeredi return 1; 1223697f514dSMiklos Szeredi } 1224697f514dSMiklos Szeredi 1225697f514dSMiklos Szeredi /* Fast lookup failed, do it the slow way */ 1226697f514dSMiklos Szeredi static int lookup_slow(struct nameidata *nd, struct qstr *name, 1227697f514dSMiklos Szeredi struct path *path) 1228697f514dSMiklos Szeredi { 1229697f514dSMiklos Szeredi struct dentry *dentry, *parent; 1230697f514dSMiklos Szeredi int err; 1231697f514dSMiklos Szeredi 1232697f514dSMiklos Szeredi parent = nd->path.dentry; 123381e6f520SAl Viro BUG_ON(nd->inode != parent->d_inode); 123481e6f520SAl Viro 123581e6f520SAl Viro mutex_lock(&parent->d_inode->i_mutex); 123672bd866aSAl Viro dentry = __lookup_hash(name, parent, nd->flags); 123781e6f520SAl Viro mutex_unlock(&parent->d_inode->i_mutex); 123881e6f520SAl Viro if (IS_ERR(dentry)) 123981e6f520SAl Viro return PTR_ERR(dentry); 1240697f514dSMiklos Szeredi path->mnt = nd->path.mnt; 1241697f514dSMiklos Szeredi path->dentry = dentry; 1242697f514dSMiklos Szeredi err = follow_managed(path, nd->flags); 1243697f514dSMiklos Szeredi if (unlikely(err < 0)) { 1244697f514dSMiklos Szeredi path_put_conditional(path, nd); 1245697f514dSMiklos Szeredi return err; 1246697f514dSMiklos Szeredi } 1247697f514dSMiklos Szeredi if (err) 1248697f514dSMiklos Szeredi nd->flags |= LOOKUP_JUMPED; 1249697f514dSMiklos Szeredi return 0; 12501da177e4SLinus Torvalds } 12511da177e4SLinus Torvalds 125252094c8aSAl Viro static inline int may_lookup(struct nameidata *nd) 125352094c8aSAl Viro { 125452094c8aSAl Viro if (nd->flags & LOOKUP_RCU) { 12554ad5abb3SAl Viro int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK); 125652094c8aSAl Viro if (err != -ECHILD) 125752094c8aSAl Viro return err; 125819660af7SAl Viro if (unlazy_walk(nd, NULL)) 125952094c8aSAl Viro return -ECHILD; 126052094c8aSAl Viro } 12614ad5abb3SAl Viro return inode_permission(nd->inode, MAY_EXEC); 126252094c8aSAl Viro } 126352094c8aSAl Viro 12649856fa1bSAl Viro static inline int handle_dots(struct nameidata *nd, int type) 12659856fa1bSAl Viro { 12669856fa1bSAl Viro if (type == LAST_DOTDOT) { 12679856fa1bSAl Viro if (nd->flags & LOOKUP_RCU) { 12689856fa1bSAl Viro if (follow_dotdot_rcu(nd)) 12699856fa1bSAl Viro return -ECHILD; 12709856fa1bSAl Viro } else 12719856fa1bSAl Viro follow_dotdot(nd); 12729856fa1bSAl Viro } 12739856fa1bSAl Viro return 0; 12749856fa1bSAl Viro } 12759856fa1bSAl Viro 1276951361f9SAl Viro static void terminate_walk(struct nameidata *nd) 1277951361f9SAl Viro { 1278951361f9SAl Viro if (!(nd->flags & LOOKUP_RCU)) { 1279951361f9SAl Viro path_put(&nd->path); 1280951361f9SAl Viro } else { 1281951361f9SAl Viro nd->flags &= ~LOOKUP_RCU; 12825b6ca027SAl Viro if (!(nd->flags & LOOKUP_ROOT)) 1283951361f9SAl Viro nd->root.mnt = NULL; 1284951361f9SAl Viro rcu_read_unlock(); 1285962830dfSAndi Kleen br_read_unlock(&vfsmount_lock); 1286951361f9SAl Viro } 1287951361f9SAl Viro } 1288951361f9SAl Viro 12893ddcd056SLinus Torvalds /* 12903ddcd056SLinus Torvalds * Do we need to follow links? We _really_ want to be able 12913ddcd056SLinus Torvalds * to do this check without having to look at inode->i_op, 12923ddcd056SLinus Torvalds * so we keep a cache of "no, this doesn't need follow_link" 12933ddcd056SLinus Torvalds * for the common case. 12943ddcd056SLinus Torvalds */ 12957813b94aSLinus Torvalds static inline int should_follow_link(struct inode *inode, int follow) 12963ddcd056SLinus Torvalds { 12973ddcd056SLinus Torvalds if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) { 12983ddcd056SLinus Torvalds if (likely(inode->i_op->follow_link)) 12993ddcd056SLinus Torvalds return follow; 13003ddcd056SLinus Torvalds 13013ddcd056SLinus Torvalds /* This gets set once for the inode lifetime */ 13023ddcd056SLinus Torvalds spin_lock(&inode->i_lock); 13033ddcd056SLinus Torvalds inode->i_opflags |= IOP_NOFOLLOW; 13043ddcd056SLinus Torvalds spin_unlock(&inode->i_lock); 13053ddcd056SLinus Torvalds } 13063ddcd056SLinus Torvalds return 0; 13073ddcd056SLinus Torvalds } 13083ddcd056SLinus Torvalds 1309ce57dfc1SAl Viro static inline int walk_component(struct nameidata *nd, struct path *path, 1310ce57dfc1SAl Viro struct qstr *name, int type, int follow) 1311ce57dfc1SAl Viro { 1312ce57dfc1SAl Viro struct inode *inode; 1313ce57dfc1SAl Viro int err; 1314ce57dfc1SAl Viro /* 1315ce57dfc1SAl Viro * "." and ".." are special - ".." especially so because it has 1316ce57dfc1SAl Viro * to be able to know about the current root directory and 1317ce57dfc1SAl Viro * parent relationships. 1318ce57dfc1SAl Viro */ 1319ce57dfc1SAl Viro if (unlikely(type != LAST_NORM)) 1320ce57dfc1SAl Viro return handle_dots(nd, type); 1321697f514dSMiklos Szeredi err = lookup_fast(nd, name, path, &inode); 1322ce57dfc1SAl Viro if (unlikely(err)) { 1323697f514dSMiklos Szeredi if (err < 0) 1324697f514dSMiklos Szeredi goto out_err; 1325697f514dSMiklos Szeredi 1326697f514dSMiklos Szeredi err = lookup_slow(nd, name, path); 1327697f514dSMiklos Szeredi if (err < 0) 1328697f514dSMiklos Szeredi goto out_err; 1329697f514dSMiklos Szeredi 1330697f514dSMiklos Szeredi inode = path->dentry->d_inode; 1331ce57dfc1SAl Viro } 1332697f514dSMiklos Szeredi err = -ENOENT; 1333697f514dSMiklos Szeredi if (!inode) 1334697f514dSMiklos Szeredi goto out_path_put; 1335697f514dSMiklos Szeredi 13367813b94aSLinus Torvalds if (should_follow_link(inode, follow)) { 133719660af7SAl Viro if (nd->flags & LOOKUP_RCU) { 133819660af7SAl Viro if (unlikely(unlazy_walk(nd, path->dentry))) { 1339697f514dSMiklos Szeredi err = -ECHILD; 1340697f514dSMiklos Szeredi goto out_err; 134119660af7SAl Viro } 134219660af7SAl Viro } 1343ce57dfc1SAl Viro BUG_ON(inode != path->dentry->d_inode); 1344ce57dfc1SAl Viro return 1; 1345ce57dfc1SAl Viro } 1346ce57dfc1SAl Viro path_to_nameidata(path, nd); 1347ce57dfc1SAl Viro nd->inode = inode; 1348ce57dfc1SAl Viro return 0; 1349697f514dSMiklos Szeredi 1350697f514dSMiklos Szeredi out_path_put: 1351697f514dSMiklos Szeredi path_to_nameidata(path, nd); 1352697f514dSMiklos Szeredi out_err: 1353697f514dSMiklos Szeredi terminate_walk(nd); 1354697f514dSMiklos Szeredi return err; 1355ce57dfc1SAl Viro } 1356ce57dfc1SAl Viro 13571da177e4SLinus Torvalds /* 1358b356379aSAl Viro * This limits recursive symlink follows to 8, while 1359b356379aSAl Viro * limiting consecutive symlinks to 40. 1360b356379aSAl Viro * 1361b356379aSAl Viro * Without that kind of total limit, nasty chains of consecutive 1362b356379aSAl Viro * symlinks can cause almost arbitrarily long lookups. 1363b356379aSAl Viro */ 1364b356379aSAl Viro static inline int nested_symlink(struct path *path, struct nameidata *nd) 1365b356379aSAl Viro { 1366b356379aSAl Viro int res; 1367b356379aSAl Viro 1368b356379aSAl Viro if (unlikely(current->link_count >= MAX_NESTED_LINKS)) { 1369b356379aSAl Viro path_put_conditional(path, nd); 1370b356379aSAl Viro path_put(&nd->path); 1371b356379aSAl Viro return -ELOOP; 1372b356379aSAl Viro } 13731a4022f8SErez Zadok BUG_ON(nd->depth >= MAX_NESTED_LINKS); 1374b356379aSAl Viro 1375b356379aSAl Viro nd->depth++; 1376b356379aSAl Viro current->link_count++; 1377b356379aSAl Viro 1378b356379aSAl Viro do { 1379b356379aSAl Viro struct path link = *path; 1380b356379aSAl Viro void *cookie; 1381574197e0SAl Viro 1382574197e0SAl Viro res = follow_link(&link, nd, &cookie); 13836d7b5aaeSAl Viro if (res) 13846d7b5aaeSAl Viro break; 1385b356379aSAl Viro res = walk_component(nd, path, &nd->last, 1386b356379aSAl Viro nd->last_type, LOOKUP_FOLLOW); 1387574197e0SAl Viro put_link(nd, &link, cookie); 1388b356379aSAl Viro } while (res > 0); 1389b356379aSAl Viro 1390b356379aSAl Viro current->link_count--; 1391b356379aSAl Viro nd->depth--; 1392b356379aSAl Viro return res; 1393b356379aSAl Viro } 1394b356379aSAl Viro 1395b356379aSAl Viro /* 13963ddcd056SLinus Torvalds * We really don't want to look at inode->i_op->lookup 13973ddcd056SLinus Torvalds * when we don't have to. So we keep a cache bit in 13983ddcd056SLinus Torvalds * the inode ->i_opflags field that says "yes, we can 13993ddcd056SLinus Torvalds * do lookup on this inode". 14003ddcd056SLinus Torvalds */ 14013ddcd056SLinus Torvalds static inline int can_lookup(struct inode *inode) 14023ddcd056SLinus Torvalds { 14033ddcd056SLinus Torvalds if (likely(inode->i_opflags & IOP_LOOKUP)) 14043ddcd056SLinus Torvalds return 1; 14053ddcd056SLinus Torvalds if (likely(!inode->i_op->lookup)) 14063ddcd056SLinus Torvalds return 0; 14073ddcd056SLinus Torvalds 14083ddcd056SLinus Torvalds /* We do this once for the lifetime of the inode */ 14093ddcd056SLinus Torvalds spin_lock(&inode->i_lock); 14103ddcd056SLinus Torvalds inode->i_opflags |= IOP_LOOKUP; 14113ddcd056SLinus Torvalds spin_unlock(&inode->i_lock); 14123ddcd056SLinus Torvalds return 1; 14133ddcd056SLinus Torvalds } 14143ddcd056SLinus Torvalds 1415bfcfaa77SLinus Torvalds /* 1416bfcfaa77SLinus Torvalds * We can do the critical dentry name comparison and hashing 1417bfcfaa77SLinus Torvalds * operations one word at a time, but we are limited to: 1418bfcfaa77SLinus Torvalds * 1419bfcfaa77SLinus Torvalds * - Architectures with fast unaligned word accesses. We could 1420bfcfaa77SLinus Torvalds * do a "get_unaligned()" if this helps and is sufficiently 1421bfcfaa77SLinus Torvalds * fast. 1422bfcfaa77SLinus Torvalds * 1423bfcfaa77SLinus Torvalds * - Little-endian machines (so that we can generate the mask 1424bfcfaa77SLinus Torvalds * of low bytes efficiently). Again, we *could* do a byte 1425bfcfaa77SLinus Torvalds * swapping load on big-endian architectures if that is not 1426bfcfaa77SLinus Torvalds * expensive enough to make the optimization worthless. 1427bfcfaa77SLinus Torvalds * 1428bfcfaa77SLinus Torvalds * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we 1429bfcfaa77SLinus Torvalds * do not trap on the (extremely unlikely) case of a page 1430bfcfaa77SLinus Torvalds * crossing operation. 1431bfcfaa77SLinus Torvalds * 1432bfcfaa77SLinus Torvalds * - Furthermore, we need an efficient 64-bit compile for the 1433bfcfaa77SLinus Torvalds * 64-bit case in order to generate the "number of bytes in 1434bfcfaa77SLinus Torvalds * the final mask". Again, that could be replaced with a 1435bfcfaa77SLinus Torvalds * efficient population count instruction or similar. 1436bfcfaa77SLinus Torvalds */ 1437bfcfaa77SLinus Torvalds #ifdef CONFIG_DCACHE_WORD_ACCESS 1438bfcfaa77SLinus Torvalds 1439f68e556eSLinus Torvalds #include <asm/word-at-a-time.h> 1440bfcfaa77SLinus Torvalds 1441f68e556eSLinus Torvalds #ifdef CONFIG_64BIT 1442bfcfaa77SLinus Torvalds 1443bfcfaa77SLinus Torvalds static inline unsigned int fold_hash(unsigned long hash) 1444bfcfaa77SLinus Torvalds { 1445bfcfaa77SLinus Torvalds hash += hash >> (8*sizeof(int)); 1446bfcfaa77SLinus Torvalds return hash; 1447bfcfaa77SLinus Torvalds } 1448bfcfaa77SLinus Torvalds 1449bfcfaa77SLinus Torvalds #else /* 32-bit case */ 1450bfcfaa77SLinus Torvalds 1451bfcfaa77SLinus Torvalds #define fold_hash(x) (x) 1452bfcfaa77SLinus Torvalds 1453bfcfaa77SLinus Torvalds #endif 1454bfcfaa77SLinus Torvalds 1455bfcfaa77SLinus Torvalds unsigned int full_name_hash(const unsigned char *name, unsigned int len) 1456bfcfaa77SLinus Torvalds { 1457bfcfaa77SLinus Torvalds unsigned long a, mask; 1458bfcfaa77SLinus Torvalds unsigned long hash = 0; 1459bfcfaa77SLinus Torvalds 1460bfcfaa77SLinus Torvalds for (;;) { 1461e419b4ccSLinus Torvalds a = load_unaligned_zeropad(name); 1462bfcfaa77SLinus Torvalds if (len < sizeof(unsigned long)) 1463bfcfaa77SLinus Torvalds break; 1464bfcfaa77SLinus Torvalds hash += a; 1465f132c5beSAl Viro hash *= 9; 1466bfcfaa77SLinus Torvalds name += sizeof(unsigned long); 1467bfcfaa77SLinus Torvalds len -= sizeof(unsigned long); 1468bfcfaa77SLinus Torvalds if (!len) 1469bfcfaa77SLinus Torvalds goto done; 1470bfcfaa77SLinus Torvalds } 1471bfcfaa77SLinus Torvalds mask = ~(~0ul << len*8); 1472bfcfaa77SLinus Torvalds hash += mask & a; 1473bfcfaa77SLinus Torvalds done: 1474bfcfaa77SLinus Torvalds return fold_hash(hash); 1475bfcfaa77SLinus Torvalds } 1476bfcfaa77SLinus Torvalds EXPORT_SYMBOL(full_name_hash); 1477bfcfaa77SLinus Torvalds 1478bfcfaa77SLinus Torvalds /* 1479bfcfaa77SLinus Torvalds * Calculate the length and hash of the path component, and 1480bfcfaa77SLinus Torvalds * return the length of the component; 1481bfcfaa77SLinus Torvalds */ 1482bfcfaa77SLinus Torvalds static inline unsigned long hash_name(const char *name, unsigned int *hashp) 1483bfcfaa77SLinus Torvalds { 148436126f8fSLinus Torvalds unsigned long a, b, adata, bdata, mask, hash, len; 148536126f8fSLinus Torvalds const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS; 1486bfcfaa77SLinus Torvalds 1487bfcfaa77SLinus Torvalds hash = a = 0; 1488bfcfaa77SLinus Torvalds len = -sizeof(unsigned long); 1489bfcfaa77SLinus Torvalds do { 1490bfcfaa77SLinus Torvalds hash = (hash + a) * 9; 1491bfcfaa77SLinus Torvalds len += sizeof(unsigned long); 1492e419b4ccSLinus Torvalds a = load_unaligned_zeropad(name+len); 149336126f8fSLinus Torvalds b = a ^ REPEAT_BYTE('/'); 149436126f8fSLinus Torvalds } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants))); 1495bfcfaa77SLinus Torvalds 149636126f8fSLinus Torvalds adata = prep_zero_mask(a, adata, &constants); 149736126f8fSLinus Torvalds bdata = prep_zero_mask(b, bdata, &constants); 149836126f8fSLinus Torvalds 149936126f8fSLinus Torvalds mask = create_zero_mask(adata | bdata); 150036126f8fSLinus Torvalds 150136126f8fSLinus Torvalds hash += a & zero_bytemask(mask); 1502bfcfaa77SLinus Torvalds *hashp = fold_hash(hash); 1503bfcfaa77SLinus Torvalds 150436126f8fSLinus Torvalds return len + find_zero(mask); 1505bfcfaa77SLinus Torvalds } 1506bfcfaa77SLinus Torvalds 1507bfcfaa77SLinus Torvalds #else 1508bfcfaa77SLinus Torvalds 15090145acc2SLinus Torvalds unsigned int full_name_hash(const unsigned char *name, unsigned int len) 15100145acc2SLinus Torvalds { 15110145acc2SLinus Torvalds unsigned long hash = init_name_hash(); 15120145acc2SLinus Torvalds while (len--) 15130145acc2SLinus Torvalds hash = partial_name_hash(*name++, hash); 15140145acc2SLinus Torvalds return end_name_hash(hash); 15150145acc2SLinus Torvalds } 1516ae942ae7SLinus Torvalds EXPORT_SYMBOL(full_name_hash); 15170145acc2SLinus Torvalds 15183ddcd056SLinus Torvalds /* 1519200e9ef7SLinus Torvalds * We know there's a real path component here of at least 1520200e9ef7SLinus Torvalds * one character. 1521200e9ef7SLinus Torvalds */ 1522200e9ef7SLinus Torvalds static inline unsigned long hash_name(const char *name, unsigned int *hashp) 1523200e9ef7SLinus Torvalds { 1524200e9ef7SLinus Torvalds unsigned long hash = init_name_hash(); 1525200e9ef7SLinus Torvalds unsigned long len = 0, c; 1526200e9ef7SLinus Torvalds 1527200e9ef7SLinus Torvalds c = (unsigned char)*name; 1528200e9ef7SLinus Torvalds do { 1529200e9ef7SLinus Torvalds len++; 1530200e9ef7SLinus Torvalds hash = partial_name_hash(c, hash); 1531200e9ef7SLinus Torvalds c = (unsigned char)name[len]; 1532200e9ef7SLinus Torvalds } while (c && c != '/'); 1533200e9ef7SLinus Torvalds *hashp = end_name_hash(hash); 1534200e9ef7SLinus Torvalds return len; 1535200e9ef7SLinus Torvalds } 1536200e9ef7SLinus Torvalds 1537bfcfaa77SLinus Torvalds #endif 1538bfcfaa77SLinus Torvalds 1539200e9ef7SLinus Torvalds /* 15401da177e4SLinus Torvalds * Name resolution. 1541ea3834d9SPrasanna Meda * This is the basic name resolution function, turning a pathname into 1542ea3834d9SPrasanna Meda * the final dentry. We expect 'base' to be positive and a directory. 15431da177e4SLinus Torvalds * 1544ea3834d9SPrasanna Meda * Returns 0 and nd will have valid dentry and mnt on success. 1545ea3834d9SPrasanna Meda * Returns error and drops reference to input namei data on failure. 15461da177e4SLinus Torvalds */ 15476de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd) 15481da177e4SLinus Torvalds { 15491da177e4SLinus Torvalds struct path next; 15501da177e4SLinus Torvalds int err; 15511da177e4SLinus Torvalds 15521da177e4SLinus Torvalds while (*name=='/') 15531da177e4SLinus Torvalds name++; 15541da177e4SLinus Torvalds if (!*name) 1555086e183aSAl Viro return 0; 15561da177e4SLinus Torvalds 15571da177e4SLinus Torvalds /* At this point we know we have a real path component. */ 15581da177e4SLinus Torvalds for(;;) { 15591da177e4SLinus Torvalds struct qstr this; 1560200e9ef7SLinus Torvalds long len; 1561fe479a58SAl Viro int type; 15621da177e4SLinus Torvalds 156352094c8aSAl Viro err = may_lookup(nd); 15641da177e4SLinus Torvalds if (err) 15651da177e4SLinus Torvalds break; 15661da177e4SLinus Torvalds 1567200e9ef7SLinus Torvalds len = hash_name(name, &this.hash); 15681da177e4SLinus Torvalds this.name = name; 1569200e9ef7SLinus Torvalds this.len = len; 15701da177e4SLinus Torvalds 1571fe479a58SAl Viro type = LAST_NORM; 1572200e9ef7SLinus Torvalds if (name[0] == '.') switch (len) { 1573fe479a58SAl Viro case 2: 1574200e9ef7SLinus Torvalds if (name[1] == '.') { 1575fe479a58SAl Viro type = LAST_DOTDOT; 157616c2cd71SAl Viro nd->flags |= LOOKUP_JUMPED; 157716c2cd71SAl Viro } 1578fe479a58SAl Viro break; 1579fe479a58SAl Viro case 1: 1580fe479a58SAl Viro type = LAST_DOT; 1581fe479a58SAl Viro } 15825a202bcdSAl Viro if (likely(type == LAST_NORM)) { 15835a202bcdSAl Viro struct dentry *parent = nd->path.dentry; 158416c2cd71SAl Viro nd->flags &= ~LOOKUP_JUMPED; 15855a202bcdSAl Viro if (unlikely(parent->d_flags & DCACHE_OP_HASH)) { 15865a202bcdSAl Viro err = parent->d_op->d_hash(parent, nd->inode, 15875a202bcdSAl Viro &this); 15885a202bcdSAl Viro if (err < 0) 15895a202bcdSAl Viro break; 15905a202bcdSAl Viro } 15915a202bcdSAl Viro } 1592fe479a58SAl Viro 1593200e9ef7SLinus Torvalds if (!name[len]) 15941da177e4SLinus Torvalds goto last_component; 1595200e9ef7SLinus Torvalds /* 1596200e9ef7SLinus Torvalds * If it wasn't NUL, we know it was '/'. Skip that 1597200e9ef7SLinus Torvalds * slash, and continue until no more slashes. 1598200e9ef7SLinus Torvalds */ 1599200e9ef7SLinus Torvalds do { 1600200e9ef7SLinus Torvalds len++; 1601200e9ef7SLinus Torvalds } while (unlikely(name[len] == '/')); 1602200e9ef7SLinus Torvalds if (!name[len]) 1603b356379aSAl Viro goto last_component; 1604200e9ef7SLinus Torvalds name += len; 16051da177e4SLinus Torvalds 1606ce57dfc1SAl Viro err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW); 1607ce57dfc1SAl Viro if (err < 0) 1608ce57dfc1SAl Viro return err; 1609fe479a58SAl Viro 1610ce57dfc1SAl Viro if (err) { 1611b356379aSAl Viro err = nested_symlink(&next, nd); 16121da177e4SLinus Torvalds if (err) 1613a7472babSAl Viro return err; 161431e6b01fSNick Piggin } 16153ddcd056SLinus Torvalds if (can_lookup(nd->inode)) 16161da177e4SLinus Torvalds continue; 16173ddcd056SLinus Torvalds err = -ENOTDIR; 16183ddcd056SLinus Torvalds break; 16191da177e4SLinus Torvalds /* here ends the main loop */ 16201da177e4SLinus Torvalds 16211da177e4SLinus Torvalds last_component: 1622ce57dfc1SAl Viro nd->last = this; 1623ce57dfc1SAl Viro nd->last_type = type; 1624ce57dfc1SAl Viro return 0; 1625ce57dfc1SAl Viro } 1626951361f9SAl Viro terminate_walk(nd); 16271da177e4SLinus Torvalds return err; 16281da177e4SLinus Torvalds } 16291da177e4SLinus Torvalds 163070e9b357SAl Viro static int path_init(int dfd, const char *name, unsigned int flags, 163170e9b357SAl Viro struct nameidata *nd, struct file **fp) 163231e6b01fSNick Piggin { 163331e6b01fSNick Piggin int retval = 0; 163431e6b01fSNick Piggin int fput_needed; 163531e6b01fSNick Piggin struct file *file; 163631e6b01fSNick Piggin 163731e6b01fSNick Piggin nd->last_type = LAST_ROOT; /* if there are only slashes... */ 163816c2cd71SAl Viro nd->flags = flags | LOOKUP_JUMPED; 163931e6b01fSNick Piggin nd->depth = 0; 16405b6ca027SAl Viro if (flags & LOOKUP_ROOT) { 16415b6ca027SAl Viro struct inode *inode = nd->root.dentry->d_inode; 164273d049a4SAl Viro if (*name) { 16435b6ca027SAl Viro if (!inode->i_op->lookup) 16445b6ca027SAl Viro return -ENOTDIR; 16455b6ca027SAl Viro retval = inode_permission(inode, MAY_EXEC); 16465b6ca027SAl Viro if (retval) 16475b6ca027SAl Viro return retval; 164873d049a4SAl Viro } 16495b6ca027SAl Viro nd->path = nd->root; 16505b6ca027SAl Viro nd->inode = inode; 16515b6ca027SAl Viro if (flags & LOOKUP_RCU) { 1652962830dfSAndi Kleen br_read_lock(&vfsmount_lock); 16535b6ca027SAl Viro rcu_read_lock(); 16545b6ca027SAl Viro nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 16555b6ca027SAl Viro } else { 16565b6ca027SAl Viro path_get(&nd->path); 16575b6ca027SAl Viro } 16585b6ca027SAl Viro return 0; 16595b6ca027SAl Viro } 16605b6ca027SAl Viro 166131e6b01fSNick Piggin nd->root.mnt = NULL; 166231e6b01fSNick Piggin 166331e6b01fSNick Piggin if (*name=='/') { 1664e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 1665962830dfSAndi Kleen br_read_lock(&vfsmount_lock); 166631e6b01fSNick Piggin rcu_read_lock(); 1667e41f7d4eSAl Viro set_root_rcu(nd); 1668e41f7d4eSAl Viro } else { 1669e41f7d4eSAl Viro set_root(nd); 1670e41f7d4eSAl Viro path_get(&nd->root); 1671e41f7d4eSAl Viro } 167231e6b01fSNick Piggin nd->path = nd->root; 167331e6b01fSNick Piggin } else if (dfd == AT_FDCWD) { 1674e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 167531e6b01fSNick Piggin struct fs_struct *fs = current->fs; 1676c28cc364SNick Piggin unsigned seq; 167731e6b01fSNick Piggin 1678962830dfSAndi Kleen br_read_lock(&vfsmount_lock); 167931e6b01fSNick Piggin rcu_read_lock(); 168031e6b01fSNick Piggin 1681c28cc364SNick Piggin do { 1682c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 168331e6b01fSNick Piggin nd->path = fs->pwd; 1684c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 1685c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 1686e41f7d4eSAl Viro } else { 1687e41f7d4eSAl Viro get_fs_pwd(current->fs, &nd->path); 1688e41f7d4eSAl Viro } 168931e6b01fSNick Piggin } else { 169031e6b01fSNick Piggin struct dentry *dentry; 169131e6b01fSNick Piggin 16921abf0c71SAl Viro file = fget_raw_light(dfd, &fput_needed); 169331e6b01fSNick Piggin retval = -EBADF; 169431e6b01fSNick Piggin if (!file) 169531e6b01fSNick Piggin goto out_fail; 169631e6b01fSNick Piggin 169731e6b01fSNick Piggin dentry = file->f_path.dentry; 169831e6b01fSNick Piggin 1699f52e0c11SAl Viro if (*name) { 170031e6b01fSNick Piggin retval = -ENOTDIR; 170131e6b01fSNick Piggin if (!S_ISDIR(dentry->d_inode->i_mode)) 170231e6b01fSNick Piggin goto fput_fail; 170331e6b01fSNick Piggin 17044ad5abb3SAl Viro retval = inode_permission(dentry->d_inode, MAY_EXEC); 170531e6b01fSNick Piggin if (retval) 170631e6b01fSNick Piggin goto fput_fail; 1707f52e0c11SAl Viro } 170831e6b01fSNick Piggin 170931e6b01fSNick Piggin nd->path = file->f_path; 1710e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 171131e6b01fSNick Piggin if (fput_needed) 171270e9b357SAl Viro *fp = file; 1713c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 1714962830dfSAndi Kleen br_read_lock(&vfsmount_lock); 171531e6b01fSNick Piggin rcu_read_lock(); 17165590ff0dSUlrich Drepper } else { 17175dd784d0SJan Blunck path_get(&file->f_path); 17185590ff0dSUlrich Drepper fput_light(file, fput_needed); 17191da177e4SLinus Torvalds } 1720e41f7d4eSAl Viro } 1721e41f7d4eSAl Viro 172231e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 17239b4a9b14SAl Viro return 0; 17242dfdd266SJosef 'Jeff' Sipek 17259b4a9b14SAl Viro fput_fail: 17269b4a9b14SAl Viro fput_light(file, fput_needed); 17279b4a9b14SAl Viro out_fail: 17289b4a9b14SAl Viro return retval; 17299b4a9b14SAl Viro } 17309b4a9b14SAl Viro 1731bd92d7feSAl Viro static inline int lookup_last(struct nameidata *nd, struct path *path) 1732bd92d7feSAl Viro { 1733bd92d7feSAl Viro if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len]) 1734bd92d7feSAl Viro nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 1735bd92d7feSAl Viro 1736bd92d7feSAl Viro nd->flags &= ~LOOKUP_PARENT; 1737bd92d7feSAl Viro return walk_component(nd, path, &nd->last, nd->last_type, 1738bd92d7feSAl Viro nd->flags & LOOKUP_FOLLOW); 1739bd92d7feSAl Viro } 1740bd92d7feSAl Viro 17419b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */ 1742ee0827cdSAl Viro static int path_lookupat(int dfd, const char *name, 17439b4a9b14SAl Viro unsigned int flags, struct nameidata *nd) 17449b4a9b14SAl Viro { 174570e9b357SAl Viro struct file *base = NULL; 1746bd92d7feSAl Viro struct path path; 1747bd92d7feSAl Viro int err; 174831e6b01fSNick Piggin 174931e6b01fSNick Piggin /* 175031e6b01fSNick Piggin * Path walking is largely split up into 2 different synchronisation 175131e6b01fSNick Piggin * schemes, rcu-walk and ref-walk (explained in 175231e6b01fSNick Piggin * Documentation/filesystems/path-lookup.txt). These share much of the 175331e6b01fSNick Piggin * path walk code, but some things particularly setup, cleanup, and 175431e6b01fSNick Piggin * following mounts are sufficiently divergent that functions are 175531e6b01fSNick Piggin * duplicated. Typically there is a function foo(), and its RCU 175631e6b01fSNick Piggin * analogue, foo_rcu(). 175731e6b01fSNick Piggin * 175831e6b01fSNick Piggin * -ECHILD is the error number of choice (just to avoid clashes) that 175931e6b01fSNick Piggin * is returned if some aspect of an rcu-walk fails. Such an error must 176031e6b01fSNick Piggin * be handled by restarting a traditional ref-walk (which will always 176131e6b01fSNick Piggin * be able to complete). 176231e6b01fSNick Piggin */ 1763bd92d7feSAl Viro err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base); 1764ee0827cdSAl Viro 1765bd92d7feSAl Viro if (unlikely(err)) 1766bd92d7feSAl Viro return err; 1767ee0827cdSAl Viro 1768ee0827cdSAl Viro current->total_link_count = 0; 1769bd92d7feSAl Viro err = link_path_walk(name, nd); 1770bd92d7feSAl Viro 1771bd92d7feSAl Viro if (!err && !(flags & LOOKUP_PARENT)) { 1772bd92d7feSAl Viro err = lookup_last(nd, &path); 1773bd92d7feSAl Viro while (err > 0) { 1774bd92d7feSAl Viro void *cookie; 1775bd92d7feSAl Viro struct path link = path; 1776bd92d7feSAl Viro nd->flags |= LOOKUP_PARENT; 1777574197e0SAl Viro err = follow_link(&link, nd, &cookie); 17786d7b5aaeSAl Viro if (err) 17796d7b5aaeSAl Viro break; 1780bd92d7feSAl Viro err = lookup_last(nd, &path); 1781574197e0SAl Viro put_link(nd, &link, cookie); 1782bd92d7feSAl Viro } 1783bd92d7feSAl Viro } 1784ee0827cdSAl Viro 17859f1fafeeSAl Viro if (!err) 17869f1fafeeSAl Viro err = complete_walk(nd); 1787bd92d7feSAl Viro 1788bd92d7feSAl Viro if (!err && nd->flags & LOOKUP_DIRECTORY) { 1789bd92d7feSAl Viro if (!nd->inode->i_op->lookup) { 1790bd92d7feSAl Viro path_put(&nd->path); 1791bd23a539SAl Viro err = -ENOTDIR; 1792bd92d7feSAl Viro } 1793bd92d7feSAl Viro } 179416c2cd71SAl Viro 179570e9b357SAl Viro if (base) 179670e9b357SAl Viro fput(base); 1797ee0827cdSAl Viro 17985b6ca027SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) { 179931e6b01fSNick Piggin path_put(&nd->root); 180031e6b01fSNick Piggin nd->root.mnt = NULL; 180131e6b01fSNick Piggin } 1802bd92d7feSAl Viro return err; 180331e6b01fSNick Piggin } 180431e6b01fSNick Piggin 1805ee0827cdSAl Viro static int do_path_lookup(int dfd, const char *name, 1806ee0827cdSAl Viro unsigned int flags, struct nameidata *nd) 1807ee0827cdSAl Viro { 1808ee0827cdSAl Viro int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd); 1809ee0827cdSAl Viro if (unlikely(retval == -ECHILD)) 1810ee0827cdSAl Viro retval = path_lookupat(dfd, name, flags, nd); 1811ee0827cdSAl Viro if (unlikely(retval == -ESTALE)) 1812ee0827cdSAl Viro retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd); 1813ee0827cdSAl Viro 181431e6b01fSNick Piggin if (likely(!retval)) { 181531e6b01fSNick Piggin if (unlikely(!audit_dummy_context())) { 181631e6b01fSNick Piggin if (nd->path.dentry && nd->inode) 181731e6b01fSNick Piggin audit_inode(name, nd->path.dentry); 181831e6b01fSNick Piggin } 181931e6b01fSNick Piggin } 1820170aa3d0SUlrich Drepper return retval; 18211da177e4SLinus Torvalds } 18221da177e4SLinus Torvalds 182379714f72SAl Viro /* does lookup, returns the object with parent locked */ 182479714f72SAl Viro struct dentry *kern_path_locked(const char *name, struct path *path) 18255590ff0dSUlrich Drepper { 182679714f72SAl Viro struct nameidata nd; 182779714f72SAl Viro struct dentry *d; 182879714f72SAl Viro int err = do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, &nd); 182979714f72SAl Viro if (err) 183079714f72SAl Viro return ERR_PTR(err); 183179714f72SAl Viro if (nd.last_type != LAST_NORM) { 183279714f72SAl Viro path_put(&nd.path); 183379714f72SAl Viro return ERR_PTR(-EINVAL); 183479714f72SAl Viro } 183579714f72SAl Viro mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 183679714f72SAl Viro d = lookup_one_len(nd.last.name, nd.path.dentry, nd.last.len); 183779714f72SAl Viro if (IS_ERR(d)) { 183879714f72SAl Viro mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 183979714f72SAl Viro path_put(&nd.path); 184079714f72SAl Viro return d; 184179714f72SAl Viro } 184279714f72SAl Viro *path = nd.path; 184379714f72SAl Viro return d; 18445590ff0dSUlrich Drepper } 18455590ff0dSUlrich Drepper 1846d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path) 1847d1811465SAl Viro { 1848d1811465SAl Viro struct nameidata nd; 1849d1811465SAl Viro int res = do_path_lookup(AT_FDCWD, name, flags, &nd); 1850d1811465SAl Viro if (!res) 1851d1811465SAl Viro *path = nd.path; 1852d1811465SAl Viro return res; 1853d1811465SAl Viro } 1854d1811465SAl Viro 185516f18200SJosef 'Jeff' Sipek /** 185616f18200SJosef 'Jeff' Sipek * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair 185716f18200SJosef 'Jeff' Sipek * @dentry: pointer to dentry of the base directory 185816f18200SJosef 'Jeff' Sipek * @mnt: pointer to vfs mount of the base directory 185916f18200SJosef 'Jeff' Sipek * @name: pointer to file name 186016f18200SJosef 'Jeff' Sipek * @flags: lookup flags 1861e0a01249SAl Viro * @path: pointer to struct path to fill 186216f18200SJosef 'Jeff' Sipek */ 186316f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt, 186416f18200SJosef 'Jeff' Sipek const char *name, unsigned int flags, 1865e0a01249SAl Viro struct path *path) 186616f18200SJosef 'Jeff' Sipek { 1867e0a01249SAl Viro struct nameidata nd; 1868e0a01249SAl Viro int err; 1869e0a01249SAl Viro nd.root.dentry = dentry; 1870e0a01249SAl Viro nd.root.mnt = mnt; 1871e0a01249SAl Viro BUG_ON(flags & LOOKUP_PARENT); 18725b6ca027SAl Viro /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */ 1873e0a01249SAl Viro err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd); 1874e0a01249SAl Viro if (!err) 1875e0a01249SAl Viro *path = nd.path; 1876e0a01249SAl Viro return err; 187716f18200SJosef 'Jeff' Sipek } 187816f18200SJosef 'Jeff' Sipek 1879057f6c01SJames Morris /* 1880057f6c01SJames Morris * Restricted form of lookup. Doesn't follow links, single-component only, 1881057f6c01SJames Morris * needs parent already locked. Doesn't follow mounts. 1882057f6c01SJames Morris * SMP-safe. 1883057f6c01SJames Morris */ 1884a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd) 18851da177e4SLinus Torvalds { 188672bd866aSAl Viro return __lookup_hash(&nd->last, nd->path.dentry, nd->flags); 18871da177e4SLinus Torvalds } 18881da177e4SLinus Torvalds 1889eead1911SChristoph Hellwig /** 1890a6b91919SRandy Dunlap * lookup_one_len - filesystem helper to lookup single pathname component 1891eead1911SChristoph Hellwig * @name: pathname component to lookup 1892eead1911SChristoph Hellwig * @base: base directory to lookup from 1893eead1911SChristoph Hellwig * @len: maximum length @len should be interpreted to 1894eead1911SChristoph Hellwig * 1895a6b91919SRandy Dunlap * Note that this routine is purely a helper for filesystem usage and should 1896a6b91919SRandy Dunlap * not be called by generic code. Also note that by using this function the 1897eead1911SChristoph Hellwig * nameidata argument is passed to the filesystem methods and a filesystem 1898eead1911SChristoph Hellwig * using this helper needs to be prepared for that. 1899eead1911SChristoph Hellwig */ 1900057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len) 1901057f6c01SJames Morris { 1902057f6c01SJames Morris struct qstr this; 19036a96ba54SAl Viro unsigned int c; 1904cda309deSMiklos Szeredi int err; 1905057f6c01SJames Morris 19062f9092e1SDavid Woodhouse WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex)); 19072f9092e1SDavid Woodhouse 19086a96ba54SAl Viro this.name = name; 19096a96ba54SAl Viro this.len = len; 19100145acc2SLinus Torvalds this.hash = full_name_hash(name, len); 19116a96ba54SAl Viro if (!len) 19126a96ba54SAl Viro return ERR_PTR(-EACCES); 19136a96ba54SAl Viro 19146a96ba54SAl Viro while (len--) { 19156a96ba54SAl Viro c = *(const unsigned char *)name++; 19166a96ba54SAl Viro if (c == '/' || c == '\0') 19176a96ba54SAl Viro return ERR_PTR(-EACCES); 19186a96ba54SAl Viro } 19195a202bcdSAl Viro /* 19205a202bcdSAl Viro * See if the low-level filesystem might want 19215a202bcdSAl Viro * to use its own hash.. 19225a202bcdSAl Viro */ 19235a202bcdSAl Viro if (base->d_flags & DCACHE_OP_HASH) { 19245a202bcdSAl Viro int err = base->d_op->d_hash(base, base->d_inode, &this); 19255a202bcdSAl Viro if (err < 0) 19265a202bcdSAl Viro return ERR_PTR(err); 19275a202bcdSAl Viro } 1928eead1911SChristoph Hellwig 1929cda309deSMiklos Szeredi err = inode_permission(base->d_inode, MAY_EXEC); 1930cda309deSMiklos Szeredi if (err) 1931cda309deSMiklos Szeredi return ERR_PTR(err); 1932cda309deSMiklos Szeredi 193372bd866aSAl Viro return __lookup_hash(&this, base, 0); 1934057f6c01SJames Morris } 1935057f6c01SJames Morris 19361fa1e7f6SAndy Whitcroft int user_path_at_empty(int dfd, const char __user *name, unsigned flags, 19371fa1e7f6SAndy Whitcroft struct path *path, int *empty) 19381da177e4SLinus Torvalds { 19392d8f3038SAl Viro struct nameidata nd; 19401fa1e7f6SAndy Whitcroft char *tmp = getname_flags(name, flags, empty); 19411da177e4SLinus Torvalds int err = PTR_ERR(tmp); 19421da177e4SLinus Torvalds if (!IS_ERR(tmp)) { 19432d8f3038SAl Viro 19442d8f3038SAl Viro BUG_ON(flags & LOOKUP_PARENT); 19452d8f3038SAl Viro 19462d8f3038SAl Viro err = do_path_lookup(dfd, tmp, flags, &nd); 19471da177e4SLinus Torvalds putname(tmp); 19482d8f3038SAl Viro if (!err) 19492d8f3038SAl Viro *path = nd.path; 19501da177e4SLinus Torvalds } 19511da177e4SLinus Torvalds return err; 19521da177e4SLinus Torvalds } 19531da177e4SLinus Torvalds 19541fa1e7f6SAndy Whitcroft int user_path_at(int dfd, const char __user *name, unsigned flags, 19551fa1e7f6SAndy Whitcroft struct path *path) 19561fa1e7f6SAndy Whitcroft { 1957f7493e5dSLinus Torvalds return user_path_at_empty(dfd, name, flags, path, NULL); 19581fa1e7f6SAndy Whitcroft } 19591fa1e7f6SAndy Whitcroft 19602ad94ae6SAl Viro static int user_path_parent(int dfd, const char __user *path, 19612ad94ae6SAl Viro struct nameidata *nd, char **name) 19622ad94ae6SAl Viro { 19632ad94ae6SAl Viro char *s = getname(path); 19642ad94ae6SAl Viro int error; 19652ad94ae6SAl Viro 19662ad94ae6SAl Viro if (IS_ERR(s)) 19672ad94ae6SAl Viro return PTR_ERR(s); 19682ad94ae6SAl Viro 19692ad94ae6SAl Viro error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd); 19702ad94ae6SAl Viro if (error) 19712ad94ae6SAl Viro putname(s); 19722ad94ae6SAl Viro else 19732ad94ae6SAl Viro *name = s; 19742ad94ae6SAl Viro 19752ad94ae6SAl Viro return error; 19762ad94ae6SAl Viro } 19772ad94ae6SAl Viro 19781da177e4SLinus Torvalds /* 19791da177e4SLinus Torvalds * It's inline, so penalty for filesystems that don't use sticky bit is 19801da177e4SLinus Torvalds * minimal. 19811da177e4SLinus Torvalds */ 19821da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode) 19831da177e4SLinus Torvalds { 19848e96e3b7SEric W. Biederman kuid_t fsuid = current_fsuid(); 1985da9592edSDavid Howells 19861da177e4SLinus Torvalds if (!(dir->i_mode & S_ISVTX)) 19871da177e4SLinus Torvalds return 0; 19888e96e3b7SEric W. Biederman if (uid_eq(inode->i_uid, fsuid)) 19891da177e4SLinus Torvalds return 0; 19908e96e3b7SEric W. Biederman if (uid_eq(dir->i_uid, fsuid)) 19911da177e4SLinus Torvalds return 0; 19921a48e2acSEric W. Biederman return !inode_capable(inode, CAP_FOWNER); 19931da177e4SLinus Torvalds } 19941da177e4SLinus Torvalds 19951da177e4SLinus Torvalds /* 19961da177e4SLinus Torvalds * Check whether we can remove a link victim from directory dir, check 19971da177e4SLinus Torvalds * whether the type of victim is right. 19981da177e4SLinus Torvalds * 1. We can't do it if dir is read-only (done in permission()) 19991da177e4SLinus Torvalds * 2. We should have write and exec permissions on dir 20001da177e4SLinus Torvalds * 3. We can't remove anything from append-only dir 20011da177e4SLinus Torvalds * 4. We can't do anything with immutable dir (done in permission()) 20021da177e4SLinus Torvalds * 5. If the sticky bit on dir is set we should either 20031da177e4SLinus Torvalds * a. be owner of dir, or 20041da177e4SLinus Torvalds * b. be owner of victim, or 20051da177e4SLinus Torvalds * c. have CAP_FOWNER capability 20061da177e4SLinus Torvalds * 6. If the victim is append-only or immutable we can't do antyhing with 20071da177e4SLinus Torvalds * links pointing to it. 20081da177e4SLinus Torvalds * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. 20091da177e4SLinus Torvalds * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. 20101da177e4SLinus Torvalds * 9. We can't remove a root or mountpoint. 20111da177e4SLinus Torvalds * 10. We don't allow removal of NFS sillyrenamed files; it's handled by 20121da177e4SLinus Torvalds * nfs_async_unlink(). 20131da177e4SLinus Torvalds */ 2014858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir) 20151da177e4SLinus Torvalds { 20161da177e4SLinus Torvalds int error; 20171da177e4SLinus Torvalds 20181da177e4SLinus Torvalds if (!victim->d_inode) 20191da177e4SLinus Torvalds return -ENOENT; 20201da177e4SLinus Torvalds 20211da177e4SLinus Torvalds BUG_ON(victim->d_parent->d_inode != dir); 2022cccc6bbaSAl Viro audit_inode_child(victim, dir); 20231da177e4SLinus Torvalds 2024f419a2e3SAl Viro error = inode_permission(dir, MAY_WRITE | MAY_EXEC); 20251da177e4SLinus Torvalds if (error) 20261da177e4SLinus Torvalds return error; 20271da177e4SLinus Torvalds if (IS_APPEND(dir)) 20281da177e4SLinus Torvalds return -EPERM; 20291da177e4SLinus Torvalds if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)|| 2030f9454548SHugh Dickins IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode)) 20311da177e4SLinus Torvalds return -EPERM; 20321da177e4SLinus Torvalds if (isdir) { 20331da177e4SLinus Torvalds if (!S_ISDIR(victim->d_inode->i_mode)) 20341da177e4SLinus Torvalds return -ENOTDIR; 20351da177e4SLinus Torvalds if (IS_ROOT(victim)) 20361da177e4SLinus Torvalds return -EBUSY; 20371da177e4SLinus Torvalds } else if (S_ISDIR(victim->d_inode->i_mode)) 20381da177e4SLinus Torvalds return -EISDIR; 20391da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 20401da177e4SLinus Torvalds return -ENOENT; 20411da177e4SLinus Torvalds if (victim->d_flags & DCACHE_NFSFS_RENAMED) 20421da177e4SLinus Torvalds return -EBUSY; 20431da177e4SLinus Torvalds return 0; 20441da177e4SLinus Torvalds } 20451da177e4SLinus Torvalds 20461da177e4SLinus Torvalds /* Check whether we can create an object with dentry child in directory 20471da177e4SLinus Torvalds * dir. 20481da177e4SLinus Torvalds * 1. We can't do it if child already exists (open has special treatment for 20491da177e4SLinus Torvalds * this case, but since we are inlined it's OK) 20501da177e4SLinus Torvalds * 2. We can't do it if dir is read-only (done in permission()) 20511da177e4SLinus Torvalds * 3. We should have write and exec permissions on dir 20521da177e4SLinus Torvalds * 4. We can't do it if dir is immutable (done in permission()) 20531da177e4SLinus Torvalds */ 2054a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child) 20551da177e4SLinus Torvalds { 20561da177e4SLinus Torvalds if (child->d_inode) 20571da177e4SLinus Torvalds return -EEXIST; 20581da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 20591da177e4SLinus Torvalds return -ENOENT; 2060f419a2e3SAl Viro return inode_permission(dir, MAY_WRITE | MAY_EXEC); 20611da177e4SLinus Torvalds } 20621da177e4SLinus Torvalds 20631da177e4SLinus Torvalds /* 20641da177e4SLinus Torvalds * p1 and p2 should be directories on the same fs. 20651da177e4SLinus Torvalds */ 20661da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2) 20671da177e4SLinus Torvalds { 20681da177e4SLinus Torvalds struct dentry *p; 20691da177e4SLinus Torvalds 20701da177e4SLinus Torvalds if (p1 == p2) { 2071f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 20721da177e4SLinus Torvalds return NULL; 20731da177e4SLinus Torvalds } 20741da177e4SLinus Torvalds 2075a11f3a05SArjan van de Ven mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 20761da177e4SLinus Torvalds 2077e2761a11SOGAWA Hirofumi p = d_ancestor(p2, p1); 2078e2761a11SOGAWA Hirofumi if (p) { 2079f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT); 2080f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD); 20811da177e4SLinus Torvalds return p; 20821da177e4SLinus Torvalds } 20831da177e4SLinus Torvalds 2084e2761a11SOGAWA Hirofumi p = d_ancestor(p1, p2); 2085e2761a11SOGAWA Hirofumi if (p) { 2086f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 2087f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 20881da177e4SLinus Torvalds return p; 20891da177e4SLinus Torvalds } 20901da177e4SLinus Torvalds 2091f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 2092f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 20931da177e4SLinus Torvalds return NULL; 20941da177e4SLinus Torvalds } 20951da177e4SLinus Torvalds 20961da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2) 20971da177e4SLinus Torvalds { 20981b1dcc1bSJes Sorensen mutex_unlock(&p1->d_inode->i_mutex); 20991da177e4SLinus Torvalds if (p1 != p2) { 21001b1dcc1bSJes Sorensen mutex_unlock(&p2->d_inode->i_mutex); 2101a11f3a05SArjan van de Ven mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 21021da177e4SLinus Torvalds } 21031da177e4SLinus Torvalds } 21041da177e4SLinus Torvalds 21054acdaf27SAl Viro int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode, 2106312b63fbSAl Viro bool want_excl) 21071da177e4SLinus Torvalds { 2108a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 21091da177e4SLinus Torvalds if (error) 21101da177e4SLinus Torvalds return error; 21111da177e4SLinus Torvalds 2112acfa4380SAl Viro if (!dir->i_op->create) 21131da177e4SLinus Torvalds return -EACCES; /* shouldn't it be ENOSYS? */ 21141da177e4SLinus Torvalds mode &= S_IALLUGO; 21151da177e4SLinus Torvalds mode |= S_IFREG; 21161da177e4SLinus Torvalds error = security_inode_create(dir, dentry, mode); 21171da177e4SLinus Torvalds if (error) 21181da177e4SLinus Torvalds return error; 2119312b63fbSAl Viro error = dir->i_op->create(dir, dentry, mode, want_excl); 2120a74574aaSStephen Smalley if (!error) 2121f38aa942SAmy Griffis fsnotify_create(dir, dentry); 21221da177e4SLinus Torvalds return error; 21231da177e4SLinus Torvalds } 21241da177e4SLinus Torvalds 212573d049a4SAl Viro static int may_open(struct path *path, int acc_mode, int flag) 21261da177e4SLinus Torvalds { 21273fb64190SChristoph Hellwig struct dentry *dentry = path->dentry; 21281da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 21291da177e4SLinus Torvalds int error; 21301da177e4SLinus Torvalds 2131bcda7652SAl Viro /* O_PATH? */ 2132bcda7652SAl Viro if (!acc_mode) 2133bcda7652SAl Viro return 0; 2134bcda7652SAl Viro 21351da177e4SLinus Torvalds if (!inode) 21361da177e4SLinus Torvalds return -ENOENT; 21371da177e4SLinus Torvalds 2138c8fe8f30SChristoph Hellwig switch (inode->i_mode & S_IFMT) { 2139c8fe8f30SChristoph Hellwig case S_IFLNK: 21401da177e4SLinus Torvalds return -ELOOP; 2141c8fe8f30SChristoph Hellwig case S_IFDIR: 2142c8fe8f30SChristoph Hellwig if (acc_mode & MAY_WRITE) 21431da177e4SLinus Torvalds return -EISDIR; 2144c8fe8f30SChristoph Hellwig break; 2145c8fe8f30SChristoph Hellwig case S_IFBLK: 2146c8fe8f30SChristoph Hellwig case S_IFCHR: 21473fb64190SChristoph Hellwig if (path->mnt->mnt_flags & MNT_NODEV) 21481da177e4SLinus Torvalds return -EACCES; 2149c8fe8f30SChristoph Hellwig /*FALLTHRU*/ 2150c8fe8f30SChristoph Hellwig case S_IFIFO: 2151c8fe8f30SChristoph Hellwig case S_IFSOCK: 21521da177e4SLinus Torvalds flag &= ~O_TRUNC; 2153c8fe8f30SChristoph Hellwig break; 21544a3fd211SDave Hansen } 2155b41572e9SDave Hansen 21563fb64190SChristoph Hellwig error = inode_permission(inode, acc_mode); 2157b41572e9SDave Hansen if (error) 2158b41572e9SDave Hansen return error; 21596146f0d5SMimi Zohar 21601da177e4SLinus Torvalds /* 21611da177e4SLinus Torvalds * An append-only file must be opened in append mode for writing. 21621da177e4SLinus Torvalds */ 21631da177e4SLinus Torvalds if (IS_APPEND(inode)) { 21648737c930SAl Viro if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND)) 21657715b521SAl Viro return -EPERM; 21661da177e4SLinus Torvalds if (flag & O_TRUNC) 21677715b521SAl Viro return -EPERM; 21681da177e4SLinus Torvalds } 21691da177e4SLinus Torvalds 21701da177e4SLinus Torvalds /* O_NOATIME can only be set by the owner or superuser */ 21712e149670SSerge E. Hallyn if (flag & O_NOATIME && !inode_owner_or_capable(inode)) 21727715b521SAl Viro return -EPERM; 21731da177e4SLinus Torvalds 2174f3c7691eSJ. Bruce Fields return 0; 21757715b521SAl Viro } 21767715b521SAl Viro 2177e1181ee6SJeff Layton static int handle_truncate(struct file *filp) 21787715b521SAl Viro { 2179e1181ee6SJeff Layton struct path *path = &filp->f_path; 21807715b521SAl Viro struct inode *inode = path->dentry->d_inode; 21817715b521SAl Viro int error = get_write_access(inode); 21821da177e4SLinus Torvalds if (error) 21837715b521SAl Viro return error; 21841da177e4SLinus Torvalds /* 21851da177e4SLinus Torvalds * Refuse to truncate files with mandatory locks held on them. 21861da177e4SLinus Torvalds */ 21871da177e4SLinus Torvalds error = locks_verify_locked(inode); 2188be6d3e56SKentaro Takeda if (!error) 2189ea0d3ab2STetsuo Handa error = security_path_truncate(path); 21901da177e4SLinus Torvalds if (!error) { 21917715b521SAl Viro error = do_truncate(path->dentry, 0, 2192d139d7ffSMiklos Szeredi ATTR_MTIME|ATTR_CTIME|ATTR_OPEN, 2193e1181ee6SJeff Layton filp); 21941da177e4SLinus Torvalds } 21951da177e4SLinus Torvalds put_write_access(inode); 2196acd0c935SMimi Zohar return error; 21971da177e4SLinus Torvalds } 21981da177e4SLinus Torvalds 2199d57999e1SDave Hansen static inline int open_to_namei_flags(int flag) 2200d57999e1SDave Hansen { 22018a5e929dSAl Viro if ((flag & O_ACCMODE) == 3) 22028a5e929dSAl Viro flag--; 2203d57999e1SDave Hansen return flag; 2204d57999e1SDave Hansen } 2205d57999e1SDave Hansen 2206d18e9008SMiklos Szeredi static int may_o_create(struct path *dir, struct dentry *dentry, umode_t mode) 2207d18e9008SMiklos Szeredi { 2208d18e9008SMiklos Szeredi int error = security_path_mknod(dir, dentry, mode, 0); 2209d18e9008SMiklos Szeredi if (error) 2210d18e9008SMiklos Szeredi return error; 2211d18e9008SMiklos Szeredi 2212d18e9008SMiklos Szeredi error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC); 2213d18e9008SMiklos Szeredi if (error) 2214d18e9008SMiklos Szeredi return error; 2215d18e9008SMiklos Szeredi 2216d18e9008SMiklos Szeredi return security_inode_create(dir->dentry->d_inode, dentry, mode); 2217d18e9008SMiklos Szeredi } 2218d18e9008SMiklos Szeredi 22191acf0af9SDavid Howells /* 22201acf0af9SDavid Howells * Attempt to atomically look up, create and open a file from a negative 22211acf0af9SDavid Howells * dentry. 22221acf0af9SDavid Howells * 22231acf0af9SDavid Howells * Returns 0 if successful. The file will have been created and attached to 22241acf0af9SDavid Howells * @file by the filesystem calling finish_open(). 22251acf0af9SDavid Howells * 22261acf0af9SDavid Howells * Returns 1 if the file was looked up only or didn't need creating. The 22271acf0af9SDavid Howells * caller will need to perform the open themselves. @path will have been 22281acf0af9SDavid Howells * updated to point to the new dentry. This may be negative. 22291acf0af9SDavid Howells * 22301acf0af9SDavid Howells * Returns an error code otherwise. 22311acf0af9SDavid Howells */ 22322675a4ebSAl Viro static int atomic_open(struct nameidata *nd, struct dentry *dentry, 223330d90494SAl Viro struct path *path, struct file *file, 2234015c3bbcSMiklos Szeredi const struct open_flags *op, 223577d660a8SMiklos Szeredi bool *want_write, bool need_lookup, 223647237687SAl Viro int *opened) 2237d18e9008SMiklos Szeredi { 2238d18e9008SMiklos Szeredi struct inode *dir = nd->path.dentry->d_inode; 2239d18e9008SMiklos Szeredi unsigned open_flag = open_to_namei_flags(op->open_flag); 2240d18e9008SMiklos Szeredi umode_t mode; 2241d18e9008SMiklos Szeredi int error; 2242d18e9008SMiklos Szeredi int acc_mode; 2243d18e9008SMiklos Szeredi int create_error = 0; 2244d18e9008SMiklos Szeredi struct dentry *const DENTRY_NOT_SET = (void *) -1UL; 2245d18e9008SMiklos Szeredi 2246d18e9008SMiklos Szeredi BUG_ON(dentry->d_inode); 2247d18e9008SMiklos Szeredi 2248d18e9008SMiklos Szeredi /* Don't create child dentry for a dead directory. */ 2249d18e9008SMiklos Szeredi if (unlikely(IS_DEADDIR(dir))) { 22502675a4ebSAl Viro error = -ENOENT; 2251d18e9008SMiklos Szeredi goto out; 2252d18e9008SMiklos Szeredi } 2253d18e9008SMiklos Szeredi 2254d18e9008SMiklos Szeredi mode = op->mode & S_IALLUGO; 2255d18e9008SMiklos Szeredi if ((open_flag & O_CREAT) && !IS_POSIXACL(dir)) 2256d18e9008SMiklos Szeredi mode &= ~current_umask(); 2257d18e9008SMiklos Szeredi 2258d18e9008SMiklos Szeredi if (open_flag & O_EXCL) { 2259d18e9008SMiklos Szeredi open_flag &= ~O_TRUNC; 226047237687SAl Viro *opened |= FILE_CREATED; 2261d18e9008SMiklos Szeredi } 2262d18e9008SMiklos Szeredi 2263d18e9008SMiklos Szeredi /* 2264d18e9008SMiklos Szeredi * Checking write permission is tricky, bacuse we don't know if we are 2265d18e9008SMiklos Szeredi * going to actually need it: O_CREAT opens should work as long as the 2266d18e9008SMiklos Szeredi * file exists. But checking existence breaks atomicity. The trick is 2267d18e9008SMiklos Szeredi * to check access and if not granted clear O_CREAT from the flags. 2268d18e9008SMiklos Szeredi * 2269d18e9008SMiklos Szeredi * Another problem is returing the "right" error value (e.g. for an 2270d18e9008SMiklos Szeredi * O_EXCL open we want to return EEXIST not EROFS). 2271d18e9008SMiklos Szeredi */ 2272d18e9008SMiklos Szeredi if ((open_flag & (O_CREAT | O_TRUNC)) || 2273d18e9008SMiklos Szeredi (open_flag & O_ACCMODE) != O_RDONLY) { 2274d18e9008SMiklos Szeredi error = mnt_want_write(nd->path.mnt); 2275d18e9008SMiklos Szeredi if (!error) { 227677d660a8SMiklos Szeredi *want_write = true; 2277d18e9008SMiklos Szeredi } else if (!(open_flag & O_CREAT)) { 2278d18e9008SMiklos Szeredi /* 2279d18e9008SMiklos Szeredi * No O_CREATE -> atomicity not a requirement -> fall 2280d18e9008SMiklos Szeredi * back to lookup + open 2281d18e9008SMiklos Szeredi */ 2282d18e9008SMiklos Szeredi goto no_open; 2283d18e9008SMiklos Szeredi } else if (open_flag & (O_EXCL | O_TRUNC)) { 2284d18e9008SMiklos Szeredi /* Fall back and fail with the right error */ 2285d18e9008SMiklos Szeredi create_error = error; 2286d18e9008SMiklos Szeredi goto no_open; 2287d18e9008SMiklos Szeredi } else { 2288d18e9008SMiklos Szeredi /* No side effects, safe to clear O_CREAT */ 2289d18e9008SMiklos Szeredi create_error = error; 2290d18e9008SMiklos Szeredi open_flag &= ~O_CREAT; 2291d18e9008SMiklos Szeredi } 2292d18e9008SMiklos Szeredi } 2293d18e9008SMiklos Szeredi 2294d18e9008SMiklos Szeredi if (open_flag & O_CREAT) { 2295d18e9008SMiklos Szeredi error = may_o_create(&nd->path, dentry, op->mode); 2296d18e9008SMiklos Szeredi if (error) { 2297d18e9008SMiklos Szeredi create_error = error; 2298d18e9008SMiklos Szeredi if (open_flag & O_EXCL) 2299d18e9008SMiklos Szeredi goto no_open; 2300d18e9008SMiklos Szeredi open_flag &= ~O_CREAT; 2301d18e9008SMiklos Szeredi } 2302d18e9008SMiklos Szeredi } 2303d18e9008SMiklos Szeredi 2304d18e9008SMiklos Szeredi if (nd->flags & LOOKUP_DIRECTORY) 2305d18e9008SMiklos Szeredi open_flag |= O_DIRECTORY; 2306d18e9008SMiklos Szeredi 230730d90494SAl Viro file->f_path.dentry = DENTRY_NOT_SET; 230830d90494SAl Viro file->f_path.mnt = nd->path.mnt; 230930d90494SAl Viro error = dir->i_op->atomic_open(dir, dentry, file, open_flag, mode, 231047237687SAl Viro opened); 2311d9585277SAl Viro if (error < 0) { 2312d9585277SAl Viro if (create_error && error == -ENOENT) 2313d9585277SAl Viro error = create_error; 2314d18e9008SMiklos Szeredi goto out; 2315d18e9008SMiklos Szeredi } 2316d18e9008SMiklos Szeredi 2317d18e9008SMiklos Szeredi acc_mode = op->acc_mode; 231847237687SAl Viro if (*opened & FILE_CREATED) { 2319d18e9008SMiklos Szeredi fsnotify_create(dir, dentry); 2320d18e9008SMiklos Szeredi acc_mode = MAY_OPEN; 2321d18e9008SMiklos Szeredi } 2322d18e9008SMiklos Szeredi 2323d9585277SAl Viro if (error) { /* returned 1, that is */ 232430d90494SAl Viro if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) { 23252675a4ebSAl Viro error = -EIO; 2326d18e9008SMiklos Szeredi goto out; 2327d18e9008SMiklos Szeredi } 232830d90494SAl Viro if (file->f_path.dentry) { 2329d18e9008SMiklos Szeredi dput(dentry); 233030d90494SAl Viro dentry = file->f_path.dentry; 2331d18e9008SMiklos Szeredi } 2332d18e9008SMiklos Szeredi goto looked_up; 2333d18e9008SMiklos Szeredi } 2334d18e9008SMiklos Szeredi 2335d18e9008SMiklos Szeredi /* 2336d18e9008SMiklos Szeredi * We didn't have the inode before the open, so check open permission 2337d18e9008SMiklos Szeredi * here. 2338d18e9008SMiklos Szeredi */ 23392675a4ebSAl Viro error = may_open(&file->f_path, acc_mode, open_flag); 23402675a4ebSAl Viro if (error) 23412675a4ebSAl Viro fput(file); 2342d18e9008SMiklos Szeredi 2343d18e9008SMiklos Szeredi out: 2344d18e9008SMiklos Szeredi dput(dentry); 23452675a4ebSAl Viro return error; 2346d18e9008SMiklos Szeredi 2347d18e9008SMiklos Szeredi no_open: 2348d18e9008SMiklos Szeredi if (need_lookup) { 234972bd866aSAl Viro dentry = lookup_real(dir, dentry, nd->flags); 2350d18e9008SMiklos Szeredi if (IS_ERR(dentry)) 23512675a4ebSAl Viro return PTR_ERR(dentry); 2352d18e9008SMiklos Szeredi 2353d18e9008SMiklos Szeredi if (create_error) { 2354d18e9008SMiklos Szeredi int open_flag = op->open_flag; 2355d18e9008SMiklos Szeredi 23562675a4ebSAl Viro error = create_error; 2357d18e9008SMiklos Szeredi if ((open_flag & O_EXCL)) { 2358d18e9008SMiklos Szeredi if (!dentry->d_inode) 2359d18e9008SMiklos Szeredi goto out; 2360d18e9008SMiklos Szeredi } else if (!dentry->d_inode) { 2361d18e9008SMiklos Szeredi goto out; 2362d18e9008SMiklos Szeredi } else if ((open_flag & O_TRUNC) && 2363d18e9008SMiklos Szeredi S_ISREG(dentry->d_inode->i_mode)) { 2364d18e9008SMiklos Szeredi goto out; 2365d18e9008SMiklos Szeredi } 2366d18e9008SMiklos Szeredi /* will fail later, go on to get the right error */ 2367d18e9008SMiklos Szeredi } 2368d18e9008SMiklos Szeredi } 2369d18e9008SMiklos Szeredi looked_up: 2370d18e9008SMiklos Szeredi path->dentry = dentry; 2371d18e9008SMiklos Szeredi path->mnt = nd->path.mnt; 23722675a4ebSAl Viro return 1; 2373d18e9008SMiklos Szeredi } 2374d18e9008SMiklos Szeredi 237531e6b01fSNick Piggin /* 23761acf0af9SDavid Howells * Look up and maybe create and open the last component. 2377d58ffd35SMiklos Szeredi * 2378d58ffd35SMiklos Szeredi * Must be called with i_mutex held on parent. 2379d58ffd35SMiklos Szeredi * 23801acf0af9SDavid Howells * Returns 0 if the file was successfully atomically created (if necessary) and 23811acf0af9SDavid Howells * opened. In this case the file will be returned attached to @file. 23821acf0af9SDavid Howells * 23831acf0af9SDavid Howells * Returns 1 if the file was not completely opened at this time, though lookups 23841acf0af9SDavid Howells * and creations will have been performed and the dentry returned in @path will 23851acf0af9SDavid Howells * be positive upon return if O_CREAT was specified. If O_CREAT wasn't 23861acf0af9SDavid Howells * specified then a negative dentry may be returned. 23871acf0af9SDavid Howells * 23881acf0af9SDavid Howells * An error code is returned otherwise. 23891acf0af9SDavid Howells * 23901acf0af9SDavid Howells * FILE_CREATE will be set in @*opened if the dentry was created and will be 23911acf0af9SDavid Howells * cleared otherwise prior to returning. 2392d58ffd35SMiklos Szeredi */ 23932675a4ebSAl Viro static int lookup_open(struct nameidata *nd, struct path *path, 239430d90494SAl Viro struct file *file, 2395d58ffd35SMiklos Szeredi const struct open_flags *op, 239647237687SAl Viro bool *want_write, int *opened) 2397d58ffd35SMiklos Szeredi { 2398d58ffd35SMiklos Szeredi struct dentry *dir = nd->path.dentry; 239954ef4872SMiklos Szeredi struct inode *dir_inode = dir->d_inode; 2400d58ffd35SMiklos Szeredi struct dentry *dentry; 2401d58ffd35SMiklos Szeredi int error; 240254ef4872SMiklos Szeredi bool need_lookup; 2403d58ffd35SMiklos Szeredi 240447237687SAl Viro *opened &= ~FILE_CREATED; 2405201f956eSAl Viro dentry = lookup_dcache(&nd->last, dir, nd->flags, &need_lookup); 2406d58ffd35SMiklos Szeredi if (IS_ERR(dentry)) 24072675a4ebSAl Viro return PTR_ERR(dentry); 2408d58ffd35SMiklos Szeredi 2409d18e9008SMiklos Szeredi /* Cached positive dentry: will open in f_op->open */ 2410d18e9008SMiklos Szeredi if (!need_lookup && dentry->d_inode) 2411d18e9008SMiklos Szeredi goto out_no_open; 2412d18e9008SMiklos Szeredi 2413d18e9008SMiklos Szeredi if ((nd->flags & LOOKUP_OPEN) && dir_inode->i_op->atomic_open) { 241430d90494SAl Viro return atomic_open(nd, dentry, path, file, op, want_write, 241547237687SAl Viro need_lookup, opened); 2416d18e9008SMiklos Szeredi } 2417d18e9008SMiklos Szeredi 241854ef4872SMiklos Szeredi if (need_lookup) { 241954ef4872SMiklos Szeredi BUG_ON(dentry->d_inode); 242054ef4872SMiklos Szeredi 242172bd866aSAl Viro dentry = lookup_real(dir_inode, dentry, nd->flags); 242254ef4872SMiklos Szeredi if (IS_ERR(dentry)) 24232675a4ebSAl Viro return PTR_ERR(dentry); 242454ef4872SMiklos Szeredi } 242554ef4872SMiklos Szeredi 2426d58ffd35SMiklos Szeredi /* Negative dentry, just create the file */ 2427d58ffd35SMiklos Szeredi if (!dentry->d_inode && (op->open_flag & O_CREAT)) { 2428d58ffd35SMiklos Szeredi umode_t mode = op->mode; 2429d58ffd35SMiklos Szeredi if (!IS_POSIXACL(dir->d_inode)) 2430d58ffd35SMiklos Szeredi mode &= ~current_umask(); 2431d58ffd35SMiklos Szeredi /* 2432d58ffd35SMiklos Szeredi * This write is needed to ensure that a 2433d58ffd35SMiklos Szeredi * rw->ro transition does not occur between 2434d58ffd35SMiklos Szeredi * the time when the file is created and when 2435d58ffd35SMiklos Szeredi * a permanent write count is taken through 2436015c3bbcSMiklos Szeredi * the 'struct file' in finish_open(). 2437d58ffd35SMiklos Szeredi */ 2438d58ffd35SMiklos Szeredi error = mnt_want_write(nd->path.mnt); 2439d58ffd35SMiklos Szeredi if (error) 2440d58ffd35SMiklos Szeredi goto out_dput; 244177d660a8SMiklos Szeredi *want_write = true; 244247237687SAl Viro *opened |= FILE_CREATED; 2443d58ffd35SMiklos Szeredi error = security_path_mknod(&nd->path, dentry, mode, 0); 2444d58ffd35SMiklos Szeredi if (error) 2445d58ffd35SMiklos Szeredi goto out_dput; 2446312b63fbSAl Viro error = vfs_create(dir->d_inode, dentry, mode, 2447312b63fbSAl Viro nd->flags & LOOKUP_EXCL); 2448d58ffd35SMiklos Szeredi if (error) 2449d58ffd35SMiklos Szeredi goto out_dput; 2450d58ffd35SMiklos Szeredi } 2451d18e9008SMiklos Szeredi out_no_open: 2452d58ffd35SMiklos Szeredi path->dentry = dentry; 2453d58ffd35SMiklos Szeredi path->mnt = nd->path.mnt; 24542675a4ebSAl Viro return 1; 2455d58ffd35SMiklos Szeredi 2456d58ffd35SMiklos Szeredi out_dput: 2457d58ffd35SMiklos Szeredi dput(dentry); 24582675a4ebSAl Viro return error; 2459d58ffd35SMiklos Szeredi } 2460d58ffd35SMiklos Szeredi 2461d58ffd35SMiklos Szeredi /* 2462fe2d35ffSAl Viro * Handle the last step of open() 246331e6b01fSNick Piggin */ 24642675a4ebSAl Viro static int do_last(struct nameidata *nd, struct path *path, 246530d90494SAl Viro struct file *file, const struct open_flags *op, 246647237687SAl Viro int *opened, const char *pathname) 2467fb1cc555SAl Viro { 2468a1e28038SAl Viro struct dentry *dir = nd->path.dentry; 2469ca344a89SAl Viro int open_flag = op->open_flag; 247077d660a8SMiklos Szeredi bool will_truncate = (open_flag & O_TRUNC) != 0; 247177d660a8SMiklos Szeredi bool want_write = false; 2472bcda7652SAl Viro int acc_mode = op->acc_mode; 2473a1eb3315SMiklos Szeredi struct inode *inode; 247477d660a8SMiklos Szeredi bool symlink_ok = false; 247516b1c1cdSMiklos Szeredi struct path save_parent = { .dentry = NULL, .mnt = NULL }; 247616b1c1cdSMiklos Szeredi bool retried = false; 247716c2cd71SAl Viro int error; 2478fb1cc555SAl Viro 2479c3e380b0SAl Viro nd->flags &= ~LOOKUP_PARENT; 2480c3e380b0SAl Viro nd->flags |= op->intent; 2481c3e380b0SAl Viro 24821f36f774SAl Viro switch (nd->last_type) { 24831f36f774SAl Viro case LAST_DOTDOT: 2484176306f5SNeil Brown case LAST_DOT: 2485fe2d35ffSAl Viro error = handle_dots(nd, nd->last_type); 2486fe2d35ffSAl Viro if (error) 24872675a4ebSAl Viro return error; 24881f36f774SAl Viro /* fallthrough */ 24891f36f774SAl Viro case LAST_ROOT: 24909f1fafeeSAl Viro error = complete_walk(nd); 249116c2cd71SAl Viro if (error) 24922675a4ebSAl Viro return error; 2493fe2d35ffSAl Viro audit_inode(pathname, nd->path.dentry); 2494ca344a89SAl Viro if (open_flag & O_CREAT) { 249516c2cd71SAl Viro error = -EISDIR; 24962675a4ebSAl Viro goto out; 2497fe2d35ffSAl Viro } 2498e83db167SMiklos Szeredi goto finish_open; 24991f36f774SAl Viro case LAST_BIND: 25009f1fafeeSAl Viro error = complete_walk(nd); 250116c2cd71SAl Viro if (error) 25022675a4ebSAl Viro return error; 25031f36f774SAl Viro audit_inode(pathname, dir); 2504e83db167SMiklos Szeredi goto finish_open; 25051f36f774SAl Viro } 2506a2c36b45SAl Viro 2507ca344a89SAl Viro if (!(open_flag & O_CREAT)) { 2508fe2d35ffSAl Viro if (nd->last.name[nd->last.len]) 2509fe2d35ffSAl Viro nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 2510bcda7652SAl Viro if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW)) 251177d660a8SMiklos Szeredi symlink_ok = true; 2512fe2d35ffSAl Viro /* we _can_ be in RCU mode here */ 2513a1eb3315SMiklos Szeredi error = lookup_fast(nd, &nd->last, path, &inode); 251471574865SMiklos Szeredi if (likely(!error)) 251571574865SMiklos Szeredi goto finish_lookup; 251671574865SMiklos Szeredi 2517ce57dfc1SAl Viro if (error < 0) 25182675a4ebSAl Viro goto out; 2519a1eb3315SMiklos Szeredi 252037d7fffcSMiklos Szeredi BUG_ON(nd->inode != dir->d_inode); 2521b6183df7SMiklos Szeredi } else { 2522fe2d35ffSAl Viro /* create side of things */ 2523a3fbbde7SAl Viro /* 2524b6183df7SMiklos Szeredi * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED 2525b6183df7SMiklos Szeredi * has been cleared when we got to the last component we are 2526b6183df7SMiklos Szeredi * about to look up 2527a3fbbde7SAl Viro */ 25289f1fafeeSAl Viro error = complete_walk(nd); 25299f1fafeeSAl Viro if (error) 25302675a4ebSAl Viro return error; 2531fe2d35ffSAl Viro 2532fe2d35ffSAl Viro audit_inode(pathname, dir); 253316c2cd71SAl Viro error = -EISDIR; 25341f36f774SAl Viro /* trailing slashes? */ 253531e6b01fSNick Piggin if (nd->last.name[nd->last.len]) 25362675a4ebSAl Viro goto out; 2537b6183df7SMiklos Szeredi } 25381f36f774SAl Viro 253916b1c1cdSMiklos Szeredi retry_lookup: 2540a1e28038SAl Viro mutex_lock(&dir->d_inode->i_mutex); 25412675a4ebSAl Viro error = lookup_open(nd, path, file, op, &want_write, opened); 2542fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2543fb1cc555SAl Viro 25442675a4ebSAl Viro if (error <= 0) { 25452675a4ebSAl Viro if (error) 2546d58ffd35SMiklos Szeredi goto out; 25476c0d46c4SAl Viro 254847237687SAl Viro if ((*opened & FILE_CREATED) || 25492675a4ebSAl Viro !S_ISREG(file->f_path.dentry->d_inode->i_mode)) 255077d660a8SMiklos Szeredi will_truncate = false; 2551d18e9008SMiklos Szeredi 25522675a4ebSAl Viro audit_inode(pathname, file->f_path.dentry); 2553d18e9008SMiklos Szeredi goto opened; 2554d18e9008SMiklos Szeredi } 2555d18e9008SMiklos Szeredi 255647237687SAl Viro if (*opened & FILE_CREATED) { 25579b44f1b3SAl Viro /* Don't check for write permission, don't truncate */ 2558ca344a89SAl Viro open_flag &= ~O_TRUNC; 255977d660a8SMiklos Szeredi will_truncate = false; 2560bcda7652SAl Viro acc_mode = MAY_OPEN; 2561d58ffd35SMiklos Szeredi path_to_nameidata(path, nd); 2562e83db167SMiklos Szeredi goto finish_open_created; 2563fb1cc555SAl Viro } 2564fb1cc555SAl Viro 2565fb1cc555SAl Viro /* 2566fb1cc555SAl Viro * It already exists. 2567fb1cc555SAl Viro */ 2568fb1cc555SAl Viro audit_inode(pathname, path->dentry); 2569fb1cc555SAl Viro 2570d18e9008SMiklos Szeredi /* 2571d18e9008SMiklos Szeredi * If atomic_open() acquired write access it is dropped now due to 2572d18e9008SMiklos Szeredi * possible mount and symlink following (this might be optimized away if 2573d18e9008SMiklos Szeredi * necessary...) 2574d18e9008SMiklos Szeredi */ 2575d18e9008SMiklos Szeredi if (want_write) { 2576d18e9008SMiklos Szeredi mnt_drop_write(nd->path.mnt); 257777d660a8SMiklos Szeredi want_write = false; 2578d18e9008SMiklos Szeredi } 2579d18e9008SMiklos Szeredi 2580fb1cc555SAl Viro error = -EEXIST; 2581ca344a89SAl Viro if (open_flag & O_EXCL) 2582fb1cc555SAl Viro goto exit_dput; 2583fb1cc555SAl Viro 25849875cf80SDavid Howells error = follow_managed(path, nd->flags); 25859875cf80SDavid Howells if (error < 0) 2586fb1cc555SAl Viro goto exit_dput; 2587fb1cc555SAl Viro 2588a3fbbde7SAl Viro if (error) 2589a3fbbde7SAl Viro nd->flags |= LOOKUP_JUMPED; 2590a3fbbde7SAl Viro 2591decf3400SMiklos Szeredi BUG_ON(nd->flags & LOOKUP_RCU); 2592decf3400SMiklos Szeredi inode = path->dentry->d_inode; 25935f5daac1SMiklos Szeredi finish_lookup: 25945f5daac1SMiklos Szeredi /* we _can_ be in RCU mode here */ 2595fb1cc555SAl Viro error = -ENOENT; 259654c33e7fSMiklos Szeredi if (!inode) { 259754c33e7fSMiklos Szeredi path_to_nameidata(path, nd); 25982675a4ebSAl Viro goto out; 259954c33e7fSMiklos Szeredi } 26009e67f361SAl Viro 2601d45ea867SMiklos Szeredi if (should_follow_link(inode, !symlink_ok)) { 2602d45ea867SMiklos Szeredi if (nd->flags & LOOKUP_RCU) { 2603d45ea867SMiklos Szeredi if (unlikely(unlazy_walk(nd, path->dentry))) { 2604d45ea867SMiklos Szeredi error = -ECHILD; 26052675a4ebSAl Viro goto out; 2606d45ea867SMiklos Szeredi } 2607d45ea867SMiklos Szeredi } 2608d45ea867SMiklos Szeredi BUG_ON(inode != path->dentry->d_inode); 26092675a4ebSAl Viro return 1; 2610d45ea867SMiklos Szeredi } 2611fb1cc555SAl Viro 261216b1c1cdSMiklos Szeredi if ((nd->flags & LOOKUP_RCU) || nd->path.mnt != path->mnt) { 2613fb1cc555SAl Viro path_to_nameidata(path, nd); 261416b1c1cdSMiklos Szeredi } else { 261516b1c1cdSMiklos Szeredi save_parent.dentry = nd->path.dentry; 261616b1c1cdSMiklos Szeredi save_parent.mnt = mntget(path->mnt); 261716b1c1cdSMiklos Szeredi nd->path.dentry = path->dentry; 261816b1c1cdSMiklos Szeredi 261916b1c1cdSMiklos Szeredi } 2620decf3400SMiklos Szeredi nd->inode = inode; 2621a3fbbde7SAl Viro /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */ 2622a3fbbde7SAl Viro error = complete_walk(nd); 262316b1c1cdSMiklos Szeredi if (error) { 262416b1c1cdSMiklos Szeredi path_put(&save_parent); 26252675a4ebSAl Viro return error; 262616b1c1cdSMiklos Szeredi } 2627fb1cc555SAl Viro error = -EISDIR; 2628050ac841SMiklos Szeredi if ((open_flag & O_CREAT) && S_ISDIR(nd->inode->i_mode)) 26292675a4ebSAl Viro goto out; 2630af2f5542SMiklos Szeredi error = -ENOTDIR; 2631af2f5542SMiklos Szeredi if ((nd->flags & LOOKUP_DIRECTORY) && !nd->inode->i_op->lookup) 26322675a4ebSAl Viro goto out; 2633d7fdd7f6SMiklos Szeredi audit_inode(pathname, nd->path.dentry); 2634e83db167SMiklos Szeredi finish_open: 26356c0d46c4SAl Viro if (!S_ISREG(nd->inode->i_mode)) 263677d660a8SMiklos Szeredi will_truncate = false; 26376c0d46c4SAl Viro 26380f9d1a10SAl Viro if (will_truncate) { 26390f9d1a10SAl Viro error = mnt_want_write(nd->path.mnt); 26400f9d1a10SAl Viro if (error) 26412675a4ebSAl Viro goto out; 264277d660a8SMiklos Szeredi want_write = true; 26430f9d1a10SAl Viro } 2644e83db167SMiklos Szeredi finish_open_created: 2645bcda7652SAl Viro error = may_open(&nd->path, acc_mode, open_flag); 2646ca344a89SAl Viro if (error) 26472675a4ebSAl Viro goto out; 264830d90494SAl Viro file->f_path.mnt = nd->path.mnt; 264930d90494SAl Viro error = finish_open(file, nd->path.dentry, NULL, opened); 265030d90494SAl Viro if (error) { 265130d90494SAl Viro if (error == -EOPENSTALE) 2652f60dc3dbSMiklos Szeredi goto stale_open; 2653015c3bbcSMiklos Szeredi goto out; 2654f60dc3dbSMiklos Szeredi } 2655a8277b9bSMiklos Szeredi opened: 26562675a4ebSAl Viro error = open_check_o_direct(file); 2657015c3bbcSMiklos Szeredi if (error) 2658015c3bbcSMiklos Szeredi goto exit_fput; 26592675a4ebSAl Viro error = ima_file_check(file, op->acc_mode); 2660aa4caadbSMiklos Szeredi if (error) 2661aa4caadbSMiklos Szeredi goto exit_fput; 2662aa4caadbSMiklos Szeredi 26630f9d1a10SAl Viro if (will_truncate) { 26642675a4ebSAl Viro error = handle_truncate(file); 2665aa4caadbSMiklos Szeredi if (error) 2666aa4caadbSMiklos Szeredi goto exit_fput; 26670f9d1a10SAl Viro } 2668ca344a89SAl Viro out: 2669ca344a89SAl Viro if (want_write) 26700f9d1a10SAl Viro mnt_drop_write(nd->path.mnt); 267116b1c1cdSMiklos Szeredi path_put(&save_parent); 2672e276ae67SMiklos Szeredi terminate_walk(nd); 26732675a4ebSAl Viro return error; 2674fb1cc555SAl Viro 2675fb1cc555SAl Viro exit_dput: 2676fb1cc555SAl Viro path_put_conditional(path, nd); 2677ca344a89SAl Viro goto out; 2678015c3bbcSMiklos Szeredi exit_fput: 26792675a4ebSAl Viro fput(file); 26802675a4ebSAl Viro goto out; 2681015c3bbcSMiklos Szeredi 2682f60dc3dbSMiklos Szeredi stale_open: 2683f60dc3dbSMiklos Szeredi /* If no saved parent or already retried then can't retry */ 2684f60dc3dbSMiklos Szeredi if (!save_parent.dentry || retried) 2685f60dc3dbSMiklos Szeredi goto out; 2686f60dc3dbSMiklos Szeredi 2687f60dc3dbSMiklos Szeredi BUG_ON(save_parent.dentry != dir); 2688f60dc3dbSMiklos Szeredi path_put(&nd->path); 2689f60dc3dbSMiklos Szeredi nd->path = save_parent; 2690f60dc3dbSMiklos Szeredi nd->inode = dir->d_inode; 2691f60dc3dbSMiklos Szeredi save_parent.mnt = NULL; 2692f60dc3dbSMiklos Szeredi save_parent.dentry = NULL; 2693f60dc3dbSMiklos Szeredi if (want_write) { 2694f60dc3dbSMiklos Szeredi mnt_drop_write(nd->path.mnt); 2695f60dc3dbSMiklos Szeredi want_write = false; 2696f60dc3dbSMiklos Szeredi } 2697f60dc3dbSMiklos Szeredi retried = true; 2698f60dc3dbSMiklos Szeredi goto retry_lookup; 2699fb1cc555SAl Viro } 2700fb1cc555SAl Viro 270113aab428SAl Viro static struct file *path_openat(int dfd, const char *pathname, 270273d049a4SAl Viro struct nameidata *nd, const struct open_flags *op, int flags) 27031da177e4SLinus Torvalds { 2704fe2d35ffSAl Viro struct file *base = NULL; 270530d90494SAl Viro struct file *file; 27069850c056SAl Viro struct path path; 270747237687SAl Viro int opened = 0; 270813aab428SAl Viro int error; 270931e6b01fSNick Piggin 271030d90494SAl Viro file = get_empty_filp(); 271130d90494SAl Viro if (!file) 271231e6b01fSNick Piggin return ERR_PTR(-ENFILE); 271331e6b01fSNick Piggin 271430d90494SAl Viro file->f_flags = op->open_flag; 271531e6b01fSNick Piggin 271673d049a4SAl Viro error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base); 271731e6b01fSNick Piggin if (unlikely(error)) 27182675a4ebSAl Viro goto out; 271931e6b01fSNick Piggin 2720fe2d35ffSAl Viro current->total_link_count = 0; 272173d049a4SAl Viro error = link_path_walk(pathname, nd); 272231e6b01fSNick Piggin if (unlikely(error)) 27232675a4ebSAl Viro goto out; 27241da177e4SLinus Torvalds 27252675a4ebSAl Viro error = do_last(nd, &path, file, op, &opened, pathname); 27262675a4ebSAl Viro while (unlikely(error > 0)) { /* trailing symlink */ 27277b9337aaSNick Piggin struct path link = path; 2728def4af30SAl Viro void *cookie; 2729574197e0SAl Viro if (!(nd->flags & LOOKUP_FOLLOW)) { 273073d049a4SAl Viro path_put_conditional(&path, nd); 273173d049a4SAl Viro path_put(&nd->path); 27322675a4ebSAl Viro error = -ELOOP; 273340b39136SAl Viro break; 273440b39136SAl Viro } 273573d049a4SAl Viro nd->flags |= LOOKUP_PARENT; 273673d049a4SAl Viro nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL); 2737574197e0SAl Viro error = follow_link(&link, nd, &cookie); 2738c3e380b0SAl Viro if (unlikely(error)) 27392675a4ebSAl Viro break; 27402675a4ebSAl Viro error = do_last(nd, &path, file, op, &opened, pathname); 2741574197e0SAl Viro put_link(nd, &link, cookie); 2742806b681cSAl Viro } 274310fa8e62SAl Viro out: 274473d049a4SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) 274573d049a4SAl Viro path_put(&nd->root); 2746fe2d35ffSAl Viro if (base) 2747fe2d35ffSAl Viro fput(base); 27482675a4ebSAl Viro if (!(opened & FILE_OPENED)) { 27492675a4ebSAl Viro BUG_ON(!error); 275030d90494SAl Viro put_filp(file); 2751015c3bbcSMiklos Szeredi } 27522675a4ebSAl Viro if (unlikely(error)) { 27532675a4ebSAl Viro if (error == -EOPENSTALE) { 27542675a4ebSAl Viro if (flags & LOOKUP_RCU) 27552675a4ebSAl Viro error = -ECHILD; 27562675a4ebSAl Viro else 27572675a4ebSAl Viro error = -ESTALE; 27582675a4ebSAl Viro } 27592675a4ebSAl Viro file = ERR_PTR(error); 27602675a4ebSAl Viro } 27612675a4ebSAl Viro return file; 2762de459215SKirill Korotaev } 27631da177e4SLinus Torvalds 276413aab428SAl Viro struct file *do_filp_open(int dfd, const char *pathname, 276513aab428SAl Viro const struct open_flags *op, int flags) 276613aab428SAl Viro { 276773d049a4SAl Viro struct nameidata nd; 276813aab428SAl Viro struct file *filp; 276913aab428SAl Viro 277073d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU); 277113aab428SAl Viro if (unlikely(filp == ERR_PTR(-ECHILD))) 277273d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags); 277313aab428SAl Viro if (unlikely(filp == ERR_PTR(-ESTALE))) 277473d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL); 277513aab428SAl Viro return filp; 277613aab428SAl Viro } 277713aab428SAl Viro 277873d049a4SAl Viro struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt, 277973d049a4SAl Viro const char *name, const struct open_flags *op, int flags) 278073d049a4SAl Viro { 278173d049a4SAl Viro struct nameidata nd; 278273d049a4SAl Viro struct file *file; 278373d049a4SAl Viro 278473d049a4SAl Viro nd.root.mnt = mnt; 278573d049a4SAl Viro nd.root.dentry = dentry; 278673d049a4SAl Viro 278773d049a4SAl Viro flags |= LOOKUP_ROOT; 278873d049a4SAl Viro 2789bcda7652SAl Viro if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN) 279073d049a4SAl Viro return ERR_PTR(-ELOOP); 279173d049a4SAl Viro 279273d049a4SAl Viro file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU); 279373d049a4SAl Viro if (unlikely(file == ERR_PTR(-ECHILD))) 279473d049a4SAl Viro file = path_openat(-1, name, &nd, op, flags); 279573d049a4SAl Viro if (unlikely(file == ERR_PTR(-ESTALE))) 279673d049a4SAl Viro file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL); 279773d049a4SAl Viro return file; 279873d049a4SAl Viro } 279973d049a4SAl Viro 2800ed75e95dSAl Viro struct dentry *kern_path_create(int dfd, const char *pathname, struct path *path, int is_dir) 28011da177e4SLinus Torvalds { 2802c663e5d8SChristoph Hellwig struct dentry *dentry = ERR_PTR(-EEXIST); 2803ed75e95dSAl Viro struct nameidata nd; 2804ed75e95dSAl Viro int error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd); 2805ed75e95dSAl Viro if (error) 2806ed75e95dSAl Viro return ERR_PTR(error); 28071da177e4SLinus Torvalds 2808c663e5d8SChristoph Hellwig /* 2809c663e5d8SChristoph Hellwig * Yucky last component or no last component at all? 2810c663e5d8SChristoph Hellwig * (foo/., foo/.., /////) 2811c663e5d8SChristoph Hellwig */ 2812ed75e95dSAl Viro if (nd.last_type != LAST_NORM) 2813ed75e95dSAl Viro goto out; 2814ed75e95dSAl Viro nd.flags &= ~LOOKUP_PARENT; 2815ed75e95dSAl Viro nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL; 2816c663e5d8SChristoph Hellwig 2817c663e5d8SChristoph Hellwig /* 2818c663e5d8SChristoph Hellwig * Do the final lookup. 2819c663e5d8SChristoph Hellwig */ 2820ed75e95dSAl Viro mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 2821ed75e95dSAl Viro dentry = lookup_hash(&nd); 28221da177e4SLinus Torvalds if (IS_ERR(dentry)) 28231da177e4SLinus Torvalds goto fail; 2824c663e5d8SChristoph Hellwig 2825e9baf6e5SAl Viro if (dentry->d_inode) 2826e9baf6e5SAl Viro goto eexist; 2827c663e5d8SChristoph Hellwig /* 2828c663e5d8SChristoph Hellwig * Special case - lookup gave negative, but... we had foo/bar/ 2829c663e5d8SChristoph Hellwig * From the vfs_mknod() POV we just have a negative dentry - 2830c663e5d8SChristoph Hellwig * all is fine. Let's be bastards - you had / on the end, you've 2831c663e5d8SChristoph Hellwig * been asking for (non-existent) directory. -ENOENT for you. 2832c663e5d8SChristoph Hellwig */ 2833ed75e95dSAl Viro if (unlikely(!is_dir && nd.last.name[nd.last.len])) { 28341da177e4SLinus Torvalds dput(dentry); 28351da177e4SLinus Torvalds dentry = ERR_PTR(-ENOENT); 2836ed75e95dSAl Viro goto fail; 2837e9baf6e5SAl Viro } 2838ed75e95dSAl Viro *path = nd.path; 2839e9baf6e5SAl Viro return dentry; 2840e9baf6e5SAl Viro eexist: 2841e9baf6e5SAl Viro dput(dentry); 2842e9baf6e5SAl Viro dentry = ERR_PTR(-EEXIST); 28431da177e4SLinus Torvalds fail: 2844dae6ad8fSAl Viro mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 2845ed75e95dSAl Viro out: 2846dae6ad8fSAl Viro path_put(&nd.path); 2847ed75e95dSAl Viro return dentry; 2848dae6ad8fSAl Viro } 2849dae6ad8fSAl Viro EXPORT_SYMBOL(kern_path_create); 2850dae6ad8fSAl Viro 2851dae6ad8fSAl Viro struct dentry *user_path_create(int dfd, const char __user *pathname, struct path *path, int is_dir) 2852dae6ad8fSAl Viro { 2853dae6ad8fSAl Viro char *tmp = getname(pathname); 2854dae6ad8fSAl Viro struct dentry *res; 2855dae6ad8fSAl Viro if (IS_ERR(tmp)) 2856dae6ad8fSAl Viro return ERR_CAST(tmp); 2857dae6ad8fSAl Viro res = kern_path_create(dfd, tmp, path, is_dir); 2858dae6ad8fSAl Viro putname(tmp); 2859dae6ad8fSAl Viro return res; 2860dae6ad8fSAl Viro } 2861dae6ad8fSAl Viro EXPORT_SYMBOL(user_path_create); 2862dae6ad8fSAl Viro 28631a67aafbSAl Viro int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev) 28641da177e4SLinus Torvalds { 2865a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 28661da177e4SLinus Torvalds 28671da177e4SLinus Torvalds if (error) 28681da177e4SLinus Torvalds return error; 28691da177e4SLinus Torvalds 2870975d6b39SEric W. Biederman if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD)) 28711da177e4SLinus Torvalds return -EPERM; 28721da177e4SLinus Torvalds 2873acfa4380SAl Viro if (!dir->i_op->mknod) 28741da177e4SLinus Torvalds return -EPERM; 28751da177e4SLinus Torvalds 287608ce5f16SSerge E. Hallyn error = devcgroup_inode_mknod(mode, dev); 287708ce5f16SSerge E. Hallyn if (error) 287808ce5f16SSerge E. Hallyn return error; 287908ce5f16SSerge E. Hallyn 28801da177e4SLinus Torvalds error = security_inode_mknod(dir, dentry, mode, dev); 28811da177e4SLinus Torvalds if (error) 28821da177e4SLinus Torvalds return error; 28831da177e4SLinus Torvalds 28841da177e4SLinus Torvalds error = dir->i_op->mknod(dir, dentry, mode, dev); 2885a74574aaSStephen Smalley if (!error) 2886f38aa942SAmy Griffis fsnotify_create(dir, dentry); 28871da177e4SLinus Torvalds return error; 28881da177e4SLinus Torvalds } 28891da177e4SLinus Torvalds 2890f69aac00SAl Viro static int may_mknod(umode_t mode) 2891463c3197SDave Hansen { 2892463c3197SDave Hansen switch (mode & S_IFMT) { 2893463c3197SDave Hansen case S_IFREG: 2894463c3197SDave Hansen case S_IFCHR: 2895463c3197SDave Hansen case S_IFBLK: 2896463c3197SDave Hansen case S_IFIFO: 2897463c3197SDave Hansen case S_IFSOCK: 2898463c3197SDave Hansen case 0: /* zero mode translates to S_IFREG */ 2899463c3197SDave Hansen return 0; 2900463c3197SDave Hansen case S_IFDIR: 2901463c3197SDave Hansen return -EPERM; 2902463c3197SDave Hansen default: 2903463c3197SDave Hansen return -EINVAL; 2904463c3197SDave Hansen } 2905463c3197SDave Hansen } 2906463c3197SDave Hansen 29078208a22bSAl Viro SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode, 29082e4d0924SHeiko Carstens unsigned, dev) 29091da177e4SLinus Torvalds { 29101da177e4SLinus Torvalds struct dentry *dentry; 2911dae6ad8fSAl Viro struct path path; 2912dae6ad8fSAl Viro int error; 29131da177e4SLinus Torvalds 29141da177e4SLinus Torvalds if (S_ISDIR(mode)) 29151da177e4SLinus Torvalds return -EPERM; 29161da177e4SLinus Torvalds 2917dae6ad8fSAl Viro dentry = user_path_create(dfd, filename, &path, 0); 2918dae6ad8fSAl Viro if (IS_ERR(dentry)) 2919dae6ad8fSAl Viro return PTR_ERR(dentry); 29202ad94ae6SAl Viro 2921dae6ad8fSAl Viro if (!IS_POSIXACL(path.dentry->d_inode)) 2922ce3b0f8dSAl Viro mode &= ~current_umask(); 2923463c3197SDave Hansen error = may_mknod(mode); 2924463c3197SDave Hansen if (error) 2925463c3197SDave Hansen goto out_dput; 2926dae6ad8fSAl Viro error = mnt_want_write(path.mnt); 2927463c3197SDave Hansen if (error) 2928463c3197SDave Hansen goto out_dput; 2929dae6ad8fSAl Viro error = security_path_mknod(&path, dentry, mode, dev); 2930be6d3e56SKentaro Takeda if (error) 2931be6d3e56SKentaro Takeda goto out_drop_write; 29321da177e4SLinus Torvalds switch (mode & S_IFMT) { 29331da177e4SLinus Torvalds case 0: case S_IFREG: 2934312b63fbSAl Viro error = vfs_create(path.dentry->d_inode,dentry,mode,true); 29351da177e4SLinus Torvalds break; 29361da177e4SLinus Torvalds case S_IFCHR: case S_IFBLK: 2937dae6ad8fSAl Viro error = vfs_mknod(path.dentry->d_inode,dentry,mode, 29381da177e4SLinus Torvalds new_decode_dev(dev)); 29391da177e4SLinus Torvalds break; 29401da177e4SLinus Torvalds case S_IFIFO: case S_IFSOCK: 2941dae6ad8fSAl Viro error = vfs_mknod(path.dentry->d_inode,dentry,mode,0); 29421da177e4SLinus Torvalds break; 29431da177e4SLinus Torvalds } 2944be6d3e56SKentaro Takeda out_drop_write: 2945dae6ad8fSAl Viro mnt_drop_write(path.mnt); 2946463c3197SDave Hansen out_dput: 29471da177e4SLinus Torvalds dput(dentry); 2948dae6ad8fSAl Viro mutex_unlock(&path.dentry->d_inode->i_mutex); 2949dae6ad8fSAl Viro path_put(&path); 29501da177e4SLinus Torvalds 29511da177e4SLinus Torvalds return error; 29521da177e4SLinus Torvalds } 29531da177e4SLinus Torvalds 29548208a22bSAl Viro SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev) 29555590ff0dSUlrich Drepper { 29565590ff0dSUlrich Drepper return sys_mknodat(AT_FDCWD, filename, mode, dev); 29575590ff0dSUlrich Drepper } 29585590ff0dSUlrich Drepper 295918bb1db3SAl Viro int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) 29601da177e4SLinus Torvalds { 2961a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 29628de52778SAl Viro unsigned max_links = dir->i_sb->s_max_links; 29631da177e4SLinus Torvalds 29641da177e4SLinus Torvalds if (error) 29651da177e4SLinus Torvalds return error; 29661da177e4SLinus Torvalds 2967acfa4380SAl Viro if (!dir->i_op->mkdir) 29681da177e4SLinus Torvalds return -EPERM; 29691da177e4SLinus Torvalds 29701da177e4SLinus Torvalds mode &= (S_IRWXUGO|S_ISVTX); 29711da177e4SLinus Torvalds error = security_inode_mkdir(dir, dentry, mode); 29721da177e4SLinus Torvalds if (error) 29731da177e4SLinus Torvalds return error; 29741da177e4SLinus Torvalds 29758de52778SAl Viro if (max_links && dir->i_nlink >= max_links) 29768de52778SAl Viro return -EMLINK; 29778de52778SAl Viro 29781da177e4SLinus Torvalds error = dir->i_op->mkdir(dir, dentry, mode); 2979a74574aaSStephen Smalley if (!error) 2980f38aa942SAmy Griffis fsnotify_mkdir(dir, dentry); 29811da177e4SLinus Torvalds return error; 29821da177e4SLinus Torvalds } 29831da177e4SLinus Torvalds 2984a218d0fdSAl Viro SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode) 29851da177e4SLinus Torvalds { 29866902d925SDave Hansen struct dentry *dentry; 2987dae6ad8fSAl Viro struct path path; 2988dae6ad8fSAl Viro int error; 29891da177e4SLinus Torvalds 2990dae6ad8fSAl Viro dentry = user_path_create(dfd, pathname, &path, 1); 29916902d925SDave Hansen if (IS_ERR(dentry)) 2992dae6ad8fSAl Viro return PTR_ERR(dentry); 29936902d925SDave Hansen 2994dae6ad8fSAl Viro if (!IS_POSIXACL(path.dentry->d_inode)) 2995ce3b0f8dSAl Viro mode &= ~current_umask(); 2996dae6ad8fSAl Viro error = mnt_want_write(path.mnt); 2997463c3197SDave Hansen if (error) 2998463c3197SDave Hansen goto out_dput; 2999dae6ad8fSAl Viro error = security_path_mkdir(&path, dentry, mode); 3000be6d3e56SKentaro Takeda if (error) 3001be6d3e56SKentaro Takeda goto out_drop_write; 3002dae6ad8fSAl Viro error = vfs_mkdir(path.dentry->d_inode, dentry, mode); 3003be6d3e56SKentaro Takeda out_drop_write: 3004dae6ad8fSAl Viro mnt_drop_write(path.mnt); 3005463c3197SDave Hansen out_dput: 30061da177e4SLinus Torvalds dput(dentry); 3007dae6ad8fSAl Viro mutex_unlock(&path.dentry->d_inode->i_mutex); 3008dae6ad8fSAl Viro path_put(&path); 30091da177e4SLinus Torvalds return error; 30101da177e4SLinus Torvalds } 30111da177e4SLinus Torvalds 3012a218d0fdSAl Viro SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode) 30135590ff0dSUlrich Drepper { 30145590ff0dSUlrich Drepper return sys_mkdirat(AT_FDCWD, pathname, mode); 30155590ff0dSUlrich Drepper } 30165590ff0dSUlrich Drepper 30171da177e4SLinus Torvalds /* 3018a71905f0SSage Weil * The dentry_unhash() helper will try to drop the dentry early: we 3019c0d02594SJ. Bruce Fields * should have a usage count of 1 if we're the only user of this 3020a71905f0SSage Weil * dentry, and if that is true (possibly after pruning the dcache), 3021a71905f0SSage Weil * then we drop the dentry now. 30221da177e4SLinus Torvalds * 30231da177e4SLinus Torvalds * A low-level filesystem can, if it choses, legally 30241da177e4SLinus Torvalds * do a 30251da177e4SLinus Torvalds * 30261da177e4SLinus Torvalds * if (!d_unhashed(dentry)) 30271da177e4SLinus Torvalds * return -EBUSY; 30281da177e4SLinus Torvalds * 30291da177e4SLinus Torvalds * if it cannot handle the case of removing a directory 30301da177e4SLinus Torvalds * that is still in use by something else.. 30311da177e4SLinus Torvalds */ 30321da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry) 30331da177e4SLinus Torvalds { 30341da177e4SLinus Torvalds shrink_dcache_parent(dentry); 30351da177e4SLinus Torvalds spin_lock(&dentry->d_lock); 303664252c75SSage Weil if (dentry->d_count == 1) 30371da177e4SLinus Torvalds __d_drop(dentry); 30381da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 30391da177e4SLinus Torvalds } 30401da177e4SLinus Torvalds 30411da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry) 30421da177e4SLinus Torvalds { 30431da177e4SLinus Torvalds int error = may_delete(dir, dentry, 1); 30441da177e4SLinus Torvalds 30451da177e4SLinus Torvalds if (error) 30461da177e4SLinus Torvalds return error; 30471da177e4SLinus Torvalds 3048acfa4380SAl Viro if (!dir->i_op->rmdir) 30491da177e4SLinus Torvalds return -EPERM; 30501da177e4SLinus Torvalds 30511d2ef590SAl Viro dget(dentry); 30521b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 3053912dbc15SSage Weil 30541da177e4SLinus Torvalds error = -EBUSY; 3055912dbc15SSage Weil if (d_mountpoint(dentry)) 3056912dbc15SSage Weil goto out; 3057912dbc15SSage Weil 30581da177e4SLinus Torvalds error = security_inode_rmdir(dir, dentry); 3059912dbc15SSage Weil if (error) 3060912dbc15SSage Weil goto out; 3061912dbc15SSage Weil 30623cebde24SSage Weil shrink_dcache_parent(dentry); 30631da177e4SLinus Torvalds error = dir->i_op->rmdir(dir, dentry); 3064912dbc15SSage Weil if (error) 3065912dbc15SSage Weil goto out; 3066912dbc15SSage Weil 30671da177e4SLinus Torvalds dentry->d_inode->i_flags |= S_DEAD; 3068d83c49f3SAl Viro dont_mount(dentry); 30691da177e4SLinus Torvalds 3070912dbc15SSage Weil out: 3071912dbc15SSage Weil mutex_unlock(&dentry->d_inode->i_mutex); 30721d2ef590SAl Viro dput(dentry); 3073912dbc15SSage Weil if (!error) 3074912dbc15SSage Weil d_delete(dentry); 30751da177e4SLinus Torvalds return error; 30761da177e4SLinus Torvalds } 30771da177e4SLinus Torvalds 30785590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname) 30791da177e4SLinus Torvalds { 30801da177e4SLinus Torvalds int error = 0; 30811da177e4SLinus Torvalds char * name; 30821da177e4SLinus Torvalds struct dentry *dentry; 30831da177e4SLinus Torvalds struct nameidata nd; 30841da177e4SLinus Torvalds 30852ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &name); 30861da177e4SLinus Torvalds if (error) 30872ad94ae6SAl Viro return error; 30881da177e4SLinus Torvalds 30891da177e4SLinus Torvalds switch(nd.last_type) { 30901da177e4SLinus Torvalds case LAST_DOTDOT: 30911da177e4SLinus Torvalds error = -ENOTEMPTY; 30921da177e4SLinus Torvalds goto exit1; 30931da177e4SLinus Torvalds case LAST_DOT: 30941da177e4SLinus Torvalds error = -EINVAL; 30951da177e4SLinus Torvalds goto exit1; 30961da177e4SLinus Torvalds case LAST_ROOT: 30971da177e4SLinus Torvalds error = -EBUSY; 30981da177e4SLinus Torvalds goto exit1; 30991da177e4SLinus Torvalds } 31000612d9fbSOGAWA Hirofumi 31010612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 31020612d9fbSOGAWA Hirofumi 31034ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 310449705b77SChristoph Hellwig dentry = lookup_hash(&nd); 31051da177e4SLinus Torvalds error = PTR_ERR(dentry); 31066902d925SDave Hansen if (IS_ERR(dentry)) 31076902d925SDave Hansen goto exit2; 3108e6bc45d6STheodore Ts'o if (!dentry->d_inode) { 3109e6bc45d6STheodore Ts'o error = -ENOENT; 3110e6bc45d6STheodore Ts'o goto exit3; 3111e6bc45d6STheodore Ts'o } 31120622753bSDave Hansen error = mnt_want_write(nd.path.mnt); 31130622753bSDave Hansen if (error) 31140622753bSDave Hansen goto exit3; 3115be6d3e56SKentaro Takeda error = security_path_rmdir(&nd.path, dentry); 3116be6d3e56SKentaro Takeda if (error) 3117be6d3e56SKentaro Takeda goto exit4; 31184ac91378SJan Blunck error = vfs_rmdir(nd.path.dentry->d_inode, dentry); 3119be6d3e56SKentaro Takeda exit4: 31200622753bSDave Hansen mnt_drop_write(nd.path.mnt); 31210622753bSDave Hansen exit3: 31221da177e4SLinus Torvalds dput(dentry); 31236902d925SDave Hansen exit2: 31244ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 31251da177e4SLinus Torvalds exit1: 31261d957f9bSJan Blunck path_put(&nd.path); 31271da177e4SLinus Torvalds putname(name); 31281da177e4SLinus Torvalds return error; 31291da177e4SLinus Torvalds } 31301da177e4SLinus Torvalds 31313cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname) 31325590ff0dSUlrich Drepper { 31335590ff0dSUlrich Drepper return do_rmdir(AT_FDCWD, pathname); 31345590ff0dSUlrich Drepper } 31355590ff0dSUlrich Drepper 31361da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry) 31371da177e4SLinus Torvalds { 31381da177e4SLinus Torvalds int error = may_delete(dir, dentry, 0); 31391da177e4SLinus Torvalds 31401da177e4SLinus Torvalds if (error) 31411da177e4SLinus Torvalds return error; 31421da177e4SLinus Torvalds 3143acfa4380SAl Viro if (!dir->i_op->unlink) 31441da177e4SLinus Torvalds return -EPERM; 31451da177e4SLinus Torvalds 31461b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 31471da177e4SLinus Torvalds if (d_mountpoint(dentry)) 31481da177e4SLinus Torvalds error = -EBUSY; 31491da177e4SLinus Torvalds else { 31501da177e4SLinus Torvalds error = security_inode_unlink(dir, dentry); 3151bec1052eSAl Viro if (!error) { 31521da177e4SLinus Torvalds error = dir->i_op->unlink(dir, dentry); 3153bec1052eSAl Viro if (!error) 3154d83c49f3SAl Viro dont_mount(dentry); 3155bec1052eSAl Viro } 31561da177e4SLinus Torvalds } 31571b1dcc1bSJes Sorensen mutex_unlock(&dentry->d_inode->i_mutex); 31581da177e4SLinus Torvalds 31591da177e4SLinus Torvalds /* We don't d_delete() NFS sillyrenamed files--they still exist. */ 31601da177e4SLinus Torvalds if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) { 3161ece95912SJan Kara fsnotify_link_count(dentry->d_inode); 31621da177e4SLinus Torvalds d_delete(dentry); 31631da177e4SLinus Torvalds } 31640eeca283SRobert Love 31651da177e4SLinus Torvalds return error; 31661da177e4SLinus Torvalds } 31671da177e4SLinus Torvalds 31681da177e4SLinus Torvalds /* 31691da177e4SLinus Torvalds * Make sure that the actual truncation of the file will occur outside its 31701b1dcc1bSJes Sorensen * directory's i_mutex. Truncate can take a long time if there is a lot of 31711da177e4SLinus Torvalds * writeout happening, and we don't want to prevent access to the directory 31721da177e4SLinus Torvalds * while waiting on the I/O. 31731da177e4SLinus Torvalds */ 31745590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname) 31751da177e4SLinus Torvalds { 31762ad94ae6SAl Viro int error; 31771da177e4SLinus Torvalds char *name; 31781da177e4SLinus Torvalds struct dentry *dentry; 31791da177e4SLinus Torvalds struct nameidata nd; 31801da177e4SLinus Torvalds struct inode *inode = NULL; 31811da177e4SLinus Torvalds 31822ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &name); 31831da177e4SLinus Torvalds if (error) 31842ad94ae6SAl Viro return error; 31852ad94ae6SAl Viro 31861da177e4SLinus Torvalds error = -EISDIR; 31871da177e4SLinus Torvalds if (nd.last_type != LAST_NORM) 31881da177e4SLinus Torvalds goto exit1; 31890612d9fbSOGAWA Hirofumi 31900612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 31910612d9fbSOGAWA Hirofumi 31924ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 319349705b77SChristoph Hellwig dentry = lookup_hash(&nd); 31941da177e4SLinus Torvalds error = PTR_ERR(dentry); 31951da177e4SLinus Torvalds if (!IS_ERR(dentry)) { 31961da177e4SLinus Torvalds /* Why not before? Because we want correct error value */ 319750338b88STörök Edwin if (nd.last.name[nd.last.len]) 319850338b88STörök Edwin goto slashes; 31991da177e4SLinus Torvalds inode = dentry->d_inode; 320050338b88STörök Edwin if (!inode) 3201e6bc45d6STheodore Ts'o goto slashes; 32027de9c6eeSAl Viro ihold(inode); 32030622753bSDave Hansen error = mnt_want_write(nd.path.mnt); 32040622753bSDave Hansen if (error) 32050622753bSDave Hansen goto exit2; 3206be6d3e56SKentaro Takeda error = security_path_unlink(&nd.path, dentry); 3207be6d3e56SKentaro Takeda if (error) 3208be6d3e56SKentaro Takeda goto exit3; 32094ac91378SJan Blunck error = vfs_unlink(nd.path.dentry->d_inode, dentry); 3210be6d3e56SKentaro Takeda exit3: 32110622753bSDave Hansen mnt_drop_write(nd.path.mnt); 32121da177e4SLinus Torvalds exit2: 32131da177e4SLinus Torvalds dput(dentry); 32141da177e4SLinus Torvalds } 32154ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 32161da177e4SLinus Torvalds if (inode) 32171da177e4SLinus Torvalds iput(inode); /* truncate the inode here */ 32181da177e4SLinus Torvalds exit1: 32191d957f9bSJan Blunck path_put(&nd.path); 32201da177e4SLinus Torvalds putname(name); 32211da177e4SLinus Torvalds return error; 32221da177e4SLinus Torvalds 32231da177e4SLinus Torvalds slashes: 32241da177e4SLinus Torvalds error = !dentry->d_inode ? -ENOENT : 32251da177e4SLinus Torvalds S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR; 32261da177e4SLinus Torvalds goto exit2; 32271da177e4SLinus Torvalds } 32281da177e4SLinus Torvalds 32292e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag) 32305590ff0dSUlrich Drepper { 32315590ff0dSUlrich Drepper if ((flag & ~AT_REMOVEDIR) != 0) 32325590ff0dSUlrich Drepper return -EINVAL; 32335590ff0dSUlrich Drepper 32345590ff0dSUlrich Drepper if (flag & AT_REMOVEDIR) 32355590ff0dSUlrich Drepper return do_rmdir(dfd, pathname); 32365590ff0dSUlrich Drepper 32375590ff0dSUlrich Drepper return do_unlinkat(dfd, pathname); 32385590ff0dSUlrich Drepper } 32395590ff0dSUlrich Drepper 32403480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname) 32415590ff0dSUlrich Drepper { 32425590ff0dSUlrich Drepper return do_unlinkat(AT_FDCWD, pathname); 32435590ff0dSUlrich Drepper } 32445590ff0dSUlrich Drepper 3245db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname) 32461da177e4SLinus Torvalds { 3247a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 32481da177e4SLinus Torvalds 32491da177e4SLinus Torvalds if (error) 32501da177e4SLinus Torvalds return error; 32511da177e4SLinus Torvalds 3252acfa4380SAl Viro if (!dir->i_op->symlink) 32531da177e4SLinus Torvalds return -EPERM; 32541da177e4SLinus Torvalds 32551da177e4SLinus Torvalds error = security_inode_symlink(dir, dentry, oldname); 32561da177e4SLinus Torvalds if (error) 32571da177e4SLinus Torvalds return error; 32581da177e4SLinus Torvalds 32591da177e4SLinus Torvalds error = dir->i_op->symlink(dir, dentry, oldname); 3260a74574aaSStephen Smalley if (!error) 3261f38aa942SAmy Griffis fsnotify_create(dir, dentry); 32621da177e4SLinus Torvalds return error; 32631da177e4SLinus Torvalds } 32641da177e4SLinus Torvalds 32652e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname, 32662e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 32671da177e4SLinus Torvalds { 32682ad94ae6SAl Viro int error; 32691da177e4SLinus Torvalds char *from; 32706902d925SDave Hansen struct dentry *dentry; 3271dae6ad8fSAl Viro struct path path; 32721da177e4SLinus Torvalds 32731da177e4SLinus Torvalds from = getname(oldname); 32741da177e4SLinus Torvalds if (IS_ERR(from)) 32751da177e4SLinus Torvalds return PTR_ERR(from); 32762ad94ae6SAl Viro 3277dae6ad8fSAl Viro dentry = user_path_create(newdfd, newname, &path, 0); 32781da177e4SLinus Torvalds error = PTR_ERR(dentry); 32796902d925SDave Hansen if (IS_ERR(dentry)) 3280dae6ad8fSAl Viro goto out_putname; 32816902d925SDave Hansen 3282dae6ad8fSAl Viro error = mnt_want_write(path.mnt); 328375c3f29dSDave Hansen if (error) 328475c3f29dSDave Hansen goto out_dput; 3285dae6ad8fSAl Viro error = security_path_symlink(&path, dentry, from); 3286be6d3e56SKentaro Takeda if (error) 3287be6d3e56SKentaro Takeda goto out_drop_write; 3288dae6ad8fSAl Viro error = vfs_symlink(path.dentry->d_inode, dentry, from); 3289be6d3e56SKentaro Takeda out_drop_write: 3290dae6ad8fSAl Viro mnt_drop_write(path.mnt); 329175c3f29dSDave Hansen out_dput: 32921da177e4SLinus Torvalds dput(dentry); 3293dae6ad8fSAl Viro mutex_unlock(&path.dentry->d_inode->i_mutex); 3294dae6ad8fSAl Viro path_put(&path); 32956902d925SDave Hansen out_putname: 32961da177e4SLinus Torvalds putname(from); 32971da177e4SLinus Torvalds return error; 32981da177e4SLinus Torvalds } 32991da177e4SLinus Torvalds 33003480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname) 33015590ff0dSUlrich Drepper { 33025590ff0dSUlrich Drepper return sys_symlinkat(oldname, AT_FDCWD, newname); 33035590ff0dSUlrich Drepper } 33045590ff0dSUlrich Drepper 33051da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) 33061da177e4SLinus Torvalds { 33071da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 33088de52778SAl Viro unsigned max_links = dir->i_sb->s_max_links; 33091da177e4SLinus Torvalds int error; 33101da177e4SLinus Torvalds 33111da177e4SLinus Torvalds if (!inode) 33121da177e4SLinus Torvalds return -ENOENT; 33131da177e4SLinus Torvalds 3314a95164d9SMiklos Szeredi error = may_create(dir, new_dentry); 33151da177e4SLinus Torvalds if (error) 33161da177e4SLinus Torvalds return error; 33171da177e4SLinus Torvalds 33181da177e4SLinus Torvalds if (dir->i_sb != inode->i_sb) 33191da177e4SLinus Torvalds return -EXDEV; 33201da177e4SLinus Torvalds 33211da177e4SLinus Torvalds /* 33221da177e4SLinus Torvalds * A link to an append-only or immutable file cannot be created. 33231da177e4SLinus Torvalds */ 33241da177e4SLinus Torvalds if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) 33251da177e4SLinus Torvalds return -EPERM; 3326acfa4380SAl Viro if (!dir->i_op->link) 33271da177e4SLinus Torvalds return -EPERM; 33287e79eedbSTetsuo Handa if (S_ISDIR(inode->i_mode)) 33291da177e4SLinus Torvalds return -EPERM; 33301da177e4SLinus Torvalds 33311da177e4SLinus Torvalds error = security_inode_link(old_dentry, dir, new_dentry); 33321da177e4SLinus Torvalds if (error) 33331da177e4SLinus Torvalds return error; 33341da177e4SLinus Torvalds 33357e79eedbSTetsuo Handa mutex_lock(&inode->i_mutex); 3336aae8a97dSAneesh Kumar K.V /* Make sure we don't allow creating hardlink to an unlinked file */ 3337aae8a97dSAneesh Kumar K.V if (inode->i_nlink == 0) 3338aae8a97dSAneesh Kumar K.V error = -ENOENT; 33398de52778SAl Viro else if (max_links && inode->i_nlink >= max_links) 33408de52778SAl Viro error = -EMLINK; 3341aae8a97dSAneesh Kumar K.V else 33421da177e4SLinus Torvalds error = dir->i_op->link(old_dentry, dir, new_dentry); 33437e79eedbSTetsuo Handa mutex_unlock(&inode->i_mutex); 3344e31e14ecSStephen Smalley if (!error) 33457e79eedbSTetsuo Handa fsnotify_link(dir, inode, new_dentry); 33461da177e4SLinus Torvalds return error; 33471da177e4SLinus Torvalds } 33481da177e4SLinus Torvalds 33491da177e4SLinus Torvalds /* 33501da177e4SLinus Torvalds * Hardlinks are often used in delicate situations. We avoid 33511da177e4SLinus Torvalds * security-related surprises by not following symlinks on the 33521da177e4SLinus Torvalds * newname. --KAB 33531da177e4SLinus Torvalds * 33541da177e4SLinus Torvalds * We don't follow them on the oldname either to be compatible 33551da177e4SLinus Torvalds * with linux 2.0, and to avoid hard-linking to directories 33561da177e4SLinus Torvalds * and other special files. --ADM 33571da177e4SLinus Torvalds */ 33582e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname, 33592e4d0924SHeiko Carstens int, newdfd, const char __user *, newname, int, flags) 33601da177e4SLinus Torvalds { 33611da177e4SLinus Torvalds struct dentry *new_dentry; 3362dae6ad8fSAl Viro struct path old_path, new_path; 336311a7b371SAneesh Kumar K.V int how = 0; 33641da177e4SLinus Torvalds int error; 33651da177e4SLinus Torvalds 336611a7b371SAneesh Kumar K.V if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0) 3367c04030e1SUlrich Drepper return -EINVAL; 336811a7b371SAneesh Kumar K.V /* 336911a7b371SAneesh Kumar K.V * To use null names we require CAP_DAC_READ_SEARCH 337011a7b371SAneesh Kumar K.V * This ensures that not everyone will be able to create 337111a7b371SAneesh Kumar K.V * handlink using the passed filedescriptor. 337211a7b371SAneesh Kumar K.V */ 337311a7b371SAneesh Kumar K.V if (flags & AT_EMPTY_PATH) { 337411a7b371SAneesh Kumar K.V if (!capable(CAP_DAC_READ_SEARCH)) 337511a7b371SAneesh Kumar K.V return -ENOENT; 337611a7b371SAneesh Kumar K.V how = LOOKUP_EMPTY; 337711a7b371SAneesh Kumar K.V } 3378c04030e1SUlrich Drepper 337911a7b371SAneesh Kumar K.V if (flags & AT_SYMLINK_FOLLOW) 338011a7b371SAneesh Kumar K.V how |= LOOKUP_FOLLOW; 338111a7b371SAneesh Kumar K.V 338211a7b371SAneesh Kumar K.V error = user_path_at(olddfd, oldname, how, &old_path); 33831da177e4SLinus Torvalds if (error) 33842ad94ae6SAl Viro return error; 33852ad94ae6SAl Viro 3386dae6ad8fSAl Viro new_dentry = user_path_create(newdfd, newname, &new_path, 0); 33871da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 33886902d925SDave Hansen if (IS_ERR(new_dentry)) 3389dae6ad8fSAl Viro goto out; 3390dae6ad8fSAl Viro 3391dae6ad8fSAl Viro error = -EXDEV; 3392dae6ad8fSAl Viro if (old_path.mnt != new_path.mnt) 3393dae6ad8fSAl Viro goto out_dput; 3394dae6ad8fSAl Viro error = mnt_want_write(new_path.mnt); 339575c3f29dSDave Hansen if (error) 339675c3f29dSDave Hansen goto out_dput; 3397dae6ad8fSAl Viro error = security_path_link(old_path.dentry, &new_path, new_dentry); 3398be6d3e56SKentaro Takeda if (error) 3399be6d3e56SKentaro Takeda goto out_drop_write; 3400dae6ad8fSAl Viro error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry); 3401be6d3e56SKentaro Takeda out_drop_write: 3402dae6ad8fSAl Viro mnt_drop_write(new_path.mnt); 340375c3f29dSDave Hansen out_dput: 34041da177e4SLinus Torvalds dput(new_dentry); 3405dae6ad8fSAl Viro mutex_unlock(&new_path.dentry->d_inode->i_mutex); 3406dae6ad8fSAl Viro path_put(&new_path); 34071da177e4SLinus Torvalds out: 34082d8f3038SAl Viro path_put(&old_path); 34091da177e4SLinus Torvalds 34101da177e4SLinus Torvalds return error; 34111da177e4SLinus Torvalds } 34121da177e4SLinus Torvalds 34133480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname) 34145590ff0dSUlrich Drepper { 3415c04030e1SUlrich Drepper return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0); 34165590ff0dSUlrich Drepper } 34175590ff0dSUlrich Drepper 34181da177e4SLinus Torvalds /* 34191da177e4SLinus Torvalds * The worst of all namespace operations - renaming directory. "Perverted" 34201da177e4SLinus Torvalds * doesn't even start to describe it. Somebody in UCB had a heck of a trip... 34211da177e4SLinus Torvalds * Problems: 34221da177e4SLinus Torvalds * a) we can get into loop creation. Check is done in is_subdir(). 34231da177e4SLinus Torvalds * b) race potential - two innocent renames can create a loop together. 34241da177e4SLinus Torvalds * That's where 4.4 screws up. Current fix: serialization on 3425a11f3a05SArjan van de Ven * sb->s_vfs_rename_mutex. We might be more accurate, but that's another 34261da177e4SLinus Torvalds * story. 34271da177e4SLinus Torvalds * c) we have to lock _three_ objects - parents and victim (if it exists). 34281b1dcc1bSJes Sorensen * And that - after we got ->i_mutex on parents (until then we don't know 34291da177e4SLinus Torvalds * whether the target exists). Solution: try to be smart with locking 34301da177e4SLinus Torvalds * order for inodes. We rely on the fact that tree topology may change 3431a11f3a05SArjan van de Ven * only under ->s_vfs_rename_mutex _and_ that parent of the object we 34321da177e4SLinus Torvalds * move will be locked. Thus we can rank directories by the tree 34331da177e4SLinus Torvalds * (ancestors first) and rank all non-directories after them. 34341da177e4SLinus Torvalds * That works since everybody except rename does "lock parent, lookup, 3435a11f3a05SArjan van de Ven * lock child" and rename is under ->s_vfs_rename_mutex. 34361da177e4SLinus Torvalds * HOWEVER, it relies on the assumption that any object with ->lookup() 34371da177e4SLinus Torvalds * has no more than 1 dentry. If "hybrid" objects will ever appear, 34381da177e4SLinus Torvalds * we'd better make sure that there's no link(2) for them. 3439e4eaac06SSage Weil * d) conversion from fhandle to dentry may come in the wrong moment - when 34401b1dcc1bSJes Sorensen * we are removing the target. Solution: we will have to grab ->i_mutex 34411da177e4SLinus Torvalds * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on 3442c41b20e7SAdam Buchbinder * ->i_mutex on parents, which works but leads to some truly excessive 34431da177e4SLinus Torvalds * locking]. 34441da177e4SLinus Torvalds */ 344575c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry, 34461da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 34471da177e4SLinus Torvalds { 34481da177e4SLinus Torvalds int error = 0; 34499055cba7SSage Weil struct inode *target = new_dentry->d_inode; 34508de52778SAl Viro unsigned max_links = new_dir->i_sb->s_max_links; 34511da177e4SLinus Torvalds 34521da177e4SLinus Torvalds /* 34531da177e4SLinus Torvalds * If we are going to change the parent - check write permissions, 34541da177e4SLinus Torvalds * we'll need to flip '..'. 34551da177e4SLinus Torvalds */ 34561da177e4SLinus Torvalds if (new_dir != old_dir) { 3457f419a2e3SAl Viro error = inode_permission(old_dentry->d_inode, MAY_WRITE); 34581da177e4SLinus Torvalds if (error) 34591da177e4SLinus Torvalds return error; 34601da177e4SLinus Torvalds } 34611da177e4SLinus Torvalds 34621da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 34631da177e4SLinus Torvalds if (error) 34641da177e4SLinus Torvalds return error; 34651da177e4SLinus Torvalds 34661d2ef590SAl Viro dget(new_dentry); 3467d83c49f3SAl Viro if (target) 34681b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 34699055cba7SSage Weil 34701da177e4SLinus Torvalds error = -EBUSY; 34719055cba7SSage Weil if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry)) 34729055cba7SSage Weil goto out; 34739055cba7SSage Weil 34748de52778SAl Viro error = -EMLINK; 34758de52778SAl Viro if (max_links && !target && new_dir != old_dir && 34768de52778SAl Viro new_dir->i_nlink >= max_links) 34778de52778SAl Viro goto out; 34788de52778SAl Viro 34793cebde24SSage Weil if (target) 34803cebde24SSage Weil shrink_dcache_parent(new_dentry); 34811da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 34829055cba7SSage Weil if (error) 34839055cba7SSage Weil goto out; 34849055cba7SSage Weil 34851da177e4SLinus Torvalds if (target) { 34861da177e4SLinus Torvalds target->i_flags |= S_DEAD; 3487d83c49f3SAl Viro dont_mount(new_dentry); 3488d83c49f3SAl Viro } 34899055cba7SSage Weil out: 34909055cba7SSage Weil if (target) 34911b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 34921d2ef590SAl Viro dput(new_dentry); 3493e31e14ecSStephen Smalley if (!error) 3494349457ccSMark Fasheh if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) 34951da177e4SLinus Torvalds d_move(old_dentry,new_dentry); 34961da177e4SLinus Torvalds return error; 34971da177e4SLinus Torvalds } 34981da177e4SLinus Torvalds 349975c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry, 35001da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 35011da177e4SLinus Torvalds { 350251892bbbSSage Weil struct inode *target = new_dentry->d_inode; 35031da177e4SLinus Torvalds int error; 35041da177e4SLinus Torvalds 35051da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 35061da177e4SLinus Torvalds if (error) 35071da177e4SLinus Torvalds return error; 35081da177e4SLinus Torvalds 35091da177e4SLinus Torvalds dget(new_dentry); 35101da177e4SLinus Torvalds if (target) 35111b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 351251892bbbSSage Weil 35131da177e4SLinus Torvalds error = -EBUSY; 351451892bbbSSage Weil if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry)) 351551892bbbSSage Weil goto out; 351651892bbbSSage Weil 35171da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 351851892bbbSSage Weil if (error) 351951892bbbSSage Weil goto out; 352051892bbbSSage Weil 3521bec1052eSAl Viro if (target) 3522d83c49f3SAl Viro dont_mount(new_dentry); 3523349457ccSMark Fasheh if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) 35241da177e4SLinus Torvalds d_move(old_dentry, new_dentry); 352551892bbbSSage Weil out: 35261da177e4SLinus Torvalds if (target) 35271b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 35281da177e4SLinus Torvalds dput(new_dentry); 35291da177e4SLinus Torvalds return error; 35301da177e4SLinus Torvalds } 35311da177e4SLinus Torvalds 35321da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry, 35331da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 35341da177e4SLinus Torvalds { 35351da177e4SLinus Torvalds int error; 35361da177e4SLinus Torvalds int is_dir = S_ISDIR(old_dentry->d_inode->i_mode); 353759b0df21SEric Paris const unsigned char *old_name; 35381da177e4SLinus Torvalds 35391da177e4SLinus Torvalds if (old_dentry->d_inode == new_dentry->d_inode) 35401da177e4SLinus Torvalds return 0; 35411da177e4SLinus Torvalds 35421da177e4SLinus Torvalds error = may_delete(old_dir, old_dentry, is_dir); 35431da177e4SLinus Torvalds if (error) 35441da177e4SLinus Torvalds return error; 35451da177e4SLinus Torvalds 35461da177e4SLinus Torvalds if (!new_dentry->d_inode) 3547a95164d9SMiklos Szeredi error = may_create(new_dir, new_dentry); 35481da177e4SLinus Torvalds else 35491da177e4SLinus Torvalds error = may_delete(new_dir, new_dentry, is_dir); 35501da177e4SLinus Torvalds if (error) 35511da177e4SLinus Torvalds return error; 35521da177e4SLinus Torvalds 3553acfa4380SAl Viro if (!old_dir->i_op->rename) 35541da177e4SLinus Torvalds return -EPERM; 35551da177e4SLinus Torvalds 35560eeca283SRobert Love old_name = fsnotify_oldname_init(old_dentry->d_name.name); 35570eeca283SRobert Love 35581da177e4SLinus Torvalds if (is_dir) 35591da177e4SLinus Torvalds error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry); 35601da177e4SLinus Torvalds else 35611da177e4SLinus Torvalds error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry); 3562123df294SAl Viro if (!error) 3563123df294SAl Viro fsnotify_move(old_dir, new_dir, old_name, is_dir, 35645a190ae6SAl Viro new_dentry->d_inode, old_dentry); 35650eeca283SRobert Love fsnotify_oldname_free(old_name); 35660eeca283SRobert Love 35671da177e4SLinus Torvalds return error; 35681da177e4SLinus Torvalds } 35691da177e4SLinus Torvalds 35702e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname, 35712e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 35721da177e4SLinus Torvalds { 35731da177e4SLinus Torvalds struct dentry *old_dir, *new_dir; 35741da177e4SLinus Torvalds struct dentry *old_dentry, *new_dentry; 35751da177e4SLinus Torvalds struct dentry *trap; 35761da177e4SLinus Torvalds struct nameidata oldnd, newnd; 35772ad94ae6SAl Viro char *from; 35782ad94ae6SAl Viro char *to; 35792ad94ae6SAl Viro int error; 35801da177e4SLinus Torvalds 35812ad94ae6SAl Viro error = user_path_parent(olddfd, oldname, &oldnd, &from); 35821da177e4SLinus Torvalds if (error) 35831da177e4SLinus Torvalds goto exit; 35841da177e4SLinus Torvalds 35852ad94ae6SAl Viro error = user_path_parent(newdfd, newname, &newnd, &to); 35861da177e4SLinus Torvalds if (error) 35871da177e4SLinus Torvalds goto exit1; 35881da177e4SLinus Torvalds 35891da177e4SLinus Torvalds error = -EXDEV; 35904ac91378SJan Blunck if (oldnd.path.mnt != newnd.path.mnt) 35911da177e4SLinus Torvalds goto exit2; 35921da177e4SLinus Torvalds 35934ac91378SJan Blunck old_dir = oldnd.path.dentry; 35941da177e4SLinus Torvalds error = -EBUSY; 35951da177e4SLinus Torvalds if (oldnd.last_type != LAST_NORM) 35961da177e4SLinus Torvalds goto exit2; 35971da177e4SLinus Torvalds 35984ac91378SJan Blunck new_dir = newnd.path.dentry; 35991da177e4SLinus Torvalds if (newnd.last_type != LAST_NORM) 36001da177e4SLinus Torvalds goto exit2; 36011da177e4SLinus Torvalds 36020612d9fbSOGAWA Hirofumi oldnd.flags &= ~LOOKUP_PARENT; 36030612d9fbSOGAWA Hirofumi newnd.flags &= ~LOOKUP_PARENT; 36044e9ed2f8SOGAWA Hirofumi newnd.flags |= LOOKUP_RENAME_TARGET; 36050612d9fbSOGAWA Hirofumi 36061da177e4SLinus Torvalds trap = lock_rename(new_dir, old_dir); 36071da177e4SLinus Torvalds 360849705b77SChristoph Hellwig old_dentry = lookup_hash(&oldnd); 36091da177e4SLinus Torvalds error = PTR_ERR(old_dentry); 36101da177e4SLinus Torvalds if (IS_ERR(old_dentry)) 36111da177e4SLinus Torvalds goto exit3; 36121da177e4SLinus Torvalds /* source must exist */ 36131da177e4SLinus Torvalds error = -ENOENT; 36141da177e4SLinus Torvalds if (!old_dentry->d_inode) 36151da177e4SLinus Torvalds goto exit4; 36161da177e4SLinus Torvalds /* unless the source is a directory trailing slashes give -ENOTDIR */ 36171da177e4SLinus Torvalds if (!S_ISDIR(old_dentry->d_inode->i_mode)) { 36181da177e4SLinus Torvalds error = -ENOTDIR; 36191da177e4SLinus Torvalds if (oldnd.last.name[oldnd.last.len]) 36201da177e4SLinus Torvalds goto exit4; 36211da177e4SLinus Torvalds if (newnd.last.name[newnd.last.len]) 36221da177e4SLinus Torvalds goto exit4; 36231da177e4SLinus Torvalds } 36241da177e4SLinus Torvalds /* source should not be ancestor of target */ 36251da177e4SLinus Torvalds error = -EINVAL; 36261da177e4SLinus Torvalds if (old_dentry == trap) 36271da177e4SLinus Torvalds goto exit4; 362849705b77SChristoph Hellwig new_dentry = lookup_hash(&newnd); 36291da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 36301da177e4SLinus Torvalds if (IS_ERR(new_dentry)) 36311da177e4SLinus Torvalds goto exit4; 36321da177e4SLinus Torvalds /* target should not be an ancestor of source */ 36331da177e4SLinus Torvalds error = -ENOTEMPTY; 36341da177e4SLinus Torvalds if (new_dentry == trap) 36351da177e4SLinus Torvalds goto exit5; 36361da177e4SLinus Torvalds 36379079b1ebSDave Hansen error = mnt_want_write(oldnd.path.mnt); 36389079b1ebSDave Hansen if (error) 36399079b1ebSDave Hansen goto exit5; 3640be6d3e56SKentaro Takeda error = security_path_rename(&oldnd.path, old_dentry, 3641be6d3e56SKentaro Takeda &newnd.path, new_dentry); 3642be6d3e56SKentaro Takeda if (error) 3643be6d3e56SKentaro Takeda goto exit6; 36441da177e4SLinus Torvalds error = vfs_rename(old_dir->d_inode, old_dentry, 36451da177e4SLinus Torvalds new_dir->d_inode, new_dentry); 3646be6d3e56SKentaro Takeda exit6: 36479079b1ebSDave Hansen mnt_drop_write(oldnd.path.mnt); 36481da177e4SLinus Torvalds exit5: 36491da177e4SLinus Torvalds dput(new_dentry); 36501da177e4SLinus Torvalds exit4: 36511da177e4SLinus Torvalds dput(old_dentry); 36521da177e4SLinus Torvalds exit3: 36531da177e4SLinus Torvalds unlock_rename(new_dir, old_dir); 36541da177e4SLinus Torvalds exit2: 36551d957f9bSJan Blunck path_put(&newnd.path); 36562ad94ae6SAl Viro putname(to); 36571da177e4SLinus Torvalds exit1: 36581d957f9bSJan Blunck path_put(&oldnd.path); 36591da177e4SLinus Torvalds putname(from); 36602ad94ae6SAl Viro exit: 36611da177e4SLinus Torvalds return error; 36621da177e4SLinus Torvalds } 36631da177e4SLinus Torvalds 3664a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname) 36655590ff0dSUlrich Drepper { 36665590ff0dSUlrich Drepper return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname); 36675590ff0dSUlrich Drepper } 36685590ff0dSUlrich Drepper 36691da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link) 36701da177e4SLinus Torvalds { 36711da177e4SLinus Torvalds int len; 36721da177e4SLinus Torvalds 36731da177e4SLinus Torvalds len = PTR_ERR(link); 36741da177e4SLinus Torvalds if (IS_ERR(link)) 36751da177e4SLinus Torvalds goto out; 36761da177e4SLinus Torvalds 36771da177e4SLinus Torvalds len = strlen(link); 36781da177e4SLinus Torvalds if (len > (unsigned) buflen) 36791da177e4SLinus Torvalds len = buflen; 36801da177e4SLinus Torvalds if (copy_to_user(buffer, link, len)) 36811da177e4SLinus Torvalds len = -EFAULT; 36821da177e4SLinus Torvalds out: 36831da177e4SLinus Torvalds return len; 36841da177e4SLinus Torvalds } 36851da177e4SLinus Torvalds 36861da177e4SLinus Torvalds /* 36871da177e4SLinus Torvalds * A helper for ->readlink(). This should be used *ONLY* for symlinks that 36881da177e4SLinus Torvalds * have ->follow_link() touching nd only in nd_set_link(). Using (or not 36891da177e4SLinus Torvalds * using) it for any given inode is up to filesystem. 36901da177e4SLinus Torvalds */ 36911da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen) 36921da177e4SLinus Torvalds { 36931da177e4SLinus Torvalds struct nameidata nd; 3694cc314eefSLinus Torvalds void *cookie; 3695694a1764SMarcin Slusarz int res; 3696cc314eefSLinus Torvalds 36971da177e4SLinus Torvalds nd.depth = 0; 3698cc314eefSLinus Torvalds cookie = dentry->d_inode->i_op->follow_link(dentry, &nd); 3699694a1764SMarcin Slusarz if (IS_ERR(cookie)) 3700694a1764SMarcin Slusarz return PTR_ERR(cookie); 3701694a1764SMarcin Slusarz 3702694a1764SMarcin Slusarz res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd)); 37031da177e4SLinus Torvalds if (dentry->d_inode->i_op->put_link) 3704cc314eefSLinus Torvalds dentry->d_inode->i_op->put_link(dentry, &nd, cookie); 3705694a1764SMarcin Slusarz return res; 37061da177e4SLinus Torvalds } 37071da177e4SLinus Torvalds 37081da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link) 37091da177e4SLinus Torvalds { 37101da177e4SLinus Torvalds return __vfs_follow_link(nd, link); 37111da177e4SLinus Torvalds } 37121da177e4SLinus Torvalds 37131da177e4SLinus Torvalds /* get the link contents into pagecache */ 37141da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage) 37151da177e4SLinus Torvalds { 3716ebd09abbSDuane Griffin char *kaddr; 37171da177e4SLinus Torvalds struct page *page; 37181da177e4SLinus Torvalds struct address_space *mapping = dentry->d_inode->i_mapping; 3719090d2b18SPekka Enberg page = read_mapping_page(mapping, 0, NULL); 37201da177e4SLinus Torvalds if (IS_ERR(page)) 37216fe6900eSNick Piggin return (char*)page; 37221da177e4SLinus Torvalds *ppage = page; 3723ebd09abbSDuane Griffin kaddr = kmap(page); 3724ebd09abbSDuane Griffin nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1); 3725ebd09abbSDuane Griffin return kaddr; 37261da177e4SLinus Torvalds } 37271da177e4SLinus Torvalds 37281da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen) 37291da177e4SLinus Torvalds { 37301da177e4SLinus Torvalds struct page *page = NULL; 37311da177e4SLinus Torvalds char *s = page_getlink(dentry, &page); 37321da177e4SLinus Torvalds int res = vfs_readlink(dentry,buffer,buflen,s); 37331da177e4SLinus Torvalds if (page) { 37341da177e4SLinus Torvalds kunmap(page); 37351da177e4SLinus Torvalds page_cache_release(page); 37361da177e4SLinus Torvalds } 37371da177e4SLinus Torvalds return res; 37381da177e4SLinus Torvalds } 37391da177e4SLinus Torvalds 3740cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd) 37411da177e4SLinus Torvalds { 3742cc314eefSLinus Torvalds struct page *page = NULL; 37431da177e4SLinus Torvalds nd_set_link(nd, page_getlink(dentry, &page)); 3744cc314eefSLinus Torvalds return page; 37451da177e4SLinus Torvalds } 37461da177e4SLinus Torvalds 3747cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie) 37481da177e4SLinus Torvalds { 3749cc314eefSLinus Torvalds struct page *page = cookie; 3750cc314eefSLinus Torvalds 3751cc314eefSLinus Torvalds if (page) { 37521da177e4SLinus Torvalds kunmap(page); 37531da177e4SLinus Torvalds page_cache_release(page); 37541da177e4SLinus Torvalds } 37551da177e4SLinus Torvalds } 37561da177e4SLinus Torvalds 375754566b2cSNick Piggin /* 375854566b2cSNick Piggin * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS 375954566b2cSNick Piggin */ 376054566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs) 37611da177e4SLinus Torvalds { 37621da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 37630adb25d2SKirill Korotaev struct page *page; 3764afddba49SNick Piggin void *fsdata; 3765beb497abSDmitriy Monakhov int err; 37661da177e4SLinus Torvalds char *kaddr; 376754566b2cSNick Piggin unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE; 376854566b2cSNick Piggin if (nofs) 376954566b2cSNick Piggin flags |= AOP_FLAG_NOFS; 37701da177e4SLinus Torvalds 37717e53cac4SNeilBrown retry: 3772afddba49SNick Piggin err = pagecache_write_begin(NULL, mapping, 0, len-1, 377354566b2cSNick Piggin flags, &page, &fsdata); 37741da177e4SLinus Torvalds if (err) 3775afddba49SNick Piggin goto fail; 3776afddba49SNick Piggin 3777e8e3c3d6SCong Wang kaddr = kmap_atomic(page); 37781da177e4SLinus Torvalds memcpy(kaddr, symname, len-1); 3779e8e3c3d6SCong Wang kunmap_atomic(kaddr); 3780afddba49SNick Piggin 3781afddba49SNick Piggin err = pagecache_write_end(NULL, mapping, 0, len-1, len-1, 3782afddba49SNick Piggin page, fsdata); 37831da177e4SLinus Torvalds if (err < 0) 37841da177e4SLinus Torvalds goto fail; 3785afddba49SNick Piggin if (err < len-1) 3786afddba49SNick Piggin goto retry; 3787afddba49SNick Piggin 37881da177e4SLinus Torvalds mark_inode_dirty(inode); 37891da177e4SLinus Torvalds return 0; 37901da177e4SLinus Torvalds fail: 37911da177e4SLinus Torvalds return err; 37921da177e4SLinus Torvalds } 37931da177e4SLinus Torvalds 37940adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len) 37950adb25d2SKirill Korotaev { 37960adb25d2SKirill Korotaev return __page_symlink(inode, symname, len, 379754566b2cSNick Piggin !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS)); 37980adb25d2SKirill Korotaev } 37990adb25d2SKirill Korotaev 380092e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = { 38011da177e4SLinus Torvalds .readlink = generic_readlink, 38021da177e4SLinus Torvalds .follow_link = page_follow_link_light, 38031da177e4SLinus Torvalds .put_link = page_put_link, 38041da177e4SLinus Torvalds }; 38051da177e4SLinus Torvalds 38062d8f3038SAl Viro EXPORT_SYMBOL(user_path_at); 3807cc53ce53SDavid Howells EXPORT_SYMBOL(follow_down_one); 38081da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down); 38091da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up); 38101da177e4SLinus Torvalds EXPORT_SYMBOL(get_write_access); /* binfmt_aout */ 38111da177e4SLinus Torvalds EXPORT_SYMBOL(getname); 38121da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename); 38131da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len); 38141da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light); 38151da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link); 38161da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink); 38170adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink); 38181da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink); 38191da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations); 3820d1811465SAl Viro EXPORT_SYMBOL(kern_path); 382116f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup); 3822f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission); 38231da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename); 38241da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create); 38251da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link); 38261da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link); 38271da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir); 38281da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod); 38291da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission); 38301da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink); 38311da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename); 38321da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir); 38331da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink); 38341da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink); 38351da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash); 38361da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink); 3837