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 */ 4545dd784d0SJan Blunck void path_get(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 */ 4671d957f9bSJan Blunck void path_put(struct path *path) 4681da177e4SLinus Torvalds { 4691d957f9bSJan Blunck dput(path->dentry); 4701d957f9bSJan Blunck mntput(path->mnt); 4711da177e4SLinus Torvalds } 4721d957f9bSJan Blunck EXPORT_SYMBOL(path_put); 4731da177e4SLinus Torvalds 47419660af7SAl Viro /* 47531e6b01fSNick Piggin * Path walking has 2 modes, rcu-walk and ref-walk (see 47619660af7SAl Viro * Documentation/filesystems/path-lookup.txt). In situations when we can't 47719660af7SAl Viro * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab 47819660af7SAl Viro * normal reference counts on dentries and vfsmounts to transition to rcu-walk 47919660af7SAl Viro * mode. Refcounts are grabbed at the last known good point before rcu-walk 48019660af7SAl Viro * got stuck, so ref-walk may continue from there. If this is not successful 48119660af7SAl Viro * (eg. a seqcount has changed), then failure is returned and it's up to caller 48219660af7SAl Viro * to restart the path walk from the beginning in ref-walk mode. 48331e6b01fSNick Piggin */ 48431e6b01fSNick Piggin 48532a7991bSAl Viro static inline void lock_rcu_walk(void) 48632a7991bSAl Viro { 48732a7991bSAl Viro br_read_lock(&vfsmount_lock); 48832a7991bSAl Viro rcu_read_lock(); 48932a7991bSAl Viro } 49032a7991bSAl Viro 49132a7991bSAl Viro static inline void unlock_rcu_walk(void) 49232a7991bSAl Viro { 49332a7991bSAl Viro rcu_read_unlock(); 49432a7991bSAl Viro br_read_unlock(&vfsmount_lock); 49532a7991bSAl Viro } 49632a7991bSAl Viro 49731e6b01fSNick Piggin /** 49819660af7SAl Viro * unlazy_walk - try to switch to ref-walk mode. 49919660af7SAl Viro * @nd: nameidata pathwalk data 50019660af7SAl Viro * @dentry: child of nd->path.dentry or NULL 50139191628SRandy Dunlap * Returns: 0 on success, -ECHILD on failure 50231e6b01fSNick Piggin * 50319660af7SAl Viro * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry 50419660af7SAl Viro * for ref-walk mode. @dentry must be a path found by a do_lookup call on 50519660af7SAl Viro * @nd or NULL. Must be called from rcu-walk context. 50631e6b01fSNick Piggin */ 50719660af7SAl Viro static int unlazy_walk(struct nameidata *nd, struct dentry *dentry) 50831e6b01fSNick Piggin { 50931e6b01fSNick Piggin struct fs_struct *fs = current->fs; 51031e6b01fSNick Piggin struct dentry *parent = nd->path.dentry; 5115b6ca027SAl Viro int want_root = 0; 51231e6b01fSNick Piggin 51331e6b01fSNick Piggin BUG_ON(!(nd->flags & LOOKUP_RCU)); 5145b6ca027SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) { 5155b6ca027SAl Viro want_root = 1; 51631e6b01fSNick Piggin spin_lock(&fs->lock); 51731e6b01fSNick Piggin if (nd->root.mnt != fs->root.mnt || 51831e6b01fSNick Piggin nd->root.dentry != fs->root.dentry) 51931e6b01fSNick Piggin goto err_root; 52031e6b01fSNick Piggin } 52131e6b01fSNick Piggin spin_lock(&parent->d_lock); 52219660af7SAl Viro if (!dentry) { 52319660af7SAl Viro if (!__d_rcu_to_refcount(parent, nd->seq)) 52419660af7SAl Viro goto err_parent; 52519660af7SAl Viro BUG_ON(nd->inode != parent->d_inode); 52619660af7SAl Viro } else { 52794c0d4ecSAl Viro if (dentry->d_parent != parent) 52894c0d4ecSAl Viro goto err_parent; 52931e6b01fSNick Piggin spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); 53031e6b01fSNick Piggin if (!__d_rcu_to_refcount(dentry, nd->seq)) 53119660af7SAl Viro goto err_child; 53231e6b01fSNick Piggin /* 53319660af7SAl Viro * If the sequence check on the child dentry passed, then 53419660af7SAl Viro * the child has not been removed from its parent. This 53519660af7SAl Viro * means the parent dentry must be valid and able to take 53619660af7SAl Viro * a reference at this point. 53731e6b01fSNick Piggin */ 53831e6b01fSNick Piggin BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent); 53931e6b01fSNick Piggin BUG_ON(!parent->d_count); 54031e6b01fSNick Piggin parent->d_count++; 54131e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 54219660af7SAl Viro } 54331e6b01fSNick Piggin spin_unlock(&parent->d_lock); 5445b6ca027SAl Viro if (want_root) { 54531e6b01fSNick Piggin path_get(&nd->root); 54631e6b01fSNick Piggin spin_unlock(&fs->lock); 54731e6b01fSNick Piggin } 54831e6b01fSNick Piggin mntget(nd->path.mnt); 54931e6b01fSNick Piggin 55032a7991bSAl Viro unlock_rcu_walk(); 55131e6b01fSNick Piggin nd->flags &= ~LOOKUP_RCU; 55231e6b01fSNick Piggin return 0; 55319660af7SAl Viro 55419660af7SAl Viro err_child: 55531e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 55619660af7SAl Viro err_parent: 55731e6b01fSNick Piggin spin_unlock(&parent->d_lock); 55831e6b01fSNick Piggin err_root: 5595b6ca027SAl Viro if (want_root) 56031e6b01fSNick Piggin spin_unlock(&fs->lock); 56131e6b01fSNick Piggin return -ECHILD; 56231e6b01fSNick Piggin } 56331e6b01fSNick Piggin 5644ce16ef3SAl Viro static inline int d_revalidate(struct dentry *dentry, unsigned int flags) 56534286d66SNick Piggin { 5664ce16ef3SAl Viro return dentry->d_op->d_revalidate(dentry, flags); 56734286d66SNick Piggin } 56834286d66SNick Piggin 5699f1fafeeSAl Viro /** 5709f1fafeeSAl Viro * complete_walk - successful completion of path walk 5719f1fafeeSAl Viro * @nd: pointer nameidata 57239159de2SJeff Layton * 5739f1fafeeSAl Viro * If we had been in RCU mode, drop out of it and legitimize nd->path. 5749f1fafeeSAl Viro * Revalidate the final result, unless we'd already done that during 5759f1fafeeSAl Viro * the path walk or the filesystem doesn't ask for it. Return 0 on 5769f1fafeeSAl Viro * success, -error on failure. In case of failure caller does not 5779f1fafeeSAl Viro * need to drop nd->path. 57839159de2SJeff Layton */ 5799f1fafeeSAl Viro static int complete_walk(struct nameidata *nd) 58039159de2SJeff Layton { 58116c2cd71SAl Viro struct dentry *dentry = nd->path.dentry; 58239159de2SJeff Layton int status; 58339159de2SJeff Layton 5849f1fafeeSAl Viro if (nd->flags & LOOKUP_RCU) { 5859f1fafeeSAl Viro nd->flags &= ~LOOKUP_RCU; 5869f1fafeeSAl Viro if (!(nd->flags & LOOKUP_ROOT)) 5879f1fafeeSAl Viro nd->root.mnt = NULL; 5889f1fafeeSAl Viro spin_lock(&dentry->d_lock); 5899f1fafeeSAl Viro if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) { 5909f1fafeeSAl Viro spin_unlock(&dentry->d_lock); 59132a7991bSAl Viro unlock_rcu_walk(); 5929f1fafeeSAl Viro return -ECHILD; 5939f1fafeeSAl Viro } 5949f1fafeeSAl Viro BUG_ON(nd->inode != dentry->d_inode); 5959f1fafeeSAl Viro spin_unlock(&dentry->d_lock); 5969f1fafeeSAl Viro mntget(nd->path.mnt); 59732a7991bSAl Viro unlock_rcu_walk(); 5989f1fafeeSAl Viro } 5999f1fafeeSAl Viro 60016c2cd71SAl Viro if (likely(!(nd->flags & LOOKUP_JUMPED))) 60139159de2SJeff Layton return 0; 60239159de2SJeff Layton 60316c2cd71SAl Viro if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE))) 60416c2cd71SAl Viro return 0; 60516c2cd71SAl Viro 60616c2cd71SAl Viro if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT))) 60716c2cd71SAl Viro return 0; 60816c2cd71SAl Viro 60916c2cd71SAl Viro /* Note: we do not d_invalidate() */ 6104ce16ef3SAl Viro status = d_revalidate(dentry, nd->flags); 61139159de2SJeff Layton if (status > 0) 61239159de2SJeff Layton return 0; 61339159de2SJeff Layton 61416c2cd71SAl Viro if (!status) 61539159de2SJeff Layton status = -ESTALE; 61616c2cd71SAl Viro 6179f1fafeeSAl Viro path_put(&nd->path); 61839159de2SJeff Layton return status; 61939159de2SJeff Layton } 62039159de2SJeff Layton 6212a737871SAl Viro static __always_inline void set_root(struct nameidata *nd) 6222a737871SAl Viro { 623f7ad3c6bSMiklos Szeredi if (!nd->root.mnt) 624f7ad3c6bSMiklos Szeredi get_fs_root(current->fs, &nd->root); 6252a737871SAl Viro } 6262a737871SAl Viro 6276de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *); 6286de88d72SAl Viro 62931e6b01fSNick Piggin static __always_inline void set_root_rcu(struct nameidata *nd) 63031e6b01fSNick Piggin { 63131e6b01fSNick Piggin if (!nd->root.mnt) { 63231e6b01fSNick Piggin struct fs_struct *fs = current->fs; 633c28cc364SNick Piggin unsigned seq; 634c28cc364SNick Piggin 635c28cc364SNick Piggin do { 636c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 63731e6b01fSNick Piggin nd->root = fs->root; 638c1530019STim Chen nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq); 639c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 64031e6b01fSNick Piggin } 64131e6b01fSNick Piggin } 64231e6b01fSNick Piggin 643f1662356SArjan van de Ven static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link) 6441da177e4SLinus Torvalds { 64531e6b01fSNick Piggin int ret; 64631e6b01fSNick Piggin 6471da177e4SLinus Torvalds if (IS_ERR(link)) 6481da177e4SLinus Torvalds goto fail; 6491da177e4SLinus Torvalds 6501da177e4SLinus Torvalds if (*link == '/') { 6512a737871SAl Viro set_root(nd); 6521d957f9bSJan Blunck path_put(&nd->path); 6532a737871SAl Viro nd->path = nd->root; 6542a737871SAl Viro path_get(&nd->root); 65516c2cd71SAl Viro nd->flags |= LOOKUP_JUMPED; 6561da177e4SLinus Torvalds } 65731e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 658b4091d5fSChristoph Hellwig 65931e6b01fSNick Piggin ret = link_path_walk(link, nd); 66031e6b01fSNick Piggin return ret; 6611da177e4SLinus Torvalds fail: 6621d957f9bSJan Blunck path_put(&nd->path); 6631da177e4SLinus Torvalds return PTR_ERR(link); 6641da177e4SLinus Torvalds } 6651da177e4SLinus Torvalds 6661d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd) 667051d3812SIan Kent { 668051d3812SIan Kent dput(path->dentry); 6694ac91378SJan Blunck if (path->mnt != nd->path.mnt) 670051d3812SIan Kent mntput(path->mnt); 671051d3812SIan Kent } 672051d3812SIan Kent 6737b9337aaSNick Piggin static inline void path_to_nameidata(const struct path *path, 6747b9337aaSNick Piggin struct nameidata *nd) 675051d3812SIan Kent { 67631e6b01fSNick Piggin if (!(nd->flags & LOOKUP_RCU)) { 6774ac91378SJan Blunck dput(nd->path.dentry); 67831e6b01fSNick Piggin if (nd->path.mnt != path->mnt) 6794ac91378SJan Blunck mntput(nd->path.mnt); 6809a229683SHuang Shijie } 68131e6b01fSNick Piggin nd->path.mnt = path->mnt; 6824ac91378SJan Blunck nd->path.dentry = path->dentry; 683051d3812SIan Kent } 684051d3812SIan Kent 685b5fb63c1SChristoph Hellwig /* 686b5fb63c1SChristoph Hellwig * Helper to directly jump to a known parsed path from ->follow_link, 687b5fb63c1SChristoph Hellwig * caller must have taken a reference to path beforehand. 688b5fb63c1SChristoph Hellwig */ 689b5fb63c1SChristoph Hellwig void nd_jump_link(struct nameidata *nd, struct path *path) 690b5fb63c1SChristoph Hellwig { 691b5fb63c1SChristoph Hellwig path_put(&nd->path); 692b5fb63c1SChristoph Hellwig 693b5fb63c1SChristoph Hellwig nd->path = *path; 694b5fb63c1SChristoph Hellwig nd->inode = nd->path.dentry->d_inode; 695b5fb63c1SChristoph Hellwig nd->flags |= LOOKUP_JUMPED; 696b5fb63c1SChristoph Hellwig 697b5fb63c1SChristoph Hellwig BUG_ON(nd->inode->i_op->follow_link); 698b5fb63c1SChristoph Hellwig } 699b5fb63c1SChristoph Hellwig 700574197e0SAl Viro static inline void put_link(struct nameidata *nd, struct path *link, void *cookie) 701574197e0SAl Viro { 702574197e0SAl Viro struct inode *inode = link->dentry->d_inode; 7036d7b5aaeSAl Viro if (inode->i_op->put_link) 704574197e0SAl Viro inode->i_op->put_link(link->dentry, nd, cookie); 705574197e0SAl Viro path_put(link); 706574197e0SAl Viro } 707574197e0SAl Viro 708561ec64aSLinus Torvalds int sysctl_protected_symlinks __read_mostly = 0; 709561ec64aSLinus Torvalds int sysctl_protected_hardlinks __read_mostly = 0; 710800179c9SKees Cook 711800179c9SKees Cook /** 712800179c9SKees Cook * may_follow_link - Check symlink following for unsafe situations 713800179c9SKees Cook * @link: The path of the symlink 71455852635SRandy Dunlap * @nd: nameidata pathwalk data 715800179c9SKees Cook * 716800179c9SKees Cook * In the case of the sysctl_protected_symlinks sysctl being enabled, 717800179c9SKees Cook * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is 718800179c9SKees Cook * in a sticky world-writable directory. This is to protect privileged 719800179c9SKees Cook * processes from failing races against path names that may change out 720800179c9SKees Cook * from under them by way of other users creating malicious symlinks. 721800179c9SKees Cook * It will permit symlinks to be followed only when outside a sticky 722800179c9SKees Cook * world-writable directory, or when the uid of the symlink and follower 723800179c9SKees Cook * match, or when the directory owner matches the symlink's owner. 724800179c9SKees Cook * 725800179c9SKees Cook * Returns 0 if following the symlink is allowed, -ve on error. 726800179c9SKees Cook */ 727800179c9SKees Cook static inline int may_follow_link(struct path *link, struct nameidata *nd) 728800179c9SKees Cook { 729800179c9SKees Cook const struct inode *inode; 730800179c9SKees Cook const struct inode *parent; 731800179c9SKees Cook 732800179c9SKees Cook if (!sysctl_protected_symlinks) 733800179c9SKees Cook return 0; 734800179c9SKees Cook 735800179c9SKees Cook /* Allowed if owner and follower match. */ 736800179c9SKees Cook inode = link->dentry->d_inode; 73781abe27bSEric W. Biederman if (uid_eq(current_cred()->fsuid, inode->i_uid)) 738800179c9SKees Cook return 0; 739800179c9SKees Cook 740800179c9SKees Cook /* Allowed if parent directory not sticky and world-writable. */ 741800179c9SKees Cook parent = nd->path.dentry->d_inode; 742800179c9SKees Cook if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH)) 743800179c9SKees Cook return 0; 744800179c9SKees Cook 745800179c9SKees Cook /* Allowed if parent directory and link owner match. */ 74681abe27bSEric W. Biederman if (uid_eq(parent->i_uid, inode->i_uid)) 747800179c9SKees Cook return 0; 748800179c9SKees Cook 749ffd8d101SSasha Levin audit_log_link_denied("follow_link", link); 750800179c9SKees Cook path_put_conditional(link, nd); 751800179c9SKees Cook path_put(&nd->path); 752800179c9SKees Cook return -EACCES; 753800179c9SKees Cook } 754800179c9SKees Cook 755800179c9SKees Cook /** 756800179c9SKees Cook * safe_hardlink_source - Check for safe hardlink conditions 757800179c9SKees Cook * @inode: the source inode to hardlink from 758800179c9SKees Cook * 759800179c9SKees Cook * Return false if at least one of the following conditions: 760800179c9SKees Cook * - inode is not a regular file 761800179c9SKees Cook * - inode is setuid 762800179c9SKees Cook * - inode is setgid and group-exec 763800179c9SKees Cook * - access failure for read and write 764800179c9SKees Cook * 765800179c9SKees Cook * Otherwise returns true. 766800179c9SKees Cook */ 767800179c9SKees Cook static bool safe_hardlink_source(struct inode *inode) 768800179c9SKees Cook { 769800179c9SKees Cook umode_t mode = inode->i_mode; 770800179c9SKees Cook 771800179c9SKees Cook /* Special files should not get pinned to the filesystem. */ 772800179c9SKees Cook if (!S_ISREG(mode)) 773800179c9SKees Cook return false; 774800179c9SKees Cook 775800179c9SKees Cook /* Setuid files should not get pinned to the filesystem. */ 776800179c9SKees Cook if (mode & S_ISUID) 777800179c9SKees Cook return false; 778800179c9SKees Cook 779800179c9SKees Cook /* Executable setgid files should not get pinned to the filesystem. */ 780800179c9SKees Cook if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) 781800179c9SKees Cook return false; 782800179c9SKees Cook 783800179c9SKees Cook /* Hardlinking to unreadable or unwritable sources is dangerous. */ 784800179c9SKees Cook if (inode_permission(inode, MAY_READ | MAY_WRITE)) 785800179c9SKees Cook return false; 786800179c9SKees Cook 787800179c9SKees Cook return true; 788800179c9SKees Cook } 789800179c9SKees Cook 790800179c9SKees Cook /** 791800179c9SKees Cook * may_linkat - Check permissions for creating a hardlink 792800179c9SKees Cook * @link: the source to hardlink from 793800179c9SKees Cook * 794800179c9SKees Cook * Block hardlink when all of: 795800179c9SKees Cook * - sysctl_protected_hardlinks enabled 796800179c9SKees Cook * - fsuid does not match inode 797800179c9SKees Cook * - hardlink source is unsafe (see safe_hardlink_source() above) 798800179c9SKees Cook * - not CAP_FOWNER 799800179c9SKees Cook * 800800179c9SKees Cook * Returns 0 if successful, -ve on error. 801800179c9SKees Cook */ 802800179c9SKees Cook static int may_linkat(struct path *link) 803800179c9SKees Cook { 804800179c9SKees Cook const struct cred *cred; 805800179c9SKees Cook struct inode *inode; 806800179c9SKees Cook 807800179c9SKees Cook if (!sysctl_protected_hardlinks) 808800179c9SKees Cook return 0; 809800179c9SKees Cook 810800179c9SKees Cook cred = current_cred(); 811800179c9SKees Cook inode = link->dentry->d_inode; 812800179c9SKees Cook 813800179c9SKees Cook /* Source inode owner (or CAP_FOWNER) can hardlink all they like, 814800179c9SKees Cook * otherwise, it must be a safe source. 815800179c9SKees Cook */ 81681abe27bSEric W. Biederman if (uid_eq(cred->fsuid, inode->i_uid) || safe_hardlink_source(inode) || 817800179c9SKees Cook capable(CAP_FOWNER)) 818800179c9SKees Cook return 0; 819800179c9SKees Cook 820a51d9eaaSKees Cook audit_log_link_denied("linkat", link); 821800179c9SKees Cook return -EPERM; 822800179c9SKees Cook } 823800179c9SKees Cook 824def4af30SAl Viro static __always_inline int 825574197e0SAl Viro follow_link(struct path *link, struct nameidata *nd, void **p) 8261da177e4SLinus Torvalds { 8277b9337aaSNick Piggin struct dentry *dentry = link->dentry; 8286d7b5aaeSAl Viro int error; 8296d7b5aaeSAl Viro char *s; 8301da177e4SLinus Torvalds 831844a3917SAl Viro BUG_ON(nd->flags & LOOKUP_RCU); 832844a3917SAl Viro 8330e794589SAl Viro if (link->mnt == nd->path.mnt) 8340e794589SAl Viro mntget(link->mnt); 8350e794589SAl Viro 8366d7b5aaeSAl Viro error = -ELOOP; 8376d7b5aaeSAl Viro if (unlikely(current->total_link_count >= 40)) 8386d7b5aaeSAl Viro goto out_put_nd_path; 8396d7b5aaeSAl Viro 840574197e0SAl Viro cond_resched(); 841574197e0SAl Viro current->total_link_count++; 842574197e0SAl Viro 84368ac1234SAl Viro touch_atime(link); 8441da177e4SLinus Torvalds nd_set_link(nd, NULL); 845cd4e91d3SAl Viro 84636f3b4f6SAl Viro error = security_inode_follow_link(link->dentry, nd); 8476d7b5aaeSAl Viro if (error) 8486d7b5aaeSAl Viro goto out_put_nd_path; 84936f3b4f6SAl Viro 85086acdca1SAl Viro nd->last_type = LAST_BIND; 851def4af30SAl Viro *p = dentry->d_inode->i_op->follow_link(dentry, nd); 852def4af30SAl Viro error = PTR_ERR(*p); 8536d7b5aaeSAl Viro if (IS_ERR(*p)) 854408ef013SChristoph Hellwig goto out_put_nd_path; 8556d7b5aaeSAl Viro 856cc314eefSLinus Torvalds error = 0; 8576d7b5aaeSAl Viro s = nd_get_link(nd); 8586d7b5aaeSAl Viro if (s) { 8591da177e4SLinus Torvalds error = __vfs_follow_link(nd, s); 8606d7b5aaeSAl Viro if (unlikely(error)) 8616d7b5aaeSAl Viro put_link(nd, link, *p); 862b5fb63c1SChristoph Hellwig } 8636d7b5aaeSAl Viro 8646d7b5aaeSAl Viro return error; 8656d7b5aaeSAl Viro 8666d7b5aaeSAl Viro out_put_nd_path: 86798f6ef64SArnd Bergmann *p = NULL; 8686d7b5aaeSAl Viro path_put(&nd->path); 8696d7b5aaeSAl Viro path_put(link); 8701da177e4SLinus Torvalds return error; 8711da177e4SLinus Torvalds } 8721da177e4SLinus Torvalds 87331e6b01fSNick Piggin static int follow_up_rcu(struct path *path) 87431e6b01fSNick Piggin { 8750714a533SAl Viro struct mount *mnt = real_mount(path->mnt); 8760714a533SAl Viro struct mount *parent; 87731e6b01fSNick Piggin struct dentry *mountpoint; 87831e6b01fSNick Piggin 8790714a533SAl Viro parent = mnt->mnt_parent; 8800714a533SAl Viro if (&parent->mnt == path->mnt) 88131e6b01fSNick Piggin return 0; 882a73324daSAl Viro mountpoint = mnt->mnt_mountpoint; 88331e6b01fSNick Piggin path->dentry = mountpoint; 8840714a533SAl Viro path->mnt = &parent->mnt; 88531e6b01fSNick Piggin return 1; 88631e6b01fSNick Piggin } 88731e6b01fSNick Piggin 888f015f126SDavid Howells /* 889f015f126SDavid Howells * follow_up - Find the mountpoint of path's vfsmount 890f015f126SDavid Howells * 891f015f126SDavid Howells * Given a path, find the mountpoint of its source file system. 892f015f126SDavid Howells * Replace @path with the path of the mountpoint in the parent mount. 893f015f126SDavid Howells * Up is towards /. 894f015f126SDavid Howells * 895f015f126SDavid Howells * Return 1 if we went up a level and 0 if we were already at the 896f015f126SDavid Howells * root. 897f015f126SDavid Howells */ 898bab77ebfSAl Viro int follow_up(struct path *path) 8991da177e4SLinus Torvalds { 9000714a533SAl Viro struct mount *mnt = real_mount(path->mnt); 9010714a533SAl Viro struct mount *parent; 9021da177e4SLinus Torvalds struct dentry *mountpoint; 90399b7db7bSNick Piggin 904962830dfSAndi Kleen br_read_lock(&vfsmount_lock); 9050714a533SAl Viro parent = mnt->mnt_parent; 9063c0a6163SAl Viro if (parent == mnt) { 907962830dfSAndi Kleen br_read_unlock(&vfsmount_lock); 9081da177e4SLinus Torvalds return 0; 9091da177e4SLinus Torvalds } 9100714a533SAl Viro mntget(&parent->mnt); 911a73324daSAl Viro mountpoint = dget(mnt->mnt_mountpoint); 912962830dfSAndi Kleen br_read_unlock(&vfsmount_lock); 913bab77ebfSAl Viro dput(path->dentry); 914bab77ebfSAl Viro path->dentry = mountpoint; 915bab77ebfSAl Viro mntput(path->mnt); 9160714a533SAl Viro path->mnt = &parent->mnt; 9171da177e4SLinus Torvalds return 1; 9181da177e4SLinus Torvalds } 9191da177e4SLinus Torvalds 920b5c84bf6SNick Piggin /* 9219875cf80SDavid Howells * Perform an automount 9229875cf80SDavid Howells * - return -EISDIR to tell follow_managed() to stop and return the path we 9239875cf80SDavid Howells * were called with. 9241da177e4SLinus Torvalds */ 9259875cf80SDavid Howells static int follow_automount(struct path *path, unsigned flags, 9269875cf80SDavid Howells bool *need_mntput) 92731e6b01fSNick Piggin { 9289875cf80SDavid Howells struct vfsmount *mnt; 929ea5b778aSDavid Howells int err; 9309875cf80SDavid Howells 9319875cf80SDavid Howells if (!path->dentry->d_op || !path->dentry->d_op->d_automount) 9329875cf80SDavid Howells return -EREMOTE; 9339875cf80SDavid Howells 9340ec26fd0SMiklos Szeredi /* We don't want to mount if someone's just doing a stat - 9350ec26fd0SMiklos Szeredi * unless they're stat'ing a directory and appended a '/' to 9360ec26fd0SMiklos Szeredi * the name. 9370ec26fd0SMiklos Szeredi * 9380ec26fd0SMiklos Szeredi * We do, however, want to mount if someone wants to open or 9395a30d8a2SDavid Howells * create a file of any type under the mountpoint, wants to 9400ec26fd0SMiklos Szeredi * traverse through the mountpoint or wants to open the 9410ec26fd0SMiklos Szeredi * mounted directory. Also, autofs may mark negative dentries 9420ec26fd0SMiklos Szeredi * as being automount points. These will need the attentions 9430ec26fd0SMiklos Szeredi * of the daemon to instantiate them before they can be used. 9445a30d8a2SDavid Howells */ 9455a30d8a2SDavid Howells if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY | 946d94c177bSLinus Torvalds LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) && 9475a30d8a2SDavid Howells path->dentry->d_inode) 9489875cf80SDavid Howells return -EISDIR; 9490ec26fd0SMiklos Szeredi 9509875cf80SDavid Howells current->total_link_count++; 9519875cf80SDavid Howells if (current->total_link_count >= 40) 9529875cf80SDavid Howells return -ELOOP; 9539875cf80SDavid Howells 9549875cf80SDavid Howells mnt = path->dentry->d_op->d_automount(path); 9559875cf80SDavid Howells if (IS_ERR(mnt)) { 9569875cf80SDavid Howells /* 9579875cf80SDavid Howells * The filesystem is allowed to return -EISDIR here to indicate 9589875cf80SDavid Howells * it doesn't want to automount. For instance, autofs would do 9599875cf80SDavid Howells * this so that its userspace daemon can mount on this dentry. 9609875cf80SDavid Howells * 9619875cf80SDavid Howells * However, we can only permit this if it's a terminal point in 9629875cf80SDavid Howells * the path being looked up; if it wasn't then the remainder of 9639875cf80SDavid Howells * the path is inaccessible and we should say so. 9649875cf80SDavid Howells */ 96549084c3bSAl Viro if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT)) 9669875cf80SDavid Howells return -EREMOTE; 9679875cf80SDavid Howells return PTR_ERR(mnt); 96831e6b01fSNick Piggin } 969ea5b778aSDavid Howells 9709875cf80SDavid Howells if (!mnt) /* mount collision */ 9719875cf80SDavid Howells return 0; 9729875cf80SDavid Howells 9738aef1884SAl Viro if (!*need_mntput) { 9748aef1884SAl Viro /* lock_mount() may release path->mnt on error */ 9758aef1884SAl Viro mntget(path->mnt); 9768aef1884SAl Viro *need_mntput = true; 9778aef1884SAl Viro } 97819a167afSAl Viro err = finish_automount(mnt, path); 979ea5b778aSDavid Howells 980ea5b778aSDavid Howells switch (err) { 981ea5b778aSDavid Howells case -EBUSY: 982ea5b778aSDavid Howells /* Someone else made a mount here whilst we were busy */ 98319a167afSAl Viro return 0; 984ea5b778aSDavid Howells case 0: 9858aef1884SAl Viro path_put(path); 9869875cf80SDavid Howells path->mnt = mnt; 9879875cf80SDavid Howells path->dentry = dget(mnt->mnt_root); 9889875cf80SDavid Howells return 0; 98919a167afSAl Viro default: 99019a167afSAl Viro return err; 9919875cf80SDavid Howells } 99219a167afSAl Viro 993ea5b778aSDavid Howells } 9949875cf80SDavid Howells 9959875cf80SDavid Howells /* 9969875cf80SDavid Howells * Handle a dentry that is managed in some way. 997cc53ce53SDavid Howells * - Flagged for transit management (autofs) 9989875cf80SDavid Howells * - Flagged as mountpoint 9999875cf80SDavid Howells * - Flagged as automount point 10009875cf80SDavid Howells * 10019875cf80SDavid Howells * This may only be called in refwalk mode. 10029875cf80SDavid Howells * 10039875cf80SDavid Howells * Serialization is taken care of in namespace.c 10049875cf80SDavid Howells */ 10059875cf80SDavid Howells static int follow_managed(struct path *path, unsigned flags) 10069875cf80SDavid Howells { 10078aef1884SAl Viro struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */ 10089875cf80SDavid Howells unsigned managed; 10099875cf80SDavid Howells bool need_mntput = false; 10108aef1884SAl Viro int ret = 0; 10119875cf80SDavid Howells 10129875cf80SDavid Howells /* Given that we're not holding a lock here, we retain the value in a 10139875cf80SDavid Howells * local variable for each dentry as we look at it so that we don't see 10149875cf80SDavid Howells * the components of that value change under us */ 10159875cf80SDavid Howells while (managed = ACCESS_ONCE(path->dentry->d_flags), 10169875cf80SDavid Howells managed &= DCACHE_MANAGED_DENTRY, 10179875cf80SDavid Howells unlikely(managed != 0)) { 1018cc53ce53SDavid Howells /* Allow the filesystem to manage the transit without i_mutex 1019cc53ce53SDavid Howells * being held. */ 1020cc53ce53SDavid Howells if (managed & DCACHE_MANAGE_TRANSIT) { 1021cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op); 1022cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op->d_manage); 10231aed3e42SAl Viro ret = path->dentry->d_op->d_manage(path->dentry, false); 1024cc53ce53SDavid Howells if (ret < 0) 10258aef1884SAl Viro break; 1026cc53ce53SDavid Howells } 1027cc53ce53SDavid Howells 10289875cf80SDavid Howells /* Transit to a mounted filesystem. */ 10299875cf80SDavid Howells if (managed & DCACHE_MOUNTED) { 10309875cf80SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 10319875cf80SDavid Howells if (mounted) { 10329875cf80SDavid Howells dput(path->dentry); 10339875cf80SDavid Howells if (need_mntput) 1034463ffb2eSAl Viro mntput(path->mnt); 1035463ffb2eSAl Viro path->mnt = mounted; 1036463ffb2eSAl Viro path->dentry = dget(mounted->mnt_root); 10379875cf80SDavid Howells need_mntput = true; 10389875cf80SDavid Howells continue; 1039463ffb2eSAl Viro } 1040463ffb2eSAl Viro 10419875cf80SDavid Howells /* Something is mounted on this dentry in another 10429875cf80SDavid Howells * namespace and/or whatever was mounted there in this 10439875cf80SDavid Howells * namespace got unmounted before we managed to get the 10449875cf80SDavid Howells * vfsmount_lock */ 10451da177e4SLinus Torvalds } 10469875cf80SDavid Howells 10479875cf80SDavid Howells /* Handle an automount point */ 10489875cf80SDavid Howells if (managed & DCACHE_NEED_AUTOMOUNT) { 10499875cf80SDavid Howells ret = follow_automount(path, flags, &need_mntput); 10509875cf80SDavid Howells if (ret < 0) 10518aef1884SAl Viro break; 10529875cf80SDavid Howells continue; 10539875cf80SDavid Howells } 10549875cf80SDavid Howells 10559875cf80SDavid Howells /* We didn't change the current path point */ 10569875cf80SDavid Howells break; 10579875cf80SDavid Howells } 10588aef1884SAl Viro 10598aef1884SAl Viro if (need_mntput && path->mnt == mnt) 10608aef1884SAl Viro mntput(path->mnt); 10618aef1884SAl Viro if (ret == -EISDIR) 10628aef1884SAl Viro ret = 0; 1063a3fbbde7SAl Viro return ret < 0 ? ret : need_mntput; 10641da177e4SLinus Torvalds } 10651da177e4SLinus Torvalds 1066cc53ce53SDavid Howells int follow_down_one(struct path *path) 10671da177e4SLinus Torvalds { 10681da177e4SLinus Torvalds struct vfsmount *mounted; 10691da177e4SLinus Torvalds 10701c755af4SAl Viro mounted = lookup_mnt(path); 10711da177e4SLinus Torvalds if (mounted) { 10729393bd07SAl Viro dput(path->dentry); 10739393bd07SAl Viro mntput(path->mnt); 10749393bd07SAl Viro path->mnt = mounted; 10759393bd07SAl Viro path->dentry = dget(mounted->mnt_root); 10761da177e4SLinus Torvalds return 1; 10771da177e4SLinus Torvalds } 10781da177e4SLinus Torvalds return 0; 10791da177e4SLinus Torvalds } 10801da177e4SLinus Torvalds 108162a7375eSIan Kent static inline bool managed_dentry_might_block(struct dentry *dentry) 108262a7375eSIan Kent { 108362a7375eSIan Kent return (dentry->d_flags & DCACHE_MANAGE_TRANSIT && 108462a7375eSIan Kent dentry->d_op->d_manage(dentry, true) < 0); 108562a7375eSIan Kent } 108662a7375eSIan Kent 10879875cf80SDavid Howells /* 1088287548e4SAl Viro * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if 1089287548e4SAl Viro * we meet a managed dentry that would need blocking. 10909875cf80SDavid Howells */ 10919875cf80SDavid Howells static bool __follow_mount_rcu(struct nameidata *nd, struct path *path, 1092287548e4SAl Viro struct inode **inode) 10939875cf80SDavid Howells { 109462a7375eSIan Kent for (;;) { 1095c7105365SAl Viro struct mount *mounted; 109662a7375eSIan Kent /* 109762a7375eSIan Kent * Don't forget we might have a non-mountpoint managed dentry 109862a7375eSIan Kent * that wants to block transit. 109962a7375eSIan Kent */ 1100287548e4SAl Viro if (unlikely(managed_dentry_might_block(path->dentry))) 1101ab90911fSDavid Howells return false; 110262a7375eSIan Kent 110362a7375eSIan Kent if (!d_mountpoint(path->dentry)) 110462a7375eSIan Kent break; 110562a7375eSIan Kent 11069875cf80SDavid Howells mounted = __lookup_mnt(path->mnt, path->dentry, 1); 11079875cf80SDavid Howells if (!mounted) 11089875cf80SDavid Howells break; 1109c7105365SAl Viro path->mnt = &mounted->mnt; 1110c7105365SAl Viro path->dentry = mounted->mnt.mnt_root; 1111a3fbbde7SAl Viro nd->flags |= LOOKUP_JUMPED; 11129875cf80SDavid Howells nd->seq = read_seqcount_begin(&path->dentry->d_seq); 111359430262SLinus Torvalds /* 111459430262SLinus Torvalds * Update the inode too. We don't need to re-check the 111559430262SLinus Torvalds * dentry sequence number here after this d_inode read, 111659430262SLinus Torvalds * because a mount-point is always pinned. 111759430262SLinus Torvalds */ 111859430262SLinus Torvalds *inode = path->dentry->d_inode; 11199875cf80SDavid Howells } 11209875cf80SDavid Howells return true; 11219875cf80SDavid Howells } 11229875cf80SDavid Howells 1123dea39376SAl Viro static void follow_mount_rcu(struct nameidata *nd) 1124287548e4SAl Viro { 1125dea39376SAl Viro while (d_mountpoint(nd->path.dentry)) { 1126c7105365SAl Viro struct mount *mounted; 1127dea39376SAl Viro mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1); 1128287548e4SAl Viro if (!mounted) 1129287548e4SAl Viro break; 1130c7105365SAl Viro nd->path.mnt = &mounted->mnt; 1131c7105365SAl Viro nd->path.dentry = mounted->mnt.mnt_root; 1132dea39376SAl Viro nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); 1133287548e4SAl Viro } 1134287548e4SAl Viro } 1135287548e4SAl Viro 113631e6b01fSNick Piggin static int follow_dotdot_rcu(struct nameidata *nd) 113731e6b01fSNick Piggin { 113831e6b01fSNick Piggin set_root_rcu(nd); 113931e6b01fSNick Piggin 114031e6b01fSNick Piggin while (1) { 114131e6b01fSNick Piggin if (nd->path.dentry == nd->root.dentry && 114231e6b01fSNick Piggin nd->path.mnt == nd->root.mnt) { 114331e6b01fSNick Piggin break; 114431e6b01fSNick Piggin } 114531e6b01fSNick Piggin if (nd->path.dentry != nd->path.mnt->mnt_root) { 114631e6b01fSNick Piggin struct dentry *old = nd->path.dentry; 114731e6b01fSNick Piggin struct dentry *parent = old->d_parent; 114831e6b01fSNick Piggin unsigned seq; 114931e6b01fSNick Piggin 115031e6b01fSNick Piggin seq = read_seqcount_begin(&parent->d_seq); 115131e6b01fSNick Piggin if (read_seqcount_retry(&old->d_seq, nd->seq)) 1152ef7562d5SAl Viro goto failed; 115331e6b01fSNick Piggin nd->path.dentry = parent; 115431e6b01fSNick Piggin nd->seq = seq; 115531e6b01fSNick Piggin break; 115631e6b01fSNick Piggin } 115731e6b01fSNick Piggin if (!follow_up_rcu(&nd->path)) 115831e6b01fSNick Piggin break; 115931e6b01fSNick Piggin nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); 116031e6b01fSNick Piggin } 1161dea39376SAl Viro follow_mount_rcu(nd); 1162dea39376SAl Viro nd->inode = nd->path.dentry->d_inode; 116331e6b01fSNick Piggin return 0; 1164ef7562d5SAl Viro 1165ef7562d5SAl Viro failed: 1166ef7562d5SAl Viro nd->flags &= ~LOOKUP_RCU; 11675b6ca027SAl Viro if (!(nd->flags & LOOKUP_ROOT)) 1168ef7562d5SAl Viro nd->root.mnt = NULL; 116932a7991bSAl Viro unlock_rcu_walk(); 1170ef7562d5SAl Viro return -ECHILD; 117131e6b01fSNick Piggin } 117231e6b01fSNick Piggin 11739875cf80SDavid Howells /* 1174cc53ce53SDavid Howells * Follow down to the covering mount currently visible to userspace. At each 1175cc53ce53SDavid Howells * point, the filesystem owning that dentry may be queried as to whether the 1176cc53ce53SDavid Howells * caller is permitted to proceed or not. 1177cc53ce53SDavid Howells */ 11787cc90cc3SAl Viro int follow_down(struct path *path) 1179cc53ce53SDavid Howells { 1180cc53ce53SDavid Howells unsigned managed; 1181cc53ce53SDavid Howells int ret; 1182cc53ce53SDavid Howells 1183cc53ce53SDavid Howells while (managed = ACCESS_ONCE(path->dentry->d_flags), 1184cc53ce53SDavid Howells unlikely(managed & DCACHE_MANAGED_DENTRY)) { 1185cc53ce53SDavid Howells /* Allow the filesystem to manage the transit without i_mutex 1186cc53ce53SDavid Howells * being held. 1187cc53ce53SDavid Howells * 1188cc53ce53SDavid Howells * We indicate to the filesystem if someone is trying to mount 1189cc53ce53SDavid Howells * something here. This gives autofs the chance to deny anyone 1190cc53ce53SDavid Howells * other than its daemon the right to mount on its 1191cc53ce53SDavid Howells * superstructure. 1192cc53ce53SDavid Howells * 1193cc53ce53SDavid Howells * The filesystem may sleep at this point. 1194cc53ce53SDavid Howells */ 1195cc53ce53SDavid Howells if (managed & DCACHE_MANAGE_TRANSIT) { 1196cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op); 1197cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op->d_manage); 1198ab90911fSDavid Howells ret = path->dentry->d_op->d_manage( 11991aed3e42SAl Viro path->dentry, false); 1200cc53ce53SDavid Howells if (ret < 0) 1201cc53ce53SDavid Howells return ret == -EISDIR ? 0 : ret; 1202cc53ce53SDavid Howells } 1203cc53ce53SDavid Howells 1204cc53ce53SDavid Howells /* Transit to a mounted filesystem. */ 1205cc53ce53SDavid Howells if (managed & DCACHE_MOUNTED) { 1206cc53ce53SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 1207cc53ce53SDavid Howells if (!mounted) 1208cc53ce53SDavid Howells break; 1209cc53ce53SDavid Howells dput(path->dentry); 1210cc53ce53SDavid Howells mntput(path->mnt); 1211cc53ce53SDavid Howells path->mnt = mounted; 1212cc53ce53SDavid Howells path->dentry = dget(mounted->mnt_root); 1213cc53ce53SDavid Howells continue; 1214cc53ce53SDavid Howells } 1215cc53ce53SDavid Howells 1216cc53ce53SDavid Howells /* Don't handle automount points here */ 1217cc53ce53SDavid Howells break; 1218cc53ce53SDavid Howells } 1219cc53ce53SDavid Howells return 0; 1220cc53ce53SDavid Howells } 1221cc53ce53SDavid Howells 1222cc53ce53SDavid Howells /* 12239875cf80SDavid Howells * Skip to top of mountpoint pile in refwalk mode for follow_dotdot() 12249875cf80SDavid Howells */ 12259875cf80SDavid Howells static void follow_mount(struct path *path) 12269875cf80SDavid Howells { 12279875cf80SDavid Howells while (d_mountpoint(path->dentry)) { 12289875cf80SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 12299875cf80SDavid Howells if (!mounted) 12309875cf80SDavid Howells break; 12319875cf80SDavid Howells dput(path->dentry); 12329875cf80SDavid Howells mntput(path->mnt); 12339875cf80SDavid Howells path->mnt = mounted; 12349875cf80SDavid Howells path->dentry = dget(mounted->mnt_root); 12359875cf80SDavid Howells } 12369875cf80SDavid Howells } 12379875cf80SDavid Howells 123831e6b01fSNick Piggin static void follow_dotdot(struct nameidata *nd) 12391da177e4SLinus Torvalds { 12402a737871SAl Viro set_root(nd); 1241e518ddb7SAndreas Mohr 12421da177e4SLinus Torvalds while(1) { 12434ac91378SJan Blunck struct dentry *old = nd->path.dentry; 12441da177e4SLinus Torvalds 12452a737871SAl Viro if (nd->path.dentry == nd->root.dentry && 12462a737871SAl Viro nd->path.mnt == nd->root.mnt) { 12471da177e4SLinus Torvalds break; 12481da177e4SLinus Torvalds } 12494ac91378SJan Blunck if (nd->path.dentry != nd->path.mnt->mnt_root) { 12503088dd70SAl Viro /* rare case of legitimate dget_parent()... */ 12513088dd70SAl Viro nd->path.dentry = dget_parent(nd->path.dentry); 12521da177e4SLinus Torvalds dput(old); 12531da177e4SLinus Torvalds break; 12541da177e4SLinus Torvalds } 12553088dd70SAl Viro if (!follow_up(&nd->path)) 12561da177e4SLinus Torvalds break; 12571da177e4SLinus Torvalds } 125879ed0226SAl Viro follow_mount(&nd->path); 125931e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 12601da177e4SLinus Torvalds } 12611da177e4SLinus Torvalds 12621da177e4SLinus Torvalds /* 1263bad61189SMiklos Szeredi * This looks up the name in dcache, possibly revalidates the old dentry and 1264bad61189SMiklos Szeredi * allocates a new one if not found or not valid. In the need_lookup argument 1265bad61189SMiklos Szeredi * returns whether i_op->lookup is necessary. 1266bad61189SMiklos Szeredi * 1267bad61189SMiklos Szeredi * dir->d_inode->i_mutex must be held 1268baa03890SNick Piggin */ 1269bad61189SMiklos Szeredi static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir, 1270201f956eSAl Viro unsigned int flags, bool *need_lookup) 1271baa03890SNick Piggin { 1272baa03890SNick Piggin struct dentry *dentry; 1273bad61189SMiklos Szeredi int error; 1274baa03890SNick Piggin 1275bad61189SMiklos Szeredi *need_lookup = false; 1276bad61189SMiklos Szeredi dentry = d_lookup(dir, name); 1277bad61189SMiklos Szeredi if (dentry) { 127839e3c955SJeff Layton if (dentry->d_flags & DCACHE_OP_REVALIDATE) { 1279201f956eSAl Viro error = d_revalidate(dentry, flags); 1280bad61189SMiklos Szeredi if (unlikely(error <= 0)) { 1281bad61189SMiklos Szeredi if (error < 0) { 1282bad61189SMiklos Szeredi dput(dentry); 1283bad61189SMiklos Szeredi return ERR_PTR(error); 1284bad61189SMiklos Szeredi } else if (!d_invalidate(dentry)) { 1285bad61189SMiklos Szeredi dput(dentry); 1286bad61189SMiklos Szeredi dentry = NULL; 1287bad61189SMiklos Szeredi } 1288bad61189SMiklos Szeredi } 1289bad61189SMiklos Szeredi } 1290bad61189SMiklos Szeredi } 1291baa03890SNick Piggin 1292bad61189SMiklos Szeredi if (!dentry) { 1293bad61189SMiklos Szeredi dentry = d_alloc(dir, name); 1294baa03890SNick Piggin if (unlikely(!dentry)) 1295baa03890SNick Piggin return ERR_PTR(-ENOMEM); 1296baa03890SNick Piggin 1297bad61189SMiklos Szeredi *need_lookup = true; 1298baa03890SNick Piggin } 1299baa03890SNick Piggin return dentry; 1300baa03890SNick Piggin } 1301baa03890SNick Piggin 1302baa03890SNick Piggin /* 1303bad61189SMiklos Szeredi * Call i_op->lookup on the dentry. The dentry must be negative but may be 1304bad61189SMiklos Szeredi * hashed if it was pouplated with DCACHE_NEED_LOOKUP. 1305bad61189SMiklos Szeredi * 1306bad61189SMiklos Szeredi * dir->d_inode->i_mutex must be held 130744396f4bSJosef Bacik */ 1308bad61189SMiklos Szeredi static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry, 130972bd866aSAl Viro unsigned int flags) 131044396f4bSJosef Bacik { 131144396f4bSJosef Bacik struct dentry *old; 131244396f4bSJosef Bacik 131344396f4bSJosef Bacik /* Don't create child dentry for a dead directory. */ 1314bad61189SMiklos Szeredi if (unlikely(IS_DEADDIR(dir))) { 1315e188dc02SMiklos Szeredi dput(dentry); 131644396f4bSJosef Bacik return ERR_PTR(-ENOENT); 1317e188dc02SMiklos Szeredi } 131844396f4bSJosef Bacik 131972bd866aSAl Viro old = dir->i_op->lookup(dir, dentry, flags); 132044396f4bSJosef Bacik if (unlikely(old)) { 132144396f4bSJosef Bacik dput(dentry); 132244396f4bSJosef Bacik dentry = old; 132344396f4bSJosef Bacik } 132444396f4bSJosef Bacik return dentry; 132544396f4bSJosef Bacik } 132644396f4bSJosef Bacik 1327a3255546SAl Viro static struct dentry *__lookup_hash(struct qstr *name, 132872bd866aSAl Viro struct dentry *base, unsigned int flags) 1329a3255546SAl Viro { 1330bad61189SMiklos Szeredi bool need_lookup; 1331a3255546SAl Viro struct dentry *dentry; 1332a3255546SAl Viro 133372bd866aSAl Viro dentry = lookup_dcache(name, base, flags, &need_lookup); 1334bad61189SMiklos Szeredi if (!need_lookup) 1335a3255546SAl Viro return dentry; 1336bad61189SMiklos Szeredi 133772bd866aSAl Viro return lookup_real(base->d_inode, dentry, flags); 1338a3255546SAl Viro } 1339a3255546SAl Viro 134044396f4bSJosef Bacik /* 13411da177e4SLinus Torvalds * It's more convoluted than I'd like it to be, but... it's still fairly 13421da177e4SLinus Torvalds * small and for now I'd prefer to have fast path as straight as possible. 13431da177e4SLinus Torvalds * It _is_ time-critical. 13441da177e4SLinus Torvalds */ 1345697f514dSMiklos Szeredi static int lookup_fast(struct nameidata *nd, struct qstr *name, 134631e6b01fSNick Piggin struct path *path, struct inode **inode) 13471da177e4SLinus Torvalds { 13484ac91378SJan Blunck struct vfsmount *mnt = nd->path.mnt; 134931e6b01fSNick Piggin struct dentry *dentry, *parent = nd->path.dentry; 13505a18fff2SAl Viro int need_reval = 1; 13515a18fff2SAl Viro int status = 1; 13529875cf80SDavid Howells int err; 13539875cf80SDavid Howells 13543cac260aSAl Viro /* 1355b04f784eSNick Piggin * Rename seqlock is not required here because in the off chance 1356b04f784eSNick Piggin * of a false negative due to a concurrent rename, we're going to 1357b04f784eSNick Piggin * do the non-racy lookup, below. 1358b04f784eSNick Piggin */ 135931e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) { 136031e6b01fSNick Piggin unsigned seq; 136112f8ad4bSLinus Torvalds dentry = __d_lookup_rcu(parent, name, &seq, nd->inode); 13625a18fff2SAl Viro if (!dentry) 13635a18fff2SAl Viro goto unlazy; 13645a18fff2SAl Viro 136512f8ad4bSLinus Torvalds /* 136612f8ad4bSLinus Torvalds * This sequence count validates that the inode matches 136712f8ad4bSLinus Torvalds * the dentry name information from lookup. 136812f8ad4bSLinus Torvalds */ 136912f8ad4bSLinus Torvalds *inode = dentry->d_inode; 137012f8ad4bSLinus Torvalds if (read_seqcount_retry(&dentry->d_seq, seq)) 137112f8ad4bSLinus Torvalds return -ECHILD; 137212f8ad4bSLinus Torvalds 137312f8ad4bSLinus Torvalds /* 137412f8ad4bSLinus Torvalds * This sequence count validates that the parent had no 137512f8ad4bSLinus Torvalds * changes while we did the lookup of the dentry above. 137612f8ad4bSLinus Torvalds * 137712f8ad4bSLinus Torvalds * The memory barrier in read_seqcount_begin of child is 137812f8ad4bSLinus Torvalds * enough, we can use __read_seqcount_retry here. 137912f8ad4bSLinus Torvalds */ 138031e6b01fSNick Piggin if (__read_seqcount_retry(&parent->d_seq, nd->seq)) 138131e6b01fSNick Piggin return -ECHILD; 138231e6b01fSNick Piggin nd->seq = seq; 13835a18fff2SAl Viro 138424643087SAl Viro if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) { 13854ce16ef3SAl Viro status = d_revalidate(dentry, nd->flags); 13865a18fff2SAl Viro if (unlikely(status <= 0)) { 13875a18fff2SAl Viro if (status != -ECHILD) 13885a18fff2SAl Viro need_reval = 0; 13895a18fff2SAl Viro goto unlazy; 13905a18fff2SAl Viro } 139124643087SAl Viro } 139231e6b01fSNick Piggin path->mnt = mnt; 139331e6b01fSNick Piggin path->dentry = dentry; 1394d6e9bd25SAl Viro if (unlikely(!__follow_mount_rcu(nd, path, inode))) 1395d6e9bd25SAl Viro goto unlazy; 1396d6e9bd25SAl Viro if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT)) 1397d6e9bd25SAl Viro goto unlazy; 13989875cf80SDavid Howells return 0; 13995a18fff2SAl Viro unlazy: 140019660af7SAl Viro if (unlazy_walk(nd, dentry)) 14015a18fff2SAl Viro return -ECHILD; 14025a18fff2SAl Viro } else { 140331e6b01fSNick Piggin dentry = __d_lookup(parent, name); 140424643087SAl Viro } 14055a18fff2SAl Viro 140681e6f520SAl Viro if (unlikely(!dentry)) 140781e6f520SAl Viro goto need_lookup; 14085a18fff2SAl Viro 14095a18fff2SAl Viro if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval) 14104ce16ef3SAl Viro status = d_revalidate(dentry, nd->flags); 14115a18fff2SAl Viro if (unlikely(status <= 0)) { 14125a18fff2SAl Viro if (status < 0) { 14135a18fff2SAl Viro dput(dentry); 14145a18fff2SAl Viro return status; 14155a18fff2SAl Viro } 14165a18fff2SAl Viro if (!d_invalidate(dentry)) { 14175a18fff2SAl Viro dput(dentry); 141881e6f520SAl Viro goto need_lookup; 14195a18fff2SAl Viro } 14205a18fff2SAl Viro } 1421697f514dSMiklos Szeredi 14221da177e4SLinus Torvalds path->mnt = mnt; 14231da177e4SLinus Torvalds path->dentry = dentry; 14249875cf80SDavid Howells err = follow_managed(path, nd->flags); 142589312214SIan Kent if (unlikely(err < 0)) { 142689312214SIan Kent path_put_conditional(path, nd); 14279875cf80SDavid Howells return err; 142889312214SIan Kent } 1429a3fbbde7SAl Viro if (err) 1430a3fbbde7SAl Viro nd->flags |= LOOKUP_JUMPED; 143131e6b01fSNick Piggin *inode = path->dentry->d_inode; 14321da177e4SLinus Torvalds return 0; 143381e6f520SAl Viro 143481e6f520SAl Viro need_lookup: 1435697f514dSMiklos Szeredi return 1; 1436697f514dSMiklos Szeredi } 1437697f514dSMiklos Szeredi 1438697f514dSMiklos Szeredi /* Fast lookup failed, do it the slow way */ 1439697f514dSMiklos Szeredi static int lookup_slow(struct nameidata *nd, struct qstr *name, 1440697f514dSMiklos Szeredi struct path *path) 1441697f514dSMiklos Szeredi { 1442697f514dSMiklos Szeredi struct dentry *dentry, *parent; 1443697f514dSMiklos Szeredi int err; 1444697f514dSMiklos Szeredi 1445697f514dSMiklos Szeredi parent = nd->path.dentry; 144681e6f520SAl Viro BUG_ON(nd->inode != parent->d_inode); 144781e6f520SAl Viro 144881e6f520SAl Viro mutex_lock(&parent->d_inode->i_mutex); 144972bd866aSAl Viro dentry = __lookup_hash(name, parent, nd->flags); 145081e6f520SAl Viro mutex_unlock(&parent->d_inode->i_mutex); 145181e6f520SAl Viro if (IS_ERR(dentry)) 145281e6f520SAl Viro return PTR_ERR(dentry); 1453697f514dSMiklos Szeredi path->mnt = nd->path.mnt; 1454697f514dSMiklos Szeredi path->dentry = dentry; 1455697f514dSMiklos Szeredi err = follow_managed(path, nd->flags); 1456697f514dSMiklos Szeredi if (unlikely(err < 0)) { 1457697f514dSMiklos Szeredi path_put_conditional(path, nd); 1458697f514dSMiklos Szeredi return err; 1459697f514dSMiklos Szeredi } 1460697f514dSMiklos Szeredi if (err) 1461697f514dSMiklos Szeredi nd->flags |= LOOKUP_JUMPED; 1462697f514dSMiklos Szeredi return 0; 14631da177e4SLinus Torvalds } 14641da177e4SLinus Torvalds 146552094c8aSAl Viro static inline int may_lookup(struct nameidata *nd) 146652094c8aSAl Viro { 146752094c8aSAl Viro if (nd->flags & LOOKUP_RCU) { 14684ad5abb3SAl Viro int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK); 146952094c8aSAl Viro if (err != -ECHILD) 147052094c8aSAl Viro return err; 147119660af7SAl Viro if (unlazy_walk(nd, NULL)) 147252094c8aSAl Viro return -ECHILD; 147352094c8aSAl Viro } 14744ad5abb3SAl Viro return inode_permission(nd->inode, MAY_EXEC); 147552094c8aSAl Viro } 147652094c8aSAl Viro 14779856fa1bSAl Viro static inline int handle_dots(struct nameidata *nd, int type) 14789856fa1bSAl Viro { 14799856fa1bSAl Viro if (type == LAST_DOTDOT) { 14809856fa1bSAl Viro if (nd->flags & LOOKUP_RCU) { 14819856fa1bSAl Viro if (follow_dotdot_rcu(nd)) 14829856fa1bSAl Viro return -ECHILD; 14839856fa1bSAl Viro } else 14849856fa1bSAl Viro follow_dotdot(nd); 14859856fa1bSAl Viro } 14869856fa1bSAl Viro return 0; 14879856fa1bSAl Viro } 14889856fa1bSAl Viro 1489951361f9SAl Viro static void terminate_walk(struct nameidata *nd) 1490951361f9SAl Viro { 1491951361f9SAl Viro if (!(nd->flags & LOOKUP_RCU)) { 1492951361f9SAl Viro path_put(&nd->path); 1493951361f9SAl Viro } else { 1494951361f9SAl Viro nd->flags &= ~LOOKUP_RCU; 14955b6ca027SAl Viro if (!(nd->flags & LOOKUP_ROOT)) 1496951361f9SAl Viro nd->root.mnt = NULL; 149732a7991bSAl Viro unlock_rcu_walk(); 1498951361f9SAl Viro } 1499951361f9SAl Viro } 1500951361f9SAl Viro 15013ddcd056SLinus Torvalds /* 15023ddcd056SLinus Torvalds * Do we need to follow links? We _really_ want to be able 15033ddcd056SLinus Torvalds * to do this check without having to look at inode->i_op, 15043ddcd056SLinus Torvalds * so we keep a cache of "no, this doesn't need follow_link" 15053ddcd056SLinus Torvalds * for the common case. 15063ddcd056SLinus Torvalds */ 15077813b94aSLinus Torvalds static inline int should_follow_link(struct inode *inode, int follow) 15083ddcd056SLinus Torvalds { 15093ddcd056SLinus Torvalds if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) { 15103ddcd056SLinus Torvalds if (likely(inode->i_op->follow_link)) 15113ddcd056SLinus Torvalds return follow; 15123ddcd056SLinus Torvalds 15133ddcd056SLinus Torvalds /* This gets set once for the inode lifetime */ 15143ddcd056SLinus Torvalds spin_lock(&inode->i_lock); 15153ddcd056SLinus Torvalds inode->i_opflags |= IOP_NOFOLLOW; 15163ddcd056SLinus Torvalds spin_unlock(&inode->i_lock); 15173ddcd056SLinus Torvalds } 15183ddcd056SLinus Torvalds return 0; 15193ddcd056SLinus Torvalds } 15203ddcd056SLinus Torvalds 1521ce57dfc1SAl Viro static inline int walk_component(struct nameidata *nd, struct path *path, 152221b9b073SAl Viro int follow) 1523ce57dfc1SAl Viro { 1524ce57dfc1SAl Viro struct inode *inode; 1525ce57dfc1SAl Viro int err; 1526ce57dfc1SAl Viro /* 1527ce57dfc1SAl Viro * "." and ".." are special - ".." especially so because it has 1528ce57dfc1SAl Viro * to be able to know about the current root directory and 1529ce57dfc1SAl Viro * parent relationships. 1530ce57dfc1SAl Viro */ 153121b9b073SAl Viro if (unlikely(nd->last_type != LAST_NORM)) 153221b9b073SAl Viro return handle_dots(nd, nd->last_type); 153321b9b073SAl Viro err = lookup_fast(nd, &nd->last, path, &inode); 1534ce57dfc1SAl Viro if (unlikely(err)) { 1535697f514dSMiklos Szeredi if (err < 0) 1536697f514dSMiklos Szeredi goto out_err; 1537697f514dSMiklos Szeredi 153821b9b073SAl Viro err = lookup_slow(nd, &nd->last, path); 1539697f514dSMiklos Szeredi if (err < 0) 1540697f514dSMiklos Szeredi goto out_err; 1541697f514dSMiklos Szeredi 1542697f514dSMiklos Szeredi inode = path->dentry->d_inode; 1543ce57dfc1SAl Viro } 1544697f514dSMiklos Szeredi err = -ENOENT; 1545697f514dSMiklos Szeredi if (!inode) 1546697f514dSMiklos Szeredi goto out_path_put; 1547697f514dSMiklos Szeredi 15487813b94aSLinus Torvalds if (should_follow_link(inode, follow)) { 154919660af7SAl Viro if (nd->flags & LOOKUP_RCU) { 155019660af7SAl Viro if (unlikely(unlazy_walk(nd, path->dentry))) { 1551697f514dSMiklos Szeredi err = -ECHILD; 1552697f514dSMiklos Szeredi goto out_err; 155319660af7SAl Viro } 155419660af7SAl Viro } 1555ce57dfc1SAl Viro BUG_ON(inode != path->dentry->d_inode); 1556ce57dfc1SAl Viro return 1; 1557ce57dfc1SAl Viro } 1558ce57dfc1SAl Viro path_to_nameidata(path, nd); 1559ce57dfc1SAl Viro nd->inode = inode; 1560ce57dfc1SAl Viro return 0; 1561697f514dSMiklos Szeredi 1562697f514dSMiklos Szeredi out_path_put: 1563697f514dSMiklos Szeredi path_to_nameidata(path, nd); 1564697f514dSMiklos Szeredi out_err: 1565697f514dSMiklos Szeredi terminate_walk(nd); 1566697f514dSMiklos Szeredi return err; 1567ce57dfc1SAl Viro } 1568ce57dfc1SAl Viro 15691da177e4SLinus Torvalds /* 1570b356379aSAl Viro * This limits recursive symlink follows to 8, while 1571b356379aSAl Viro * limiting consecutive symlinks to 40. 1572b356379aSAl Viro * 1573b356379aSAl Viro * Without that kind of total limit, nasty chains of consecutive 1574b356379aSAl Viro * symlinks can cause almost arbitrarily long lookups. 1575b356379aSAl Viro */ 1576b356379aSAl Viro static inline int nested_symlink(struct path *path, struct nameidata *nd) 1577b356379aSAl Viro { 1578b356379aSAl Viro int res; 1579b356379aSAl Viro 1580b356379aSAl Viro if (unlikely(current->link_count >= MAX_NESTED_LINKS)) { 1581b356379aSAl Viro path_put_conditional(path, nd); 1582b356379aSAl Viro path_put(&nd->path); 1583b356379aSAl Viro return -ELOOP; 1584b356379aSAl Viro } 15851a4022f8SErez Zadok BUG_ON(nd->depth >= MAX_NESTED_LINKS); 1586b356379aSAl Viro 1587b356379aSAl Viro nd->depth++; 1588b356379aSAl Viro current->link_count++; 1589b356379aSAl Viro 1590b356379aSAl Viro do { 1591b356379aSAl Viro struct path link = *path; 1592b356379aSAl Viro void *cookie; 1593574197e0SAl Viro 1594574197e0SAl Viro res = follow_link(&link, nd, &cookie); 15956d7b5aaeSAl Viro if (res) 15966d7b5aaeSAl Viro break; 159721b9b073SAl Viro res = walk_component(nd, path, LOOKUP_FOLLOW); 1598574197e0SAl Viro put_link(nd, &link, cookie); 1599b356379aSAl Viro } while (res > 0); 1600b356379aSAl Viro 1601b356379aSAl Viro current->link_count--; 1602b356379aSAl Viro nd->depth--; 1603b356379aSAl Viro return res; 1604b356379aSAl Viro } 1605b356379aSAl Viro 1606b356379aSAl Viro /* 16073ddcd056SLinus Torvalds * We really don't want to look at inode->i_op->lookup 16083ddcd056SLinus Torvalds * when we don't have to. So we keep a cache bit in 16093ddcd056SLinus Torvalds * the inode ->i_opflags field that says "yes, we can 16103ddcd056SLinus Torvalds * do lookup on this inode". 16113ddcd056SLinus Torvalds */ 16123ddcd056SLinus Torvalds static inline int can_lookup(struct inode *inode) 16133ddcd056SLinus Torvalds { 16143ddcd056SLinus Torvalds if (likely(inode->i_opflags & IOP_LOOKUP)) 16153ddcd056SLinus Torvalds return 1; 16163ddcd056SLinus Torvalds if (likely(!inode->i_op->lookup)) 16173ddcd056SLinus Torvalds return 0; 16183ddcd056SLinus Torvalds 16193ddcd056SLinus Torvalds /* We do this once for the lifetime of the inode */ 16203ddcd056SLinus Torvalds spin_lock(&inode->i_lock); 16213ddcd056SLinus Torvalds inode->i_opflags |= IOP_LOOKUP; 16223ddcd056SLinus Torvalds spin_unlock(&inode->i_lock); 16233ddcd056SLinus Torvalds return 1; 16243ddcd056SLinus Torvalds } 16253ddcd056SLinus Torvalds 1626bfcfaa77SLinus Torvalds /* 1627bfcfaa77SLinus Torvalds * We can do the critical dentry name comparison and hashing 1628bfcfaa77SLinus Torvalds * operations one word at a time, but we are limited to: 1629bfcfaa77SLinus Torvalds * 1630bfcfaa77SLinus Torvalds * - Architectures with fast unaligned word accesses. We could 1631bfcfaa77SLinus Torvalds * do a "get_unaligned()" if this helps and is sufficiently 1632bfcfaa77SLinus Torvalds * fast. 1633bfcfaa77SLinus Torvalds * 1634bfcfaa77SLinus Torvalds * - Little-endian machines (so that we can generate the mask 1635bfcfaa77SLinus Torvalds * of low bytes efficiently). Again, we *could* do a byte 1636bfcfaa77SLinus Torvalds * swapping load on big-endian architectures if that is not 1637bfcfaa77SLinus Torvalds * expensive enough to make the optimization worthless. 1638bfcfaa77SLinus Torvalds * 1639bfcfaa77SLinus Torvalds * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we 1640bfcfaa77SLinus Torvalds * do not trap on the (extremely unlikely) case of a page 1641bfcfaa77SLinus Torvalds * crossing operation. 1642bfcfaa77SLinus Torvalds * 1643bfcfaa77SLinus Torvalds * - Furthermore, we need an efficient 64-bit compile for the 1644bfcfaa77SLinus Torvalds * 64-bit case in order to generate the "number of bytes in 1645bfcfaa77SLinus Torvalds * the final mask". Again, that could be replaced with a 1646bfcfaa77SLinus Torvalds * efficient population count instruction or similar. 1647bfcfaa77SLinus Torvalds */ 1648bfcfaa77SLinus Torvalds #ifdef CONFIG_DCACHE_WORD_ACCESS 1649bfcfaa77SLinus Torvalds 1650f68e556eSLinus Torvalds #include <asm/word-at-a-time.h> 1651bfcfaa77SLinus Torvalds 1652f68e556eSLinus Torvalds #ifdef CONFIG_64BIT 1653bfcfaa77SLinus Torvalds 1654bfcfaa77SLinus Torvalds static inline unsigned int fold_hash(unsigned long hash) 1655bfcfaa77SLinus Torvalds { 1656bfcfaa77SLinus Torvalds hash += hash >> (8*sizeof(int)); 1657bfcfaa77SLinus Torvalds return hash; 1658bfcfaa77SLinus Torvalds } 1659bfcfaa77SLinus Torvalds 1660bfcfaa77SLinus Torvalds #else /* 32-bit case */ 1661bfcfaa77SLinus Torvalds 1662bfcfaa77SLinus Torvalds #define fold_hash(x) (x) 1663bfcfaa77SLinus Torvalds 1664bfcfaa77SLinus Torvalds #endif 1665bfcfaa77SLinus Torvalds 1666bfcfaa77SLinus Torvalds unsigned int full_name_hash(const unsigned char *name, unsigned int len) 1667bfcfaa77SLinus Torvalds { 1668bfcfaa77SLinus Torvalds unsigned long a, mask; 1669bfcfaa77SLinus Torvalds unsigned long hash = 0; 1670bfcfaa77SLinus Torvalds 1671bfcfaa77SLinus Torvalds for (;;) { 1672e419b4ccSLinus Torvalds a = load_unaligned_zeropad(name); 1673bfcfaa77SLinus Torvalds if (len < sizeof(unsigned long)) 1674bfcfaa77SLinus Torvalds break; 1675bfcfaa77SLinus Torvalds hash += a; 1676f132c5beSAl Viro hash *= 9; 1677bfcfaa77SLinus Torvalds name += sizeof(unsigned long); 1678bfcfaa77SLinus Torvalds len -= sizeof(unsigned long); 1679bfcfaa77SLinus Torvalds if (!len) 1680bfcfaa77SLinus Torvalds goto done; 1681bfcfaa77SLinus Torvalds } 1682bfcfaa77SLinus Torvalds mask = ~(~0ul << len*8); 1683bfcfaa77SLinus Torvalds hash += mask & a; 1684bfcfaa77SLinus Torvalds done: 1685bfcfaa77SLinus Torvalds return fold_hash(hash); 1686bfcfaa77SLinus Torvalds } 1687bfcfaa77SLinus Torvalds EXPORT_SYMBOL(full_name_hash); 1688bfcfaa77SLinus Torvalds 1689bfcfaa77SLinus Torvalds /* 1690bfcfaa77SLinus Torvalds * Calculate the length and hash of the path component, and 1691bfcfaa77SLinus Torvalds * return the length of the component; 1692bfcfaa77SLinus Torvalds */ 1693bfcfaa77SLinus Torvalds static inline unsigned long hash_name(const char *name, unsigned int *hashp) 1694bfcfaa77SLinus Torvalds { 169536126f8fSLinus Torvalds unsigned long a, b, adata, bdata, mask, hash, len; 169636126f8fSLinus Torvalds const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS; 1697bfcfaa77SLinus Torvalds 1698bfcfaa77SLinus Torvalds hash = a = 0; 1699bfcfaa77SLinus Torvalds len = -sizeof(unsigned long); 1700bfcfaa77SLinus Torvalds do { 1701bfcfaa77SLinus Torvalds hash = (hash + a) * 9; 1702bfcfaa77SLinus Torvalds len += sizeof(unsigned long); 1703e419b4ccSLinus Torvalds a = load_unaligned_zeropad(name+len); 170436126f8fSLinus Torvalds b = a ^ REPEAT_BYTE('/'); 170536126f8fSLinus Torvalds } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants))); 1706bfcfaa77SLinus Torvalds 170736126f8fSLinus Torvalds adata = prep_zero_mask(a, adata, &constants); 170836126f8fSLinus Torvalds bdata = prep_zero_mask(b, bdata, &constants); 170936126f8fSLinus Torvalds 171036126f8fSLinus Torvalds mask = create_zero_mask(adata | bdata); 171136126f8fSLinus Torvalds 171236126f8fSLinus Torvalds hash += a & zero_bytemask(mask); 1713bfcfaa77SLinus Torvalds *hashp = fold_hash(hash); 1714bfcfaa77SLinus Torvalds 171536126f8fSLinus Torvalds return len + find_zero(mask); 1716bfcfaa77SLinus Torvalds } 1717bfcfaa77SLinus Torvalds 1718bfcfaa77SLinus Torvalds #else 1719bfcfaa77SLinus Torvalds 17200145acc2SLinus Torvalds unsigned int full_name_hash(const unsigned char *name, unsigned int len) 17210145acc2SLinus Torvalds { 17220145acc2SLinus Torvalds unsigned long hash = init_name_hash(); 17230145acc2SLinus Torvalds while (len--) 17240145acc2SLinus Torvalds hash = partial_name_hash(*name++, hash); 17250145acc2SLinus Torvalds return end_name_hash(hash); 17260145acc2SLinus Torvalds } 1727ae942ae7SLinus Torvalds EXPORT_SYMBOL(full_name_hash); 17280145acc2SLinus Torvalds 17293ddcd056SLinus Torvalds /* 1730200e9ef7SLinus Torvalds * We know there's a real path component here of at least 1731200e9ef7SLinus Torvalds * one character. 1732200e9ef7SLinus Torvalds */ 1733200e9ef7SLinus Torvalds static inline unsigned long hash_name(const char *name, unsigned int *hashp) 1734200e9ef7SLinus Torvalds { 1735200e9ef7SLinus Torvalds unsigned long hash = init_name_hash(); 1736200e9ef7SLinus Torvalds unsigned long len = 0, c; 1737200e9ef7SLinus Torvalds 1738200e9ef7SLinus Torvalds c = (unsigned char)*name; 1739200e9ef7SLinus Torvalds do { 1740200e9ef7SLinus Torvalds len++; 1741200e9ef7SLinus Torvalds hash = partial_name_hash(c, hash); 1742200e9ef7SLinus Torvalds c = (unsigned char)name[len]; 1743200e9ef7SLinus Torvalds } while (c && c != '/'); 1744200e9ef7SLinus Torvalds *hashp = end_name_hash(hash); 1745200e9ef7SLinus Torvalds return len; 1746200e9ef7SLinus Torvalds } 1747200e9ef7SLinus Torvalds 1748bfcfaa77SLinus Torvalds #endif 1749bfcfaa77SLinus Torvalds 1750200e9ef7SLinus Torvalds /* 17511da177e4SLinus Torvalds * Name resolution. 1752ea3834d9SPrasanna Meda * This is the basic name resolution function, turning a pathname into 1753ea3834d9SPrasanna Meda * the final dentry. We expect 'base' to be positive and a directory. 17541da177e4SLinus Torvalds * 1755ea3834d9SPrasanna Meda * Returns 0 and nd will have valid dentry and mnt on success. 1756ea3834d9SPrasanna Meda * Returns error and drops reference to input namei data on failure. 17571da177e4SLinus Torvalds */ 17586de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd) 17591da177e4SLinus Torvalds { 17601da177e4SLinus Torvalds struct path next; 17611da177e4SLinus Torvalds int err; 17621da177e4SLinus Torvalds 17631da177e4SLinus Torvalds while (*name=='/') 17641da177e4SLinus Torvalds name++; 17651da177e4SLinus Torvalds if (!*name) 1766086e183aSAl Viro return 0; 17671da177e4SLinus Torvalds 17681da177e4SLinus Torvalds /* At this point we know we have a real path component. */ 17691da177e4SLinus Torvalds for(;;) { 17701da177e4SLinus Torvalds struct qstr this; 1771200e9ef7SLinus Torvalds long len; 1772fe479a58SAl Viro int type; 17731da177e4SLinus Torvalds 177452094c8aSAl Viro err = may_lookup(nd); 17751da177e4SLinus Torvalds if (err) 17761da177e4SLinus Torvalds break; 17771da177e4SLinus Torvalds 1778200e9ef7SLinus Torvalds len = hash_name(name, &this.hash); 17791da177e4SLinus Torvalds this.name = name; 1780200e9ef7SLinus Torvalds this.len = len; 17811da177e4SLinus Torvalds 1782fe479a58SAl Viro type = LAST_NORM; 1783200e9ef7SLinus Torvalds if (name[0] == '.') switch (len) { 1784fe479a58SAl Viro case 2: 1785200e9ef7SLinus Torvalds if (name[1] == '.') { 1786fe479a58SAl Viro type = LAST_DOTDOT; 178716c2cd71SAl Viro nd->flags |= LOOKUP_JUMPED; 178816c2cd71SAl Viro } 1789fe479a58SAl Viro break; 1790fe479a58SAl Viro case 1: 1791fe479a58SAl Viro type = LAST_DOT; 1792fe479a58SAl Viro } 17935a202bcdSAl Viro if (likely(type == LAST_NORM)) { 17945a202bcdSAl Viro struct dentry *parent = nd->path.dentry; 179516c2cd71SAl Viro nd->flags &= ~LOOKUP_JUMPED; 17965a202bcdSAl Viro if (unlikely(parent->d_flags & DCACHE_OP_HASH)) { 17975a202bcdSAl Viro err = parent->d_op->d_hash(parent, nd->inode, 17985a202bcdSAl Viro &this); 17995a202bcdSAl Viro if (err < 0) 18005a202bcdSAl Viro break; 18015a202bcdSAl Viro } 18025a202bcdSAl Viro } 1803fe479a58SAl Viro 18045f4a6a69SAl Viro nd->last = this; 18055f4a6a69SAl Viro nd->last_type = type; 18065f4a6a69SAl Viro 1807200e9ef7SLinus Torvalds if (!name[len]) 18085f4a6a69SAl Viro return 0; 1809200e9ef7SLinus Torvalds /* 1810200e9ef7SLinus Torvalds * If it wasn't NUL, we know it was '/'. Skip that 1811200e9ef7SLinus Torvalds * slash, and continue until no more slashes. 1812200e9ef7SLinus Torvalds */ 1813200e9ef7SLinus Torvalds do { 1814200e9ef7SLinus Torvalds len++; 1815200e9ef7SLinus Torvalds } while (unlikely(name[len] == '/')); 1816200e9ef7SLinus Torvalds if (!name[len]) 18175f4a6a69SAl Viro return 0; 18185f4a6a69SAl Viro 1819200e9ef7SLinus Torvalds name += len; 18201da177e4SLinus Torvalds 182121b9b073SAl Viro err = walk_component(nd, &next, LOOKUP_FOLLOW); 1822ce57dfc1SAl Viro if (err < 0) 1823ce57dfc1SAl Viro return err; 1824fe479a58SAl Viro 1825ce57dfc1SAl Viro if (err) { 1826b356379aSAl Viro err = nested_symlink(&next, nd); 18271da177e4SLinus Torvalds if (err) 1828a7472babSAl Viro return err; 182931e6b01fSNick Piggin } 18305f4a6a69SAl Viro if (!can_lookup(nd->inode)) { 18313ddcd056SLinus Torvalds err = -ENOTDIR; 18323ddcd056SLinus Torvalds break; 18335f4a6a69SAl Viro } 1834ce57dfc1SAl Viro } 1835951361f9SAl Viro terminate_walk(nd); 18361da177e4SLinus Torvalds return err; 18371da177e4SLinus Torvalds } 18381da177e4SLinus Torvalds 183970e9b357SAl Viro static int path_init(int dfd, const char *name, unsigned int flags, 184070e9b357SAl Viro struct nameidata *nd, struct file **fp) 184131e6b01fSNick Piggin { 184231e6b01fSNick Piggin int retval = 0; 184331e6b01fSNick Piggin 184431e6b01fSNick Piggin nd->last_type = LAST_ROOT; /* if there are only slashes... */ 184516c2cd71SAl Viro nd->flags = flags | LOOKUP_JUMPED; 184631e6b01fSNick Piggin nd->depth = 0; 18475b6ca027SAl Viro if (flags & LOOKUP_ROOT) { 18485b6ca027SAl Viro struct inode *inode = nd->root.dentry->d_inode; 184973d049a4SAl Viro if (*name) { 1850741b7c3fSAl Viro if (!can_lookup(inode)) 18515b6ca027SAl Viro return -ENOTDIR; 18525b6ca027SAl Viro retval = inode_permission(inode, MAY_EXEC); 18535b6ca027SAl Viro if (retval) 18545b6ca027SAl Viro return retval; 185573d049a4SAl Viro } 18565b6ca027SAl Viro nd->path = nd->root; 18575b6ca027SAl Viro nd->inode = inode; 18585b6ca027SAl Viro if (flags & LOOKUP_RCU) { 185932a7991bSAl Viro lock_rcu_walk(); 18605b6ca027SAl Viro nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 18615b6ca027SAl Viro } else { 18625b6ca027SAl Viro path_get(&nd->path); 18635b6ca027SAl Viro } 18645b6ca027SAl Viro return 0; 18655b6ca027SAl Viro } 18665b6ca027SAl Viro 186731e6b01fSNick Piggin nd->root.mnt = NULL; 186831e6b01fSNick Piggin 186931e6b01fSNick Piggin if (*name=='/') { 1870e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 187132a7991bSAl Viro lock_rcu_walk(); 1872e41f7d4eSAl Viro set_root_rcu(nd); 1873e41f7d4eSAl Viro } else { 1874e41f7d4eSAl Viro set_root(nd); 1875e41f7d4eSAl Viro path_get(&nd->root); 1876e41f7d4eSAl Viro } 187731e6b01fSNick Piggin nd->path = nd->root; 187831e6b01fSNick Piggin } else if (dfd == AT_FDCWD) { 1879e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 188031e6b01fSNick Piggin struct fs_struct *fs = current->fs; 1881c28cc364SNick Piggin unsigned seq; 188231e6b01fSNick Piggin 188332a7991bSAl Viro lock_rcu_walk(); 188431e6b01fSNick Piggin 1885c28cc364SNick Piggin do { 1886c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 188731e6b01fSNick Piggin nd->path = fs->pwd; 1888c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 1889c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 1890e41f7d4eSAl Viro } else { 1891e41f7d4eSAl Viro get_fs_pwd(current->fs, &nd->path); 1892e41f7d4eSAl Viro } 189331e6b01fSNick Piggin } else { 1894582aa64aSJeff Layton /* Caller must check execute permissions on the starting path component */ 18952903ff01SAl Viro struct fd f = fdget_raw(dfd); 189631e6b01fSNick Piggin struct dentry *dentry; 189731e6b01fSNick Piggin 18982903ff01SAl Viro if (!f.file) 18992903ff01SAl Viro return -EBADF; 190031e6b01fSNick Piggin 19012903ff01SAl Viro dentry = f.file->f_path.dentry; 190231e6b01fSNick Piggin 1903f52e0c11SAl Viro if (*name) { 1904741b7c3fSAl Viro if (!can_lookup(dentry->d_inode)) { 19052903ff01SAl Viro fdput(f); 19062903ff01SAl Viro return -ENOTDIR; 1907f52e0c11SAl Viro } 19082903ff01SAl Viro } 19092903ff01SAl Viro 19102903ff01SAl Viro nd->path = f.file->f_path; 1911e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 19122903ff01SAl Viro if (f.need_put) 19132903ff01SAl Viro *fp = f.file; 1914c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 191532a7991bSAl Viro lock_rcu_walk(); 19165590ff0dSUlrich Drepper } else { 19172903ff01SAl Viro path_get(&nd->path); 19182903ff01SAl Viro fdput(f); 19191da177e4SLinus Torvalds } 1920e41f7d4eSAl Viro } 1921e41f7d4eSAl Viro 192231e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 19239b4a9b14SAl Viro return 0; 19249b4a9b14SAl Viro } 19259b4a9b14SAl Viro 1926bd92d7feSAl Viro static inline int lookup_last(struct nameidata *nd, struct path *path) 1927bd92d7feSAl Viro { 1928bd92d7feSAl Viro if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len]) 1929bd92d7feSAl Viro nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 1930bd92d7feSAl Viro 1931bd92d7feSAl Viro nd->flags &= ~LOOKUP_PARENT; 193221b9b073SAl Viro return walk_component(nd, path, nd->flags & LOOKUP_FOLLOW); 1933bd92d7feSAl Viro } 1934bd92d7feSAl Viro 19359b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */ 1936ee0827cdSAl Viro static int path_lookupat(int dfd, const char *name, 19379b4a9b14SAl Viro unsigned int flags, struct nameidata *nd) 19389b4a9b14SAl Viro { 193970e9b357SAl Viro struct file *base = NULL; 1940bd92d7feSAl Viro struct path path; 1941bd92d7feSAl Viro int err; 194231e6b01fSNick Piggin 194331e6b01fSNick Piggin /* 194431e6b01fSNick Piggin * Path walking is largely split up into 2 different synchronisation 194531e6b01fSNick Piggin * schemes, rcu-walk and ref-walk (explained in 194631e6b01fSNick Piggin * Documentation/filesystems/path-lookup.txt). These share much of the 194731e6b01fSNick Piggin * path walk code, but some things particularly setup, cleanup, and 194831e6b01fSNick Piggin * following mounts are sufficiently divergent that functions are 194931e6b01fSNick Piggin * duplicated. Typically there is a function foo(), and its RCU 195031e6b01fSNick Piggin * analogue, foo_rcu(). 195131e6b01fSNick Piggin * 195231e6b01fSNick Piggin * -ECHILD is the error number of choice (just to avoid clashes) that 195331e6b01fSNick Piggin * is returned if some aspect of an rcu-walk fails. Such an error must 195431e6b01fSNick Piggin * be handled by restarting a traditional ref-walk (which will always 195531e6b01fSNick Piggin * be able to complete). 195631e6b01fSNick Piggin */ 1957bd92d7feSAl Viro err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base); 1958ee0827cdSAl Viro 1959bd92d7feSAl Viro if (unlikely(err)) 1960bd92d7feSAl Viro return err; 1961ee0827cdSAl Viro 1962ee0827cdSAl Viro current->total_link_count = 0; 1963bd92d7feSAl Viro err = link_path_walk(name, nd); 1964bd92d7feSAl Viro 1965bd92d7feSAl Viro if (!err && !(flags & LOOKUP_PARENT)) { 1966bd92d7feSAl Viro err = lookup_last(nd, &path); 1967bd92d7feSAl Viro while (err > 0) { 1968bd92d7feSAl Viro void *cookie; 1969bd92d7feSAl Viro struct path link = path; 1970800179c9SKees Cook err = may_follow_link(&link, nd); 1971800179c9SKees Cook if (unlikely(err)) 1972800179c9SKees Cook break; 1973bd92d7feSAl Viro nd->flags |= LOOKUP_PARENT; 1974574197e0SAl Viro err = follow_link(&link, nd, &cookie); 19756d7b5aaeSAl Viro if (err) 19766d7b5aaeSAl Viro break; 1977bd92d7feSAl Viro err = lookup_last(nd, &path); 1978574197e0SAl Viro put_link(nd, &link, cookie); 1979bd92d7feSAl Viro } 1980bd92d7feSAl Viro } 1981ee0827cdSAl Viro 19829f1fafeeSAl Viro if (!err) 19839f1fafeeSAl Viro err = complete_walk(nd); 1984bd92d7feSAl Viro 1985bd92d7feSAl Viro if (!err && nd->flags & LOOKUP_DIRECTORY) { 1986bd92d7feSAl Viro if (!nd->inode->i_op->lookup) { 1987bd92d7feSAl Viro path_put(&nd->path); 1988bd23a539SAl Viro err = -ENOTDIR; 1989bd92d7feSAl Viro } 1990bd92d7feSAl Viro } 199116c2cd71SAl Viro 199270e9b357SAl Viro if (base) 199370e9b357SAl Viro fput(base); 1994ee0827cdSAl Viro 19955b6ca027SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) { 199631e6b01fSNick Piggin path_put(&nd->root); 199731e6b01fSNick Piggin nd->root.mnt = NULL; 199831e6b01fSNick Piggin } 1999bd92d7feSAl Viro return err; 200031e6b01fSNick Piggin } 200131e6b01fSNick Piggin 2002873f1eedSJeff Layton static int filename_lookup(int dfd, struct filename *name, 2003873f1eedSJeff Layton unsigned int flags, struct nameidata *nd) 2004873f1eedSJeff Layton { 2005873f1eedSJeff Layton int retval = path_lookupat(dfd, name->name, flags | LOOKUP_RCU, nd); 2006873f1eedSJeff Layton if (unlikely(retval == -ECHILD)) 2007873f1eedSJeff Layton retval = path_lookupat(dfd, name->name, flags, nd); 2008873f1eedSJeff Layton if (unlikely(retval == -ESTALE)) 2009873f1eedSJeff Layton retval = path_lookupat(dfd, name->name, 2010873f1eedSJeff Layton flags | LOOKUP_REVAL, nd); 2011873f1eedSJeff Layton 2012873f1eedSJeff Layton if (likely(!retval)) 2013adb5c247SJeff Layton audit_inode(name, nd->path.dentry, flags & LOOKUP_PARENT); 2014873f1eedSJeff Layton return retval; 2015873f1eedSJeff Layton } 2016873f1eedSJeff Layton 2017ee0827cdSAl Viro static int do_path_lookup(int dfd, const char *name, 2018ee0827cdSAl Viro unsigned int flags, struct nameidata *nd) 2019ee0827cdSAl Viro { 2020873f1eedSJeff Layton struct filename filename = { .name = name }; 2021ee0827cdSAl Viro 2022873f1eedSJeff Layton return filename_lookup(dfd, &filename, flags, nd); 20231da177e4SLinus Torvalds } 20241da177e4SLinus Torvalds 202579714f72SAl Viro /* does lookup, returns the object with parent locked */ 202679714f72SAl Viro struct dentry *kern_path_locked(const char *name, struct path *path) 20275590ff0dSUlrich Drepper { 202879714f72SAl Viro struct nameidata nd; 202979714f72SAl Viro struct dentry *d; 203079714f72SAl Viro int err = do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, &nd); 203179714f72SAl Viro if (err) 203279714f72SAl Viro return ERR_PTR(err); 203379714f72SAl Viro if (nd.last_type != LAST_NORM) { 203479714f72SAl Viro path_put(&nd.path); 203579714f72SAl Viro return ERR_PTR(-EINVAL); 203679714f72SAl Viro } 203779714f72SAl Viro mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 20381e0ea001SAl Viro d = __lookup_hash(&nd.last, nd.path.dentry, 0); 203979714f72SAl Viro if (IS_ERR(d)) { 204079714f72SAl Viro mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 204179714f72SAl Viro path_put(&nd.path); 204279714f72SAl Viro return d; 204379714f72SAl Viro } 204479714f72SAl Viro *path = nd.path; 204579714f72SAl Viro return d; 20465590ff0dSUlrich Drepper } 20475590ff0dSUlrich Drepper 2048d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path) 2049d1811465SAl Viro { 2050d1811465SAl Viro struct nameidata nd; 2051d1811465SAl Viro int res = do_path_lookup(AT_FDCWD, name, flags, &nd); 2052d1811465SAl Viro if (!res) 2053d1811465SAl Viro *path = nd.path; 2054d1811465SAl Viro return res; 2055d1811465SAl Viro } 2056d1811465SAl Viro 205716f18200SJosef 'Jeff' Sipek /** 205816f18200SJosef 'Jeff' Sipek * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair 205916f18200SJosef 'Jeff' Sipek * @dentry: pointer to dentry of the base directory 206016f18200SJosef 'Jeff' Sipek * @mnt: pointer to vfs mount of the base directory 206116f18200SJosef 'Jeff' Sipek * @name: pointer to file name 206216f18200SJosef 'Jeff' Sipek * @flags: lookup flags 2063e0a01249SAl Viro * @path: pointer to struct path to fill 206416f18200SJosef 'Jeff' Sipek */ 206516f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt, 206616f18200SJosef 'Jeff' Sipek const char *name, unsigned int flags, 2067e0a01249SAl Viro struct path *path) 206816f18200SJosef 'Jeff' Sipek { 2069e0a01249SAl Viro struct nameidata nd; 2070e0a01249SAl Viro int err; 2071e0a01249SAl Viro nd.root.dentry = dentry; 2072e0a01249SAl Viro nd.root.mnt = mnt; 2073e0a01249SAl Viro BUG_ON(flags & LOOKUP_PARENT); 20745b6ca027SAl Viro /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */ 2075e0a01249SAl Viro err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd); 2076e0a01249SAl Viro if (!err) 2077e0a01249SAl Viro *path = nd.path; 2078e0a01249SAl Viro return err; 207916f18200SJosef 'Jeff' Sipek } 208016f18200SJosef 'Jeff' Sipek 2081057f6c01SJames Morris /* 2082057f6c01SJames Morris * Restricted form of lookup. Doesn't follow links, single-component only, 2083057f6c01SJames Morris * needs parent already locked. Doesn't follow mounts. 2084057f6c01SJames Morris * SMP-safe. 2085057f6c01SJames Morris */ 2086a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd) 20871da177e4SLinus Torvalds { 208872bd866aSAl Viro return __lookup_hash(&nd->last, nd->path.dentry, nd->flags); 20891da177e4SLinus Torvalds } 20901da177e4SLinus Torvalds 2091eead1911SChristoph Hellwig /** 2092a6b91919SRandy Dunlap * lookup_one_len - filesystem helper to lookup single pathname component 2093eead1911SChristoph Hellwig * @name: pathname component to lookup 2094eead1911SChristoph Hellwig * @base: base directory to lookup from 2095eead1911SChristoph Hellwig * @len: maximum length @len should be interpreted to 2096eead1911SChristoph Hellwig * 2097a6b91919SRandy Dunlap * Note that this routine is purely a helper for filesystem usage and should 2098a6b91919SRandy Dunlap * not be called by generic code. Also note that by using this function the 2099eead1911SChristoph Hellwig * nameidata argument is passed to the filesystem methods and a filesystem 2100eead1911SChristoph Hellwig * using this helper needs to be prepared for that. 2101eead1911SChristoph Hellwig */ 2102057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len) 2103057f6c01SJames Morris { 2104057f6c01SJames Morris struct qstr this; 21056a96ba54SAl Viro unsigned int c; 2106cda309deSMiklos Szeredi int err; 2107057f6c01SJames Morris 21082f9092e1SDavid Woodhouse WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex)); 21092f9092e1SDavid Woodhouse 21106a96ba54SAl Viro this.name = name; 21116a96ba54SAl Viro this.len = len; 21120145acc2SLinus Torvalds this.hash = full_name_hash(name, len); 21136a96ba54SAl Viro if (!len) 21146a96ba54SAl Viro return ERR_PTR(-EACCES); 21156a96ba54SAl Viro 211621d8a15aSAl Viro if (unlikely(name[0] == '.')) { 211721d8a15aSAl Viro if (len < 2 || (len == 2 && name[1] == '.')) 211821d8a15aSAl Viro return ERR_PTR(-EACCES); 211921d8a15aSAl Viro } 212021d8a15aSAl Viro 21216a96ba54SAl Viro while (len--) { 21226a96ba54SAl Viro c = *(const unsigned char *)name++; 21236a96ba54SAl Viro if (c == '/' || c == '\0') 21246a96ba54SAl Viro return ERR_PTR(-EACCES); 21256a96ba54SAl Viro } 21265a202bcdSAl Viro /* 21275a202bcdSAl Viro * See if the low-level filesystem might want 21285a202bcdSAl Viro * to use its own hash.. 21295a202bcdSAl Viro */ 21305a202bcdSAl Viro if (base->d_flags & DCACHE_OP_HASH) { 21315a202bcdSAl Viro int err = base->d_op->d_hash(base, base->d_inode, &this); 21325a202bcdSAl Viro if (err < 0) 21335a202bcdSAl Viro return ERR_PTR(err); 21345a202bcdSAl Viro } 2135eead1911SChristoph Hellwig 2136cda309deSMiklos Szeredi err = inode_permission(base->d_inode, MAY_EXEC); 2137cda309deSMiklos Szeredi if (err) 2138cda309deSMiklos Szeredi return ERR_PTR(err); 2139cda309deSMiklos Szeredi 214072bd866aSAl Viro return __lookup_hash(&this, base, 0); 2141057f6c01SJames Morris } 2142057f6c01SJames Morris 21431fa1e7f6SAndy Whitcroft int user_path_at_empty(int dfd, const char __user *name, unsigned flags, 21441fa1e7f6SAndy Whitcroft struct path *path, int *empty) 21451da177e4SLinus Torvalds { 21462d8f3038SAl Viro struct nameidata nd; 214791a27b2aSJeff Layton struct filename *tmp = getname_flags(name, flags, empty); 21481da177e4SLinus Torvalds int err = PTR_ERR(tmp); 21491da177e4SLinus Torvalds if (!IS_ERR(tmp)) { 21502d8f3038SAl Viro 21512d8f3038SAl Viro BUG_ON(flags & LOOKUP_PARENT); 21522d8f3038SAl Viro 2153873f1eedSJeff Layton err = filename_lookup(dfd, tmp, flags, &nd); 21541da177e4SLinus Torvalds putname(tmp); 21552d8f3038SAl Viro if (!err) 21562d8f3038SAl Viro *path = nd.path; 21571da177e4SLinus Torvalds } 21581da177e4SLinus Torvalds return err; 21591da177e4SLinus Torvalds } 21601da177e4SLinus Torvalds 21611fa1e7f6SAndy Whitcroft int user_path_at(int dfd, const char __user *name, unsigned flags, 21621fa1e7f6SAndy Whitcroft struct path *path) 21631fa1e7f6SAndy Whitcroft { 2164f7493e5dSLinus Torvalds return user_path_at_empty(dfd, name, flags, path, NULL); 21651fa1e7f6SAndy Whitcroft } 21661fa1e7f6SAndy Whitcroft 2167873f1eedSJeff Layton /* 2168873f1eedSJeff Layton * NB: most callers don't do anything directly with the reference to the 2169873f1eedSJeff Layton * to struct filename, but the nd->last pointer points into the name string 2170873f1eedSJeff Layton * allocated by getname. So we must hold the reference to it until all 2171873f1eedSJeff Layton * path-walking is complete. 2172873f1eedSJeff Layton */ 217391a27b2aSJeff Layton static struct filename * 21749e790bd6SJeff Layton user_path_parent(int dfd, const char __user *path, struct nameidata *nd, 21759e790bd6SJeff Layton unsigned int flags) 21762ad94ae6SAl Viro { 217791a27b2aSJeff Layton struct filename *s = getname(path); 21782ad94ae6SAl Viro int error; 21792ad94ae6SAl Viro 21809e790bd6SJeff Layton /* only LOOKUP_REVAL is allowed in extra flags */ 21819e790bd6SJeff Layton flags &= LOOKUP_REVAL; 21829e790bd6SJeff Layton 21832ad94ae6SAl Viro if (IS_ERR(s)) 218491a27b2aSJeff Layton return s; 21852ad94ae6SAl Viro 21869e790bd6SJeff Layton error = filename_lookup(dfd, s, flags | LOOKUP_PARENT, nd); 218791a27b2aSJeff Layton if (error) { 21882ad94ae6SAl Viro putname(s); 218991a27b2aSJeff Layton return ERR_PTR(error); 219091a27b2aSJeff Layton } 21912ad94ae6SAl Viro 219291a27b2aSJeff Layton return s; 21932ad94ae6SAl Viro } 21942ad94ae6SAl Viro 21951da177e4SLinus Torvalds /* 21961da177e4SLinus Torvalds * It's inline, so penalty for filesystems that don't use sticky bit is 21971da177e4SLinus Torvalds * minimal. 21981da177e4SLinus Torvalds */ 21991da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode) 22001da177e4SLinus Torvalds { 22018e96e3b7SEric W. Biederman kuid_t fsuid = current_fsuid(); 2202da9592edSDavid Howells 22031da177e4SLinus Torvalds if (!(dir->i_mode & S_ISVTX)) 22041da177e4SLinus Torvalds return 0; 22058e96e3b7SEric W. Biederman if (uid_eq(inode->i_uid, fsuid)) 22061da177e4SLinus Torvalds return 0; 22078e96e3b7SEric W. Biederman if (uid_eq(dir->i_uid, fsuid)) 22081da177e4SLinus Torvalds return 0; 22091a48e2acSEric W. Biederman return !inode_capable(inode, CAP_FOWNER); 22101da177e4SLinus Torvalds } 22111da177e4SLinus Torvalds 22121da177e4SLinus Torvalds /* 22131da177e4SLinus Torvalds * Check whether we can remove a link victim from directory dir, check 22141da177e4SLinus Torvalds * whether the type of victim is right. 22151da177e4SLinus Torvalds * 1. We can't do it if dir is read-only (done in permission()) 22161da177e4SLinus Torvalds * 2. We should have write and exec permissions on dir 22171da177e4SLinus Torvalds * 3. We can't remove anything from append-only dir 22181da177e4SLinus Torvalds * 4. We can't do anything with immutable dir (done in permission()) 22191da177e4SLinus Torvalds * 5. If the sticky bit on dir is set we should either 22201da177e4SLinus Torvalds * a. be owner of dir, or 22211da177e4SLinus Torvalds * b. be owner of victim, or 22221da177e4SLinus Torvalds * c. have CAP_FOWNER capability 22231da177e4SLinus Torvalds * 6. If the victim is append-only or immutable we can't do antyhing with 22241da177e4SLinus Torvalds * links pointing to it. 22251da177e4SLinus Torvalds * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. 22261da177e4SLinus Torvalds * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. 22271da177e4SLinus Torvalds * 9. We can't remove a root or mountpoint. 22281da177e4SLinus Torvalds * 10. We don't allow removal of NFS sillyrenamed files; it's handled by 22291da177e4SLinus Torvalds * nfs_async_unlink(). 22301da177e4SLinus Torvalds */ 2231858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir) 22321da177e4SLinus Torvalds { 22331da177e4SLinus Torvalds int error; 22341da177e4SLinus Torvalds 22351da177e4SLinus Torvalds if (!victim->d_inode) 22361da177e4SLinus Torvalds return -ENOENT; 22371da177e4SLinus Torvalds 22381da177e4SLinus Torvalds BUG_ON(victim->d_parent->d_inode != dir); 22394fa6b5ecSJeff Layton audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE); 22401da177e4SLinus Torvalds 2241f419a2e3SAl Viro error = inode_permission(dir, MAY_WRITE | MAY_EXEC); 22421da177e4SLinus Torvalds if (error) 22431da177e4SLinus Torvalds return error; 22441da177e4SLinus Torvalds if (IS_APPEND(dir)) 22451da177e4SLinus Torvalds return -EPERM; 22461da177e4SLinus Torvalds if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)|| 2247f9454548SHugh Dickins IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode)) 22481da177e4SLinus Torvalds return -EPERM; 22491da177e4SLinus Torvalds if (isdir) { 22501da177e4SLinus Torvalds if (!S_ISDIR(victim->d_inode->i_mode)) 22511da177e4SLinus Torvalds return -ENOTDIR; 22521da177e4SLinus Torvalds if (IS_ROOT(victim)) 22531da177e4SLinus Torvalds return -EBUSY; 22541da177e4SLinus Torvalds } else if (S_ISDIR(victim->d_inode->i_mode)) 22551da177e4SLinus Torvalds return -EISDIR; 22561da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 22571da177e4SLinus Torvalds return -ENOENT; 22581da177e4SLinus Torvalds if (victim->d_flags & DCACHE_NFSFS_RENAMED) 22591da177e4SLinus Torvalds return -EBUSY; 22601da177e4SLinus Torvalds return 0; 22611da177e4SLinus Torvalds } 22621da177e4SLinus Torvalds 22631da177e4SLinus Torvalds /* Check whether we can create an object with dentry child in directory 22641da177e4SLinus Torvalds * dir. 22651da177e4SLinus Torvalds * 1. We can't do it if child already exists (open has special treatment for 22661da177e4SLinus Torvalds * this case, but since we are inlined it's OK) 22671da177e4SLinus Torvalds * 2. We can't do it if dir is read-only (done in permission()) 22681da177e4SLinus Torvalds * 3. We should have write and exec permissions on dir 22691da177e4SLinus Torvalds * 4. We can't do it if dir is immutable (done in permission()) 22701da177e4SLinus Torvalds */ 2271a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child) 22721da177e4SLinus Torvalds { 22731da177e4SLinus Torvalds if (child->d_inode) 22741da177e4SLinus Torvalds return -EEXIST; 22751da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 22761da177e4SLinus Torvalds return -ENOENT; 2277f419a2e3SAl Viro return inode_permission(dir, MAY_WRITE | MAY_EXEC); 22781da177e4SLinus Torvalds } 22791da177e4SLinus Torvalds 22801da177e4SLinus Torvalds /* 22811da177e4SLinus Torvalds * p1 and p2 should be directories on the same fs. 22821da177e4SLinus Torvalds */ 22831da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2) 22841da177e4SLinus Torvalds { 22851da177e4SLinus Torvalds struct dentry *p; 22861da177e4SLinus Torvalds 22871da177e4SLinus Torvalds if (p1 == p2) { 2288f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 22891da177e4SLinus Torvalds return NULL; 22901da177e4SLinus Torvalds } 22911da177e4SLinus Torvalds 2292a11f3a05SArjan van de Ven mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 22931da177e4SLinus Torvalds 2294e2761a11SOGAWA Hirofumi p = d_ancestor(p2, p1); 2295e2761a11SOGAWA Hirofumi if (p) { 2296f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT); 2297f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD); 22981da177e4SLinus Torvalds return p; 22991da177e4SLinus Torvalds } 23001da177e4SLinus Torvalds 2301e2761a11SOGAWA Hirofumi p = d_ancestor(p1, p2); 2302e2761a11SOGAWA Hirofumi if (p) { 2303f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 2304f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 23051da177e4SLinus Torvalds return p; 23061da177e4SLinus Torvalds } 23071da177e4SLinus Torvalds 2308f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 2309f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 23101da177e4SLinus Torvalds return NULL; 23111da177e4SLinus Torvalds } 23121da177e4SLinus Torvalds 23131da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2) 23141da177e4SLinus Torvalds { 23151b1dcc1bSJes Sorensen mutex_unlock(&p1->d_inode->i_mutex); 23161da177e4SLinus Torvalds if (p1 != p2) { 23171b1dcc1bSJes Sorensen mutex_unlock(&p2->d_inode->i_mutex); 2318a11f3a05SArjan van de Ven mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 23191da177e4SLinus Torvalds } 23201da177e4SLinus Torvalds } 23211da177e4SLinus Torvalds 23224acdaf27SAl Viro int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode, 2323312b63fbSAl Viro bool want_excl) 23241da177e4SLinus Torvalds { 2325a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 23261da177e4SLinus Torvalds if (error) 23271da177e4SLinus Torvalds return error; 23281da177e4SLinus Torvalds 2329acfa4380SAl Viro if (!dir->i_op->create) 23301da177e4SLinus Torvalds return -EACCES; /* shouldn't it be ENOSYS? */ 23311da177e4SLinus Torvalds mode &= S_IALLUGO; 23321da177e4SLinus Torvalds mode |= S_IFREG; 23331da177e4SLinus Torvalds error = security_inode_create(dir, dentry, mode); 23341da177e4SLinus Torvalds if (error) 23351da177e4SLinus Torvalds return error; 2336312b63fbSAl Viro error = dir->i_op->create(dir, dentry, mode, want_excl); 2337a74574aaSStephen Smalley if (!error) 2338f38aa942SAmy Griffis fsnotify_create(dir, dentry); 23391da177e4SLinus Torvalds return error; 23401da177e4SLinus Torvalds } 23411da177e4SLinus Torvalds 234273d049a4SAl Viro static int may_open(struct path *path, int acc_mode, int flag) 23431da177e4SLinus Torvalds { 23443fb64190SChristoph Hellwig struct dentry *dentry = path->dentry; 23451da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 23461da177e4SLinus Torvalds int error; 23471da177e4SLinus Torvalds 2348bcda7652SAl Viro /* O_PATH? */ 2349bcda7652SAl Viro if (!acc_mode) 2350bcda7652SAl Viro return 0; 2351bcda7652SAl Viro 23521da177e4SLinus Torvalds if (!inode) 23531da177e4SLinus Torvalds return -ENOENT; 23541da177e4SLinus Torvalds 2355c8fe8f30SChristoph Hellwig switch (inode->i_mode & S_IFMT) { 2356c8fe8f30SChristoph Hellwig case S_IFLNK: 23571da177e4SLinus Torvalds return -ELOOP; 2358c8fe8f30SChristoph Hellwig case S_IFDIR: 2359c8fe8f30SChristoph Hellwig if (acc_mode & MAY_WRITE) 23601da177e4SLinus Torvalds return -EISDIR; 2361c8fe8f30SChristoph Hellwig break; 2362c8fe8f30SChristoph Hellwig case S_IFBLK: 2363c8fe8f30SChristoph Hellwig case S_IFCHR: 23643fb64190SChristoph Hellwig if (path->mnt->mnt_flags & MNT_NODEV) 23651da177e4SLinus Torvalds return -EACCES; 2366c8fe8f30SChristoph Hellwig /*FALLTHRU*/ 2367c8fe8f30SChristoph Hellwig case S_IFIFO: 2368c8fe8f30SChristoph Hellwig case S_IFSOCK: 23691da177e4SLinus Torvalds flag &= ~O_TRUNC; 2370c8fe8f30SChristoph Hellwig break; 23714a3fd211SDave Hansen } 2372b41572e9SDave Hansen 23733fb64190SChristoph Hellwig error = inode_permission(inode, acc_mode); 2374b41572e9SDave Hansen if (error) 2375b41572e9SDave Hansen return error; 23766146f0d5SMimi Zohar 23771da177e4SLinus Torvalds /* 23781da177e4SLinus Torvalds * An append-only file must be opened in append mode for writing. 23791da177e4SLinus Torvalds */ 23801da177e4SLinus Torvalds if (IS_APPEND(inode)) { 23818737c930SAl Viro if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND)) 23827715b521SAl Viro return -EPERM; 23831da177e4SLinus Torvalds if (flag & O_TRUNC) 23847715b521SAl Viro return -EPERM; 23851da177e4SLinus Torvalds } 23861da177e4SLinus Torvalds 23871da177e4SLinus Torvalds /* O_NOATIME can only be set by the owner or superuser */ 23882e149670SSerge E. Hallyn if (flag & O_NOATIME && !inode_owner_or_capable(inode)) 23897715b521SAl Viro return -EPERM; 23901da177e4SLinus Torvalds 2391f3c7691eSJ. Bruce Fields return 0; 23927715b521SAl Viro } 23937715b521SAl Viro 2394e1181ee6SJeff Layton static int handle_truncate(struct file *filp) 23957715b521SAl Viro { 2396e1181ee6SJeff Layton struct path *path = &filp->f_path; 23977715b521SAl Viro struct inode *inode = path->dentry->d_inode; 23987715b521SAl Viro int error = get_write_access(inode); 23991da177e4SLinus Torvalds if (error) 24007715b521SAl Viro return error; 24011da177e4SLinus Torvalds /* 24021da177e4SLinus Torvalds * Refuse to truncate files with mandatory locks held on them. 24031da177e4SLinus Torvalds */ 24041da177e4SLinus Torvalds error = locks_verify_locked(inode); 2405be6d3e56SKentaro Takeda if (!error) 2406ea0d3ab2STetsuo Handa error = security_path_truncate(path); 24071da177e4SLinus Torvalds if (!error) { 24087715b521SAl Viro error = do_truncate(path->dentry, 0, 2409d139d7ffSMiklos Szeredi ATTR_MTIME|ATTR_CTIME|ATTR_OPEN, 2410e1181ee6SJeff Layton filp); 24111da177e4SLinus Torvalds } 24121da177e4SLinus Torvalds put_write_access(inode); 2413acd0c935SMimi Zohar return error; 24141da177e4SLinus Torvalds } 24151da177e4SLinus Torvalds 2416d57999e1SDave Hansen static inline int open_to_namei_flags(int flag) 2417d57999e1SDave Hansen { 24188a5e929dSAl Viro if ((flag & O_ACCMODE) == 3) 24198a5e929dSAl Viro flag--; 2420d57999e1SDave Hansen return flag; 2421d57999e1SDave Hansen } 2422d57999e1SDave Hansen 2423d18e9008SMiklos Szeredi static int may_o_create(struct path *dir, struct dentry *dentry, umode_t mode) 2424d18e9008SMiklos Szeredi { 2425d18e9008SMiklos Szeredi int error = security_path_mknod(dir, dentry, mode, 0); 2426d18e9008SMiklos Szeredi if (error) 2427d18e9008SMiklos Szeredi return error; 2428d18e9008SMiklos Szeredi 2429d18e9008SMiklos Szeredi error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC); 2430d18e9008SMiklos Szeredi if (error) 2431d18e9008SMiklos Szeredi return error; 2432d18e9008SMiklos Szeredi 2433d18e9008SMiklos Szeredi return security_inode_create(dir->dentry->d_inode, dentry, mode); 2434d18e9008SMiklos Szeredi } 2435d18e9008SMiklos Szeredi 24361acf0af9SDavid Howells /* 24371acf0af9SDavid Howells * Attempt to atomically look up, create and open a file from a negative 24381acf0af9SDavid Howells * dentry. 24391acf0af9SDavid Howells * 24401acf0af9SDavid Howells * Returns 0 if successful. The file will have been created and attached to 24411acf0af9SDavid Howells * @file by the filesystem calling finish_open(). 24421acf0af9SDavid Howells * 24431acf0af9SDavid Howells * Returns 1 if the file was looked up only or didn't need creating. The 24441acf0af9SDavid Howells * caller will need to perform the open themselves. @path will have been 24451acf0af9SDavid Howells * updated to point to the new dentry. This may be negative. 24461acf0af9SDavid Howells * 24471acf0af9SDavid Howells * Returns an error code otherwise. 24481acf0af9SDavid Howells */ 24492675a4ebSAl Viro static int atomic_open(struct nameidata *nd, struct dentry *dentry, 245030d90494SAl Viro struct path *path, struct file *file, 2451015c3bbcSMiklos Szeredi const struct open_flags *op, 245264894cf8SAl Viro bool got_write, bool need_lookup, 245347237687SAl Viro int *opened) 2454d18e9008SMiklos Szeredi { 2455d18e9008SMiklos Szeredi struct inode *dir = nd->path.dentry->d_inode; 2456d18e9008SMiklos Szeredi unsigned open_flag = open_to_namei_flags(op->open_flag); 2457d18e9008SMiklos Szeredi umode_t mode; 2458d18e9008SMiklos Szeredi int error; 2459d18e9008SMiklos Szeredi int acc_mode; 2460d18e9008SMiklos Szeredi int create_error = 0; 2461d18e9008SMiklos Szeredi struct dentry *const DENTRY_NOT_SET = (void *) -1UL; 2462d18e9008SMiklos Szeredi 2463d18e9008SMiklos Szeredi BUG_ON(dentry->d_inode); 2464d18e9008SMiklos Szeredi 2465d18e9008SMiklos Szeredi /* Don't create child dentry for a dead directory. */ 2466d18e9008SMiklos Szeredi if (unlikely(IS_DEADDIR(dir))) { 24672675a4ebSAl Viro error = -ENOENT; 2468d18e9008SMiklos Szeredi goto out; 2469d18e9008SMiklos Szeredi } 2470d18e9008SMiklos Szeredi 247162b259d8SMiklos Szeredi mode = op->mode; 2472d18e9008SMiklos Szeredi if ((open_flag & O_CREAT) && !IS_POSIXACL(dir)) 2473d18e9008SMiklos Szeredi mode &= ~current_umask(); 2474d18e9008SMiklos Szeredi 2475f8310c59SAl Viro if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT)) { 2476d18e9008SMiklos Szeredi open_flag &= ~O_TRUNC; 247747237687SAl Viro *opened |= FILE_CREATED; 2478d18e9008SMiklos Szeredi } 2479d18e9008SMiklos Szeredi 2480d18e9008SMiklos Szeredi /* 2481d18e9008SMiklos Szeredi * Checking write permission is tricky, bacuse we don't know if we are 2482d18e9008SMiklos Szeredi * going to actually need it: O_CREAT opens should work as long as the 2483d18e9008SMiklos Szeredi * file exists. But checking existence breaks atomicity. The trick is 2484d18e9008SMiklos Szeredi * to check access and if not granted clear O_CREAT from the flags. 2485d18e9008SMiklos Szeredi * 2486d18e9008SMiklos Szeredi * Another problem is returing the "right" error value (e.g. for an 2487d18e9008SMiklos Szeredi * O_EXCL open we want to return EEXIST not EROFS). 2488d18e9008SMiklos Szeredi */ 248964894cf8SAl Viro if (((open_flag & (O_CREAT | O_TRUNC)) || 249064894cf8SAl Viro (open_flag & O_ACCMODE) != O_RDONLY) && unlikely(!got_write)) { 249164894cf8SAl Viro if (!(open_flag & O_CREAT)) { 2492d18e9008SMiklos Szeredi /* 2493d18e9008SMiklos Szeredi * No O_CREATE -> atomicity not a requirement -> fall 2494d18e9008SMiklos Szeredi * back to lookup + open 2495d18e9008SMiklos Szeredi */ 2496d18e9008SMiklos Szeredi goto no_open; 2497d18e9008SMiklos Szeredi } else if (open_flag & (O_EXCL | O_TRUNC)) { 2498d18e9008SMiklos Szeredi /* Fall back and fail with the right error */ 249964894cf8SAl Viro create_error = -EROFS; 2500d18e9008SMiklos Szeredi goto no_open; 2501d18e9008SMiklos Szeredi } else { 2502d18e9008SMiklos Szeredi /* No side effects, safe to clear O_CREAT */ 250364894cf8SAl Viro create_error = -EROFS; 2504d18e9008SMiklos Szeredi open_flag &= ~O_CREAT; 2505d18e9008SMiklos Szeredi } 2506d18e9008SMiklos Szeredi } 2507d18e9008SMiklos Szeredi 2508d18e9008SMiklos Szeredi if (open_flag & O_CREAT) { 250938227f78SMiklos Szeredi error = may_o_create(&nd->path, dentry, mode); 2510d18e9008SMiklos Szeredi if (error) { 2511d18e9008SMiklos Szeredi create_error = error; 2512d18e9008SMiklos Szeredi if (open_flag & O_EXCL) 2513d18e9008SMiklos Szeredi goto no_open; 2514d18e9008SMiklos Szeredi open_flag &= ~O_CREAT; 2515d18e9008SMiklos Szeredi } 2516d18e9008SMiklos Szeredi } 2517d18e9008SMiklos Szeredi 2518d18e9008SMiklos Szeredi if (nd->flags & LOOKUP_DIRECTORY) 2519d18e9008SMiklos Szeredi open_flag |= O_DIRECTORY; 2520d18e9008SMiklos Szeredi 252130d90494SAl Viro file->f_path.dentry = DENTRY_NOT_SET; 252230d90494SAl Viro file->f_path.mnt = nd->path.mnt; 252330d90494SAl Viro error = dir->i_op->atomic_open(dir, dentry, file, open_flag, mode, 252447237687SAl Viro opened); 2525d9585277SAl Viro if (error < 0) { 2526d9585277SAl Viro if (create_error && error == -ENOENT) 2527d9585277SAl Viro error = create_error; 2528d18e9008SMiklos Szeredi goto out; 2529d18e9008SMiklos Szeredi } 2530d18e9008SMiklos Szeredi 2531d18e9008SMiklos Szeredi acc_mode = op->acc_mode; 253247237687SAl Viro if (*opened & FILE_CREATED) { 2533d18e9008SMiklos Szeredi fsnotify_create(dir, dentry); 2534d18e9008SMiklos Szeredi acc_mode = MAY_OPEN; 2535d18e9008SMiklos Szeredi } 2536d18e9008SMiklos Szeredi 2537d9585277SAl Viro if (error) { /* returned 1, that is */ 253830d90494SAl Viro if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) { 25392675a4ebSAl Viro error = -EIO; 2540d18e9008SMiklos Szeredi goto out; 2541d18e9008SMiklos Szeredi } 254230d90494SAl Viro if (file->f_path.dentry) { 2543d18e9008SMiklos Szeredi dput(dentry); 254430d90494SAl Viro dentry = file->f_path.dentry; 2545d18e9008SMiklos Szeredi } 254662b2ce96SSage Weil if (create_error && dentry->d_inode == NULL) { 254762b2ce96SSage Weil error = create_error; 254862b2ce96SSage Weil goto out; 254962b2ce96SSage Weil } 2550d18e9008SMiklos Szeredi goto looked_up; 2551d18e9008SMiklos Szeredi } 2552d18e9008SMiklos Szeredi 2553d18e9008SMiklos Szeredi /* 2554d18e9008SMiklos Szeredi * We didn't have the inode before the open, so check open permission 2555d18e9008SMiklos Szeredi * here. 2556d18e9008SMiklos Szeredi */ 25572675a4ebSAl Viro error = may_open(&file->f_path, acc_mode, open_flag); 25582675a4ebSAl Viro if (error) 25592675a4ebSAl Viro fput(file); 2560d18e9008SMiklos Szeredi 2561d18e9008SMiklos Szeredi out: 2562d18e9008SMiklos Szeredi dput(dentry); 25632675a4ebSAl Viro return error; 2564d18e9008SMiklos Szeredi 2565d18e9008SMiklos Szeredi no_open: 2566d18e9008SMiklos Szeredi if (need_lookup) { 256772bd866aSAl Viro dentry = lookup_real(dir, dentry, nd->flags); 2568d18e9008SMiklos Szeredi if (IS_ERR(dentry)) 25692675a4ebSAl Viro return PTR_ERR(dentry); 2570d18e9008SMiklos Szeredi 2571d18e9008SMiklos Szeredi if (create_error) { 2572d18e9008SMiklos Szeredi int open_flag = op->open_flag; 2573d18e9008SMiklos Szeredi 25742675a4ebSAl Viro error = create_error; 2575d18e9008SMiklos Szeredi if ((open_flag & O_EXCL)) { 2576d18e9008SMiklos Szeredi if (!dentry->d_inode) 2577d18e9008SMiklos Szeredi goto out; 2578d18e9008SMiklos Szeredi } else if (!dentry->d_inode) { 2579d18e9008SMiklos Szeredi goto out; 2580d18e9008SMiklos Szeredi } else if ((open_flag & O_TRUNC) && 2581d18e9008SMiklos Szeredi S_ISREG(dentry->d_inode->i_mode)) { 2582d18e9008SMiklos Szeredi goto out; 2583d18e9008SMiklos Szeredi } 2584d18e9008SMiklos Szeredi /* will fail later, go on to get the right error */ 2585d18e9008SMiklos Szeredi } 2586d18e9008SMiklos Szeredi } 2587d18e9008SMiklos Szeredi looked_up: 2588d18e9008SMiklos Szeredi path->dentry = dentry; 2589d18e9008SMiklos Szeredi path->mnt = nd->path.mnt; 25902675a4ebSAl Viro return 1; 2591d18e9008SMiklos Szeredi } 2592d18e9008SMiklos Szeredi 259331e6b01fSNick Piggin /* 25941acf0af9SDavid Howells * Look up and maybe create and open the last component. 2595d58ffd35SMiklos Szeredi * 2596d58ffd35SMiklos Szeredi * Must be called with i_mutex held on parent. 2597d58ffd35SMiklos Szeredi * 25981acf0af9SDavid Howells * Returns 0 if the file was successfully atomically created (if necessary) and 25991acf0af9SDavid Howells * opened. In this case the file will be returned attached to @file. 26001acf0af9SDavid Howells * 26011acf0af9SDavid Howells * Returns 1 if the file was not completely opened at this time, though lookups 26021acf0af9SDavid Howells * and creations will have been performed and the dentry returned in @path will 26031acf0af9SDavid Howells * be positive upon return if O_CREAT was specified. If O_CREAT wasn't 26041acf0af9SDavid Howells * specified then a negative dentry may be returned. 26051acf0af9SDavid Howells * 26061acf0af9SDavid Howells * An error code is returned otherwise. 26071acf0af9SDavid Howells * 26081acf0af9SDavid Howells * FILE_CREATE will be set in @*opened if the dentry was created and will be 26091acf0af9SDavid Howells * cleared otherwise prior to returning. 2610d58ffd35SMiklos Szeredi */ 26112675a4ebSAl Viro static int lookup_open(struct nameidata *nd, struct path *path, 261230d90494SAl Viro struct file *file, 2613d58ffd35SMiklos Szeredi const struct open_flags *op, 261464894cf8SAl Viro bool got_write, int *opened) 2615d58ffd35SMiklos Szeredi { 2616d58ffd35SMiklos Szeredi struct dentry *dir = nd->path.dentry; 261754ef4872SMiklos Szeredi struct inode *dir_inode = dir->d_inode; 2618d58ffd35SMiklos Szeredi struct dentry *dentry; 2619d58ffd35SMiklos Szeredi int error; 262054ef4872SMiklos Szeredi bool need_lookup; 2621d58ffd35SMiklos Szeredi 262247237687SAl Viro *opened &= ~FILE_CREATED; 2623201f956eSAl Viro dentry = lookup_dcache(&nd->last, dir, nd->flags, &need_lookup); 2624d58ffd35SMiklos Szeredi if (IS_ERR(dentry)) 26252675a4ebSAl Viro return PTR_ERR(dentry); 2626d58ffd35SMiklos Szeredi 2627d18e9008SMiklos Szeredi /* Cached positive dentry: will open in f_op->open */ 2628d18e9008SMiklos Szeredi if (!need_lookup && dentry->d_inode) 2629d18e9008SMiklos Szeredi goto out_no_open; 2630d18e9008SMiklos Szeredi 2631d18e9008SMiklos Szeredi if ((nd->flags & LOOKUP_OPEN) && dir_inode->i_op->atomic_open) { 263264894cf8SAl Viro return atomic_open(nd, dentry, path, file, op, got_write, 263347237687SAl Viro need_lookup, opened); 2634d18e9008SMiklos Szeredi } 2635d18e9008SMiklos Szeredi 263654ef4872SMiklos Szeredi if (need_lookup) { 263754ef4872SMiklos Szeredi BUG_ON(dentry->d_inode); 263854ef4872SMiklos Szeredi 263972bd866aSAl Viro dentry = lookup_real(dir_inode, dentry, nd->flags); 264054ef4872SMiklos Szeredi if (IS_ERR(dentry)) 26412675a4ebSAl Viro return PTR_ERR(dentry); 264254ef4872SMiklos Szeredi } 264354ef4872SMiklos Szeredi 2644d58ffd35SMiklos Szeredi /* Negative dentry, just create the file */ 2645d58ffd35SMiklos Szeredi if (!dentry->d_inode && (op->open_flag & O_CREAT)) { 2646d58ffd35SMiklos Szeredi umode_t mode = op->mode; 2647d58ffd35SMiklos Szeredi if (!IS_POSIXACL(dir->d_inode)) 2648d58ffd35SMiklos Szeredi mode &= ~current_umask(); 2649d58ffd35SMiklos Szeredi /* 2650d58ffd35SMiklos Szeredi * This write is needed to ensure that a 2651d58ffd35SMiklos Szeredi * rw->ro transition does not occur between 2652d58ffd35SMiklos Szeredi * the time when the file is created and when 2653d58ffd35SMiklos Szeredi * a permanent write count is taken through 2654015c3bbcSMiklos Szeredi * the 'struct file' in finish_open(). 2655d58ffd35SMiklos Szeredi */ 265664894cf8SAl Viro if (!got_write) { 265764894cf8SAl Viro error = -EROFS; 2658d58ffd35SMiklos Szeredi goto out_dput; 265964894cf8SAl Viro } 266047237687SAl Viro *opened |= FILE_CREATED; 2661d58ffd35SMiklos Szeredi error = security_path_mknod(&nd->path, dentry, mode, 0); 2662d58ffd35SMiklos Szeredi if (error) 2663d58ffd35SMiklos Szeredi goto out_dput; 2664312b63fbSAl Viro error = vfs_create(dir->d_inode, dentry, mode, 2665312b63fbSAl Viro nd->flags & LOOKUP_EXCL); 2666d58ffd35SMiklos Szeredi if (error) 2667d58ffd35SMiklos Szeredi goto out_dput; 2668d58ffd35SMiklos Szeredi } 2669d18e9008SMiklos Szeredi out_no_open: 2670d58ffd35SMiklos Szeredi path->dentry = dentry; 2671d58ffd35SMiklos Szeredi path->mnt = nd->path.mnt; 26722675a4ebSAl Viro return 1; 2673d58ffd35SMiklos Szeredi 2674d58ffd35SMiklos Szeredi out_dput: 2675d58ffd35SMiklos Szeredi dput(dentry); 26762675a4ebSAl Viro return error; 2677d58ffd35SMiklos Szeredi } 2678d58ffd35SMiklos Szeredi 2679d58ffd35SMiklos Szeredi /* 2680fe2d35ffSAl Viro * Handle the last step of open() 268131e6b01fSNick Piggin */ 26822675a4ebSAl Viro static int do_last(struct nameidata *nd, struct path *path, 268330d90494SAl Viro struct file *file, const struct open_flags *op, 2684669abf4eSJeff Layton int *opened, struct filename *name) 2685fb1cc555SAl Viro { 2686a1e28038SAl Viro struct dentry *dir = nd->path.dentry; 2687ca344a89SAl Viro int open_flag = op->open_flag; 268877d660a8SMiklos Szeredi bool will_truncate = (open_flag & O_TRUNC) != 0; 268964894cf8SAl Viro bool got_write = false; 2690bcda7652SAl Viro int acc_mode = op->acc_mode; 2691a1eb3315SMiklos Szeredi struct inode *inode; 269277d660a8SMiklos Szeredi bool symlink_ok = false; 269316b1c1cdSMiklos Szeredi struct path save_parent = { .dentry = NULL, .mnt = NULL }; 269416b1c1cdSMiklos Szeredi bool retried = false; 269516c2cd71SAl Viro int error; 2696fb1cc555SAl Viro 2697c3e380b0SAl Viro nd->flags &= ~LOOKUP_PARENT; 2698c3e380b0SAl Viro nd->flags |= op->intent; 2699c3e380b0SAl Viro 27001f36f774SAl Viro switch (nd->last_type) { 27011f36f774SAl Viro case LAST_DOTDOT: 2702176306f5SNeil Brown case LAST_DOT: 2703fe2d35ffSAl Viro error = handle_dots(nd, nd->last_type); 2704fe2d35ffSAl Viro if (error) 27052675a4ebSAl Viro return error; 27061f36f774SAl Viro /* fallthrough */ 27071f36f774SAl Viro case LAST_ROOT: 27089f1fafeeSAl Viro error = complete_walk(nd); 270916c2cd71SAl Viro if (error) 27102675a4ebSAl Viro return error; 2711adb5c247SJeff Layton audit_inode(name, nd->path.dentry, 0); 2712ca344a89SAl Viro if (open_flag & O_CREAT) { 271316c2cd71SAl Viro error = -EISDIR; 27142675a4ebSAl Viro goto out; 2715fe2d35ffSAl Viro } 2716e83db167SMiklos Szeredi goto finish_open; 27171f36f774SAl Viro case LAST_BIND: 27189f1fafeeSAl Viro error = complete_walk(nd); 271916c2cd71SAl Viro if (error) 27202675a4ebSAl Viro return error; 2721adb5c247SJeff Layton audit_inode(name, dir, 0); 2722e83db167SMiklos Szeredi goto finish_open; 27231f36f774SAl Viro } 2724a2c36b45SAl Viro 2725ca344a89SAl Viro if (!(open_flag & O_CREAT)) { 2726fe2d35ffSAl Viro if (nd->last.name[nd->last.len]) 2727fe2d35ffSAl Viro nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 2728bcda7652SAl Viro if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW)) 272977d660a8SMiklos Szeredi symlink_ok = true; 2730fe2d35ffSAl Viro /* we _can_ be in RCU mode here */ 2731a1eb3315SMiklos Szeredi error = lookup_fast(nd, &nd->last, path, &inode); 273271574865SMiklos Szeredi if (likely(!error)) 273371574865SMiklos Szeredi goto finish_lookup; 273471574865SMiklos Szeredi 2735ce57dfc1SAl Viro if (error < 0) 27362675a4ebSAl Viro goto out; 2737a1eb3315SMiklos Szeredi 273837d7fffcSMiklos Szeredi BUG_ON(nd->inode != dir->d_inode); 2739b6183df7SMiklos Szeredi } else { 2740fe2d35ffSAl Viro /* create side of things */ 2741a3fbbde7SAl Viro /* 2742b6183df7SMiklos Szeredi * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED 2743b6183df7SMiklos Szeredi * has been cleared when we got to the last component we are 2744b6183df7SMiklos Szeredi * about to look up 2745a3fbbde7SAl Viro */ 27469f1fafeeSAl Viro error = complete_walk(nd); 27479f1fafeeSAl Viro if (error) 27482675a4ebSAl Viro return error; 2749fe2d35ffSAl Viro 2750adb5c247SJeff Layton audit_inode(name, dir, 0); 275116c2cd71SAl Viro error = -EISDIR; 27521f36f774SAl Viro /* trailing slashes? */ 275331e6b01fSNick Piggin if (nd->last.name[nd->last.len]) 27542675a4ebSAl Viro goto out; 2755b6183df7SMiklos Szeredi } 27561f36f774SAl Viro 275716b1c1cdSMiklos Szeredi retry_lookup: 275864894cf8SAl Viro if (op->open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) { 275964894cf8SAl Viro error = mnt_want_write(nd->path.mnt); 276064894cf8SAl Viro if (!error) 276164894cf8SAl Viro got_write = true; 276264894cf8SAl Viro /* 276364894cf8SAl Viro * do _not_ fail yet - we might not need that or fail with 276464894cf8SAl Viro * a different error; let lookup_open() decide; we'll be 276564894cf8SAl Viro * dropping this one anyway. 276664894cf8SAl Viro */ 276764894cf8SAl Viro } 2768a1e28038SAl Viro mutex_lock(&dir->d_inode->i_mutex); 276964894cf8SAl Viro error = lookup_open(nd, path, file, op, got_write, opened); 2770fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2771fb1cc555SAl Viro 27722675a4ebSAl Viro if (error <= 0) { 27732675a4ebSAl Viro if (error) 2774d58ffd35SMiklos Szeredi goto out; 27756c0d46c4SAl Viro 277647237687SAl Viro if ((*opened & FILE_CREATED) || 2777496ad9aaSAl Viro !S_ISREG(file_inode(file)->i_mode)) 277877d660a8SMiklos Szeredi will_truncate = false; 2779d18e9008SMiklos Szeredi 2780adb5c247SJeff Layton audit_inode(name, file->f_path.dentry, 0); 2781d18e9008SMiklos Szeredi goto opened; 2782d18e9008SMiklos Szeredi } 2783d18e9008SMiklos Szeredi 278447237687SAl Viro if (*opened & FILE_CREATED) { 27859b44f1b3SAl Viro /* Don't check for write permission, don't truncate */ 2786ca344a89SAl Viro open_flag &= ~O_TRUNC; 278777d660a8SMiklos Szeredi will_truncate = false; 2788bcda7652SAl Viro acc_mode = MAY_OPEN; 2789d58ffd35SMiklos Szeredi path_to_nameidata(path, nd); 2790e83db167SMiklos Szeredi goto finish_open_created; 2791fb1cc555SAl Viro } 2792fb1cc555SAl Viro 2793fb1cc555SAl Viro /* 27943134f37eSJeff Layton * create/update audit record if it already exists. 2795fb1cc555SAl Viro */ 27963134f37eSJeff Layton if (path->dentry->d_inode) 2797adb5c247SJeff Layton audit_inode(name, path->dentry, 0); 2798fb1cc555SAl Viro 2799d18e9008SMiklos Szeredi /* 2800d18e9008SMiklos Szeredi * If atomic_open() acquired write access it is dropped now due to 2801d18e9008SMiklos Szeredi * possible mount and symlink following (this might be optimized away if 2802d18e9008SMiklos Szeredi * necessary...) 2803d18e9008SMiklos Szeredi */ 280464894cf8SAl Viro if (got_write) { 2805d18e9008SMiklos Szeredi mnt_drop_write(nd->path.mnt); 280664894cf8SAl Viro got_write = false; 2807d18e9008SMiklos Szeredi } 2808d18e9008SMiklos Szeredi 2809fb1cc555SAl Viro error = -EEXIST; 2810f8310c59SAl Viro if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT)) 2811fb1cc555SAl Viro goto exit_dput; 2812fb1cc555SAl Viro 28139875cf80SDavid Howells error = follow_managed(path, nd->flags); 28149875cf80SDavid Howells if (error < 0) 2815fb1cc555SAl Viro goto exit_dput; 2816fb1cc555SAl Viro 2817a3fbbde7SAl Viro if (error) 2818a3fbbde7SAl Viro nd->flags |= LOOKUP_JUMPED; 2819a3fbbde7SAl Viro 2820decf3400SMiklos Szeredi BUG_ON(nd->flags & LOOKUP_RCU); 2821decf3400SMiklos Szeredi inode = path->dentry->d_inode; 28225f5daac1SMiklos Szeredi finish_lookup: 28235f5daac1SMiklos Szeredi /* we _can_ be in RCU mode here */ 2824fb1cc555SAl Viro error = -ENOENT; 282554c33e7fSMiklos Szeredi if (!inode) { 282654c33e7fSMiklos Szeredi path_to_nameidata(path, nd); 28272675a4ebSAl Viro goto out; 282854c33e7fSMiklos Szeredi } 28299e67f361SAl Viro 2830d45ea867SMiklos Szeredi if (should_follow_link(inode, !symlink_ok)) { 2831d45ea867SMiklos Szeredi if (nd->flags & LOOKUP_RCU) { 2832d45ea867SMiklos Szeredi if (unlikely(unlazy_walk(nd, path->dentry))) { 2833d45ea867SMiklos Szeredi error = -ECHILD; 28342675a4ebSAl Viro goto out; 2835d45ea867SMiklos Szeredi } 2836d45ea867SMiklos Szeredi } 2837d45ea867SMiklos Szeredi BUG_ON(inode != path->dentry->d_inode); 28382675a4ebSAl Viro return 1; 2839d45ea867SMiklos Szeredi } 2840fb1cc555SAl Viro 284116b1c1cdSMiklos Szeredi if ((nd->flags & LOOKUP_RCU) || nd->path.mnt != path->mnt) { 2842fb1cc555SAl Viro path_to_nameidata(path, nd); 284316b1c1cdSMiklos Szeredi } else { 284416b1c1cdSMiklos Szeredi save_parent.dentry = nd->path.dentry; 284516b1c1cdSMiklos Szeredi save_parent.mnt = mntget(path->mnt); 284616b1c1cdSMiklos Szeredi nd->path.dentry = path->dentry; 284716b1c1cdSMiklos Szeredi 284816b1c1cdSMiklos Szeredi } 2849decf3400SMiklos Szeredi nd->inode = inode; 2850a3fbbde7SAl Viro /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */ 2851a3fbbde7SAl Viro error = complete_walk(nd); 285216b1c1cdSMiklos Szeredi if (error) { 285316b1c1cdSMiklos Szeredi path_put(&save_parent); 28542675a4ebSAl Viro return error; 285516b1c1cdSMiklos Szeredi } 2856fb1cc555SAl Viro error = -EISDIR; 2857050ac841SMiklos Szeredi if ((open_flag & O_CREAT) && S_ISDIR(nd->inode->i_mode)) 28582675a4ebSAl Viro goto out; 2859af2f5542SMiklos Szeredi error = -ENOTDIR; 2860af2f5542SMiklos Szeredi if ((nd->flags & LOOKUP_DIRECTORY) && !nd->inode->i_op->lookup) 28612675a4ebSAl Viro goto out; 2862adb5c247SJeff Layton audit_inode(name, nd->path.dentry, 0); 2863e83db167SMiklos Szeredi finish_open: 28646c0d46c4SAl Viro if (!S_ISREG(nd->inode->i_mode)) 286577d660a8SMiklos Szeredi will_truncate = false; 28666c0d46c4SAl Viro 28670f9d1a10SAl Viro if (will_truncate) { 28680f9d1a10SAl Viro error = mnt_want_write(nd->path.mnt); 28690f9d1a10SAl Viro if (error) 28702675a4ebSAl Viro goto out; 287164894cf8SAl Viro got_write = true; 28720f9d1a10SAl Viro } 2873e83db167SMiklos Szeredi finish_open_created: 2874bcda7652SAl Viro error = may_open(&nd->path, acc_mode, open_flag); 2875ca344a89SAl Viro if (error) 28762675a4ebSAl Viro goto out; 287730d90494SAl Viro file->f_path.mnt = nd->path.mnt; 287830d90494SAl Viro error = finish_open(file, nd->path.dentry, NULL, opened); 287930d90494SAl Viro if (error) { 288030d90494SAl Viro if (error == -EOPENSTALE) 2881f60dc3dbSMiklos Szeredi goto stale_open; 2882015c3bbcSMiklos Szeredi goto out; 2883f60dc3dbSMiklos Szeredi } 2884a8277b9bSMiklos Szeredi opened: 28852675a4ebSAl Viro error = open_check_o_direct(file); 2886015c3bbcSMiklos Szeredi if (error) 2887015c3bbcSMiklos Szeredi goto exit_fput; 28882675a4ebSAl Viro error = ima_file_check(file, op->acc_mode); 2889aa4caadbSMiklos Szeredi if (error) 2890aa4caadbSMiklos Szeredi goto exit_fput; 2891aa4caadbSMiklos Szeredi 28920f9d1a10SAl Viro if (will_truncate) { 28932675a4ebSAl Viro error = handle_truncate(file); 2894aa4caadbSMiklos Szeredi if (error) 2895aa4caadbSMiklos Szeredi goto exit_fput; 28960f9d1a10SAl Viro } 2897ca344a89SAl Viro out: 289864894cf8SAl Viro if (got_write) 28990f9d1a10SAl Viro mnt_drop_write(nd->path.mnt); 290016b1c1cdSMiklos Szeredi path_put(&save_parent); 2901e276ae67SMiklos Szeredi terminate_walk(nd); 29022675a4ebSAl Viro return error; 2903fb1cc555SAl Viro 2904fb1cc555SAl Viro exit_dput: 2905fb1cc555SAl Viro path_put_conditional(path, nd); 2906ca344a89SAl Viro goto out; 2907015c3bbcSMiklos Szeredi exit_fput: 29082675a4ebSAl Viro fput(file); 29092675a4ebSAl Viro goto out; 2910015c3bbcSMiklos Szeredi 2911f60dc3dbSMiklos Szeredi stale_open: 2912f60dc3dbSMiklos Szeredi /* If no saved parent or already retried then can't retry */ 2913f60dc3dbSMiklos Szeredi if (!save_parent.dentry || retried) 2914f60dc3dbSMiklos Szeredi goto out; 2915f60dc3dbSMiklos Szeredi 2916f60dc3dbSMiklos Szeredi BUG_ON(save_parent.dentry != dir); 2917f60dc3dbSMiklos Szeredi path_put(&nd->path); 2918f60dc3dbSMiklos Szeredi nd->path = save_parent; 2919f60dc3dbSMiklos Szeredi nd->inode = dir->d_inode; 2920f60dc3dbSMiklos Szeredi save_parent.mnt = NULL; 2921f60dc3dbSMiklos Szeredi save_parent.dentry = NULL; 292264894cf8SAl Viro if (got_write) { 2923f60dc3dbSMiklos Szeredi mnt_drop_write(nd->path.mnt); 292464894cf8SAl Viro got_write = false; 2925f60dc3dbSMiklos Szeredi } 2926f60dc3dbSMiklos Szeredi retried = true; 2927f60dc3dbSMiklos Szeredi goto retry_lookup; 2928fb1cc555SAl Viro } 2929fb1cc555SAl Viro 2930669abf4eSJeff Layton static struct file *path_openat(int dfd, struct filename *pathname, 293173d049a4SAl Viro struct nameidata *nd, const struct open_flags *op, int flags) 29321da177e4SLinus Torvalds { 2933fe2d35ffSAl Viro struct file *base = NULL; 293430d90494SAl Viro struct file *file; 29359850c056SAl Viro struct path path; 293647237687SAl Viro int opened = 0; 293713aab428SAl Viro int error; 293831e6b01fSNick Piggin 293930d90494SAl Viro file = get_empty_filp(); 29401afc99beSAl Viro if (IS_ERR(file)) 29411afc99beSAl Viro return file; 294231e6b01fSNick Piggin 294330d90494SAl Viro file->f_flags = op->open_flag; 294431e6b01fSNick Piggin 2945669abf4eSJeff Layton error = path_init(dfd, pathname->name, flags | LOOKUP_PARENT, nd, &base); 294631e6b01fSNick Piggin if (unlikely(error)) 29472675a4ebSAl Viro goto out; 294831e6b01fSNick Piggin 2949fe2d35ffSAl Viro current->total_link_count = 0; 2950669abf4eSJeff Layton error = link_path_walk(pathname->name, nd); 295131e6b01fSNick Piggin if (unlikely(error)) 29522675a4ebSAl Viro goto out; 29531da177e4SLinus Torvalds 29542675a4ebSAl Viro error = do_last(nd, &path, file, op, &opened, pathname); 29552675a4ebSAl Viro while (unlikely(error > 0)) { /* trailing symlink */ 29567b9337aaSNick Piggin struct path link = path; 2957def4af30SAl Viro void *cookie; 2958574197e0SAl Viro if (!(nd->flags & LOOKUP_FOLLOW)) { 295973d049a4SAl Viro path_put_conditional(&path, nd); 296073d049a4SAl Viro path_put(&nd->path); 29612675a4ebSAl Viro error = -ELOOP; 296240b39136SAl Viro break; 296340b39136SAl Viro } 2964800179c9SKees Cook error = may_follow_link(&link, nd); 2965800179c9SKees Cook if (unlikely(error)) 2966800179c9SKees Cook break; 296773d049a4SAl Viro nd->flags |= LOOKUP_PARENT; 296873d049a4SAl Viro nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL); 2969574197e0SAl Viro error = follow_link(&link, nd, &cookie); 2970c3e380b0SAl Viro if (unlikely(error)) 29712675a4ebSAl Viro break; 29722675a4ebSAl Viro error = do_last(nd, &path, file, op, &opened, pathname); 2973574197e0SAl Viro put_link(nd, &link, cookie); 2974806b681cSAl Viro } 297510fa8e62SAl Viro out: 297673d049a4SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) 297773d049a4SAl Viro path_put(&nd->root); 2978fe2d35ffSAl Viro if (base) 2979fe2d35ffSAl Viro fput(base); 29802675a4ebSAl Viro if (!(opened & FILE_OPENED)) { 29812675a4ebSAl Viro BUG_ON(!error); 298230d90494SAl Viro put_filp(file); 2983015c3bbcSMiklos Szeredi } 29842675a4ebSAl Viro if (unlikely(error)) { 29852675a4ebSAl Viro if (error == -EOPENSTALE) { 29862675a4ebSAl Viro if (flags & LOOKUP_RCU) 29872675a4ebSAl Viro error = -ECHILD; 29882675a4ebSAl Viro else 29892675a4ebSAl Viro error = -ESTALE; 29902675a4ebSAl Viro } 29912675a4ebSAl Viro file = ERR_PTR(error); 29922675a4ebSAl Viro } 29932675a4ebSAl Viro return file; 2994de459215SKirill Korotaev } 29951da177e4SLinus Torvalds 2996669abf4eSJeff Layton struct file *do_filp_open(int dfd, struct filename *pathname, 299713aab428SAl Viro const struct open_flags *op, int flags) 299813aab428SAl Viro { 299973d049a4SAl Viro struct nameidata nd; 300013aab428SAl Viro struct file *filp; 300113aab428SAl Viro 300273d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU); 300313aab428SAl Viro if (unlikely(filp == ERR_PTR(-ECHILD))) 300473d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags); 300513aab428SAl Viro if (unlikely(filp == ERR_PTR(-ESTALE))) 300673d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL); 300713aab428SAl Viro return filp; 300813aab428SAl Viro } 300913aab428SAl Viro 301073d049a4SAl Viro struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt, 301173d049a4SAl Viro const char *name, const struct open_flags *op, int flags) 301273d049a4SAl Viro { 301373d049a4SAl Viro struct nameidata nd; 301473d049a4SAl Viro struct file *file; 3015669abf4eSJeff Layton struct filename filename = { .name = name }; 301673d049a4SAl Viro 301773d049a4SAl Viro nd.root.mnt = mnt; 301873d049a4SAl Viro nd.root.dentry = dentry; 301973d049a4SAl Viro 302073d049a4SAl Viro flags |= LOOKUP_ROOT; 302173d049a4SAl Viro 3022bcda7652SAl Viro if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN) 302373d049a4SAl Viro return ERR_PTR(-ELOOP); 302473d049a4SAl Viro 3025669abf4eSJeff Layton file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_RCU); 302673d049a4SAl Viro if (unlikely(file == ERR_PTR(-ECHILD))) 3027669abf4eSJeff Layton file = path_openat(-1, &filename, &nd, op, flags); 302873d049a4SAl Viro if (unlikely(file == ERR_PTR(-ESTALE))) 3029669abf4eSJeff Layton file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_REVAL); 303073d049a4SAl Viro return file; 303173d049a4SAl Viro } 303273d049a4SAl Viro 30331ac12b4bSJeff Layton struct dentry *kern_path_create(int dfd, const char *pathname, 30341ac12b4bSJeff Layton struct path *path, unsigned int lookup_flags) 30351da177e4SLinus Torvalds { 3036c663e5d8SChristoph Hellwig struct dentry *dentry = ERR_PTR(-EEXIST); 3037ed75e95dSAl Viro struct nameidata nd; 3038c30dabfeSJan Kara int err2; 30391ac12b4bSJeff Layton int error; 30401ac12b4bSJeff Layton bool is_dir = (lookup_flags & LOOKUP_DIRECTORY); 30411ac12b4bSJeff Layton 30421ac12b4bSJeff Layton /* 30431ac12b4bSJeff Layton * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any 30441ac12b4bSJeff Layton * other flags passed in are ignored! 30451ac12b4bSJeff Layton */ 30461ac12b4bSJeff Layton lookup_flags &= LOOKUP_REVAL; 30471ac12b4bSJeff Layton 30481ac12b4bSJeff Layton error = do_path_lookup(dfd, pathname, LOOKUP_PARENT|lookup_flags, &nd); 3049ed75e95dSAl Viro if (error) 3050ed75e95dSAl Viro return ERR_PTR(error); 30511da177e4SLinus Torvalds 3052c663e5d8SChristoph Hellwig /* 3053c663e5d8SChristoph Hellwig * Yucky last component or no last component at all? 3054c663e5d8SChristoph Hellwig * (foo/., foo/.., /////) 3055c663e5d8SChristoph Hellwig */ 3056ed75e95dSAl Viro if (nd.last_type != LAST_NORM) 3057ed75e95dSAl Viro goto out; 3058ed75e95dSAl Viro nd.flags &= ~LOOKUP_PARENT; 3059ed75e95dSAl Viro nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL; 3060c663e5d8SChristoph Hellwig 3061c30dabfeSJan Kara /* don't fail immediately if it's r/o, at least try to report other errors */ 3062c30dabfeSJan Kara err2 = mnt_want_write(nd.path.mnt); 3063c663e5d8SChristoph Hellwig /* 3064c663e5d8SChristoph Hellwig * Do the final lookup. 3065c663e5d8SChristoph Hellwig */ 3066ed75e95dSAl Viro mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 3067ed75e95dSAl Viro dentry = lookup_hash(&nd); 30681da177e4SLinus Torvalds if (IS_ERR(dentry)) 3069a8104a9fSAl Viro goto unlock; 3070c663e5d8SChristoph Hellwig 3071a8104a9fSAl Viro error = -EEXIST; 3072e9baf6e5SAl Viro if (dentry->d_inode) 3073a8104a9fSAl Viro goto fail; 3074c663e5d8SChristoph Hellwig /* 3075c663e5d8SChristoph Hellwig * Special case - lookup gave negative, but... we had foo/bar/ 3076c663e5d8SChristoph Hellwig * From the vfs_mknod() POV we just have a negative dentry - 3077c663e5d8SChristoph Hellwig * all is fine. Let's be bastards - you had / on the end, you've 3078c663e5d8SChristoph Hellwig * been asking for (non-existent) directory. -ENOENT for you. 3079c663e5d8SChristoph Hellwig */ 3080ed75e95dSAl Viro if (unlikely(!is_dir && nd.last.name[nd.last.len])) { 3081a8104a9fSAl Viro error = -ENOENT; 3082ed75e95dSAl Viro goto fail; 3083e9baf6e5SAl Viro } 3084c30dabfeSJan Kara if (unlikely(err2)) { 3085c30dabfeSJan Kara error = err2; 3086a8104a9fSAl Viro goto fail; 3087c30dabfeSJan Kara } 3088ed75e95dSAl Viro *path = nd.path; 3089e9baf6e5SAl Viro return dentry; 30901da177e4SLinus Torvalds fail: 3091a8104a9fSAl Viro dput(dentry); 3092a8104a9fSAl Viro dentry = ERR_PTR(error); 3093a8104a9fSAl Viro unlock: 3094dae6ad8fSAl Viro mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 3095c30dabfeSJan Kara if (!err2) 3096c30dabfeSJan Kara mnt_drop_write(nd.path.mnt); 3097ed75e95dSAl Viro out: 3098dae6ad8fSAl Viro path_put(&nd.path); 3099ed75e95dSAl Viro return dentry; 3100dae6ad8fSAl Viro } 3101dae6ad8fSAl Viro EXPORT_SYMBOL(kern_path_create); 3102dae6ad8fSAl Viro 3103921a1650SAl Viro void done_path_create(struct path *path, struct dentry *dentry) 3104921a1650SAl Viro { 3105921a1650SAl Viro dput(dentry); 3106921a1650SAl Viro mutex_unlock(&path->dentry->d_inode->i_mutex); 3107a8104a9fSAl Viro mnt_drop_write(path->mnt); 3108921a1650SAl Viro path_put(path); 3109921a1650SAl Viro } 3110921a1650SAl Viro EXPORT_SYMBOL(done_path_create); 3111921a1650SAl Viro 31121ac12b4bSJeff Layton struct dentry *user_path_create(int dfd, const char __user *pathname, 31131ac12b4bSJeff Layton struct path *path, unsigned int lookup_flags) 3114dae6ad8fSAl Viro { 311591a27b2aSJeff Layton struct filename *tmp = getname(pathname); 3116dae6ad8fSAl Viro struct dentry *res; 3117dae6ad8fSAl Viro if (IS_ERR(tmp)) 3118dae6ad8fSAl Viro return ERR_CAST(tmp); 31191ac12b4bSJeff Layton res = kern_path_create(dfd, tmp->name, path, lookup_flags); 3120dae6ad8fSAl Viro putname(tmp); 3121dae6ad8fSAl Viro return res; 3122dae6ad8fSAl Viro } 3123dae6ad8fSAl Viro EXPORT_SYMBOL(user_path_create); 3124dae6ad8fSAl Viro 31251a67aafbSAl Viro int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev) 31261da177e4SLinus Torvalds { 3127a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 31281da177e4SLinus Torvalds 31291da177e4SLinus Torvalds if (error) 31301da177e4SLinus Torvalds return error; 31311da177e4SLinus Torvalds 3132975d6b39SEric W. Biederman if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD)) 31331da177e4SLinus Torvalds return -EPERM; 31341da177e4SLinus Torvalds 3135acfa4380SAl Viro if (!dir->i_op->mknod) 31361da177e4SLinus Torvalds return -EPERM; 31371da177e4SLinus Torvalds 313808ce5f16SSerge E. Hallyn error = devcgroup_inode_mknod(mode, dev); 313908ce5f16SSerge E. Hallyn if (error) 314008ce5f16SSerge E. Hallyn return error; 314108ce5f16SSerge E. Hallyn 31421da177e4SLinus Torvalds error = security_inode_mknod(dir, dentry, mode, dev); 31431da177e4SLinus Torvalds if (error) 31441da177e4SLinus Torvalds return error; 31451da177e4SLinus Torvalds 31461da177e4SLinus Torvalds error = dir->i_op->mknod(dir, dentry, mode, dev); 3147a74574aaSStephen Smalley if (!error) 3148f38aa942SAmy Griffis fsnotify_create(dir, dentry); 31491da177e4SLinus Torvalds return error; 31501da177e4SLinus Torvalds } 31511da177e4SLinus Torvalds 3152f69aac00SAl Viro static int may_mknod(umode_t mode) 3153463c3197SDave Hansen { 3154463c3197SDave Hansen switch (mode & S_IFMT) { 3155463c3197SDave Hansen case S_IFREG: 3156463c3197SDave Hansen case S_IFCHR: 3157463c3197SDave Hansen case S_IFBLK: 3158463c3197SDave Hansen case S_IFIFO: 3159463c3197SDave Hansen case S_IFSOCK: 3160463c3197SDave Hansen case 0: /* zero mode translates to S_IFREG */ 3161463c3197SDave Hansen return 0; 3162463c3197SDave Hansen case S_IFDIR: 3163463c3197SDave Hansen return -EPERM; 3164463c3197SDave Hansen default: 3165463c3197SDave Hansen return -EINVAL; 3166463c3197SDave Hansen } 3167463c3197SDave Hansen } 3168463c3197SDave Hansen 31698208a22bSAl Viro SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode, 31702e4d0924SHeiko Carstens unsigned, dev) 31711da177e4SLinus Torvalds { 31721da177e4SLinus Torvalds struct dentry *dentry; 3173dae6ad8fSAl Viro struct path path; 3174dae6ad8fSAl Viro int error; 3175972567f1SJeff Layton unsigned int lookup_flags = 0; 31761da177e4SLinus Torvalds 31778e4bfca1SAl Viro error = may_mknod(mode); 31788e4bfca1SAl Viro if (error) 31798e4bfca1SAl Viro return error; 3180972567f1SJeff Layton retry: 3181972567f1SJeff Layton dentry = user_path_create(dfd, filename, &path, lookup_flags); 3182dae6ad8fSAl Viro if (IS_ERR(dentry)) 3183dae6ad8fSAl Viro return PTR_ERR(dentry); 31842ad94ae6SAl Viro 3185dae6ad8fSAl Viro if (!IS_POSIXACL(path.dentry->d_inode)) 3186ce3b0f8dSAl Viro mode &= ~current_umask(); 3187dae6ad8fSAl Viro error = security_path_mknod(&path, dentry, mode, dev); 3188be6d3e56SKentaro Takeda if (error) 3189a8104a9fSAl Viro goto out; 31901da177e4SLinus Torvalds switch (mode & S_IFMT) { 31911da177e4SLinus Torvalds case 0: case S_IFREG: 3192312b63fbSAl Viro error = vfs_create(path.dentry->d_inode,dentry,mode,true); 31931da177e4SLinus Torvalds break; 31941da177e4SLinus Torvalds case S_IFCHR: case S_IFBLK: 3195dae6ad8fSAl Viro error = vfs_mknod(path.dentry->d_inode,dentry,mode, 31961da177e4SLinus Torvalds new_decode_dev(dev)); 31971da177e4SLinus Torvalds break; 31981da177e4SLinus Torvalds case S_IFIFO: case S_IFSOCK: 3199dae6ad8fSAl Viro error = vfs_mknod(path.dentry->d_inode,dentry,mode,0); 32001da177e4SLinus Torvalds break; 32011da177e4SLinus Torvalds } 3202a8104a9fSAl Viro out: 3203921a1650SAl Viro done_path_create(&path, dentry); 3204972567f1SJeff Layton if (retry_estale(error, lookup_flags)) { 3205972567f1SJeff Layton lookup_flags |= LOOKUP_REVAL; 3206972567f1SJeff Layton goto retry; 3207972567f1SJeff Layton } 32081da177e4SLinus Torvalds return error; 32091da177e4SLinus Torvalds } 32101da177e4SLinus Torvalds 32118208a22bSAl Viro SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev) 32125590ff0dSUlrich Drepper { 32135590ff0dSUlrich Drepper return sys_mknodat(AT_FDCWD, filename, mode, dev); 32145590ff0dSUlrich Drepper } 32155590ff0dSUlrich Drepper 321618bb1db3SAl Viro int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) 32171da177e4SLinus Torvalds { 3218a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 32198de52778SAl Viro unsigned max_links = dir->i_sb->s_max_links; 32201da177e4SLinus Torvalds 32211da177e4SLinus Torvalds if (error) 32221da177e4SLinus Torvalds return error; 32231da177e4SLinus Torvalds 3224acfa4380SAl Viro if (!dir->i_op->mkdir) 32251da177e4SLinus Torvalds return -EPERM; 32261da177e4SLinus Torvalds 32271da177e4SLinus Torvalds mode &= (S_IRWXUGO|S_ISVTX); 32281da177e4SLinus Torvalds error = security_inode_mkdir(dir, dentry, mode); 32291da177e4SLinus Torvalds if (error) 32301da177e4SLinus Torvalds return error; 32311da177e4SLinus Torvalds 32328de52778SAl Viro if (max_links && dir->i_nlink >= max_links) 32338de52778SAl Viro return -EMLINK; 32348de52778SAl Viro 32351da177e4SLinus Torvalds error = dir->i_op->mkdir(dir, dentry, mode); 3236a74574aaSStephen Smalley if (!error) 3237f38aa942SAmy Griffis fsnotify_mkdir(dir, dentry); 32381da177e4SLinus Torvalds return error; 32391da177e4SLinus Torvalds } 32401da177e4SLinus Torvalds 3241a218d0fdSAl Viro SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode) 32421da177e4SLinus Torvalds { 32436902d925SDave Hansen struct dentry *dentry; 3244dae6ad8fSAl Viro struct path path; 3245dae6ad8fSAl Viro int error; 3246b76d8b82SJeff Layton unsigned int lookup_flags = LOOKUP_DIRECTORY; 32471da177e4SLinus Torvalds 3248b76d8b82SJeff Layton retry: 3249b76d8b82SJeff Layton dentry = user_path_create(dfd, pathname, &path, lookup_flags); 32506902d925SDave Hansen if (IS_ERR(dentry)) 3251dae6ad8fSAl Viro return PTR_ERR(dentry); 32526902d925SDave Hansen 3253dae6ad8fSAl Viro if (!IS_POSIXACL(path.dentry->d_inode)) 3254ce3b0f8dSAl Viro mode &= ~current_umask(); 3255dae6ad8fSAl Viro error = security_path_mkdir(&path, dentry, mode); 3256a8104a9fSAl Viro if (!error) 3257dae6ad8fSAl Viro error = vfs_mkdir(path.dentry->d_inode, dentry, mode); 3258921a1650SAl Viro done_path_create(&path, dentry); 3259b76d8b82SJeff Layton if (retry_estale(error, lookup_flags)) { 3260b76d8b82SJeff Layton lookup_flags |= LOOKUP_REVAL; 3261b76d8b82SJeff Layton goto retry; 3262b76d8b82SJeff Layton } 32631da177e4SLinus Torvalds return error; 32641da177e4SLinus Torvalds } 32651da177e4SLinus Torvalds 3266a218d0fdSAl Viro SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode) 32675590ff0dSUlrich Drepper { 32685590ff0dSUlrich Drepper return sys_mkdirat(AT_FDCWD, pathname, mode); 32695590ff0dSUlrich Drepper } 32705590ff0dSUlrich Drepper 32711da177e4SLinus Torvalds /* 3272a71905f0SSage Weil * The dentry_unhash() helper will try to drop the dentry early: we 3273c0d02594SJ. Bruce Fields * should have a usage count of 1 if we're the only user of this 3274a71905f0SSage Weil * dentry, and if that is true (possibly after pruning the dcache), 3275a71905f0SSage Weil * then we drop the dentry now. 32761da177e4SLinus Torvalds * 32771da177e4SLinus Torvalds * A low-level filesystem can, if it choses, legally 32781da177e4SLinus Torvalds * do a 32791da177e4SLinus Torvalds * 32801da177e4SLinus Torvalds * if (!d_unhashed(dentry)) 32811da177e4SLinus Torvalds * return -EBUSY; 32821da177e4SLinus Torvalds * 32831da177e4SLinus Torvalds * if it cannot handle the case of removing a directory 32841da177e4SLinus Torvalds * that is still in use by something else.. 32851da177e4SLinus Torvalds */ 32861da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry) 32871da177e4SLinus Torvalds { 32881da177e4SLinus Torvalds shrink_dcache_parent(dentry); 32891da177e4SLinus Torvalds spin_lock(&dentry->d_lock); 329064252c75SSage Weil if (dentry->d_count == 1) 32911da177e4SLinus Torvalds __d_drop(dentry); 32921da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 32931da177e4SLinus Torvalds } 32941da177e4SLinus Torvalds 32951da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry) 32961da177e4SLinus Torvalds { 32971da177e4SLinus Torvalds int error = may_delete(dir, dentry, 1); 32981da177e4SLinus Torvalds 32991da177e4SLinus Torvalds if (error) 33001da177e4SLinus Torvalds return error; 33011da177e4SLinus Torvalds 3302acfa4380SAl Viro if (!dir->i_op->rmdir) 33031da177e4SLinus Torvalds return -EPERM; 33041da177e4SLinus Torvalds 33051d2ef590SAl Viro dget(dentry); 33061b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 3307912dbc15SSage Weil 33081da177e4SLinus Torvalds error = -EBUSY; 3309912dbc15SSage Weil if (d_mountpoint(dentry)) 3310912dbc15SSage Weil goto out; 3311912dbc15SSage Weil 33121da177e4SLinus Torvalds error = security_inode_rmdir(dir, dentry); 3313912dbc15SSage Weil if (error) 3314912dbc15SSage Weil goto out; 3315912dbc15SSage Weil 33163cebde24SSage Weil shrink_dcache_parent(dentry); 33171da177e4SLinus Torvalds error = dir->i_op->rmdir(dir, dentry); 3318912dbc15SSage Weil if (error) 3319912dbc15SSage Weil goto out; 3320912dbc15SSage Weil 33211da177e4SLinus Torvalds dentry->d_inode->i_flags |= S_DEAD; 3322d83c49f3SAl Viro dont_mount(dentry); 33231da177e4SLinus Torvalds 3324912dbc15SSage Weil out: 3325912dbc15SSage Weil mutex_unlock(&dentry->d_inode->i_mutex); 33261d2ef590SAl Viro dput(dentry); 3327912dbc15SSage Weil if (!error) 3328912dbc15SSage Weil d_delete(dentry); 33291da177e4SLinus Torvalds return error; 33301da177e4SLinus Torvalds } 33311da177e4SLinus Torvalds 33325590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname) 33331da177e4SLinus Torvalds { 33341da177e4SLinus Torvalds int error = 0; 333591a27b2aSJeff Layton struct filename *name; 33361da177e4SLinus Torvalds struct dentry *dentry; 33371da177e4SLinus Torvalds struct nameidata nd; 3338c6ee9206SJeff Layton unsigned int lookup_flags = 0; 3339c6ee9206SJeff Layton retry: 3340c6ee9206SJeff Layton name = user_path_parent(dfd, pathname, &nd, lookup_flags); 334191a27b2aSJeff Layton if (IS_ERR(name)) 334291a27b2aSJeff Layton return PTR_ERR(name); 33431da177e4SLinus Torvalds 33441da177e4SLinus Torvalds switch(nd.last_type) { 33451da177e4SLinus Torvalds case LAST_DOTDOT: 33461da177e4SLinus Torvalds error = -ENOTEMPTY; 33471da177e4SLinus Torvalds goto exit1; 33481da177e4SLinus Torvalds case LAST_DOT: 33491da177e4SLinus Torvalds error = -EINVAL; 33501da177e4SLinus Torvalds goto exit1; 33511da177e4SLinus Torvalds case LAST_ROOT: 33521da177e4SLinus Torvalds error = -EBUSY; 33531da177e4SLinus Torvalds goto exit1; 33541da177e4SLinus Torvalds } 33550612d9fbSOGAWA Hirofumi 33560612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 3357c30dabfeSJan Kara error = mnt_want_write(nd.path.mnt); 3358c30dabfeSJan Kara if (error) 3359c30dabfeSJan Kara goto exit1; 33600612d9fbSOGAWA Hirofumi 33614ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 336249705b77SChristoph Hellwig dentry = lookup_hash(&nd); 33631da177e4SLinus Torvalds error = PTR_ERR(dentry); 33646902d925SDave Hansen if (IS_ERR(dentry)) 33656902d925SDave Hansen goto exit2; 3366e6bc45d6STheodore Ts'o if (!dentry->d_inode) { 3367e6bc45d6STheodore Ts'o error = -ENOENT; 3368e6bc45d6STheodore Ts'o goto exit3; 3369e6bc45d6STheodore Ts'o } 3370be6d3e56SKentaro Takeda error = security_path_rmdir(&nd.path, dentry); 3371be6d3e56SKentaro Takeda if (error) 3372c30dabfeSJan Kara goto exit3; 33734ac91378SJan Blunck error = vfs_rmdir(nd.path.dentry->d_inode, dentry); 33740622753bSDave Hansen exit3: 33751da177e4SLinus Torvalds dput(dentry); 33766902d925SDave Hansen exit2: 33774ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 3378c30dabfeSJan Kara mnt_drop_write(nd.path.mnt); 33791da177e4SLinus Torvalds exit1: 33801d957f9bSJan Blunck path_put(&nd.path); 33811da177e4SLinus Torvalds putname(name); 3382c6ee9206SJeff Layton if (retry_estale(error, lookup_flags)) { 3383c6ee9206SJeff Layton lookup_flags |= LOOKUP_REVAL; 3384c6ee9206SJeff Layton goto retry; 3385c6ee9206SJeff Layton } 33861da177e4SLinus Torvalds return error; 33871da177e4SLinus Torvalds } 33881da177e4SLinus Torvalds 33893cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname) 33905590ff0dSUlrich Drepper { 33915590ff0dSUlrich Drepper return do_rmdir(AT_FDCWD, pathname); 33925590ff0dSUlrich Drepper } 33935590ff0dSUlrich Drepper 33941da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry) 33951da177e4SLinus Torvalds { 33961da177e4SLinus Torvalds int error = may_delete(dir, dentry, 0); 33971da177e4SLinus Torvalds 33981da177e4SLinus Torvalds if (error) 33991da177e4SLinus Torvalds return error; 34001da177e4SLinus Torvalds 3401acfa4380SAl Viro if (!dir->i_op->unlink) 34021da177e4SLinus Torvalds return -EPERM; 34031da177e4SLinus Torvalds 34041b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 34051da177e4SLinus Torvalds if (d_mountpoint(dentry)) 34061da177e4SLinus Torvalds error = -EBUSY; 34071da177e4SLinus Torvalds else { 34081da177e4SLinus Torvalds error = security_inode_unlink(dir, dentry); 3409bec1052eSAl Viro if (!error) { 34101da177e4SLinus Torvalds error = dir->i_op->unlink(dir, dentry); 3411bec1052eSAl Viro if (!error) 3412d83c49f3SAl Viro dont_mount(dentry); 3413bec1052eSAl Viro } 34141da177e4SLinus Torvalds } 34151b1dcc1bSJes Sorensen mutex_unlock(&dentry->d_inode->i_mutex); 34161da177e4SLinus Torvalds 34171da177e4SLinus Torvalds /* We don't d_delete() NFS sillyrenamed files--they still exist. */ 34181da177e4SLinus Torvalds if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) { 3419ece95912SJan Kara fsnotify_link_count(dentry->d_inode); 34201da177e4SLinus Torvalds d_delete(dentry); 34211da177e4SLinus Torvalds } 34220eeca283SRobert Love 34231da177e4SLinus Torvalds return error; 34241da177e4SLinus Torvalds } 34251da177e4SLinus Torvalds 34261da177e4SLinus Torvalds /* 34271da177e4SLinus Torvalds * Make sure that the actual truncation of the file will occur outside its 34281b1dcc1bSJes Sorensen * directory's i_mutex. Truncate can take a long time if there is a lot of 34291da177e4SLinus Torvalds * writeout happening, and we don't want to prevent access to the directory 34301da177e4SLinus Torvalds * while waiting on the I/O. 34311da177e4SLinus Torvalds */ 34325590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname) 34331da177e4SLinus Torvalds { 34342ad94ae6SAl Viro int error; 343591a27b2aSJeff Layton struct filename *name; 34361da177e4SLinus Torvalds struct dentry *dentry; 34371da177e4SLinus Torvalds struct nameidata nd; 34381da177e4SLinus Torvalds struct inode *inode = NULL; 34395d18f813SJeff Layton unsigned int lookup_flags = 0; 34405d18f813SJeff Layton retry: 34415d18f813SJeff Layton name = user_path_parent(dfd, pathname, &nd, lookup_flags); 344291a27b2aSJeff Layton if (IS_ERR(name)) 344391a27b2aSJeff Layton return PTR_ERR(name); 34442ad94ae6SAl Viro 34451da177e4SLinus Torvalds error = -EISDIR; 34461da177e4SLinus Torvalds if (nd.last_type != LAST_NORM) 34471da177e4SLinus Torvalds goto exit1; 34480612d9fbSOGAWA Hirofumi 34490612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 3450c30dabfeSJan Kara error = mnt_want_write(nd.path.mnt); 3451c30dabfeSJan Kara if (error) 3452c30dabfeSJan Kara goto exit1; 34530612d9fbSOGAWA Hirofumi 34544ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 345549705b77SChristoph Hellwig dentry = lookup_hash(&nd); 34561da177e4SLinus Torvalds error = PTR_ERR(dentry); 34571da177e4SLinus Torvalds if (!IS_ERR(dentry)) { 34581da177e4SLinus Torvalds /* Why not before? Because we want correct error value */ 345950338b88STörök Edwin if (nd.last.name[nd.last.len]) 346050338b88STörök Edwin goto slashes; 34611da177e4SLinus Torvalds inode = dentry->d_inode; 346250338b88STörök Edwin if (!inode) 3463e6bc45d6STheodore Ts'o goto slashes; 34647de9c6eeSAl Viro ihold(inode); 3465be6d3e56SKentaro Takeda error = security_path_unlink(&nd.path, dentry); 3466be6d3e56SKentaro Takeda if (error) 3467c30dabfeSJan Kara goto exit2; 34684ac91378SJan Blunck error = vfs_unlink(nd.path.dentry->d_inode, dentry); 34691da177e4SLinus Torvalds exit2: 34701da177e4SLinus Torvalds dput(dentry); 34711da177e4SLinus Torvalds } 34724ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 34731da177e4SLinus Torvalds if (inode) 34741da177e4SLinus Torvalds iput(inode); /* truncate the inode here */ 3475c30dabfeSJan Kara mnt_drop_write(nd.path.mnt); 34761da177e4SLinus Torvalds exit1: 34771d957f9bSJan Blunck path_put(&nd.path); 34781da177e4SLinus Torvalds putname(name); 34795d18f813SJeff Layton if (retry_estale(error, lookup_flags)) { 34805d18f813SJeff Layton lookup_flags |= LOOKUP_REVAL; 34815d18f813SJeff Layton inode = NULL; 34825d18f813SJeff Layton goto retry; 34835d18f813SJeff Layton } 34841da177e4SLinus Torvalds return error; 34851da177e4SLinus Torvalds 34861da177e4SLinus Torvalds slashes: 34871da177e4SLinus Torvalds error = !dentry->d_inode ? -ENOENT : 34881da177e4SLinus Torvalds S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR; 34891da177e4SLinus Torvalds goto exit2; 34901da177e4SLinus Torvalds } 34911da177e4SLinus Torvalds 34922e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag) 34935590ff0dSUlrich Drepper { 34945590ff0dSUlrich Drepper if ((flag & ~AT_REMOVEDIR) != 0) 34955590ff0dSUlrich Drepper return -EINVAL; 34965590ff0dSUlrich Drepper 34975590ff0dSUlrich Drepper if (flag & AT_REMOVEDIR) 34985590ff0dSUlrich Drepper return do_rmdir(dfd, pathname); 34995590ff0dSUlrich Drepper 35005590ff0dSUlrich Drepper return do_unlinkat(dfd, pathname); 35015590ff0dSUlrich Drepper } 35025590ff0dSUlrich Drepper 35033480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname) 35045590ff0dSUlrich Drepper { 35055590ff0dSUlrich Drepper return do_unlinkat(AT_FDCWD, pathname); 35065590ff0dSUlrich Drepper } 35075590ff0dSUlrich Drepper 3508db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname) 35091da177e4SLinus Torvalds { 3510a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 35111da177e4SLinus Torvalds 35121da177e4SLinus Torvalds if (error) 35131da177e4SLinus Torvalds return error; 35141da177e4SLinus Torvalds 3515acfa4380SAl Viro if (!dir->i_op->symlink) 35161da177e4SLinus Torvalds return -EPERM; 35171da177e4SLinus Torvalds 35181da177e4SLinus Torvalds error = security_inode_symlink(dir, dentry, oldname); 35191da177e4SLinus Torvalds if (error) 35201da177e4SLinus Torvalds return error; 35211da177e4SLinus Torvalds 35221da177e4SLinus Torvalds error = dir->i_op->symlink(dir, dentry, oldname); 3523a74574aaSStephen Smalley if (!error) 3524f38aa942SAmy Griffis fsnotify_create(dir, dentry); 35251da177e4SLinus Torvalds return error; 35261da177e4SLinus Torvalds } 35271da177e4SLinus Torvalds 35282e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname, 35292e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 35301da177e4SLinus Torvalds { 35312ad94ae6SAl Viro int error; 353291a27b2aSJeff Layton struct filename *from; 35336902d925SDave Hansen struct dentry *dentry; 3534dae6ad8fSAl Viro struct path path; 3535f46d3567SJeff Layton unsigned int lookup_flags = 0; 35361da177e4SLinus Torvalds 35371da177e4SLinus Torvalds from = getname(oldname); 35381da177e4SLinus Torvalds if (IS_ERR(from)) 35391da177e4SLinus Torvalds return PTR_ERR(from); 3540f46d3567SJeff Layton retry: 3541f46d3567SJeff Layton dentry = user_path_create(newdfd, newname, &path, lookup_flags); 35421da177e4SLinus Torvalds error = PTR_ERR(dentry); 35436902d925SDave Hansen if (IS_ERR(dentry)) 3544dae6ad8fSAl Viro goto out_putname; 35456902d925SDave Hansen 354691a27b2aSJeff Layton error = security_path_symlink(&path, dentry, from->name); 3547a8104a9fSAl Viro if (!error) 354891a27b2aSJeff Layton error = vfs_symlink(path.dentry->d_inode, dentry, from->name); 3549921a1650SAl Viro done_path_create(&path, dentry); 3550f46d3567SJeff Layton if (retry_estale(error, lookup_flags)) { 3551f46d3567SJeff Layton lookup_flags |= LOOKUP_REVAL; 3552f46d3567SJeff Layton goto retry; 3553f46d3567SJeff Layton } 35546902d925SDave Hansen out_putname: 35551da177e4SLinus Torvalds putname(from); 35561da177e4SLinus Torvalds return error; 35571da177e4SLinus Torvalds } 35581da177e4SLinus Torvalds 35593480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname) 35605590ff0dSUlrich Drepper { 35615590ff0dSUlrich Drepper return sys_symlinkat(oldname, AT_FDCWD, newname); 35625590ff0dSUlrich Drepper } 35635590ff0dSUlrich Drepper 35641da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) 35651da177e4SLinus Torvalds { 35661da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 35678de52778SAl Viro unsigned max_links = dir->i_sb->s_max_links; 35681da177e4SLinus Torvalds int error; 35691da177e4SLinus Torvalds 35701da177e4SLinus Torvalds if (!inode) 35711da177e4SLinus Torvalds return -ENOENT; 35721da177e4SLinus Torvalds 3573a95164d9SMiklos Szeredi error = may_create(dir, new_dentry); 35741da177e4SLinus Torvalds if (error) 35751da177e4SLinus Torvalds return error; 35761da177e4SLinus Torvalds 35771da177e4SLinus Torvalds if (dir->i_sb != inode->i_sb) 35781da177e4SLinus Torvalds return -EXDEV; 35791da177e4SLinus Torvalds 35801da177e4SLinus Torvalds /* 35811da177e4SLinus Torvalds * A link to an append-only or immutable file cannot be created. 35821da177e4SLinus Torvalds */ 35831da177e4SLinus Torvalds if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) 35841da177e4SLinus Torvalds return -EPERM; 3585acfa4380SAl Viro if (!dir->i_op->link) 35861da177e4SLinus Torvalds return -EPERM; 35877e79eedbSTetsuo Handa if (S_ISDIR(inode->i_mode)) 35881da177e4SLinus Torvalds return -EPERM; 35891da177e4SLinus Torvalds 35901da177e4SLinus Torvalds error = security_inode_link(old_dentry, dir, new_dentry); 35911da177e4SLinus Torvalds if (error) 35921da177e4SLinus Torvalds return error; 35931da177e4SLinus Torvalds 35947e79eedbSTetsuo Handa mutex_lock(&inode->i_mutex); 3595aae8a97dSAneesh Kumar K.V /* Make sure we don't allow creating hardlink to an unlinked file */ 3596aae8a97dSAneesh Kumar K.V if (inode->i_nlink == 0) 3597aae8a97dSAneesh Kumar K.V error = -ENOENT; 35988de52778SAl Viro else if (max_links && inode->i_nlink >= max_links) 35998de52778SAl Viro error = -EMLINK; 3600aae8a97dSAneesh Kumar K.V else 36011da177e4SLinus Torvalds error = dir->i_op->link(old_dentry, dir, new_dentry); 36027e79eedbSTetsuo Handa mutex_unlock(&inode->i_mutex); 3603e31e14ecSStephen Smalley if (!error) 36047e79eedbSTetsuo Handa fsnotify_link(dir, inode, new_dentry); 36051da177e4SLinus Torvalds return error; 36061da177e4SLinus Torvalds } 36071da177e4SLinus Torvalds 36081da177e4SLinus Torvalds /* 36091da177e4SLinus Torvalds * Hardlinks are often used in delicate situations. We avoid 36101da177e4SLinus Torvalds * security-related surprises by not following symlinks on the 36111da177e4SLinus Torvalds * newname. --KAB 36121da177e4SLinus Torvalds * 36131da177e4SLinus Torvalds * We don't follow them on the oldname either to be compatible 36141da177e4SLinus Torvalds * with linux 2.0, and to avoid hard-linking to directories 36151da177e4SLinus Torvalds * and other special files. --ADM 36161da177e4SLinus Torvalds */ 36172e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname, 36182e4d0924SHeiko Carstens int, newdfd, const char __user *, newname, int, flags) 36191da177e4SLinus Torvalds { 36201da177e4SLinus Torvalds struct dentry *new_dentry; 3621dae6ad8fSAl Viro struct path old_path, new_path; 362211a7b371SAneesh Kumar K.V int how = 0; 36231da177e4SLinus Torvalds int error; 36241da177e4SLinus Torvalds 362511a7b371SAneesh Kumar K.V if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0) 3626c04030e1SUlrich Drepper return -EINVAL; 362711a7b371SAneesh Kumar K.V /* 362811a7b371SAneesh Kumar K.V * To use null names we require CAP_DAC_READ_SEARCH 362911a7b371SAneesh Kumar K.V * This ensures that not everyone will be able to create 363011a7b371SAneesh Kumar K.V * handlink using the passed filedescriptor. 363111a7b371SAneesh Kumar K.V */ 363211a7b371SAneesh Kumar K.V if (flags & AT_EMPTY_PATH) { 363311a7b371SAneesh Kumar K.V if (!capable(CAP_DAC_READ_SEARCH)) 363411a7b371SAneesh Kumar K.V return -ENOENT; 363511a7b371SAneesh Kumar K.V how = LOOKUP_EMPTY; 363611a7b371SAneesh Kumar K.V } 3637c04030e1SUlrich Drepper 363811a7b371SAneesh Kumar K.V if (flags & AT_SYMLINK_FOLLOW) 363911a7b371SAneesh Kumar K.V how |= LOOKUP_FOLLOW; 3640442e31caSJeff Layton retry: 364111a7b371SAneesh Kumar K.V error = user_path_at(olddfd, oldname, how, &old_path); 36421da177e4SLinus Torvalds if (error) 36432ad94ae6SAl Viro return error; 36442ad94ae6SAl Viro 3645442e31caSJeff Layton new_dentry = user_path_create(newdfd, newname, &new_path, 3646442e31caSJeff Layton (how & LOOKUP_REVAL)); 36471da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 36486902d925SDave Hansen if (IS_ERR(new_dentry)) 3649dae6ad8fSAl Viro goto out; 3650dae6ad8fSAl Viro 3651dae6ad8fSAl Viro error = -EXDEV; 3652dae6ad8fSAl Viro if (old_path.mnt != new_path.mnt) 3653dae6ad8fSAl Viro goto out_dput; 3654800179c9SKees Cook error = may_linkat(&old_path); 3655800179c9SKees Cook if (unlikely(error)) 3656800179c9SKees Cook goto out_dput; 3657dae6ad8fSAl Viro error = security_path_link(old_path.dentry, &new_path, new_dentry); 3658be6d3e56SKentaro Takeda if (error) 3659a8104a9fSAl Viro goto out_dput; 3660dae6ad8fSAl Viro error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry); 366175c3f29dSDave Hansen out_dput: 3662921a1650SAl Viro done_path_create(&new_path, new_dentry); 3663442e31caSJeff Layton if (retry_estale(error, how)) { 3664442e31caSJeff Layton how |= LOOKUP_REVAL; 3665442e31caSJeff Layton goto retry; 3666442e31caSJeff Layton } 36671da177e4SLinus Torvalds out: 36682d8f3038SAl Viro path_put(&old_path); 36691da177e4SLinus Torvalds 36701da177e4SLinus Torvalds return error; 36711da177e4SLinus Torvalds } 36721da177e4SLinus Torvalds 36733480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname) 36745590ff0dSUlrich Drepper { 3675c04030e1SUlrich Drepper return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0); 36765590ff0dSUlrich Drepper } 36775590ff0dSUlrich Drepper 36781da177e4SLinus Torvalds /* 36791da177e4SLinus Torvalds * The worst of all namespace operations - renaming directory. "Perverted" 36801da177e4SLinus Torvalds * doesn't even start to describe it. Somebody in UCB had a heck of a trip... 36811da177e4SLinus Torvalds * Problems: 36821da177e4SLinus Torvalds * a) we can get into loop creation. Check is done in is_subdir(). 36831da177e4SLinus Torvalds * b) race potential - two innocent renames can create a loop together. 36841da177e4SLinus Torvalds * That's where 4.4 screws up. Current fix: serialization on 3685a11f3a05SArjan van de Ven * sb->s_vfs_rename_mutex. We might be more accurate, but that's another 36861da177e4SLinus Torvalds * story. 36871da177e4SLinus Torvalds * c) we have to lock _three_ objects - parents and victim (if it exists). 36881b1dcc1bSJes Sorensen * And that - after we got ->i_mutex on parents (until then we don't know 36891da177e4SLinus Torvalds * whether the target exists). Solution: try to be smart with locking 36901da177e4SLinus Torvalds * order for inodes. We rely on the fact that tree topology may change 3691a11f3a05SArjan van de Ven * only under ->s_vfs_rename_mutex _and_ that parent of the object we 36921da177e4SLinus Torvalds * move will be locked. Thus we can rank directories by the tree 36931da177e4SLinus Torvalds * (ancestors first) and rank all non-directories after them. 36941da177e4SLinus Torvalds * That works since everybody except rename does "lock parent, lookup, 3695a11f3a05SArjan van de Ven * lock child" and rename is under ->s_vfs_rename_mutex. 36961da177e4SLinus Torvalds * HOWEVER, it relies on the assumption that any object with ->lookup() 36971da177e4SLinus Torvalds * has no more than 1 dentry. If "hybrid" objects will ever appear, 36981da177e4SLinus Torvalds * we'd better make sure that there's no link(2) for them. 3699e4eaac06SSage Weil * d) conversion from fhandle to dentry may come in the wrong moment - when 37001b1dcc1bSJes Sorensen * we are removing the target. Solution: we will have to grab ->i_mutex 37011da177e4SLinus Torvalds * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on 3702c41b20e7SAdam Buchbinder * ->i_mutex on parents, which works but leads to some truly excessive 37031da177e4SLinus Torvalds * locking]. 37041da177e4SLinus Torvalds */ 370575c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry, 37061da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 37071da177e4SLinus Torvalds { 37081da177e4SLinus Torvalds int error = 0; 37099055cba7SSage Weil struct inode *target = new_dentry->d_inode; 37108de52778SAl Viro unsigned max_links = new_dir->i_sb->s_max_links; 37111da177e4SLinus Torvalds 37121da177e4SLinus Torvalds /* 37131da177e4SLinus Torvalds * If we are going to change the parent - check write permissions, 37141da177e4SLinus Torvalds * we'll need to flip '..'. 37151da177e4SLinus Torvalds */ 37161da177e4SLinus Torvalds if (new_dir != old_dir) { 3717f419a2e3SAl Viro error = inode_permission(old_dentry->d_inode, MAY_WRITE); 37181da177e4SLinus Torvalds if (error) 37191da177e4SLinus Torvalds return error; 37201da177e4SLinus Torvalds } 37211da177e4SLinus Torvalds 37221da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 37231da177e4SLinus Torvalds if (error) 37241da177e4SLinus Torvalds return error; 37251da177e4SLinus Torvalds 37261d2ef590SAl Viro dget(new_dentry); 3727d83c49f3SAl Viro if (target) 37281b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 37299055cba7SSage Weil 37301da177e4SLinus Torvalds error = -EBUSY; 37319055cba7SSage Weil if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry)) 37329055cba7SSage Weil goto out; 37339055cba7SSage Weil 37348de52778SAl Viro error = -EMLINK; 37358de52778SAl Viro if (max_links && !target && new_dir != old_dir && 37368de52778SAl Viro new_dir->i_nlink >= max_links) 37378de52778SAl Viro goto out; 37388de52778SAl Viro 37393cebde24SSage Weil if (target) 37403cebde24SSage Weil shrink_dcache_parent(new_dentry); 37411da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 37429055cba7SSage Weil if (error) 37439055cba7SSage Weil goto out; 37449055cba7SSage Weil 37451da177e4SLinus Torvalds if (target) { 37461da177e4SLinus Torvalds target->i_flags |= S_DEAD; 3747d83c49f3SAl Viro dont_mount(new_dentry); 3748d83c49f3SAl Viro } 37499055cba7SSage Weil out: 37509055cba7SSage Weil if (target) 37511b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 37521d2ef590SAl Viro dput(new_dentry); 3753e31e14ecSStephen Smalley if (!error) 3754349457ccSMark Fasheh if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) 37551da177e4SLinus Torvalds d_move(old_dentry,new_dentry); 37561da177e4SLinus Torvalds return error; 37571da177e4SLinus Torvalds } 37581da177e4SLinus Torvalds 375975c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry, 37601da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 37611da177e4SLinus Torvalds { 376251892bbbSSage Weil struct inode *target = new_dentry->d_inode; 37631da177e4SLinus Torvalds int error; 37641da177e4SLinus Torvalds 37651da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 37661da177e4SLinus Torvalds if (error) 37671da177e4SLinus Torvalds return error; 37681da177e4SLinus Torvalds 37691da177e4SLinus Torvalds dget(new_dentry); 37701da177e4SLinus Torvalds if (target) 37711b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 377251892bbbSSage Weil 37731da177e4SLinus Torvalds error = -EBUSY; 377451892bbbSSage Weil if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry)) 377551892bbbSSage Weil goto out; 377651892bbbSSage Weil 37771da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 377851892bbbSSage Weil if (error) 377951892bbbSSage Weil goto out; 378051892bbbSSage Weil 3781bec1052eSAl Viro if (target) 3782d83c49f3SAl Viro dont_mount(new_dentry); 3783349457ccSMark Fasheh if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) 37841da177e4SLinus Torvalds d_move(old_dentry, new_dentry); 378551892bbbSSage Weil out: 37861da177e4SLinus Torvalds if (target) 37871b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 37881da177e4SLinus Torvalds dput(new_dentry); 37891da177e4SLinus Torvalds return error; 37901da177e4SLinus Torvalds } 37911da177e4SLinus Torvalds 37921da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry, 37931da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 37941da177e4SLinus Torvalds { 37951da177e4SLinus Torvalds int error; 37961da177e4SLinus Torvalds int is_dir = S_ISDIR(old_dentry->d_inode->i_mode); 379759b0df21SEric Paris const unsigned char *old_name; 37981da177e4SLinus Torvalds 37991da177e4SLinus Torvalds if (old_dentry->d_inode == new_dentry->d_inode) 38001da177e4SLinus Torvalds return 0; 38011da177e4SLinus Torvalds 38021da177e4SLinus Torvalds error = may_delete(old_dir, old_dentry, is_dir); 38031da177e4SLinus Torvalds if (error) 38041da177e4SLinus Torvalds return error; 38051da177e4SLinus Torvalds 38061da177e4SLinus Torvalds if (!new_dentry->d_inode) 3807a95164d9SMiklos Szeredi error = may_create(new_dir, new_dentry); 38081da177e4SLinus Torvalds else 38091da177e4SLinus Torvalds error = may_delete(new_dir, new_dentry, is_dir); 38101da177e4SLinus Torvalds if (error) 38111da177e4SLinus Torvalds return error; 38121da177e4SLinus Torvalds 3813acfa4380SAl Viro if (!old_dir->i_op->rename) 38141da177e4SLinus Torvalds return -EPERM; 38151da177e4SLinus Torvalds 38160eeca283SRobert Love old_name = fsnotify_oldname_init(old_dentry->d_name.name); 38170eeca283SRobert Love 38181da177e4SLinus Torvalds if (is_dir) 38191da177e4SLinus Torvalds error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry); 38201da177e4SLinus Torvalds else 38211da177e4SLinus Torvalds error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry); 3822123df294SAl Viro if (!error) 3823123df294SAl Viro fsnotify_move(old_dir, new_dir, old_name, is_dir, 38245a190ae6SAl Viro new_dentry->d_inode, old_dentry); 38250eeca283SRobert Love fsnotify_oldname_free(old_name); 38260eeca283SRobert Love 38271da177e4SLinus Torvalds return error; 38281da177e4SLinus Torvalds } 38291da177e4SLinus Torvalds 38302e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname, 38312e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 38321da177e4SLinus Torvalds { 38331da177e4SLinus Torvalds struct dentry *old_dir, *new_dir; 38341da177e4SLinus Torvalds struct dentry *old_dentry, *new_dentry; 38351da177e4SLinus Torvalds struct dentry *trap; 38361da177e4SLinus Torvalds struct nameidata oldnd, newnd; 383791a27b2aSJeff Layton struct filename *from; 383891a27b2aSJeff Layton struct filename *to; 3839c6a94284SJeff Layton unsigned int lookup_flags = 0; 3840c6a94284SJeff Layton bool should_retry = false; 38412ad94ae6SAl Viro int error; 3842c6a94284SJeff Layton retry: 3843c6a94284SJeff Layton from = user_path_parent(olddfd, oldname, &oldnd, lookup_flags); 384491a27b2aSJeff Layton if (IS_ERR(from)) { 384591a27b2aSJeff Layton error = PTR_ERR(from); 38461da177e4SLinus Torvalds goto exit; 384791a27b2aSJeff Layton } 38481da177e4SLinus Torvalds 3849c6a94284SJeff Layton to = user_path_parent(newdfd, newname, &newnd, lookup_flags); 385091a27b2aSJeff Layton if (IS_ERR(to)) { 385191a27b2aSJeff Layton error = PTR_ERR(to); 38521da177e4SLinus Torvalds goto exit1; 385391a27b2aSJeff Layton } 38541da177e4SLinus Torvalds 38551da177e4SLinus Torvalds error = -EXDEV; 38564ac91378SJan Blunck if (oldnd.path.mnt != newnd.path.mnt) 38571da177e4SLinus Torvalds goto exit2; 38581da177e4SLinus Torvalds 38594ac91378SJan Blunck old_dir = oldnd.path.dentry; 38601da177e4SLinus Torvalds error = -EBUSY; 38611da177e4SLinus Torvalds if (oldnd.last_type != LAST_NORM) 38621da177e4SLinus Torvalds goto exit2; 38631da177e4SLinus Torvalds 38644ac91378SJan Blunck new_dir = newnd.path.dentry; 38651da177e4SLinus Torvalds if (newnd.last_type != LAST_NORM) 38661da177e4SLinus Torvalds goto exit2; 38671da177e4SLinus Torvalds 3868c30dabfeSJan Kara error = mnt_want_write(oldnd.path.mnt); 3869c30dabfeSJan Kara if (error) 3870c30dabfeSJan Kara goto exit2; 3871c30dabfeSJan Kara 38720612d9fbSOGAWA Hirofumi oldnd.flags &= ~LOOKUP_PARENT; 38730612d9fbSOGAWA Hirofumi newnd.flags &= ~LOOKUP_PARENT; 38744e9ed2f8SOGAWA Hirofumi newnd.flags |= LOOKUP_RENAME_TARGET; 38750612d9fbSOGAWA Hirofumi 38761da177e4SLinus Torvalds trap = lock_rename(new_dir, old_dir); 38771da177e4SLinus Torvalds 387849705b77SChristoph Hellwig old_dentry = lookup_hash(&oldnd); 38791da177e4SLinus Torvalds error = PTR_ERR(old_dentry); 38801da177e4SLinus Torvalds if (IS_ERR(old_dentry)) 38811da177e4SLinus Torvalds goto exit3; 38821da177e4SLinus Torvalds /* source must exist */ 38831da177e4SLinus Torvalds error = -ENOENT; 38841da177e4SLinus Torvalds if (!old_dentry->d_inode) 38851da177e4SLinus Torvalds goto exit4; 38861da177e4SLinus Torvalds /* unless the source is a directory trailing slashes give -ENOTDIR */ 38871da177e4SLinus Torvalds if (!S_ISDIR(old_dentry->d_inode->i_mode)) { 38881da177e4SLinus Torvalds error = -ENOTDIR; 38891da177e4SLinus Torvalds if (oldnd.last.name[oldnd.last.len]) 38901da177e4SLinus Torvalds goto exit4; 38911da177e4SLinus Torvalds if (newnd.last.name[newnd.last.len]) 38921da177e4SLinus Torvalds goto exit4; 38931da177e4SLinus Torvalds } 38941da177e4SLinus Torvalds /* source should not be ancestor of target */ 38951da177e4SLinus Torvalds error = -EINVAL; 38961da177e4SLinus Torvalds if (old_dentry == trap) 38971da177e4SLinus Torvalds goto exit4; 389849705b77SChristoph Hellwig new_dentry = lookup_hash(&newnd); 38991da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 39001da177e4SLinus Torvalds if (IS_ERR(new_dentry)) 39011da177e4SLinus Torvalds goto exit4; 39021da177e4SLinus Torvalds /* target should not be an ancestor of source */ 39031da177e4SLinus Torvalds error = -ENOTEMPTY; 39041da177e4SLinus Torvalds if (new_dentry == trap) 39051da177e4SLinus Torvalds goto exit5; 39061da177e4SLinus Torvalds 3907be6d3e56SKentaro Takeda error = security_path_rename(&oldnd.path, old_dentry, 3908be6d3e56SKentaro Takeda &newnd.path, new_dentry); 3909be6d3e56SKentaro Takeda if (error) 3910c30dabfeSJan Kara goto exit5; 39111da177e4SLinus Torvalds error = vfs_rename(old_dir->d_inode, old_dentry, 39121da177e4SLinus Torvalds new_dir->d_inode, new_dentry); 39131da177e4SLinus Torvalds exit5: 39141da177e4SLinus Torvalds dput(new_dentry); 39151da177e4SLinus Torvalds exit4: 39161da177e4SLinus Torvalds dput(old_dentry); 39171da177e4SLinus Torvalds exit3: 39181da177e4SLinus Torvalds unlock_rename(new_dir, old_dir); 3919c30dabfeSJan Kara mnt_drop_write(oldnd.path.mnt); 39201da177e4SLinus Torvalds exit2: 3921c6a94284SJeff Layton if (retry_estale(error, lookup_flags)) 3922c6a94284SJeff Layton should_retry = true; 39231d957f9bSJan Blunck path_put(&newnd.path); 39242ad94ae6SAl Viro putname(to); 39251da177e4SLinus Torvalds exit1: 39261d957f9bSJan Blunck path_put(&oldnd.path); 39271da177e4SLinus Torvalds putname(from); 3928c6a94284SJeff Layton if (should_retry) { 3929c6a94284SJeff Layton should_retry = false; 3930c6a94284SJeff Layton lookup_flags |= LOOKUP_REVAL; 3931c6a94284SJeff Layton goto retry; 3932c6a94284SJeff Layton } 39332ad94ae6SAl Viro exit: 39341da177e4SLinus Torvalds return error; 39351da177e4SLinus Torvalds } 39361da177e4SLinus Torvalds 3937a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname) 39385590ff0dSUlrich Drepper { 39395590ff0dSUlrich Drepper return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname); 39405590ff0dSUlrich Drepper } 39415590ff0dSUlrich Drepper 39421da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link) 39431da177e4SLinus Torvalds { 39441da177e4SLinus Torvalds int len; 39451da177e4SLinus Torvalds 39461da177e4SLinus Torvalds len = PTR_ERR(link); 39471da177e4SLinus Torvalds if (IS_ERR(link)) 39481da177e4SLinus Torvalds goto out; 39491da177e4SLinus Torvalds 39501da177e4SLinus Torvalds len = strlen(link); 39511da177e4SLinus Torvalds if (len > (unsigned) buflen) 39521da177e4SLinus Torvalds len = buflen; 39531da177e4SLinus Torvalds if (copy_to_user(buffer, link, len)) 39541da177e4SLinus Torvalds len = -EFAULT; 39551da177e4SLinus Torvalds out: 39561da177e4SLinus Torvalds return len; 39571da177e4SLinus Torvalds } 39581da177e4SLinus Torvalds 39591da177e4SLinus Torvalds /* 39601da177e4SLinus Torvalds * A helper for ->readlink(). This should be used *ONLY* for symlinks that 39611da177e4SLinus Torvalds * have ->follow_link() touching nd only in nd_set_link(). Using (or not 39621da177e4SLinus Torvalds * using) it for any given inode is up to filesystem. 39631da177e4SLinus Torvalds */ 39641da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen) 39651da177e4SLinus Torvalds { 39661da177e4SLinus Torvalds struct nameidata nd; 3967cc314eefSLinus Torvalds void *cookie; 3968694a1764SMarcin Slusarz int res; 3969cc314eefSLinus Torvalds 39701da177e4SLinus Torvalds nd.depth = 0; 3971cc314eefSLinus Torvalds cookie = dentry->d_inode->i_op->follow_link(dentry, &nd); 3972694a1764SMarcin Slusarz if (IS_ERR(cookie)) 3973694a1764SMarcin Slusarz return PTR_ERR(cookie); 3974694a1764SMarcin Slusarz 3975694a1764SMarcin Slusarz res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd)); 39761da177e4SLinus Torvalds if (dentry->d_inode->i_op->put_link) 3977cc314eefSLinus Torvalds dentry->d_inode->i_op->put_link(dentry, &nd, cookie); 3978694a1764SMarcin Slusarz return res; 39791da177e4SLinus Torvalds } 39801da177e4SLinus Torvalds 39811da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link) 39821da177e4SLinus Torvalds { 39831da177e4SLinus Torvalds return __vfs_follow_link(nd, link); 39841da177e4SLinus Torvalds } 39851da177e4SLinus Torvalds 39861da177e4SLinus Torvalds /* get the link contents into pagecache */ 39871da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage) 39881da177e4SLinus Torvalds { 3989ebd09abbSDuane Griffin char *kaddr; 39901da177e4SLinus Torvalds struct page *page; 39911da177e4SLinus Torvalds struct address_space *mapping = dentry->d_inode->i_mapping; 3992090d2b18SPekka Enberg page = read_mapping_page(mapping, 0, NULL); 39931da177e4SLinus Torvalds if (IS_ERR(page)) 39946fe6900eSNick Piggin return (char*)page; 39951da177e4SLinus Torvalds *ppage = page; 3996ebd09abbSDuane Griffin kaddr = kmap(page); 3997ebd09abbSDuane Griffin nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1); 3998ebd09abbSDuane Griffin return kaddr; 39991da177e4SLinus Torvalds } 40001da177e4SLinus Torvalds 40011da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen) 40021da177e4SLinus Torvalds { 40031da177e4SLinus Torvalds struct page *page = NULL; 40041da177e4SLinus Torvalds char *s = page_getlink(dentry, &page); 40051da177e4SLinus Torvalds int res = vfs_readlink(dentry,buffer,buflen,s); 40061da177e4SLinus Torvalds if (page) { 40071da177e4SLinus Torvalds kunmap(page); 40081da177e4SLinus Torvalds page_cache_release(page); 40091da177e4SLinus Torvalds } 40101da177e4SLinus Torvalds return res; 40111da177e4SLinus Torvalds } 40121da177e4SLinus Torvalds 4013cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd) 40141da177e4SLinus Torvalds { 4015cc314eefSLinus Torvalds struct page *page = NULL; 40161da177e4SLinus Torvalds nd_set_link(nd, page_getlink(dentry, &page)); 4017cc314eefSLinus Torvalds return page; 40181da177e4SLinus Torvalds } 40191da177e4SLinus Torvalds 4020cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie) 40211da177e4SLinus Torvalds { 4022cc314eefSLinus Torvalds struct page *page = cookie; 4023cc314eefSLinus Torvalds 4024cc314eefSLinus Torvalds if (page) { 40251da177e4SLinus Torvalds kunmap(page); 40261da177e4SLinus Torvalds page_cache_release(page); 40271da177e4SLinus Torvalds } 40281da177e4SLinus Torvalds } 40291da177e4SLinus Torvalds 403054566b2cSNick Piggin /* 403154566b2cSNick Piggin * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS 403254566b2cSNick Piggin */ 403354566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs) 40341da177e4SLinus Torvalds { 40351da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 40360adb25d2SKirill Korotaev struct page *page; 4037afddba49SNick Piggin void *fsdata; 4038beb497abSDmitriy Monakhov int err; 40391da177e4SLinus Torvalds char *kaddr; 404054566b2cSNick Piggin unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE; 404154566b2cSNick Piggin if (nofs) 404254566b2cSNick Piggin flags |= AOP_FLAG_NOFS; 40431da177e4SLinus Torvalds 40447e53cac4SNeilBrown retry: 4045afddba49SNick Piggin err = pagecache_write_begin(NULL, mapping, 0, len-1, 404654566b2cSNick Piggin flags, &page, &fsdata); 40471da177e4SLinus Torvalds if (err) 4048afddba49SNick Piggin goto fail; 4049afddba49SNick Piggin 4050e8e3c3d6SCong Wang kaddr = kmap_atomic(page); 40511da177e4SLinus Torvalds memcpy(kaddr, symname, len-1); 4052e8e3c3d6SCong Wang kunmap_atomic(kaddr); 4053afddba49SNick Piggin 4054afddba49SNick Piggin err = pagecache_write_end(NULL, mapping, 0, len-1, len-1, 4055afddba49SNick Piggin page, fsdata); 40561da177e4SLinus Torvalds if (err < 0) 40571da177e4SLinus Torvalds goto fail; 4058afddba49SNick Piggin if (err < len-1) 4059afddba49SNick Piggin goto retry; 4060afddba49SNick Piggin 40611da177e4SLinus Torvalds mark_inode_dirty(inode); 40621da177e4SLinus Torvalds return 0; 40631da177e4SLinus Torvalds fail: 40641da177e4SLinus Torvalds return err; 40651da177e4SLinus Torvalds } 40661da177e4SLinus Torvalds 40670adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len) 40680adb25d2SKirill Korotaev { 40690adb25d2SKirill Korotaev return __page_symlink(inode, symname, len, 407054566b2cSNick Piggin !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS)); 40710adb25d2SKirill Korotaev } 40720adb25d2SKirill Korotaev 407392e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = { 40741da177e4SLinus Torvalds .readlink = generic_readlink, 40751da177e4SLinus Torvalds .follow_link = page_follow_link_light, 40761da177e4SLinus Torvalds .put_link = page_put_link, 40771da177e4SLinus Torvalds }; 40781da177e4SLinus Torvalds 40792d8f3038SAl Viro EXPORT_SYMBOL(user_path_at); 4080cc53ce53SDavid Howells EXPORT_SYMBOL(follow_down_one); 40811da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down); 40821da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up); 4083f6d2ac5cSAl Viro EXPORT_SYMBOL(get_write_access); /* nfsd */ 40841da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename); 40851da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len); 40861da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light); 40871da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link); 40881da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink); 40890adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink); 40901da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink); 40911da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations); 4092d1811465SAl Viro EXPORT_SYMBOL(kern_path); 409316f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup); 4094f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission); 40951da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename); 40961da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create); 40971da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link); 40981da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link); 40991da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir); 41001da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod); 41011da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission); 41021da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink); 41031da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename); 41041da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir); 41051da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink); 41061da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink); 41071da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash); 41081da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink); 4109