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 708800179c9SKees Cook int sysctl_protected_symlinks __read_mostly = 1; 709800179c9SKees Cook int sysctl_protected_hardlinks __read_mostly = 1; 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) { 1278bad61189SMiklos Szeredi if (d_need_lookup(dentry)) { 1279bad61189SMiklos Szeredi *need_lookup = true; 1280bad61189SMiklos Szeredi } else if (dentry->d_flags & DCACHE_OP_REVALIDATE) { 1281201f956eSAl Viro error = d_revalidate(dentry, flags); 1282bad61189SMiklos Szeredi if (unlikely(error <= 0)) { 1283bad61189SMiklos Szeredi if (error < 0) { 1284bad61189SMiklos Szeredi dput(dentry); 1285bad61189SMiklos Szeredi return ERR_PTR(error); 1286bad61189SMiklos Szeredi } else if (!d_invalidate(dentry)) { 1287bad61189SMiklos Szeredi dput(dentry); 1288bad61189SMiklos Szeredi dentry = NULL; 1289bad61189SMiklos Szeredi } 1290bad61189SMiklos Szeredi } 1291bad61189SMiklos Szeredi } 1292bad61189SMiklos Szeredi } 1293baa03890SNick Piggin 1294bad61189SMiklos Szeredi if (!dentry) { 1295bad61189SMiklos Szeredi dentry = d_alloc(dir, name); 1296baa03890SNick Piggin if (unlikely(!dentry)) 1297baa03890SNick Piggin return ERR_PTR(-ENOMEM); 1298baa03890SNick Piggin 1299bad61189SMiklos Szeredi *need_lookup = true; 1300baa03890SNick Piggin } 1301baa03890SNick Piggin return dentry; 1302baa03890SNick Piggin } 1303baa03890SNick Piggin 1304baa03890SNick Piggin /* 1305bad61189SMiklos Szeredi * Call i_op->lookup on the dentry. The dentry must be negative but may be 1306bad61189SMiklos Szeredi * hashed if it was pouplated with DCACHE_NEED_LOOKUP. 1307bad61189SMiklos Szeredi * 1308bad61189SMiklos Szeredi * dir->d_inode->i_mutex must be held 130944396f4bSJosef Bacik */ 1310bad61189SMiklos Szeredi static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry, 131172bd866aSAl Viro unsigned int flags) 131244396f4bSJosef Bacik { 131344396f4bSJosef Bacik struct dentry *old; 131444396f4bSJosef Bacik 131544396f4bSJosef Bacik /* Don't create child dentry for a dead directory. */ 1316bad61189SMiklos Szeredi if (unlikely(IS_DEADDIR(dir))) { 1317e188dc02SMiklos Szeredi dput(dentry); 131844396f4bSJosef Bacik return ERR_PTR(-ENOENT); 1319e188dc02SMiklos Szeredi } 132044396f4bSJosef Bacik 132172bd866aSAl Viro old = dir->i_op->lookup(dir, dentry, flags); 132244396f4bSJosef Bacik if (unlikely(old)) { 132344396f4bSJosef Bacik dput(dentry); 132444396f4bSJosef Bacik dentry = old; 132544396f4bSJosef Bacik } 132644396f4bSJosef Bacik return dentry; 132744396f4bSJosef Bacik } 132844396f4bSJosef Bacik 1329a3255546SAl Viro static struct dentry *__lookup_hash(struct qstr *name, 133072bd866aSAl Viro struct dentry *base, unsigned int flags) 1331a3255546SAl Viro { 1332bad61189SMiklos Szeredi bool need_lookup; 1333a3255546SAl Viro struct dentry *dentry; 1334a3255546SAl Viro 133572bd866aSAl Viro dentry = lookup_dcache(name, base, flags, &need_lookup); 1336bad61189SMiklos Szeredi if (!need_lookup) 1337a3255546SAl Viro return dentry; 1338bad61189SMiklos Szeredi 133972bd866aSAl Viro return lookup_real(base->d_inode, dentry, flags); 1340a3255546SAl Viro } 1341a3255546SAl Viro 134244396f4bSJosef Bacik /* 13431da177e4SLinus Torvalds * It's more convoluted than I'd like it to be, but... it's still fairly 13441da177e4SLinus Torvalds * small and for now I'd prefer to have fast path as straight as possible. 13451da177e4SLinus Torvalds * It _is_ time-critical. 13461da177e4SLinus Torvalds */ 1347697f514dSMiklos Szeredi static int lookup_fast(struct nameidata *nd, struct qstr *name, 134831e6b01fSNick Piggin struct path *path, struct inode **inode) 13491da177e4SLinus Torvalds { 13504ac91378SJan Blunck struct vfsmount *mnt = nd->path.mnt; 135131e6b01fSNick Piggin struct dentry *dentry, *parent = nd->path.dentry; 13525a18fff2SAl Viro int need_reval = 1; 13535a18fff2SAl Viro int status = 1; 13549875cf80SDavid Howells int err; 13559875cf80SDavid Howells 13563cac260aSAl Viro /* 1357b04f784eSNick Piggin * Rename seqlock is not required here because in the off chance 1358b04f784eSNick Piggin * of a false negative due to a concurrent rename, we're going to 1359b04f784eSNick Piggin * do the non-racy lookup, below. 1360b04f784eSNick Piggin */ 136131e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) { 136231e6b01fSNick Piggin unsigned seq; 136312f8ad4bSLinus Torvalds dentry = __d_lookup_rcu(parent, name, &seq, nd->inode); 13645a18fff2SAl Viro if (!dentry) 13655a18fff2SAl Viro goto unlazy; 13665a18fff2SAl Viro 136712f8ad4bSLinus Torvalds /* 136812f8ad4bSLinus Torvalds * This sequence count validates that the inode matches 136912f8ad4bSLinus Torvalds * the dentry name information from lookup. 137012f8ad4bSLinus Torvalds */ 137112f8ad4bSLinus Torvalds *inode = dentry->d_inode; 137212f8ad4bSLinus Torvalds if (read_seqcount_retry(&dentry->d_seq, seq)) 137312f8ad4bSLinus Torvalds return -ECHILD; 137412f8ad4bSLinus Torvalds 137512f8ad4bSLinus Torvalds /* 137612f8ad4bSLinus Torvalds * This sequence count validates that the parent had no 137712f8ad4bSLinus Torvalds * changes while we did the lookup of the dentry above. 137812f8ad4bSLinus Torvalds * 137912f8ad4bSLinus Torvalds * The memory barrier in read_seqcount_begin of child is 138012f8ad4bSLinus Torvalds * enough, we can use __read_seqcount_retry here. 138112f8ad4bSLinus Torvalds */ 138231e6b01fSNick Piggin if (__read_seqcount_retry(&parent->d_seq, nd->seq)) 138331e6b01fSNick Piggin return -ECHILD; 138431e6b01fSNick Piggin nd->seq = seq; 13855a18fff2SAl Viro 1386fa4ee159SMiklos Szeredi if (unlikely(d_need_lookup(dentry))) 1387fa4ee159SMiklos Szeredi goto unlazy; 138824643087SAl Viro if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) { 13894ce16ef3SAl Viro status = d_revalidate(dentry, nd->flags); 13905a18fff2SAl Viro if (unlikely(status <= 0)) { 13915a18fff2SAl Viro if (status != -ECHILD) 13925a18fff2SAl Viro need_reval = 0; 13935a18fff2SAl Viro goto unlazy; 13945a18fff2SAl Viro } 139524643087SAl Viro } 139631e6b01fSNick Piggin path->mnt = mnt; 139731e6b01fSNick Piggin path->dentry = dentry; 1398d6e9bd25SAl Viro if (unlikely(!__follow_mount_rcu(nd, path, inode))) 1399d6e9bd25SAl Viro goto unlazy; 1400d6e9bd25SAl Viro if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT)) 1401d6e9bd25SAl Viro goto unlazy; 14029875cf80SDavid Howells return 0; 14035a18fff2SAl Viro unlazy: 140419660af7SAl Viro if (unlazy_walk(nd, dentry)) 14055a18fff2SAl Viro return -ECHILD; 14065a18fff2SAl Viro } else { 140731e6b01fSNick Piggin dentry = __d_lookup(parent, name); 140824643087SAl Viro } 14095a18fff2SAl Viro 141081e6f520SAl Viro if (unlikely(!dentry)) 141181e6f520SAl Viro goto need_lookup; 14125a18fff2SAl Viro 141381e6f520SAl Viro if (unlikely(d_need_lookup(dentry))) { 141481e6f520SAl Viro dput(dentry); 141581e6f520SAl Viro goto need_lookup; 14165a18fff2SAl Viro } 141781e6f520SAl Viro 14185a18fff2SAl Viro if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval) 14194ce16ef3SAl Viro status = d_revalidate(dentry, nd->flags); 14205a18fff2SAl Viro if (unlikely(status <= 0)) { 14215a18fff2SAl Viro if (status < 0) { 14225a18fff2SAl Viro dput(dentry); 14235a18fff2SAl Viro return status; 14245a18fff2SAl Viro } 14255a18fff2SAl Viro if (!d_invalidate(dentry)) { 14265a18fff2SAl Viro dput(dentry); 142781e6f520SAl Viro goto need_lookup; 14285a18fff2SAl Viro } 14295a18fff2SAl Viro } 1430697f514dSMiklos Szeredi 14311da177e4SLinus Torvalds path->mnt = mnt; 14321da177e4SLinus Torvalds path->dentry = dentry; 14339875cf80SDavid Howells err = follow_managed(path, nd->flags); 143489312214SIan Kent if (unlikely(err < 0)) { 143589312214SIan Kent path_put_conditional(path, nd); 14369875cf80SDavid Howells return err; 143789312214SIan Kent } 1438a3fbbde7SAl Viro if (err) 1439a3fbbde7SAl Viro nd->flags |= LOOKUP_JUMPED; 144031e6b01fSNick Piggin *inode = path->dentry->d_inode; 14411da177e4SLinus Torvalds return 0; 144281e6f520SAl Viro 144381e6f520SAl Viro need_lookup: 1444697f514dSMiklos Szeredi return 1; 1445697f514dSMiklos Szeredi } 1446697f514dSMiklos Szeredi 1447697f514dSMiklos Szeredi /* Fast lookup failed, do it the slow way */ 1448697f514dSMiklos Szeredi static int lookup_slow(struct nameidata *nd, struct qstr *name, 1449697f514dSMiklos Szeredi struct path *path) 1450697f514dSMiklos Szeredi { 1451697f514dSMiklos Szeredi struct dentry *dentry, *parent; 1452697f514dSMiklos Szeredi int err; 1453697f514dSMiklos Szeredi 1454697f514dSMiklos Szeredi parent = nd->path.dentry; 145581e6f520SAl Viro BUG_ON(nd->inode != parent->d_inode); 145681e6f520SAl Viro 145781e6f520SAl Viro mutex_lock(&parent->d_inode->i_mutex); 145872bd866aSAl Viro dentry = __lookup_hash(name, parent, nd->flags); 145981e6f520SAl Viro mutex_unlock(&parent->d_inode->i_mutex); 146081e6f520SAl Viro if (IS_ERR(dentry)) 146181e6f520SAl Viro return PTR_ERR(dentry); 1462697f514dSMiklos Szeredi path->mnt = nd->path.mnt; 1463697f514dSMiklos Szeredi path->dentry = dentry; 1464697f514dSMiklos Szeredi err = follow_managed(path, nd->flags); 1465697f514dSMiklos Szeredi if (unlikely(err < 0)) { 1466697f514dSMiklos Szeredi path_put_conditional(path, nd); 1467697f514dSMiklos Szeredi return err; 1468697f514dSMiklos Szeredi } 1469697f514dSMiklos Szeredi if (err) 1470697f514dSMiklos Szeredi nd->flags |= LOOKUP_JUMPED; 1471697f514dSMiklos Szeredi return 0; 14721da177e4SLinus Torvalds } 14731da177e4SLinus Torvalds 147452094c8aSAl Viro static inline int may_lookup(struct nameidata *nd) 147552094c8aSAl Viro { 147652094c8aSAl Viro if (nd->flags & LOOKUP_RCU) { 14774ad5abb3SAl Viro int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK); 147852094c8aSAl Viro if (err != -ECHILD) 147952094c8aSAl Viro return err; 148019660af7SAl Viro if (unlazy_walk(nd, NULL)) 148152094c8aSAl Viro return -ECHILD; 148252094c8aSAl Viro } 14834ad5abb3SAl Viro return inode_permission(nd->inode, MAY_EXEC); 148452094c8aSAl Viro } 148552094c8aSAl Viro 14869856fa1bSAl Viro static inline int handle_dots(struct nameidata *nd, int type) 14879856fa1bSAl Viro { 14889856fa1bSAl Viro if (type == LAST_DOTDOT) { 14899856fa1bSAl Viro if (nd->flags & LOOKUP_RCU) { 14909856fa1bSAl Viro if (follow_dotdot_rcu(nd)) 14919856fa1bSAl Viro return -ECHILD; 14929856fa1bSAl Viro } else 14939856fa1bSAl Viro follow_dotdot(nd); 14949856fa1bSAl Viro } 14959856fa1bSAl Viro return 0; 14969856fa1bSAl Viro } 14979856fa1bSAl Viro 1498951361f9SAl Viro static void terminate_walk(struct nameidata *nd) 1499951361f9SAl Viro { 1500951361f9SAl Viro if (!(nd->flags & LOOKUP_RCU)) { 1501951361f9SAl Viro path_put(&nd->path); 1502951361f9SAl Viro } else { 1503951361f9SAl Viro nd->flags &= ~LOOKUP_RCU; 15045b6ca027SAl Viro if (!(nd->flags & LOOKUP_ROOT)) 1505951361f9SAl Viro nd->root.mnt = NULL; 150632a7991bSAl Viro unlock_rcu_walk(); 1507951361f9SAl Viro } 1508951361f9SAl Viro } 1509951361f9SAl Viro 15103ddcd056SLinus Torvalds /* 15113ddcd056SLinus Torvalds * Do we need to follow links? We _really_ want to be able 15123ddcd056SLinus Torvalds * to do this check without having to look at inode->i_op, 15133ddcd056SLinus Torvalds * so we keep a cache of "no, this doesn't need follow_link" 15143ddcd056SLinus Torvalds * for the common case. 15153ddcd056SLinus Torvalds */ 15167813b94aSLinus Torvalds static inline int should_follow_link(struct inode *inode, int follow) 15173ddcd056SLinus Torvalds { 15183ddcd056SLinus Torvalds if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) { 15193ddcd056SLinus Torvalds if (likely(inode->i_op->follow_link)) 15203ddcd056SLinus Torvalds return follow; 15213ddcd056SLinus Torvalds 15223ddcd056SLinus Torvalds /* This gets set once for the inode lifetime */ 15233ddcd056SLinus Torvalds spin_lock(&inode->i_lock); 15243ddcd056SLinus Torvalds inode->i_opflags |= IOP_NOFOLLOW; 15253ddcd056SLinus Torvalds spin_unlock(&inode->i_lock); 15263ddcd056SLinus Torvalds } 15273ddcd056SLinus Torvalds return 0; 15283ddcd056SLinus Torvalds } 15293ddcd056SLinus Torvalds 1530ce57dfc1SAl Viro static inline int walk_component(struct nameidata *nd, struct path *path, 1531ce57dfc1SAl Viro struct qstr *name, int type, int follow) 1532ce57dfc1SAl Viro { 1533ce57dfc1SAl Viro struct inode *inode; 1534ce57dfc1SAl Viro int err; 1535ce57dfc1SAl Viro /* 1536ce57dfc1SAl Viro * "." and ".." are special - ".." especially so because it has 1537ce57dfc1SAl Viro * to be able to know about the current root directory and 1538ce57dfc1SAl Viro * parent relationships. 1539ce57dfc1SAl Viro */ 1540ce57dfc1SAl Viro if (unlikely(type != LAST_NORM)) 1541ce57dfc1SAl Viro return handle_dots(nd, type); 1542697f514dSMiklos Szeredi err = lookup_fast(nd, name, path, &inode); 1543ce57dfc1SAl Viro if (unlikely(err)) { 1544697f514dSMiklos Szeredi if (err < 0) 1545697f514dSMiklos Szeredi goto out_err; 1546697f514dSMiklos Szeredi 1547697f514dSMiklos Szeredi err = lookup_slow(nd, name, path); 1548697f514dSMiklos Szeredi if (err < 0) 1549697f514dSMiklos Szeredi goto out_err; 1550697f514dSMiklos Szeredi 1551697f514dSMiklos Szeredi inode = path->dentry->d_inode; 1552ce57dfc1SAl Viro } 1553697f514dSMiklos Szeredi err = -ENOENT; 1554697f514dSMiklos Szeredi if (!inode) 1555697f514dSMiklos Szeredi goto out_path_put; 1556697f514dSMiklos Szeredi 15577813b94aSLinus Torvalds if (should_follow_link(inode, follow)) { 155819660af7SAl Viro if (nd->flags & LOOKUP_RCU) { 155919660af7SAl Viro if (unlikely(unlazy_walk(nd, path->dentry))) { 1560697f514dSMiklos Szeredi err = -ECHILD; 1561697f514dSMiklos Szeredi goto out_err; 156219660af7SAl Viro } 156319660af7SAl Viro } 1564ce57dfc1SAl Viro BUG_ON(inode != path->dentry->d_inode); 1565ce57dfc1SAl Viro return 1; 1566ce57dfc1SAl Viro } 1567ce57dfc1SAl Viro path_to_nameidata(path, nd); 1568ce57dfc1SAl Viro nd->inode = inode; 1569ce57dfc1SAl Viro return 0; 1570697f514dSMiklos Szeredi 1571697f514dSMiklos Szeredi out_path_put: 1572697f514dSMiklos Szeredi path_to_nameidata(path, nd); 1573697f514dSMiklos Szeredi out_err: 1574697f514dSMiklos Szeredi terminate_walk(nd); 1575697f514dSMiklos Szeredi return err; 1576ce57dfc1SAl Viro } 1577ce57dfc1SAl Viro 15781da177e4SLinus Torvalds /* 1579b356379aSAl Viro * This limits recursive symlink follows to 8, while 1580b356379aSAl Viro * limiting consecutive symlinks to 40. 1581b356379aSAl Viro * 1582b356379aSAl Viro * Without that kind of total limit, nasty chains of consecutive 1583b356379aSAl Viro * symlinks can cause almost arbitrarily long lookups. 1584b356379aSAl Viro */ 1585b356379aSAl Viro static inline int nested_symlink(struct path *path, struct nameidata *nd) 1586b356379aSAl Viro { 1587b356379aSAl Viro int res; 1588b356379aSAl Viro 1589b356379aSAl Viro if (unlikely(current->link_count >= MAX_NESTED_LINKS)) { 1590b356379aSAl Viro path_put_conditional(path, nd); 1591b356379aSAl Viro path_put(&nd->path); 1592b356379aSAl Viro return -ELOOP; 1593b356379aSAl Viro } 15941a4022f8SErez Zadok BUG_ON(nd->depth >= MAX_NESTED_LINKS); 1595b356379aSAl Viro 1596b356379aSAl Viro nd->depth++; 1597b356379aSAl Viro current->link_count++; 1598b356379aSAl Viro 1599b356379aSAl Viro do { 1600b356379aSAl Viro struct path link = *path; 1601b356379aSAl Viro void *cookie; 1602574197e0SAl Viro 1603574197e0SAl Viro res = follow_link(&link, nd, &cookie); 16046d7b5aaeSAl Viro if (res) 16056d7b5aaeSAl Viro break; 1606b356379aSAl Viro res = walk_component(nd, path, &nd->last, 1607b356379aSAl Viro nd->last_type, LOOKUP_FOLLOW); 1608574197e0SAl Viro put_link(nd, &link, cookie); 1609b356379aSAl Viro } while (res > 0); 1610b356379aSAl Viro 1611b356379aSAl Viro current->link_count--; 1612b356379aSAl Viro nd->depth--; 1613b356379aSAl Viro return res; 1614b356379aSAl Viro } 1615b356379aSAl Viro 1616b356379aSAl Viro /* 16173ddcd056SLinus Torvalds * We really don't want to look at inode->i_op->lookup 16183ddcd056SLinus Torvalds * when we don't have to. So we keep a cache bit in 16193ddcd056SLinus Torvalds * the inode ->i_opflags field that says "yes, we can 16203ddcd056SLinus Torvalds * do lookup on this inode". 16213ddcd056SLinus Torvalds */ 16223ddcd056SLinus Torvalds static inline int can_lookup(struct inode *inode) 16233ddcd056SLinus Torvalds { 16243ddcd056SLinus Torvalds if (likely(inode->i_opflags & IOP_LOOKUP)) 16253ddcd056SLinus Torvalds return 1; 16263ddcd056SLinus Torvalds if (likely(!inode->i_op->lookup)) 16273ddcd056SLinus Torvalds return 0; 16283ddcd056SLinus Torvalds 16293ddcd056SLinus Torvalds /* We do this once for the lifetime of the inode */ 16303ddcd056SLinus Torvalds spin_lock(&inode->i_lock); 16313ddcd056SLinus Torvalds inode->i_opflags |= IOP_LOOKUP; 16323ddcd056SLinus Torvalds spin_unlock(&inode->i_lock); 16333ddcd056SLinus Torvalds return 1; 16343ddcd056SLinus Torvalds } 16353ddcd056SLinus Torvalds 1636bfcfaa77SLinus Torvalds /* 1637bfcfaa77SLinus Torvalds * We can do the critical dentry name comparison and hashing 1638bfcfaa77SLinus Torvalds * operations one word at a time, but we are limited to: 1639bfcfaa77SLinus Torvalds * 1640bfcfaa77SLinus Torvalds * - Architectures with fast unaligned word accesses. We could 1641bfcfaa77SLinus Torvalds * do a "get_unaligned()" if this helps and is sufficiently 1642bfcfaa77SLinus Torvalds * fast. 1643bfcfaa77SLinus Torvalds * 1644bfcfaa77SLinus Torvalds * - Little-endian machines (so that we can generate the mask 1645bfcfaa77SLinus Torvalds * of low bytes efficiently). Again, we *could* do a byte 1646bfcfaa77SLinus Torvalds * swapping load on big-endian architectures if that is not 1647bfcfaa77SLinus Torvalds * expensive enough to make the optimization worthless. 1648bfcfaa77SLinus Torvalds * 1649bfcfaa77SLinus Torvalds * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we 1650bfcfaa77SLinus Torvalds * do not trap on the (extremely unlikely) case of a page 1651bfcfaa77SLinus Torvalds * crossing operation. 1652bfcfaa77SLinus Torvalds * 1653bfcfaa77SLinus Torvalds * - Furthermore, we need an efficient 64-bit compile for the 1654bfcfaa77SLinus Torvalds * 64-bit case in order to generate the "number of bytes in 1655bfcfaa77SLinus Torvalds * the final mask". Again, that could be replaced with a 1656bfcfaa77SLinus Torvalds * efficient population count instruction or similar. 1657bfcfaa77SLinus Torvalds */ 1658bfcfaa77SLinus Torvalds #ifdef CONFIG_DCACHE_WORD_ACCESS 1659bfcfaa77SLinus Torvalds 1660f68e556eSLinus Torvalds #include <asm/word-at-a-time.h> 1661bfcfaa77SLinus Torvalds 1662f68e556eSLinus Torvalds #ifdef CONFIG_64BIT 1663bfcfaa77SLinus Torvalds 1664bfcfaa77SLinus Torvalds static inline unsigned int fold_hash(unsigned long hash) 1665bfcfaa77SLinus Torvalds { 1666bfcfaa77SLinus Torvalds hash += hash >> (8*sizeof(int)); 1667bfcfaa77SLinus Torvalds return hash; 1668bfcfaa77SLinus Torvalds } 1669bfcfaa77SLinus Torvalds 1670bfcfaa77SLinus Torvalds #else /* 32-bit case */ 1671bfcfaa77SLinus Torvalds 1672bfcfaa77SLinus Torvalds #define fold_hash(x) (x) 1673bfcfaa77SLinus Torvalds 1674bfcfaa77SLinus Torvalds #endif 1675bfcfaa77SLinus Torvalds 1676bfcfaa77SLinus Torvalds unsigned int full_name_hash(const unsigned char *name, unsigned int len) 1677bfcfaa77SLinus Torvalds { 1678bfcfaa77SLinus Torvalds unsigned long a, mask; 1679bfcfaa77SLinus Torvalds unsigned long hash = 0; 1680bfcfaa77SLinus Torvalds 1681bfcfaa77SLinus Torvalds for (;;) { 1682e419b4ccSLinus Torvalds a = load_unaligned_zeropad(name); 1683bfcfaa77SLinus Torvalds if (len < sizeof(unsigned long)) 1684bfcfaa77SLinus Torvalds break; 1685bfcfaa77SLinus Torvalds hash += a; 1686f132c5beSAl Viro hash *= 9; 1687bfcfaa77SLinus Torvalds name += sizeof(unsigned long); 1688bfcfaa77SLinus Torvalds len -= sizeof(unsigned long); 1689bfcfaa77SLinus Torvalds if (!len) 1690bfcfaa77SLinus Torvalds goto done; 1691bfcfaa77SLinus Torvalds } 1692bfcfaa77SLinus Torvalds mask = ~(~0ul << len*8); 1693bfcfaa77SLinus Torvalds hash += mask & a; 1694bfcfaa77SLinus Torvalds done: 1695bfcfaa77SLinus Torvalds return fold_hash(hash); 1696bfcfaa77SLinus Torvalds } 1697bfcfaa77SLinus Torvalds EXPORT_SYMBOL(full_name_hash); 1698bfcfaa77SLinus Torvalds 1699bfcfaa77SLinus Torvalds /* 1700bfcfaa77SLinus Torvalds * Calculate the length and hash of the path component, and 1701bfcfaa77SLinus Torvalds * return the length of the component; 1702bfcfaa77SLinus Torvalds */ 1703bfcfaa77SLinus Torvalds static inline unsigned long hash_name(const char *name, unsigned int *hashp) 1704bfcfaa77SLinus Torvalds { 170536126f8fSLinus Torvalds unsigned long a, b, adata, bdata, mask, hash, len; 170636126f8fSLinus Torvalds const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS; 1707bfcfaa77SLinus Torvalds 1708bfcfaa77SLinus Torvalds hash = a = 0; 1709bfcfaa77SLinus Torvalds len = -sizeof(unsigned long); 1710bfcfaa77SLinus Torvalds do { 1711bfcfaa77SLinus Torvalds hash = (hash + a) * 9; 1712bfcfaa77SLinus Torvalds len += sizeof(unsigned long); 1713e419b4ccSLinus Torvalds a = load_unaligned_zeropad(name+len); 171436126f8fSLinus Torvalds b = a ^ REPEAT_BYTE('/'); 171536126f8fSLinus Torvalds } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants))); 1716bfcfaa77SLinus Torvalds 171736126f8fSLinus Torvalds adata = prep_zero_mask(a, adata, &constants); 171836126f8fSLinus Torvalds bdata = prep_zero_mask(b, bdata, &constants); 171936126f8fSLinus Torvalds 172036126f8fSLinus Torvalds mask = create_zero_mask(adata | bdata); 172136126f8fSLinus Torvalds 172236126f8fSLinus Torvalds hash += a & zero_bytemask(mask); 1723bfcfaa77SLinus Torvalds *hashp = fold_hash(hash); 1724bfcfaa77SLinus Torvalds 172536126f8fSLinus Torvalds return len + find_zero(mask); 1726bfcfaa77SLinus Torvalds } 1727bfcfaa77SLinus Torvalds 1728bfcfaa77SLinus Torvalds #else 1729bfcfaa77SLinus Torvalds 17300145acc2SLinus Torvalds unsigned int full_name_hash(const unsigned char *name, unsigned int len) 17310145acc2SLinus Torvalds { 17320145acc2SLinus Torvalds unsigned long hash = init_name_hash(); 17330145acc2SLinus Torvalds while (len--) 17340145acc2SLinus Torvalds hash = partial_name_hash(*name++, hash); 17350145acc2SLinus Torvalds return end_name_hash(hash); 17360145acc2SLinus Torvalds } 1737ae942ae7SLinus Torvalds EXPORT_SYMBOL(full_name_hash); 17380145acc2SLinus Torvalds 17393ddcd056SLinus Torvalds /* 1740200e9ef7SLinus Torvalds * We know there's a real path component here of at least 1741200e9ef7SLinus Torvalds * one character. 1742200e9ef7SLinus Torvalds */ 1743200e9ef7SLinus Torvalds static inline unsigned long hash_name(const char *name, unsigned int *hashp) 1744200e9ef7SLinus Torvalds { 1745200e9ef7SLinus Torvalds unsigned long hash = init_name_hash(); 1746200e9ef7SLinus Torvalds unsigned long len = 0, c; 1747200e9ef7SLinus Torvalds 1748200e9ef7SLinus Torvalds c = (unsigned char)*name; 1749200e9ef7SLinus Torvalds do { 1750200e9ef7SLinus Torvalds len++; 1751200e9ef7SLinus Torvalds hash = partial_name_hash(c, hash); 1752200e9ef7SLinus Torvalds c = (unsigned char)name[len]; 1753200e9ef7SLinus Torvalds } while (c && c != '/'); 1754200e9ef7SLinus Torvalds *hashp = end_name_hash(hash); 1755200e9ef7SLinus Torvalds return len; 1756200e9ef7SLinus Torvalds } 1757200e9ef7SLinus Torvalds 1758bfcfaa77SLinus Torvalds #endif 1759bfcfaa77SLinus Torvalds 1760200e9ef7SLinus Torvalds /* 17611da177e4SLinus Torvalds * Name resolution. 1762ea3834d9SPrasanna Meda * This is the basic name resolution function, turning a pathname into 1763ea3834d9SPrasanna Meda * the final dentry. We expect 'base' to be positive and a directory. 17641da177e4SLinus Torvalds * 1765ea3834d9SPrasanna Meda * Returns 0 and nd will have valid dentry and mnt on success. 1766ea3834d9SPrasanna Meda * Returns error and drops reference to input namei data on failure. 17671da177e4SLinus Torvalds */ 17686de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd) 17691da177e4SLinus Torvalds { 17701da177e4SLinus Torvalds struct path next; 17711da177e4SLinus Torvalds int err; 17721da177e4SLinus Torvalds 17731da177e4SLinus Torvalds while (*name=='/') 17741da177e4SLinus Torvalds name++; 17751da177e4SLinus Torvalds if (!*name) 1776086e183aSAl Viro return 0; 17771da177e4SLinus Torvalds 17781da177e4SLinus Torvalds /* At this point we know we have a real path component. */ 17791da177e4SLinus Torvalds for(;;) { 17801da177e4SLinus Torvalds struct qstr this; 1781200e9ef7SLinus Torvalds long len; 1782fe479a58SAl Viro int type; 17831da177e4SLinus Torvalds 178452094c8aSAl Viro err = may_lookup(nd); 17851da177e4SLinus Torvalds if (err) 17861da177e4SLinus Torvalds break; 17871da177e4SLinus Torvalds 1788200e9ef7SLinus Torvalds len = hash_name(name, &this.hash); 17891da177e4SLinus Torvalds this.name = name; 1790200e9ef7SLinus Torvalds this.len = len; 17911da177e4SLinus Torvalds 1792fe479a58SAl Viro type = LAST_NORM; 1793200e9ef7SLinus Torvalds if (name[0] == '.') switch (len) { 1794fe479a58SAl Viro case 2: 1795200e9ef7SLinus Torvalds if (name[1] == '.') { 1796fe479a58SAl Viro type = LAST_DOTDOT; 179716c2cd71SAl Viro nd->flags |= LOOKUP_JUMPED; 179816c2cd71SAl Viro } 1799fe479a58SAl Viro break; 1800fe479a58SAl Viro case 1: 1801fe479a58SAl Viro type = LAST_DOT; 1802fe479a58SAl Viro } 18035a202bcdSAl Viro if (likely(type == LAST_NORM)) { 18045a202bcdSAl Viro struct dentry *parent = nd->path.dentry; 180516c2cd71SAl Viro nd->flags &= ~LOOKUP_JUMPED; 18065a202bcdSAl Viro if (unlikely(parent->d_flags & DCACHE_OP_HASH)) { 18075a202bcdSAl Viro err = parent->d_op->d_hash(parent, nd->inode, 18085a202bcdSAl Viro &this); 18095a202bcdSAl Viro if (err < 0) 18105a202bcdSAl Viro break; 18115a202bcdSAl Viro } 18125a202bcdSAl Viro } 1813fe479a58SAl Viro 1814200e9ef7SLinus Torvalds if (!name[len]) 18151da177e4SLinus Torvalds goto last_component; 1816200e9ef7SLinus Torvalds /* 1817200e9ef7SLinus Torvalds * If it wasn't NUL, we know it was '/'. Skip that 1818200e9ef7SLinus Torvalds * slash, and continue until no more slashes. 1819200e9ef7SLinus Torvalds */ 1820200e9ef7SLinus Torvalds do { 1821200e9ef7SLinus Torvalds len++; 1822200e9ef7SLinus Torvalds } while (unlikely(name[len] == '/')); 1823200e9ef7SLinus Torvalds if (!name[len]) 1824b356379aSAl Viro goto last_component; 1825200e9ef7SLinus Torvalds name += len; 18261da177e4SLinus Torvalds 1827ce57dfc1SAl Viro err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW); 1828ce57dfc1SAl Viro if (err < 0) 1829ce57dfc1SAl Viro return err; 1830fe479a58SAl Viro 1831ce57dfc1SAl Viro if (err) { 1832b356379aSAl Viro err = nested_symlink(&next, nd); 18331da177e4SLinus Torvalds if (err) 1834a7472babSAl Viro return err; 183531e6b01fSNick Piggin } 18363ddcd056SLinus Torvalds if (can_lookup(nd->inode)) 18371da177e4SLinus Torvalds continue; 18383ddcd056SLinus Torvalds err = -ENOTDIR; 18393ddcd056SLinus Torvalds break; 18401da177e4SLinus Torvalds /* here ends the main loop */ 18411da177e4SLinus Torvalds 18421da177e4SLinus Torvalds last_component: 1843ce57dfc1SAl Viro nd->last = this; 1844ce57dfc1SAl Viro nd->last_type = type; 1845ce57dfc1SAl Viro return 0; 1846ce57dfc1SAl Viro } 1847951361f9SAl Viro terminate_walk(nd); 18481da177e4SLinus Torvalds return err; 18491da177e4SLinus Torvalds } 18501da177e4SLinus Torvalds 185170e9b357SAl Viro static int path_init(int dfd, const char *name, unsigned int flags, 185270e9b357SAl Viro struct nameidata *nd, struct file **fp) 185331e6b01fSNick Piggin { 185431e6b01fSNick Piggin int retval = 0; 185531e6b01fSNick Piggin 185631e6b01fSNick Piggin nd->last_type = LAST_ROOT; /* if there are only slashes... */ 185716c2cd71SAl Viro nd->flags = flags | LOOKUP_JUMPED; 185831e6b01fSNick Piggin nd->depth = 0; 18595b6ca027SAl Viro if (flags & LOOKUP_ROOT) { 18605b6ca027SAl Viro struct inode *inode = nd->root.dentry->d_inode; 186173d049a4SAl Viro if (*name) { 18625b6ca027SAl Viro if (!inode->i_op->lookup) 18635b6ca027SAl Viro return -ENOTDIR; 18645b6ca027SAl Viro retval = inode_permission(inode, MAY_EXEC); 18655b6ca027SAl Viro if (retval) 18665b6ca027SAl Viro return retval; 186773d049a4SAl Viro } 18685b6ca027SAl Viro nd->path = nd->root; 18695b6ca027SAl Viro nd->inode = inode; 18705b6ca027SAl Viro if (flags & LOOKUP_RCU) { 187132a7991bSAl Viro lock_rcu_walk(); 18725b6ca027SAl Viro nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 18735b6ca027SAl Viro } else { 18745b6ca027SAl Viro path_get(&nd->path); 18755b6ca027SAl Viro } 18765b6ca027SAl Viro return 0; 18775b6ca027SAl Viro } 18785b6ca027SAl Viro 187931e6b01fSNick Piggin nd->root.mnt = NULL; 188031e6b01fSNick Piggin 188131e6b01fSNick Piggin if (*name=='/') { 1882e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 188332a7991bSAl Viro lock_rcu_walk(); 1884e41f7d4eSAl Viro set_root_rcu(nd); 1885e41f7d4eSAl Viro } else { 1886e41f7d4eSAl Viro set_root(nd); 1887e41f7d4eSAl Viro path_get(&nd->root); 1888e41f7d4eSAl Viro } 188931e6b01fSNick Piggin nd->path = nd->root; 189031e6b01fSNick Piggin } else if (dfd == AT_FDCWD) { 1891e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 189231e6b01fSNick Piggin struct fs_struct *fs = current->fs; 1893c28cc364SNick Piggin unsigned seq; 189431e6b01fSNick Piggin 189532a7991bSAl Viro lock_rcu_walk(); 189631e6b01fSNick Piggin 1897c28cc364SNick Piggin do { 1898c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 189931e6b01fSNick Piggin nd->path = fs->pwd; 1900c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 1901c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 1902e41f7d4eSAl Viro } else { 1903e41f7d4eSAl Viro get_fs_pwd(current->fs, &nd->path); 1904e41f7d4eSAl Viro } 190531e6b01fSNick Piggin } else { 19062903ff01SAl Viro struct fd f = fdget_raw(dfd); 190731e6b01fSNick Piggin struct dentry *dentry; 190831e6b01fSNick Piggin 19092903ff01SAl Viro if (!f.file) 19102903ff01SAl Viro return -EBADF; 191131e6b01fSNick Piggin 19122903ff01SAl Viro dentry = f.file->f_path.dentry; 191331e6b01fSNick Piggin 1914f52e0c11SAl Viro if (*name) { 19152903ff01SAl Viro if (!S_ISDIR(dentry->d_inode->i_mode)) { 19162903ff01SAl Viro fdput(f); 19172903ff01SAl Viro return -ENOTDIR; 1918f52e0c11SAl Viro } 191931e6b01fSNick Piggin 19202903ff01SAl Viro retval = inode_permission(dentry->d_inode, MAY_EXEC); 19212903ff01SAl Viro if (retval) { 19222903ff01SAl Viro fdput(f); 19232903ff01SAl Viro return retval; 19242903ff01SAl Viro } 19252903ff01SAl Viro } 19262903ff01SAl Viro 19272903ff01SAl Viro nd->path = f.file->f_path; 1928e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 19292903ff01SAl Viro if (f.need_put) 19302903ff01SAl Viro *fp = f.file; 1931c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 193232a7991bSAl Viro lock_rcu_walk(); 19335590ff0dSUlrich Drepper } else { 19342903ff01SAl Viro path_get(&nd->path); 19352903ff01SAl Viro fdput(f); 19361da177e4SLinus Torvalds } 1937e41f7d4eSAl Viro } 1938e41f7d4eSAl Viro 193931e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 19409b4a9b14SAl Viro return 0; 19419b4a9b14SAl Viro } 19429b4a9b14SAl Viro 1943bd92d7feSAl Viro static inline int lookup_last(struct nameidata *nd, struct path *path) 1944bd92d7feSAl Viro { 1945bd92d7feSAl Viro if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len]) 1946bd92d7feSAl Viro nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 1947bd92d7feSAl Viro 1948bd92d7feSAl Viro nd->flags &= ~LOOKUP_PARENT; 1949bd92d7feSAl Viro return walk_component(nd, path, &nd->last, nd->last_type, 1950bd92d7feSAl Viro nd->flags & LOOKUP_FOLLOW); 1951bd92d7feSAl Viro } 1952bd92d7feSAl Viro 19539b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */ 1954ee0827cdSAl Viro static int path_lookupat(int dfd, const char *name, 19559b4a9b14SAl Viro unsigned int flags, struct nameidata *nd) 19569b4a9b14SAl Viro { 195770e9b357SAl Viro struct file *base = NULL; 1958bd92d7feSAl Viro struct path path; 1959bd92d7feSAl Viro int err; 196031e6b01fSNick Piggin 196131e6b01fSNick Piggin /* 196231e6b01fSNick Piggin * Path walking is largely split up into 2 different synchronisation 196331e6b01fSNick Piggin * schemes, rcu-walk and ref-walk (explained in 196431e6b01fSNick Piggin * Documentation/filesystems/path-lookup.txt). These share much of the 196531e6b01fSNick Piggin * path walk code, but some things particularly setup, cleanup, and 196631e6b01fSNick Piggin * following mounts are sufficiently divergent that functions are 196731e6b01fSNick Piggin * duplicated. Typically there is a function foo(), and its RCU 196831e6b01fSNick Piggin * analogue, foo_rcu(). 196931e6b01fSNick Piggin * 197031e6b01fSNick Piggin * -ECHILD is the error number of choice (just to avoid clashes) that 197131e6b01fSNick Piggin * is returned if some aspect of an rcu-walk fails. Such an error must 197231e6b01fSNick Piggin * be handled by restarting a traditional ref-walk (which will always 197331e6b01fSNick Piggin * be able to complete). 197431e6b01fSNick Piggin */ 1975bd92d7feSAl Viro err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base); 1976ee0827cdSAl Viro 1977bd92d7feSAl Viro if (unlikely(err)) 1978bd92d7feSAl Viro return err; 1979ee0827cdSAl Viro 1980ee0827cdSAl Viro current->total_link_count = 0; 1981bd92d7feSAl Viro err = link_path_walk(name, nd); 1982bd92d7feSAl Viro 1983bd92d7feSAl Viro if (!err && !(flags & LOOKUP_PARENT)) { 1984bd92d7feSAl Viro err = lookup_last(nd, &path); 1985bd92d7feSAl Viro while (err > 0) { 1986bd92d7feSAl Viro void *cookie; 1987bd92d7feSAl Viro struct path link = path; 1988800179c9SKees Cook err = may_follow_link(&link, nd); 1989800179c9SKees Cook if (unlikely(err)) 1990800179c9SKees Cook break; 1991bd92d7feSAl Viro nd->flags |= LOOKUP_PARENT; 1992574197e0SAl Viro err = follow_link(&link, nd, &cookie); 19936d7b5aaeSAl Viro if (err) 19946d7b5aaeSAl Viro break; 1995bd92d7feSAl Viro err = lookup_last(nd, &path); 1996574197e0SAl Viro put_link(nd, &link, cookie); 1997bd92d7feSAl Viro } 1998bd92d7feSAl Viro } 1999ee0827cdSAl Viro 20009f1fafeeSAl Viro if (!err) 20019f1fafeeSAl Viro err = complete_walk(nd); 2002bd92d7feSAl Viro 2003bd92d7feSAl Viro if (!err && nd->flags & LOOKUP_DIRECTORY) { 2004bd92d7feSAl Viro if (!nd->inode->i_op->lookup) { 2005bd92d7feSAl Viro path_put(&nd->path); 2006bd23a539SAl Viro err = -ENOTDIR; 2007bd92d7feSAl Viro } 2008bd92d7feSAl Viro } 200916c2cd71SAl Viro 201070e9b357SAl Viro if (base) 201170e9b357SAl Viro fput(base); 2012ee0827cdSAl Viro 20135b6ca027SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) { 201431e6b01fSNick Piggin path_put(&nd->root); 201531e6b01fSNick Piggin nd->root.mnt = NULL; 201631e6b01fSNick Piggin } 2017bd92d7feSAl Viro return err; 201831e6b01fSNick Piggin } 201931e6b01fSNick Piggin 2020873f1eedSJeff Layton static int filename_lookup(int dfd, struct filename *name, 2021873f1eedSJeff Layton unsigned int flags, struct nameidata *nd) 2022873f1eedSJeff Layton { 2023873f1eedSJeff Layton int retval = path_lookupat(dfd, name->name, flags | LOOKUP_RCU, nd); 2024873f1eedSJeff Layton if (unlikely(retval == -ECHILD)) 2025873f1eedSJeff Layton retval = path_lookupat(dfd, name->name, flags, nd); 2026873f1eedSJeff Layton if (unlikely(retval == -ESTALE)) 2027873f1eedSJeff Layton retval = path_lookupat(dfd, name->name, 2028873f1eedSJeff Layton flags | LOOKUP_REVAL, nd); 2029873f1eedSJeff Layton 2030873f1eedSJeff Layton if (likely(!retval)) 2031adb5c247SJeff Layton audit_inode(name, nd->path.dentry, flags & LOOKUP_PARENT); 2032873f1eedSJeff Layton return retval; 2033873f1eedSJeff Layton } 2034873f1eedSJeff Layton 2035ee0827cdSAl Viro static int do_path_lookup(int dfd, const char *name, 2036ee0827cdSAl Viro unsigned int flags, struct nameidata *nd) 2037ee0827cdSAl Viro { 2038873f1eedSJeff Layton struct filename filename = { .name = name }; 2039ee0827cdSAl Viro 2040873f1eedSJeff Layton return filename_lookup(dfd, &filename, flags, nd); 20411da177e4SLinus Torvalds } 20421da177e4SLinus Torvalds 204379714f72SAl Viro /* does lookup, returns the object with parent locked */ 204479714f72SAl Viro struct dentry *kern_path_locked(const char *name, struct path *path) 20455590ff0dSUlrich Drepper { 204679714f72SAl Viro struct nameidata nd; 204779714f72SAl Viro struct dentry *d; 204879714f72SAl Viro int err = do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, &nd); 204979714f72SAl Viro if (err) 205079714f72SAl Viro return ERR_PTR(err); 205179714f72SAl Viro if (nd.last_type != LAST_NORM) { 205279714f72SAl Viro path_put(&nd.path); 205379714f72SAl Viro return ERR_PTR(-EINVAL); 205479714f72SAl Viro } 205579714f72SAl Viro mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 20561e0ea001SAl Viro d = __lookup_hash(&nd.last, nd.path.dentry, 0); 205779714f72SAl Viro if (IS_ERR(d)) { 205879714f72SAl Viro mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 205979714f72SAl Viro path_put(&nd.path); 206079714f72SAl Viro return d; 206179714f72SAl Viro } 206279714f72SAl Viro *path = nd.path; 206379714f72SAl Viro return d; 20645590ff0dSUlrich Drepper } 20655590ff0dSUlrich Drepper 2066d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path) 2067d1811465SAl Viro { 2068d1811465SAl Viro struct nameidata nd; 2069d1811465SAl Viro int res = do_path_lookup(AT_FDCWD, name, flags, &nd); 2070d1811465SAl Viro if (!res) 2071d1811465SAl Viro *path = nd.path; 2072d1811465SAl Viro return res; 2073d1811465SAl Viro } 2074d1811465SAl Viro 207516f18200SJosef 'Jeff' Sipek /** 207616f18200SJosef 'Jeff' Sipek * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair 207716f18200SJosef 'Jeff' Sipek * @dentry: pointer to dentry of the base directory 207816f18200SJosef 'Jeff' Sipek * @mnt: pointer to vfs mount of the base directory 207916f18200SJosef 'Jeff' Sipek * @name: pointer to file name 208016f18200SJosef 'Jeff' Sipek * @flags: lookup flags 2081e0a01249SAl Viro * @path: pointer to struct path to fill 208216f18200SJosef 'Jeff' Sipek */ 208316f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt, 208416f18200SJosef 'Jeff' Sipek const char *name, unsigned int flags, 2085e0a01249SAl Viro struct path *path) 208616f18200SJosef 'Jeff' Sipek { 2087e0a01249SAl Viro struct nameidata nd; 2088e0a01249SAl Viro int err; 2089e0a01249SAl Viro nd.root.dentry = dentry; 2090e0a01249SAl Viro nd.root.mnt = mnt; 2091e0a01249SAl Viro BUG_ON(flags & LOOKUP_PARENT); 20925b6ca027SAl Viro /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */ 2093e0a01249SAl Viro err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd); 2094e0a01249SAl Viro if (!err) 2095e0a01249SAl Viro *path = nd.path; 2096e0a01249SAl Viro return err; 209716f18200SJosef 'Jeff' Sipek } 209816f18200SJosef 'Jeff' Sipek 2099057f6c01SJames Morris /* 2100057f6c01SJames Morris * Restricted form of lookup. Doesn't follow links, single-component only, 2101057f6c01SJames Morris * needs parent already locked. Doesn't follow mounts. 2102057f6c01SJames Morris * SMP-safe. 2103057f6c01SJames Morris */ 2104a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd) 21051da177e4SLinus Torvalds { 210672bd866aSAl Viro return __lookup_hash(&nd->last, nd->path.dentry, nd->flags); 21071da177e4SLinus Torvalds } 21081da177e4SLinus Torvalds 2109eead1911SChristoph Hellwig /** 2110a6b91919SRandy Dunlap * lookup_one_len - filesystem helper to lookup single pathname component 2111eead1911SChristoph Hellwig * @name: pathname component to lookup 2112eead1911SChristoph Hellwig * @base: base directory to lookup from 2113eead1911SChristoph Hellwig * @len: maximum length @len should be interpreted to 2114eead1911SChristoph Hellwig * 2115a6b91919SRandy Dunlap * Note that this routine is purely a helper for filesystem usage and should 2116a6b91919SRandy Dunlap * not be called by generic code. Also note that by using this function the 2117eead1911SChristoph Hellwig * nameidata argument is passed to the filesystem methods and a filesystem 2118eead1911SChristoph Hellwig * using this helper needs to be prepared for that. 2119eead1911SChristoph Hellwig */ 2120057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len) 2121057f6c01SJames Morris { 2122057f6c01SJames Morris struct qstr this; 21236a96ba54SAl Viro unsigned int c; 2124cda309deSMiklos Szeredi int err; 2125057f6c01SJames Morris 21262f9092e1SDavid Woodhouse WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex)); 21272f9092e1SDavid Woodhouse 21286a96ba54SAl Viro this.name = name; 21296a96ba54SAl Viro this.len = len; 21300145acc2SLinus Torvalds this.hash = full_name_hash(name, len); 21316a96ba54SAl Viro if (!len) 21326a96ba54SAl Viro return ERR_PTR(-EACCES); 21336a96ba54SAl Viro 21346a96ba54SAl Viro while (len--) { 21356a96ba54SAl Viro c = *(const unsigned char *)name++; 21366a96ba54SAl Viro if (c == '/' || c == '\0') 21376a96ba54SAl Viro return ERR_PTR(-EACCES); 21386a96ba54SAl Viro } 21395a202bcdSAl Viro /* 21405a202bcdSAl Viro * See if the low-level filesystem might want 21415a202bcdSAl Viro * to use its own hash.. 21425a202bcdSAl Viro */ 21435a202bcdSAl Viro if (base->d_flags & DCACHE_OP_HASH) { 21445a202bcdSAl Viro int err = base->d_op->d_hash(base, base->d_inode, &this); 21455a202bcdSAl Viro if (err < 0) 21465a202bcdSAl Viro return ERR_PTR(err); 21475a202bcdSAl Viro } 2148eead1911SChristoph Hellwig 2149cda309deSMiklos Szeredi err = inode_permission(base->d_inode, MAY_EXEC); 2150cda309deSMiklos Szeredi if (err) 2151cda309deSMiklos Szeredi return ERR_PTR(err); 2152cda309deSMiklos Szeredi 215372bd866aSAl Viro return __lookup_hash(&this, base, 0); 2154057f6c01SJames Morris } 2155057f6c01SJames Morris 21561fa1e7f6SAndy Whitcroft int user_path_at_empty(int dfd, const char __user *name, unsigned flags, 21571fa1e7f6SAndy Whitcroft struct path *path, int *empty) 21581da177e4SLinus Torvalds { 21592d8f3038SAl Viro struct nameidata nd; 216091a27b2aSJeff Layton struct filename *tmp = getname_flags(name, flags, empty); 21611da177e4SLinus Torvalds int err = PTR_ERR(tmp); 21621da177e4SLinus Torvalds if (!IS_ERR(tmp)) { 21632d8f3038SAl Viro 21642d8f3038SAl Viro BUG_ON(flags & LOOKUP_PARENT); 21652d8f3038SAl Viro 2166873f1eedSJeff Layton err = filename_lookup(dfd, tmp, flags, &nd); 21671da177e4SLinus Torvalds putname(tmp); 21682d8f3038SAl Viro if (!err) 21692d8f3038SAl Viro *path = nd.path; 21701da177e4SLinus Torvalds } 21711da177e4SLinus Torvalds return err; 21721da177e4SLinus Torvalds } 21731da177e4SLinus Torvalds 21741fa1e7f6SAndy Whitcroft int user_path_at(int dfd, const char __user *name, unsigned flags, 21751fa1e7f6SAndy Whitcroft struct path *path) 21761fa1e7f6SAndy Whitcroft { 2177f7493e5dSLinus Torvalds return user_path_at_empty(dfd, name, flags, path, NULL); 21781fa1e7f6SAndy Whitcroft } 21791fa1e7f6SAndy Whitcroft 2180873f1eedSJeff Layton /* 2181873f1eedSJeff Layton * NB: most callers don't do anything directly with the reference to the 2182873f1eedSJeff Layton * to struct filename, but the nd->last pointer points into the name string 2183873f1eedSJeff Layton * allocated by getname. So we must hold the reference to it until all 2184873f1eedSJeff Layton * path-walking is complete. 2185873f1eedSJeff Layton */ 218691a27b2aSJeff Layton static struct filename * 218791a27b2aSJeff Layton user_path_parent(int dfd, const char __user *path, struct nameidata *nd) 21882ad94ae6SAl Viro { 218991a27b2aSJeff Layton struct filename *s = getname(path); 21902ad94ae6SAl Viro int error; 21912ad94ae6SAl Viro 21922ad94ae6SAl Viro if (IS_ERR(s)) 219391a27b2aSJeff Layton return s; 21942ad94ae6SAl Viro 2195873f1eedSJeff Layton error = filename_lookup(dfd, s, LOOKUP_PARENT, nd); 219691a27b2aSJeff Layton if (error) { 21972ad94ae6SAl Viro putname(s); 219891a27b2aSJeff Layton return ERR_PTR(error); 219991a27b2aSJeff Layton } 22002ad94ae6SAl Viro 220191a27b2aSJeff Layton return s; 22022ad94ae6SAl Viro } 22032ad94ae6SAl Viro 22041da177e4SLinus Torvalds /* 22051da177e4SLinus Torvalds * It's inline, so penalty for filesystems that don't use sticky bit is 22061da177e4SLinus Torvalds * minimal. 22071da177e4SLinus Torvalds */ 22081da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode) 22091da177e4SLinus Torvalds { 22108e96e3b7SEric W. Biederman kuid_t fsuid = current_fsuid(); 2211da9592edSDavid Howells 22121da177e4SLinus Torvalds if (!(dir->i_mode & S_ISVTX)) 22131da177e4SLinus Torvalds return 0; 22148e96e3b7SEric W. Biederman if (uid_eq(inode->i_uid, fsuid)) 22151da177e4SLinus Torvalds return 0; 22168e96e3b7SEric W. Biederman if (uid_eq(dir->i_uid, fsuid)) 22171da177e4SLinus Torvalds return 0; 22181a48e2acSEric W. Biederman return !inode_capable(inode, CAP_FOWNER); 22191da177e4SLinus Torvalds } 22201da177e4SLinus Torvalds 22211da177e4SLinus Torvalds /* 22221da177e4SLinus Torvalds * Check whether we can remove a link victim from directory dir, check 22231da177e4SLinus Torvalds * whether the type of victim is right. 22241da177e4SLinus Torvalds * 1. We can't do it if dir is read-only (done in permission()) 22251da177e4SLinus Torvalds * 2. We should have write and exec permissions on dir 22261da177e4SLinus Torvalds * 3. We can't remove anything from append-only dir 22271da177e4SLinus Torvalds * 4. We can't do anything with immutable dir (done in permission()) 22281da177e4SLinus Torvalds * 5. If the sticky bit on dir is set we should either 22291da177e4SLinus Torvalds * a. be owner of dir, or 22301da177e4SLinus Torvalds * b. be owner of victim, or 22311da177e4SLinus Torvalds * c. have CAP_FOWNER capability 22321da177e4SLinus Torvalds * 6. If the victim is append-only or immutable we can't do antyhing with 22331da177e4SLinus Torvalds * links pointing to it. 22341da177e4SLinus Torvalds * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. 22351da177e4SLinus Torvalds * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. 22361da177e4SLinus Torvalds * 9. We can't remove a root or mountpoint. 22371da177e4SLinus Torvalds * 10. We don't allow removal of NFS sillyrenamed files; it's handled by 22381da177e4SLinus Torvalds * nfs_async_unlink(). 22391da177e4SLinus Torvalds */ 2240858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir) 22411da177e4SLinus Torvalds { 22421da177e4SLinus Torvalds int error; 22431da177e4SLinus Torvalds 22441da177e4SLinus Torvalds if (!victim->d_inode) 22451da177e4SLinus Torvalds return -ENOENT; 22461da177e4SLinus Torvalds 22471da177e4SLinus Torvalds BUG_ON(victim->d_parent->d_inode != dir); 22484fa6b5ecSJeff Layton audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE); 22491da177e4SLinus Torvalds 2250f419a2e3SAl Viro error = inode_permission(dir, MAY_WRITE | MAY_EXEC); 22511da177e4SLinus Torvalds if (error) 22521da177e4SLinus Torvalds return error; 22531da177e4SLinus Torvalds if (IS_APPEND(dir)) 22541da177e4SLinus Torvalds return -EPERM; 22551da177e4SLinus Torvalds if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)|| 2256f9454548SHugh Dickins IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode)) 22571da177e4SLinus Torvalds return -EPERM; 22581da177e4SLinus Torvalds if (isdir) { 22591da177e4SLinus Torvalds if (!S_ISDIR(victim->d_inode->i_mode)) 22601da177e4SLinus Torvalds return -ENOTDIR; 22611da177e4SLinus Torvalds if (IS_ROOT(victim)) 22621da177e4SLinus Torvalds return -EBUSY; 22631da177e4SLinus Torvalds } else if (S_ISDIR(victim->d_inode->i_mode)) 22641da177e4SLinus Torvalds return -EISDIR; 22651da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 22661da177e4SLinus Torvalds return -ENOENT; 22671da177e4SLinus Torvalds if (victim->d_flags & DCACHE_NFSFS_RENAMED) 22681da177e4SLinus Torvalds return -EBUSY; 22691da177e4SLinus Torvalds return 0; 22701da177e4SLinus Torvalds } 22711da177e4SLinus Torvalds 22721da177e4SLinus Torvalds /* Check whether we can create an object with dentry child in directory 22731da177e4SLinus Torvalds * dir. 22741da177e4SLinus Torvalds * 1. We can't do it if child already exists (open has special treatment for 22751da177e4SLinus Torvalds * this case, but since we are inlined it's OK) 22761da177e4SLinus Torvalds * 2. We can't do it if dir is read-only (done in permission()) 22771da177e4SLinus Torvalds * 3. We should have write and exec permissions on dir 22781da177e4SLinus Torvalds * 4. We can't do it if dir is immutable (done in permission()) 22791da177e4SLinus Torvalds */ 2280a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child) 22811da177e4SLinus Torvalds { 22821da177e4SLinus Torvalds if (child->d_inode) 22831da177e4SLinus Torvalds return -EEXIST; 22841da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 22851da177e4SLinus Torvalds return -ENOENT; 2286f419a2e3SAl Viro return inode_permission(dir, MAY_WRITE | MAY_EXEC); 22871da177e4SLinus Torvalds } 22881da177e4SLinus Torvalds 22891da177e4SLinus Torvalds /* 22901da177e4SLinus Torvalds * p1 and p2 should be directories on the same fs. 22911da177e4SLinus Torvalds */ 22921da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2) 22931da177e4SLinus Torvalds { 22941da177e4SLinus Torvalds struct dentry *p; 22951da177e4SLinus Torvalds 22961da177e4SLinus Torvalds if (p1 == p2) { 2297f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 22981da177e4SLinus Torvalds return NULL; 22991da177e4SLinus Torvalds } 23001da177e4SLinus Torvalds 2301a11f3a05SArjan van de Ven mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 23021da177e4SLinus Torvalds 2303e2761a11SOGAWA Hirofumi p = d_ancestor(p2, p1); 2304e2761a11SOGAWA Hirofumi if (p) { 2305f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT); 2306f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD); 23071da177e4SLinus Torvalds return p; 23081da177e4SLinus Torvalds } 23091da177e4SLinus Torvalds 2310e2761a11SOGAWA Hirofumi p = d_ancestor(p1, p2); 2311e2761a11SOGAWA Hirofumi if (p) { 2312f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 2313f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 23141da177e4SLinus Torvalds return p; 23151da177e4SLinus Torvalds } 23161da177e4SLinus Torvalds 2317f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 2318f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 23191da177e4SLinus Torvalds return NULL; 23201da177e4SLinus Torvalds } 23211da177e4SLinus Torvalds 23221da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2) 23231da177e4SLinus Torvalds { 23241b1dcc1bSJes Sorensen mutex_unlock(&p1->d_inode->i_mutex); 23251da177e4SLinus Torvalds if (p1 != p2) { 23261b1dcc1bSJes Sorensen mutex_unlock(&p2->d_inode->i_mutex); 2327a11f3a05SArjan van de Ven mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 23281da177e4SLinus Torvalds } 23291da177e4SLinus Torvalds } 23301da177e4SLinus Torvalds 23314acdaf27SAl Viro int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode, 2332312b63fbSAl Viro bool want_excl) 23331da177e4SLinus Torvalds { 2334a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 23351da177e4SLinus Torvalds if (error) 23361da177e4SLinus Torvalds return error; 23371da177e4SLinus Torvalds 2338acfa4380SAl Viro if (!dir->i_op->create) 23391da177e4SLinus Torvalds return -EACCES; /* shouldn't it be ENOSYS? */ 23401da177e4SLinus Torvalds mode &= S_IALLUGO; 23411da177e4SLinus Torvalds mode |= S_IFREG; 23421da177e4SLinus Torvalds error = security_inode_create(dir, dentry, mode); 23431da177e4SLinus Torvalds if (error) 23441da177e4SLinus Torvalds return error; 2345312b63fbSAl Viro error = dir->i_op->create(dir, dentry, mode, want_excl); 2346a74574aaSStephen Smalley if (!error) 2347f38aa942SAmy Griffis fsnotify_create(dir, dentry); 23481da177e4SLinus Torvalds return error; 23491da177e4SLinus Torvalds } 23501da177e4SLinus Torvalds 235173d049a4SAl Viro static int may_open(struct path *path, int acc_mode, int flag) 23521da177e4SLinus Torvalds { 23533fb64190SChristoph Hellwig struct dentry *dentry = path->dentry; 23541da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 23551da177e4SLinus Torvalds int error; 23561da177e4SLinus Torvalds 2357bcda7652SAl Viro /* O_PATH? */ 2358bcda7652SAl Viro if (!acc_mode) 2359bcda7652SAl Viro return 0; 2360bcda7652SAl Viro 23611da177e4SLinus Torvalds if (!inode) 23621da177e4SLinus Torvalds return -ENOENT; 23631da177e4SLinus Torvalds 2364c8fe8f30SChristoph Hellwig switch (inode->i_mode & S_IFMT) { 2365c8fe8f30SChristoph Hellwig case S_IFLNK: 23661da177e4SLinus Torvalds return -ELOOP; 2367c8fe8f30SChristoph Hellwig case S_IFDIR: 2368c8fe8f30SChristoph Hellwig if (acc_mode & MAY_WRITE) 23691da177e4SLinus Torvalds return -EISDIR; 2370c8fe8f30SChristoph Hellwig break; 2371c8fe8f30SChristoph Hellwig case S_IFBLK: 2372c8fe8f30SChristoph Hellwig case S_IFCHR: 23733fb64190SChristoph Hellwig if (path->mnt->mnt_flags & MNT_NODEV) 23741da177e4SLinus Torvalds return -EACCES; 2375c8fe8f30SChristoph Hellwig /*FALLTHRU*/ 2376c8fe8f30SChristoph Hellwig case S_IFIFO: 2377c8fe8f30SChristoph Hellwig case S_IFSOCK: 23781da177e4SLinus Torvalds flag &= ~O_TRUNC; 2379c8fe8f30SChristoph Hellwig break; 23804a3fd211SDave Hansen } 2381b41572e9SDave Hansen 23823fb64190SChristoph Hellwig error = inode_permission(inode, acc_mode); 2383b41572e9SDave Hansen if (error) 2384b41572e9SDave Hansen return error; 23856146f0d5SMimi Zohar 23861da177e4SLinus Torvalds /* 23871da177e4SLinus Torvalds * An append-only file must be opened in append mode for writing. 23881da177e4SLinus Torvalds */ 23891da177e4SLinus Torvalds if (IS_APPEND(inode)) { 23908737c930SAl Viro if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND)) 23917715b521SAl Viro return -EPERM; 23921da177e4SLinus Torvalds if (flag & O_TRUNC) 23937715b521SAl Viro return -EPERM; 23941da177e4SLinus Torvalds } 23951da177e4SLinus Torvalds 23961da177e4SLinus Torvalds /* O_NOATIME can only be set by the owner or superuser */ 23972e149670SSerge E. Hallyn if (flag & O_NOATIME && !inode_owner_or_capable(inode)) 23987715b521SAl Viro return -EPERM; 23991da177e4SLinus Torvalds 2400f3c7691eSJ. Bruce Fields return 0; 24017715b521SAl Viro } 24027715b521SAl Viro 2403e1181ee6SJeff Layton static int handle_truncate(struct file *filp) 24047715b521SAl Viro { 2405e1181ee6SJeff Layton struct path *path = &filp->f_path; 24067715b521SAl Viro struct inode *inode = path->dentry->d_inode; 24077715b521SAl Viro int error = get_write_access(inode); 24081da177e4SLinus Torvalds if (error) 24097715b521SAl Viro return error; 24101da177e4SLinus Torvalds /* 24111da177e4SLinus Torvalds * Refuse to truncate files with mandatory locks held on them. 24121da177e4SLinus Torvalds */ 24131da177e4SLinus Torvalds error = locks_verify_locked(inode); 2414be6d3e56SKentaro Takeda if (!error) 2415ea0d3ab2STetsuo Handa error = security_path_truncate(path); 24161da177e4SLinus Torvalds if (!error) { 24177715b521SAl Viro error = do_truncate(path->dentry, 0, 2418d139d7ffSMiklos Szeredi ATTR_MTIME|ATTR_CTIME|ATTR_OPEN, 2419e1181ee6SJeff Layton filp); 24201da177e4SLinus Torvalds } 24211da177e4SLinus Torvalds put_write_access(inode); 2422acd0c935SMimi Zohar return error; 24231da177e4SLinus Torvalds } 24241da177e4SLinus Torvalds 2425d57999e1SDave Hansen static inline int open_to_namei_flags(int flag) 2426d57999e1SDave Hansen { 24278a5e929dSAl Viro if ((flag & O_ACCMODE) == 3) 24288a5e929dSAl Viro flag--; 2429d57999e1SDave Hansen return flag; 2430d57999e1SDave Hansen } 2431d57999e1SDave Hansen 2432d18e9008SMiklos Szeredi static int may_o_create(struct path *dir, struct dentry *dentry, umode_t mode) 2433d18e9008SMiklos Szeredi { 2434d18e9008SMiklos Szeredi int error = security_path_mknod(dir, dentry, mode, 0); 2435d18e9008SMiklos Szeredi if (error) 2436d18e9008SMiklos Szeredi return error; 2437d18e9008SMiklos Szeredi 2438d18e9008SMiklos Szeredi error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC); 2439d18e9008SMiklos Szeredi if (error) 2440d18e9008SMiklos Szeredi return error; 2441d18e9008SMiklos Szeredi 2442d18e9008SMiklos Szeredi return security_inode_create(dir->dentry->d_inode, dentry, mode); 2443d18e9008SMiklos Szeredi } 2444d18e9008SMiklos Szeredi 24451acf0af9SDavid Howells /* 24461acf0af9SDavid Howells * Attempt to atomically look up, create and open a file from a negative 24471acf0af9SDavid Howells * dentry. 24481acf0af9SDavid Howells * 24491acf0af9SDavid Howells * Returns 0 if successful. The file will have been created and attached to 24501acf0af9SDavid Howells * @file by the filesystem calling finish_open(). 24511acf0af9SDavid Howells * 24521acf0af9SDavid Howells * Returns 1 if the file was looked up only or didn't need creating. The 24531acf0af9SDavid Howells * caller will need to perform the open themselves. @path will have been 24541acf0af9SDavid Howells * updated to point to the new dentry. This may be negative. 24551acf0af9SDavid Howells * 24561acf0af9SDavid Howells * Returns an error code otherwise. 24571acf0af9SDavid Howells */ 24582675a4ebSAl Viro static int atomic_open(struct nameidata *nd, struct dentry *dentry, 245930d90494SAl Viro struct path *path, struct file *file, 2460015c3bbcSMiklos Szeredi const struct open_flags *op, 246164894cf8SAl Viro bool got_write, bool need_lookup, 246247237687SAl Viro int *opened) 2463d18e9008SMiklos Szeredi { 2464d18e9008SMiklos Szeredi struct inode *dir = nd->path.dentry->d_inode; 2465d18e9008SMiklos Szeredi unsigned open_flag = open_to_namei_flags(op->open_flag); 2466d18e9008SMiklos Szeredi umode_t mode; 2467d18e9008SMiklos Szeredi int error; 2468d18e9008SMiklos Szeredi int acc_mode; 2469d18e9008SMiklos Szeredi int create_error = 0; 2470d18e9008SMiklos Szeredi struct dentry *const DENTRY_NOT_SET = (void *) -1UL; 2471d18e9008SMiklos Szeredi 2472d18e9008SMiklos Szeredi BUG_ON(dentry->d_inode); 2473d18e9008SMiklos Szeredi 2474d18e9008SMiklos Szeredi /* Don't create child dentry for a dead directory. */ 2475d18e9008SMiklos Szeredi if (unlikely(IS_DEADDIR(dir))) { 24762675a4ebSAl Viro error = -ENOENT; 2477d18e9008SMiklos Szeredi goto out; 2478d18e9008SMiklos Szeredi } 2479d18e9008SMiklos Szeredi 248062b259d8SMiklos Szeredi mode = op->mode; 2481d18e9008SMiklos Szeredi if ((open_flag & O_CREAT) && !IS_POSIXACL(dir)) 2482d18e9008SMiklos Szeredi mode &= ~current_umask(); 2483d18e9008SMiklos Szeredi 2484f8310c59SAl Viro if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT)) { 2485d18e9008SMiklos Szeredi open_flag &= ~O_TRUNC; 248647237687SAl Viro *opened |= FILE_CREATED; 2487d18e9008SMiklos Szeredi } 2488d18e9008SMiklos Szeredi 2489d18e9008SMiklos Szeredi /* 2490d18e9008SMiklos Szeredi * Checking write permission is tricky, bacuse we don't know if we are 2491d18e9008SMiklos Szeredi * going to actually need it: O_CREAT opens should work as long as the 2492d18e9008SMiklos Szeredi * file exists. But checking existence breaks atomicity. The trick is 2493d18e9008SMiklos Szeredi * to check access and if not granted clear O_CREAT from the flags. 2494d18e9008SMiklos Szeredi * 2495d18e9008SMiklos Szeredi * Another problem is returing the "right" error value (e.g. for an 2496d18e9008SMiklos Szeredi * O_EXCL open we want to return EEXIST not EROFS). 2497d18e9008SMiklos Szeredi */ 249864894cf8SAl Viro if (((open_flag & (O_CREAT | O_TRUNC)) || 249964894cf8SAl Viro (open_flag & O_ACCMODE) != O_RDONLY) && unlikely(!got_write)) { 250064894cf8SAl Viro if (!(open_flag & O_CREAT)) { 2501d18e9008SMiklos Szeredi /* 2502d18e9008SMiklos Szeredi * No O_CREATE -> atomicity not a requirement -> fall 2503d18e9008SMiklos Szeredi * back to lookup + open 2504d18e9008SMiklos Szeredi */ 2505d18e9008SMiklos Szeredi goto no_open; 2506d18e9008SMiklos Szeredi } else if (open_flag & (O_EXCL | O_TRUNC)) { 2507d18e9008SMiklos Szeredi /* Fall back and fail with the right error */ 250864894cf8SAl Viro create_error = -EROFS; 2509d18e9008SMiklos Szeredi goto no_open; 2510d18e9008SMiklos Szeredi } else { 2511d18e9008SMiklos Szeredi /* No side effects, safe to clear O_CREAT */ 251264894cf8SAl Viro create_error = -EROFS; 2513d18e9008SMiklos Szeredi open_flag &= ~O_CREAT; 2514d18e9008SMiklos Szeredi } 2515d18e9008SMiklos Szeredi } 2516d18e9008SMiklos Szeredi 2517d18e9008SMiklos Szeredi if (open_flag & O_CREAT) { 251838227f78SMiklos Szeredi error = may_o_create(&nd->path, dentry, mode); 2519d18e9008SMiklos Szeredi if (error) { 2520d18e9008SMiklos Szeredi create_error = error; 2521d18e9008SMiklos Szeredi if (open_flag & O_EXCL) 2522d18e9008SMiklos Szeredi goto no_open; 2523d18e9008SMiklos Szeredi open_flag &= ~O_CREAT; 2524d18e9008SMiklos Szeredi } 2525d18e9008SMiklos Szeredi } 2526d18e9008SMiklos Szeredi 2527d18e9008SMiklos Szeredi if (nd->flags & LOOKUP_DIRECTORY) 2528d18e9008SMiklos Szeredi open_flag |= O_DIRECTORY; 2529d18e9008SMiklos Szeredi 253030d90494SAl Viro file->f_path.dentry = DENTRY_NOT_SET; 253130d90494SAl Viro file->f_path.mnt = nd->path.mnt; 253230d90494SAl Viro error = dir->i_op->atomic_open(dir, dentry, file, open_flag, mode, 253347237687SAl Viro opened); 2534d9585277SAl Viro if (error < 0) { 2535d9585277SAl Viro if (create_error && error == -ENOENT) 2536d9585277SAl Viro error = create_error; 2537d18e9008SMiklos Szeredi goto out; 2538d18e9008SMiklos Szeredi } 2539d18e9008SMiklos Szeredi 2540d18e9008SMiklos Szeredi acc_mode = op->acc_mode; 254147237687SAl Viro if (*opened & FILE_CREATED) { 2542d18e9008SMiklos Szeredi fsnotify_create(dir, dentry); 2543d18e9008SMiklos Szeredi acc_mode = MAY_OPEN; 2544d18e9008SMiklos Szeredi } 2545d18e9008SMiklos Szeredi 2546d9585277SAl Viro if (error) { /* returned 1, that is */ 254730d90494SAl Viro if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) { 25482675a4ebSAl Viro error = -EIO; 2549d18e9008SMiklos Szeredi goto out; 2550d18e9008SMiklos Szeredi } 255130d90494SAl Viro if (file->f_path.dentry) { 2552d18e9008SMiklos Szeredi dput(dentry); 255330d90494SAl Viro dentry = file->f_path.dentry; 2554d18e9008SMiklos Szeredi } 255562b2ce96SSage Weil if (create_error && dentry->d_inode == NULL) { 255662b2ce96SSage Weil error = create_error; 255762b2ce96SSage Weil goto out; 255862b2ce96SSage Weil } 2559d18e9008SMiklos Szeredi goto looked_up; 2560d18e9008SMiklos Szeredi } 2561d18e9008SMiklos Szeredi 2562d18e9008SMiklos Szeredi /* 2563d18e9008SMiklos Szeredi * We didn't have the inode before the open, so check open permission 2564d18e9008SMiklos Szeredi * here. 2565d18e9008SMiklos Szeredi */ 25662675a4ebSAl Viro error = may_open(&file->f_path, acc_mode, open_flag); 25672675a4ebSAl Viro if (error) 25682675a4ebSAl Viro fput(file); 2569d18e9008SMiklos Szeredi 2570d18e9008SMiklos Szeredi out: 2571d18e9008SMiklos Szeredi dput(dentry); 25722675a4ebSAl Viro return error; 2573d18e9008SMiklos Szeredi 2574d18e9008SMiklos Szeredi no_open: 2575d18e9008SMiklos Szeredi if (need_lookup) { 257672bd866aSAl Viro dentry = lookup_real(dir, dentry, nd->flags); 2577d18e9008SMiklos Szeredi if (IS_ERR(dentry)) 25782675a4ebSAl Viro return PTR_ERR(dentry); 2579d18e9008SMiklos Szeredi 2580d18e9008SMiklos Szeredi if (create_error) { 2581d18e9008SMiklos Szeredi int open_flag = op->open_flag; 2582d18e9008SMiklos Szeredi 25832675a4ebSAl Viro error = create_error; 2584d18e9008SMiklos Szeredi if ((open_flag & O_EXCL)) { 2585d18e9008SMiklos Szeredi if (!dentry->d_inode) 2586d18e9008SMiklos Szeredi goto out; 2587d18e9008SMiklos Szeredi } else if (!dentry->d_inode) { 2588d18e9008SMiklos Szeredi goto out; 2589d18e9008SMiklos Szeredi } else if ((open_flag & O_TRUNC) && 2590d18e9008SMiklos Szeredi S_ISREG(dentry->d_inode->i_mode)) { 2591d18e9008SMiklos Szeredi goto out; 2592d18e9008SMiklos Szeredi } 2593d18e9008SMiklos Szeredi /* will fail later, go on to get the right error */ 2594d18e9008SMiklos Szeredi } 2595d18e9008SMiklos Szeredi } 2596d18e9008SMiklos Szeredi looked_up: 2597d18e9008SMiklos Szeredi path->dentry = dentry; 2598d18e9008SMiklos Szeredi path->mnt = nd->path.mnt; 25992675a4ebSAl Viro return 1; 2600d18e9008SMiklos Szeredi } 2601d18e9008SMiklos Szeredi 260231e6b01fSNick Piggin /* 26031acf0af9SDavid Howells * Look up and maybe create and open the last component. 2604d58ffd35SMiklos Szeredi * 2605d58ffd35SMiklos Szeredi * Must be called with i_mutex held on parent. 2606d58ffd35SMiklos Szeredi * 26071acf0af9SDavid Howells * Returns 0 if the file was successfully atomically created (if necessary) and 26081acf0af9SDavid Howells * opened. In this case the file will be returned attached to @file. 26091acf0af9SDavid Howells * 26101acf0af9SDavid Howells * Returns 1 if the file was not completely opened at this time, though lookups 26111acf0af9SDavid Howells * and creations will have been performed and the dentry returned in @path will 26121acf0af9SDavid Howells * be positive upon return if O_CREAT was specified. If O_CREAT wasn't 26131acf0af9SDavid Howells * specified then a negative dentry may be returned. 26141acf0af9SDavid Howells * 26151acf0af9SDavid Howells * An error code is returned otherwise. 26161acf0af9SDavid Howells * 26171acf0af9SDavid Howells * FILE_CREATE will be set in @*opened if the dentry was created and will be 26181acf0af9SDavid Howells * cleared otherwise prior to returning. 2619d58ffd35SMiklos Szeredi */ 26202675a4ebSAl Viro static int lookup_open(struct nameidata *nd, struct path *path, 262130d90494SAl Viro struct file *file, 2622d58ffd35SMiklos Szeredi const struct open_flags *op, 262364894cf8SAl Viro bool got_write, int *opened) 2624d58ffd35SMiklos Szeredi { 2625d58ffd35SMiklos Szeredi struct dentry *dir = nd->path.dentry; 262654ef4872SMiklos Szeredi struct inode *dir_inode = dir->d_inode; 2627d58ffd35SMiklos Szeredi struct dentry *dentry; 2628d58ffd35SMiklos Szeredi int error; 262954ef4872SMiklos Szeredi bool need_lookup; 2630d58ffd35SMiklos Szeredi 263147237687SAl Viro *opened &= ~FILE_CREATED; 2632201f956eSAl Viro dentry = lookup_dcache(&nd->last, dir, nd->flags, &need_lookup); 2633d58ffd35SMiklos Szeredi if (IS_ERR(dentry)) 26342675a4ebSAl Viro return PTR_ERR(dentry); 2635d58ffd35SMiklos Szeredi 2636d18e9008SMiklos Szeredi /* Cached positive dentry: will open in f_op->open */ 2637d18e9008SMiklos Szeredi if (!need_lookup && dentry->d_inode) 2638d18e9008SMiklos Szeredi goto out_no_open; 2639d18e9008SMiklos Szeredi 2640d18e9008SMiklos Szeredi if ((nd->flags & LOOKUP_OPEN) && dir_inode->i_op->atomic_open) { 264164894cf8SAl Viro return atomic_open(nd, dentry, path, file, op, got_write, 264247237687SAl Viro need_lookup, opened); 2643d18e9008SMiklos Szeredi } 2644d18e9008SMiklos Szeredi 264554ef4872SMiklos Szeredi if (need_lookup) { 264654ef4872SMiklos Szeredi BUG_ON(dentry->d_inode); 264754ef4872SMiklos Szeredi 264872bd866aSAl Viro dentry = lookup_real(dir_inode, dentry, nd->flags); 264954ef4872SMiklos Szeredi if (IS_ERR(dentry)) 26502675a4ebSAl Viro return PTR_ERR(dentry); 265154ef4872SMiklos Szeredi } 265254ef4872SMiklos Szeredi 2653d58ffd35SMiklos Szeredi /* Negative dentry, just create the file */ 2654d58ffd35SMiklos Szeredi if (!dentry->d_inode && (op->open_flag & O_CREAT)) { 2655d58ffd35SMiklos Szeredi umode_t mode = op->mode; 2656d58ffd35SMiklos Szeredi if (!IS_POSIXACL(dir->d_inode)) 2657d58ffd35SMiklos Szeredi mode &= ~current_umask(); 2658d58ffd35SMiklos Szeredi /* 2659d58ffd35SMiklos Szeredi * This write is needed to ensure that a 2660d58ffd35SMiklos Szeredi * rw->ro transition does not occur between 2661d58ffd35SMiklos Szeredi * the time when the file is created and when 2662d58ffd35SMiklos Szeredi * a permanent write count is taken through 2663015c3bbcSMiklos Szeredi * the 'struct file' in finish_open(). 2664d58ffd35SMiklos Szeredi */ 266564894cf8SAl Viro if (!got_write) { 266664894cf8SAl Viro error = -EROFS; 2667d58ffd35SMiklos Szeredi goto out_dput; 266864894cf8SAl Viro } 266947237687SAl Viro *opened |= FILE_CREATED; 2670d58ffd35SMiklos Szeredi error = security_path_mknod(&nd->path, dentry, mode, 0); 2671d58ffd35SMiklos Szeredi if (error) 2672d58ffd35SMiklos Szeredi goto out_dput; 2673312b63fbSAl Viro error = vfs_create(dir->d_inode, dentry, mode, 2674312b63fbSAl Viro nd->flags & LOOKUP_EXCL); 2675d58ffd35SMiklos Szeredi if (error) 2676d58ffd35SMiklos Szeredi goto out_dput; 2677d58ffd35SMiklos Szeredi } 2678d18e9008SMiklos Szeredi out_no_open: 2679d58ffd35SMiklos Szeredi path->dentry = dentry; 2680d58ffd35SMiklos Szeredi path->mnt = nd->path.mnt; 26812675a4ebSAl Viro return 1; 2682d58ffd35SMiklos Szeredi 2683d58ffd35SMiklos Szeredi out_dput: 2684d58ffd35SMiklos Szeredi dput(dentry); 26852675a4ebSAl Viro return error; 2686d58ffd35SMiklos Szeredi } 2687d58ffd35SMiklos Szeredi 2688d58ffd35SMiklos Szeredi /* 2689fe2d35ffSAl Viro * Handle the last step of open() 269031e6b01fSNick Piggin */ 26912675a4ebSAl Viro static int do_last(struct nameidata *nd, struct path *path, 269230d90494SAl Viro struct file *file, const struct open_flags *op, 2693669abf4eSJeff Layton int *opened, struct filename *name) 2694fb1cc555SAl Viro { 2695a1e28038SAl Viro struct dentry *dir = nd->path.dentry; 2696ca344a89SAl Viro int open_flag = op->open_flag; 269777d660a8SMiklos Szeredi bool will_truncate = (open_flag & O_TRUNC) != 0; 269864894cf8SAl Viro bool got_write = false; 2699bcda7652SAl Viro int acc_mode = op->acc_mode; 2700a1eb3315SMiklos Szeredi struct inode *inode; 270177d660a8SMiklos Szeredi bool symlink_ok = false; 270216b1c1cdSMiklos Szeredi struct path save_parent = { .dentry = NULL, .mnt = NULL }; 270316b1c1cdSMiklos Szeredi bool retried = false; 270416c2cd71SAl Viro int error; 2705fb1cc555SAl Viro 2706c3e380b0SAl Viro nd->flags &= ~LOOKUP_PARENT; 2707c3e380b0SAl Viro nd->flags |= op->intent; 2708c3e380b0SAl Viro 27091f36f774SAl Viro switch (nd->last_type) { 27101f36f774SAl Viro case LAST_DOTDOT: 2711176306f5SNeil Brown case LAST_DOT: 2712fe2d35ffSAl Viro error = handle_dots(nd, nd->last_type); 2713fe2d35ffSAl Viro if (error) 27142675a4ebSAl Viro return error; 27151f36f774SAl Viro /* fallthrough */ 27161f36f774SAl Viro case LAST_ROOT: 27179f1fafeeSAl Viro error = complete_walk(nd); 271816c2cd71SAl Viro if (error) 27192675a4ebSAl Viro return error; 2720adb5c247SJeff Layton audit_inode(name, nd->path.dentry, 0); 2721ca344a89SAl Viro if (open_flag & O_CREAT) { 272216c2cd71SAl Viro error = -EISDIR; 27232675a4ebSAl Viro goto out; 2724fe2d35ffSAl Viro } 2725e83db167SMiklos Szeredi goto finish_open; 27261f36f774SAl Viro case LAST_BIND: 27279f1fafeeSAl Viro error = complete_walk(nd); 272816c2cd71SAl Viro if (error) 27292675a4ebSAl Viro return error; 2730adb5c247SJeff Layton audit_inode(name, dir, 0); 2731e83db167SMiklos Szeredi goto finish_open; 27321f36f774SAl Viro } 2733a2c36b45SAl Viro 2734ca344a89SAl Viro if (!(open_flag & O_CREAT)) { 2735fe2d35ffSAl Viro if (nd->last.name[nd->last.len]) 2736fe2d35ffSAl Viro nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 2737bcda7652SAl Viro if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW)) 273877d660a8SMiklos Szeredi symlink_ok = true; 2739fe2d35ffSAl Viro /* we _can_ be in RCU mode here */ 2740a1eb3315SMiklos Szeredi error = lookup_fast(nd, &nd->last, path, &inode); 274171574865SMiklos Szeredi if (likely(!error)) 274271574865SMiklos Szeredi goto finish_lookup; 274371574865SMiklos Szeredi 2744ce57dfc1SAl Viro if (error < 0) 27452675a4ebSAl Viro goto out; 2746a1eb3315SMiklos Szeredi 274737d7fffcSMiklos Szeredi BUG_ON(nd->inode != dir->d_inode); 2748b6183df7SMiklos Szeredi } else { 2749fe2d35ffSAl Viro /* create side of things */ 2750a3fbbde7SAl Viro /* 2751b6183df7SMiklos Szeredi * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED 2752b6183df7SMiklos Szeredi * has been cleared when we got to the last component we are 2753b6183df7SMiklos Szeredi * about to look up 2754a3fbbde7SAl Viro */ 27559f1fafeeSAl Viro error = complete_walk(nd); 27569f1fafeeSAl Viro if (error) 27572675a4ebSAl Viro return error; 2758fe2d35ffSAl Viro 2759adb5c247SJeff Layton audit_inode(name, dir, 0); 276016c2cd71SAl Viro error = -EISDIR; 27611f36f774SAl Viro /* trailing slashes? */ 276231e6b01fSNick Piggin if (nd->last.name[nd->last.len]) 27632675a4ebSAl Viro goto out; 2764b6183df7SMiklos Szeredi } 27651f36f774SAl Viro 276616b1c1cdSMiklos Szeredi retry_lookup: 276764894cf8SAl Viro if (op->open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) { 276864894cf8SAl Viro error = mnt_want_write(nd->path.mnt); 276964894cf8SAl Viro if (!error) 277064894cf8SAl Viro got_write = true; 277164894cf8SAl Viro /* 277264894cf8SAl Viro * do _not_ fail yet - we might not need that or fail with 277364894cf8SAl Viro * a different error; let lookup_open() decide; we'll be 277464894cf8SAl Viro * dropping this one anyway. 277564894cf8SAl Viro */ 277664894cf8SAl Viro } 2777a1e28038SAl Viro mutex_lock(&dir->d_inode->i_mutex); 277864894cf8SAl Viro error = lookup_open(nd, path, file, op, got_write, opened); 2779fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2780fb1cc555SAl Viro 27812675a4ebSAl Viro if (error <= 0) { 27822675a4ebSAl Viro if (error) 2783d58ffd35SMiklos Szeredi goto out; 27846c0d46c4SAl Viro 278547237687SAl Viro if ((*opened & FILE_CREATED) || 27862675a4ebSAl Viro !S_ISREG(file->f_path.dentry->d_inode->i_mode)) 278777d660a8SMiklos Szeredi will_truncate = false; 2788d18e9008SMiklos Szeredi 2789adb5c247SJeff Layton audit_inode(name, file->f_path.dentry, 0); 2790d18e9008SMiklos Szeredi goto opened; 2791d18e9008SMiklos Szeredi } 2792d18e9008SMiklos Szeredi 279347237687SAl Viro if (*opened & FILE_CREATED) { 27949b44f1b3SAl Viro /* Don't check for write permission, don't truncate */ 2795ca344a89SAl Viro open_flag &= ~O_TRUNC; 279677d660a8SMiklos Szeredi will_truncate = false; 2797bcda7652SAl Viro acc_mode = MAY_OPEN; 2798d58ffd35SMiklos Szeredi path_to_nameidata(path, nd); 2799e83db167SMiklos Szeredi goto finish_open_created; 2800fb1cc555SAl Viro } 2801fb1cc555SAl Viro 2802fb1cc555SAl Viro /* 28033134f37eSJeff Layton * create/update audit record if it already exists. 2804fb1cc555SAl Viro */ 28053134f37eSJeff Layton if (path->dentry->d_inode) 2806adb5c247SJeff Layton audit_inode(name, path->dentry, 0); 2807fb1cc555SAl Viro 2808d18e9008SMiklos Szeredi /* 2809d18e9008SMiklos Szeredi * If atomic_open() acquired write access it is dropped now due to 2810d18e9008SMiklos Szeredi * possible mount and symlink following (this might be optimized away if 2811d18e9008SMiklos Szeredi * necessary...) 2812d18e9008SMiklos Szeredi */ 281364894cf8SAl Viro if (got_write) { 2814d18e9008SMiklos Szeredi mnt_drop_write(nd->path.mnt); 281564894cf8SAl Viro got_write = false; 2816d18e9008SMiklos Szeredi } 2817d18e9008SMiklos Szeredi 2818fb1cc555SAl Viro error = -EEXIST; 2819f8310c59SAl Viro if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT)) 2820fb1cc555SAl Viro goto exit_dput; 2821fb1cc555SAl Viro 28229875cf80SDavid Howells error = follow_managed(path, nd->flags); 28239875cf80SDavid Howells if (error < 0) 2824fb1cc555SAl Viro goto exit_dput; 2825fb1cc555SAl Viro 2826a3fbbde7SAl Viro if (error) 2827a3fbbde7SAl Viro nd->flags |= LOOKUP_JUMPED; 2828a3fbbde7SAl Viro 2829decf3400SMiklos Szeredi BUG_ON(nd->flags & LOOKUP_RCU); 2830decf3400SMiklos Szeredi inode = path->dentry->d_inode; 28315f5daac1SMiklos Szeredi finish_lookup: 28325f5daac1SMiklos Szeredi /* we _can_ be in RCU mode here */ 2833fb1cc555SAl Viro error = -ENOENT; 283454c33e7fSMiklos Szeredi if (!inode) { 283554c33e7fSMiklos Szeredi path_to_nameidata(path, nd); 28362675a4ebSAl Viro goto out; 283754c33e7fSMiklos Szeredi } 28389e67f361SAl Viro 2839d45ea867SMiklos Szeredi if (should_follow_link(inode, !symlink_ok)) { 2840d45ea867SMiklos Szeredi if (nd->flags & LOOKUP_RCU) { 2841d45ea867SMiklos Szeredi if (unlikely(unlazy_walk(nd, path->dentry))) { 2842d45ea867SMiklos Szeredi error = -ECHILD; 28432675a4ebSAl Viro goto out; 2844d45ea867SMiklos Szeredi } 2845d45ea867SMiklos Szeredi } 2846d45ea867SMiklos Szeredi BUG_ON(inode != path->dentry->d_inode); 28472675a4ebSAl Viro return 1; 2848d45ea867SMiklos Szeredi } 2849fb1cc555SAl Viro 285016b1c1cdSMiklos Szeredi if ((nd->flags & LOOKUP_RCU) || nd->path.mnt != path->mnt) { 2851fb1cc555SAl Viro path_to_nameidata(path, nd); 285216b1c1cdSMiklos Szeredi } else { 285316b1c1cdSMiklos Szeredi save_parent.dentry = nd->path.dentry; 285416b1c1cdSMiklos Szeredi save_parent.mnt = mntget(path->mnt); 285516b1c1cdSMiklos Szeredi nd->path.dentry = path->dentry; 285616b1c1cdSMiklos Szeredi 285716b1c1cdSMiklos Szeredi } 2858decf3400SMiklos Szeredi nd->inode = inode; 2859a3fbbde7SAl Viro /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */ 2860a3fbbde7SAl Viro error = complete_walk(nd); 286116b1c1cdSMiklos Szeredi if (error) { 286216b1c1cdSMiklos Szeredi path_put(&save_parent); 28632675a4ebSAl Viro return error; 286416b1c1cdSMiklos Szeredi } 2865fb1cc555SAl Viro error = -EISDIR; 2866050ac841SMiklos Szeredi if ((open_flag & O_CREAT) && S_ISDIR(nd->inode->i_mode)) 28672675a4ebSAl Viro goto out; 2868af2f5542SMiklos Szeredi error = -ENOTDIR; 2869af2f5542SMiklos Szeredi if ((nd->flags & LOOKUP_DIRECTORY) && !nd->inode->i_op->lookup) 28702675a4ebSAl Viro goto out; 2871adb5c247SJeff Layton audit_inode(name, nd->path.dentry, 0); 2872e83db167SMiklos Szeredi finish_open: 28736c0d46c4SAl Viro if (!S_ISREG(nd->inode->i_mode)) 287477d660a8SMiklos Szeredi will_truncate = false; 28756c0d46c4SAl Viro 28760f9d1a10SAl Viro if (will_truncate) { 28770f9d1a10SAl Viro error = mnt_want_write(nd->path.mnt); 28780f9d1a10SAl Viro if (error) 28792675a4ebSAl Viro goto out; 288064894cf8SAl Viro got_write = true; 28810f9d1a10SAl Viro } 2882e83db167SMiklos Szeredi finish_open_created: 2883bcda7652SAl Viro error = may_open(&nd->path, acc_mode, open_flag); 2884ca344a89SAl Viro if (error) 28852675a4ebSAl Viro goto out; 288630d90494SAl Viro file->f_path.mnt = nd->path.mnt; 288730d90494SAl Viro error = finish_open(file, nd->path.dentry, NULL, opened); 288830d90494SAl Viro if (error) { 288930d90494SAl Viro if (error == -EOPENSTALE) 2890f60dc3dbSMiklos Szeredi goto stale_open; 2891015c3bbcSMiklos Szeredi goto out; 2892f60dc3dbSMiklos Szeredi } 2893a8277b9bSMiklos Szeredi opened: 28942675a4ebSAl Viro error = open_check_o_direct(file); 2895015c3bbcSMiklos Szeredi if (error) 2896015c3bbcSMiklos Szeredi goto exit_fput; 28972675a4ebSAl Viro error = ima_file_check(file, op->acc_mode); 2898aa4caadbSMiklos Szeredi if (error) 2899aa4caadbSMiklos Szeredi goto exit_fput; 2900aa4caadbSMiklos Szeredi 29010f9d1a10SAl Viro if (will_truncate) { 29022675a4ebSAl Viro error = handle_truncate(file); 2903aa4caadbSMiklos Szeredi if (error) 2904aa4caadbSMiklos Szeredi goto exit_fput; 29050f9d1a10SAl Viro } 2906ca344a89SAl Viro out: 290764894cf8SAl Viro if (got_write) 29080f9d1a10SAl Viro mnt_drop_write(nd->path.mnt); 290916b1c1cdSMiklos Szeredi path_put(&save_parent); 2910e276ae67SMiklos Szeredi terminate_walk(nd); 29112675a4ebSAl Viro return error; 2912fb1cc555SAl Viro 2913fb1cc555SAl Viro exit_dput: 2914fb1cc555SAl Viro path_put_conditional(path, nd); 2915ca344a89SAl Viro goto out; 2916015c3bbcSMiklos Szeredi exit_fput: 29172675a4ebSAl Viro fput(file); 29182675a4ebSAl Viro goto out; 2919015c3bbcSMiklos Szeredi 2920f60dc3dbSMiklos Szeredi stale_open: 2921f60dc3dbSMiklos Szeredi /* If no saved parent or already retried then can't retry */ 2922f60dc3dbSMiklos Szeredi if (!save_parent.dentry || retried) 2923f60dc3dbSMiklos Szeredi goto out; 2924f60dc3dbSMiklos Szeredi 2925f60dc3dbSMiklos Szeredi BUG_ON(save_parent.dentry != dir); 2926f60dc3dbSMiklos Szeredi path_put(&nd->path); 2927f60dc3dbSMiklos Szeredi nd->path = save_parent; 2928f60dc3dbSMiklos Szeredi nd->inode = dir->d_inode; 2929f60dc3dbSMiklos Szeredi save_parent.mnt = NULL; 2930f60dc3dbSMiklos Szeredi save_parent.dentry = NULL; 293164894cf8SAl Viro if (got_write) { 2932f60dc3dbSMiklos Szeredi mnt_drop_write(nd->path.mnt); 293364894cf8SAl Viro got_write = false; 2934f60dc3dbSMiklos Szeredi } 2935f60dc3dbSMiklos Szeredi retried = true; 2936f60dc3dbSMiklos Szeredi goto retry_lookup; 2937fb1cc555SAl Viro } 2938fb1cc555SAl Viro 2939669abf4eSJeff Layton static struct file *path_openat(int dfd, struct filename *pathname, 294073d049a4SAl Viro struct nameidata *nd, const struct open_flags *op, int flags) 29411da177e4SLinus Torvalds { 2942fe2d35ffSAl Viro struct file *base = NULL; 294330d90494SAl Viro struct file *file; 29449850c056SAl Viro struct path path; 294547237687SAl Viro int opened = 0; 294613aab428SAl Viro int error; 294731e6b01fSNick Piggin 294830d90494SAl Viro file = get_empty_filp(); 294930d90494SAl Viro if (!file) 295031e6b01fSNick Piggin return ERR_PTR(-ENFILE); 295131e6b01fSNick Piggin 295230d90494SAl Viro file->f_flags = op->open_flag; 295331e6b01fSNick Piggin 2954669abf4eSJeff Layton error = path_init(dfd, pathname->name, flags | LOOKUP_PARENT, nd, &base); 295531e6b01fSNick Piggin if (unlikely(error)) 29562675a4ebSAl Viro goto out; 295731e6b01fSNick Piggin 2958fe2d35ffSAl Viro current->total_link_count = 0; 2959669abf4eSJeff Layton error = link_path_walk(pathname->name, nd); 296031e6b01fSNick Piggin if (unlikely(error)) 29612675a4ebSAl Viro goto out; 29621da177e4SLinus Torvalds 29632675a4ebSAl Viro error = do_last(nd, &path, file, op, &opened, pathname); 29642675a4ebSAl Viro while (unlikely(error > 0)) { /* trailing symlink */ 29657b9337aaSNick Piggin struct path link = path; 2966def4af30SAl Viro void *cookie; 2967574197e0SAl Viro if (!(nd->flags & LOOKUP_FOLLOW)) { 296873d049a4SAl Viro path_put_conditional(&path, nd); 296973d049a4SAl Viro path_put(&nd->path); 29702675a4ebSAl Viro error = -ELOOP; 297140b39136SAl Viro break; 297240b39136SAl Viro } 2973800179c9SKees Cook error = may_follow_link(&link, nd); 2974800179c9SKees Cook if (unlikely(error)) 2975800179c9SKees Cook break; 297673d049a4SAl Viro nd->flags |= LOOKUP_PARENT; 297773d049a4SAl Viro nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL); 2978574197e0SAl Viro error = follow_link(&link, nd, &cookie); 2979c3e380b0SAl Viro if (unlikely(error)) 29802675a4ebSAl Viro break; 29812675a4ebSAl Viro error = do_last(nd, &path, file, op, &opened, pathname); 2982574197e0SAl Viro put_link(nd, &link, cookie); 2983806b681cSAl Viro } 298410fa8e62SAl Viro out: 298573d049a4SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) 298673d049a4SAl Viro path_put(&nd->root); 2987fe2d35ffSAl Viro if (base) 2988fe2d35ffSAl Viro fput(base); 29892675a4ebSAl Viro if (!(opened & FILE_OPENED)) { 29902675a4ebSAl Viro BUG_ON(!error); 299130d90494SAl Viro put_filp(file); 2992015c3bbcSMiklos Szeredi } 29932675a4ebSAl Viro if (unlikely(error)) { 29942675a4ebSAl Viro if (error == -EOPENSTALE) { 29952675a4ebSAl Viro if (flags & LOOKUP_RCU) 29962675a4ebSAl Viro error = -ECHILD; 29972675a4ebSAl Viro else 29982675a4ebSAl Viro error = -ESTALE; 29992675a4ebSAl Viro } 30002675a4ebSAl Viro file = ERR_PTR(error); 30012675a4ebSAl Viro } 30022675a4ebSAl Viro return file; 3003de459215SKirill Korotaev } 30041da177e4SLinus Torvalds 3005669abf4eSJeff Layton struct file *do_filp_open(int dfd, struct filename *pathname, 300613aab428SAl Viro const struct open_flags *op, int flags) 300713aab428SAl Viro { 300873d049a4SAl Viro struct nameidata nd; 300913aab428SAl Viro struct file *filp; 301013aab428SAl Viro 301173d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU); 301213aab428SAl Viro if (unlikely(filp == ERR_PTR(-ECHILD))) 301373d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags); 301413aab428SAl Viro if (unlikely(filp == ERR_PTR(-ESTALE))) 301573d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL); 301613aab428SAl Viro return filp; 301713aab428SAl Viro } 301813aab428SAl Viro 301973d049a4SAl Viro struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt, 302073d049a4SAl Viro const char *name, const struct open_flags *op, int flags) 302173d049a4SAl Viro { 302273d049a4SAl Viro struct nameidata nd; 302373d049a4SAl Viro struct file *file; 3024669abf4eSJeff Layton struct filename filename = { .name = name }; 302573d049a4SAl Viro 302673d049a4SAl Viro nd.root.mnt = mnt; 302773d049a4SAl Viro nd.root.dentry = dentry; 302873d049a4SAl Viro 302973d049a4SAl Viro flags |= LOOKUP_ROOT; 303073d049a4SAl Viro 3031bcda7652SAl Viro if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN) 303273d049a4SAl Viro return ERR_PTR(-ELOOP); 303373d049a4SAl Viro 3034669abf4eSJeff Layton file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_RCU); 303573d049a4SAl Viro if (unlikely(file == ERR_PTR(-ECHILD))) 3036669abf4eSJeff Layton file = path_openat(-1, &filename, &nd, op, flags); 303773d049a4SAl Viro if (unlikely(file == ERR_PTR(-ESTALE))) 3038669abf4eSJeff Layton file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_REVAL); 303973d049a4SAl Viro return file; 304073d049a4SAl Viro } 304173d049a4SAl Viro 3042ed75e95dSAl Viro struct dentry *kern_path_create(int dfd, const char *pathname, struct path *path, int is_dir) 30431da177e4SLinus Torvalds { 3044c663e5d8SChristoph Hellwig struct dentry *dentry = ERR_PTR(-EEXIST); 3045ed75e95dSAl Viro struct nameidata nd; 3046c30dabfeSJan Kara int err2; 3047ed75e95dSAl Viro int error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd); 3048ed75e95dSAl Viro if (error) 3049ed75e95dSAl Viro return ERR_PTR(error); 30501da177e4SLinus Torvalds 3051c663e5d8SChristoph Hellwig /* 3052c663e5d8SChristoph Hellwig * Yucky last component or no last component at all? 3053c663e5d8SChristoph Hellwig * (foo/., foo/.., /////) 3054c663e5d8SChristoph Hellwig */ 3055ed75e95dSAl Viro if (nd.last_type != LAST_NORM) 3056ed75e95dSAl Viro goto out; 3057ed75e95dSAl Viro nd.flags &= ~LOOKUP_PARENT; 3058ed75e95dSAl Viro nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL; 3059c663e5d8SChristoph Hellwig 3060c30dabfeSJan Kara /* don't fail immediately if it's r/o, at least try to report other errors */ 3061c30dabfeSJan Kara err2 = mnt_want_write(nd.path.mnt); 3062c663e5d8SChristoph Hellwig /* 3063c663e5d8SChristoph Hellwig * Do the final lookup. 3064c663e5d8SChristoph Hellwig */ 3065ed75e95dSAl Viro mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 3066ed75e95dSAl Viro dentry = lookup_hash(&nd); 30671da177e4SLinus Torvalds if (IS_ERR(dentry)) 3068a8104a9fSAl Viro goto unlock; 3069c663e5d8SChristoph Hellwig 3070a8104a9fSAl Viro error = -EEXIST; 3071e9baf6e5SAl Viro if (dentry->d_inode) 3072a8104a9fSAl Viro goto fail; 3073c663e5d8SChristoph Hellwig /* 3074c663e5d8SChristoph Hellwig * Special case - lookup gave negative, but... we had foo/bar/ 3075c663e5d8SChristoph Hellwig * From the vfs_mknod() POV we just have a negative dentry - 3076c663e5d8SChristoph Hellwig * all is fine. Let's be bastards - you had / on the end, you've 3077c663e5d8SChristoph Hellwig * been asking for (non-existent) directory. -ENOENT for you. 3078c663e5d8SChristoph Hellwig */ 3079ed75e95dSAl Viro if (unlikely(!is_dir && nd.last.name[nd.last.len])) { 3080a8104a9fSAl Viro error = -ENOENT; 3081ed75e95dSAl Viro goto fail; 3082e9baf6e5SAl Viro } 3083c30dabfeSJan Kara if (unlikely(err2)) { 3084c30dabfeSJan Kara error = err2; 3085a8104a9fSAl Viro goto fail; 3086c30dabfeSJan Kara } 3087ed75e95dSAl Viro *path = nd.path; 3088e9baf6e5SAl Viro return dentry; 30891da177e4SLinus Torvalds fail: 3090a8104a9fSAl Viro dput(dentry); 3091a8104a9fSAl Viro dentry = ERR_PTR(error); 3092a8104a9fSAl Viro unlock: 3093dae6ad8fSAl Viro mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 3094c30dabfeSJan Kara if (!err2) 3095c30dabfeSJan Kara mnt_drop_write(nd.path.mnt); 3096ed75e95dSAl Viro out: 3097dae6ad8fSAl Viro path_put(&nd.path); 3098ed75e95dSAl Viro return dentry; 3099dae6ad8fSAl Viro } 3100dae6ad8fSAl Viro EXPORT_SYMBOL(kern_path_create); 3101dae6ad8fSAl Viro 3102921a1650SAl Viro void done_path_create(struct path *path, struct dentry *dentry) 3103921a1650SAl Viro { 3104921a1650SAl Viro dput(dentry); 3105921a1650SAl Viro mutex_unlock(&path->dentry->d_inode->i_mutex); 3106a8104a9fSAl Viro mnt_drop_write(path->mnt); 3107921a1650SAl Viro path_put(path); 3108921a1650SAl Viro } 3109921a1650SAl Viro EXPORT_SYMBOL(done_path_create); 3110921a1650SAl Viro 3111dae6ad8fSAl Viro struct dentry *user_path_create(int dfd, const char __user *pathname, struct path *path, int is_dir) 3112dae6ad8fSAl Viro { 311391a27b2aSJeff Layton struct filename *tmp = getname(pathname); 3114dae6ad8fSAl Viro struct dentry *res; 3115dae6ad8fSAl Viro if (IS_ERR(tmp)) 3116dae6ad8fSAl Viro return ERR_CAST(tmp); 311791a27b2aSJeff Layton res = kern_path_create(dfd, tmp->name, path, is_dir); 3118dae6ad8fSAl Viro putname(tmp); 3119dae6ad8fSAl Viro return res; 3120dae6ad8fSAl Viro } 3121dae6ad8fSAl Viro EXPORT_SYMBOL(user_path_create); 3122dae6ad8fSAl Viro 31231a67aafbSAl Viro int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev) 31241da177e4SLinus Torvalds { 3125a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 31261da177e4SLinus Torvalds 31271da177e4SLinus Torvalds if (error) 31281da177e4SLinus Torvalds return error; 31291da177e4SLinus Torvalds 3130975d6b39SEric W. Biederman if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD)) 31311da177e4SLinus Torvalds return -EPERM; 31321da177e4SLinus Torvalds 3133acfa4380SAl Viro if (!dir->i_op->mknod) 31341da177e4SLinus Torvalds return -EPERM; 31351da177e4SLinus Torvalds 313608ce5f16SSerge E. Hallyn error = devcgroup_inode_mknod(mode, dev); 313708ce5f16SSerge E. Hallyn if (error) 313808ce5f16SSerge E. Hallyn return error; 313908ce5f16SSerge E. Hallyn 31401da177e4SLinus Torvalds error = security_inode_mknod(dir, dentry, mode, dev); 31411da177e4SLinus Torvalds if (error) 31421da177e4SLinus Torvalds return error; 31431da177e4SLinus Torvalds 31441da177e4SLinus Torvalds error = dir->i_op->mknod(dir, dentry, mode, dev); 3145a74574aaSStephen Smalley if (!error) 3146f38aa942SAmy Griffis fsnotify_create(dir, dentry); 31471da177e4SLinus Torvalds return error; 31481da177e4SLinus Torvalds } 31491da177e4SLinus Torvalds 3150f69aac00SAl Viro static int may_mknod(umode_t mode) 3151463c3197SDave Hansen { 3152463c3197SDave Hansen switch (mode & S_IFMT) { 3153463c3197SDave Hansen case S_IFREG: 3154463c3197SDave Hansen case S_IFCHR: 3155463c3197SDave Hansen case S_IFBLK: 3156463c3197SDave Hansen case S_IFIFO: 3157463c3197SDave Hansen case S_IFSOCK: 3158463c3197SDave Hansen case 0: /* zero mode translates to S_IFREG */ 3159463c3197SDave Hansen return 0; 3160463c3197SDave Hansen case S_IFDIR: 3161463c3197SDave Hansen return -EPERM; 3162463c3197SDave Hansen default: 3163463c3197SDave Hansen return -EINVAL; 3164463c3197SDave Hansen } 3165463c3197SDave Hansen } 3166463c3197SDave Hansen 31678208a22bSAl Viro SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode, 31682e4d0924SHeiko Carstens unsigned, dev) 31691da177e4SLinus Torvalds { 31701da177e4SLinus Torvalds struct dentry *dentry; 3171dae6ad8fSAl Viro struct path path; 3172dae6ad8fSAl Viro int error; 31731da177e4SLinus Torvalds 31748e4bfca1SAl Viro error = may_mknod(mode); 31758e4bfca1SAl Viro if (error) 31768e4bfca1SAl Viro return error; 31771da177e4SLinus Torvalds 3178dae6ad8fSAl Viro dentry = user_path_create(dfd, filename, &path, 0); 3179dae6ad8fSAl Viro if (IS_ERR(dentry)) 3180dae6ad8fSAl Viro return PTR_ERR(dentry); 31812ad94ae6SAl Viro 3182dae6ad8fSAl Viro if (!IS_POSIXACL(path.dentry->d_inode)) 3183ce3b0f8dSAl Viro mode &= ~current_umask(); 3184dae6ad8fSAl Viro error = security_path_mknod(&path, dentry, mode, dev); 3185be6d3e56SKentaro Takeda if (error) 3186a8104a9fSAl Viro goto out; 31871da177e4SLinus Torvalds switch (mode & S_IFMT) { 31881da177e4SLinus Torvalds case 0: case S_IFREG: 3189312b63fbSAl Viro error = vfs_create(path.dentry->d_inode,dentry,mode,true); 31901da177e4SLinus Torvalds break; 31911da177e4SLinus Torvalds case S_IFCHR: case S_IFBLK: 3192dae6ad8fSAl Viro error = vfs_mknod(path.dentry->d_inode,dentry,mode, 31931da177e4SLinus Torvalds new_decode_dev(dev)); 31941da177e4SLinus Torvalds break; 31951da177e4SLinus Torvalds case S_IFIFO: case S_IFSOCK: 3196dae6ad8fSAl Viro error = vfs_mknod(path.dentry->d_inode,dentry,mode,0); 31971da177e4SLinus Torvalds break; 31981da177e4SLinus Torvalds } 3199a8104a9fSAl Viro out: 3200921a1650SAl Viro done_path_create(&path, dentry); 32011da177e4SLinus Torvalds return error; 32021da177e4SLinus Torvalds } 32031da177e4SLinus Torvalds 32048208a22bSAl Viro SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev) 32055590ff0dSUlrich Drepper { 32065590ff0dSUlrich Drepper return sys_mknodat(AT_FDCWD, filename, mode, dev); 32075590ff0dSUlrich Drepper } 32085590ff0dSUlrich Drepper 320918bb1db3SAl Viro int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) 32101da177e4SLinus Torvalds { 3211a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 32128de52778SAl Viro unsigned max_links = dir->i_sb->s_max_links; 32131da177e4SLinus Torvalds 32141da177e4SLinus Torvalds if (error) 32151da177e4SLinus Torvalds return error; 32161da177e4SLinus Torvalds 3217acfa4380SAl Viro if (!dir->i_op->mkdir) 32181da177e4SLinus Torvalds return -EPERM; 32191da177e4SLinus Torvalds 32201da177e4SLinus Torvalds mode &= (S_IRWXUGO|S_ISVTX); 32211da177e4SLinus Torvalds error = security_inode_mkdir(dir, dentry, mode); 32221da177e4SLinus Torvalds if (error) 32231da177e4SLinus Torvalds return error; 32241da177e4SLinus Torvalds 32258de52778SAl Viro if (max_links && dir->i_nlink >= max_links) 32268de52778SAl Viro return -EMLINK; 32278de52778SAl Viro 32281da177e4SLinus Torvalds error = dir->i_op->mkdir(dir, dentry, mode); 3229a74574aaSStephen Smalley if (!error) 3230f38aa942SAmy Griffis fsnotify_mkdir(dir, dentry); 32311da177e4SLinus Torvalds return error; 32321da177e4SLinus Torvalds } 32331da177e4SLinus Torvalds 3234a218d0fdSAl Viro SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode) 32351da177e4SLinus Torvalds { 32366902d925SDave Hansen struct dentry *dentry; 3237dae6ad8fSAl Viro struct path path; 3238dae6ad8fSAl Viro int error; 32391da177e4SLinus Torvalds 3240dae6ad8fSAl Viro dentry = user_path_create(dfd, pathname, &path, 1); 32416902d925SDave Hansen if (IS_ERR(dentry)) 3242dae6ad8fSAl Viro return PTR_ERR(dentry); 32436902d925SDave Hansen 3244dae6ad8fSAl Viro if (!IS_POSIXACL(path.dentry->d_inode)) 3245ce3b0f8dSAl Viro mode &= ~current_umask(); 3246dae6ad8fSAl Viro error = security_path_mkdir(&path, dentry, mode); 3247a8104a9fSAl Viro if (!error) 3248dae6ad8fSAl Viro error = vfs_mkdir(path.dentry->d_inode, dentry, mode); 3249921a1650SAl Viro done_path_create(&path, dentry); 32501da177e4SLinus Torvalds return error; 32511da177e4SLinus Torvalds } 32521da177e4SLinus Torvalds 3253a218d0fdSAl Viro SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode) 32545590ff0dSUlrich Drepper { 32555590ff0dSUlrich Drepper return sys_mkdirat(AT_FDCWD, pathname, mode); 32565590ff0dSUlrich Drepper } 32575590ff0dSUlrich Drepper 32581da177e4SLinus Torvalds /* 3259a71905f0SSage Weil * The dentry_unhash() helper will try to drop the dentry early: we 3260c0d02594SJ. Bruce Fields * should have a usage count of 1 if we're the only user of this 3261a71905f0SSage Weil * dentry, and if that is true (possibly after pruning the dcache), 3262a71905f0SSage Weil * then we drop the dentry now. 32631da177e4SLinus Torvalds * 32641da177e4SLinus Torvalds * A low-level filesystem can, if it choses, legally 32651da177e4SLinus Torvalds * do a 32661da177e4SLinus Torvalds * 32671da177e4SLinus Torvalds * if (!d_unhashed(dentry)) 32681da177e4SLinus Torvalds * return -EBUSY; 32691da177e4SLinus Torvalds * 32701da177e4SLinus Torvalds * if it cannot handle the case of removing a directory 32711da177e4SLinus Torvalds * that is still in use by something else.. 32721da177e4SLinus Torvalds */ 32731da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry) 32741da177e4SLinus Torvalds { 32751da177e4SLinus Torvalds shrink_dcache_parent(dentry); 32761da177e4SLinus Torvalds spin_lock(&dentry->d_lock); 327764252c75SSage Weil if (dentry->d_count == 1) 32781da177e4SLinus Torvalds __d_drop(dentry); 32791da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 32801da177e4SLinus Torvalds } 32811da177e4SLinus Torvalds 32821da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry) 32831da177e4SLinus Torvalds { 32841da177e4SLinus Torvalds int error = may_delete(dir, dentry, 1); 32851da177e4SLinus Torvalds 32861da177e4SLinus Torvalds if (error) 32871da177e4SLinus Torvalds return error; 32881da177e4SLinus Torvalds 3289acfa4380SAl Viro if (!dir->i_op->rmdir) 32901da177e4SLinus Torvalds return -EPERM; 32911da177e4SLinus Torvalds 32921d2ef590SAl Viro dget(dentry); 32931b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 3294912dbc15SSage Weil 32951da177e4SLinus Torvalds error = -EBUSY; 3296912dbc15SSage Weil if (d_mountpoint(dentry)) 3297912dbc15SSage Weil goto out; 3298912dbc15SSage Weil 32991da177e4SLinus Torvalds error = security_inode_rmdir(dir, dentry); 3300912dbc15SSage Weil if (error) 3301912dbc15SSage Weil goto out; 3302912dbc15SSage Weil 33033cebde24SSage Weil shrink_dcache_parent(dentry); 33041da177e4SLinus Torvalds error = dir->i_op->rmdir(dir, dentry); 3305912dbc15SSage Weil if (error) 3306912dbc15SSage Weil goto out; 3307912dbc15SSage Weil 33081da177e4SLinus Torvalds dentry->d_inode->i_flags |= S_DEAD; 3309d83c49f3SAl Viro dont_mount(dentry); 33101da177e4SLinus Torvalds 3311912dbc15SSage Weil out: 3312912dbc15SSage Weil mutex_unlock(&dentry->d_inode->i_mutex); 33131d2ef590SAl Viro dput(dentry); 3314912dbc15SSage Weil if (!error) 3315912dbc15SSage Weil d_delete(dentry); 33161da177e4SLinus Torvalds return error; 33171da177e4SLinus Torvalds } 33181da177e4SLinus Torvalds 33195590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname) 33201da177e4SLinus Torvalds { 33211da177e4SLinus Torvalds int error = 0; 332291a27b2aSJeff Layton struct filename *name; 33231da177e4SLinus Torvalds struct dentry *dentry; 33241da177e4SLinus Torvalds struct nameidata nd; 33251da177e4SLinus Torvalds 332691a27b2aSJeff Layton name = user_path_parent(dfd, pathname, &nd); 332791a27b2aSJeff Layton if (IS_ERR(name)) 332891a27b2aSJeff Layton return PTR_ERR(name); 33291da177e4SLinus Torvalds 33301da177e4SLinus Torvalds switch(nd.last_type) { 33311da177e4SLinus Torvalds case LAST_DOTDOT: 33321da177e4SLinus Torvalds error = -ENOTEMPTY; 33331da177e4SLinus Torvalds goto exit1; 33341da177e4SLinus Torvalds case LAST_DOT: 33351da177e4SLinus Torvalds error = -EINVAL; 33361da177e4SLinus Torvalds goto exit1; 33371da177e4SLinus Torvalds case LAST_ROOT: 33381da177e4SLinus Torvalds error = -EBUSY; 33391da177e4SLinus Torvalds goto exit1; 33401da177e4SLinus Torvalds } 33410612d9fbSOGAWA Hirofumi 33420612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 3343c30dabfeSJan Kara error = mnt_want_write(nd.path.mnt); 3344c30dabfeSJan Kara if (error) 3345c30dabfeSJan Kara goto exit1; 33460612d9fbSOGAWA Hirofumi 33474ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 334849705b77SChristoph Hellwig dentry = lookup_hash(&nd); 33491da177e4SLinus Torvalds error = PTR_ERR(dentry); 33506902d925SDave Hansen if (IS_ERR(dentry)) 33516902d925SDave Hansen goto exit2; 3352e6bc45d6STheodore Ts'o if (!dentry->d_inode) { 3353e6bc45d6STheodore Ts'o error = -ENOENT; 3354e6bc45d6STheodore Ts'o goto exit3; 3355e6bc45d6STheodore Ts'o } 3356be6d3e56SKentaro Takeda error = security_path_rmdir(&nd.path, dentry); 3357be6d3e56SKentaro Takeda if (error) 3358c30dabfeSJan Kara goto exit3; 33594ac91378SJan Blunck error = vfs_rmdir(nd.path.dentry->d_inode, dentry); 33600622753bSDave Hansen exit3: 33611da177e4SLinus Torvalds dput(dentry); 33626902d925SDave Hansen exit2: 33634ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 3364c30dabfeSJan Kara mnt_drop_write(nd.path.mnt); 33651da177e4SLinus Torvalds exit1: 33661d957f9bSJan Blunck path_put(&nd.path); 33671da177e4SLinus Torvalds putname(name); 33681da177e4SLinus Torvalds return error; 33691da177e4SLinus Torvalds } 33701da177e4SLinus Torvalds 33713cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname) 33725590ff0dSUlrich Drepper { 33735590ff0dSUlrich Drepper return do_rmdir(AT_FDCWD, pathname); 33745590ff0dSUlrich Drepper } 33755590ff0dSUlrich Drepper 33761da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry) 33771da177e4SLinus Torvalds { 33781da177e4SLinus Torvalds int error = may_delete(dir, dentry, 0); 33791da177e4SLinus Torvalds 33801da177e4SLinus Torvalds if (error) 33811da177e4SLinus Torvalds return error; 33821da177e4SLinus Torvalds 3383acfa4380SAl Viro if (!dir->i_op->unlink) 33841da177e4SLinus Torvalds return -EPERM; 33851da177e4SLinus Torvalds 33861b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 33871da177e4SLinus Torvalds if (d_mountpoint(dentry)) 33881da177e4SLinus Torvalds error = -EBUSY; 33891da177e4SLinus Torvalds else { 33901da177e4SLinus Torvalds error = security_inode_unlink(dir, dentry); 3391bec1052eSAl Viro if (!error) { 33921da177e4SLinus Torvalds error = dir->i_op->unlink(dir, dentry); 3393bec1052eSAl Viro if (!error) 3394d83c49f3SAl Viro dont_mount(dentry); 3395bec1052eSAl Viro } 33961da177e4SLinus Torvalds } 33971b1dcc1bSJes Sorensen mutex_unlock(&dentry->d_inode->i_mutex); 33981da177e4SLinus Torvalds 33991da177e4SLinus Torvalds /* We don't d_delete() NFS sillyrenamed files--they still exist. */ 34001da177e4SLinus Torvalds if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) { 3401ece95912SJan Kara fsnotify_link_count(dentry->d_inode); 34021da177e4SLinus Torvalds d_delete(dentry); 34031da177e4SLinus Torvalds } 34040eeca283SRobert Love 34051da177e4SLinus Torvalds return error; 34061da177e4SLinus Torvalds } 34071da177e4SLinus Torvalds 34081da177e4SLinus Torvalds /* 34091da177e4SLinus Torvalds * Make sure that the actual truncation of the file will occur outside its 34101b1dcc1bSJes Sorensen * directory's i_mutex. Truncate can take a long time if there is a lot of 34111da177e4SLinus Torvalds * writeout happening, and we don't want to prevent access to the directory 34121da177e4SLinus Torvalds * while waiting on the I/O. 34131da177e4SLinus Torvalds */ 34145590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname) 34151da177e4SLinus Torvalds { 34162ad94ae6SAl Viro int error; 341791a27b2aSJeff Layton struct filename *name; 34181da177e4SLinus Torvalds struct dentry *dentry; 34191da177e4SLinus Torvalds struct nameidata nd; 34201da177e4SLinus Torvalds struct inode *inode = NULL; 34211da177e4SLinus Torvalds 342291a27b2aSJeff Layton name = user_path_parent(dfd, pathname, &nd); 342391a27b2aSJeff Layton if (IS_ERR(name)) 342491a27b2aSJeff Layton return PTR_ERR(name); 34252ad94ae6SAl Viro 34261da177e4SLinus Torvalds error = -EISDIR; 34271da177e4SLinus Torvalds if (nd.last_type != LAST_NORM) 34281da177e4SLinus Torvalds goto exit1; 34290612d9fbSOGAWA Hirofumi 34300612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 3431c30dabfeSJan Kara error = mnt_want_write(nd.path.mnt); 3432c30dabfeSJan Kara if (error) 3433c30dabfeSJan Kara goto exit1; 34340612d9fbSOGAWA Hirofumi 34354ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 343649705b77SChristoph Hellwig dentry = lookup_hash(&nd); 34371da177e4SLinus Torvalds error = PTR_ERR(dentry); 34381da177e4SLinus Torvalds if (!IS_ERR(dentry)) { 34391da177e4SLinus Torvalds /* Why not before? Because we want correct error value */ 344050338b88STörök Edwin if (nd.last.name[nd.last.len]) 344150338b88STörök Edwin goto slashes; 34421da177e4SLinus Torvalds inode = dentry->d_inode; 344350338b88STörök Edwin if (!inode) 3444e6bc45d6STheodore Ts'o goto slashes; 34457de9c6eeSAl Viro ihold(inode); 3446be6d3e56SKentaro Takeda error = security_path_unlink(&nd.path, dentry); 3447be6d3e56SKentaro Takeda if (error) 3448c30dabfeSJan Kara goto exit2; 34494ac91378SJan Blunck error = vfs_unlink(nd.path.dentry->d_inode, dentry); 34501da177e4SLinus Torvalds exit2: 34511da177e4SLinus Torvalds dput(dentry); 34521da177e4SLinus Torvalds } 34534ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 34541da177e4SLinus Torvalds if (inode) 34551da177e4SLinus Torvalds iput(inode); /* truncate the inode here */ 3456c30dabfeSJan Kara mnt_drop_write(nd.path.mnt); 34571da177e4SLinus Torvalds exit1: 34581d957f9bSJan Blunck path_put(&nd.path); 34591da177e4SLinus Torvalds putname(name); 34601da177e4SLinus Torvalds return error; 34611da177e4SLinus Torvalds 34621da177e4SLinus Torvalds slashes: 34631da177e4SLinus Torvalds error = !dentry->d_inode ? -ENOENT : 34641da177e4SLinus Torvalds S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR; 34651da177e4SLinus Torvalds goto exit2; 34661da177e4SLinus Torvalds } 34671da177e4SLinus Torvalds 34682e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag) 34695590ff0dSUlrich Drepper { 34705590ff0dSUlrich Drepper if ((flag & ~AT_REMOVEDIR) != 0) 34715590ff0dSUlrich Drepper return -EINVAL; 34725590ff0dSUlrich Drepper 34735590ff0dSUlrich Drepper if (flag & AT_REMOVEDIR) 34745590ff0dSUlrich Drepper return do_rmdir(dfd, pathname); 34755590ff0dSUlrich Drepper 34765590ff0dSUlrich Drepper return do_unlinkat(dfd, pathname); 34775590ff0dSUlrich Drepper } 34785590ff0dSUlrich Drepper 34793480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname) 34805590ff0dSUlrich Drepper { 34815590ff0dSUlrich Drepper return do_unlinkat(AT_FDCWD, pathname); 34825590ff0dSUlrich Drepper } 34835590ff0dSUlrich Drepper 3484db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname) 34851da177e4SLinus Torvalds { 3486a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 34871da177e4SLinus Torvalds 34881da177e4SLinus Torvalds if (error) 34891da177e4SLinus Torvalds return error; 34901da177e4SLinus Torvalds 3491acfa4380SAl Viro if (!dir->i_op->symlink) 34921da177e4SLinus Torvalds return -EPERM; 34931da177e4SLinus Torvalds 34941da177e4SLinus Torvalds error = security_inode_symlink(dir, dentry, oldname); 34951da177e4SLinus Torvalds if (error) 34961da177e4SLinus Torvalds return error; 34971da177e4SLinus Torvalds 34981da177e4SLinus Torvalds error = dir->i_op->symlink(dir, dentry, oldname); 3499a74574aaSStephen Smalley if (!error) 3500f38aa942SAmy Griffis fsnotify_create(dir, dentry); 35011da177e4SLinus Torvalds return error; 35021da177e4SLinus Torvalds } 35031da177e4SLinus Torvalds 35042e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname, 35052e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 35061da177e4SLinus Torvalds { 35072ad94ae6SAl Viro int error; 350891a27b2aSJeff Layton struct filename *from; 35096902d925SDave Hansen struct dentry *dentry; 3510dae6ad8fSAl Viro struct path path; 35111da177e4SLinus Torvalds 35121da177e4SLinus Torvalds from = getname(oldname); 35131da177e4SLinus Torvalds if (IS_ERR(from)) 35141da177e4SLinus Torvalds return PTR_ERR(from); 35152ad94ae6SAl Viro 3516dae6ad8fSAl Viro dentry = user_path_create(newdfd, newname, &path, 0); 35171da177e4SLinus Torvalds error = PTR_ERR(dentry); 35186902d925SDave Hansen if (IS_ERR(dentry)) 3519dae6ad8fSAl Viro goto out_putname; 35206902d925SDave Hansen 352191a27b2aSJeff Layton error = security_path_symlink(&path, dentry, from->name); 3522a8104a9fSAl Viro if (!error) 352391a27b2aSJeff Layton error = vfs_symlink(path.dentry->d_inode, dentry, from->name); 3524921a1650SAl Viro done_path_create(&path, dentry); 35256902d925SDave Hansen out_putname: 35261da177e4SLinus Torvalds putname(from); 35271da177e4SLinus Torvalds return error; 35281da177e4SLinus Torvalds } 35291da177e4SLinus Torvalds 35303480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname) 35315590ff0dSUlrich Drepper { 35325590ff0dSUlrich Drepper return sys_symlinkat(oldname, AT_FDCWD, newname); 35335590ff0dSUlrich Drepper } 35345590ff0dSUlrich Drepper 35351da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) 35361da177e4SLinus Torvalds { 35371da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 35388de52778SAl Viro unsigned max_links = dir->i_sb->s_max_links; 35391da177e4SLinus Torvalds int error; 35401da177e4SLinus Torvalds 35411da177e4SLinus Torvalds if (!inode) 35421da177e4SLinus Torvalds return -ENOENT; 35431da177e4SLinus Torvalds 3544a95164d9SMiklos Szeredi error = may_create(dir, new_dentry); 35451da177e4SLinus Torvalds if (error) 35461da177e4SLinus Torvalds return error; 35471da177e4SLinus Torvalds 35481da177e4SLinus Torvalds if (dir->i_sb != inode->i_sb) 35491da177e4SLinus Torvalds return -EXDEV; 35501da177e4SLinus Torvalds 35511da177e4SLinus Torvalds /* 35521da177e4SLinus Torvalds * A link to an append-only or immutable file cannot be created. 35531da177e4SLinus Torvalds */ 35541da177e4SLinus Torvalds if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) 35551da177e4SLinus Torvalds return -EPERM; 3556acfa4380SAl Viro if (!dir->i_op->link) 35571da177e4SLinus Torvalds return -EPERM; 35587e79eedbSTetsuo Handa if (S_ISDIR(inode->i_mode)) 35591da177e4SLinus Torvalds return -EPERM; 35601da177e4SLinus Torvalds 35611da177e4SLinus Torvalds error = security_inode_link(old_dentry, dir, new_dentry); 35621da177e4SLinus Torvalds if (error) 35631da177e4SLinus Torvalds return error; 35641da177e4SLinus Torvalds 35657e79eedbSTetsuo Handa mutex_lock(&inode->i_mutex); 3566aae8a97dSAneesh Kumar K.V /* Make sure we don't allow creating hardlink to an unlinked file */ 3567aae8a97dSAneesh Kumar K.V if (inode->i_nlink == 0) 3568aae8a97dSAneesh Kumar K.V error = -ENOENT; 35698de52778SAl Viro else if (max_links && inode->i_nlink >= max_links) 35708de52778SAl Viro error = -EMLINK; 3571aae8a97dSAneesh Kumar K.V else 35721da177e4SLinus Torvalds error = dir->i_op->link(old_dentry, dir, new_dentry); 35737e79eedbSTetsuo Handa mutex_unlock(&inode->i_mutex); 3574e31e14ecSStephen Smalley if (!error) 35757e79eedbSTetsuo Handa fsnotify_link(dir, inode, new_dentry); 35761da177e4SLinus Torvalds return error; 35771da177e4SLinus Torvalds } 35781da177e4SLinus Torvalds 35791da177e4SLinus Torvalds /* 35801da177e4SLinus Torvalds * Hardlinks are often used in delicate situations. We avoid 35811da177e4SLinus Torvalds * security-related surprises by not following symlinks on the 35821da177e4SLinus Torvalds * newname. --KAB 35831da177e4SLinus Torvalds * 35841da177e4SLinus Torvalds * We don't follow them on the oldname either to be compatible 35851da177e4SLinus Torvalds * with linux 2.0, and to avoid hard-linking to directories 35861da177e4SLinus Torvalds * and other special files. --ADM 35871da177e4SLinus Torvalds */ 35882e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname, 35892e4d0924SHeiko Carstens int, newdfd, const char __user *, newname, int, flags) 35901da177e4SLinus Torvalds { 35911da177e4SLinus Torvalds struct dentry *new_dentry; 3592dae6ad8fSAl Viro struct path old_path, new_path; 359311a7b371SAneesh Kumar K.V int how = 0; 35941da177e4SLinus Torvalds int error; 35951da177e4SLinus Torvalds 359611a7b371SAneesh Kumar K.V if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0) 3597c04030e1SUlrich Drepper return -EINVAL; 359811a7b371SAneesh Kumar K.V /* 359911a7b371SAneesh Kumar K.V * To use null names we require CAP_DAC_READ_SEARCH 360011a7b371SAneesh Kumar K.V * This ensures that not everyone will be able to create 360111a7b371SAneesh Kumar K.V * handlink using the passed filedescriptor. 360211a7b371SAneesh Kumar K.V */ 360311a7b371SAneesh Kumar K.V if (flags & AT_EMPTY_PATH) { 360411a7b371SAneesh Kumar K.V if (!capable(CAP_DAC_READ_SEARCH)) 360511a7b371SAneesh Kumar K.V return -ENOENT; 360611a7b371SAneesh Kumar K.V how = LOOKUP_EMPTY; 360711a7b371SAneesh Kumar K.V } 3608c04030e1SUlrich Drepper 360911a7b371SAneesh Kumar K.V if (flags & AT_SYMLINK_FOLLOW) 361011a7b371SAneesh Kumar K.V how |= LOOKUP_FOLLOW; 361111a7b371SAneesh Kumar K.V 361211a7b371SAneesh Kumar K.V error = user_path_at(olddfd, oldname, how, &old_path); 36131da177e4SLinus Torvalds if (error) 36142ad94ae6SAl Viro return error; 36152ad94ae6SAl Viro 3616dae6ad8fSAl Viro new_dentry = user_path_create(newdfd, newname, &new_path, 0); 36171da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 36186902d925SDave Hansen if (IS_ERR(new_dentry)) 3619dae6ad8fSAl Viro goto out; 3620dae6ad8fSAl Viro 3621dae6ad8fSAl Viro error = -EXDEV; 3622dae6ad8fSAl Viro if (old_path.mnt != new_path.mnt) 3623dae6ad8fSAl Viro goto out_dput; 3624800179c9SKees Cook error = may_linkat(&old_path); 3625800179c9SKees Cook if (unlikely(error)) 3626800179c9SKees Cook goto out_dput; 3627dae6ad8fSAl Viro error = security_path_link(old_path.dentry, &new_path, new_dentry); 3628be6d3e56SKentaro Takeda if (error) 3629a8104a9fSAl Viro goto out_dput; 3630dae6ad8fSAl Viro error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry); 363175c3f29dSDave Hansen out_dput: 3632921a1650SAl Viro done_path_create(&new_path, new_dentry); 36331da177e4SLinus Torvalds out: 36342d8f3038SAl Viro path_put(&old_path); 36351da177e4SLinus Torvalds 36361da177e4SLinus Torvalds return error; 36371da177e4SLinus Torvalds } 36381da177e4SLinus Torvalds 36393480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname) 36405590ff0dSUlrich Drepper { 3641c04030e1SUlrich Drepper return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0); 36425590ff0dSUlrich Drepper } 36435590ff0dSUlrich Drepper 36441da177e4SLinus Torvalds /* 36451da177e4SLinus Torvalds * The worst of all namespace operations - renaming directory. "Perverted" 36461da177e4SLinus Torvalds * doesn't even start to describe it. Somebody in UCB had a heck of a trip... 36471da177e4SLinus Torvalds * Problems: 36481da177e4SLinus Torvalds * a) we can get into loop creation. Check is done in is_subdir(). 36491da177e4SLinus Torvalds * b) race potential - two innocent renames can create a loop together. 36501da177e4SLinus Torvalds * That's where 4.4 screws up. Current fix: serialization on 3651a11f3a05SArjan van de Ven * sb->s_vfs_rename_mutex. We might be more accurate, but that's another 36521da177e4SLinus Torvalds * story. 36531da177e4SLinus Torvalds * c) we have to lock _three_ objects - parents and victim (if it exists). 36541b1dcc1bSJes Sorensen * And that - after we got ->i_mutex on parents (until then we don't know 36551da177e4SLinus Torvalds * whether the target exists). Solution: try to be smart with locking 36561da177e4SLinus Torvalds * order for inodes. We rely on the fact that tree topology may change 3657a11f3a05SArjan van de Ven * only under ->s_vfs_rename_mutex _and_ that parent of the object we 36581da177e4SLinus Torvalds * move will be locked. Thus we can rank directories by the tree 36591da177e4SLinus Torvalds * (ancestors first) and rank all non-directories after them. 36601da177e4SLinus Torvalds * That works since everybody except rename does "lock parent, lookup, 3661a11f3a05SArjan van de Ven * lock child" and rename is under ->s_vfs_rename_mutex. 36621da177e4SLinus Torvalds * HOWEVER, it relies on the assumption that any object with ->lookup() 36631da177e4SLinus Torvalds * has no more than 1 dentry. If "hybrid" objects will ever appear, 36641da177e4SLinus Torvalds * we'd better make sure that there's no link(2) for them. 3665e4eaac06SSage Weil * d) conversion from fhandle to dentry may come in the wrong moment - when 36661b1dcc1bSJes Sorensen * we are removing the target. Solution: we will have to grab ->i_mutex 36671da177e4SLinus Torvalds * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on 3668c41b20e7SAdam Buchbinder * ->i_mutex on parents, which works but leads to some truly excessive 36691da177e4SLinus Torvalds * locking]. 36701da177e4SLinus Torvalds */ 367175c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry, 36721da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 36731da177e4SLinus Torvalds { 36741da177e4SLinus Torvalds int error = 0; 36759055cba7SSage Weil struct inode *target = new_dentry->d_inode; 36768de52778SAl Viro unsigned max_links = new_dir->i_sb->s_max_links; 36771da177e4SLinus Torvalds 36781da177e4SLinus Torvalds /* 36791da177e4SLinus Torvalds * If we are going to change the parent - check write permissions, 36801da177e4SLinus Torvalds * we'll need to flip '..'. 36811da177e4SLinus Torvalds */ 36821da177e4SLinus Torvalds if (new_dir != old_dir) { 3683f419a2e3SAl Viro error = inode_permission(old_dentry->d_inode, MAY_WRITE); 36841da177e4SLinus Torvalds if (error) 36851da177e4SLinus Torvalds return error; 36861da177e4SLinus Torvalds } 36871da177e4SLinus Torvalds 36881da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 36891da177e4SLinus Torvalds if (error) 36901da177e4SLinus Torvalds return error; 36911da177e4SLinus Torvalds 36921d2ef590SAl Viro dget(new_dentry); 3693d83c49f3SAl Viro if (target) 36941b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 36959055cba7SSage Weil 36961da177e4SLinus Torvalds error = -EBUSY; 36979055cba7SSage Weil if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry)) 36989055cba7SSage Weil goto out; 36999055cba7SSage Weil 37008de52778SAl Viro error = -EMLINK; 37018de52778SAl Viro if (max_links && !target && new_dir != old_dir && 37028de52778SAl Viro new_dir->i_nlink >= max_links) 37038de52778SAl Viro goto out; 37048de52778SAl Viro 37053cebde24SSage Weil if (target) 37063cebde24SSage Weil shrink_dcache_parent(new_dentry); 37071da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 37089055cba7SSage Weil if (error) 37099055cba7SSage Weil goto out; 37109055cba7SSage Weil 37111da177e4SLinus Torvalds if (target) { 37121da177e4SLinus Torvalds target->i_flags |= S_DEAD; 3713d83c49f3SAl Viro dont_mount(new_dentry); 3714d83c49f3SAl Viro } 37159055cba7SSage Weil out: 37169055cba7SSage Weil if (target) 37171b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 37181d2ef590SAl Viro dput(new_dentry); 3719e31e14ecSStephen Smalley if (!error) 3720349457ccSMark Fasheh if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) 37211da177e4SLinus Torvalds d_move(old_dentry,new_dentry); 37221da177e4SLinus Torvalds return error; 37231da177e4SLinus Torvalds } 37241da177e4SLinus Torvalds 372575c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry, 37261da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 37271da177e4SLinus Torvalds { 372851892bbbSSage Weil struct inode *target = new_dentry->d_inode; 37291da177e4SLinus Torvalds int error; 37301da177e4SLinus Torvalds 37311da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 37321da177e4SLinus Torvalds if (error) 37331da177e4SLinus Torvalds return error; 37341da177e4SLinus Torvalds 37351da177e4SLinus Torvalds dget(new_dentry); 37361da177e4SLinus Torvalds if (target) 37371b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 373851892bbbSSage Weil 37391da177e4SLinus Torvalds error = -EBUSY; 374051892bbbSSage Weil if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry)) 374151892bbbSSage Weil goto out; 374251892bbbSSage Weil 37431da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 374451892bbbSSage Weil if (error) 374551892bbbSSage Weil goto out; 374651892bbbSSage Weil 3747bec1052eSAl Viro if (target) 3748d83c49f3SAl Viro dont_mount(new_dentry); 3749349457ccSMark Fasheh if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) 37501da177e4SLinus Torvalds d_move(old_dentry, new_dentry); 375151892bbbSSage Weil out: 37521da177e4SLinus Torvalds if (target) 37531b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 37541da177e4SLinus Torvalds dput(new_dentry); 37551da177e4SLinus Torvalds return error; 37561da177e4SLinus Torvalds } 37571da177e4SLinus Torvalds 37581da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry, 37591da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 37601da177e4SLinus Torvalds { 37611da177e4SLinus Torvalds int error; 37621da177e4SLinus Torvalds int is_dir = S_ISDIR(old_dentry->d_inode->i_mode); 376359b0df21SEric Paris const unsigned char *old_name; 37641da177e4SLinus Torvalds 37651da177e4SLinus Torvalds if (old_dentry->d_inode == new_dentry->d_inode) 37661da177e4SLinus Torvalds return 0; 37671da177e4SLinus Torvalds 37681da177e4SLinus Torvalds error = may_delete(old_dir, old_dentry, is_dir); 37691da177e4SLinus Torvalds if (error) 37701da177e4SLinus Torvalds return error; 37711da177e4SLinus Torvalds 37721da177e4SLinus Torvalds if (!new_dentry->d_inode) 3773a95164d9SMiklos Szeredi error = may_create(new_dir, new_dentry); 37741da177e4SLinus Torvalds else 37751da177e4SLinus Torvalds error = may_delete(new_dir, new_dentry, is_dir); 37761da177e4SLinus Torvalds if (error) 37771da177e4SLinus Torvalds return error; 37781da177e4SLinus Torvalds 3779acfa4380SAl Viro if (!old_dir->i_op->rename) 37801da177e4SLinus Torvalds return -EPERM; 37811da177e4SLinus Torvalds 37820eeca283SRobert Love old_name = fsnotify_oldname_init(old_dentry->d_name.name); 37830eeca283SRobert Love 37841da177e4SLinus Torvalds if (is_dir) 37851da177e4SLinus Torvalds error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry); 37861da177e4SLinus Torvalds else 37871da177e4SLinus Torvalds error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry); 3788123df294SAl Viro if (!error) 3789123df294SAl Viro fsnotify_move(old_dir, new_dir, old_name, is_dir, 37905a190ae6SAl Viro new_dentry->d_inode, old_dentry); 37910eeca283SRobert Love fsnotify_oldname_free(old_name); 37920eeca283SRobert Love 37931da177e4SLinus Torvalds return error; 37941da177e4SLinus Torvalds } 37951da177e4SLinus Torvalds 37962e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname, 37972e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 37981da177e4SLinus Torvalds { 37991da177e4SLinus Torvalds struct dentry *old_dir, *new_dir; 38001da177e4SLinus Torvalds struct dentry *old_dentry, *new_dentry; 38011da177e4SLinus Torvalds struct dentry *trap; 38021da177e4SLinus Torvalds struct nameidata oldnd, newnd; 380391a27b2aSJeff Layton struct filename *from; 380491a27b2aSJeff Layton struct filename *to; 38052ad94ae6SAl Viro int error; 38061da177e4SLinus Torvalds 380791a27b2aSJeff Layton from = user_path_parent(olddfd, oldname, &oldnd); 380891a27b2aSJeff Layton if (IS_ERR(from)) { 380991a27b2aSJeff Layton error = PTR_ERR(from); 38101da177e4SLinus Torvalds goto exit; 381191a27b2aSJeff Layton } 38121da177e4SLinus Torvalds 381391a27b2aSJeff Layton to = user_path_parent(newdfd, newname, &newnd); 381491a27b2aSJeff Layton if (IS_ERR(to)) { 381591a27b2aSJeff Layton error = PTR_ERR(to); 38161da177e4SLinus Torvalds goto exit1; 381791a27b2aSJeff Layton } 38181da177e4SLinus Torvalds 38191da177e4SLinus Torvalds error = -EXDEV; 38204ac91378SJan Blunck if (oldnd.path.mnt != newnd.path.mnt) 38211da177e4SLinus Torvalds goto exit2; 38221da177e4SLinus Torvalds 38234ac91378SJan Blunck old_dir = oldnd.path.dentry; 38241da177e4SLinus Torvalds error = -EBUSY; 38251da177e4SLinus Torvalds if (oldnd.last_type != LAST_NORM) 38261da177e4SLinus Torvalds goto exit2; 38271da177e4SLinus Torvalds 38284ac91378SJan Blunck new_dir = newnd.path.dentry; 38291da177e4SLinus Torvalds if (newnd.last_type != LAST_NORM) 38301da177e4SLinus Torvalds goto exit2; 38311da177e4SLinus Torvalds 3832c30dabfeSJan Kara error = mnt_want_write(oldnd.path.mnt); 3833c30dabfeSJan Kara if (error) 3834c30dabfeSJan Kara goto exit2; 3835c30dabfeSJan Kara 38360612d9fbSOGAWA Hirofumi oldnd.flags &= ~LOOKUP_PARENT; 38370612d9fbSOGAWA Hirofumi newnd.flags &= ~LOOKUP_PARENT; 38384e9ed2f8SOGAWA Hirofumi newnd.flags |= LOOKUP_RENAME_TARGET; 38390612d9fbSOGAWA Hirofumi 38401da177e4SLinus Torvalds trap = lock_rename(new_dir, old_dir); 38411da177e4SLinus Torvalds 384249705b77SChristoph Hellwig old_dentry = lookup_hash(&oldnd); 38431da177e4SLinus Torvalds error = PTR_ERR(old_dentry); 38441da177e4SLinus Torvalds if (IS_ERR(old_dentry)) 38451da177e4SLinus Torvalds goto exit3; 38461da177e4SLinus Torvalds /* source must exist */ 38471da177e4SLinus Torvalds error = -ENOENT; 38481da177e4SLinus Torvalds if (!old_dentry->d_inode) 38491da177e4SLinus Torvalds goto exit4; 38501da177e4SLinus Torvalds /* unless the source is a directory trailing slashes give -ENOTDIR */ 38511da177e4SLinus Torvalds if (!S_ISDIR(old_dentry->d_inode->i_mode)) { 38521da177e4SLinus Torvalds error = -ENOTDIR; 38531da177e4SLinus Torvalds if (oldnd.last.name[oldnd.last.len]) 38541da177e4SLinus Torvalds goto exit4; 38551da177e4SLinus Torvalds if (newnd.last.name[newnd.last.len]) 38561da177e4SLinus Torvalds goto exit4; 38571da177e4SLinus Torvalds } 38581da177e4SLinus Torvalds /* source should not be ancestor of target */ 38591da177e4SLinus Torvalds error = -EINVAL; 38601da177e4SLinus Torvalds if (old_dentry == trap) 38611da177e4SLinus Torvalds goto exit4; 386249705b77SChristoph Hellwig new_dentry = lookup_hash(&newnd); 38631da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 38641da177e4SLinus Torvalds if (IS_ERR(new_dentry)) 38651da177e4SLinus Torvalds goto exit4; 38661da177e4SLinus Torvalds /* target should not be an ancestor of source */ 38671da177e4SLinus Torvalds error = -ENOTEMPTY; 38681da177e4SLinus Torvalds if (new_dentry == trap) 38691da177e4SLinus Torvalds goto exit5; 38701da177e4SLinus Torvalds 3871be6d3e56SKentaro Takeda error = security_path_rename(&oldnd.path, old_dentry, 3872be6d3e56SKentaro Takeda &newnd.path, new_dentry); 3873be6d3e56SKentaro Takeda if (error) 3874c30dabfeSJan Kara goto exit5; 38751da177e4SLinus Torvalds error = vfs_rename(old_dir->d_inode, old_dentry, 38761da177e4SLinus Torvalds new_dir->d_inode, new_dentry); 38771da177e4SLinus Torvalds exit5: 38781da177e4SLinus Torvalds dput(new_dentry); 38791da177e4SLinus Torvalds exit4: 38801da177e4SLinus Torvalds dput(old_dentry); 38811da177e4SLinus Torvalds exit3: 38821da177e4SLinus Torvalds unlock_rename(new_dir, old_dir); 3883c30dabfeSJan Kara mnt_drop_write(oldnd.path.mnt); 38841da177e4SLinus Torvalds exit2: 38851d957f9bSJan Blunck path_put(&newnd.path); 38862ad94ae6SAl Viro putname(to); 38871da177e4SLinus Torvalds exit1: 38881d957f9bSJan Blunck path_put(&oldnd.path); 38891da177e4SLinus Torvalds putname(from); 38902ad94ae6SAl Viro exit: 38911da177e4SLinus Torvalds return error; 38921da177e4SLinus Torvalds } 38931da177e4SLinus Torvalds 3894a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname) 38955590ff0dSUlrich Drepper { 38965590ff0dSUlrich Drepper return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname); 38975590ff0dSUlrich Drepper } 38985590ff0dSUlrich Drepper 38991da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link) 39001da177e4SLinus Torvalds { 39011da177e4SLinus Torvalds int len; 39021da177e4SLinus Torvalds 39031da177e4SLinus Torvalds len = PTR_ERR(link); 39041da177e4SLinus Torvalds if (IS_ERR(link)) 39051da177e4SLinus Torvalds goto out; 39061da177e4SLinus Torvalds 39071da177e4SLinus Torvalds len = strlen(link); 39081da177e4SLinus Torvalds if (len > (unsigned) buflen) 39091da177e4SLinus Torvalds len = buflen; 39101da177e4SLinus Torvalds if (copy_to_user(buffer, link, len)) 39111da177e4SLinus Torvalds len = -EFAULT; 39121da177e4SLinus Torvalds out: 39131da177e4SLinus Torvalds return len; 39141da177e4SLinus Torvalds } 39151da177e4SLinus Torvalds 39161da177e4SLinus Torvalds /* 39171da177e4SLinus Torvalds * A helper for ->readlink(). This should be used *ONLY* for symlinks that 39181da177e4SLinus Torvalds * have ->follow_link() touching nd only in nd_set_link(). Using (or not 39191da177e4SLinus Torvalds * using) it for any given inode is up to filesystem. 39201da177e4SLinus Torvalds */ 39211da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen) 39221da177e4SLinus Torvalds { 39231da177e4SLinus Torvalds struct nameidata nd; 3924cc314eefSLinus Torvalds void *cookie; 3925694a1764SMarcin Slusarz int res; 3926cc314eefSLinus Torvalds 39271da177e4SLinus Torvalds nd.depth = 0; 3928cc314eefSLinus Torvalds cookie = dentry->d_inode->i_op->follow_link(dentry, &nd); 3929694a1764SMarcin Slusarz if (IS_ERR(cookie)) 3930694a1764SMarcin Slusarz return PTR_ERR(cookie); 3931694a1764SMarcin Slusarz 3932694a1764SMarcin Slusarz res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd)); 39331da177e4SLinus Torvalds if (dentry->d_inode->i_op->put_link) 3934cc314eefSLinus Torvalds dentry->d_inode->i_op->put_link(dentry, &nd, cookie); 3935694a1764SMarcin Slusarz return res; 39361da177e4SLinus Torvalds } 39371da177e4SLinus Torvalds 39381da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link) 39391da177e4SLinus Torvalds { 39401da177e4SLinus Torvalds return __vfs_follow_link(nd, link); 39411da177e4SLinus Torvalds } 39421da177e4SLinus Torvalds 39431da177e4SLinus Torvalds /* get the link contents into pagecache */ 39441da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage) 39451da177e4SLinus Torvalds { 3946ebd09abbSDuane Griffin char *kaddr; 39471da177e4SLinus Torvalds struct page *page; 39481da177e4SLinus Torvalds struct address_space *mapping = dentry->d_inode->i_mapping; 3949090d2b18SPekka Enberg page = read_mapping_page(mapping, 0, NULL); 39501da177e4SLinus Torvalds if (IS_ERR(page)) 39516fe6900eSNick Piggin return (char*)page; 39521da177e4SLinus Torvalds *ppage = page; 3953ebd09abbSDuane Griffin kaddr = kmap(page); 3954ebd09abbSDuane Griffin nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1); 3955ebd09abbSDuane Griffin return kaddr; 39561da177e4SLinus Torvalds } 39571da177e4SLinus Torvalds 39581da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen) 39591da177e4SLinus Torvalds { 39601da177e4SLinus Torvalds struct page *page = NULL; 39611da177e4SLinus Torvalds char *s = page_getlink(dentry, &page); 39621da177e4SLinus Torvalds int res = vfs_readlink(dentry,buffer,buflen,s); 39631da177e4SLinus Torvalds if (page) { 39641da177e4SLinus Torvalds kunmap(page); 39651da177e4SLinus Torvalds page_cache_release(page); 39661da177e4SLinus Torvalds } 39671da177e4SLinus Torvalds return res; 39681da177e4SLinus Torvalds } 39691da177e4SLinus Torvalds 3970cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd) 39711da177e4SLinus Torvalds { 3972cc314eefSLinus Torvalds struct page *page = NULL; 39731da177e4SLinus Torvalds nd_set_link(nd, page_getlink(dentry, &page)); 3974cc314eefSLinus Torvalds return page; 39751da177e4SLinus Torvalds } 39761da177e4SLinus Torvalds 3977cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie) 39781da177e4SLinus Torvalds { 3979cc314eefSLinus Torvalds struct page *page = cookie; 3980cc314eefSLinus Torvalds 3981cc314eefSLinus Torvalds if (page) { 39821da177e4SLinus Torvalds kunmap(page); 39831da177e4SLinus Torvalds page_cache_release(page); 39841da177e4SLinus Torvalds } 39851da177e4SLinus Torvalds } 39861da177e4SLinus Torvalds 398754566b2cSNick Piggin /* 398854566b2cSNick Piggin * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS 398954566b2cSNick Piggin */ 399054566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs) 39911da177e4SLinus Torvalds { 39921da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 39930adb25d2SKirill Korotaev struct page *page; 3994afddba49SNick Piggin void *fsdata; 3995beb497abSDmitriy Monakhov int err; 39961da177e4SLinus Torvalds char *kaddr; 399754566b2cSNick Piggin unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE; 399854566b2cSNick Piggin if (nofs) 399954566b2cSNick Piggin flags |= AOP_FLAG_NOFS; 40001da177e4SLinus Torvalds 40017e53cac4SNeilBrown retry: 4002afddba49SNick Piggin err = pagecache_write_begin(NULL, mapping, 0, len-1, 400354566b2cSNick Piggin flags, &page, &fsdata); 40041da177e4SLinus Torvalds if (err) 4005afddba49SNick Piggin goto fail; 4006afddba49SNick Piggin 4007e8e3c3d6SCong Wang kaddr = kmap_atomic(page); 40081da177e4SLinus Torvalds memcpy(kaddr, symname, len-1); 4009e8e3c3d6SCong Wang kunmap_atomic(kaddr); 4010afddba49SNick Piggin 4011afddba49SNick Piggin err = pagecache_write_end(NULL, mapping, 0, len-1, len-1, 4012afddba49SNick Piggin page, fsdata); 40131da177e4SLinus Torvalds if (err < 0) 40141da177e4SLinus Torvalds goto fail; 4015afddba49SNick Piggin if (err < len-1) 4016afddba49SNick Piggin goto retry; 4017afddba49SNick Piggin 40181da177e4SLinus Torvalds mark_inode_dirty(inode); 40191da177e4SLinus Torvalds return 0; 40201da177e4SLinus Torvalds fail: 40211da177e4SLinus Torvalds return err; 40221da177e4SLinus Torvalds } 40231da177e4SLinus Torvalds 40240adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len) 40250adb25d2SKirill Korotaev { 40260adb25d2SKirill Korotaev return __page_symlink(inode, symname, len, 402754566b2cSNick Piggin !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS)); 40280adb25d2SKirill Korotaev } 40290adb25d2SKirill Korotaev 403092e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = { 40311da177e4SLinus Torvalds .readlink = generic_readlink, 40321da177e4SLinus Torvalds .follow_link = page_follow_link_light, 40331da177e4SLinus Torvalds .put_link = page_put_link, 40341da177e4SLinus Torvalds }; 40351da177e4SLinus Torvalds 40362d8f3038SAl Viro EXPORT_SYMBOL(user_path_at); 4037cc53ce53SDavid Howells EXPORT_SYMBOL(follow_down_one); 40381da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down); 40391da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up); 4040f6d2ac5cSAl Viro EXPORT_SYMBOL(get_write_access); /* nfsd */ 40411da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename); 40421da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len); 40431da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light); 40441da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link); 40451da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink); 40460adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink); 40471da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink); 40481da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations); 4049d1811465SAl Viro EXPORT_SYMBOL(kern_path); 405016f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup); 4051f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission); 40521da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename); 40531da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create); 40541da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link); 40551da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link); 40561da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir); 40571da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod); 40581da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission); 40591da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink); 40601da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename); 40611da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir); 40621da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink); 40631da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink); 40641da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash); 40651da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink); 4066