11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/fs/namei.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 1991, 1992 Linus Torvalds 51da177e4SLinus Torvalds */ 61da177e4SLinus Torvalds 71da177e4SLinus Torvalds /* 81da177e4SLinus Torvalds * Some corrections by tytso. 91da177e4SLinus Torvalds */ 101da177e4SLinus Torvalds 111da177e4SLinus Torvalds /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname 121da177e4SLinus Torvalds * lookup logic. 131da177e4SLinus Torvalds */ 141da177e4SLinus Torvalds /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture. 151da177e4SLinus Torvalds */ 161da177e4SLinus Torvalds 171da177e4SLinus Torvalds #include <linux/init.h> 18630d9c47SPaul Gortmaker #include <linux/export.h> 1944696908SDavid S. Miller #include <linux/kernel.h> 201da177e4SLinus Torvalds #include <linux/slab.h> 211da177e4SLinus Torvalds #include <linux/fs.h> 221da177e4SLinus Torvalds #include <linux/namei.h> 231da177e4SLinus Torvalds #include <linux/pagemap.h> 240eeca283SRobert Love #include <linux/fsnotify.h> 251da177e4SLinus Torvalds #include <linux/personality.h> 261da177e4SLinus Torvalds #include <linux/security.h> 276146f0d5SMimi Zohar #include <linux/ima.h> 281da177e4SLinus Torvalds #include <linux/syscalls.h> 291da177e4SLinus Torvalds #include <linux/mount.h> 301da177e4SLinus Torvalds #include <linux/audit.h> 3116f7e0feSRandy Dunlap #include <linux/capability.h> 32834f2a4aSTrond Myklebust #include <linux/file.h> 335590ff0dSUlrich Drepper #include <linux/fcntl.h> 3408ce5f16SSerge E. Hallyn #include <linux/device_cgroup.h> 355ad4e53bSAl Viro #include <linux/fs_struct.h> 36e77819e5SLinus Torvalds #include <linux/posix_acl.h> 371da177e4SLinus Torvalds #include <asm/uaccess.h> 381da177e4SLinus Torvalds 39e81e3f4dSEric Paris #include "internal.h" 40c7105365SAl Viro #include "mount.h" 41e81e3f4dSEric Paris 421da177e4SLinus Torvalds /* [Feb-1997 T. Schoebel-Theuer] 431da177e4SLinus Torvalds * Fundamental changes in the pathname lookup mechanisms (namei) 441da177e4SLinus Torvalds * were necessary because of omirr. The reason is that omirr needs 451da177e4SLinus Torvalds * to know the _real_ pathname, not the user-supplied one, in case 461da177e4SLinus Torvalds * of symlinks (and also when transname replacements occur). 471da177e4SLinus Torvalds * 481da177e4SLinus Torvalds * The new code replaces the old recursive symlink resolution with 491da177e4SLinus Torvalds * an iterative one (in case of non-nested symlink chains). It does 501da177e4SLinus Torvalds * this with calls to <fs>_follow_link(). 511da177e4SLinus Torvalds * As a side effect, dir_namei(), _namei() and follow_link() are now 521da177e4SLinus Torvalds * replaced with a single function lookup_dentry() that can handle all 531da177e4SLinus Torvalds * the special cases of the former code. 541da177e4SLinus Torvalds * 551da177e4SLinus Torvalds * With the new dcache, the pathname is stored at each inode, at least as 561da177e4SLinus Torvalds * long as the refcount of the inode is positive. As a side effect, the 571da177e4SLinus Torvalds * size of the dcache depends on the inode cache and thus is dynamic. 581da177e4SLinus Torvalds * 591da177e4SLinus Torvalds * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink 601da177e4SLinus Torvalds * resolution to correspond with current state of the code. 611da177e4SLinus Torvalds * 621da177e4SLinus Torvalds * Note that the symlink resolution is not *completely* iterative. 631da177e4SLinus Torvalds * There is still a significant amount of tail- and mid- recursion in 641da177e4SLinus Torvalds * the algorithm. Also, note that <fs>_readlink() is not used in 651da177e4SLinus Torvalds * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink() 661da177e4SLinus Torvalds * may return different results than <fs>_follow_link(). Many virtual 671da177e4SLinus Torvalds * filesystems (including /proc) exhibit this behavior. 681da177e4SLinus Torvalds */ 691da177e4SLinus Torvalds 701da177e4SLinus Torvalds /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation: 711da177e4SLinus Torvalds * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL 721da177e4SLinus Torvalds * and the name already exists in form of a symlink, try to create the new 731da177e4SLinus Torvalds * name indicated by the symlink. The old code always complained that the 741da177e4SLinus Torvalds * name already exists, due to not following the symlink even if its target 751da177e4SLinus Torvalds * is nonexistent. The new semantics affects also mknod() and link() when 7625985edcSLucas De Marchi * the name is a symlink pointing to a non-existent name. 771da177e4SLinus Torvalds * 781da177e4SLinus Torvalds * I don't know which semantics is the right one, since I have no access 791da177e4SLinus Torvalds * to standards. But I found by trial that HP-UX 9.0 has the full "new" 801da177e4SLinus Torvalds * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the 811da177e4SLinus Torvalds * "old" one. Personally, I think the new semantics is much more logical. 821da177e4SLinus Torvalds * Note that "ln old new" where "new" is a symlink pointing to a non-existing 831da177e4SLinus Torvalds * file does succeed in both HP-UX and SunOs, but not in Solaris 841da177e4SLinus Torvalds * and in the old Linux semantics. 851da177e4SLinus Torvalds */ 861da177e4SLinus Torvalds 871da177e4SLinus Torvalds /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink 881da177e4SLinus Torvalds * semantics. See the comments in "open_namei" and "do_link" below. 891da177e4SLinus Torvalds * 901da177e4SLinus Torvalds * [10-Sep-98 Alan Modra] Another symlink change. 911da177e4SLinus Torvalds */ 921da177e4SLinus Torvalds 931da177e4SLinus Torvalds /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks: 941da177e4SLinus Torvalds * inside the path - always follow. 951da177e4SLinus Torvalds * in the last component in creation/removal/renaming - never follow. 961da177e4SLinus Torvalds * if LOOKUP_FOLLOW passed - follow. 971da177e4SLinus Torvalds * if the pathname has trailing slashes - follow. 981da177e4SLinus Torvalds * otherwise - don't follow. 991da177e4SLinus Torvalds * (applied in that order). 1001da177e4SLinus Torvalds * 1011da177e4SLinus Torvalds * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT 1021da177e4SLinus Torvalds * restored for 2.4. This is the last surviving part of old 4.2BSD bug. 1031da177e4SLinus Torvalds * During the 2.4 we need to fix the userland stuff depending on it - 1041da177e4SLinus Torvalds * hopefully we will be able to get rid of that wart in 2.5. So far only 1051da177e4SLinus Torvalds * XEmacs seems to be relying on it... 1061da177e4SLinus Torvalds */ 1071da177e4SLinus Torvalds /* 1081da177e4SLinus Torvalds * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland) 109a11f3a05SArjan van de Ven * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives 1101da177e4SLinus Torvalds * any extra contention... 1111da177e4SLinus Torvalds */ 1121da177e4SLinus Torvalds 1131da177e4SLinus Torvalds /* In order to reduce some races, while at the same time doing additional 1141da177e4SLinus Torvalds * checking and hopefully speeding things up, we copy filenames to the 1151da177e4SLinus Torvalds * kernel data space before using them.. 1161da177e4SLinus Torvalds * 1171da177e4SLinus Torvalds * POSIX.1 2.4: an empty pathname is invalid (ENOENT). 1181da177e4SLinus Torvalds * PATH_MAX includes the nul terminator --RR. 1191da177e4SLinus Torvalds */ 12091a27b2aSJeff Layton void final_putname(struct filename *name) 1211da177e4SLinus Torvalds { 1227950e385SJeff Layton if (name->separate) { 12391a27b2aSJeff Layton __putname(name->name); 12491a27b2aSJeff Layton kfree(name); 1257950e385SJeff Layton } else { 1267950e385SJeff Layton __putname(name); 12791a27b2aSJeff Layton } 1287950e385SJeff Layton } 1297950e385SJeff Layton 1307950e385SJeff Layton #define EMBEDDED_NAME_MAX (PATH_MAX - sizeof(struct filename)) 13191a27b2aSJeff Layton 13291a27b2aSJeff Layton static struct filename * 13391a27b2aSJeff Layton getname_flags(const char __user *filename, int flags, int *empty) 13491a27b2aSJeff Layton { 13591a27b2aSJeff Layton struct filename *result, *err; 1363f9f0aa6SLinus Torvalds int len; 1377950e385SJeff Layton long max; 1387950e385SJeff Layton char *kname; 1391da177e4SLinus Torvalds 1407ac86265SJeff Layton result = audit_reusename(filename); 1417ac86265SJeff Layton if (result) 1427ac86265SJeff Layton return result; 1437ac86265SJeff Layton 1447950e385SJeff Layton result = __getname(); 1453f9f0aa6SLinus Torvalds if (unlikely(!result)) 1464043cde8SEric Paris return ERR_PTR(-ENOMEM); 1471da177e4SLinus Torvalds 1487950e385SJeff Layton /* 1497950e385SJeff Layton * First, try to embed the struct filename inside the names_cache 1507950e385SJeff Layton * allocation 1517950e385SJeff Layton */ 1527950e385SJeff Layton kname = (char *)result + sizeof(*result); 15391a27b2aSJeff Layton result->name = kname; 1547950e385SJeff Layton result->separate = false; 1557950e385SJeff Layton max = EMBEDDED_NAME_MAX; 1567950e385SJeff Layton 1577950e385SJeff Layton recopy: 1587950e385SJeff Layton len = strncpy_from_user(kname, filename, max); 15991a27b2aSJeff Layton if (unlikely(len < 0)) { 1603f9f0aa6SLinus Torvalds err = ERR_PTR(len); 1613f9f0aa6SLinus Torvalds goto error; 16291a27b2aSJeff Layton } 1633f9f0aa6SLinus Torvalds 1647950e385SJeff Layton /* 1657950e385SJeff Layton * Uh-oh. We have a name that's approaching PATH_MAX. Allocate a 1667950e385SJeff Layton * separate struct filename so we can dedicate the entire 1677950e385SJeff Layton * names_cache allocation for the pathname, and re-do the copy from 1687950e385SJeff Layton * userland. 1697950e385SJeff Layton */ 1707950e385SJeff Layton if (len == EMBEDDED_NAME_MAX && max == EMBEDDED_NAME_MAX) { 1717950e385SJeff Layton kname = (char *)result; 1727950e385SJeff Layton 1737950e385SJeff Layton result = kzalloc(sizeof(*result), GFP_KERNEL); 1747950e385SJeff Layton if (!result) { 1757950e385SJeff Layton err = ERR_PTR(-ENOMEM); 1767950e385SJeff Layton result = (struct filename *)kname; 1777950e385SJeff Layton goto error; 1787950e385SJeff Layton } 1797950e385SJeff Layton result->name = kname; 1807950e385SJeff Layton result->separate = true; 1817950e385SJeff Layton max = PATH_MAX; 1827950e385SJeff Layton goto recopy; 1837950e385SJeff Layton } 1847950e385SJeff Layton 1853f9f0aa6SLinus Torvalds /* The empty path is special. */ 1863f9f0aa6SLinus Torvalds if (unlikely(!len)) { 1873f9f0aa6SLinus Torvalds if (empty) 1881fa1e7f6SAndy Whitcroft *empty = 1; 1893f9f0aa6SLinus Torvalds err = ERR_PTR(-ENOENT); 1903f9f0aa6SLinus Torvalds if (!(flags & LOOKUP_EMPTY)) 1913f9f0aa6SLinus Torvalds goto error; 1921da177e4SLinus Torvalds } 1933f9f0aa6SLinus Torvalds 1943f9f0aa6SLinus Torvalds err = ERR_PTR(-ENAMETOOLONG); 1957950e385SJeff Layton if (unlikely(len >= PATH_MAX)) 1967950e385SJeff Layton goto error; 1977950e385SJeff Layton 1987950e385SJeff Layton result->uptr = filename; 1991da177e4SLinus Torvalds audit_getname(result); 2001da177e4SLinus Torvalds return result; 2011da177e4SLinus Torvalds 2023f9f0aa6SLinus Torvalds error: 2037950e385SJeff Layton final_putname(result); 2043f9f0aa6SLinus Torvalds return err; 2053f9f0aa6SLinus Torvalds } 2063f9f0aa6SLinus Torvalds 20791a27b2aSJeff Layton struct filename * 20891a27b2aSJeff Layton getname(const char __user * filename) 209f52e0c11SAl Viro { 210f7493e5dSLinus Torvalds return getname_flags(filename, 0, NULL); 211f52e0c11SAl Viro } 21291a27b2aSJeff Layton EXPORT_SYMBOL(getname); 213f52e0c11SAl Viro 2141da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL 21591a27b2aSJeff Layton void putname(struct filename *name) 2161da177e4SLinus Torvalds { 2175ac3a9c2SAl Viro if (unlikely(!audit_dummy_context())) 21891a27b2aSJeff Layton return audit_putname(name); 21991a27b2aSJeff Layton final_putname(name); 2201da177e4SLinus Torvalds } 2211da177e4SLinus Torvalds #endif 2221da177e4SLinus Torvalds 223e77819e5SLinus Torvalds static int check_acl(struct inode *inode, int mask) 224e77819e5SLinus Torvalds { 22584635d68SLinus Torvalds #ifdef CONFIG_FS_POSIX_ACL 226e77819e5SLinus Torvalds struct posix_acl *acl; 227e77819e5SLinus Torvalds 228e77819e5SLinus Torvalds if (mask & MAY_NOT_BLOCK) { 2293567866bSAl Viro acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS); 2303567866bSAl Viro if (!acl) 231e77819e5SLinus Torvalds return -EAGAIN; 2323567866bSAl Viro /* no ->get_acl() calls in RCU mode... */ 2333567866bSAl Viro if (acl == ACL_NOT_CACHED) 234e77819e5SLinus Torvalds return -ECHILD; 235206b1d09SAri Savolainen return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK); 236e77819e5SLinus Torvalds } 237e77819e5SLinus Torvalds 238e77819e5SLinus Torvalds acl = get_cached_acl(inode, ACL_TYPE_ACCESS); 239e77819e5SLinus Torvalds 240e77819e5SLinus Torvalds /* 2414e34e719SChristoph Hellwig * A filesystem can force a ACL callback by just never filling the 2424e34e719SChristoph Hellwig * ACL cache. But normally you'd fill the cache either at inode 2434e34e719SChristoph Hellwig * instantiation time, or on the first ->get_acl call. 244e77819e5SLinus Torvalds * 2454e34e719SChristoph Hellwig * If the filesystem doesn't have a get_acl() function at all, we'll 2464e34e719SChristoph Hellwig * just create the negative cache entry. 247e77819e5SLinus Torvalds */ 248e77819e5SLinus Torvalds if (acl == ACL_NOT_CACHED) { 2494e34e719SChristoph Hellwig if (inode->i_op->get_acl) { 2504e34e719SChristoph Hellwig acl = inode->i_op->get_acl(inode, ACL_TYPE_ACCESS); 2514e34e719SChristoph Hellwig if (IS_ERR(acl)) 2524e34e719SChristoph Hellwig return PTR_ERR(acl); 2534e34e719SChristoph Hellwig } else { 254e77819e5SLinus Torvalds set_cached_acl(inode, ACL_TYPE_ACCESS, NULL); 255e77819e5SLinus Torvalds return -EAGAIN; 256e77819e5SLinus Torvalds } 2574e34e719SChristoph Hellwig } 258e77819e5SLinus Torvalds 259e77819e5SLinus Torvalds if (acl) { 260e77819e5SLinus Torvalds int error = posix_acl_permission(inode, acl, mask); 261e77819e5SLinus Torvalds posix_acl_release(acl); 262e77819e5SLinus Torvalds return error; 263e77819e5SLinus Torvalds } 26484635d68SLinus Torvalds #endif 265e77819e5SLinus Torvalds 266e77819e5SLinus Torvalds return -EAGAIN; 267e77819e5SLinus Torvalds } 268e77819e5SLinus Torvalds 2695909ccaaSLinus Torvalds /* 270948409c7SAndreas Gruenbacher * This does the basic permission checking 2715909ccaaSLinus Torvalds */ 2727e40145eSAl Viro static int acl_permission_check(struct inode *inode, int mask) 2735909ccaaSLinus Torvalds { 27426cf46beSLinus Torvalds unsigned int mode = inode->i_mode; 2755909ccaaSLinus Torvalds 2768e96e3b7SEric W. Biederman if (likely(uid_eq(current_fsuid(), inode->i_uid))) 2775909ccaaSLinus Torvalds mode >>= 6; 2785909ccaaSLinus Torvalds else { 279e77819e5SLinus Torvalds if (IS_POSIXACL(inode) && (mode & S_IRWXG)) { 2807e40145eSAl Viro int error = check_acl(inode, mask); 2815909ccaaSLinus Torvalds if (error != -EAGAIN) 2825909ccaaSLinus Torvalds return error; 2835909ccaaSLinus Torvalds } 2845909ccaaSLinus Torvalds 2855909ccaaSLinus Torvalds if (in_group_p(inode->i_gid)) 2865909ccaaSLinus Torvalds mode >>= 3; 2875909ccaaSLinus Torvalds } 2885909ccaaSLinus Torvalds 2895909ccaaSLinus Torvalds /* 2905909ccaaSLinus Torvalds * If the DACs are ok we don't need any capability check. 2915909ccaaSLinus Torvalds */ 2929c2c7039SAl Viro if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0) 2935909ccaaSLinus Torvalds return 0; 2945909ccaaSLinus Torvalds return -EACCES; 2955909ccaaSLinus Torvalds } 2961da177e4SLinus Torvalds 2971da177e4SLinus Torvalds /** 2981da177e4SLinus Torvalds * generic_permission - check for access rights on a Posix-like filesystem 2991da177e4SLinus Torvalds * @inode: inode to check access rights for 3008fd90c8dSAndreas Gruenbacher * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...) 3011da177e4SLinus Torvalds * 3021da177e4SLinus Torvalds * Used to check for read/write/execute permissions on a file. 3031da177e4SLinus Torvalds * We use "fsuid" for this, letting us set arbitrary permissions 3041da177e4SLinus Torvalds * for filesystem access without changing the "normal" uids which 305b74c79e9SNick Piggin * are used for other things. 306b74c79e9SNick Piggin * 307b74c79e9SNick Piggin * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk 308b74c79e9SNick Piggin * request cannot be satisfied (eg. requires blocking or too much complexity). 309b74c79e9SNick Piggin * It would then be called again in ref-walk mode. 3101da177e4SLinus Torvalds */ 3112830ba7fSAl Viro int generic_permission(struct inode *inode, int mask) 3121da177e4SLinus Torvalds { 3135909ccaaSLinus Torvalds int ret; 3141da177e4SLinus Torvalds 3151da177e4SLinus Torvalds /* 316948409c7SAndreas Gruenbacher * Do the basic permission checks. 3171da177e4SLinus Torvalds */ 3187e40145eSAl Viro ret = acl_permission_check(inode, mask); 3195909ccaaSLinus Torvalds if (ret != -EACCES) 3205909ccaaSLinus Torvalds return ret; 3211da177e4SLinus Torvalds 322d594e7ecSAl Viro if (S_ISDIR(inode->i_mode)) { 323d594e7ecSAl Viro /* DACs are overridable for directories */ 3241a48e2acSEric W. Biederman if (inode_capable(inode, CAP_DAC_OVERRIDE)) 325d594e7ecSAl Viro return 0; 326d594e7ecSAl Viro if (!(mask & MAY_WRITE)) 3271a48e2acSEric W. Biederman if (inode_capable(inode, CAP_DAC_READ_SEARCH)) 328d594e7ecSAl Viro return 0; 329d594e7ecSAl Viro return -EACCES; 330d594e7ecSAl Viro } 3311da177e4SLinus Torvalds /* 3321da177e4SLinus Torvalds * Read/write DACs are always overridable. 333d594e7ecSAl Viro * Executable DACs are overridable when there is 334d594e7ecSAl Viro * at least one exec bit set. 3351da177e4SLinus Torvalds */ 336d594e7ecSAl Viro if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO)) 3371a48e2acSEric W. Biederman if (inode_capable(inode, CAP_DAC_OVERRIDE)) 3381da177e4SLinus Torvalds return 0; 3391da177e4SLinus Torvalds 3401da177e4SLinus Torvalds /* 3411da177e4SLinus Torvalds * Searching includes executable on directories, else just read. 3421da177e4SLinus Torvalds */ 3437ea66001SSerge E. Hallyn mask &= MAY_READ | MAY_WRITE | MAY_EXEC; 344d594e7ecSAl Viro if (mask == MAY_READ) 3451a48e2acSEric W. Biederman if (inode_capable(inode, CAP_DAC_READ_SEARCH)) 3461da177e4SLinus Torvalds return 0; 3471da177e4SLinus Torvalds 3481da177e4SLinus Torvalds return -EACCES; 3491da177e4SLinus Torvalds } 3501da177e4SLinus Torvalds 3513ddcd056SLinus Torvalds /* 3523ddcd056SLinus Torvalds * We _really_ want to just do "generic_permission()" without 3533ddcd056SLinus Torvalds * even looking at the inode->i_op values. So we keep a cache 3543ddcd056SLinus Torvalds * flag in inode->i_opflags, that says "this has not special 3553ddcd056SLinus Torvalds * permission function, use the fast case". 3563ddcd056SLinus Torvalds */ 3573ddcd056SLinus Torvalds static inline int do_inode_permission(struct inode *inode, int mask) 3583ddcd056SLinus Torvalds { 3593ddcd056SLinus Torvalds if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) { 3603ddcd056SLinus Torvalds if (likely(inode->i_op->permission)) 3613ddcd056SLinus Torvalds return inode->i_op->permission(inode, mask); 3623ddcd056SLinus Torvalds 3633ddcd056SLinus Torvalds /* This gets set once for the inode lifetime */ 3643ddcd056SLinus Torvalds spin_lock(&inode->i_lock); 3653ddcd056SLinus Torvalds inode->i_opflags |= IOP_FASTPERM; 3663ddcd056SLinus Torvalds spin_unlock(&inode->i_lock); 3673ddcd056SLinus Torvalds } 3683ddcd056SLinus Torvalds return generic_permission(inode, mask); 3693ddcd056SLinus Torvalds } 3703ddcd056SLinus Torvalds 371cb23beb5SChristoph Hellwig /** 3720bdaea90SDavid Howells * __inode_permission - Check for access rights to a given inode 3730bdaea90SDavid Howells * @inode: Inode to check permission on 3740bdaea90SDavid Howells * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) 375cb23beb5SChristoph Hellwig * 3760bdaea90SDavid Howells * Check for read/write/execute permissions on an inode. 377948409c7SAndreas Gruenbacher * 378948409c7SAndreas Gruenbacher * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask. 3790bdaea90SDavid Howells * 3800bdaea90SDavid Howells * This does not check for a read-only file system. You probably want 3810bdaea90SDavid Howells * inode_permission(). 382cb23beb5SChristoph Hellwig */ 3830bdaea90SDavid Howells int __inode_permission(struct inode *inode, int mask) 3841da177e4SLinus Torvalds { 385e6305c43SAl Viro int retval; 3861da177e4SLinus Torvalds 3873ddcd056SLinus Torvalds if (unlikely(mask & MAY_WRITE)) { 3881da177e4SLinus Torvalds /* 3891da177e4SLinus Torvalds * Nobody gets write access to an immutable file. 3901da177e4SLinus Torvalds */ 3911da177e4SLinus Torvalds if (IS_IMMUTABLE(inode)) 3921da177e4SLinus Torvalds return -EACCES; 3931da177e4SLinus Torvalds } 3941da177e4SLinus Torvalds 3953ddcd056SLinus Torvalds retval = do_inode_permission(inode, mask); 3961da177e4SLinus Torvalds if (retval) 3971da177e4SLinus Torvalds return retval; 3981da177e4SLinus Torvalds 39908ce5f16SSerge E. Hallyn retval = devcgroup_inode_permission(inode, mask); 40008ce5f16SSerge E. Hallyn if (retval) 40108ce5f16SSerge E. Hallyn return retval; 40208ce5f16SSerge E. Hallyn 403d09ca739SEric Paris return security_inode_permission(inode, mask); 4041da177e4SLinus Torvalds } 4051da177e4SLinus Torvalds 406f4d6ff89SAl Viro /** 4070bdaea90SDavid Howells * sb_permission - Check superblock-level permissions 4080bdaea90SDavid Howells * @sb: Superblock of inode to check permission on 40955852635SRandy Dunlap * @inode: Inode to check permission on 4100bdaea90SDavid Howells * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) 4110bdaea90SDavid Howells * 4120bdaea90SDavid Howells * Separate out file-system wide checks from inode-specific permission checks. 4130bdaea90SDavid Howells */ 4140bdaea90SDavid Howells static int sb_permission(struct super_block *sb, struct inode *inode, int mask) 4150bdaea90SDavid Howells { 4160bdaea90SDavid Howells if (unlikely(mask & MAY_WRITE)) { 4170bdaea90SDavid Howells umode_t mode = inode->i_mode; 4180bdaea90SDavid Howells 4190bdaea90SDavid Howells /* Nobody gets write access to a read-only fs. */ 4200bdaea90SDavid Howells if ((sb->s_flags & MS_RDONLY) && 4210bdaea90SDavid Howells (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) 4220bdaea90SDavid Howells return -EROFS; 4230bdaea90SDavid Howells } 4240bdaea90SDavid Howells return 0; 4250bdaea90SDavid Howells } 4260bdaea90SDavid Howells 4270bdaea90SDavid Howells /** 4280bdaea90SDavid Howells * inode_permission - Check for access rights to a given inode 4290bdaea90SDavid Howells * @inode: Inode to check permission on 4300bdaea90SDavid Howells * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) 4310bdaea90SDavid Howells * 4320bdaea90SDavid Howells * Check for read/write/execute permissions on an inode. We use fs[ug]id for 4330bdaea90SDavid Howells * this, letting us set arbitrary permissions for filesystem access without 4340bdaea90SDavid Howells * changing the "normal" UIDs which are used for other things. 4350bdaea90SDavid Howells * 4360bdaea90SDavid Howells * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask. 4370bdaea90SDavid Howells */ 4380bdaea90SDavid Howells int inode_permission(struct inode *inode, int mask) 4390bdaea90SDavid Howells { 4400bdaea90SDavid Howells int retval; 4410bdaea90SDavid Howells 4420bdaea90SDavid Howells retval = sb_permission(inode->i_sb, inode, mask); 4430bdaea90SDavid Howells if (retval) 4440bdaea90SDavid Howells return retval; 4450bdaea90SDavid Howells return __inode_permission(inode, mask); 4460bdaea90SDavid Howells } 4470bdaea90SDavid Howells 4480bdaea90SDavid Howells /** 4495dd784d0SJan Blunck * path_get - get a reference to a path 4505dd784d0SJan Blunck * @path: path to get the reference to 4515dd784d0SJan Blunck * 4525dd784d0SJan Blunck * Given a path increment the reference count to the dentry and the vfsmount. 4535dd784d0SJan Blunck */ 4545dd784d0SJan Blunck void path_get(struct path *path) 4555dd784d0SJan Blunck { 4565dd784d0SJan Blunck mntget(path->mnt); 4575dd784d0SJan Blunck dget(path->dentry); 4585dd784d0SJan Blunck } 4595dd784d0SJan Blunck EXPORT_SYMBOL(path_get); 4605dd784d0SJan Blunck 4615dd784d0SJan Blunck /** 4621d957f9bSJan Blunck * path_put - put a reference to a path 4631d957f9bSJan Blunck * @path: path to put the reference to 4641d957f9bSJan Blunck * 4651d957f9bSJan Blunck * Given a path decrement the reference count to the dentry and the vfsmount. 4661d957f9bSJan Blunck */ 4671d957f9bSJan Blunck void path_put(struct path *path) 4681da177e4SLinus Torvalds { 4691d957f9bSJan Blunck dput(path->dentry); 4701d957f9bSJan Blunck mntput(path->mnt); 4711da177e4SLinus Torvalds } 4721d957f9bSJan Blunck EXPORT_SYMBOL(path_put); 4731da177e4SLinus Torvalds 47419660af7SAl Viro /* 47531e6b01fSNick Piggin * Path walking has 2 modes, rcu-walk and ref-walk (see 47619660af7SAl Viro * Documentation/filesystems/path-lookup.txt). In situations when we can't 47719660af7SAl Viro * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab 47819660af7SAl Viro * normal reference counts on dentries and vfsmounts to transition to rcu-walk 47919660af7SAl Viro * mode. Refcounts are grabbed at the last known good point before rcu-walk 48019660af7SAl Viro * got stuck, so ref-walk may continue from there. If this is not successful 48119660af7SAl Viro * (eg. a seqcount has changed), then failure is returned and it's up to caller 48219660af7SAl Viro * to restart the path walk from the beginning in ref-walk mode. 48331e6b01fSNick Piggin */ 48431e6b01fSNick Piggin 48532a7991bSAl Viro static inline void lock_rcu_walk(void) 48632a7991bSAl Viro { 48732a7991bSAl Viro br_read_lock(&vfsmount_lock); 48832a7991bSAl Viro rcu_read_lock(); 48932a7991bSAl Viro } 49032a7991bSAl Viro 49132a7991bSAl Viro static inline void unlock_rcu_walk(void) 49232a7991bSAl Viro { 49332a7991bSAl Viro rcu_read_unlock(); 49432a7991bSAl Viro br_read_unlock(&vfsmount_lock); 49532a7991bSAl Viro } 49632a7991bSAl Viro 49731e6b01fSNick Piggin /** 49819660af7SAl Viro * unlazy_walk - try to switch to ref-walk mode. 49919660af7SAl Viro * @nd: nameidata pathwalk data 50019660af7SAl Viro * @dentry: child of nd->path.dentry or NULL 50139191628SRandy Dunlap * Returns: 0 on success, -ECHILD on failure 50231e6b01fSNick Piggin * 50319660af7SAl Viro * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry 50419660af7SAl Viro * for ref-walk mode. @dentry must be a path found by a do_lookup call on 50519660af7SAl Viro * @nd or NULL. Must be called from rcu-walk context. 50631e6b01fSNick Piggin */ 50719660af7SAl Viro static int unlazy_walk(struct nameidata *nd, struct dentry *dentry) 50831e6b01fSNick Piggin { 50931e6b01fSNick Piggin struct fs_struct *fs = current->fs; 51031e6b01fSNick Piggin struct dentry *parent = nd->path.dentry; 5115b6ca027SAl Viro int want_root = 0; 51231e6b01fSNick Piggin 51331e6b01fSNick Piggin BUG_ON(!(nd->flags & LOOKUP_RCU)); 5145b6ca027SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) { 5155b6ca027SAl Viro want_root = 1; 51631e6b01fSNick Piggin spin_lock(&fs->lock); 51731e6b01fSNick Piggin if (nd->root.mnt != fs->root.mnt || 51831e6b01fSNick Piggin nd->root.dentry != fs->root.dentry) 51931e6b01fSNick Piggin goto err_root; 52031e6b01fSNick Piggin } 52131e6b01fSNick Piggin spin_lock(&parent->d_lock); 52219660af7SAl Viro if (!dentry) { 52319660af7SAl Viro if (!__d_rcu_to_refcount(parent, nd->seq)) 52419660af7SAl Viro goto err_parent; 52519660af7SAl Viro BUG_ON(nd->inode != parent->d_inode); 52619660af7SAl Viro } else { 52794c0d4ecSAl Viro if (dentry->d_parent != parent) 52894c0d4ecSAl Viro goto err_parent; 52931e6b01fSNick Piggin spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); 53031e6b01fSNick Piggin if (!__d_rcu_to_refcount(dentry, nd->seq)) 53119660af7SAl Viro goto err_child; 53231e6b01fSNick Piggin /* 53319660af7SAl Viro * If the sequence check on the child dentry passed, then 53419660af7SAl Viro * the child has not been removed from its parent. This 53519660af7SAl Viro * means the parent dentry must be valid and able to take 53619660af7SAl Viro * a reference at this point. 53731e6b01fSNick Piggin */ 53831e6b01fSNick Piggin BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent); 53931e6b01fSNick Piggin BUG_ON(!parent->d_count); 54031e6b01fSNick Piggin parent->d_count++; 54131e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 54219660af7SAl Viro } 54331e6b01fSNick Piggin spin_unlock(&parent->d_lock); 5445b6ca027SAl Viro if (want_root) { 54531e6b01fSNick Piggin path_get(&nd->root); 54631e6b01fSNick Piggin spin_unlock(&fs->lock); 54731e6b01fSNick Piggin } 54831e6b01fSNick Piggin mntget(nd->path.mnt); 54931e6b01fSNick Piggin 55032a7991bSAl Viro unlock_rcu_walk(); 55131e6b01fSNick Piggin nd->flags &= ~LOOKUP_RCU; 55231e6b01fSNick Piggin return 0; 55319660af7SAl Viro 55419660af7SAl Viro err_child: 55531e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 55619660af7SAl Viro err_parent: 55731e6b01fSNick Piggin spin_unlock(&parent->d_lock); 55831e6b01fSNick Piggin err_root: 5595b6ca027SAl Viro if (want_root) 56031e6b01fSNick Piggin spin_unlock(&fs->lock); 56131e6b01fSNick Piggin return -ECHILD; 56231e6b01fSNick Piggin } 56331e6b01fSNick Piggin 5644ce16ef3SAl Viro static inline int d_revalidate(struct dentry *dentry, unsigned int flags) 56534286d66SNick Piggin { 5664ce16ef3SAl Viro return dentry->d_op->d_revalidate(dentry, flags); 56734286d66SNick Piggin } 56834286d66SNick Piggin 5699f1fafeeSAl Viro /** 5709f1fafeeSAl Viro * complete_walk - successful completion of path walk 5719f1fafeeSAl Viro * @nd: pointer nameidata 57239159de2SJeff Layton * 5739f1fafeeSAl Viro * If we had been in RCU mode, drop out of it and legitimize nd->path. 5749f1fafeeSAl Viro * Revalidate the final result, unless we'd already done that during 5759f1fafeeSAl Viro * the path walk or the filesystem doesn't ask for it. Return 0 on 5769f1fafeeSAl Viro * success, -error on failure. In case of failure caller does not 5779f1fafeeSAl Viro * need to drop nd->path. 57839159de2SJeff Layton */ 5799f1fafeeSAl Viro static int complete_walk(struct nameidata *nd) 58039159de2SJeff Layton { 58116c2cd71SAl Viro struct dentry *dentry = nd->path.dentry; 58239159de2SJeff Layton int status; 58339159de2SJeff Layton 5849f1fafeeSAl Viro if (nd->flags & LOOKUP_RCU) { 5859f1fafeeSAl Viro nd->flags &= ~LOOKUP_RCU; 5869f1fafeeSAl Viro if (!(nd->flags & LOOKUP_ROOT)) 5879f1fafeeSAl Viro nd->root.mnt = NULL; 5889f1fafeeSAl Viro spin_lock(&dentry->d_lock); 5899f1fafeeSAl Viro if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) { 5909f1fafeeSAl Viro spin_unlock(&dentry->d_lock); 59132a7991bSAl Viro unlock_rcu_walk(); 5929f1fafeeSAl Viro return -ECHILD; 5939f1fafeeSAl Viro } 5949f1fafeeSAl Viro BUG_ON(nd->inode != dentry->d_inode); 5959f1fafeeSAl Viro spin_unlock(&dentry->d_lock); 5969f1fafeeSAl Viro mntget(nd->path.mnt); 59732a7991bSAl Viro unlock_rcu_walk(); 5989f1fafeeSAl Viro } 5999f1fafeeSAl Viro 60016c2cd71SAl Viro if (likely(!(nd->flags & LOOKUP_JUMPED))) 60139159de2SJeff Layton return 0; 60239159de2SJeff Layton 60316c2cd71SAl Viro if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE))) 60416c2cd71SAl Viro return 0; 60516c2cd71SAl Viro 60616c2cd71SAl Viro if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT))) 60716c2cd71SAl Viro return 0; 60816c2cd71SAl Viro 60916c2cd71SAl Viro /* Note: we do not d_invalidate() */ 6104ce16ef3SAl Viro status = d_revalidate(dentry, nd->flags); 61139159de2SJeff Layton if (status > 0) 61239159de2SJeff Layton return 0; 61339159de2SJeff Layton 61416c2cd71SAl Viro if (!status) 61539159de2SJeff Layton status = -ESTALE; 61616c2cd71SAl Viro 6179f1fafeeSAl Viro path_put(&nd->path); 61839159de2SJeff Layton return status; 61939159de2SJeff Layton } 62039159de2SJeff Layton 6212a737871SAl Viro static __always_inline void set_root(struct nameidata *nd) 6222a737871SAl Viro { 623f7ad3c6bSMiklos Szeredi if (!nd->root.mnt) 624f7ad3c6bSMiklos Szeredi get_fs_root(current->fs, &nd->root); 6252a737871SAl Viro } 6262a737871SAl Viro 6276de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *); 6286de88d72SAl Viro 62931e6b01fSNick Piggin static __always_inline void set_root_rcu(struct nameidata *nd) 63031e6b01fSNick Piggin { 63131e6b01fSNick Piggin if (!nd->root.mnt) { 63231e6b01fSNick Piggin struct fs_struct *fs = current->fs; 633c28cc364SNick Piggin unsigned seq; 634c28cc364SNick Piggin 635c28cc364SNick Piggin do { 636c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 63731e6b01fSNick Piggin nd->root = fs->root; 638c1530019STim Chen nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq); 639c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 64031e6b01fSNick Piggin } 64131e6b01fSNick Piggin } 64231e6b01fSNick Piggin 643f1662356SArjan van de Ven static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link) 6441da177e4SLinus Torvalds { 64531e6b01fSNick Piggin int ret; 64631e6b01fSNick Piggin 6471da177e4SLinus Torvalds if (IS_ERR(link)) 6481da177e4SLinus Torvalds goto fail; 6491da177e4SLinus Torvalds 6501da177e4SLinus Torvalds if (*link == '/') { 6512a737871SAl Viro set_root(nd); 6521d957f9bSJan Blunck path_put(&nd->path); 6532a737871SAl Viro nd->path = nd->root; 6542a737871SAl Viro path_get(&nd->root); 65516c2cd71SAl Viro nd->flags |= LOOKUP_JUMPED; 6561da177e4SLinus Torvalds } 65731e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 658b4091d5fSChristoph Hellwig 65931e6b01fSNick Piggin ret = link_path_walk(link, nd); 66031e6b01fSNick Piggin return ret; 6611da177e4SLinus Torvalds fail: 6621d957f9bSJan Blunck path_put(&nd->path); 6631da177e4SLinus Torvalds return PTR_ERR(link); 6641da177e4SLinus Torvalds } 6651da177e4SLinus Torvalds 6661d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd) 667051d3812SIan Kent { 668051d3812SIan Kent dput(path->dentry); 6694ac91378SJan Blunck if (path->mnt != nd->path.mnt) 670051d3812SIan Kent mntput(path->mnt); 671051d3812SIan Kent } 672051d3812SIan Kent 6737b9337aaSNick Piggin static inline void path_to_nameidata(const struct path *path, 6747b9337aaSNick Piggin struct nameidata *nd) 675051d3812SIan Kent { 67631e6b01fSNick Piggin if (!(nd->flags & LOOKUP_RCU)) { 6774ac91378SJan Blunck dput(nd->path.dentry); 67831e6b01fSNick Piggin if (nd->path.mnt != path->mnt) 6794ac91378SJan Blunck mntput(nd->path.mnt); 6809a229683SHuang Shijie } 68131e6b01fSNick Piggin nd->path.mnt = path->mnt; 6824ac91378SJan Blunck nd->path.dentry = path->dentry; 683051d3812SIan Kent } 684051d3812SIan Kent 685b5fb63c1SChristoph Hellwig /* 686b5fb63c1SChristoph Hellwig * Helper to directly jump to a known parsed path from ->follow_link, 687b5fb63c1SChristoph Hellwig * caller must have taken a reference to path beforehand. 688b5fb63c1SChristoph Hellwig */ 689b5fb63c1SChristoph Hellwig void nd_jump_link(struct nameidata *nd, struct path *path) 690b5fb63c1SChristoph Hellwig { 691b5fb63c1SChristoph Hellwig path_put(&nd->path); 692b5fb63c1SChristoph Hellwig 693b5fb63c1SChristoph Hellwig nd->path = *path; 694b5fb63c1SChristoph Hellwig nd->inode = nd->path.dentry->d_inode; 695b5fb63c1SChristoph Hellwig nd->flags |= LOOKUP_JUMPED; 696b5fb63c1SChristoph Hellwig 697b5fb63c1SChristoph Hellwig BUG_ON(nd->inode->i_op->follow_link); 698b5fb63c1SChristoph Hellwig } 699b5fb63c1SChristoph Hellwig 700574197e0SAl Viro static inline void put_link(struct nameidata *nd, struct path *link, void *cookie) 701574197e0SAl Viro { 702574197e0SAl Viro struct inode *inode = link->dentry->d_inode; 7036d7b5aaeSAl Viro if (inode->i_op->put_link) 704574197e0SAl Viro inode->i_op->put_link(link->dentry, nd, cookie); 705574197e0SAl Viro path_put(link); 706574197e0SAl Viro } 707574197e0SAl Viro 708561ec64aSLinus Torvalds int sysctl_protected_symlinks __read_mostly = 0; 709561ec64aSLinus Torvalds int sysctl_protected_hardlinks __read_mostly = 0; 710800179c9SKees Cook 711800179c9SKees Cook /** 712800179c9SKees Cook * may_follow_link - Check symlink following for unsafe situations 713800179c9SKees Cook * @link: The path of the symlink 71455852635SRandy Dunlap * @nd: nameidata pathwalk data 715800179c9SKees Cook * 716800179c9SKees Cook * In the case of the sysctl_protected_symlinks sysctl being enabled, 717800179c9SKees Cook * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is 718800179c9SKees Cook * in a sticky world-writable directory. This is to protect privileged 719800179c9SKees Cook * processes from failing races against path names that may change out 720800179c9SKees Cook * from under them by way of other users creating malicious symlinks. 721800179c9SKees Cook * It will permit symlinks to be followed only when outside a sticky 722800179c9SKees Cook * world-writable directory, or when the uid of the symlink and follower 723800179c9SKees Cook * match, or when the directory owner matches the symlink's owner. 724800179c9SKees Cook * 725800179c9SKees Cook * Returns 0 if following the symlink is allowed, -ve on error. 726800179c9SKees Cook */ 727800179c9SKees Cook static inline int may_follow_link(struct path *link, struct nameidata *nd) 728800179c9SKees Cook { 729800179c9SKees Cook const struct inode *inode; 730800179c9SKees Cook const struct inode *parent; 731800179c9SKees Cook 732800179c9SKees Cook if (!sysctl_protected_symlinks) 733800179c9SKees Cook return 0; 734800179c9SKees Cook 735800179c9SKees Cook /* Allowed if owner and follower match. */ 736800179c9SKees Cook inode = link->dentry->d_inode; 73781abe27bSEric W. Biederman if (uid_eq(current_cred()->fsuid, inode->i_uid)) 738800179c9SKees Cook return 0; 739800179c9SKees Cook 740800179c9SKees Cook /* Allowed if parent directory not sticky and world-writable. */ 741800179c9SKees Cook parent = nd->path.dentry->d_inode; 742800179c9SKees Cook if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH)) 743800179c9SKees Cook return 0; 744800179c9SKees Cook 745800179c9SKees Cook /* Allowed if parent directory and link owner match. */ 74681abe27bSEric W. Biederman if (uid_eq(parent->i_uid, inode->i_uid)) 747800179c9SKees Cook return 0; 748800179c9SKees Cook 749ffd8d101SSasha Levin audit_log_link_denied("follow_link", link); 750800179c9SKees Cook path_put_conditional(link, nd); 751800179c9SKees Cook path_put(&nd->path); 752800179c9SKees Cook return -EACCES; 753800179c9SKees Cook } 754800179c9SKees Cook 755800179c9SKees Cook /** 756800179c9SKees Cook * safe_hardlink_source - Check for safe hardlink conditions 757800179c9SKees Cook * @inode: the source inode to hardlink from 758800179c9SKees Cook * 759800179c9SKees Cook * Return false if at least one of the following conditions: 760800179c9SKees Cook * - inode is not a regular file 761800179c9SKees Cook * - inode is setuid 762800179c9SKees Cook * - inode is setgid and group-exec 763800179c9SKees Cook * - access failure for read and write 764800179c9SKees Cook * 765800179c9SKees Cook * Otherwise returns true. 766800179c9SKees Cook */ 767800179c9SKees Cook static bool safe_hardlink_source(struct inode *inode) 768800179c9SKees Cook { 769800179c9SKees Cook umode_t mode = inode->i_mode; 770800179c9SKees Cook 771800179c9SKees Cook /* Special files should not get pinned to the filesystem. */ 772800179c9SKees Cook if (!S_ISREG(mode)) 773800179c9SKees Cook return false; 774800179c9SKees Cook 775800179c9SKees Cook /* Setuid files should not get pinned to the filesystem. */ 776800179c9SKees Cook if (mode & S_ISUID) 777800179c9SKees Cook return false; 778800179c9SKees Cook 779800179c9SKees Cook /* Executable setgid files should not get pinned to the filesystem. */ 780800179c9SKees Cook if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) 781800179c9SKees Cook return false; 782800179c9SKees Cook 783800179c9SKees Cook /* Hardlinking to unreadable or unwritable sources is dangerous. */ 784800179c9SKees Cook if (inode_permission(inode, MAY_READ | MAY_WRITE)) 785800179c9SKees Cook return false; 786800179c9SKees Cook 787800179c9SKees Cook return true; 788800179c9SKees Cook } 789800179c9SKees Cook 790800179c9SKees Cook /** 791800179c9SKees Cook * may_linkat - Check permissions for creating a hardlink 792800179c9SKees Cook * @link: the source to hardlink from 793800179c9SKees Cook * 794800179c9SKees Cook * Block hardlink when all of: 795800179c9SKees Cook * - sysctl_protected_hardlinks enabled 796800179c9SKees Cook * - fsuid does not match inode 797800179c9SKees Cook * - hardlink source is unsafe (see safe_hardlink_source() above) 798800179c9SKees Cook * - not CAP_FOWNER 799800179c9SKees Cook * 800800179c9SKees Cook * Returns 0 if successful, -ve on error. 801800179c9SKees Cook */ 802800179c9SKees Cook static int may_linkat(struct path *link) 803800179c9SKees Cook { 804800179c9SKees Cook const struct cred *cred; 805800179c9SKees Cook struct inode *inode; 806800179c9SKees Cook 807800179c9SKees Cook if (!sysctl_protected_hardlinks) 808800179c9SKees Cook return 0; 809800179c9SKees Cook 810800179c9SKees Cook cred = current_cred(); 811800179c9SKees Cook inode = link->dentry->d_inode; 812800179c9SKees Cook 813800179c9SKees Cook /* Source inode owner (or CAP_FOWNER) can hardlink all they like, 814800179c9SKees Cook * otherwise, it must be a safe source. 815800179c9SKees Cook */ 81681abe27bSEric W. Biederman if (uid_eq(cred->fsuid, inode->i_uid) || safe_hardlink_source(inode) || 817800179c9SKees Cook capable(CAP_FOWNER)) 818800179c9SKees Cook return 0; 819800179c9SKees Cook 820a51d9eaaSKees Cook audit_log_link_denied("linkat", link); 821800179c9SKees Cook return -EPERM; 822800179c9SKees Cook } 823800179c9SKees Cook 824def4af30SAl Viro static __always_inline int 825574197e0SAl Viro follow_link(struct path *link, struct nameidata *nd, void **p) 8261da177e4SLinus Torvalds { 8277b9337aaSNick Piggin struct dentry *dentry = link->dentry; 8286d7b5aaeSAl Viro int error; 8296d7b5aaeSAl Viro char *s; 8301da177e4SLinus Torvalds 831844a3917SAl Viro BUG_ON(nd->flags & LOOKUP_RCU); 832844a3917SAl Viro 8330e794589SAl Viro if (link->mnt == nd->path.mnt) 8340e794589SAl Viro mntget(link->mnt); 8350e794589SAl Viro 8366d7b5aaeSAl Viro error = -ELOOP; 8376d7b5aaeSAl Viro if (unlikely(current->total_link_count >= 40)) 8386d7b5aaeSAl Viro goto out_put_nd_path; 8396d7b5aaeSAl Viro 840574197e0SAl Viro cond_resched(); 841574197e0SAl Viro current->total_link_count++; 842574197e0SAl Viro 84368ac1234SAl Viro touch_atime(link); 8441da177e4SLinus Torvalds nd_set_link(nd, NULL); 845cd4e91d3SAl Viro 84636f3b4f6SAl Viro error = security_inode_follow_link(link->dentry, nd); 8476d7b5aaeSAl Viro if (error) 8486d7b5aaeSAl Viro goto out_put_nd_path; 84936f3b4f6SAl Viro 85086acdca1SAl Viro nd->last_type = LAST_BIND; 851def4af30SAl Viro *p = dentry->d_inode->i_op->follow_link(dentry, nd); 852def4af30SAl Viro error = PTR_ERR(*p); 8536d7b5aaeSAl Viro if (IS_ERR(*p)) 854408ef013SChristoph Hellwig goto out_put_nd_path; 8556d7b5aaeSAl Viro 856cc314eefSLinus Torvalds error = 0; 8576d7b5aaeSAl Viro s = nd_get_link(nd); 8586d7b5aaeSAl Viro if (s) { 8591da177e4SLinus Torvalds error = __vfs_follow_link(nd, s); 8606d7b5aaeSAl Viro if (unlikely(error)) 8616d7b5aaeSAl Viro put_link(nd, link, *p); 862b5fb63c1SChristoph Hellwig } 8636d7b5aaeSAl Viro 8646d7b5aaeSAl Viro return error; 8656d7b5aaeSAl Viro 8666d7b5aaeSAl Viro out_put_nd_path: 86798f6ef64SArnd Bergmann *p = NULL; 8686d7b5aaeSAl Viro path_put(&nd->path); 8696d7b5aaeSAl Viro path_put(link); 8701da177e4SLinus Torvalds return error; 8711da177e4SLinus Torvalds } 8721da177e4SLinus Torvalds 87331e6b01fSNick Piggin static int follow_up_rcu(struct path *path) 87431e6b01fSNick Piggin { 8750714a533SAl Viro struct mount *mnt = real_mount(path->mnt); 8760714a533SAl Viro struct mount *parent; 87731e6b01fSNick Piggin struct dentry *mountpoint; 87831e6b01fSNick Piggin 8790714a533SAl Viro parent = mnt->mnt_parent; 8800714a533SAl Viro if (&parent->mnt == path->mnt) 88131e6b01fSNick Piggin return 0; 882a73324daSAl Viro mountpoint = mnt->mnt_mountpoint; 88331e6b01fSNick Piggin path->dentry = mountpoint; 8840714a533SAl Viro path->mnt = &parent->mnt; 88531e6b01fSNick Piggin return 1; 88631e6b01fSNick Piggin } 88731e6b01fSNick Piggin 888f015f126SDavid Howells /* 889f015f126SDavid Howells * follow_up - Find the mountpoint of path's vfsmount 890f015f126SDavid Howells * 891f015f126SDavid Howells * Given a path, find the mountpoint of its source file system. 892f015f126SDavid Howells * Replace @path with the path of the mountpoint in the parent mount. 893f015f126SDavid Howells * Up is towards /. 894f015f126SDavid Howells * 895f015f126SDavid Howells * Return 1 if we went up a level and 0 if we were already at the 896f015f126SDavid Howells * root. 897f015f126SDavid Howells */ 898bab77ebfSAl Viro int follow_up(struct path *path) 8991da177e4SLinus Torvalds { 9000714a533SAl Viro struct mount *mnt = real_mount(path->mnt); 9010714a533SAl Viro struct mount *parent; 9021da177e4SLinus Torvalds struct dentry *mountpoint; 90399b7db7bSNick Piggin 904962830dfSAndi Kleen br_read_lock(&vfsmount_lock); 9050714a533SAl Viro parent = mnt->mnt_parent; 9063c0a6163SAl Viro if (parent == mnt) { 907962830dfSAndi Kleen br_read_unlock(&vfsmount_lock); 9081da177e4SLinus Torvalds return 0; 9091da177e4SLinus Torvalds } 9100714a533SAl Viro mntget(&parent->mnt); 911a73324daSAl Viro mountpoint = dget(mnt->mnt_mountpoint); 912962830dfSAndi Kleen br_read_unlock(&vfsmount_lock); 913bab77ebfSAl Viro dput(path->dentry); 914bab77ebfSAl Viro path->dentry = mountpoint; 915bab77ebfSAl Viro mntput(path->mnt); 9160714a533SAl Viro path->mnt = &parent->mnt; 9171da177e4SLinus Torvalds return 1; 9181da177e4SLinus Torvalds } 9191da177e4SLinus Torvalds 920b5c84bf6SNick Piggin /* 9219875cf80SDavid Howells * Perform an automount 9229875cf80SDavid Howells * - return -EISDIR to tell follow_managed() to stop and return the path we 9239875cf80SDavid Howells * were called with. 9241da177e4SLinus Torvalds */ 9259875cf80SDavid Howells static int follow_automount(struct path *path, unsigned flags, 9269875cf80SDavid Howells bool *need_mntput) 92731e6b01fSNick Piggin { 9289875cf80SDavid Howells struct vfsmount *mnt; 929ea5b778aSDavid Howells int err; 9309875cf80SDavid Howells 9319875cf80SDavid Howells if (!path->dentry->d_op || !path->dentry->d_op->d_automount) 9329875cf80SDavid Howells return -EREMOTE; 9339875cf80SDavid Howells 9340ec26fd0SMiklos Szeredi /* We don't want to mount if someone's just doing a stat - 9350ec26fd0SMiklos Szeredi * unless they're stat'ing a directory and appended a '/' to 9360ec26fd0SMiklos Szeredi * the name. 9370ec26fd0SMiklos Szeredi * 9380ec26fd0SMiklos Szeredi * We do, however, want to mount if someone wants to open or 9395a30d8a2SDavid Howells * create a file of any type under the mountpoint, wants to 9400ec26fd0SMiklos Szeredi * traverse through the mountpoint or wants to open the 9410ec26fd0SMiklos Szeredi * mounted directory. Also, autofs may mark negative dentries 9420ec26fd0SMiklos Szeredi * as being automount points. These will need the attentions 9430ec26fd0SMiklos Szeredi * of the daemon to instantiate them before they can be used. 9445a30d8a2SDavid Howells */ 9455a30d8a2SDavid Howells if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY | 946d94c177bSLinus Torvalds LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) && 9475a30d8a2SDavid Howells path->dentry->d_inode) 9489875cf80SDavid Howells return -EISDIR; 9490ec26fd0SMiklos Szeredi 9509875cf80SDavid Howells current->total_link_count++; 9519875cf80SDavid Howells if (current->total_link_count >= 40) 9529875cf80SDavid Howells return -ELOOP; 9539875cf80SDavid Howells 9549875cf80SDavid Howells mnt = path->dentry->d_op->d_automount(path); 9559875cf80SDavid Howells if (IS_ERR(mnt)) { 9569875cf80SDavid Howells /* 9579875cf80SDavid Howells * The filesystem is allowed to return -EISDIR here to indicate 9589875cf80SDavid Howells * it doesn't want to automount. For instance, autofs would do 9599875cf80SDavid Howells * this so that its userspace daemon can mount on this dentry. 9609875cf80SDavid Howells * 9619875cf80SDavid Howells * However, we can only permit this if it's a terminal point in 9629875cf80SDavid Howells * the path being looked up; if it wasn't then the remainder of 9639875cf80SDavid Howells * the path is inaccessible and we should say so. 9649875cf80SDavid Howells */ 96549084c3bSAl Viro if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT)) 9669875cf80SDavid Howells return -EREMOTE; 9679875cf80SDavid Howells return PTR_ERR(mnt); 96831e6b01fSNick Piggin } 969ea5b778aSDavid Howells 9709875cf80SDavid Howells if (!mnt) /* mount collision */ 9719875cf80SDavid Howells return 0; 9729875cf80SDavid Howells 9738aef1884SAl Viro if (!*need_mntput) { 9748aef1884SAl Viro /* lock_mount() may release path->mnt on error */ 9758aef1884SAl Viro mntget(path->mnt); 9768aef1884SAl Viro *need_mntput = true; 9778aef1884SAl Viro } 97819a167afSAl Viro err = finish_automount(mnt, path); 979ea5b778aSDavid Howells 980ea5b778aSDavid Howells switch (err) { 981ea5b778aSDavid Howells case -EBUSY: 982ea5b778aSDavid Howells /* Someone else made a mount here whilst we were busy */ 98319a167afSAl Viro return 0; 984ea5b778aSDavid Howells case 0: 9858aef1884SAl Viro path_put(path); 9869875cf80SDavid Howells path->mnt = mnt; 9879875cf80SDavid Howells path->dentry = dget(mnt->mnt_root); 9889875cf80SDavid Howells return 0; 98919a167afSAl Viro default: 99019a167afSAl Viro return err; 9919875cf80SDavid Howells } 99219a167afSAl Viro 993ea5b778aSDavid Howells } 9949875cf80SDavid Howells 9959875cf80SDavid Howells /* 9969875cf80SDavid Howells * Handle a dentry that is managed in some way. 997cc53ce53SDavid Howells * - Flagged for transit management (autofs) 9989875cf80SDavid Howells * - Flagged as mountpoint 9999875cf80SDavid Howells * - Flagged as automount point 10009875cf80SDavid Howells * 10019875cf80SDavid Howells * This may only be called in refwalk mode. 10029875cf80SDavid Howells * 10039875cf80SDavid Howells * Serialization is taken care of in namespace.c 10049875cf80SDavid Howells */ 10059875cf80SDavid Howells static int follow_managed(struct path *path, unsigned flags) 10069875cf80SDavid Howells { 10078aef1884SAl Viro struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */ 10089875cf80SDavid Howells unsigned managed; 10099875cf80SDavid Howells bool need_mntput = false; 10108aef1884SAl Viro int ret = 0; 10119875cf80SDavid Howells 10129875cf80SDavid Howells /* Given that we're not holding a lock here, we retain the value in a 10139875cf80SDavid Howells * local variable for each dentry as we look at it so that we don't see 10149875cf80SDavid Howells * the components of that value change under us */ 10159875cf80SDavid Howells while (managed = ACCESS_ONCE(path->dentry->d_flags), 10169875cf80SDavid Howells managed &= DCACHE_MANAGED_DENTRY, 10179875cf80SDavid Howells unlikely(managed != 0)) { 1018cc53ce53SDavid Howells /* Allow the filesystem to manage the transit without i_mutex 1019cc53ce53SDavid Howells * being held. */ 1020cc53ce53SDavid Howells if (managed & DCACHE_MANAGE_TRANSIT) { 1021cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op); 1022cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op->d_manage); 10231aed3e42SAl Viro ret = path->dentry->d_op->d_manage(path->dentry, false); 1024cc53ce53SDavid Howells if (ret < 0) 10258aef1884SAl Viro break; 1026cc53ce53SDavid Howells } 1027cc53ce53SDavid Howells 10289875cf80SDavid Howells /* Transit to a mounted filesystem. */ 10299875cf80SDavid Howells if (managed & DCACHE_MOUNTED) { 10309875cf80SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 10319875cf80SDavid Howells if (mounted) { 10329875cf80SDavid Howells dput(path->dentry); 10339875cf80SDavid Howells if (need_mntput) 1034463ffb2eSAl Viro mntput(path->mnt); 1035463ffb2eSAl Viro path->mnt = mounted; 1036463ffb2eSAl Viro path->dentry = dget(mounted->mnt_root); 10379875cf80SDavid Howells need_mntput = true; 10389875cf80SDavid Howells continue; 1039463ffb2eSAl Viro } 1040463ffb2eSAl Viro 10419875cf80SDavid Howells /* Something is mounted on this dentry in another 10429875cf80SDavid Howells * namespace and/or whatever was mounted there in this 10439875cf80SDavid Howells * namespace got unmounted before we managed to get the 10449875cf80SDavid Howells * vfsmount_lock */ 10451da177e4SLinus Torvalds } 10469875cf80SDavid Howells 10479875cf80SDavid Howells /* Handle an automount point */ 10489875cf80SDavid Howells if (managed & DCACHE_NEED_AUTOMOUNT) { 10499875cf80SDavid Howells ret = follow_automount(path, flags, &need_mntput); 10509875cf80SDavid Howells if (ret < 0) 10518aef1884SAl Viro break; 10529875cf80SDavid Howells continue; 10539875cf80SDavid Howells } 10549875cf80SDavid Howells 10559875cf80SDavid Howells /* We didn't change the current path point */ 10569875cf80SDavid Howells break; 10579875cf80SDavid Howells } 10588aef1884SAl Viro 10598aef1884SAl Viro if (need_mntput && path->mnt == mnt) 10608aef1884SAl Viro mntput(path->mnt); 10618aef1884SAl Viro if (ret == -EISDIR) 10628aef1884SAl Viro ret = 0; 1063a3fbbde7SAl Viro return ret < 0 ? ret : need_mntput; 10641da177e4SLinus Torvalds } 10651da177e4SLinus Torvalds 1066cc53ce53SDavid Howells int follow_down_one(struct path *path) 10671da177e4SLinus Torvalds { 10681da177e4SLinus Torvalds struct vfsmount *mounted; 10691da177e4SLinus Torvalds 10701c755af4SAl Viro mounted = lookup_mnt(path); 10711da177e4SLinus Torvalds if (mounted) { 10729393bd07SAl Viro dput(path->dentry); 10739393bd07SAl Viro mntput(path->mnt); 10749393bd07SAl Viro path->mnt = mounted; 10759393bd07SAl Viro path->dentry = dget(mounted->mnt_root); 10761da177e4SLinus Torvalds return 1; 10771da177e4SLinus Torvalds } 10781da177e4SLinus Torvalds return 0; 10791da177e4SLinus Torvalds } 10801da177e4SLinus Torvalds 108162a7375eSIan Kent static inline bool managed_dentry_might_block(struct dentry *dentry) 108262a7375eSIan Kent { 108362a7375eSIan Kent return (dentry->d_flags & DCACHE_MANAGE_TRANSIT && 108462a7375eSIan Kent dentry->d_op->d_manage(dentry, true) < 0); 108562a7375eSIan Kent } 108662a7375eSIan Kent 10879875cf80SDavid Howells /* 1088287548e4SAl Viro * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if 1089287548e4SAl Viro * we meet a managed dentry that would need blocking. 10909875cf80SDavid Howells */ 10919875cf80SDavid Howells static bool __follow_mount_rcu(struct nameidata *nd, struct path *path, 1092287548e4SAl Viro struct inode **inode) 10939875cf80SDavid Howells { 109462a7375eSIan Kent for (;;) { 1095c7105365SAl Viro struct mount *mounted; 109662a7375eSIan Kent /* 109762a7375eSIan Kent * Don't forget we might have a non-mountpoint managed dentry 109862a7375eSIan Kent * that wants to block transit. 109962a7375eSIan Kent */ 1100287548e4SAl Viro if (unlikely(managed_dentry_might_block(path->dentry))) 1101ab90911fSDavid Howells return false; 110262a7375eSIan Kent 110362a7375eSIan Kent if (!d_mountpoint(path->dentry)) 110462a7375eSIan Kent break; 110562a7375eSIan Kent 11069875cf80SDavid Howells mounted = __lookup_mnt(path->mnt, path->dentry, 1); 11079875cf80SDavid Howells if (!mounted) 11089875cf80SDavid Howells break; 1109c7105365SAl Viro path->mnt = &mounted->mnt; 1110c7105365SAl Viro path->dentry = mounted->mnt.mnt_root; 1111a3fbbde7SAl Viro nd->flags |= LOOKUP_JUMPED; 11129875cf80SDavid Howells nd->seq = read_seqcount_begin(&path->dentry->d_seq); 111359430262SLinus Torvalds /* 111459430262SLinus Torvalds * Update the inode too. We don't need to re-check the 111559430262SLinus Torvalds * dentry sequence number here after this d_inode read, 111659430262SLinus Torvalds * because a mount-point is always pinned. 111759430262SLinus Torvalds */ 111859430262SLinus Torvalds *inode = path->dentry->d_inode; 11199875cf80SDavid Howells } 11209875cf80SDavid Howells return true; 11219875cf80SDavid Howells } 11229875cf80SDavid Howells 1123dea39376SAl Viro static void follow_mount_rcu(struct nameidata *nd) 1124287548e4SAl Viro { 1125dea39376SAl Viro while (d_mountpoint(nd->path.dentry)) { 1126c7105365SAl Viro struct mount *mounted; 1127dea39376SAl Viro mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1); 1128287548e4SAl Viro if (!mounted) 1129287548e4SAl Viro break; 1130c7105365SAl Viro nd->path.mnt = &mounted->mnt; 1131c7105365SAl Viro nd->path.dentry = mounted->mnt.mnt_root; 1132dea39376SAl Viro nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); 1133287548e4SAl Viro } 1134287548e4SAl Viro } 1135287548e4SAl Viro 113631e6b01fSNick Piggin static int follow_dotdot_rcu(struct nameidata *nd) 113731e6b01fSNick Piggin { 113831e6b01fSNick Piggin set_root_rcu(nd); 113931e6b01fSNick Piggin 114031e6b01fSNick Piggin while (1) { 114131e6b01fSNick Piggin if (nd->path.dentry == nd->root.dentry && 114231e6b01fSNick Piggin nd->path.mnt == nd->root.mnt) { 114331e6b01fSNick Piggin break; 114431e6b01fSNick Piggin } 114531e6b01fSNick Piggin if (nd->path.dentry != nd->path.mnt->mnt_root) { 114631e6b01fSNick Piggin struct dentry *old = nd->path.dentry; 114731e6b01fSNick Piggin struct dentry *parent = old->d_parent; 114831e6b01fSNick Piggin unsigned seq; 114931e6b01fSNick Piggin 115031e6b01fSNick Piggin seq = read_seqcount_begin(&parent->d_seq); 115131e6b01fSNick Piggin if (read_seqcount_retry(&old->d_seq, nd->seq)) 1152ef7562d5SAl Viro goto failed; 115331e6b01fSNick Piggin nd->path.dentry = parent; 115431e6b01fSNick Piggin nd->seq = seq; 115531e6b01fSNick Piggin break; 115631e6b01fSNick Piggin } 115731e6b01fSNick Piggin if (!follow_up_rcu(&nd->path)) 115831e6b01fSNick Piggin break; 115931e6b01fSNick Piggin nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); 116031e6b01fSNick Piggin } 1161dea39376SAl Viro follow_mount_rcu(nd); 1162dea39376SAl Viro nd->inode = nd->path.dentry->d_inode; 116331e6b01fSNick Piggin return 0; 1164ef7562d5SAl Viro 1165ef7562d5SAl Viro failed: 1166ef7562d5SAl Viro nd->flags &= ~LOOKUP_RCU; 11675b6ca027SAl Viro if (!(nd->flags & LOOKUP_ROOT)) 1168ef7562d5SAl Viro nd->root.mnt = NULL; 116932a7991bSAl Viro unlock_rcu_walk(); 1170ef7562d5SAl Viro return -ECHILD; 117131e6b01fSNick Piggin } 117231e6b01fSNick Piggin 11739875cf80SDavid Howells /* 1174cc53ce53SDavid Howells * Follow down to the covering mount currently visible to userspace. At each 1175cc53ce53SDavid Howells * point, the filesystem owning that dentry may be queried as to whether the 1176cc53ce53SDavid Howells * caller is permitted to proceed or not. 1177cc53ce53SDavid Howells */ 11787cc90cc3SAl Viro int follow_down(struct path *path) 1179cc53ce53SDavid Howells { 1180cc53ce53SDavid Howells unsigned managed; 1181cc53ce53SDavid Howells int ret; 1182cc53ce53SDavid Howells 1183cc53ce53SDavid Howells while (managed = ACCESS_ONCE(path->dentry->d_flags), 1184cc53ce53SDavid Howells unlikely(managed & DCACHE_MANAGED_DENTRY)) { 1185cc53ce53SDavid Howells /* Allow the filesystem to manage the transit without i_mutex 1186cc53ce53SDavid Howells * being held. 1187cc53ce53SDavid Howells * 1188cc53ce53SDavid Howells * We indicate to the filesystem if someone is trying to mount 1189cc53ce53SDavid Howells * something here. This gives autofs the chance to deny anyone 1190cc53ce53SDavid Howells * other than its daemon the right to mount on its 1191cc53ce53SDavid Howells * superstructure. 1192cc53ce53SDavid Howells * 1193cc53ce53SDavid Howells * The filesystem may sleep at this point. 1194cc53ce53SDavid Howells */ 1195cc53ce53SDavid Howells if (managed & DCACHE_MANAGE_TRANSIT) { 1196cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op); 1197cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op->d_manage); 1198ab90911fSDavid Howells ret = path->dentry->d_op->d_manage( 11991aed3e42SAl Viro path->dentry, false); 1200cc53ce53SDavid Howells if (ret < 0) 1201cc53ce53SDavid Howells return ret == -EISDIR ? 0 : ret; 1202cc53ce53SDavid Howells } 1203cc53ce53SDavid Howells 1204cc53ce53SDavid Howells /* Transit to a mounted filesystem. */ 1205cc53ce53SDavid Howells if (managed & DCACHE_MOUNTED) { 1206cc53ce53SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 1207cc53ce53SDavid Howells if (!mounted) 1208cc53ce53SDavid Howells break; 1209cc53ce53SDavid Howells dput(path->dentry); 1210cc53ce53SDavid Howells mntput(path->mnt); 1211cc53ce53SDavid Howells path->mnt = mounted; 1212cc53ce53SDavid Howells path->dentry = dget(mounted->mnt_root); 1213cc53ce53SDavid Howells continue; 1214cc53ce53SDavid Howells } 1215cc53ce53SDavid Howells 1216cc53ce53SDavid Howells /* Don't handle automount points here */ 1217cc53ce53SDavid Howells break; 1218cc53ce53SDavid Howells } 1219cc53ce53SDavid Howells return 0; 1220cc53ce53SDavid Howells } 1221cc53ce53SDavid Howells 1222cc53ce53SDavid Howells /* 12239875cf80SDavid Howells * Skip to top of mountpoint pile in refwalk mode for follow_dotdot() 12249875cf80SDavid Howells */ 12259875cf80SDavid Howells static void follow_mount(struct path *path) 12269875cf80SDavid Howells { 12279875cf80SDavid Howells while (d_mountpoint(path->dentry)) { 12289875cf80SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 12299875cf80SDavid Howells if (!mounted) 12309875cf80SDavid Howells break; 12319875cf80SDavid Howells dput(path->dentry); 12329875cf80SDavid Howells mntput(path->mnt); 12339875cf80SDavid Howells path->mnt = mounted; 12349875cf80SDavid Howells path->dentry = dget(mounted->mnt_root); 12359875cf80SDavid Howells } 12369875cf80SDavid Howells } 12379875cf80SDavid Howells 123831e6b01fSNick Piggin static void follow_dotdot(struct nameidata *nd) 12391da177e4SLinus Torvalds { 12402a737871SAl Viro set_root(nd); 1241e518ddb7SAndreas Mohr 12421da177e4SLinus Torvalds while(1) { 12434ac91378SJan Blunck struct dentry *old = nd->path.dentry; 12441da177e4SLinus Torvalds 12452a737871SAl Viro if (nd->path.dentry == nd->root.dentry && 12462a737871SAl Viro nd->path.mnt == nd->root.mnt) { 12471da177e4SLinus Torvalds break; 12481da177e4SLinus Torvalds } 12494ac91378SJan Blunck if (nd->path.dentry != nd->path.mnt->mnt_root) { 12503088dd70SAl Viro /* rare case of legitimate dget_parent()... */ 12513088dd70SAl Viro nd->path.dentry = dget_parent(nd->path.dentry); 12521da177e4SLinus Torvalds dput(old); 12531da177e4SLinus Torvalds break; 12541da177e4SLinus Torvalds } 12553088dd70SAl Viro if (!follow_up(&nd->path)) 12561da177e4SLinus Torvalds break; 12571da177e4SLinus Torvalds } 125879ed0226SAl Viro follow_mount(&nd->path); 125931e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 12601da177e4SLinus Torvalds } 12611da177e4SLinus Torvalds 12621da177e4SLinus Torvalds /* 1263bad61189SMiklos Szeredi * This looks up the name in dcache, possibly revalidates the old dentry and 1264bad61189SMiklos Szeredi * allocates a new one if not found or not valid. In the need_lookup argument 1265bad61189SMiklos Szeredi * returns whether i_op->lookup is necessary. 1266bad61189SMiklos Szeredi * 1267bad61189SMiklos Szeredi * dir->d_inode->i_mutex must be held 1268baa03890SNick Piggin */ 1269bad61189SMiklos Szeredi static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir, 1270201f956eSAl Viro unsigned int flags, bool *need_lookup) 1271baa03890SNick Piggin { 1272baa03890SNick Piggin struct dentry *dentry; 1273bad61189SMiklos Szeredi int error; 1274baa03890SNick Piggin 1275bad61189SMiklos Szeredi *need_lookup = false; 1276bad61189SMiklos Szeredi dentry = d_lookup(dir, name); 1277bad61189SMiklos Szeredi if (dentry) { 127839e3c955SJeff Layton if (dentry->d_flags & DCACHE_OP_REVALIDATE) { 1279201f956eSAl Viro error = d_revalidate(dentry, flags); 1280bad61189SMiklos Szeredi if (unlikely(error <= 0)) { 1281bad61189SMiklos Szeredi if (error < 0) { 1282bad61189SMiklos Szeredi dput(dentry); 1283bad61189SMiklos Szeredi return ERR_PTR(error); 1284bad61189SMiklos Szeredi } else if (!d_invalidate(dentry)) { 1285bad61189SMiklos Szeredi dput(dentry); 1286bad61189SMiklos Szeredi dentry = NULL; 1287bad61189SMiklos Szeredi } 1288bad61189SMiklos Szeredi } 1289bad61189SMiklos Szeredi } 1290bad61189SMiklos Szeredi } 1291baa03890SNick Piggin 1292bad61189SMiklos Szeredi if (!dentry) { 1293bad61189SMiklos Szeredi dentry = d_alloc(dir, name); 1294baa03890SNick Piggin if (unlikely(!dentry)) 1295baa03890SNick Piggin return ERR_PTR(-ENOMEM); 1296baa03890SNick Piggin 1297bad61189SMiklos Szeredi *need_lookup = true; 1298baa03890SNick Piggin } 1299baa03890SNick Piggin return dentry; 1300baa03890SNick Piggin } 1301baa03890SNick Piggin 1302baa03890SNick Piggin /* 1303bad61189SMiklos Szeredi * Call i_op->lookup on the dentry. The dentry must be negative but may be 1304bad61189SMiklos Szeredi * hashed if it was pouplated with DCACHE_NEED_LOOKUP. 1305bad61189SMiklos Szeredi * 1306bad61189SMiklos Szeredi * dir->d_inode->i_mutex must be held 130744396f4bSJosef Bacik */ 1308bad61189SMiklos Szeredi static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry, 130972bd866aSAl Viro unsigned int flags) 131044396f4bSJosef Bacik { 131144396f4bSJosef Bacik struct dentry *old; 131244396f4bSJosef Bacik 131344396f4bSJosef Bacik /* Don't create child dentry for a dead directory. */ 1314bad61189SMiklos Szeredi if (unlikely(IS_DEADDIR(dir))) { 1315e188dc02SMiklos Szeredi dput(dentry); 131644396f4bSJosef Bacik return ERR_PTR(-ENOENT); 1317e188dc02SMiklos Szeredi } 131844396f4bSJosef Bacik 131972bd866aSAl Viro old = dir->i_op->lookup(dir, dentry, flags); 132044396f4bSJosef Bacik if (unlikely(old)) { 132144396f4bSJosef Bacik dput(dentry); 132244396f4bSJosef Bacik dentry = old; 132344396f4bSJosef Bacik } 132444396f4bSJosef Bacik return dentry; 132544396f4bSJosef Bacik } 132644396f4bSJosef Bacik 1327a3255546SAl Viro static struct dentry *__lookup_hash(struct qstr *name, 132872bd866aSAl Viro struct dentry *base, unsigned int flags) 1329a3255546SAl Viro { 1330bad61189SMiklos Szeredi bool need_lookup; 1331a3255546SAl Viro struct dentry *dentry; 1332a3255546SAl Viro 133372bd866aSAl Viro dentry = lookup_dcache(name, base, flags, &need_lookup); 1334bad61189SMiklos Szeredi if (!need_lookup) 1335a3255546SAl Viro return dentry; 1336bad61189SMiklos Szeredi 133772bd866aSAl Viro return lookup_real(base->d_inode, dentry, flags); 1338a3255546SAl Viro } 1339a3255546SAl Viro 134044396f4bSJosef Bacik /* 13411da177e4SLinus Torvalds * It's more convoluted than I'd like it to be, but... it's still fairly 13421da177e4SLinus Torvalds * small and for now I'd prefer to have fast path as straight as possible. 13431da177e4SLinus Torvalds * It _is_ time-critical. 13441da177e4SLinus Torvalds */ 1345697f514dSMiklos Szeredi static int lookup_fast(struct nameidata *nd, struct qstr *name, 134631e6b01fSNick Piggin struct path *path, struct inode **inode) 13471da177e4SLinus Torvalds { 13484ac91378SJan Blunck struct vfsmount *mnt = nd->path.mnt; 134931e6b01fSNick Piggin struct dentry *dentry, *parent = nd->path.dentry; 13505a18fff2SAl Viro int need_reval = 1; 13515a18fff2SAl Viro int status = 1; 13529875cf80SDavid Howells int err; 13539875cf80SDavid Howells 13543cac260aSAl Viro /* 1355b04f784eSNick Piggin * Rename seqlock is not required here because in the off chance 1356b04f784eSNick Piggin * of a false negative due to a concurrent rename, we're going to 1357b04f784eSNick Piggin * do the non-racy lookup, below. 1358b04f784eSNick Piggin */ 135931e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) { 136031e6b01fSNick Piggin unsigned seq; 136112f8ad4bSLinus Torvalds dentry = __d_lookup_rcu(parent, name, &seq, nd->inode); 13625a18fff2SAl Viro if (!dentry) 13635a18fff2SAl Viro goto unlazy; 13645a18fff2SAl Viro 136512f8ad4bSLinus Torvalds /* 136612f8ad4bSLinus Torvalds * This sequence count validates that the inode matches 136712f8ad4bSLinus Torvalds * the dentry name information from lookup. 136812f8ad4bSLinus Torvalds */ 136912f8ad4bSLinus Torvalds *inode = dentry->d_inode; 137012f8ad4bSLinus Torvalds if (read_seqcount_retry(&dentry->d_seq, seq)) 137112f8ad4bSLinus Torvalds return -ECHILD; 137212f8ad4bSLinus Torvalds 137312f8ad4bSLinus Torvalds /* 137412f8ad4bSLinus Torvalds * This sequence count validates that the parent had no 137512f8ad4bSLinus Torvalds * changes while we did the lookup of the dentry above. 137612f8ad4bSLinus Torvalds * 137712f8ad4bSLinus Torvalds * The memory barrier in read_seqcount_begin of child is 137812f8ad4bSLinus Torvalds * enough, we can use __read_seqcount_retry here. 137912f8ad4bSLinus Torvalds */ 138031e6b01fSNick Piggin if (__read_seqcount_retry(&parent->d_seq, nd->seq)) 138131e6b01fSNick Piggin return -ECHILD; 138231e6b01fSNick Piggin nd->seq = seq; 13835a18fff2SAl Viro 138424643087SAl Viro if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) { 13854ce16ef3SAl Viro status = d_revalidate(dentry, nd->flags); 13865a18fff2SAl Viro if (unlikely(status <= 0)) { 13875a18fff2SAl Viro if (status != -ECHILD) 13885a18fff2SAl Viro need_reval = 0; 13895a18fff2SAl Viro goto unlazy; 13905a18fff2SAl Viro } 139124643087SAl Viro } 139231e6b01fSNick Piggin path->mnt = mnt; 139331e6b01fSNick Piggin path->dentry = dentry; 1394d6e9bd25SAl Viro if (unlikely(!__follow_mount_rcu(nd, path, inode))) 1395d6e9bd25SAl Viro goto unlazy; 1396d6e9bd25SAl Viro if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT)) 1397d6e9bd25SAl Viro goto unlazy; 13989875cf80SDavid Howells return 0; 13995a18fff2SAl Viro unlazy: 140019660af7SAl Viro if (unlazy_walk(nd, dentry)) 14015a18fff2SAl Viro return -ECHILD; 14025a18fff2SAl Viro } else { 140331e6b01fSNick Piggin dentry = __d_lookup(parent, name); 140424643087SAl Viro } 14055a18fff2SAl Viro 140681e6f520SAl Viro if (unlikely(!dentry)) 140781e6f520SAl Viro goto need_lookup; 14085a18fff2SAl Viro 14095a18fff2SAl Viro if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval) 14104ce16ef3SAl Viro status = d_revalidate(dentry, nd->flags); 14115a18fff2SAl Viro if (unlikely(status <= 0)) { 14125a18fff2SAl Viro if (status < 0) { 14135a18fff2SAl Viro dput(dentry); 14145a18fff2SAl Viro return status; 14155a18fff2SAl Viro } 14165a18fff2SAl Viro if (!d_invalidate(dentry)) { 14175a18fff2SAl Viro dput(dentry); 141881e6f520SAl Viro goto need_lookup; 14195a18fff2SAl Viro } 14205a18fff2SAl Viro } 1421697f514dSMiklos Szeredi 14221da177e4SLinus Torvalds path->mnt = mnt; 14231da177e4SLinus Torvalds path->dentry = dentry; 14249875cf80SDavid Howells err = follow_managed(path, nd->flags); 142589312214SIan Kent if (unlikely(err < 0)) { 142689312214SIan Kent path_put_conditional(path, nd); 14279875cf80SDavid Howells return err; 142889312214SIan Kent } 1429a3fbbde7SAl Viro if (err) 1430a3fbbde7SAl Viro nd->flags |= LOOKUP_JUMPED; 143131e6b01fSNick Piggin *inode = path->dentry->d_inode; 14321da177e4SLinus Torvalds return 0; 143381e6f520SAl Viro 143481e6f520SAl Viro need_lookup: 1435697f514dSMiklos Szeredi return 1; 1436697f514dSMiklos Szeredi } 1437697f514dSMiklos Szeredi 1438697f514dSMiklos Szeredi /* Fast lookup failed, do it the slow way */ 1439697f514dSMiklos Szeredi static int lookup_slow(struct nameidata *nd, struct qstr *name, 1440697f514dSMiklos Szeredi struct path *path) 1441697f514dSMiklos Szeredi { 1442697f514dSMiklos Szeredi struct dentry *dentry, *parent; 1443697f514dSMiklos Szeredi int err; 1444697f514dSMiklos Szeredi 1445697f514dSMiklos Szeredi parent = nd->path.dentry; 144681e6f520SAl Viro BUG_ON(nd->inode != parent->d_inode); 144781e6f520SAl Viro 144881e6f520SAl Viro mutex_lock(&parent->d_inode->i_mutex); 144972bd866aSAl Viro dentry = __lookup_hash(name, parent, nd->flags); 145081e6f520SAl Viro mutex_unlock(&parent->d_inode->i_mutex); 145181e6f520SAl Viro if (IS_ERR(dentry)) 145281e6f520SAl Viro return PTR_ERR(dentry); 1453697f514dSMiklos Szeredi path->mnt = nd->path.mnt; 1454697f514dSMiklos Szeredi path->dentry = dentry; 1455697f514dSMiklos Szeredi err = follow_managed(path, nd->flags); 1456697f514dSMiklos Szeredi if (unlikely(err < 0)) { 1457697f514dSMiklos Szeredi path_put_conditional(path, nd); 1458697f514dSMiklos Szeredi return err; 1459697f514dSMiklos Szeredi } 1460697f514dSMiklos Szeredi if (err) 1461697f514dSMiklos Szeredi nd->flags |= LOOKUP_JUMPED; 1462697f514dSMiklos Szeredi return 0; 14631da177e4SLinus Torvalds } 14641da177e4SLinus Torvalds 146552094c8aSAl Viro static inline int may_lookup(struct nameidata *nd) 146652094c8aSAl Viro { 146752094c8aSAl Viro if (nd->flags & LOOKUP_RCU) { 14684ad5abb3SAl Viro int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK); 146952094c8aSAl Viro if (err != -ECHILD) 147052094c8aSAl Viro return err; 147119660af7SAl Viro if (unlazy_walk(nd, NULL)) 147252094c8aSAl Viro return -ECHILD; 147352094c8aSAl Viro } 14744ad5abb3SAl Viro return inode_permission(nd->inode, MAY_EXEC); 147552094c8aSAl Viro } 147652094c8aSAl Viro 14779856fa1bSAl Viro static inline int handle_dots(struct nameidata *nd, int type) 14789856fa1bSAl Viro { 14799856fa1bSAl Viro if (type == LAST_DOTDOT) { 14809856fa1bSAl Viro if (nd->flags & LOOKUP_RCU) { 14819856fa1bSAl Viro if (follow_dotdot_rcu(nd)) 14829856fa1bSAl Viro return -ECHILD; 14839856fa1bSAl Viro } else 14849856fa1bSAl Viro follow_dotdot(nd); 14859856fa1bSAl Viro } 14869856fa1bSAl Viro return 0; 14879856fa1bSAl Viro } 14889856fa1bSAl Viro 1489951361f9SAl Viro static void terminate_walk(struct nameidata *nd) 1490951361f9SAl Viro { 1491951361f9SAl Viro if (!(nd->flags & LOOKUP_RCU)) { 1492951361f9SAl Viro path_put(&nd->path); 1493951361f9SAl Viro } else { 1494951361f9SAl Viro nd->flags &= ~LOOKUP_RCU; 14955b6ca027SAl Viro if (!(nd->flags & LOOKUP_ROOT)) 1496951361f9SAl Viro nd->root.mnt = NULL; 149732a7991bSAl Viro unlock_rcu_walk(); 1498951361f9SAl Viro } 1499951361f9SAl Viro } 1500951361f9SAl Viro 15013ddcd056SLinus Torvalds /* 15023ddcd056SLinus Torvalds * Do we need to follow links? We _really_ want to be able 15033ddcd056SLinus Torvalds * to do this check without having to look at inode->i_op, 15043ddcd056SLinus Torvalds * so we keep a cache of "no, this doesn't need follow_link" 15053ddcd056SLinus Torvalds * for the common case. 15063ddcd056SLinus Torvalds */ 15077813b94aSLinus Torvalds static inline int should_follow_link(struct inode *inode, int follow) 15083ddcd056SLinus Torvalds { 15093ddcd056SLinus Torvalds if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) { 15103ddcd056SLinus Torvalds if (likely(inode->i_op->follow_link)) 15113ddcd056SLinus Torvalds return follow; 15123ddcd056SLinus Torvalds 15133ddcd056SLinus Torvalds /* This gets set once for the inode lifetime */ 15143ddcd056SLinus Torvalds spin_lock(&inode->i_lock); 15153ddcd056SLinus Torvalds inode->i_opflags |= IOP_NOFOLLOW; 15163ddcd056SLinus Torvalds spin_unlock(&inode->i_lock); 15173ddcd056SLinus Torvalds } 15183ddcd056SLinus Torvalds return 0; 15193ddcd056SLinus Torvalds } 15203ddcd056SLinus Torvalds 1521ce57dfc1SAl Viro static inline int walk_component(struct nameidata *nd, struct path *path, 1522ce57dfc1SAl Viro struct qstr *name, int type, int follow) 1523ce57dfc1SAl Viro { 1524ce57dfc1SAl Viro struct inode *inode; 1525ce57dfc1SAl Viro int err; 1526ce57dfc1SAl Viro /* 1527ce57dfc1SAl Viro * "." and ".." are special - ".." especially so because it has 1528ce57dfc1SAl Viro * to be able to know about the current root directory and 1529ce57dfc1SAl Viro * parent relationships. 1530ce57dfc1SAl Viro */ 1531ce57dfc1SAl Viro if (unlikely(type != LAST_NORM)) 1532ce57dfc1SAl Viro return handle_dots(nd, type); 1533697f514dSMiklos Szeredi err = lookup_fast(nd, name, path, &inode); 1534ce57dfc1SAl Viro if (unlikely(err)) { 1535697f514dSMiklos Szeredi if (err < 0) 1536697f514dSMiklos Szeredi goto out_err; 1537697f514dSMiklos Szeredi 1538697f514dSMiklos Szeredi err = lookup_slow(nd, name, path); 1539697f514dSMiklos Szeredi if (err < 0) 1540697f514dSMiklos Szeredi goto out_err; 1541697f514dSMiklos Szeredi 1542697f514dSMiklos Szeredi inode = path->dentry->d_inode; 1543ce57dfc1SAl Viro } 1544697f514dSMiklos Szeredi err = -ENOENT; 1545697f514dSMiklos Szeredi if (!inode) 1546697f514dSMiklos Szeredi goto out_path_put; 1547697f514dSMiklos Szeredi 15487813b94aSLinus Torvalds if (should_follow_link(inode, follow)) { 154919660af7SAl Viro if (nd->flags & LOOKUP_RCU) { 155019660af7SAl Viro if (unlikely(unlazy_walk(nd, path->dentry))) { 1551697f514dSMiklos Szeredi err = -ECHILD; 1552697f514dSMiklos Szeredi goto out_err; 155319660af7SAl Viro } 155419660af7SAl Viro } 1555ce57dfc1SAl Viro BUG_ON(inode != path->dentry->d_inode); 1556ce57dfc1SAl Viro return 1; 1557ce57dfc1SAl Viro } 1558ce57dfc1SAl Viro path_to_nameidata(path, nd); 1559ce57dfc1SAl Viro nd->inode = inode; 1560ce57dfc1SAl Viro return 0; 1561697f514dSMiklos Szeredi 1562697f514dSMiklos Szeredi out_path_put: 1563697f514dSMiklos Szeredi path_to_nameidata(path, nd); 1564697f514dSMiklos Szeredi out_err: 1565697f514dSMiklos Szeredi terminate_walk(nd); 1566697f514dSMiklos Szeredi return err; 1567ce57dfc1SAl Viro } 1568ce57dfc1SAl Viro 15691da177e4SLinus Torvalds /* 1570b356379aSAl Viro * This limits recursive symlink follows to 8, while 1571b356379aSAl Viro * limiting consecutive symlinks to 40. 1572b356379aSAl Viro * 1573b356379aSAl Viro * Without that kind of total limit, nasty chains of consecutive 1574b356379aSAl Viro * symlinks can cause almost arbitrarily long lookups. 1575b356379aSAl Viro */ 1576b356379aSAl Viro static inline int nested_symlink(struct path *path, struct nameidata *nd) 1577b356379aSAl Viro { 1578b356379aSAl Viro int res; 1579b356379aSAl Viro 1580b356379aSAl Viro if (unlikely(current->link_count >= MAX_NESTED_LINKS)) { 1581b356379aSAl Viro path_put_conditional(path, nd); 1582b356379aSAl Viro path_put(&nd->path); 1583b356379aSAl Viro return -ELOOP; 1584b356379aSAl Viro } 15851a4022f8SErez Zadok BUG_ON(nd->depth >= MAX_NESTED_LINKS); 1586b356379aSAl Viro 1587b356379aSAl Viro nd->depth++; 1588b356379aSAl Viro current->link_count++; 1589b356379aSAl Viro 1590b356379aSAl Viro do { 1591b356379aSAl Viro struct path link = *path; 1592b356379aSAl Viro void *cookie; 1593574197e0SAl Viro 1594574197e0SAl Viro res = follow_link(&link, nd, &cookie); 15956d7b5aaeSAl Viro if (res) 15966d7b5aaeSAl Viro break; 1597b356379aSAl Viro res = walk_component(nd, path, &nd->last, 1598b356379aSAl Viro nd->last_type, LOOKUP_FOLLOW); 1599574197e0SAl Viro put_link(nd, &link, cookie); 1600b356379aSAl Viro } while (res > 0); 1601b356379aSAl Viro 1602b356379aSAl Viro current->link_count--; 1603b356379aSAl Viro nd->depth--; 1604b356379aSAl Viro return res; 1605b356379aSAl Viro } 1606b356379aSAl Viro 1607b356379aSAl Viro /* 16083ddcd056SLinus Torvalds * We really don't want to look at inode->i_op->lookup 16093ddcd056SLinus Torvalds * when we don't have to. So we keep a cache bit in 16103ddcd056SLinus Torvalds * the inode ->i_opflags field that says "yes, we can 16113ddcd056SLinus Torvalds * do lookup on this inode". 16123ddcd056SLinus Torvalds */ 16133ddcd056SLinus Torvalds static inline int can_lookup(struct inode *inode) 16143ddcd056SLinus Torvalds { 16153ddcd056SLinus Torvalds if (likely(inode->i_opflags & IOP_LOOKUP)) 16163ddcd056SLinus Torvalds return 1; 16173ddcd056SLinus Torvalds if (likely(!inode->i_op->lookup)) 16183ddcd056SLinus Torvalds return 0; 16193ddcd056SLinus Torvalds 16203ddcd056SLinus Torvalds /* We do this once for the lifetime of the inode */ 16213ddcd056SLinus Torvalds spin_lock(&inode->i_lock); 16223ddcd056SLinus Torvalds inode->i_opflags |= IOP_LOOKUP; 16233ddcd056SLinus Torvalds spin_unlock(&inode->i_lock); 16243ddcd056SLinus Torvalds return 1; 16253ddcd056SLinus Torvalds } 16263ddcd056SLinus Torvalds 1627bfcfaa77SLinus Torvalds /* 1628bfcfaa77SLinus Torvalds * We can do the critical dentry name comparison and hashing 1629bfcfaa77SLinus Torvalds * operations one word at a time, but we are limited to: 1630bfcfaa77SLinus Torvalds * 1631bfcfaa77SLinus Torvalds * - Architectures with fast unaligned word accesses. We could 1632bfcfaa77SLinus Torvalds * do a "get_unaligned()" if this helps and is sufficiently 1633bfcfaa77SLinus Torvalds * fast. 1634bfcfaa77SLinus Torvalds * 1635bfcfaa77SLinus Torvalds * - Little-endian machines (so that we can generate the mask 1636bfcfaa77SLinus Torvalds * of low bytes efficiently). Again, we *could* do a byte 1637bfcfaa77SLinus Torvalds * swapping load on big-endian architectures if that is not 1638bfcfaa77SLinus Torvalds * expensive enough to make the optimization worthless. 1639bfcfaa77SLinus Torvalds * 1640bfcfaa77SLinus Torvalds * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we 1641bfcfaa77SLinus Torvalds * do not trap on the (extremely unlikely) case of a page 1642bfcfaa77SLinus Torvalds * crossing operation. 1643bfcfaa77SLinus Torvalds * 1644bfcfaa77SLinus Torvalds * - Furthermore, we need an efficient 64-bit compile for the 1645bfcfaa77SLinus Torvalds * 64-bit case in order to generate the "number of bytes in 1646bfcfaa77SLinus Torvalds * the final mask". Again, that could be replaced with a 1647bfcfaa77SLinus Torvalds * efficient population count instruction or similar. 1648bfcfaa77SLinus Torvalds */ 1649bfcfaa77SLinus Torvalds #ifdef CONFIG_DCACHE_WORD_ACCESS 1650bfcfaa77SLinus Torvalds 1651f68e556eSLinus Torvalds #include <asm/word-at-a-time.h> 1652bfcfaa77SLinus Torvalds 1653f68e556eSLinus Torvalds #ifdef CONFIG_64BIT 1654bfcfaa77SLinus Torvalds 1655bfcfaa77SLinus Torvalds static inline unsigned int fold_hash(unsigned long hash) 1656bfcfaa77SLinus Torvalds { 1657bfcfaa77SLinus Torvalds hash += hash >> (8*sizeof(int)); 1658bfcfaa77SLinus Torvalds return hash; 1659bfcfaa77SLinus Torvalds } 1660bfcfaa77SLinus Torvalds 1661bfcfaa77SLinus Torvalds #else /* 32-bit case */ 1662bfcfaa77SLinus Torvalds 1663bfcfaa77SLinus Torvalds #define fold_hash(x) (x) 1664bfcfaa77SLinus Torvalds 1665bfcfaa77SLinus Torvalds #endif 1666bfcfaa77SLinus Torvalds 1667bfcfaa77SLinus Torvalds unsigned int full_name_hash(const unsigned char *name, unsigned int len) 1668bfcfaa77SLinus Torvalds { 1669bfcfaa77SLinus Torvalds unsigned long a, mask; 1670bfcfaa77SLinus Torvalds unsigned long hash = 0; 1671bfcfaa77SLinus Torvalds 1672bfcfaa77SLinus Torvalds for (;;) { 1673e419b4ccSLinus Torvalds a = load_unaligned_zeropad(name); 1674bfcfaa77SLinus Torvalds if (len < sizeof(unsigned long)) 1675bfcfaa77SLinus Torvalds break; 1676bfcfaa77SLinus Torvalds hash += a; 1677f132c5beSAl Viro hash *= 9; 1678bfcfaa77SLinus Torvalds name += sizeof(unsigned long); 1679bfcfaa77SLinus Torvalds len -= sizeof(unsigned long); 1680bfcfaa77SLinus Torvalds if (!len) 1681bfcfaa77SLinus Torvalds goto done; 1682bfcfaa77SLinus Torvalds } 1683bfcfaa77SLinus Torvalds mask = ~(~0ul << len*8); 1684bfcfaa77SLinus Torvalds hash += mask & a; 1685bfcfaa77SLinus Torvalds done: 1686bfcfaa77SLinus Torvalds return fold_hash(hash); 1687bfcfaa77SLinus Torvalds } 1688bfcfaa77SLinus Torvalds EXPORT_SYMBOL(full_name_hash); 1689bfcfaa77SLinus Torvalds 1690bfcfaa77SLinus Torvalds /* 1691bfcfaa77SLinus Torvalds * Calculate the length and hash of the path component, and 1692bfcfaa77SLinus Torvalds * return the length of the component; 1693bfcfaa77SLinus Torvalds */ 1694bfcfaa77SLinus Torvalds static inline unsigned long hash_name(const char *name, unsigned int *hashp) 1695bfcfaa77SLinus Torvalds { 169636126f8fSLinus Torvalds unsigned long a, b, adata, bdata, mask, hash, len; 169736126f8fSLinus Torvalds const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS; 1698bfcfaa77SLinus Torvalds 1699bfcfaa77SLinus Torvalds hash = a = 0; 1700bfcfaa77SLinus Torvalds len = -sizeof(unsigned long); 1701bfcfaa77SLinus Torvalds do { 1702bfcfaa77SLinus Torvalds hash = (hash + a) * 9; 1703bfcfaa77SLinus Torvalds len += sizeof(unsigned long); 1704e419b4ccSLinus Torvalds a = load_unaligned_zeropad(name+len); 170536126f8fSLinus Torvalds b = a ^ REPEAT_BYTE('/'); 170636126f8fSLinus Torvalds } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants))); 1707bfcfaa77SLinus Torvalds 170836126f8fSLinus Torvalds adata = prep_zero_mask(a, adata, &constants); 170936126f8fSLinus Torvalds bdata = prep_zero_mask(b, bdata, &constants); 171036126f8fSLinus Torvalds 171136126f8fSLinus Torvalds mask = create_zero_mask(adata | bdata); 171236126f8fSLinus Torvalds 171336126f8fSLinus Torvalds hash += a & zero_bytemask(mask); 1714bfcfaa77SLinus Torvalds *hashp = fold_hash(hash); 1715bfcfaa77SLinus Torvalds 171636126f8fSLinus Torvalds return len + find_zero(mask); 1717bfcfaa77SLinus Torvalds } 1718bfcfaa77SLinus Torvalds 1719bfcfaa77SLinus Torvalds #else 1720bfcfaa77SLinus Torvalds 17210145acc2SLinus Torvalds unsigned int full_name_hash(const unsigned char *name, unsigned int len) 17220145acc2SLinus Torvalds { 17230145acc2SLinus Torvalds unsigned long hash = init_name_hash(); 17240145acc2SLinus Torvalds while (len--) 17250145acc2SLinus Torvalds hash = partial_name_hash(*name++, hash); 17260145acc2SLinus Torvalds return end_name_hash(hash); 17270145acc2SLinus Torvalds } 1728ae942ae7SLinus Torvalds EXPORT_SYMBOL(full_name_hash); 17290145acc2SLinus Torvalds 17303ddcd056SLinus Torvalds /* 1731200e9ef7SLinus Torvalds * We know there's a real path component here of at least 1732200e9ef7SLinus Torvalds * one character. 1733200e9ef7SLinus Torvalds */ 1734200e9ef7SLinus Torvalds static inline unsigned long hash_name(const char *name, unsigned int *hashp) 1735200e9ef7SLinus Torvalds { 1736200e9ef7SLinus Torvalds unsigned long hash = init_name_hash(); 1737200e9ef7SLinus Torvalds unsigned long len = 0, c; 1738200e9ef7SLinus Torvalds 1739200e9ef7SLinus Torvalds c = (unsigned char)*name; 1740200e9ef7SLinus Torvalds do { 1741200e9ef7SLinus Torvalds len++; 1742200e9ef7SLinus Torvalds hash = partial_name_hash(c, hash); 1743200e9ef7SLinus Torvalds c = (unsigned char)name[len]; 1744200e9ef7SLinus Torvalds } while (c && c != '/'); 1745200e9ef7SLinus Torvalds *hashp = end_name_hash(hash); 1746200e9ef7SLinus Torvalds return len; 1747200e9ef7SLinus Torvalds } 1748200e9ef7SLinus Torvalds 1749bfcfaa77SLinus Torvalds #endif 1750bfcfaa77SLinus Torvalds 1751200e9ef7SLinus Torvalds /* 17521da177e4SLinus Torvalds * Name resolution. 1753ea3834d9SPrasanna Meda * This is the basic name resolution function, turning a pathname into 1754ea3834d9SPrasanna Meda * the final dentry. We expect 'base' to be positive and a directory. 17551da177e4SLinus Torvalds * 1756ea3834d9SPrasanna Meda * Returns 0 and nd will have valid dentry and mnt on success. 1757ea3834d9SPrasanna Meda * Returns error and drops reference to input namei data on failure. 17581da177e4SLinus Torvalds */ 17596de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd) 17601da177e4SLinus Torvalds { 17611da177e4SLinus Torvalds struct path next; 17621da177e4SLinus Torvalds int err; 17631da177e4SLinus Torvalds 17641da177e4SLinus Torvalds while (*name=='/') 17651da177e4SLinus Torvalds name++; 17661da177e4SLinus Torvalds if (!*name) 1767086e183aSAl Viro return 0; 17681da177e4SLinus Torvalds 17691da177e4SLinus Torvalds /* At this point we know we have a real path component. */ 17701da177e4SLinus Torvalds for(;;) { 17711da177e4SLinus Torvalds struct qstr this; 1772200e9ef7SLinus Torvalds long len; 1773fe479a58SAl Viro int type; 17741da177e4SLinus Torvalds 177552094c8aSAl Viro err = may_lookup(nd); 17761da177e4SLinus Torvalds if (err) 17771da177e4SLinus Torvalds break; 17781da177e4SLinus Torvalds 1779200e9ef7SLinus Torvalds len = hash_name(name, &this.hash); 17801da177e4SLinus Torvalds this.name = name; 1781200e9ef7SLinus Torvalds this.len = len; 17821da177e4SLinus Torvalds 1783fe479a58SAl Viro type = LAST_NORM; 1784200e9ef7SLinus Torvalds if (name[0] == '.') switch (len) { 1785fe479a58SAl Viro case 2: 1786200e9ef7SLinus Torvalds if (name[1] == '.') { 1787fe479a58SAl Viro type = LAST_DOTDOT; 178816c2cd71SAl Viro nd->flags |= LOOKUP_JUMPED; 178916c2cd71SAl Viro } 1790fe479a58SAl Viro break; 1791fe479a58SAl Viro case 1: 1792fe479a58SAl Viro type = LAST_DOT; 1793fe479a58SAl Viro } 17945a202bcdSAl Viro if (likely(type == LAST_NORM)) { 17955a202bcdSAl Viro struct dentry *parent = nd->path.dentry; 179616c2cd71SAl Viro nd->flags &= ~LOOKUP_JUMPED; 17975a202bcdSAl Viro if (unlikely(parent->d_flags & DCACHE_OP_HASH)) { 17985a202bcdSAl Viro err = parent->d_op->d_hash(parent, nd->inode, 17995a202bcdSAl Viro &this); 18005a202bcdSAl Viro if (err < 0) 18015a202bcdSAl Viro break; 18025a202bcdSAl Viro } 18035a202bcdSAl Viro } 1804fe479a58SAl Viro 1805200e9ef7SLinus Torvalds if (!name[len]) 18061da177e4SLinus Torvalds goto last_component; 1807200e9ef7SLinus Torvalds /* 1808200e9ef7SLinus Torvalds * If it wasn't NUL, we know it was '/'. Skip that 1809200e9ef7SLinus Torvalds * slash, and continue until no more slashes. 1810200e9ef7SLinus Torvalds */ 1811200e9ef7SLinus Torvalds do { 1812200e9ef7SLinus Torvalds len++; 1813200e9ef7SLinus Torvalds } while (unlikely(name[len] == '/')); 1814200e9ef7SLinus Torvalds if (!name[len]) 1815b356379aSAl Viro goto last_component; 1816200e9ef7SLinus Torvalds name += len; 18171da177e4SLinus Torvalds 1818ce57dfc1SAl Viro err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW); 1819ce57dfc1SAl Viro if (err < 0) 1820ce57dfc1SAl Viro return err; 1821fe479a58SAl Viro 1822ce57dfc1SAl Viro if (err) { 1823b356379aSAl Viro err = nested_symlink(&next, nd); 18241da177e4SLinus Torvalds if (err) 1825a7472babSAl Viro return err; 182631e6b01fSNick Piggin } 18273ddcd056SLinus Torvalds if (can_lookup(nd->inode)) 18281da177e4SLinus Torvalds continue; 18293ddcd056SLinus Torvalds err = -ENOTDIR; 18303ddcd056SLinus Torvalds break; 18311da177e4SLinus Torvalds /* here ends the main loop */ 18321da177e4SLinus Torvalds 18331da177e4SLinus Torvalds last_component: 1834ce57dfc1SAl Viro nd->last = this; 1835ce57dfc1SAl Viro nd->last_type = type; 1836ce57dfc1SAl Viro return 0; 1837ce57dfc1SAl Viro } 1838951361f9SAl Viro terminate_walk(nd); 18391da177e4SLinus Torvalds return err; 18401da177e4SLinus Torvalds } 18411da177e4SLinus Torvalds 184270e9b357SAl Viro static int path_init(int dfd, const char *name, unsigned int flags, 184370e9b357SAl Viro struct nameidata *nd, struct file **fp) 184431e6b01fSNick Piggin { 184531e6b01fSNick Piggin int retval = 0; 184631e6b01fSNick Piggin 184731e6b01fSNick Piggin nd->last_type = LAST_ROOT; /* if there are only slashes... */ 184816c2cd71SAl Viro nd->flags = flags | LOOKUP_JUMPED; 184931e6b01fSNick Piggin nd->depth = 0; 18505b6ca027SAl Viro if (flags & LOOKUP_ROOT) { 18515b6ca027SAl Viro struct inode *inode = nd->root.dentry->d_inode; 185273d049a4SAl Viro if (*name) { 1853741b7c3fSAl Viro if (!can_lookup(inode)) 18545b6ca027SAl Viro return -ENOTDIR; 18555b6ca027SAl Viro retval = inode_permission(inode, MAY_EXEC); 18565b6ca027SAl Viro if (retval) 18575b6ca027SAl Viro return retval; 185873d049a4SAl Viro } 18595b6ca027SAl Viro nd->path = nd->root; 18605b6ca027SAl Viro nd->inode = inode; 18615b6ca027SAl Viro if (flags & LOOKUP_RCU) { 186232a7991bSAl Viro lock_rcu_walk(); 18635b6ca027SAl Viro nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 18645b6ca027SAl Viro } else { 18655b6ca027SAl Viro path_get(&nd->path); 18665b6ca027SAl Viro } 18675b6ca027SAl Viro return 0; 18685b6ca027SAl Viro } 18695b6ca027SAl Viro 187031e6b01fSNick Piggin nd->root.mnt = NULL; 187131e6b01fSNick Piggin 187231e6b01fSNick Piggin if (*name=='/') { 1873e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 187432a7991bSAl Viro lock_rcu_walk(); 1875e41f7d4eSAl Viro set_root_rcu(nd); 1876e41f7d4eSAl Viro } else { 1877e41f7d4eSAl Viro set_root(nd); 1878e41f7d4eSAl Viro path_get(&nd->root); 1879e41f7d4eSAl Viro } 188031e6b01fSNick Piggin nd->path = nd->root; 188131e6b01fSNick Piggin } else if (dfd == AT_FDCWD) { 1882e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 188331e6b01fSNick Piggin struct fs_struct *fs = current->fs; 1884c28cc364SNick Piggin unsigned seq; 188531e6b01fSNick Piggin 188632a7991bSAl Viro lock_rcu_walk(); 188731e6b01fSNick Piggin 1888c28cc364SNick Piggin do { 1889c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 189031e6b01fSNick Piggin nd->path = fs->pwd; 1891c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 1892c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 1893e41f7d4eSAl Viro } else { 1894e41f7d4eSAl Viro get_fs_pwd(current->fs, &nd->path); 1895e41f7d4eSAl Viro } 189631e6b01fSNick Piggin } else { 1897582aa64aSJeff Layton /* Caller must check execute permissions on the starting path component */ 18982903ff01SAl Viro struct fd f = fdget_raw(dfd); 189931e6b01fSNick Piggin struct dentry *dentry; 190031e6b01fSNick Piggin 19012903ff01SAl Viro if (!f.file) 19022903ff01SAl Viro return -EBADF; 190331e6b01fSNick Piggin 19042903ff01SAl Viro dentry = f.file->f_path.dentry; 190531e6b01fSNick Piggin 1906f52e0c11SAl Viro if (*name) { 1907741b7c3fSAl Viro if (!can_lookup(dentry->d_inode)) { 19082903ff01SAl Viro fdput(f); 19092903ff01SAl Viro return -ENOTDIR; 1910f52e0c11SAl Viro } 19112903ff01SAl Viro } 19122903ff01SAl Viro 19132903ff01SAl Viro nd->path = f.file->f_path; 1914e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 19152903ff01SAl Viro if (f.need_put) 19162903ff01SAl Viro *fp = f.file; 1917c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 191832a7991bSAl Viro lock_rcu_walk(); 19195590ff0dSUlrich Drepper } else { 19202903ff01SAl Viro path_get(&nd->path); 19212903ff01SAl Viro fdput(f); 19221da177e4SLinus Torvalds } 1923e41f7d4eSAl Viro } 1924e41f7d4eSAl Viro 192531e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 19269b4a9b14SAl Viro return 0; 19279b4a9b14SAl Viro } 19289b4a9b14SAl Viro 1929bd92d7feSAl Viro static inline int lookup_last(struct nameidata *nd, struct path *path) 1930bd92d7feSAl Viro { 1931bd92d7feSAl Viro if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len]) 1932bd92d7feSAl Viro nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 1933bd92d7feSAl Viro 1934bd92d7feSAl Viro nd->flags &= ~LOOKUP_PARENT; 1935bd92d7feSAl Viro return walk_component(nd, path, &nd->last, nd->last_type, 1936bd92d7feSAl Viro nd->flags & LOOKUP_FOLLOW); 1937bd92d7feSAl Viro } 1938bd92d7feSAl Viro 19399b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */ 1940ee0827cdSAl Viro static int path_lookupat(int dfd, const char *name, 19419b4a9b14SAl Viro unsigned int flags, struct nameidata *nd) 19429b4a9b14SAl Viro { 194370e9b357SAl Viro struct file *base = NULL; 1944bd92d7feSAl Viro struct path path; 1945bd92d7feSAl Viro int err; 194631e6b01fSNick Piggin 194731e6b01fSNick Piggin /* 194831e6b01fSNick Piggin * Path walking is largely split up into 2 different synchronisation 194931e6b01fSNick Piggin * schemes, rcu-walk and ref-walk (explained in 195031e6b01fSNick Piggin * Documentation/filesystems/path-lookup.txt). These share much of the 195131e6b01fSNick Piggin * path walk code, but some things particularly setup, cleanup, and 195231e6b01fSNick Piggin * following mounts are sufficiently divergent that functions are 195331e6b01fSNick Piggin * duplicated. Typically there is a function foo(), and its RCU 195431e6b01fSNick Piggin * analogue, foo_rcu(). 195531e6b01fSNick Piggin * 195631e6b01fSNick Piggin * -ECHILD is the error number of choice (just to avoid clashes) that 195731e6b01fSNick Piggin * is returned if some aspect of an rcu-walk fails. Such an error must 195831e6b01fSNick Piggin * be handled by restarting a traditional ref-walk (which will always 195931e6b01fSNick Piggin * be able to complete). 196031e6b01fSNick Piggin */ 1961bd92d7feSAl Viro err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base); 1962ee0827cdSAl Viro 1963bd92d7feSAl Viro if (unlikely(err)) 1964bd92d7feSAl Viro return err; 1965ee0827cdSAl Viro 1966ee0827cdSAl Viro current->total_link_count = 0; 1967bd92d7feSAl Viro err = link_path_walk(name, nd); 1968bd92d7feSAl Viro 1969bd92d7feSAl Viro if (!err && !(flags & LOOKUP_PARENT)) { 1970bd92d7feSAl Viro err = lookup_last(nd, &path); 1971bd92d7feSAl Viro while (err > 0) { 1972bd92d7feSAl Viro void *cookie; 1973bd92d7feSAl Viro struct path link = path; 1974800179c9SKees Cook err = may_follow_link(&link, nd); 1975800179c9SKees Cook if (unlikely(err)) 1976800179c9SKees Cook break; 1977bd92d7feSAl Viro nd->flags |= LOOKUP_PARENT; 1978574197e0SAl Viro err = follow_link(&link, nd, &cookie); 19796d7b5aaeSAl Viro if (err) 19806d7b5aaeSAl Viro break; 1981bd92d7feSAl Viro err = lookup_last(nd, &path); 1982574197e0SAl Viro put_link(nd, &link, cookie); 1983bd92d7feSAl Viro } 1984bd92d7feSAl Viro } 1985ee0827cdSAl Viro 19869f1fafeeSAl Viro if (!err) 19879f1fafeeSAl Viro err = complete_walk(nd); 1988bd92d7feSAl Viro 1989bd92d7feSAl Viro if (!err && nd->flags & LOOKUP_DIRECTORY) { 1990bd92d7feSAl Viro if (!nd->inode->i_op->lookup) { 1991bd92d7feSAl Viro path_put(&nd->path); 1992bd23a539SAl Viro err = -ENOTDIR; 1993bd92d7feSAl Viro } 1994bd92d7feSAl Viro } 199516c2cd71SAl Viro 199670e9b357SAl Viro if (base) 199770e9b357SAl Viro fput(base); 1998ee0827cdSAl Viro 19995b6ca027SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) { 200031e6b01fSNick Piggin path_put(&nd->root); 200131e6b01fSNick Piggin nd->root.mnt = NULL; 200231e6b01fSNick Piggin } 2003bd92d7feSAl Viro return err; 200431e6b01fSNick Piggin } 200531e6b01fSNick Piggin 2006873f1eedSJeff Layton static int filename_lookup(int dfd, struct filename *name, 2007873f1eedSJeff Layton unsigned int flags, struct nameidata *nd) 2008873f1eedSJeff Layton { 2009873f1eedSJeff Layton int retval = path_lookupat(dfd, name->name, flags | LOOKUP_RCU, nd); 2010873f1eedSJeff Layton if (unlikely(retval == -ECHILD)) 2011873f1eedSJeff Layton retval = path_lookupat(dfd, name->name, flags, nd); 2012873f1eedSJeff Layton if (unlikely(retval == -ESTALE)) 2013873f1eedSJeff Layton retval = path_lookupat(dfd, name->name, 2014873f1eedSJeff Layton flags | LOOKUP_REVAL, nd); 2015873f1eedSJeff Layton 2016873f1eedSJeff Layton if (likely(!retval)) 2017adb5c247SJeff Layton audit_inode(name, nd->path.dentry, flags & LOOKUP_PARENT); 2018873f1eedSJeff Layton return retval; 2019873f1eedSJeff Layton } 2020873f1eedSJeff Layton 2021ee0827cdSAl Viro static int do_path_lookup(int dfd, const char *name, 2022ee0827cdSAl Viro unsigned int flags, struct nameidata *nd) 2023ee0827cdSAl Viro { 2024873f1eedSJeff Layton struct filename filename = { .name = name }; 2025ee0827cdSAl Viro 2026873f1eedSJeff Layton return filename_lookup(dfd, &filename, flags, nd); 20271da177e4SLinus Torvalds } 20281da177e4SLinus Torvalds 202979714f72SAl Viro /* does lookup, returns the object with parent locked */ 203079714f72SAl Viro struct dentry *kern_path_locked(const char *name, struct path *path) 20315590ff0dSUlrich Drepper { 203279714f72SAl Viro struct nameidata nd; 203379714f72SAl Viro struct dentry *d; 203479714f72SAl Viro int err = do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, &nd); 203579714f72SAl Viro if (err) 203679714f72SAl Viro return ERR_PTR(err); 203779714f72SAl Viro if (nd.last_type != LAST_NORM) { 203879714f72SAl Viro path_put(&nd.path); 203979714f72SAl Viro return ERR_PTR(-EINVAL); 204079714f72SAl Viro } 204179714f72SAl Viro mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 20421e0ea001SAl Viro d = __lookup_hash(&nd.last, nd.path.dentry, 0); 204379714f72SAl Viro if (IS_ERR(d)) { 204479714f72SAl Viro mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 204579714f72SAl Viro path_put(&nd.path); 204679714f72SAl Viro return d; 204779714f72SAl Viro } 204879714f72SAl Viro *path = nd.path; 204979714f72SAl Viro return d; 20505590ff0dSUlrich Drepper } 20515590ff0dSUlrich Drepper 2052d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path) 2053d1811465SAl Viro { 2054d1811465SAl Viro struct nameidata nd; 2055d1811465SAl Viro int res = do_path_lookup(AT_FDCWD, name, flags, &nd); 2056d1811465SAl Viro if (!res) 2057d1811465SAl Viro *path = nd.path; 2058d1811465SAl Viro return res; 2059d1811465SAl Viro } 2060d1811465SAl Viro 206116f18200SJosef 'Jeff' Sipek /** 206216f18200SJosef 'Jeff' Sipek * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair 206316f18200SJosef 'Jeff' Sipek * @dentry: pointer to dentry of the base directory 206416f18200SJosef 'Jeff' Sipek * @mnt: pointer to vfs mount of the base directory 206516f18200SJosef 'Jeff' Sipek * @name: pointer to file name 206616f18200SJosef 'Jeff' Sipek * @flags: lookup flags 2067e0a01249SAl Viro * @path: pointer to struct path to fill 206816f18200SJosef 'Jeff' Sipek */ 206916f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt, 207016f18200SJosef 'Jeff' Sipek const char *name, unsigned int flags, 2071e0a01249SAl Viro struct path *path) 207216f18200SJosef 'Jeff' Sipek { 2073e0a01249SAl Viro struct nameidata nd; 2074e0a01249SAl Viro int err; 2075e0a01249SAl Viro nd.root.dentry = dentry; 2076e0a01249SAl Viro nd.root.mnt = mnt; 2077e0a01249SAl Viro BUG_ON(flags & LOOKUP_PARENT); 20785b6ca027SAl Viro /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */ 2079e0a01249SAl Viro err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd); 2080e0a01249SAl Viro if (!err) 2081e0a01249SAl Viro *path = nd.path; 2082e0a01249SAl Viro return err; 208316f18200SJosef 'Jeff' Sipek } 208416f18200SJosef 'Jeff' Sipek 2085057f6c01SJames Morris /* 2086057f6c01SJames Morris * Restricted form of lookup. Doesn't follow links, single-component only, 2087057f6c01SJames Morris * needs parent already locked. Doesn't follow mounts. 2088057f6c01SJames Morris * SMP-safe. 2089057f6c01SJames Morris */ 2090a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd) 20911da177e4SLinus Torvalds { 209272bd866aSAl Viro return __lookup_hash(&nd->last, nd->path.dentry, nd->flags); 20931da177e4SLinus Torvalds } 20941da177e4SLinus Torvalds 2095eead1911SChristoph Hellwig /** 2096a6b91919SRandy Dunlap * lookup_one_len - filesystem helper to lookup single pathname component 2097eead1911SChristoph Hellwig * @name: pathname component to lookup 2098eead1911SChristoph Hellwig * @base: base directory to lookup from 2099eead1911SChristoph Hellwig * @len: maximum length @len should be interpreted to 2100eead1911SChristoph Hellwig * 2101a6b91919SRandy Dunlap * Note that this routine is purely a helper for filesystem usage and should 2102a6b91919SRandy Dunlap * not be called by generic code. Also note that by using this function the 2103eead1911SChristoph Hellwig * nameidata argument is passed to the filesystem methods and a filesystem 2104eead1911SChristoph Hellwig * using this helper needs to be prepared for that. 2105eead1911SChristoph Hellwig */ 2106057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len) 2107057f6c01SJames Morris { 2108057f6c01SJames Morris struct qstr this; 21096a96ba54SAl Viro unsigned int c; 2110cda309deSMiklos Szeredi int err; 2111057f6c01SJames Morris 21122f9092e1SDavid Woodhouse WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex)); 21132f9092e1SDavid Woodhouse 21146a96ba54SAl Viro this.name = name; 21156a96ba54SAl Viro this.len = len; 21160145acc2SLinus Torvalds this.hash = full_name_hash(name, len); 21176a96ba54SAl Viro if (!len) 21186a96ba54SAl Viro return ERR_PTR(-EACCES); 21196a96ba54SAl Viro 212021d8a15aSAl Viro if (unlikely(name[0] == '.')) { 212121d8a15aSAl Viro if (len < 2 || (len == 2 && name[1] == '.')) 212221d8a15aSAl Viro return ERR_PTR(-EACCES); 212321d8a15aSAl Viro } 212421d8a15aSAl Viro 21256a96ba54SAl Viro while (len--) { 21266a96ba54SAl Viro c = *(const unsigned char *)name++; 21276a96ba54SAl Viro if (c == '/' || c == '\0') 21286a96ba54SAl Viro return ERR_PTR(-EACCES); 21296a96ba54SAl Viro } 21305a202bcdSAl Viro /* 21315a202bcdSAl Viro * See if the low-level filesystem might want 21325a202bcdSAl Viro * to use its own hash.. 21335a202bcdSAl Viro */ 21345a202bcdSAl Viro if (base->d_flags & DCACHE_OP_HASH) { 21355a202bcdSAl Viro int err = base->d_op->d_hash(base, base->d_inode, &this); 21365a202bcdSAl Viro if (err < 0) 21375a202bcdSAl Viro return ERR_PTR(err); 21385a202bcdSAl Viro } 2139eead1911SChristoph Hellwig 2140cda309deSMiklos Szeredi err = inode_permission(base->d_inode, MAY_EXEC); 2141cda309deSMiklos Szeredi if (err) 2142cda309deSMiklos Szeredi return ERR_PTR(err); 2143cda309deSMiklos Szeredi 214472bd866aSAl Viro return __lookup_hash(&this, base, 0); 2145057f6c01SJames Morris } 2146057f6c01SJames Morris 21471fa1e7f6SAndy Whitcroft int user_path_at_empty(int dfd, const char __user *name, unsigned flags, 21481fa1e7f6SAndy Whitcroft struct path *path, int *empty) 21491da177e4SLinus Torvalds { 21502d8f3038SAl Viro struct nameidata nd; 215191a27b2aSJeff Layton struct filename *tmp = getname_flags(name, flags, empty); 21521da177e4SLinus Torvalds int err = PTR_ERR(tmp); 21531da177e4SLinus Torvalds if (!IS_ERR(tmp)) { 21542d8f3038SAl Viro 21552d8f3038SAl Viro BUG_ON(flags & LOOKUP_PARENT); 21562d8f3038SAl Viro 2157873f1eedSJeff Layton err = filename_lookup(dfd, tmp, flags, &nd); 21581da177e4SLinus Torvalds putname(tmp); 21592d8f3038SAl Viro if (!err) 21602d8f3038SAl Viro *path = nd.path; 21611da177e4SLinus Torvalds } 21621da177e4SLinus Torvalds return err; 21631da177e4SLinus Torvalds } 21641da177e4SLinus Torvalds 21651fa1e7f6SAndy Whitcroft int user_path_at(int dfd, const char __user *name, unsigned flags, 21661fa1e7f6SAndy Whitcroft struct path *path) 21671fa1e7f6SAndy Whitcroft { 2168f7493e5dSLinus Torvalds return user_path_at_empty(dfd, name, flags, path, NULL); 21691fa1e7f6SAndy Whitcroft } 21701fa1e7f6SAndy Whitcroft 2171873f1eedSJeff Layton /* 2172873f1eedSJeff Layton * NB: most callers don't do anything directly with the reference to the 2173873f1eedSJeff Layton * to struct filename, but the nd->last pointer points into the name string 2174873f1eedSJeff Layton * allocated by getname. So we must hold the reference to it until all 2175873f1eedSJeff Layton * path-walking is complete. 2176873f1eedSJeff Layton */ 217791a27b2aSJeff Layton static struct filename * 21789e790bd6SJeff Layton user_path_parent(int dfd, const char __user *path, struct nameidata *nd, 21799e790bd6SJeff Layton unsigned int flags) 21802ad94ae6SAl Viro { 218191a27b2aSJeff Layton struct filename *s = getname(path); 21822ad94ae6SAl Viro int error; 21832ad94ae6SAl Viro 21849e790bd6SJeff Layton /* only LOOKUP_REVAL is allowed in extra flags */ 21859e790bd6SJeff Layton flags &= LOOKUP_REVAL; 21869e790bd6SJeff Layton 21872ad94ae6SAl Viro if (IS_ERR(s)) 218891a27b2aSJeff Layton return s; 21892ad94ae6SAl Viro 21909e790bd6SJeff Layton error = filename_lookup(dfd, s, flags | LOOKUP_PARENT, nd); 219191a27b2aSJeff Layton if (error) { 21922ad94ae6SAl Viro putname(s); 219391a27b2aSJeff Layton return ERR_PTR(error); 219491a27b2aSJeff Layton } 21952ad94ae6SAl Viro 219691a27b2aSJeff Layton return s; 21972ad94ae6SAl Viro } 21982ad94ae6SAl Viro 21991da177e4SLinus Torvalds /* 22001da177e4SLinus Torvalds * It's inline, so penalty for filesystems that don't use sticky bit is 22011da177e4SLinus Torvalds * minimal. 22021da177e4SLinus Torvalds */ 22031da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode) 22041da177e4SLinus Torvalds { 22058e96e3b7SEric W. Biederman kuid_t fsuid = current_fsuid(); 2206da9592edSDavid Howells 22071da177e4SLinus Torvalds if (!(dir->i_mode & S_ISVTX)) 22081da177e4SLinus Torvalds return 0; 22098e96e3b7SEric W. Biederman if (uid_eq(inode->i_uid, fsuid)) 22101da177e4SLinus Torvalds return 0; 22118e96e3b7SEric W. Biederman if (uid_eq(dir->i_uid, fsuid)) 22121da177e4SLinus Torvalds return 0; 22131a48e2acSEric W. Biederman return !inode_capable(inode, CAP_FOWNER); 22141da177e4SLinus Torvalds } 22151da177e4SLinus Torvalds 22161da177e4SLinus Torvalds /* 22171da177e4SLinus Torvalds * Check whether we can remove a link victim from directory dir, check 22181da177e4SLinus Torvalds * whether the type of victim is right. 22191da177e4SLinus Torvalds * 1. We can't do it if dir is read-only (done in permission()) 22201da177e4SLinus Torvalds * 2. We should have write and exec permissions on dir 22211da177e4SLinus Torvalds * 3. We can't remove anything from append-only dir 22221da177e4SLinus Torvalds * 4. We can't do anything with immutable dir (done in permission()) 22231da177e4SLinus Torvalds * 5. If the sticky bit on dir is set we should either 22241da177e4SLinus Torvalds * a. be owner of dir, or 22251da177e4SLinus Torvalds * b. be owner of victim, or 22261da177e4SLinus Torvalds * c. have CAP_FOWNER capability 22271da177e4SLinus Torvalds * 6. If the victim is append-only or immutable we can't do antyhing with 22281da177e4SLinus Torvalds * links pointing to it. 22291da177e4SLinus Torvalds * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. 22301da177e4SLinus Torvalds * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. 22311da177e4SLinus Torvalds * 9. We can't remove a root or mountpoint. 22321da177e4SLinus Torvalds * 10. We don't allow removal of NFS sillyrenamed files; it's handled by 22331da177e4SLinus Torvalds * nfs_async_unlink(). 22341da177e4SLinus Torvalds */ 2235858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir) 22361da177e4SLinus Torvalds { 22371da177e4SLinus Torvalds int error; 22381da177e4SLinus Torvalds 22391da177e4SLinus Torvalds if (!victim->d_inode) 22401da177e4SLinus Torvalds return -ENOENT; 22411da177e4SLinus Torvalds 22421da177e4SLinus Torvalds BUG_ON(victim->d_parent->d_inode != dir); 22434fa6b5ecSJeff Layton audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE); 22441da177e4SLinus Torvalds 2245f419a2e3SAl Viro error = inode_permission(dir, MAY_WRITE | MAY_EXEC); 22461da177e4SLinus Torvalds if (error) 22471da177e4SLinus Torvalds return error; 22481da177e4SLinus Torvalds if (IS_APPEND(dir)) 22491da177e4SLinus Torvalds return -EPERM; 22501da177e4SLinus Torvalds if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)|| 2251f9454548SHugh Dickins IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode)) 22521da177e4SLinus Torvalds return -EPERM; 22531da177e4SLinus Torvalds if (isdir) { 22541da177e4SLinus Torvalds if (!S_ISDIR(victim->d_inode->i_mode)) 22551da177e4SLinus Torvalds return -ENOTDIR; 22561da177e4SLinus Torvalds if (IS_ROOT(victim)) 22571da177e4SLinus Torvalds return -EBUSY; 22581da177e4SLinus Torvalds } else if (S_ISDIR(victim->d_inode->i_mode)) 22591da177e4SLinus Torvalds return -EISDIR; 22601da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 22611da177e4SLinus Torvalds return -ENOENT; 22621da177e4SLinus Torvalds if (victim->d_flags & DCACHE_NFSFS_RENAMED) 22631da177e4SLinus Torvalds return -EBUSY; 22641da177e4SLinus Torvalds return 0; 22651da177e4SLinus Torvalds } 22661da177e4SLinus Torvalds 22671da177e4SLinus Torvalds /* Check whether we can create an object with dentry child in directory 22681da177e4SLinus Torvalds * dir. 22691da177e4SLinus Torvalds * 1. We can't do it if child already exists (open has special treatment for 22701da177e4SLinus Torvalds * this case, but since we are inlined it's OK) 22711da177e4SLinus Torvalds * 2. We can't do it if dir is read-only (done in permission()) 22721da177e4SLinus Torvalds * 3. We should have write and exec permissions on dir 22731da177e4SLinus Torvalds * 4. We can't do it if dir is immutable (done in permission()) 22741da177e4SLinus Torvalds */ 2275a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child) 22761da177e4SLinus Torvalds { 22771da177e4SLinus Torvalds if (child->d_inode) 22781da177e4SLinus Torvalds return -EEXIST; 22791da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 22801da177e4SLinus Torvalds return -ENOENT; 2281f419a2e3SAl Viro return inode_permission(dir, MAY_WRITE | MAY_EXEC); 22821da177e4SLinus Torvalds } 22831da177e4SLinus Torvalds 22841da177e4SLinus Torvalds /* 22851da177e4SLinus Torvalds * p1 and p2 should be directories on the same fs. 22861da177e4SLinus Torvalds */ 22871da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2) 22881da177e4SLinus Torvalds { 22891da177e4SLinus Torvalds struct dentry *p; 22901da177e4SLinus Torvalds 22911da177e4SLinus Torvalds if (p1 == p2) { 2292f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 22931da177e4SLinus Torvalds return NULL; 22941da177e4SLinus Torvalds } 22951da177e4SLinus Torvalds 2296a11f3a05SArjan van de Ven mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 22971da177e4SLinus Torvalds 2298e2761a11SOGAWA Hirofumi p = d_ancestor(p2, p1); 2299e2761a11SOGAWA Hirofumi if (p) { 2300f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT); 2301f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD); 23021da177e4SLinus Torvalds return p; 23031da177e4SLinus Torvalds } 23041da177e4SLinus Torvalds 2305e2761a11SOGAWA Hirofumi p = d_ancestor(p1, p2); 2306e2761a11SOGAWA Hirofumi if (p) { 2307f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 2308f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 23091da177e4SLinus Torvalds return p; 23101da177e4SLinus Torvalds } 23111da177e4SLinus Torvalds 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 NULL; 23151da177e4SLinus Torvalds } 23161da177e4SLinus Torvalds 23171da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2) 23181da177e4SLinus Torvalds { 23191b1dcc1bSJes Sorensen mutex_unlock(&p1->d_inode->i_mutex); 23201da177e4SLinus Torvalds if (p1 != p2) { 23211b1dcc1bSJes Sorensen mutex_unlock(&p2->d_inode->i_mutex); 2322a11f3a05SArjan van de Ven mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 23231da177e4SLinus Torvalds } 23241da177e4SLinus Torvalds } 23251da177e4SLinus Torvalds 23264acdaf27SAl Viro int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode, 2327312b63fbSAl Viro bool want_excl) 23281da177e4SLinus Torvalds { 2329a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 23301da177e4SLinus Torvalds if (error) 23311da177e4SLinus Torvalds return error; 23321da177e4SLinus Torvalds 2333acfa4380SAl Viro if (!dir->i_op->create) 23341da177e4SLinus Torvalds return -EACCES; /* shouldn't it be ENOSYS? */ 23351da177e4SLinus Torvalds mode &= S_IALLUGO; 23361da177e4SLinus Torvalds mode |= S_IFREG; 23371da177e4SLinus Torvalds error = security_inode_create(dir, dentry, mode); 23381da177e4SLinus Torvalds if (error) 23391da177e4SLinus Torvalds return error; 2340312b63fbSAl Viro error = dir->i_op->create(dir, dentry, mode, want_excl); 2341a74574aaSStephen Smalley if (!error) 2342f38aa942SAmy Griffis fsnotify_create(dir, dentry); 23431da177e4SLinus Torvalds return error; 23441da177e4SLinus Torvalds } 23451da177e4SLinus Torvalds 234673d049a4SAl Viro static int may_open(struct path *path, int acc_mode, int flag) 23471da177e4SLinus Torvalds { 23483fb64190SChristoph Hellwig struct dentry *dentry = path->dentry; 23491da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 23501da177e4SLinus Torvalds int error; 23511da177e4SLinus Torvalds 2352bcda7652SAl Viro /* O_PATH? */ 2353bcda7652SAl Viro if (!acc_mode) 2354bcda7652SAl Viro return 0; 2355bcda7652SAl Viro 23561da177e4SLinus Torvalds if (!inode) 23571da177e4SLinus Torvalds return -ENOENT; 23581da177e4SLinus Torvalds 2359c8fe8f30SChristoph Hellwig switch (inode->i_mode & S_IFMT) { 2360c8fe8f30SChristoph Hellwig case S_IFLNK: 23611da177e4SLinus Torvalds return -ELOOP; 2362c8fe8f30SChristoph Hellwig case S_IFDIR: 2363c8fe8f30SChristoph Hellwig if (acc_mode & MAY_WRITE) 23641da177e4SLinus Torvalds return -EISDIR; 2365c8fe8f30SChristoph Hellwig break; 2366c8fe8f30SChristoph Hellwig case S_IFBLK: 2367c8fe8f30SChristoph Hellwig case S_IFCHR: 23683fb64190SChristoph Hellwig if (path->mnt->mnt_flags & MNT_NODEV) 23691da177e4SLinus Torvalds return -EACCES; 2370c8fe8f30SChristoph Hellwig /*FALLTHRU*/ 2371c8fe8f30SChristoph Hellwig case S_IFIFO: 2372c8fe8f30SChristoph Hellwig case S_IFSOCK: 23731da177e4SLinus Torvalds flag &= ~O_TRUNC; 2374c8fe8f30SChristoph Hellwig break; 23754a3fd211SDave Hansen } 2376b41572e9SDave Hansen 23773fb64190SChristoph Hellwig error = inode_permission(inode, acc_mode); 2378b41572e9SDave Hansen if (error) 2379b41572e9SDave Hansen return error; 23806146f0d5SMimi Zohar 23811da177e4SLinus Torvalds /* 23821da177e4SLinus Torvalds * An append-only file must be opened in append mode for writing. 23831da177e4SLinus Torvalds */ 23841da177e4SLinus Torvalds if (IS_APPEND(inode)) { 23858737c930SAl Viro if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND)) 23867715b521SAl Viro return -EPERM; 23871da177e4SLinus Torvalds if (flag & O_TRUNC) 23887715b521SAl Viro return -EPERM; 23891da177e4SLinus Torvalds } 23901da177e4SLinus Torvalds 23911da177e4SLinus Torvalds /* O_NOATIME can only be set by the owner or superuser */ 23922e149670SSerge E. Hallyn if (flag & O_NOATIME && !inode_owner_or_capable(inode)) 23937715b521SAl Viro return -EPERM; 23941da177e4SLinus Torvalds 2395f3c7691eSJ. Bruce Fields return 0; 23967715b521SAl Viro } 23977715b521SAl Viro 2398e1181ee6SJeff Layton static int handle_truncate(struct file *filp) 23997715b521SAl Viro { 2400e1181ee6SJeff Layton struct path *path = &filp->f_path; 24017715b521SAl Viro struct inode *inode = path->dentry->d_inode; 24027715b521SAl Viro int error = get_write_access(inode); 24031da177e4SLinus Torvalds if (error) 24047715b521SAl Viro return error; 24051da177e4SLinus Torvalds /* 24061da177e4SLinus Torvalds * Refuse to truncate files with mandatory locks held on them. 24071da177e4SLinus Torvalds */ 24081da177e4SLinus Torvalds error = locks_verify_locked(inode); 2409be6d3e56SKentaro Takeda if (!error) 2410ea0d3ab2STetsuo Handa error = security_path_truncate(path); 24111da177e4SLinus Torvalds if (!error) { 24127715b521SAl Viro error = do_truncate(path->dentry, 0, 2413d139d7ffSMiklos Szeredi ATTR_MTIME|ATTR_CTIME|ATTR_OPEN, 2414e1181ee6SJeff Layton filp); 24151da177e4SLinus Torvalds } 24161da177e4SLinus Torvalds put_write_access(inode); 2417acd0c935SMimi Zohar return error; 24181da177e4SLinus Torvalds } 24191da177e4SLinus Torvalds 2420d57999e1SDave Hansen static inline int open_to_namei_flags(int flag) 2421d57999e1SDave Hansen { 24228a5e929dSAl Viro if ((flag & O_ACCMODE) == 3) 24238a5e929dSAl Viro flag--; 2424d57999e1SDave Hansen return flag; 2425d57999e1SDave Hansen } 2426d57999e1SDave Hansen 2427d18e9008SMiklos Szeredi static int may_o_create(struct path *dir, struct dentry *dentry, umode_t mode) 2428d18e9008SMiklos Szeredi { 2429d18e9008SMiklos Szeredi int error = security_path_mknod(dir, dentry, mode, 0); 2430d18e9008SMiklos Szeredi if (error) 2431d18e9008SMiklos Szeredi return error; 2432d18e9008SMiklos Szeredi 2433d18e9008SMiklos Szeredi error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC); 2434d18e9008SMiklos Szeredi if (error) 2435d18e9008SMiklos Szeredi return error; 2436d18e9008SMiklos Szeredi 2437d18e9008SMiklos Szeredi return security_inode_create(dir->dentry->d_inode, dentry, mode); 2438d18e9008SMiklos Szeredi } 2439d18e9008SMiklos Szeredi 24401acf0af9SDavid Howells /* 24411acf0af9SDavid Howells * Attempt to atomically look up, create and open a file from a negative 24421acf0af9SDavid Howells * dentry. 24431acf0af9SDavid Howells * 24441acf0af9SDavid Howells * Returns 0 if successful. The file will have been created and attached to 24451acf0af9SDavid Howells * @file by the filesystem calling finish_open(). 24461acf0af9SDavid Howells * 24471acf0af9SDavid Howells * Returns 1 if the file was looked up only or didn't need creating. The 24481acf0af9SDavid Howells * caller will need to perform the open themselves. @path will have been 24491acf0af9SDavid Howells * updated to point to the new dentry. This may be negative. 24501acf0af9SDavid Howells * 24511acf0af9SDavid Howells * Returns an error code otherwise. 24521acf0af9SDavid Howells */ 24532675a4ebSAl Viro static int atomic_open(struct nameidata *nd, struct dentry *dentry, 245430d90494SAl Viro struct path *path, struct file *file, 2455015c3bbcSMiklos Szeredi const struct open_flags *op, 245664894cf8SAl Viro bool got_write, bool need_lookup, 245747237687SAl Viro int *opened) 2458d18e9008SMiklos Szeredi { 2459d18e9008SMiklos Szeredi struct inode *dir = nd->path.dentry->d_inode; 2460d18e9008SMiklos Szeredi unsigned open_flag = open_to_namei_flags(op->open_flag); 2461d18e9008SMiklos Szeredi umode_t mode; 2462d18e9008SMiklos Szeredi int error; 2463d18e9008SMiklos Szeredi int acc_mode; 2464d18e9008SMiklos Szeredi int create_error = 0; 2465d18e9008SMiklos Szeredi struct dentry *const DENTRY_NOT_SET = (void *) -1UL; 2466d18e9008SMiklos Szeredi 2467d18e9008SMiklos Szeredi BUG_ON(dentry->d_inode); 2468d18e9008SMiklos Szeredi 2469d18e9008SMiklos Szeredi /* Don't create child dentry for a dead directory. */ 2470d18e9008SMiklos Szeredi if (unlikely(IS_DEADDIR(dir))) { 24712675a4ebSAl Viro error = -ENOENT; 2472d18e9008SMiklos Szeredi goto out; 2473d18e9008SMiklos Szeredi } 2474d18e9008SMiklos Szeredi 247562b259d8SMiklos Szeredi mode = op->mode; 2476d18e9008SMiklos Szeredi if ((open_flag & O_CREAT) && !IS_POSIXACL(dir)) 2477d18e9008SMiklos Szeredi mode &= ~current_umask(); 2478d18e9008SMiklos Szeredi 2479f8310c59SAl Viro if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT)) { 2480d18e9008SMiklos Szeredi open_flag &= ~O_TRUNC; 248147237687SAl Viro *opened |= FILE_CREATED; 2482d18e9008SMiklos Szeredi } 2483d18e9008SMiklos Szeredi 2484d18e9008SMiklos Szeredi /* 2485d18e9008SMiklos Szeredi * Checking write permission is tricky, bacuse we don't know if we are 2486d18e9008SMiklos Szeredi * going to actually need it: O_CREAT opens should work as long as the 2487d18e9008SMiklos Szeredi * file exists. But checking existence breaks atomicity. The trick is 2488d18e9008SMiklos Szeredi * to check access and if not granted clear O_CREAT from the flags. 2489d18e9008SMiklos Szeredi * 2490d18e9008SMiklos Szeredi * Another problem is returing the "right" error value (e.g. for an 2491d18e9008SMiklos Szeredi * O_EXCL open we want to return EEXIST not EROFS). 2492d18e9008SMiklos Szeredi */ 249364894cf8SAl Viro if (((open_flag & (O_CREAT | O_TRUNC)) || 249464894cf8SAl Viro (open_flag & O_ACCMODE) != O_RDONLY) && unlikely(!got_write)) { 249564894cf8SAl Viro if (!(open_flag & O_CREAT)) { 2496d18e9008SMiklos Szeredi /* 2497d18e9008SMiklos Szeredi * No O_CREATE -> atomicity not a requirement -> fall 2498d18e9008SMiklos Szeredi * back to lookup + open 2499d18e9008SMiklos Szeredi */ 2500d18e9008SMiklos Szeredi goto no_open; 2501d18e9008SMiklos Szeredi } else if (open_flag & (O_EXCL | O_TRUNC)) { 2502d18e9008SMiklos Szeredi /* Fall back and fail with the right error */ 250364894cf8SAl Viro create_error = -EROFS; 2504d18e9008SMiklos Szeredi goto no_open; 2505d18e9008SMiklos Szeredi } else { 2506d18e9008SMiklos Szeredi /* No side effects, safe to clear O_CREAT */ 250764894cf8SAl Viro create_error = -EROFS; 2508d18e9008SMiklos Szeredi open_flag &= ~O_CREAT; 2509d18e9008SMiklos Szeredi } 2510d18e9008SMiklos Szeredi } 2511d18e9008SMiklos Szeredi 2512d18e9008SMiklos Szeredi if (open_flag & O_CREAT) { 251338227f78SMiklos Szeredi error = may_o_create(&nd->path, dentry, mode); 2514d18e9008SMiklos Szeredi if (error) { 2515d18e9008SMiklos Szeredi create_error = error; 2516d18e9008SMiklos Szeredi if (open_flag & O_EXCL) 2517d18e9008SMiklos Szeredi goto no_open; 2518d18e9008SMiklos Szeredi open_flag &= ~O_CREAT; 2519d18e9008SMiklos Szeredi } 2520d18e9008SMiklos Szeredi } 2521d18e9008SMiklos Szeredi 2522d18e9008SMiklos Szeredi if (nd->flags & LOOKUP_DIRECTORY) 2523d18e9008SMiklos Szeredi open_flag |= O_DIRECTORY; 2524d18e9008SMiklos Szeredi 252530d90494SAl Viro file->f_path.dentry = DENTRY_NOT_SET; 252630d90494SAl Viro file->f_path.mnt = nd->path.mnt; 252730d90494SAl Viro error = dir->i_op->atomic_open(dir, dentry, file, open_flag, mode, 252847237687SAl Viro opened); 2529d9585277SAl Viro if (error < 0) { 2530d9585277SAl Viro if (create_error && error == -ENOENT) 2531d9585277SAl Viro error = create_error; 2532d18e9008SMiklos Szeredi goto out; 2533d18e9008SMiklos Szeredi } 2534d18e9008SMiklos Szeredi 2535d18e9008SMiklos Szeredi acc_mode = op->acc_mode; 253647237687SAl Viro if (*opened & FILE_CREATED) { 2537d18e9008SMiklos Szeredi fsnotify_create(dir, dentry); 2538d18e9008SMiklos Szeredi acc_mode = MAY_OPEN; 2539d18e9008SMiklos Szeredi } 2540d18e9008SMiklos Szeredi 2541d9585277SAl Viro if (error) { /* returned 1, that is */ 254230d90494SAl Viro if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) { 25432675a4ebSAl Viro error = -EIO; 2544d18e9008SMiklos Szeredi goto out; 2545d18e9008SMiklos Szeredi } 254630d90494SAl Viro if (file->f_path.dentry) { 2547d18e9008SMiklos Szeredi dput(dentry); 254830d90494SAl Viro dentry = file->f_path.dentry; 2549d18e9008SMiklos Szeredi } 255062b2ce96SSage Weil if (create_error && dentry->d_inode == NULL) { 255162b2ce96SSage Weil error = create_error; 255262b2ce96SSage Weil goto out; 255362b2ce96SSage Weil } 2554d18e9008SMiklos Szeredi goto looked_up; 2555d18e9008SMiklos Szeredi } 2556d18e9008SMiklos Szeredi 2557d18e9008SMiklos Szeredi /* 2558d18e9008SMiklos Szeredi * We didn't have the inode before the open, so check open permission 2559d18e9008SMiklos Szeredi * here. 2560d18e9008SMiklos Szeredi */ 25612675a4ebSAl Viro error = may_open(&file->f_path, acc_mode, open_flag); 25622675a4ebSAl Viro if (error) 25632675a4ebSAl Viro fput(file); 2564d18e9008SMiklos Szeredi 2565d18e9008SMiklos Szeredi out: 2566d18e9008SMiklos Szeredi dput(dentry); 25672675a4ebSAl Viro return error; 2568d18e9008SMiklos Szeredi 2569d18e9008SMiklos Szeredi no_open: 2570d18e9008SMiklos Szeredi if (need_lookup) { 257172bd866aSAl Viro dentry = lookup_real(dir, dentry, nd->flags); 2572d18e9008SMiklos Szeredi if (IS_ERR(dentry)) 25732675a4ebSAl Viro return PTR_ERR(dentry); 2574d18e9008SMiklos Szeredi 2575d18e9008SMiklos Szeredi if (create_error) { 2576d18e9008SMiklos Szeredi int open_flag = op->open_flag; 2577d18e9008SMiklos Szeredi 25782675a4ebSAl Viro error = create_error; 2579d18e9008SMiklos Szeredi if ((open_flag & O_EXCL)) { 2580d18e9008SMiklos Szeredi if (!dentry->d_inode) 2581d18e9008SMiklos Szeredi goto out; 2582d18e9008SMiklos Szeredi } else if (!dentry->d_inode) { 2583d18e9008SMiklos Szeredi goto out; 2584d18e9008SMiklos Szeredi } else if ((open_flag & O_TRUNC) && 2585d18e9008SMiklos Szeredi S_ISREG(dentry->d_inode->i_mode)) { 2586d18e9008SMiklos Szeredi goto out; 2587d18e9008SMiklos Szeredi } 2588d18e9008SMiklos Szeredi /* will fail later, go on to get the right error */ 2589d18e9008SMiklos Szeredi } 2590d18e9008SMiklos Szeredi } 2591d18e9008SMiklos Szeredi looked_up: 2592d18e9008SMiklos Szeredi path->dentry = dentry; 2593d18e9008SMiklos Szeredi path->mnt = nd->path.mnt; 25942675a4ebSAl Viro return 1; 2595d18e9008SMiklos Szeredi } 2596d18e9008SMiklos Szeredi 259731e6b01fSNick Piggin /* 25981acf0af9SDavid Howells * Look up and maybe create and open the last component. 2599d58ffd35SMiklos Szeredi * 2600d58ffd35SMiklos Szeredi * Must be called with i_mutex held on parent. 2601d58ffd35SMiklos Szeredi * 26021acf0af9SDavid Howells * Returns 0 if the file was successfully atomically created (if necessary) and 26031acf0af9SDavid Howells * opened. In this case the file will be returned attached to @file. 26041acf0af9SDavid Howells * 26051acf0af9SDavid Howells * Returns 1 if the file was not completely opened at this time, though lookups 26061acf0af9SDavid Howells * and creations will have been performed and the dentry returned in @path will 26071acf0af9SDavid Howells * be positive upon return if O_CREAT was specified. If O_CREAT wasn't 26081acf0af9SDavid Howells * specified then a negative dentry may be returned. 26091acf0af9SDavid Howells * 26101acf0af9SDavid Howells * An error code is returned otherwise. 26111acf0af9SDavid Howells * 26121acf0af9SDavid Howells * FILE_CREATE will be set in @*opened if the dentry was created and will be 26131acf0af9SDavid Howells * cleared otherwise prior to returning. 2614d58ffd35SMiklos Szeredi */ 26152675a4ebSAl Viro static int lookup_open(struct nameidata *nd, struct path *path, 261630d90494SAl Viro struct file *file, 2617d58ffd35SMiklos Szeredi const struct open_flags *op, 261864894cf8SAl Viro bool got_write, int *opened) 2619d58ffd35SMiklos Szeredi { 2620d58ffd35SMiklos Szeredi struct dentry *dir = nd->path.dentry; 262154ef4872SMiklos Szeredi struct inode *dir_inode = dir->d_inode; 2622d58ffd35SMiklos Szeredi struct dentry *dentry; 2623d58ffd35SMiklos Szeredi int error; 262454ef4872SMiklos Szeredi bool need_lookup; 2625d58ffd35SMiklos Szeredi 262647237687SAl Viro *opened &= ~FILE_CREATED; 2627201f956eSAl Viro dentry = lookup_dcache(&nd->last, dir, nd->flags, &need_lookup); 2628d58ffd35SMiklos Szeredi if (IS_ERR(dentry)) 26292675a4ebSAl Viro return PTR_ERR(dentry); 2630d58ffd35SMiklos Szeredi 2631d18e9008SMiklos Szeredi /* Cached positive dentry: will open in f_op->open */ 2632d18e9008SMiklos Szeredi if (!need_lookup && dentry->d_inode) 2633d18e9008SMiklos Szeredi goto out_no_open; 2634d18e9008SMiklos Szeredi 2635d18e9008SMiklos Szeredi if ((nd->flags & LOOKUP_OPEN) && dir_inode->i_op->atomic_open) { 263664894cf8SAl Viro return atomic_open(nd, dentry, path, file, op, got_write, 263747237687SAl Viro need_lookup, opened); 2638d18e9008SMiklos Szeredi } 2639d18e9008SMiklos Szeredi 264054ef4872SMiklos Szeredi if (need_lookup) { 264154ef4872SMiklos Szeredi BUG_ON(dentry->d_inode); 264254ef4872SMiklos Szeredi 264372bd866aSAl Viro dentry = lookup_real(dir_inode, dentry, nd->flags); 264454ef4872SMiklos Szeredi if (IS_ERR(dentry)) 26452675a4ebSAl Viro return PTR_ERR(dentry); 264654ef4872SMiklos Szeredi } 264754ef4872SMiklos Szeredi 2648d58ffd35SMiklos Szeredi /* Negative dentry, just create the file */ 2649d58ffd35SMiklos Szeredi if (!dentry->d_inode && (op->open_flag & O_CREAT)) { 2650d58ffd35SMiklos Szeredi umode_t mode = op->mode; 2651d58ffd35SMiklos Szeredi if (!IS_POSIXACL(dir->d_inode)) 2652d58ffd35SMiklos Szeredi mode &= ~current_umask(); 2653d58ffd35SMiklos Szeredi /* 2654d58ffd35SMiklos Szeredi * This write is needed to ensure that a 2655d58ffd35SMiklos Szeredi * rw->ro transition does not occur between 2656d58ffd35SMiklos Szeredi * the time when the file is created and when 2657d58ffd35SMiklos Szeredi * a permanent write count is taken through 2658015c3bbcSMiklos Szeredi * the 'struct file' in finish_open(). 2659d58ffd35SMiklos Szeredi */ 266064894cf8SAl Viro if (!got_write) { 266164894cf8SAl Viro error = -EROFS; 2662d58ffd35SMiklos Szeredi goto out_dput; 266364894cf8SAl Viro } 266447237687SAl Viro *opened |= FILE_CREATED; 2665d58ffd35SMiklos Szeredi error = security_path_mknod(&nd->path, dentry, mode, 0); 2666d58ffd35SMiklos Szeredi if (error) 2667d58ffd35SMiklos Szeredi goto out_dput; 2668312b63fbSAl Viro error = vfs_create(dir->d_inode, dentry, mode, 2669312b63fbSAl Viro nd->flags & LOOKUP_EXCL); 2670d58ffd35SMiklos Szeredi if (error) 2671d58ffd35SMiklos Szeredi goto out_dput; 2672d58ffd35SMiklos Szeredi } 2673d18e9008SMiklos Szeredi out_no_open: 2674d58ffd35SMiklos Szeredi path->dentry = dentry; 2675d58ffd35SMiklos Szeredi path->mnt = nd->path.mnt; 26762675a4ebSAl Viro return 1; 2677d58ffd35SMiklos Szeredi 2678d58ffd35SMiklos Szeredi out_dput: 2679d58ffd35SMiklos Szeredi dput(dentry); 26802675a4ebSAl Viro return error; 2681d58ffd35SMiklos Szeredi } 2682d58ffd35SMiklos Szeredi 2683d58ffd35SMiklos Szeredi /* 2684fe2d35ffSAl Viro * Handle the last step of open() 268531e6b01fSNick Piggin */ 26862675a4ebSAl Viro static int do_last(struct nameidata *nd, struct path *path, 268730d90494SAl Viro struct file *file, const struct open_flags *op, 2688669abf4eSJeff Layton int *opened, struct filename *name) 2689fb1cc555SAl Viro { 2690a1e28038SAl Viro struct dentry *dir = nd->path.dentry; 2691ca344a89SAl Viro int open_flag = op->open_flag; 269277d660a8SMiklos Szeredi bool will_truncate = (open_flag & O_TRUNC) != 0; 269364894cf8SAl Viro bool got_write = false; 2694bcda7652SAl Viro int acc_mode = op->acc_mode; 2695a1eb3315SMiklos Szeredi struct inode *inode; 269677d660a8SMiklos Szeredi bool symlink_ok = false; 269716b1c1cdSMiklos Szeredi struct path save_parent = { .dentry = NULL, .mnt = NULL }; 269816b1c1cdSMiklos Szeredi bool retried = false; 269916c2cd71SAl Viro int error; 2700fb1cc555SAl Viro 2701c3e380b0SAl Viro nd->flags &= ~LOOKUP_PARENT; 2702c3e380b0SAl Viro nd->flags |= op->intent; 2703c3e380b0SAl Viro 27041f36f774SAl Viro switch (nd->last_type) { 27051f36f774SAl Viro case LAST_DOTDOT: 2706176306f5SNeil Brown case LAST_DOT: 2707fe2d35ffSAl Viro error = handle_dots(nd, nd->last_type); 2708fe2d35ffSAl Viro if (error) 27092675a4ebSAl Viro return error; 27101f36f774SAl Viro /* fallthrough */ 27111f36f774SAl Viro case LAST_ROOT: 27129f1fafeeSAl Viro error = complete_walk(nd); 271316c2cd71SAl Viro if (error) 27142675a4ebSAl Viro return error; 2715adb5c247SJeff Layton audit_inode(name, nd->path.dentry, 0); 2716ca344a89SAl Viro if (open_flag & O_CREAT) { 271716c2cd71SAl Viro error = -EISDIR; 27182675a4ebSAl Viro goto out; 2719fe2d35ffSAl Viro } 2720e83db167SMiklos Szeredi goto finish_open; 27211f36f774SAl Viro case LAST_BIND: 27229f1fafeeSAl Viro error = complete_walk(nd); 272316c2cd71SAl Viro if (error) 27242675a4ebSAl Viro return error; 2725adb5c247SJeff Layton audit_inode(name, dir, 0); 2726e83db167SMiklos Szeredi goto finish_open; 27271f36f774SAl Viro } 2728a2c36b45SAl Viro 2729ca344a89SAl Viro if (!(open_flag & O_CREAT)) { 2730fe2d35ffSAl Viro if (nd->last.name[nd->last.len]) 2731fe2d35ffSAl Viro nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 2732bcda7652SAl Viro if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW)) 273377d660a8SMiklos Szeredi symlink_ok = true; 2734fe2d35ffSAl Viro /* we _can_ be in RCU mode here */ 2735a1eb3315SMiklos Szeredi error = lookup_fast(nd, &nd->last, path, &inode); 273671574865SMiklos Szeredi if (likely(!error)) 273771574865SMiklos Szeredi goto finish_lookup; 273871574865SMiklos Szeredi 2739ce57dfc1SAl Viro if (error < 0) 27402675a4ebSAl Viro goto out; 2741a1eb3315SMiklos Szeredi 274237d7fffcSMiklos Szeredi BUG_ON(nd->inode != dir->d_inode); 2743b6183df7SMiklos Szeredi } else { 2744fe2d35ffSAl Viro /* create side of things */ 2745a3fbbde7SAl Viro /* 2746b6183df7SMiklos Szeredi * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED 2747b6183df7SMiklos Szeredi * has been cleared when we got to the last component we are 2748b6183df7SMiklos Szeredi * about to look up 2749a3fbbde7SAl Viro */ 27509f1fafeeSAl Viro error = complete_walk(nd); 27519f1fafeeSAl Viro if (error) 27522675a4ebSAl Viro return error; 2753fe2d35ffSAl Viro 2754adb5c247SJeff Layton audit_inode(name, dir, 0); 275516c2cd71SAl Viro error = -EISDIR; 27561f36f774SAl Viro /* trailing slashes? */ 275731e6b01fSNick Piggin if (nd->last.name[nd->last.len]) 27582675a4ebSAl Viro goto out; 2759b6183df7SMiklos Szeredi } 27601f36f774SAl Viro 276116b1c1cdSMiklos Szeredi retry_lookup: 276264894cf8SAl Viro if (op->open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) { 276364894cf8SAl Viro error = mnt_want_write(nd->path.mnt); 276464894cf8SAl Viro if (!error) 276564894cf8SAl Viro got_write = true; 276664894cf8SAl Viro /* 276764894cf8SAl Viro * do _not_ fail yet - we might not need that or fail with 276864894cf8SAl Viro * a different error; let lookup_open() decide; we'll be 276964894cf8SAl Viro * dropping this one anyway. 277064894cf8SAl Viro */ 277164894cf8SAl Viro } 2772a1e28038SAl Viro mutex_lock(&dir->d_inode->i_mutex); 277364894cf8SAl Viro error = lookup_open(nd, path, file, op, got_write, opened); 2774fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2775fb1cc555SAl Viro 27762675a4ebSAl Viro if (error <= 0) { 27772675a4ebSAl Viro if (error) 2778d58ffd35SMiklos Szeredi goto out; 27796c0d46c4SAl Viro 278047237687SAl Viro if ((*opened & FILE_CREATED) || 27812675a4ebSAl Viro !S_ISREG(file->f_path.dentry->d_inode->i_mode)) 278277d660a8SMiklos Szeredi will_truncate = false; 2783d18e9008SMiklos Szeredi 2784adb5c247SJeff Layton audit_inode(name, file->f_path.dentry, 0); 2785d18e9008SMiklos Szeredi goto opened; 2786d18e9008SMiklos Szeredi } 2787d18e9008SMiklos Szeredi 278847237687SAl Viro if (*opened & FILE_CREATED) { 27899b44f1b3SAl Viro /* Don't check for write permission, don't truncate */ 2790ca344a89SAl Viro open_flag &= ~O_TRUNC; 279177d660a8SMiklos Szeredi will_truncate = false; 2792bcda7652SAl Viro acc_mode = MAY_OPEN; 2793d58ffd35SMiklos Szeredi path_to_nameidata(path, nd); 2794e83db167SMiklos Szeredi goto finish_open_created; 2795fb1cc555SAl Viro } 2796fb1cc555SAl Viro 2797fb1cc555SAl Viro /* 27983134f37eSJeff Layton * create/update audit record if it already exists. 2799fb1cc555SAl Viro */ 28003134f37eSJeff Layton if (path->dentry->d_inode) 2801adb5c247SJeff Layton audit_inode(name, path->dentry, 0); 2802fb1cc555SAl Viro 2803d18e9008SMiklos Szeredi /* 2804d18e9008SMiklos Szeredi * If atomic_open() acquired write access it is dropped now due to 2805d18e9008SMiklos Szeredi * possible mount and symlink following (this might be optimized away if 2806d18e9008SMiklos Szeredi * necessary...) 2807d18e9008SMiklos Szeredi */ 280864894cf8SAl Viro if (got_write) { 2809d18e9008SMiklos Szeredi mnt_drop_write(nd->path.mnt); 281064894cf8SAl Viro got_write = false; 2811d18e9008SMiklos Szeredi } 2812d18e9008SMiklos Szeredi 2813fb1cc555SAl Viro error = -EEXIST; 2814f8310c59SAl Viro if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT)) 2815fb1cc555SAl Viro goto exit_dput; 2816fb1cc555SAl Viro 28179875cf80SDavid Howells error = follow_managed(path, nd->flags); 28189875cf80SDavid Howells if (error < 0) 2819fb1cc555SAl Viro goto exit_dput; 2820fb1cc555SAl Viro 2821a3fbbde7SAl Viro if (error) 2822a3fbbde7SAl Viro nd->flags |= LOOKUP_JUMPED; 2823a3fbbde7SAl Viro 2824decf3400SMiklos Szeredi BUG_ON(nd->flags & LOOKUP_RCU); 2825decf3400SMiklos Szeredi inode = path->dentry->d_inode; 28265f5daac1SMiklos Szeredi finish_lookup: 28275f5daac1SMiklos Szeredi /* we _can_ be in RCU mode here */ 2828fb1cc555SAl Viro error = -ENOENT; 282954c33e7fSMiklos Szeredi if (!inode) { 283054c33e7fSMiklos Szeredi path_to_nameidata(path, nd); 28312675a4ebSAl Viro goto out; 283254c33e7fSMiklos Szeredi } 28339e67f361SAl Viro 2834d45ea867SMiklos Szeredi if (should_follow_link(inode, !symlink_ok)) { 2835d45ea867SMiklos Szeredi if (nd->flags & LOOKUP_RCU) { 2836d45ea867SMiklos Szeredi if (unlikely(unlazy_walk(nd, path->dentry))) { 2837d45ea867SMiklos Szeredi error = -ECHILD; 28382675a4ebSAl Viro goto out; 2839d45ea867SMiklos Szeredi } 2840d45ea867SMiklos Szeredi } 2841d45ea867SMiklos Szeredi BUG_ON(inode != path->dentry->d_inode); 28422675a4ebSAl Viro return 1; 2843d45ea867SMiklos Szeredi } 2844fb1cc555SAl Viro 284516b1c1cdSMiklos Szeredi if ((nd->flags & LOOKUP_RCU) || nd->path.mnt != path->mnt) { 2846fb1cc555SAl Viro path_to_nameidata(path, nd); 284716b1c1cdSMiklos Szeredi } else { 284816b1c1cdSMiklos Szeredi save_parent.dentry = nd->path.dentry; 284916b1c1cdSMiklos Szeredi save_parent.mnt = mntget(path->mnt); 285016b1c1cdSMiklos Szeredi nd->path.dentry = path->dentry; 285116b1c1cdSMiklos Szeredi 285216b1c1cdSMiklos Szeredi } 2853decf3400SMiklos Szeredi nd->inode = inode; 2854a3fbbde7SAl Viro /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */ 2855a3fbbde7SAl Viro error = complete_walk(nd); 285616b1c1cdSMiklos Szeredi if (error) { 285716b1c1cdSMiklos Szeredi path_put(&save_parent); 28582675a4ebSAl Viro return error; 285916b1c1cdSMiklos Szeredi } 2860fb1cc555SAl Viro error = -EISDIR; 2861050ac841SMiklos Szeredi if ((open_flag & O_CREAT) && S_ISDIR(nd->inode->i_mode)) 28622675a4ebSAl Viro goto out; 2863af2f5542SMiklos Szeredi error = -ENOTDIR; 2864af2f5542SMiklos Szeredi if ((nd->flags & LOOKUP_DIRECTORY) && !nd->inode->i_op->lookup) 28652675a4ebSAl Viro goto out; 2866adb5c247SJeff Layton audit_inode(name, nd->path.dentry, 0); 2867e83db167SMiklos Szeredi finish_open: 28686c0d46c4SAl Viro if (!S_ISREG(nd->inode->i_mode)) 286977d660a8SMiklos Szeredi will_truncate = false; 28706c0d46c4SAl Viro 28710f9d1a10SAl Viro if (will_truncate) { 28720f9d1a10SAl Viro error = mnt_want_write(nd->path.mnt); 28730f9d1a10SAl Viro if (error) 28742675a4ebSAl Viro goto out; 287564894cf8SAl Viro got_write = true; 28760f9d1a10SAl Viro } 2877e83db167SMiklos Szeredi finish_open_created: 2878bcda7652SAl Viro error = may_open(&nd->path, acc_mode, open_flag); 2879ca344a89SAl Viro if (error) 28802675a4ebSAl Viro goto out; 288130d90494SAl Viro file->f_path.mnt = nd->path.mnt; 288230d90494SAl Viro error = finish_open(file, nd->path.dentry, NULL, opened); 288330d90494SAl Viro if (error) { 288430d90494SAl Viro if (error == -EOPENSTALE) 2885f60dc3dbSMiklos Szeredi goto stale_open; 2886015c3bbcSMiklos Szeredi goto out; 2887f60dc3dbSMiklos Szeredi } 2888a8277b9bSMiklos Szeredi opened: 28892675a4ebSAl Viro error = open_check_o_direct(file); 2890015c3bbcSMiklos Szeredi if (error) 2891015c3bbcSMiklos Szeredi goto exit_fput; 28922675a4ebSAl Viro error = ima_file_check(file, op->acc_mode); 2893aa4caadbSMiklos Szeredi if (error) 2894aa4caadbSMiklos Szeredi goto exit_fput; 2895aa4caadbSMiklos Szeredi 28960f9d1a10SAl Viro if (will_truncate) { 28972675a4ebSAl Viro error = handle_truncate(file); 2898aa4caadbSMiklos Szeredi if (error) 2899aa4caadbSMiklos Szeredi goto exit_fput; 29000f9d1a10SAl Viro } 2901ca344a89SAl Viro out: 290264894cf8SAl Viro if (got_write) 29030f9d1a10SAl Viro mnt_drop_write(nd->path.mnt); 290416b1c1cdSMiklos Szeredi path_put(&save_parent); 2905e276ae67SMiklos Szeredi terminate_walk(nd); 29062675a4ebSAl Viro return error; 2907fb1cc555SAl Viro 2908fb1cc555SAl Viro exit_dput: 2909fb1cc555SAl Viro path_put_conditional(path, nd); 2910ca344a89SAl Viro goto out; 2911015c3bbcSMiklos Szeredi exit_fput: 29122675a4ebSAl Viro fput(file); 29132675a4ebSAl Viro goto out; 2914015c3bbcSMiklos Szeredi 2915f60dc3dbSMiklos Szeredi stale_open: 2916f60dc3dbSMiklos Szeredi /* If no saved parent or already retried then can't retry */ 2917f60dc3dbSMiklos Szeredi if (!save_parent.dentry || retried) 2918f60dc3dbSMiklos Szeredi goto out; 2919f60dc3dbSMiklos Szeredi 2920f60dc3dbSMiklos Szeredi BUG_ON(save_parent.dentry != dir); 2921f60dc3dbSMiklos Szeredi path_put(&nd->path); 2922f60dc3dbSMiklos Szeredi nd->path = save_parent; 2923f60dc3dbSMiklos Szeredi nd->inode = dir->d_inode; 2924f60dc3dbSMiklos Szeredi save_parent.mnt = NULL; 2925f60dc3dbSMiklos Szeredi save_parent.dentry = NULL; 292664894cf8SAl Viro if (got_write) { 2927f60dc3dbSMiklos Szeredi mnt_drop_write(nd->path.mnt); 292864894cf8SAl Viro got_write = false; 2929f60dc3dbSMiklos Szeredi } 2930f60dc3dbSMiklos Szeredi retried = true; 2931f60dc3dbSMiklos Szeredi goto retry_lookup; 2932fb1cc555SAl Viro } 2933fb1cc555SAl Viro 2934669abf4eSJeff Layton static struct file *path_openat(int dfd, struct filename *pathname, 293573d049a4SAl Viro struct nameidata *nd, const struct open_flags *op, int flags) 29361da177e4SLinus Torvalds { 2937fe2d35ffSAl Viro struct file *base = NULL; 293830d90494SAl Viro struct file *file; 29399850c056SAl Viro struct path path; 294047237687SAl Viro int opened = 0; 294113aab428SAl Viro int error; 294231e6b01fSNick Piggin 294330d90494SAl Viro file = get_empty_filp(); 294430d90494SAl Viro if (!file) 294531e6b01fSNick Piggin return ERR_PTR(-ENFILE); 294631e6b01fSNick Piggin 294730d90494SAl Viro file->f_flags = op->open_flag; 294831e6b01fSNick Piggin 2949669abf4eSJeff Layton error = path_init(dfd, pathname->name, flags | LOOKUP_PARENT, nd, &base); 295031e6b01fSNick Piggin if (unlikely(error)) 29512675a4ebSAl Viro goto out; 295231e6b01fSNick Piggin 2953fe2d35ffSAl Viro current->total_link_count = 0; 2954669abf4eSJeff Layton error = link_path_walk(pathname->name, nd); 295531e6b01fSNick Piggin if (unlikely(error)) 29562675a4ebSAl Viro goto out; 29571da177e4SLinus Torvalds 29582675a4ebSAl Viro error = do_last(nd, &path, file, op, &opened, pathname); 29592675a4ebSAl Viro while (unlikely(error > 0)) { /* trailing symlink */ 29607b9337aaSNick Piggin struct path link = path; 2961def4af30SAl Viro void *cookie; 2962574197e0SAl Viro if (!(nd->flags & LOOKUP_FOLLOW)) { 296373d049a4SAl Viro path_put_conditional(&path, nd); 296473d049a4SAl Viro path_put(&nd->path); 29652675a4ebSAl Viro error = -ELOOP; 296640b39136SAl Viro break; 296740b39136SAl Viro } 2968800179c9SKees Cook error = may_follow_link(&link, nd); 2969800179c9SKees Cook if (unlikely(error)) 2970800179c9SKees Cook break; 297173d049a4SAl Viro nd->flags |= LOOKUP_PARENT; 297273d049a4SAl Viro nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL); 2973574197e0SAl Viro error = follow_link(&link, nd, &cookie); 2974c3e380b0SAl Viro if (unlikely(error)) 29752675a4ebSAl Viro break; 29762675a4ebSAl Viro error = do_last(nd, &path, file, op, &opened, pathname); 2977574197e0SAl Viro put_link(nd, &link, cookie); 2978806b681cSAl Viro } 297910fa8e62SAl Viro out: 298073d049a4SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) 298173d049a4SAl Viro path_put(&nd->root); 2982fe2d35ffSAl Viro if (base) 2983fe2d35ffSAl Viro fput(base); 29842675a4ebSAl Viro if (!(opened & FILE_OPENED)) { 29852675a4ebSAl Viro BUG_ON(!error); 298630d90494SAl Viro put_filp(file); 2987015c3bbcSMiklos Szeredi } 29882675a4ebSAl Viro if (unlikely(error)) { 29892675a4ebSAl Viro if (error == -EOPENSTALE) { 29902675a4ebSAl Viro if (flags & LOOKUP_RCU) 29912675a4ebSAl Viro error = -ECHILD; 29922675a4ebSAl Viro else 29932675a4ebSAl Viro error = -ESTALE; 29942675a4ebSAl Viro } 29952675a4ebSAl Viro file = ERR_PTR(error); 29962675a4ebSAl Viro } 29972675a4ebSAl Viro return file; 2998de459215SKirill Korotaev } 29991da177e4SLinus Torvalds 3000669abf4eSJeff Layton struct file *do_filp_open(int dfd, struct filename *pathname, 300113aab428SAl Viro const struct open_flags *op, int flags) 300213aab428SAl Viro { 300373d049a4SAl Viro struct nameidata nd; 300413aab428SAl Viro struct file *filp; 300513aab428SAl Viro 300673d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU); 300713aab428SAl Viro if (unlikely(filp == ERR_PTR(-ECHILD))) 300873d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags); 300913aab428SAl Viro if (unlikely(filp == ERR_PTR(-ESTALE))) 301073d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL); 301113aab428SAl Viro return filp; 301213aab428SAl Viro } 301313aab428SAl Viro 301473d049a4SAl Viro struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt, 301573d049a4SAl Viro const char *name, const struct open_flags *op, int flags) 301673d049a4SAl Viro { 301773d049a4SAl Viro struct nameidata nd; 301873d049a4SAl Viro struct file *file; 3019669abf4eSJeff Layton struct filename filename = { .name = name }; 302073d049a4SAl Viro 302173d049a4SAl Viro nd.root.mnt = mnt; 302273d049a4SAl Viro nd.root.dentry = dentry; 302373d049a4SAl Viro 302473d049a4SAl Viro flags |= LOOKUP_ROOT; 302573d049a4SAl Viro 3026bcda7652SAl Viro if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN) 302773d049a4SAl Viro return ERR_PTR(-ELOOP); 302873d049a4SAl Viro 3029669abf4eSJeff Layton file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_RCU); 303073d049a4SAl Viro if (unlikely(file == ERR_PTR(-ECHILD))) 3031669abf4eSJeff Layton file = path_openat(-1, &filename, &nd, op, flags); 303273d049a4SAl Viro if (unlikely(file == ERR_PTR(-ESTALE))) 3033669abf4eSJeff Layton file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_REVAL); 303473d049a4SAl Viro return file; 303573d049a4SAl Viro } 303673d049a4SAl Viro 30371ac12b4bSJeff Layton struct dentry *kern_path_create(int dfd, const char *pathname, 30381ac12b4bSJeff Layton struct path *path, unsigned int lookup_flags) 30391da177e4SLinus Torvalds { 3040c663e5d8SChristoph Hellwig struct dentry *dentry = ERR_PTR(-EEXIST); 3041ed75e95dSAl Viro struct nameidata nd; 3042c30dabfeSJan Kara int err2; 30431ac12b4bSJeff Layton int error; 30441ac12b4bSJeff Layton bool is_dir = (lookup_flags & LOOKUP_DIRECTORY); 30451ac12b4bSJeff Layton 30461ac12b4bSJeff Layton /* 30471ac12b4bSJeff Layton * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any 30481ac12b4bSJeff Layton * other flags passed in are ignored! 30491ac12b4bSJeff Layton */ 30501ac12b4bSJeff Layton lookup_flags &= LOOKUP_REVAL; 30511ac12b4bSJeff Layton 30521ac12b4bSJeff Layton error = do_path_lookup(dfd, pathname, LOOKUP_PARENT|lookup_flags, &nd); 3053ed75e95dSAl Viro if (error) 3054ed75e95dSAl Viro return ERR_PTR(error); 30551da177e4SLinus Torvalds 3056c663e5d8SChristoph Hellwig /* 3057c663e5d8SChristoph Hellwig * Yucky last component or no last component at all? 3058c663e5d8SChristoph Hellwig * (foo/., foo/.., /////) 3059c663e5d8SChristoph Hellwig */ 3060ed75e95dSAl Viro if (nd.last_type != LAST_NORM) 3061ed75e95dSAl Viro goto out; 3062ed75e95dSAl Viro nd.flags &= ~LOOKUP_PARENT; 3063ed75e95dSAl Viro nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL; 3064c663e5d8SChristoph Hellwig 3065c30dabfeSJan Kara /* don't fail immediately if it's r/o, at least try to report other errors */ 3066c30dabfeSJan Kara err2 = mnt_want_write(nd.path.mnt); 3067c663e5d8SChristoph Hellwig /* 3068c663e5d8SChristoph Hellwig * Do the final lookup. 3069c663e5d8SChristoph Hellwig */ 3070ed75e95dSAl Viro mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 3071ed75e95dSAl Viro dentry = lookup_hash(&nd); 30721da177e4SLinus Torvalds if (IS_ERR(dentry)) 3073a8104a9fSAl Viro goto unlock; 3074c663e5d8SChristoph Hellwig 3075a8104a9fSAl Viro error = -EEXIST; 3076e9baf6e5SAl Viro if (dentry->d_inode) 3077a8104a9fSAl Viro goto fail; 3078c663e5d8SChristoph Hellwig /* 3079c663e5d8SChristoph Hellwig * Special case - lookup gave negative, but... we had foo/bar/ 3080c663e5d8SChristoph Hellwig * From the vfs_mknod() POV we just have a negative dentry - 3081c663e5d8SChristoph Hellwig * all is fine. Let's be bastards - you had / on the end, you've 3082c663e5d8SChristoph Hellwig * been asking for (non-existent) directory. -ENOENT for you. 3083c663e5d8SChristoph Hellwig */ 3084ed75e95dSAl Viro if (unlikely(!is_dir && nd.last.name[nd.last.len])) { 3085a8104a9fSAl Viro error = -ENOENT; 3086ed75e95dSAl Viro goto fail; 3087e9baf6e5SAl Viro } 3088c30dabfeSJan Kara if (unlikely(err2)) { 3089c30dabfeSJan Kara error = err2; 3090a8104a9fSAl Viro goto fail; 3091c30dabfeSJan Kara } 3092ed75e95dSAl Viro *path = nd.path; 3093e9baf6e5SAl Viro return dentry; 30941da177e4SLinus Torvalds fail: 3095a8104a9fSAl Viro dput(dentry); 3096a8104a9fSAl Viro dentry = ERR_PTR(error); 3097a8104a9fSAl Viro unlock: 3098dae6ad8fSAl Viro mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 3099c30dabfeSJan Kara if (!err2) 3100c30dabfeSJan Kara mnt_drop_write(nd.path.mnt); 3101ed75e95dSAl Viro out: 3102dae6ad8fSAl Viro path_put(&nd.path); 3103ed75e95dSAl Viro return dentry; 3104dae6ad8fSAl Viro } 3105dae6ad8fSAl Viro EXPORT_SYMBOL(kern_path_create); 3106dae6ad8fSAl Viro 3107921a1650SAl Viro void done_path_create(struct path *path, struct dentry *dentry) 3108921a1650SAl Viro { 3109921a1650SAl Viro dput(dentry); 3110921a1650SAl Viro mutex_unlock(&path->dentry->d_inode->i_mutex); 3111a8104a9fSAl Viro mnt_drop_write(path->mnt); 3112921a1650SAl Viro path_put(path); 3113921a1650SAl Viro } 3114921a1650SAl Viro EXPORT_SYMBOL(done_path_create); 3115921a1650SAl Viro 31161ac12b4bSJeff Layton struct dentry *user_path_create(int dfd, const char __user *pathname, 31171ac12b4bSJeff Layton struct path *path, unsigned int lookup_flags) 3118dae6ad8fSAl Viro { 311991a27b2aSJeff Layton struct filename *tmp = getname(pathname); 3120dae6ad8fSAl Viro struct dentry *res; 3121dae6ad8fSAl Viro if (IS_ERR(tmp)) 3122dae6ad8fSAl Viro return ERR_CAST(tmp); 31231ac12b4bSJeff Layton res = kern_path_create(dfd, tmp->name, path, lookup_flags); 3124dae6ad8fSAl Viro putname(tmp); 3125dae6ad8fSAl Viro return res; 3126dae6ad8fSAl Viro } 3127dae6ad8fSAl Viro EXPORT_SYMBOL(user_path_create); 3128dae6ad8fSAl Viro 31291a67aafbSAl Viro int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev) 31301da177e4SLinus Torvalds { 3131a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 31321da177e4SLinus Torvalds 31331da177e4SLinus Torvalds if (error) 31341da177e4SLinus Torvalds return error; 31351da177e4SLinus Torvalds 3136975d6b39SEric W. Biederman if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD)) 31371da177e4SLinus Torvalds return -EPERM; 31381da177e4SLinus Torvalds 3139acfa4380SAl Viro if (!dir->i_op->mknod) 31401da177e4SLinus Torvalds return -EPERM; 31411da177e4SLinus Torvalds 314208ce5f16SSerge E. Hallyn error = devcgroup_inode_mknod(mode, dev); 314308ce5f16SSerge E. Hallyn if (error) 314408ce5f16SSerge E. Hallyn return error; 314508ce5f16SSerge E. Hallyn 31461da177e4SLinus Torvalds error = security_inode_mknod(dir, dentry, mode, dev); 31471da177e4SLinus Torvalds if (error) 31481da177e4SLinus Torvalds return error; 31491da177e4SLinus Torvalds 31501da177e4SLinus Torvalds error = dir->i_op->mknod(dir, dentry, mode, dev); 3151a74574aaSStephen Smalley if (!error) 3152f38aa942SAmy Griffis fsnotify_create(dir, dentry); 31531da177e4SLinus Torvalds return error; 31541da177e4SLinus Torvalds } 31551da177e4SLinus Torvalds 3156f69aac00SAl Viro static int may_mknod(umode_t mode) 3157463c3197SDave Hansen { 3158463c3197SDave Hansen switch (mode & S_IFMT) { 3159463c3197SDave Hansen case S_IFREG: 3160463c3197SDave Hansen case S_IFCHR: 3161463c3197SDave Hansen case S_IFBLK: 3162463c3197SDave Hansen case S_IFIFO: 3163463c3197SDave Hansen case S_IFSOCK: 3164463c3197SDave Hansen case 0: /* zero mode translates to S_IFREG */ 3165463c3197SDave Hansen return 0; 3166463c3197SDave Hansen case S_IFDIR: 3167463c3197SDave Hansen return -EPERM; 3168463c3197SDave Hansen default: 3169463c3197SDave Hansen return -EINVAL; 3170463c3197SDave Hansen } 3171463c3197SDave Hansen } 3172463c3197SDave Hansen 31738208a22bSAl Viro SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode, 31742e4d0924SHeiko Carstens unsigned, dev) 31751da177e4SLinus Torvalds { 31761da177e4SLinus Torvalds struct dentry *dentry; 3177dae6ad8fSAl Viro struct path path; 3178dae6ad8fSAl Viro int error; 3179972567f1SJeff Layton unsigned int lookup_flags = 0; 31801da177e4SLinus Torvalds 31818e4bfca1SAl Viro error = may_mknod(mode); 31828e4bfca1SAl Viro if (error) 31838e4bfca1SAl Viro return error; 3184972567f1SJeff Layton retry: 3185972567f1SJeff Layton dentry = user_path_create(dfd, filename, &path, lookup_flags); 3186dae6ad8fSAl Viro if (IS_ERR(dentry)) 3187dae6ad8fSAl Viro return PTR_ERR(dentry); 31882ad94ae6SAl Viro 3189dae6ad8fSAl Viro if (!IS_POSIXACL(path.dentry->d_inode)) 3190ce3b0f8dSAl Viro mode &= ~current_umask(); 3191dae6ad8fSAl Viro error = security_path_mknod(&path, dentry, mode, dev); 3192be6d3e56SKentaro Takeda if (error) 3193a8104a9fSAl Viro goto out; 31941da177e4SLinus Torvalds switch (mode & S_IFMT) { 31951da177e4SLinus Torvalds case 0: case S_IFREG: 3196312b63fbSAl Viro error = vfs_create(path.dentry->d_inode,dentry,mode,true); 31971da177e4SLinus Torvalds break; 31981da177e4SLinus Torvalds case S_IFCHR: case S_IFBLK: 3199dae6ad8fSAl Viro error = vfs_mknod(path.dentry->d_inode,dentry,mode, 32001da177e4SLinus Torvalds new_decode_dev(dev)); 32011da177e4SLinus Torvalds break; 32021da177e4SLinus Torvalds case S_IFIFO: case S_IFSOCK: 3203dae6ad8fSAl Viro error = vfs_mknod(path.dentry->d_inode,dentry,mode,0); 32041da177e4SLinus Torvalds break; 32051da177e4SLinus Torvalds } 3206a8104a9fSAl Viro out: 3207921a1650SAl Viro done_path_create(&path, dentry); 3208972567f1SJeff Layton if (retry_estale(error, lookup_flags)) { 3209972567f1SJeff Layton lookup_flags |= LOOKUP_REVAL; 3210972567f1SJeff Layton goto retry; 3211972567f1SJeff Layton } 32121da177e4SLinus Torvalds return error; 32131da177e4SLinus Torvalds } 32141da177e4SLinus Torvalds 32158208a22bSAl Viro SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev) 32165590ff0dSUlrich Drepper { 32175590ff0dSUlrich Drepper return sys_mknodat(AT_FDCWD, filename, mode, dev); 32185590ff0dSUlrich Drepper } 32195590ff0dSUlrich Drepper 322018bb1db3SAl Viro int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) 32211da177e4SLinus Torvalds { 3222a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 32238de52778SAl Viro unsigned max_links = dir->i_sb->s_max_links; 32241da177e4SLinus Torvalds 32251da177e4SLinus Torvalds if (error) 32261da177e4SLinus Torvalds return error; 32271da177e4SLinus Torvalds 3228acfa4380SAl Viro if (!dir->i_op->mkdir) 32291da177e4SLinus Torvalds return -EPERM; 32301da177e4SLinus Torvalds 32311da177e4SLinus Torvalds mode &= (S_IRWXUGO|S_ISVTX); 32321da177e4SLinus Torvalds error = security_inode_mkdir(dir, dentry, mode); 32331da177e4SLinus Torvalds if (error) 32341da177e4SLinus Torvalds return error; 32351da177e4SLinus Torvalds 32368de52778SAl Viro if (max_links && dir->i_nlink >= max_links) 32378de52778SAl Viro return -EMLINK; 32388de52778SAl Viro 32391da177e4SLinus Torvalds error = dir->i_op->mkdir(dir, dentry, mode); 3240a74574aaSStephen Smalley if (!error) 3241f38aa942SAmy Griffis fsnotify_mkdir(dir, dentry); 32421da177e4SLinus Torvalds return error; 32431da177e4SLinus Torvalds } 32441da177e4SLinus Torvalds 3245a218d0fdSAl Viro SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode) 32461da177e4SLinus Torvalds { 32476902d925SDave Hansen struct dentry *dentry; 3248dae6ad8fSAl Viro struct path path; 3249dae6ad8fSAl Viro int error; 3250b76d8b82SJeff Layton unsigned int lookup_flags = LOOKUP_DIRECTORY; 32511da177e4SLinus Torvalds 3252b76d8b82SJeff Layton retry: 3253b76d8b82SJeff Layton dentry = user_path_create(dfd, pathname, &path, lookup_flags); 32546902d925SDave Hansen if (IS_ERR(dentry)) 3255dae6ad8fSAl Viro return PTR_ERR(dentry); 32566902d925SDave Hansen 3257dae6ad8fSAl Viro if (!IS_POSIXACL(path.dentry->d_inode)) 3258ce3b0f8dSAl Viro mode &= ~current_umask(); 3259dae6ad8fSAl Viro error = security_path_mkdir(&path, dentry, mode); 3260a8104a9fSAl Viro if (!error) 3261dae6ad8fSAl Viro error = vfs_mkdir(path.dentry->d_inode, dentry, mode); 3262921a1650SAl Viro done_path_create(&path, dentry); 3263b76d8b82SJeff Layton if (retry_estale(error, lookup_flags)) { 3264b76d8b82SJeff Layton lookup_flags |= LOOKUP_REVAL; 3265b76d8b82SJeff Layton goto retry; 3266b76d8b82SJeff Layton } 32671da177e4SLinus Torvalds return error; 32681da177e4SLinus Torvalds } 32691da177e4SLinus Torvalds 3270a218d0fdSAl Viro SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode) 32715590ff0dSUlrich Drepper { 32725590ff0dSUlrich Drepper return sys_mkdirat(AT_FDCWD, pathname, mode); 32735590ff0dSUlrich Drepper } 32745590ff0dSUlrich Drepper 32751da177e4SLinus Torvalds /* 3276a71905f0SSage Weil * The dentry_unhash() helper will try to drop the dentry early: we 3277c0d02594SJ. Bruce Fields * should have a usage count of 1 if we're the only user of this 3278a71905f0SSage Weil * dentry, and if that is true (possibly after pruning the dcache), 3279a71905f0SSage Weil * then we drop the dentry now. 32801da177e4SLinus Torvalds * 32811da177e4SLinus Torvalds * A low-level filesystem can, if it choses, legally 32821da177e4SLinus Torvalds * do a 32831da177e4SLinus Torvalds * 32841da177e4SLinus Torvalds * if (!d_unhashed(dentry)) 32851da177e4SLinus Torvalds * return -EBUSY; 32861da177e4SLinus Torvalds * 32871da177e4SLinus Torvalds * if it cannot handle the case of removing a directory 32881da177e4SLinus Torvalds * that is still in use by something else.. 32891da177e4SLinus Torvalds */ 32901da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry) 32911da177e4SLinus Torvalds { 32921da177e4SLinus Torvalds shrink_dcache_parent(dentry); 32931da177e4SLinus Torvalds spin_lock(&dentry->d_lock); 329464252c75SSage Weil if (dentry->d_count == 1) 32951da177e4SLinus Torvalds __d_drop(dentry); 32961da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 32971da177e4SLinus Torvalds } 32981da177e4SLinus Torvalds 32991da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry) 33001da177e4SLinus Torvalds { 33011da177e4SLinus Torvalds int error = may_delete(dir, dentry, 1); 33021da177e4SLinus Torvalds 33031da177e4SLinus Torvalds if (error) 33041da177e4SLinus Torvalds return error; 33051da177e4SLinus Torvalds 3306acfa4380SAl Viro if (!dir->i_op->rmdir) 33071da177e4SLinus Torvalds return -EPERM; 33081da177e4SLinus Torvalds 33091d2ef590SAl Viro dget(dentry); 33101b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 3311912dbc15SSage Weil 33121da177e4SLinus Torvalds error = -EBUSY; 3313912dbc15SSage Weil if (d_mountpoint(dentry)) 3314912dbc15SSage Weil goto out; 3315912dbc15SSage Weil 33161da177e4SLinus Torvalds error = security_inode_rmdir(dir, dentry); 3317912dbc15SSage Weil if (error) 3318912dbc15SSage Weil goto out; 3319912dbc15SSage Weil 33203cebde24SSage Weil shrink_dcache_parent(dentry); 33211da177e4SLinus Torvalds error = dir->i_op->rmdir(dir, dentry); 3322912dbc15SSage Weil if (error) 3323912dbc15SSage Weil goto out; 3324912dbc15SSage Weil 33251da177e4SLinus Torvalds dentry->d_inode->i_flags |= S_DEAD; 3326d83c49f3SAl Viro dont_mount(dentry); 33271da177e4SLinus Torvalds 3328912dbc15SSage Weil out: 3329912dbc15SSage Weil mutex_unlock(&dentry->d_inode->i_mutex); 33301d2ef590SAl Viro dput(dentry); 3331912dbc15SSage Weil if (!error) 3332912dbc15SSage Weil d_delete(dentry); 33331da177e4SLinus Torvalds return error; 33341da177e4SLinus Torvalds } 33351da177e4SLinus Torvalds 33365590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname) 33371da177e4SLinus Torvalds { 33381da177e4SLinus Torvalds int error = 0; 333991a27b2aSJeff Layton struct filename *name; 33401da177e4SLinus Torvalds struct dentry *dentry; 33411da177e4SLinus Torvalds struct nameidata nd; 33421da177e4SLinus Torvalds 33439e790bd6SJeff Layton name = user_path_parent(dfd, pathname, &nd, 0); 334491a27b2aSJeff Layton if (IS_ERR(name)) 334591a27b2aSJeff Layton return PTR_ERR(name); 33461da177e4SLinus Torvalds 33471da177e4SLinus Torvalds switch(nd.last_type) { 33481da177e4SLinus Torvalds case LAST_DOTDOT: 33491da177e4SLinus Torvalds error = -ENOTEMPTY; 33501da177e4SLinus Torvalds goto exit1; 33511da177e4SLinus Torvalds case LAST_DOT: 33521da177e4SLinus Torvalds error = -EINVAL; 33531da177e4SLinus Torvalds goto exit1; 33541da177e4SLinus Torvalds case LAST_ROOT: 33551da177e4SLinus Torvalds error = -EBUSY; 33561da177e4SLinus Torvalds goto exit1; 33571da177e4SLinus Torvalds } 33580612d9fbSOGAWA Hirofumi 33590612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 3360c30dabfeSJan Kara error = mnt_want_write(nd.path.mnt); 3361c30dabfeSJan Kara if (error) 3362c30dabfeSJan Kara goto exit1; 33630612d9fbSOGAWA Hirofumi 33644ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 336549705b77SChristoph Hellwig dentry = lookup_hash(&nd); 33661da177e4SLinus Torvalds error = PTR_ERR(dentry); 33676902d925SDave Hansen if (IS_ERR(dentry)) 33686902d925SDave Hansen goto exit2; 3369e6bc45d6STheodore Ts'o if (!dentry->d_inode) { 3370e6bc45d6STheodore Ts'o error = -ENOENT; 3371e6bc45d6STheodore Ts'o goto exit3; 3372e6bc45d6STheodore Ts'o } 3373be6d3e56SKentaro Takeda error = security_path_rmdir(&nd.path, dentry); 3374be6d3e56SKentaro Takeda if (error) 3375c30dabfeSJan Kara goto exit3; 33764ac91378SJan Blunck error = vfs_rmdir(nd.path.dentry->d_inode, dentry); 33770622753bSDave Hansen exit3: 33781da177e4SLinus Torvalds dput(dentry); 33796902d925SDave Hansen exit2: 33804ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 3381c30dabfeSJan Kara mnt_drop_write(nd.path.mnt); 33821da177e4SLinus Torvalds exit1: 33831d957f9bSJan Blunck path_put(&nd.path); 33841da177e4SLinus Torvalds putname(name); 33851da177e4SLinus Torvalds return error; 33861da177e4SLinus Torvalds } 33871da177e4SLinus Torvalds 33883cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname) 33895590ff0dSUlrich Drepper { 33905590ff0dSUlrich Drepper return do_rmdir(AT_FDCWD, pathname); 33915590ff0dSUlrich Drepper } 33925590ff0dSUlrich Drepper 33931da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry) 33941da177e4SLinus Torvalds { 33951da177e4SLinus Torvalds int error = may_delete(dir, dentry, 0); 33961da177e4SLinus Torvalds 33971da177e4SLinus Torvalds if (error) 33981da177e4SLinus Torvalds return error; 33991da177e4SLinus Torvalds 3400acfa4380SAl Viro if (!dir->i_op->unlink) 34011da177e4SLinus Torvalds return -EPERM; 34021da177e4SLinus Torvalds 34031b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 34041da177e4SLinus Torvalds if (d_mountpoint(dentry)) 34051da177e4SLinus Torvalds error = -EBUSY; 34061da177e4SLinus Torvalds else { 34071da177e4SLinus Torvalds error = security_inode_unlink(dir, dentry); 3408bec1052eSAl Viro if (!error) { 34091da177e4SLinus Torvalds error = dir->i_op->unlink(dir, dentry); 3410bec1052eSAl Viro if (!error) 3411d83c49f3SAl Viro dont_mount(dentry); 3412bec1052eSAl Viro } 34131da177e4SLinus Torvalds } 34141b1dcc1bSJes Sorensen mutex_unlock(&dentry->d_inode->i_mutex); 34151da177e4SLinus Torvalds 34161da177e4SLinus Torvalds /* We don't d_delete() NFS sillyrenamed files--they still exist. */ 34171da177e4SLinus Torvalds if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) { 3418ece95912SJan Kara fsnotify_link_count(dentry->d_inode); 34191da177e4SLinus Torvalds d_delete(dentry); 34201da177e4SLinus Torvalds } 34210eeca283SRobert Love 34221da177e4SLinus Torvalds return error; 34231da177e4SLinus Torvalds } 34241da177e4SLinus Torvalds 34251da177e4SLinus Torvalds /* 34261da177e4SLinus Torvalds * Make sure that the actual truncation of the file will occur outside its 34271b1dcc1bSJes Sorensen * directory's i_mutex. Truncate can take a long time if there is a lot of 34281da177e4SLinus Torvalds * writeout happening, and we don't want to prevent access to the directory 34291da177e4SLinus Torvalds * while waiting on the I/O. 34301da177e4SLinus Torvalds */ 34315590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname) 34321da177e4SLinus Torvalds { 34332ad94ae6SAl Viro int error; 343491a27b2aSJeff Layton struct filename *name; 34351da177e4SLinus Torvalds struct dentry *dentry; 34361da177e4SLinus Torvalds struct nameidata nd; 34371da177e4SLinus Torvalds struct inode *inode = NULL; 34381da177e4SLinus Torvalds 34399e790bd6SJeff Layton name = user_path_parent(dfd, pathname, &nd, 0); 344091a27b2aSJeff Layton if (IS_ERR(name)) 344191a27b2aSJeff Layton return PTR_ERR(name); 34422ad94ae6SAl Viro 34431da177e4SLinus Torvalds error = -EISDIR; 34441da177e4SLinus Torvalds if (nd.last_type != LAST_NORM) 34451da177e4SLinus Torvalds goto exit1; 34460612d9fbSOGAWA Hirofumi 34470612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 3448c30dabfeSJan Kara error = mnt_want_write(nd.path.mnt); 3449c30dabfeSJan Kara if (error) 3450c30dabfeSJan Kara goto exit1; 34510612d9fbSOGAWA Hirofumi 34524ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 345349705b77SChristoph Hellwig dentry = lookup_hash(&nd); 34541da177e4SLinus Torvalds error = PTR_ERR(dentry); 34551da177e4SLinus Torvalds if (!IS_ERR(dentry)) { 34561da177e4SLinus Torvalds /* Why not before? Because we want correct error value */ 345750338b88STörök Edwin if (nd.last.name[nd.last.len]) 345850338b88STörök Edwin goto slashes; 34591da177e4SLinus Torvalds inode = dentry->d_inode; 346050338b88STörök Edwin if (!inode) 3461e6bc45d6STheodore Ts'o goto slashes; 34627de9c6eeSAl Viro ihold(inode); 3463be6d3e56SKentaro Takeda error = security_path_unlink(&nd.path, dentry); 3464be6d3e56SKentaro Takeda if (error) 3465c30dabfeSJan Kara goto exit2; 34664ac91378SJan Blunck error = vfs_unlink(nd.path.dentry->d_inode, dentry); 34671da177e4SLinus Torvalds exit2: 34681da177e4SLinus Torvalds dput(dentry); 34691da177e4SLinus Torvalds } 34704ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 34711da177e4SLinus Torvalds if (inode) 34721da177e4SLinus Torvalds iput(inode); /* truncate the inode here */ 3473c30dabfeSJan Kara mnt_drop_write(nd.path.mnt); 34741da177e4SLinus Torvalds exit1: 34751d957f9bSJan Blunck path_put(&nd.path); 34761da177e4SLinus Torvalds putname(name); 34771da177e4SLinus Torvalds return error; 34781da177e4SLinus Torvalds 34791da177e4SLinus Torvalds slashes: 34801da177e4SLinus Torvalds error = !dentry->d_inode ? -ENOENT : 34811da177e4SLinus Torvalds S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR; 34821da177e4SLinus Torvalds goto exit2; 34831da177e4SLinus Torvalds } 34841da177e4SLinus Torvalds 34852e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag) 34865590ff0dSUlrich Drepper { 34875590ff0dSUlrich Drepper if ((flag & ~AT_REMOVEDIR) != 0) 34885590ff0dSUlrich Drepper return -EINVAL; 34895590ff0dSUlrich Drepper 34905590ff0dSUlrich Drepper if (flag & AT_REMOVEDIR) 34915590ff0dSUlrich Drepper return do_rmdir(dfd, pathname); 34925590ff0dSUlrich Drepper 34935590ff0dSUlrich Drepper return do_unlinkat(dfd, pathname); 34945590ff0dSUlrich Drepper } 34955590ff0dSUlrich Drepper 34963480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname) 34975590ff0dSUlrich Drepper { 34985590ff0dSUlrich Drepper return do_unlinkat(AT_FDCWD, pathname); 34995590ff0dSUlrich Drepper } 35005590ff0dSUlrich Drepper 3501db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname) 35021da177e4SLinus Torvalds { 3503a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 35041da177e4SLinus Torvalds 35051da177e4SLinus Torvalds if (error) 35061da177e4SLinus Torvalds return error; 35071da177e4SLinus Torvalds 3508acfa4380SAl Viro if (!dir->i_op->symlink) 35091da177e4SLinus Torvalds return -EPERM; 35101da177e4SLinus Torvalds 35111da177e4SLinus Torvalds error = security_inode_symlink(dir, dentry, oldname); 35121da177e4SLinus Torvalds if (error) 35131da177e4SLinus Torvalds return error; 35141da177e4SLinus Torvalds 35151da177e4SLinus Torvalds error = dir->i_op->symlink(dir, dentry, oldname); 3516a74574aaSStephen Smalley if (!error) 3517f38aa942SAmy Griffis fsnotify_create(dir, dentry); 35181da177e4SLinus Torvalds return error; 35191da177e4SLinus Torvalds } 35201da177e4SLinus Torvalds 35212e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname, 35222e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 35231da177e4SLinus Torvalds { 35242ad94ae6SAl Viro int error; 352591a27b2aSJeff Layton struct filename *from; 35266902d925SDave Hansen struct dentry *dentry; 3527dae6ad8fSAl Viro struct path path; 3528f46d3567SJeff Layton unsigned int lookup_flags = 0; 35291da177e4SLinus Torvalds 35301da177e4SLinus Torvalds from = getname(oldname); 35311da177e4SLinus Torvalds if (IS_ERR(from)) 35321da177e4SLinus Torvalds return PTR_ERR(from); 3533f46d3567SJeff Layton retry: 3534f46d3567SJeff Layton dentry = user_path_create(newdfd, newname, &path, lookup_flags); 35351da177e4SLinus Torvalds error = PTR_ERR(dentry); 35366902d925SDave Hansen if (IS_ERR(dentry)) 3537dae6ad8fSAl Viro goto out_putname; 35386902d925SDave Hansen 353991a27b2aSJeff Layton error = security_path_symlink(&path, dentry, from->name); 3540a8104a9fSAl Viro if (!error) 354191a27b2aSJeff Layton error = vfs_symlink(path.dentry->d_inode, dentry, from->name); 3542921a1650SAl Viro done_path_create(&path, dentry); 3543f46d3567SJeff Layton if (retry_estale(error, lookup_flags)) { 3544f46d3567SJeff Layton lookup_flags |= LOOKUP_REVAL; 3545f46d3567SJeff Layton goto retry; 3546f46d3567SJeff Layton } 35476902d925SDave Hansen out_putname: 35481da177e4SLinus Torvalds putname(from); 35491da177e4SLinus Torvalds return error; 35501da177e4SLinus Torvalds } 35511da177e4SLinus Torvalds 35523480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname) 35535590ff0dSUlrich Drepper { 35545590ff0dSUlrich Drepper return sys_symlinkat(oldname, AT_FDCWD, newname); 35555590ff0dSUlrich Drepper } 35565590ff0dSUlrich Drepper 35571da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) 35581da177e4SLinus Torvalds { 35591da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 35608de52778SAl Viro unsigned max_links = dir->i_sb->s_max_links; 35611da177e4SLinus Torvalds int error; 35621da177e4SLinus Torvalds 35631da177e4SLinus Torvalds if (!inode) 35641da177e4SLinus Torvalds return -ENOENT; 35651da177e4SLinus Torvalds 3566a95164d9SMiklos Szeredi error = may_create(dir, new_dentry); 35671da177e4SLinus Torvalds if (error) 35681da177e4SLinus Torvalds return error; 35691da177e4SLinus Torvalds 35701da177e4SLinus Torvalds if (dir->i_sb != inode->i_sb) 35711da177e4SLinus Torvalds return -EXDEV; 35721da177e4SLinus Torvalds 35731da177e4SLinus Torvalds /* 35741da177e4SLinus Torvalds * A link to an append-only or immutable file cannot be created. 35751da177e4SLinus Torvalds */ 35761da177e4SLinus Torvalds if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) 35771da177e4SLinus Torvalds return -EPERM; 3578acfa4380SAl Viro if (!dir->i_op->link) 35791da177e4SLinus Torvalds return -EPERM; 35807e79eedbSTetsuo Handa if (S_ISDIR(inode->i_mode)) 35811da177e4SLinus Torvalds return -EPERM; 35821da177e4SLinus Torvalds 35831da177e4SLinus Torvalds error = security_inode_link(old_dentry, dir, new_dentry); 35841da177e4SLinus Torvalds if (error) 35851da177e4SLinus Torvalds return error; 35861da177e4SLinus Torvalds 35877e79eedbSTetsuo Handa mutex_lock(&inode->i_mutex); 3588aae8a97dSAneesh Kumar K.V /* Make sure we don't allow creating hardlink to an unlinked file */ 3589aae8a97dSAneesh Kumar K.V if (inode->i_nlink == 0) 3590aae8a97dSAneesh Kumar K.V error = -ENOENT; 35918de52778SAl Viro else if (max_links && inode->i_nlink >= max_links) 35928de52778SAl Viro error = -EMLINK; 3593aae8a97dSAneesh Kumar K.V else 35941da177e4SLinus Torvalds error = dir->i_op->link(old_dentry, dir, new_dentry); 35957e79eedbSTetsuo Handa mutex_unlock(&inode->i_mutex); 3596e31e14ecSStephen Smalley if (!error) 35977e79eedbSTetsuo Handa fsnotify_link(dir, inode, new_dentry); 35981da177e4SLinus Torvalds return error; 35991da177e4SLinus Torvalds } 36001da177e4SLinus Torvalds 36011da177e4SLinus Torvalds /* 36021da177e4SLinus Torvalds * Hardlinks are often used in delicate situations. We avoid 36031da177e4SLinus Torvalds * security-related surprises by not following symlinks on the 36041da177e4SLinus Torvalds * newname. --KAB 36051da177e4SLinus Torvalds * 36061da177e4SLinus Torvalds * We don't follow them on the oldname either to be compatible 36071da177e4SLinus Torvalds * with linux 2.0, and to avoid hard-linking to directories 36081da177e4SLinus Torvalds * and other special files. --ADM 36091da177e4SLinus Torvalds */ 36102e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname, 36112e4d0924SHeiko Carstens int, newdfd, const char __user *, newname, int, flags) 36121da177e4SLinus Torvalds { 36131da177e4SLinus Torvalds struct dentry *new_dentry; 3614dae6ad8fSAl Viro struct path old_path, new_path; 361511a7b371SAneesh Kumar K.V int how = 0; 36161da177e4SLinus Torvalds int error; 36171da177e4SLinus Torvalds 361811a7b371SAneesh Kumar K.V if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0) 3619c04030e1SUlrich Drepper return -EINVAL; 362011a7b371SAneesh Kumar K.V /* 362111a7b371SAneesh Kumar K.V * To use null names we require CAP_DAC_READ_SEARCH 362211a7b371SAneesh Kumar K.V * This ensures that not everyone will be able to create 362311a7b371SAneesh Kumar K.V * handlink using the passed filedescriptor. 362411a7b371SAneesh Kumar K.V */ 362511a7b371SAneesh Kumar K.V if (flags & AT_EMPTY_PATH) { 362611a7b371SAneesh Kumar K.V if (!capable(CAP_DAC_READ_SEARCH)) 362711a7b371SAneesh Kumar K.V return -ENOENT; 362811a7b371SAneesh Kumar K.V how = LOOKUP_EMPTY; 362911a7b371SAneesh Kumar K.V } 3630c04030e1SUlrich Drepper 363111a7b371SAneesh Kumar K.V if (flags & AT_SYMLINK_FOLLOW) 363211a7b371SAneesh Kumar K.V how |= LOOKUP_FOLLOW; 3633442e31caSJeff Layton retry: 363411a7b371SAneesh Kumar K.V error = user_path_at(olddfd, oldname, how, &old_path); 36351da177e4SLinus Torvalds if (error) 36362ad94ae6SAl Viro return error; 36372ad94ae6SAl Viro 3638442e31caSJeff Layton new_dentry = user_path_create(newdfd, newname, &new_path, 3639442e31caSJeff Layton (how & LOOKUP_REVAL)); 36401da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 36416902d925SDave Hansen if (IS_ERR(new_dentry)) 3642dae6ad8fSAl Viro goto out; 3643dae6ad8fSAl Viro 3644dae6ad8fSAl Viro error = -EXDEV; 3645dae6ad8fSAl Viro if (old_path.mnt != new_path.mnt) 3646dae6ad8fSAl Viro goto out_dput; 3647800179c9SKees Cook error = may_linkat(&old_path); 3648800179c9SKees Cook if (unlikely(error)) 3649800179c9SKees Cook goto out_dput; 3650dae6ad8fSAl Viro error = security_path_link(old_path.dentry, &new_path, new_dentry); 3651be6d3e56SKentaro Takeda if (error) 3652a8104a9fSAl Viro goto out_dput; 3653dae6ad8fSAl Viro error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry); 365475c3f29dSDave Hansen out_dput: 3655921a1650SAl Viro done_path_create(&new_path, new_dentry); 3656442e31caSJeff Layton if (retry_estale(error, how)) { 3657442e31caSJeff Layton how |= LOOKUP_REVAL; 3658442e31caSJeff Layton goto retry; 3659442e31caSJeff Layton } 36601da177e4SLinus Torvalds out: 36612d8f3038SAl Viro path_put(&old_path); 36621da177e4SLinus Torvalds 36631da177e4SLinus Torvalds return error; 36641da177e4SLinus Torvalds } 36651da177e4SLinus Torvalds 36663480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname) 36675590ff0dSUlrich Drepper { 3668c04030e1SUlrich Drepper return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0); 36695590ff0dSUlrich Drepper } 36705590ff0dSUlrich Drepper 36711da177e4SLinus Torvalds /* 36721da177e4SLinus Torvalds * The worst of all namespace operations - renaming directory. "Perverted" 36731da177e4SLinus Torvalds * doesn't even start to describe it. Somebody in UCB had a heck of a trip... 36741da177e4SLinus Torvalds * Problems: 36751da177e4SLinus Torvalds * a) we can get into loop creation. Check is done in is_subdir(). 36761da177e4SLinus Torvalds * b) race potential - two innocent renames can create a loop together. 36771da177e4SLinus Torvalds * That's where 4.4 screws up. Current fix: serialization on 3678a11f3a05SArjan van de Ven * sb->s_vfs_rename_mutex. We might be more accurate, but that's another 36791da177e4SLinus Torvalds * story. 36801da177e4SLinus Torvalds * c) we have to lock _three_ objects - parents and victim (if it exists). 36811b1dcc1bSJes Sorensen * And that - after we got ->i_mutex on parents (until then we don't know 36821da177e4SLinus Torvalds * whether the target exists). Solution: try to be smart with locking 36831da177e4SLinus Torvalds * order for inodes. We rely on the fact that tree topology may change 3684a11f3a05SArjan van de Ven * only under ->s_vfs_rename_mutex _and_ that parent of the object we 36851da177e4SLinus Torvalds * move will be locked. Thus we can rank directories by the tree 36861da177e4SLinus Torvalds * (ancestors first) and rank all non-directories after them. 36871da177e4SLinus Torvalds * That works since everybody except rename does "lock parent, lookup, 3688a11f3a05SArjan van de Ven * lock child" and rename is under ->s_vfs_rename_mutex. 36891da177e4SLinus Torvalds * HOWEVER, it relies on the assumption that any object with ->lookup() 36901da177e4SLinus Torvalds * has no more than 1 dentry. If "hybrid" objects will ever appear, 36911da177e4SLinus Torvalds * we'd better make sure that there's no link(2) for them. 3692e4eaac06SSage Weil * d) conversion from fhandle to dentry may come in the wrong moment - when 36931b1dcc1bSJes Sorensen * we are removing the target. Solution: we will have to grab ->i_mutex 36941da177e4SLinus Torvalds * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on 3695c41b20e7SAdam Buchbinder * ->i_mutex on parents, which works but leads to some truly excessive 36961da177e4SLinus Torvalds * locking]. 36971da177e4SLinus Torvalds */ 369875c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry, 36991da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 37001da177e4SLinus Torvalds { 37011da177e4SLinus Torvalds int error = 0; 37029055cba7SSage Weil struct inode *target = new_dentry->d_inode; 37038de52778SAl Viro unsigned max_links = new_dir->i_sb->s_max_links; 37041da177e4SLinus Torvalds 37051da177e4SLinus Torvalds /* 37061da177e4SLinus Torvalds * If we are going to change the parent - check write permissions, 37071da177e4SLinus Torvalds * we'll need to flip '..'. 37081da177e4SLinus Torvalds */ 37091da177e4SLinus Torvalds if (new_dir != old_dir) { 3710f419a2e3SAl Viro error = inode_permission(old_dentry->d_inode, MAY_WRITE); 37111da177e4SLinus Torvalds if (error) 37121da177e4SLinus Torvalds return error; 37131da177e4SLinus Torvalds } 37141da177e4SLinus Torvalds 37151da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 37161da177e4SLinus Torvalds if (error) 37171da177e4SLinus Torvalds return error; 37181da177e4SLinus Torvalds 37191d2ef590SAl Viro dget(new_dentry); 3720d83c49f3SAl Viro if (target) 37211b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 37229055cba7SSage Weil 37231da177e4SLinus Torvalds error = -EBUSY; 37249055cba7SSage Weil if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry)) 37259055cba7SSage Weil goto out; 37269055cba7SSage Weil 37278de52778SAl Viro error = -EMLINK; 37288de52778SAl Viro if (max_links && !target && new_dir != old_dir && 37298de52778SAl Viro new_dir->i_nlink >= max_links) 37308de52778SAl Viro goto out; 37318de52778SAl Viro 37323cebde24SSage Weil if (target) 37333cebde24SSage Weil shrink_dcache_parent(new_dentry); 37341da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 37359055cba7SSage Weil if (error) 37369055cba7SSage Weil goto out; 37379055cba7SSage Weil 37381da177e4SLinus Torvalds if (target) { 37391da177e4SLinus Torvalds target->i_flags |= S_DEAD; 3740d83c49f3SAl Viro dont_mount(new_dentry); 3741d83c49f3SAl Viro } 37429055cba7SSage Weil out: 37439055cba7SSage Weil if (target) 37441b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 37451d2ef590SAl Viro dput(new_dentry); 3746e31e14ecSStephen Smalley if (!error) 3747349457ccSMark Fasheh if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) 37481da177e4SLinus Torvalds d_move(old_dentry,new_dentry); 37491da177e4SLinus Torvalds return error; 37501da177e4SLinus Torvalds } 37511da177e4SLinus Torvalds 375275c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry, 37531da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 37541da177e4SLinus Torvalds { 375551892bbbSSage Weil struct inode *target = new_dentry->d_inode; 37561da177e4SLinus Torvalds int error; 37571da177e4SLinus Torvalds 37581da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 37591da177e4SLinus Torvalds if (error) 37601da177e4SLinus Torvalds return error; 37611da177e4SLinus Torvalds 37621da177e4SLinus Torvalds dget(new_dentry); 37631da177e4SLinus Torvalds if (target) 37641b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 376551892bbbSSage Weil 37661da177e4SLinus Torvalds error = -EBUSY; 376751892bbbSSage Weil if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry)) 376851892bbbSSage Weil goto out; 376951892bbbSSage Weil 37701da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 377151892bbbSSage Weil if (error) 377251892bbbSSage Weil goto out; 377351892bbbSSage Weil 3774bec1052eSAl Viro if (target) 3775d83c49f3SAl Viro dont_mount(new_dentry); 3776349457ccSMark Fasheh if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) 37771da177e4SLinus Torvalds d_move(old_dentry, new_dentry); 377851892bbbSSage Weil out: 37791da177e4SLinus Torvalds if (target) 37801b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 37811da177e4SLinus Torvalds dput(new_dentry); 37821da177e4SLinus Torvalds return error; 37831da177e4SLinus Torvalds } 37841da177e4SLinus Torvalds 37851da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry, 37861da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 37871da177e4SLinus Torvalds { 37881da177e4SLinus Torvalds int error; 37891da177e4SLinus Torvalds int is_dir = S_ISDIR(old_dentry->d_inode->i_mode); 379059b0df21SEric Paris const unsigned char *old_name; 37911da177e4SLinus Torvalds 37921da177e4SLinus Torvalds if (old_dentry->d_inode == new_dentry->d_inode) 37931da177e4SLinus Torvalds return 0; 37941da177e4SLinus Torvalds 37951da177e4SLinus Torvalds error = may_delete(old_dir, old_dentry, is_dir); 37961da177e4SLinus Torvalds if (error) 37971da177e4SLinus Torvalds return error; 37981da177e4SLinus Torvalds 37991da177e4SLinus Torvalds if (!new_dentry->d_inode) 3800a95164d9SMiklos Szeredi error = may_create(new_dir, new_dentry); 38011da177e4SLinus Torvalds else 38021da177e4SLinus Torvalds error = may_delete(new_dir, new_dentry, is_dir); 38031da177e4SLinus Torvalds if (error) 38041da177e4SLinus Torvalds return error; 38051da177e4SLinus Torvalds 3806acfa4380SAl Viro if (!old_dir->i_op->rename) 38071da177e4SLinus Torvalds return -EPERM; 38081da177e4SLinus Torvalds 38090eeca283SRobert Love old_name = fsnotify_oldname_init(old_dentry->d_name.name); 38100eeca283SRobert Love 38111da177e4SLinus Torvalds if (is_dir) 38121da177e4SLinus Torvalds error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry); 38131da177e4SLinus Torvalds else 38141da177e4SLinus Torvalds error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry); 3815123df294SAl Viro if (!error) 3816123df294SAl Viro fsnotify_move(old_dir, new_dir, old_name, is_dir, 38175a190ae6SAl Viro new_dentry->d_inode, old_dentry); 38180eeca283SRobert Love fsnotify_oldname_free(old_name); 38190eeca283SRobert Love 38201da177e4SLinus Torvalds return error; 38211da177e4SLinus Torvalds } 38221da177e4SLinus Torvalds 38232e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname, 38242e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 38251da177e4SLinus Torvalds { 38261da177e4SLinus Torvalds struct dentry *old_dir, *new_dir; 38271da177e4SLinus Torvalds struct dentry *old_dentry, *new_dentry; 38281da177e4SLinus Torvalds struct dentry *trap; 38291da177e4SLinus Torvalds struct nameidata oldnd, newnd; 383091a27b2aSJeff Layton struct filename *from; 383191a27b2aSJeff Layton struct filename *to; 38322ad94ae6SAl Viro int error; 38331da177e4SLinus Torvalds 38349e790bd6SJeff Layton from = user_path_parent(olddfd, oldname, &oldnd, 0); 383591a27b2aSJeff Layton if (IS_ERR(from)) { 383691a27b2aSJeff Layton error = PTR_ERR(from); 38371da177e4SLinus Torvalds goto exit; 383891a27b2aSJeff Layton } 38391da177e4SLinus Torvalds 38409e790bd6SJeff Layton to = user_path_parent(newdfd, newname, &newnd, 0); 384191a27b2aSJeff Layton if (IS_ERR(to)) { 384291a27b2aSJeff Layton error = PTR_ERR(to); 38431da177e4SLinus Torvalds goto exit1; 384491a27b2aSJeff Layton } 38451da177e4SLinus Torvalds 38461da177e4SLinus Torvalds error = -EXDEV; 38474ac91378SJan Blunck if (oldnd.path.mnt != newnd.path.mnt) 38481da177e4SLinus Torvalds goto exit2; 38491da177e4SLinus Torvalds 38504ac91378SJan Blunck old_dir = oldnd.path.dentry; 38511da177e4SLinus Torvalds error = -EBUSY; 38521da177e4SLinus Torvalds if (oldnd.last_type != LAST_NORM) 38531da177e4SLinus Torvalds goto exit2; 38541da177e4SLinus Torvalds 38554ac91378SJan Blunck new_dir = newnd.path.dentry; 38561da177e4SLinus Torvalds if (newnd.last_type != LAST_NORM) 38571da177e4SLinus Torvalds goto exit2; 38581da177e4SLinus Torvalds 3859c30dabfeSJan Kara error = mnt_want_write(oldnd.path.mnt); 3860c30dabfeSJan Kara if (error) 3861c30dabfeSJan Kara goto exit2; 3862c30dabfeSJan Kara 38630612d9fbSOGAWA Hirofumi oldnd.flags &= ~LOOKUP_PARENT; 38640612d9fbSOGAWA Hirofumi newnd.flags &= ~LOOKUP_PARENT; 38654e9ed2f8SOGAWA Hirofumi newnd.flags |= LOOKUP_RENAME_TARGET; 38660612d9fbSOGAWA Hirofumi 38671da177e4SLinus Torvalds trap = lock_rename(new_dir, old_dir); 38681da177e4SLinus Torvalds 386949705b77SChristoph Hellwig old_dentry = lookup_hash(&oldnd); 38701da177e4SLinus Torvalds error = PTR_ERR(old_dentry); 38711da177e4SLinus Torvalds if (IS_ERR(old_dentry)) 38721da177e4SLinus Torvalds goto exit3; 38731da177e4SLinus Torvalds /* source must exist */ 38741da177e4SLinus Torvalds error = -ENOENT; 38751da177e4SLinus Torvalds if (!old_dentry->d_inode) 38761da177e4SLinus Torvalds goto exit4; 38771da177e4SLinus Torvalds /* unless the source is a directory trailing slashes give -ENOTDIR */ 38781da177e4SLinus Torvalds if (!S_ISDIR(old_dentry->d_inode->i_mode)) { 38791da177e4SLinus Torvalds error = -ENOTDIR; 38801da177e4SLinus Torvalds if (oldnd.last.name[oldnd.last.len]) 38811da177e4SLinus Torvalds goto exit4; 38821da177e4SLinus Torvalds if (newnd.last.name[newnd.last.len]) 38831da177e4SLinus Torvalds goto exit4; 38841da177e4SLinus Torvalds } 38851da177e4SLinus Torvalds /* source should not be ancestor of target */ 38861da177e4SLinus Torvalds error = -EINVAL; 38871da177e4SLinus Torvalds if (old_dentry == trap) 38881da177e4SLinus Torvalds goto exit4; 388949705b77SChristoph Hellwig new_dentry = lookup_hash(&newnd); 38901da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 38911da177e4SLinus Torvalds if (IS_ERR(new_dentry)) 38921da177e4SLinus Torvalds goto exit4; 38931da177e4SLinus Torvalds /* target should not be an ancestor of source */ 38941da177e4SLinus Torvalds error = -ENOTEMPTY; 38951da177e4SLinus Torvalds if (new_dentry == trap) 38961da177e4SLinus Torvalds goto exit5; 38971da177e4SLinus Torvalds 3898be6d3e56SKentaro Takeda error = security_path_rename(&oldnd.path, old_dentry, 3899be6d3e56SKentaro Takeda &newnd.path, new_dentry); 3900be6d3e56SKentaro Takeda if (error) 3901c30dabfeSJan Kara goto exit5; 39021da177e4SLinus Torvalds error = vfs_rename(old_dir->d_inode, old_dentry, 39031da177e4SLinus Torvalds new_dir->d_inode, new_dentry); 39041da177e4SLinus Torvalds exit5: 39051da177e4SLinus Torvalds dput(new_dentry); 39061da177e4SLinus Torvalds exit4: 39071da177e4SLinus Torvalds dput(old_dentry); 39081da177e4SLinus Torvalds exit3: 39091da177e4SLinus Torvalds unlock_rename(new_dir, old_dir); 3910c30dabfeSJan Kara mnt_drop_write(oldnd.path.mnt); 39111da177e4SLinus Torvalds exit2: 39121d957f9bSJan Blunck path_put(&newnd.path); 39132ad94ae6SAl Viro putname(to); 39141da177e4SLinus Torvalds exit1: 39151d957f9bSJan Blunck path_put(&oldnd.path); 39161da177e4SLinus Torvalds putname(from); 39172ad94ae6SAl Viro exit: 39181da177e4SLinus Torvalds return error; 39191da177e4SLinus Torvalds } 39201da177e4SLinus Torvalds 3921a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname) 39225590ff0dSUlrich Drepper { 39235590ff0dSUlrich Drepper return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname); 39245590ff0dSUlrich Drepper } 39255590ff0dSUlrich Drepper 39261da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link) 39271da177e4SLinus Torvalds { 39281da177e4SLinus Torvalds int len; 39291da177e4SLinus Torvalds 39301da177e4SLinus Torvalds len = PTR_ERR(link); 39311da177e4SLinus Torvalds if (IS_ERR(link)) 39321da177e4SLinus Torvalds goto out; 39331da177e4SLinus Torvalds 39341da177e4SLinus Torvalds len = strlen(link); 39351da177e4SLinus Torvalds if (len > (unsigned) buflen) 39361da177e4SLinus Torvalds len = buflen; 39371da177e4SLinus Torvalds if (copy_to_user(buffer, link, len)) 39381da177e4SLinus Torvalds len = -EFAULT; 39391da177e4SLinus Torvalds out: 39401da177e4SLinus Torvalds return len; 39411da177e4SLinus Torvalds } 39421da177e4SLinus Torvalds 39431da177e4SLinus Torvalds /* 39441da177e4SLinus Torvalds * A helper for ->readlink(). This should be used *ONLY* for symlinks that 39451da177e4SLinus Torvalds * have ->follow_link() touching nd only in nd_set_link(). Using (or not 39461da177e4SLinus Torvalds * using) it for any given inode is up to filesystem. 39471da177e4SLinus Torvalds */ 39481da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen) 39491da177e4SLinus Torvalds { 39501da177e4SLinus Torvalds struct nameidata nd; 3951cc314eefSLinus Torvalds void *cookie; 3952694a1764SMarcin Slusarz int res; 3953cc314eefSLinus Torvalds 39541da177e4SLinus Torvalds nd.depth = 0; 3955cc314eefSLinus Torvalds cookie = dentry->d_inode->i_op->follow_link(dentry, &nd); 3956694a1764SMarcin Slusarz if (IS_ERR(cookie)) 3957694a1764SMarcin Slusarz return PTR_ERR(cookie); 3958694a1764SMarcin Slusarz 3959694a1764SMarcin Slusarz res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd)); 39601da177e4SLinus Torvalds if (dentry->d_inode->i_op->put_link) 3961cc314eefSLinus Torvalds dentry->d_inode->i_op->put_link(dentry, &nd, cookie); 3962694a1764SMarcin Slusarz return res; 39631da177e4SLinus Torvalds } 39641da177e4SLinus Torvalds 39651da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link) 39661da177e4SLinus Torvalds { 39671da177e4SLinus Torvalds return __vfs_follow_link(nd, link); 39681da177e4SLinus Torvalds } 39691da177e4SLinus Torvalds 39701da177e4SLinus Torvalds /* get the link contents into pagecache */ 39711da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage) 39721da177e4SLinus Torvalds { 3973ebd09abbSDuane Griffin char *kaddr; 39741da177e4SLinus Torvalds struct page *page; 39751da177e4SLinus Torvalds struct address_space *mapping = dentry->d_inode->i_mapping; 3976090d2b18SPekka Enberg page = read_mapping_page(mapping, 0, NULL); 39771da177e4SLinus Torvalds if (IS_ERR(page)) 39786fe6900eSNick Piggin return (char*)page; 39791da177e4SLinus Torvalds *ppage = page; 3980ebd09abbSDuane Griffin kaddr = kmap(page); 3981ebd09abbSDuane Griffin nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1); 3982ebd09abbSDuane Griffin return kaddr; 39831da177e4SLinus Torvalds } 39841da177e4SLinus Torvalds 39851da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen) 39861da177e4SLinus Torvalds { 39871da177e4SLinus Torvalds struct page *page = NULL; 39881da177e4SLinus Torvalds char *s = page_getlink(dentry, &page); 39891da177e4SLinus Torvalds int res = vfs_readlink(dentry,buffer,buflen,s); 39901da177e4SLinus Torvalds if (page) { 39911da177e4SLinus Torvalds kunmap(page); 39921da177e4SLinus Torvalds page_cache_release(page); 39931da177e4SLinus Torvalds } 39941da177e4SLinus Torvalds return res; 39951da177e4SLinus Torvalds } 39961da177e4SLinus Torvalds 3997cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd) 39981da177e4SLinus Torvalds { 3999cc314eefSLinus Torvalds struct page *page = NULL; 40001da177e4SLinus Torvalds nd_set_link(nd, page_getlink(dentry, &page)); 4001cc314eefSLinus Torvalds return page; 40021da177e4SLinus Torvalds } 40031da177e4SLinus Torvalds 4004cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie) 40051da177e4SLinus Torvalds { 4006cc314eefSLinus Torvalds struct page *page = cookie; 4007cc314eefSLinus Torvalds 4008cc314eefSLinus Torvalds if (page) { 40091da177e4SLinus Torvalds kunmap(page); 40101da177e4SLinus Torvalds page_cache_release(page); 40111da177e4SLinus Torvalds } 40121da177e4SLinus Torvalds } 40131da177e4SLinus Torvalds 401454566b2cSNick Piggin /* 401554566b2cSNick Piggin * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS 401654566b2cSNick Piggin */ 401754566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs) 40181da177e4SLinus Torvalds { 40191da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 40200adb25d2SKirill Korotaev struct page *page; 4021afddba49SNick Piggin void *fsdata; 4022beb497abSDmitriy Monakhov int err; 40231da177e4SLinus Torvalds char *kaddr; 402454566b2cSNick Piggin unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE; 402554566b2cSNick Piggin if (nofs) 402654566b2cSNick Piggin flags |= AOP_FLAG_NOFS; 40271da177e4SLinus Torvalds 40287e53cac4SNeilBrown retry: 4029afddba49SNick Piggin err = pagecache_write_begin(NULL, mapping, 0, len-1, 403054566b2cSNick Piggin flags, &page, &fsdata); 40311da177e4SLinus Torvalds if (err) 4032afddba49SNick Piggin goto fail; 4033afddba49SNick Piggin 4034e8e3c3d6SCong Wang kaddr = kmap_atomic(page); 40351da177e4SLinus Torvalds memcpy(kaddr, symname, len-1); 4036e8e3c3d6SCong Wang kunmap_atomic(kaddr); 4037afddba49SNick Piggin 4038afddba49SNick Piggin err = pagecache_write_end(NULL, mapping, 0, len-1, len-1, 4039afddba49SNick Piggin page, fsdata); 40401da177e4SLinus Torvalds if (err < 0) 40411da177e4SLinus Torvalds goto fail; 4042afddba49SNick Piggin if (err < len-1) 4043afddba49SNick Piggin goto retry; 4044afddba49SNick Piggin 40451da177e4SLinus Torvalds mark_inode_dirty(inode); 40461da177e4SLinus Torvalds return 0; 40471da177e4SLinus Torvalds fail: 40481da177e4SLinus Torvalds return err; 40491da177e4SLinus Torvalds } 40501da177e4SLinus Torvalds 40510adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len) 40520adb25d2SKirill Korotaev { 40530adb25d2SKirill Korotaev return __page_symlink(inode, symname, len, 405454566b2cSNick Piggin !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS)); 40550adb25d2SKirill Korotaev } 40560adb25d2SKirill Korotaev 405792e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = { 40581da177e4SLinus Torvalds .readlink = generic_readlink, 40591da177e4SLinus Torvalds .follow_link = page_follow_link_light, 40601da177e4SLinus Torvalds .put_link = page_put_link, 40611da177e4SLinus Torvalds }; 40621da177e4SLinus Torvalds 40632d8f3038SAl Viro EXPORT_SYMBOL(user_path_at); 4064cc53ce53SDavid Howells EXPORT_SYMBOL(follow_down_one); 40651da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down); 40661da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up); 4067f6d2ac5cSAl Viro EXPORT_SYMBOL(get_write_access); /* nfsd */ 40681da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename); 40691da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len); 40701da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light); 40711da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link); 40721da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink); 40730adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink); 40741da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink); 40751da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations); 4076d1811465SAl Viro EXPORT_SYMBOL(kern_path); 407716f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup); 4078f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission); 40791da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename); 40801da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create); 40811da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link); 40821da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link); 40831da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir); 40841da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod); 40851da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission); 40861da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink); 40871da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename); 40881da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir); 40891da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink); 40901da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink); 40911da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash); 40921da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink); 4093