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 49715570086SLinus Torvalds /* 49815570086SLinus Torvalds * When we move over from the RCU domain to properly refcounted 49915570086SLinus Torvalds * long-lived dentries, we need to check the sequence numbers 50015570086SLinus Torvalds * we got before lookup very carefully. 50115570086SLinus Torvalds * 50215570086SLinus Torvalds * We cannot blindly increment a dentry refcount - even if it 50315570086SLinus Torvalds * is not locked - if it is zero, because it may have gone 50415570086SLinus Torvalds * through the final d_kill() logic already. 50515570086SLinus Torvalds * 50615570086SLinus Torvalds * So for a zero refcount, we need to get the spinlock (which is 50715570086SLinus Torvalds * safe even for a dead dentry because the de-allocation is 50815570086SLinus Torvalds * RCU-delayed), and check the sequence count under the lock. 50915570086SLinus Torvalds * 51015570086SLinus Torvalds * Once we have checked the sequence count, we know it is live, 51115570086SLinus Torvalds * and since we hold the spinlock it cannot die from under us. 51215570086SLinus Torvalds * 51315570086SLinus Torvalds * In contrast, if the reference count wasn't zero, we can just 51415570086SLinus Torvalds * increment the lockref without having to take the spinlock. 51515570086SLinus Torvalds * Even if the sequence number ends up being stale, we haven't 51615570086SLinus Torvalds * gone through the final dput() and killed the dentry yet. 51715570086SLinus Torvalds */ 51815570086SLinus Torvalds static inline int d_rcu_to_refcount(struct dentry *dentry, seqcount_t *validate, unsigned seq) 51915570086SLinus Torvalds { 52015570086SLinus Torvalds int gotref; 52115570086SLinus Torvalds 52215570086SLinus Torvalds gotref = lockref_get_or_lock(&dentry->d_lockref); 52315570086SLinus Torvalds 52415570086SLinus Torvalds /* Does the sequence number still match? */ 52515570086SLinus Torvalds if (read_seqcount_retry(validate, seq)) { 52615570086SLinus Torvalds if (gotref) 52715570086SLinus Torvalds dput(dentry); 52815570086SLinus Torvalds else 52915570086SLinus Torvalds spin_unlock(&dentry->d_lock); 53015570086SLinus Torvalds return -ECHILD; 53115570086SLinus Torvalds } 53215570086SLinus Torvalds 53315570086SLinus Torvalds /* Get the ref now, if we couldn't get it originally */ 53415570086SLinus Torvalds if (!gotref) { 53515570086SLinus Torvalds dentry->d_lockref.count++; 53615570086SLinus Torvalds spin_unlock(&dentry->d_lock); 53715570086SLinus Torvalds } 53815570086SLinus Torvalds return 0; 53915570086SLinus Torvalds } 54015570086SLinus Torvalds 54131e6b01fSNick Piggin /** 54219660af7SAl Viro * unlazy_walk - try to switch to ref-walk mode. 54319660af7SAl Viro * @nd: nameidata pathwalk data 54419660af7SAl Viro * @dentry: child of nd->path.dentry or NULL 54539191628SRandy Dunlap * Returns: 0 on success, -ECHILD on failure 54631e6b01fSNick Piggin * 54719660af7SAl Viro * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry 54819660af7SAl Viro * for ref-walk mode. @dentry must be a path found by a do_lookup call on 54919660af7SAl Viro * @nd or NULL. Must be called from rcu-walk context. 55031e6b01fSNick Piggin */ 55119660af7SAl Viro static int unlazy_walk(struct nameidata *nd, struct dentry *dentry) 55231e6b01fSNick Piggin { 55331e6b01fSNick Piggin struct fs_struct *fs = current->fs; 55431e6b01fSNick Piggin struct dentry *parent = nd->path.dentry; 5555b6ca027SAl Viro int want_root = 0; 55631e6b01fSNick Piggin 55731e6b01fSNick Piggin BUG_ON(!(nd->flags & LOOKUP_RCU)); 5585b6ca027SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) { 5595b6ca027SAl Viro want_root = 1; 56031e6b01fSNick Piggin spin_lock(&fs->lock); 56131e6b01fSNick Piggin if (nd->root.mnt != fs->root.mnt || 56231e6b01fSNick Piggin nd->root.dentry != fs->root.dentry) 56331e6b01fSNick Piggin goto err_root; 56431e6b01fSNick Piggin } 56515570086SLinus Torvalds 56615570086SLinus Torvalds /* 56715570086SLinus Torvalds * For a negative lookup, the lookup sequence point is the parents 56815570086SLinus Torvalds * sequence point, and it only needs to revalidate the parent dentry. 56915570086SLinus Torvalds * 57015570086SLinus Torvalds * For a positive lookup, we need to move both the parent and the 57115570086SLinus Torvalds * dentry from the RCU domain to be properly refcounted. And the 57215570086SLinus Torvalds * sequence number in the dentry validates *both* dentry counters, 57315570086SLinus Torvalds * since we checked the sequence number of the parent after we got 57415570086SLinus Torvalds * the child sequence number. So we know the parent must still 57515570086SLinus Torvalds * be valid if the child sequence number is still valid. 57615570086SLinus Torvalds */ 57719660af7SAl Viro if (!dentry) { 57815570086SLinus Torvalds if (d_rcu_to_refcount(parent, &parent->d_seq, nd->seq) < 0) 57915570086SLinus Torvalds goto err_root; 58019660af7SAl Viro BUG_ON(nd->inode != parent->d_inode); 58119660af7SAl Viro } else { 58215570086SLinus Torvalds if (d_rcu_to_refcount(dentry, &dentry->d_seq, nd->seq) < 0) 58315570086SLinus Torvalds goto err_root; 58415570086SLinus Torvalds if (d_rcu_to_refcount(parent, &dentry->d_seq, nd->seq) < 0) 58594c0d4ecSAl Viro goto err_parent; 58619660af7SAl Viro } 5875b6ca027SAl Viro if (want_root) { 58831e6b01fSNick Piggin path_get(&nd->root); 58931e6b01fSNick Piggin spin_unlock(&fs->lock); 59031e6b01fSNick Piggin } 59131e6b01fSNick Piggin mntget(nd->path.mnt); 59231e6b01fSNick Piggin 59332a7991bSAl Viro unlock_rcu_walk(); 59431e6b01fSNick Piggin nd->flags &= ~LOOKUP_RCU; 59531e6b01fSNick Piggin return 0; 59619660af7SAl Viro 59719660af7SAl Viro err_parent: 59815570086SLinus Torvalds dput(dentry); 59931e6b01fSNick Piggin err_root: 6005b6ca027SAl Viro if (want_root) 60131e6b01fSNick Piggin spin_unlock(&fs->lock); 60231e6b01fSNick Piggin return -ECHILD; 60331e6b01fSNick Piggin } 60431e6b01fSNick Piggin 6054ce16ef3SAl Viro static inline int d_revalidate(struct dentry *dentry, unsigned int flags) 60634286d66SNick Piggin { 6074ce16ef3SAl Viro return dentry->d_op->d_revalidate(dentry, flags); 60834286d66SNick Piggin } 60934286d66SNick Piggin 6109f1fafeeSAl Viro /** 6119f1fafeeSAl Viro * complete_walk - successful completion of path walk 6129f1fafeeSAl Viro * @nd: pointer nameidata 61339159de2SJeff Layton * 6149f1fafeeSAl Viro * If we had been in RCU mode, drop out of it and legitimize nd->path. 6159f1fafeeSAl Viro * Revalidate the final result, unless we'd already done that during 6169f1fafeeSAl Viro * the path walk or the filesystem doesn't ask for it. Return 0 on 6179f1fafeeSAl Viro * success, -error on failure. In case of failure caller does not 6189f1fafeeSAl Viro * need to drop nd->path. 61939159de2SJeff Layton */ 6209f1fafeeSAl Viro static int complete_walk(struct nameidata *nd) 62139159de2SJeff Layton { 62216c2cd71SAl Viro struct dentry *dentry = nd->path.dentry; 62339159de2SJeff Layton int status; 62439159de2SJeff Layton 6259f1fafeeSAl Viro if (nd->flags & LOOKUP_RCU) { 6269f1fafeeSAl Viro nd->flags &= ~LOOKUP_RCU; 6279f1fafeeSAl Viro if (!(nd->flags & LOOKUP_ROOT)) 6289f1fafeeSAl Viro nd->root.mnt = NULL; 62915570086SLinus Torvalds 63015570086SLinus Torvalds if (d_rcu_to_refcount(dentry, &dentry->d_seq, nd->seq) < 0) { 63132a7991bSAl Viro unlock_rcu_walk(); 6329f1fafeeSAl Viro return -ECHILD; 6339f1fafeeSAl Viro } 6349f1fafeeSAl Viro mntget(nd->path.mnt); 63532a7991bSAl Viro unlock_rcu_walk(); 6369f1fafeeSAl Viro } 6379f1fafeeSAl Viro 63816c2cd71SAl Viro if (likely(!(nd->flags & LOOKUP_JUMPED))) 63939159de2SJeff Layton return 0; 64039159de2SJeff Layton 641ecf3d1f1SJeff Layton if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE))) 64216c2cd71SAl Viro return 0; 64316c2cd71SAl Viro 644ecf3d1f1SJeff Layton status = dentry->d_op->d_weak_revalidate(dentry, nd->flags); 64539159de2SJeff Layton if (status > 0) 64639159de2SJeff Layton return 0; 64739159de2SJeff Layton 64816c2cd71SAl Viro if (!status) 64939159de2SJeff Layton status = -ESTALE; 65016c2cd71SAl Viro 6519f1fafeeSAl Viro path_put(&nd->path); 65239159de2SJeff Layton return status; 65339159de2SJeff Layton } 65439159de2SJeff Layton 6552a737871SAl Viro static __always_inline void set_root(struct nameidata *nd) 6562a737871SAl Viro { 657f7ad3c6bSMiklos Szeredi if (!nd->root.mnt) 658f7ad3c6bSMiklos Szeredi get_fs_root(current->fs, &nd->root); 6592a737871SAl Viro } 6602a737871SAl Viro 6616de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *); 6626de88d72SAl Viro 66331e6b01fSNick Piggin static __always_inline void set_root_rcu(struct nameidata *nd) 66431e6b01fSNick Piggin { 66531e6b01fSNick Piggin if (!nd->root.mnt) { 66631e6b01fSNick Piggin struct fs_struct *fs = current->fs; 667c28cc364SNick Piggin unsigned seq; 668c28cc364SNick Piggin 669c28cc364SNick Piggin do { 670c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 67131e6b01fSNick Piggin nd->root = fs->root; 672c1530019STim Chen nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq); 673c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 67431e6b01fSNick Piggin } 67531e6b01fSNick Piggin } 67631e6b01fSNick Piggin 677f1662356SArjan van de Ven static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link) 6781da177e4SLinus Torvalds { 67931e6b01fSNick Piggin int ret; 68031e6b01fSNick Piggin 6811da177e4SLinus Torvalds if (IS_ERR(link)) 6821da177e4SLinus Torvalds goto fail; 6831da177e4SLinus Torvalds 6841da177e4SLinus Torvalds if (*link == '/') { 6852a737871SAl Viro set_root(nd); 6861d957f9bSJan Blunck path_put(&nd->path); 6872a737871SAl Viro nd->path = nd->root; 6882a737871SAl Viro path_get(&nd->root); 68916c2cd71SAl Viro nd->flags |= LOOKUP_JUMPED; 6901da177e4SLinus Torvalds } 69131e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 692b4091d5fSChristoph Hellwig 69331e6b01fSNick Piggin ret = link_path_walk(link, nd); 69431e6b01fSNick Piggin return ret; 6951da177e4SLinus Torvalds fail: 6961d957f9bSJan Blunck path_put(&nd->path); 6971da177e4SLinus Torvalds return PTR_ERR(link); 6981da177e4SLinus Torvalds } 6991da177e4SLinus Torvalds 7001d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd) 701051d3812SIan Kent { 702051d3812SIan Kent dput(path->dentry); 7034ac91378SJan Blunck if (path->mnt != nd->path.mnt) 704051d3812SIan Kent mntput(path->mnt); 705051d3812SIan Kent } 706051d3812SIan Kent 7077b9337aaSNick Piggin static inline void path_to_nameidata(const struct path *path, 7087b9337aaSNick Piggin struct nameidata *nd) 709051d3812SIan Kent { 71031e6b01fSNick Piggin if (!(nd->flags & LOOKUP_RCU)) { 7114ac91378SJan Blunck dput(nd->path.dentry); 71231e6b01fSNick Piggin if (nd->path.mnt != path->mnt) 7134ac91378SJan Blunck mntput(nd->path.mnt); 7149a229683SHuang Shijie } 71531e6b01fSNick Piggin nd->path.mnt = path->mnt; 7164ac91378SJan Blunck nd->path.dentry = path->dentry; 717051d3812SIan Kent } 718051d3812SIan Kent 719b5fb63c1SChristoph Hellwig /* 720b5fb63c1SChristoph Hellwig * Helper to directly jump to a known parsed path from ->follow_link, 721b5fb63c1SChristoph Hellwig * caller must have taken a reference to path beforehand. 722b5fb63c1SChristoph Hellwig */ 723b5fb63c1SChristoph Hellwig void nd_jump_link(struct nameidata *nd, struct path *path) 724b5fb63c1SChristoph Hellwig { 725b5fb63c1SChristoph Hellwig path_put(&nd->path); 726b5fb63c1SChristoph Hellwig 727b5fb63c1SChristoph Hellwig nd->path = *path; 728b5fb63c1SChristoph Hellwig nd->inode = nd->path.dentry->d_inode; 729b5fb63c1SChristoph Hellwig nd->flags |= LOOKUP_JUMPED; 730b5fb63c1SChristoph Hellwig } 731b5fb63c1SChristoph Hellwig 732574197e0SAl Viro static inline void put_link(struct nameidata *nd, struct path *link, void *cookie) 733574197e0SAl Viro { 734574197e0SAl Viro struct inode *inode = link->dentry->d_inode; 7356d7b5aaeSAl Viro if (inode->i_op->put_link) 736574197e0SAl Viro inode->i_op->put_link(link->dentry, nd, cookie); 737574197e0SAl Viro path_put(link); 738574197e0SAl Viro } 739574197e0SAl Viro 740561ec64aSLinus Torvalds int sysctl_protected_symlinks __read_mostly = 0; 741561ec64aSLinus Torvalds int sysctl_protected_hardlinks __read_mostly = 0; 742800179c9SKees Cook 743800179c9SKees Cook /** 744800179c9SKees Cook * may_follow_link - Check symlink following for unsafe situations 745800179c9SKees Cook * @link: The path of the symlink 74655852635SRandy Dunlap * @nd: nameidata pathwalk data 747800179c9SKees Cook * 748800179c9SKees Cook * In the case of the sysctl_protected_symlinks sysctl being enabled, 749800179c9SKees Cook * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is 750800179c9SKees Cook * in a sticky world-writable directory. This is to protect privileged 751800179c9SKees Cook * processes from failing races against path names that may change out 752800179c9SKees Cook * from under them by way of other users creating malicious symlinks. 753800179c9SKees Cook * It will permit symlinks to be followed only when outside a sticky 754800179c9SKees Cook * world-writable directory, or when the uid of the symlink and follower 755800179c9SKees Cook * match, or when the directory owner matches the symlink's owner. 756800179c9SKees Cook * 757800179c9SKees Cook * Returns 0 if following the symlink is allowed, -ve on error. 758800179c9SKees Cook */ 759800179c9SKees Cook static inline int may_follow_link(struct path *link, struct nameidata *nd) 760800179c9SKees Cook { 761800179c9SKees Cook const struct inode *inode; 762800179c9SKees Cook const struct inode *parent; 763800179c9SKees Cook 764800179c9SKees Cook if (!sysctl_protected_symlinks) 765800179c9SKees Cook return 0; 766800179c9SKees Cook 767800179c9SKees Cook /* Allowed if owner and follower match. */ 768800179c9SKees Cook inode = link->dentry->d_inode; 76981abe27bSEric W. Biederman if (uid_eq(current_cred()->fsuid, inode->i_uid)) 770800179c9SKees Cook return 0; 771800179c9SKees Cook 772800179c9SKees Cook /* Allowed if parent directory not sticky and world-writable. */ 773800179c9SKees Cook parent = nd->path.dentry->d_inode; 774800179c9SKees Cook if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH)) 775800179c9SKees Cook return 0; 776800179c9SKees Cook 777800179c9SKees Cook /* Allowed if parent directory and link owner match. */ 77881abe27bSEric W. Biederman if (uid_eq(parent->i_uid, inode->i_uid)) 779800179c9SKees Cook return 0; 780800179c9SKees Cook 781ffd8d101SSasha Levin audit_log_link_denied("follow_link", link); 782800179c9SKees Cook path_put_conditional(link, nd); 783800179c9SKees Cook path_put(&nd->path); 784800179c9SKees Cook return -EACCES; 785800179c9SKees Cook } 786800179c9SKees Cook 787800179c9SKees Cook /** 788800179c9SKees Cook * safe_hardlink_source - Check for safe hardlink conditions 789800179c9SKees Cook * @inode: the source inode to hardlink from 790800179c9SKees Cook * 791800179c9SKees Cook * Return false if at least one of the following conditions: 792800179c9SKees Cook * - inode is not a regular file 793800179c9SKees Cook * - inode is setuid 794800179c9SKees Cook * - inode is setgid and group-exec 795800179c9SKees Cook * - access failure for read and write 796800179c9SKees Cook * 797800179c9SKees Cook * Otherwise returns true. 798800179c9SKees Cook */ 799800179c9SKees Cook static bool safe_hardlink_source(struct inode *inode) 800800179c9SKees Cook { 801800179c9SKees Cook umode_t mode = inode->i_mode; 802800179c9SKees Cook 803800179c9SKees Cook /* Special files should not get pinned to the filesystem. */ 804800179c9SKees Cook if (!S_ISREG(mode)) 805800179c9SKees Cook return false; 806800179c9SKees Cook 807800179c9SKees Cook /* Setuid files should not get pinned to the filesystem. */ 808800179c9SKees Cook if (mode & S_ISUID) 809800179c9SKees Cook return false; 810800179c9SKees Cook 811800179c9SKees Cook /* Executable setgid files should not get pinned to the filesystem. */ 812800179c9SKees Cook if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) 813800179c9SKees Cook return false; 814800179c9SKees Cook 815800179c9SKees Cook /* Hardlinking to unreadable or unwritable sources is dangerous. */ 816800179c9SKees Cook if (inode_permission(inode, MAY_READ | MAY_WRITE)) 817800179c9SKees Cook return false; 818800179c9SKees Cook 819800179c9SKees Cook return true; 820800179c9SKees Cook } 821800179c9SKees Cook 822800179c9SKees Cook /** 823800179c9SKees Cook * may_linkat - Check permissions for creating a hardlink 824800179c9SKees Cook * @link: the source to hardlink from 825800179c9SKees Cook * 826800179c9SKees Cook * Block hardlink when all of: 827800179c9SKees Cook * - sysctl_protected_hardlinks enabled 828800179c9SKees Cook * - fsuid does not match inode 829800179c9SKees Cook * - hardlink source is unsafe (see safe_hardlink_source() above) 830800179c9SKees Cook * - not CAP_FOWNER 831800179c9SKees Cook * 832800179c9SKees Cook * Returns 0 if successful, -ve on error. 833800179c9SKees Cook */ 834800179c9SKees Cook static int may_linkat(struct path *link) 835800179c9SKees Cook { 836800179c9SKees Cook const struct cred *cred; 837800179c9SKees Cook struct inode *inode; 838800179c9SKees Cook 839800179c9SKees Cook if (!sysctl_protected_hardlinks) 840800179c9SKees Cook return 0; 841800179c9SKees Cook 842800179c9SKees Cook cred = current_cred(); 843800179c9SKees Cook inode = link->dentry->d_inode; 844800179c9SKees Cook 845800179c9SKees Cook /* Source inode owner (or CAP_FOWNER) can hardlink all they like, 846800179c9SKees Cook * otherwise, it must be a safe source. 847800179c9SKees Cook */ 84881abe27bSEric W. Biederman if (uid_eq(cred->fsuid, inode->i_uid) || safe_hardlink_source(inode) || 849800179c9SKees Cook capable(CAP_FOWNER)) 850800179c9SKees Cook return 0; 851800179c9SKees Cook 852a51d9eaaSKees Cook audit_log_link_denied("linkat", link); 853800179c9SKees Cook return -EPERM; 854800179c9SKees Cook } 855800179c9SKees Cook 856def4af30SAl Viro static __always_inline int 857574197e0SAl Viro follow_link(struct path *link, struct nameidata *nd, void **p) 8581da177e4SLinus Torvalds { 8597b9337aaSNick Piggin struct dentry *dentry = link->dentry; 8606d7b5aaeSAl Viro int error; 8616d7b5aaeSAl Viro char *s; 8621da177e4SLinus Torvalds 863844a3917SAl Viro BUG_ON(nd->flags & LOOKUP_RCU); 864844a3917SAl Viro 8650e794589SAl Viro if (link->mnt == nd->path.mnt) 8660e794589SAl Viro mntget(link->mnt); 8670e794589SAl Viro 8686d7b5aaeSAl Viro error = -ELOOP; 8696d7b5aaeSAl Viro if (unlikely(current->total_link_count >= 40)) 8706d7b5aaeSAl Viro goto out_put_nd_path; 8716d7b5aaeSAl Viro 872574197e0SAl Viro cond_resched(); 873574197e0SAl Viro current->total_link_count++; 874574197e0SAl Viro 87568ac1234SAl Viro touch_atime(link); 8761da177e4SLinus Torvalds nd_set_link(nd, NULL); 877cd4e91d3SAl Viro 87836f3b4f6SAl Viro error = security_inode_follow_link(link->dentry, nd); 8796d7b5aaeSAl Viro if (error) 8806d7b5aaeSAl Viro goto out_put_nd_path; 88136f3b4f6SAl Viro 88286acdca1SAl Viro nd->last_type = LAST_BIND; 883def4af30SAl Viro *p = dentry->d_inode->i_op->follow_link(dentry, nd); 884def4af30SAl Viro error = PTR_ERR(*p); 8856d7b5aaeSAl Viro if (IS_ERR(*p)) 886408ef013SChristoph Hellwig goto out_put_nd_path; 8876d7b5aaeSAl Viro 888cc314eefSLinus Torvalds error = 0; 8896d7b5aaeSAl Viro s = nd_get_link(nd); 8906d7b5aaeSAl Viro if (s) { 8911da177e4SLinus Torvalds error = __vfs_follow_link(nd, s); 8926d7b5aaeSAl Viro if (unlikely(error)) 8936d7b5aaeSAl Viro put_link(nd, link, *p); 894b5fb63c1SChristoph Hellwig } 8956d7b5aaeSAl Viro 8966d7b5aaeSAl Viro return error; 8976d7b5aaeSAl Viro 8986d7b5aaeSAl Viro out_put_nd_path: 89998f6ef64SArnd Bergmann *p = NULL; 9006d7b5aaeSAl Viro path_put(&nd->path); 9016d7b5aaeSAl Viro path_put(link); 9021da177e4SLinus Torvalds return error; 9031da177e4SLinus Torvalds } 9041da177e4SLinus Torvalds 90531e6b01fSNick Piggin static int follow_up_rcu(struct path *path) 90631e6b01fSNick Piggin { 9070714a533SAl Viro struct mount *mnt = real_mount(path->mnt); 9080714a533SAl Viro struct mount *parent; 90931e6b01fSNick Piggin struct dentry *mountpoint; 91031e6b01fSNick Piggin 9110714a533SAl Viro parent = mnt->mnt_parent; 9120714a533SAl Viro if (&parent->mnt == path->mnt) 91331e6b01fSNick Piggin return 0; 914a73324daSAl Viro mountpoint = mnt->mnt_mountpoint; 91531e6b01fSNick Piggin path->dentry = mountpoint; 9160714a533SAl Viro path->mnt = &parent->mnt; 91731e6b01fSNick Piggin return 1; 91831e6b01fSNick Piggin } 91931e6b01fSNick Piggin 920f015f126SDavid Howells /* 921f015f126SDavid Howells * follow_up - Find the mountpoint of path's vfsmount 922f015f126SDavid Howells * 923f015f126SDavid Howells * Given a path, find the mountpoint of its source file system. 924f015f126SDavid Howells * Replace @path with the path of the mountpoint in the parent mount. 925f015f126SDavid Howells * Up is towards /. 926f015f126SDavid Howells * 927f015f126SDavid Howells * Return 1 if we went up a level and 0 if we were already at the 928f015f126SDavid Howells * root. 929f015f126SDavid Howells */ 930bab77ebfSAl Viro int follow_up(struct path *path) 9311da177e4SLinus Torvalds { 9320714a533SAl Viro struct mount *mnt = real_mount(path->mnt); 9330714a533SAl Viro struct mount *parent; 9341da177e4SLinus Torvalds struct dentry *mountpoint; 93599b7db7bSNick Piggin 936962830dfSAndi Kleen br_read_lock(&vfsmount_lock); 9370714a533SAl Viro parent = mnt->mnt_parent; 9383c0a6163SAl Viro if (parent == mnt) { 939962830dfSAndi Kleen br_read_unlock(&vfsmount_lock); 9401da177e4SLinus Torvalds return 0; 9411da177e4SLinus Torvalds } 9420714a533SAl Viro mntget(&parent->mnt); 943a73324daSAl Viro mountpoint = dget(mnt->mnt_mountpoint); 944962830dfSAndi Kleen br_read_unlock(&vfsmount_lock); 945bab77ebfSAl Viro dput(path->dentry); 946bab77ebfSAl Viro path->dentry = mountpoint; 947bab77ebfSAl Viro mntput(path->mnt); 9480714a533SAl Viro path->mnt = &parent->mnt; 9491da177e4SLinus Torvalds return 1; 9501da177e4SLinus Torvalds } 9511da177e4SLinus Torvalds 952b5c84bf6SNick Piggin /* 9539875cf80SDavid Howells * Perform an automount 9549875cf80SDavid Howells * - return -EISDIR to tell follow_managed() to stop and return the path we 9559875cf80SDavid Howells * were called with. 9561da177e4SLinus Torvalds */ 9579875cf80SDavid Howells static int follow_automount(struct path *path, unsigned flags, 9589875cf80SDavid Howells bool *need_mntput) 95931e6b01fSNick Piggin { 9609875cf80SDavid Howells struct vfsmount *mnt; 961ea5b778aSDavid Howells int err; 9629875cf80SDavid Howells 9639875cf80SDavid Howells if (!path->dentry->d_op || !path->dentry->d_op->d_automount) 9649875cf80SDavid Howells return -EREMOTE; 9659875cf80SDavid Howells 9660ec26fd0SMiklos Szeredi /* We don't want to mount if someone's just doing a stat - 9670ec26fd0SMiklos Szeredi * unless they're stat'ing a directory and appended a '/' to 9680ec26fd0SMiklos Szeredi * the name. 9690ec26fd0SMiklos Szeredi * 9700ec26fd0SMiklos Szeredi * We do, however, want to mount if someone wants to open or 9715a30d8a2SDavid Howells * create a file of any type under the mountpoint, wants to 9720ec26fd0SMiklos Szeredi * traverse through the mountpoint or wants to open the 9730ec26fd0SMiklos Szeredi * mounted directory. Also, autofs may mark negative dentries 9740ec26fd0SMiklos Szeredi * as being automount points. These will need the attentions 9750ec26fd0SMiklos Szeredi * of the daemon to instantiate them before they can be used. 9765a30d8a2SDavid Howells */ 9775a30d8a2SDavid Howells if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY | 978d94c177bSLinus Torvalds LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) && 9795a30d8a2SDavid Howells path->dentry->d_inode) 9809875cf80SDavid Howells return -EISDIR; 9810ec26fd0SMiklos Szeredi 9829875cf80SDavid Howells current->total_link_count++; 9839875cf80SDavid Howells if (current->total_link_count >= 40) 9849875cf80SDavid Howells return -ELOOP; 9859875cf80SDavid Howells 9869875cf80SDavid Howells mnt = path->dentry->d_op->d_automount(path); 9879875cf80SDavid Howells if (IS_ERR(mnt)) { 9889875cf80SDavid Howells /* 9899875cf80SDavid Howells * The filesystem is allowed to return -EISDIR here to indicate 9909875cf80SDavid Howells * it doesn't want to automount. For instance, autofs would do 9919875cf80SDavid Howells * this so that its userspace daemon can mount on this dentry. 9929875cf80SDavid Howells * 9939875cf80SDavid Howells * However, we can only permit this if it's a terminal point in 9949875cf80SDavid Howells * the path being looked up; if it wasn't then the remainder of 9959875cf80SDavid Howells * the path is inaccessible and we should say so. 9969875cf80SDavid Howells */ 99749084c3bSAl Viro if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT)) 9989875cf80SDavid Howells return -EREMOTE; 9999875cf80SDavid Howells return PTR_ERR(mnt); 100031e6b01fSNick Piggin } 1001ea5b778aSDavid Howells 10029875cf80SDavid Howells if (!mnt) /* mount collision */ 10039875cf80SDavid Howells return 0; 10049875cf80SDavid Howells 10058aef1884SAl Viro if (!*need_mntput) { 10068aef1884SAl Viro /* lock_mount() may release path->mnt on error */ 10078aef1884SAl Viro mntget(path->mnt); 10088aef1884SAl Viro *need_mntput = true; 10098aef1884SAl Viro } 101019a167afSAl Viro err = finish_automount(mnt, path); 1011ea5b778aSDavid Howells 1012ea5b778aSDavid Howells switch (err) { 1013ea5b778aSDavid Howells case -EBUSY: 1014ea5b778aSDavid Howells /* Someone else made a mount here whilst we were busy */ 101519a167afSAl Viro return 0; 1016ea5b778aSDavid Howells case 0: 10178aef1884SAl Viro path_put(path); 10189875cf80SDavid Howells path->mnt = mnt; 10199875cf80SDavid Howells path->dentry = dget(mnt->mnt_root); 10209875cf80SDavid Howells return 0; 102119a167afSAl Viro default: 102219a167afSAl Viro return err; 10239875cf80SDavid Howells } 102419a167afSAl Viro 1025ea5b778aSDavid Howells } 10269875cf80SDavid Howells 10279875cf80SDavid Howells /* 10289875cf80SDavid Howells * Handle a dentry that is managed in some way. 1029cc53ce53SDavid Howells * - Flagged for transit management (autofs) 10309875cf80SDavid Howells * - Flagged as mountpoint 10319875cf80SDavid Howells * - Flagged as automount point 10329875cf80SDavid Howells * 10339875cf80SDavid Howells * This may only be called in refwalk mode. 10349875cf80SDavid Howells * 10359875cf80SDavid Howells * Serialization is taken care of in namespace.c 10369875cf80SDavid Howells */ 10379875cf80SDavid Howells static int follow_managed(struct path *path, unsigned flags) 10389875cf80SDavid Howells { 10398aef1884SAl Viro struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */ 10409875cf80SDavid Howells unsigned managed; 10419875cf80SDavid Howells bool need_mntput = false; 10428aef1884SAl Viro int ret = 0; 10439875cf80SDavid Howells 10449875cf80SDavid Howells /* Given that we're not holding a lock here, we retain the value in a 10459875cf80SDavid Howells * local variable for each dentry as we look at it so that we don't see 10469875cf80SDavid Howells * the components of that value change under us */ 10479875cf80SDavid Howells while (managed = ACCESS_ONCE(path->dentry->d_flags), 10489875cf80SDavid Howells managed &= DCACHE_MANAGED_DENTRY, 10499875cf80SDavid Howells unlikely(managed != 0)) { 1050cc53ce53SDavid Howells /* Allow the filesystem to manage the transit without i_mutex 1051cc53ce53SDavid Howells * being held. */ 1052cc53ce53SDavid Howells if (managed & DCACHE_MANAGE_TRANSIT) { 1053cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op); 1054cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op->d_manage); 10551aed3e42SAl Viro ret = path->dentry->d_op->d_manage(path->dentry, false); 1056cc53ce53SDavid Howells if (ret < 0) 10578aef1884SAl Viro break; 1058cc53ce53SDavid Howells } 1059cc53ce53SDavid Howells 10609875cf80SDavid Howells /* Transit to a mounted filesystem. */ 10619875cf80SDavid Howells if (managed & DCACHE_MOUNTED) { 10629875cf80SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 10639875cf80SDavid Howells if (mounted) { 10649875cf80SDavid Howells dput(path->dentry); 10659875cf80SDavid Howells if (need_mntput) 1066463ffb2eSAl Viro mntput(path->mnt); 1067463ffb2eSAl Viro path->mnt = mounted; 1068463ffb2eSAl Viro path->dentry = dget(mounted->mnt_root); 10699875cf80SDavid Howells need_mntput = true; 10709875cf80SDavid Howells continue; 1071463ffb2eSAl Viro } 1072463ffb2eSAl Viro 10739875cf80SDavid Howells /* Something is mounted on this dentry in another 10749875cf80SDavid Howells * namespace and/or whatever was mounted there in this 10759875cf80SDavid Howells * namespace got unmounted before we managed to get the 10769875cf80SDavid Howells * vfsmount_lock */ 10771da177e4SLinus Torvalds } 10789875cf80SDavid Howells 10799875cf80SDavid Howells /* Handle an automount point */ 10809875cf80SDavid Howells if (managed & DCACHE_NEED_AUTOMOUNT) { 10819875cf80SDavid Howells ret = follow_automount(path, flags, &need_mntput); 10829875cf80SDavid Howells if (ret < 0) 10838aef1884SAl Viro break; 10849875cf80SDavid Howells continue; 10859875cf80SDavid Howells } 10869875cf80SDavid Howells 10879875cf80SDavid Howells /* We didn't change the current path point */ 10889875cf80SDavid Howells break; 10899875cf80SDavid Howells } 10908aef1884SAl Viro 10918aef1884SAl Viro if (need_mntput && path->mnt == mnt) 10928aef1884SAl Viro mntput(path->mnt); 10938aef1884SAl Viro if (ret == -EISDIR) 10948aef1884SAl Viro ret = 0; 1095a3fbbde7SAl Viro return ret < 0 ? ret : need_mntput; 10961da177e4SLinus Torvalds } 10971da177e4SLinus Torvalds 1098cc53ce53SDavid Howells int follow_down_one(struct path *path) 10991da177e4SLinus Torvalds { 11001da177e4SLinus Torvalds struct vfsmount *mounted; 11011da177e4SLinus Torvalds 11021c755af4SAl Viro mounted = lookup_mnt(path); 11031da177e4SLinus Torvalds if (mounted) { 11049393bd07SAl Viro dput(path->dentry); 11059393bd07SAl Viro mntput(path->mnt); 11069393bd07SAl Viro path->mnt = mounted; 11079393bd07SAl Viro path->dentry = dget(mounted->mnt_root); 11081da177e4SLinus Torvalds return 1; 11091da177e4SLinus Torvalds } 11101da177e4SLinus Torvalds return 0; 11111da177e4SLinus Torvalds } 11121da177e4SLinus Torvalds 111362a7375eSIan Kent static inline bool managed_dentry_might_block(struct dentry *dentry) 111462a7375eSIan Kent { 111562a7375eSIan Kent return (dentry->d_flags & DCACHE_MANAGE_TRANSIT && 111662a7375eSIan Kent dentry->d_op->d_manage(dentry, true) < 0); 111762a7375eSIan Kent } 111862a7375eSIan Kent 11199875cf80SDavid Howells /* 1120287548e4SAl Viro * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if 1121287548e4SAl Viro * we meet a managed dentry that would need blocking. 11229875cf80SDavid Howells */ 11239875cf80SDavid Howells static bool __follow_mount_rcu(struct nameidata *nd, struct path *path, 1124287548e4SAl Viro struct inode **inode) 11259875cf80SDavid Howells { 112662a7375eSIan Kent for (;;) { 1127c7105365SAl Viro struct mount *mounted; 112862a7375eSIan Kent /* 112962a7375eSIan Kent * Don't forget we might have a non-mountpoint managed dentry 113062a7375eSIan Kent * that wants to block transit. 113162a7375eSIan Kent */ 1132287548e4SAl Viro if (unlikely(managed_dentry_might_block(path->dentry))) 1133ab90911fSDavid Howells return false; 113462a7375eSIan Kent 113562a7375eSIan Kent if (!d_mountpoint(path->dentry)) 113662a7375eSIan Kent break; 113762a7375eSIan Kent 11389875cf80SDavid Howells mounted = __lookup_mnt(path->mnt, path->dentry, 1); 11399875cf80SDavid Howells if (!mounted) 11409875cf80SDavid Howells break; 1141c7105365SAl Viro path->mnt = &mounted->mnt; 1142c7105365SAl Viro path->dentry = mounted->mnt.mnt_root; 1143a3fbbde7SAl Viro nd->flags |= LOOKUP_JUMPED; 11449875cf80SDavid Howells nd->seq = read_seqcount_begin(&path->dentry->d_seq); 114559430262SLinus Torvalds /* 114659430262SLinus Torvalds * Update the inode too. We don't need to re-check the 114759430262SLinus Torvalds * dentry sequence number here after this d_inode read, 114859430262SLinus Torvalds * because a mount-point is always pinned. 114959430262SLinus Torvalds */ 115059430262SLinus Torvalds *inode = path->dentry->d_inode; 11519875cf80SDavid Howells } 11529875cf80SDavid Howells return true; 11539875cf80SDavid Howells } 11549875cf80SDavid Howells 1155dea39376SAl Viro static void follow_mount_rcu(struct nameidata *nd) 1156287548e4SAl Viro { 1157dea39376SAl Viro while (d_mountpoint(nd->path.dentry)) { 1158c7105365SAl Viro struct mount *mounted; 1159dea39376SAl Viro mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1); 1160287548e4SAl Viro if (!mounted) 1161287548e4SAl Viro break; 1162c7105365SAl Viro nd->path.mnt = &mounted->mnt; 1163c7105365SAl Viro nd->path.dentry = mounted->mnt.mnt_root; 1164dea39376SAl Viro nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); 1165287548e4SAl Viro } 1166287548e4SAl Viro } 1167287548e4SAl Viro 116831e6b01fSNick Piggin static int follow_dotdot_rcu(struct nameidata *nd) 116931e6b01fSNick Piggin { 117031e6b01fSNick Piggin set_root_rcu(nd); 117131e6b01fSNick Piggin 117231e6b01fSNick Piggin while (1) { 117331e6b01fSNick Piggin if (nd->path.dentry == nd->root.dentry && 117431e6b01fSNick Piggin nd->path.mnt == nd->root.mnt) { 117531e6b01fSNick Piggin break; 117631e6b01fSNick Piggin } 117731e6b01fSNick Piggin if (nd->path.dentry != nd->path.mnt->mnt_root) { 117831e6b01fSNick Piggin struct dentry *old = nd->path.dentry; 117931e6b01fSNick Piggin struct dentry *parent = old->d_parent; 118031e6b01fSNick Piggin unsigned seq; 118131e6b01fSNick Piggin 118231e6b01fSNick Piggin seq = read_seqcount_begin(&parent->d_seq); 118331e6b01fSNick Piggin if (read_seqcount_retry(&old->d_seq, nd->seq)) 1184ef7562d5SAl Viro goto failed; 118531e6b01fSNick Piggin nd->path.dentry = parent; 118631e6b01fSNick Piggin nd->seq = seq; 118731e6b01fSNick Piggin break; 118831e6b01fSNick Piggin } 118931e6b01fSNick Piggin if (!follow_up_rcu(&nd->path)) 119031e6b01fSNick Piggin break; 119131e6b01fSNick Piggin nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); 119231e6b01fSNick Piggin } 1193dea39376SAl Viro follow_mount_rcu(nd); 1194dea39376SAl Viro nd->inode = nd->path.dentry->d_inode; 119531e6b01fSNick Piggin return 0; 1196ef7562d5SAl Viro 1197ef7562d5SAl Viro failed: 1198ef7562d5SAl Viro nd->flags &= ~LOOKUP_RCU; 11995b6ca027SAl Viro if (!(nd->flags & LOOKUP_ROOT)) 1200ef7562d5SAl Viro nd->root.mnt = NULL; 120132a7991bSAl Viro unlock_rcu_walk(); 1202ef7562d5SAl Viro return -ECHILD; 120331e6b01fSNick Piggin } 120431e6b01fSNick Piggin 12059875cf80SDavid Howells /* 1206cc53ce53SDavid Howells * Follow down to the covering mount currently visible to userspace. At each 1207cc53ce53SDavid Howells * point, the filesystem owning that dentry may be queried as to whether the 1208cc53ce53SDavid Howells * caller is permitted to proceed or not. 1209cc53ce53SDavid Howells */ 12107cc90cc3SAl Viro int follow_down(struct path *path) 1211cc53ce53SDavid Howells { 1212cc53ce53SDavid Howells unsigned managed; 1213cc53ce53SDavid Howells int ret; 1214cc53ce53SDavid Howells 1215cc53ce53SDavid Howells while (managed = ACCESS_ONCE(path->dentry->d_flags), 1216cc53ce53SDavid Howells unlikely(managed & DCACHE_MANAGED_DENTRY)) { 1217cc53ce53SDavid Howells /* Allow the filesystem to manage the transit without i_mutex 1218cc53ce53SDavid Howells * being held. 1219cc53ce53SDavid Howells * 1220cc53ce53SDavid Howells * We indicate to the filesystem if someone is trying to mount 1221cc53ce53SDavid Howells * something here. This gives autofs the chance to deny anyone 1222cc53ce53SDavid Howells * other than its daemon the right to mount on its 1223cc53ce53SDavid Howells * superstructure. 1224cc53ce53SDavid Howells * 1225cc53ce53SDavid Howells * The filesystem may sleep at this point. 1226cc53ce53SDavid Howells */ 1227cc53ce53SDavid Howells if (managed & DCACHE_MANAGE_TRANSIT) { 1228cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op); 1229cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op->d_manage); 1230ab90911fSDavid Howells ret = path->dentry->d_op->d_manage( 12311aed3e42SAl Viro path->dentry, false); 1232cc53ce53SDavid Howells if (ret < 0) 1233cc53ce53SDavid Howells return ret == -EISDIR ? 0 : ret; 1234cc53ce53SDavid Howells } 1235cc53ce53SDavid Howells 1236cc53ce53SDavid Howells /* Transit to a mounted filesystem. */ 1237cc53ce53SDavid Howells if (managed & DCACHE_MOUNTED) { 1238cc53ce53SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 1239cc53ce53SDavid Howells if (!mounted) 1240cc53ce53SDavid Howells break; 1241cc53ce53SDavid Howells dput(path->dentry); 1242cc53ce53SDavid Howells mntput(path->mnt); 1243cc53ce53SDavid Howells path->mnt = mounted; 1244cc53ce53SDavid Howells path->dentry = dget(mounted->mnt_root); 1245cc53ce53SDavid Howells continue; 1246cc53ce53SDavid Howells } 1247cc53ce53SDavid Howells 1248cc53ce53SDavid Howells /* Don't handle automount points here */ 1249cc53ce53SDavid Howells break; 1250cc53ce53SDavid Howells } 1251cc53ce53SDavid Howells return 0; 1252cc53ce53SDavid Howells } 1253cc53ce53SDavid Howells 1254cc53ce53SDavid Howells /* 12559875cf80SDavid Howells * Skip to top of mountpoint pile in refwalk mode for follow_dotdot() 12569875cf80SDavid Howells */ 12579875cf80SDavid Howells static void follow_mount(struct path *path) 12589875cf80SDavid Howells { 12599875cf80SDavid Howells while (d_mountpoint(path->dentry)) { 12609875cf80SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 12619875cf80SDavid Howells if (!mounted) 12629875cf80SDavid Howells break; 12639875cf80SDavid Howells dput(path->dentry); 12649875cf80SDavid Howells mntput(path->mnt); 12659875cf80SDavid Howells path->mnt = mounted; 12669875cf80SDavid Howells path->dentry = dget(mounted->mnt_root); 12679875cf80SDavid Howells } 12689875cf80SDavid Howells } 12699875cf80SDavid Howells 127031e6b01fSNick Piggin static void follow_dotdot(struct nameidata *nd) 12711da177e4SLinus Torvalds { 12722a737871SAl Viro set_root(nd); 1273e518ddb7SAndreas Mohr 12741da177e4SLinus Torvalds while(1) { 12754ac91378SJan Blunck struct dentry *old = nd->path.dentry; 12761da177e4SLinus Torvalds 12772a737871SAl Viro if (nd->path.dentry == nd->root.dentry && 12782a737871SAl Viro nd->path.mnt == nd->root.mnt) { 12791da177e4SLinus Torvalds break; 12801da177e4SLinus Torvalds } 12814ac91378SJan Blunck if (nd->path.dentry != nd->path.mnt->mnt_root) { 12823088dd70SAl Viro /* rare case of legitimate dget_parent()... */ 12833088dd70SAl Viro nd->path.dentry = dget_parent(nd->path.dentry); 12841da177e4SLinus Torvalds dput(old); 12851da177e4SLinus Torvalds break; 12861da177e4SLinus Torvalds } 12873088dd70SAl Viro if (!follow_up(&nd->path)) 12881da177e4SLinus Torvalds break; 12891da177e4SLinus Torvalds } 129079ed0226SAl Viro follow_mount(&nd->path); 129131e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 12921da177e4SLinus Torvalds } 12931da177e4SLinus Torvalds 12941da177e4SLinus Torvalds /* 1295bad61189SMiklos Szeredi * This looks up the name in dcache, possibly revalidates the old dentry and 1296bad61189SMiklos Szeredi * allocates a new one if not found or not valid. In the need_lookup argument 1297bad61189SMiklos Szeredi * returns whether i_op->lookup is necessary. 1298bad61189SMiklos Szeredi * 1299bad61189SMiklos Szeredi * dir->d_inode->i_mutex must be held 1300baa03890SNick Piggin */ 1301bad61189SMiklos Szeredi static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir, 1302201f956eSAl Viro unsigned int flags, bool *need_lookup) 1303baa03890SNick Piggin { 1304baa03890SNick Piggin struct dentry *dentry; 1305bad61189SMiklos Szeredi int error; 1306baa03890SNick Piggin 1307bad61189SMiklos Szeredi *need_lookup = false; 1308bad61189SMiklos Szeredi dentry = d_lookup(dir, name); 1309bad61189SMiklos Szeredi if (dentry) { 131039e3c955SJeff Layton if (dentry->d_flags & DCACHE_OP_REVALIDATE) { 1311201f956eSAl Viro error = d_revalidate(dentry, flags); 1312bad61189SMiklos Szeredi if (unlikely(error <= 0)) { 1313bad61189SMiklos Szeredi if (error < 0) { 1314bad61189SMiklos Szeredi dput(dentry); 1315bad61189SMiklos Szeredi return ERR_PTR(error); 1316bad61189SMiklos Szeredi } else if (!d_invalidate(dentry)) { 1317bad61189SMiklos Szeredi dput(dentry); 1318bad61189SMiklos Szeredi dentry = NULL; 1319bad61189SMiklos Szeredi } 1320bad61189SMiklos Szeredi } 1321bad61189SMiklos Szeredi } 1322bad61189SMiklos Szeredi } 1323baa03890SNick Piggin 1324bad61189SMiklos Szeredi if (!dentry) { 1325bad61189SMiklos Szeredi dentry = d_alloc(dir, name); 1326baa03890SNick Piggin if (unlikely(!dentry)) 1327baa03890SNick Piggin return ERR_PTR(-ENOMEM); 1328baa03890SNick Piggin 1329bad61189SMiklos Szeredi *need_lookup = true; 1330baa03890SNick Piggin } 1331baa03890SNick Piggin return dentry; 1332baa03890SNick Piggin } 1333baa03890SNick Piggin 1334baa03890SNick Piggin /* 1335bad61189SMiklos Szeredi * Call i_op->lookup on the dentry. The dentry must be negative but may be 1336bad61189SMiklos Szeredi * hashed if it was pouplated with DCACHE_NEED_LOOKUP. 1337bad61189SMiklos Szeredi * 1338bad61189SMiklos Szeredi * dir->d_inode->i_mutex must be held 133944396f4bSJosef Bacik */ 1340bad61189SMiklos Szeredi static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry, 134172bd866aSAl Viro unsigned int flags) 134244396f4bSJosef Bacik { 134344396f4bSJosef Bacik struct dentry *old; 134444396f4bSJosef Bacik 134544396f4bSJosef Bacik /* Don't create child dentry for a dead directory. */ 1346bad61189SMiklos Szeredi if (unlikely(IS_DEADDIR(dir))) { 1347e188dc02SMiklos Szeredi dput(dentry); 134844396f4bSJosef Bacik return ERR_PTR(-ENOENT); 1349e188dc02SMiklos Szeredi } 135044396f4bSJosef Bacik 135172bd866aSAl Viro old = dir->i_op->lookup(dir, dentry, flags); 135244396f4bSJosef Bacik if (unlikely(old)) { 135344396f4bSJosef Bacik dput(dentry); 135444396f4bSJosef Bacik dentry = old; 135544396f4bSJosef Bacik } 135644396f4bSJosef Bacik return dentry; 135744396f4bSJosef Bacik } 135844396f4bSJosef Bacik 1359a3255546SAl Viro static struct dentry *__lookup_hash(struct qstr *name, 136072bd866aSAl Viro struct dentry *base, unsigned int flags) 1361a3255546SAl Viro { 1362bad61189SMiklos Szeredi bool need_lookup; 1363a3255546SAl Viro struct dentry *dentry; 1364a3255546SAl Viro 136572bd866aSAl Viro dentry = lookup_dcache(name, base, flags, &need_lookup); 1366bad61189SMiklos Szeredi if (!need_lookup) 1367a3255546SAl Viro return dentry; 1368bad61189SMiklos Szeredi 136972bd866aSAl Viro return lookup_real(base->d_inode, dentry, flags); 1370a3255546SAl Viro } 1371a3255546SAl Viro 137244396f4bSJosef Bacik /* 13731da177e4SLinus Torvalds * It's more convoluted than I'd like it to be, but... it's still fairly 13741da177e4SLinus Torvalds * small and for now I'd prefer to have fast path as straight as possible. 13751da177e4SLinus Torvalds * It _is_ time-critical. 13761da177e4SLinus Torvalds */ 1377e97cdc87SAl Viro static int lookup_fast(struct nameidata *nd, 137831e6b01fSNick Piggin struct path *path, struct inode **inode) 13791da177e4SLinus Torvalds { 13804ac91378SJan Blunck struct vfsmount *mnt = nd->path.mnt; 138131e6b01fSNick Piggin struct dentry *dentry, *parent = nd->path.dentry; 13825a18fff2SAl Viro int need_reval = 1; 13835a18fff2SAl Viro int status = 1; 13849875cf80SDavid Howells int err; 13859875cf80SDavid Howells 13863cac260aSAl Viro /* 1387b04f784eSNick Piggin * Rename seqlock is not required here because in the off chance 1388b04f784eSNick Piggin * of a false negative due to a concurrent rename, we're going to 1389b04f784eSNick Piggin * do the non-racy lookup, below. 1390b04f784eSNick Piggin */ 139131e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) { 139231e6b01fSNick Piggin unsigned seq; 1393da53be12SLinus Torvalds dentry = __d_lookup_rcu(parent, &nd->last, &seq); 13945a18fff2SAl Viro if (!dentry) 13955a18fff2SAl Viro goto unlazy; 13965a18fff2SAl Viro 139712f8ad4bSLinus Torvalds /* 139812f8ad4bSLinus Torvalds * This sequence count validates that the inode matches 139912f8ad4bSLinus Torvalds * the dentry name information from lookup. 140012f8ad4bSLinus Torvalds */ 140112f8ad4bSLinus Torvalds *inode = dentry->d_inode; 140212f8ad4bSLinus Torvalds if (read_seqcount_retry(&dentry->d_seq, seq)) 140312f8ad4bSLinus Torvalds return -ECHILD; 140412f8ad4bSLinus Torvalds 140512f8ad4bSLinus Torvalds /* 140612f8ad4bSLinus Torvalds * This sequence count validates that the parent had no 140712f8ad4bSLinus Torvalds * changes while we did the lookup of the dentry above. 140812f8ad4bSLinus Torvalds * 140912f8ad4bSLinus Torvalds * The memory barrier in read_seqcount_begin of child is 141012f8ad4bSLinus Torvalds * enough, we can use __read_seqcount_retry here. 141112f8ad4bSLinus Torvalds */ 141231e6b01fSNick Piggin if (__read_seqcount_retry(&parent->d_seq, nd->seq)) 141331e6b01fSNick Piggin return -ECHILD; 141431e6b01fSNick Piggin nd->seq = seq; 14155a18fff2SAl Viro 141624643087SAl Viro if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) { 14174ce16ef3SAl Viro status = d_revalidate(dentry, nd->flags); 14185a18fff2SAl Viro if (unlikely(status <= 0)) { 14195a18fff2SAl Viro if (status != -ECHILD) 14205a18fff2SAl Viro need_reval = 0; 14215a18fff2SAl Viro goto unlazy; 14225a18fff2SAl Viro } 142324643087SAl Viro } 142431e6b01fSNick Piggin path->mnt = mnt; 142531e6b01fSNick Piggin path->dentry = dentry; 1426d6e9bd25SAl Viro if (unlikely(!__follow_mount_rcu(nd, path, inode))) 1427d6e9bd25SAl Viro goto unlazy; 1428d6e9bd25SAl Viro if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT)) 1429d6e9bd25SAl Viro goto unlazy; 14309875cf80SDavid Howells return 0; 14315a18fff2SAl Viro unlazy: 143219660af7SAl Viro if (unlazy_walk(nd, dentry)) 14335a18fff2SAl Viro return -ECHILD; 14345a18fff2SAl Viro } else { 1435e97cdc87SAl Viro dentry = __d_lookup(parent, &nd->last); 143624643087SAl Viro } 14375a18fff2SAl Viro 143881e6f520SAl Viro if (unlikely(!dentry)) 143981e6f520SAl Viro goto need_lookup; 14405a18fff2SAl Viro 14415a18fff2SAl Viro if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval) 14424ce16ef3SAl Viro status = d_revalidate(dentry, nd->flags); 14435a18fff2SAl Viro if (unlikely(status <= 0)) { 14445a18fff2SAl Viro if (status < 0) { 14455a18fff2SAl Viro dput(dentry); 14465a18fff2SAl Viro return status; 14475a18fff2SAl Viro } 14485a18fff2SAl Viro if (!d_invalidate(dentry)) { 14495a18fff2SAl Viro dput(dentry); 145081e6f520SAl Viro goto need_lookup; 14515a18fff2SAl Viro } 14525a18fff2SAl Viro } 1453697f514dSMiklos Szeredi 14541da177e4SLinus Torvalds path->mnt = mnt; 14551da177e4SLinus Torvalds path->dentry = dentry; 14569875cf80SDavid Howells err = follow_managed(path, nd->flags); 145789312214SIan Kent if (unlikely(err < 0)) { 145889312214SIan Kent path_put_conditional(path, nd); 14599875cf80SDavid Howells return err; 146089312214SIan Kent } 1461a3fbbde7SAl Viro if (err) 1462a3fbbde7SAl Viro nd->flags |= LOOKUP_JUMPED; 146331e6b01fSNick Piggin *inode = path->dentry->d_inode; 14641da177e4SLinus Torvalds return 0; 146581e6f520SAl Viro 146681e6f520SAl Viro need_lookup: 1467697f514dSMiklos Szeredi return 1; 1468697f514dSMiklos Szeredi } 1469697f514dSMiklos Szeredi 1470697f514dSMiklos Szeredi /* Fast lookup failed, do it the slow way */ 1471cc2a5271SAl Viro static int lookup_slow(struct nameidata *nd, struct path *path) 1472697f514dSMiklos Szeredi { 1473697f514dSMiklos Szeredi struct dentry *dentry, *parent; 1474697f514dSMiklos Szeredi int err; 1475697f514dSMiklos Szeredi 1476697f514dSMiklos Szeredi parent = nd->path.dentry; 147781e6f520SAl Viro BUG_ON(nd->inode != parent->d_inode); 147881e6f520SAl Viro 147981e6f520SAl Viro mutex_lock(&parent->d_inode->i_mutex); 1480cc2a5271SAl Viro dentry = __lookup_hash(&nd->last, parent, nd->flags); 148181e6f520SAl Viro mutex_unlock(&parent->d_inode->i_mutex); 148281e6f520SAl Viro if (IS_ERR(dentry)) 148381e6f520SAl Viro return PTR_ERR(dentry); 1484697f514dSMiklos Szeredi path->mnt = nd->path.mnt; 1485697f514dSMiklos Szeredi path->dentry = dentry; 1486697f514dSMiklos Szeredi err = follow_managed(path, nd->flags); 1487697f514dSMiklos Szeredi if (unlikely(err < 0)) { 1488697f514dSMiklos Szeredi path_put_conditional(path, nd); 1489697f514dSMiklos Szeredi return err; 1490697f514dSMiklos Szeredi } 1491697f514dSMiklos Szeredi if (err) 1492697f514dSMiklos Szeredi nd->flags |= LOOKUP_JUMPED; 1493697f514dSMiklos Szeredi return 0; 14941da177e4SLinus Torvalds } 14951da177e4SLinus Torvalds 149652094c8aSAl Viro static inline int may_lookup(struct nameidata *nd) 149752094c8aSAl Viro { 149852094c8aSAl Viro if (nd->flags & LOOKUP_RCU) { 14994ad5abb3SAl Viro int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK); 150052094c8aSAl Viro if (err != -ECHILD) 150152094c8aSAl Viro return err; 150219660af7SAl Viro if (unlazy_walk(nd, NULL)) 150352094c8aSAl Viro return -ECHILD; 150452094c8aSAl Viro } 15054ad5abb3SAl Viro return inode_permission(nd->inode, MAY_EXEC); 150652094c8aSAl Viro } 150752094c8aSAl Viro 15089856fa1bSAl Viro static inline int handle_dots(struct nameidata *nd, int type) 15099856fa1bSAl Viro { 15109856fa1bSAl Viro if (type == LAST_DOTDOT) { 15119856fa1bSAl Viro if (nd->flags & LOOKUP_RCU) { 15129856fa1bSAl Viro if (follow_dotdot_rcu(nd)) 15139856fa1bSAl Viro return -ECHILD; 15149856fa1bSAl Viro } else 15159856fa1bSAl Viro follow_dotdot(nd); 15169856fa1bSAl Viro } 15179856fa1bSAl Viro return 0; 15189856fa1bSAl Viro } 15199856fa1bSAl Viro 1520951361f9SAl Viro static void terminate_walk(struct nameidata *nd) 1521951361f9SAl Viro { 1522951361f9SAl Viro if (!(nd->flags & LOOKUP_RCU)) { 1523951361f9SAl Viro path_put(&nd->path); 1524951361f9SAl Viro } else { 1525951361f9SAl Viro nd->flags &= ~LOOKUP_RCU; 15265b6ca027SAl Viro if (!(nd->flags & LOOKUP_ROOT)) 1527951361f9SAl Viro nd->root.mnt = NULL; 152832a7991bSAl Viro unlock_rcu_walk(); 1529951361f9SAl Viro } 1530951361f9SAl Viro } 1531951361f9SAl Viro 15323ddcd056SLinus Torvalds /* 15333ddcd056SLinus Torvalds * Do we need to follow links? We _really_ want to be able 15343ddcd056SLinus Torvalds * to do this check without having to look at inode->i_op, 15353ddcd056SLinus Torvalds * so we keep a cache of "no, this doesn't need follow_link" 15363ddcd056SLinus Torvalds * for the common case. 15373ddcd056SLinus Torvalds */ 15387813b94aSLinus Torvalds static inline int should_follow_link(struct inode *inode, int follow) 15393ddcd056SLinus Torvalds { 15403ddcd056SLinus Torvalds if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) { 15413ddcd056SLinus Torvalds if (likely(inode->i_op->follow_link)) 15423ddcd056SLinus Torvalds return follow; 15433ddcd056SLinus Torvalds 15443ddcd056SLinus Torvalds /* This gets set once for the inode lifetime */ 15453ddcd056SLinus Torvalds spin_lock(&inode->i_lock); 15463ddcd056SLinus Torvalds inode->i_opflags |= IOP_NOFOLLOW; 15473ddcd056SLinus Torvalds spin_unlock(&inode->i_lock); 15483ddcd056SLinus Torvalds } 15493ddcd056SLinus Torvalds return 0; 15503ddcd056SLinus Torvalds } 15513ddcd056SLinus Torvalds 1552ce57dfc1SAl Viro static inline int walk_component(struct nameidata *nd, struct path *path, 155321b9b073SAl Viro int follow) 1554ce57dfc1SAl Viro { 1555ce57dfc1SAl Viro struct inode *inode; 1556ce57dfc1SAl Viro int err; 1557ce57dfc1SAl Viro /* 1558ce57dfc1SAl Viro * "." and ".." are special - ".." especially so because it has 1559ce57dfc1SAl Viro * to be able to know about the current root directory and 1560ce57dfc1SAl Viro * parent relationships. 1561ce57dfc1SAl Viro */ 156221b9b073SAl Viro if (unlikely(nd->last_type != LAST_NORM)) 156321b9b073SAl Viro return handle_dots(nd, nd->last_type); 1564e97cdc87SAl Viro err = lookup_fast(nd, path, &inode); 1565ce57dfc1SAl Viro if (unlikely(err)) { 1566697f514dSMiklos Szeredi if (err < 0) 1567697f514dSMiklos Szeredi goto out_err; 1568697f514dSMiklos Szeredi 1569cc2a5271SAl Viro err = lookup_slow(nd, path); 1570697f514dSMiklos Szeredi if (err < 0) 1571697f514dSMiklos Szeredi goto out_err; 1572697f514dSMiklos Szeredi 1573697f514dSMiklos Szeredi inode = path->dentry->d_inode; 1574ce57dfc1SAl Viro } 1575697f514dSMiklos Szeredi err = -ENOENT; 1576697f514dSMiklos Szeredi if (!inode) 1577697f514dSMiklos Szeredi goto out_path_put; 1578697f514dSMiklos Szeredi 15797813b94aSLinus Torvalds if (should_follow_link(inode, follow)) { 158019660af7SAl Viro if (nd->flags & LOOKUP_RCU) { 158119660af7SAl Viro if (unlikely(unlazy_walk(nd, path->dentry))) { 1582697f514dSMiklos Szeredi err = -ECHILD; 1583697f514dSMiklos Szeredi goto out_err; 158419660af7SAl Viro } 158519660af7SAl Viro } 1586ce57dfc1SAl Viro BUG_ON(inode != path->dentry->d_inode); 1587ce57dfc1SAl Viro return 1; 1588ce57dfc1SAl Viro } 1589ce57dfc1SAl Viro path_to_nameidata(path, nd); 1590ce57dfc1SAl Viro nd->inode = inode; 1591ce57dfc1SAl Viro return 0; 1592697f514dSMiklos Szeredi 1593697f514dSMiklos Szeredi out_path_put: 1594697f514dSMiklos Szeredi path_to_nameidata(path, nd); 1595697f514dSMiklos Szeredi out_err: 1596697f514dSMiklos Szeredi terminate_walk(nd); 1597697f514dSMiklos Szeredi return err; 1598ce57dfc1SAl Viro } 1599ce57dfc1SAl Viro 16001da177e4SLinus Torvalds /* 1601b356379aSAl Viro * This limits recursive symlink follows to 8, while 1602b356379aSAl Viro * limiting consecutive symlinks to 40. 1603b356379aSAl Viro * 1604b356379aSAl Viro * Without that kind of total limit, nasty chains of consecutive 1605b356379aSAl Viro * symlinks can cause almost arbitrarily long lookups. 1606b356379aSAl Viro */ 1607b356379aSAl Viro static inline int nested_symlink(struct path *path, struct nameidata *nd) 1608b356379aSAl Viro { 1609b356379aSAl Viro int res; 1610b356379aSAl Viro 1611b356379aSAl Viro if (unlikely(current->link_count >= MAX_NESTED_LINKS)) { 1612b356379aSAl Viro path_put_conditional(path, nd); 1613b356379aSAl Viro path_put(&nd->path); 1614b356379aSAl Viro return -ELOOP; 1615b356379aSAl Viro } 16161a4022f8SErez Zadok BUG_ON(nd->depth >= MAX_NESTED_LINKS); 1617b356379aSAl Viro 1618b356379aSAl Viro nd->depth++; 1619b356379aSAl Viro current->link_count++; 1620b356379aSAl Viro 1621b356379aSAl Viro do { 1622b356379aSAl Viro struct path link = *path; 1623b356379aSAl Viro void *cookie; 1624574197e0SAl Viro 1625574197e0SAl Viro res = follow_link(&link, nd, &cookie); 16266d7b5aaeSAl Viro if (res) 16276d7b5aaeSAl Viro break; 162821b9b073SAl Viro res = walk_component(nd, path, LOOKUP_FOLLOW); 1629574197e0SAl Viro put_link(nd, &link, cookie); 1630b356379aSAl Viro } while (res > 0); 1631b356379aSAl Viro 1632b356379aSAl Viro current->link_count--; 1633b356379aSAl Viro nd->depth--; 1634b356379aSAl Viro return res; 1635b356379aSAl Viro } 1636b356379aSAl Viro 1637b356379aSAl Viro /* 16383ddcd056SLinus Torvalds * We really don't want to look at inode->i_op->lookup 16393ddcd056SLinus Torvalds * when we don't have to. So we keep a cache bit in 16403ddcd056SLinus Torvalds * the inode ->i_opflags field that says "yes, we can 16413ddcd056SLinus Torvalds * do lookup on this inode". 16423ddcd056SLinus Torvalds */ 16433ddcd056SLinus Torvalds static inline int can_lookup(struct inode *inode) 16443ddcd056SLinus Torvalds { 16453ddcd056SLinus Torvalds if (likely(inode->i_opflags & IOP_LOOKUP)) 16463ddcd056SLinus Torvalds return 1; 16473ddcd056SLinus Torvalds if (likely(!inode->i_op->lookup)) 16483ddcd056SLinus Torvalds return 0; 16493ddcd056SLinus Torvalds 16503ddcd056SLinus Torvalds /* We do this once for the lifetime of the inode */ 16513ddcd056SLinus Torvalds spin_lock(&inode->i_lock); 16523ddcd056SLinus Torvalds inode->i_opflags |= IOP_LOOKUP; 16533ddcd056SLinus Torvalds spin_unlock(&inode->i_lock); 16543ddcd056SLinus Torvalds return 1; 16553ddcd056SLinus Torvalds } 16563ddcd056SLinus Torvalds 1657bfcfaa77SLinus Torvalds /* 1658bfcfaa77SLinus Torvalds * We can do the critical dentry name comparison and hashing 1659bfcfaa77SLinus Torvalds * operations one word at a time, but we are limited to: 1660bfcfaa77SLinus Torvalds * 1661bfcfaa77SLinus Torvalds * - Architectures with fast unaligned word accesses. We could 1662bfcfaa77SLinus Torvalds * do a "get_unaligned()" if this helps and is sufficiently 1663bfcfaa77SLinus Torvalds * fast. 1664bfcfaa77SLinus Torvalds * 1665bfcfaa77SLinus Torvalds * - Little-endian machines (so that we can generate the mask 1666bfcfaa77SLinus Torvalds * of low bytes efficiently). Again, we *could* do a byte 1667bfcfaa77SLinus Torvalds * swapping load on big-endian architectures if that is not 1668bfcfaa77SLinus Torvalds * expensive enough to make the optimization worthless. 1669bfcfaa77SLinus Torvalds * 1670bfcfaa77SLinus Torvalds * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we 1671bfcfaa77SLinus Torvalds * do not trap on the (extremely unlikely) case of a page 1672bfcfaa77SLinus Torvalds * crossing operation. 1673bfcfaa77SLinus Torvalds * 1674bfcfaa77SLinus Torvalds * - Furthermore, we need an efficient 64-bit compile for the 1675bfcfaa77SLinus Torvalds * 64-bit case in order to generate the "number of bytes in 1676bfcfaa77SLinus Torvalds * the final mask". Again, that could be replaced with a 1677bfcfaa77SLinus Torvalds * efficient population count instruction or similar. 1678bfcfaa77SLinus Torvalds */ 1679bfcfaa77SLinus Torvalds #ifdef CONFIG_DCACHE_WORD_ACCESS 1680bfcfaa77SLinus Torvalds 1681f68e556eSLinus Torvalds #include <asm/word-at-a-time.h> 1682bfcfaa77SLinus Torvalds 1683f68e556eSLinus Torvalds #ifdef CONFIG_64BIT 1684bfcfaa77SLinus Torvalds 1685bfcfaa77SLinus Torvalds static inline unsigned int fold_hash(unsigned long hash) 1686bfcfaa77SLinus Torvalds { 1687bfcfaa77SLinus Torvalds hash += hash >> (8*sizeof(int)); 1688bfcfaa77SLinus Torvalds return hash; 1689bfcfaa77SLinus Torvalds } 1690bfcfaa77SLinus Torvalds 1691bfcfaa77SLinus Torvalds #else /* 32-bit case */ 1692bfcfaa77SLinus Torvalds 1693bfcfaa77SLinus Torvalds #define fold_hash(x) (x) 1694bfcfaa77SLinus Torvalds 1695bfcfaa77SLinus Torvalds #endif 1696bfcfaa77SLinus Torvalds 1697bfcfaa77SLinus Torvalds unsigned int full_name_hash(const unsigned char *name, unsigned int len) 1698bfcfaa77SLinus Torvalds { 1699bfcfaa77SLinus Torvalds unsigned long a, mask; 1700bfcfaa77SLinus Torvalds unsigned long hash = 0; 1701bfcfaa77SLinus Torvalds 1702bfcfaa77SLinus Torvalds for (;;) { 1703e419b4ccSLinus Torvalds a = load_unaligned_zeropad(name); 1704bfcfaa77SLinus Torvalds if (len < sizeof(unsigned long)) 1705bfcfaa77SLinus Torvalds break; 1706bfcfaa77SLinus Torvalds hash += a; 1707f132c5beSAl Viro hash *= 9; 1708bfcfaa77SLinus Torvalds name += sizeof(unsigned long); 1709bfcfaa77SLinus Torvalds len -= sizeof(unsigned long); 1710bfcfaa77SLinus Torvalds if (!len) 1711bfcfaa77SLinus Torvalds goto done; 1712bfcfaa77SLinus Torvalds } 1713bfcfaa77SLinus Torvalds mask = ~(~0ul << len*8); 1714bfcfaa77SLinus Torvalds hash += mask & a; 1715bfcfaa77SLinus Torvalds done: 1716bfcfaa77SLinus Torvalds return fold_hash(hash); 1717bfcfaa77SLinus Torvalds } 1718bfcfaa77SLinus Torvalds EXPORT_SYMBOL(full_name_hash); 1719bfcfaa77SLinus Torvalds 1720bfcfaa77SLinus Torvalds /* 1721bfcfaa77SLinus Torvalds * Calculate the length and hash of the path component, and 1722bfcfaa77SLinus Torvalds * return the length of the component; 1723bfcfaa77SLinus Torvalds */ 1724bfcfaa77SLinus Torvalds static inline unsigned long hash_name(const char *name, unsigned int *hashp) 1725bfcfaa77SLinus Torvalds { 172636126f8fSLinus Torvalds unsigned long a, b, adata, bdata, mask, hash, len; 172736126f8fSLinus Torvalds const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS; 1728bfcfaa77SLinus Torvalds 1729bfcfaa77SLinus Torvalds hash = a = 0; 1730bfcfaa77SLinus Torvalds len = -sizeof(unsigned long); 1731bfcfaa77SLinus Torvalds do { 1732bfcfaa77SLinus Torvalds hash = (hash + a) * 9; 1733bfcfaa77SLinus Torvalds len += sizeof(unsigned long); 1734e419b4ccSLinus Torvalds a = load_unaligned_zeropad(name+len); 173536126f8fSLinus Torvalds b = a ^ REPEAT_BYTE('/'); 173636126f8fSLinus Torvalds } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants))); 1737bfcfaa77SLinus Torvalds 173836126f8fSLinus Torvalds adata = prep_zero_mask(a, adata, &constants); 173936126f8fSLinus Torvalds bdata = prep_zero_mask(b, bdata, &constants); 174036126f8fSLinus Torvalds 174136126f8fSLinus Torvalds mask = create_zero_mask(adata | bdata); 174236126f8fSLinus Torvalds 174336126f8fSLinus Torvalds hash += a & zero_bytemask(mask); 1744bfcfaa77SLinus Torvalds *hashp = fold_hash(hash); 1745bfcfaa77SLinus Torvalds 174636126f8fSLinus Torvalds return len + find_zero(mask); 1747bfcfaa77SLinus Torvalds } 1748bfcfaa77SLinus Torvalds 1749bfcfaa77SLinus Torvalds #else 1750bfcfaa77SLinus Torvalds 17510145acc2SLinus Torvalds unsigned int full_name_hash(const unsigned char *name, unsigned int len) 17520145acc2SLinus Torvalds { 17530145acc2SLinus Torvalds unsigned long hash = init_name_hash(); 17540145acc2SLinus Torvalds while (len--) 17550145acc2SLinus Torvalds hash = partial_name_hash(*name++, hash); 17560145acc2SLinus Torvalds return end_name_hash(hash); 17570145acc2SLinus Torvalds } 1758ae942ae7SLinus Torvalds EXPORT_SYMBOL(full_name_hash); 17590145acc2SLinus Torvalds 17603ddcd056SLinus Torvalds /* 1761200e9ef7SLinus Torvalds * We know there's a real path component here of at least 1762200e9ef7SLinus Torvalds * one character. 1763200e9ef7SLinus Torvalds */ 1764200e9ef7SLinus Torvalds static inline unsigned long hash_name(const char *name, unsigned int *hashp) 1765200e9ef7SLinus Torvalds { 1766200e9ef7SLinus Torvalds unsigned long hash = init_name_hash(); 1767200e9ef7SLinus Torvalds unsigned long len = 0, c; 1768200e9ef7SLinus Torvalds 1769200e9ef7SLinus Torvalds c = (unsigned char)*name; 1770200e9ef7SLinus Torvalds do { 1771200e9ef7SLinus Torvalds len++; 1772200e9ef7SLinus Torvalds hash = partial_name_hash(c, hash); 1773200e9ef7SLinus Torvalds c = (unsigned char)name[len]; 1774200e9ef7SLinus Torvalds } while (c && c != '/'); 1775200e9ef7SLinus Torvalds *hashp = end_name_hash(hash); 1776200e9ef7SLinus Torvalds return len; 1777200e9ef7SLinus Torvalds } 1778200e9ef7SLinus Torvalds 1779bfcfaa77SLinus Torvalds #endif 1780bfcfaa77SLinus Torvalds 1781200e9ef7SLinus Torvalds /* 17821da177e4SLinus Torvalds * Name resolution. 1783ea3834d9SPrasanna Meda * This is the basic name resolution function, turning a pathname into 1784ea3834d9SPrasanna Meda * the final dentry. We expect 'base' to be positive and a directory. 17851da177e4SLinus Torvalds * 1786ea3834d9SPrasanna Meda * Returns 0 and nd will have valid dentry and mnt on success. 1787ea3834d9SPrasanna Meda * Returns error and drops reference to input namei data on failure. 17881da177e4SLinus Torvalds */ 17896de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd) 17901da177e4SLinus Torvalds { 17911da177e4SLinus Torvalds struct path next; 17921da177e4SLinus Torvalds int err; 17931da177e4SLinus Torvalds 17941da177e4SLinus Torvalds while (*name=='/') 17951da177e4SLinus Torvalds name++; 17961da177e4SLinus Torvalds if (!*name) 1797086e183aSAl Viro return 0; 17981da177e4SLinus Torvalds 17991da177e4SLinus Torvalds /* At this point we know we have a real path component. */ 18001da177e4SLinus Torvalds for(;;) { 18011da177e4SLinus Torvalds struct qstr this; 1802200e9ef7SLinus Torvalds long len; 1803fe479a58SAl Viro int type; 18041da177e4SLinus Torvalds 180552094c8aSAl Viro err = may_lookup(nd); 18061da177e4SLinus Torvalds if (err) 18071da177e4SLinus Torvalds break; 18081da177e4SLinus Torvalds 1809200e9ef7SLinus Torvalds len = hash_name(name, &this.hash); 18101da177e4SLinus Torvalds this.name = name; 1811200e9ef7SLinus Torvalds this.len = len; 18121da177e4SLinus Torvalds 1813fe479a58SAl Viro type = LAST_NORM; 1814200e9ef7SLinus Torvalds if (name[0] == '.') switch (len) { 1815fe479a58SAl Viro case 2: 1816200e9ef7SLinus Torvalds if (name[1] == '.') { 1817fe479a58SAl Viro type = LAST_DOTDOT; 181816c2cd71SAl Viro nd->flags |= LOOKUP_JUMPED; 181916c2cd71SAl Viro } 1820fe479a58SAl Viro break; 1821fe479a58SAl Viro case 1: 1822fe479a58SAl Viro type = LAST_DOT; 1823fe479a58SAl Viro } 18245a202bcdSAl Viro if (likely(type == LAST_NORM)) { 18255a202bcdSAl Viro struct dentry *parent = nd->path.dentry; 182616c2cd71SAl Viro nd->flags &= ~LOOKUP_JUMPED; 18275a202bcdSAl Viro if (unlikely(parent->d_flags & DCACHE_OP_HASH)) { 1828da53be12SLinus Torvalds err = parent->d_op->d_hash(parent, &this); 18295a202bcdSAl Viro if (err < 0) 18305a202bcdSAl Viro break; 18315a202bcdSAl Viro } 18325a202bcdSAl Viro } 1833fe479a58SAl Viro 18345f4a6a69SAl Viro nd->last = this; 18355f4a6a69SAl Viro nd->last_type = type; 18365f4a6a69SAl Viro 1837200e9ef7SLinus Torvalds if (!name[len]) 18385f4a6a69SAl Viro return 0; 1839200e9ef7SLinus Torvalds /* 1840200e9ef7SLinus Torvalds * If it wasn't NUL, we know it was '/'. Skip that 1841200e9ef7SLinus Torvalds * slash, and continue until no more slashes. 1842200e9ef7SLinus Torvalds */ 1843200e9ef7SLinus Torvalds do { 1844200e9ef7SLinus Torvalds len++; 1845200e9ef7SLinus Torvalds } while (unlikely(name[len] == '/')); 1846200e9ef7SLinus Torvalds if (!name[len]) 18475f4a6a69SAl Viro return 0; 18485f4a6a69SAl Viro 1849200e9ef7SLinus Torvalds name += len; 18501da177e4SLinus Torvalds 185121b9b073SAl Viro err = walk_component(nd, &next, LOOKUP_FOLLOW); 1852ce57dfc1SAl Viro if (err < 0) 1853ce57dfc1SAl Viro return err; 1854fe479a58SAl Viro 1855ce57dfc1SAl Viro if (err) { 1856b356379aSAl Viro err = nested_symlink(&next, nd); 18571da177e4SLinus Torvalds if (err) 1858a7472babSAl Viro return err; 185931e6b01fSNick Piggin } 18605f4a6a69SAl Viro if (!can_lookup(nd->inode)) { 18613ddcd056SLinus Torvalds err = -ENOTDIR; 18623ddcd056SLinus Torvalds break; 18635f4a6a69SAl Viro } 1864ce57dfc1SAl Viro } 1865951361f9SAl Viro terminate_walk(nd); 18661da177e4SLinus Torvalds return err; 18671da177e4SLinus Torvalds } 18681da177e4SLinus Torvalds 186970e9b357SAl Viro static int path_init(int dfd, const char *name, unsigned int flags, 187070e9b357SAl Viro struct nameidata *nd, struct file **fp) 187131e6b01fSNick Piggin { 187231e6b01fSNick Piggin int retval = 0; 187331e6b01fSNick Piggin 187431e6b01fSNick Piggin nd->last_type = LAST_ROOT; /* if there are only slashes... */ 187516c2cd71SAl Viro nd->flags = flags | LOOKUP_JUMPED; 187631e6b01fSNick Piggin nd->depth = 0; 18775b6ca027SAl Viro if (flags & LOOKUP_ROOT) { 18785b6ca027SAl Viro struct inode *inode = nd->root.dentry->d_inode; 187973d049a4SAl Viro if (*name) { 1880741b7c3fSAl Viro if (!can_lookup(inode)) 18815b6ca027SAl Viro return -ENOTDIR; 18825b6ca027SAl Viro retval = inode_permission(inode, MAY_EXEC); 18835b6ca027SAl Viro if (retval) 18845b6ca027SAl Viro return retval; 188573d049a4SAl Viro } 18865b6ca027SAl Viro nd->path = nd->root; 18875b6ca027SAl Viro nd->inode = inode; 18885b6ca027SAl Viro if (flags & LOOKUP_RCU) { 188932a7991bSAl Viro lock_rcu_walk(); 18905b6ca027SAl Viro nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 18915b6ca027SAl Viro } else { 18925b6ca027SAl Viro path_get(&nd->path); 18935b6ca027SAl Viro } 18945b6ca027SAl Viro return 0; 18955b6ca027SAl Viro } 18965b6ca027SAl Viro 189731e6b01fSNick Piggin nd->root.mnt = NULL; 189831e6b01fSNick Piggin 189931e6b01fSNick Piggin if (*name=='/') { 1900e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 190132a7991bSAl Viro lock_rcu_walk(); 1902e41f7d4eSAl Viro set_root_rcu(nd); 1903e41f7d4eSAl Viro } else { 1904e41f7d4eSAl Viro set_root(nd); 1905e41f7d4eSAl Viro path_get(&nd->root); 1906e41f7d4eSAl Viro } 190731e6b01fSNick Piggin nd->path = nd->root; 190831e6b01fSNick Piggin } else if (dfd == AT_FDCWD) { 1909e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 191031e6b01fSNick Piggin struct fs_struct *fs = current->fs; 1911c28cc364SNick Piggin unsigned seq; 191231e6b01fSNick Piggin 191332a7991bSAl Viro lock_rcu_walk(); 191431e6b01fSNick Piggin 1915c28cc364SNick Piggin do { 1916c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 191731e6b01fSNick Piggin nd->path = fs->pwd; 1918c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 1919c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 1920e41f7d4eSAl Viro } else { 1921e41f7d4eSAl Viro get_fs_pwd(current->fs, &nd->path); 1922e41f7d4eSAl Viro } 192331e6b01fSNick Piggin } else { 1924582aa64aSJeff Layton /* Caller must check execute permissions on the starting path component */ 19252903ff01SAl Viro struct fd f = fdget_raw(dfd); 192631e6b01fSNick Piggin struct dentry *dentry; 192731e6b01fSNick Piggin 19282903ff01SAl Viro if (!f.file) 19292903ff01SAl Viro return -EBADF; 193031e6b01fSNick Piggin 19312903ff01SAl Viro dentry = f.file->f_path.dentry; 193231e6b01fSNick Piggin 1933f52e0c11SAl Viro if (*name) { 1934741b7c3fSAl Viro if (!can_lookup(dentry->d_inode)) { 19352903ff01SAl Viro fdput(f); 19362903ff01SAl Viro return -ENOTDIR; 1937f52e0c11SAl Viro } 19382903ff01SAl Viro } 19392903ff01SAl Viro 19402903ff01SAl Viro nd->path = f.file->f_path; 1941e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 19422903ff01SAl Viro if (f.need_put) 19432903ff01SAl Viro *fp = f.file; 1944c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 194532a7991bSAl Viro lock_rcu_walk(); 19465590ff0dSUlrich Drepper } else { 19472903ff01SAl Viro path_get(&nd->path); 19482903ff01SAl Viro fdput(f); 19491da177e4SLinus Torvalds } 1950e41f7d4eSAl Viro } 1951e41f7d4eSAl Viro 195231e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 19539b4a9b14SAl Viro return 0; 19549b4a9b14SAl Viro } 19559b4a9b14SAl Viro 1956bd92d7feSAl Viro static inline int lookup_last(struct nameidata *nd, struct path *path) 1957bd92d7feSAl Viro { 1958bd92d7feSAl Viro if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len]) 1959bd92d7feSAl Viro nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 1960bd92d7feSAl Viro 1961bd92d7feSAl Viro nd->flags &= ~LOOKUP_PARENT; 196221b9b073SAl Viro return walk_component(nd, path, nd->flags & LOOKUP_FOLLOW); 1963bd92d7feSAl Viro } 1964bd92d7feSAl Viro 19659b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */ 1966ee0827cdSAl Viro static int path_lookupat(int dfd, const char *name, 19679b4a9b14SAl Viro unsigned int flags, struct nameidata *nd) 19689b4a9b14SAl Viro { 196970e9b357SAl Viro struct file *base = NULL; 1970bd92d7feSAl Viro struct path path; 1971bd92d7feSAl Viro int err; 197231e6b01fSNick Piggin 197331e6b01fSNick Piggin /* 197431e6b01fSNick Piggin * Path walking is largely split up into 2 different synchronisation 197531e6b01fSNick Piggin * schemes, rcu-walk and ref-walk (explained in 197631e6b01fSNick Piggin * Documentation/filesystems/path-lookup.txt). These share much of the 197731e6b01fSNick Piggin * path walk code, but some things particularly setup, cleanup, and 197831e6b01fSNick Piggin * following mounts are sufficiently divergent that functions are 197931e6b01fSNick Piggin * duplicated. Typically there is a function foo(), and its RCU 198031e6b01fSNick Piggin * analogue, foo_rcu(). 198131e6b01fSNick Piggin * 198231e6b01fSNick Piggin * -ECHILD is the error number of choice (just to avoid clashes) that 198331e6b01fSNick Piggin * is returned if some aspect of an rcu-walk fails. Such an error must 198431e6b01fSNick Piggin * be handled by restarting a traditional ref-walk (which will always 198531e6b01fSNick Piggin * be able to complete). 198631e6b01fSNick Piggin */ 1987bd92d7feSAl Viro err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base); 1988ee0827cdSAl Viro 1989bd92d7feSAl Viro if (unlikely(err)) 1990bd92d7feSAl Viro return err; 1991ee0827cdSAl Viro 1992ee0827cdSAl Viro current->total_link_count = 0; 1993bd92d7feSAl Viro err = link_path_walk(name, nd); 1994bd92d7feSAl Viro 1995bd92d7feSAl Viro if (!err && !(flags & LOOKUP_PARENT)) { 1996bd92d7feSAl Viro err = lookup_last(nd, &path); 1997bd92d7feSAl Viro while (err > 0) { 1998bd92d7feSAl Viro void *cookie; 1999bd92d7feSAl Viro struct path link = path; 2000800179c9SKees Cook err = may_follow_link(&link, nd); 2001800179c9SKees Cook if (unlikely(err)) 2002800179c9SKees Cook break; 2003bd92d7feSAl Viro nd->flags |= LOOKUP_PARENT; 2004574197e0SAl Viro err = follow_link(&link, nd, &cookie); 20056d7b5aaeSAl Viro if (err) 20066d7b5aaeSAl Viro break; 2007bd92d7feSAl Viro err = lookup_last(nd, &path); 2008574197e0SAl Viro put_link(nd, &link, cookie); 2009bd92d7feSAl Viro } 2010bd92d7feSAl Viro } 2011ee0827cdSAl Viro 20129f1fafeeSAl Viro if (!err) 20139f1fafeeSAl Viro err = complete_walk(nd); 2014bd92d7feSAl Viro 2015bd92d7feSAl Viro if (!err && nd->flags & LOOKUP_DIRECTORY) { 201605252901SAl Viro if (!can_lookup(nd->inode)) { 2017bd92d7feSAl Viro path_put(&nd->path); 2018bd23a539SAl Viro err = -ENOTDIR; 2019bd92d7feSAl Viro } 2020bd92d7feSAl Viro } 202116c2cd71SAl Viro 202270e9b357SAl Viro if (base) 202370e9b357SAl Viro fput(base); 2024ee0827cdSAl Viro 20255b6ca027SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) { 202631e6b01fSNick Piggin path_put(&nd->root); 202731e6b01fSNick Piggin nd->root.mnt = NULL; 202831e6b01fSNick Piggin } 2029bd92d7feSAl Viro return err; 203031e6b01fSNick Piggin } 203131e6b01fSNick Piggin 2032873f1eedSJeff Layton static int filename_lookup(int dfd, struct filename *name, 2033873f1eedSJeff Layton unsigned int flags, struct nameidata *nd) 2034873f1eedSJeff Layton { 2035873f1eedSJeff Layton int retval = path_lookupat(dfd, name->name, flags | LOOKUP_RCU, nd); 2036873f1eedSJeff Layton if (unlikely(retval == -ECHILD)) 2037873f1eedSJeff Layton retval = path_lookupat(dfd, name->name, flags, nd); 2038873f1eedSJeff Layton if (unlikely(retval == -ESTALE)) 2039873f1eedSJeff Layton retval = path_lookupat(dfd, name->name, 2040873f1eedSJeff Layton flags | LOOKUP_REVAL, nd); 2041873f1eedSJeff Layton 2042873f1eedSJeff Layton if (likely(!retval)) 2043adb5c247SJeff Layton audit_inode(name, nd->path.dentry, flags & LOOKUP_PARENT); 2044873f1eedSJeff Layton return retval; 2045873f1eedSJeff Layton } 2046873f1eedSJeff Layton 2047ee0827cdSAl Viro static int do_path_lookup(int dfd, const char *name, 2048ee0827cdSAl Viro unsigned int flags, struct nameidata *nd) 2049ee0827cdSAl Viro { 2050873f1eedSJeff Layton struct filename filename = { .name = name }; 2051ee0827cdSAl Viro 2052873f1eedSJeff Layton return filename_lookup(dfd, &filename, flags, nd); 20531da177e4SLinus Torvalds } 20541da177e4SLinus Torvalds 205579714f72SAl Viro /* does lookup, returns the object with parent locked */ 205679714f72SAl Viro struct dentry *kern_path_locked(const char *name, struct path *path) 20575590ff0dSUlrich Drepper { 205879714f72SAl Viro struct nameidata nd; 205979714f72SAl Viro struct dentry *d; 206079714f72SAl Viro int err = do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, &nd); 206179714f72SAl Viro if (err) 206279714f72SAl Viro return ERR_PTR(err); 206379714f72SAl Viro if (nd.last_type != LAST_NORM) { 206479714f72SAl Viro path_put(&nd.path); 206579714f72SAl Viro return ERR_PTR(-EINVAL); 206679714f72SAl Viro } 206779714f72SAl Viro mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 20681e0ea001SAl Viro d = __lookup_hash(&nd.last, nd.path.dentry, 0); 206979714f72SAl Viro if (IS_ERR(d)) { 207079714f72SAl Viro mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 207179714f72SAl Viro path_put(&nd.path); 207279714f72SAl Viro return d; 207379714f72SAl Viro } 207479714f72SAl Viro *path = nd.path; 207579714f72SAl Viro return d; 20765590ff0dSUlrich Drepper } 20775590ff0dSUlrich Drepper 2078d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path) 2079d1811465SAl Viro { 2080d1811465SAl Viro struct nameidata nd; 2081d1811465SAl Viro int res = do_path_lookup(AT_FDCWD, name, flags, &nd); 2082d1811465SAl Viro if (!res) 2083d1811465SAl Viro *path = nd.path; 2084d1811465SAl Viro return res; 2085d1811465SAl Viro } 2086d1811465SAl Viro 208716f18200SJosef 'Jeff' Sipek /** 208816f18200SJosef 'Jeff' Sipek * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair 208916f18200SJosef 'Jeff' Sipek * @dentry: pointer to dentry of the base directory 209016f18200SJosef 'Jeff' Sipek * @mnt: pointer to vfs mount of the base directory 209116f18200SJosef 'Jeff' Sipek * @name: pointer to file name 209216f18200SJosef 'Jeff' Sipek * @flags: lookup flags 2093e0a01249SAl Viro * @path: pointer to struct path to fill 209416f18200SJosef 'Jeff' Sipek */ 209516f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt, 209616f18200SJosef 'Jeff' Sipek const char *name, unsigned int flags, 2097e0a01249SAl Viro struct path *path) 209816f18200SJosef 'Jeff' Sipek { 2099e0a01249SAl Viro struct nameidata nd; 2100e0a01249SAl Viro int err; 2101e0a01249SAl Viro nd.root.dentry = dentry; 2102e0a01249SAl Viro nd.root.mnt = mnt; 2103e0a01249SAl Viro BUG_ON(flags & LOOKUP_PARENT); 21045b6ca027SAl Viro /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */ 2105e0a01249SAl Viro err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd); 2106e0a01249SAl Viro if (!err) 2107e0a01249SAl Viro *path = nd.path; 2108e0a01249SAl Viro return err; 210916f18200SJosef 'Jeff' Sipek } 211016f18200SJosef 'Jeff' Sipek 2111057f6c01SJames Morris /* 2112057f6c01SJames Morris * Restricted form of lookup. Doesn't follow links, single-component only, 2113057f6c01SJames Morris * needs parent already locked. Doesn't follow mounts. 2114057f6c01SJames Morris * SMP-safe. 2115057f6c01SJames Morris */ 2116a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd) 21171da177e4SLinus Torvalds { 211872bd866aSAl Viro return __lookup_hash(&nd->last, nd->path.dentry, nd->flags); 21191da177e4SLinus Torvalds } 21201da177e4SLinus Torvalds 2121eead1911SChristoph Hellwig /** 2122a6b91919SRandy Dunlap * lookup_one_len - filesystem helper to lookup single pathname component 2123eead1911SChristoph Hellwig * @name: pathname component to lookup 2124eead1911SChristoph Hellwig * @base: base directory to lookup from 2125eead1911SChristoph Hellwig * @len: maximum length @len should be interpreted to 2126eead1911SChristoph Hellwig * 2127a6b91919SRandy Dunlap * Note that this routine is purely a helper for filesystem usage and should 2128a6b91919SRandy Dunlap * not be called by generic code. Also note that by using this function the 2129eead1911SChristoph Hellwig * nameidata argument is passed to the filesystem methods and a filesystem 2130eead1911SChristoph Hellwig * using this helper needs to be prepared for that. 2131eead1911SChristoph Hellwig */ 2132057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len) 2133057f6c01SJames Morris { 2134057f6c01SJames Morris struct qstr this; 21356a96ba54SAl Viro unsigned int c; 2136cda309deSMiklos Szeredi int err; 2137057f6c01SJames Morris 21382f9092e1SDavid Woodhouse WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex)); 21392f9092e1SDavid Woodhouse 21406a96ba54SAl Viro this.name = name; 21416a96ba54SAl Viro this.len = len; 21420145acc2SLinus Torvalds this.hash = full_name_hash(name, len); 21436a96ba54SAl Viro if (!len) 21446a96ba54SAl Viro return ERR_PTR(-EACCES); 21456a96ba54SAl Viro 214621d8a15aSAl Viro if (unlikely(name[0] == '.')) { 214721d8a15aSAl Viro if (len < 2 || (len == 2 && name[1] == '.')) 214821d8a15aSAl Viro return ERR_PTR(-EACCES); 214921d8a15aSAl Viro } 215021d8a15aSAl Viro 21516a96ba54SAl Viro while (len--) { 21526a96ba54SAl Viro c = *(const unsigned char *)name++; 21536a96ba54SAl Viro if (c == '/' || c == '\0') 21546a96ba54SAl Viro return ERR_PTR(-EACCES); 21556a96ba54SAl Viro } 21565a202bcdSAl Viro /* 21575a202bcdSAl Viro * See if the low-level filesystem might want 21585a202bcdSAl Viro * to use its own hash.. 21595a202bcdSAl Viro */ 21605a202bcdSAl Viro if (base->d_flags & DCACHE_OP_HASH) { 2161da53be12SLinus Torvalds int err = base->d_op->d_hash(base, &this); 21625a202bcdSAl Viro if (err < 0) 21635a202bcdSAl Viro return ERR_PTR(err); 21645a202bcdSAl Viro } 2165eead1911SChristoph Hellwig 2166cda309deSMiklos Szeredi err = inode_permission(base->d_inode, MAY_EXEC); 2167cda309deSMiklos Szeredi if (err) 2168cda309deSMiklos Szeredi return ERR_PTR(err); 2169cda309deSMiklos Szeredi 217072bd866aSAl Viro return __lookup_hash(&this, base, 0); 2171057f6c01SJames Morris } 2172057f6c01SJames Morris 21731fa1e7f6SAndy Whitcroft int user_path_at_empty(int dfd, const char __user *name, unsigned flags, 21741fa1e7f6SAndy Whitcroft struct path *path, int *empty) 21751da177e4SLinus Torvalds { 21762d8f3038SAl Viro struct nameidata nd; 217791a27b2aSJeff Layton struct filename *tmp = getname_flags(name, flags, empty); 21781da177e4SLinus Torvalds int err = PTR_ERR(tmp); 21791da177e4SLinus Torvalds if (!IS_ERR(tmp)) { 21802d8f3038SAl Viro 21812d8f3038SAl Viro BUG_ON(flags & LOOKUP_PARENT); 21822d8f3038SAl Viro 2183873f1eedSJeff Layton err = filename_lookup(dfd, tmp, flags, &nd); 21841da177e4SLinus Torvalds putname(tmp); 21852d8f3038SAl Viro if (!err) 21862d8f3038SAl Viro *path = nd.path; 21871da177e4SLinus Torvalds } 21881da177e4SLinus Torvalds return err; 21891da177e4SLinus Torvalds } 21901da177e4SLinus Torvalds 21911fa1e7f6SAndy Whitcroft int user_path_at(int dfd, const char __user *name, unsigned flags, 21921fa1e7f6SAndy Whitcroft struct path *path) 21931fa1e7f6SAndy Whitcroft { 2194f7493e5dSLinus Torvalds return user_path_at_empty(dfd, name, flags, path, NULL); 21951fa1e7f6SAndy Whitcroft } 21961fa1e7f6SAndy Whitcroft 2197873f1eedSJeff Layton /* 2198873f1eedSJeff Layton * NB: most callers don't do anything directly with the reference to the 2199873f1eedSJeff Layton * to struct filename, but the nd->last pointer points into the name string 2200873f1eedSJeff Layton * allocated by getname. So we must hold the reference to it until all 2201873f1eedSJeff Layton * path-walking is complete. 2202873f1eedSJeff Layton */ 220391a27b2aSJeff Layton static struct filename * 22049e790bd6SJeff Layton user_path_parent(int dfd, const char __user *path, struct nameidata *nd, 22059e790bd6SJeff Layton unsigned int flags) 22062ad94ae6SAl Viro { 220791a27b2aSJeff Layton struct filename *s = getname(path); 22082ad94ae6SAl Viro int error; 22092ad94ae6SAl Viro 22109e790bd6SJeff Layton /* only LOOKUP_REVAL is allowed in extra flags */ 22119e790bd6SJeff Layton flags &= LOOKUP_REVAL; 22129e790bd6SJeff Layton 22132ad94ae6SAl Viro if (IS_ERR(s)) 221491a27b2aSJeff Layton return s; 22152ad94ae6SAl Viro 22169e790bd6SJeff Layton error = filename_lookup(dfd, s, flags | LOOKUP_PARENT, nd); 221791a27b2aSJeff Layton if (error) { 22182ad94ae6SAl Viro putname(s); 221991a27b2aSJeff Layton return ERR_PTR(error); 222091a27b2aSJeff Layton } 22212ad94ae6SAl Viro 222291a27b2aSJeff Layton return s; 22232ad94ae6SAl Viro } 22242ad94ae6SAl Viro 22251da177e4SLinus Torvalds /* 22261da177e4SLinus Torvalds * It's inline, so penalty for filesystems that don't use sticky bit is 22271da177e4SLinus Torvalds * minimal. 22281da177e4SLinus Torvalds */ 22291da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode) 22301da177e4SLinus Torvalds { 22318e96e3b7SEric W. Biederman kuid_t fsuid = current_fsuid(); 2232da9592edSDavid Howells 22331da177e4SLinus Torvalds if (!(dir->i_mode & S_ISVTX)) 22341da177e4SLinus Torvalds return 0; 22358e96e3b7SEric W. Biederman if (uid_eq(inode->i_uid, fsuid)) 22361da177e4SLinus Torvalds return 0; 22378e96e3b7SEric W. Biederman if (uid_eq(dir->i_uid, fsuid)) 22381da177e4SLinus Torvalds return 0; 22391a48e2acSEric W. Biederman return !inode_capable(inode, CAP_FOWNER); 22401da177e4SLinus Torvalds } 22411da177e4SLinus Torvalds 22421da177e4SLinus Torvalds /* 22431da177e4SLinus Torvalds * Check whether we can remove a link victim from directory dir, check 22441da177e4SLinus Torvalds * whether the type of victim is right. 22451da177e4SLinus Torvalds * 1. We can't do it if dir is read-only (done in permission()) 22461da177e4SLinus Torvalds * 2. We should have write and exec permissions on dir 22471da177e4SLinus Torvalds * 3. We can't remove anything from append-only dir 22481da177e4SLinus Torvalds * 4. We can't do anything with immutable dir (done in permission()) 22491da177e4SLinus Torvalds * 5. If the sticky bit on dir is set we should either 22501da177e4SLinus Torvalds * a. be owner of dir, or 22511da177e4SLinus Torvalds * b. be owner of victim, or 22521da177e4SLinus Torvalds * c. have CAP_FOWNER capability 22531da177e4SLinus Torvalds * 6. If the victim is append-only or immutable we can't do antyhing with 22541da177e4SLinus Torvalds * links pointing to it. 22551da177e4SLinus Torvalds * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. 22561da177e4SLinus Torvalds * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. 22571da177e4SLinus Torvalds * 9. We can't remove a root or mountpoint. 22581da177e4SLinus Torvalds * 10. We don't allow removal of NFS sillyrenamed files; it's handled by 22591da177e4SLinus Torvalds * nfs_async_unlink(). 22601da177e4SLinus Torvalds */ 2261858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir) 22621da177e4SLinus Torvalds { 22631da177e4SLinus Torvalds int error; 22641da177e4SLinus Torvalds 22651da177e4SLinus Torvalds if (!victim->d_inode) 22661da177e4SLinus Torvalds return -ENOENT; 22671da177e4SLinus Torvalds 22681da177e4SLinus Torvalds BUG_ON(victim->d_parent->d_inode != dir); 22694fa6b5ecSJeff Layton audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE); 22701da177e4SLinus Torvalds 2271f419a2e3SAl Viro error = inode_permission(dir, MAY_WRITE | MAY_EXEC); 22721da177e4SLinus Torvalds if (error) 22731da177e4SLinus Torvalds return error; 22741da177e4SLinus Torvalds if (IS_APPEND(dir)) 22751da177e4SLinus Torvalds return -EPERM; 22761da177e4SLinus Torvalds if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)|| 2277f9454548SHugh Dickins IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode)) 22781da177e4SLinus Torvalds return -EPERM; 22791da177e4SLinus Torvalds if (isdir) { 22801da177e4SLinus Torvalds if (!S_ISDIR(victim->d_inode->i_mode)) 22811da177e4SLinus Torvalds return -ENOTDIR; 22821da177e4SLinus Torvalds if (IS_ROOT(victim)) 22831da177e4SLinus Torvalds return -EBUSY; 22841da177e4SLinus Torvalds } else if (S_ISDIR(victim->d_inode->i_mode)) 22851da177e4SLinus Torvalds return -EISDIR; 22861da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 22871da177e4SLinus Torvalds return -ENOENT; 22881da177e4SLinus Torvalds if (victim->d_flags & DCACHE_NFSFS_RENAMED) 22891da177e4SLinus Torvalds return -EBUSY; 22901da177e4SLinus Torvalds return 0; 22911da177e4SLinus Torvalds } 22921da177e4SLinus Torvalds 22931da177e4SLinus Torvalds /* Check whether we can create an object with dentry child in directory 22941da177e4SLinus Torvalds * dir. 22951da177e4SLinus Torvalds * 1. We can't do it if child already exists (open has special treatment for 22961da177e4SLinus Torvalds * this case, but since we are inlined it's OK) 22971da177e4SLinus Torvalds * 2. We can't do it if dir is read-only (done in permission()) 22981da177e4SLinus Torvalds * 3. We should have write and exec permissions on dir 22991da177e4SLinus Torvalds * 4. We can't do it if dir is immutable (done in permission()) 23001da177e4SLinus Torvalds */ 2301a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child) 23021da177e4SLinus Torvalds { 23031da177e4SLinus Torvalds if (child->d_inode) 23041da177e4SLinus Torvalds return -EEXIST; 23051da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 23061da177e4SLinus Torvalds return -ENOENT; 2307f419a2e3SAl Viro return inode_permission(dir, MAY_WRITE | MAY_EXEC); 23081da177e4SLinus Torvalds } 23091da177e4SLinus Torvalds 23101da177e4SLinus Torvalds /* 23111da177e4SLinus Torvalds * p1 and p2 should be directories on the same fs. 23121da177e4SLinus Torvalds */ 23131da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2) 23141da177e4SLinus Torvalds { 23151da177e4SLinus Torvalds struct dentry *p; 23161da177e4SLinus Torvalds 23171da177e4SLinus Torvalds if (p1 == p2) { 2318f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 23191da177e4SLinus Torvalds return NULL; 23201da177e4SLinus Torvalds } 23211da177e4SLinus Torvalds 2322a11f3a05SArjan van de Ven mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 23231da177e4SLinus Torvalds 2324e2761a11SOGAWA Hirofumi p = d_ancestor(p2, p1); 2325e2761a11SOGAWA Hirofumi if (p) { 2326f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT); 2327f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD); 23281da177e4SLinus Torvalds return p; 23291da177e4SLinus Torvalds } 23301da177e4SLinus Torvalds 2331e2761a11SOGAWA Hirofumi p = d_ancestor(p1, p2); 2332e2761a11SOGAWA Hirofumi if (p) { 2333f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 2334f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 23351da177e4SLinus Torvalds return p; 23361da177e4SLinus Torvalds } 23371da177e4SLinus Torvalds 2338f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 2339f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 23401da177e4SLinus Torvalds return NULL; 23411da177e4SLinus Torvalds } 23421da177e4SLinus Torvalds 23431da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2) 23441da177e4SLinus Torvalds { 23451b1dcc1bSJes Sorensen mutex_unlock(&p1->d_inode->i_mutex); 23461da177e4SLinus Torvalds if (p1 != p2) { 23471b1dcc1bSJes Sorensen mutex_unlock(&p2->d_inode->i_mutex); 2348a11f3a05SArjan van de Ven mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 23491da177e4SLinus Torvalds } 23501da177e4SLinus Torvalds } 23511da177e4SLinus Torvalds 23524acdaf27SAl Viro int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode, 2353312b63fbSAl Viro bool want_excl) 23541da177e4SLinus Torvalds { 2355a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 23561da177e4SLinus Torvalds if (error) 23571da177e4SLinus Torvalds return error; 23581da177e4SLinus Torvalds 2359acfa4380SAl Viro if (!dir->i_op->create) 23601da177e4SLinus Torvalds return -EACCES; /* shouldn't it be ENOSYS? */ 23611da177e4SLinus Torvalds mode &= S_IALLUGO; 23621da177e4SLinus Torvalds mode |= S_IFREG; 23631da177e4SLinus Torvalds error = security_inode_create(dir, dentry, mode); 23641da177e4SLinus Torvalds if (error) 23651da177e4SLinus Torvalds return error; 2366312b63fbSAl Viro error = dir->i_op->create(dir, dentry, mode, want_excl); 2367a74574aaSStephen Smalley if (!error) 2368f38aa942SAmy Griffis fsnotify_create(dir, dentry); 23691da177e4SLinus Torvalds return error; 23701da177e4SLinus Torvalds } 23711da177e4SLinus Torvalds 237273d049a4SAl Viro static int may_open(struct path *path, int acc_mode, int flag) 23731da177e4SLinus Torvalds { 23743fb64190SChristoph Hellwig struct dentry *dentry = path->dentry; 23751da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 23761da177e4SLinus Torvalds int error; 23771da177e4SLinus Torvalds 2378bcda7652SAl Viro /* O_PATH? */ 2379bcda7652SAl Viro if (!acc_mode) 2380bcda7652SAl Viro return 0; 2381bcda7652SAl Viro 23821da177e4SLinus Torvalds if (!inode) 23831da177e4SLinus Torvalds return -ENOENT; 23841da177e4SLinus Torvalds 2385c8fe8f30SChristoph Hellwig switch (inode->i_mode & S_IFMT) { 2386c8fe8f30SChristoph Hellwig case S_IFLNK: 23871da177e4SLinus Torvalds return -ELOOP; 2388c8fe8f30SChristoph Hellwig case S_IFDIR: 2389c8fe8f30SChristoph Hellwig if (acc_mode & MAY_WRITE) 23901da177e4SLinus Torvalds return -EISDIR; 2391c8fe8f30SChristoph Hellwig break; 2392c8fe8f30SChristoph Hellwig case S_IFBLK: 2393c8fe8f30SChristoph Hellwig case S_IFCHR: 23943fb64190SChristoph Hellwig if (path->mnt->mnt_flags & MNT_NODEV) 23951da177e4SLinus Torvalds return -EACCES; 2396c8fe8f30SChristoph Hellwig /*FALLTHRU*/ 2397c8fe8f30SChristoph Hellwig case S_IFIFO: 2398c8fe8f30SChristoph Hellwig case S_IFSOCK: 23991da177e4SLinus Torvalds flag &= ~O_TRUNC; 2400c8fe8f30SChristoph Hellwig break; 24014a3fd211SDave Hansen } 2402b41572e9SDave Hansen 24033fb64190SChristoph Hellwig error = inode_permission(inode, acc_mode); 2404b41572e9SDave Hansen if (error) 2405b41572e9SDave Hansen return error; 24066146f0d5SMimi Zohar 24071da177e4SLinus Torvalds /* 24081da177e4SLinus Torvalds * An append-only file must be opened in append mode for writing. 24091da177e4SLinus Torvalds */ 24101da177e4SLinus Torvalds if (IS_APPEND(inode)) { 24118737c930SAl Viro if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND)) 24127715b521SAl Viro return -EPERM; 24131da177e4SLinus Torvalds if (flag & O_TRUNC) 24147715b521SAl Viro return -EPERM; 24151da177e4SLinus Torvalds } 24161da177e4SLinus Torvalds 24171da177e4SLinus Torvalds /* O_NOATIME can only be set by the owner or superuser */ 24182e149670SSerge E. Hallyn if (flag & O_NOATIME && !inode_owner_or_capable(inode)) 24197715b521SAl Viro return -EPERM; 24201da177e4SLinus Torvalds 2421f3c7691eSJ. Bruce Fields return 0; 24227715b521SAl Viro } 24237715b521SAl Viro 2424e1181ee6SJeff Layton static int handle_truncate(struct file *filp) 24257715b521SAl Viro { 2426e1181ee6SJeff Layton struct path *path = &filp->f_path; 24277715b521SAl Viro struct inode *inode = path->dentry->d_inode; 24287715b521SAl Viro int error = get_write_access(inode); 24291da177e4SLinus Torvalds if (error) 24307715b521SAl Viro return error; 24311da177e4SLinus Torvalds /* 24321da177e4SLinus Torvalds * Refuse to truncate files with mandatory locks held on them. 24331da177e4SLinus Torvalds */ 24341da177e4SLinus Torvalds error = locks_verify_locked(inode); 2435be6d3e56SKentaro Takeda if (!error) 2436ea0d3ab2STetsuo Handa error = security_path_truncate(path); 24371da177e4SLinus Torvalds if (!error) { 24387715b521SAl Viro error = do_truncate(path->dentry, 0, 2439d139d7ffSMiklos Szeredi ATTR_MTIME|ATTR_CTIME|ATTR_OPEN, 2440e1181ee6SJeff Layton filp); 24411da177e4SLinus Torvalds } 24421da177e4SLinus Torvalds put_write_access(inode); 2443acd0c935SMimi Zohar return error; 24441da177e4SLinus Torvalds } 24451da177e4SLinus Torvalds 2446d57999e1SDave Hansen static inline int open_to_namei_flags(int flag) 2447d57999e1SDave Hansen { 24488a5e929dSAl Viro if ((flag & O_ACCMODE) == 3) 24498a5e929dSAl Viro flag--; 2450d57999e1SDave Hansen return flag; 2451d57999e1SDave Hansen } 2452d57999e1SDave Hansen 2453d18e9008SMiklos Szeredi static int may_o_create(struct path *dir, struct dentry *dentry, umode_t mode) 2454d18e9008SMiklos Szeredi { 2455d18e9008SMiklos Szeredi int error = security_path_mknod(dir, dentry, mode, 0); 2456d18e9008SMiklos Szeredi if (error) 2457d18e9008SMiklos Szeredi return error; 2458d18e9008SMiklos Szeredi 2459d18e9008SMiklos Szeredi error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC); 2460d18e9008SMiklos Szeredi if (error) 2461d18e9008SMiklos Szeredi return error; 2462d18e9008SMiklos Szeredi 2463d18e9008SMiklos Szeredi return security_inode_create(dir->dentry->d_inode, dentry, mode); 2464d18e9008SMiklos Szeredi } 2465d18e9008SMiklos Szeredi 24661acf0af9SDavid Howells /* 24671acf0af9SDavid Howells * Attempt to atomically look up, create and open a file from a negative 24681acf0af9SDavid Howells * dentry. 24691acf0af9SDavid Howells * 24701acf0af9SDavid Howells * Returns 0 if successful. The file will have been created and attached to 24711acf0af9SDavid Howells * @file by the filesystem calling finish_open(). 24721acf0af9SDavid Howells * 24731acf0af9SDavid Howells * Returns 1 if the file was looked up only or didn't need creating. The 24741acf0af9SDavid Howells * caller will need to perform the open themselves. @path will have been 24751acf0af9SDavid Howells * updated to point to the new dentry. This may be negative. 24761acf0af9SDavid Howells * 24771acf0af9SDavid Howells * Returns an error code otherwise. 24781acf0af9SDavid Howells */ 24792675a4ebSAl Viro static int atomic_open(struct nameidata *nd, struct dentry *dentry, 248030d90494SAl Viro struct path *path, struct file *file, 2481015c3bbcSMiklos Szeredi const struct open_flags *op, 248264894cf8SAl Viro bool got_write, bool need_lookup, 248347237687SAl Viro int *opened) 2484d18e9008SMiklos Szeredi { 2485d18e9008SMiklos Szeredi struct inode *dir = nd->path.dentry->d_inode; 2486d18e9008SMiklos Szeredi unsigned open_flag = open_to_namei_flags(op->open_flag); 2487d18e9008SMiklos Szeredi umode_t mode; 2488d18e9008SMiklos Szeredi int error; 2489d18e9008SMiklos Szeredi int acc_mode; 2490d18e9008SMiklos Szeredi int create_error = 0; 2491d18e9008SMiklos Szeredi struct dentry *const DENTRY_NOT_SET = (void *) -1UL; 2492d18e9008SMiklos Szeredi 2493d18e9008SMiklos Szeredi BUG_ON(dentry->d_inode); 2494d18e9008SMiklos Szeredi 2495d18e9008SMiklos Szeredi /* Don't create child dentry for a dead directory. */ 2496d18e9008SMiklos Szeredi if (unlikely(IS_DEADDIR(dir))) { 24972675a4ebSAl Viro error = -ENOENT; 2498d18e9008SMiklos Szeredi goto out; 2499d18e9008SMiklos Szeredi } 2500d18e9008SMiklos Szeredi 250162b259d8SMiklos Szeredi mode = op->mode; 2502d18e9008SMiklos Szeredi if ((open_flag & O_CREAT) && !IS_POSIXACL(dir)) 2503d18e9008SMiklos Szeredi mode &= ~current_umask(); 2504d18e9008SMiklos Szeredi 2505f8310c59SAl Viro if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT)) { 2506d18e9008SMiklos Szeredi open_flag &= ~O_TRUNC; 250747237687SAl Viro *opened |= FILE_CREATED; 2508d18e9008SMiklos Szeredi } 2509d18e9008SMiklos Szeredi 2510d18e9008SMiklos Szeredi /* 2511d18e9008SMiklos Szeredi * Checking write permission is tricky, bacuse we don't know if we are 2512d18e9008SMiklos Szeredi * going to actually need it: O_CREAT opens should work as long as the 2513d18e9008SMiklos Szeredi * file exists. But checking existence breaks atomicity. The trick is 2514d18e9008SMiklos Szeredi * to check access and if not granted clear O_CREAT from the flags. 2515d18e9008SMiklos Szeredi * 2516d18e9008SMiklos Szeredi * Another problem is returing the "right" error value (e.g. for an 2517d18e9008SMiklos Szeredi * O_EXCL open we want to return EEXIST not EROFS). 2518d18e9008SMiklos Szeredi */ 251964894cf8SAl Viro if (((open_flag & (O_CREAT | O_TRUNC)) || 252064894cf8SAl Viro (open_flag & O_ACCMODE) != O_RDONLY) && unlikely(!got_write)) { 252164894cf8SAl Viro if (!(open_flag & O_CREAT)) { 2522d18e9008SMiklos Szeredi /* 2523d18e9008SMiklos Szeredi * No O_CREATE -> atomicity not a requirement -> fall 2524d18e9008SMiklos Szeredi * back to lookup + open 2525d18e9008SMiklos Szeredi */ 2526d18e9008SMiklos Szeredi goto no_open; 2527d18e9008SMiklos Szeredi } else if (open_flag & (O_EXCL | O_TRUNC)) { 2528d18e9008SMiklos Szeredi /* Fall back and fail with the right error */ 252964894cf8SAl Viro create_error = -EROFS; 2530d18e9008SMiklos Szeredi goto no_open; 2531d18e9008SMiklos Szeredi } else { 2532d18e9008SMiklos Szeredi /* No side effects, safe to clear O_CREAT */ 253364894cf8SAl Viro create_error = -EROFS; 2534d18e9008SMiklos Szeredi open_flag &= ~O_CREAT; 2535d18e9008SMiklos Szeredi } 2536d18e9008SMiklos Szeredi } 2537d18e9008SMiklos Szeredi 2538d18e9008SMiklos Szeredi if (open_flag & O_CREAT) { 253938227f78SMiklos Szeredi error = may_o_create(&nd->path, dentry, mode); 2540d18e9008SMiklos Szeredi if (error) { 2541d18e9008SMiklos Szeredi create_error = error; 2542d18e9008SMiklos Szeredi if (open_flag & O_EXCL) 2543d18e9008SMiklos Szeredi goto no_open; 2544d18e9008SMiklos Szeredi open_flag &= ~O_CREAT; 2545d18e9008SMiklos Szeredi } 2546d18e9008SMiklos Szeredi } 2547d18e9008SMiklos Szeredi 2548d18e9008SMiklos Szeredi if (nd->flags & LOOKUP_DIRECTORY) 2549d18e9008SMiklos Szeredi open_flag |= O_DIRECTORY; 2550d18e9008SMiklos Szeredi 255130d90494SAl Viro file->f_path.dentry = DENTRY_NOT_SET; 255230d90494SAl Viro file->f_path.mnt = nd->path.mnt; 255330d90494SAl Viro error = dir->i_op->atomic_open(dir, dentry, file, open_flag, mode, 255447237687SAl Viro opened); 2555d9585277SAl Viro if (error < 0) { 2556d9585277SAl Viro if (create_error && error == -ENOENT) 2557d9585277SAl Viro error = create_error; 2558d18e9008SMiklos Szeredi goto out; 2559d18e9008SMiklos Szeredi } 2560d18e9008SMiklos Szeredi 2561d18e9008SMiklos Szeredi acc_mode = op->acc_mode; 256247237687SAl Viro if (*opened & FILE_CREATED) { 2563d18e9008SMiklos Szeredi fsnotify_create(dir, dentry); 2564d18e9008SMiklos Szeredi acc_mode = MAY_OPEN; 2565d18e9008SMiklos Szeredi } 2566d18e9008SMiklos Szeredi 2567d9585277SAl Viro if (error) { /* returned 1, that is */ 256830d90494SAl Viro if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) { 25692675a4ebSAl Viro error = -EIO; 2570d18e9008SMiklos Szeredi goto out; 2571d18e9008SMiklos Szeredi } 257230d90494SAl Viro if (file->f_path.dentry) { 2573d18e9008SMiklos Szeredi dput(dentry); 257430d90494SAl Viro dentry = file->f_path.dentry; 2575d18e9008SMiklos Szeredi } 257662b2ce96SSage Weil if (create_error && dentry->d_inode == NULL) { 257762b2ce96SSage Weil error = create_error; 257862b2ce96SSage Weil goto out; 257962b2ce96SSage Weil } 2580d18e9008SMiklos Szeredi goto looked_up; 2581d18e9008SMiklos Szeredi } 2582d18e9008SMiklos Szeredi 2583d18e9008SMiklos Szeredi /* 2584d18e9008SMiklos Szeredi * We didn't have the inode before the open, so check open permission 2585d18e9008SMiklos Szeredi * here. 2586d18e9008SMiklos Szeredi */ 25872675a4ebSAl Viro error = may_open(&file->f_path, acc_mode, open_flag); 25882675a4ebSAl Viro if (error) 25892675a4ebSAl Viro fput(file); 2590d18e9008SMiklos Szeredi 2591d18e9008SMiklos Szeredi out: 2592d18e9008SMiklos Szeredi dput(dentry); 25932675a4ebSAl Viro return error; 2594d18e9008SMiklos Szeredi 2595d18e9008SMiklos Szeredi no_open: 2596d18e9008SMiklos Szeredi if (need_lookup) { 259772bd866aSAl Viro dentry = lookup_real(dir, dentry, nd->flags); 2598d18e9008SMiklos Szeredi if (IS_ERR(dentry)) 25992675a4ebSAl Viro return PTR_ERR(dentry); 2600d18e9008SMiklos Szeredi 2601d18e9008SMiklos Szeredi if (create_error) { 2602d18e9008SMiklos Szeredi int open_flag = op->open_flag; 2603d18e9008SMiklos Szeredi 26042675a4ebSAl Viro error = create_error; 2605d18e9008SMiklos Szeredi if ((open_flag & O_EXCL)) { 2606d18e9008SMiklos Szeredi if (!dentry->d_inode) 2607d18e9008SMiklos Szeredi goto out; 2608d18e9008SMiklos Szeredi } else if (!dentry->d_inode) { 2609d18e9008SMiklos Szeredi goto out; 2610d18e9008SMiklos Szeredi } else if ((open_flag & O_TRUNC) && 2611d18e9008SMiklos Szeredi S_ISREG(dentry->d_inode->i_mode)) { 2612d18e9008SMiklos Szeredi goto out; 2613d18e9008SMiklos Szeredi } 2614d18e9008SMiklos Szeredi /* will fail later, go on to get the right error */ 2615d18e9008SMiklos Szeredi } 2616d18e9008SMiklos Szeredi } 2617d18e9008SMiklos Szeredi looked_up: 2618d18e9008SMiklos Szeredi path->dentry = dentry; 2619d18e9008SMiklos Szeredi path->mnt = nd->path.mnt; 26202675a4ebSAl Viro return 1; 2621d18e9008SMiklos Szeredi } 2622d18e9008SMiklos Szeredi 262331e6b01fSNick Piggin /* 26241acf0af9SDavid Howells * Look up and maybe create and open the last component. 2625d58ffd35SMiklos Szeredi * 2626d58ffd35SMiklos Szeredi * Must be called with i_mutex held on parent. 2627d58ffd35SMiklos Szeredi * 26281acf0af9SDavid Howells * Returns 0 if the file was successfully atomically created (if necessary) and 26291acf0af9SDavid Howells * opened. In this case the file will be returned attached to @file. 26301acf0af9SDavid Howells * 26311acf0af9SDavid Howells * Returns 1 if the file was not completely opened at this time, though lookups 26321acf0af9SDavid Howells * and creations will have been performed and the dentry returned in @path will 26331acf0af9SDavid Howells * be positive upon return if O_CREAT was specified. If O_CREAT wasn't 26341acf0af9SDavid Howells * specified then a negative dentry may be returned. 26351acf0af9SDavid Howells * 26361acf0af9SDavid Howells * An error code is returned otherwise. 26371acf0af9SDavid Howells * 26381acf0af9SDavid Howells * FILE_CREATE will be set in @*opened if the dentry was created and will be 26391acf0af9SDavid Howells * cleared otherwise prior to returning. 2640d58ffd35SMiklos Szeredi */ 26412675a4ebSAl Viro static int lookup_open(struct nameidata *nd, struct path *path, 264230d90494SAl Viro struct file *file, 2643d58ffd35SMiklos Szeredi const struct open_flags *op, 264464894cf8SAl Viro bool got_write, int *opened) 2645d58ffd35SMiklos Szeredi { 2646d58ffd35SMiklos Szeredi struct dentry *dir = nd->path.dentry; 264754ef4872SMiklos Szeredi struct inode *dir_inode = dir->d_inode; 2648d58ffd35SMiklos Szeredi struct dentry *dentry; 2649d58ffd35SMiklos Szeredi int error; 265054ef4872SMiklos Szeredi bool need_lookup; 2651d58ffd35SMiklos Szeredi 265247237687SAl Viro *opened &= ~FILE_CREATED; 2653201f956eSAl Viro dentry = lookup_dcache(&nd->last, dir, nd->flags, &need_lookup); 2654d58ffd35SMiklos Szeredi if (IS_ERR(dentry)) 26552675a4ebSAl Viro return PTR_ERR(dentry); 2656d58ffd35SMiklos Szeredi 2657d18e9008SMiklos Szeredi /* Cached positive dentry: will open in f_op->open */ 2658d18e9008SMiklos Szeredi if (!need_lookup && dentry->d_inode) 2659d18e9008SMiklos Szeredi goto out_no_open; 2660d18e9008SMiklos Szeredi 2661d18e9008SMiklos Szeredi if ((nd->flags & LOOKUP_OPEN) && dir_inode->i_op->atomic_open) { 266264894cf8SAl Viro return atomic_open(nd, dentry, path, file, op, got_write, 266347237687SAl Viro need_lookup, opened); 2664d18e9008SMiklos Szeredi } 2665d18e9008SMiklos Szeredi 266654ef4872SMiklos Szeredi if (need_lookup) { 266754ef4872SMiklos Szeredi BUG_ON(dentry->d_inode); 266854ef4872SMiklos Szeredi 266972bd866aSAl Viro dentry = lookup_real(dir_inode, dentry, nd->flags); 267054ef4872SMiklos Szeredi if (IS_ERR(dentry)) 26712675a4ebSAl Viro return PTR_ERR(dentry); 267254ef4872SMiklos Szeredi } 267354ef4872SMiklos Szeredi 2674d58ffd35SMiklos Szeredi /* Negative dentry, just create the file */ 2675d58ffd35SMiklos Szeredi if (!dentry->d_inode && (op->open_flag & O_CREAT)) { 2676d58ffd35SMiklos Szeredi umode_t mode = op->mode; 2677d58ffd35SMiklos Szeredi if (!IS_POSIXACL(dir->d_inode)) 2678d58ffd35SMiklos Szeredi mode &= ~current_umask(); 2679d58ffd35SMiklos Szeredi /* 2680d58ffd35SMiklos Szeredi * This write is needed to ensure that a 2681d58ffd35SMiklos Szeredi * rw->ro transition does not occur between 2682d58ffd35SMiklos Szeredi * the time when the file is created and when 2683d58ffd35SMiklos Szeredi * a permanent write count is taken through 2684015c3bbcSMiklos Szeredi * the 'struct file' in finish_open(). 2685d58ffd35SMiklos Szeredi */ 268664894cf8SAl Viro if (!got_write) { 268764894cf8SAl Viro error = -EROFS; 2688d58ffd35SMiklos Szeredi goto out_dput; 268964894cf8SAl Viro } 269047237687SAl Viro *opened |= FILE_CREATED; 2691d58ffd35SMiklos Szeredi error = security_path_mknod(&nd->path, dentry, mode, 0); 2692d58ffd35SMiklos Szeredi if (error) 2693d58ffd35SMiklos Szeredi goto out_dput; 2694312b63fbSAl Viro error = vfs_create(dir->d_inode, dentry, mode, 2695312b63fbSAl Viro nd->flags & LOOKUP_EXCL); 2696d58ffd35SMiklos Szeredi if (error) 2697d58ffd35SMiklos Szeredi goto out_dput; 2698d58ffd35SMiklos Szeredi } 2699d18e9008SMiklos Szeredi out_no_open: 2700d58ffd35SMiklos Szeredi path->dentry = dentry; 2701d58ffd35SMiklos Szeredi path->mnt = nd->path.mnt; 27022675a4ebSAl Viro return 1; 2703d58ffd35SMiklos Szeredi 2704d58ffd35SMiklos Szeredi out_dput: 2705d58ffd35SMiklos Szeredi dput(dentry); 27062675a4ebSAl Viro return error; 2707d58ffd35SMiklos Szeredi } 2708d58ffd35SMiklos Szeredi 2709d58ffd35SMiklos Szeredi /* 2710fe2d35ffSAl Viro * Handle the last step of open() 271131e6b01fSNick Piggin */ 27122675a4ebSAl Viro static int do_last(struct nameidata *nd, struct path *path, 271330d90494SAl Viro struct file *file, const struct open_flags *op, 2714669abf4eSJeff Layton int *opened, struct filename *name) 2715fb1cc555SAl Viro { 2716a1e28038SAl Viro struct dentry *dir = nd->path.dentry; 2717ca344a89SAl Viro int open_flag = op->open_flag; 271877d660a8SMiklos Szeredi bool will_truncate = (open_flag & O_TRUNC) != 0; 271964894cf8SAl Viro bool got_write = false; 2720bcda7652SAl Viro int acc_mode = op->acc_mode; 2721a1eb3315SMiklos Szeredi struct inode *inode; 272277d660a8SMiklos Szeredi bool symlink_ok = false; 272316b1c1cdSMiklos Szeredi struct path save_parent = { .dentry = NULL, .mnt = NULL }; 272416b1c1cdSMiklos Szeredi bool retried = false; 272516c2cd71SAl Viro int error; 2726fb1cc555SAl Viro 2727c3e380b0SAl Viro nd->flags &= ~LOOKUP_PARENT; 2728c3e380b0SAl Viro nd->flags |= op->intent; 2729c3e380b0SAl Viro 2730bc77daa7SAl Viro if (nd->last_type != LAST_NORM) { 2731fe2d35ffSAl Viro error = handle_dots(nd, nd->last_type); 2732fe2d35ffSAl Viro if (error) 27332675a4ebSAl Viro return error; 2734e83db167SMiklos Szeredi goto finish_open; 27351f36f774SAl Viro } 2736a2c36b45SAl Viro 2737ca344a89SAl Viro if (!(open_flag & O_CREAT)) { 2738fe2d35ffSAl Viro if (nd->last.name[nd->last.len]) 2739fe2d35ffSAl Viro nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 2740bcda7652SAl Viro if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW)) 274177d660a8SMiklos Szeredi symlink_ok = true; 2742fe2d35ffSAl Viro /* we _can_ be in RCU mode here */ 2743e97cdc87SAl Viro error = lookup_fast(nd, path, &inode); 274471574865SMiklos Szeredi if (likely(!error)) 274571574865SMiklos Szeredi goto finish_lookup; 274671574865SMiklos Szeredi 2747ce57dfc1SAl Viro if (error < 0) 27482675a4ebSAl Viro goto out; 2749a1eb3315SMiklos Szeredi 275037d7fffcSMiklos Szeredi BUG_ON(nd->inode != dir->d_inode); 2751b6183df7SMiklos Szeredi } else { 2752fe2d35ffSAl Viro /* create side of things */ 2753a3fbbde7SAl Viro /* 2754b6183df7SMiklos Szeredi * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED 2755b6183df7SMiklos Szeredi * has been cleared when we got to the last component we are 2756b6183df7SMiklos Szeredi * about to look up 2757a3fbbde7SAl Viro */ 27589f1fafeeSAl Viro error = complete_walk(nd); 27599f1fafeeSAl Viro if (error) 27602675a4ebSAl Viro return error; 2761fe2d35ffSAl Viro 276233e2208aSJeff Layton audit_inode(name, dir, LOOKUP_PARENT); 276316c2cd71SAl Viro error = -EISDIR; 27641f36f774SAl Viro /* trailing slashes? */ 276531e6b01fSNick Piggin if (nd->last.name[nd->last.len]) 27662675a4ebSAl Viro goto out; 2767b6183df7SMiklos Szeredi } 27681f36f774SAl Viro 276916b1c1cdSMiklos Szeredi retry_lookup: 277064894cf8SAl Viro if (op->open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) { 277164894cf8SAl Viro error = mnt_want_write(nd->path.mnt); 277264894cf8SAl Viro if (!error) 277364894cf8SAl Viro got_write = true; 277464894cf8SAl Viro /* 277564894cf8SAl Viro * do _not_ fail yet - we might not need that or fail with 277664894cf8SAl Viro * a different error; let lookup_open() decide; we'll be 277764894cf8SAl Viro * dropping this one anyway. 277864894cf8SAl Viro */ 277964894cf8SAl Viro } 2780a1e28038SAl Viro mutex_lock(&dir->d_inode->i_mutex); 278164894cf8SAl Viro error = lookup_open(nd, path, file, op, got_write, opened); 2782fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2783fb1cc555SAl Viro 27842675a4ebSAl Viro if (error <= 0) { 27852675a4ebSAl Viro if (error) 2786d58ffd35SMiklos Szeredi goto out; 27876c0d46c4SAl Viro 278847237687SAl Viro if ((*opened & FILE_CREATED) || 2789496ad9aaSAl Viro !S_ISREG(file_inode(file)->i_mode)) 279077d660a8SMiklos Szeredi will_truncate = false; 2791d18e9008SMiklos Szeredi 2792adb5c247SJeff Layton audit_inode(name, file->f_path.dentry, 0); 2793d18e9008SMiklos Szeredi goto opened; 2794d18e9008SMiklos Szeredi } 2795d18e9008SMiklos Szeredi 279647237687SAl Viro if (*opened & FILE_CREATED) { 27979b44f1b3SAl Viro /* Don't check for write permission, don't truncate */ 2798ca344a89SAl Viro open_flag &= ~O_TRUNC; 279977d660a8SMiklos Szeredi will_truncate = false; 2800bcda7652SAl Viro acc_mode = MAY_OPEN; 2801d58ffd35SMiklos Szeredi path_to_nameidata(path, nd); 2802e83db167SMiklos Szeredi goto finish_open_created; 2803fb1cc555SAl Viro } 2804fb1cc555SAl Viro 2805fb1cc555SAl Viro /* 28063134f37eSJeff Layton * create/update audit record if it already exists. 2807fb1cc555SAl Viro */ 28083134f37eSJeff Layton if (path->dentry->d_inode) 2809adb5c247SJeff Layton audit_inode(name, path->dentry, 0); 2810fb1cc555SAl Viro 2811d18e9008SMiklos Szeredi /* 2812d18e9008SMiklos Szeredi * If atomic_open() acquired write access it is dropped now due to 2813d18e9008SMiklos Szeredi * possible mount and symlink following (this might be optimized away if 2814d18e9008SMiklos Szeredi * necessary...) 2815d18e9008SMiklos Szeredi */ 281664894cf8SAl Viro if (got_write) { 2817d18e9008SMiklos Szeredi mnt_drop_write(nd->path.mnt); 281864894cf8SAl Viro got_write = false; 2819d18e9008SMiklos Szeredi } 2820d18e9008SMiklos Szeredi 2821fb1cc555SAl Viro error = -EEXIST; 2822f8310c59SAl Viro if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT)) 2823fb1cc555SAl Viro goto exit_dput; 2824fb1cc555SAl Viro 28259875cf80SDavid Howells error = follow_managed(path, nd->flags); 28269875cf80SDavid Howells if (error < 0) 2827fb1cc555SAl Viro goto exit_dput; 2828fb1cc555SAl Viro 2829a3fbbde7SAl Viro if (error) 2830a3fbbde7SAl Viro nd->flags |= LOOKUP_JUMPED; 2831a3fbbde7SAl Viro 2832decf3400SMiklos Szeredi BUG_ON(nd->flags & LOOKUP_RCU); 2833decf3400SMiklos Szeredi inode = path->dentry->d_inode; 28345f5daac1SMiklos Szeredi finish_lookup: 28355f5daac1SMiklos Szeredi /* we _can_ be in RCU mode here */ 2836fb1cc555SAl Viro error = -ENOENT; 283754c33e7fSMiklos Szeredi if (!inode) { 283854c33e7fSMiklos Szeredi path_to_nameidata(path, nd); 28392675a4ebSAl Viro goto out; 284054c33e7fSMiklos Szeredi } 28419e67f361SAl Viro 2842d45ea867SMiklos Szeredi if (should_follow_link(inode, !symlink_ok)) { 2843d45ea867SMiklos Szeredi if (nd->flags & LOOKUP_RCU) { 2844d45ea867SMiklos Szeredi if (unlikely(unlazy_walk(nd, path->dentry))) { 2845d45ea867SMiklos Szeredi error = -ECHILD; 28462675a4ebSAl Viro goto out; 2847d45ea867SMiklos Szeredi } 2848d45ea867SMiklos Szeredi } 2849d45ea867SMiklos Szeredi BUG_ON(inode != path->dentry->d_inode); 28502675a4ebSAl Viro return 1; 2851d45ea867SMiklos Szeredi } 2852fb1cc555SAl Viro 285316b1c1cdSMiklos Szeredi if ((nd->flags & LOOKUP_RCU) || nd->path.mnt != path->mnt) { 2854fb1cc555SAl Viro path_to_nameidata(path, nd); 285516b1c1cdSMiklos Szeredi } else { 285616b1c1cdSMiklos Szeredi save_parent.dentry = nd->path.dentry; 285716b1c1cdSMiklos Szeredi save_parent.mnt = mntget(path->mnt); 285816b1c1cdSMiklos Szeredi nd->path.dentry = path->dentry; 285916b1c1cdSMiklos Szeredi 286016b1c1cdSMiklos Szeredi } 2861decf3400SMiklos Szeredi nd->inode = inode; 2862a3fbbde7SAl Viro /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */ 2863bc77daa7SAl Viro finish_open: 2864a3fbbde7SAl Viro error = complete_walk(nd); 286516b1c1cdSMiklos Szeredi if (error) { 286616b1c1cdSMiklos Szeredi path_put(&save_parent); 28672675a4ebSAl Viro return error; 286816b1c1cdSMiklos Szeredi } 2869bc77daa7SAl Viro audit_inode(name, nd->path.dentry, 0); 2870fb1cc555SAl Viro error = -EISDIR; 2871050ac841SMiklos Szeredi if ((open_flag & O_CREAT) && S_ISDIR(nd->inode->i_mode)) 28722675a4ebSAl Viro goto out; 2873af2f5542SMiklos Szeredi error = -ENOTDIR; 287405252901SAl Viro if ((nd->flags & LOOKUP_DIRECTORY) && !can_lookup(nd->inode)) 28752675a4ebSAl Viro goto out; 28766c0d46c4SAl Viro if (!S_ISREG(nd->inode->i_mode)) 287777d660a8SMiklos Szeredi will_truncate = false; 28786c0d46c4SAl Viro 28790f9d1a10SAl Viro if (will_truncate) { 28800f9d1a10SAl Viro error = mnt_want_write(nd->path.mnt); 28810f9d1a10SAl Viro if (error) 28822675a4ebSAl Viro goto out; 288364894cf8SAl Viro got_write = true; 28840f9d1a10SAl Viro } 2885e83db167SMiklos Szeredi finish_open_created: 2886bcda7652SAl Viro error = may_open(&nd->path, acc_mode, open_flag); 2887ca344a89SAl Viro if (error) 28882675a4ebSAl Viro goto out; 288930d90494SAl Viro file->f_path.mnt = nd->path.mnt; 289030d90494SAl Viro error = finish_open(file, nd->path.dentry, NULL, opened); 289130d90494SAl Viro if (error) { 289230d90494SAl Viro if (error == -EOPENSTALE) 2893f60dc3dbSMiklos Szeredi goto stale_open; 2894015c3bbcSMiklos Szeredi goto out; 2895f60dc3dbSMiklos Szeredi } 2896a8277b9bSMiklos Szeredi opened: 28972675a4ebSAl Viro error = open_check_o_direct(file); 2898015c3bbcSMiklos Szeredi if (error) 2899015c3bbcSMiklos Szeredi goto exit_fput; 29002675a4ebSAl Viro error = ima_file_check(file, op->acc_mode); 2901aa4caadbSMiklos Szeredi if (error) 2902aa4caadbSMiklos Szeredi goto exit_fput; 2903aa4caadbSMiklos Szeredi 29040f9d1a10SAl Viro if (will_truncate) { 29052675a4ebSAl Viro error = handle_truncate(file); 2906aa4caadbSMiklos Szeredi if (error) 2907aa4caadbSMiklos Szeredi goto exit_fput; 29080f9d1a10SAl Viro } 2909ca344a89SAl Viro out: 291064894cf8SAl Viro if (got_write) 29110f9d1a10SAl Viro mnt_drop_write(nd->path.mnt); 291216b1c1cdSMiklos Szeredi path_put(&save_parent); 2913e276ae67SMiklos Szeredi terminate_walk(nd); 29142675a4ebSAl Viro return error; 2915fb1cc555SAl Viro 2916fb1cc555SAl Viro exit_dput: 2917fb1cc555SAl Viro path_put_conditional(path, nd); 2918ca344a89SAl Viro goto out; 2919015c3bbcSMiklos Szeredi exit_fput: 29202675a4ebSAl Viro fput(file); 29212675a4ebSAl Viro goto out; 2922015c3bbcSMiklos Szeredi 2923f60dc3dbSMiklos Szeredi stale_open: 2924f60dc3dbSMiklos Szeredi /* If no saved parent or already retried then can't retry */ 2925f60dc3dbSMiklos Szeredi if (!save_parent.dentry || retried) 2926f60dc3dbSMiklos Szeredi goto out; 2927f60dc3dbSMiklos Szeredi 2928f60dc3dbSMiklos Szeredi BUG_ON(save_parent.dentry != dir); 2929f60dc3dbSMiklos Szeredi path_put(&nd->path); 2930f60dc3dbSMiklos Szeredi nd->path = save_parent; 2931f60dc3dbSMiklos Szeredi nd->inode = dir->d_inode; 2932f60dc3dbSMiklos Szeredi save_parent.mnt = NULL; 2933f60dc3dbSMiklos Szeredi save_parent.dentry = NULL; 293464894cf8SAl Viro if (got_write) { 2935f60dc3dbSMiklos Szeredi mnt_drop_write(nd->path.mnt); 293664894cf8SAl Viro got_write = false; 2937f60dc3dbSMiklos Szeredi } 2938f60dc3dbSMiklos Szeredi retried = true; 2939f60dc3dbSMiklos Szeredi goto retry_lookup; 2940fb1cc555SAl Viro } 2941fb1cc555SAl Viro 294260545d0dSAl Viro static int do_tmpfile(int dfd, struct filename *pathname, 294360545d0dSAl Viro struct nameidata *nd, int flags, 294460545d0dSAl Viro const struct open_flags *op, 294560545d0dSAl Viro struct file *file, int *opened) 294660545d0dSAl Viro { 294760545d0dSAl Viro static const struct qstr name = QSTR_INIT("/", 1); 294860545d0dSAl Viro struct dentry *dentry, *child; 294960545d0dSAl Viro struct inode *dir; 295060545d0dSAl Viro int error = path_lookupat(dfd, pathname->name, 295160545d0dSAl Viro flags | LOOKUP_DIRECTORY, nd); 295260545d0dSAl Viro if (unlikely(error)) 295360545d0dSAl Viro return error; 295460545d0dSAl Viro error = mnt_want_write(nd->path.mnt); 295560545d0dSAl Viro if (unlikely(error)) 295660545d0dSAl Viro goto out; 295760545d0dSAl Viro /* we want directory to be writable */ 295860545d0dSAl Viro error = inode_permission(nd->inode, MAY_WRITE | MAY_EXEC); 295960545d0dSAl Viro if (error) 296060545d0dSAl Viro goto out2; 296160545d0dSAl Viro dentry = nd->path.dentry; 296260545d0dSAl Viro dir = dentry->d_inode; 296360545d0dSAl Viro if (!dir->i_op->tmpfile) { 296460545d0dSAl Viro error = -EOPNOTSUPP; 296560545d0dSAl Viro goto out2; 296660545d0dSAl Viro } 296760545d0dSAl Viro child = d_alloc(dentry, &name); 296860545d0dSAl Viro if (unlikely(!child)) { 296960545d0dSAl Viro error = -ENOMEM; 297060545d0dSAl Viro goto out2; 297160545d0dSAl Viro } 297260545d0dSAl Viro nd->flags &= ~LOOKUP_DIRECTORY; 297360545d0dSAl Viro nd->flags |= op->intent; 297460545d0dSAl Viro dput(nd->path.dentry); 297560545d0dSAl Viro nd->path.dentry = child; 297660545d0dSAl Viro error = dir->i_op->tmpfile(dir, nd->path.dentry, op->mode); 297760545d0dSAl Viro if (error) 297860545d0dSAl Viro goto out2; 297960545d0dSAl Viro audit_inode(pathname, nd->path.dentry, 0); 298060545d0dSAl Viro error = may_open(&nd->path, op->acc_mode, op->open_flag); 298160545d0dSAl Viro if (error) 298260545d0dSAl Viro goto out2; 298360545d0dSAl Viro file->f_path.mnt = nd->path.mnt; 298460545d0dSAl Viro error = finish_open(file, nd->path.dentry, NULL, opened); 298560545d0dSAl Viro if (error) 298660545d0dSAl Viro goto out2; 298760545d0dSAl Viro error = open_check_o_direct(file); 2988f4e0c30cSAl Viro if (error) { 298960545d0dSAl Viro fput(file); 2990f4e0c30cSAl Viro } else if (!(op->open_flag & O_EXCL)) { 2991f4e0c30cSAl Viro struct inode *inode = file_inode(file); 2992f4e0c30cSAl Viro spin_lock(&inode->i_lock); 2993f4e0c30cSAl Viro inode->i_state |= I_LINKABLE; 2994f4e0c30cSAl Viro spin_unlock(&inode->i_lock); 2995f4e0c30cSAl Viro } 299660545d0dSAl Viro out2: 299760545d0dSAl Viro mnt_drop_write(nd->path.mnt); 299860545d0dSAl Viro out: 299960545d0dSAl Viro path_put(&nd->path); 300060545d0dSAl Viro return error; 300160545d0dSAl Viro } 300260545d0dSAl Viro 3003669abf4eSJeff Layton static struct file *path_openat(int dfd, struct filename *pathname, 300473d049a4SAl Viro struct nameidata *nd, const struct open_flags *op, int flags) 30051da177e4SLinus Torvalds { 3006fe2d35ffSAl Viro struct file *base = NULL; 300730d90494SAl Viro struct file *file; 30089850c056SAl Viro struct path path; 300947237687SAl Viro int opened = 0; 301013aab428SAl Viro int error; 301131e6b01fSNick Piggin 301230d90494SAl Viro file = get_empty_filp(); 30131afc99beSAl Viro if (IS_ERR(file)) 30141afc99beSAl Viro return file; 301531e6b01fSNick Piggin 301630d90494SAl Viro file->f_flags = op->open_flag; 301731e6b01fSNick Piggin 3018bb458c64SAl Viro if (unlikely(file->f_flags & __O_TMPFILE)) { 301960545d0dSAl Viro error = do_tmpfile(dfd, pathname, nd, flags, op, file, &opened); 302060545d0dSAl Viro goto out; 302160545d0dSAl Viro } 302260545d0dSAl Viro 3023669abf4eSJeff Layton error = path_init(dfd, pathname->name, flags | LOOKUP_PARENT, nd, &base); 302431e6b01fSNick Piggin if (unlikely(error)) 30252675a4ebSAl Viro goto out; 302631e6b01fSNick Piggin 3027fe2d35ffSAl Viro current->total_link_count = 0; 3028669abf4eSJeff Layton error = link_path_walk(pathname->name, nd); 302931e6b01fSNick Piggin if (unlikely(error)) 30302675a4ebSAl Viro goto out; 30311da177e4SLinus Torvalds 30322675a4ebSAl Viro error = do_last(nd, &path, file, op, &opened, pathname); 30332675a4ebSAl Viro while (unlikely(error > 0)) { /* trailing symlink */ 30347b9337aaSNick Piggin struct path link = path; 3035def4af30SAl Viro void *cookie; 3036574197e0SAl Viro if (!(nd->flags & LOOKUP_FOLLOW)) { 303773d049a4SAl Viro path_put_conditional(&path, nd); 303873d049a4SAl Viro path_put(&nd->path); 30392675a4ebSAl Viro error = -ELOOP; 304040b39136SAl Viro break; 304140b39136SAl Viro } 3042800179c9SKees Cook error = may_follow_link(&link, nd); 3043800179c9SKees Cook if (unlikely(error)) 3044800179c9SKees Cook break; 304573d049a4SAl Viro nd->flags |= LOOKUP_PARENT; 304673d049a4SAl Viro nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL); 3047574197e0SAl Viro error = follow_link(&link, nd, &cookie); 3048c3e380b0SAl Viro if (unlikely(error)) 30492675a4ebSAl Viro break; 30502675a4ebSAl Viro error = do_last(nd, &path, file, op, &opened, pathname); 3051574197e0SAl Viro put_link(nd, &link, cookie); 3052806b681cSAl Viro } 305310fa8e62SAl Viro out: 305473d049a4SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) 305573d049a4SAl Viro path_put(&nd->root); 3056fe2d35ffSAl Viro if (base) 3057fe2d35ffSAl Viro fput(base); 30582675a4ebSAl Viro if (!(opened & FILE_OPENED)) { 30592675a4ebSAl Viro BUG_ON(!error); 306030d90494SAl Viro put_filp(file); 3061015c3bbcSMiklos Szeredi } 30622675a4ebSAl Viro if (unlikely(error)) { 30632675a4ebSAl Viro if (error == -EOPENSTALE) { 30642675a4ebSAl Viro if (flags & LOOKUP_RCU) 30652675a4ebSAl Viro error = -ECHILD; 30662675a4ebSAl Viro else 30672675a4ebSAl Viro error = -ESTALE; 30682675a4ebSAl Viro } 30692675a4ebSAl Viro file = ERR_PTR(error); 30702675a4ebSAl Viro } 30712675a4ebSAl Viro return file; 3072de459215SKirill Korotaev } 30731da177e4SLinus Torvalds 3074669abf4eSJeff Layton struct file *do_filp_open(int dfd, struct filename *pathname, 3075f9652e10SAl Viro const struct open_flags *op) 307613aab428SAl Viro { 307773d049a4SAl Viro struct nameidata nd; 3078f9652e10SAl Viro int flags = op->lookup_flags; 307913aab428SAl Viro struct file *filp; 308013aab428SAl Viro 308173d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU); 308213aab428SAl Viro if (unlikely(filp == ERR_PTR(-ECHILD))) 308373d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags); 308413aab428SAl Viro if (unlikely(filp == ERR_PTR(-ESTALE))) 308573d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL); 308613aab428SAl Viro return filp; 308713aab428SAl Viro } 308813aab428SAl Viro 308973d049a4SAl Viro struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt, 3090f9652e10SAl Viro const char *name, const struct open_flags *op) 309173d049a4SAl Viro { 309273d049a4SAl Viro struct nameidata nd; 309373d049a4SAl Viro struct file *file; 3094669abf4eSJeff Layton struct filename filename = { .name = name }; 3095f9652e10SAl Viro int flags = op->lookup_flags | LOOKUP_ROOT; 309673d049a4SAl Viro 309773d049a4SAl Viro nd.root.mnt = mnt; 309873d049a4SAl Viro nd.root.dentry = dentry; 309973d049a4SAl Viro 3100bcda7652SAl Viro if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN) 310173d049a4SAl Viro return ERR_PTR(-ELOOP); 310273d049a4SAl Viro 3103669abf4eSJeff Layton file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_RCU); 310473d049a4SAl Viro if (unlikely(file == ERR_PTR(-ECHILD))) 3105669abf4eSJeff Layton file = path_openat(-1, &filename, &nd, op, flags); 310673d049a4SAl Viro if (unlikely(file == ERR_PTR(-ESTALE))) 3107669abf4eSJeff Layton file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_REVAL); 310873d049a4SAl Viro return file; 310973d049a4SAl Viro } 311073d049a4SAl Viro 31111ac12b4bSJeff Layton struct dentry *kern_path_create(int dfd, const char *pathname, 31121ac12b4bSJeff Layton struct path *path, unsigned int lookup_flags) 31131da177e4SLinus Torvalds { 3114c663e5d8SChristoph Hellwig struct dentry *dentry = ERR_PTR(-EEXIST); 3115ed75e95dSAl Viro struct nameidata nd; 3116c30dabfeSJan Kara int err2; 31171ac12b4bSJeff Layton int error; 31181ac12b4bSJeff Layton bool is_dir = (lookup_flags & LOOKUP_DIRECTORY); 31191ac12b4bSJeff Layton 31201ac12b4bSJeff Layton /* 31211ac12b4bSJeff Layton * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any 31221ac12b4bSJeff Layton * other flags passed in are ignored! 31231ac12b4bSJeff Layton */ 31241ac12b4bSJeff Layton lookup_flags &= LOOKUP_REVAL; 31251ac12b4bSJeff Layton 31261ac12b4bSJeff Layton error = do_path_lookup(dfd, pathname, LOOKUP_PARENT|lookup_flags, &nd); 3127ed75e95dSAl Viro if (error) 3128ed75e95dSAl Viro return ERR_PTR(error); 31291da177e4SLinus Torvalds 3130c663e5d8SChristoph Hellwig /* 3131c663e5d8SChristoph Hellwig * Yucky last component or no last component at all? 3132c663e5d8SChristoph Hellwig * (foo/., foo/.., /////) 3133c663e5d8SChristoph Hellwig */ 3134ed75e95dSAl Viro if (nd.last_type != LAST_NORM) 3135ed75e95dSAl Viro goto out; 3136ed75e95dSAl Viro nd.flags &= ~LOOKUP_PARENT; 3137ed75e95dSAl Viro nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL; 3138c663e5d8SChristoph Hellwig 3139c30dabfeSJan Kara /* don't fail immediately if it's r/o, at least try to report other errors */ 3140c30dabfeSJan Kara err2 = mnt_want_write(nd.path.mnt); 3141c663e5d8SChristoph Hellwig /* 3142c663e5d8SChristoph Hellwig * Do the final lookup. 3143c663e5d8SChristoph Hellwig */ 3144ed75e95dSAl Viro mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 3145ed75e95dSAl Viro dentry = lookup_hash(&nd); 31461da177e4SLinus Torvalds if (IS_ERR(dentry)) 3147a8104a9fSAl Viro goto unlock; 3148c663e5d8SChristoph Hellwig 3149a8104a9fSAl Viro error = -EEXIST; 3150e9baf6e5SAl Viro if (dentry->d_inode) 3151a8104a9fSAl Viro goto fail; 3152c663e5d8SChristoph Hellwig /* 3153c663e5d8SChristoph Hellwig * Special case - lookup gave negative, but... we had foo/bar/ 3154c663e5d8SChristoph Hellwig * From the vfs_mknod() POV we just have a negative dentry - 3155c663e5d8SChristoph Hellwig * all is fine. Let's be bastards - you had / on the end, you've 3156c663e5d8SChristoph Hellwig * been asking for (non-existent) directory. -ENOENT for you. 3157c663e5d8SChristoph Hellwig */ 3158ed75e95dSAl Viro if (unlikely(!is_dir && nd.last.name[nd.last.len])) { 3159a8104a9fSAl Viro error = -ENOENT; 3160ed75e95dSAl Viro goto fail; 3161e9baf6e5SAl Viro } 3162c30dabfeSJan Kara if (unlikely(err2)) { 3163c30dabfeSJan Kara error = err2; 3164a8104a9fSAl Viro goto fail; 3165c30dabfeSJan Kara } 3166ed75e95dSAl Viro *path = nd.path; 3167e9baf6e5SAl Viro return dentry; 31681da177e4SLinus Torvalds fail: 3169a8104a9fSAl Viro dput(dentry); 3170a8104a9fSAl Viro dentry = ERR_PTR(error); 3171a8104a9fSAl Viro unlock: 3172dae6ad8fSAl Viro mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 3173c30dabfeSJan Kara if (!err2) 3174c30dabfeSJan Kara mnt_drop_write(nd.path.mnt); 3175ed75e95dSAl Viro out: 3176dae6ad8fSAl Viro path_put(&nd.path); 3177ed75e95dSAl Viro return dentry; 3178dae6ad8fSAl Viro } 3179dae6ad8fSAl Viro EXPORT_SYMBOL(kern_path_create); 3180dae6ad8fSAl Viro 3181921a1650SAl Viro void done_path_create(struct path *path, struct dentry *dentry) 3182921a1650SAl Viro { 3183921a1650SAl Viro dput(dentry); 3184921a1650SAl Viro mutex_unlock(&path->dentry->d_inode->i_mutex); 3185a8104a9fSAl Viro mnt_drop_write(path->mnt); 3186921a1650SAl Viro path_put(path); 3187921a1650SAl Viro } 3188921a1650SAl Viro EXPORT_SYMBOL(done_path_create); 3189921a1650SAl Viro 31901ac12b4bSJeff Layton struct dentry *user_path_create(int dfd, const char __user *pathname, 31911ac12b4bSJeff Layton struct path *path, unsigned int lookup_flags) 3192dae6ad8fSAl Viro { 319391a27b2aSJeff Layton struct filename *tmp = getname(pathname); 3194dae6ad8fSAl Viro struct dentry *res; 3195dae6ad8fSAl Viro if (IS_ERR(tmp)) 3196dae6ad8fSAl Viro return ERR_CAST(tmp); 31971ac12b4bSJeff Layton res = kern_path_create(dfd, tmp->name, path, lookup_flags); 3198dae6ad8fSAl Viro putname(tmp); 3199dae6ad8fSAl Viro return res; 3200dae6ad8fSAl Viro } 3201dae6ad8fSAl Viro EXPORT_SYMBOL(user_path_create); 3202dae6ad8fSAl Viro 32031a67aafbSAl Viro int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev) 32041da177e4SLinus Torvalds { 3205a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 32061da177e4SLinus Torvalds 32071da177e4SLinus Torvalds if (error) 32081da177e4SLinus Torvalds return error; 32091da177e4SLinus Torvalds 3210975d6b39SEric W. Biederman if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD)) 32111da177e4SLinus Torvalds return -EPERM; 32121da177e4SLinus Torvalds 3213acfa4380SAl Viro if (!dir->i_op->mknod) 32141da177e4SLinus Torvalds return -EPERM; 32151da177e4SLinus Torvalds 321608ce5f16SSerge E. Hallyn error = devcgroup_inode_mknod(mode, dev); 321708ce5f16SSerge E. Hallyn if (error) 321808ce5f16SSerge E. Hallyn return error; 321908ce5f16SSerge E. Hallyn 32201da177e4SLinus Torvalds error = security_inode_mknod(dir, dentry, mode, dev); 32211da177e4SLinus Torvalds if (error) 32221da177e4SLinus Torvalds return error; 32231da177e4SLinus Torvalds 32241da177e4SLinus Torvalds error = dir->i_op->mknod(dir, dentry, mode, dev); 3225a74574aaSStephen Smalley if (!error) 3226f38aa942SAmy Griffis fsnotify_create(dir, dentry); 32271da177e4SLinus Torvalds return error; 32281da177e4SLinus Torvalds } 32291da177e4SLinus Torvalds 3230f69aac00SAl Viro static int may_mknod(umode_t mode) 3231463c3197SDave Hansen { 3232463c3197SDave Hansen switch (mode & S_IFMT) { 3233463c3197SDave Hansen case S_IFREG: 3234463c3197SDave Hansen case S_IFCHR: 3235463c3197SDave Hansen case S_IFBLK: 3236463c3197SDave Hansen case S_IFIFO: 3237463c3197SDave Hansen case S_IFSOCK: 3238463c3197SDave Hansen case 0: /* zero mode translates to S_IFREG */ 3239463c3197SDave Hansen return 0; 3240463c3197SDave Hansen case S_IFDIR: 3241463c3197SDave Hansen return -EPERM; 3242463c3197SDave Hansen default: 3243463c3197SDave Hansen return -EINVAL; 3244463c3197SDave Hansen } 3245463c3197SDave Hansen } 3246463c3197SDave Hansen 32478208a22bSAl Viro SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode, 32482e4d0924SHeiko Carstens unsigned, dev) 32491da177e4SLinus Torvalds { 32501da177e4SLinus Torvalds struct dentry *dentry; 3251dae6ad8fSAl Viro struct path path; 3252dae6ad8fSAl Viro int error; 3253972567f1SJeff Layton unsigned int lookup_flags = 0; 32541da177e4SLinus Torvalds 32558e4bfca1SAl Viro error = may_mknod(mode); 32568e4bfca1SAl Viro if (error) 32578e4bfca1SAl Viro return error; 3258972567f1SJeff Layton retry: 3259972567f1SJeff Layton dentry = user_path_create(dfd, filename, &path, lookup_flags); 3260dae6ad8fSAl Viro if (IS_ERR(dentry)) 3261dae6ad8fSAl Viro return PTR_ERR(dentry); 32622ad94ae6SAl Viro 3263dae6ad8fSAl Viro if (!IS_POSIXACL(path.dentry->d_inode)) 3264ce3b0f8dSAl Viro mode &= ~current_umask(); 3265dae6ad8fSAl Viro error = security_path_mknod(&path, dentry, mode, dev); 3266be6d3e56SKentaro Takeda if (error) 3267a8104a9fSAl Viro goto out; 32681da177e4SLinus Torvalds switch (mode & S_IFMT) { 32691da177e4SLinus Torvalds case 0: case S_IFREG: 3270312b63fbSAl Viro error = vfs_create(path.dentry->d_inode,dentry,mode,true); 32711da177e4SLinus Torvalds break; 32721da177e4SLinus Torvalds case S_IFCHR: case S_IFBLK: 3273dae6ad8fSAl Viro error = vfs_mknod(path.dentry->d_inode,dentry,mode, 32741da177e4SLinus Torvalds new_decode_dev(dev)); 32751da177e4SLinus Torvalds break; 32761da177e4SLinus Torvalds case S_IFIFO: case S_IFSOCK: 3277dae6ad8fSAl Viro error = vfs_mknod(path.dentry->d_inode,dentry,mode,0); 32781da177e4SLinus Torvalds break; 32791da177e4SLinus Torvalds } 3280a8104a9fSAl Viro out: 3281921a1650SAl Viro done_path_create(&path, dentry); 3282972567f1SJeff Layton if (retry_estale(error, lookup_flags)) { 3283972567f1SJeff Layton lookup_flags |= LOOKUP_REVAL; 3284972567f1SJeff Layton goto retry; 3285972567f1SJeff Layton } 32861da177e4SLinus Torvalds return error; 32871da177e4SLinus Torvalds } 32881da177e4SLinus Torvalds 32898208a22bSAl Viro SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev) 32905590ff0dSUlrich Drepper { 32915590ff0dSUlrich Drepper return sys_mknodat(AT_FDCWD, filename, mode, dev); 32925590ff0dSUlrich Drepper } 32935590ff0dSUlrich Drepper 329418bb1db3SAl Viro int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) 32951da177e4SLinus Torvalds { 3296a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 32978de52778SAl Viro unsigned max_links = dir->i_sb->s_max_links; 32981da177e4SLinus Torvalds 32991da177e4SLinus Torvalds if (error) 33001da177e4SLinus Torvalds return error; 33011da177e4SLinus Torvalds 3302acfa4380SAl Viro if (!dir->i_op->mkdir) 33031da177e4SLinus Torvalds return -EPERM; 33041da177e4SLinus Torvalds 33051da177e4SLinus Torvalds mode &= (S_IRWXUGO|S_ISVTX); 33061da177e4SLinus Torvalds error = security_inode_mkdir(dir, dentry, mode); 33071da177e4SLinus Torvalds if (error) 33081da177e4SLinus Torvalds return error; 33091da177e4SLinus Torvalds 33108de52778SAl Viro if (max_links && dir->i_nlink >= max_links) 33118de52778SAl Viro return -EMLINK; 33128de52778SAl Viro 33131da177e4SLinus Torvalds error = dir->i_op->mkdir(dir, dentry, mode); 3314a74574aaSStephen Smalley if (!error) 3315f38aa942SAmy Griffis fsnotify_mkdir(dir, dentry); 33161da177e4SLinus Torvalds return error; 33171da177e4SLinus Torvalds } 33181da177e4SLinus Torvalds 3319a218d0fdSAl Viro SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode) 33201da177e4SLinus Torvalds { 33216902d925SDave Hansen struct dentry *dentry; 3322dae6ad8fSAl Viro struct path path; 3323dae6ad8fSAl Viro int error; 3324b76d8b82SJeff Layton unsigned int lookup_flags = LOOKUP_DIRECTORY; 33251da177e4SLinus Torvalds 3326b76d8b82SJeff Layton retry: 3327b76d8b82SJeff Layton dentry = user_path_create(dfd, pathname, &path, lookup_flags); 33286902d925SDave Hansen if (IS_ERR(dentry)) 3329dae6ad8fSAl Viro return PTR_ERR(dentry); 33306902d925SDave Hansen 3331dae6ad8fSAl Viro if (!IS_POSIXACL(path.dentry->d_inode)) 3332ce3b0f8dSAl Viro mode &= ~current_umask(); 3333dae6ad8fSAl Viro error = security_path_mkdir(&path, dentry, mode); 3334a8104a9fSAl Viro if (!error) 3335dae6ad8fSAl Viro error = vfs_mkdir(path.dentry->d_inode, dentry, mode); 3336921a1650SAl Viro done_path_create(&path, dentry); 3337b76d8b82SJeff Layton if (retry_estale(error, lookup_flags)) { 3338b76d8b82SJeff Layton lookup_flags |= LOOKUP_REVAL; 3339b76d8b82SJeff Layton goto retry; 3340b76d8b82SJeff Layton } 33411da177e4SLinus Torvalds return error; 33421da177e4SLinus Torvalds } 33431da177e4SLinus Torvalds 3344a218d0fdSAl Viro SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode) 33455590ff0dSUlrich Drepper { 33465590ff0dSUlrich Drepper return sys_mkdirat(AT_FDCWD, pathname, mode); 33475590ff0dSUlrich Drepper } 33485590ff0dSUlrich Drepper 33491da177e4SLinus Torvalds /* 3350a71905f0SSage Weil * The dentry_unhash() helper will try to drop the dentry early: we 3351c0d02594SJ. Bruce Fields * should have a usage count of 1 if we're the only user of this 3352a71905f0SSage Weil * dentry, and if that is true (possibly after pruning the dcache), 3353a71905f0SSage Weil * then we drop the dentry now. 33541da177e4SLinus Torvalds * 33551da177e4SLinus Torvalds * A low-level filesystem can, if it choses, legally 33561da177e4SLinus Torvalds * do a 33571da177e4SLinus Torvalds * 33581da177e4SLinus Torvalds * if (!d_unhashed(dentry)) 33591da177e4SLinus Torvalds * return -EBUSY; 33601da177e4SLinus Torvalds * 33611da177e4SLinus Torvalds * if it cannot handle the case of removing a directory 33621da177e4SLinus Torvalds * that is still in use by something else.. 33631da177e4SLinus Torvalds */ 33641da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry) 33651da177e4SLinus Torvalds { 33661da177e4SLinus Torvalds shrink_dcache_parent(dentry); 33671da177e4SLinus Torvalds spin_lock(&dentry->d_lock); 336898474236SWaiman Long if (dentry->d_lockref.count == 1) 33691da177e4SLinus Torvalds __d_drop(dentry); 33701da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 33711da177e4SLinus Torvalds } 33721da177e4SLinus Torvalds 33731da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry) 33741da177e4SLinus Torvalds { 33751da177e4SLinus Torvalds int error = may_delete(dir, dentry, 1); 33761da177e4SLinus Torvalds 33771da177e4SLinus Torvalds if (error) 33781da177e4SLinus Torvalds return error; 33791da177e4SLinus Torvalds 3380acfa4380SAl Viro if (!dir->i_op->rmdir) 33811da177e4SLinus Torvalds return -EPERM; 33821da177e4SLinus Torvalds 33831d2ef590SAl Viro dget(dentry); 33841b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 3385912dbc15SSage Weil 33861da177e4SLinus Torvalds error = -EBUSY; 3387912dbc15SSage Weil if (d_mountpoint(dentry)) 3388912dbc15SSage Weil goto out; 3389912dbc15SSage Weil 33901da177e4SLinus Torvalds error = security_inode_rmdir(dir, dentry); 3391912dbc15SSage Weil if (error) 3392912dbc15SSage Weil goto out; 3393912dbc15SSage Weil 33943cebde24SSage Weil shrink_dcache_parent(dentry); 33951da177e4SLinus Torvalds error = dir->i_op->rmdir(dir, dentry); 3396912dbc15SSage Weil if (error) 3397912dbc15SSage Weil goto out; 3398912dbc15SSage Weil 33991da177e4SLinus Torvalds dentry->d_inode->i_flags |= S_DEAD; 3400d83c49f3SAl Viro dont_mount(dentry); 34011da177e4SLinus Torvalds 3402912dbc15SSage Weil out: 3403912dbc15SSage Weil mutex_unlock(&dentry->d_inode->i_mutex); 34041d2ef590SAl Viro dput(dentry); 3405912dbc15SSage Weil if (!error) 3406912dbc15SSage Weil d_delete(dentry); 34071da177e4SLinus Torvalds return error; 34081da177e4SLinus Torvalds } 34091da177e4SLinus Torvalds 34105590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname) 34111da177e4SLinus Torvalds { 34121da177e4SLinus Torvalds int error = 0; 341391a27b2aSJeff Layton struct filename *name; 34141da177e4SLinus Torvalds struct dentry *dentry; 34151da177e4SLinus Torvalds struct nameidata nd; 3416c6ee9206SJeff Layton unsigned int lookup_flags = 0; 3417c6ee9206SJeff Layton retry: 3418c6ee9206SJeff Layton name = user_path_parent(dfd, pathname, &nd, lookup_flags); 341991a27b2aSJeff Layton if (IS_ERR(name)) 342091a27b2aSJeff Layton return PTR_ERR(name); 34211da177e4SLinus Torvalds 34221da177e4SLinus Torvalds switch(nd.last_type) { 34231da177e4SLinus Torvalds case LAST_DOTDOT: 34241da177e4SLinus Torvalds error = -ENOTEMPTY; 34251da177e4SLinus Torvalds goto exit1; 34261da177e4SLinus Torvalds case LAST_DOT: 34271da177e4SLinus Torvalds error = -EINVAL; 34281da177e4SLinus Torvalds goto exit1; 34291da177e4SLinus Torvalds case LAST_ROOT: 34301da177e4SLinus Torvalds error = -EBUSY; 34311da177e4SLinus Torvalds goto exit1; 34321da177e4SLinus Torvalds } 34330612d9fbSOGAWA Hirofumi 34340612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 3435c30dabfeSJan Kara error = mnt_want_write(nd.path.mnt); 3436c30dabfeSJan Kara if (error) 3437c30dabfeSJan Kara goto exit1; 34380612d9fbSOGAWA Hirofumi 34394ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 344049705b77SChristoph Hellwig dentry = lookup_hash(&nd); 34411da177e4SLinus Torvalds error = PTR_ERR(dentry); 34426902d925SDave Hansen if (IS_ERR(dentry)) 34436902d925SDave Hansen goto exit2; 3444e6bc45d6STheodore Ts'o if (!dentry->d_inode) { 3445e6bc45d6STheodore Ts'o error = -ENOENT; 3446e6bc45d6STheodore Ts'o goto exit3; 3447e6bc45d6STheodore Ts'o } 3448be6d3e56SKentaro Takeda error = security_path_rmdir(&nd.path, dentry); 3449be6d3e56SKentaro Takeda if (error) 3450c30dabfeSJan Kara goto exit3; 34514ac91378SJan Blunck error = vfs_rmdir(nd.path.dentry->d_inode, dentry); 34520622753bSDave Hansen exit3: 34531da177e4SLinus Torvalds dput(dentry); 34546902d925SDave Hansen exit2: 34554ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 3456c30dabfeSJan Kara mnt_drop_write(nd.path.mnt); 34571da177e4SLinus Torvalds exit1: 34581d957f9bSJan Blunck path_put(&nd.path); 34591da177e4SLinus Torvalds putname(name); 3460c6ee9206SJeff Layton if (retry_estale(error, lookup_flags)) { 3461c6ee9206SJeff Layton lookup_flags |= LOOKUP_REVAL; 3462c6ee9206SJeff Layton goto retry; 3463c6ee9206SJeff Layton } 34641da177e4SLinus Torvalds return error; 34651da177e4SLinus Torvalds } 34661da177e4SLinus Torvalds 34673cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname) 34685590ff0dSUlrich Drepper { 34695590ff0dSUlrich Drepper return do_rmdir(AT_FDCWD, pathname); 34705590ff0dSUlrich Drepper } 34715590ff0dSUlrich Drepper 34721da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry) 34731da177e4SLinus Torvalds { 34741da177e4SLinus Torvalds int error = may_delete(dir, dentry, 0); 34751da177e4SLinus Torvalds 34761da177e4SLinus Torvalds if (error) 34771da177e4SLinus Torvalds return error; 34781da177e4SLinus Torvalds 3479acfa4380SAl Viro if (!dir->i_op->unlink) 34801da177e4SLinus Torvalds return -EPERM; 34811da177e4SLinus Torvalds 34821b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 34831da177e4SLinus Torvalds if (d_mountpoint(dentry)) 34841da177e4SLinus Torvalds error = -EBUSY; 34851da177e4SLinus Torvalds else { 34861da177e4SLinus Torvalds error = security_inode_unlink(dir, dentry); 3487bec1052eSAl Viro if (!error) { 34881da177e4SLinus Torvalds error = dir->i_op->unlink(dir, dentry); 3489bec1052eSAl Viro if (!error) 3490d83c49f3SAl Viro dont_mount(dentry); 3491bec1052eSAl Viro } 34921da177e4SLinus Torvalds } 34931b1dcc1bSJes Sorensen mutex_unlock(&dentry->d_inode->i_mutex); 34941da177e4SLinus Torvalds 34951da177e4SLinus Torvalds /* We don't d_delete() NFS sillyrenamed files--they still exist. */ 34961da177e4SLinus Torvalds if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) { 3497ece95912SJan Kara fsnotify_link_count(dentry->d_inode); 34981da177e4SLinus Torvalds d_delete(dentry); 34991da177e4SLinus Torvalds } 35000eeca283SRobert Love 35011da177e4SLinus Torvalds return error; 35021da177e4SLinus Torvalds } 35031da177e4SLinus Torvalds 35041da177e4SLinus Torvalds /* 35051da177e4SLinus Torvalds * Make sure that the actual truncation of the file will occur outside its 35061b1dcc1bSJes Sorensen * directory's i_mutex. Truncate can take a long time if there is a lot of 35071da177e4SLinus Torvalds * writeout happening, and we don't want to prevent access to the directory 35081da177e4SLinus Torvalds * while waiting on the I/O. 35091da177e4SLinus Torvalds */ 35105590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname) 35111da177e4SLinus Torvalds { 35122ad94ae6SAl Viro int error; 351391a27b2aSJeff Layton struct filename *name; 35141da177e4SLinus Torvalds struct dentry *dentry; 35151da177e4SLinus Torvalds struct nameidata nd; 35161da177e4SLinus Torvalds struct inode *inode = NULL; 35175d18f813SJeff Layton unsigned int lookup_flags = 0; 35185d18f813SJeff Layton retry: 35195d18f813SJeff Layton name = user_path_parent(dfd, pathname, &nd, lookup_flags); 352091a27b2aSJeff Layton if (IS_ERR(name)) 352191a27b2aSJeff Layton return PTR_ERR(name); 35222ad94ae6SAl Viro 35231da177e4SLinus Torvalds error = -EISDIR; 35241da177e4SLinus Torvalds if (nd.last_type != LAST_NORM) 35251da177e4SLinus Torvalds goto exit1; 35260612d9fbSOGAWA Hirofumi 35270612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 3528c30dabfeSJan Kara error = mnt_want_write(nd.path.mnt); 3529c30dabfeSJan Kara if (error) 3530c30dabfeSJan Kara goto exit1; 35310612d9fbSOGAWA Hirofumi 35324ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 353349705b77SChristoph Hellwig dentry = lookup_hash(&nd); 35341da177e4SLinus Torvalds error = PTR_ERR(dentry); 35351da177e4SLinus Torvalds if (!IS_ERR(dentry)) { 35361da177e4SLinus Torvalds /* Why not before? Because we want correct error value */ 353750338b88STörök Edwin if (nd.last.name[nd.last.len]) 353850338b88STörök Edwin goto slashes; 35391da177e4SLinus Torvalds inode = dentry->d_inode; 354050338b88STörök Edwin if (!inode) 3541e6bc45d6STheodore Ts'o goto slashes; 35427de9c6eeSAl Viro ihold(inode); 3543be6d3e56SKentaro Takeda error = security_path_unlink(&nd.path, dentry); 3544be6d3e56SKentaro Takeda if (error) 3545c30dabfeSJan Kara goto exit2; 35464ac91378SJan Blunck error = vfs_unlink(nd.path.dentry->d_inode, dentry); 35471da177e4SLinus Torvalds exit2: 35481da177e4SLinus Torvalds dput(dentry); 35491da177e4SLinus Torvalds } 35504ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 35511da177e4SLinus Torvalds if (inode) 35521da177e4SLinus Torvalds iput(inode); /* truncate the inode here */ 3553c30dabfeSJan Kara mnt_drop_write(nd.path.mnt); 35541da177e4SLinus Torvalds exit1: 35551d957f9bSJan Blunck path_put(&nd.path); 35561da177e4SLinus Torvalds putname(name); 35575d18f813SJeff Layton if (retry_estale(error, lookup_flags)) { 35585d18f813SJeff Layton lookup_flags |= LOOKUP_REVAL; 35595d18f813SJeff Layton inode = NULL; 35605d18f813SJeff Layton goto retry; 35615d18f813SJeff Layton } 35621da177e4SLinus Torvalds return error; 35631da177e4SLinus Torvalds 35641da177e4SLinus Torvalds slashes: 35651da177e4SLinus Torvalds error = !dentry->d_inode ? -ENOENT : 35661da177e4SLinus Torvalds S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR; 35671da177e4SLinus Torvalds goto exit2; 35681da177e4SLinus Torvalds } 35691da177e4SLinus Torvalds 35702e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag) 35715590ff0dSUlrich Drepper { 35725590ff0dSUlrich Drepper if ((flag & ~AT_REMOVEDIR) != 0) 35735590ff0dSUlrich Drepper return -EINVAL; 35745590ff0dSUlrich Drepper 35755590ff0dSUlrich Drepper if (flag & AT_REMOVEDIR) 35765590ff0dSUlrich Drepper return do_rmdir(dfd, pathname); 35775590ff0dSUlrich Drepper 35785590ff0dSUlrich Drepper return do_unlinkat(dfd, pathname); 35795590ff0dSUlrich Drepper } 35805590ff0dSUlrich Drepper 35813480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname) 35825590ff0dSUlrich Drepper { 35835590ff0dSUlrich Drepper return do_unlinkat(AT_FDCWD, pathname); 35845590ff0dSUlrich Drepper } 35855590ff0dSUlrich Drepper 3586db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname) 35871da177e4SLinus Torvalds { 3588a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 35891da177e4SLinus Torvalds 35901da177e4SLinus Torvalds if (error) 35911da177e4SLinus Torvalds return error; 35921da177e4SLinus Torvalds 3593acfa4380SAl Viro if (!dir->i_op->symlink) 35941da177e4SLinus Torvalds return -EPERM; 35951da177e4SLinus Torvalds 35961da177e4SLinus Torvalds error = security_inode_symlink(dir, dentry, oldname); 35971da177e4SLinus Torvalds if (error) 35981da177e4SLinus Torvalds return error; 35991da177e4SLinus Torvalds 36001da177e4SLinus Torvalds error = dir->i_op->symlink(dir, dentry, oldname); 3601a74574aaSStephen Smalley if (!error) 3602f38aa942SAmy Griffis fsnotify_create(dir, dentry); 36031da177e4SLinus Torvalds return error; 36041da177e4SLinus Torvalds } 36051da177e4SLinus Torvalds 36062e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname, 36072e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 36081da177e4SLinus Torvalds { 36092ad94ae6SAl Viro int error; 361091a27b2aSJeff Layton struct filename *from; 36116902d925SDave Hansen struct dentry *dentry; 3612dae6ad8fSAl Viro struct path path; 3613f46d3567SJeff Layton unsigned int lookup_flags = 0; 36141da177e4SLinus Torvalds 36151da177e4SLinus Torvalds from = getname(oldname); 36161da177e4SLinus Torvalds if (IS_ERR(from)) 36171da177e4SLinus Torvalds return PTR_ERR(from); 3618f46d3567SJeff Layton retry: 3619f46d3567SJeff Layton dentry = user_path_create(newdfd, newname, &path, lookup_flags); 36201da177e4SLinus Torvalds error = PTR_ERR(dentry); 36216902d925SDave Hansen if (IS_ERR(dentry)) 3622dae6ad8fSAl Viro goto out_putname; 36236902d925SDave Hansen 362491a27b2aSJeff Layton error = security_path_symlink(&path, dentry, from->name); 3625a8104a9fSAl Viro if (!error) 362691a27b2aSJeff Layton error = vfs_symlink(path.dentry->d_inode, dentry, from->name); 3627921a1650SAl Viro done_path_create(&path, dentry); 3628f46d3567SJeff Layton if (retry_estale(error, lookup_flags)) { 3629f46d3567SJeff Layton lookup_flags |= LOOKUP_REVAL; 3630f46d3567SJeff Layton goto retry; 3631f46d3567SJeff Layton } 36326902d925SDave Hansen out_putname: 36331da177e4SLinus Torvalds putname(from); 36341da177e4SLinus Torvalds return error; 36351da177e4SLinus Torvalds } 36361da177e4SLinus Torvalds 36373480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname) 36385590ff0dSUlrich Drepper { 36395590ff0dSUlrich Drepper return sys_symlinkat(oldname, AT_FDCWD, newname); 36405590ff0dSUlrich Drepper } 36415590ff0dSUlrich Drepper 36421da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) 36431da177e4SLinus Torvalds { 36441da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 36458de52778SAl Viro unsigned max_links = dir->i_sb->s_max_links; 36461da177e4SLinus Torvalds int error; 36471da177e4SLinus Torvalds 36481da177e4SLinus Torvalds if (!inode) 36491da177e4SLinus Torvalds return -ENOENT; 36501da177e4SLinus Torvalds 3651a95164d9SMiklos Szeredi error = may_create(dir, new_dentry); 36521da177e4SLinus Torvalds if (error) 36531da177e4SLinus Torvalds return error; 36541da177e4SLinus Torvalds 36551da177e4SLinus Torvalds if (dir->i_sb != inode->i_sb) 36561da177e4SLinus Torvalds return -EXDEV; 36571da177e4SLinus Torvalds 36581da177e4SLinus Torvalds /* 36591da177e4SLinus Torvalds * A link to an append-only or immutable file cannot be created. 36601da177e4SLinus Torvalds */ 36611da177e4SLinus Torvalds if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) 36621da177e4SLinus Torvalds return -EPERM; 3663acfa4380SAl Viro if (!dir->i_op->link) 36641da177e4SLinus Torvalds return -EPERM; 36657e79eedbSTetsuo Handa if (S_ISDIR(inode->i_mode)) 36661da177e4SLinus Torvalds return -EPERM; 36671da177e4SLinus Torvalds 36681da177e4SLinus Torvalds error = security_inode_link(old_dentry, dir, new_dentry); 36691da177e4SLinus Torvalds if (error) 36701da177e4SLinus Torvalds return error; 36711da177e4SLinus Torvalds 36727e79eedbSTetsuo Handa mutex_lock(&inode->i_mutex); 3673aae8a97dSAneesh Kumar K.V /* Make sure we don't allow creating hardlink to an unlinked file */ 3674f4e0c30cSAl Viro if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE)) 3675aae8a97dSAneesh Kumar K.V error = -ENOENT; 36768de52778SAl Viro else if (max_links && inode->i_nlink >= max_links) 36778de52778SAl Viro error = -EMLINK; 3678aae8a97dSAneesh Kumar K.V else 36791da177e4SLinus Torvalds error = dir->i_op->link(old_dentry, dir, new_dentry); 3680f4e0c30cSAl Viro 3681f4e0c30cSAl Viro if (!error && (inode->i_state & I_LINKABLE)) { 3682f4e0c30cSAl Viro spin_lock(&inode->i_lock); 3683f4e0c30cSAl Viro inode->i_state &= ~I_LINKABLE; 3684f4e0c30cSAl Viro spin_unlock(&inode->i_lock); 3685f4e0c30cSAl Viro } 36867e79eedbSTetsuo Handa mutex_unlock(&inode->i_mutex); 3687e31e14ecSStephen Smalley if (!error) 36887e79eedbSTetsuo Handa fsnotify_link(dir, inode, new_dentry); 36891da177e4SLinus Torvalds return error; 36901da177e4SLinus Torvalds } 36911da177e4SLinus Torvalds 36921da177e4SLinus Torvalds /* 36931da177e4SLinus Torvalds * Hardlinks are often used in delicate situations. We avoid 36941da177e4SLinus Torvalds * security-related surprises by not following symlinks on the 36951da177e4SLinus Torvalds * newname. --KAB 36961da177e4SLinus Torvalds * 36971da177e4SLinus Torvalds * We don't follow them on the oldname either to be compatible 36981da177e4SLinus Torvalds * with linux 2.0, and to avoid hard-linking to directories 36991da177e4SLinus Torvalds * and other special files. --ADM 37001da177e4SLinus Torvalds */ 37012e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname, 37022e4d0924SHeiko Carstens int, newdfd, const char __user *, newname, int, flags) 37031da177e4SLinus Torvalds { 37041da177e4SLinus Torvalds struct dentry *new_dentry; 3705dae6ad8fSAl Viro struct path old_path, new_path; 370611a7b371SAneesh Kumar K.V int how = 0; 37071da177e4SLinus Torvalds int error; 37081da177e4SLinus Torvalds 370911a7b371SAneesh Kumar K.V if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0) 3710c04030e1SUlrich Drepper return -EINVAL; 371111a7b371SAneesh Kumar K.V /* 3712f0cc6ffbSLinus Torvalds * To use null names we require CAP_DAC_READ_SEARCH 3713f0cc6ffbSLinus Torvalds * This ensures that not everyone will be able to create 3714f0cc6ffbSLinus Torvalds * handlink using the passed filedescriptor. 371511a7b371SAneesh Kumar K.V */ 3716f0cc6ffbSLinus Torvalds if (flags & AT_EMPTY_PATH) { 3717f0cc6ffbSLinus Torvalds if (!capable(CAP_DAC_READ_SEARCH)) 3718f0cc6ffbSLinus Torvalds return -ENOENT; 371911a7b371SAneesh Kumar K.V how = LOOKUP_EMPTY; 3720f0cc6ffbSLinus Torvalds } 3721c04030e1SUlrich Drepper 372211a7b371SAneesh Kumar K.V if (flags & AT_SYMLINK_FOLLOW) 372311a7b371SAneesh Kumar K.V how |= LOOKUP_FOLLOW; 3724442e31caSJeff Layton retry: 372511a7b371SAneesh Kumar K.V error = user_path_at(olddfd, oldname, how, &old_path); 37261da177e4SLinus Torvalds if (error) 37272ad94ae6SAl Viro return error; 37282ad94ae6SAl Viro 3729442e31caSJeff Layton new_dentry = user_path_create(newdfd, newname, &new_path, 3730442e31caSJeff Layton (how & LOOKUP_REVAL)); 37311da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 37326902d925SDave Hansen if (IS_ERR(new_dentry)) 3733dae6ad8fSAl Viro goto out; 3734dae6ad8fSAl Viro 3735dae6ad8fSAl Viro error = -EXDEV; 3736dae6ad8fSAl Viro if (old_path.mnt != new_path.mnt) 3737dae6ad8fSAl Viro goto out_dput; 3738800179c9SKees Cook error = may_linkat(&old_path); 3739800179c9SKees Cook if (unlikely(error)) 3740800179c9SKees Cook goto out_dput; 3741dae6ad8fSAl Viro error = security_path_link(old_path.dentry, &new_path, new_dentry); 3742be6d3e56SKentaro Takeda if (error) 3743a8104a9fSAl Viro goto out_dput; 3744dae6ad8fSAl Viro error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry); 374575c3f29dSDave Hansen out_dput: 3746921a1650SAl Viro done_path_create(&new_path, new_dentry); 3747442e31caSJeff Layton if (retry_estale(error, how)) { 3748442e31caSJeff Layton how |= LOOKUP_REVAL; 3749442e31caSJeff Layton goto retry; 3750442e31caSJeff Layton } 37511da177e4SLinus Torvalds out: 37522d8f3038SAl Viro path_put(&old_path); 37531da177e4SLinus Torvalds 37541da177e4SLinus Torvalds return error; 37551da177e4SLinus Torvalds } 37561da177e4SLinus Torvalds 37573480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname) 37585590ff0dSUlrich Drepper { 3759c04030e1SUlrich Drepper return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0); 37605590ff0dSUlrich Drepper } 37615590ff0dSUlrich Drepper 37621da177e4SLinus Torvalds /* 37631da177e4SLinus Torvalds * The worst of all namespace operations - renaming directory. "Perverted" 37641da177e4SLinus Torvalds * doesn't even start to describe it. Somebody in UCB had a heck of a trip... 37651da177e4SLinus Torvalds * Problems: 37661da177e4SLinus Torvalds * a) we can get into loop creation. Check is done in is_subdir(). 37671da177e4SLinus Torvalds * b) race potential - two innocent renames can create a loop together. 37681da177e4SLinus Torvalds * That's where 4.4 screws up. Current fix: serialization on 3769a11f3a05SArjan van de Ven * sb->s_vfs_rename_mutex. We might be more accurate, but that's another 37701da177e4SLinus Torvalds * story. 37711da177e4SLinus Torvalds * c) we have to lock _three_ objects - parents and victim (if it exists). 37721b1dcc1bSJes Sorensen * And that - after we got ->i_mutex on parents (until then we don't know 37731da177e4SLinus Torvalds * whether the target exists). Solution: try to be smart with locking 37741da177e4SLinus Torvalds * order for inodes. We rely on the fact that tree topology may change 3775a11f3a05SArjan van de Ven * only under ->s_vfs_rename_mutex _and_ that parent of the object we 37761da177e4SLinus Torvalds * move will be locked. Thus we can rank directories by the tree 37771da177e4SLinus Torvalds * (ancestors first) and rank all non-directories after them. 37781da177e4SLinus Torvalds * That works since everybody except rename does "lock parent, lookup, 3779a11f3a05SArjan van de Ven * lock child" and rename is under ->s_vfs_rename_mutex. 37801da177e4SLinus Torvalds * HOWEVER, it relies on the assumption that any object with ->lookup() 37811da177e4SLinus Torvalds * has no more than 1 dentry. If "hybrid" objects will ever appear, 37821da177e4SLinus Torvalds * we'd better make sure that there's no link(2) for them. 3783e4eaac06SSage Weil * d) conversion from fhandle to dentry may come in the wrong moment - when 37841b1dcc1bSJes Sorensen * we are removing the target. Solution: we will have to grab ->i_mutex 37851da177e4SLinus Torvalds * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on 3786c41b20e7SAdam Buchbinder * ->i_mutex on parents, which works but leads to some truly excessive 37871da177e4SLinus Torvalds * locking]. 37881da177e4SLinus Torvalds */ 378975c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry, 37901da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 37911da177e4SLinus Torvalds { 37921da177e4SLinus Torvalds int error = 0; 37939055cba7SSage Weil struct inode *target = new_dentry->d_inode; 37948de52778SAl Viro unsigned max_links = new_dir->i_sb->s_max_links; 37951da177e4SLinus Torvalds 37961da177e4SLinus Torvalds /* 37971da177e4SLinus Torvalds * If we are going to change the parent - check write permissions, 37981da177e4SLinus Torvalds * we'll need to flip '..'. 37991da177e4SLinus Torvalds */ 38001da177e4SLinus Torvalds if (new_dir != old_dir) { 3801f419a2e3SAl Viro error = inode_permission(old_dentry->d_inode, MAY_WRITE); 38021da177e4SLinus Torvalds if (error) 38031da177e4SLinus Torvalds return error; 38041da177e4SLinus Torvalds } 38051da177e4SLinus Torvalds 38061da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 38071da177e4SLinus Torvalds if (error) 38081da177e4SLinus Torvalds return error; 38091da177e4SLinus Torvalds 38101d2ef590SAl Viro dget(new_dentry); 3811d83c49f3SAl Viro if (target) 38121b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 38139055cba7SSage Weil 38141da177e4SLinus Torvalds error = -EBUSY; 38159055cba7SSage Weil if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry)) 38169055cba7SSage Weil goto out; 38179055cba7SSage Weil 38188de52778SAl Viro error = -EMLINK; 38198de52778SAl Viro if (max_links && !target && new_dir != old_dir && 38208de52778SAl Viro new_dir->i_nlink >= max_links) 38218de52778SAl Viro goto out; 38228de52778SAl Viro 38233cebde24SSage Weil if (target) 38243cebde24SSage Weil shrink_dcache_parent(new_dentry); 38251da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 38269055cba7SSage Weil if (error) 38279055cba7SSage Weil goto out; 38289055cba7SSage Weil 38291da177e4SLinus Torvalds if (target) { 38301da177e4SLinus Torvalds target->i_flags |= S_DEAD; 3831d83c49f3SAl Viro dont_mount(new_dentry); 3832d83c49f3SAl Viro } 38339055cba7SSage Weil out: 38349055cba7SSage Weil if (target) 38351b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 38361d2ef590SAl Viro dput(new_dentry); 3837e31e14ecSStephen Smalley if (!error) 3838349457ccSMark Fasheh if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) 38391da177e4SLinus Torvalds d_move(old_dentry,new_dentry); 38401da177e4SLinus Torvalds return error; 38411da177e4SLinus Torvalds } 38421da177e4SLinus Torvalds 384375c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry, 38441da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 38451da177e4SLinus Torvalds { 384651892bbbSSage Weil struct inode *target = new_dentry->d_inode; 38471da177e4SLinus Torvalds int error; 38481da177e4SLinus Torvalds 38491da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 38501da177e4SLinus Torvalds if (error) 38511da177e4SLinus Torvalds return error; 38521da177e4SLinus Torvalds 38531da177e4SLinus Torvalds dget(new_dentry); 38541da177e4SLinus Torvalds if (target) 38551b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 385651892bbbSSage Weil 38571da177e4SLinus Torvalds error = -EBUSY; 385851892bbbSSage Weil if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry)) 385951892bbbSSage Weil goto out; 386051892bbbSSage Weil 38611da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 386251892bbbSSage Weil if (error) 386351892bbbSSage Weil goto out; 386451892bbbSSage Weil 3865bec1052eSAl Viro if (target) 3866d83c49f3SAl Viro dont_mount(new_dentry); 3867349457ccSMark Fasheh if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) 38681da177e4SLinus Torvalds d_move(old_dentry, new_dentry); 386951892bbbSSage Weil out: 38701da177e4SLinus Torvalds if (target) 38711b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 38721da177e4SLinus Torvalds dput(new_dentry); 38731da177e4SLinus Torvalds return error; 38741da177e4SLinus Torvalds } 38751da177e4SLinus Torvalds 38761da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry, 38771da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 38781da177e4SLinus Torvalds { 38791da177e4SLinus Torvalds int error; 38801da177e4SLinus Torvalds int is_dir = S_ISDIR(old_dentry->d_inode->i_mode); 388159b0df21SEric Paris const unsigned char *old_name; 38821da177e4SLinus Torvalds 38831da177e4SLinus Torvalds if (old_dentry->d_inode == new_dentry->d_inode) 38841da177e4SLinus Torvalds return 0; 38851da177e4SLinus Torvalds 38861da177e4SLinus Torvalds error = may_delete(old_dir, old_dentry, is_dir); 38871da177e4SLinus Torvalds if (error) 38881da177e4SLinus Torvalds return error; 38891da177e4SLinus Torvalds 38901da177e4SLinus Torvalds if (!new_dentry->d_inode) 3891a95164d9SMiklos Szeredi error = may_create(new_dir, new_dentry); 38921da177e4SLinus Torvalds else 38931da177e4SLinus Torvalds error = may_delete(new_dir, new_dentry, is_dir); 38941da177e4SLinus Torvalds if (error) 38951da177e4SLinus Torvalds return error; 38961da177e4SLinus Torvalds 3897acfa4380SAl Viro if (!old_dir->i_op->rename) 38981da177e4SLinus Torvalds return -EPERM; 38991da177e4SLinus Torvalds 39000eeca283SRobert Love old_name = fsnotify_oldname_init(old_dentry->d_name.name); 39010eeca283SRobert Love 39021da177e4SLinus Torvalds if (is_dir) 39031da177e4SLinus Torvalds error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry); 39041da177e4SLinus Torvalds else 39051da177e4SLinus Torvalds error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry); 3906123df294SAl Viro if (!error) 3907123df294SAl Viro fsnotify_move(old_dir, new_dir, old_name, is_dir, 39085a190ae6SAl Viro new_dentry->d_inode, old_dentry); 39090eeca283SRobert Love fsnotify_oldname_free(old_name); 39100eeca283SRobert Love 39111da177e4SLinus Torvalds return error; 39121da177e4SLinus Torvalds } 39131da177e4SLinus Torvalds 39142e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname, 39152e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 39161da177e4SLinus Torvalds { 39171da177e4SLinus Torvalds struct dentry *old_dir, *new_dir; 39181da177e4SLinus Torvalds struct dentry *old_dentry, *new_dentry; 39191da177e4SLinus Torvalds struct dentry *trap; 39201da177e4SLinus Torvalds struct nameidata oldnd, newnd; 392191a27b2aSJeff Layton struct filename *from; 392291a27b2aSJeff Layton struct filename *to; 3923c6a94284SJeff Layton unsigned int lookup_flags = 0; 3924c6a94284SJeff Layton bool should_retry = false; 39252ad94ae6SAl Viro int error; 3926c6a94284SJeff Layton retry: 3927c6a94284SJeff Layton from = user_path_parent(olddfd, oldname, &oldnd, lookup_flags); 392891a27b2aSJeff Layton if (IS_ERR(from)) { 392991a27b2aSJeff Layton error = PTR_ERR(from); 39301da177e4SLinus Torvalds goto exit; 393191a27b2aSJeff Layton } 39321da177e4SLinus Torvalds 3933c6a94284SJeff Layton to = user_path_parent(newdfd, newname, &newnd, lookup_flags); 393491a27b2aSJeff Layton if (IS_ERR(to)) { 393591a27b2aSJeff Layton error = PTR_ERR(to); 39361da177e4SLinus Torvalds goto exit1; 393791a27b2aSJeff Layton } 39381da177e4SLinus Torvalds 39391da177e4SLinus Torvalds error = -EXDEV; 39404ac91378SJan Blunck if (oldnd.path.mnt != newnd.path.mnt) 39411da177e4SLinus Torvalds goto exit2; 39421da177e4SLinus Torvalds 39434ac91378SJan Blunck old_dir = oldnd.path.dentry; 39441da177e4SLinus Torvalds error = -EBUSY; 39451da177e4SLinus Torvalds if (oldnd.last_type != LAST_NORM) 39461da177e4SLinus Torvalds goto exit2; 39471da177e4SLinus Torvalds 39484ac91378SJan Blunck new_dir = newnd.path.dentry; 39491da177e4SLinus Torvalds if (newnd.last_type != LAST_NORM) 39501da177e4SLinus Torvalds goto exit2; 39511da177e4SLinus Torvalds 3952c30dabfeSJan Kara error = mnt_want_write(oldnd.path.mnt); 3953c30dabfeSJan Kara if (error) 3954c30dabfeSJan Kara goto exit2; 3955c30dabfeSJan Kara 39560612d9fbSOGAWA Hirofumi oldnd.flags &= ~LOOKUP_PARENT; 39570612d9fbSOGAWA Hirofumi newnd.flags &= ~LOOKUP_PARENT; 39584e9ed2f8SOGAWA Hirofumi newnd.flags |= LOOKUP_RENAME_TARGET; 39590612d9fbSOGAWA Hirofumi 39601da177e4SLinus Torvalds trap = lock_rename(new_dir, old_dir); 39611da177e4SLinus Torvalds 396249705b77SChristoph Hellwig old_dentry = lookup_hash(&oldnd); 39631da177e4SLinus Torvalds error = PTR_ERR(old_dentry); 39641da177e4SLinus Torvalds if (IS_ERR(old_dentry)) 39651da177e4SLinus Torvalds goto exit3; 39661da177e4SLinus Torvalds /* source must exist */ 39671da177e4SLinus Torvalds error = -ENOENT; 39681da177e4SLinus Torvalds if (!old_dentry->d_inode) 39691da177e4SLinus Torvalds goto exit4; 39701da177e4SLinus Torvalds /* unless the source is a directory trailing slashes give -ENOTDIR */ 39711da177e4SLinus Torvalds if (!S_ISDIR(old_dentry->d_inode->i_mode)) { 39721da177e4SLinus Torvalds error = -ENOTDIR; 39731da177e4SLinus Torvalds if (oldnd.last.name[oldnd.last.len]) 39741da177e4SLinus Torvalds goto exit4; 39751da177e4SLinus Torvalds if (newnd.last.name[newnd.last.len]) 39761da177e4SLinus Torvalds goto exit4; 39771da177e4SLinus Torvalds } 39781da177e4SLinus Torvalds /* source should not be ancestor of target */ 39791da177e4SLinus Torvalds error = -EINVAL; 39801da177e4SLinus Torvalds if (old_dentry == trap) 39811da177e4SLinus Torvalds goto exit4; 398249705b77SChristoph Hellwig new_dentry = lookup_hash(&newnd); 39831da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 39841da177e4SLinus Torvalds if (IS_ERR(new_dentry)) 39851da177e4SLinus Torvalds goto exit4; 39861da177e4SLinus Torvalds /* target should not be an ancestor of source */ 39871da177e4SLinus Torvalds error = -ENOTEMPTY; 39881da177e4SLinus Torvalds if (new_dentry == trap) 39891da177e4SLinus Torvalds goto exit5; 39901da177e4SLinus Torvalds 3991be6d3e56SKentaro Takeda error = security_path_rename(&oldnd.path, old_dentry, 3992be6d3e56SKentaro Takeda &newnd.path, new_dentry); 3993be6d3e56SKentaro Takeda if (error) 3994c30dabfeSJan Kara goto exit5; 39951da177e4SLinus Torvalds error = vfs_rename(old_dir->d_inode, old_dentry, 39961da177e4SLinus Torvalds new_dir->d_inode, new_dentry); 39971da177e4SLinus Torvalds exit5: 39981da177e4SLinus Torvalds dput(new_dentry); 39991da177e4SLinus Torvalds exit4: 40001da177e4SLinus Torvalds dput(old_dentry); 40011da177e4SLinus Torvalds exit3: 40021da177e4SLinus Torvalds unlock_rename(new_dir, old_dir); 4003c30dabfeSJan Kara mnt_drop_write(oldnd.path.mnt); 40041da177e4SLinus Torvalds exit2: 4005c6a94284SJeff Layton if (retry_estale(error, lookup_flags)) 4006c6a94284SJeff Layton should_retry = true; 40071d957f9bSJan Blunck path_put(&newnd.path); 40082ad94ae6SAl Viro putname(to); 40091da177e4SLinus Torvalds exit1: 40101d957f9bSJan Blunck path_put(&oldnd.path); 40111da177e4SLinus Torvalds putname(from); 4012c6a94284SJeff Layton if (should_retry) { 4013c6a94284SJeff Layton should_retry = false; 4014c6a94284SJeff Layton lookup_flags |= LOOKUP_REVAL; 4015c6a94284SJeff Layton goto retry; 4016c6a94284SJeff Layton } 40172ad94ae6SAl Viro exit: 40181da177e4SLinus Torvalds return error; 40191da177e4SLinus Torvalds } 40201da177e4SLinus Torvalds 4021a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname) 40225590ff0dSUlrich Drepper { 40235590ff0dSUlrich Drepper return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname); 40245590ff0dSUlrich Drepper } 40255590ff0dSUlrich Drepper 40261da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link) 40271da177e4SLinus Torvalds { 40281da177e4SLinus Torvalds int len; 40291da177e4SLinus Torvalds 40301da177e4SLinus Torvalds len = PTR_ERR(link); 40311da177e4SLinus Torvalds if (IS_ERR(link)) 40321da177e4SLinus Torvalds goto out; 40331da177e4SLinus Torvalds 40341da177e4SLinus Torvalds len = strlen(link); 40351da177e4SLinus Torvalds if (len > (unsigned) buflen) 40361da177e4SLinus Torvalds len = buflen; 40371da177e4SLinus Torvalds if (copy_to_user(buffer, link, len)) 40381da177e4SLinus Torvalds len = -EFAULT; 40391da177e4SLinus Torvalds out: 40401da177e4SLinus Torvalds return len; 40411da177e4SLinus Torvalds } 40421da177e4SLinus Torvalds 40431da177e4SLinus Torvalds /* 40441da177e4SLinus Torvalds * A helper for ->readlink(). This should be used *ONLY* for symlinks that 40451da177e4SLinus Torvalds * have ->follow_link() touching nd only in nd_set_link(). Using (or not 40461da177e4SLinus Torvalds * using) it for any given inode is up to filesystem. 40471da177e4SLinus Torvalds */ 40481da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen) 40491da177e4SLinus Torvalds { 40501da177e4SLinus Torvalds struct nameidata nd; 4051cc314eefSLinus Torvalds void *cookie; 4052694a1764SMarcin Slusarz int res; 4053cc314eefSLinus Torvalds 40541da177e4SLinus Torvalds nd.depth = 0; 4055cc314eefSLinus Torvalds cookie = dentry->d_inode->i_op->follow_link(dentry, &nd); 4056694a1764SMarcin Slusarz if (IS_ERR(cookie)) 4057694a1764SMarcin Slusarz return PTR_ERR(cookie); 4058694a1764SMarcin Slusarz 4059694a1764SMarcin Slusarz res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd)); 40601da177e4SLinus Torvalds if (dentry->d_inode->i_op->put_link) 4061cc314eefSLinus Torvalds dentry->d_inode->i_op->put_link(dentry, &nd, cookie); 4062694a1764SMarcin Slusarz return res; 40631da177e4SLinus Torvalds } 40641da177e4SLinus Torvalds 40651da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link) 40661da177e4SLinus Torvalds { 40671da177e4SLinus Torvalds return __vfs_follow_link(nd, link); 40681da177e4SLinus Torvalds } 40691da177e4SLinus Torvalds 40701da177e4SLinus Torvalds /* get the link contents into pagecache */ 40711da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage) 40721da177e4SLinus Torvalds { 4073ebd09abbSDuane Griffin char *kaddr; 40741da177e4SLinus Torvalds struct page *page; 40751da177e4SLinus Torvalds struct address_space *mapping = dentry->d_inode->i_mapping; 4076090d2b18SPekka Enberg page = read_mapping_page(mapping, 0, NULL); 40771da177e4SLinus Torvalds if (IS_ERR(page)) 40786fe6900eSNick Piggin return (char*)page; 40791da177e4SLinus Torvalds *ppage = page; 4080ebd09abbSDuane Griffin kaddr = kmap(page); 4081ebd09abbSDuane Griffin nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1); 4082ebd09abbSDuane Griffin return kaddr; 40831da177e4SLinus Torvalds } 40841da177e4SLinus Torvalds 40851da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen) 40861da177e4SLinus Torvalds { 40871da177e4SLinus Torvalds struct page *page = NULL; 40881da177e4SLinus Torvalds char *s = page_getlink(dentry, &page); 40891da177e4SLinus Torvalds int res = vfs_readlink(dentry,buffer,buflen,s); 40901da177e4SLinus Torvalds if (page) { 40911da177e4SLinus Torvalds kunmap(page); 40921da177e4SLinus Torvalds page_cache_release(page); 40931da177e4SLinus Torvalds } 40941da177e4SLinus Torvalds return res; 40951da177e4SLinus Torvalds } 40961da177e4SLinus Torvalds 4097cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd) 40981da177e4SLinus Torvalds { 4099cc314eefSLinus Torvalds struct page *page = NULL; 41001da177e4SLinus Torvalds nd_set_link(nd, page_getlink(dentry, &page)); 4101cc314eefSLinus Torvalds return page; 41021da177e4SLinus Torvalds } 41031da177e4SLinus Torvalds 4104cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie) 41051da177e4SLinus Torvalds { 4106cc314eefSLinus Torvalds struct page *page = cookie; 4107cc314eefSLinus Torvalds 4108cc314eefSLinus Torvalds if (page) { 41091da177e4SLinus Torvalds kunmap(page); 41101da177e4SLinus Torvalds page_cache_release(page); 41111da177e4SLinus Torvalds } 41121da177e4SLinus Torvalds } 41131da177e4SLinus Torvalds 411454566b2cSNick Piggin /* 411554566b2cSNick Piggin * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS 411654566b2cSNick Piggin */ 411754566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs) 41181da177e4SLinus Torvalds { 41191da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 41200adb25d2SKirill Korotaev struct page *page; 4121afddba49SNick Piggin void *fsdata; 4122beb497abSDmitriy Monakhov int err; 41231da177e4SLinus Torvalds char *kaddr; 412454566b2cSNick Piggin unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE; 412554566b2cSNick Piggin if (nofs) 412654566b2cSNick Piggin flags |= AOP_FLAG_NOFS; 41271da177e4SLinus Torvalds 41287e53cac4SNeilBrown retry: 4129afddba49SNick Piggin err = pagecache_write_begin(NULL, mapping, 0, len-1, 413054566b2cSNick Piggin flags, &page, &fsdata); 41311da177e4SLinus Torvalds if (err) 4132afddba49SNick Piggin goto fail; 4133afddba49SNick Piggin 4134e8e3c3d6SCong Wang kaddr = kmap_atomic(page); 41351da177e4SLinus Torvalds memcpy(kaddr, symname, len-1); 4136e8e3c3d6SCong Wang kunmap_atomic(kaddr); 4137afddba49SNick Piggin 4138afddba49SNick Piggin err = pagecache_write_end(NULL, mapping, 0, len-1, len-1, 4139afddba49SNick Piggin page, fsdata); 41401da177e4SLinus Torvalds if (err < 0) 41411da177e4SLinus Torvalds goto fail; 4142afddba49SNick Piggin if (err < len-1) 4143afddba49SNick Piggin goto retry; 4144afddba49SNick Piggin 41451da177e4SLinus Torvalds mark_inode_dirty(inode); 41461da177e4SLinus Torvalds return 0; 41471da177e4SLinus Torvalds fail: 41481da177e4SLinus Torvalds return err; 41491da177e4SLinus Torvalds } 41501da177e4SLinus Torvalds 41510adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len) 41520adb25d2SKirill Korotaev { 41530adb25d2SKirill Korotaev return __page_symlink(inode, symname, len, 415454566b2cSNick Piggin !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS)); 41550adb25d2SKirill Korotaev } 41560adb25d2SKirill Korotaev 415792e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = { 41581da177e4SLinus Torvalds .readlink = generic_readlink, 41591da177e4SLinus Torvalds .follow_link = page_follow_link_light, 41601da177e4SLinus Torvalds .put_link = page_put_link, 41611da177e4SLinus Torvalds }; 41621da177e4SLinus Torvalds 41632d8f3038SAl Viro EXPORT_SYMBOL(user_path_at); 4164cc53ce53SDavid Howells EXPORT_SYMBOL(follow_down_one); 41651da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down); 41661da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up); 4167f6d2ac5cSAl Viro EXPORT_SYMBOL(get_write_access); /* nfsd */ 41681da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename); 41691da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len); 41701da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light); 41711da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link); 41721da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink); 41730adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink); 41741da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink); 41751da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations); 4176d1811465SAl Viro EXPORT_SYMBOL(kern_path); 417716f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup); 4178f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission); 41791da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename); 41801da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create); 41811da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link); 41821da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link); 41831da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir); 41841da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod); 41851da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission); 41861da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink); 41871da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename); 41881da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir); 41891da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink); 41901da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink); 41911da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash); 41921da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink); 4193