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 */ 12091a27b2aSJeff Layton void final_putname(struct filename *name) 1211da177e4SLinus Torvalds { 1227950e385SJeff Layton if (name->separate) { 12391a27b2aSJeff Layton __putname(name->name); 12491a27b2aSJeff Layton kfree(name); 1257950e385SJeff Layton } else { 1267950e385SJeff Layton __putname(name); 12791a27b2aSJeff Layton } 1287950e385SJeff Layton } 1297950e385SJeff Layton 1307950e385SJeff Layton #define EMBEDDED_NAME_MAX (PATH_MAX - sizeof(struct filename)) 13191a27b2aSJeff Layton 13291a27b2aSJeff Layton static struct filename * 13391a27b2aSJeff Layton getname_flags(const char __user *filename, int flags, int *empty) 13491a27b2aSJeff Layton { 13591a27b2aSJeff Layton struct filename *result, *err; 1363f9f0aa6SLinus Torvalds int len; 1377950e385SJeff Layton long max; 1387950e385SJeff Layton char *kname; 1391da177e4SLinus Torvalds 1407ac86265SJeff Layton result = audit_reusename(filename); 1417ac86265SJeff Layton if (result) 1427ac86265SJeff Layton return result; 1437ac86265SJeff Layton 1447950e385SJeff Layton result = __getname(); 1453f9f0aa6SLinus Torvalds if (unlikely(!result)) 1464043cde8SEric Paris return ERR_PTR(-ENOMEM); 1471da177e4SLinus Torvalds 1487950e385SJeff Layton /* 1497950e385SJeff Layton * First, try to embed the struct filename inside the names_cache 1507950e385SJeff Layton * allocation 1517950e385SJeff Layton */ 1527950e385SJeff Layton kname = (char *)result + sizeof(*result); 15391a27b2aSJeff Layton result->name = kname; 1547950e385SJeff Layton result->separate = false; 1557950e385SJeff Layton max = EMBEDDED_NAME_MAX; 1567950e385SJeff Layton 1577950e385SJeff Layton recopy: 1587950e385SJeff Layton len = strncpy_from_user(kname, filename, max); 15991a27b2aSJeff Layton if (unlikely(len < 0)) { 1603f9f0aa6SLinus Torvalds err = ERR_PTR(len); 1613f9f0aa6SLinus Torvalds goto error; 16291a27b2aSJeff Layton } 1633f9f0aa6SLinus Torvalds 1647950e385SJeff Layton /* 1657950e385SJeff Layton * Uh-oh. We have a name that's approaching PATH_MAX. Allocate a 1667950e385SJeff Layton * separate struct filename so we can dedicate the entire 1677950e385SJeff Layton * names_cache allocation for the pathname, and re-do the copy from 1687950e385SJeff Layton * userland. 1697950e385SJeff Layton */ 1707950e385SJeff Layton if (len == EMBEDDED_NAME_MAX && max == EMBEDDED_NAME_MAX) { 1717950e385SJeff Layton kname = (char *)result; 1727950e385SJeff Layton 1737950e385SJeff Layton result = kzalloc(sizeof(*result), GFP_KERNEL); 1747950e385SJeff Layton if (!result) { 1757950e385SJeff Layton err = ERR_PTR(-ENOMEM); 1767950e385SJeff Layton result = (struct filename *)kname; 1777950e385SJeff Layton goto error; 1787950e385SJeff Layton } 1797950e385SJeff Layton result->name = kname; 1807950e385SJeff Layton result->separate = true; 1817950e385SJeff Layton max = PATH_MAX; 1827950e385SJeff Layton goto recopy; 1837950e385SJeff Layton } 1847950e385SJeff Layton 1853f9f0aa6SLinus Torvalds /* The empty path is special. */ 1863f9f0aa6SLinus Torvalds if (unlikely(!len)) { 1873f9f0aa6SLinus Torvalds if (empty) 1881fa1e7f6SAndy Whitcroft *empty = 1; 1893f9f0aa6SLinus Torvalds err = ERR_PTR(-ENOENT); 1903f9f0aa6SLinus Torvalds if (!(flags & LOOKUP_EMPTY)) 1913f9f0aa6SLinus Torvalds goto error; 1921da177e4SLinus Torvalds } 1933f9f0aa6SLinus Torvalds 1943f9f0aa6SLinus Torvalds err = ERR_PTR(-ENAMETOOLONG); 1957950e385SJeff Layton if (unlikely(len >= PATH_MAX)) 1967950e385SJeff Layton goto error; 1977950e385SJeff Layton 1987950e385SJeff Layton result->uptr = filename; 1991da177e4SLinus Torvalds audit_getname(result); 2001da177e4SLinus Torvalds return result; 2011da177e4SLinus Torvalds 2023f9f0aa6SLinus Torvalds error: 2037950e385SJeff Layton final_putname(result); 2043f9f0aa6SLinus Torvalds return err; 2053f9f0aa6SLinus Torvalds } 2063f9f0aa6SLinus Torvalds 20791a27b2aSJeff Layton struct filename * 20891a27b2aSJeff Layton getname(const char __user * filename) 209f52e0c11SAl Viro { 210f7493e5dSLinus Torvalds return getname_flags(filename, 0, NULL); 211f52e0c11SAl Viro } 21291a27b2aSJeff Layton EXPORT_SYMBOL(getname); 213f52e0c11SAl Viro 2141da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL 21591a27b2aSJeff Layton void putname(struct filename *name) 2161da177e4SLinus Torvalds { 2175ac3a9c2SAl Viro if (unlikely(!audit_dummy_context())) 21891a27b2aSJeff Layton return audit_putname(name); 21991a27b2aSJeff Layton final_putname(name); 2201da177e4SLinus Torvalds } 2211da177e4SLinus Torvalds #endif 2221da177e4SLinus Torvalds 223e77819e5SLinus Torvalds static int check_acl(struct inode *inode, int mask) 224e77819e5SLinus Torvalds { 22584635d68SLinus Torvalds #ifdef CONFIG_FS_POSIX_ACL 226e77819e5SLinus Torvalds struct posix_acl *acl; 227e77819e5SLinus Torvalds 228e77819e5SLinus Torvalds if (mask & MAY_NOT_BLOCK) { 2293567866bSAl Viro acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS); 2303567866bSAl Viro if (!acl) 231e77819e5SLinus Torvalds return -EAGAIN; 2323567866bSAl Viro /* no ->get_acl() calls in RCU mode... */ 2333567866bSAl Viro if (acl == ACL_NOT_CACHED) 234e77819e5SLinus Torvalds return -ECHILD; 235206b1d09SAri Savolainen return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK); 236e77819e5SLinus Torvalds } 237e77819e5SLinus Torvalds 238e77819e5SLinus Torvalds acl = get_cached_acl(inode, ACL_TYPE_ACCESS); 239e77819e5SLinus Torvalds 240e77819e5SLinus Torvalds /* 2414e34e719SChristoph Hellwig * A filesystem can force a ACL callback by just never filling the 2424e34e719SChristoph Hellwig * ACL cache. But normally you'd fill the cache either at inode 2434e34e719SChristoph Hellwig * instantiation time, or on the first ->get_acl call. 244e77819e5SLinus Torvalds * 2454e34e719SChristoph Hellwig * If the filesystem doesn't have a get_acl() function at all, we'll 2464e34e719SChristoph Hellwig * just create the negative cache entry. 247e77819e5SLinus Torvalds */ 248e77819e5SLinus Torvalds if (acl == ACL_NOT_CACHED) { 2494e34e719SChristoph Hellwig if (inode->i_op->get_acl) { 2504e34e719SChristoph Hellwig acl = inode->i_op->get_acl(inode, ACL_TYPE_ACCESS); 2514e34e719SChristoph Hellwig if (IS_ERR(acl)) 2524e34e719SChristoph Hellwig return PTR_ERR(acl); 2534e34e719SChristoph Hellwig } else { 254e77819e5SLinus Torvalds set_cached_acl(inode, ACL_TYPE_ACCESS, NULL); 255e77819e5SLinus Torvalds return -EAGAIN; 256e77819e5SLinus Torvalds } 2574e34e719SChristoph Hellwig } 258e77819e5SLinus Torvalds 259e77819e5SLinus Torvalds if (acl) { 260e77819e5SLinus Torvalds int error = posix_acl_permission(inode, acl, mask); 261e77819e5SLinus Torvalds posix_acl_release(acl); 262e77819e5SLinus Torvalds return error; 263e77819e5SLinus Torvalds } 26484635d68SLinus Torvalds #endif 265e77819e5SLinus Torvalds 266e77819e5SLinus Torvalds return -EAGAIN; 267e77819e5SLinus Torvalds } 268e77819e5SLinus Torvalds 2695909ccaaSLinus Torvalds /* 270948409c7SAndreas Gruenbacher * This does the basic permission checking 2715909ccaaSLinus Torvalds */ 2727e40145eSAl Viro static int acl_permission_check(struct inode *inode, int mask) 2735909ccaaSLinus Torvalds { 27426cf46beSLinus Torvalds unsigned int mode = inode->i_mode; 2755909ccaaSLinus Torvalds 2768e96e3b7SEric W. Biederman if (likely(uid_eq(current_fsuid(), inode->i_uid))) 2775909ccaaSLinus Torvalds mode >>= 6; 2785909ccaaSLinus Torvalds else { 279e77819e5SLinus Torvalds if (IS_POSIXACL(inode) && (mode & S_IRWXG)) { 2807e40145eSAl Viro int error = check_acl(inode, mask); 2815909ccaaSLinus Torvalds if (error != -EAGAIN) 2825909ccaaSLinus Torvalds return error; 2835909ccaaSLinus Torvalds } 2845909ccaaSLinus Torvalds 2855909ccaaSLinus Torvalds if (in_group_p(inode->i_gid)) 2865909ccaaSLinus Torvalds mode >>= 3; 2875909ccaaSLinus Torvalds } 2885909ccaaSLinus Torvalds 2895909ccaaSLinus Torvalds /* 2905909ccaaSLinus Torvalds * If the DACs are ok we don't need any capability check. 2915909ccaaSLinus Torvalds */ 2929c2c7039SAl Viro if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0) 2935909ccaaSLinus Torvalds return 0; 2945909ccaaSLinus Torvalds return -EACCES; 2955909ccaaSLinus Torvalds } 2961da177e4SLinus Torvalds 2971da177e4SLinus Torvalds /** 2981da177e4SLinus Torvalds * generic_permission - check for access rights on a Posix-like filesystem 2991da177e4SLinus Torvalds * @inode: inode to check access rights for 3008fd90c8dSAndreas Gruenbacher * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...) 3011da177e4SLinus Torvalds * 3021da177e4SLinus Torvalds * Used to check for read/write/execute permissions on a file. 3031da177e4SLinus Torvalds * We use "fsuid" for this, letting us set arbitrary permissions 3041da177e4SLinus Torvalds * for filesystem access without changing the "normal" uids which 305b74c79e9SNick Piggin * are used for other things. 306b74c79e9SNick Piggin * 307b74c79e9SNick Piggin * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk 308b74c79e9SNick Piggin * request cannot be satisfied (eg. requires blocking or too much complexity). 309b74c79e9SNick Piggin * It would then be called again in ref-walk mode. 3101da177e4SLinus Torvalds */ 3112830ba7fSAl Viro int generic_permission(struct inode *inode, int mask) 3121da177e4SLinus Torvalds { 3135909ccaaSLinus Torvalds int ret; 3141da177e4SLinus Torvalds 3151da177e4SLinus Torvalds /* 316948409c7SAndreas Gruenbacher * Do the basic permission checks. 3171da177e4SLinus Torvalds */ 3187e40145eSAl Viro ret = acl_permission_check(inode, mask); 3195909ccaaSLinus Torvalds if (ret != -EACCES) 3205909ccaaSLinus Torvalds return ret; 3211da177e4SLinus Torvalds 322d594e7ecSAl Viro if (S_ISDIR(inode->i_mode)) { 323d594e7ecSAl Viro /* DACs are overridable for directories */ 3241a48e2acSEric W. Biederman if (inode_capable(inode, CAP_DAC_OVERRIDE)) 325d594e7ecSAl Viro return 0; 326d594e7ecSAl Viro if (!(mask & MAY_WRITE)) 3271a48e2acSEric W. Biederman if (inode_capable(inode, CAP_DAC_READ_SEARCH)) 328d594e7ecSAl Viro return 0; 329d594e7ecSAl Viro return -EACCES; 330d594e7ecSAl Viro } 3311da177e4SLinus Torvalds /* 3321da177e4SLinus Torvalds * Read/write DACs are always overridable. 333d594e7ecSAl Viro * Executable DACs are overridable when there is 334d594e7ecSAl Viro * at least one exec bit set. 3351da177e4SLinus Torvalds */ 336d594e7ecSAl Viro if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO)) 3371a48e2acSEric W. Biederman if (inode_capable(inode, CAP_DAC_OVERRIDE)) 3381da177e4SLinus Torvalds return 0; 3391da177e4SLinus Torvalds 3401da177e4SLinus Torvalds /* 3411da177e4SLinus Torvalds * Searching includes executable on directories, else just read. 3421da177e4SLinus Torvalds */ 3437ea66001SSerge E. Hallyn mask &= MAY_READ | MAY_WRITE | MAY_EXEC; 344d594e7ecSAl Viro if (mask == MAY_READ) 3451a48e2acSEric W. Biederman if (inode_capable(inode, CAP_DAC_READ_SEARCH)) 3461da177e4SLinus Torvalds return 0; 3471da177e4SLinus Torvalds 3481da177e4SLinus Torvalds return -EACCES; 3491da177e4SLinus Torvalds } 3501da177e4SLinus Torvalds 3513ddcd056SLinus Torvalds /* 3523ddcd056SLinus Torvalds * We _really_ want to just do "generic_permission()" without 3533ddcd056SLinus Torvalds * even looking at the inode->i_op values. So we keep a cache 3543ddcd056SLinus Torvalds * flag in inode->i_opflags, that says "this has not special 3553ddcd056SLinus Torvalds * permission function, use the fast case". 3563ddcd056SLinus Torvalds */ 3573ddcd056SLinus Torvalds static inline int do_inode_permission(struct inode *inode, int mask) 3583ddcd056SLinus Torvalds { 3593ddcd056SLinus Torvalds if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) { 3603ddcd056SLinus Torvalds if (likely(inode->i_op->permission)) 3613ddcd056SLinus Torvalds return inode->i_op->permission(inode, mask); 3623ddcd056SLinus Torvalds 3633ddcd056SLinus Torvalds /* This gets set once for the inode lifetime */ 3643ddcd056SLinus Torvalds spin_lock(&inode->i_lock); 3653ddcd056SLinus Torvalds inode->i_opflags |= IOP_FASTPERM; 3663ddcd056SLinus Torvalds spin_unlock(&inode->i_lock); 3673ddcd056SLinus Torvalds } 3683ddcd056SLinus Torvalds return generic_permission(inode, mask); 3693ddcd056SLinus Torvalds } 3703ddcd056SLinus Torvalds 371cb23beb5SChristoph Hellwig /** 3720bdaea90SDavid Howells * __inode_permission - Check for access rights to a given inode 3730bdaea90SDavid Howells * @inode: Inode to check permission on 3740bdaea90SDavid Howells * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) 375cb23beb5SChristoph Hellwig * 3760bdaea90SDavid Howells * Check for read/write/execute permissions on an inode. 377948409c7SAndreas Gruenbacher * 378948409c7SAndreas Gruenbacher * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask. 3790bdaea90SDavid Howells * 3800bdaea90SDavid Howells * This does not check for a read-only file system. You probably want 3810bdaea90SDavid Howells * inode_permission(). 382cb23beb5SChristoph Hellwig */ 3830bdaea90SDavid Howells int __inode_permission(struct inode *inode, int mask) 3841da177e4SLinus Torvalds { 385e6305c43SAl Viro int retval; 3861da177e4SLinus Torvalds 3873ddcd056SLinus Torvalds if (unlikely(mask & MAY_WRITE)) { 3881da177e4SLinus Torvalds /* 3891da177e4SLinus Torvalds * Nobody gets write access to an immutable file. 3901da177e4SLinus Torvalds */ 3911da177e4SLinus Torvalds if (IS_IMMUTABLE(inode)) 3921da177e4SLinus Torvalds return -EACCES; 3931da177e4SLinus Torvalds } 3941da177e4SLinus Torvalds 3953ddcd056SLinus Torvalds retval = do_inode_permission(inode, mask); 3961da177e4SLinus Torvalds if (retval) 3971da177e4SLinus Torvalds return retval; 3981da177e4SLinus Torvalds 39908ce5f16SSerge E. Hallyn retval = devcgroup_inode_permission(inode, mask); 40008ce5f16SSerge E. Hallyn if (retval) 40108ce5f16SSerge E. Hallyn return retval; 40208ce5f16SSerge E. Hallyn 403d09ca739SEric Paris return security_inode_permission(inode, mask); 4041da177e4SLinus Torvalds } 4051da177e4SLinus Torvalds 406f4d6ff89SAl Viro /** 4070bdaea90SDavid Howells * sb_permission - Check superblock-level permissions 4080bdaea90SDavid Howells * @sb: Superblock of inode to check permission on 40955852635SRandy Dunlap * @inode: Inode to check permission on 4100bdaea90SDavid Howells * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) 4110bdaea90SDavid Howells * 4120bdaea90SDavid Howells * Separate out file-system wide checks from inode-specific permission checks. 4130bdaea90SDavid Howells */ 4140bdaea90SDavid Howells static int sb_permission(struct super_block *sb, struct inode *inode, int mask) 4150bdaea90SDavid Howells { 4160bdaea90SDavid Howells if (unlikely(mask & MAY_WRITE)) { 4170bdaea90SDavid Howells umode_t mode = inode->i_mode; 4180bdaea90SDavid Howells 4190bdaea90SDavid Howells /* Nobody gets write access to a read-only fs. */ 4200bdaea90SDavid Howells if ((sb->s_flags & MS_RDONLY) && 4210bdaea90SDavid Howells (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) 4220bdaea90SDavid Howells return -EROFS; 4230bdaea90SDavid Howells } 4240bdaea90SDavid Howells return 0; 4250bdaea90SDavid Howells } 4260bdaea90SDavid Howells 4270bdaea90SDavid Howells /** 4280bdaea90SDavid Howells * inode_permission - Check for access rights to a given inode 4290bdaea90SDavid Howells * @inode: Inode to check permission on 4300bdaea90SDavid Howells * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) 4310bdaea90SDavid Howells * 4320bdaea90SDavid Howells * Check for read/write/execute permissions on an inode. We use fs[ug]id for 4330bdaea90SDavid Howells * this, letting us set arbitrary permissions for filesystem access without 4340bdaea90SDavid Howells * changing the "normal" UIDs which are used for other things. 4350bdaea90SDavid Howells * 4360bdaea90SDavid Howells * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask. 4370bdaea90SDavid Howells */ 4380bdaea90SDavid Howells int inode_permission(struct inode *inode, int mask) 4390bdaea90SDavid Howells { 4400bdaea90SDavid Howells int retval; 4410bdaea90SDavid Howells 4420bdaea90SDavid Howells retval = sb_permission(inode->i_sb, inode, mask); 4430bdaea90SDavid Howells if (retval) 4440bdaea90SDavid Howells return retval; 4450bdaea90SDavid Howells return __inode_permission(inode, mask); 4460bdaea90SDavid Howells } 4470bdaea90SDavid Howells 4480bdaea90SDavid Howells /** 4495dd784d0SJan Blunck * path_get - get a reference to a path 4505dd784d0SJan Blunck * @path: path to get the reference to 4515dd784d0SJan Blunck * 4525dd784d0SJan Blunck * Given a path increment the reference count to the dentry and the vfsmount. 4535dd784d0SJan Blunck */ 454dcf787f3SAl Viro void path_get(const struct path *path) 4555dd784d0SJan Blunck { 4565dd784d0SJan Blunck mntget(path->mnt); 4575dd784d0SJan Blunck dget(path->dentry); 4585dd784d0SJan Blunck } 4595dd784d0SJan Blunck EXPORT_SYMBOL(path_get); 4605dd784d0SJan Blunck 4615dd784d0SJan Blunck /** 4621d957f9bSJan Blunck * path_put - put a reference to a path 4631d957f9bSJan Blunck * @path: path to put the reference to 4641d957f9bSJan Blunck * 4651d957f9bSJan Blunck * Given a path decrement the reference count to the dentry and the vfsmount. 4661d957f9bSJan Blunck */ 467dcf787f3SAl Viro void path_put(const struct path *path) 4681da177e4SLinus Torvalds { 4691d957f9bSJan Blunck dput(path->dentry); 4701d957f9bSJan Blunck mntput(path->mnt); 4711da177e4SLinus Torvalds } 4721d957f9bSJan Blunck EXPORT_SYMBOL(path_put); 4731da177e4SLinus Torvalds 47419660af7SAl Viro /* 47531e6b01fSNick Piggin * Path walking has 2 modes, rcu-walk and ref-walk (see 47619660af7SAl Viro * Documentation/filesystems/path-lookup.txt). In situations when we can't 47719660af7SAl Viro * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab 47819660af7SAl Viro * normal reference counts on dentries and vfsmounts to transition to rcu-walk 47919660af7SAl Viro * mode. Refcounts are grabbed at the last known good point before rcu-walk 48019660af7SAl Viro * got stuck, so ref-walk may continue from there. If this is not successful 48119660af7SAl Viro * (eg. a seqcount has changed), then failure is returned and it's up to caller 48219660af7SAl Viro * to restart the path walk from the beginning in ref-walk mode. 48331e6b01fSNick Piggin */ 48431e6b01fSNick Piggin 48532a7991bSAl Viro static inline void lock_rcu_walk(void) 48632a7991bSAl Viro { 48732a7991bSAl Viro br_read_lock(&vfsmount_lock); 48832a7991bSAl Viro rcu_read_lock(); 48932a7991bSAl Viro } 49032a7991bSAl Viro 49132a7991bSAl Viro static inline void unlock_rcu_walk(void) 49232a7991bSAl Viro { 49332a7991bSAl Viro rcu_read_unlock(); 49432a7991bSAl Viro br_read_unlock(&vfsmount_lock); 49532a7991bSAl Viro } 49632a7991bSAl Viro 49731e6b01fSNick Piggin /** 49819660af7SAl Viro * unlazy_walk - try to switch to ref-walk mode. 49919660af7SAl Viro * @nd: nameidata pathwalk data 50019660af7SAl Viro * @dentry: child of nd->path.dentry or NULL 50139191628SRandy Dunlap * Returns: 0 on success, -ECHILD on failure 50231e6b01fSNick Piggin * 50319660af7SAl Viro * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry 50419660af7SAl Viro * for ref-walk mode. @dentry must be a path found by a do_lookup call on 50519660af7SAl Viro * @nd or NULL. Must be called from rcu-walk context. 50631e6b01fSNick Piggin */ 50719660af7SAl Viro static int unlazy_walk(struct nameidata *nd, struct dentry *dentry) 50831e6b01fSNick Piggin { 50931e6b01fSNick Piggin struct fs_struct *fs = current->fs; 51031e6b01fSNick Piggin struct dentry *parent = nd->path.dentry; 5115b6ca027SAl Viro int want_root = 0; 51231e6b01fSNick Piggin 51331e6b01fSNick Piggin BUG_ON(!(nd->flags & LOOKUP_RCU)); 5145b6ca027SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) { 5155b6ca027SAl Viro want_root = 1; 51631e6b01fSNick Piggin spin_lock(&fs->lock); 51731e6b01fSNick Piggin if (nd->root.mnt != fs->root.mnt || 51831e6b01fSNick Piggin nd->root.dentry != fs->root.dentry) 51931e6b01fSNick Piggin goto err_root; 52031e6b01fSNick Piggin } 52131e6b01fSNick Piggin spin_lock(&parent->d_lock); 52219660af7SAl Viro if (!dentry) { 52319660af7SAl Viro if (!__d_rcu_to_refcount(parent, nd->seq)) 52419660af7SAl Viro goto err_parent; 52519660af7SAl Viro BUG_ON(nd->inode != parent->d_inode); 52619660af7SAl Viro } else { 52794c0d4ecSAl Viro if (dentry->d_parent != parent) 52894c0d4ecSAl Viro goto err_parent; 52931e6b01fSNick Piggin spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); 53031e6b01fSNick Piggin if (!__d_rcu_to_refcount(dentry, nd->seq)) 53119660af7SAl Viro goto err_child; 53231e6b01fSNick Piggin /* 53319660af7SAl Viro * If the sequence check on the child dentry passed, then 53419660af7SAl Viro * the child has not been removed from its parent. This 53519660af7SAl Viro * means the parent dentry must be valid and able to take 53619660af7SAl Viro * a reference at this point. 53731e6b01fSNick Piggin */ 53831e6b01fSNick Piggin BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent); 53931e6b01fSNick Piggin BUG_ON(!parent->d_count); 54031e6b01fSNick Piggin parent->d_count++; 54131e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 54219660af7SAl Viro } 54331e6b01fSNick Piggin spin_unlock(&parent->d_lock); 5445b6ca027SAl Viro if (want_root) { 54531e6b01fSNick Piggin path_get(&nd->root); 54631e6b01fSNick Piggin spin_unlock(&fs->lock); 54731e6b01fSNick Piggin } 54831e6b01fSNick Piggin mntget(nd->path.mnt); 54931e6b01fSNick Piggin 55032a7991bSAl Viro unlock_rcu_walk(); 55131e6b01fSNick Piggin nd->flags &= ~LOOKUP_RCU; 55231e6b01fSNick Piggin return 0; 55319660af7SAl Viro 55419660af7SAl Viro err_child: 55531e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 55619660af7SAl Viro err_parent: 55731e6b01fSNick Piggin spin_unlock(&parent->d_lock); 55831e6b01fSNick Piggin err_root: 5595b6ca027SAl Viro if (want_root) 56031e6b01fSNick Piggin spin_unlock(&fs->lock); 56131e6b01fSNick Piggin return -ECHILD; 56231e6b01fSNick Piggin } 56331e6b01fSNick Piggin 5644ce16ef3SAl Viro static inline int d_revalidate(struct dentry *dentry, unsigned int flags) 56534286d66SNick Piggin { 5664ce16ef3SAl Viro return dentry->d_op->d_revalidate(dentry, flags); 56734286d66SNick Piggin } 56834286d66SNick Piggin 5699f1fafeeSAl Viro /** 5709f1fafeeSAl Viro * complete_walk - successful completion of path walk 5719f1fafeeSAl Viro * @nd: pointer nameidata 57239159de2SJeff Layton * 5739f1fafeeSAl Viro * If we had been in RCU mode, drop out of it and legitimize nd->path. 5749f1fafeeSAl Viro * Revalidate the final result, unless we'd already done that during 5759f1fafeeSAl Viro * the path walk or the filesystem doesn't ask for it. Return 0 on 5769f1fafeeSAl Viro * success, -error on failure. In case of failure caller does not 5779f1fafeeSAl Viro * need to drop nd->path. 57839159de2SJeff Layton */ 5799f1fafeeSAl Viro static int complete_walk(struct nameidata *nd) 58039159de2SJeff Layton { 58116c2cd71SAl Viro struct dentry *dentry = nd->path.dentry; 58239159de2SJeff Layton int status; 58339159de2SJeff Layton 5849f1fafeeSAl Viro if (nd->flags & LOOKUP_RCU) { 5859f1fafeeSAl Viro nd->flags &= ~LOOKUP_RCU; 5869f1fafeeSAl Viro if (!(nd->flags & LOOKUP_ROOT)) 5879f1fafeeSAl Viro nd->root.mnt = NULL; 5889f1fafeeSAl Viro spin_lock(&dentry->d_lock); 5899f1fafeeSAl Viro if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) { 5909f1fafeeSAl Viro spin_unlock(&dentry->d_lock); 59132a7991bSAl Viro unlock_rcu_walk(); 5929f1fafeeSAl Viro return -ECHILD; 5939f1fafeeSAl Viro } 5949f1fafeeSAl Viro BUG_ON(nd->inode != dentry->d_inode); 5959f1fafeeSAl Viro spin_unlock(&dentry->d_lock); 5969f1fafeeSAl Viro mntget(nd->path.mnt); 59732a7991bSAl Viro unlock_rcu_walk(); 5989f1fafeeSAl Viro } 5999f1fafeeSAl Viro 60016c2cd71SAl Viro if (likely(!(nd->flags & LOOKUP_JUMPED))) 60139159de2SJeff Layton return 0; 60239159de2SJeff Layton 603ecf3d1f1SJeff Layton if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE))) 60416c2cd71SAl Viro return 0; 60516c2cd71SAl Viro 606ecf3d1f1SJeff Layton status = dentry->d_op->d_weak_revalidate(dentry, nd->flags); 60739159de2SJeff Layton if (status > 0) 60839159de2SJeff Layton return 0; 60939159de2SJeff Layton 61016c2cd71SAl Viro if (!status) 61139159de2SJeff Layton status = -ESTALE; 61216c2cd71SAl Viro 6139f1fafeeSAl Viro path_put(&nd->path); 61439159de2SJeff Layton return status; 61539159de2SJeff Layton } 61639159de2SJeff Layton 6172a737871SAl Viro static __always_inline void set_root(struct nameidata *nd) 6182a737871SAl Viro { 619f7ad3c6bSMiklos Szeredi if (!nd->root.mnt) 620f7ad3c6bSMiklos Szeredi get_fs_root(current->fs, &nd->root); 6212a737871SAl Viro } 6222a737871SAl Viro 6236de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *); 6246de88d72SAl Viro 62531e6b01fSNick Piggin static __always_inline void set_root_rcu(struct nameidata *nd) 62631e6b01fSNick Piggin { 62731e6b01fSNick Piggin if (!nd->root.mnt) { 62831e6b01fSNick Piggin struct fs_struct *fs = current->fs; 629c28cc364SNick Piggin unsigned seq; 630c28cc364SNick Piggin 631c28cc364SNick Piggin do { 632c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 63331e6b01fSNick Piggin nd->root = fs->root; 634c1530019STim Chen nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq); 635c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 63631e6b01fSNick Piggin } 63731e6b01fSNick Piggin } 63831e6b01fSNick Piggin 639f1662356SArjan van de Ven static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link) 6401da177e4SLinus Torvalds { 64131e6b01fSNick Piggin int ret; 64231e6b01fSNick Piggin 6431da177e4SLinus Torvalds if (IS_ERR(link)) 6441da177e4SLinus Torvalds goto fail; 6451da177e4SLinus Torvalds 6461da177e4SLinus Torvalds if (*link == '/') { 6472a737871SAl Viro set_root(nd); 6481d957f9bSJan Blunck path_put(&nd->path); 6492a737871SAl Viro nd->path = nd->root; 6502a737871SAl Viro path_get(&nd->root); 65116c2cd71SAl Viro nd->flags |= LOOKUP_JUMPED; 6521da177e4SLinus Torvalds } 65331e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 654b4091d5fSChristoph Hellwig 65531e6b01fSNick Piggin ret = link_path_walk(link, nd); 65631e6b01fSNick Piggin return ret; 6571da177e4SLinus Torvalds fail: 6581d957f9bSJan Blunck path_put(&nd->path); 6591da177e4SLinus Torvalds return PTR_ERR(link); 6601da177e4SLinus Torvalds } 6611da177e4SLinus Torvalds 6621d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd) 663051d3812SIan Kent { 664051d3812SIan Kent dput(path->dentry); 6654ac91378SJan Blunck if (path->mnt != nd->path.mnt) 666051d3812SIan Kent mntput(path->mnt); 667051d3812SIan Kent } 668051d3812SIan Kent 6697b9337aaSNick Piggin static inline void path_to_nameidata(const struct path *path, 6707b9337aaSNick Piggin struct nameidata *nd) 671051d3812SIan Kent { 67231e6b01fSNick Piggin if (!(nd->flags & LOOKUP_RCU)) { 6734ac91378SJan Blunck dput(nd->path.dentry); 67431e6b01fSNick Piggin if (nd->path.mnt != path->mnt) 6754ac91378SJan Blunck mntput(nd->path.mnt); 6769a229683SHuang Shijie } 67731e6b01fSNick Piggin nd->path.mnt = path->mnt; 6784ac91378SJan Blunck nd->path.dentry = path->dentry; 679051d3812SIan Kent } 680051d3812SIan Kent 681b5fb63c1SChristoph Hellwig /* 682b5fb63c1SChristoph Hellwig * Helper to directly jump to a known parsed path from ->follow_link, 683b5fb63c1SChristoph Hellwig * caller must have taken a reference to path beforehand. 684b5fb63c1SChristoph Hellwig */ 685b5fb63c1SChristoph Hellwig void nd_jump_link(struct nameidata *nd, struct path *path) 686b5fb63c1SChristoph Hellwig { 687b5fb63c1SChristoph Hellwig path_put(&nd->path); 688b5fb63c1SChristoph Hellwig 689b5fb63c1SChristoph Hellwig nd->path = *path; 690b5fb63c1SChristoph Hellwig nd->inode = nd->path.dentry->d_inode; 691b5fb63c1SChristoph Hellwig nd->flags |= LOOKUP_JUMPED; 692b5fb63c1SChristoph Hellwig } 693b5fb63c1SChristoph Hellwig 694574197e0SAl Viro static inline void put_link(struct nameidata *nd, struct path *link, void *cookie) 695574197e0SAl Viro { 696574197e0SAl Viro struct inode *inode = link->dentry->d_inode; 6976d7b5aaeSAl Viro if (inode->i_op->put_link) 698574197e0SAl Viro inode->i_op->put_link(link->dentry, nd, cookie); 699574197e0SAl Viro path_put(link); 700574197e0SAl Viro } 701574197e0SAl Viro 702561ec64aSLinus Torvalds int sysctl_protected_symlinks __read_mostly = 0; 703561ec64aSLinus Torvalds int sysctl_protected_hardlinks __read_mostly = 0; 704800179c9SKees Cook 705800179c9SKees Cook /** 706800179c9SKees Cook * may_follow_link - Check symlink following for unsafe situations 707800179c9SKees Cook * @link: The path of the symlink 70855852635SRandy Dunlap * @nd: nameidata pathwalk data 709800179c9SKees Cook * 710800179c9SKees Cook * In the case of the sysctl_protected_symlinks sysctl being enabled, 711800179c9SKees Cook * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is 712800179c9SKees Cook * in a sticky world-writable directory. This is to protect privileged 713800179c9SKees Cook * processes from failing races against path names that may change out 714800179c9SKees Cook * from under them by way of other users creating malicious symlinks. 715800179c9SKees Cook * It will permit symlinks to be followed only when outside a sticky 716800179c9SKees Cook * world-writable directory, or when the uid of the symlink and follower 717800179c9SKees Cook * match, or when the directory owner matches the symlink's owner. 718800179c9SKees Cook * 719800179c9SKees Cook * Returns 0 if following the symlink is allowed, -ve on error. 720800179c9SKees Cook */ 721800179c9SKees Cook static inline int may_follow_link(struct path *link, struct nameidata *nd) 722800179c9SKees Cook { 723800179c9SKees Cook const struct inode *inode; 724800179c9SKees Cook const struct inode *parent; 725800179c9SKees Cook 726800179c9SKees Cook if (!sysctl_protected_symlinks) 727800179c9SKees Cook return 0; 728800179c9SKees Cook 729800179c9SKees Cook /* Allowed if owner and follower match. */ 730800179c9SKees Cook inode = link->dentry->d_inode; 73181abe27bSEric W. Biederman if (uid_eq(current_cred()->fsuid, inode->i_uid)) 732800179c9SKees Cook return 0; 733800179c9SKees Cook 734800179c9SKees Cook /* Allowed if parent directory not sticky and world-writable. */ 735800179c9SKees Cook parent = nd->path.dentry->d_inode; 736800179c9SKees Cook if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH)) 737800179c9SKees Cook return 0; 738800179c9SKees Cook 739800179c9SKees Cook /* Allowed if parent directory and link owner match. */ 74081abe27bSEric W. Biederman if (uid_eq(parent->i_uid, inode->i_uid)) 741800179c9SKees Cook return 0; 742800179c9SKees Cook 743ffd8d101SSasha Levin audit_log_link_denied("follow_link", link); 744800179c9SKees Cook path_put_conditional(link, nd); 745800179c9SKees Cook path_put(&nd->path); 746800179c9SKees Cook return -EACCES; 747800179c9SKees Cook } 748800179c9SKees Cook 749800179c9SKees Cook /** 750800179c9SKees Cook * safe_hardlink_source - Check for safe hardlink conditions 751800179c9SKees Cook * @inode: the source inode to hardlink from 752800179c9SKees Cook * 753800179c9SKees Cook * Return false if at least one of the following conditions: 754800179c9SKees Cook * - inode is not a regular file 755800179c9SKees Cook * - inode is setuid 756800179c9SKees Cook * - inode is setgid and group-exec 757800179c9SKees Cook * - access failure for read and write 758800179c9SKees Cook * 759800179c9SKees Cook * Otherwise returns true. 760800179c9SKees Cook */ 761800179c9SKees Cook static bool safe_hardlink_source(struct inode *inode) 762800179c9SKees Cook { 763800179c9SKees Cook umode_t mode = inode->i_mode; 764800179c9SKees Cook 765800179c9SKees Cook /* Special files should not get pinned to the filesystem. */ 766800179c9SKees Cook if (!S_ISREG(mode)) 767800179c9SKees Cook return false; 768800179c9SKees Cook 769800179c9SKees Cook /* Setuid files should not get pinned to the filesystem. */ 770800179c9SKees Cook if (mode & S_ISUID) 771800179c9SKees Cook return false; 772800179c9SKees Cook 773800179c9SKees Cook /* Executable setgid files should not get pinned to the filesystem. */ 774800179c9SKees Cook if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) 775800179c9SKees Cook return false; 776800179c9SKees Cook 777800179c9SKees Cook /* Hardlinking to unreadable or unwritable sources is dangerous. */ 778800179c9SKees Cook if (inode_permission(inode, MAY_READ | MAY_WRITE)) 779800179c9SKees Cook return false; 780800179c9SKees Cook 781800179c9SKees Cook return true; 782800179c9SKees Cook } 783800179c9SKees Cook 784800179c9SKees Cook /** 785800179c9SKees Cook * may_linkat - Check permissions for creating a hardlink 786800179c9SKees Cook * @link: the source to hardlink from 787800179c9SKees Cook * 788800179c9SKees Cook * Block hardlink when all of: 789800179c9SKees Cook * - sysctl_protected_hardlinks enabled 790800179c9SKees Cook * - fsuid does not match inode 791800179c9SKees Cook * - hardlink source is unsafe (see safe_hardlink_source() above) 792800179c9SKees Cook * - not CAP_FOWNER 793800179c9SKees Cook * 794800179c9SKees Cook * Returns 0 if successful, -ve on error. 795800179c9SKees Cook */ 796800179c9SKees Cook static int may_linkat(struct path *link) 797800179c9SKees Cook { 798800179c9SKees Cook const struct cred *cred; 799800179c9SKees Cook struct inode *inode; 800800179c9SKees Cook 801800179c9SKees Cook if (!sysctl_protected_hardlinks) 802800179c9SKees Cook return 0; 803800179c9SKees Cook 804800179c9SKees Cook cred = current_cred(); 805800179c9SKees Cook inode = link->dentry->d_inode; 806800179c9SKees Cook 807800179c9SKees Cook /* Source inode owner (or CAP_FOWNER) can hardlink all they like, 808800179c9SKees Cook * otherwise, it must be a safe source. 809800179c9SKees Cook */ 81081abe27bSEric W. Biederman if (uid_eq(cred->fsuid, inode->i_uid) || safe_hardlink_source(inode) || 811800179c9SKees Cook capable(CAP_FOWNER)) 812800179c9SKees Cook return 0; 813800179c9SKees Cook 814a51d9eaaSKees Cook audit_log_link_denied("linkat", link); 815800179c9SKees Cook return -EPERM; 816800179c9SKees Cook } 817800179c9SKees Cook 818def4af30SAl Viro static __always_inline int 819574197e0SAl Viro follow_link(struct path *link, struct nameidata *nd, void **p) 8201da177e4SLinus Torvalds { 8217b9337aaSNick Piggin struct dentry *dentry = link->dentry; 8226d7b5aaeSAl Viro int error; 8236d7b5aaeSAl Viro char *s; 8241da177e4SLinus Torvalds 825844a3917SAl Viro BUG_ON(nd->flags & LOOKUP_RCU); 826844a3917SAl Viro 8270e794589SAl Viro if (link->mnt == nd->path.mnt) 8280e794589SAl Viro mntget(link->mnt); 8290e794589SAl Viro 8306d7b5aaeSAl Viro error = -ELOOP; 8316d7b5aaeSAl Viro if (unlikely(current->total_link_count >= 40)) 8326d7b5aaeSAl Viro goto out_put_nd_path; 8336d7b5aaeSAl Viro 834574197e0SAl Viro cond_resched(); 835574197e0SAl Viro current->total_link_count++; 836574197e0SAl Viro 83768ac1234SAl Viro touch_atime(link); 8381da177e4SLinus Torvalds nd_set_link(nd, NULL); 839cd4e91d3SAl Viro 84036f3b4f6SAl Viro error = security_inode_follow_link(link->dentry, nd); 8416d7b5aaeSAl Viro if (error) 8426d7b5aaeSAl Viro goto out_put_nd_path; 84336f3b4f6SAl Viro 84486acdca1SAl Viro nd->last_type = LAST_BIND; 845def4af30SAl Viro *p = dentry->d_inode->i_op->follow_link(dentry, nd); 846def4af30SAl Viro error = PTR_ERR(*p); 8476d7b5aaeSAl Viro if (IS_ERR(*p)) 848408ef013SChristoph Hellwig goto out_put_nd_path; 8496d7b5aaeSAl Viro 850cc314eefSLinus Torvalds error = 0; 8516d7b5aaeSAl Viro s = nd_get_link(nd); 8526d7b5aaeSAl Viro if (s) { 8531da177e4SLinus Torvalds error = __vfs_follow_link(nd, s); 8546d7b5aaeSAl Viro if (unlikely(error)) 8556d7b5aaeSAl Viro put_link(nd, link, *p); 856b5fb63c1SChristoph Hellwig } 8576d7b5aaeSAl Viro 8586d7b5aaeSAl Viro return error; 8596d7b5aaeSAl Viro 8606d7b5aaeSAl Viro out_put_nd_path: 86198f6ef64SArnd Bergmann *p = NULL; 8626d7b5aaeSAl Viro path_put(&nd->path); 8636d7b5aaeSAl Viro path_put(link); 8641da177e4SLinus Torvalds return error; 8651da177e4SLinus Torvalds } 8661da177e4SLinus Torvalds 86731e6b01fSNick Piggin static int follow_up_rcu(struct path *path) 86831e6b01fSNick Piggin { 8690714a533SAl Viro struct mount *mnt = real_mount(path->mnt); 8700714a533SAl Viro struct mount *parent; 87131e6b01fSNick Piggin struct dentry *mountpoint; 87231e6b01fSNick Piggin 8730714a533SAl Viro parent = mnt->mnt_parent; 8740714a533SAl Viro if (&parent->mnt == path->mnt) 87531e6b01fSNick Piggin return 0; 876a73324daSAl Viro mountpoint = mnt->mnt_mountpoint; 87731e6b01fSNick Piggin path->dentry = mountpoint; 8780714a533SAl Viro path->mnt = &parent->mnt; 87931e6b01fSNick Piggin return 1; 88031e6b01fSNick Piggin } 88131e6b01fSNick Piggin 882f015f126SDavid Howells /* 883f015f126SDavid Howells * follow_up - Find the mountpoint of path's vfsmount 884f015f126SDavid Howells * 885f015f126SDavid Howells * Given a path, find the mountpoint of its source file system. 886f015f126SDavid Howells * Replace @path with the path of the mountpoint in the parent mount. 887f015f126SDavid Howells * Up is towards /. 888f015f126SDavid Howells * 889f015f126SDavid Howells * Return 1 if we went up a level and 0 if we were already at the 890f015f126SDavid Howells * root. 891f015f126SDavid Howells */ 892bab77ebfSAl Viro int follow_up(struct path *path) 8931da177e4SLinus Torvalds { 8940714a533SAl Viro struct mount *mnt = real_mount(path->mnt); 8950714a533SAl Viro struct mount *parent; 8961da177e4SLinus Torvalds struct dentry *mountpoint; 89799b7db7bSNick Piggin 898962830dfSAndi Kleen br_read_lock(&vfsmount_lock); 8990714a533SAl Viro parent = mnt->mnt_parent; 9003c0a6163SAl Viro if (parent == mnt) { 901962830dfSAndi Kleen br_read_unlock(&vfsmount_lock); 9021da177e4SLinus Torvalds return 0; 9031da177e4SLinus Torvalds } 9040714a533SAl Viro mntget(&parent->mnt); 905a73324daSAl Viro mountpoint = dget(mnt->mnt_mountpoint); 906962830dfSAndi Kleen br_read_unlock(&vfsmount_lock); 907bab77ebfSAl Viro dput(path->dentry); 908bab77ebfSAl Viro path->dentry = mountpoint; 909bab77ebfSAl Viro mntput(path->mnt); 9100714a533SAl Viro path->mnt = &parent->mnt; 9111da177e4SLinus Torvalds return 1; 9121da177e4SLinus Torvalds } 9131da177e4SLinus Torvalds 914b5c84bf6SNick Piggin /* 9159875cf80SDavid Howells * Perform an automount 9169875cf80SDavid Howells * - return -EISDIR to tell follow_managed() to stop and return the path we 9179875cf80SDavid Howells * were called with. 9181da177e4SLinus Torvalds */ 9199875cf80SDavid Howells static int follow_automount(struct path *path, unsigned flags, 9209875cf80SDavid Howells bool *need_mntput) 92131e6b01fSNick Piggin { 9229875cf80SDavid Howells struct vfsmount *mnt; 923ea5b778aSDavid Howells int err; 9249875cf80SDavid Howells 9259875cf80SDavid Howells if (!path->dentry->d_op || !path->dentry->d_op->d_automount) 9269875cf80SDavid Howells return -EREMOTE; 9279875cf80SDavid Howells 9280ec26fd0SMiklos Szeredi /* We don't want to mount if someone's just doing a stat - 9290ec26fd0SMiklos Szeredi * unless they're stat'ing a directory and appended a '/' to 9300ec26fd0SMiklos Szeredi * the name. 9310ec26fd0SMiklos Szeredi * 9320ec26fd0SMiklos Szeredi * We do, however, want to mount if someone wants to open or 9335a30d8a2SDavid Howells * create a file of any type under the mountpoint, wants to 9340ec26fd0SMiklos Szeredi * traverse through the mountpoint or wants to open the 9350ec26fd0SMiklos Szeredi * mounted directory. Also, autofs may mark negative dentries 9360ec26fd0SMiklos Szeredi * as being automount points. These will need the attentions 9370ec26fd0SMiklos Szeredi * of the daemon to instantiate them before they can be used. 9385a30d8a2SDavid Howells */ 9395a30d8a2SDavid Howells if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY | 940d94c177bSLinus Torvalds LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) && 9415a30d8a2SDavid Howells path->dentry->d_inode) 9429875cf80SDavid Howells return -EISDIR; 9430ec26fd0SMiklos Szeredi 9449875cf80SDavid Howells current->total_link_count++; 9459875cf80SDavid Howells if (current->total_link_count >= 40) 9469875cf80SDavid Howells return -ELOOP; 9479875cf80SDavid Howells 9489875cf80SDavid Howells mnt = path->dentry->d_op->d_automount(path); 9499875cf80SDavid Howells if (IS_ERR(mnt)) { 9509875cf80SDavid Howells /* 9519875cf80SDavid Howells * The filesystem is allowed to return -EISDIR here to indicate 9529875cf80SDavid Howells * it doesn't want to automount. For instance, autofs would do 9539875cf80SDavid Howells * this so that its userspace daemon can mount on this dentry. 9549875cf80SDavid Howells * 9559875cf80SDavid Howells * However, we can only permit this if it's a terminal point in 9569875cf80SDavid Howells * the path being looked up; if it wasn't then the remainder of 9579875cf80SDavid Howells * the path is inaccessible and we should say so. 9589875cf80SDavid Howells */ 95949084c3bSAl Viro if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT)) 9609875cf80SDavid Howells return -EREMOTE; 9619875cf80SDavid Howells return PTR_ERR(mnt); 96231e6b01fSNick Piggin } 963ea5b778aSDavid Howells 9649875cf80SDavid Howells if (!mnt) /* mount collision */ 9659875cf80SDavid Howells return 0; 9669875cf80SDavid Howells 9678aef1884SAl Viro if (!*need_mntput) { 9688aef1884SAl Viro /* lock_mount() may release path->mnt on error */ 9698aef1884SAl Viro mntget(path->mnt); 9708aef1884SAl Viro *need_mntput = true; 9718aef1884SAl Viro } 97219a167afSAl Viro err = finish_automount(mnt, path); 973ea5b778aSDavid Howells 974ea5b778aSDavid Howells switch (err) { 975ea5b778aSDavid Howells case -EBUSY: 976ea5b778aSDavid Howells /* Someone else made a mount here whilst we were busy */ 97719a167afSAl Viro return 0; 978ea5b778aSDavid Howells case 0: 9798aef1884SAl Viro path_put(path); 9809875cf80SDavid Howells path->mnt = mnt; 9819875cf80SDavid Howells path->dentry = dget(mnt->mnt_root); 9829875cf80SDavid Howells return 0; 98319a167afSAl Viro default: 98419a167afSAl Viro return err; 9859875cf80SDavid Howells } 98619a167afSAl Viro 987ea5b778aSDavid Howells } 9889875cf80SDavid Howells 9899875cf80SDavid Howells /* 9909875cf80SDavid Howells * Handle a dentry that is managed in some way. 991cc53ce53SDavid Howells * - Flagged for transit management (autofs) 9929875cf80SDavid Howells * - Flagged as mountpoint 9939875cf80SDavid Howells * - Flagged as automount point 9949875cf80SDavid Howells * 9959875cf80SDavid Howells * This may only be called in refwalk mode. 9969875cf80SDavid Howells * 9979875cf80SDavid Howells * Serialization is taken care of in namespace.c 9989875cf80SDavid Howells */ 9999875cf80SDavid Howells static int follow_managed(struct path *path, unsigned flags) 10009875cf80SDavid Howells { 10018aef1884SAl Viro struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */ 10029875cf80SDavid Howells unsigned managed; 10039875cf80SDavid Howells bool need_mntput = false; 10048aef1884SAl Viro int ret = 0; 10059875cf80SDavid Howells 10069875cf80SDavid Howells /* Given that we're not holding a lock here, we retain the value in a 10079875cf80SDavid Howells * local variable for each dentry as we look at it so that we don't see 10089875cf80SDavid Howells * the components of that value change under us */ 10099875cf80SDavid Howells while (managed = ACCESS_ONCE(path->dentry->d_flags), 10109875cf80SDavid Howells managed &= DCACHE_MANAGED_DENTRY, 10119875cf80SDavid Howells unlikely(managed != 0)) { 1012cc53ce53SDavid Howells /* Allow the filesystem to manage the transit without i_mutex 1013cc53ce53SDavid Howells * being held. */ 1014cc53ce53SDavid Howells if (managed & DCACHE_MANAGE_TRANSIT) { 1015cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op); 1016cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op->d_manage); 10171aed3e42SAl Viro ret = path->dentry->d_op->d_manage(path->dentry, false); 1018cc53ce53SDavid Howells if (ret < 0) 10198aef1884SAl Viro break; 1020cc53ce53SDavid Howells } 1021cc53ce53SDavid Howells 10229875cf80SDavid Howells /* Transit to a mounted filesystem. */ 10239875cf80SDavid Howells if (managed & DCACHE_MOUNTED) { 10249875cf80SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 10259875cf80SDavid Howells if (mounted) { 10269875cf80SDavid Howells dput(path->dentry); 10279875cf80SDavid Howells if (need_mntput) 1028463ffb2eSAl Viro mntput(path->mnt); 1029463ffb2eSAl Viro path->mnt = mounted; 1030463ffb2eSAl Viro path->dentry = dget(mounted->mnt_root); 10319875cf80SDavid Howells need_mntput = true; 10329875cf80SDavid Howells continue; 1033463ffb2eSAl Viro } 1034463ffb2eSAl Viro 10359875cf80SDavid Howells /* Something is mounted on this dentry in another 10369875cf80SDavid Howells * namespace and/or whatever was mounted there in this 10379875cf80SDavid Howells * namespace got unmounted before we managed to get the 10389875cf80SDavid Howells * vfsmount_lock */ 10391da177e4SLinus Torvalds } 10409875cf80SDavid Howells 10419875cf80SDavid Howells /* Handle an automount point */ 10429875cf80SDavid Howells if (managed & DCACHE_NEED_AUTOMOUNT) { 10439875cf80SDavid Howells ret = follow_automount(path, flags, &need_mntput); 10449875cf80SDavid Howells if (ret < 0) 10458aef1884SAl Viro break; 10469875cf80SDavid Howells continue; 10479875cf80SDavid Howells } 10489875cf80SDavid Howells 10499875cf80SDavid Howells /* We didn't change the current path point */ 10509875cf80SDavid Howells break; 10519875cf80SDavid Howells } 10528aef1884SAl Viro 10538aef1884SAl Viro if (need_mntput && path->mnt == mnt) 10548aef1884SAl Viro mntput(path->mnt); 10558aef1884SAl Viro if (ret == -EISDIR) 10568aef1884SAl Viro ret = 0; 1057a3fbbde7SAl Viro return ret < 0 ? ret : need_mntput; 10581da177e4SLinus Torvalds } 10591da177e4SLinus Torvalds 1060cc53ce53SDavid Howells int follow_down_one(struct path *path) 10611da177e4SLinus Torvalds { 10621da177e4SLinus Torvalds struct vfsmount *mounted; 10631da177e4SLinus Torvalds 10641c755af4SAl Viro mounted = lookup_mnt(path); 10651da177e4SLinus Torvalds if (mounted) { 10669393bd07SAl Viro dput(path->dentry); 10679393bd07SAl Viro mntput(path->mnt); 10689393bd07SAl Viro path->mnt = mounted; 10699393bd07SAl Viro path->dentry = dget(mounted->mnt_root); 10701da177e4SLinus Torvalds return 1; 10711da177e4SLinus Torvalds } 10721da177e4SLinus Torvalds return 0; 10731da177e4SLinus Torvalds } 10741da177e4SLinus Torvalds 107562a7375eSIan Kent static inline bool managed_dentry_might_block(struct dentry *dentry) 107662a7375eSIan Kent { 107762a7375eSIan Kent return (dentry->d_flags & DCACHE_MANAGE_TRANSIT && 107862a7375eSIan Kent dentry->d_op->d_manage(dentry, true) < 0); 107962a7375eSIan Kent } 108062a7375eSIan Kent 10819875cf80SDavid Howells /* 1082287548e4SAl Viro * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if 1083287548e4SAl Viro * we meet a managed dentry that would need blocking. 10849875cf80SDavid Howells */ 10859875cf80SDavid Howells static bool __follow_mount_rcu(struct nameidata *nd, struct path *path, 1086287548e4SAl Viro struct inode **inode) 10879875cf80SDavid Howells { 108862a7375eSIan Kent for (;;) { 1089c7105365SAl Viro struct mount *mounted; 109062a7375eSIan Kent /* 109162a7375eSIan Kent * Don't forget we might have a non-mountpoint managed dentry 109262a7375eSIan Kent * that wants to block transit. 109362a7375eSIan Kent */ 1094287548e4SAl Viro if (unlikely(managed_dentry_might_block(path->dentry))) 1095ab90911fSDavid Howells return false; 109662a7375eSIan Kent 109762a7375eSIan Kent if (!d_mountpoint(path->dentry)) 109862a7375eSIan Kent break; 109962a7375eSIan Kent 11009875cf80SDavid Howells mounted = __lookup_mnt(path->mnt, path->dentry, 1); 11019875cf80SDavid Howells if (!mounted) 11029875cf80SDavid Howells break; 1103c7105365SAl Viro path->mnt = &mounted->mnt; 1104c7105365SAl Viro path->dentry = mounted->mnt.mnt_root; 1105a3fbbde7SAl Viro nd->flags |= LOOKUP_JUMPED; 11069875cf80SDavid Howells nd->seq = read_seqcount_begin(&path->dentry->d_seq); 110759430262SLinus Torvalds /* 110859430262SLinus Torvalds * Update the inode too. We don't need to re-check the 110959430262SLinus Torvalds * dentry sequence number here after this d_inode read, 111059430262SLinus Torvalds * because a mount-point is always pinned. 111159430262SLinus Torvalds */ 111259430262SLinus Torvalds *inode = path->dentry->d_inode; 11139875cf80SDavid Howells } 11149875cf80SDavid Howells return true; 11159875cf80SDavid Howells } 11169875cf80SDavid Howells 1117dea39376SAl Viro static void follow_mount_rcu(struct nameidata *nd) 1118287548e4SAl Viro { 1119dea39376SAl Viro while (d_mountpoint(nd->path.dentry)) { 1120c7105365SAl Viro struct mount *mounted; 1121dea39376SAl Viro mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1); 1122287548e4SAl Viro if (!mounted) 1123287548e4SAl Viro break; 1124c7105365SAl Viro nd->path.mnt = &mounted->mnt; 1125c7105365SAl Viro nd->path.dentry = mounted->mnt.mnt_root; 1126dea39376SAl Viro nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); 1127287548e4SAl Viro } 1128287548e4SAl Viro } 1129287548e4SAl Viro 113031e6b01fSNick Piggin static int follow_dotdot_rcu(struct nameidata *nd) 113131e6b01fSNick Piggin { 113231e6b01fSNick Piggin set_root_rcu(nd); 113331e6b01fSNick Piggin 113431e6b01fSNick Piggin while (1) { 113531e6b01fSNick Piggin if (nd->path.dentry == nd->root.dentry && 113631e6b01fSNick Piggin nd->path.mnt == nd->root.mnt) { 113731e6b01fSNick Piggin break; 113831e6b01fSNick Piggin } 113931e6b01fSNick Piggin if (nd->path.dentry != nd->path.mnt->mnt_root) { 114031e6b01fSNick Piggin struct dentry *old = nd->path.dentry; 114131e6b01fSNick Piggin struct dentry *parent = old->d_parent; 114231e6b01fSNick Piggin unsigned seq; 114331e6b01fSNick Piggin 114431e6b01fSNick Piggin seq = read_seqcount_begin(&parent->d_seq); 114531e6b01fSNick Piggin if (read_seqcount_retry(&old->d_seq, nd->seq)) 1146ef7562d5SAl Viro goto failed; 114731e6b01fSNick Piggin nd->path.dentry = parent; 114831e6b01fSNick Piggin nd->seq = seq; 114931e6b01fSNick Piggin break; 115031e6b01fSNick Piggin } 115131e6b01fSNick Piggin if (!follow_up_rcu(&nd->path)) 115231e6b01fSNick Piggin break; 115331e6b01fSNick Piggin nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); 115431e6b01fSNick Piggin } 1155dea39376SAl Viro follow_mount_rcu(nd); 1156dea39376SAl Viro nd->inode = nd->path.dentry->d_inode; 115731e6b01fSNick Piggin return 0; 1158ef7562d5SAl Viro 1159ef7562d5SAl Viro failed: 1160ef7562d5SAl Viro nd->flags &= ~LOOKUP_RCU; 11615b6ca027SAl Viro if (!(nd->flags & LOOKUP_ROOT)) 1162ef7562d5SAl Viro nd->root.mnt = NULL; 116332a7991bSAl Viro unlock_rcu_walk(); 1164ef7562d5SAl Viro return -ECHILD; 116531e6b01fSNick Piggin } 116631e6b01fSNick Piggin 11679875cf80SDavid Howells /* 1168cc53ce53SDavid Howells * Follow down to the covering mount currently visible to userspace. At each 1169cc53ce53SDavid Howells * point, the filesystem owning that dentry may be queried as to whether the 1170cc53ce53SDavid Howells * caller is permitted to proceed or not. 1171cc53ce53SDavid Howells */ 11727cc90cc3SAl Viro int follow_down(struct path *path) 1173cc53ce53SDavid Howells { 1174cc53ce53SDavid Howells unsigned managed; 1175cc53ce53SDavid Howells int ret; 1176cc53ce53SDavid Howells 1177cc53ce53SDavid Howells while (managed = ACCESS_ONCE(path->dentry->d_flags), 1178cc53ce53SDavid Howells unlikely(managed & DCACHE_MANAGED_DENTRY)) { 1179cc53ce53SDavid Howells /* Allow the filesystem to manage the transit without i_mutex 1180cc53ce53SDavid Howells * being held. 1181cc53ce53SDavid Howells * 1182cc53ce53SDavid Howells * We indicate to the filesystem if someone is trying to mount 1183cc53ce53SDavid Howells * something here. This gives autofs the chance to deny anyone 1184cc53ce53SDavid Howells * other than its daemon the right to mount on its 1185cc53ce53SDavid Howells * superstructure. 1186cc53ce53SDavid Howells * 1187cc53ce53SDavid Howells * The filesystem may sleep at this point. 1188cc53ce53SDavid Howells */ 1189cc53ce53SDavid Howells if (managed & DCACHE_MANAGE_TRANSIT) { 1190cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op); 1191cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op->d_manage); 1192ab90911fSDavid Howells ret = path->dentry->d_op->d_manage( 11931aed3e42SAl Viro path->dentry, false); 1194cc53ce53SDavid Howells if (ret < 0) 1195cc53ce53SDavid Howells return ret == -EISDIR ? 0 : ret; 1196cc53ce53SDavid Howells } 1197cc53ce53SDavid Howells 1198cc53ce53SDavid Howells /* Transit to a mounted filesystem. */ 1199cc53ce53SDavid Howells if (managed & DCACHE_MOUNTED) { 1200cc53ce53SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 1201cc53ce53SDavid Howells if (!mounted) 1202cc53ce53SDavid Howells break; 1203cc53ce53SDavid Howells dput(path->dentry); 1204cc53ce53SDavid Howells mntput(path->mnt); 1205cc53ce53SDavid Howells path->mnt = mounted; 1206cc53ce53SDavid Howells path->dentry = dget(mounted->mnt_root); 1207cc53ce53SDavid Howells continue; 1208cc53ce53SDavid Howells } 1209cc53ce53SDavid Howells 1210cc53ce53SDavid Howells /* Don't handle automount points here */ 1211cc53ce53SDavid Howells break; 1212cc53ce53SDavid Howells } 1213cc53ce53SDavid Howells return 0; 1214cc53ce53SDavid Howells } 1215cc53ce53SDavid Howells 1216cc53ce53SDavid Howells /* 12179875cf80SDavid Howells * Skip to top of mountpoint pile in refwalk mode for follow_dotdot() 12189875cf80SDavid Howells */ 12199875cf80SDavid Howells static void follow_mount(struct path *path) 12209875cf80SDavid Howells { 12219875cf80SDavid Howells while (d_mountpoint(path->dentry)) { 12229875cf80SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 12239875cf80SDavid Howells if (!mounted) 12249875cf80SDavid Howells break; 12259875cf80SDavid Howells dput(path->dentry); 12269875cf80SDavid Howells mntput(path->mnt); 12279875cf80SDavid Howells path->mnt = mounted; 12289875cf80SDavid Howells path->dentry = dget(mounted->mnt_root); 12299875cf80SDavid Howells } 12309875cf80SDavid Howells } 12319875cf80SDavid Howells 123231e6b01fSNick Piggin static void follow_dotdot(struct nameidata *nd) 12331da177e4SLinus Torvalds { 12342a737871SAl Viro set_root(nd); 1235e518ddb7SAndreas Mohr 12361da177e4SLinus Torvalds while(1) { 12374ac91378SJan Blunck struct dentry *old = nd->path.dentry; 12381da177e4SLinus Torvalds 12392a737871SAl Viro if (nd->path.dentry == nd->root.dentry && 12402a737871SAl Viro nd->path.mnt == nd->root.mnt) { 12411da177e4SLinus Torvalds break; 12421da177e4SLinus Torvalds } 12434ac91378SJan Blunck if (nd->path.dentry != nd->path.mnt->mnt_root) { 12443088dd70SAl Viro /* rare case of legitimate dget_parent()... */ 12453088dd70SAl Viro nd->path.dentry = dget_parent(nd->path.dentry); 12461da177e4SLinus Torvalds dput(old); 12471da177e4SLinus Torvalds break; 12481da177e4SLinus Torvalds } 12493088dd70SAl Viro if (!follow_up(&nd->path)) 12501da177e4SLinus Torvalds break; 12511da177e4SLinus Torvalds } 125279ed0226SAl Viro follow_mount(&nd->path); 125331e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 12541da177e4SLinus Torvalds } 12551da177e4SLinus Torvalds 12561da177e4SLinus Torvalds /* 1257bad61189SMiklos Szeredi * This looks up the name in dcache, possibly revalidates the old dentry and 1258bad61189SMiklos Szeredi * allocates a new one if not found or not valid. In the need_lookup argument 1259bad61189SMiklos Szeredi * returns whether i_op->lookup is necessary. 1260bad61189SMiklos Szeredi * 1261bad61189SMiklos Szeredi * dir->d_inode->i_mutex must be held 1262baa03890SNick Piggin */ 1263bad61189SMiklos Szeredi static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir, 1264201f956eSAl Viro unsigned int flags, bool *need_lookup) 1265baa03890SNick Piggin { 1266baa03890SNick Piggin struct dentry *dentry; 1267bad61189SMiklos Szeredi int error; 1268baa03890SNick Piggin 1269bad61189SMiklos Szeredi *need_lookup = false; 1270bad61189SMiklos Szeredi dentry = d_lookup(dir, name); 1271bad61189SMiklos Szeredi if (dentry) { 127239e3c955SJeff Layton if (dentry->d_flags & DCACHE_OP_REVALIDATE) { 1273201f956eSAl Viro error = d_revalidate(dentry, flags); 1274bad61189SMiklos Szeredi if (unlikely(error <= 0)) { 1275bad61189SMiklos Szeredi if (error < 0) { 1276bad61189SMiklos Szeredi dput(dentry); 1277bad61189SMiklos Szeredi return ERR_PTR(error); 1278bad61189SMiklos Szeredi } else if (!d_invalidate(dentry)) { 1279bad61189SMiklos Szeredi dput(dentry); 1280bad61189SMiklos Szeredi dentry = NULL; 1281bad61189SMiklos Szeredi } 1282bad61189SMiklos Szeredi } 1283bad61189SMiklos Szeredi } 1284bad61189SMiklos Szeredi } 1285baa03890SNick Piggin 1286bad61189SMiklos Szeredi if (!dentry) { 1287bad61189SMiklos Szeredi dentry = d_alloc(dir, name); 1288baa03890SNick Piggin if (unlikely(!dentry)) 1289baa03890SNick Piggin return ERR_PTR(-ENOMEM); 1290baa03890SNick Piggin 1291bad61189SMiklos Szeredi *need_lookup = true; 1292baa03890SNick Piggin } 1293baa03890SNick Piggin return dentry; 1294baa03890SNick Piggin } 1295baa03890SNick Piggin 1296baa03890SNick Piggin /* 1297bad61189SMiklos Szeredi * Call i_op->lookup on the dentry. The dentry must be negative but may be 1298bad61189SMiklos Szeredi * hashed if it was pouplated with DCACHE_NEED_LOOKUP. 1299bad61189SMiklos Szeredi * 1300bad61189SMiklos Szeredi * dir->d_inode->i_mutex must be held 130144396f4bSJosef Bacik */ 1302bad61189SMiklos Szeredi static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry, 130372bd866aSAl Viro unsigned int flags) 130444396f4bSJosef Bacik { 130544396f4bSJosef Bacik struct dentry *old; 130644396f4bSJosef Bacik 130744396f4bSJosef Bacik /* Don't create child dentry for a dead directory. */ 1308bad61189SMiklos Szeredi if (unlikely(IS_DEADDIR(dir))) { 1309e188dc02SMiklos Szeredi dput(dentry); 131044396f4bSJosef Bacik return ERR_PTR(-ENOENT); 1311e188dc02SMiklos Szeredi } 131244396f4bSJosef Bacik 131372bd866aSAl Viro old = dir->i_op->lookup(dir, dentry, flags); 131444396f4bSJosef Bacik if (unlikely(old)) { 131544396f4bSJosef Bacik dput(dentry); 131644396f4bSJosef Bacik dentry = old; 131744396f4bSJosef Bacik } 131844396f4bSJosef Bacik return dentry; 131944396f4bSJosef Bacik } 132044396f4bSJosef Bacik 1321a3255546SAl Viro static struct dentry *__lookup_hash(struct qstr *name, 132272bd866aSAl Viro struct dentry *base, unsigned int flags) 1323a3255546SAl Viro { 1324bad61189SMiklos Szeredi bool need_lookup; 1325a3255546SAl Viro struct dentry *dentry; 1326a3255546SAl Viro 132772bd866aSAl Viro dentry = lookup_dcache(name, base, flags, &need_lookup); 1328bad61189SMiklos Szeredi if (!need_lookup) 1329a3255546SAl Viro return dentry; 1330bad61189SMiklos Szeredi 133172bd866aSAl Viro return lookup_real(base->d_inode, dentry, flags); 1332a3255546SAl Viro } 1333a3255546SAl Viro 133444396f4bSJosef Bacik /* 13351da177e4SLinus Torvalds * It's more convoluted than I'd like it to be, but... it's still fairly 13361da177e4SLinus Torvalds * small and for now I'd prefer to have fast path as straight as possible. 13371da177e4SLinus Torvalds * It _is_ time-critical. 13381da177e4SLinus Torvalds */ 1339e97cdc87SAl Viro static int lookup_fast(struct nameidata *nd, 134031e6b01fSNick Piggin struct path *path, struct inode **inode) 13411da177e4SLinus Torvalds { 13424ac91378SJan Blunck struct vfsmount *mnt = nd->path.mnt; 134331e6b01fSNick Piggin struct dentry *dentry, *parent = nd->path.dentry; 13445a18fff2SAl Viro int need_reval = 1; 13455a18fff2SAl Viro int status = 1; 13469875cf80SDavid Howells int err; 13479875cf80SDavid Howells 13483cac260aSAl Viro /* 1349b04f784eSNick Piggin * Rename seqlock is not required here because in the off chance 1350b04f784eSNick Piggin * of a false negative due to a concurrent rename, we're going to 1351b04f784eSNick Piggin * do the non-racy lookup, below. 1352b04f784eSNick Piggin */ 135331e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) { 135431e6b01fSNick Piggin unsigned seq; 1355da53be12SLinus Torvalds dentry = __d_lookup_rcu(parent, &nd->last, &seq); 13565a18fff2SAl Viro if (!dentry) 13575a18fff2SAl Viro goto unlazy; 13585a18fff2SAl Viro 135912f8ad4bSLinus Torvalds /* 136012f8ad4bSLinus Torvalds * This sequence count validates that the inode matches 136112f8ad4bSLinus Torvalds * the dentry name information from lookup. 136212f8ad4bSLinus Torvalds */ 136312f8ad4bSLinus Torvalds *inode = dentry->d_inode; 136412f8ad4bSLinus Torvalds if (read_seqcount_retry(&dentry->d_seq, seq)) 136512f8ad4bSLinus Torvalds return -ECHILD; 136612f8ad4bSLinus Torvalds 136712f8ad4bSLinus Torvalds /* 136812f8ad4bSLinus Torvalds * This sequence count validates that the parent had no 136912f8ad4bSLinus Torvalds * changes while we did the lookup of the dentry above. 137012f8ad4bSLinus Torvalds * 137112f8ad4bSLinus Torvalds * The memory barrier in read_seqcount_begin of child is 137212f8ad4bSLinus Torvalds * enough, we can use __read_seqcount_retry here. 137312f8ad4bSLinus Torvalds */ 137431e6b01fSNick Piggin if (__read_seqcount_retry(&parent->d_seq, nd->seq)) 137531e6b01fSNick Piggin return -ECHILD; 137631e6b01fSNick Piggin nd->seq = seq; 13775a18fff2SAl Viro 137824643087SAl Viro if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) { 13794ce16ef3SAl Viro status = d_revalidate(dentry, nd->flags); 13805a18fff2SAl Viro if (unlikely(status <= 0)) { 13815a18fff2SAl Viro if (status != -ECHILD) 13825a18fff2SAl Viro need_reval = 0; 13835a18fff2SAl Viro goto unlazy; 13845a18fff2SAl Viro } 138524643087SAl Viro } 138631e6b01fSNick Piggin path->mnt = mnt; 138731e6b01fSNick Piggin path->dentry = dentry; 1388d6e9bd25SAl Viro if (unlikely(!__follow_mount_rcu(nd, path, inode))) 1389d6e9bd25SAl Viro goto unlazy; 1390d6e9bd25SAl Viro if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT)) 1391d6e9bd25SAl Viro goto unlazy; 13929875cf80SDavid Howells return 0; 13935a18fff2SAl Viro unlazy: 139419660af7SAl Viro if (unlazy_walk(nd, dentry)) 13955a18fff2SAl Viro return -ECHILD; 13965a18fff2SAl Viro } else { 1397e97cdc87SAl Viro dentry = __d_lookup(parent, &nd->last); 139824643087SAl Viro } 13995a18fff2SAl Viro 140081e6f520SAl Viro if (unlikely(!dentry)) 140181e6f520SAl Viro goto need_lookup; 14025a18fff2SAl Viro 14035a18fff2SAl Viro if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval) 14044ce16ef3SAl Viro status = d_revalidate(dentry, nd->flags); 14055a18fff2SAl Viro if (unlikely(status <= 0)) { 14065a18fff2SAl Viro if (status < 0) { 14075a18fff2SAl Viro dput(dentry); 14085a18fff2SAl Viro return status; 14095a18fff2SAl Viro } 14105a18fff2SAl Viro if (!d_invalidate(dentry)) { 14115a18fff2SAl Viro dput(dentry); 141281e6f520SAl Viro goto need_lookup; 14135a18fff2SAl Viro } 14145a18fff2SAl Viro } 1415697f514dSMiklos Szeredi 14161da177e4SLinus Torvalds path->mnt = mnt; 14171da177e4SLinus Torvalds path->dentry = dentry; 14189875cf80SDavid Howells err = follow_managed(path, nd->flags); 141989312214SIan Kent if (unlikely(err < 0)) { 142089312214SIan Kent path_put_conditional(path, nd); 14219875cf80SDavid Howells return err; 142289312214SIan Kent } 1423a3fbbde7SAl Viro if (err) 1424a3fbbde7SAl Viro nd->flags |= LOOKUP_JUMPED; 142531e6b01fSNick Piggin *inode = path->dentry->d_inode; 14261da177e4SLinus Torvalds return 0; 142781e6f520SAl Viro 142881e6f520SAl Viro need_lookup: 1429697f514dSMiklos Szeredi return 1; 1430697f514dSMiklos Szeredi } 1431697f514dSMiklos Szeredi 1432697f514dSMiklos Szeredi /* Fast lookup failed, do it the slow way */ 1433cc2a5271SAl Viro static int lookup_slow(struct nameidata *nd, struct path *path) 1434697f514dSMiklos Szeredi { 1435697f514dSMiklos Szeredi struct dentry *dentry, *parent; 1436697f514dSMiklos Szeredi int err; 1437697f514dSMiklos Szeredi 1438697f514dSMiklos Szeredi parent = nd->path.dentry; 143981e6f520SAl Viro BUG_ON(nd->inode != parent->d_inode); 144081e6f520SAl Viro 144181e6f520SAl Viro mutex_lock(&parent->d_inode->i_mutex); 1442cc2a5271SAl Viro dentry = __lookup_hash(&nd->last, parent, nd->flags); 144381e6f520SAl Viro mutex_unlock(&parent->d_inode->i_mutex); 144481e6f520SAl Viro if (IS_ERR(dentry)) 144581e6f520SAl Viro return PTR_ERR(dentry); 1446697f514dSMiklos Szeredi path->mnt = nd->path.mnt; 1447697f514dSMiklos Szeredi path->dentry = dentry; 1448697f514dSMiklos Szeredi err = follow_managed(path, nd->flags); 1449697f514dSMiklos Szeredi if (unlikely(err < 0)) { 1450697f514dSMiklos Szeredi path_put_conditional(path, nd); 1451697f514dSMiklos Szeredi return err; 1452697f514dSMiklos Szeredi } 1453697f514dSMiklos Szeredi if (err) 1454697f514dSMiklos Szeredi nd->flags |= LOOKUP_JUMPED; 1455697f514dSMiklos Szeredi return 0; 14561da177e4SLinus Torvalds } 14571da177e4SLinus Torvalds 145852094c8aSAl Viro static inline int may_lookup(struct nameidata *nd) 145952094c8aSAl Viro { 146052094c8aSAl Viro if (nd->flags & LOOKUP_RCU) { 14614ad5abb3SAl Viro int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK); 146252094c8aSAl Viro if (err != -ECHILD) 146352094c8aSAl Viro return err; 146419660af7SAl Viro if (unlazy_walk(nd, NULL)) 146552094c8aSAl Viro return -ECHILD; 146652094c8aSAl Viro } 14674ad5abb3SAl Viro return inode_permission(nd->inode, MAY_EXEC); 146852094c8aSAl Viro } 146952094c8aSAl Viro 14709856fa1bSAl Viro static inline int handle_dots(struct nameidata *nd, int type) 14719856fa1bSAl Viro { 14729856fa1bSAl Viro if (type == LAST_DOTDOT) { 14739856fa1bSAl Viro if (nd->flags & LOOKUP_RCU) { 14749856fa1bSAl Viro if (follow_dotdot_rcu(nd)) 14759856fa1bSAl Viro return -ECHILD; 14769856fa1bSAl Viro } else 14779856fa1bSAl Viro follow_dotdot(nd); 14789856fa1bSAl Viro } 14799856fa1bSAl Viro return 0; 14809856fa1bSAl Viro } 14819856fa1bSAl Viro 1482951361f9SAl Viro static void terminate_walk(struct nameidata *nd) 1483951361f9SAl Viro { 1484951361f9SAl Viro if (!(nd->flags & LOOKUP_RCU)) { 1485951361f9SAl Viro path_put(&nd->path); 1486951361f9SAl Viro } else { 1487951361f9SAl Viro nd->flags &= ~LOOKUP_RCU; 14885b6ca027SAl Viro if (!(nd->flags & LOOKUP_ROOT)) 1489951361f9SAl Viro nd->root.mnt = NULL; 149032a7991bSAl Viro unlock_rcu_walk(); 1491951361f9SAl Viro } 1492951361f9SAl Viro } 1493951361f9SAl Viro 14943ddcd056SLinus Torvalds /* 14953ddcd056SLinus Torvalds * Do we need to follow links? We _really_ want to be able 14963ddcd056SLinus Torvalds * to do this check without having to look at inode->i_op, 14973ddcd056SLinus Torvalds * so we keep a cache of "no, this doesn't need follow_link" 14983ddcd056SLinus Torvalds * for the common case. 14993ddcd056SLinus Torvalds */ 15007813b94aSLinus Torvalds static inline int should_follow_link(struct inode *inode, int follow) 15013ddcd056SLinus Torvalds { 15023ddcd056SLinus Torvalds if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) { 15033ddcd056SLinus Torvalds if (likely(inode->i_op->follow_link)) 15043ddcd056SLinus Torvalds return follow; 15053ddcd056SLinus Torvalds 15063ddcd056SLinus Torvalds /* This gets set once for the inode lifetime */ 15073ddcd056SLinus Torvalds spin_lock(&inode->i_lock); 15083ddcd056SLinus Torvalds inode->i_opflags |= IOP_NOFOLLOW; 15093ddcd056SLinus Torvalds spin_unlock(&inode->i_lock); 15103ddcd056SLinus Torvalds } 15113ddcd056SLinus Torvalds return 0; 15123ddcd056SLinus Torvalds } 15133ddcd056SLinus Torvalds 1514ce57dfc1SAl Viro static inline int walk_component(struct nameidata *nd, struct path *path, 151521b9b073SAl Viro int follow) 1516ce57dfc1SAl Viro { 1517ce57dfc1SAl Viro struct inode *inode; 1518ce57dfc1SAl Viro int err; 1519ce57dfc1SAl Viro /* 1520ce57dfc1SAl Viro * "." and ".." are special - ".." especially so because it has 1521ce57dfc1SAl Viro * to be able to know about the current root directory and 1522ce57dfc1SAl Viro * parent relationships. 1523ce57dfc1SAl Viro */ 152421b9b073SAl Viro if (unlikely(nd->last_type != LAST_NORM)) 152521b9b073SAl Viro return handle_dots(nd, nd->last_type); 1526e97cdc87SAl Viro err = lookup_fast(nd, path, &inode); 1527ce57dfc1SAl Viro if (unlikely(err)) { 1528697f514dSMiklos Szeredi if (err < 0) 1529697f514dSMiklos Szeredi goto out_err; 1530697f514dSMiklos Szeredi 1531cc2a5271SAl Viro err = lookup_slow(nd, path); 1532697f514dSMiklos Szeredi if (err < 0) 1533697f514dSMiklos Szeredi goto out_err; 1534697f514dSMiklos Szeredi 1535697f514dSMiklos Szeredi inode = path->dentry->d_inode; 1536ce57dfc1SAl Viro } 1537697f514dSMiklos Szeredi err = -ENOENT; 1538697f514dSMiklos Szeredi if (!inode) 1539697f514dSMiklos Szeredi goto out_path_put; 1540697f514dSMiklos Szeredi 15417813b94aSLinus Torvalds if (should_follow_link(inode, follow)) { 154219660af7SAl Viro if (nd->flags & LOOKUP_RCU) { 154319660af7SAl Viro if (unlikely(unlazy_walk(nd, path->dentry))) { 1544697f514dSMiklos Szeredi err = -ECHILD; 1545697f514dSMiklos Szeredi goto out_err; 154619660af7SAl Viro } 154719660af7SAl Viro } 1548ce57dfc1SAl Viro BUG_ON(inode != path->dentry->d_inode); 1549ce57dfc1SAl Viro return 1; 1550ce57dfc1SAl Viro } 1551ce57dfc1SAl Viro path_to_nameidata(path, nd); 1552ce57dfc1SAl Viro nd->inode = inode; 1553ce57dfc1SAl Viro return 0; 1554697f514dSMiklos Szeredi 1555697f514dSMiklos Szeredi out_path_put: 1556697f514dSMiklos Szeredi path_to_nameidata(path, nd); 1557697f514dSMiklos Szeredi out_err: 1558697f514dSMiklos Szeredi terminate_walk(nd); 1559697f514dSMiklos Szeredi return err; 1560ce57dfc1SAl Viro } 1561ce57dfc1SAl Viro 15621da177e4SLinus Torvalds /* 1563b356379aSAl Viro * This limits recursive symlink follows to 8, while 1564b356379aSAl Viro * limiting consecutive symlinks to 40. 1565b356379aSAl Viro * 1566b356379aSAl Viro * Without that kind of total limit, nasty chains of consecutive 1567b356379aSAl Viro * symlinks can cause almost arbitrarily long lookups. 1568b356379aSAl Viro */ 1569b356379aSAl Viro static inline int nested_symlink(struct path *path, struct nameidata *nd) 1570b356379aSAl Viro { 1571b356379aSAl Viro int res; 1572b356379aSAl Viro 1573b356379aSAl Viro if (unlikely(current->link_count >= MAX_NESTED_LINKS)) { 1574b356379aSAl Viro path_put_conditional(path, nd); 1575b356379aSAl Viro path_put(&nd->path); 1576b356379aSAl Viro return -ELOOP; 1577b356379aSAl Viro } 15781a4022f8SErez Zadok BUG_ON(nd->depth >= MAX_NESTED_LINKS); 1579b356379aSAl Viro 1580b356379aSAl Viro nd->depth++; 1581b356379aSAl Viro current->link_count++; 1582b356379aSAl Viro 1583b356379aSAl Viro do { 1584b356379aSAl Viro struct path link = *path; 1585b356379aSAl Viro void *cookie; 1586574197e0SAl Viro 1587574197e0SAl Viro res = follow_link(&link, nd, &cookie); 15886d7b5aaeSAl Viro if (res) 15896d7b5aaeSAl Viro break; 159021b9b073SAl Viro res = walk_component(nd, path, LOOKUP_FOLLOW); 1591574197e0SAl Viro put_link(nd, &link, cookie); 1592b356379aSAl Viro } while (res > 0); 1593b356379aSAl Viro 1594b356379aSAl Viro current->link_count--; 1595b356379aSAl Viro nd->depth--; 1596b356379aSAl Viro return res; 1597b356379aSAl Viro } 1598b356379aSAl Viro 1599b356379aSAl Viro /* 16003ddcd056SLinus Torvalds * We really don't want to look at inode->i_op->lookup 16013ddcd056SLinus Torvalds * when we don't have to. So we keep a cache bit in 16023ddcd056SLinus Torvalds * the inode ->i_opflags field that says "yes, we can 16033ddcd056SLinus Torvalds * do lookup on this inode". 16043ddcd056SLinus Torvalds */ 16053ddcd056SLinus Torvalds static inline int can_lookup(struct inode *inode) 16063ddcd056SLinus Torvalds { 16073ddcd056SLinus Torvalds if (likely(inode->i_opflags & IOP_LOOKUP)) 16083ddcd056SLinus Torvalds return 1; 16093ddcd056SLinus Torvalds if (likely(!inode->i_op->lookup)) 16103ddcd056SLinus Torvalds return 0; 16113ddcd056SLinus Torvalds 16123ddcd056SLinus Torvalds /* We do this once for the lifetime of the inode */ 16133ddcd056SLinus Torvalds spin_lock(&inode->i_lock); 16143ddcd056SLinus Torvalds inode->i_opflags |= IOP_LOOKUP; 16153ddcd056SLinus Torvalds spin_unlock(&inode->i_lock); 16163ddcd056SLinus Torvalds return 1; 16173ddcd056SLinus Torvalds } 16183ddcd056SLinus Torvalds 1619bfcfaa77SLinus Torvalds /* 1620bfcfaa77SLinus Torvalds * We can do the critical dentry name comparison and hashing 1621bfcfaa77SLinus Torvalds * operations one word at a time, but we are limited to: 1622bfcfaa77SLinus Torvalds * 1623bfcfaa77SLinus Torvalds * - Architectures with fast unaligned word accesses. We could 1624bfcfaa77SLinus Torvalds * do a "get_unaligned()" if this helps and is sufficiently 1625bfcfaa77SLinus Torvalds * fast. 1626bfcfaa77SLinus Torvalds * 1627bfcfaa77SLinus Torvalds * - Little-endian machines (so that we can generate the mask 1628bfcfaa77SLinus Torvalds * of low bytes efficiently). Again, we *could* do a byte 1629bfcfaa77SLinus Torvalds * swapping load on big-endian architectures if that is not 1630bfcfaa77SLinus Torvalds * expensive enough to make the optimization worthless. 1631bfcfaa77SLinus Torvalds * 1632bfcfaa77SLinus Torvalds * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we 1633bfcfaa77SLinus Torvalds * do not trap on the (extremely unlikely) case of a page 1634bfcfaa77SLinus Torvalds * crossing operation. 1635bfcfaa77SLinus Torvalds * 1636bfcfaa77SLinus Torvalds * - Furthermore, we need an efficient 64-bit compile for the 1637bfcfaa77SLinus Torvalds * 64-bit case in order to generate the "number of bytes in 1638bfcfaa77SLinus Torvalds * the final mask". Again, that could be replaced with a 1639bfcfaa77SLinus Torvalds * efficient population count instruction or similar. 1640bfcfaa77SLinus Torvalds */ 1641bfcfaa77SLinus Torvalds #ifdef CONFIG_DCACHE_WORD_ACCESS 1642bfcfaa77SLinus Torvalds 1643f68e556eSLinus Torvalds #include <asm/word-at-a-time.h> 1644bfcfaa77SLinus Torvalds 1645f68e556eSLinus Torvalds #ifdef CONFIG_64BIT 1646bfcfaa77SLinus Torvalds 1647bfcfaa77SLinus Torvalds static inline unsigned int fold_hash(unsigned long hash) 1648bfcfaa77SLinus Torvalds { 1649bfcfaa77SLinus Torvalds hash += hash >> (8*sizeof(int)); 1650bfcfaa77SLinus Torvalds return hash; 1651bfcfaa77SLinus Torvalds } 1652bfcfaa77SLinus Torvalds 1653bfcfaa77SLinus Torvalds #else /* 32-bit case */ 1654bfcfaa77SLinus Torvalds 1655bfcfaa77SLinus Torvalds #define fold_hash(x) (x) 1656bfcfaa77SLinus Torvalds 1657bfcfaa77SLinus Torvalds #endif 1658bfcfaa77SLinus Torvalds 1659bfcfaa77SLinus Torvalds unsigned int full_name_hash(const unsigned char *name, unsigned int len) 1660bfcfaa77SLinus Torvalds { 1661bfcfaa77SLinus Torvalds unsigned long a, mask; 1662bfcfaa77SLinus Torvalds unsigned long hash = 0; 1663bfcfaa77SLinus Torvalds 1664bfcfaa77SLinus Torvalds for (;;) { 1665e419b4ccSLinus Torvalds a = load_unaligned_zeropad(name); 1666bfcfaa77SLinus Torvalds if (len < sizeof(unsigned long)) 1667bfcfaa77SLinus Torvalds break; 1668bfcfaa77SLinus Torvalds hash += a; 1669f132c5beSAl Viro hash *= 9; 1670bfcfaa77SLinus Torvalds name += sizeof(unsigned long); 1671bfcfaa77SLinus Torvalds len -= sizeof(unsigned long); 1672bfcfaa77SLinus Torvalds if (!len) 1673bfcfaa77SLinus Torvalds goto done; 1674bfcfaa77SLinus Torvalds } 1675bfcfaa77SLinus Torvalds mask = ~(~0ul << len*8); 1676bfcfaa77SLinus Torvalds hash += mask & a; 1677bfcfaa77SLinus Torvalds done: 1678bfcfaa77SLinus Torvalds return fold_hash(hash); 1679bfcfaa77SLinus Torvalds } 1680bfcfaa77SLinus Torvalds EXPORT_SYMBOL(full_name_hash); 1681bfcfaa77SLinus Torvalds 1682bfcfaa77SLinus Torvalds /* 1683bfcfaa77SLinus Torvalds * Calculate the length and hash of the path component, and 1684bfcfaa77SLinus Torvalds * return the length of the component; 1685bfcfaa77SLinus Torvalds */ 1686bfcfaa77SLinus Torvalds static inline unsigned long hash_name(const char *name, unsigned int *hashp) 1687bfcfaa77SLinus Torvalds { 168836126f8fSLinus Torvalds unsigned long a, b, adata, bdata, mask, hash, len; 168936126f8fSLinus Torvalds const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS; 1690bfcfaa77SLinus Torvalds 1691bfcfaa77SLinus Torvalds hash = a = 0; 1692bfcfaa77SLinus Torvalds len = -sizeof(unsigned long); 1693bfcfaa77SLinus Torvalds do { 1694bfcfaa77SLinus Torvalds hash = (hash + a) * 9; 1695bfcfaa77SLinus Torvalds len += sizeof(unsigned long); 1696e419b4ccSLinus Torvalds a = load_unaligned_zeropad(name+len); 169736126f8fSLinus Torvalds b = a ^ REPEAT_BYTE('/'); 169836126f8fSLinus Torvalds } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants))); 1699bfcfaa77SLinus Torvalds 170036126f8fSLinus Torvalds adata = prep_zero_mask(a, adata, &constants); 170136126f8fSLinus Torvalds bdata = prep_zero_mask(b, bdata, &constants); 170236126f8fSLinus Torvalds 170336126f8fSLinus Torvalds mask = create_zero_mask(adata | bdata); 170436126f8fSLinus Torvalds 170536126f8fSLinus Torvalds hash += a & zero_bytemask(mask); 1706bfcfaa77SLinus Torvalds *hashp = fold_hash(hash); 1707bfcfaa77SLinus Torvalds 170836126f8fSLinus Torvalds return len + find_zero(mask); 1709bfcfaa77SLinus Torvalds } 1710bfcfaa77SLinus Torvalds 1711bfcfaa77SLinus Torvalds #else 1712bfcfaa77SLinus Torvalds 17130145acc2SLinus Torvalds unsigned int full_name_hash(const unsigned char *name, unsigned int len) 17140145acc2SLinus Torvalds { 17150145acc2SLinus Torvalds unsigned long hash = init_name_hash(); 17160145acc2SLinus Torvalds while (len--) 17170145acc2SLinus Torvalds hash = partial_name_hash(*name++, hash); 17180145acc2SLinus Torvalds return end_name_hash(hash); 17190145acc2SLinus Torvalds } 1720ae942ae7SLinus Torvalds EXPORT_SYMBOL(full_name_hash); 17210145acc2SLinus Torvalds 17223ddcd056SLinus Torvalds /* 1723200e9ef7SLinus Torvalds * We know there's a real path component here of at least 1724200e9ef7SLinus Torvalds * one character. 1725200e9ef7SLinus Torvalds */ 1726200e9ef7SLinus Torvalds static inline unsigned long hash_name(const char *name, unsigned int *hashp) 1727200e9ef7SLinus Torvalds { 1728200e9ef7SLinus Torvalds unsigned long hash = init_name_hash(); 1729200e9ef7SLinus Torvalds unsigned long len = 0, c; 1730200e9ef7SLinus Torvalds 1731200e9ef7SLinus Torvalds c = (unsigned char)*name; 1732200e9ef7SLinus Torvalds do { 1733200e9ef7SLinus Torvalds len++; 1734200e9ef7SLinus Torvalds hash = partial_name_hash(c, hash); 1735200e9ef7SLinus Torvalds c = (unsigned char)name[len]; 1736200e9ef7SLinus Torvalds } while (c && c != '/'); 1737200e9ef7SLinus Torvalds *hashp = end_name_hash(hash); 1738200e9ef7SLinus Torvalds return len; 1739200e9ef7SLinus Torvalds } 1740200e9ef7SLinus Torvalds 1741bfcfaa77SLinus Torvalds #endif 1742bfcfaa77SLinus Torvalds 1743200e9ef7SLinus Torvalds /* 17441da177e4SLinus Torvalds * Name resolution. 1745ea3834d9SPrasanna Meda * This is the basic name resolution function, turning a pathname into 1746ea3834d9SPrasanna Meda * the final dentry. We expect 'base' to be positive and a directory. 17471da177e4SLinus Torvalds * 1748ea3834d9SPrasanna Meda * Returns 0 and nd will have valid dentry and mnt on success. 1749ea3834d9SPrasanna Meda * Returns error and drops reference to input namei data on failure. 17501da177e4SLinus Torvalds */ 17516de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd) 17521da177e4SLinus Torvalds { 17531da177e4SLinus Torvalds struct path next; 17541da177e4SLinus Torvalds int err; 17551da177e4SLinus Torvalds 17561da177e4SLinus Torvalds while (*name=='/') 17571da177e4SLinus Torvalds name++; 17581da177e4SLinus Torvalds if (!*name) 1759086e183aSAl Viro return 0; 17601da177e4SLinus Torvalds 17611da177e4SLinus Torvalds /* At this point we know we have a real path component. */ 17621da177e4SLinus Torvalds for(;;) { 17631da177e4SLinus Torvalds struct qstr this; 1764200e9ef7SLinus Torvalds long len; 1765fe479a58SAl Viro int type; 17661da177e4SLinus Torvalds 176752094c8aSAl Viro err = may_lookup(nd); 17681da177e4SLinus Torvalds if (err) 17691da177e4SLinus Torvalds break; 17701da177e4SLinus Torvalds 1771200e9ef7SLinus Torvalds len = hash_name(name, &this.hash); 17721da177e4SLinus Torvalds this.name = name; 1773200e9ef7SLinus Torvalds this.len = len; 17741da177e4SLinus Torvalds 1775fe479a58SAl Viro type = LAST_NORM; 1776200e9ef7SLinus Torvalds if (name[0] == '.') switch (len) { 1777fe479a58SAl Viro case 2: 1778200e9ef7SLinus Torvalds if (name[1] == '.') { 1779fe479a58SAl Viro type = LAST_DOTDOT; 178016c2cd71SAl Viro nd->flags |= LOOKUP_JUMPED; 178116c2cd71SAl Viro } 1782fe479a58SAl Viro break; 1783fe479a58SAl Viro case 1: 1784fe479a58SAl Viro type = LAST_DOT; 1785fe479a58SAl Viro } 17865a202bcdSAl Viro if (likely(type == LAST_NORM)) { 17875a202bcdSAl Viro struct dentry *parent = nd->path.dentry; 178816c2cd71SAl Viro nd->flags &= ~LOOKUP_JUMPED; 17895a202bcdSAl Viro if (unlikely(parent->d_flags & DCACHE_OP_HASH)) { 1790da53be12SLinus Torvalds err = parent->d_op->d_hash(parent, &this); 17915a202bcdSAl Viro if (err < 0) 17925a202bcdSAl Viro break; 17935a202bcdSAl Viro } 17945a202bcdSAl Viro } 1795fe479a58SAl Viro 17965f4a6a69SAl Viro nd->last = this; 17975f4a6a69SAl Viro nd->last_type = type; 17985f4a6a69SAl Viro 1799200e9ef7SLinus Torvalds if (!name[len]) 18005f4a6a69SAl Viro return 0; 1801200e9ef7SLinus Torvalds /* 1802200e9ef7SLinus Torvalds * If it wasn't NUL, we know it was '/'. Skip that 1803200e9ef7SLinus Torvalds * slash, and continue until no more slashes. 1804200e9ef7SLinus Torvalds */ 1805200e9ef7SLinus Torvalds do { 1806200e9ef7SLinus Torvalds len++; 1807200e9ef7SLinus Torvalds } while (unlikely(name[len] == '/')); 1808200e9ef7SLinus Torvalds if (!name[len]) 18095f4a6a69SAl Viro return 0; 18105f4a6a69SAl Viro 1811200e9ef7SLinus Torvalds name += len; 18121da177e4SLinus Torvalds 181321b9b073SAl Viro err = walk_component(nd, &next, LOOKUP_FOLLOW); 1814ce57dfc1SAl Viro if (err < 0) 1815ce57dfc1SAl Viro return err; 1816fe479a58SAl Viro 1817ce57dfc1SAl Viro if (err) { 1818b356379aSAl Viro err = nested_symlink(&next, nd); 18191da177e4SLinus Torvalds if (err) 1820a7472babSAl Viro return err; 182131e6b01fSNick Piggin } 18225f4a6a69SAl Viro if (!can_lookup(nd->inode)) { 18233ddcd056SLinus Torvalds err = -ENOTDIR; 18243ddcd056SLinus Torvalds break; 18255f4a6a69SAl Viro } 1826ce57dfc1SAl Viro } 1827951361f9SAl Viro terminate_walk(nd); 18281da177e4SLinus Torvalds return err; 18291da177e4SLinus Torvalds } 18301da177e4SLinus Torvalds 183170e9b357SAl Viro static int path_init(int dfd, const char *name, unsigned int flags, 183270e9b357SAl Viro struct nameidata *nd, struct file **fp) 183331e6b01fSNick Piggin { 183431e6b01fSNick Piggin int retval = 0; 183531e6b01fSNick Piggin 183631e6b01fSNick Piggin nd->last_type = LAST_ROOT; /* if there are only slashes... */ 183716c2cd71SAl Viro nd->flags = flags | LOOKUP_JUMPED; 183831e6b01fSNick Piggin nd->depth = 0; 18395b6ca027SAl Viro if (flags & LOOKUP_ROOT) { 18405b6ca027SAl Viro struct inode *inode = nd->root.dentry->d_inode; 184173d049a4SAl Viro if (*name) { 1842741b7c3fSAl Viro if (!can_lookup(inode)) 18435b6ca027SAl Viro return -ENOTDIR; 18445b6ca027SAl Viro retval = inode_permission(inode, MAY_EXEC); 18455b6ca027SAl Viro if (retval) 18465b6ca027SAl Viro return retval; 184773d049a4SAl Viro } 18485b6ca027SAl Viro nd->path = nd->root; 18495b6ca027SAl Viro nd->inode = inode; 18505b6ca027SAl Viro if (flags & LOOKUP_RCU) { 185132a7991bSAl Viro lock_rcu_walk(); 18525b6ca027SAl Viro nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 18535b6ca027SAl Viro } else { 18545b6ca027SAl Viro path_get(&nd->path); 18555b6ca027SAl Viro } 18565b6ca027SAl Viro return 0; 18575b6ca027SAl Viro } 18585b6ca027SAl Viro 185931e6b01fSNick Piggin nd->root.mnt = NULL; 186031e6b01fSNick Piggin 186131e6b01fSNick Piggin if (*name=='/') { 1862e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 186332a7991bSAl Viro lock_rcu_walk(); 1864e41f7d4eSAl Viro set_root_rcu(nd); 1865e41f7d4eSAl Viro } else { 1866e41f7d4eSAl Viro set_root(nd); 1867e41f7d4eSAl Viro path_get(&nd->root); 1868e41f7d4eSAl Viro } 186931e6b01fSNick Piggin nd->path = nd->root; 187031e6b01fSNick Piggin } else if (dfd == AT_FDCWD) { 1871e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 187231e6b01fSNick Piggin struct fs_struct *fs = current->fs; 1873c28cc364SNick Piggin unsigned seq; 187431e6b01fSNick Piggin 187532a7991bSAl Viro lock_rcu_walk(); 187631e6b01fSNick Piggin 1877c28cc364SNick Piggin do { 1878c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 187931e6b01fSNick Piggin nd->path = fs->pwd; 1880c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 1881c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 1882e41f7d4eSAl Viro } else { 1883e41f7d4eSAl Viro get_fs_pwd(current->fs, &nd->path); 1884e41f7d4eSAl Viro } 188531e6b01fSNick Piggin } else { 1886582aa64aSJeff Layton /* Caller must check execute permissions on the starting path component */ 18872903ff01SAl Viro struct fd f = fdget_raw(dfd); 188831e6b01fSNick Piggin struct dentry *dentry; 188931e6b01fSNick Piggin 18902903ff01SAl Viro if (!f.file) 18912903ff01SAl Viro return -EBADF; 189231e6b01fSNick Piggin 18932903ff01SAl Viro dentry = f.file->f_path.dentry; 189431e6b01fSNick Piggin 1895f52e0c11SAl Viro if (*name) { 1896741b7c3fSAl Viro if (!can_lookup(dentry->d_inode)) { 18972903ff01SAl Viro fdput(f); 18982903ff01SAl Viro return -ENOTDIR; 1899f52e0c11SAl Viro } 19002903ff01SAl Viro } 19012903ff01SAl Viro 19022903ff01SAl Viro nd->path = f.file->f_path; 1903e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 19042903ff01SAl Viro if (f.need_put) 19052903ff01SAl Viro *fp = f.file; 1906c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 190732a7991bSAl Viro lock_rcu_walk(); 19085590ff0dSUlrich Drepper } else { 19092903ff01SAl Viro path_get(&nd->path); 19102903ff01SAl Viro fdput(f); 19111da177e4SLinus Torvalds } 1912e41f7d4eSAl Viro } 1913e41f7d4eSAl Viro 191431e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 19159b4a9b14SAl Viro return 0; 19169b4a9b14SAl Viro } 19179b4a9b14SAl Viro 1918bd92d7feSAl Viro static inline int lookup_last(struct nameidata *nd, struct path *path) 1919bd92d7feSAl Viro { 1920bd92d7feSAl Viro if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len]) 1921bd92d7feSAl Viro nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 1922bd92d7feSAl Viro 1923bd92d7feSAl Viro nd->flags &= ~LOOKUP_PARENT; 192421b9b073SAl Viro return walk_component(nd, path, nd->flags & LOOKUP_FOLLOW); 1925bd92d7feSAl Viro } 1926bd92d7feSAl Viro 19279b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */ 1928ee0827cdSAl Viro static int path_lookupat(int dfd, const char *name, 19299b4a9b14SAl Viro unsigned int flags, struct nameidata *nd) 19309b4a9b14SAl Viro { 193170e9b357SAl Viro struct file *base = NULL; 1932bd92d7feSAl Viro struct path path; 1933bd92d7feSAl Viro int err; 193431e6b01fSNick Piggin 193531e6b01fSNick Piggin /* 193631e6b01fSNick Piggin * Path walking is largely split up into 2 different synchronisation 193731e6b01fSNick Piggin * schemes, rcu-walk and ref-walk (explained in 193831e6b01fSNick Piggin * Documentation/filesystems/path-lookup.txt). These share much of the 193931e6b01fSNick Piggin * path walk code, but some things particularly setup, cleanup, and 194031e6b01fSNick Piggin * following mounts are sufficiently divergent that functions are 194131e6b01fSNick Piggin * duplicated. Typically there is a function foo(), and its RCU 194231e6b01fSNick Piggin * analogue, foo_rcu(). 194331e6b01fSNick Piggin * 194431e6b01fSNick Piggin * -ECHILD is the error number of choice (just to avoid clashes) that 194531e6b01fSNick Piggin * is returned if some aspect of an rcu-walk fails. Such an error must 194631e6b01fSNick Piggin * be handled by restarting a traditional ref-walk (which will always 194731e6b01fSNick Piggin * be able to complete). 194831e6b01fSNick Piggin */ 1949bd92d7feSAl Viro err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base); 1950ee0827cdSAl Viro 1951bd92d7feSAl Viro if (unlikely(err)) 1952bd92d7feSAl Viro return err; 1953ee0827cdSAl Viro 1954ee0827cdSAl Viro current->total_link_count = 0; 1955bd92d7feSAl Viro err = link_path_walk(name, nd); 1956bd92d7feSAl Viro 1957bd92d7feSAl Viro if (!err && !(flags & LOOKUP_PARENT)) { 1958bd92d7feSAl Viro err = lookup_last(nd, &path); 1959bd92d7feSAl Viro while (err > 0) { 1960bd92d7feSAl Viro void *cookie; 1961bd92d7feSAl Viro struct path link = path; 1962800179c9SKees Cook err = may_follow_link(&link, nd); 1963800179c9SKees Cook if (unlikely(err)) 1964800179c9SKees Cook break; 1965bd92d7feSAl Viro nd->flags |= LOOKUP_PARENT; 1966574197e0SAl Viro err = follow_link(&link, nd, &cookie); 19676d7b5aaeSAl Viro if (err) 19686d7b5aaeSAl Viro break; 1969bd92d7feSAl Viro err = lookup_last(nd, &path); 1970574197e0SAl Viro put_link(nd, &link, cookie); 1971bd92d7feSAl Viro } 1972bd92d7feSAl Viro } 1973ee0827cdSAl Viro 19749f1fafeeSAl Viro if (!err) 19759f1fafeeSAl Viro err = complete_walk(nd); 1976bd92d7feSAl Viro 1977bd92d7feSAl Viro if (!err && nd->flags & LOOKUP_DIRECTORY) { 197805252901SAl Viro if (!can_lookup(nd->inode)) { 1979bd92d7feSAl Viro path_put(&nd->path); 1980bd23a539SAl Viro err = -ENOTDIR; 1981bd92d7feSAl Viro } 1982bd92d7feSAl Viro } 198316c2cd71SAl Viro 198470e9b357SAl Viro if (base) 198570e9b357SAl Viro fput(base); 1986ee0827cdSAl Viro 19875b6ca027SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) { 198831e6b01fSNick Piggin path_put(&nd->root); 198931e6b01fSNick Piggin nd->root.mnt = NULL; 199031e6b01fSNick Piggin } 1991bd92d7feSAl Viro return err; 199231e6b01fSNick Piggin } 199331e6b01fSNick Piggin 1994873f1eedSJeff Layton static int filename_lookup(int dfd, struct filename *name, 1995873f1eedSJeff Layton unsigned int flags, struct nameidata *nd) 1996873f1eedSJeff Layton { 1997873f1eedSJeff Layton int retval = path_lookupat(dfd, name->name, flags | LOOKUP_RCU, nd); 1998873f1eedSJeff Layton if (unlikely(retval == -ECHILD)) 1999873f1eedSJeff Layton retval = path_lookupat(dfd, name->name, flags, nd); 2000873f1eedSJeff Layton if (unlikely(retval == -ESTALE)) 2001873f1eedSJeff Layton retval = path_lookupat(dfd, name->name, 2002873f1eedSJeff Layton flags | LOOKUP_REVAL, nd); 2003873f1eedSJeff Layton 2004873f1eedSJeff Layton if (likely(!retval)) 2005adb5c247SJeff Layton audit_inode(name, nd->path.dentry, flags & LOOKUP_PARENT); 2006873f1eedSJeff Layton return retval; 2007873f1eedSJeff Layton } 2008873f1eedSJeff Layton 2009ee0827cdSAl Viro static int do_path_lookup(int dfd, const char *name, 2010ee0827cdSAl Viro unsigned int flags, struct nameidata *nd) 2011ee0827cdSAl Viro { 2012873f1eedSJeff Layton struct filename filename = { .name = name }; 2013ee0827cdSAl Viro 2014873f1eedSJeff Layton return filename_lookup(dfd, &filename, flags, nd); 20151da177e4SLinus Torvalds } 20161da177e4SLinus Torvalds 201779714f72SAl Viro /* does lookup, returns the object with parent locked */ 201879714f72SAl Viro struct dentry *kern_path_locked(const char *name, struct path *path) 20195590ff0dSUlrich Drepper { 202079714f72SAl Viro struct nameidata nd; 202179714f72SAl Viro struct dentry *d; 202279714f72SAl Viro int err = do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, &nd); 202379714f72SAl Viro if (err) 202479714f72SAl Viro return ERR_PTR(err); 202579714f72SAl Viro if (nd.last_type != LAST_NORM) { 202679714f72SAl Viro path_put(&nd.path); 202779714f72SAl Viro return ERR_PTR(-EINVAL); 202879714f72SAl Viro } 202979714f72SAl Viro mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 20301e0ea001SAl Viro d = __lookup_hash(&nd.last, nd.path.dentry, 0); 203179714f72SAl Viro if (IS_ERR(d)) { 203279714f72SAl Viro mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 203379714f72SAl Viro path_put(&nd.path); 203479714f72SAl Viro return d; 203579714f72SAl Viro } 203679714f72SAl Viro *path = nd.path; 203779714f72SAl Viro return d; 20385590ff0dSUlrich Drepper } 20395590ff0dSUlrich Drepper 2040d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path) 2041d1811465SAl Viro { 2042d1811465SAl Viro struct nameidata nd; 2043d1811465SAl Viro int res = do_path_lookup(AT_FDCWD, name, flags, &nd); 2044d1811465SAl Viro if (!res) 2045d1811465SAl Viro *path = nd.path; 2046d1811465SAl Viro return res; 2047d1811465SAl Viro } 2048d1811465SAl Viro 204916f18200SJosef 'Jeff' Sipek /** 205016f18200SJosef 'Jeff' Sipek * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair 205116f18200SJosef 'Jeff' Sipek * @dentry: pointer to dentry of the base directory 205216f18200SJosef 'Jeff' Sipek * @mnt: pointer to vfs mount of the base directory 205316f18200SJosef 'Jeff' Sipek * @name: pointer to file name 205416f18200SJosef 'Jeff' Sipek * @flags: lookup flags 2055e0a01249SAl Viro * @path: pointer to struct path to fill 205616f18200SJosef 'Jeff' Sipek */ 205716f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt, 205816f18200SJosef 'Jeff' Sipek const char *name, unsigned int flags, 2059e0a01249SAl Viro struct path *path) 206016f18200SJosef 'Jeff' Sipek { 2061e0a01249SAl Viro struct nameidata nd; 2062e0a01249SAl Viro int err; 2063e0a01249SAl Viro nd.root.dentry = dentry; 2064e0a01249SAl Viro nd.root.mnt = mnt; 2065e0a01249SAl Viro BUG_ON(flags & LOOKUP_PARENT); 20665b6ca027SAl Viro /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */ 2067e0a01249SAl Viro err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd); 2068e0a01249SAl Viro if (!err) 2069e0a01249SAl Viro *path = nd.path; 2070e0a01249SAl Viro return err; 207116f18200SJosef 'Jeff' Sipek } 207216f18200SJosef 'Jeff' Sipek 2073057f6c01SJames Morris /* 2074057f6c01SJames Morris * Restricted form of lookup. Doesn't follow links, single-component only, 2075057f6c01SJames Morris * needs parent already locked. Doesn't follow mounts. 2076057f6c01SJames Morris * SMP-safe. 2077057f6c01SJames Morris */ 2078a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd) 20791da177e4SLinus Torvalds { 208072bd866aSAl Viro return __lookup_hash(&nd->last, nd->path.dentry, nd->flags); 20811da177e4SLinus Torvalds } 20821da177e4SLinus Torvalds 2083eead1911SChristoph Hellwig /** 2084a6b91919SRandy Dunlap * lookup_one_len - filesystem helper to lookup single pathname component 2085eead1911SChristoph Hellwig * @name: pathname component to lookup 2086eead1911SChristoph Hellwig * @base: base directory to lookup from 2087eead1911SChristoph Hellwig * @len: maximum length @len should be interpreted to 2088eead1911SChristoph Hellwig * 2089a6b91919SRandy Dunlap * Note that this routine is purely a helper for filesystem usage and should 2090a6b91919SRandy Dunlap * not be called by generic code. Also note that by using this function the 2091eead1911SChristoph Hellwig * nameidata argument is passed to the filesystem methods and a filesystem 2092eead1911SChristoph Hellwig * using this helper needs to be prepared for that. 2093eead1911SChristoph Hellwig */ 2094057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len) 2095057f6c01SJames Morris { 2096057f6c01SJames Morris struct qstr this; 20976a96ba54SAl Viro unsigned int c; 2098cda309deSMiklos Szeredi int err; 2099057f6c01SJames Morris 21002f9092e1SDavid Woodhouse WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex)); 21012f9092e1SDavid Woodhouse 21026a96ba54SAl Viro this.name = name; 21036a96ba54SAl Viro this.len = len; 21040145acc2SLinus Torvalds this.hash = full_name_hash(name, len); 21056a96ba54SAl Viro if (!len) 21066a96ba54SAl Viro return ERR_PTR(-EACCES); 21076a96ba54SAl Viro 210821d8a15aSAl Viro if (unlikely(name[0] == '.')) { 210921d8a15aSAl Viro if (len < 2 || (len == 2 && name[1] == '.')) 211021d8a15aSAl Viro return ERR_PTR(-EACCES); 211121d8a15aSAl Viro } 211221d8a15aSAl Viro 21136a96ba54SAl Viro while (len--) { 21146a96ba54SAl Viro c = *(const unsigned char *)name++; 21156a96ba54SAl Viro if (c == '/' || c == '\0') 21166a96ba54SAl Viro return ERR_PTR(-EACCES); 21176a96ba54SAl Viro } 21185a202bcdSAl Viro /* 21195a202bcdSAl Viro * See if the low-level filesystem might want 21205a202bcdSAl Viro * to use its own hash.. 21215a202bcdSAl Viro */ 21225a202bcdSAl Viro if (base->d_flags & DCACHE_OP_HASH) { 2123da53be12SLinus Torvalds int err = base->d_op->d_hash(base, &this); 21245a202bcdSAl Viro if (err < 0) 21255a202bcdSAl Viro return ERR_PTR(err); 21265a202bcdSAl Viro } 2127eead1911SChristoph Hellwig 2128cda309deSMiklos Szeredi err = inode_permission(base->d_inode, MAY_EXEC); 2129cda309deSMiklos Szeredi if (err) 2130cda309deSMiklos Szeredi return ERR_PTR(err); 2131cda309deSMiklos Szeredi 213272bd866aSAl Viro return __lookup_hash(&this, base, 0); 2133057f6c01SJames Morris } 2134057f6c01SJames Morris 21351fa1e7f6SAndy Whitcroft int user_path_at_empty(int dfd, const char __user *name, unsigned flags, 21361fa1e7f6SAndy Whitcroft struct path *path, int *empty) 21371da177e4SLinus Torvalds { 21382d8f3038SAl Viro struct nameidata nd; 213991a27b2aSJeff Layton struct filename *tmp = getname_flags(name, flags, empty); 21401da177e4SLinus Torvalds int err = PTR_ERR(tmp); 21411da177e4SLinus Torvalds if (!IS_ERR(tmp)) { 21422d8f3038SAl Viro 21432d8f3038SAl Viro BUG_ON(flags & LOOKUP_PARENT); 21442d8f3038SAl Viro 2145873f1eedSJeff Layton err = filename_lookup(dfd, tmp, flags, &nd); 21461da177e4SLinus Torvalds putname(tmp); 21472d8f3038SAl Viro if (!err) 21482d8f3038SAl Viro *path = nd.path; 21491da177e4SLinus Torvalds } 21501da177e4SLinus Torvalds return err; 21511da177e4SLinus Torvalds } 21521da177e4SLinus Torvalds 21531fa1e7f6SAndy Whitcroft int user_path_at(int dfd, const char __user *name, unsigned flags, 21541fa1e7f6SAndy Whitcroft struct path *path) 21551fa1e7f6SAndy Whitcroft { 2156f7493e5dSLinus Torvalds return user_path_at_empty(dfd, name, flags, path, NULL); 21571fa1e7f6SAndy Whitcroft } 21581fa1e7f6SAndy Whitcroft 2159873f1eedSJeff Layton /* 2160873f1eedSJeff Layton * NB: most callers don't do anything directly with the reference to the 2161873f1eedSJeff Layton * to struct filename, but the nd->last pointer points into the name string 2162873f1eedSJeff Layton * allocated by getname. So we must hold the reference to it until all 2163873f1eedSJeff Layton * path-walking is complete. 2164873f1eedSJeff Layton */ 216591a27b2aSJeff Layton static struct filename * 21669e790bd6SJeff Layton user_path_parent(int dfd, const char __user *path, struct nameidata *nd, 21679e790bd6SJeff Layton unsigned int flags) 21682ad94ae6SAl Viro { 216991a27b2aSJeff Layton struct filename *s = getname(path); 21702ad94ae6SAl Viro int error; 21712ad94ae6SAl Viro 21729e790bd6SJeff Layton /* only LOOKUP_REVAL is allowed in extra flags */ 21739e790bd6SJeff Layton flags &= LOOKUP_REVAL; 21749e790bd6SJeff Layton 21752ad94ae6SAl Viro if (IS_ERR(s)) 217691a27b2aSJeff Layton return s; 21772ad94ae6SAl Viro 21789e790bd6SJeff Layton error = filename_lookup(dfd, s, flags | LOOKUP_PARENT, nd); 217991a27b2aSJeff Layton if (error) { 21802ad94ae6SAl Viro putname(s); 218191a27b2aSJeff Layton return ERR_PTR(error); 218291a27b2aSJeff Layton } 21832ad94ae6SAl Viro 218491a27b2aSJeff Layton return s; 21852ad94ae6SAl Viro } 21862ad94ae6SAl Viro 21871da177e4SLinus Torvalds /* 21881da177e4SLinus Torvalds * It's inline, so penalty for filesystems that don't use sticky bit is 21891da177e4SLinus Torvalds * minimal. 21901da177e4SLinus Torvalds */ 21911da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode) 21921da177e4SLinus Torvalds { 21938e96e3b7SEric W. Biederman kuid_t fsuid = current_fsuid(); 2194da9592edSDavid Howells 21951da177e4SLinus Torvalds if (!(dir->i_mode & S_ISVTX)) 21961da177e4SLinus Torvalds return 0; 21978e96e3b7SEric W. Biederman if (uid_eq(inode->i_uid, fsuid)) 21981da177e4SLinus Torvalds return 0; 21998e96e3b7SEric W. Biederman if (uid_eq(dir->i_uid, fsuid)) 22001da177e4SLinus Torvalds return 0; 22011a48e2acSEric W. Biederman return !inode_capable(inode, CAP_FOWNER); 22021da177e4SLinus Torvalds } 22031da177e4SLinus Torvalds 22041da177e4SLinus Torvalds /* 22051da177e4SLinus Torvalds * Check whether we can remove a link victim from directory dir, check 22061da177e4SLinus Torvalds * whether the type of victim is right. 22071da177e4SLinus Torvalds * 1. We can't do it if dir is read-only (done in permission()) 22081da177e4SLinus Torvalds * 2. We should have write and exec permissions on dir 22091da177e4SLinus Torvalds * 3. We can't remove anything from append-only dir 22101da177e4SLinus Torvalds * 4. We can't do anything with immutable dir (done in permission()) 22111da177e4SLinus Torvalds * 5. If the sticky bit on dir is set we should either 22121da177e4SLinus Torvalds * a. be owner of dir, or 22131da177e4SLinus Torvalds * b. be owner of victim, or 22141da177e4SLinus Torvalds * c. have CAP_FOWNER capability 22151da177e4SLinus Torvalds * 6. If the victim is append-only or immutable we can't do antyhing with 22161da177e4SLinus Torvalds * links pointing to it. 22171da177e4SLinus Torvalds * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. 22181da177e4SLinus Torvalds * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. 22191da177e4SLinus Torvalds * 9. We can't remove a root or mountpoint. 22201da177e4SLinus Torvalds * 10. We don't allow removal of NFS sillyrenamed files; it's handled by 22211da177e4SLinus Torvalds * nfs_async_unlink(). 22221da177e4SLinus Torvalds */ 2223858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir) 22241da177e4SLinus Torvalds { 22251da177e4SLinus Torvalds int error; 22261da177e4SLinus Torvalds 22271da177e4SLinus Torvalds if (!victim->d_inode) 22281da177e4SLinus Torvalds return -ENOENT; 22291da177e4SLinus Torvalds 22301da177e4SLinus Torvalds BUG_ON(victim->d_parent->d_inode != dir); 22314fa6b5ecSJeff Layton audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE); 22321da177e4SLinus Torvalds 2233f419a2e3SAl Viro error = inode_permission(dir, MAY_WRITE | MAY_EXEC); 22341da177e4SLinus Torvalds if (error) 22351da177e4SLinus Torvalds return error; 22361da177e4SLinus Torvalds if (IS_APPEND(dir)) 22371da177e4SLinus Torvalds return -EPERM; 22381da177e4SLinus Torvalds if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)|| 2239f9454548SHugh Dickins IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode)) 22401da177e4SLinus Torvalds return -EPERM; 22411da177e4SLinus Torvalds if (isdir) { 22421da177e4SLinus Torvalds if (!S_ISDIR(victim->d_inode->i_mode)) 22431da177e4SLinus Torvalds return -ENOTDIR; 22441da177e4SLinus Torvalds if (IS_ROOT(victim)) 22451da177e4SLinus Torvalds return -EBUSY; 22461da177e4SLinus Torvalds } else if (S_ISDIR(victim->d_inode->i_mode)) 22471da177e4SLinus Torvalds return -EISDIR; 22481da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 22491da177e4SLinus Torvalds return -ENOENT; 22501da177e4SLinus Torvalds if (victim->d_flags & DCACHE_NFSFS_RENAMED) 22511da177e4SLinus Torvalds return -EBUSY; 22521da177e4SLinus Torvalds return 0; 22531da177e4SLinus Torvalds } 22541da177e4SLinus Torvalds 22551da177e4SLinus Torvalds /* Check whether we can create an object with dentry child in directory 22561da177e4SLinus Torvalds * dir. 22571da177e4SLinus Torvalds * 1. We can't do it if child already exists (open has special treatment for 22581da177e4SLinus Torvalds * this case, but since we are inlined it's OK) 22591da177e4SLinus Torvalds * 2. We can't do it if dir is read-only (done in permission()) 22601da177e4SLinus Torvalds * 3. We should have write and exec permissions on dir 22611da177e4SLinus Torvalds * 4. We can't do it if dir is immutable (done in permission()) 22621da177e4SLinus Torvalds */ 2263a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child) 22641da177e4SLinus Torvalds { 22651da177e4SLinus Torvalds if (child->d_inode) 22661da177e4SLinus Torvalds return -EEXIST; 22671da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 22681da177e4SLinus Torvalds return -ENOENT; 2269f419a2e3SAl Viro return inode_permission(dir, MAY_WRITE | MAY_EXEC); 22701da177e4SLinus Torvalds } 22711da177e4SLinus Torvalds 22721da177e4SLinus Torvalds /* 22731da177e4SLinus Torvalds * p1 and p2 should be directories on the same fs. 22741da177e4SLinus Torvalds */ 22751da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2) 22761da177e4SLinus Torvalds { 22771da177e4SLinus Torvalds struct dentry *p; 22781da177e4SLinus Torvalds 22791da177e4SLinus Torvalds if (p1 == p2) { 2280f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 22811da177e4SLinus Torvalds return NULL; 22821da177e4SLinus Torvalds } 22831da177e4SLinus Torvalds 2284a11f3a05SArjan van de Ven mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 22851da177e4SLinus Torvalds 2286e2761a11SOGAWA Hirofumi p = d_ancestor(p2, p1); 2287e2761a11SOGAWA Hirofumi if (p) { 2288f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT); 2289f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD); 22901da177e4SLinus Torvalds return p; 22911da177e4SLinus Torvalds } 22921da177e4SLinus Torvalds 2293e2761a11SOGAWA Hirofumi p = d_ancestor(p1, p2); 2294e2761a11SOGAWA Hirofumi if (p) { 2295f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 2296f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 22971da177e4SLinus Torvalds return p; 22981da177e4SLinus Torvalds } 22991da177e4SLinus Torvalds 2300f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 2301f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 23021da177e4SLinus Torvalds return NULL; 23031da177e4SLinus Torvalds } 23041da177e4SLinus Torvalds 23051da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2) 23061da177e4SLinus Torvalds { 23071b1dcc1bSJes Sorensen mutex_unlock(&p1->d_inode->i_mutex); 23081da177e4SLinus Torvalds if (p1 != p2) { 23091b1dcc1bSJes Sorensen mutex_unlock(&p2->d_inode->i_mutex); 2310a11f3a05SArjan van de Ven mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 23111da177e4SLinus Torvalds } 23121da177e4SLinus Torvalds } 23131da177e4SLinus Torvalds 23144acdaf27SAl Viro int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode, 2315312b63fbSAl Viro bool want_excl) 23161da177e4SLinus Torvalds { 2317a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 23181da177e4SLinus Torvalds if (error) 23191da177e4SLinus Torvalds return error; 23201da177e4SLinus Torvalds 2321acfa4380SAl Viro if (!dir->i_op->create) 23221da177e4SLinus Torvalds return -EACCES; /* shouldn't it be ENOSYS? */ 23231da177e4SLinus Torvalds mode &= S_IALLUGO; 23241da177e4SLinus Torvalds mode |= S_IFREG; 23251da177e4SLinus Torvalds error = security_inode_create(dir, dentry, mode); 23261da177e4SLinus Torvalds if (error) 23271da177e4SLinus Torvalds return error; 2328312b63fbSAl Viro error = dir->i_op->create(dir, dentry, mode, want_excl); 2329a74574aaSStephen Smalley if (!error) 2330f38aa942SAmy Griffis fsnotify_create(dir, dentry); 23311da177e4SLinus Torvalds return error; 23321da177e4SLinus Torvalds } 23331da177e4SLinus Torvalds 233473d049a4SAl Viro static int may_open(struct path *path, int acc_mode, int flag) 23351da177e4SLinus Torvalds { 23363fb64190SChristoph Hellwig struct dentry *dentry = path->dentry; 23371da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 23381da177e4SLinus Torvalds int error; 23391da177e4SLinus Torvalds 2340bcda7652SAl Viro /* O_PATH? */ 2341bcda7652SAl Viro if (!acc_mode) 2342bcda7652SAl Viro return 0; 2343bcda7652SAl Viro 23441da177e4SLinus Torvalds if (!inode) 23451da177e4SLinus Torvalds return -ENOENT; 23461da177e4SLinus Torvalds 2347c8fe8f30SChristoph Hellwig switch (inode->i_mode & S_IFMT) { 2348c8fe8f30SChristoph Hellwig case S_IFLNK: 23491da177e4SLinus Torvalds return -ELOOP; 2350c8fe8f30SChristoph Hellwig case S_IFDIR: 2351c8fe8f30SChristoph Hellwig if (acc_mode & MAY_WRITE) 23521da177e4SLinus Torvalds return -EISDIR; 2353c8fe8f30SChristoph Hellwig break; 2354c8fe8f30SChristoph Hellwig case S_IFBLK: 2355c8fe8f30SChristoph Hellwig case S_IFCHR: 23563fb64190SChristoph Hellwig if (path->mnt->mnt_flags & MNT_NODEV) 23571da177e4SLinus Torvalds return -EACCES; 2358c8fe8f30SChristoph Hellwig /*FALLTHRU*/ 2359c8fe8f30SChristoph Hellwig case S_IFIFO: 2360c8fe8f30SChristoph Hellwig case S_IFSOCK: 23611da177e4SLinus Torvalds flag &= ~O_TRUNC; 2362c8fe8f30SChristoph Hellwig break; 23634a3fd211SDave Hansen } 2364b41572e9SDave Hansen 23653fb64190SChristoph Hellwig error = inode_permission(inode, acc_mode); 2366b41572e9SDave Hansen if (error) 2367b41572e9SDave Hansen return error; 23686146f0d5SMimi Zohar 23691da177e4SLinus Torvalds /* 23701da177e4SLinus Torvalds * An append-only file must be opened in append mode for writing. 23711da177e4SLinus Torvalds */ 23721da177e4SLinus Torvalds if (IS_APPEND(inode)) { 23738737c930SAl Viro if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND)) 23747715b521SAl Viro return -EPERM; 23751da177e4SLinus Torvalds if (flag & O_TRUNC) 23767715b521SAl Viro return -EPERM; 23771da177e4SLinus Torvalds } 23781da177e4SLinus Torvalds 23791da177e4SLinus Torvalds /* O_NOATIME can only be set by the owner or superuser */ 23802e149670SSerge E. Hallyn if (flag & O_NOATIME && !inode_owner_or_capable(inode)) 23817715b521SAl Viro return -EPERM; 23821da177e4SLinus Torvalds 2383f3c7691eSJ. Bruce Fields return 0; 23847715b521SAl Viro } 23857715b521SAl Viro 2386e1181ee6SJeff Layton static int handle_truncate(struct file *filp) 23877715b521SAl Viro { 2388e1181ee6SJeff Layton struct path *path = &filp->f_path; 23897715b521SAl Viro struct inode *inode = path->dentry->d_inode; 23907715b521SAl Viro int error = get_write_access(inode); 23911da177e4SLinus Torvalds if (error) 23927715b521SAl Viro return error; 23931da177e4SLinus Torvalds /* 23941da177e4SLinus Torvalds * Refuse to truncate files with mandatory locks held on them. 23951da177e4SLinus Torvalds */ 23961da177e4SLinus Torvalds error = locks_verify_locked(inode); 2397be6d3e56SKentaro Takeda if (!error) 2398ea0d3ab2STetsuo Handa error = security_path_truncate(path); 23991da177e4SLinus Torvalds if (!error) { 24007715b521SAl Viro error = do_truncate(path->dentry, 0, 2401d139d7ffSMiklos Szeredi ATTR_MTIME|ATTR_CTIME|ATTR_OPEN, 2402e1181ee6SJeff Layton filp); 24031da177e4SLinus Torvalds } 24041da177e4SLinus Torvalds put_write_access(inode); 2405acd0c935SMimi Zohar return error; 24061da177e4SLinus Torvalds } 24071da177e4SLinus Torvalds 2408d57999e1SDave Hansen static inline int open_to_namei_flags(int flag) 2409d57999e1SDave Hansen { 24108a5e929dSAl Viro if ((flag & O_ACCMODE) == 3) 24118a5e929dSAl Viro flag--; 2412d57999e1SDave Hansen return flag; 2413d57999e1SDave Hansen } 2414d57999e1SDave Hansen 2415d18e9008SMiklos Szeredi static int may_o_create(struct path *dir, struct dentry *dentry, umode_t mode) 2416d18e9008SMiklos Szeredi { 2417d18e9008SMiklos Szeredi int error = security_path_mknod(dir, dentry, mode, 0); 2418d18e9008SMiklos Szeredi if (error) 2419d18e9008SMiklos Szeredi return error; 2420d18e9008SMiklos Szeredi 2421d18e9008SMiklos Szeredi error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC); 2422d18e9008SMiklos Szeredi if (error) 2423d18e9008SMiklos Szeredi return error; 2424d18e9008SMiklos Szeredi 2425d18e9008SMiklos Szeredi return security_inode_create(dir->dentry->d_inode, dentry, mode); 2426d18e9008SMiklos Szeredi } 2427d18e9008SMiklos Szeredi 24281acf0af9SDavid Howells /* 24291acf0af9SDavid Howells * Attempt to atomically look up, create and open a file from a negative 24301acf0af9SDavid Howells * dentry. 24311acf0af9SDavid Howells * 24321acf0af9SDavid Howells * Returns 0 if successful. The file will have been created and attached to 24331acf0af9SDavid Howells * @file by the filesystem calling finish_open(). 24341acf0af9SDavid Howells * 24351acf0af9SDavid Howells * Returns 1 if the file was looked up only or didn't need creating. The 24361acf0af9SDavid Howells * caller will need to perform the open themselves. @path will have been 24371acf0af9SDavid Howells * updated to point to the new dentry. This may be negative. 24381acf0af9SDavid Howells * 24391acf0af9SDavid Howells * Returns an error code otherwise. 24401acf0af9SDavid Howells */ 24412675a4ebSAl Viro static int atomic_open(struct nameidata *nd, struct dentry *dentry, 244230d90494SAl Viro struct path *path, struct file *file, 2443015c3bbcSMiklos Szeredi const struct open_flags *op, 244464894cf8SAl Viro bool got_write, bool need_lookup, 244547237687SAl Viro int *opened) 2446d18e9008SMiklos Szeredi { 2447d18e9008SMiklos Szeredi struct inode *dir = nd->path.dentry->d_inode; 2448d18e9008SMiklos Szeredi unsigned open_flag = open_to_namei_flags(op->open_flag); 2449d18e9008SMiklos Szeredi umode_t mode; 2450d18e9008SMiklos Szeredi int error; 2451d18e9008SMiklos Szeredi int acc_mode; 2452d18e9008SMiklos Szeredi int create_error = 0; 2453d18e9008SMiklos Szeredi struct dentry *const DENTRY_NOT_SET = (void *) -1UL; 2454d18e9008SMiklos Szeredi 2455d18e9008SMiklos Szeredi BUG_ON(dentry->d_inode); 2456d18e9008SMiklos Szeredi 2457d18e9008SMiklos Szeredi /* Don't create child dentry for a dead directory. */ 2458d18e9008SMiklos Szeredi if (unlikely(IS_DEADDIR(dir))) { 24592675a4ebSAl Viro error = -ENOENT; 2460d18e9008SMiklos Szeredi goto out; 2461d18e9008SMiklos Szeredi } 2462d18e9008SMiklos Szeredi 246362b259d8SMiklos Szeredi mode = op->mode; 2464d18e9008SMiklos Szeredi if ((open_flag & O_CREAT) && !IS_POSIXACL(dir)) 2465d18e9008SMiklos Szeredi mode &= ~current_umask(); 2466d18e9008SMiklos Szeredi 2467f8310c59SAl Viro if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT)) { 2468d18e9008SMiklos Szeredi open_flag &= ~O_TRUNC; 246947237687SAl Viro *opened |= FILE_CREATED; 2470d18e9008SMiklos Szeredi } 2471d18e9008SMiklos Szeredi 2472d18e9008SMiklos Szeredi /* 2473d18e9008SMiklos Szeredi * Checking write permission is tricky, bacuse we don't know if we are 2474d18e9008SMiklos Szeredi * going to actually need it: O_CREAT opens should work as long as the 2475d18e9008SMiklos Szeredi * file exists. But checking existence breaks atomicity. The trick is 2476d18e9008SMiklos Szeredi * to check access and if not granted clear O_CREAT from the flags. 2477d18e9008SMiklos Szeredi * 2478d18e9008SMiklos Szeredi * Another problem is returing the "right" error value (e.g. for an 2479d18e9008SMiklos Szeredi * O_EXCL open we want to return EEXIST not EROFS). 2480d18e9008SMiklos Szeredi */ 248164894cf8SAl Viro if (((open_flag & (O_CREAT | O_TRUNC)) || 248264894cf8SAl Viro (open_flag & O_ACCMODE) != O_RDONLY) && unlikely(!got_write)) { 248364894cf8SAl Viro if (!(open_flag & O_CREAT)) { 2484d18e9008SMiklos Szeredi /* 2485d18e9008SMiklos Szeredi * No O_CREATE -> atomicity not a requirement -> fall 2486d18e9008SMiklos Szeredi * back to lookup + open 2487d18e9008SMiklos Szeredi */ 2488d18e9008SMiklos Szeredi goto no_open; 2489d18e9008SMiklos Szeredi } else if (open_flag & (O_EXCL | O_TRUNC)) { 2490d18e9008SMiklos Szeredi /* Fall back and fail with the right error */ 249164894cf8SAl Viro create_error = -EROFS; 2492d18e9008SMiklos Szeredi goto no_open; 2493d18e9008SMiklos Szeredi } else { 2494d18e9008SMiklos Szeredi /* No side effects, safe to clear O_CREAT */ 249564894cf8SAl Viro create_error = -EROFS; 2496d18e9008SMiklos Szeredi open_flag &= ~O_CREAT; 2497d18e9008SMiklos Szeredi } 2498d18e9008SMiklos Szeredi } 2499d18e9008SMiklos Szeredi 2500d18e9008SMiklos Szeredi if (open_flag & O_CREAT) { 250138227f78SMiklos Szeredi error = may_o_create(&nd->path, dentry, mode); 2502d18e9008SMiklos Szeredi if (error) { 2503d18e9008SMiklos Szeredi create_error = error; 2504d18e9008SMiklos Szeredi if (open_flag & O_EXCL) 2505d18e9008SMiklos Szeredi goto no_open; 2506d18e9008SMiklos Szeredi open_flag &= ~O_CREAT; 2507d18e9008SMiklos Szeredi } 2508d18e9008SMiklos Szeredi } 2509d18e9008SMiklos Szeredi 2510d18e9008SMiklos Szeredi if (nd->flags & LOOKUP_DIRECTORY) 2511d18e9008SMiklos Szeredi open_flag |= O_DIRECTORY; 2512d18e9008SMiklos Szeredi 251330d90494SAl Viro file->f_path.dentry = DENTRY_NOT_SET; 251430d90494SAl Viro file->f_path.mnt = nd->path.mnt; 251530d90494SAl Viro error = dir->i_op->atomic_open(dir, dentry, file, open_flag, mode, 251647237687SAl Viro opened); 2517d9585277SAl Viro if (error < 0) { 2518d9585277SAl Viro if (create_error && error == -ENOENT) 2519d9585277SAl Viro error = create_error; 2520d18e9008SMiklos Szeredi goto out; 2521d18e9008SMiklos Szeredi } 2522d18e9008SMiklos Szeredi 2523d18e9008SMiklos Szeredi acc_mode = op->acc_mode; 252447237687SAl Viro if (*opened & FILE_CREATED) { 2525d18e9008SMiklos Szeredi fsnotify_create(dir, dentry); 2526d18e9008SMiklos Szeredi acc_mode = MAY_OPEN; 2527d18e9008SMiklos Szeredi } 2528d18e9008SMiklos Szeredi 2529d9585277SAl Viro if (error) { /* returned 1, that is */ 253030d90494SAl Viro if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) { 25312675a4ebSAl Viro error = -EIO; 2532d18e9008SMiklos Szeredi goto out; 2533d18e9008SMiklos Szeredi } 253430d90494SAl Viro if (file->f_path.dentry) { 2535d18e9008SMiklos Szeredi dput(dentry); 253630d90494SAl Viro dentry = file->f_path.dentry; 2537d18e9008SMiklos Szeredi } 253862b2ce96SSage Weil if (create_error && dentry->d_inode == NULL) { 253962b2ce96SSage Weil error = create_error; 254062b2ce96SSage Weil goto out; 254162b2ce96SSage Weil } 2542d18e9008SMiklos Szeredi goto looked_up; 2543d18e9008SMiklos Szeredi } 2544d18e9008SMiklos Szeredi 2545d18e9008SMiklos Szeredi /* 2546d18e9008SMiklos Szeredi * We didn't have the inode before the open, so check open permission 2547d18e9008SMiklos Szeredi * here. 2548d18e9008SMiklos Szeredi */ 25492675a4ebSAl Viro error = may_open(&file->f_path, acc_mode, open_flag); 25502675a4ebSAl Viro if (error) 25512675a4ebSAl Viro fput(file); 2552d18e9008SMiklos Szeredi 2553d18e9008SMiklos Szeredi out: 2554d18e9008SMiklos Szeredi dput(dentry); 25552675a4ebSAl Viro return error; 2556d18e9008SMiklos Szeredi 2557d18e9008SMiklos Szeredi no_open: 2558d18e9008SMiklos Szeredi if (need_lookup) { 255972bd866aSAl Viro dentry = lookup_real(dir, dentry, nd->flags); 2560d18e9008SMiklos Szeredi if (IS_ERR(dentry)) 25612675a4ebSAl Viro return PTR_ERR(dentry); 2562d18e9008SMiklos Szeredi 2563d18e9008SMiklos Szeredi if (create_error) { 2564d18e9008SMiklos Szeredi int open_flag = op->open_flag; 2565d18e9008SMiklos Szeredi 25662675a4ebSAl Viro error = create_error; 2567d18e9008SMiklos Szeredi if ((open_flag & O_EXCL)) { 2568d18e9008SMiklos Szeredi if (!dentry->d_inode) 2569d18e9008SMiklos Szeredi goto out; 2570d18e9008SMiklos Szeredi } else if (!dentry->d_inode) { 2571d18e9008SMiklos Szeredi goto out; 2572d18e9008SMiklos Szeredi } else if ((open_flag & O_TRUNC) && 2573d18e9008SMiklos Szeredi S_ISREG(dentry->d_inode->i_mode)) { 2574d18e9008SMiklos Szeredi goto out; 2575d18e9008SMiklos Szeredi } 2576d18e9008SMiklos Szeredi /* will fail later, go on to get the right error */ 2577d18e9008SMiklos Szeredi } 2578d18e9008SMiklos Szeredi } 2579d18e9008SMiklos Szeredi looked_up: 2580d18e9008SMiklos Szeredi path->dentry = dentry; 2581d18e9008SMiklos Szeredi path->mnt = nd->path.mnt; 25822675a4ebSAl Viro return 1; 2583d18e9008SMiklos Szeredi } 2584d18e9008SMiklos Szeredi 258531e6b01fSNick Piggin /* 25861acf0af9SDavid Howells * Look up and maybe create and open the last component. 2587d58ffd35SMiklos Szeredi * 2588d58ffd35SMiklos Szeredi * Must be called with i_mutex held on parent. 2589d58ffd35SMiklos Szeredi * 25901acf0af9SDavid Howells * Returns 0 if the file was successfully atomically created (if necessary) and 25911acf0af9SDavid Howells * opened. In this case the file will be returned attached to @file. 25921acf0af9SDavid Howells * 25931acf0af9SDavid Howells * Returns 1 if the file was not completely opened at this time, though lookups 25941acf0af9SDavid Howells * and creations will have been performed and the dentry returned in @path will 25951acf0af9SDavid Howells * be positive upon return if O_CREAT was specified. If O_CREAT wasn't 25961acf0af9SDavid Howells * specified then a negative dentry may be returned. 25971acf0af9SDavid Howells * 25981acf0af9SDavid Howells * An error code is returned otherwise. 25991acf0af9SDavid Howells * 26001acf0af9SDavid Howells * FILE_CREATE will be set in @*opened if the dentry was created and will be 26011acf0af9SDavid Howells * cleared otherwise prior to returning. 2602d58ffd35SMiklos Szeredi */ 26032675a4ebSAl Viro static int lookup_open(struct nameidata *nd, struct path *path, 260430d90494SAl Viro struct file *file, 2605d58ffd35SMiklos Szeredi const struct open_flags *op, 260664894cf8SAl Viro bool got_write, int *opened) 2607d58ffd35SMiklos Szeredi { 2608d58ffd35SMiklos Szeredi struct dentry *dir = nd->path.dentry; 260954ef4872SMiklos Szeredi struct inode *dir_inode = dir->d_inode; 2610d58ffd35SMiklos Szeredi struct dentry *dentry; 2611d58ffd35SMiklos Szeredi int error; 261254ef4872SMiklos Szeredi bool need_lookup; 2613d58ffd35SMiklos Szeredi 261447237687SAl Viro *opened &= ~FILE_CREATED; 2615201f956eSAl Viro dentry = lookup_dcache(&nd->last, dir, nd->flags, &need_lookup); 2616d58ffd35SMiklos Szeredi if (IS_ERR(dentry)) 26172675a4ebSAl Viro return PTR_ERR(dentry); 2618d58ffd35SMiklos Szeredi 2619d18e9008SMiklos Szeredi /* Cached positive dentry: will open in f_op->open */ 2620d18e9008SMiklos Szeredi if (!need_lookup && dentry->d_inode) 2621d18e9008SMiklos Szeredi goto out_no_open; 2622d18e9008SMiklos Szeredi 2623d18e9008SMiklos Szeredi if ((nd->flags & LOOKUP_OPEN) && dir_inode->i_op->atomic_open) { 262464894cf8SAl Viro return atomic_open(nd, dentry, path, file, op, got_write, 262547237687SAl Viro need_lookup, opened); 2626d18e9008SMiklos Szeredi } 2627d18e9008SMiklos Szeredi 262854ef4872SMiklos Szeredi if (need_lookup) { 262954ef4872SMiklos Szeredi BUG_ON(dentry->d_inode); 263054ef4872SMiklos Szeredi 263172bd866aSAl Viro dentry = lookup_real(dir_inode, dentry, nd->flags); 263254ef4872SMiklos Szeredi if (IS_ERR(dentry)) 26332675a4ebSAl Viro return PTR_ERR(dentry); 263454ef4872SMiklos Szeredi } 263554ef4872SMiklos Szeredi 2636d58ffd35SMiklos Szeredi /* Negative dentry, just create the file */ 2637d58ffd35SMiklos Szeredi if (!dentry->d_inode && (op->open_flag & O_CREAT)) { 2638d58ffd35SMiklos Szeredi umode_t mode = op->mode; 2639d58ffd35SMiklos Szeredi if (!IS_POSIXACL(dir->d_inode)) 2640d58ffd35SMiklos Szeredi mode &= ~current_umask(); 2641d58ffd35SMiklos Szeredi /* 2642d58ffd35SMiklos Szeredi * This write is needed to ensure that a 2643d58ffd35SMiklos Szeredi * rw->ro transition does not occur between 2644d58ffd35SMiklos Szeredi * the time when the file is created and when 2645d58ffd35SMiklos Szeredi * a permanent write count is taken through 2646015c3bbcSMiklos Szeredi * the 'struct file' in finish_open(). 2647d58ffd35SMiklos Szeredi */ 264864894cf8SAl Viro if (!got_write) { 264964894cf8SAl Viro error = -EROFS; 2650d58ffd35SMiklos Szeredi goto out_dput; 265164894cf8SAl Viro } 265247237687SAl Viro *opened |= FILE_CREATED; 2653d58ffd35SMiklos Szeredi error = security_path_mknod(&nd->path, dentry, mode, 0); 2654d58ffd35SMiklos Szeredi if (error) 2655d58ffd35SMiklos Szeredi goto out_dput; 2656312b63fbSAl Viro error = vfs_create(dir->d_inode, dentry, mode, 2657312b63fbSAl Viro nd->flags & LOOKUP_EXCL); 2658d58ffd35SMiklos Szeredi if (error) 2659d58ffd35SMiklos Szeredi goto out_dput; 2660d58ffd35SMiklos Szeredi } 2661d18e9008SMiklos Szeredi out_no_open: 2662d58ffd35SMiklos Szeredi path->dentry = dentry; 2663d58ffd35SMiklos Szeredi path->mnt = nd->path.mnt; 26642675a4ebSAl Viro return 1; 2665d58ffd35SMiklos Szeredi 2666d58ffd35SMiklos Szeredi out_dput: 2667d58ffd35SMiklos Szeredi dput(dentry); 26682675a4ebSAl Viro return error; 2669d58ffd35SMiklos Szeredi } 2670d58ffd35SMiklos Szeredi 2671d58ffd35SMiklos Szeredi /* 2672fe2d35ffSAl Viro * Handle the last step of open() 267331e6b01fSNick Piggin */ 26742675a4ebSAl Viro static int do_last(struct nameidata *nd, struct path *path, 267530d90494SAl Viro struct file *file, const struct open_flags *op, 2676669abf4eSJeff Layton int *opened, struct filename *name) 2677fb1cc555SAl Viro { 2678a1e28038SAl Viro struct dentry *dir = nd->path.dentry; 2679ca344a89SAl Viro int open_flag = op->open_flag; 268077d660a8SMiklos Szeredi bool will_truncate = (open_flag & O_TRUNC) != 0; 268164894cf8SAl Viro bool got_write = false; 2682bcda7652SAl Viro int acc_mode = op->acc_mode; 2683a1eb3315SMiklos Szeredi struct inode *inode; 268477d660a8SMiklos Szeredi bool symlink_ok = false; 268516b1c1cdSMiklos Szeredi struct path save_parent = { .dentry = NULL, .mnt = NULL }; 268616b1c1cdSMiklos Szeredi bool retried = false; 268716c2cd71SAl Viro int error; 2688fb1cc555SAl Viro 2689c3e380b0SAl Viro nd->flags &= ~LOOKUP_PARENT; 2690c3e380b0SAl Viro nd->flags |= op->intent; 2691c3e380b0SAl Viro 2692bc77daa7SAl Viro if (nd->last_type != LAST_NORM) { 2693fe2d35ffSAl Viro error = handle_dots(nd, nd->last_type); 2694fe2d35ffSAl Viro if (error) 26952675a4ebSAl Viro return error; 2696e83db167SMiklos Szeredi goto finish_open; 26971f36f774SAl Viro } 2698a2c36b45SAl Viro 2699ca344a89SAl Viro if (!(open_flag & O_CREAT)) { 2700fe2d35ffSAl Viro if (nd->last.name[nd->last.len]) 2701fe2d35ffSAl Viro nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 2702bcda7652SAl Viro if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW)) 270377d660a8SMiklos Szeredi symlink_ok = true; 2704fe2d35ffSAl Viro /* we _can_ be in RCU mode here */ 2705e97cdc87SAl Viro error = lookup_fast(nd, path, &inode); 270671574865SMiklos Szeredi if (likely(!error)) 270771574865SMiklos Szeredi goto finish_lookup; 270871574865SMiklos Szeredi 2709ce57dfc1SAl Viro if (error < 0) 27102675a4ebSAl Viro goto out; 2711a1eb3315SMiklos Szeredi 271237d7fffcSMiklos Szeredi BUG_ON(nd->inode != dir->d_inode); 2713b6183df7SMiklos Szeredi } else { 2714fe2d35ffSAl Viro /* create side of things */ 2715a3fbbde7SAl Viro /* 2716b6183df7SMiklos Szeredi * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED 2717b6183df7SMiklos Szeredi * has been cleared when we got to the last component we are 2718b6183df7SMiklos Szeredi * about to look up 2719a3fbbde7SAl Viro */ 27209f1fafeeSAl Viro error = complete_walk(nd); 27219f1fafeeSAl Viro if (error) 27222675a4ebSAl Viro return error; 2723fe2d35ffSAl Viro 272433e2208aSJeff Layton audit_inode(name, dir, LOOKUP_PARENT); 272516c2cd71SAl Viro error = -EISDIR; 27261f36f774SAl Viro /* trailing slashes? */ 272731e6b01fSNick Piggin if (nd->last.name[nd->last.len]) 27282675a4ebSAl Viro goto out; 2729b6183df7SMiklos Szeredi } 27301f36f774SAl Viro 273116b1c1cdSMiklos Szeredi retry_lookup: 273264894cf8SAl Viro if (op->open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) { 273364894cf8SAl Viro error = mnt_want_write(nd->path.mnt); 273464894cf8SAl Viro if (!error) 273564894cf8SAl Viro got_write = true; 273664894cf8SAl Viro /* 273764894cf8SAl Viro * do _not_ fail yet - we might not need that or fail with 273864894cf8SAl Viro * a different error; let lookup_open() decide; we'll be 273964894cf8SAl Viro * dropping this one anyway. 274064894cf8SAl Viro */ 274164894cf8SAl Viro } 2742a1e28038SAl Viro mutex_lock(&dir->d_inode->i_mutex); 274364894cf8SAl Viro error = lookup_open(nd, path, file, op, got_write, opened); 2744fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2745fb1cc555SAl Viro 27462675a4ebSAl Viro if (error <= 0) { 27472675a4ebSAl Viro if (error) 2748d58ffd35SMiklos Szeredi goto out; 27496c0d46c4SAl Viro 275047237687SAl Viro if ((*opened & FILE_CREATED) || 2751496ad9aaSAl Viro !S_ISREG(file_inode(file)->i_mode)) 275277d660a8SMiklos Szeredi will_truncate = false; 2753d18e9008SMiklos Szeredi 2754adb5c247SJeff Layton audit_inode(name, file->f_path.dentry, 0); 2755d18e9008SMiklos Szeredi goto opened; 2756d18e9008SMiklos Szeredi } 2757d18e9008SMiklos Szeredi 275847237687SAl Viro if (*opened & FILE_CREATED) { 27599b44f1b3SAl Viro /* Don't check for write permission, don't truncate */ 2760ca344a89SAl Viro open_flag &= ~O_TRUNC; 276177d660a8SMiklos Szeredi will_truncate = false; 2762bcda7652SAl Viro acc_mode = MAY_OPEN; 2763d58ffd35SMiklos Szeredi path_to_nameidata(path, nd); 2764e83db167SMiklos Szeredi goto finish_open_created; 2765fb1cc555SAl Viro } 2766fb1cc555SAl Viro 2767fb1cc555SAl Viro /* 27683134f37eSJeff Layton * create/update audit record if it already exists. 2769fb1cc555SAl Viro */ 27703134f37eSJeff Layton if (path->dentry->d_inode) 2771adb5c247SJeff Layton audit_inode(name, path->dentry, 0); 2772fb1cc555SAl Viro 2773d18e9008SMiklos Szeredi /* 2774d18e9008SMiklos Szeredi * If atomic_open() acquired write access it is dropped now due to 2775d18e9008SMiklos Szeredi * possible mount and symlink following (this might be optimized away if 2776d18e9008SMiklos Szeredi * necessary...) 2777d18e9008SMiklos Szeredi */ 277864894cf8SAl Viro if (got_write) { 2779d18e9008SMiklos Szeredi mnt_drop_write(nd->path.mnt); 278064894cf8SAl Viro got_write = false; 2781d18e9008SMiklos Szeredi } 2782d18e9008SMiklos Szeredi 2783fb1cc555SAl Viro error = -EEXIST; 2784f8310c59SAl Viro if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT)) 2785fb1cc555SAl Viro goto exit_dput; 2786fb1cc555SAl Viro 27879875cf80SDavid Howells error = follow_managed(path, nd->flags); 27889875cf80SDavid Howells if (error < 0) 2789fb1cc555SAl Viro goto exit_dput; 2790fb1cc555SAl Viro 2791a3fbbde7SAl Viro if (error) 2792a3fbbde7SAl Viro nd->flags |= LOOKUP_JUMPED; 2793a3fbbde7SAl Viro 2794decf3400SMiklos Szeredi BUG_ON(nd->flags & LOOKUP_RCU); 2795decf3400SMiklos Szeredi inode = path->dentry->d_inode; 27965f5daac1SMiklos Szeredi finish_lookup: 27975f5daac1SMiklos Szeredi /* we _can_ be in RCU mode here */ 2798fb1cc555SAl Viro error = -ENOENT; 279954c33e7fSMiklos Szeredi if (!inode) { 280054c33e7fSMiklos Szeredi path_to_nameidata(path, nd); 28012675a4ebSAl Viro goto out; 280254c33e7fSMiklos Szeredi } 28039e67f361SAl Viro 2804d45ea867SMiklos Szeredi if (should_follow_link(inode, !symlink_ok)) { 2805d45ea867SMiklos Szeredi if (nd->flags & LOOKUP_RCU) { 2806d45ea867SMiklos Szeredi if (unlikely(unlazy_walk(nd, path->dentry))) { 2807d45ea867SMiklos Szeredi error = -ECHILD; 28082675a4ebSAl Viro goto out; 2809d45ea867SMiklos Szeredi } 2810d45ea867SMiklos Szeredi } 2811d45ea867SMiklos Szeredi BUG_ON(inode != path->dentry->d_inode); 28122675a4ebSAl Viro return 1; 2813d45ea867SMiklos Szeredi } 2814fb1cc555SAl Viro 281516b1c1cdSMiklos Szeredi if ((nd->flags & LOOKUP_RCU) || nd->path.mnt != path->mnt) { 2816fb1cc555SAl Viro path_to_nameidata(path, nd); 281716b1c1cdSMiklos Szeredi } else { 281816b1c1cdSMiklos Szeredi save_parent.dentry = nd->path.dentry; 281916b1c1cdSMiklos Szeredi save_parent.mnt = mntget(path->mnt); 282016b1c1cdSMiklos Szeredi nd->path.dentry = path->dentry; 282116b1c1cdSMiklos Szeredi 282216b1c1cdSMiklos Szeredi } 2823decf3400SMiklos Szeredi nd->inode = inode; 2824a3fbbde7SAl Viro /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */ 2825bc77daa7SAl Viro finish_open: 2826a3fbbde7SAl Viro error = complete_walk(nd); 282716b1c1cdSMiklos Szeredi if (error) { 282816b1c1cdSMiklos Szeredi path_put(&save_parent); 28292675a4ebSAl Viro return error; 283016b1c1cdSMiklos Szeredi } 2831bc77daa7SAl Viro audit_inode(name, nd->path.dentry, 0); 2832fb1cc555SAl Viro error = -EISDIR; 2833050ac841SMiklos Szeredi if ((open_flag & O_CREAT) && S_ISDIR(nd->inode->i_mode)) 28342675a4ebSAl Viro goto out; 2835af2f5542SMiklos Szeredi error = -ENOTDIR; 283605252901SAl Viro if ((nd->flags & LOOKUP_DIRECTORY) && !can_lookup(nd->inode)) 28372675a4ebSAl Viro goto out; 28386c0d46c4SAl Viro if (!S_ISREG(nd->inode->i_mode)) 283977d660a8SMiklos Szeredi will_truncate = false; 28406c0d46c4SAl Viro 28410f9d1a10SAl Viro if (will_truncate) { 28420f9d1a10SAl Viro error = mnt_want_write(nd->path.mnt); 28430f9d1a10SAl Viro if (error) 28442675a4ebSAl Viro goto out; 284564894cf8SAl Viro got_write = true; 28460f9d1a10SAl Viro } 2847e83db167SMiklos Szeredi finish_open_created: 2848bcda7652SAl Viro error = may_open(&nd->path, acc_mode, open_flag); 2849ca344a89SAl Viro if (error) 28502675a4ebSAl Viro goto out; 285130d90494SAl Viro file->f_path.mnt = nd->path.mnt; 285230d90494SAl Viro error = finish_open(file, nd->path.dentry, NULL, opened); 285330d90494SAl Viro if (error) { 285430d90494SAl Viro if (error == -EOPENSTALE) 2855f60dc3dbSMiklos Szeredi goto stale_open; 2856015c3bbcSMiklos Szeredi goto out; 2857f60dc3dbSMiklos Szeredi } 2858a8277b9bSMiklos Szeredi opened: 28592675a4ebSAl Viro error = open_check_o_direct(file); 2860015c3bbcSMiklos Szeredi if (error) 2861015c3bbcSMiklos Szeredi goto exit_fput; 28622675a4ebSAl Viro error = ima_file_check(file, op->acc_mode); 2863aa4caadbSMiklos Szeredi if (error) 2864aa4caadbSMiklos Szeredi goto exit_fput; 2865aa4caadbSMiklos Szeredi 28660f9d1a10SAl Viro if (will_truncate) { 28672675a4ebSAl Viro error = handle_truncate(file); 2868aa4caadbSMiklos Szeredi if (error) 2869aa4caadbSMiklos Szeredi goto exit_fput; 28700f9d1a10SAl Viro } 2871ca344a89SAl Viro out: 287264894cf8SAl Viro if (got_write) 28730f9d1a10SAl Viro mnt_drop_write(nd->path.mnt); 287416b1c1cdSMiklos Szeredi path_put(&save_parent); 2875e276ae67SMiklos Szeredi terminate_walk(nd); 28762675a4ebSAl Viro return error; 2877fb1cc555SAl Viro 2878fb1cc555SAl Viro exit_dput: 2879fb1cc555SAl Viro path_put_conditional(path, nd); 2880ca344a89SAl Viro goto out; 2881015c3bbcSMiklos Szeredi exit_fput: 28822675a4ebSAl Viro fput(file); 28832675a4ebSAl Viro goto out; 2884015c3bbcSMiklos Szeredi 2885f60dc3dbSMiklos Szeredi stale_open: 2886f60dc3dbSMiklos Szeredi /* If no saved parent or already retried then can't retry */ 2887f60dc3dbSMiklos Szeredi if (!save_parent.dentry || retried) 2888f60dc3dbSMiklos Szeredi goto out; 2889f60dc3dbSMiklos Szeredi 2890f60dc3dbSMiklos Szeredi BUG_ON(save_parent.dentry != dir); 2891f60dc3dbSMiklos Szeredi path_put(&nd->path); 2892f60dc3dbSMiklos Szeredi nd->path = save_parent; 2893f60dc3dbSMiklos Szeredi nd->inode = dir->d_inode; 2894f60dc3dbSMiklos Szeredi save_parent.mnt = NULL; 2895f60dc3dbSMiklos Szeredi save_parent.dentry = NULL; 289664894cf8SAl Viro if (got_write) { 2897f60dc3dbSMiklos Szeredi mnt_drop_write(nd->path.mnt); 289864894cf8SAl Viro got_write = false; 2899f60dc3dbSMiklos Szeredi } 2900f60dc3dbSMiklos Szeredi retried = true; 2901f60dc3dbSMiklos Szeredi goto retry_lookup; 2902fb1cc555SAl Viro } 2903fb1cc555SAl Viro 290460545d0dSAl Viro static int do_tmpfile(int dfd, struct filename *pathname, 290560545d0dSAl Viro struct nameidata *nd, int flags, 290660545d0dSAl Viro const struct open_flags *op, 290760545d0dSAl Viro struct file *file, int *opened) 290860545d0dSAl Viro { 290960545d0dSAl Viro static const struct qstr name = QSTR_INIT("/", 1); 291060545d0dSAl Viro struct dentry *dentry, *child; 291160545d0dSAl Viro struct inode *dir; 291260545d0dSAl Viro int error = path_lookupat(dfd, pathname->name, 291360545d0dSAl Viro flags | LOOKUP_DIRECTORY, nd); 291460545d0dSAl Viro if (unlikely(error)) 291560545d0dSAl Viro return error; 291660545d0dSAl Viro error = mnt_want_write(nd->path.mnt); 291760545d0dSAl Viro if (unlikely(error)) 291860545d0dSAl Viro goto out; 291960545d0dSAl Viro /* we want directory to be writable */ 292060545d0dSAl Viro error = inode_permission(nd->inode, MAY_WRITE | MAY_EXEC); 292160545d0dSAl Viro if (error) 292260545d0dSAl Viro goto out2; 292360545d0dSAl Viro dentry = nd->path.dentry; 292460545d0dSAl Viro dir = dentry->d_inode; 292560545d0dSAl Viro if (!dir->i_op->tmpfile) { 292660545d0dSAl Viro error = -EOPNOTSUPP; 292760545d0dSAl Viro goto out2; 292860545d0dSAl Viro } 292960545d0dSAl Viro child = d_alloc(dentry, &name); 293060545d0dSAl Viro if (unlikely(!child)) { 293160545d0dSAl Viro error = -ENOMEM; 293260545d0dSAl Viro goto out2; 293360545d0dSAl Viro } 293460545d0dSAl Viro nd->flags &= ~LOOKUP_DIRECTORY; 293560545d0dSAl Viro nd->flags |= op->intent; 293660545d0dSAl Viro dput(nd->path.dentry); 293760545d0dSAl Viro nd->path.dentry = child; 293860545d0dSAl Viro error = dir->i_op->tmpfile(dir, nd->path.dentry, op->mode); 293960545d0dSAl Viro if (error) 294060545d0dSAl Viro goto out2; 294160545d0dSAl Viro audit_inode(pathname, nd->path.dentry, 0); 294260545d0dSAl Viro error = may_open(&nd->path, op->acc_mode, op->open_flag); 294360545d0dSAl Viro if (error) 294460545d0dSAl Viro goto out2; 294560545d0dSAl Viro file->f_path.mnt = nd->path.mnt; 294660545d0dSAl Viro error = finish_open(file, nd->path.dentry, NULL, opened); 294760545d0dSAl Viro if (error) 294860545d0dSAl Viro goto out2; 294960545d0dSAl Viro error = open_check_o_direct(file); 2950f4e0c30cSAl Viro if (error) { 295160545d0dSAl Viro fput(file); 2952f4e0c30cSAl Viro } else if (!(op->open_flag & O_EXCL)) { 2953f4e0c30cSAl Viro struct inode *inode = file_inode(file); 2954f4e0c30cSAl Viro spin_lock(&inode->i_lock); 2955f4e0c30cSAl Viro inode->i_state |= I_LINKABLE; 2956f4e0c30cSAl Viro spin_unlock(&inode->i_lock); 2957f4e0c30cSAl Viro } 295860545d0dSAl Viro out2: 295960545d0dSAl Viro mnt_drop_write(nd->path.mnt); 296060545d0dSAl Viro out: 296160545d0dSAl Viro path_put(&nd->path); 296260545d0dSAl Viro return error; 296360545d0dSAl Viro } 296460545d0dSAl Viro 2965669abf4eSJeff Layton static struct file *path_openat(int dfd, struct filename *pathname, 296673d049a4SAl Viro struct nameidata *nd, const struct open_flags *op, int flags) 29671da177e4SLinus Torvalds { 2968fe2d35ffSAl Viro struct file *base = NULL; 296930d90494SAl Viro struct file *file; 29709850c056SAl Viro struct path path; 297147237687SAl Viro int opened = 0; 297213aab428SAl Viro int error; 297331e6b01fSNick Piggin 297430d90494SAl Viro file = get_empty_filp(); 29751afc99beSAl Viro if (IS_ERR(file)) 29761afc99beSAl Viro return file; 297731e6b01fSNick Piggin 297830d90494SAl Viro file->f_flags = op->open_flag; 297931e6b01fSNick Piggin 2980bb458c64SAl Viro if (unlikely(file->f_flags & __O_TMPFILE)) { 298160545d0dSAl Viro error = do_tmpfile(dfd, pathname, nd, flags, op, file, &opened); 298260545d0dSAl Viro goto out; 298360545d0dSAl Viro } 298460545d0dSAl Viro 2985669abf4eSJeff Layton error = path_init(dfd, pathname->name, flags | LOOKUP_PARENT, nd, &base); 298631e6b01fSNick Piggin if (unlikely(error)) 29872675a4ebSAl Viro goto out; 298831e6b01fSNick Piggin 2989fe2d35ffSAl Viro current->total_link_count = 0; 2990669abf4eSJeff Layton error = link_path_walk(pathname->name, nd); 299131e6b01fSNick Piggin if (unlikely(error)) 29922675a4ebSAl Viro goto out; 29931da177e4SLinus Torvalds 29942675a4ebSAl Viro error = do_last(nd, &path, file, op, &opened, pathname); 29952675a4ebSAl Viro while (unlikely(error > 0)) { /* trailing symlink */ 29967b9337aaSNick Piggin struct path link = path; 2997def4af30SAl Viro void *cookie; 2998574197e0SAl Viro if (!(nd->flags & LOOKUP_FOLLOW)) { 299973d049a4SAl Viro path_put_conditional(&path, nd); 300073d049a4SAl Viro path_put(&nd->path); 30012675a4ebSAl Viro error = -ELOOP; 300240b39136SAl Viro break; 300340b39136SAl Viro } 3004800179c9SKees Cook error = may_follow_link(&link, nd); 3005800179c9SKees Cook if (unlikely(error)) 3006800179c9SKees Cook break; 300773d049a4SAl Viro nd->flags |= LOOKUP_PARENT; 300873d049a4SAl Viro nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL); 3009574197e0SAl Viro error = follow_link(&link, nd, &cookie); 3010c3e380b0SAl Viro if (unlikely(error)) 30112675a4ebSAl Viro break; 30122675a4ebSAl Viro error = do_last(nd, &path, file, op, &opened, pathname); 3013574197e0SAl Viro put_link(nd, &link, cookie); 3014806b681cSAl Viro } 301510fa8e62SAl Viro out: 301673d049a4SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) 301773d049a4SAl Viro path_put(&nd->root); 3018fe2d35ffSAl Viro if (base) 3019fe2d35ffSAl Viro fput(base); 30202675a4ebSAl Viro if (!(opened & FILE_OPENED)) { 30212675a4ebSAl Viro BUG_ON(!error); 302230d90494SAl Viro put_filp(file); 3023015c3bbcSMiklos Szeredi } 30242675a4ebSAl Viro if (unlikely(error)) { 30252675a4ebSAl Viro if (error == -EOPENSTALE) { 30262675a4ebSAl Viro if (flags & LOOKUP_RCU) 30272675a4ebSAl Viro error = -ECHILD; 30282675a4ebSAl Viro else 30292675a4ebSAl Viro error = -ESTALE; 30302675a4ebSAl Viro } 30312675a4ebSAl Viro file = ERR_PTR(error); 30322675a4ebSAl Viro } 30332675a4ebSAl Viro return file; 3034de459215SKirill Korotaev } 30351da177e4SLinus Torvalds 3036669abf4eSJeff Layton struct file *do_filp_open(int dfd, struct filename *pathname, 3037f9652e10SAl Viro const struct open_flags *op) 303813aab428SAl Viro { 303973d049a4SAl Viro struct nameidata nd; 3040f9652e10SAl Viro int flags = op->lookup_flags; 304113aab428SAl Viro struct file *filp; 304213aab428SAl Viro 304373d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU); 304413aab428SAl Viro if (unlikely(filp == ERR_PTR(-ECHILD))) 304573d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags); 304613aab428SAl Viro if (unlikely(filp == ERR_PTR(-ESTALE))) 304773d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL); 304813aab428SAl Viro return filp; 304913aab428SAl Viro } 305013aab428SAl Viro 305173d049a4SAl Viro struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt, 3052f9652e10SAl Viro const char *name, const struct open_flags *op) 305373d049a4SAl Viro { 305473d049a4SAl Viro struct nameidata nd; 305573d049a4SAl Viro struct file *file; 3056669abf4eSJeff Layton struct filename filename = { .name = name }; 3057f9652e10SAl Viro int flags = op->lookup_flags | LOOKUP_ROOT; 305873d049a4SAl Viro 305973d049a4SAl Viro nd.root.mnt = mnt; 306073d049a4SAl Viro nd.root.dentry = dentry; 306173d049a4SAl Viro 3062bcda7652SAl Viro if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN) 306373d049a4SAl Viro return ERR_PTR(-ELOOP); 306473d049a4SAl Viro 3065669abf4eSJeff Layton file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_RCU); 306673d049a4SAl Viro if (unlikely(file == ERR_PTR(-ECHILD))) 3067669abf4eSJeff Layton file = path_openat(-1, &filename, &nd, op, flags); 306873d049a4SAl Viro if (unlikely(file == ERR_PTR(-ESTALE))) 3069669abf4eSJeff Layton file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_REVAL); 307073d049a4SAl Viro return file; 307173d049a4SAl Viro } 307273d049a4SAl Viro 30731ac12b4bSJeff Layton struct dentry *kern_path_create(int dfd, const char *pathname, 30741ac12b4bSJeff Layton struct path *path, unsigned int lookup_flags) 30751da177e4SLinus Torvalds { 3076c663e5d8SChristoph Hellwig struct dentry *dentry = ERR_PTR(-EEXIST); 3077ed75e95dSAl Viro struct nameidata nd; 3078c30dabfeSJan Kara int err2; 30791ac12b4bSJeff Layton int error; 30801ac12b4bSJeff Layton bool is_dir = (lookup_flags & LOOKUP_DIRECTORY); 30811ac12b4bSJeff Layton 30821ac12b4bSJeff Layton /* 30831ac12b4bSJeff Layton * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any 30841ac12b4bSJeff Layton * other flags passed in are ignored! 30851ac12b4bSJeff Layton */ 30861ac12b4bSJeff Layton lookup_flags &= LOOKUP_REVAL; 30871ac12b4bSJeff Layton 30881ac12b4bSJeff Layton error = do_path_lookup(dfd, pathname, LOOKUP_PARENT|lookup_flags, &nd); 3089ed75e95dSAl Viro if (error) 3090ed75e95dSAl Viro return ERR_PTR(error); 30911da177e4SLinus Torvalds 3092c663e5d8SChristoph Hellwig /* 3093c663e5d8SChristoph Hellwig * Yucky last component or no last component at all? 3094c663e5d8SChristoph Hellwig * (foo/., foo/.., /////) 3095c663e5d8SChristoph Hellwig */ 3096ed75e95dSAl Viro if (nd.last_type != LAST_NORM) 3097ed75e95dSAl Viro goto out; 3098ed75e95dSAl Viro nd.flags &= ~LOOKUP_PARENT; 3099ed75e95dSAl Viro nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL; 3100c663e5d8SChristoph Hellwig 3101c30dabfeSJan Kara /* don't fail immediately if it's r/o, at least try to report other errors */ 3102c30dabfeSJan Kara err2 = mnt_want_write(nd.path.mnt); 3103c663e5d8SChristoph Hellwig /* 3104c663e5d8SChristoph Hellwig * Do the final lookup. 3105c663e5d8SChristoph Hellwig */ 3106ed75e95dSAl Viro mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 3107ed75e95dSAl Viro dentry = lookup_hash(&nd); 31081da177e4SLinus Torvalds if (IS_ERR(dentry)) 3109a8104a9fSAl Viro goto unlock; 3110c663e5d8SChristoph Hellwig 3111a8104a9fSAl Viro error = -EEXIST; 3112e9baf6e5SAl Viro if (dentry->d_inode) 3113a8104a9fSAl Viro goto fail; 3114c663e5d8SChristoph Hellwig /* 3115c663e5d8SChristoph Hellwig * Special case - lookup gave negative, but... we had foo/bar/ 3116c663e5d8SChristoph Hellwig * From the vfs_mknod() POV we just have a negative dentry - 3117c663e5d8SChristoph Hellwig * all is fine. Let's be bastards - you had / on the end, you've 3118c663e5d8SChristoph Hellwig * been asking for (non-existent) directory. -ENOENT for you. 3119c663e5d8SChristoph Hellwig */ 3120ed75e95dSAl Viro if (unlikely(!is_dir && nd.last.name[nd.last.len])) { 3121a8104a9fSAl Viro error = -ENOENT; 3122ed75e95dSAl Viro goto fail; 3123e9baf6e5SAl Viro } 3124c30dabfeSJan Kara if (unlikely(err2)) { 3125c30dabfeSJan Kara error = err2; 3126a8104a9fSAl Viro goto fail; 3127c30dabfeSJan Kara } 3128ed75e95dSAl Viro *path = nd.path; 3129e9baf6e5SAl Viro return dentry; 31301da177e4SLinus Torvalds fail: 3131a8104a9fSAl Viro dput(dentry); 3132a8104a9fSAl Viro dentry = ERR_PTR(error); 3133a8104a9fSAl Viro unlock: 3134dae6ad8fSAl Viro mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 3135c30dabfeSJan Kara if (!err2) 3136c30dabfeSJan Kara mnt_drop_write(nd.path.mnt); 3137ed75e95dSAl Viro out: 3138dae6ad8fSAl Viro path_put(&nd.path); 3139ed75e95dSAl Viro return dentry; 3140dae6ad8fSAl Viro } 3141dae6ad8fSAl Viro EXPORT_SYMBOL(kern_path_create); 3142dae6ad8fSAl Viro 3143921a1650SAl Viro void done_path_create(struct path *path, struct dentry *dentry) 3144921a1650SAl Viro { 3145921a1650SAl Viro dput(dentry); 3146921a1650SAl Viro mutex_unlock(&path->dentry->d_inode->i_mutex); 3147a8104a9fSAl Viro mnt_drop_write(path->mnt); 3148921a1650SAl Viro path_put(path); 3149921a1650SAl Viro } 3150921a1650SAl Viro EXPORT_SYMBOL(done_path_create); 3151921a1650SAl Viro 31521ac12b4bSJeff Layton struct dentry *user_path_create(int dfd, const char __user *pathname, 31531ac12b4bSJeff Layton struct path *path, unsigned int lookup_flags) 3154dae6ad8fSAl Viro { 315591a27b2aSJeff Layton struct filename *tmp = getname(pathname); 3156dae6ad8fSAl Viro struct dentry *res; 3157dae6ad8fSAl Viro if (IS_ERR(tmp)) 3158dae6ad8fSAl Viro return ERR_CAST(tmp); 31591ac12b4bSJeff Layton res = kern_path_create(dfd, tmp->name, path, lookup_flags); 3160dae6ad8fSAl Viro putname(tmp); 3161dae6ad8fSAl Viro return res; 3162dae6ad8fSAl Viro } 3163dae6ad8fSAl Viro EXPORT_SYMBOL(user_path_create); 3164dae6ad8fSAl Viro 31651a67aafbSAl Viro int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev) 31661da177e4SLinus Torvalds { 3167a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 31681da177e4SLinus Torvalds 31691da177e4SLinus Torvalds if (error) 31701da177e4SLinus Torvalds return error; 31711da177e4SLinus Torvalds 3172975d6b39SEric W. Biederman if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD)) 31731da177e4SLinus Torvalds return -EPERM; 31741da177e4SLinus Torvalds 3175acfa4380SAl Viro if (!dir->i_op->mknod) 31761da177e4SLinus Torvalds return -EPERM; 31771da177e4SLinus Torvalds 317808ce5f16SSerge E. Hallyn error = devcgroup_inode_mknod(mode, dev); 317908ce5f16SSerge E. Hallyn if (error) 318008ce5f16SSerge E. Hallyn return error; 318108ce5f16SSerge E. Hallyn 31821da177e4SLinus Torvalds error = security_inode_mknod(dir, dentry, mode, dev); 31831da177e4SLinus Torvalds if (error) 31841da177e4SLinus Torvalds return error; 31851da177e4SLinus Torvalds 31861da177e4SLinus Torvalds error = dir->i_op->mknod(dir, dentry, mode, dev); 3187a74574aaSStephen Smalley if (!error) 3188f38aa942SAmy Griffis fsnotify_create(dir, dentry); 31891da177e4SLinus Torvalds return error; 31901da177e4SLinus Torvalds } 31911da177e4SLinus Torvalds 3192f69aac00SAl Viro static int may_mknod(umode_t mode) 3193463c3197SDave Hansen { 3194463c3197SDave Hansen switch (mode & S_IFMT) { 3195463c3197SDave Hansen case S_IFREG: 3196463c3197SDave Hansen case S_IFCHR: 3197463c3197SDave Hansen case S_IFBLK: 3198463c3197SDave Hansen case S_IFIFO: 3199463c3197SDave Hansen case S_IFSOCK: 3200463c3197SDave Hansen case 0: /* zero mode translates to S_IFREG */ 3201463c3197SDave Hansen return 0; 3202463c3197SDave Hansen case S_IFDIR: 3203463c3197SDave Hansen return -EPERM; 3204463c3197SDave Hansen default: 3205463c3197SDave Hansen return -EINVAL; 3206463c3197SDave Hansen } 3207463c3197SDave Hansen } 3208463c3197SDave Hansen 32098208a22bSAl Viro SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode, 32102e4d0924SHeiko Carstens unsigned, dev) 32111da177e4SLinus Torvalds { 32121da177e4SLinus Torvalds struct dentry *dentry; 3213dae6ad8fSAl Viro struct path path; 3214dae6ad8fSAl Viro int error; 3215972567f1SJeff Layton unsigned int lookup_flags = 0; 32161da177e4SLinus Torvalds 32178e4bfca1SAl Viro error = may_mknod(mode); 32188e4bfca1SAl Viro if (error) 32198e4bfca1SAl Viro return error; 3220972567f1SJeff Layton retry: 3221972567f1SJeff Layton dentry = user_path_create(dfd, filename, &path, lookup_flags); 3222dae6ad8fSAl Viro if (IS_ERR(dentry)) 3223dae6ad8fSAl Viro return PTR_ERR(dentry); 32242ad94ae6SAl Viro 3225dae6ad8fSAl Viro if (!IS_POSIXACL(path.dentry->d_inode)) 3226ce3b0f8dSAl Viro mode &= ~current_umask(); 3227dae6ad8fSAl Viro error = security_path_mknod(&path, dentry, mode, dev); 3228be6d3e56SKentaro Takeda if (error) 3229a8104a9fSAl Viro goto out; 32301da177e4SLinus Torvalds switch (mode & S_IFMT) { 32311da177e4SLinus Torvalds case 0: case S_IFREG: 3232312b63fbSAl Viro error = vfs_create(path.dentry->d_inode,dentry,mode,true); 32331da177e4SLinus Torvalds break; 32341da177e4SLinus Torvalds case S_IFCHR: case S_IFBLK: 3235dae6ad8fSAl Viro error = vfs_mknod(path.dentry->d_inode,dentry,mode, 32361da177e4SLinus Torvalds new_decode_dev(dev)); 32371da177e4SLinus Torvalds break; 32381da177e4SLinus Torvalds case S_IFIFO: case S_IFSOCK: 3239dae6ad8fSAl Viro error = vfs_mknod(path.dentry->d_inode,dentry,mode,0); 32401da177e4SLinus Torvalds break; 32411da177e4SLinus Torvalds } 3242a8104a9fSAl Viro out: 3243921a1650SAl Viro done_path_create(&path, dentry); 3244972567f1SJeff Layton if (retry_estale(error, lookup_flags)) { 3245972567f1SJeff Layton lookup_flags |= LOOKUP_REVAL; 3246972567f1SJeff Layton goto retry; 3247972567f1SJeff Layton } 32481da177e4SLinus Torvalds return error; 32491da177e4SLinus Torvalds } 32501da177e4SLinus Torvalds 32518208a22bSAl Viro SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev) 32525590ff0dSUlrich Drepper { 32535590ff0dSUlrich Drepper return sys_mknodat(AT_FDCWD, filename, mode, dev); 32545590ff0dSUlrich Drepper } 32555590ff0dSUlrich Drepper 325618bb1db3SAl Viro int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) 32571da177e4SLinus Torvalds { 3258a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 32598de52778SAl Viro unsigned max_links = dir->i_sb->s_max_links; 32601da177e4SLinus Torvalds 32611da177e4SLinus Torvalds if (error) 32621da177e4SLinus Torvalds return error; 32631da177e4SLinus Torvalds 3264acfa4380SAl Viro if (!dir->i_op->mkdir) 32651da177e4SLinus Torvalds return -EPERM; 32661da177e4SLinus Torvalds 32671da177e4SLinus Torvalds mode &= (S_IRWXUGO|S_ISVTX); 32681da177e4SLinus Torvalds error = security_inode_mkdir(dir, dentry, mode); 32691da177e4SLinus Torvalds if (error) 32701da177e4SLinus Torvalds return error; 32711da177e4SLinus Torvalds 32728de52778SAl Viro if (max_links && dir->i_nlink >= max_links) 32738de52778SAl Viro return -EMLINK; 32748de52778SAl Viro 32751da177e4SLinus Torvalds error = dir->i_op->mkdir(dir, dentry, mode); 3276a74574aaSStephen Smalley if (!error) 3277f38aa942SAmy Griffis fsnotify_mkdir(dir, dentry); 32781da177e4SLinus Torvalds return error; 32791da177e4SLinus Torvalds } 32801da177e4SLinus Torvalds 3281a218d0fdSAl Viro SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode) 32821da177e4SLinus Torvalds { 32836902d925SDave Hansen struct dentry *dentry; 3284dae6ad8fSAl Viro struct path path; 3285dae6ad8fSAl Viro int error; 3286b76d8b82SJeff Layton unsigned int lookup_flags = LOOKUP_DIRECTORY; 32871da177e4SLinus Torvalds 3288b76d8b82SJeff Layton retry: 3289b76d8b82SJeff Layton dentry = user_path_create(dfd, pathname, &path, lookup_flags); 32906902d925SDave Hansen if (IS_ERR(dentry)) 3291dae6ad8fSAl Viro return PTR_ERR(dentry); 32926902d925SDave Hansen 3293dae6ad8fSAl Viro if (!IS_POSIXACL(path.dentry->d_inode)) 3294ce3b0f8dSAl Viro mode &= ~current_umask(); 3295dae6ad8fSAl Viro error = security_path_mkdir(&path, dentry, mode); 3296a8104a9fSAl Viro if (!error) 3297dae6ad8fSAl Viro error = vfs_mkdir(path.dentry->d_inode, dentry, mode); 3298921a1650SAl Viro done_path_create(&path, dentry); 3299b76d8b82SJeff Layton if (retry_estale(error, lookup_flags)) { 3300b76d8b82SJeff Layton lookup_flags |= LOOKUP_REVAL; 3301b76d8b82SJeff Layton goto retry; 3302b76d8b82SJeff Layton } 33031da177e4SLinus Torvalds return error; 33041da177e4SLinus Torvalds } 33051da177e4SLinus Torvalds 3306a218d0fdSAl Viro SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode) 33075590ff0dSUlrich Drepper { 33085590ff0dSUlrich Drepper return sys_mkdirat(AT_FDCWD, pathname, mode); 33095590ff0dSUlrich Drepper } 33105590ff0dSUlrich Drepper 33111da177e4SLinus Torvalds /* 3312a71905f0SSage Weil * The dentry_unhash() helper will try to drop the dentry early: we 3313c0d02594SJ. Bruce Fields * should have a usage count of 1 if we're the only user of this 3314a71905f0SSage Weil * dentry, and if that is true (possibly after pruning the dcache), 3315a71905f0SSage Weil * then we drop the dentry now. 33161da177e4SLinus Torvalds * 33171da177e4SLinus Torvalds * A low-level filesystem can, if it choses, legally 33181da177e4SLinus Torvalds * do a 33191da177e4SLinus Torvalds * 33201da177e4SLinus Torvalds * if (!d_unhashed(dentry)) 33211da177e4SLinus Torvalds * return -EBUSY; 33221da177e4SLinus Torvalds * 33231da177e4SLinus Torvalds * if it cannot handle the case of removing a directory 33241da177e4SLinus Torvalds * that is still in use by something else.. 33251da177e4SLinus Torvalds */ 33261da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry) 33271da177e4SLinus Torvalds { 33281da177e4SLinus Torvalds shrink_dcache_parent(dentry); 33291da177e4SLinus Torvalds spin_lock(&dentry->d_lock); 333064252c75SSage Weil if (dentry->d_count == 1) 33311da177e4SLinus Torvalds __d_drop(dentry); 33321da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 33331da177e4SLinus Torvalds } 33341da177e4SLinus Torvalds 33351da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry) 33361da177e4SLinus Torvalds { 33371da177e4SLinus Torvalds int error = may_delete(dir, dentry, 1); 33381da177e4SLinus Torvalds 33391da177e4SLinus Torvalds if (error) 33401da177e4SLinus Torvalds return error; 33411da177e4SLinus Torvalds 3342acfa4380SAl Viro if (!dir->i_op->rmdir) 33431da177e4SLinus Torvalds return -EPERM; 33441da177e4SLinus Torvalds 33451d2ef590SAl Viro dget(dentry); 33461b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 3347912dbc15SSage Weil 33481da177e4SLinus Torvalds error = -EBUSY; 3349912dbc15SSage Weil if (d_mountpoint(dentry)) 3350912dbc15SSage Weil goto out; 3351912dbc15SSage Weil 33521da177e4SLinus Torvalds error = security_inode_rmdir(dir, dentry); 3353912dbc15SSage Weil if (error) 3354912dbc15SSage Weil goto out; 3355912dbc15SSage Weil 33563cebde24SSage Weil shrink_dcache_parent(dentry); 33571da177e4SLinus Torvalds error = dir->i_op->rmdir(dir, dentry); 3358912dbc15SSage Weil if (error) 3359912dbc15SSage Weil goto out; 3360912dbc15SSage Weil 33611da177e4SLinus Torvalds dentry->d_inode->i_flags |= S_DEAD; 3362d83c49f3SAl Viro dont_mount(dentry); 33631da177e4SLinus Torvalds 3364912dbc15SSage Weil out: 3365912dbc15SSage Weil mutex_unlock(&dentry->d_inode->i_mutex); 33661d2ef590SAl Viro dput(dentry); 3367912dbc15SSage Weil if (!error) 3368912dbc15SSage Weil d_delete(dentry); 33691da177e4SLinus Torvalds return error; 33701da177e4SLinus Torvalds } 33711da177e4SLinus Torvalds 33725590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname) 33731da177e4SLinus Torvalds { 33741da177e4SLinus Torvalds int error = 0; 337591a27b2aSJeff Layton struct filename *name; 33761da177e4SLinus Torvalds struct dentry *dentry; 33771da177e4SLinus Torvalds struct nameidata nd; 3378c6ee9206SJeff Layton unsigned int lookup_flags = 0; 3379c6ee9206SJeff Layton retry: 3380c6ee9206SJeff Layton name = user_path_parent(dfd, pathname, &nd, lookup_flags); 338191a27b2aSJeff Layton if (IS_ERR(name)) 338291a27b2aSJeff Layton return PTR_ERR(name); 33831da177e4SLinus Torvalds 33841da177e4SLinus Torvalds switch(nd.last_type) { 33851da177e4SLinus Torvalds case LAST_DOTDOT: 33861da177e4SLinus Torvalds error = -ENOTEMPTY; 33871da177e4SLinus Torvalds goto exit1; 33881da177e4SLinus Torvalds case LAST_DOT: 33891da177e4SLinus Torvalds error = -EINVAL; 33901da177e4SLinus Torvalds goto exit1; 33911da177e4SLinus Torvalds case LAST_ROOT: 33921da177e4SLinus Torvalds error = -EBUSY; 33931da177e4SLinus Torvalds goto exit1; 33941da177e4SLinus Torvalds } 33950612d9fbSOGAWA Hirofumi 33960612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 3397c30dabfeSJan Kara error = mnt_want_write(nd.path.mnt); 3398c30dabfeSJan Kara if (error) 3399c30dabfeSJan Kara goto exit1; 34000612d9fbSOGAWA Hirofumi 34014ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 340249705b77SChristoph Hellwig dentry = lookup_hash(&nd); 34031da177e4SLinus Torvalds error = PTR_ERR(dentry); 34046902d925SDave Hansen if (IS_ERR(dentry)) 34056902d925SDave Hansen goto exit2; 3406e6bc45d6STheodore Ts'o if (!dentry->d_inode) { 3407e6bc45d6STheodore Ts'o error = -ENOENT; 3408e6bc45d6STheodore Ts'o goto exit3; 3409e6bc45d6STheodore Ts'o } 3410be6d3e56SKentaro Takeda error = security_path_rmdir(&nd.path, dentry); 3411be6d3e56SKentaro Takeda if (error) 3412c30dabfeSJan Kara goto exit3; 34134ac91378SJan Blunck error = vfs_rmdir(nd.path.dentry->d_inode, dentry); 34140622753bSDave Hansen exit3: 34151da177e4SLinus Torvalds dput(dentry); 34166902d925SDave Hansen exit2: 34174ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 3418c30dabfeSJan Kara mnt_drop_write(nd.path.mnt); 34191da177e4SLinus Torvalds exit1: 34201d957f9bSJan Blunck path_put(&nd.path); 34211da177e4SLinus Torvalds putname(name); 3422c6ee9206SJeff Layton if (retry_estale(error, lookup_flags)) { 3423c6ee9206SJeff Layton lookup_flags |= LOOKUP_REVAL; 3424c6ee9206SJeff Layton goto retry; 3425c6ee9206SJeff Layton } 34261da177e4SLinus Torvalds return error; 34271da177e4SLinus Torvalds } 34281da177e4SLinus Torvalds 34293cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname) 34305590ff0dSUlrich Drepper { 34315590ff0dSUlrich Drepper return do_rmdir(AT_FDCWD, pathname); 34325590ff0dSUlrich Drepper } 34335590ff0dSUlrich Drepper 34341da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry) 34351da177e4SLinus Torvalds { 34361da177e4SLinus Torvalds int error = may_delete(dir, dentry, 0); 34371da177e4SLinus Torvalds 34381da177e4SLinus Torvalds if (error) 34391da177e4SLinus Torvalds return error; 34401da177e4SLinus Torvalds 3441acfa4380SAl Viro if (!dir->i_op->unlink) 34421da177e4SLinus Torvalds return -EPERM; 34431da177e4SLinus Torvalds 34441b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 34451da177e4SLinus Torvalds if (d_mountpoint(dentry)) 34461da177e4SLinus Torvalds error = -EBUSY; 34471da177e4SLinus Torvalds else { 34481da177e4SLinus Torvalds error = security_inode_unlink(dir, dentry); 3449bec1052eSAl Viro if (!error) { 34501da177e4SLinus Torvalds error = dir->i_op->unlink(dir, dentry); 3451bec1052eSAl Viro if (!error) 3452d83c49f3SAl Viro dont_mount(dentry); 3453bec1052eSAl Viro } 34541da177e4SLinus Torvalds } 34551b1dcc1bSJes Sorensen mutex_unlock(&dentry->d_inode->i_mutex); 34561da177e4SLinus Torvalds 34571da177e4SLinus Torvalds /* We don't d_delete() NFS sillyrenamed files--they still exist. */ 34581da177e4SLinus Torvalds if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) { 3459ece95912SJan Kara fsnotify_link_count(dentry->d_inode); 34601da177e4SLinus Torvalds d_delete(dentry); 34611da177e4SLinus Torvalds } 34620eeca283SRobert Love 34631da177e4SLinus Torvalds return error; 34641da177e4SLinus Torvalds } 34651da177e4SLinus Torvalds 34661da177e4SLinus Torvalds /* 34671da177e4SLinus Torvalds * Make sure that the actual truncation of the file will occur outside its 34681b1dcc1bSJes Sorensen * directory's i_mutex. Truncate can take a long time if there is a lot of 34691da177e4SLinus Torvalds * writeout happening, and we don't want to prevent access to the directory 34701da177e4SLinus Torvalds * while waiting on the I/O. 34711da177e4SLinus Torvalds */ 34725590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname) 34731da177e4SLinus Torvalds { 34742ad94ae6SAl Viro int error; 347591a27b2aSJeff Layton struct filename *name; 34761da177e4SLinus Torvalds struct dentry *dentry; 34771da177e4SLinus Torvalds struct nameidata nd; 34781da177e4SLinus Torvalds struct inode *inode = NULL; 34795d18f813SJeff Layton unsigned int lookup_flags = 0; 34805d18f813SJeff Layton retry: 34815d18f813SJeff Layton name = user_path_parent(dfd, pathname, &nd, lookup_flags); 348291a27b2aSJeff Layton if (IS_ERR(name)) 348391a27b2aSJeff Layton return PTR_ERR(name); 34842ad94ae6SAl Viro 34851da177e4SLinus Torvalds error = -EISDIR; 34861da177e4SLinus Torvalds if (nd.last_type != LAST_NORM) 34871da177e4SLinus Torvalds goto exit1; 34880612d9fbSOGAWA Hirofumi 34890612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 3490c30dabfeSJan Kara error = mnt_want_write(nd.path.mnt); 3491c30dabfeSJan Kara if (error) 3492c30dabfeSJan Kara goto exit1; 34930612d9fbSOGAWA Hirofumi 34944ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 349549705b77SChristoph Hellwig dentry = lookup_hash(&nd); 34961da177e4SLinus Torvalds error = PTR_ERR(dentry); 34971da177e4SLinus Torvalds if (!IS_ERR(dentry)) { 34981da177e4SLinus Torvalds /* Why not before? Because we want correct error value */ 349950338b88STörök Edwin if (nd.last.name[nd.last.len]) 350050338b88STörök Edwin goto slashes; 35011da177e4SLinus Torvalds inode = dentry->d_inode; 350250338b88STörök Edwin if (!inode) 3503e6bc45d6STheodore Ts'o goto slashes; 35047de9c6eeSAl Viro ihold(inode); 3505be6d3e56SKentaro Takeda error = security_path_unlink(&nd.path, dentry); 3506be6d3e56SKentaro Takeda if (error) 3507c30dabfeSJan Kara goto exit2; 35084ac91378SJan Blunck error = vfs_unlink(nd.path.dentry->d_inode, dentry); 35091da177e4SLinus Torvalds exit2: 35101da177e4SLinus Torvalds dput(dentry); 35111da177e4SLinus Torvalds } 35124ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 35131da177e4SLinus Torvalds if (inode) 35141da177e4SLinus Torvalds iput(inode); /* truncate the inode here */ 3515c30dabfeSJan Kara mnt_drop_write(nd.path.mnt); 35161da177e4SLinus Torvalds exit1: 35171d957f9bSJan Blunck path_put(&nd.path); 35181da177e4SLinus Torvalds putname(name); 35195d18f813SJeff Layton if (retry_estale(error, lookup_flags)) { 35205d18f813SJeff Layton lookup_flags |= LOOKUP_REVAL; 35215d18f813SJeff Layton inode = NULL; 35225d18f813SJeff Layton goto retry; 35235d18f813SJeff Layton } 35241da177e4SLinus Torvalds return error; 35251da177e4SLinus Torvalds 35261da177e4SLinus Torvalds slashes: 35271da177e4SLinus Torvalds error = !dentry->d_inode ? -ENOENT : 35281da177e4SLinus Torvalds S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR; 35291da177e4SLinus Torvalds goto exit2; 35301da177e4SLinus Torvalds } 35311da177e4SLinus Torvalds 35322e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag) 35335590ff0dSUlrich Drepper { 35345590ff0dSUlrich Drepper if ((flag & ~AT_REMOVEDIR) != 0) 35355590ff0dSUlrich Drepper return -EINVAL; 35365590ff0dSUlrich Drepper 35375590ff0dSUlrich Drepper if (flag & AT_REMOVEDIR) 35385590ff0dSUlrich Drepper return do_rmdir(dfd, pathname); 35395590ff0dSUlrich Drepper 35405590ff0dSUlrich Drepper return do_unlinkat(dfd, pathname); 35415590ff0dSUlrich Drepper } 35425590ff0dSUlrich Drepper 35433480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname) 35445590ff0dSUlrich Drepper { 35455590ff0dSUlrich Drepper return do_unlinkat(AT_FDCWD, pathname); 35465590ff0dSUlrich Drepper } 35475590ff0dSUlrich Drepper 3548db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname) 35491da177e4SLinus Torvalds { 3550a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 35511da177e4SLinus Torvalds 35521da177e4SLinus Torvalds if (error) 35531da177e4SLinus Torvalds return error; 35541da177e4SLinus Torvalds 3555acfa4380SAl Viro if (!dir->i_op->symlink) 35561da177e4SLinus Torvalds return -EPERM; 35571da177e4SLinus Torvalds 35581da177e4SLinus Torvalds error = security_inode_symlink(dir, dentry, oldname); 35591da177e4SLinus Torvalds if (error) 35601da177e4SLinus Torvalds return error; 35611da177e4SLinus Torvalds 35621da177e4SLinus Torvalds error = dir->i_op->symlink(dir, dentry, oldname); 3563a74574aaSStephen Smalley if (!error) 3564f38aa942SAmy Griffis fsnotify_create(dir, dentry); 35651da177e4SLinus Torvalds return error; 35661da177e4SLinus Torvalds } 35671da177e4SLinus Torvalds 35682e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname, 35692e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 35701da177e4SLinus Torvalds { 35712ad94ae6SAl Viro int error; 357291a27b2aSJeff Layton struct filename *from; 35736902d925SDave Hansen struct dentry *dentry; 3574dae6ad8fSAl Viro struct path path; 3575f46d3567SJeff Layton unsigned int lookup_flags = 0; 35761da177e4SLinus Torvalds 35771da177e4SLinus Torvalds from = getname(oldname); 35781da177e4SLinus Torvalds if (IS_ERR(from)) 35791da177e4SLinus Torvalds return PTR_ERR(from); 3580f46d3567SJeff Layton retry: 3581f46d3567SJeff Layton dentry = user_path_create(newdfd, newname, &path, lookup_flags); 35821da177e4SLinus Torvalds error = PTR_ERR(dentry); 35836902d925SDave Hansen if (IS_ERR(dentry)) 3584dae6ad8fSAl Viro goto out_putname; 35856902d925SDave Hansen 358691a27b2aSJeff Layton error = security_path_symlink(&path, dentry, from->name); 3587a8104a9fSAl Viro if (!error) 358891a27b2aSJeff Layton error = vfs_symlink(path.dentry->d_inode, dentry, from->name); 3589921a1650SAl Viro done_path_create(&path, dentry); 3590f46d3567SJeff Layton if (retry_estale(error, lookup_flags)) { 3591f46d3567SJeff Layton lookup_flags |= LOOKUP_REVAL; 3592f46d3567SJeff Layton goto retry; 3593f46d3567SJeff Layton } 35946902d925SDave Hansen out_putname: 35951da177e4SLinus Torvalds putname(from); 35961da177e4SLinus Torvalds return error; 35971da177e4SLinus Torvalds } 35981da177e4SLinus Torvalds 35993480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname) 36005590ff0dSUlrich Drepper { 36015590ff0dSUlrich Drepper return sys_symlinkat(oldname, AT_FDCWD, newname); 36025590ff0dSUlrich Drepper } 36035590ff0dSUlrich Drepper 36041da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) 36051da177e4SLinus Torvalds { 36061da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 36078de52778SAl Viro unsigned max_links = dir->i_sb->s_max_links; 36081da177e4SLinus Torvalds int error; 36091da177e4SLinus Torvalds 36101da177e4SLinus Torvalds if (!inode) 36111da177e4SLinus Torvalds return -ENOENT; 36121da177e4SLinus Torvalds 3613a95164d9SMiklos Szeredi error = may_create(dir, new_dentry); 36141da177e4SLinus Torvalds if (error) 36151da177e4SLinus Torvalds return error; 36161da177e4SLinus Torvalds 36171da177e4SLinus Torvalds if (dir->i_sb != inode->i_sb) 36181da177e4SLinus Torvalds return -EXDEV; 36191da177e4SLinus Torvalds 36201da177e4SLinus Torvalds /* 36211da177e4SLinus Torvalds * A link to an append-only or immutable file cannot be created. 36221da177e4SLinus Torvalds */ 36231da177e4SLinus Torvalds if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) 36241da177e4SLinus Torvalds return -EPERM; 3625acfa4380SAl Viro if (!dir->i_op->link) 36261da177e4SLinus Torvalds return -EPERM; 36277e79eedbSTetsuo Handa if (S_ISDIR(inode->i_mode)) 36281da177e4SLinus Torvalds return -EPERM; 36291da177e4SLinus Torvalds 36301da177e4SLinus Torvalds error = security_inode_link(old_dentry, dir, new_dentry); 36311da177e4SLinus Torvalds if (error) 36321da177e4SLinus Torvalds return error; 36331da177e4SLinus Torvalds 36347e79eedbSTetsuo Handa mutex_lock(&inode->i_mutex); 3635aae8a97dSAneesh Kumar K.V /* Make sure we don't allow creating hardlink to an unlinked file */ 3636f4e0c30cSAl Viro if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE)) 3637aae8a97dSAneesh Kumar K.V error = -ENOENT; 36388de52778SAl Viro else if (max_links && inode->i_nlink >= max_links) 36398de52778SAl Viro error = -EMLINK; 3640aae8a97dSAneesh Kumar K.V else 36411da177e4SLinus Torvalds error = dir->i_op->link(old_dentry, dir, new_dentry); 3642f4e0c30cSAl Viro 3643f4e0c30cSAl Viro if (!error && (inode->i_state & I_LINKABLE)) { 3644f4e0c30cSAl Viro spin_lock(&inode->i_lock); 3645f4e0c30cSAl Viro inode->i_state &= ~I_LINKABLE; 3646f4e0c30cSAl Viro spin_unlock(&inode->i_lock); 3647f4e0c30cSAl Viro } 36487e79eedbSTetsuo Handa mutex_unlock(&inode->i_mutex); 3649e31e14ecSStephen Smalley if (!error) 36507e79eedbSTetsuo Handa fsnotify_link(dir, inode, new_dentry); 36511da177e4SLinus Torvalds return error; 36521da177e4SLinus Torvalds } 36531da177e4SLinus Torvalds 36541da177e4SLinus Torvalds /* 36551da177e4SLinus Torvalds * Hardlinks are often used in delicate situations. We avoid 36561da177e4SLinus Torvalds * security-related surprises by not following symlinks on the 36571da177e4SLinus Torvalds * newname. --KAB 36581da177e4SLinus Torvalds * 36591da177e4SLinus Torvalds * We don't follow them on the oldname either to be compatible 36601da177e4SLinus Torvalds * with linux 2.0, and to avoid hard-linking to directories 36611da177e4SLinus Torvalds * and other special files. --ADM 36621da177e4SLinus Torvalds */ 36632e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname, 36642e4d0924SHeiko Carstens int, newdfd, const char __user *, newname, int, flags) 36651da177e4SLinus Torvalds { 36661da177e4SLinus Torvalds struct dentry *new_dentry; 3667dae6ad8fSAl Viro struct path old_path, new_path; 366811a7b371SAneesh Kumar K.V int how = 0; 36691da177e4SLinus Torvalds int error; 36701da177e4SLinus Torvalds 367111a7b371SAneesh Kumar K.V if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0) 3672c04030e1SUlrich Drepper return -EINVAL; 367311a7b371SAneesh Kumar K.V /* 367411a7b371SAneesh Kumar K.V * To use null names we require CAP_DAC_READ_SEARCH 367511a7b371SAneesh Kumar K.V * This ensures that not everyone will be able to create 367611a7b371SAneesh Kumar K.V * handlink using the passed filedescriptor. 367711a7b371SAneesh Kumar K.V */ 367811a7b371SAneesh Kumar K.V if (flags & AT_EMPTY_PATH) { 367911a7b371SAneesh Kumar K.V if (!capable(CAP_DAC_READ_SEARCH)) 368011a7b371SAneesh Kumar K.V return -ENOENT; 368111a7b371SAneesh Kumar K.V how = LOOKUP_EMPTY; 368211a7b371SAneesh Kumar K.V } 3683c04030e1SUlrich Drepper 368411a7b371SAneesh Kumar K.V if (flags & AT_SYMLINK_FOLLOW) 368511a7b371SAneesh Kumar K.V how |= LOOKUP_FOLLOW; 3686442e31caSJeff Layton retry: 368711a7b371SAneesh Kumar K.V error = user_path_at(olddfd, oldname, how, &old_path); 36881da177e4SLinus Torvalds if (error) 36892ad94ae6SAl Viro return error; 36902ad94ae6SAl Viro 3691442e31caSJeff Layton new_dentry = user_path_create(newdfd, newname, &new_path, 3692442e31caSJeff Layton (how & LOOKUP_REVAL)); 36931da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 36946902d925SDave Hansen if (IS_ERR(new_dentry)) 3695dae6ad8fSAl Viro goto out; 3696dae6ad8fSAl Viro 3697dae6ad8fSAl Viro error = -EXDEV; 3698dae6ad8fSAl Viro if (old_path.mnt != new_path.mnt) 3699dae6ad8fSAl Viro goto out_dput; 3700800179c9SKees Cook error = may_linkat(&old_path); 3701800179c9SKees Cook if (unlikely(error)) 3702800179c9SKees Cook goto out_dput; 3703dae6ad8fSAl Viro error = security_path_link(old_path.dentry, &new_path, new_dentry); 3704be6d3e56SKentaro Takeda if (error) 3705a8104a9fSAl Viro goto out_dput; 3706dae6ad8fSAl Viro error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry); 370775c3f29dSDave Hansen out_dput: 3708921a1650SAl Viro done_path_create(&new_path, new_dentry); 3709442e31caSJeff Layton if (retry_estale(error, how)) { 3710442e31caSJeff Layton how |= LOOKUP_REVAL; 3711442e31caSJeff Layton goto retry; 3712442e31caSJeff Layton } 37131da177e4SLinus Torvalds out: 37142d8f3038SAl Viro path_put(&old_path); 37151da177e4SLinus Torvalds 37161da177e4SLinus Torvalds return error; 37171da177e4SLinus Torvalds } 37181da177e4SLinus Torvalds 37193480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname) 37205590ff0dSUlrich Drepper { 3721c04030e1SUlrich Drepper return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0); 37225590ff0dSUlrich Drepper } 37235590ff0dSUlrich Drepper 37241da177e4SLinus Torvalds /* 37251da177e4SLinus Torvalds * The worst of all namespace operations - renaming directory. "Perverted" 37261da177e4SLinus Torvalds * doesn't even start to describe it. Somebody in UCB had a heck of a trip... 37271da177e4SLinus Torvalds * Problems: 37281da177e4SLinus Torvalds * a) we can get into loop creation. Check is done in is_subdir(). 37291da177e4SLinus Torvalds * b) race potential - two innocent renames can create a loop together. 37301da177e4SLinus Torvalds * That's where 4.4 screws up. Current fix: serialization on 3731a11f3a05SArjan van de Ven * sb->s_vfs_rename_mutex. We might be more accurate, but that's another 37321da177e4SLinus Torvalds * story. 37331da177e4SLinus Torvalds * c) we have to lock _three_ objects - parents and victim (if it exists). 37341b1dcc1bSJes Sorensen * And that - after we got ->i_mutex on parents (until then we don't know 37351da177e4SLinus Torvalds * whether the target exists). Solution: try to be smart with locking 37361da177e4SLinus Torvalds * order for inodes. We rely on the fact that tree topology may change 3737a11f3a05SArjan van de Ven * only under ->s_vfs_rename_mutex _and_ that parent of the object we 37381da177e4SLinus Torvalds * move will be locked. Thus we can rank directories by the tree 37391da177e4SLinus Torvalds * (ancestors first) and rank all non-directories after them. 37401da177e4SLinus Torvalds * That works since everybody except rename does "lock parent, lookup, 3741a11f3a05SArjan van de Ven * lock child" and rename is under ->s_vfs_rename_mutex. 37421da177e4SLinus Torvalds * HOWEVER, it relies on the assumption that any object with ->lookup() 37431da177e4SLinus Torvalds * has no more than 1 dentry. If "hybrid" objects will ever appear, 37441da177e4SLinus Torvalds * we'd better make sure that there's no link(2) for them. 3745e4eaac06SSage Weil * d) conversion from fhandle to dentry may come in the wrong moment - when 37461b1dcc1bSJes Sorensen * we are removing the target. Solution: we will have to grab ->i_mutex 37471da177e4SLinus Torvalds * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on 3748c41b20e7SAdam Buchbinder * ->i_mutex on parents, which works but leads to some truly excessive 37491da177e4SLinus Torvalds * locking]. 37501da177e4SLinus Torvalds */ 375175c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry, 37521da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 37531da177e4SLinus Torvalds { 37541da177e4SLinus Torvalds int error = 0; 37559055cba7SSage Weil struct inode *target = new_dentry->d_inode; 37568de52778SAl Viro unsigned max_links = new_dir->i_sb->s_max_links; 37571da177e4SLinus Torvalds 37581da177e4SLinus Torvalds /* 37591da177e4SLinus Torvalds * If we are going to change the parent - check write permissions, 37601da177e4SLinus Torvalds * we'll need to flip '..'. 37611da177e4SLinus Torvalds */ 37621da177e4SLinus Torvalds if (new_dir != old_dir) { 3763f419a2e3SAl Viro error = inode_permission(old_dentry->d_inode, MAY_WRITE); 37641da177e4SLinus Torvalds if (error) 37651da177e4SLinus Torvalds return error; 37661da177e4SLinus Torvalds } 37671da177e4SLinus Torvalds 37681da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 37691da177e4SLinus Torvalds if (error) 37701da177e4SLinus Torvalds return error; 37711da177e4SLinus Torvalds 37721d2ef590SAl Viro dget(new_dentry); 3773d83c49f3SAl Viro if (target) 37741b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 37759055cba7SSage Weil 37761da177e4SLinus Torvalds error = -EBUSY; 37779055cba7SSage Weil if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry)) 37789055cba7SSage Weil goto out; 37799055cba7SSage Weil 37808de52778SAl Viro error = -EMLINK; 37818de52778SAl Viro if (max_links && !target && new_dir != old_dir && 37828de52778SAl Viro new_dir->i_nlink >= max_links) 37838de52778SAl Viro goto out; 37848de52778SAl Viro 37853cebde24SSage Weil if (target) 37863cebde24SSage Weil shrink_dcache_parent(new_dentry); 37871da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 37889055cba7SSage Weil if (error) 37899055cba7SSage Weil goto out; 37909055cba7SSage Weil 37911da177e4SLinus Torvalds if (target) { 37921da177e4SLinus Torvalds target->i_flags |= S_DEAD; 3793d83c49f3SAl Viro dont_mount(new_dentry); 3794d83c49f3SAl Viro } 37959055cba7SSage Weil out: 37969055cba7SSage Weil if (target) 37971b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 37981d2ef590SAl Viro dput(new_dentry); 3799e31e14ecSStephen Smalley if (!error) 3800349457ccSMark Fasheh if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) 38011da177e4SLinus Torvalds d_move(old_dentry,new_dentry); 38021da177e4SLinus Torvalds return error; 38031da177e4SLinus Torvalds } 38041da177e4SLinus Torvalds 380575c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry, 38061da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 38071da177e4SLinus Torvalds { 380851892bbbSSage Weil struct inode *target = new_dentry->d_inode; 38091da177e4SLinus Torvalds int error; 38101da177e4SLinus Torvalds 38111da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 38121da177e4SLinus Torvalds if (error) 38131da177e4SLinus Torvalds return error; 38141da177e4SLinus Torvalds 38151da177e4SLinus Torvalds dget(new_dentry); 38161da177e4SLinus Torvalds if (target) 38171b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 381851892bbbSSage Weil 38191da177e4SLinus Torvalds error = -EBUSY; 382051892bbbSSage Weil if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry)) 382151892bbbSSage Weil goto out; 382251892bbbSSage Weil 38231da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 382451892bbbSSage Weil if (error) 382551892bbbSSage Weil goto out; 382651892bbbSSage Weil 3827bec1052eSAl Viro if (target) 3828d83c49f3SAl Viro dont_mount(new_dentry); 3829349457ccSMark Fasheh if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) 38301da177e4SLinus Torvalds d_move(old_dentry, new_dentry); 383151892bbbSSage Weil out: 38321da177e4SLinus Torvalds if (target) 38331b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 38341da177e4SLinus Torvalds dput(new_dentry); 38351da177e4SLinus Torvalds return error; 38361da177e4SLinus Torvalds } 38371da177e4SLinus Torvalds 38381da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry, 38391da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 38401da177e4SLinus Torvalds { 38411da177e4SLinus Torvalds int error; 38421da177e4SLinus Torvalds int is_dir = S_ISDIR(old_dentry->d_inode->i_mode); 384359b0df21SEric Paris const unsigned char *old_name; 38441da177e4SLinus Torvalds 38451da177e4SLinus Torvalds if (old_dentry->d_inode == new_dentry->d_inode) 38461da177e4SLinus Torvalds return 0; 38471da177e4SLinus Torvalds 38481da177e4SLinus Torvalds error = may_delete(old_dir, old_dentry, is_dir); 38491da177e4SLinus Torvalds if (error) 38501da177e4SLinus Torvalds return error; 38511da177e4SLinus Torvalds 38521da177e4SLinus Torvalds if (!new_dentry->d_inode) 3853a95164d9SMiklos Szeredi error = may_create(new_dir, new_dentry); 38541da177e4SLinus Torvalds else 38551da177e4SLinus Torvalds error = may_delete(new_dir, new_dentry, is_dir); 38561da177e4SLinus Torvalds if (error) 38571da177e4SLinus Torvalds return error; 38581da177e4SLinus Torvalds 3859acfa4380SAl Viro if (!old_dir->i_op->rename) 38601da177e4SLinus Torvalds return -EPERM; 38611da177e4SLinus Torvalds 38620eeca283SRobert Love old_name = fsnotify_oldname_init(old_dentry->d_name.name); 38630eeca283SRobert Love 38641da177e4SLinus Torvalds if (is_dir) 38651da177e4SLinus Torvalds error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry); 38661da177e4SLinus Torvalds else 38671da177e4SLinus Torvalds error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry); 3868123df294SAl Viro if (!error) 3869123df294SAl Viro fsnotify_move(old_dir, new_dir, old_name, is_dir, 38705a190ae6SAl Viro new_dentry->d_inode, old_dentry); 38710eeca283SRobert Love fsnotify_oldname_free(old_name); 38720eeca283SRobert Love 38731da177e4SLinus Torvalds return error; 38741da177e4SLinus Torvalds } 38751da177e4SLinus Torvalds 38762e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname, 38772e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 38781da177e4SLinus Torvalds { 38791da177e4SLinus Torvalds struct dentry *old_dir, *new_dir; 38801da177e4SLinus Torvalds struct dentry *old_dentry, *new_dentry; 38811da177e4SLinus Torvalds struct dentry *trap; 38821da177e4SLinus Torvalds struct nameidata oldnd, newnd; 388391a27b2aSJeff Layton struct filename *from; 388491a27b2aSJeff Layton struct filename *to; 3885c6a94284SJeff Layton unsigned int lookup_flags = 0; 3886c6a94284SJeff Layton bool should_retry = false; 38872ad94ae6SAl Viro int error; 3888c6a94284SJeff Layton retry: 3889c6a94284SJeff Layton from = user_path_parent(olddfd, oldname, &oldnd, lookup_flags); 389091a27b2aSJeff Layton if (IS_ERR(from)) { 389191a27b2aSJeff Layton error = PTR_ERR(from); 38921da177e4SLinus Torvalds goto exit; 389391a27b2aSJeff Layton } 38941da177e4SLinus Torvalds 3895c6a94284SJeff Layton to = user_path_parent(newdfd, newname, &newnd, lookup_flags); 389691a27b2aSJeff Layton if (IS_ERR(to)) { 389791a27b2aSJeff Layton error = PTR_ERR(to); 38981da177e4SLinus Torvalds goto exit1; 389991a27b2aSJeff Layton } 39001da177e4SLinus Torvalds 39011da177e4SLinus Torvalds error = -EXDEV; 39024ac91378SJan Blunck if (oldnd.path.mnt != newnd.path.mnt) 39031da177e4SLinus Torvalds goto exit2; 39041da177e4SLinus Torvalds 39054ac91378SJan Blunck old_dir = oldnd.path.dentry; 39061da177e4SLinus Torvalds error = -EBUSY; 39071da177e4SLinus Torvalds if (oldnd.last_type != LAST_NORM) 39081da177e4SLinus Torvalds goto exit2; 39091da177e4SLinus Torvalds 39104ac91378SJan Blunck new_dir = newnd.path.dentry; 39111da177e4SLinus Torvalds if (newnd.last_type != LAST_NORM) 39121da177e4SLinus Torvalds goto exit2; 39131da177e4SLinus Torvalds 3914c30dabfeSJan Kara error = mnt_want_write(oldnd.path.mnt); 3915c30dabfeSJan Kara if (error) 3916c30dabfeSJan Kara goto exit2; 3917c30dabfeSJan Kara 39180612d9fbSOGAWA Hirofumi oldnd.flags &= ~LOOKUP_PARENT; 39190612d9fbSOGAWA Hirofumi newnd.flags &= ~LOOKUP_PARENT; 39204e9ed2f8SOGAWA Hirofumi newnd.flags |= LOOKUP_RENAME_TARGET; 39210612d9fbSOGAWA Hirofumi 39221da177e4SLinus Torvalds trap = lock_rename(new_dir, old_dir); 39231da177e4SLinus Torvalds 392449705b77SChristoph Hellwig old_dentry = lookup_hash(&oldnd); 39251da177e4SLinus Torvalds error = PTR_ERR(old_dentry); 39261da177e4SLinus Torvalds if (IS_ERR(old_dentry)) 39271da177e4SLinus Torvalds goto exit3; 39281da177e4SLinus Torvalds /* source must exist */ 39291da177e4SLinus Torvalds error = -ENOENT; 39301da177e4SLinus Torvalds if (!old_dentry->d_inode) 39311da177e4SLinus Torvalds goto exit4; 39321da177e4SLinus Torvalds /* unless the source is a directory trailing slashes give -ENOTDIR */ 39331da177e4SLinus Torvalds if (!S_ISDIR(old_dentry->d_inode->i_mode)) { 39341da177e4SLinus Torvalds error = -ENOTDIR; 39351da177e4SLinus Torvalds if (oldnd.last.name[oldnd.last.len]) 39361da177e4SLinus Torvalds goto exit4; 39371da177e4SLinus Torvalds if (newnd.last.name[newnd.last.len]) 39381da177e4SLinus Torvalds goto exit4; 39391da177e4SLinus Torvalds } 39401da177e4SLinus Torvalds /* source should not be ancestor of target */ 39411da177e4SLinus Torvalds error = -EINVAL; 39421da177e4SLinus Torvalds if (old_dentry == trap) 39431da177e4SLinus Torvalds goto exit4; 394449705b77SChristoph Hellwig new_dentry = lookup_hash(&newnd); 39451da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 39461da177e4SLinus Torvalds if (IS_ERR(new_dentry)) 39471da177e4SLinus Torvalds goto exit4; 39481da177e4SLinus Torvalds /* target should not be an ancestor of source */ 39491da177e4SLinus Torvalds error = -ENOTEMPTY; 39501da177e4SLinus Torvalds if (new_dentry == trap) 39511da177e4SLinus Torvalds goto exit5; 39521da177e4SLinus Torvalds 3953be6d3e56SKentaro Takeda error = security_path_rename(&oldnd.path, old_dentry, 3954be6d3e56SKentaro Takeda &newnd.path, new_dentry); 3955be6d3e56SKentaro Takeda if (error) 3956c30dabfeSJan Kara goto exit5; 39571da177e4SLinus Torvalds error = vfs_rename(old_dir->d_inode, old_dentry, 39581da177e4SLinus Torvalds new_dir->d_inode, new_dentry); 39591da177e4SLinus Torvalds exit5: 39601da177e4SLinus Torvalds dput(new_dentry); 39611da177e4SLinus Torvalds exit4: 39621da177e4SLinus Torvalds dput(old_dentry); 39631da177e4SLinus Torvalds exit3: 39641da177e4SLinus Torvalds unlock_rename(new_dir, old_dir); 3965c30dabfeSJan Kara mnt_drop_write(oldnd.path.mnt); 39661da177e4SLinus Torvalds exit2: 3967c6a94284SJeff Layton if (retry_estale(error, lookup_flags)) 3968c6a94284SJeff Layton should_retry = true; 39691d957f9bSJan Blunck path_put(&newnd.path); 39702ad94ae6SAl Viro putname(to); 39711da177e4SLinus Torvalds exit1: 39721d957f9bSJan Blunck path_put(&oldnd.path); 39731da177e4SLinus Torvalds putname(from); 3974c6a94284SJeff Layton if (should_retry) { 3975c6a94284SJeff Layton should_retry = false; 3976c6a94284SJeff Layton lookup_flags |= LOOKUP_REVAL; 3977c6a94284SJeff Layton goto retry; 3978c6a94284SJeff Layton } 39792ad94ae6SAl Viro exit: 39801da177e4SLinus Torvalds return error; 39811da177e4SLinus Torvalds } 39821da177e4SLinus Torvalds 3983a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname) 39845590ff0dSUlrich Drepper { 39855590ff0dSUlrich Drepper return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname); 39865590ff0dSUlrich Drepper } 39875590ff0dSUlrich Drepper 39881da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link) 39891da177e4SLinus Torvalds { 39901da177e4SLinus Torvalds int len; 39911da177e4SLinus Torvalds 39921da177e4SLinus Torvalds len = PTR_ERR(link); 39931da177e4SLinus Torvalds if (IS_ERR(link)) 39941da177e4SLinus Torvalds goto out; 39951da177e4SLinus Torvalds 39961da177e4SLinus Torvalds len = strlen(link); 39971da177e4SLinus Torvalds if (len > (unsigned) buflen) 39981da177e4SLinus Torvalds len = buflen; 39991da177e4SLinus Torvalds if (copy_to_user(buffer, link, len)) 40001da177e4SLinus Torvalds len = -EFAULT; 40011da177e4SLinus Torvalds out: 40021da177e4SLinus Torvalds return len; 40031da177e4SLinus Torvalds } 40041da177e4SLinus Torvalds 40051da177e4SLinus Torvalds /* 40061da177e4SLinus Torvalds * A helper for ->readlink(). This should be used *ONLY* for symlinks that 40071da177e4SLinus Torvalds * have ->follow_link() touching nd only in nd_set_link(). Using (or not 40081da177e4SLinus Torvalds * using) it for any given inode is up to filesystem. 40091da177e4SLinus Torvalds */ 40101da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen) 40111da177e4SLinus Torvalds { 40121da177e4SLinus Torvalds struct nameidata nd; 4013cc314eefSLinus Torvalds void *cookie; 4014694a1764SMarcin Slusarz int res; 4015cc314eefSLinus Torvalds 40161da177e4SLinus Torvalds nd.depth = 0; 4017cc314eefSLinus Torvalds cookie = dentry->d_inode->i_op->follow_link(dentry, &nd); 4018694a1764SMarcin Slusarz if (IS_ERR(cookie)) 4019694a1764SMarcin Slusarz return PTR_ERR(cookie); 4020694a1764SMarcin Slusarz 4021694a1764SMarcin Slusarz res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd)); 40221da177e4SLinus Torvalds if (dentry->d_inode->i_op->put_link) 4023cc314eefSLinus Torvalds dentry->d_inode->i_op->put_link(dentry, &nd, cookie); 4024694a1764SMarcin Slusarz return res; 40251da177e4SLinus Torvalds } 40261da177e4SLinus Torvalds 40271da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link) 40281da177e4SLinus Torvalds { 40291da177e4SLinus Torvalds return __vfs_follow_link(nd, link); 40301da177e4SLinus Torvalds } 40311da177e4SLinus Torvalds 40321da177e4SLinus Torvalds /* get the link contents into pagecache */ 40331da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage) 40341da177e4SLinus Torvalds { 4035ebd09abbSDuane Griffin char *kaddr; 40361da177e4SLinus Torvalds struct page *page; 40371da177e4SLinus Torvalds struct address_space *mapping = dentry->d_inode->i_mapping; 4038090d2b18SPekka Enberg page = read_mapping_page(mapping, 0, NULL); 40391da177e4SLinus Torvalds if (IS_ERR(page)) 40406fe6900eSNick Piggin return (char*)page; 40411da177e4SLinus Torvalds *ppage = page; 4042ebd09abbSDuane Griffin kaddr = kmap(page); 4043ebd09abbSDuane Griffin nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1); 4044ebd09abbSDuane Griffin return kaddr; 40451da177e4SLinus Torvalds } 40461da177e4SLinus Torvalds 40471da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen) 40481da177e4SLinus Torvalds { 40491da177e4SLinus Torvalds struct page *page = NULL; 40501da177e4SLinus Torvalds char *s = page_getlink(dentry, &page); 40511da177e4SLinus Torvalds int res = vfs_readlink(dentry,buffer,buflen,s); 40521da177e4SLinus Torvalds if (page) { 40531da177e4SLinus Torvalds kunmap(page); 40541da177e4SLinus Torvalds page_cache_release(page); 40551da177e4SLinus Torvalds } 40561da177e4SLinus Torvalds return res; 40571da177e4SLinus Torvalds } 40581da177e4SLinus Torvalds 4059cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd) 40601da177e4SLinus Torvalds { 4061cc314eefSLinus Torvalds struct page *page = NULL; 40621da177e4SLinus Torvalds nd_set_link(nd, page_getlink(dentry, &page)); 4063cc314eefSLinus Torvalds return page; 40641da177e4SLinus Torvalds } 40651da177e4SLinus Torvalds 4066cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie) 40671da177e4SLinus Torvalds { 4068cc314eefSLinus Torvalds struct page *page = cookie; 4069cc314eefSLinus Torvalds 4070cc314eefSLinus Torvalds if (page) { 40711da177e4SLinus Torvalds kunmap(page); 40721da177e4SLinus Torvalds page_cache_release(page); 40731da177e4SLinus Torvalds } 40741da177e4SLinus Torvalds } 40751da177e4SLinus Torvalds 407654566b2cSNick Piggin /* 407754566b2cSNick Piggin * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS 407854566b2cSNick Piggin */ 407954566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs) 40801da177e4SLinus Torvalds { 40811da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 40820adb25d2SKirill Korotaev struct page *page; 4083afddba49SNick Piggin void *fsdata; 4084beb497abSDmitriy Monakhov int err; 40851da177e4SLinus Torvalds char *kaddr; 408654566b2cSNick Piggin unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE; 408754566b2cSNick Piggin if (nofs) 408854566b2cSNick Piggin flags |= AOP_FLAG_NOFS; 40891da177e4SLinus Torvalds 40907e53cac4SNeilBrown retry: 4091afddba49SNick Piggin err = pagecache_write_begin(NULL, mapping, 0, len-1, 409254566b2cSNick Piggin flags, &page, &fsdata); 40931da177e4SLinus Torvalds if (err) 4094afddba49SNick Piggin goto fail; 4095afddba49SNick Piggin 4096e8e3c3d6SCong Wang kaddr = kmap_atomic(page); 40971da177e4SLinus Torvalds memcpy(kaddr, symname, len-1); 4098e8e3c3d6SCong Wang kunmap_atomic(kaddr); 4099afddba49SNick Piggin 4100afddba49SNick Piggin err = pagecache_write_end(NULL, mapping, 0, len-1, len-1, 4101afddba49SNick Piggin page, fsdata); 41021da177e4SLinus Torvalds if (err < 0) 41031da177e4SLinus Torvalds goto fail; 4104afddba49SNick Piggin if (err < len-1) 4105afddba49SNick Piggin goto retry; 4106afddba49SNick Piggin 41071da177e4SLinus Torvalds mark_inode_dirty(inode); 41081da177e4SLinus Torvalds return 0; 41091da177e4SLinus Torvalds fail: 41101da177e4SLinus Torvalds return err; 41111da177e4SLinus Torvalds } 41121da177e4SLinus Torvalds 41130adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len) 41140adb25d2SKirill Korotaev { 41150adb25d2SKirill Korotaev return __page_symlink(inode, symname, len, 411654566b2cSNick Piggin !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS)); 41170adb25d2SKirill Korotaev } 41180adb25d2SKirill Korotaev 411992e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = { 41201da177e4SLinus Torvalds .readlink = generic_readlink, 41211da177e4SLinus Torvalds .follow_link = page_follow_link_light, 41221da177e4SLinus Torvalds .put_link = page_put_link, 41231da177e4SLinus Torvalds }; 41241da177e4SLinus Torvalds 41252d8f3038SAl Viro EXPORT_SYMBOL(user_path_at); 4126cc53ce53SDavid Howells EXPORT_SYMBOL(follow_down_one); 41271da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down); 41281da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up); 4129f6d2ac5cSAl Viro EXPORT_SYMBOL(get_write_access); /* nfsd */ 41301da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename); 41311da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len); 41321da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light); 41331da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link); 41341da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink); 41350adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink); 41361da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink); 41371da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations); 4138d1811465SAl Viro EXPORT_SYMBOL(kern_path); 413916f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup); 4140f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission); 41411da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename); 41421da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create); 41431da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link); 41441da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link); 41451da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir); 41461da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod); 41471da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission); 41481da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink); 41491da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename); 41501da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir); 41511da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink); 41521da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink); 41531da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash); 41541da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink); 4155