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 { 12291a27b2aSJeff Layton __putname(name->name); 12391a27b2aSJeff Layton kfree(name); 12491a27b2aSJeff Layton } 12591a27b2aSJeff Layton 12691a27b2aSJeff Layton static struct filename * 12791a27b2aSJeff Layton getname_flags(const char __user *filename, int flags, int *empty) 12891a27b2aSJeff Layton { 12991a27b2aSJeff Layton struct filename *result, *err; 13091a27b2aSJeff Layton char *kname; 1313f9f0aa6SLinus Torvalds int len; 1321da177e4SLinus Torvalds 13391a27b2aSJeff Layton /* FIXME: create dedicated slabcache? */ 13491a27b2aSJeff Layton result = kzalloc(sizeof(*result), GFP_KERNEL); 1353f9f0aa6SLinus Torvalds if (unlikely(!result)) 1364043cde8SEric Paris return ERR_PTR(-ENOMEM); 1371da177e4SLinus Torvalds 13891a27b2aSJeff Layton kname = __getname(); 13991a27b2aSJeff Layton if (unlikely(!kname)) { 14091a27b2aSJeff Layton err = ERR_PTR(-ENOMEM); 14191a27b2aSJeff Layton goto error_free_name; 14291a27b2aSJeff Layton } 14391a27b2aSJeff Layton 14491a27b2aSJeff Layton result->name = kname; 14591a27b2aSJeff Layton result->uptr = filename; 14691a27b2aSJeff Layton len = strncpy_from_user(kname, filename, PATH_MAX); 14791a27b2aSJeff Layton if (unlikely(len < 0)) { 1483f9f0aa6SLinus Torvalds err = ERR_PTR(len); 1493f9f0aa6SLinus Torvalds goto error; 15091a27b2aSJeff Layton } 1513f9f0aa6SLinus Torvalds 1523f9f0aa6SLinus Torvalds /* The empty path is special. */ 1533f9f0aa6SLinus Torvalds if (unlikely(!len)) { 1543f9f0aa6SLinus Torvalds if (empty) 1551fa1e7f6SAndy Whitcroft *empty = 1; 1563f9f0aa6SLinus Torvalds err = ERR_PTR(-ENOENT); 1573f9f0aa6SLinus Torvalds if (!(flags & LOOKUP_EMPTY)) 1583f9f0aa6SLinus Torvalds goto error; 1591da177e4SLinus Torvalds } 1603f9f0aa6SLinus Torvalds 1613f9f0aa6SLinus Torvalds err = ERR_PTR(-ENAMETOOLONG); 1623f9f0aa6SLinus Torvalds if (likely(len < PATH_MAX)) { 1631da177e4SLinus Torvalds audit_getname(result); 1641da177e4SLinus Torvalds return result; 1651da177e4SLinus Torvalds } 1661da177e4SLinus Torvalds 1673f9f0aa6SLinus Torvalds error: 16891a27b2aSJeff Layton __putname(kname); 16991a27b2aSJeff Layton error_free_name: 17091a27b2aSJeff Layton kfree(result); 1713f9f0aa6SLinus Torvalds return err; 1723f9f0aa6SLinus Torvalds } 1733f9f0aa6SLinus Torvalds 17491a27b2aSJeff Layton struct filename * 17591a27b2aSJeff Layton getname(const char __user * filename) 176f52e0c11SAl Viro { 177f7493e5dSLinus Torvalds return getname_flags(filename, 0, NULL); 178f52e0c11SAl Viro } 17991a27b2aSJeff Layton EXPORT_SYMBOL(getname); 180f52e0c11SAl Viro 1811da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL 18291a27b2aSJeff Layton void putname(struct filename *name) 1831da177e4SLinus Torvalds { 1845ac3a9c2SAl Viro if (unlikely(!audit_dummy_context())) 18591a27b2aSJeff Layton return audit_putname(name); 18691a27b2aSJeff Layton final_putname(name); 1871da177e4SLinus Torvalds } 1881da177e4SLinus Torvalds #endif 1891da177e4SLinus Torvalds 190e77819e5SLinus Torvalds static int check_acl(struct inode *inode, int mask) 191e77819e5SLinus Torvalds { 19284635d68SLinus Torvalds #ifdef CONFIG_FS_POSIX_ACL 193e77819e5SLinus Torvalds struct posix_acl *acl; 194e77819e5SLinus Torvalds 195e77819e5SLinus Torvalds if (mask & MAY_NOT_BLOCK) { 1963567866bSAl Viro acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS); 1973567866bSAl Viro if (!acl) 198e77819e5SLinus Torvalds return -EAGAIN; 1993567866bSAl Viro /* no ->get_acl() calls in RCU mode... */ 2003567866bSAl Viro if (acl == ACL_NOT_CACHED) 201e77819e5SLinus Torvalds return -ECHILD; 202206b1d09SAri Savolainen return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK); 203e77819e5SLinus Torvalds } 204e77819e5SLinus Torvalds 205e77819e5SLinus Torvalds acl = get_cached_acl(inode, ACL_TYPE_ACCESS); 206e77819e5SLinus Torvalds 207e77819e5SLinus Torvalds /* 2084e34e719SChristoph Hellwig * A filesystem can force a ACL callback by just never filling the 2094e34e719SChristoph Hellwig * ACL cache. But normally you'd fill the cache either at inode 2104e34e719SChristoph Hellwig * instantiation time, or on the first ->get_acl call. 211e77819e5SLinus Torvalds * 2124e34e719SChristoph Hellwig * If the filesystem doesn't have a get_acl() function at all, we'll 2134e34e719SChristoph Hellwig * just create the negative cache entry. 214e77819e5SLinus Torvalds */ 215e77819e5SLinus Torvalds if (acl == ACL_NOT_CACHED) { 2164e34e719SChristoph Hellwig if (inode->i_op->get_acl) { 2174e34e719SChristoph Hellwig acl = inode->i_op->get_acl(inode, ACL_TYPE_ACCESS); 2184e34e719SChristoph Hellwig if (IS_ERR(acl)) 2194e34e719SChristoph Hellwig return PTR_ERR(acl); 2204e34e719SChristoph Hellwig } else { 221e77819e5SLinus Torvalds set_cached_acl(inode, ACL_TYPE_ACCESS, NULL); 222e77819e5SLinus Torvalds return -EAGAIN; 223e77819e5SLinus Torvalds } 2244e34e719SChristoph Hellwig } 225e77819e5SLinus Torvalds 226e77819e5SLinus Torvalds if (acl) { 227e77819e5SLinus Torvalds int error = posix_acl_permission(inode, acl, mask); 228e77819e5SLinus Torvalds posix_acl_release(acl); 229e77819e5SLinus Torvalds return error; 230e77819e5SLinus Torvalds } 23184635d68SLinus Torvalds #endif 232e77819e5SLinus Torvalds 233e77819e5SLinus Torvalds return -EAGAIN; 234e77819e5SLinus Torvalds } 235e77819e5SLinus Torvalds 2365909ccaaSLinus Torvalds /* 237948409c7SAndreas Gruenbacher * This does the basic permission checking 2385909ccaaSLinus Torvalds */ 2397e40145eSAl Viro static int acl_permission_check(struct inode *inode, int mask) 2405909ccaaSLinus Torvalds { 24126cf46beSLinus Torvalds unsigned int mode = inode->i_mode; 2425909ccaaSLinus Torvalds 2438e96e3b7SEric W. Biederman if (likely(uid_eq(current_fsuid(), inode->i_uid))) 2445909ccaaSLinus Torvalds mode >>= 6; 2455909ccaaSLinus Torvalds else { 246e77819e5SLinus Torvalds if (IS_POSIXACL(inode) && (mode & S_IRWXG)) { 2477e40145eSAl Viro int error = check_acl(inode, mask); 2485909ccaaSLinus Torvalds if (error != -EAGAIN) 2495909ccaaSLinus Torvalds return error; 2505909ccaaSLinus Torvalds } 2515909ccaaSLinus Torvalds 2525909ccaaSLinus Torvalds if (in_group_p(inode->i_gid)) 2535909ccaaSLinus Torvalds mode >>= 3; 2545909ccaaSLinus Torvalds } 2555909ccaaSLinus Torvalds 2565909ccaaSLinus Torvalds /* 2575909ccaaSLinus Torvalds * If the DACs are ok we don't need any capability check. 2585909ccaaSLinus Torvalds */ 2599c2c7039SAl Viro if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0) 2605909ccaaSLinus Torvalds return 0; 2615909ccaaSLinus Torvalds return -EACCES; 2625909ccaaSLinus Torvalds } 2631da177e4SLinus Torvalds 2641da177e4SLinus Torvalds /** 2651da177e4SLinus Torvalds * generic_permission - check for access rights on a Posix-like filesystem 2661da177e4SLinus Torvalds * @inode: inode to check access rights for 2678fd90c8dSAndreas Gruenbacher * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...) 2681da177e4SLinus Torvalds * 2691da177e4SLinus Torvalds * Used to check for read/write/execute permissions on a file. 2701da177e4SLinus Torvalds * We use "fsuid" for this, letting us set arbitrary permissions 2711da177e4SLinus Torvalds * for filesystem access without changing the "normal" uids which 272b74c79e9SNick Piggin * are used for other things. 273b74c79e9SNick Piggin * 274b74c79e9SNick Piggin * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk 275b74c79e9SNick Piggin * request cannot be satisfied (eg. requires blocking or too much complexity). 276b74c79e9SNick Piggin * It would then be called again in ref-walk mode. 2771da177e4SLinus Torvalds */ 2782830ba7fSAl Viro int generic_permission(struct inode *inode, int mask) 2791da177e4SLinus Torvalds { 2805909ccaaSLinus Torvalds int ret; 2811da177e4SLinus Torvalds 2821da177e4SLinus Torvalds /* 283948409c7SAndreas Gruenbacher * Do the basic permission checks. 2841da177e4SLinus Torvalds */ 2857e40145eSAl Viro ret = acl_permission_check(inode, mask); 2865909ccaaSLinus Torvalds if (ret != -EACCES) 2875909ccaaSLinus Torvalds return ret; 2881da177e4SLinus Torvalds 289d594e7ecSAl Viro if (S_ISDIR(inode->i_mode)) { 290d594e7ecSAl Viro /* DACs are overridable for directories */ 2911a48e2acSEric W. Biederman if (inode_capable(inode, CAP_DAC_OVERRIDE)) 292d594e7ecSAl Viro return 0; 293d594e7ecSAl Viro if (!(mask & MAY_WRITE)) 2941a48e2acSEric W. Biederman if (inode_capable(inode, CAP_DAC_READ_SEARCH)) 295d594e7ecSAl Viro return 0; 296d594e7ecSAl Viro return -EACCES; 297d594e7ecSAl Viro } 2981da177e4SLinus Torvalds /* 2991da177e4SLinus Torvalds * Read/write DACs are always overridable. 300d594e7ecSAl Viro * Executable DACs are overridable when there is 301d594e7ecSAl Viro * at least one exec bit set. 3021da177e4SLinus Torvalds */ 303d594e7ecSAl Viro if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO)) 3041a48e2acSEric W. Biederman if (inode_capable(inode, CAP_DAC_OVERRIDE)) 3051da177e4SLinus Torvalds return 0; 3061da177e4SLinus Torvalds 3071da177e4SLinus Torvalds /* 3081da177e4SLinus Torvalds * Searching includes executable on directories, else just read. 3091da177e4SLinus Torvalds */ 3107ea66001SSerge E. Hallyn mask &= MAY_READ | MAY_WRITE | MAY_EXEC; 311d594e7ecSAl Viro if (mask == MAY_READ) 3121a48e2acSEric W. Biederman if (inode_capable(inode, CAP_DAC_READ_SEARCH)) 3131da177e4SLinus Torvalds return 0; 3141da177e4SLinus Torvalds 3151da177e4SLinus Torvalds return -EACCES; 3161da177e4SLinus Torvalds } 3171da177e4SLinus Torvalds 3183ddcd056SLinus Torvalds /* 3193ddcd056SLinus Torvalds * We _really_ want to just do "generic_permission()" without 3203ddcd056SLinus Torvalds * even looking at the inode->i_op values. So we keep a cache 3213ddcd056SLinus Torvalds * flag in inode->i_opflags, that says "this has not special 3223ddcd056SLinus Torvalds * permission function, use the fast case". 3233ddcd056SLinus Torvalds */ 3243ddcd056SLinus Torvalds static inline int do_inode_permission(struct inode *inode, int mask) 3253ddcd056SLinus Torvalds { 3263ddcd056SLinus Torvalds if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) { 3273ddcd056SLinus Torvalds if (likely(inode->i_op->permission)) 3283ddcd056SLinus Torvalds return inode->i_op->permission(inode, mask); 3293ddcd056SLinus Torvalds 3303ddcd056SLinus Torvalds /* This gets set once for the inode lifetime */ 3313ddcd056SLinus Torvalds spin_lock(&inode->i_lock); 3323ddcd056SLinus Torvalds inode->i_opflags |= IOP_FASTPERM; 3333ddcd056SLinus Torvalds spin_unlock(&inode->i_lock); 3343ddcd056SLinus Torvalds } 3353ddcd056SLinus Torvalds return generic_permission(inode, mask); 3363ddcd056SLinus Torvalds } 3373ddcd056SLinus Torvalds 338cb23beb5SChristoph Hellwig /** 3390bdaea90SDavid Howells * __inode_permission - Check for access rights to a given inode 3400bdaea90SDavid Howells * @inode: Inode to check permission on 3410bdaea90SDavid Howells * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) 342cb23beb5SChristoph Hellwig * 3430bdaea90SDavid Howells * Check for read/write/execute permissions on an inode. 344948409c7SAndreas Gruenbacher * 345948409c7SAndreas Gruenbacher * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask. 3460bdaea90SDavid Howells * 3470bdaea90SDavid Howells * This does not check for a read-only file system. You probably want 3480bdaea90SDavid Howells * inode_permission(). 349cb23beb5SChristoph Hellwig */ 3500bdaea90SDavid Howells int __inode_permission(struct inode *inode, int mask) 3511da177e4SLinus Torvalds { 352e6305c43SAl Viro int retval; 3531da177e4SLinus Torvalds 3543ddcd056SLinus Torvalds if (unlikely(mask & MAY_WRITE)) { 3551da177e4SLinus Torvalds /* 3561da177e4SLinus Torvalds * Nobody gets write access to an immutable file. 3571da177e4SLinus Torvalds */ 3581da177e4SLinus Torvalds if (IS_IMMUTABLE(inode)) 3591da177e4SLinus Torvalds return -EACCES; 3601da177e4SLinus Torvalds } 3611da177e4SLinus Torvalds 3623ddcd056SLinus Torvalds retval = do_inode_permission(inode, mask); 3631da177e4SLinus Torvalds if (retval) 3641da177e4SLinus Torvalds return retval; 3651da177e4SLinus Torvalds 36608ce5f16SSerge E. Hallyn retval = devcgroup_inode_permission(inode, mask); 36708ce5f16SSerge E. Hallyn if (retval) 36808ce5f16SSerge E. Hallyn return retval; 36908ce5f16SSerge E. Hallyn 370d09ca739SEric Paris return security_inode_permission(inode, mask); 3711da177e4SLinus Torvalds } 3721da177e4SLinus Torvalds 373f4d6ff89SAl Viro /** 3740bdaea90SDavid Howells * sb_permission - Check superblock-level permissions 3750bdaea90SDavid Howells * @sb: Superblock of inode to check permission on 37655852635SRandy Dunlap * @inode: Inode to check permission on 3770bdaea90SDavid Howells * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) 3780bdaea90SDavid Howells * 3790bdaea90SDavid Howells * Separate out file-system wide checks from inode-specific permission checks. 3800bdaea90SDavid Howells */ 3810bdaea90SDavid Howells static int sb_permission(struct super_block *sb, struct inode *inode, int mask) 3820bdaea90SDavid Howells { 3830bdaea90SDavid Howells if (unlikely(mask & MAY_WRITE)) { 3840bdaea90SDavid Howells umode_t mode = inode->i_mode; 3850bdaea90SDavid Howells 3860bdaea90SDavid Howells /* Nobody gets write access to a read-only fs. */ 3870bdaea90SDavid Howells if ((sb->s_flags & MS_RDONLY) && 3880bdaea90SDavid Howells (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) 3890bdaea90SDavid Howells return -EROFS; 3900bdaea90SDavid Howells } 3910bdaea90SDavid Howells return 0; 3920bdaea90SDavid Howells } 3930bdaea90SDavid Howells 3940bdaea90SDavid Howells /** 3950bdaea90SDavid Howells * inode_permission - Check for access rights to a given inode 3960bdaea90SDavid Howells * @inode: Inode to check permission on 3970bdaea90SDavid Howells * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) 3980bdaea90SDavid Howells * 3990bdaea90SDavid Howells * Check for read/write/execute permissions on an inode. We use fs[ug]id for 4000bdaea90SDavid Howells * this, letting us set arbitrary permissions for filesystem access without 4010bdaea90SDavid Howells * changing the "normal" UIDs which are used for other things. 4020bdaea90SDavid Howells * 4030bdaea90SDavid Howells * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask. 4040bdaea90SDavid Howells */ 4050bdaea90SDavid Howells int inode_permission(struct inode *inode, int mask) 4060bdaea90SDavid Howells { 4070bdaea90SDavid Howells int retval; 4080bdaea90SDavid Howells 4090bdaea90SDavid Howells retval = sb_permission(inode->i_sb, inode, mask); 4100bdaea90SDavid Howells if (retval) 4110bdaea90SDavid Howells return retval; 4120bdaea90SDavid Howells return __inode_permission(inode, mask); 4130bdaea90SDavid Howells } 4140bdaea90SDavid Howells 4150bdaea90SDavid Howells /** 4165dd784d0SJan Blunck * path_get - get a reference to a path 4175dd784d0SJan Blunck * @path: path to get the reference to 4185dd784d0SJan Blunck * 4195dd784d0SJan Blunck * Given a path increment the reference count to the dentry and the vfsmount. 4205dd784d0SJan Blunck */ 4215dd784d0SJan Blunck void path_get(struct path *path) 4225dd784d0SJan Blunck { 4235dd784d0SJan Blunck mntget(path->mnt); 4245dd784d0SJan Blunck dget(path->dentry); 4255dd784d0SJan Blunck } 4265dd784d0SJan Blunck EXPORT_SYMBOL(path_get); 4275dd784d0SJan Blunck 4285dd784d0SJan Blunck /** 4291d957f9bSJan Blunck * path_put - put a reference to a path 4301d957f9bSJan Blunck * @path: path to put the reference to 4311d957f9bSJan Blunck * 4321d957f9bSJan Blunck * Given a path decrement the reference count to the dentry and the vfsmount. 4331d957f9bSJan Blunck */ 4341d957f9bSJan Blunck void path_put(struct path *path) 4351da177e4SLinus Torvalds { 4361d957f9bSJan Blunck dput(path->dentry); 4371d957f9bSJan Blunck mntput(path->mnt); 4381da177e4SLinus Torvalds } 4391d957f9bSJan Blunck EXPORT_SYMBOL(path_put); 4401da177e4SLinus Torvalds 44119660af7SAl Viro /* 44231e6b01fSNick Piggin * Path walking has 2 modes, rcu-walk and ref-walk (see 44319660af7SAl Viro * Documentation/filesystems/path-lookup.txt). In situations when we can't 44419660af7SAl Viro * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab 44519660af7SAl Viro * normal reference counts on dentries and vfsmounts to transition to rcu-walk 44619660af7SAl Viro * mode. Refcounts are grabbed at the last known good point before rcu-walk 44719660af7SAl Viro * got stuck, so ref-walk may continue from there. If this is not successful 44819660af7SAl Viro * (eg. a seqcount has changed), then failure is returned and it's up to caller 44919660af7SAl Viro * to restart the path walk from the beginning in ref-walk mode. 45031e6b01fSNick Piggin */ 45131e6b01fSNick Piggin 45232a7991bSAl Viro static inline void lock_rcu_walk(void) 45332a7991bSAl Viro { 45432a7991bSAl Viro br_read_lock(&vfsmount_lock); 45532a7991bSAl Viro rcu_read_lock(); 45632a7991bSAl Viro } 45732a7991bSAl Viro 45832a7991bSAl Viro static inline void unlock_rcu_walk(void) 45932a7991bSAl Viro { 46032a7991bSAl Viro rcu_read_unlock(); 46132a7991bSAl Viro br_read_unlock(&vfsmount_lock); 46232a7991bSAl Viro } 46332a7991bSAl Viro 46431e6b01fSNick Piggin /** 46519660af7SAl Viro * unlazy_walk - try to switch to ref-walk mode. 46619660af7SAl Viro * @nd: nameidata pathwalk data 46719660af7SAl Viro * @dentry: child of nd->path.dentry or NULL 46839191628SRandy Dunlap * Returns: 0 on success, -ECHILD on failure 46931e6b01fSNick Piggin * 47019660af7SAl Viro * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry 47119660af7SAl Viro * for ref-walk mode. @dentry must be a path found by a do_lookup call on 47219660af7SAl Viro * @nd or NULL. Must be called from rcu-walk context. 47331e6b01fSNick Piggin */ 47419660af7SAl Viro static int unlazy_walk(struct nameidata *nd, struct dentry *dentry) 47531e6b01fSNick Piggin { 47631e6b01fSNick Piggin struct fs_struct *fs = current->fs; 47731e6b01fSNick Piggin struct dentry *parent = nd->path.dentry; 4785b6ca027SAl Viro int want_root = 0; 47931e6b01fSNick Piggin 48031e6b01fSNick Piggin BUG_ON(!(nd->flags & LOOKUP_RCU)); 4815b6ca027SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) { 4825b6ca027SAl Viro want_root = 1; 48331e6b01fSNick Piggin spin_lock(&fs->lock); 48431e6b01fSNick Piggin if (nd->root.mnt != fs->root.mnt || 48531e6b01fSNick Piggin nd->root.dentry != fs->root.dentry) 48631e6b01fSNick Piggin goto err_root; 48731e6b01fSNick Piggin } 48831e6b01fSNick Piggin spin_lock(&parent->d_lock); 48919660af7SAl Viro if (!dentry) { 49019660af7SAl Viro if (!__d_rcu_to_refcount(parent, nd->seq)) 49119660af7SAl Viro goto err_parent; 49219660af7SAl Viro BUG_ON(nd->inode != parent->d_inode); 49319660af7SAl Viro } else { 49494c0d4ecSAl Viro if (dentry->d_parent != parent) 49594c0d4ecSAl Viro goto err_parent; 49631e6b01fSNick Piggin spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); 49731e6b01fSNick Piggin if (!__d_rcu_to_refcount(dentry, nd->seq)) 49819660af7SAl Viro goto err_child; 49931e6b01fSNick Piggin /* 50019660af7SAl Viro * If the sequence check on the child dentry passed, then 50119660af7SAl Viro * the child has not been removed from its parent. This 50219660af7SAl Viro * means the parent dentry must be valid and able to take 50319660af7SAl Viro * a reference at this point. 50431e6b01fSNick Piggin */ 50531e6b01fSNick Piggin BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent); 50631e6b01fSNick Piggin BUG_ON(!parent->d_count); 50731e6b01fSNick Piggin parent->d_count++; 50831e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 50919660af7SAl Viro } 51031e6b01fSNick Piggin spin_unlock(&parent->d_lock); 5115b6ca027SAl Viro if (want_root) { 51231e6b01fSNick Piggin path_get(&nd->root); 51331e6b01fSNick Piggin spin_unlock(&fs->lock); 51431e6b01fSNick Piggin } 51531e6b01fSNick Piggin mntget(nd->path.mnt); 51631e6b01fSNick Piggin 51732a7991bSAl Viro unlock_rcu_walk(); 51831e6b01fSNick Piggin nd->flags &= ~LOOKUP_RCU; 51931e6b01fSNick Piggin return 0; 52019660af7SAl Viro 52119660af7SAl Viro err_child: 52231e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 52319660af7SAl Viro err_parent: 52431e6b01fSNick Piggin spin_unlock(&parent->d_lock); 52531e6b01fSNick Piggin err_root: 5265b6ca027SAl Viro if (want_root) 52731e6b01fSNick Piggin spin_unlock(&fs->lock); 52831e6b01fSNick Piggin return -ECHILD; 52931e6b01fSNick Piggin } 53031e6b01fSNick Piggin 5314ce16ef3SAl Viro static inline int d_revalidate(struct dentry *dentry, unsigned int flags) 53234286d66SNick Piggin { 5334ce16ef3SAl Viro return dentry->d_op->d_revalidate(dentry, flags); 53434286d66SNick Piggin } 53534286d66SNick Piggin 5369f1fafeeSAl Viro /** 5379f1fafeeSAl Viro * complete_walk - successful completion of path walk 5389f1fafeeSAl Viro * @nd: pointer nameidata 53939159de2SJeff Layton * 5409f1fafeeSAl Viro * If we had been in RCU mode, drop out of it and legitimize nd->path. 5419f1fafeeSAl Viro * Revalidate the final result, unless we'd already done that during 5429f1fafeeSAl Viro * the path walk or the filesystem doesn't ask for it. Return 0 on 5439f1fafeeSAl Viro * success, -error on failure. In case of failure caller does not 5449f1fafeeSAl Viro * need to drop nd->path. 54539159de2SJeff Layton */ 5469f1fafeeSAl Viro static int complete_walk(struct nameidata *nd) 54739159de2SJeff Layton { 54816c2cd71SAl Viro struct dentry *dentry = nd->path.dentry; 54939159de2SJeff Layton int status; 55039159de2SJeff Layton 5519f1fafeeSAl Viro if (nd->flags & LOOKUP_RCU) { 5529f1fafeeSAl Viro nd->flags &= ~LOOKUP_RCU; 5539f1fafeeSAl Viro if (!(nd->flags & LOOKUP_ROOT)) 5549f1fafeeSAl Viro nd->root.mnt = NULL; 5559f1fafeeSAl Viro spin_lock(&dentry->d_lock); 5569f1fafeeSAl Viro if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) { 5579f1fafeeSAl Viro spin_unlock(&dentry->d_lock); 55832a7991bSAl Viro unlock_rcu_walk(); 5599f1fafeeSAl Viro return -ECHILD; 5609f1fafeeSAl Viro } 5619f1fafeeSAl Viro BUG_ON(nd->inode != dentry->d_inode); 5629f1fafeeSAl Viro spin_unlock(&dentry->d_lock); 5639f1fafeeSAl Viro mntget(nd->path.mnt); 56432a7991bSAl Viro unlock_rcu_walk(); 5659f1fafeeSAl Viro } 5669f1fafeeSAl Viro 56716c2cd71SAl Viro if (likely(!(nd->flags & LOOKUP_JUMPED))) 56839159de2SJeff Layton return 0; 56939159de2SJeff Layton 57016c2cd71SAl Viro if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE))) 57116c2cd71SAl Viro return 0; 57216c2cd71SAl Viro 57316c2cd71SAl Viro if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT))) 57416c2cd71SAl Viro return 0; 57516c2cd71SAl Viro 57616c2cd71SAl Viro /* Note: we do not d_invalidate() */ 5774ce16ef3SAl Viro status = d_revalidate(dentry, nd->flags); 57839159de2SJeff Layton if (status > 0) 57939159de2SJeff Layton return 0; 58039159de2SJeff Layton 58116c2cd71SAl Viro if (!status) 58239159de2SJeff Layton status = -ESTALE; 58316c2cd71SAl Viro 5849f1fafeeSAl Viro path_put(&nd->path); 58539159de2SJeff Layton return status; 58639159de2SJeff Layton } 58739159de2SJeff Layton 5882a737871SAl Viro static __always_inline void set_root(struct nameidata *nd) 5892a737871SAl Viro { 590f7ad3c6bSMiklos Szeredi if (!nd->root.mnt) 591f7ad3c6bSMiklos Szeredi get_fs_root(current->fs, &nd->root); 5922a737871SAl Viro } 5932a737871SAl Viro 5946de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *); 5956de88d72SAl Viro 59631e6b01fSNick Piggin static __always_inline void set_root_rcu(struct nameidata *nd) 59731e6b01fSNick Piggin { 59831e6b01fSNick Piggin if (!nd->root.mnt) { 59931e6b01fSNick Piggin struct fs_struct *fs = current->fs; 600c28cc364SNick Piggin unsigned seq; 601c28cc364SNick Piggin 602c28cc364SNick Piggin do { 603c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 60431e6b01fSNick Piggin nd->root = fs->root; 605c1530019STim Chen nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq); 606c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 60731e6b01fSNick Piggin } 60831e6b01fSNick Piggin } 60931e6b01fSNick Piggin 610f1662356SArjan van de Ven static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link) 6111da177e4SLinus Torvalds { 61231e6b01fSNick Piggin int ret; 61331e6b01fSNick Piggin 6141da177e4SLinus Torvalds if (IS_ERR(link)) 6151da177e4SLinus Torvalds goto fail; 6161da177e4SLinus Torvalds 6171da177e4SLinus Torvalds if (*link == '/') { 6182a737871SAl Viro set_root(nd); 6191d957f9bSJan Blunck path_put(&nd->path); 6202a737871SAl Viro nd->path = nd->root; 6212a737871SAl Viro path_get(&nd->root); 62216c2cd71SAl Viro nd->flags |= LOOKUP_JUMPED; 6231da177e4SLinus Torvalds } 62431e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 625b4091d5fSChristoph Hellwig 62631e6b01fSNick Piggin ret = link_path_walk(link, nd); 62731e6b01fSNick Piggin return ret; 6281da177e4SLinus Torvalds fail: 6291d957f9bSJan Blunck path_put(&nd->path); 6301da177e4SLinus Torvalds return PTR_ERR(link); 6311da177e4SLinus Torvalds } 6321da177e4SLinus Torvalds 6331d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd) 634051d3812SIan Kent { 635051d3812SIan Kent dput(path->dentry); 6364ac91378SJan Blunck if (path->mnt != nd->path.mnt) 637051d3812SIan Kent mntput(path->mnt); 638051d3812SIan Kent } 639051d3812SIan Kent 6407b9337aaSNick Piggin static inline void path_to_nameidata(const struct path *path, 6417b9337aaSNick Piggin struct nameidata *nd) 642051d3812SIan Kent { 64331e6b01fSNick Piggin if (!(nd->flags & LOOKUP_RCU)) { 6444ac91378SJan Blunck dput(nd->path.dentry); 64531e6b01fSNick Piggin if (nd->path.mnt != path->mnt) 6464ac91378SJan Blunck mntput(nd->path.mnt); 6479a229683SHuang Shijie } 64831e6b01fSNick Piggin nd->path.mnt = path->mnt; 6494ac91378SJan Blunck nd->path.dentry = path->dentry; 650051d3812SIan Kent } 651051d3812SIan Kent 652b5fb63c1SChristoph Hellwig /* 653b5fb63c1SChristoph Hellwig * Helper to directly jump to a known parsed path from ->follow_link, 654b5fb63c1SChristoph Hellwig * caller must have taken a reference to path beforehand. 655b5fb63c1SChristoph Hellwig */ 656b5fb63c1SChristoph Hellwig void nd_jump_link(struct nameidata *nd, struct path *path) 657b5fb63c1SChristoph Hellwig { 658b5fb63c1SChristoph Hellwig path_put(&nd->path); 659b5fb63c1SChristoph Hellwig 660b5fb63c1SChristoph Hellwig nd->path = *path; 661b5fb63c1SChristoph Hellwig nd->inode = nd->path.dentry->d_inode; 662b5fb63c1SChristoph Hellwig nd->flags |= LOOKUP_JUMPED; 663b5fb63c1SChristoph Hellwig 664b5fb63c1SChristoph Hellwig BUG_ON(nd->inode->i_op->follow_link); 665b5fb63c1SChristoph Hellwig } 666b5fb63c1SChristoph Hellwig 667574197e0SAl Viro static inline void put_link(struct nameidata *nd, struct path *link, void *cookie) 668574197e0SAl Viro { 669574197e0SAl Viro struct inode *inode = link->dentry->d_inode; 6706d7b5aaeSAl Viro if (inode->i_op->put_link) 671574197e0SAl Viro inode->i_op->put_link(link->dentry, nd, cookie); 672574197e0SAl Viro path_put(link); 673574197e0SAl Viro } 674574197e0SAl Viro 675800179c9SKees Cook int sysctl_protected_symlinks __read_mostly = 1; 676800179c9SKees Cook int sysctl_protected_hardlinks __read_mostly = 1; 677800179c9SKees Cook 678800179c9SKees Cook /** 679800179c9SKees Cook * may_follow_link - Check symlink following for unsafe situations 680800179c9SKees Cook * @link: The path of the symlink 68155852635SRandy Dunlap * @nd: nameidata pathwalk data 682800179c9SKees Cook * 683800179c9SKees Cook * In the case of the sysctl_protected_symlinks sysctl being enabled, 684800179c9SKees Cook * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is 685800179c9SKees Cook * in a sticky world-writable directory. This is to protect privileged 686800179c9SKees Cook * processes from failing races against path names that may change out 687800179c9SKees Cook * from under them by way of other users creating malicious symlinks. 688800179c9SKees Cook * It will permit symlinks to be followed only when outside a sticky 689800179c9SKees Cook * world-writable directory, or when the uid of the symlink and follower 690800179c9SKees Cook * match, or when the directory owner matches the symlink's owner. 691800179c9SKees Cook * 692800179c9SKees Cook * Returns 0 if following the symlink is allowed, -ve on error. 693800179c9SKees Cook */ 694800179c9SKees Cook static inline int may_follow_link(struct path *link, struct nameidata *nd) 695800179c9SKees Cook { 696800179c9SKees Cook const struct inode *inode; 697800179c9SKees Cook const struct inode *parent; 698800179c9SKees Cook 699800179c9SKees Cook if (!sysctl_protected_symlinks) 700800179c9SKees Cook return 0; 701800179c9SKees Cook 702800179c9SKees Cook /* Allowed if owner and follower match. */ 703800179c9SKees Cook inode = link->dentry->d_inode; 70481abe27bSEric W. Biederman if (uid_eq(current_cred()->fsuid, inode->i_uid)) 705800179c9SKees Cook return 0; 706800179c9SKees Cook 707800179c9SKees Cook /* Allowed if parent directory not sticky and world-writable. */ 708800179c9SKees Cook parent = nd->path.dentry->d_inode; 709800179c9SKees Cook if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH)) 710800179c9SKees Cook return 0; 711800179c9SKees Cook 712800179c9SKees Cook /* Allowed if parent directory and link owner match. */ 71381abe27bSEric W. Biederman if (uid_eq(parent->i_uid, inode->i_uid)) 714800179c9SKees Cook return 0; 715800179c9SKees Cook 716ffd8d101SSasha Levin audit_log_link_denied("follow_link", link); 717800179c9SKees Cook path_put_conditional(link, nd); 718800179c9SKees Cook path_put(&nd->path); 719800179c9SKees Cook return -EACCES; 720800179c9SKees Cook } 721800179c9SKees Cook 722800179c9SKees Cook /** 723800179c9SKees Cook * safe_hardlink_source - Check for safe hardlink conditions 724800179c9SKees Cook * @inode: the source inode to hardlink from 725800179c9SKees Cook * 726800179c9SKees Cook * Return false if at least one of the following conditions: 727800179c9SKees Cook * - inode is not a regular file 728800179c9SKees Cook * - inode is setuid 729800179c9SKees Cook * - inode is setgid and group-exec 730800179c9SKees Cook * - access failure for read and write 731800179c9SKees Cook * 732800179c9SKees Cook * Otherwise returns true. 733800179c9SKees Cook */ 734800179c9SKees Cook static bool safe_hardlink_source(struct inode *inode) 735800179c9SKees Cook { 736800179c9SKees Cook umode_t mode = inode->i_mode; 737800179c9SKees Cook 738800179c9SKees Cook /* Special files should not get pinned to the filesystem. */ 739800179c9SKees Cook if (!S_ISREG(mode)) 740800179c9SKees Cook return false; 741800179c9SKees Cook 742800179c9SKees Cook /* Setuid files should not get pinned to the filesystem. */ 743800179c9SKees Cook if (mode & S_ISUID) 744800179c9SKees Cook return false; 745800179c9SKees Cook 746800179c9SKees Cook /* Executable setgid files should not get pinned to the filesystem. */ 747800179c9SKees Cook if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) 748800179c9SKees Cook return false; 749800179c9SKees Cook 750800179c9SKees Cook /* Hardlinking to unreadable or unwritable sources is dangerous. */ 751800179c9SKees Cook if (inode_permission(inode, MAY_READ | MAY_WRITE)) 752800179c9SKees Cook return false; 753800179c9SKees Cook 754800179c9SKees Cook return true; 755800179c9SKees Cook } 756800179c9SKees Cook 757800179c9SKees Cook /** 758800179c9SKees Cook * may_linkat - Check permissions for creating a hardlink 759800179c9SKees Cook * @link: the source to hardlink from 760800179c9SKees Cook * 761800179c9SKees Cook * Block hardlink when all of: 762800179c9SKees Cook * - sysctl_protected_hardlinks enabled 763800179c9SKees Cook * - fsuid does not match inode 764800179c9SKees Cook * - hardlink source is unsafe (see safe_hardlink_source() above) 765800179c9SKees Cook * - not CAP_FOWNER 766800179c9SKees Cook * 767800179c9SKees Cook * Returns 0 if successful, -ve on error. 768800179c9SKees Cook */ 769800179c9SKees Cook static int may_linkat(struct path *link) 770800179c9SKees Cook { 771800179c9SKees Cook const struct cred *cred; 772800179c9SKees Cook struct inode *inode; 773800179c9SKees Cook 774800179c9SKees Cook if (!sysctl_protected_hardlinks) 775800179c9SKees Cook return 0; 776800179c9SKees Cook 777800179c9SKees Cook cred = current_cred(); 778800179c9SKees Cook inode = link->dentry->d_inode; 779800179c9SKees Cook 780800179c9SKees Cook /* Source inode owner (or CAP_FOWNER) can hardlink all they like, 781800179c9SKees Cook * otherwise, it must be a safe source. 782800179c9SKees Cook */ 78381abe27bSEric W. Biederman if (uid_eq(cred->fsuid, inode->i_uid) || safe_hardlink_source(inode) || 784800179c9SKees Cook capable(CAP_FOWNER)) 785800179c9SKees Cook return 0; 786800179c9SKees Cook 787a51d9eaaSKees Cook audit_log_link_denied("linkat", link); 788800179c9SKees Cook return -EPERM; 789800179c9SKees Cook } 790800179c9SKees Cook 791def4af30SAl Viro static __always_inline int 792574197e0SAl Viro follow_link(struct path *link, struct nameidata *nd, void **p) 7931da177e4SLinus Torvalds { 7947b9337aaSNick Piggin struct dentry *dentry = link->dentry; 7956d7b5aaeSAl Viro int error; 7966d7b5aaeSAl Viro char *s; 7971da177e4SLinus Torvalds 798844a3917SAl Viro BUG_ON(nd->flags & LOOKUP_RCU); 799844a3917SAl Viro 8000e794589SAl Viro if (link->mnt == nd->path.mnt) 8010e794589SAl Viro mntget(link->mnt); 8020e794589SAl Viro 8036d7b5aaeSAl Viro error = -ELOOP; 8046d7b5aaeSAl Viro if (unlikely(current->total_link_count >= 40)) 8056d7b5aaeSAl Viro goto out_put_nd_path; 8066d7b5aaeSAl Viro 807574197e0SAl Viro cond_resched(); 808574197e0SAl Viro current->total_link_count++; 809574197e0SAl Viro 81068ac1234SAl Viro touch_atime(link); 8111da177e4SLinus Torvalds nd_set_link(nd, NULL); 812cd4e91d3SAl Viro 81336f3b4f6SAl Viro error = security_inode_follow_link(link->dentry, nd); 8146d7b5aaeSAl Viro if (error) 8156d7b5aaeSAl Viro goto out_put_nd_path; 81636f3b4f6SAl Viro 81786acdca1SAl Viro nd->last_type = LAST_BIND; 818def4af30SAl Viro *p = dentry->d_inode->i_op->follow_link(dentry, nd); 819def4af30SAl Viro error = PTR_ERR(*p); 8206d7b5aaeSAl Viro if (IS_ERR(*p)) 821408ef013SChristoph Hellwig goto out_put_nd_path; 8226d7b5aaeSAl Viro 823cc314eefSLinus Torvalds error = 0; 8246d7b5aaeSAl Viro s = nd_get_link(nd); 8256d7b5aaeSAl Viro if (s) { 8261da177e4SLinus Torvalds error = __vfs_follow_link(nd, s); 8276d7b5aaeSAl Viro if (unlikely(error)) 8286d7b5aaeSAl Viro put_link(nd, link, *p); 829b5fb63c1SChristoph Hellwig } 8306d7b5aaeSAl Viro 8316d7b5aaeSAl Viro return error; 8326d7b5aaeSAl Viro 8336d7b5aaeSAl Viro out_put_nd_path: 83498f6ef64SArnd Bergmann *p = NULL; 8356d7b5aaeSAl Viro path_put(&nd->path); 8366d7b5aaeSAl Viro path_put(link); 8371da177e4SLinus Torvalds return error; 8381da177e4SLinus Torvalds } 8391da177e4SLinus Torvalds 84031e6b01fSNick Piggin static int follow_up_rcu(struct path *path) 84131e6b01fSNick Piggin { 8420714a533SAl Viro struct mount *mnt = real_mount(path->mnt); 8430714a533SAl Viro struct mount *parent; 84431e6b01fSNick Piggin struct dentry *mountpoint; 84531e6b01fSNick Piggin 8460714a533SAl Viro parent = mnt->mnt_parent; 8470714a533SAl Viro if (&parent->mnt == path->mnt) 84831e6b01fSNick Piggin return 0; 849a73324daSAl Viro mountpoint = mnt->mnt_mountpoint; 85031e6b01fSNick Piggin path->dentry = mountpoint; 8510714a533SAl Viro path->mnt = &parent->mnt; 85231e6b01fSNick Piggin return 1; 85331e6b01fSNick Piggin } 85431e6b01fSNick Piggin 855f015f126SDavid Howells /* 856f015f126SDavid Howells * follow_up - Find the mountpoint of path's vfsmount 857f015f126SDavid Howells * 858f015f126SDavid Howells * Given a path, find the mountpoint of its source file system. 859f015f126SDavid Howells * Replace @path with the path of the mountpoint in the parent mount. 860f015f126SDavid Howells * Up is towards /. 861f015f126SDavid Howells * 862f015f126SDavid Howells * Return 1 if we went up a level and 0 if we were already at the 863f015f126SDavid Howells * root. 864f015f126SDavid Howells */ 865bab77ebfSAl Viro int follow_up(struct path *path) 8661da177e4SLinus Torvalds { 8670714a533SAl Viro struct mount *mnt = real_mount(path->mnt); 8680714a533SAl Viro struct mount *parent; 8691da177e4SLinus Torvalds struct dentry *mountpoint; 87099b7db7bSNick Piggin 871962830dfSAndi Kleen br_read_lock(&vfsmount_lock); 8720714a533SAl Viro parent = mnt->mnt_parent; 8733c0a6163SAl Viro if (parent == mnt) { 874962830dfSAndi Kleen br_read_unlock(&vfsmount_lock); 8751da177e4SLinus Torvalds return 0; 8761da177e4SLinus Torvalds } 8770714a533SAl Viro mntget(&parent->mnt); 878a73324daSAl Viro mountpoint = dget(mnt->mnt_mountpoint); 879962830dfSAndi Kleen br_read_unlock(&vfsmount_lock); 880bab77ebfSAl Viro dput(path->dentry); 881bab77ebfSAl Viro path->dentry = mountpoint; 882bab77ebfSAl Viro mntput(path->mnt); 8830714a533SAl Viro path->mnt = &parent->mnt; 8841da177e4SLinus Torvalds return 1; 8851da177e4SLinus Torvalds } 8861da177e4SLinus Torvalds 887b5c84bf6SNick Piggin /* 8889875cf80SDavid Howells * Perform an automount 8899875cf80SDavid Howells * - return -EISDIR to tell follow_managed() to stop and return the path we 8909875cf80SDavid Howells * were called with. 8911da177e4SLinus Torvalds */ 8929875cf80SDavid Howells static int follow_automount(struct path *path, unsigned flags, 8939875cf80SDavid Howells bool *need_mntput) 89431e6b01fSNick Piggin { 8959875cf80SDavid Howells struct vfsmount *mnt; 896ea5b778aSDavid Howells int err; 8979875cf80SDavid Howells 8989875cf80SDavid Howells if (!path->dentry->d_op || !path->dentry->d_op->d_automount) 8999875cf80SDavid Howells return -EREMOTE; 9009875cf80SDavid Howells 9010ec26fd0SMiklos Szeredi /* We don't want to mount if someone's just doing a stat - 9020ec26fd0SMiklos Szeredi * unless they're stat'ing a directory and appended a '/' to 9030ec26fd0SMiklos Szeredi * the name. 9040ec26fd0SMiklos Szeredi * 9050ec26fd0SMiklos Szeredi * We do, however, want to mount if someone wants to open or 9065a30d8a2SDavid Howells * create a file of any type under the mountpoint, wants to 9070ec26fd0SMiklos Szeredi * traverse through the mountpoint or wants to open the 9080ec26fd0SMiklos Szeredi * mounted directory. Also, autofs may mark negative dentries 9090ec26fd0SMiklos Szeredi * as being automount points. These will need the attentions 9100ec26fd0SMiklos Szeredi * of the daemon to instantiate them before they can be used. 9115a30d8a2SDavid Howells */ 9125a30d8a2SDavid Howells if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY | 913d94c177bSLinus Torvalds LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) && 9145a30d8a2SDavid Howells path->dentry->d_inode) 9159875cf80SDavid Howells return -EISDIR; 9160ec26fd0SMiklos Szeredi 9179875cf80SDavid Howells current->total_link_count++; 9189875cf80SDavid Howells if (current->total_link_count >= 40) 9199875cf80SDavid Howells return -ELOOP; 9209875cf80SDavid Howells 9219875cf80SDavid Howells mnt = path->dentry->d_op->d_automount(path); 9229875cf80SDavid Howells if (IS_ERR(mnt)) { 9239875cf80SDavid Howells /* 9249875cf80SDavid Howells * The filesystem is allowed to return -EISDIR here to indicate 9259875cf80SDavid Howells * it doesn't want to automount. For instance, autofs would do 9269875cf80SDavid Howells * this so that its userspace daemon can mount on this dentry. 9279875cf80SDavid Howells * 9289875cf80SDavid Howells * However, we can only permit this if it's a terminal point in 9299875cf80SDavid Howells * the path being looked up; if it wasn't then the remainder of 9309875cf80SDavid Howells * the path is inaccessible and we should say so. 9319875cf80SDavid Howells */ 93249084c3bSAl Viro if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT)) 9339875cf80SDavid Howells return -EREMOTE; 9349875cf80SDavid Howells return PTR_ERR(mnt); 93531e6b01fSNick Piggin } 936ea5b778aSDavid Howells 9379875cf80SDavid Howells if (!mnt) /* mount collision */ 9389875cf80SDavid Howells return 0; 9399875cf80SDavid Howells 9408aef1884SAl Viro if (!*need_mntput) { 9418aef1884SAl Viro /* lock_mount() may release path->mnt on error */ 9428aef1884SAl Viro mntget(path->mnt); 9438aef1884SAl Viro *need_mntput = true; 9448aef1884SAl Viro } 94519a167afSAl Viro err = finish_automount(mnt, path); 946ea5b778aSDavid Howells 947ea5b778aSDavid Howells switch (err) { 948ea5b778aSDavid Howells case -EBUSY: 949ea5b778aSDavid Howells /* Someone else made a mount here whilst we were busy */ 95019a167afSAl Viro return 0; 951ea5b778aSDavid Howells case 0: 9528aef1884SAl Viro path_put(path); 9539875cf80SDavid Howells path->mnt = mnt; 9549875cf80SDavid Howells path->dentry = dget(mnt->mnt_root); 9559875cf80SDavid Howells return 0; 95619a167afSAl Viro default: 95719a167afSAl Viro return err; 9589875cf80SDavid Howells } 95919a167afSAl Viro 960ea5b778aSDavid Howells } 9619875cf80SDavid Howells 9629875cf80SDavid Howells /* 9639875cf80SDavid Howells * Handle a dentry that is managed in some way. 964cc53ce53SDavid Howells * - Flagged for transit management (autofs) 9659875cf80SDavid Howells * - Flagged as mountpoint 9669875cf80SDavid Howells * - Flagged as automount point 9679875cf80SDavid Howells * 9689875cf80SDavid Howells * This may only be called in refwalk mode. 9699875cf80SDavid Howells * 9709875cf80SDavid Howells * Serialization is taken care of in namespace.c 9719875cf80SDavid Howells */ 9729875cf80SDavid Howells static int follow_managed(struct path *path, unsigned flags) 9739875cf80SDavid Howells { 9748aef1884SAl Viro struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */ 9759875cf80SDavid Howells unsigned managed; 9769875cf80SDavid Howells bool need_mntput = false; 9778aef1884SAl Viro int ret = 0; 9789875cf80SDavid Howells 9799875cf80SDavid Howells /* Given that we're not holding a lock here, we retain the value in a 9809875cf80SDavid Howells * local variable for each dentry as we look at it so that we don't see 9819875cf80SDavid Howells * the components of that value change under us */ 9829875cf80SDavid Howells while (managed = ACCESS_ONCE(path->dentry->d_flags), 9839875cf80SDavid Howells managed &= DCACHE_MANAGED_DENTRY, 9849875cf80SDavid Howells unlikely(managed != 0)) { 985cc53ce53SDavid Howells /* Allow the filesystem to manage the transit without i_mutex 986cc53ce53SDavid Howells * being held. */ 987cc53ce53SDavid Howells if (managed & DCACHE_MANAGE_TRANSIT) { 988cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op); 989cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op->d_manage); 9901aed3e42SAl Viro ret = path->dentry->d_op->d_manage(path->dentry, false); 991cc53ce53SDavid Howells if (ret < 0) 9928aef1884SAl Viro break; 993cc53ce53SDavid Howells } 994cc53ce53SDavid Howells 9959875cf80SDavid Howells /* Transit to a mounted filesystem. */ 9969875cf80SDavid Howells if (managed & DCACHE_MOUNTED) { 9979875cf80SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 9989875cf80SDavid Howells if (mounted) { 9999875cf80SDavid Howells dput(path->dentry); 10009875cf80SDavid Howells if (need_mntput) 1001463ffb2eSAl Viro mntput(path->mnt); 1002463ffb2eSAl Viro path->mnt = mounted; 1003463ffb2eSAl Viro path->dentry = dget(mounted->mnt_root); 10049875cf80SDavid Howells need_mntput = true; 10059875cf80SDavid Howells continue; 1006463ffb2eSAl Viro } 1007463ffb2eSAl Viro 10089875cf80SDavid Howells /* Something is mounted on this dentry in another 10099875cf80SDavid Howells * namespace and/or whatever was mounted there in this 10109875cf80SDavid Howells * namespace got unmounted before we managed to get the 10119875cf80SDavid Howells * vfsmount_lock */ 10121da177e4SLinus Torvalds } 10139875cf80SDavid Howells 10149875cf80SDavid Howells /* Handle an automount point */ 10159875cf80SDavid Howells if (managed & DCACHE_NEED_AUTOMOUNT) { 10169875cf80SDavid Howells ret = follow_automount(path, flags, &need_mntput); 10179875cf80SDavid Howells if (ret < 0) 10188aef1884SAl Viro break; 10199875cf80SDavid Howells continue; 10209875cf80SDavid Howells } 10219875cf80SDavid Howells 10229875cf80SDavid Howells /* We didn't change the current path point */ 10239875cf80SDavid Howells break; 10249875cf80SDavid Howells } 10258aef1884SAl Viro 10268aef1884SAl Viro if (need_mntput && path->mnt == mnt) 10278aef1884SAl Viro mntput(path->mnt); 10288aef1884SAl Viro if (ret == -EISDIR) 10298aef1884SAl Viro ret = 0; 1030a3fbbde7SAl Viro return ret < 0 ? ret : need_mntput; 10311da177e4SLinus Torvalds } 10321da177e4SLinus Torvalds 1033cc53ce53SDavid Howells int follow_down_one(struct path *path) 10341da177e4SLinus Torvalds { 10351da177e4SLinus Torvalds struct vfsmount *mounted; 10361da177e4SLinus Torvalds 10371c755af4SAl Viro mounted = lookup_mnt(path); 10381da177e4SLinus Torvalds if (mounted) { 10399393bd07SAl Viro dput(path->dentry); 10409393bd07SAl Viro mntput(path->mnt); 10419393bd07SAl Viro path->mnt = mounted; 10429393bd07SAl Viro path->dentry = dget(mounted->mnt_root); 10431da177e4SLinus Torvalds return 1; 10441da177e4SLinus Torvalds } 10451da177e4SLinus Torvalds return 0; 10461da177e4SLinus Torvalds } 10471da177e4SLinus Torvalds 104862a7375eSIan Kent static inline bool managed_dentry_might_block(struct dentry *dentry) 104962a7375eSIan Kent { 105062a7375eSIan Kent return (dentry->d_flags & DCACHE_MANAGE_TRANSIT && 105162a7375eSIan Kent dentry->d_op->d_manage(dentry, true) < 0); 105262a7375eSIan Kent } 105362a7375eSIan Kent 10549875cf80SDavid Howells /* 1055287548e4SAl Viro * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if 1056287548e4SAl Viro * we meet a managed dentry that would need blocking. 10579875cf80SDavid Howells */ 10589875cf80SDavid Howells static bool __follow_mount_rcu(struct nameidata *nd, struct path *path, 1059287548e4SAl Viro struct inode **inode) 10609875cf80SDavid Howells { 106162a7375eSIan Kent for (;;) { 1062c7105365SAl Viro struct mount *mounted; 106362a7375eSIan Kent /* 106462a7375eSIan Kent * Don't forget we might have a non-mountpoint managed dentry 106562a7375eSIan Kent * that wants to block transit. 106662a7375eSIan Kent */ 1067287548e4SAl Viro if (unlikely(managed_dentry_might_block(path->dentry))) 1068ab90911fSDavid Howells return false; 106962a7375eSIan Kent 107062a7375eSIan Kent if (!d_mountpoint(path->dentry)) 107162a7375eSIan Kent break; 107262a7375eSIan Kent 10739875cf80SDavid Howells mounted = __lookup_mnt(path->mnt, path->dentry, 1); 10749875cf80SDavid Howells if (!mounted) 10759875cf80SDavid Howells break; 1076c7105365SAl Viro path->mnt = &mounted->mnt; 1077c7105365SAl Viro path->dentry = mounted->mnt.mnt_root; 1078a3fbbde7SAl Viro nd->flags |= LOOKUP_JUMPED; 10799875cf80SDavid Howells nd->seq = read_seqcount_begin(&path->dentry->d_seq); 108059430262SLinus Torvalds /* 108159430262SLinus Torvalds * Update the inode too. We don't need to re-check the 108259430262SLinus Torvalds * dentry sequence number here after this d_inode read, 108359430262SLinus Torvalds * because a mount-point is always pinned. 108459430262SLinus Torvalds */ 108559430262SLinus Torvalds *inode = path->dentry->d_inode; 10869875cf80SDavid Howells } 10879875cf80SDavid Howells return true; 10889875cf80SDavid Howells } 10899875cf80SDavid Howells 1090dea39376SAl Viro static void follow_mount_rcu(struct nameidata *nd) 1091287548e4SAl Viro { 1092dea39376SAl Viro while (d_mountpoint(nd->path.dentry)) { 1093c7105365SAl Viro struct mount *mounted; 1094dea39376SAl Viro mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1); 1095287548e4SAl Viro if (!mounted) 1096287548e4SAl Viro break; 1097c7105365SAl Viro nd->path.mnt = &mounted->mnt; 1098c7105365SAl Viro nd->path.dentry = mounted->mnt.mnt_root; 1099dea39376SAl Viro nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); 1100287548e4SAl Viro } 1101287548e4SAl Viro } 1102287548e4SAl Viro 110331e6b01fSNick Piggin static int follow_dotdot_rcu(struct nameidata *nd) 110431e6b01fSNick Piggin { 110531e6b01fSNick Piggin set_root_rcu(nd); 110631e6b01fSNick Piggin 110731e6b01fSNick Piggin while (1) { 110831e6b01fSNick Piggin if (nd->path.dentry == nd->root.dentry && 110931e6b01fSNick Piggin nd->path.mnt == nd->root.mnt) { 111031e6b01fSNick Piggin break; 111131e6b01fSNick Piggin } 111231e6b01fSNick Piggin if (nd->path.dentry != nd->path.mnt->mnt_root) { 111331e6b01fSNick Piggin struct dentry *old = nd->path.dentry; 111431e6b01fSNick Piggin struct dentry *parent = old->d_parent; 111531e6b01fSNick Piggin unsigned seq; 111631e6b01fSNick Piggin 111731e6b01fSNick Piggin seq = read_seqcount_begin(&parent->d_seq); 111831e6b01fSNick Piggin if (read_seqcount_retry(&old->d_seq, nd->seq)) 1119ef7562d5SAl Viro goto failed; 112031e6b01fSNick Piggin nd->path.dentry = parent; 112131e6b01fSNick Piggin nd->seq = seq; 112231e6b01fSNick Piggin break; 112331e6b01fSNick Piggin } 112431e6b01fSNick Piggin if (!follow_up_rcu(&nd->path)) 112531e6b01fSNick Piggin break; 112631e6b01fSNick Piggin nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); 112731e6b01fSNick Piggin } 1128dea39376SAl Viro follow_mount_rcu(nd); 1129dea39376SAl Viro nd->inode = nd->path.dentry->d_inode; 113031e6b01fSNick Piggin return 0; 1131ef7562d5SAl Viro 1132ef7562d5SAl Viro failed: 1133ef7562d5SAl Viro nd->flags &= ~LOOKUP_RCU; 11345b6ca027SAl Viro if (!(nd->flags & LOOKUP_ROOT)) 1135ef7562d5SAl Viro nd->root.mnt = NULL; 113632a7991bSAl Viro unlock_rcu_walk(); 1137ef7562d5SAl Viro return -ECHILD; 113831e6b01fSNick Piggin } 113931e6b01fSNick Piggin 11409875cf80SDavid Howells /* 1141cc53ce53SDavid Howells * Follow down to the covering mount currently visible to userspace. At each 1142cc53ce53SDavid Howells * point, the filesystem owning that dentry may be queried as to whether the 1143cc53ce53SDavid Howells * caller is permitted to proceed or not. 1144cc53ce53SDavid Howells */ 11457cc90cc3SAl Viro int follow_down(struct path *path) 1146cc53ce53SDavid Howells { 1147cc53ce53SDavid Howells unsigned managed; 1148cc53ce53SDavid Howells int ret; 1149cc53ce53SDavid Howells 1150cc53ce53SDavid Howells while (managed = ACCESS_ONCE(path->dentry->d_flags), 1151cc53ce53SDavid Howells unlikely(managed & DCACHE_MANAGED_DENTRY)) { 1152cc53ce53SDavid Howells /* Allow the filesystem to manage the transit without i_mutex 1153cc53ce53SDavid Howells * being held. 1154cc53ce53SDavid Howells * 1155cc53ce53SDavid Howells * We indicate to the filesystem if someone is trying to mount 1156cc53ce53SDavid Howells * something here. This gives autofs the chance to deny anyone 1157cc53ce53SDavid Howells * other than its daemon the right to mount on its 1158cc53ce53SDavid Howells * superstructure. 1159cc53ce53SDavid Howells * 1160cc53ce53SDavid Howells * The filesystem may sleep at this point. 1161cc53ce53SDavid Howells */ 1162cc53ce53SDavid Howells if (managed & DCACHE_MANAGE_TRANSIT) { 1163cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op); 1164cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op->d_manage); 1165ab90911fSDavid Howells ret = path->dentry->d_op->d_manage( 11661aed3e42SAl Viro path->dentry, false); 1167cc53ce53SDavid Howells if (ret < 0) 1168cc53ce53SDavid Howells return ret == -EISDIR ? 0 : ret; 1169cc53ce53SDavid Howells } 1170cc53ce53SDavid Howells 1171cc53ce53SDavid Howells /* Transit to a mounted filesystem. */ 1172cc53ce53SDavid Howells if (managed & DCACHE_MOUNTED) { 1173cc53ce53SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 1174cc53ce53SDavid Howells if (!mounted) 1175cc53ce53SDavid Howells break; 1176cc53ce53SDavid Howells dput(path->dentry); 1177cc53ce53SDavid Howells mntput(path->mnt); 1178cc53ce53SDavid Howells path->mnt = mounted; 1179cc53ce53SDavid Howells path->dentry = dget(mounted->mnt_root); 1180cc53ce53SDavid Howells continue; 1181cc53ce53SDavid Howells } 1182cc53ce53SDavid Howells 1183cc53ce53SDavid Howells /* Don't handle automount points here */ 1184cc53ce53SDavid Howells break; 1185cc53ce53SDavid Howells } 1186cc53ce53SDavid Howells return 0; 1187cc53ce53SDavid Howells } 1188cc53ce53SDavid Howells 1189cc53ce53SDavid Howells /* 11909875cf80SDavid Howells * Skip to top of mountpoint pile in refwalk mode for follow_dotdot() 11919875cf80SDavid Howells */ 11929875cf80SDavid Howells static void follow_mount(struct path *path) 11939875cf80SDavid Howells { 11949875cf80SDavid Howells while (d_mountpoint(path->dentry)) { 11959875cf80SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 11969875cf80SDavid Howells if (!mounted) 11979875cf80SDavid Howells break; 11989875cf80SDavid Howells dput(path->dentry); 11999875cf80SDavid Howells mntput(path->mnt); 12009875cf80SDavid Howells path->mnt = mounted; 12019875cf80SDavid Howells path->dentry = dget(mounted->mnt_root); 12029875cf80SDavid Howells } 12039875cf80SDavid Howells } 12049875cf80SDavid Howells 120531e6b01fSNick Piggin static void follow_dotdot(struct nameidata *nd) 12061da177e4SLinus Torvalds { 12072a737871SAl Viro set_root(nd); 1208e518ddb7SAndreas Mohr 12091da177e4SLinus Torvalds while(1) { 12104ac91378SJan Blunck struct dentry *old = nd->path.dentry; 12111da177e4SLinus Torvalds 12122a737871SAl Viro if (nd->path.dentry == nd->root.dentry && 12132a737871SAl Viro nd->path.mnt == nd->root.mnt) { 12141da177e4SLinus Torvalds break; 12151da177e4SLinus Torvalds } 12164ac91378SJan Blunck if (nd->path.dentry != nd->path.mnt->mnt_root) { 12173088dd70SAl Viro /* rare case of legitimate dget_parent()... */ 12183088dd70SAl Viro nd->path.dentry = dget_parent(nd->path.dentry); 12191da177e4SLinus Torvalds dput(old); 12201da177e4SLinus Torvalds break; 12211da177e4SLinus Torvalds } 12223088dd70SAl Viro if (!follow_up(&nd->path)) 12231da177e4SLinus Torvalds break; 12241da177e4SLinus Torvalds } 122579ed0226SAl Viro follow_mount(&nd->path); 122631e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 12271da177e4SLinus Torvalds } 12281da177e4SLinus Torvalds 12291da177e4SLinus Torvalds /* 1230bad61189SMiklos Szeredi * This looks up the name in dcache, possibly revalidates the old dentry and 1231bad61189SMiklos Szeredi * allocates a new one if not found or not valid. In the need_lookup argument 1232bad61189SMiklos Szeredi * returns whether i_op->lookup is necessary. 1233bad61189SMiklos Szeredi * 1234bad61189SMiklos Szeredi * dir->d_inode->i_mutex must be held 1235baa03890SNick Piggin */ 1236bad61189SMiklos Szeredi static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir, 1237201f956eSAl Viro unsigned int flags, bool *need_lookup) 1238baa03890SNick Piggin { 1239baa03890SNick Piggin struct dentry *dentry; 1240bad61189SMiklos Szeredi int error; 1241baa03890SNick Piggin 1242bad61189SMiklos Szeredi *need_lookup = false; 1243bad61189SMiklos Szeredi dentry = d_lookup(dir, name); 1244bad61189SMiklos Szeredi if (dentry) { 1245bad61189SMiklos Szeredi if (d_need_lookup(dentry)) { 1246bad61189SMiklos Szeredi *need_lookup = true; 1247bad61189SMiklos Szeredi } else if (dentry->d_flags & DCACHE_OP_REVALIDATE) { 1248201f956eSAl Viro error = d_revalidate(dentry, flags); 1249bad61189SMiklos Szeredi if (unlikely(error <= 0)) { 1250bad61189SMiklos Szeredi if (error < 0) { 1251bad61189SMiklos Szeredi dput(dentry); 1252bad61189SMiklos Szeredi return ERR_PTR(error); 1253bad61189SMiklos Szeredi } else if (!d_invalidate(dentry)) { 1254bad61189SMiklos Szeredi dput(dentry); 1255bad61189SMiklos Szeredi dentry = NULL; 1256bad61189SMiklos Szeredi } 1257bad61189SMiklos Szeredi } 1258bad61189SMiklos Szeredi } 1259bad61189SMiklos Szeredi } 1260baa03890SNick Piggin 1261bad61189SMiklos Szeredi if (!dentry) { 1262bad61189SMiklos Szeredi dentry = d_alloc(dir, name); 1263baa03890SNick Piggin if (unlikely(!dentry)) 1264baa03890SNick Piggin return ERR_PTR(-ENOMEM); 1265baa03890SNick Piggin 1266bad61189SMiklos Szeredi *need_lookup = true; 1267baa03890SNick Piggin } 1268baa03890SNick Piggin return dentry; 1269baa03890SNick Piggin } 1270baa03890SNick Piggin 1271baa03890SNick Piggin /* 1272bad61189SMiklos Szeredi * Call i_op->lookup on the dentry. The dentry must be negative but may be 1273bad61189SMiklos Szeredi * hashed if it was pouplated with DCACHE_NEED_LOOKUP. 1274bad61189SMiklos Szeredi * 1275bad61189SMiklos Szeredi * dir->d_inode->i_mutex must be held 127644396f4bSJosef Bacik */ 1277bad61189SMiklos Szeredi static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry, 127872bd866aSAl Viro unsigned int flags) 127944396f4bSJosef Bacik { 128044396f4bSJosef Bacik struct dentry *old; 128144396f4bSJosef Bacik 128244396f4bSJosef Bacik /* Don't create child dentry for a dead directory. */ 1283bad61189SMiklos Szeredi if (unlikely(IS_DEADDIR(dir))) { 1284e188dc02SMiklos Szeredi dput(dentry); 128544396f4bSJosef Bacik return ERR_PTR(-ENOENT); 1286e188dc02SMiklos Szeredi } 128744396f4bSJosef Bacik 128872bd866aSAl Viro old = dir->i_op->lookup(dir, dentry, flags); 128944396f4bSJosef Bacik if (unlikely(old)) { 129044396f4bSJosef Bacik dput(dentry); 129144396f4bSJosef Bacik dentry = old; 129244396f4bSJosef Bacik } 129344396f4bSJosef Bacik return dentry; 129444396f4bSJosef Bacik } 129544396f4bSJosef Bacik 1296a3255546SAl Viro static struct dentry *__lookup_hash(struct qstr *name, 129772bd866aSAl Viro struct dentry *base, unsigned int flags) 1298a3255546SAl Viro { 1299bad61189SMiklos Szeredi bool need_lookup; 1300a3255546SAl Viro struct dentry *dentry; 1301a3255546SAl Viro 130272bd866aSAl Viro dentry = lookup_dcache(name, base, flags, &need_lookup); 1303bad61189SMiklos Szeredi if (!need_lookup) 1304a3255546SAl Viro return dentry; 1305bad61189SMiklos Szeredi 130672bd866aSAl Viro return lookup_real(base->d_inode, dentry, flags); 1307a3255546SAl Viro } 1308a3255546SAl Viro 130944396f4bSJosef Bacik /* 13101da177e4SLinus Torvalds * It's more convoluted than I'd like it to be, but... it's still fairly 13111da177e4SLinus Torvalds * small and for now I'd prefer to have fast path as straight as possible. 13121da177e4SLinus Torvalds * It _is_ time-critical. 13131da177e4SLinus Torvalds */ 1314697f514dSMiklos Szeredi static int lookup_fast(struct nameidata *nd, struct qstr *name, 131531e6b01fSNick Piggin struct path *path, struct inode **inode) 13161da177e4SLinus Torvalds { 13174ac91378SJan Blunck struct vfsmount *mnt = nd->path.mnt; 131831e6b01fSNick Piggin struct dentry *dentry, *parent = nd->path.dentry; 13195a18fff2SAl Viro int need_reval = 1; 13205a18fff2SAl Viro int status = 1; 13219875cf80SDavid Howells int err; 13229875cf80SDavid Howells 13233cac260aSAl Viro /* 1324b04f784eSNick Piggin * Rename seqlock is not required here because in the off chance 1325b04f784eSNick Piggin * of a false negative due to a concurrent rename, we're going to 1326b04f784eSNick Piggin * do the non-racy lookup, below. 1327b04f784eSNick Piggin */ 132831e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) { 132931e6b01fSNick Piggin unsigned seq; 133012f8ad4bSLinus Torvalds dentry = __d_lookup_rcu(parent, name, &seq, nd->inode); 13315a18fff2SAl Viro if (!dentry) 13325a18fff2SAl Viro goto unlazy; 13335a18fff2SAl Viro 133412f8ad4bSLinus Torvalds /* 133512f8ad4bSLinus Torvalds * This sequence count validates that the inode matches 133612f8ad4bSLinus Torvalds * the dentry name information from lookup. 133712f8ad4bSLinus Torvalds */ 133812f8ad4bSLinus Torvalds *inode = dentry->d_inode; 133912f8ad4bSLinus Torvalds if (read_seqcount_retry(&dentry->d_seq, seq)) 134012f8ad4bSLinus Torvalds return -ECHILD; 134112f8ad4bSLinus Torvalds 134212f8ad4bSLinus Torvalds /* 134312f8ad4bSLinus Torvalds * This sequence count validates that the parent had no 134412f8ad4bSLinus Torvalds * changes while we did the lookup of the dentry above. 134512f8ad4bSLinus Torvalds * 134612f8ad4bSLinus Torvalds * The memory barrier in read_seqcount_begin of child is 134712f8ad4bSLinus Torvalds * enough, we can use __read_seqcount_retry here. 134812f8ad4bSLinus Torvalds */ 134931e6b01fSNick Piggin if (__read_seqcount_retry(&parent->d_seq, nd->seq)) 135031e6b01fSNick Piggin return -ECHILD; 135131e6b01fSNick Piggin nd->seq = seq; 13525a18fff2SAl Viro 1353fa4ee159SMiklos Szeredi if (unlikely(d_need_lookup(dentry))) 1354fa4ee159SMiklos Szeredi goto unlazy; 135524643087SAl Viro if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) { 13564ce16ef3SAl Viro status = d_revalidate(dentry, nd->flags); 13575a18fff2SAl Viro if (unlikely(status <= 0)) { 13585a18fff2SAl Viro if (status != -ECHILD) 13595a18fff2SAl Viro need_reval = 0; 13605a18fff2SAl Viro goto unlazy; 13615a18fff2SAl Viro } 136224643087SAl Viro } 136331e6b01fSNick Piggin path->mnt = mnt; 136431e6b01fSNick Piggin path->dentry = dentry; 1365d6e9bd25SAl Viro if (unlikely(!__follow_mount_rcu(nd, path, inode))) 1366d6e9bd25SAl Viro goto unlazy; 1367d6e9bd25SAl Viro if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT)) 1368d6e9bd25SAl Viro goto unlazy; 13699875cf80SDavid Howells return 0; 13705a18fff2SAl Viro unlazy: 137119660af7SAl Viro if (unlazy_walk(nd, dentry)) 13725a18fff2SAl Viro return -ECHILD; 13735a18fff2SAl Viro } else { 137431e6b01fSNick Piggin dentry = __d_lookup(parent, name); 137524643087SAl Viro } 13765a18fff2SAl Viro 137781e6f520SAl Viro if (unlikely(!dentry)) 137881e6f520SAl Viro goto need_lookup; 13795a18fff2SAl Viro 138081e6f520SAl Viro if (unlikely(d_need_lookup(dentry))) { 138181e6f520SAl Viro dput(dentry); 138281e6f520SAl Viro goto need_lookup; 13835a18fff2SAl Viro } 138481e6f520SAl Viro 13855a18fff2SAl Viro if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval) 13864ce16ef3SAl Viro status = d_revalidate(dentry, nd->flags); 13875a18fff2SAl Viro if (unlikely(status <= 0)) { 13885a18fff2SAl Viro if (status < 0) { 13895a18fff2SAl Viro dput(dentry); 13905a18fff2SAl Viro return status; 13915a18fff2SAl Viro } 13925a18fff2SAl Viro if (!d_invalidate(dentry)) { 13935a18fff2SAl Viro dput(dentry); 139481e6f520SAl Viro goto need_lookup; 13955a18fff2SAl Viro } 13965a18fff2SAl Viro } 1397697f514dSMiklos Szeredi 13981da177e4SLinus Torvalds path->mnt = mnt; 13991da177e4SLinus Torvalds path->dentry = dentry; 14009875cf80SDavid Howells err = follow_managed(path, nd->flags); 140189312214SIan Kent if (unlikely(err < 0)) { 140289312214SIan Kent path_put_conditional(path, nd); 14039875cf80SDavid Howells return err; 140489312214SIan Kent } 1405a3fbbde7SAl Viro if (err) 1406a3fbbde7SAl Viro nd->flags |= LOOKUP_JUMPED; 140731e6b01fSNick Piggin *inode = path->dentry->d_inode; 14081da177e4SLinus Torvalds return 0; 140981e6f520SAl Viro 141081e6f520SAl Viro need_lookup: 1411697f514dSMiklos Szeredi return 1; 1412697f514dSMiklos Szeredi } 1413697f514dSMiklos Szeredi 1414697f514dSMiklos Szeredi /* Fast lookup failed, do it the slow way */ 1415697f514dSMiklos Szeredi static int lookup_slow(struct nameidata *nd, struct qstr *name, 1416697f514dSMiklos Szeredi struct path *path) 1417697f514dSMiklos Szeredi { 1418697f514dSMiklos Szeredi struct dentry *dentry, *parent; 1419697f514dSMiklos Szeredi int err; 1420697f514dSMiklos Szeredi 1421697f514dSMiklos Szeredi parent = nd->path.dentry; 142281e6f520SAl Viro BUG_ON(nd->inode != parent->d_inode); 142381e6f520SAl Viro 142481e6f520SAl Viro mutex_lock(&parent->d_inode->i_mutex); 142572bd866aSAl Viro dentry = __lookup_hash(name, parent, nd->flags); 142681e6f520SAl Viro mutex_unlock(&parent->d_inode->i_mutex); 142781e6f520SAl Viro if (IS_ERR(dentry)) 142881e6f520SAl Viro return PTR_ERR(dentry); 1429697f514dSMiklos Szeredi path->mnt = nd->path.mnt; 1430697f514dSMiklos Szeredi path->dentry = dentry; 1431697f514dSMiklos Szeredi err = follow_managed(path, nd->flags); 1432697f514dSMiklos Szeredi if (unlikely(err < 0)) { 1433697f514dSMiklos Szeredi path_put_conditional(path, nd); 1434697f514dSMiklos Szeredi return err; 1435697f514dSMiklos Szeredi } 1436697f514dSMiklos Szeredi if (err) 1437697f514dSMiklos Szeredi nd->flags |= LOOKUP_JUMPED; 1438697f514dSMiklos Szeredi return 0; 14391da177e4SLinus Torvalds } 14401da177e4SLinus Torvalds 144152094c8aSAl Viro static inline int may_lookup(struct nameidata *nd) 144252094c8aSAl Viro { 144352094c8aSAl Viro if (nd->flags & LOOKUP_RCU) { 14444ad5abb3SAl Viro int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK); 144552094c8aSAl Viro if (err != -ECHILD) 144652094c8aSAl Viro return err; 144719660af7SAl Viro if (unlazy_walk(nd, NULL)) 144852094c8aSAl Viro return -ECHILD; 144952094c8aSAl Viro } 14504ad5abb3SAl Viro return inode_permission(nd->inode, MAY_EXEC); 145152094c8aSAl Viro } 145252094c8aSAl Viro 14539856fa1bSAl Viro static inline int handle_dots(struct nameidata *nd, int type) 14549856fa1bSAl Viro { 14559856fa1bSAl Viro if (type == LAST_DOTDOT) { 14569856fa1bSAl Viro if (nd->flags & LOOKUP_RCU) { 14579856fa1bSAl Viro if (follow_dotdot_rcu(nd)) 14589856fa1bSAl Viro return -ECHILD; 14599856fa1bSAl Viro } else 14609856fa1bSAl Viro follow_dotdot(nd); 14619856fa1bSAl Viro } 14629856fa1bSAl Viro return 0; 14639856fa1bSAl Viro } 14649856fa1bSAl Viro 1465951361f9SAl Viro static void terminate_walk(struct nameidata *nd) 1466951361f9SAl Viro { 1467951361f9SAl Viro if (!(nd->flags & LOOKUP_RCU)) { 1468951361f9SAl Viro path_put(&nd->path); 1469951361f9SAl Viro } else { 1470951361f9SAl Viro nd->flags &= ~LOOKUP_RCU; 14715b6ca027SAl Viro if (!(nd->flags & LOOKUP_ROOT)) 1472951361f9SAl Viro nd->root.mnt = NULL; 147332a7991bSAl Viro unlock_rcu_walk(); 1474951361f9SAl Viro } 1475951361f9SAl Viro } 1476951361f9SAl Viro 14773ddcd056SLinus Torvalds /* 14783ddcd056SLinus Torvalds * Do we need to follow links? We _really_ want to be able 14793ddcd056SLinus Torvalds * to do this check without having to look at inode->i_op, 14803ddcd056SLinus Torvalds * so we keep a cache of "no, this doesn't need follow_link" 14813ddcd056SLinus Torvalds * for the common case. 14823ddcd056SLinus Torvalds */ 14837813b94aSLinus Torvalds static inline int should_follow_link(struct inode *inode, int follow) 14843ddcd056SLinus Torvalds { 14853ddcd056SLinus Torvalds if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) { 14863ddcd056SLinus Torvalds if (likely(inode->i_op->follow_link)) 14873ddcd056SLinus Torvalds return follow; 14883ddcd056SLinus Torvalds 14893ddcd056SLinus Torvalds /* This gets set once for the inode lifetime */ 14903ddcd056SLinus Torvalds spin_lock(&inode->i_lock); 14913ddcd056SLinus Torvalds inode->i_opflags |= IOP_NOFOLLOW; 14923ddcd056SLinus Torvalds spin_unlock(&inode->i_lock); 14933ddcd056SLinus Torvalds } 14943ddcd056SLinus Torvalds return 0; 14953ddcd056SLinus Torvalds } 14963ddcd056SLinus Torvalds 1497ce57dfc1SAl Viro static inline int walk_component(struct nameidata *nd, struct path *path, 1498ce57dfc1SAl Viro struct qstr *name, int type, int follow) 1499ce57dfc1SAl Viro { 1500ce57dfc1SAl Viro struct inode *inode; 1501ce57dfc1SAl Viro int err; 1502ce57dfc1SAl Viro /* 1503ce57dfc1SAl Viro * "." and ".." are special - ".." especially so because it has 1504ce57dfc1SAl Viro * to be able to know about the current root directory and 1505ce57dfc1SAl Viro * parent relationships. 1506ce57dfc1SAl Viro */ 1507ce57dfc1SAl Viro if (unlikely(type != LAST_NORM)) 1508ce57dfc1SAl Viro return handle_dots(nd, type); 1509697f514dSMiklos Szeredi err = lookup_fast(nd, name, path, &inode); 1510ce57dfc1SAl Viro if (unlikely(err)) { 1511697f514dSMiklos Szeredi if (err < 0) 1512697f514dSMiklos Szeredi goto out_err; 1513697f514dSMiklos Szeredi 1514697f514dSMiklos Szeredi err = lookup_slow(nd, name, path); 1515697f514dSMiklos Szeredi if (err < 0) 1516697f514dSMiklos Szeredi goto out_err; 1517697f514dSMiklos Szeredi 1518697f514dSMiklos Szeredi inode = path->dentry->d_inode; 1519ce57dfc1SAl Viro } 1520697f514dSMiklos Szeredi err = -ENOENT; 1521697f514dSMiklos Szeredi if (!inode) 1522697f514dSMiklos Szeredi goto out_path_put; 1523697f514dSMiklos Szeredi 15247813b94aSLinus Torvalds if (should_follow_link(inode, follow)) { 152519660af7SAl Viro if (nd->flags & LOOKUP_RCU) { 152619660af7SAl Viro if (unlikely(unlazy_walk(nd, path->dentry))) { 1527697f514dSMiklos Szeredi err = -ECHILD; 1528697f514dSMiklos Szeredi goto out_err; 152919660af7SAl Viro } 153019660af7SAl Viro } 1531ce57dfc1SAl Viro BUG_ON(inode != path->dentry->d_inode); 1532ce57dfc1SAl Viro return 1; 1533ce57dfc1SAl Viro } 1534ce57dfc1SAl Viro path_to_nameidata(path, nd); 1535ce57dfc1SAl Viro nd->inode = inode; 1536ce57dfc1SAl Viro return 0; 1537697f514dSMiklos Szeredi 1538697f514dSMiklos Szeredi out_path_put: 1539697f514dSMiklos Szeredi path_to_nameidata(path, nd); 1540697f514dSMiklos Szeredi out_err: 1541697f514dSMiklos Szeredi terminate_walk(nd); 1542697f514dSMiklos Szeredi return err; 1543ce57dfc1SAl Viro } 1544ce57dfc1SAl Viro 15451da177e4SLinus Torvalds /* 1546b356379aSAl Viro * This limits recursive symlink follows to 8, while 1547b356379aSAl Viro * limiting consecutive symlinks to 40. 1548b356379aSAl Viro * 1549b356379aSAl Viro * Without that kind of total limit, nasty chains of consecutive 1550b356379aSAl Viro * symlinks can cause almost arbitrarily long lookups. 1551b356379aSAl Viro */ 1552b356379aSAl Viro static inline int nested_symlink(struct path *path, struct nameidata *nd) 1553b356379aSAl Viro { 1554b356379aSAl Viro int res; 1555b356379aSAl Viro 1556b356379aSAl Viro if (unlikely(current->link_count >= MAX_NESTED_LINKS)) { 1557b356379aSAl Viro path_put_conditional(path, nd); 1558b356379aSAl Viro path_put(&nd->path); 1559b356379aSAl Viro return -ELOOP; 1560b356379aSAl Viro } 15611a4022f8SErez Zadok BUG_ON(nd->depth >= MAX_NESTED_LINKS); 1562b356379aSAl Viro 1563b356379aSAl Viro nd->depth++; 1564b356379aSAl Viro current->link_count++; 1565b356379aSAl Viro 1566b356379aSAl Viro do { 1567b356379aSAl Viro struct path link = *path; 1568b356379aSAl Viro void *cookie; 1569574197e0SAl Viro 1570574197e0SAl Viro res = follow_link(&link, nd, &cookie); 15716d7b5aaeSAl Viro if (res) 15726d7b5aaeSAl Viro break; 1573b356379aSAl Viro res = walk_component(nd, path, &nd->last, 1574b356379aSAl Viro nd->last_type, LOOKUP_FOLLOW); 1575574197e0SAl Viro put_link(nd, &link, cookie); 1576b356379aSAl Viro } while (res > 0); 1577b356379aSAl Viro 1578b356379aSAl Viro current->link_count--; 1579b356379aSAl Viro nd->depth--; 1580b356379aSAl Viro return res; 1581b356379aSAl Viro } 1582b356379aSAl Viro 1583b356379aSAl Viro /* 15843ddcd056SLinus Torvalds * We really don't want to look at inode->i_op->lookup 15853ddcd056SLinus Torvalds * when we don't have to. So we keep a cache bit in 15863ddcd056SLinus Torvalds * the inode ->i_opflags field that says "yes, we can 15873ddcd056SLinus Torvalds * do lookup on this inode". 15883ddcd056SLinus Torvalds */ 15893ddcd056SLinus Torvalds static inline int can_lookup(struct inode *inode) 15903ddcd056SLinus Torvalds { 15913ddcd056SLinus Torvalds if (likely(inode->i_opflags & IOP_LOOKUP)) 15923ddcd056SLinus Torvalds return 1; 15933ddcd056SLinus Torvalds if (likely(!inode->i_op->lookup)) 15943ddcd056SLinus Torvalds return 0; 15953ddcd056SLinus Torvalds 15963ddcd056SLinus Torvalds /* We do this once for the lifetime of the inode */ 15973ddcd056SLinus Torvalds spin_lock(&inode->i_lock); 15983ddcd056SLinus Torvalds inode->i_opflags |= IOP_LOOKUP; 15993ddcd056SLinus Torvalds spin_unlock(&inode->i_lock); 16003ddcd056SLinus Torvalds return 1; 16013ddcd056SLinus Torvalds } 16023ddcd056SLinus Torvalds 1603bfcfaa77SLinus Torvalds /* 1604bfcfaa77SLinus Torvalds * We can do the critical dentry name comparison and hashing 1605bfcfaa77SLinus Torvalds * operations one word at a time, but we are limited to: 1606bfcfaa77SLinus Torvalds * 1607bfcfaa77SLinus Torvalds * - Architectures with fast unaligned word accesses. We could 1608bfcfaa77SLinus Torvalds * do a "get_unaligned()" if this helps and is sufficiently 1609bfcfaa77SLinus Torvalds * fast. 1610bfcfaa77SLinus Torvalds * 1611bfcfaa77SLinus Torvalds * - Little-endian machines (so that we can generate the mask 1612bfcfaa77SLinus Torvalds * of low bytes efficiently). Again, we *could* do a byte 1613bfcfaa77SLinus Torvalds * swapping load on big-endian architectures if that is not 1614bfcfaa77SLinus Torvalds * expensive enough to make the optimization worthless. 1615bfcfaa77SLinus Torvalds * 1616bfcfaa77SLinus Torvalds * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we 1617bfcfaa77SLinus Torvalds * do not trap on the (extremely unlikely) case of a page 1618bfcfaa77SLinus Torvalds * crossing operation. 1619bfcfaa77SLinus Torvalds * 1620bfcfaa77SLinus Torvalds * - Furthermore, we need an efficient 64-bit compile for the 1621bfcfaa77SLinus Torvalds * 64-bit case in order to generate the "number of bytes in 1622bfcfaa77SLinus Torvalds * the final mask". Again, that could be replaced with a 1623bfcfaa77SLinus Torvalds * efficient population count instruction or similar. 1624bfcfaa77SLinus Torvalds */ 1625bfcfaa77SLinus Torvalds #ifdef CONFIG_DCACHE_WORD_ACCESS 1626bfcfaa77SLinus Torvalds 1627f68e556eSLinus Torvalds #include <asm/word-at-a-time.h> 1628bfcfaa77SLinus Torvalds 1629f68e556eSLinus Torvalds #ifdef CONFIG_64BIT 1630bfcfaa77SLinus Torvalds 1631bfcfaa77SLinus Torvalds static inline unsigned int fold_hash(unsigned long hash) 1632bfcfaa77SLinus Torvalds { 1633bfcfaa77SLinus Torvalds hash += hash >> (8*sizeof(int)); 1634bfcfaa77SLinus Torvalds return hash; 1635bfcfaa77SLinus Torvalds } 1636bfcfaa77SLinus Torvalds 1637bfcfaa77SLinus Torvalds #else /* 32-bit case */ 1638bfcfaa77SLinus Torvalds 1639bfcfaa77SLinus Torvalds #define fold_hash(x) (x) 1640bfcfaa77SLinus Torvalds 1641bfcfaa77SLinus Torvalds #endif 1642bfcfaa77SLinus Torvalds 1643bfcfaa77SLinus Torvalds unsigned int full_name_hash(const unsigned char *name, unsigned int len) 1644bfcfaa77SLinus Torvalds { 1645bfcfaa77SLinus Torvalds unsigned long a, mask; 1646bfcfaa77SLinus Torvalds unsigned long hash = 0; 1647bfcfaa77SLinus Torvalds 1648bfcfaa77SLinus Torvalds for (;;) { 1649e419b4ccSLinus Torvalds a = load_unaligned_zeropad(name); 1650bfcfaa77SLinus Torvalds if (len < sizeof(unsigned long)) 1651bfcfaa77SLinus Torvalds break; 1652bfcfaa77SLinus Torvalds hash += a; 1653f132c5beSAl Viro hash *= 9; 1654bfcfaa77SLinus Torvalds name += sizeof(unsigned long); 1655bfcfaa77SLinus Torvalds len -= sizeof(unsigned long); 1656bfcfaa77SLinus Torvalds if (!len) 1657bfcfaa77SLinus Torvalds goto done; 1658bfcfaa77SLinus Torvalds } 1659bfcfaa77SLinus Torvalds mask = ~(~0ul << len*8); 1660bfcfaa77SLinus Torvalds hash += mask & a; 1661bfcfaa77SLinus Torvalds done: 1662bfcfaa77SLinus Torvalds return fold_hash(hash); 1663bfcfaa77SLinus Torvalds } 1664bfcfaa77SLinus Torvalds EXPORT_SYMBOL(full_name_hash); 1665bfcfaa77SLinus Torvalds 1666bfcfaa77SLinus Torvalds /* 1667bfcfaa77SLinus Torvalds * Calculate the length and hash of the path component, and 1668bfcfaa77SLinus Torvalds * return the length of the component; 1669bfcfaa77SLinus Torvalds */ 1670bfcfaa77SLinus Torvalds static inline unsigned long hash_name(const char *name, unsigned int *hashp) 1671bfcfaa77SLinus Torvalds { 167236126f8fSLinus Torvalds unsigned long a, b, adata, bdata, mask, hash, len; 167336126f8fSLinus Torvalds const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS; 1674bfcfaa77SLinus Torvalds 1675bfcfaa77SLinus Torvalds hash = a = 0; 1676bfcfaa77SLinus Torvalds len = -sizeof(unsigned long); 1677bfcfaa77SLinus Torvalds do { 1678bfcfaa77SLinus Torvalds hash = (hash + a) * 9; 1679bfcfaa77SLinus Torvalds len += sizeof(unsigned long); 1680e419b4ccSLinus Torvalds a = load_unaligned_zeropad(name+len); 168136126f8fSLinus Torvalds b = a ^ REPEAT_BYTE('/'); 168236126f8fSLinus Torvalds } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants))); 1683bfcfaa77SLinus Torvalds 168436126f8fSLinus Torvalds adata = prep_zero_mask(a, adata, &constants); 168536126f8fSLinus Torvalds bdata = prep_zero_mask(b, bdata, &constants); 168636126f8fSLinus Torvalds 168736126f8fSLinus Torvalds mask = create_zero_mask(adata | bdata); 168836126f8fSLinus Torvalds 168936126f8fSLinus Torvalds hash += a & zero_bytemask(mask); 1690bfcfaa77SLinus Torvalds *hashp = fold_hash(hash); 1691bfcfaa77SLinus Torvalds 169236126f8fSLinus Torvalds return len + find_zero(mask); 1693bfcfaa77SLinus Torvalds } 1694bfcfaa77SLinus Torvalds 1695bfcfaa77SLinus Torvalds #else 1696bfcfaa77SLinus Torvalds 16970145acc2SLinus Torvalds unsigned int full_name_hash(const unsigned char *name, unsigned int len) 16980145acc2SLinus Torvalds { 16990145acc2SLinus Torvalds unsigned long hash = init_name_hash(); 17000145acc2SLinus Torvalds while (len--) 17010145acc2SLinus Torvalds hash = partial_name_hash(*name++, hash); 17020145acc2SLinus Torvalds return end_name_hash(hash); 17030145acc2SLinus Torvalds } 1704ae942ae7SLinus Torvalds EXPORT_SYMBOL(full_name_hash); 17050145acc2SLinus Torvalds 17063ddcd056SLinus Torvalds /* 1707200e9ef7SLinus Torvalds * We know there's a real path component here of at least 1708200e9ef7SLinus Torvalds * one character. 1709200e9ef7SLinus Torvalds */ 1710200e9ef7SLinus Torvalds static inline unsigned long hash_name(const char *name, unsigned int *hashp) 1711200e9ef7SLinus Torvalds { 1712200e9ef7SLinus Torvalds unsigned long hash = init_name_hash(); 1713200e9ef7SLinus Torvalds unsigned long len = 0, c; 1714200e9ef7SLinus Torvalds 1715200e9ef7SLinus Torvalds c = (unsigned char)*name; 1716200e9ef7SLinus Torvalds do { 1717200e9ef7SLinus Torvalds len++; 1718200e9ef7SLinus Torvalds hash = partial_name_hash(c, hash); 1719200e9ef7SLinus Torvalds c = (unsigned char)name[len]; 1720200e9ef7SLinus Torvalds } while (c && c != '/'); 1721200e9ef7SLinus Torvalds *hashp = end_name_hash(hash); 1722200e9ef7SLinus Torvalds return len; 1723200e9ef7SLinus Torvalds } 1724200e9ef7SLinus Torvalds 1725bfcfaa77SLinus Torvalds #endif 1726bfcfaa77SLinus Torvalds 1727200e9ef7SLinus Torvalds /* 17281da177e4SLinus Torvalds * Name resolution. 1729ea3834d9SPrasanna Meda * This is the basic name resolution function, turning a pathname into 1730ea3834d9SPrasanna Meda * the final dentry. We expect 'base' to be positive and a directory. 17311da177e4SLinus Torvalds * 1732ea3834d9SPrasanna Meda * Returns 0 and nd will have valid dentry and mnt on success. 1733ea3834d9SPrasanna Meda * Returns error and drops reference to input namei data on failure. 17341da177e4SLinus Torvalds */ 17356de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd) 17361da177e4SLinus Torvalds { 17371da177e4SLinus Torvalds struct path next; 17381da177e4SLinus Torvalds int err; 17391da177e4SLinus Torvalds 17401da177e4SLinus Torvalds while (*name=='/') 17411da177e4SLinus Torvalds name++; 17421da177e4SLinus Torvalds if (!*name) 1743086e183aSAl Viro return 0; 17441da177e4SLinus Torvalds 17451da177e4SLinus Torvalds /* At this point we know we have a real path component. */ 17461da177e4SLinus Torvalds for(;;) { 17471da177e4SLinus Torvalds struct qstr this; 1748200e9ef7SLinus Torvalds long len; 1749fe479a58SAl Viro int type; 17501da177e4SLinus Torvalds 175152094c8aSAl Viro err = may_lookup(nd); 17521da177e4SLinus Torvalds if (err) 17531da177e4SLinus Torvalds break; 17541da177e4SLinus Torvalds 1755200e9ef7SLinus Torvalds len = hash_name(name, &this.hash); 17561da177e4SLinus Torvalds this.name = name; 1757200e9ef7SLinus Torvalds this.len = len; 17581da177e4SLinus Torvalds 1759fe479a58SAl Viro type = LAST_NORM; 1760200e9ef7SLinus Torvalds if (name[0] == '.') switch (len) { 1761fe479a58SAl Viro case 2: 1762200e9ef7SLinus Torvalds if (name[1] == '.') { 1763fe479a58SAl Viro type = LAST_DOTDOT; 176416c2cd71SAl Viro nd->flags |= LOOKUP_JUMPED; 176516c2cd71SAl Viro } 1766fe479a58SAl Viro break; 1767fe479a58SAl Viro case 1: 1768fe479a58SAl Viro type = LAST_DOT; 1769fe479a58SAl Viro } 17705a202bcdSAl Viro if (likely(type == LAST_NORM)) { 17715a202bcdSAl Viro struct dentry *parent = nd->path.dentry; 177216c2cd71SAl Viro nd->flags &= ~LOOKUP_JUMPED; 17735a202bcdSAl Viro if (unlikely(parent->d_flags & DCACHE_OP_HASH)) { 17745a202bcdSAl Viro err = parent->d_op->d_hash(parent, nd->inode, 17755a202bcdSAl Viro &this); 17765a202bcdSAl Viro if (err < 0) 17775a202bcdSAl Viro break; 17785a202bcdSAl Viro } 17795a202bcdSAl Viro } 1780fe479a58SAl Viro 1781200e9ef7SLinus Torvalds if (!name[len]) 17821da177e4SLinus Torvalds goto last_component; 1783200e9ef7SLinus Torvalds /* 1784200e9ef7SLinus Torvalds * If it wasn't NUL, we know it was '/'. Skip that 1785200e9ef7SLinus Torvalds * slash, and continue until no more slashes. 1786200e9ef7SLinus Torvalds */ 1787200e9ef7SLinus Torvalds do { 1788200e9ef7SLinus Torvalds len++; 1789200e9ef7SLinus Torvalds } while (unlikely(name[len] == '/')); 1790200e9ef7SLinus Torvalds if (!name[len]) 1791b356379aSAl Viro goto last_component; 1792200e9ef7SLinus Torvalds name += len; 17931da177e4SLinus Torvalds 1794ce57dfc1SAl Viro err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW); 1795ce57dfc1SAl Viro if (err < 0) 1796ce57dfc1SAl Viro return err; 1797fe479a58SAl Viro 1798ce57dfc1SAl Viro if (err) { 1799b356379aSAl Viro err = nested_symlink(&next, nd); 18001da177e4SLinus Torvalds if (err) 1801a7472babSAl Viro return err; 180231e6b01fSNick Piggin } 18033ddcd056SLinus Torvalds if (can_lookup(nd->inode)) 18041da177e4SLinus Torvalds continue; 18053ddcd056SLinus Torvalds err = -ENOTDIR; 18063ddcd056SLinus Torvalds break; 18071da177e4SLinus Torvalds /* here ends the main loop */ 18081da177e4SLinus Torvalds 18091da177e4SLinus Torvalds last_component: 1810ce57dfc1SAl Viro nd->last = this; 1811ce57dfc1SAl Viro nd->last_type = type; 1812ce57dfc1SAl Viro return 0; 1813ce57dfc1SAl Viro } 1814951361f9SAl Viro terminate_walk(nd); 18151da177e4SLinus Torvalds return err; 18161da177e4SLinus Torvalds } 18171da177e4SLinus Torvalds 181870e9b357SAl Viro static int path_init(int dfd, const char *name, unsigned int flags, 181970e9b357SAl Viro struct nameidata *nd, struct file **fp) 182031e6b01fSNick Piggin { 182131e6b01fSNick Piggin int retval = 0; 182231e6b01fSNick Piggin 182331e6b01fSNick Piggin nd->last_type = LAST_ROOT; /* if there are only slashes... */ 182416c2cd71SAl Viro nd->flags = flags | LOOKUP_JUMPED; 182531e6b01fSNick Piggin nd->depth = 0; 18265b6ca027SAl Viro if (flags & LOOKUP_ROOT) { 18275b6ca027SAl Viro struct inode *inode = nd->root.dentry->d_inode; 182873d049a4SAl Viro if (*name) { 18295b6ca027SAl Viro if (!inode->i_op->lookup) 18305b6ca027SAl Viro return -ENOTDIR; 18315b6ca027SAl Viro retval = inode_permission(inode, MAY_EXEC); 18325b6ca027SAl Viro if (retval) 18335b6ca027SAl Viro return retval; 183473d049a4SAl Viro } 18355b6ca027SAl Viro nd->path = nd->root; 18365b6ca027SAl Viro nd->inode = inode; 18375b6ca027SAl Viro if (flags & LOOKUP_RCU) { 183832a7991bSAl Viro lock_rcu_walk(); 18395b6ca027SAl Viro nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 18405b6ca027SAl Viro } else { 18415b6ca027SAl Viro path_get(&nd->path); 18425b6ca027SAl Viro } 18435b6ca027SAl Viro return 0; 18445b6ca027SAl Viro } 18455b6ca027SAl Viro 184631e6b01fSNick Piggin nd->root.mnt = NULL; 184731e6b01fSNick Piggin 184831e6b01fSNick Piggin if (*name=='/') { 1849e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 185032a7991bSAl Viro lock_rcu_walk(); 1851e41f7d4eSAl Viro set_root_rcu(nd); 1852e41f7d4eSAl Viro } else { 1853e41f7d4eSAl Viro set_root(nd); 1854e41f7d4eSAl Viro path_get(&nd->root); 1855e41f7d4eSAl Viro } 185631e6b01fSNick Piggin nd->path = nd->root; 185731e6b01fSNick Piggin } else if (dfd == AT_FDCWD) { 1858e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 185931e6b01fSNick Piggin struct fs_struct *fs = current->fs; 1860c28cc364SNick Piggin unsigned seq; 186131e6b01fSNick Piggin 186232a7991bSAl Viro lock_rcu_walk(); 186331e6b01fSNick Piggin 1864c28cc364SNick Piggin do { 1865c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 186631e6b01fSNick Piggin nd->path = fs->pwd; 1867c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 1868c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 1869e41f7d4eSAl Viro } else { 1870e41f7d4eSAl Viro get_fs_pwd(current->fs, &nd->path); 1871e41f7d4eSAl Viro } 187231e6b01fSNick Piggin } else { 18732903ff01SAl Viro struct fd f = fdget_raw(dfd); 187431e6b01fSNick Piggin struct dentry *dentry; 187531e6b01fSNick Piggin 18762903ff01SAl Viro if (!f.file) 18772903ff01SAl Viro return -EBADF; 187831e6b01fSNick Piggin 18792903ff01SAl Viro dentry = f.file->f_path.dentry; 188031e6b01fSNick Piggin 1881f52e0c11SAl Viro if (*name) { 18822903ff01SAl Viro if (!S_ISDIR(dentry->d_inode->i_mode)) { 18832903ff01SAl Viro fdput(f); 18842903ff01SAl Viro return -ENOTDIR; 1885f52e0c11SAl Viro } 188631e6b01fSNick Piggin 18872903ff01SAl Viro retval = inode_permission(dentry->d_inode, MAY_EXEC); 18882903ff01SAl Viro if (retval) { 18892903ff01SAl Viro fdput(f); 18902903ff01SAl Viro return retval; 18912903ff01SAl Viro } 18922903ff01SAl Viro } 18932903ff01SAl Viro 18942903ff01SAl Viro nd->path = f.file->f_path; 1895e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 18962903ff01SAl Viro if (f.need_put) 18972903ff01SAl Viro *fp = f.file; 1898c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 189932a7991bSAl Viro lock_rcu_walk(); 19005590ff0dSUlrich Drepper } else { 19012903ff01SAl Viro path_get(&nd->path); 19022903ff01SAl Viro fdput(f); 19031da177e4SLinus Torvalds } 1904e41f7d4eSAl Viro } 1905e41f7d4eSAl Viro 190631e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 19079b4a9b14SAl Viro return 0; 19089b4a9b14SAl Viro } 19099b4a9b14SAl Viro 1910bd92d7feSAl Viro static inline int lookup_last(struct nameidata *nd, struct path *path) 1911bd92d7feSAl Viro { 1912bd92d7feSAl Viro if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len]) 1913bd92d7feSAl Viro nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 1914bd92d7feSAl Viro 1915bd92d7feSAl Viro nd->flags &= ~LOOKUP_PARENT; 1916bd92d7feSAl Viro return walk_component(nd, path, &nd->last, nd->last_type, 1917bd92d7feSAl Viro nd->flags & LOOKUP_FOLLOW); 1918bd92d7feSAl Viro } 1919bd92d7feSAl Viro 19209b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */ 1921ee0827cdSAl Viro static int path_lookupat(int dfd, const char *name, 19229b4a9b14SAl Viro unsigned int flags, struct nameidata *nd) 19239b4a9b14SAl Viro { 192470e9b357SAl Viro struct file *base = NULL; 1925bd92d7feSAl Viro struct path path; 1926bd92d7feSAl Viro int err; 192731e6b01fSNick Piggin 192831e6b01fSNick Piggin /* 192931e6b01fSNick Piggin * Path walking is largely split up into 2 different synchronisation 193031e6b01fSNick Piggin * schemes, rcu-walk and ref-walk (explained in 193131e6b01fSNick Piggin * Documentation/filesystems/path-lookup.txt). These share much of the 193231e6b01fSNick Piggin * path walk code, but some things particularly setup, cleanup, and 193331e6b01fSNick Piggin * following mounts are sufficiently divergent that functions are 193431e6b01fSNick Piggin * duplicated. Typically there is a function foo(), and its RCU 193531e6b01fSNick Piggin * analogue, foo_rcu(). 193631e6b01fSNick Piggin * 193731e6b01fSNick Piggin * -ECHILD is the error number of choice (just to avoid clashes) that 193831e6b01fSNick Piggin * is returned if some aspect of an rcu-walk fails. Such an error must 193931e6b01fSNick Piggin * be handled by restarting a traditional ref-walk (which will always 194031e6b01fSNick Piggin * be able to complete). 194131e6b01fSNick Piggin */ 1942bd92d7feSAl Viro err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base); 1943ee0827cdSAl Viro 1944bd92d7feSAl Viro if (unlikely(err)) 1945bd92d7feSAl Viro return err; 1946ee0827cdSAl Viro 1947ee0827cdSAl Viro current->total_link_count = 0; 1948bd92d7feSAl Viro err = link_path_walk(name, nd); 1949bd92d7feSAl Viro 1950bd92d7feSAl Viro if (!err && !(flags & LOOKUP_PARENT)) { 1951bd92d7feSAl Viro err = lookup_last(nd, &path); 1952bd92d7feSAl Viro while (err > 0) { 1953bd92d7feSAl Viro void *cookie; 1954bd92d7feSAl Viro struct path link = path; 1955800179c9SKees Cook err = may_follow_link(&link, nd); 1956800179c9SKees Cook if (unlikely(err)) 1957800179c9SKees Cook break; 1958bd92d7feSAl Viro nd->flags |= LOOKUP_PARENT; 1959574197e0SAl Viro err = follow_link(&link, nd, &cookie); 19606d7b5aaeSAl Viro if (err) 19616d7b5aaeSAl Viro break; 1962bd92d7feSAl Viro err = lookup_last(nd, &path); 1963574197e0SAl Viro put_link(nd, &link, cookie); 1964bd92d7feSAl Viro } 1965bd92d7feSAl Viro } 1966ee0827cdSAl Viro 19679f1fafeeSAl Viro if (!err) 19689f1fafeeSAl Viro err = complete_walk(nd); 1969bd92d7feSAl Viro 1970bd92d7feSAl Viro if (!err && nd->flags & LOOKUP_DIRECTORY) { 1971bd92d7feSAl Viro if (!nd->inode->i_op->lookup) { 1972bd92d7feSAl Viro path_put(&nd->path); 1973bd23a539SAl Viro err = -ENOTDIR; 1974bd92d7feSAl Viro } 1975bd92d7feSAl Viro } 197616c2cd71SAl Viro 197770e9b357SAl Viro if (base) 197870e9b357SAl Viro fput(base); 1979ee0827cdSAl Viro 19805b6ca027SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) { 198131e6b01fSNick Piggin path_put(&nd->root); 198231e6b01fSNick Piggin nd->root.mnt = NULL; 198331e6b01fSNick Piggin } 1984bd92d7feSAl Viro return err; 198531e6b01fSNick Piggin } 198631e6b01fSNick Piggin 1987ee0827cdSAl Viro static int do_path_lookup(int dfd, const char *name, 1988ee0827cdSAl Viro unsigned int flags, struct nameidata *nd) 1989ee0827cdSAl Viro { 1990ee0827cdSAl Viro int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd); 1991ee0827cdSAl Viro if (unlikely(retval == -ECHILD)) 1992ee0827cdSAl Viro retval = path_lookupat(dfd, name, flags, nd); 1993ee0827cdSAl Viro if (unlikely(retval == -ESTALE)) 1994ee0827cdSAl Viro retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd); 1995ee0827cdSAl Viro 1996f78570ddSJeff Layton if (likely(!retval)) 1997bfcec708SJeff Layton audit_inode(name, nd->path.dentry, flags & LOOKUP_PARENT); 1998170aa3d0SUlrich Drepper return retval; 19991da177e4SLinus Torvalds } 20001da177e4SLinus Torvalds 200179714f72SAl Viro /* does lookup, returns the object with parent locked */ 200279714f72SAl Viro struct dentry *kern_path_locked(const char *name, struct path *path) 20035590ff0dSUlrich Drepper { 200479714f72SAl Viro struct nameidata nd; 200579714f72SAl Viro struct dentry *d; 200679714f72SAl Viro int err = do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, &nd); 200779714f72SAl Viro if (err) 200879714f72SAl Viro return ERR_PTR(err); 200979714f72SAl Viro if (nd.last_type != LAST_NORM) { 201079714f72SAl Viro path_put(&nd.path); 201179714f72SAl Viro return ERR_PTR(-EINVAL); 201279714f72SAl Viro } 201379714f72SAl Viro mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 20141e0ea001SAl Viro d = __lookup_hash(&nd.last, nd.path.dentry, 0); 201579714f72SAl Viro if (IS_ERR(d)) { 201679714f72SAl Viro mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 201779714f72SAl Viro path_put(&nd.path); 201879714f72SAl Viro return d; 201979714f72SAl Viro } 202079714f72SAl Viro *path = nd.path; 202179714f72SAl Viro return d; 20225590ff0dSUlrich Drepper } 20235590ff0dSUlrich Drepper 2024d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path) 2025d1811465SAl Viro { 2026d1811465SAl Viro struct nameidata nd; 2027d1811465SAl Viro int res = do_path_lookup(AT_FDCWD, name, flags, &nd); 2028d1811465SAl Viro if (!res) 2029d1811465SAl Viro *path = nd.path; 2030d1811465SAl Viro return res; 2031d1811465SAl Viro } 2032d1811465SAl Viro 203316f18200SJosef 'Jeff' Sipek /** 203416f18200SJosef 'Jeff' Sipek * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair 203516f18200SJosef 'Jeff' Sipek * @dentry: pointer to dentry of the base directory 203616f18200SJosef 'Jeff' Sipek * @mnt: pointer to vfs mount of the base directory 203716f18200SJosef 'Jeff' Sipek * @name: pointer to file name 203816f18200SJosef 'Jeff' Sipek * @flags: lookup flags 2039e0a01249SAl Viro * @path: pointer to struct path to fill 204016f18200SJosef 'Jeff' Sipek */ 204116f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt, 204216f18200SJosef 'Jeff' Sipek const char *name, unsigned int flags, 2043e0a01249SAl Viro struct path *path) 204416f18200SJosef 'Jeff' Sipek { 2045e0a01249SAl Viro struct nameidata nd; 2046e0a01249SAl Viro int err; 2047e0a01249SAl Viro nd.root.dentry = dentry; 2048e0a01249SAl Viro nd.root.mnt = mnt; 2049e0a01249SAl Viro BUG_ON(flags & LOOKUP_PARENT); 20505b6ca027SAl Viro /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */ 2051e0a01249SAl Viro err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd); 2052e0a01249SAl Viro if (!err) 2053e0a01249SAl Viro *path = nd.path; 2054e0a01249SAl Viro return err; 205516f18200SJosef 'Jeff' Sipek } 205616f18200SJosef 'Jeff' Sipek 2057057f6c01SJames Morris /* 2058057f6c01SJames Morris * Restricted form of lookup. Doesn't follow links, single-component only, 2059057f6c01SJames Morris * needs parent already locked. Doesn't follow mounts. 2060057f6c01SJames Morris * SMP-safe. 2061057f6c01SJames Morris */ 2062a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd) 20631da177e4SLinus Torvalds { 206472bd866aSAl Viro return __lookup_hash(&nd->last, nd->path.dentry, nd->flags); 20651da177e4SLinus Torvalds } 20661da177e4SLinus Torvalds 2067eead1911SChristoph Hellwig /** 2068a6b91919SRandy Dunlap * lookup_one_len - filesystem helper to lookup single pathname component 2069eead1911SChristoph Hellwig * @name: pathname component to lookup 2070eead1911SChristoph Hellwig * @base: base directory to lookup from 2071eead1911SChristoph Hellwig * @len: maximum length @len should be interpreted to 2072eead1911SChristoph Hellwig * 2073a6b91919SRandy Dunlap * Note that this routine is purely a helper for filesystem usage and should 2074a6b91919SRandy Dunlap * not be called by generic code. Also note that by using this function the 2075eead1911SChristoph Hellwig * nameidata argument is passed to the filesystem methods and a filesystem 2076eead1911SChristoph Hellwig * using this helper needs to be prepared for that. 2077eead1911SChristoph Hellwig */ 2078057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len) 2079057f6c01SJames Morris { 2080057f6c01SJames Morris struct qstr this; 20816a96ba54SAl Viro unsigned int c; 2082cda309deSMiklos Szeredi int err; 2083057f6c01SJames Morris 20842f9092e1SDavid Woodhouse WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex)); 20852f9092e1SDavid Woodhouse 20866a96ba54SAl Viro this.name = name; 20876a96ba54SAl Viro this.len = len; 20880145acc2SLinus Torvalds this.hash = full_name_hash(name, len); 20896a96ba54SAl Viro if (!len) 20906a96ba54SAl Viro return ERR_PTR(-EACCES); 20916a96ba54SAl Viro 20926a96ba54SAl Viro while (len--) { 20936a96ba54SAl Viro c = *(const unsigned char *)name++; 20946a96ba54SAl Viro if (c == '/' || c == '\0') 20956a96ba54SAl Viro return ERR_PTR(-EACCES); 20966a96ba54SAl Viro } 20975a202bcdSAl Viro /* 20985a202bcdSAl Viro * See if the low-level filesystem might want 20995a202bcdSAl Viro * to use its own hash.. 21005a202bcdSAl Viro */ 21015a202bcdSAl Viro if (base->d_flags & DCACHE_OP_HASH) { 21025a202bcdSAl Viro int err = base->d_op->d_hash(base, base->d_inode, &this); 21035a202bcdSAl Viro if (err < 0) 21045a202bcdSAl Viro return ERR_PTR(err); 21055a202bcdSAl Viro } 2106eead1911SChristoph Hellwig 2107cda309deSMiklos Szeredi err = inode_permission(base->d_inode, MAY_EXEC); 2108cda309deSMiklos Szeredi if (err) 2109cda309deSMiklos Szeredi return ERR_PTR(err); 2110cda309deSMiklos Szeredi 211172bd866aSAl Viro return __lookup_hash(&this, base, 0); 2112057f6c01SJames Morris } 2113057f6c01SJames Morris 21141fa1e7f6SAndy Whitcroft int user_path_at_empty(int dfd, const char __user *name, unsigned flags, 21151fa1e7f6SAndy Whitcroft struct path *path, int *empty) 21161da177e4SLinus Torvalds { 21172d8f3038SAl Viro struct nameidata nd; 211891a27b2aSJeff Layton struct filename *tmp = getname_flags(name, flags, empty); 21191da177e4SLinus Torvalds int err = PTR_ERR(tmp); 21201da177e4SLinus Torvalds if (!IS_ERR(tmp)) { 21212d8f3038SAl Viro 21222d8f3038SAl Viro BUG_ON(flags & LOOKUP_PARENT); 21232d8f3038SAl Viro 212491a27b2aSJeff Layton err = do_path_lookup(dfd, tmp->name, flags, &nd); 21251da177e4SLinus Torvalds putname(tmp); 21262d8f3038SAl Viro if (!err) 21272d8f3038SAl Viro *path = nd.path; 21281da177e4SLinus Torvalds } 21291da177e4SLinus Torvalds return err; 21301da177e4SLinus Torvalds } 21311da177e4SLinus Torvalds 21321fa1e7f6SAndy Whitcroft int user_path_at(int dfd, const char __user *name, unsigned flags, 21331fa1e7f6SAndy Whitcroft struct path *path) 21341fa1e7f6SAndy Whitcroft { 2135f7493e5dSLinus Torvalds return user_path_at_empty(dfd, name, flags, path, NULL); 21361fa1e7f6SAndy Whitcroft } 21371fa1e7f6SAndy Whitcroft 213891a27b2aSJeff Layton static struct filename * 213991a27b2aSJeff Layton user_path_parent(int dfd, const char __user *path, struct nameidata *nd) 21402ad94ae6SAl Viro { 214191a27b2aSJeff Layton struct filename *s = getname(path); 21422ad94ae6SAl Viro int error; 21432ad94ae6SAl Viro 21442ad94ae6SAl Viro if (IS_ERR(s)) 214591a27b2aSJeff Layton return s; 21462ad94ae6SAl Viro 214791a27b2aSJeff Layton error = do_path_lookup(dfd, s->name, LOOKUP_PARENT, nd); 214891a27b2aSJeff Layton if (error) { 21492ad94ae6SAl Viro putname(s); 215091a27b2aSJeff Layton return ERR_PTR(error); 215191a27b2aSJeff Layton } 21522ad94ae6SAl Viro 215391a27b2aSJeff Layton return s; 21542ad94ae6SAl Viro } 21552ad94ae6SAl Viro 21561da177e4SLinus Torvalds /* 21571da177e4SLinus Torvalds * It's inline, so penalty for filesystems that don't use sticky bit is 21581da177e4SLinus Torvalds * minimal. 21591da177e4SLinus Torvalds */ 21601da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode) 21611da177e4SLinus Torvalds { 21628e96e3b7SEric W. Biederman kuid_t fsuid = current_fsuid(); 2163da9592edSDavid Howells 21641da177e4SLinus Torvalds if (!(dir->i_mode & S_ISVTX)) 21651da177e4SLinus Torvalds return 0; 21668e96e3b7SEric W. Biederman if (uid_eq(inode->i_uid, fsuid)) 21671da177e4SLinus Torvalds return 0; 21688e96e3b7SEric W. Biederman if (uid_eq(dir->i_uid, fsuid)) 21691da177e4SLinus Torvalds return 0; 21701a48e2acSEric W. Biederman return !inode_capable(inode, CAP_FOWNER); 21711da177e4SLinus Torvalds } 21721da177e4SLinus Torvalds 21731da177e4SLinus Torvalds /* 21741da177e4SLinus Torvalds * Check whether we can remove a link victim from directory dir, check 21751da177e4SLinus Torvalds * whether the type of victim is right. 21761da177e4SLinus Torvalds * 1. We can't do it if dir is read-only (done in permission()) 21771da177e4SLinus Torvalds * 2. We should have write and exec permissions on dir 21781da177e4SLinus Torvalds * 3. We can't remove anything from append-only dir 21791da177e4SLinus Torvalds * 4. We can't do anything with immutable dir (done in permission()) 21801da177e4SLinus Torvalds * 5. If the sticky bit on dir is set we should either 21811da177e4SLinus Torvalds * a. be owner of dir, or 21821da177e4SLinus Torvalds * b. be owner of victim, or 21831da177e4SLinus Torvalds * c. have CAP_FOWNER capability 21841da177e4SLinus Torvalds * 6. If the victim is append-only or immutable we can't do antyhing with 21851da177e4SLinus Torvalds * links pointing to it. 21861da177e4SLinus Torvalds * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. 21871da177e4SLinus Torvalds * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. 21881da177e4SLinus Torvalds * 9. We can't remove a root or mountpoint. 21891da177e4SLinus Torvalds * 10. We don't allow removal of NFS sillyrenamed files; it's handled by 21901da177e4SLinus Torvalds * nfs_async_unlink(). 21911da177e4SLinus Torvalds */ 2192858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir) 21931da177e4SLinus Torvalds { 21941da177e4SLinus Torvalds int error; 21951da177e4SLinus Torvalds 21961da177e4SLinus Torvalds if (!victim->d_inode) 21971da177e4SLinus Torvalds return -ENOENT; 21981da177e4SLinus Torvalds 21991da177e4SLinus Torvalds BUG_ON(victim->d_parent->d_inode != dir); 22004fa6b5ecSJeff Layton audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE); 22011da177e4SLinus Torvalds 2202f419a2e3SAl Viro error = inode_permission(dir, MAY_WRITE | MAY_EXEC); 22031da177e4SLinus Torvalds if (error) 22041da177e4SLinus Torvalds return error; 22051da177e4SLinus Torvalds if (IS_APPEND(dir)) 22061da177e4SLinus Torvalds return -EPERM; 22071da177e4SLinus Torvalds if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)|| 2208f9454548SHugh Dickins IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode)) 22091da177e4SLinus Torvalds return -EPERM; 22101da177e4SLinus Torvalds if (isdir) { 22111da177e4SLinus Torvalds if (!S_ISDIR(victim->d_inode->i_mode)) 22121da177e4SLinus Torvalds return -ENOTDIR; 22131da177e4SLinus Torvalds if (IS_ROOT(victim)) 22141da177e4SLinus Torvalds return -EBUSY; 22151da177e4SLinus Torvalds } else if (S_ISDIR(victim->d_inode->i_mode)) 22161da177e4SLinus Torvalds return -EISDIR; 22171da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 22181da177e4SLinus Torvalds return -ENOENT; 22191da177e4SLinus Torvalds if (victim->d_flags & DCACHE_NFSFS_RENAMED) 22201da177e4SLinus Torvalds return -EBUSY; 22211da177e4SLinus Torvalds return 0; 22221da177e4SLinus Torvalds } 22231da177e4SLinus Torvalds 22241da177e4SLinus Torvalds /* Check whether we can create an object with dentry child in directory 22251da177e4SLinus Torvalds * dir. 22261da177e4SLinus Torvalds * 1. We can't do it if child already exists (open has special treatment for 22271da177e4SLinus Torvalds * this case, but since we are inlined it's OK) 22281da177e4SLinus Torvalds * 2. We can't do it if dir is read-only (done in permission()) 22291da177e4SLinus Torvalds * 3. We should have write and exec permissions on dir 22301da177e4SLinus Torvalds * 4. We can't do it if dir is immutable (done in permission()) 22311da177e4SLinus Torvalds */ 2232a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child) 22331da177e4SLinus Torvalds { 22341da177e4SLinus Torvalds if (child->d_inode) 22351da177e4SLinus Torvalds return -EEXIST; 22361da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 22371da177e4SLinus Torvalds return -ENOENT; 2238f419a2e3SAl Viro return inode_permission(dir, MAY_WRITE | MAY_EXEC); 22391da177e4SLinus Torvalds } 22401da177e4SLinus Torvalds 22411da177e4SLinus Torvalds /* 22421da177e4SLinus Torvalds * p1 and p2 should be directories on the same fs. 22431da177e4SLinus Torvalds */ 22441da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2) 22451da177e4SLinus Torvalds { 22461da177e4SLinus Torvalds struct dentry *p; 22471da177e4SLinus Torvalds 22481da177e4SLinus Torvalds if (p1 == p2) { 2249f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 22501da177e4SLinus Torvalds return NULL; 22511da177e4SLinus Torvalds } 22521da177e4SLinus Torvalds 2253a11f3a05SArjan van de Ven mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 22541da177e4SLinus Torvalds 2255e2761a11SOGAWA Hirofumi p = d_ancestor(p2, p1); 2256e2761a11SOGAWA Hirofumi if (p) { 2257f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT); 2258f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD); 22591da177e4SLinus Torvalds return p; 22601da177e4SLinus Torvalds } 22611da177e4SLinus Torvalds 2262e2761a11SOGAWA Hirofumi p = d_ancestor(p1, p2); 2263e2761a11SOGAWA Hirofumi if (p) { 2264f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 2265f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 22661da177e4SLinus Torvalds return p; 22671da177e4SLinus Torvalds } 22681da177e4SLinus Torvalds 2269f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 2270f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 22711da177e4SLinus Torvalds return NULL; 22721da177e4SLinus Torvalds } 22731da177e4SLinus Torvalds 22741da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2) 22751da177e4SLinus Torvalds { 22761b1dcc1bSJes Sorensen mutex_unlock(&p1->d_inode->i_mutex); 22771da177e4SLinus Torvalds if (p1 != p2) { 22781b1dcc1bSJes Sorensen mutex_unlock(&p2->d_inode->i_mutex); 2279a11f3a05SArjan van de Ven mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 22801da177e4SLinus Torvalds } 22811da177e4SLinus Torvalds } 22821da177e4SLinus Torvalds 22834acdaf27SAl Viro int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode, 2284312b63fbSAl Viro bool want_excl) 22851da177e4SLinus Torvalds { 2286a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 22871da177e4SLinus Torvalds if (error) 22881da177e4SLinus Torvalds return error; 22891da177e4SLinus Torvalds 2290acfa4380SAl Viro if (!dir->i_op->create) 22911da177e4SLinus Torvalds return -EACCES; /* shouldn't it be ENOSYS? */ 22921da177e4SLinus Torvalds mode &= S_IALLUGO; 22931da177e4SLinus Torvalds mode |= S_IFREG; 22941da177e4SLinus Torvalds error = security_inode_create(dir, dentry, mode); 22951da177e4SLinus Torvalds if (error) 22961da177e4SLinus Torvalds return error; 2297312b63fbSAl Viro error = dir->i_op->create(dir, dentry, mode, want_excl); 2298a74574aaSStephen Smalley if (!error) 2299f38aa942SAmy Griffis fsnotify_create(dir, dentry); 23001da177e4SLinus Torvalds return error; 23011da177e4SLinus Torvalds } 23021da177e4SLinus Torvalds 230373d049a4SAl Viro static int may_open(struct path *path, int acc_mode, int flag) 23041da177e4SLinus Torvalds { 23053fb64190SChristoph Hellwig struct dentry *dentry = path->dentry; 23061da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 23071da177e4SLinus Torvalds int error; 23081da177e4SLinus Torvalds 2309bcda7652SAl Viro /* O_PATH? */ 2310bcda7652SAl Viro if (!acc_mode) 2311bcda7652SAl Viro return 0; 2312bcda7652SAl Viro 23131da177e4SLinus Torvalds if (!inode) 23141da177e4SLinus Torvalds return -ENOENT; 23151da177e4SLinus Torvalds 2316c8fe8f30SChristoph Hellwig switch (inode->i_mode & S_IFMT) { 2317c8fe8f30SChristoph Hellwig case S_IFLNK: 23181da177e4SLinus Torvalds return -ELOOP; 2319c8fe8f30SChristoph Hellwig case S_IFDIR: 2320c8fe8f30SChristoph Hellwig if (acc_mode & MAY_WRITE) 23211da177e4SLinus Torvalds return -EISDIR; 2322c8fe8f30SChristoph Hellwig break; 2323c8fe8f30SChristoph Hellwig case S_IFBLK: 2324c8fe8f30SChristoph Hellwig case S_IFCHR: 23253fb64190SChristoph Hellwig if (path->mnt->mnt_flags & MNT_NODEV) 23261da177e4SLinus Torvalds return -EACCES; 2327c8fe8f30SChristoph Hellwig /*FALLTHRU*/ 2328c8fe8f30SChristoph Hellwig case S_IFIFO: 2329c8fe8f30SChristoph Hellwig case S_IFSOCK: 23301da177e4SLinus Torvalds flag &= ~O_TRUNC; 2331c8fe8f30SChristoph Hellwig break; 23324a3fd211SDave Hansen } 2333b41572e9SDave Hansen 23343fb64190SChristoph Hellwig error = inode_permission(inode, acc_mode); 2335b41572e9SDave Hansen if (error) 2336b41572e9SDave Hansen return error; 23376146f0d5SMimi Zohar 23381da177e4SLinus Torvalds /* 23391da177e4SLinus Torvalds * An append-only file must be opened in append mode for writing. 23401da177e4SLinus Torvalds */ 23411da177e4SLinus Torvalds if (IS_APPEND(inode)) { 23428737c930SAl Viro if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND)) 23437715b521SAl Viro return -EPERM; 23441da177e4SLinus Torvalds if (flag & O_TRUNC) 23457715b521SAl Viro return -EPERM; 23461da177e4SLinus Torvalds } 23471da177e4SLinus Torvalds 23481da177e4SLinus Torvalds /* O_NOATIME can only be set by the owner or superuser */ 23492e149670SSerge E. Hallyn if (flag & O_NOATIME && !inode_owner_or_capable(inode)) 23507715b521SAl Viro return -EPERM; 23511da177e4SLinus Torvalds 2352f3c7691eSJ. Bruce Fields return 0; 23537715b521SAl Viro } 23547715b521SAl Viro 2355e1181ee6SJeff Layton static int handle_truncate(struct file *filp) 23567715b521SAl Viro { 2357e1181ee6SJeff Layton struct path *path = &filp->f_path; 23587715b521SAl Viro struct inode *inode = path->dentry->d_inode; 23597715b521SAl Viro int error = get_write_access(inode); 23601da177e4SLinus Torvalds if (error) 23617715b521SAl Viro return error; 23621da177e4SLinus Torvalds /* 23631da177e4SLinus Torvalds * Refuse to truncate files with mandatory locks held on them. 23641da177e4SLinus Torvalds */ 23651da177e4SLinus Torvalds error = locks_verify_locked(inode); 2366be6d3e56SKentaro Takeda if (!error) 2367ea0d3ab2STetsuo Handa error = security_path_truncate(path); 23681da177e4SLinus Torvalds if (!error) { 23697715b521SAl Viro error = do_truncate(path->dentry, 0, 2370d139d7ffSMiklos Szeredi ATTR_MTIME|ATTR_CTIME|ATTR_OPEN, 2371e1181ee6SJeff Layton filp); 23721da177e4SLinus Torvalds } 23731da177e4SLinus Torvalds put_write_access(inode); 2374acd0c935SMimi Zohar return error; 23751da177e4SLinus Torvalds } 23761da177e4SLinus Torvalds 2377d57999e1SDave Hansen static inline int open_to_namei_flags(int flag) 2378d57999e1SDave Hansen { 23798a5e929dSAl Viro if ((flag & O_ACCMODE) == 3) 23808a5e929dSAl Viro flag--; 2381d57999e1SDave Hansen return flag; 2382d57999e1SDave Hansen } 2383d57999e1SDave Hansen 2384d18e9008SMiklos Szeredi static int may_o_create(struct path *dir, struct dentry *dentry, umode_t mode) 2385d18e9008SMiklos Szeredi { 2386d18e9008SMiklos Szeredi int error = security_path_mknod(dir, dentry, mode, 0); 2387d18e9008SMiklos Szeredi if (error) 2388d18e9008SMiklos Szeredi return error; 2389d18e9008SMiklos Szeredi 2390d18e9008SMiklos Szeredi error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC); 2391d18e9008SMiklos Szeredi if (error) 2392d18e9008SMiklos Szeredi return error; 2393d18e9008SMiklos Szeredi 2394d18e9008SMiklos Szeredi return security_inode_create(dir->dentry->d_inode, dentry, mode); 2395d18e9008SMiklos Szeredi } 2396d18e9008SMiklos Szeredi 23971acf0af9SDavid Howells /* 23981acf0af9SDavid Howells * Attempt to atomically look up, create and open a file from a negative 23991acf0af9SDavid Howells * dentry. 24001acf0af9SDavid Howells * 24011acf0af9SDavid Howells * Returns 0 if successful. The file will have been created and attached to 24021acf0af9SDavid Howells * @file by the filesystem calling finish_open(). 24031acf0af9SDavid Howells * 24041acf0af9SDavid Howells * Returns 1 if the file was looked up only or didn't need creating. The 24051acf0af9SDavid Howells * caller will need to perform the open themselves. @path will have been 24061acf0af9SDavid Howells * updated to point to the new dentry. This may be negative. 24071acf0af9SDavid Howells * 24081acf0af9SDavid Howells * Returns an error code otherwise. 24091acf0af9SDavid Howells */ 24102675a4ebSAl Viro static int atomic_open(struct nameidata *nd, struct dentry *dentry, 241130d90494SAl Viro struct path *path, struct file *file, 2412015c3bbcSMiklos Szeredi const struct open_flags *op, 241364894cf8SAl Viro bool got_write, bool need_lookup, 241447237687SAl Viro int *opened) 2415d18e9008SMiklos Szeredi { 2416d18e9008SMiklos Szeredi struct inode *dir = nd->path.dentry->d_inode; 2417d18e9008SMiklos Szeredi unsigned open_flag = open_to_namei_flags(op->open_flag); 2418d18e9008SMiklos Szeredi umode_t mode; 2419d18e9008SMiklos Szeredi int error; 2420d18e9008SMiklos Szeredi int acc_mode; 2421d18e9008SMiklos Szeredi int create_error = 0; 2422d18e9008SMiklos Szeredi struct dentry *const DENTRY_NOT_SET = (void *) -1UL; 2423d18e9008SMiklos Szeredi 2424d18e9008SMiklos Szeredi BUG_ON(dentry->d_inode); 2425d18e9008SMiklos Szeredi 2426d18e9008SMiklos Szeredi /* Don't create child dentry for a dead directory. */ 2427d18e9008SMiklos Szeredi if (unlikely(IS_DEADDIR(dir))) { 24282675a4ebSAl Viro error = -ENOENT; 2429d18e9008SMiklos Szeredi goto out; 2430d18e9008SMiklos Szeredi } 2431d18e9008SMiklos Szeredi 243262b259d8SMiklos Szeredi mode = op->mode; 2433d18e9008SMiklos Szeredi if ((open_flag & O_CREAT) && !IS_POSIXACL(dir)) 2434d18e9008SMiklos Szeredi mode &= ~current_umask(); 2435d18e9008SMiklos Szeredi 2436f8310c59SAl Viro if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT)) { 2437d18e9008SMiklos Szeredi open_flag &= ~O_TRUNC; 243847237687SAl Viro *opened |= FILE_CREATED; 2439d18e9008SMiklos Szeredi } 2440d18e9008SMiklos Szeredi 2441d18e9008SMiklos Szeredi /* 2442d18e9008SMiklos Szeredi * Checking write permission is tricky, bacuse we don't know if we are 2443d18e9008SMiklos Szeredi * going to actually need it: O_CREAT opens should work as long as the 2444d18e9008SMiklos Szeredi * file exists. But checking existence breaks atomicity. The trick is 2445d18e9008SMiklos Szeredi * to check access and if not granted clear O_CREAT from the flags. 2446d18e9008SMiklos Szeredi * 2447d18e9008SMiklos Szeredi * Another problem is returing the "right" error value (e.g. for an 2448d18e9008SMiklos Szeredi * O_EXCL open we want to return EEXIST not EROFS). 2449d18e9008SMiklos Szeredi */ 245064894cf8SAl Viro if (((open_flag & (O_CREAT | O_TRUNC)) || 245164894cf8SAl Viro (open_flag & O_ACCMODE) != O_RDONLY) && unlikely(!got_write)) { 245264894cf8SAl Viro if (!(open_flag & O_CREAT)) { 2453d18e9008SMiklos Szeredi /* 2454d18e9008SMiklos Szeredi * No O_CREATE -> atomicity not a requirement -> fall 2455d18e9008SMiklos Szeredi * back to lookup + open 2456d18e9008SMiklos Szeredi */ 2457d18e9008SMiklos Szeredi goto no_open; 2458d18e9008SMiklos Szeredi } else if (open_flag & (O_EXCL | O_TRUNC)) { 2459d18e9008SMiklos Szeredi /* Fall back and fail with the right error */ 246064894cf8SAl Viro create_error = -EROFS; 2461d18e9008SMiklos Szeredi goto no_open; 2462d18e9008SMiklos Szeredi } else { 2463d18e9008SMiklos Szeredi /* No side effects, safe to clear O_CREAT */ 246464894cf8SAl Viro create_error = -EROFS; 2465d18e9008SMiklos Szeredi open_flag &= ~O_CREAT; 2466d18e9008SMiklos Szeredi } 2467d18e9008SMiklos Szeredi } 2468d18e9008SMiklos Szeredi 2469d18e9008SMiklos Szeredi if (open_flag & O_CREAT) { 247038227f78SMiklos Szeredi error = may_o_create(&nd->path, dentry, mode); 2471d18e9008SMiklos Szeredi if (error) { 2472d18e9008SMiklos Szeredi create_error = error; 2473d18e9008SMiklos Szeredi if (open_flag & O_EXCL) 2474d18e9008SMiklos Szeredi goto no_open; 2475d18e9008SMiklos Szeredi open_flag &= ~O_CREAT; 2476d18e9008SMiklos Szeredi } 2477d18e9008SMiklos Szeredi } 2478d18e9008SMiklos Szeredi 2479d18e9008SMiklos Szeredi if (nd->flags & LOOKUP_DIRECTORY) 2480d18e9008SMiklos Szeredi open_flag |= O_DIRECTORY; 2481d18e9008SMiklos Szeredi 248230d90494SAl Viro file->f_path.dentry = DENTRY_NOT_SET; 248330d90494SAl Viro file->f_path.mnt = nd->path.mnt; 248430d90494SAl Viro error = dir->i_op->atomic_open(dir, dentry, file, open_flag, mode, 248547237687SAl Viro opened); 2486d9585277SAl Viro if (error < 0) { 2487d9585277SAl Viro if (create_error && error == -ENOENT) 2488d9585277SAl Viro error = create_error; 2489d18e9008SMiklos Szeredi goto out; 2490d18e9008SMiklos Szeredi } 2491d18e9008SMiklos Szeredi 2492d18e9008SMiklos Szeredi acc_mode = op->acc_mode; 249347237687SAl Viro if (*opened & FILE_CREATED) { 2494d18e9008SMiklos Szeredi fsnotify_create(dir, dentry); 2495d18e9008SMiklos Szeredi acc_mode = MAY_OPEN; 2496d18e9008SMiklos Szeredi } 2497d18e9008SMiklos Szeredi 2498d9585277SAl Viro if (error) { /* returned 1, that is */ 249930d90494SAl Viro if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) { 25002675a4ebSAl Viro error = -EIO; 2501d18e9008SMiklos Szeredi goto out; 2502d18e9008SMiklos Szeredi } 250330d90494SAl Viro if (file->f_path.dentry) { 2504d18e9008SMiklos Szeredi dput(dentry); 250530d90494SAl Viro dentry = file->f_path.dentry; 2506d18e9008SMiklos Szeredi } 250762b2ce96SSage Weil if (create_error && dentry->d_inode == NULL) { 250862b2ce96SSage Weil error = create_error; 250962b2ce96SSage Weil goto out; 251062b2ce96SSage Weil } 2511d18e9008SMiklos Szeredi goto looked_up; 2512d18e9008SMiklos Szeredi } 2513d18e9008SMiklos Szeredi 2514d18e9008SMiklos Szeredi /* 2515d18e9008SMiklos Szeredi * We didn't have the inode before the open, so check open permission 2516d18e9008SMiklos Szeredi * here. 2517d18e9008SMiklos Szeredi */ 25182675a4ebSAl Viro error = may_open(&file->f_path, acc_mode, open_flag); 25192675a4ebSAl Viro if (error) 25202675a4ebSAl Viro fput(file); 2521d18e9008SMiklos Szeredi 2522d18e9008SMiklos Szeredi out: 2523d18e9008SMiklos Szeredi dput(dentry); 25242675a4ebSAl Viro return error; 2525d18e9008SMiklos Szeredi 2526d18e9008SMiklos Szeredi no_open: 2527d18e9008SMiklos Szeredi if (need_lookup) { 252872bd866aSAl Viro dentry = lookup_real(dir, dentry, nd->flags); 2529d18e9008SMiklos Szeredi if (IS_ERR(dentry)) 25302675a4ebSAl Viro return PTR_ERR(dentry); 2531d18e9008SMiklos Szeredi 2532d18e9008SMiklos Szeredi if (create_error) { 2533d18e9008SMiklos Szeredi int open_flag = op->open_flag; 2534d18e9008SMiklos Szeredi 25352675a4ebSAl Viro error = create_error; 2536d18e9008SMiklos Szeredi if ((open_flag & O_EXCL)) { 2537d18e9008SMiklos Szeredi if (!dentry->d_inode) 2538d18e9008SMiklos Szeredi goto out; 2539d18e9008SMiklos Szeredi } else if (!dentry->d_inode) { 2540d18e9008SMiklos Szeredi goto out; 2541d18e9008SMiklos Szeredi } else if ((open_flag & O_TRUNC) && 2542d18e9008SMiklos Szeredi S_ISREG(dentry->d_inode->i_mode)) { 2543d18e9008SMiklos Szeredi goto out; 2544d18e9008SMiklos Szeredi } 2545d18e9008SMiklos Szeredi /* will fail later, go on to get the right error */ 2546d18e9008SMiklos Szeredi } 2547d18e9008SMiklos Szeredi } 2548d18e9008SMiklos Szeredi looked_up: 2549d18e9008SMiklos Szeredi path->dentry = dentry; 2550d18e9008SMiklos Szeredi path->mnt = nd->path.mnt; 25512675a4ebSAl Viro return 1; 2552d18e9008SMiklos Szeredi } 2553d18e9008SMiklos Szeredi 255431e6b01fSNick Piggin /* 25551acf0af9SDavid Howells * Look up and maybe create and open the last component. 2556d58ffd35SMiklos Szeredi * 2557d58ffd35SMiklos Szeredi * Must be called with i_mutex held on parent. 2558d58ffd35SMiklos Szeredi * 25591acf0af9SDavid Howells * Returns 0 if the file was successfully atomically created (if necessary) and 25601acf0af9SDavid Howells * opened. In this case the file will be returned attached to @file. 25611acf0af9SDavid Howells * 25621acf0af9SDavid Howells * Returns 1 if the file was not completely opened at this time, though lookups 25631acf0af9SDavid Howells * and creations will have been performed and the dentry returned in @path will 25641acf0af9SDavid Howells * be positive upon return if O_CREAT was specified. If O_CREAT wasn't 25651acf0af9SDavid Howells * specified then a negative dentry may be returned. 25661acf0af9SDavid Howells * 25671acf0af9SDavid Howells * An error code is returned otherwise. 25681acf0af9SDavid Howells * 25691acf0af9SDavid Howells * FILE_CREATE will be set in @*opened if the dentry was created and will be 25701acf0af9SDavid Howells * cleared otherwise prior to returning. 2571d58ffd35SMiklos Szeredi */ 25722675a4ebSAl Viro static int lookup_open(struct nameidata *nd, struct path *path, 257330d90494SAl Viro struct file *file, 2574d58ffd35SMiklos Szeredi const struct open_flags *op, 257564894cf8SAl Viro bool got_write, int *opened) 2576d58ffd35SMiklos Szeredi { 2577d58ffd35SMiklos Szeredi struct dentry *dir = nd->path.dentry; 257854ef4872SMiklos Szeredi struct inode *dir_inode = dir->d_inode; 2579d58ffd35SMiklos Szeredi struct dentry *dentry; 2580d58ffd35SMiklos Szeredi int error; 258154ef4872SMiklos Szeredi bool need_lookup; 2582d58ffd35SMiklos Szeredi 258347237687SAl Viro *opened &= ~FILE_CREATED; 2584201f956eSAl Viro dentry = lookup_dcache(&nd->last, dir, nd->flags, &need_lookup); 2585d58ffd35SMiklos Szeredi if (IS_ERR(dentry)) 25862675a4ebSAl Viro return PTR_ERR(dentry); 2587d58ffd35SMiklos Szeredi 2588d18e9008SMiklos Szeredi /* Cached positive dentry: will open in f_op->open */ 2589d18e9008SMiklos Szeredi if (!need_lookup && dentry->d_inode) 2590d18e9008SMiklos Szeredi goto out_no_open; 2591d18e9008SMiklos Szeredi 2592d18e9008SMiklos Szeredi if ((nd->flags & LOOKUP_OPEN) && dir_inode->i_op->atomic_open) { 259364894cf8SAl Viro return atomic_open(nd, dentry, path, file, op, got_write, 259447237687SAl Viro need_lookup, opened); 2595d18e9008SMiklos Szeredi } 2596d18e9008SMiklos Szeredi 259754ef4872SMiklos Szeredi if (need_lookup) { 259854ef4872SMiklos Szeredi BUG_ON(dentry->d_inode); 259954ef4872SMiklos Szeredi 260072bd866aSAl Viro dentry = lookup_real(dir_inode, dentry, nd->flags); 260154ef4872SMiklos Szeredi if (IS_ERR(dentry)) 26022675a4ebSAl Viro return PTR_ERR(dentry); 260354ef4872SMiklos Szeredi } 260454ef4872SMiklos Szeredi 2605d58ffd35SMiklos Szeredi /* Negative dentry, just create the file */ 2606d58ffd35SMiklos Szeredi if (!dentry->d_inode && (op->open_flag & O_CREAT)) { 2607d58ffd35SMiklos Szeredi umode_t mode = op->mode; 2608d58ffd35SMiklos Szeredi if (!IS_POSIXACL(dir->d_inode)) 2609d58ffd35SMiklos Szeredi mode &= ~current_umask(); 2610d58ffd35SMiklos Szeredi /* 2611d58ffd35SMiklos Szeredi * This write is needed to ensure that a 2612d58ffd35SMiklos Szeredi * rw->ro transition does not occur between 2613d58ffd35SMiklos Szeredi * the time when the file is created and when 2614d58ffd35SMiklos Szeredi * a permanent write count is taken through 2615015c3bbcSMiklos Szeredi * the 'struct file' in finish_open(). 2616d58ffd35SMiklos Szeredi */ 261764894cf8SAl Viro if (!got_write) { 261864894cf8SAl Viro error = -EROFS; 2619d58ffd35SMiklos Szeredi goto out_dput; 262064894cf8SAl Viro } 262147237687SAl Viro *opened |= FILE_CREATED; 2622d58ffd35SMiklos Szeredi error = security_path_mknod(&nd->path, dentry, mode, 0); 2623d58ffd35SMiklos Szeredi if (error) 2624d58ffd35SMiklos Szeredi goto out_dput; 2625312b63fbSAl Viro error = vfs_create(dir->d_inode, dentry, mode, 2626312b63fbSAl Viro nd->flags & LOOKUP_EXCL); 2627d58ffd35SMiklos Szeredi if (error) 2628d58ffd35SMiklos Szeredi goto out_dput; 2629d58ffd35SMiklos Szeredi } 2630d18e9008SMiklos Szeredi out_no_open: 2631d58ffd35SMiklos Szeredi path->dentry = dentry; 2632d58ffd35SMiklos Szeredi path->mnt = nd->path.mnt; 26332675a4ebSAl Viro return 1; 2634d58ffd35SMiklos Szeredi 2635d58ffd35SMiklos Szeredi out_dput: 2636d58ffd35SMiklos Szeredi dput(dentry); 26372675a4ebSAl Viro return error; 2638d58ffd35SMiklos Szeredi } 2639d58ffd35SMiklos Szeredi 2640d58ffd35SMiklos Szeredi /* 2641fe2d35ffSAl Viro * Handle the last step of open() 264231e6b01fSNick Piggin */ 26432675a4ebSAl Viro static int do_last(struct nameidata *nd, struct path *path, 264430d90494SAl Viro struct file *file, const struct open_flags *op, 264547237687SAl Viro int *opened, const char *pathname) 2646fb1cc555SAl Viro { 2647a1e28038SAl Viro struct dentry *dir = nd->path.dentry; 2648ca344a89SAl Viro int open_flag = op->open_flag; 264977d660a8SMiklos Szeredi bool will_truncate = (open_flag & O_TRUNC) != 0; 265064894cf8SAl Viro bool got_write = false; 2651bcda7652SAl Viro int acc_mode = op->acc_mode; 2652a1eb3315SMiklos Szeredi struct inode *inode; 265377d660a8SMiklos Szeredi bool symlink_ok = false; 265416b1c1cdSMiklos Szeredi struct path save_parent = { .dentry = NULL, .mnt = NULL }; 265516b1c1cdSMiklos Szeredi bool retried = false; 265616c2cd71SAl Viro int error; 2657fb1cc555SAl Viro 2658c3e380b0SAl Viro nd->flags &= ~LOOKUP_PARENT; 2659c3e380b0SAl Viro nd->flags |= op->intent; 2660c3e380b0SAl Viro 26611f36f774SAl Viro switch (nd->last_type) { 26621f36f774SAl Viro case LAST_DOTDOT: 2663176306f5SNeil Brown case LAST_DOT: 2664fe2d35ffSAl Viro error = handle_dots(nd, nd->last_type); 2665fe2d35ffSAl Viro if (error) 26662675a4ebSAl Viro return error; 26671f36f774SAl Viro /* fallthrough */ 26681f36f774SAl Viro case LAST_ROOT: 26699f1fafeeSAl Viro error = complete_walk(nd); 267016c2cd71SAl Viro if (error) 26712675a4ebSAl Viro return error; 2672bfcec708SJeff Layton audit_inode(pathname, nd->path.dentry, 0); 2673ca344a89SAl Viro if (open_flag & O_CREAT) { 267416c2cd71SAl Viro error = -EISDIR; 26752675a4ebSAl Viro goto out; 2676fe2d35ffSAl Viro } 2677e83db167SMiklos Szeredi goto finish_open; 26781f36f774SAl Viro case LAST_BIND: 26799f1fafeeSAl Viro error = complete_walk(nd); 268016c2cd71SAl Viro if (error) 26812675a4ebSAl Viro return error; 2682bfcec708SJeff Layton audit_inode(pathname, dir, 0); 2683e83db167SMiklos Szeredi goto finish_open; 26841f36f774SAl Viro } 2685a2c36b45SAl Viro 2686ca344a89SAl Viro if (!(open_flag & O_CREAT)) { 2687fe2d35ffSAl Viro if (nd->last.name[nd->last.len]) 2688fe2d35ffSAl Viro nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 2689bcda7652SAl Viro if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW)) 269077d660a8SMiklos Szeredi symlink_ok = true; 2691fe2d35ffSAl Viro /* we _can_ be in RCU mode here */ 2692a1eb3315SMiklos Szeredi error = lookup_fast(nd, &nd->last, path, &inode); 269371574865SMiklos Szeredi if (likely(!error)) 269471574865SMiklos Szeredi goto finish_lookup; 269571574865SMiklos Szeredi 2696ce57dfc1SAl Viro if (error < 0) 26972675a4ebSAl Viro goto out; 2698a1eb3315SMiklos Szeredi 269937d7fffcSMiklos Szeredi BUG_ON(nd->inode != dir->d_inode); 2700b6183df7SMiklos Szeredi } else { 2701fe2d35ffSAl Viro /* create side of things */ 2702a3fbbde7SAl Viro /* 2703b6183df7SMiklos Szeredi * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED 2704b6183df7SMiklos Szeredi * has been cleared when we got to the last component we are 2705b6183df7SMiklos Szeredi * about to look up 2706a3fbbde7SAl Viro */ 27079f1fafeeSAl Viro error = complete_walk(nd); 27089f1fafeeSAl Viro if (error) 27092675a4ebSAl Viro return error; 2710fe2d35ffSAl Viro 2711bfcec708SJeff Layton audit_inode(pathname, dir, 0); 271216c2cd71SAl Viro error = -EISDIR; 27131f36f774SAl Viro /* trailing slashes? */ 271431e6b01fSNick Piggin if (nd->last.name[nd->last.len]) 27152675a4ebSAl Viro goto out; 2716b6183df7SMiklos Szeredi } 27171f36f774SAl Viro 271816b1c1cdSMiklos Szeredi retry_lookup: 271964894cf8SAl Viro if (op->open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) { 272064894cf8SAl Viro error = mnt_want_write(nd->path.mnt); 272164894cf8SAl Viro if (!error) 272264894cf8SAl Viro got_write = true; 272364894cf8SAl Viro /* 272464894cf8SAl Viro * do _not_ fail yet - we might not need that or fail with 272564894cf8SAl Viro * a different error; let lookup_open() decide; we'll be 272664894cf8SAl Viro * dropping this one anyway. 272764894cf8SAl Viro */ 272864894cf8SAl Viro } 2729a1e28038SAl Viro mutex_lock(&dir->d_inode->i_mutex); 273064894cf8SAl Viro error = lookup_open(nd, path, file, op, got_write, opened); 2731fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2732fb1cc555SAl Viro 27332675a4ebSAl Viro if (error <= 0) { 27342675a4ebSAl Viro if (error) 2735d58ffd35SMiklos Szeredi goto out; 27366c0d46c4SAl Viro 273747237687SAl Viro if ((*opened & FILE_CREATED) || 27382675a4ebSAl Viro !S_ISREG(file->f_path.dentry->d_inode->i_mode)) 273977d660a8SMiklos Szeredi will_truncate = false; 2740d18e9008SMiklos Szeredi 2741bfcec708SJeff Layton audit_inode(pathname, file->f_path.dentry, 0); 2742d18e9008SMiklos Szeredi goto opened; 2743d18e9008SMiklos Szeredi } 2744d18e9008SMiklos Szeredi 274547237687SAl Viro if (*opened & FILE_CREATED) { 27469b44f1b3SAl Viro /* Don't check for write permission, don't truncate */ 2747ca344a89SAl Viro open_flag &= ~O_TRUNC; 274877d660a8SMiklos Szeredi will_truncate = false; 2749bcda7652SAl Viro acc_mode = MAY_OPEN; 2750d58ffd35SMiklos Szeredi path_to_nameidata(path, nd); 2751e83db167SMiklos Szeredi goto finish_open_created; 2752fb1cc555SAl Viro } 2753fb1cc555SAl Viro 2754fb1cc555SAl Viro /* 27553134f37eSJeff Layton * create/update audit record if it already exists. 2756fb1cc555SAl Viro */ 27573134f37eSJeff Layton if (path->dentry->d_inode) 2758bfcec708SJeff Layton audit_inode(pathname, path->dentry, 0); 2759fb1cc555SAl Viro 2760d18e9008SMiklos Szeredi /* 2761d18e9008SMiklos Szeredi * If atomic_open() acquired write access it is dropped now due to 2762d18e9008SMiklos Szeredi * possible mount and symlink following (this might be optimized away if 2763d18e9008SMiklos Szeredi * necessary...) 2764d18e9008SMiklos Szeredi */ 276564894cf8SAl Viro if (got_write) { 2766d18e9008SMiklos Szeredi mnt_drop_write(nd->path.mnt); 276764894cf8SAl Viro got_write = false; 2768d18e9008SMiklos Szeredi } 2769d18e9008SMiklos Szeredi 2770fb1cc555SAl Viro error = -EEXIST; 2771f8310c59SAl Viro if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT)) 2772fb1cc555SAl Viro goto exit_dput; 2773fb1cc555SAl Viro 27749875cf80SDavid Howells error = follow_managed(path, nd->flags); 27759875cf80SDavid Howells if (error < 0) 2776fb1cc555SAl Viro goto exit_dput; 2777fb1cc555SAl Viro 2778a3fbbde7SAl Viro if (error) 2779a3fbbde7SAl Viro nd->flags |= LOOKUP_JUMPED; 2780a3fbbde7SAl Viro 2781decf3400SMiklos Szeredi BUG_ON(nd->flags & LOOKUP_RCU); 2782decf3400SMiklos Szeredi inode = path->dentry->d_inode; 27835f5daac1SMiklos Szeredi finish_lookup: 27845f5daac1SMiklos Szeredi /* we _can_ be in RCU mode here */ 2785fb1cc555SAl Viro error = -ENOENT; 278654c33e7fSMiklos Szeredi if (!inode) { 278754c33e7fSMiklos Szeredi path_to_nameidata(path, nd); 27882675a4ebSAl Viro goto out; 278954c33e7fSMiklos Szeredi } 27909e67f361SAl Viro 2791d45ea867SMiklos Szeredi if (should_follow_link(inode, !symlink_ok)) { 2792d45ea867SMiklos Szeredi if (nd->flags & LOOKUP_RCU) { 2793d45ea867SMiklos Szeredi if (unlikely(unlazy_walk(nd, path->dentry))) { 2794d45ea867SMiklos Szeredi error = -ECHILD; 27952675a4ebSAl Viro goto out; 2796d45ea867SMiklos Szeredi } 2797d45ea867SMiklos Szeredi } 2798d45ea867SMiklos Szeredi BUG_ON(inode != path->dentry->d_inode); 27992675a4ebSAl Viro return 1; 2800d45ea867SMiklos Szeredi } 2801fb1cc555SAl Viro 280216b1c1cdSMiklos Szeredi if ((nd->flags & LOOKUP_RCU) || nd->path.mnt != path->mnt) { 2803fb1cc555SAl Viro path_to_nameidata(path, nd); 280416b1c1cdSMiklos Szeredi } else { 280516b1c1cdSMiklos Szeredi save_parent.dentry = nd->path.dentry; 280616b1c1cdSMiklos Szeredi save_parent.mnt = mntget(path->mnt); 280716b1c1cdSMiklos Szeredi nd->path.dentry = path->dentry; 280816b1c1cdSMiklos Szeredi 280916b1c1cdSMiklos Szeredi } 2810decf3400SMiklos Szeredi nd->inode = inode; 2811a3fbbde7SAl Viro /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */ 2812a3fbbde7SAl Viro error = complete_walk(nd); 281316b1c1cdSMiklos Szeredi if (error) { 281416b1c1cdSMiklos Szeredi path_put(&save_parent); 28152675a4ebSAl Viro return error; 281616b1c1cdSMiklos Szeredi } 2817fb1cc555SAl Viro error = -EISDIR; 2818050ac841SMiklos Szeredi if ((open_flag & O_CREAT) && S_ISDIR(nd->inode->i_mode)) 28192675a4ebSAl Viro goto out; 2820af2f5542SMiklos Szeredi error = -ENOTDIR; 2821af2f5542SMiklos Szeredi if ((nd->flags & LOOKUP_DIRECTORY) && !nd->inode->i_op->lookup) 28222675a4ebSAl Viro goto out; 2823bfcec708SJeff Layton audit_inode(pathname, nd->path.dentry, 0); 2824e83db167SMiklos Szeredi finish_open: 28256c0d46c4SAl Viro if (!S_ISREG(nd->inode->i_mode)) 282677d660a8SMiklos Szeredi will_truncate = false; 28276c0d46c4SAl Viro 28280f9d1a10SAl Viro if (will_truncate) { 28290f9d1a10SAl Viro error = mnt_want_write(nd->path.mnt); 28300f9d1a10SAl Viro if (error) 28312675a4ebSAl Viro goto out; 283264894cf8SAl Viro got_write = true; 28330f9d1a10SAl Viro } 2834e83db167SMiklos Szeredi finish_open_created: 2835bcda7652SAl Viro error = may_open(&nd->path, acc_mode, open_flag); 2836ca344a89SAl Viro if (error) 28372675a4ebSAl Viro goto out; 283830d90494SAl Viro file->f_path.mnt = nd->path.mnt; 283930d90494SAl Viro error = finish_open(file, nd->path.dentry, NULL, opened); 284030d90494SAl Viro if (error) { 284130d90494SAl Viro if (error == -EOPENSTALE) 2842f60dc3dbSMiklos Szeredi goto stale_open; 2843015c3bbcSMiklos Szeredi goto out; 2844f60dc3dbSMiklos Szeredi } 2845a8277b9bSMiklos Szeredi opened: 28462675a4ebSAl Viro error = open_check_o_direct(file); 2847015c3bbcSMiklos Szeredi if (error) 2848015c3bbcSMiklos Szeredi goto exit_fput; 28492675a4ebSAl Viro error = ima_file_check(file, op->acc_mode); 2850aa4caadbSMiklos Szeredi if (error) 2851aa4caadbSMiklos Szeredi goto exit_fput; 2852aa4caadbSMiklos Szeredi 28530f9d1a10SAl Viro if (will_truncate) { 28542675a4ebSAl Viro error = handle_truncate(file); 2855aa4caadbSMiklos Szeredi if (error) 2856aa4caadbSMiklos Szeredi goto exit_fput; 28570f9d1a10SAl Viro } 2858ca344a89SAl Viro out: 285964894cf8SAl Viro if (got_write) 28600f9d1a10SAl Viro mnt_drop_write(nd->path.mnt); 286116b1c1cdSMiklos Szeredi path_put(&save_parent); 2862e276ae67SMiklos Szeredi terminate_walk(nd); 28632675a4ebSAl Viro return error; 2864fb1cc555SAl Viro 2865fb1cc555SAl Viro exit_dput: 2866fb1cc555SAl Viro path_put_conditional(path, nd); 2867ca344a89SAl Viro goto out; 2868015c3bbcSMiklos Szeredi exit_fput: 28692675a4ebSAl Viro fput(file); 28702675a4ebSAl Viro goto out; 2871015c3bbcSMiklos Szeredi 2872f60dc3dbSMiklos Szeredi stale_open: 2873f60dc3dbSMiklos Szeredi /* If no saved parent or already retried then can't retry */ 2874f60dc3dbSMiklos Szeredi if (!save_parent.dentry || retried) 2875f60dc3dbSMiklos Szeredi goto out; 2876f60dc3dbSMiklos Szeredi 2877f60dc3dbSMiklos Szeredi BUG_ON(save_parent.dentry != dir); 2878f60dc3dbSMiklos Szeredi path_put(&nd->path); 2879f60dc3dbSMiklos Szeredi nd->path = save_parent; 2880f60dc3dbSMiklos Szeredi nd->inode = dir->d_inode; 2881f60dc3dbSMiklos Szeredi save_parent.mnt = NULL; 2882f60dc3dbSMiklos Szeredi save_parent.dentry = NULL; 288364894cf8SAl Viro if (got_write) { 2884f60dc3dbSMiklos Szeredi mnt_drop_write(nd->path.mnt); 288564894cf8SAl Viro got_write = false; 2886f60dc3dbSMiklos Szeredi } 2887f60dc3dbSMiklos Szeredi retried = true; 2888f60dc3dbSMiklos Szeredi goto retry_lookup; 2889fb1cc555SAl Viro } 2890fb1cc555SAl Viro 289113aab428SAl Viro static struct file *path_openat(int dfd, const char *pathname, 289273d049a4SAl Viro struct nameidata *nd, const struct open_flags *op, int flags) 28931da177e4SLinus Torvalds { 2894fe2d35ffSAl Viro struct file *base = NULL; 289530d90494SAl Viro struct file *file; 28969850c056SAl Viro struct path path; 289747237687SAl Viro int opened = 0; 289813aab428SAl Viro int error; 289931e6b01fSNick Piggin 290030d90494SAl Viro file = get_empty_filp(); 290130d90494SAl Viro if (!file) 290231e6b01fSNick Piggin return ERR_PTR(-ENFILE); 290331e6b01fSNick Piggin 290430d90494SAl Viro file->f_flags = op->open_flag; 290531e6b01fSNick Piggin 290673d049a4SAl Viro error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base); 290731e6b01fSNick Piggin if (unlikely(error)) 29082675a4ebSAl Viro goto out; 290931e6b01fSNick Piggin 2910fe2d35ffSAl Viro current->total_link_count = 0; 291173d049a4SAl Viro error = link_path_walk(pathname, nd); 291231e6b01fSNick Piggin if (unlikely(error)) 29132675a4ebSAl Viro goto out; 29141da177e4SLinus Torvalds 29152675a4ebSAl Viro error = do_last(nd, &path, file, op, &opened, pathname); 29162675a4ebSAl Viro while (unlikely(error > 0)) { /* trailing symlink */ 29177b9337aaSNick Piggin struct path link = path; 2918def4af30SAl Viro void *cookie; 2919574197e0SAl Viro if (!(nd->flags & LOOKUP_FOLLOW)) { 292073d049a4SAl Viro path_put_conditional(&path, nd); 292173d049a4SAl Viro path_put(&nd->path); 29222675a4ebSAl Viro error = -ELOOP; 292340b39136SAl Viro break; 292440b39136SAl Viro } 2925800179c9SKees Cook error = may_follow_link(&link, nd); 2926800179c9SKees Cook if (unlikely(error)) 2927800179c9SKees Cook break; 292873d049a4SAl Viro nd->flags |= LOOKUP_PARENT; 292973d049a4SAl Viro nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL); 2930574197e0SAl Viro error = follow_link(&link, nd, &cookie); 2931c3e380b0SAl Viro if (unlikely(error)) 29322675a4ebSAl Viro break; 29332675a4ebSAl Viro error = do_last(nd, &path, file, op, &opened, pathname); 2934574197e0SAl Viro put_link(nd, &link, cookie); 2935806b681cSAl Viro } 293610fa8e62SAl Viro out: 293773d049a4SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) 293873d049a4SAl Viro path_put(&nd->root); 2939fe2d35ffSAl Viro if (base) 2940fe2d35ffSAl Viro fput(base); 29412675a4ebSAl Viro if (!(opened & FILE_OPENED)) { 29422675a4ebSAl Viro BUG_ON(!error); 294330d90494SAl Viro put_filp(file); 2944015c3bbcSMiklos Szeredi } 29452675a4ebSAl Viro if (unlikely(error)) { 29462675a4ebSAl Viro if (error == -EOPENSTALE) { 29472675a4ebSAl Viro if (flags & LOOKUP_RCU) 29482675a4ebSAl Viro error = -ECHILD; 29492675a4ebSAl Viro else 29502675a4ebSAl Viro error = -ESTALE; 29512675a4ebSAl Viro } 29522675a4ebSAl Viro file = ERR_PTR(error); 29532675a4ebSAl Viro } 29542675a4ebSAl Viro return file; 2955de459215SKirill Korotaev } 29561da177e4SLinus Torvalds 295713aab428SAl Viro struct file *do_filp_open(int dfd, const char *pathname, 295813aab428SAl Viro const struct open_flags *op, int flags) 295913aab428SAl Viro { 296073d049a4SAl Viro struct nameidata nd; 296113aab428SAl Viro struct file *filp; 296213aab428SAl Viro 296373d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU); 296413aab428SAl Viro if (unlikely(filp == ERR_PTR(-ECHILD))) 296573d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags); 296613aab428SAl Viro if (unlikely(filp == ERR_PTR(-ESTALE))) 296773d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL); 296813aab428SAl Viro return filp; 296913aab428SAl Viro } 297013aab428SAl Viro 297173d049a4SAl Viro struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt, 297273d049a4SAl Viro const char *name, const struct open_flags *op, int flags) 297373d049a4SAl Viro { 297473d049a4SAl Viro struct nameidata nd; 297573d049a4SAl Viro struct file *file; 297673d049a4SAl Viro 297773d049a4SAl Viro nd.root.mnt = mnt; 297873d049a4SAl Viro nd.root.dentry = dentry; 297973d049a4SAl Viro 298073d049a4SAl Viro flags |= LOOKUP_ROOT; 298173d049a4SAl Viro 2982bcda7652SAl Viro if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN) 298373d049a4SAl Viro return ERR_PTR(-ELOOP); 298473d049a4SAl Viro 298573d049a4SAl Viro file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU); 298673d049a4SAl Viro if (unlikely(file == ERR_PTR(-ECHILD))) 298773d049a4SAl Viro file = path_openat(-1, name, &nd, op, flags); 298873d049a4SAl Viro if (unlikely(file == ERR_PTR(-ESTALE))) 298973d049a4SAl Viro file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL); 299073d049a4SAl Viro return file; 299173d049a4SAl Viro } 299273d049a4SAl Viro 2993ed75e95dSAl Viro struct dentry *kern_path_create(int dfd, const char *pathname, struct path *path, int is_dir) 29941da177e4SLinus Torvalds { 2995c663e5d8SChristoph Hellwig struct dentry *dentry = ERR_PTR(-EEXIST); 2996ed75e95dSAl Viro struct nameidata nd; 2997c30dabfeSJan Kara int err2; 2998ed75e95dSAl Viro int error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd); 2999ed75e95dSAl Viro if (error) 3000ed75e95dSAl Viro return ERR_PTR(error); 30011da177e4SLinus Torvalds 3002c663e5d8SChristoph Hellwig /* 3003c663e5d8SChristoph Hellwig * Yucky last component or no last component at all? 3004c663e5d8SChristoph Hellwig * (foo/., foo/.., /////) 3005c663e5d8SChristoph Hellwig */ 3006ed75e95dSAl Viro if (nd.last_type != LAST_NORM) 3007ed75e95dSAl Viro goto out; 3008ed75e95dSAl Viro nd.flags &= ~LOOKUP_PARENT; 3009ed75e95dSAl Viro nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL; 3010c663e5d8SChristoph Hellwig 3011c30dabfeSJan Kara /* don't fail immediately if it's r/o, at least try to report other errors */ 3012c30dabfeSJan Kara err2 = mnt_want_write(nd.path.mnt); 3013c663e5d8SChristoph Hellwig /* 3014c663e5d8SChristoph Hellwig * Do the final lookup. 3015c663e5d8SChristoph Hellwig */ 3016ed75e95dSAl Viro mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 3017ed75e95dSAl Viro dentry = lookup_hash(&nd); 30181da177e4SLinus Torvalds if (IS_ERR(dentry)) 3019a8104a9fSAl Viro goto unlock; 3020c663e5d8SChristoph Hellwig 3021a8104a9fSAl Viro error = -EEXIST; 3022e9baf6e5SAl Viro if (dentry->d_inode) 3023a8104a9fSAl Viro goto fail; 3024c663e5d8SChristoph Hellwig /* 3025c663e5d8SChristoph Hellwig * Special case - lookup gave negative, but... we had foo/bar/ 3026c663e5d8SChristoph Hellwig * From the vfs_mknod() POV we just have a negative dentry - 3027c663e5d8SChristoph Hellwig * all is fine. Let's be bastards - you had / on the end, you've 3028c663e5d8SChristoph Hellwig * been asking for (non-existent) directory. -ENOENT for you. 3029c663e5d8SChristoph Hellwig */ 3030ed75e95dSAl Viro if (unlikely(!is_dir && nd.last.name[nd.last.len])) { 3031a8104a9fSAl Viro error = -ENOENT; 3032ed75e95dSAl Viro goto fail; 3033e9baf6e5SAl Viro } 3034c30dabfeSJan Kara if (unlikely(err2)) { 3035c30dabfeSJan Kara error = err2; 3036a8104a9fSAl Viro goto fail; 3037c30dabfeSJan Kara } 3038ed75e95dSAl Viro *path = nd.path; 3039e9baf6e5SAl Viro return dentry; 30401da177e4SLinus Torvalds fail: 3041a8104a9fSAl Viro dput(dentry); 3042a8104a9fSAl Viro dentry = ERR_PTR(error); 3043a8104a9fSAl Viro unlock: 3044dae6ad8fSAl Viro mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 3045c30dabfeSJan Kara if (!err2) 3046c30dabfeSJan Kara mnt_drop_write(nd.path.mnt); 3047ed75e95dSAl Viro out: 3048dae6ad8fSAl Viro path_put(&nd.path); 3049ed75e95dSAl Viro return dentry; 3050dae6ad8fSAl Viro } 3051dae6ad8fSAl Viro EXPORT_SYMBOL(kern_path_create); 3052dae6ad8fSAl Viro 3053921a1650SAl Viro void done_path_create(struct path *path, struct dentry *dentry) 3054921a1650SAl Viro { 3055921a1650SAl Viro dput(dentry); 3056921a1650SAl Viro mutex_unlock(&path->dentry->d_inode->i_mutex); 3057a8104a9fSAl Viro mnt_drop_write(path->mnt); 3058921a1650SAl Viro path_put(path); 3059921a1650SAl Viro } 3060921a1650SAl Viro EXPORT_SYMBOL(done_path_create); 3061921a1650SAl Viro 3062dae6ad8fSAl Viro struct dentry *user_path_create(int dfd, const char __user *pathname, struct path *path, int is_dir) 3063dae6ad8fSAl Viro { 306491a27b2aSJeff Layton struct filename *tmp = getname(pathname); 3065dae6ad8fSAl Viro struct dentry *res; 3066dae6ad8fSAl Viro if (IS_ERR(tmp)) 3067dae6ad8fSAl Viro return ERR_CAST(tmp); 306891a27b2aSJeff Layton res = kern_path_create(dfd, tmp->name, path, is_dir); 3069dae6ad8fSAl Viro putname(tmp); 3070dae6ad8fSAl Viro return res; 3071dae6ad8fSAl Viro } 3072dae6ad8fSAl Viro EXPORT_SYMBOL(user_path_create); 3073dae6ad8fSAl Viro 30741a67aafbSAl Viro int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev) 30751da177e4SLinus Torvalds { 3076a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 30771da177e4SLinus Torvalds 30781da177e4SLinus Torvalds if (error) 30791da177e4SLinus Torvalds return error; 30801da177e4SLinus Torvalds 3081975d6b39SEric W. Biederman if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD)) 30821da177e4SLinus Torvalds return -EPERM; 30831da177e4SLinus Torvalds 3084acfa4380SAl Viro if (!dir->i_op->mknod) 30851da177e4SLinus Torvalds return -EPERM; 30861da177e4SLinus Torvalds 308708ce5f16SSerge E. Hallyn error = devcgroup_inode_mknod(mode, dev); 308808ce5f16SSerge E. Hallyn if (error) 308908ce5f16SSerge E. Hallyn return error; 309008ce5f16SSerge E. Hallyn 30911da177e4SLinus Torvalds error = security_inode_mknod(dir, dentry, mode, dev); 30921da177e4SLinus Torvalds if (error) 30931da177e4SLinus Torvalds return error; 30941da177e4SLinus Torvalds 30951da177e4SLinus Torvalds error = dir->i_op->mknod(dir, dentry, mode, dev); 3096a74574aaSStephen Smalley if (!error) 3097f38aa942SAmy Griffis fsnotify_create(dir, dentry); 30981da177e4SLinus Torvalds return error; 30991da177e4SLinus Torvalds } 31001da177e4SLinus Torvalds 3101f69aac00SAl Viro static int may_mknod(umode_t mode) 3102463c3197SDave Hansen { 3103463c3197SDave Hansen switch (mode & S_IFMT) { 3104463c3197SDave Hansen case S_IFREG: 3105463c3197SDave Hansen case S_IFCHR: 3106463c3197SDave Hansen case S_IFBLK: 3107463c3197SDave Hansen case S_IFIFO: 3108463c3197SDave Hansen case S_IFSOCK: 3109463c3197SDave Hansen case 0: /* zero mode translates to S_IFREG */ 3110463c3197SDave Hansen return 0; 3111463c3197SDave Hansen case S_IFDIR: 3112463c3197SDave Hansen return -EPERM; 3113463c3197SDave Hansen default: 3114463c3197SDave Hansen return -EINVAL; 3115463c3197SDave Hansen } 3116463c3197SDave Hansen } 3117463c3197SDave Hansen 31188208a22bSAl Viro SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode, 31192e4d0924SHeiko Carstens unsigned, dev) 31201da177e4SLinus Torvalds { 31211da177e4SLinus Torvalds struct dentry *dentry; 3122dae6ad8fSAl Viro struct path path; 3123dae6ad8fSAl Viro int error; 31241da177e4SLinus Torvalds 31258e4bfca1SAl Viro error = may_mknod(mode); 31268e4bfca1SAl Viro if (error) 31278e4bfca1SAl Viro return error; 31281da177e4SLinus Torvalds 3129dae6ad8fSAl Viro dentry = user_path_create(dfd, filename, &path, 0); 3130dae6ad8fSAl Viro if (IS_ERR(dentry)) 3131dae6ad8fSAl Viro return PTR_ERR(dentry); 31322ad94ae6SAl Viro 3133dae6ad8fSAl Viro if (!IS_POSIXACL(path.dentry->d_inode)) 3134ce3b0f8dSAl Viro mode &= ~current_umask(); 3135dae6ad8fSAl Viro error = security_path_mknod(&path, dentry, mode, dev); 3136be6d3e56SKentaro Takeda if (error) 3137a8104a9fSAl Viro goto out; 31381da177e4SLinus Torvalds switch (mode & S_IFMT) { 31391da177e4SLinus Torvalds case 0: case S_IFREG: 3140312b63fbSAl Viro error = vfs_create(path.dentry->d_inode,dentry,mode,true); 31411da177e4SLinus Torvalds break; 31421da177e4SLinus Torvalds case S_IFCHR: case S_IFBLK: 3143dae6ad8fSAl Viro error = vfs_mknod(path.dentry->d_inode,dentry,mode, 31441da177e4SLinus Torvalds new_decode_dev(dev)); 31451da177e4SLinus Torvalds break; 31461da177e4SLinus Torvalds case S_IFIFO: case S_IFSOCK: 3147dae6ad8fSAl Viro error = vfs_mknod(path.dentry->d_inode,dentry,mode,0); 31481da177e4SLinus Torvalds break; 31491da177e4SLinus Torvalds } 3150a8104a9fSAl Viro out: 3151921a1650SAl Viro done_path_create(&path, dentry); 31521da177e4SLinus Torvalds return error; 31531da177e4SLinus Torvalds } 31541da177e4SLinus Torvalds 31558208a22bSAl Viro SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev) 31565590ff0dSUlrich Drepper { 31575590ff0dSUlrich Drepper return sys_mknodat(AT_FDCWD, filename, mode, dev); 31585590ff0dSUlrich Drepper } 31595590ff0dSUlrich Drepper 316018bb1db3SAl Viro int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) 31611da177e4SLinus Torvalds { 3162a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 31638de52778SAl Viro unsigned max_links = dir->i_sb->s_max_links; 31641da177e4SLinus Torvalds 31651da177e4SLinus Torvalds if (error) 31661da177e4SLinus Torvalds return error; 31671da177e4SLinus Torvalds 3168acfa4380SAl Viro if (!dir->i_op->mkdir) 31691da177e4SLinus Torvalds return -EPERM; 31701da177e4SLinus Torvalds 31711da177e4SLinus Torvalds mode &= (S_IRWXUGO|S_ISVTX); 31721da177e4SLinus Torvalds error = security_inode_mkdir(dir, dentry, mode); 31731da177e4SLinus Torvalds if (error) 31741da177e4SLinus Torvalds return error; 31751da177e4SLinus Torvalds 31768de52778SAl Viro if (max_links && dir->i_nlink >= max_links) 31778de52778SAl Viro return -EMLINK; 31788de52778SAl Viro 31791da177e4SLinus Torvalds error = dir->i_op->mkdir(dir, dentry, mode); 3180a74574aaSStephen Smalley if (!error) 3181f38aa942SAmy Griffis fsnotify_mkdir(dir, dentry); 31821da177e4SLinus Torvalds return error; 31831da177e4SLinus Torvalds } 31841da177e4SLinus Torvalds 3185a218d0fdSAl Viro SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode) 31861da177e4SLinus Torvalds { 31876902d925SDave Hansen struct dentry *dentry; 3188dae6ad8fSAl Viro struct path path; 3189dae6ad8fSAl Viro int error; 31901da177e4SLinus Torvalds 3191dae6ad8fSAl Viro dentry = user_path_create(dfd, pathname, &path, 1); 31926902d925SDave Hansen if (IS_ERR(dentry)) 3193dae6ad8fSAl Viro return PTR_ERR(dentry); 31946902d925SDave Hansen 3195dae6ad8fSAl Viro if (!IS_POSIXACL(path.dentry->d_inode)) 3196ce3b0f8dSAl Viro mode &= ~current_umask(); 3197dae6ad8fSAl Viro error = security_path_mkdir(&path, dentry, mode); 3198a8104a9fSAl Viro if (!error) 3199dae6ad8fSAl Viro error = vfs_mkdir(path.dentry->d_inode, dentry, mode); 3200921a1650SAl Viro done_path_create(&path, dentry); 32011da177e4SLinus Torvalds return error; 32021da177e4SLinus Torvalds } 32031da177e4SLinus Torvalds 3204a218d0fdSAl Viro SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode) 32055590ff0dSUlrich Drepper { 32065590ff0dSUlrich Drepper return sys_mkdirat(AT_FDCWD, pathname, mode); 32075590ff0dSUlrich Drepper } 32085590ff0dSUlrich Drepper 32091da177e4SLinus Torvalds /* 3210a71905f0SSage Weil * The dentry_unhash() helper will try to drop the dentry early: we 3211c0d02594SJ. Bruce Fields * should have a usage count of 1 if we're the only user of this 3212a71905f0SSage Weil * dentry, and if that is true (possibly after pruning the dcache), 3213a71905f0SSage Weil * then we drop the dentry now. 32141da177e4SLinus Torvalds * 32151da177e4SLinus Torvalds * A low-level filesystem can, if it choses, legally 32161da177e4SLinus Torvalds * do a 32171da177e4SLinus Torvalds * 32181da177e4SLinus Torvalds * if (!d_unhashed(dentry)) 32191da177e4SLinus Torvalds * return -EBUSY; 32201da177e4SLinus Torvalds * 32211da177e4SLinus Torvalds * if it cannot handle the case of removing a directory 32221da177e4SLinus Torvalds * that is still in use by something else.. 32231da177e4SLinus Torvalds */ 32241da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry) 32251da177e4SLinus Torvalds { 32261da177e4SLinus Torvalds shrink_dcache_parent(dentry); 32271da177e4SLinus Torvalds spin_lock(&dentry->d_lock); 322864252c75SSage Weil if (dentry->d_count == 1) 32291da177e4SLinus Torvalds __d_drop(dentry); 32301da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 32311da177e4SLinus Torvalds } 32321da177e4SLinus Torvalds 32331da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry) 32341da177e4SLinus Torvalds { 32351da177e4SLinus Torvalds int error = may_delete(dir, dentry, 1); 32361da177e4SLinus Torvalds 32371da177e4SLinus Torvalds if (error) 32381da177e4SLinus Torvalds return error; 32391da177e4SLinus Torvalds 3240acfa4380SAl Viro if (!dir->i_op->rmdir) 32411da177e4SLinus Torvalds return -EPERM; 32421da177e4SLinus Torvalds 32431d2ef590SAl Viro dget(dentry); 32441b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 3245912dbc15SSage Weil 32461da177e4SLinus Torvalds error = -EBUSY; 3247912dbc15SSage Weil if (d_mountpoint(dentry)) 3248912dbc15SSage Weil goto out; 3249912dbc15SSage Weil 32501da177e4SLinus Torvalds error = security_inode_rmdir(dir, dentry); 3251912dbc15SSage Weil if (error) 3252912dbc15SSage Weil goto out; 3253912dbc15SSage Weil 32543cebde24SSage Weil shrink_dcache_parent(dentry); 32551da177e4SLinus Torvalds error = dir->i_op->rmdir(dir, dentry); 3256912dbc15SSage Weil if (error) 3257912dbc15SSage Weil goto out; 3258912dbc15SSage Weil 32591da177e4SLinus Torvalds dentry->d_inode->i_flags |= S_DEAD; 3260d83c49f3SAl Viro dont_mount(dentry); 32611da177e4SLinus Torvalds 3262912dbc15SSage Weil out: 3263912dbc15SSage Weil mutex_unlock(&dentry->d_inode->i_mutex); 32641d2ef590SAl Viro dput(dentry); 3265912dbc15SSage Weil if (!error) 3266912dbc15SSage Weil d_delete(dentry); 32671da177e4SLinus Torvalds return error; 32681da177e4SLinus Torvalds } 32691da177e4SLinus Torvalds 32705590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname) 32711da177e4SLinus Torvalds { 32721da177e4SLinus Torvalds int error = 0; 327391a27b2aSJeff Layton struct filename *name; 32741da177e4SLinus Torvalds struct dentry *dentry; 32751da177e4SLinus Torvalds struct nameidata nd; 32761da177e4SLinus Torvalds 327791a27b2aSJeff Layton name = user_path_parent(dfd, pathname, &nd); 327891a27b2aSJeff Layton if (IS_ERR(name)) 327991a27b2aSJeff Layton return PTR_ERR(name); 32801da177e4SLinus Torvalds 32811da177e4SLinus Torvalds switch(nd.last_type) { 32821da177e4SLinus Torvalds case LAST_DOTDOT: 32831da177e4SLinus Torvalds error = -ENOTEMPTY; 32841da177e4SLinus Torvalds goto exit1; 32851da177e4SLinus Torvalds case LAST_DOT: 32861da177e4SLinus Torvalds error = -EINVAL; 32871da177e4SLinus Torvalds goto exit1; 32881da177e4SLinus Torvalds case LAST_ROOT: 32891da177e4SLinus Torvalds error = -EBUSY; 32901da177e4SLinus Torvalds goto exit1; 32911da177e4SLinus Torvalds } 32920612d9fbSOGAWA Hirofumi 32930612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 3294c30dabfeSJan Kara error = mnt_want_write(nd.path.mnt); 3295c30dabfeSJan Kara if (error) 3296c30dabfeSJan Kara goto exit1; 32970612d9fbSOGAWA Hirofumi 32984ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 329949705b77SChristoph Hellwig dentry = lookup_hash(&nd); 33001da177e4SLinus Torvalds error = PTR_ERR(dentry); 33016902d925SDave Hansen if (IS_ERR(dentry)) 33026902d925SDave Hansen goto exit2; 3303e6bc45d6STheodore Ts'o if (!dentry->d_inode) { 3304e6bc45d6STheodore Ts'o error = -ENOENT; 3305e6bc45d6STheodore Ts'o goto exit3; 3306e6bc45d6STheodore Ts'o } 3307be6d3e56SKentaro Takeda error = security_path_rmdir(&nd.path, dentry); 3308be6d3e56SKentaro Takeda if (error) 3309c30dabfeSJan Kara goto exit3; 33104ac91378SJan Blunck error = vfs_rmdir(nd.path.dentry->d_inode, dentry); 33110622753bSDave Hansen exit3: 33121da177e4SLinus Torvalds dput(dentry); 33136902d925SDave Hansen exit2: 33144ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 3315c30dabfeSJan Kara mnt_drop_write(nd.path.mnt); 33161da177e4SLinus Torvalds exit1: 33171d957f9bSJan Blunck path_put(&nd.path); 33181da177e4SLinus Torvalds putname(name); 33191da177e4SLinus Torvalds return error; 33201da177e4SLinus Torvalds } 33211da177e4SLinus Torvalds 33223cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname) 33235590ff0dSUlrich Drepper { 33245590ff0dSUlrich Drepper return do_rmdir(AT_FDCWD, pathname); 33255590ff0dSUlrich Drepper } 33265590ff0dSUlrich Drepper 33271da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry) 33281da177e4SLinus Torvalds { 33291da177e4SLinus Torvalds int error = may_delete(dir, dentry, 0); 33301da177e4SLinus Torvalds 33311da177e4SLinus Torvalds if (error) 33321da177e4SLinus Torvalds return error; 33331da177e4SLinus Torvalds 3334acfa4380SAl Viro if (!dir->i_op->unlink) 33351da177e4SLinus Torvalds return -EPERM; 33361da177e4SLinus Torvalds 33371b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 33381da177e4SLinus Torvalds if (d_mountpoint(dentry)) 33391da177e4SLinus Torvalds error = -EBUSY; 33401da177e4SLinus Torvalds else { 33411da177e4SLinus Torvalds error = security_inode_unlink(dir, dentry); 3342bec1052eSAl Viro if (!error) { 33431da177e4SLinus Torvalds error = dir->i_op->unlink(dir, dentry); 3344bec1052eSAl Viro if (!error) 3345d83c49f3SAl Viro dont_mount(dentry); 3346bec1052eSAl Viro } 33471da177e4SLinus Torvalds } 33481b1dcc1bSJes Sorensen mutex_unlock(&dentry->d_inode->i_mutex); 33491da177e4SLinus Torvalds 33501da177e4SLinus Torvalds /* We don't d_delete() NFS sillyrenamed files--they still exist. */ 33511da177e4SLinus Torvalds if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) { 3352ece95912SJan Kara fsnotify_link_count(dentry->d_inode); 33531da177e4SLinus Torvalds d_delete(dentry); 33541da177e4SLinus Torvalds } 33550eeca283SRobert Love 33561da177e4SLinus Torvalds return error; 33571da177e4SLinus Torvalds } 33581da177e4SLinus Torvalds 33591da177e4SLinus Torvalds /* 33601da177e4SLinus Torvalds * Make sure that the actual truncation of the file will occur outside its 33611b1dcc1bSJes Sorensen * directory's i_mutex. Truncate can take a long time if there is a lot of 33621da177e4SLinus Torvalds * writeout happening, and we don't want to prevent access to the directory 33631da177e4SLinus Torvalds * while waiting on the I/O. 33641da177e4SLinus Torvalds */ 33655590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname) 33661da177e4SLinus Torvalds { 33672ad94ae6SAl Viro int error; 336891a27b2aSJeff Layton struct filename *name; 33691da177e4SLinus Torvalds struct dentry *dentry; 33701da177e4SLinus Torvalds struct nameidata nd; 33711da177e4SLinus Torvalds struct inode *inode = NULL; 33721da177e4SLinus Torvalds 337391a27b2aSJeff Layton name = user_path_parent(dfd, pathname, &nd); 337491a27b2aSJeff Layton if (IS_ERR(name)) 337591a27b2aSJeff Layton return PTR_ERR(name); 33762ad94ae6SAl Viro 33771da177e4SLinus Torvalds error = -EISDIR; 33781da177e4SLinus Torvalds if (nd.last_type != LAST_NORM) 33791da177e4SLinus Torvalds goto exit1; 33800612d9fbSOGAWA Hirofumi 33810612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 3382c30dabfeSJan Kara error = mnt_want_write(nd.path.mnt); 3383c30dabfeSJan Kara if (error) 3384c30dabfeSJan Kara goto exit1; 33850612d9fbSOGAWA Hirofumi 33864ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 338749705b77SChristoph Hellwig dentry = lookup_hash(&nd); 33881da177e4SLinus Torvalds error = PTR_ERR(dentry); 33891da177e4SLinus Torvalds if (!IS_ERR(dentry)) { 33901da177e4SLinus Torvalds /* Why not before? Because we want correct error value */ 339150338b88STörök Edwin if (nd.last.name[nd.last.len]) 339250338b88STörök Edwin goto slashes; 33931da177e4SLinus Torvalds inode = dentry->d_inode; 339450338b88STörök Edwin if (!inode) 3395e6bc45d6STheodore Ts'o goto slashes; 33967de9c6eeSAl Viro ihold(inode); 3397be6d3e56SKentaro Takeda error = security_path_unlink(&nd.path, dentry); 3398be6d3e56SKentaro Takeda if (error) 3399c30dabfeSJan Kara goto exit2; 34004ac91378SJan Blunck error = vfs_unlink(nd.path.dentry->d_inode, dentry); 34011da177e4SLinus Torvalds exit2: 34021da177e4SLinus Torvalds dput(dentry); 34031da177e4SLinus Torvalds } 34044ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 34051da177e4SLinus Torvalds if (inode) 34061da177e4SLinus Torvalds iput(inode); /* truncate the inode here */ 3407c30dabfeSJan Kara mnt_drop_write(nd.path.mnt); 34081da177e4SLinus Torvalds exit1: 34091d957f9bSJan Blunck path_put(&nd.path); 34101da177e4SLinus Torvalds putname(name); 34111da177e4SLinus Torvalds return error; 34121da177e4SLinus Torvalds 34131da177e4SLinus Torvalds slashes: 34141da177e4SLinus Torvalds error = !dentry->d_inode ? -ENOENT : 34151da177e4SLinus Torvalds S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR; 34161da177e4SLinus Torvalds goto exit2; 34171da177e4SLinus Torvalds } 34181da177e4SLinus Torvalds 34192e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag) 34205590ff0dSUlrich Drepper { 34215590ff0dSUlrich Drepper if ((flag & ~AT_REMOVEDIR) != 0) 34225590ff0dSUlrich Drepper return -EINVAL; 34235590ff0dSUlrich Drepper 34245590ff0dSUlrich Drepper if (flag & AT_REMOVEDIR) 34255590ff0dSUlrich Drepper return do_rmdir(dfd, pathname); 34265590ff0dSUlrich Drepper 34275590ff0dSUlrich Drepper return do_unlinkat(dfd, pathname); 34285590ff0dSUlrich Drepper } 34295590ff0dSUlrich Drepper 34303480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname) 34315590ff0dSUlrich Drepper { 34325590ff0dSUlrich Drepper return do_unlinkat(AT_FDCWD, pathname); 34335590ff0dSUlrich Drepper } 34345590ff0dSUlrich Drepper 3435db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname) 34361da177e4SLinus Torvalds { 3437a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 34381da177e4SLinus Torvalds 34391da177e4SLinus Torvalds if (error) 34401da177e4SLinus Torvalds return error; 34411da177e4SLinus Torvalds 3442acfa4380SAl Viro if (!dir->i_op->symlink) 34431da177e4SLinus Torvalds return -EPERM; 34441da177e4SLinus Torvalds 34451da177e4SLinus Torvalds error = security_inode_symlink(dir, dentry, oldname); 34461da177e4SLinus Torvalds if (error) 34471da177e4SLinus Torvalds return error; 34481da177e4SLinus Torvalds 34491da177e4SLinus Torvalds error = dir->i_op->symlink(dir, dentry, oldname); 3450a74574aaSStephen Smalley if (!error) 3451f38aa942SAmy Griffis fsnotify_create(dir, dentry); 34521da177e4SLinus Torvalds return error; 34531da177e4SLinus Torvalds } 34541da177e4SLinus Torvalds 34552e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname, 34562e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 34571da177e4SLinus Torvalds { 34582ad94ae6SAl Viro int error; 345991a27b2aSJeff Layton struct filename *from; 34606902d925SDave Hansen struct dentry *dentry; 3461dae6ad8fSAl Viro struct path path; 34621da177e4SLinus Torvalds 34631da177e4SLinus Torvalds from = getname(oldname); 34641da177e4SLinus Torvalds if (IS_ERR(from)) 34651da177e4SLinus Torvalds return PTR_ERR(from); 34662ad94ae6SAl Viro 3467dae6ad8fSAl Viro dentry = user_path_create(newdfd, newname, &path, 0); 34681da177e4SLinus Torvalds error = PTR_ERR(dentry); 34696902d925SDave Hansen if (IS_ERR(dentry)) 3470dae6ad8fSAl Viro goto out_putname; 34716902d925SDave Hansen 347291a27b2aSJeff Layton error = security_path_symlink(&path, dentry, from->name); 3473a8104a9fSAl Viro if (!error) 347491a27b2aSJeff Layton error = vfs_symlink(path.dentry->d_inode, dentry, from->name); 3475921a1650SAl Viro done_path_create(&path, dentry); 34766902d925SDave Hansen out_putname: 34771da177e4SLinus Torvalds putname(from); 34781da177e4SLinus Torvalds return error; 34791da177e4SLinus Torvalds } 34801da177e4SLinus Torvalds 34813480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname) 34825590ff0dSUlrich Drepper { 34835590ff0dSUlrich Drepper return sys_symlinkat(oldname, AT_FDCWD, newname); 34845590ff0dSUlrich Drepper } 34855590ff0dSUlrich Drepper 34861da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) 34871da177e4SLinus Torvalds { 34881da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 34898de52778SAl Viro unsigned max_links = dir->i_sb->s_max_links; 34901da177e4SLinus Torvalds int error; 34911da177e4SLinus Torvalds 34921da177e4SLinus Torvalds if (!inode) 34931da177e4SLinus Torvalds return -ENOENT; 34941da177e4SLinus Torvalds 3495a95164d9SMiklos Szeredi error = may_create(dir, new_dentry); 34961da177e4SLinus Torvalds if (error) 34971da177e4SLinus Torvalds return error; 34981da177e4SLinus Torvalds 34991da177e4SLinus Torvalds if (dir->i_sb != inode->i_sb) 35001da177e4SLinus Torvalds return -EXDEV; 35011da177e4SLinus Torvalds 35021da177e4SLinus Torvalds /* 35031da177e4SLinus Torvalds * A link to an append-only or immutable file cannot be created. 35041da177e4SLinus Torvalds */ 35051da177e4SLinus Torvalds if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) 35061da177e4SLinus Torvalds return -EPERM; 3507acfa4380SAl Viro if (!dir->i_op->link) 35081da177e4SLinus Torvalds return -EPERM; 35097e79eedbSTetsuo Handa if (S_ISDIR(inode->i_mode)) 35101da177e4SLinus Torvalds return -EPERM; 35111da177e4SLinus Torvalds 35121da177e4SLinus Torvalds error = security_inode_link(old_dentry, dir, new_dentry); 35131da177e4SLinus Torvalds if (error) 35141da177e4SLinus Torvalds return error; 35151da177e4SLinus Torvalds 35167e79eedbSTetsuo Handa mutex_lock(&inode->i_mutex); 3517aae8a97dSAneesh Kumar K.V /* Make sure we don't allow creating hardlink to an unlinked file */ 3518aae8a97dSAneesh Kumar K.V if (inode->i_nlink == 0) 3519aae8a97dSAneesh Kumar K.V error = -ENOENT; 35208de52778SAl Viro else if (max_links && inode->i_nlink >= max_links) 35218de52778SAl Viro error = -EMLINK; 3522aae8a97dSAneesh Kumar K.V else 35231da177e4SLinus Torvalds error = dir->i_op->link(old_dentry, dir, new_dentry); 35247e79eedbSTetsuo Handa mutex_unlock(&inode->i_mutex); 3525e31e14ecSStephen Smalley if (!error) 35267e79eedbSTetsuo Handa fsnotify_link(dir, inode, new_dentry); 35271da177e4SLinus Torvalds return error; 35281da177e4SLinus Torvalds } 35291da177e4SLinus Torvalds 35301da177e4SLinus Torvalds /* 35311da177e4SLinus Torvalds * Hardlinks are often used in delicate situations. We avoid 35321da177e4SLinus Torvalds * security-related surprises by not following symlinks on the 35331da177e4SLinus Torvalds * newname. --KAB 35341da177e4SLinus Torvalds * 35351da177e4SLinus Torvalds * We don't follow them on the oldname either to be compatible 35361da177e4SLinus Torvalds * with linux 2.0, and to avoid hard-linking to directories 35371da177e4SLinus Torvalds * and other special files. --ADM 35381da177e4SLinus Torvalds */ 35392e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname, 35402e4d0924SHeiko Carstens int, newdfd, const char __user *, newname, int, flags) 35411da177e4SLinus Torvalds { 35421da177e4SLinus Torvalds struct dentry *new_dentry; 3543dae6ad8fSAl Viro struct path old_path, new_path; 354411a7b371SAneesh Kumar K.V int how = 0; 35451da177e4SLinus Torvalds int error; 35461da177e4SLinus Torvalds 354711a7b371SAneesh Kumar K.V if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0) 3548c04030e1SUlrich Drepper return -EINVAL; 354911a7b371SAneesh Kumar K.V /* 355011a7b371SAneesh Kumar K.V * To use null names we require CAP_DAC_READ_SEARCH 355111a7b371SAneesh Kumar K.V * This ensures that not everyone will be able to create 355211a7b371SAneesh Kumar K.V * handlink using the passed filedescriptor. 355311a7b371SAneesh Kumar K.V */ 355411a7b371SAneesh Kumar K.V if (flags & AT_EMPTY_PATH) { 355511a7b371SAneesh Kumar K.V if (!capable(CAP_DAC_READ_SEARCH)) 355611a7b371SAneesh Kumar K.V return -ENOENT; 355711a7b371SAneesh Kumar K.V how = LOOKUP_EMPTY; 355811a7b371SAneesh Kumar K.V } 3559c04030e1SUlrich Drepper 356011a7b371SAneesh Kumar K.V if (flags & AT_SYMLINK_FOLLOW) 356111a7b371SAneesh Kumar K.V how |= LOOKUP_FOLLOW; 356211a7b371SAneesh Kumar K.V 356311a7b371SAneesh Kumar K.V error = user_path_at(olddfd, oldname, how, &old_path); 35641da177e4SLinus Torvalds if (error) 35652ad94ae6SAl Viro return error; 35662ad94ae6SAl Viro 3567dae6ad8fSAl Viro new_dentry = user_path_create(newdfd, newname, &new_path, 0); 35681da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 35696902d925SDave Hansen if (IS_ERR(new_dentry)) 3570dae6ad8fSAl Viro goto out; 3571dae6ad8fSAl Viro 3572dae6ad8fSAl Viro error = -EXDEV; 3573dae6ad8fSAl Viro if (old_path.mnt != new_path.mnt) 3574dae6ad8fSAl Viro goto out_dput; 3575800179c9SKees Cook error = may_linkat(&old_path); 3576800179c9SKees Cook if (unlikely(error)) 3577800179c9SKees Cook goto out_dput; 3578dae6ad8fSAl Viro error = security_path_link(old_path.dentry, &new_path, new_dentry); 3579be6d3e56SKentaro Takeda if (error) 3580a8104a9fSAl Viro goto out_dput; 3581dae6ad8fSAl Viro error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry); 358275c3f29dSDave Hansen out_dput: 3583921a1650SAl Viro done_path_create(&new_path, new_dentry); 35841da177e4SLinus Torvalds out: 35852d8f3038SAl Viro path_put(&old_path); 35861da177e4SLinus Torvalds 35871da177e4SLinus Torvalds return error; 35881da177e4SLinus Torvalds } 35891da177e4SLinus Torvalds 35903480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname) 35915590ff0dSUlrich Drepper { 3592c04030e1SUlrich Drepper return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0); 35935590ff0dSUlrich Drepper } 35945590ff0dSUlrich Drepper 35951da177e4SLinus Torvalds /* 35961da177e4SLinus Torvalds * The worst of all namespace operations - renaming directory. "Perverted" 35971da177e4SLinus Torvalds * doesn't even start to describe it. Somebody in UCB had a heck of a trip... 35981da177e4SLinus Torvalds * Problems: 35991da177e4SLinus Torvalds * a) we can get into loop creation. Check is done in is_subdir(). 36001da177e4SLinus Torvalds * b) race potential - two innocent renames can create a loop together. 36011da177e4SLinus Torvalds * That's where 4.4 screws up. Current fix: serialization on 3602a11f3a05SArjan van de Ven * sb->s_vfs_rename_mutex. We might be more accurate, but that's another 36031da177e4SLinus Torvalds * story. 36041da177e4SLinus Torvalds * c) we have to lock _three_ objects - parents and victim (if it exists). 36051b1dcc1bSJes Sorensen * And that - after we got ->i_mutex on parents (until then we don't know 36061da177e4SLinus Torvalds * whether the target exists). Solution: try to be smart with locking 36071da177e4SLinus Torvalds * order for inodes. We rely on the fact that tree topology may change 3608a11f3a05SArjan van de Ven * only under ->s_vfs_rename_mutex _and_ that parent of the object we 36091da177e4SLinus Torvalds * move will be locked. Thus we can rank directories by the tree 36101da177e4SLinus Torvalds * (ancestors first) and rank all non-directories after them. 36111da177e4SLinus Torvalds * That works since everybody except rename does "lock parent, lookup, 3612a11f3a05SArjan van de Ven * lock child" and rename is under ->s_vfs_rename_mutex. 36131da177e4SLinus Torvalds * HOWEVER, it relies on the assumption that any object with ->lookup() 36141da177e4SLinus Torvalds * has no more than 1 dentry. If "hybrid" objects will ever appear, 36151da177e4SLinus Torvalds * we'd better make sure that there's no link(2) for them. 3616e4eaac06SSage Weil * d) conversion from fhandle to dentry may come in the wrong moment - when 36171b1dcc1bSJes Sorensen * we are removing the target. Solution: we will have to grab ->i_mutex 36181da177e4SLinus Torvalds * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on 3619c41b20e7SAdam Buchbinder * ->i_mutex on parents, which works but leads to some truly excessive 36201da177e4SLinus Torvalds * locking]. 36211da177e4SLinus Torvalds */ 362275c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry, 36231da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 36241da177e4SLinus Torvalds { 36251da177e4SLinus Torvalds int error = 0; 36269055cba7SSage Weil struct inode *target = new_dentry->d_inode; 36278de52778SAl Viro unsigned max_links = new_dir->i_sb->s_max_links; 36281da177e4SLinus Torvalds 36291da177e4SLinus Torvalds /* 36301da177e4SLinus Torvalds * If we are going to change the parent - check write permissions, 36311da177e4SLinus Torvalds * we'll need to flip '..'. 36321da177e4SLinus Torvalds */ 36331da177e4SLinus Torvalds if (new_dir != old_dir) { 3634f419a2e3SAl Viro error = inode_permission(old_dentry->d_inode, MAY_WRITE); 36351da177e4SLinus Torvalds if (error) 36361da177e4SLinus Torvalds return error; 36371da177e4SLinus Torvalds } 36381da177e4SLinus Torvalds 36391da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 36401da177e4SLinus Torvalds if (error) 36411da177e4SLinus Torvalds return error; 36421da177e4SLinus Torvalds 36431d2ef590SAl Viro dget(new_dentry); 3644d83c49f3SAl Viro if (target) 36451b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 36469055cba7SSage Weil 36471da177e4SLinus Torvalds error = -EBUSY; 36489055cba7SSage Weil if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry)) 36499055cba7SSage Weil goto out; 36509055cba7SSage Weil 36518de52778SAl Viro error = -EMLINK; 36528de52778SAl Viro if (max_links && !target && new_dir != old_dir && 36538de52778SAl Viro new_dir->i_nlink >= max_links) 36548de52778SAl Viro goto out; 36558de52778SAl Viro 36563cebde24SSage Weil if (target) 36573cebde24SSage Weil shrink_dcache_parent(new_dentry); 36581da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 36599055cba7SSage Weil if (error) 36609055cba7SSage Weil goto out; 36619055cba7SSage Weil 36621da177e4SLinus Torvalds if (target) { 36631da177e4SLinus Torvalds target->i_flags |= S_DEAD; 3664d83c49f3SAl Viro dont_mount(new_dentry); 3665d83c49f3SAl Viro } 36669055cba7SSage Weil out: 36679055cba7SSage Weil if (target) 36681b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 36691d2ef590SAl Viro dput(new_dentry); 3670e31e14ecSStephen Smalley if (!error) 3671349457ccSMark Fasheh if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) 36721da177e4SLinus Torvalds d_move(old_dentry,new_dentry); 36731da177e4SLinus Torvalds return error; 36741da177e4SLinus Torvalds } 36751da177e4SLinus Torvalds 367675c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry, 36771da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 36781da177e4SLinus Torvalds { 367951892bbbSSage Weil struct inode *target = new_dentry->d_inode; 36801da177e4SLinus Torvalds int error; 36811da177e4SLinus Torvalds 36821da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 36831da177e4SLinus Torvalds if (error) 36841da177e4SLinus Torvalds return error; 36851da177e4SLinus Torvalds 36861da177e4SLinus Torvalds dget(new_dentry); 36871da177e4SLinus Torvalds if (target) 36881b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 368951892bbbSSage Weil 36901da177e4SLinus Torvalds error = -EBUSY; 369151892bbbSSage Weil if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry)) 369251892bbbSSage Weil goto out; 369351892bbbSSage Weil 36941da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 369551892bbbSSage Weil if (error) 369651892bbbSSage Weil goto out; 369751892bbbSSage Weil 3698bec1052eSAl Viro if (target) 3699d83c49f3SAl Viro dont_mount(new_dentry); 3700349457ccSMark Fasheh if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) 37011da177e4SLinus Torvalds d_move(old_dentry, new_dentry); 370251892bbbSSage Weil out: 37031da177e4SLinus Torvalds if (target) 37041b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 37051da177e4SLinus Torvalds dput(new_dentry); 37061da177e4SLinus Torvalds return error; 37071da177e4SLinus Torvalds } 37081da177e4SLinus Torvalds 37091da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry, 37101da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 37111da177e4SLinus Torvalds { 37121da177e4SLinus Torvalds int error; 37131da177e4SLinus Torvalds int is_dir = S_ISDIR(old_dentry->d_inode->i_mode); 371459b0df21SEric Paris const unsigned char *old_name; 37151da177e4SLinus Torvalds 37161da177e4SLinus Torvalds if (old_dentry->d_inode == new_dentry->d_inode) 37171da177e4SLinus Torvalds return 0; 37181da177e4SLinus Torvalds 37191da177e4SLinus Torvalds error = may_delete(old_dir, old_dentry, is_dir); 37201da177e4SLinus Torvalds if (error) 37211da177e4SLinus Torvalds return error; 37221da177e4SLinus Torvalds 37231da177e4SLinus Torvalds if (!new_dentry->d_inode) 3724a95164d9SMiklos Szeredi error = may_create(new_dir, new_dentry); 37251da177e4SLinus Torvalds else 37261da177e4SLinus Torvalds error = may_delete(new_dir, new_dentry, is_dir); 37271da177e4SLinus Torvalds if (error) 37281da177e4SLinus Torvalds return error; 37291da177e4SLinus Torvalds 3730acfa4380SAl Viro if (!old_dir->i_op->rename) 37311da177e4SLinus Torvalds return -EPERM; 37321da177e4SLinus Torvalds 37330eeca283SRobert Love old_name = fsnotify_oldname_init(old_dentry->d_name.name); 37340eeca283SRobert Love 37351da177e4SLinus Torvalds if (is_dir) 37361da177e4SLinus Torvalds error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry); 37371da177e4SLinus Torvalds else 37381da177e4SLinus Torvalds error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry); 3739123df294SAl Viro if (!error) 3740123df294SAl Viro fsnotify_move(old_dir, new_dir, old_name, is_dir, 37415a190ae6SAl Viro new_dentry->d_inode, old_dentry); 37420eeca283SRobert Love fsnotify_oldname_free(old_name); 37430eeca283SRobert Love 37441da177e4SLinus Torvalds return error; 37451da177e4SLinus Torvalds } 37461da177e4SLinus Torvalds 37472e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname, 37482e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 37491da177e4SLinus Torvalds { 37501da177e4SLinus Torvalds struct dentry *old_dir, *new_dir; 37511da177e4SLinus Torvalds struct dentry *old_dentry, *new_dentry; 37521da177e4SLinus Torvalds struct dentry *trap; 37531da177e4SLinus Torvalds struct nameidata oldnd, newnd; 375491a27b2aSJeff Layton struct filename *from; 375591a27b2aSJeff Layton struct filename *to; 37562ad94ae6SAl Viro int error; 37571da177e4SLinus Torvalds 375891a27b2aSJeff Layton from = user_path_parent(olddfd, oldname, &oldnd); 375991a27b2aSJeff Layton if (IS_ERR(from)) { 376091a27b2aSJeff Layton error = PTR_ERR(from); 37611da177e4SLinus Torvalds goto exit; 376291a27b2aSJeff Layton } 37631da177e4SLinus Torvalds 376491a27b2aSJeff Layton to = user_path_parent(newdfd, newname, &newnd); 376591a27b2aSJeff Layton if (IS_ERR(to)) { 376691a27b2aSJeff Layton error = PTR_ERR(to); 37671da177e4SLinus Torvalds goto exit1; 376891a27b2aSJeff Layton } 37691da177e4SLinus Torvalds 37701da177e4SLinus Torvalds error = -EXDEV; 37714ac91378SJan Blunck if (oldnd.path.mnt != newnd.path.mnt) 37721da177e4SLinus Torvalds goto exit2; 37731da177e4SLinus Torvalds 37744ac91378SJan Blunck old_dir = oldnd.path.dentry; 37751da177e4SLinus Torvalds error = -EBUSY; 37761da177e4SLinus Torvalds if (oldnd.last_type != LAST_NORM) 37771da177e4SLinus Torvalds goto exit2; 37781da177e4SLinus Torvalds 37794ac91378SJan Blunck new_dir = newnd.path.dentry; 37801da177e4SLinus Torvalds if (newnd.last_type != LAST_NORM) 37811da177e4SLinus Torvalds goto exit2; 37821da177e4SLinus Torvalds 3783c30dabfeSJan Kara error = mnt_want_write(oldnd.path.mnt); 3784c30dabfeSJan Kara if (error) 3785c30dabfeSJan Kara goto exit2; 3786c30dabfeSJan Kara 37870612d9fbSOGAWA Hirofumi oldnd.flags &= ~LOOKUP_PARENT; 37880612d9fbSOGAWA Hirofumi newnd.flags &= ~LOOKUP_PARENT; 37894e9ed2f8SOGAWA Hirofumi newnd.flags |= LOOKUP_RENAME_TARGET; 37900612d9fbSOGAWA Hirofumi 37911da177e4SLinus Torvalds trap = lock_rename(new_dir, old_dir); 37921da177e4SLinus Torvalds 379349705b77SChristoph Hellwig old_dentry = lookup_hash(&oldnd); 37941da177e4SLinus Torvalds error = PTR_ERR(old_dentry); 37951da177e4SLinus Torvalds if (IS_ERR(old_dentry)) 37961da177e4SLinus Torvalds goto exit3; 37971da177e4SLinus Torvalds /* source must exist */ 37981da177e4SLinus Torvalds error = -ENOENT; 37991da177e4SLinus Torvalds if (!old_dentry->d_inode) 38001da177e4SLinus Torvalds goto exit4; 38011da177e4SLinus Torvalds /* unless the source is a directory trailing slashes give -ENOTDIR */ 38021da177e4SLinus Torvalds if (!S_ISDIR(old_dentry->d_inode->i_mode)) { 38031da177e4SLinus Torvalds error = -ENOTDIR; 38041da177e4SLinus Torvalds if (oldnd.last.name[oldnd.last.len]) 38051da177e4SLinus Torvalds goto exit4; 38061da177e4SLinus Torvalds if (newnd.last.name[newnd.last.len]) 38071da177e4SLinus Torvalds goto exit4; 38081da177e4SLinus Torvalds } 38091da177e4SLinus Torvalds /* source should not be ancestor of target */ 38101da177e4SLinus Torvalds error = -EINVAL; 38111da177e4SLinus Torvalds if (old_dentry == trap) 38121da177e4SLinus Torvalds goto exit4; 381349705b77SChristoph Hellwig new_dentry = lookup_hash(&newnd); 38141da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 38151da177e4SLinus Torvalds if (IS_ERR(new_dentry)) 38161da177e4SLinus Torvalds goto exit4; 38171da177e4SLinus Torvalds /* target should not be an ancestor of source */ 38181da177e4SLinus Torvalds error = -ENOTEMPTY; 38191da177e4SLinus Torvalds if (new_dentry == trap) 38201da177e4SLinus Torvalds goto exit5; 38211da177e4SLinus Torvalds 3822be6d3e56SKentaro Takeda error = security_path_rename(&oldnd.path, old_dentry, 3823be6d3e56SKentaro Takeda &newnd.path, new_dentry); 3824be6d3e56SKentaro Takeda if (error) 3825c30dabfeSJan Kara goto exit5; 38261da177e4SLinus Torvalds error = vfs_rename(old_dir->d_inode, old_dentry, 38271da177e4SLinus Torvalds new_dir->d_inode, new_dentry); 38281da177e4SLinus Torvalds exit5: 38291da177e4SLinus Torvalds dput(new_dentry); 38301da177e4SLinus Torvalds exit4: 38311da177e4SLinus Torvalds dput(old_dentry); 38321da177e4SLinus Torvalds exit3: 38331da177e4SLinus Torvalds unlock_rename(new_dir, old_dir); 3834c30dabfeSJan Kara mnt_drop_write(oldnd.path.mnt); 38351da177e4SLinus Torvalds exit2: 38361d957f9bSJan Blunck path_put(&newnd.path); 38372ad94ae6SAl Viro putname(to); 38381da177e4SLinus Torvalds exit1: 38391d957f9bSJan Blunck path_put(&oldnd.path); 38401da177e4SLinus Torvalds putname(from); 38412ad94ae6SAl Viro exit: 38421da177e4SLinus Torvalds return error; 38431da177e4SLinus Torvalds } 38441da177e4SLinus Torvalds 3845a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname) 38465590ff0dSUlrich Drepper { 38475590ff0dSUlrich Drepper return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname); 38485590ff0dSUlrich Drepper } 38495590ff0dSUlrich Drepper 38501da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link) 38511da177e4SLinus Torvalds { 38521da177e4SLinus Torvalds int len; 38531da177e4SLinus Torvalds 38541da177e4SLinus Torvalds len = PTR_ERR(link); 38551da177e4SLinus Torvalds if (IS_ERR(link)) 38561da177e4SLinus Torvalds goto out; 38571da177e4SLinus Torvalds 38581da177e4SLinus Torvalds len = strlen(link); 38591da177e4SLinus Torvalds if (len > (unsigned) buflen) 38601da177e4SLinus Torvalds len = buflen; 38611da177e4SLinus Torvalds if (copy_to_user(buffer, link, len)) 38621da177e4SLinus Torvalds len = -EFAULT; 38631da177e4SLinus Torvalds out: 38641da177e4SLinus Torvalds return len; 38651da177e4SLinus Torvalds } 38661da177e4SLinus Torvalds 38671da177e4SLinus Torvalds /* 38681da177e4SLinus Torvalds * A helper for ->readlink(). This should be used *ONLY* for symlinks that 38691da177e4SLinus Torvalds * have ->follow_link() touching nd only in nd_set_link(). Using (or not 38701da177e4SLinus Torvalds * using) it for any given inode is up to filesystem. 38711da177e4SLinus Torvalds */ 38721da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen) 38731da177e4SLinus Torvalds { 38741da177e4SLinus Torvalds struct nameidata nd; 3875cc314eefSLinus Torvalds void *cookie; 3876694a1764SMarcin Slusarz int res; 3877cc314eefSLinus Torvalds 38781da177e4SLinus Torvalds nd.depth = 0; 3879cc314eefSLinus Torvalds cookie = dentry->d_inode->i_op->follow_link(dentry, &nd); 3880694a1764SMarcin Slusarz if (IS_ERR(cookie)) 3881694a1764SMarcin Slusarz return PTR_ERR(cookie); 3882694a1764SMarcin Slusarz 3883694a1764SMarcin Slusarz res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd)); 38841da177e4SLinus Torvalds if (dentry->d_inode->i_op->put_link) 3885cc314eefSLinus Torvalds dentry->d_inode->i_op->put_link(dentry, &nd, cookie); 3886694a1764SMarcin Slusarz return res; 38871da177e4SLinus Torvalds } 38881da177e4SLinus Torvalds 38891da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link) 38901da177e4SLinus Torvalds { 38911da177e4SLinus Torvalds return __vfs_follow_link(nd, link); 38921da177e4SLinus Torvalds } 38931da177e4SLinus Torvalds 38941da177e4SLinus Torvalds /* get the link contents into pagecache */ 38951da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage) 38961da177e4SLinus Torvalds { 3897ebd09abbSDuane Griffin char *kaddr; 38981da177e4SLinus Torvalds struct page *page; 38991da177e4SLinus Torvalds struct address_space *mapping = dentry->d_inode->i_mapping; 3900090d2b18SPekka Enberg page = read_mapping_page(mapping, 0, NULL); 39011da177e4SLinus Torvalds if (IS_ERR(page)) 39026fe6900eSNick Piggin return (char*)page; 39031da177e4SLinus Torvalds *ppage = page; 3904ebd09abbSDuane Griffin kaddr = kmap(page); 3905ebd09abbSDuane Griffin nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1); 3906ebd09abbSDuane Griffin return kaddr; 39071da177e4SLinus Torvalds } 39081da177e4SLinus Torvalds 39091da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen) 39101da177e4SLinus Torvalds { 39111da177e4SLinus Torvalds struct page *page = NULL; 39121da177e4SLinus Torvalds char *s = page_getlink(dentry, &page); 39131da177e4SLinus Torvalds int res = vfs_readlink(dentry,buffer,buflen,s); 39141da177e4SLinus Torvalds if (page) { 39151da177e4SLinus Torvalds kunmap(page); 39161da177e4SLinus Torvalds page_cache_release(page); 39171da177e4SLinus Torvalds } 39181da177e4SLinus Torvalds return res; 39191da177e4SLinus Torvalds } 39201da177e4SLinus Torvalds 3921cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd) 39221da177e4SLinus Torvalds { 3923cc314eefSLinus Torvalds struct page *page = NULL; 39241da177e4SLinus Torvalds nd_set_link(nd, page_getlink(dentry, &page)); 3925cc314eefSLinus Torvalds return page; 39261da177e4SLinus Torvalds } 39271da177e4SLinus Torvalds 3928cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie) 39291da177e4SLinus Torvalds { 3930cc314eefSLinus Torvalds struct page *page = cookie; 3931cc314eefSLinus Torvalds 3932cc314eefSLinus Torvalds if (page) { 39331da177e4SLinus Torvalds kunmap(page); 39341da177e4SLinus Torvalds page_cache_release(page); 39351da177e4SLinus Torvalds } 39361da177e4SLinus Torvalds } 39371da177e4SLinus Torvalds 393854566b2cSNick Piggin /* 393954566b2cSNick Piggin * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS 394054566b2cSNick Piggin */ 394154566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs) 39421da177e4SLinus Torvalds { 39431da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 39440adb25d2SKirill Korotaev struct page *page; 3945afddba49SNick Piggin void *fsdata; 3946beb497abSDmitriy Monakhov int err; 39471da177e4SLinus Torvalds char *kaddr; 394854566b2cSNick Piggin unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE; 394954566b2cSNick Piggin if (nofs) 395054566b2cSNick Piggin flags |= AOP_FLAG_NOFS; 39511da177e4SLinus Torvalds 39527e53cac4SNeilBrown retry: 3953afddba49SNick Piggin err = pagecache_write_begin(NULL, mapping, 0, len-1, 395454566b2cSNick Piggin flags, &page, &fsdata); 39551da177e4SLinus Torvalds if (err) 3956afddba49SNick Piggin goto fail; 3957afddba49SNick Piggin 3958e8e3c3d6SCong Wang kaddr = kmap_atomic(page); 39591da177e4SLinus Torvalds memcpy(kaddr, symname, len-1); 3960e8e3c3d6SCong Wang kunmap_atomic(kaddr); 3961afddba49SNick Piggin 3962afddba49SNick Piggin err = pagecache_write_end(NULL, mapping, 0, len-1, len-1, 3963afddba49SNick Piggin page, fsdata); 39641da177e4SLinus Torvalds if (err < 0) 39651da177e4SLinus Torvalds goto fail; 3966afddba49SNick Piggin if (err < len-1) 3967afddba49SNick Piggin goto retry; 3968afddba49SNick Piggin 39691da177e4SLinus Torvalds mark_inode_dirty(inode); 39701da177e4SLinus Torvalds return 0; 39711da177e4SLinus Torvalds fail: 39721da177e4SLinus Torvalds return err; 39731da177e4SLinus Torvalds } 39741da177e4SLinus Torvalds 39750adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len) 39760adb25d2SKirill Korotaev { 39770adb25d2SKirill Korotaev return __page_symlink(inode, symname, len, 397854566b2cSNick Piggin !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS)); 39790adb25d2SKirill Korotaev } 39800adb25d2SKirill Korotaev 398192e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = { 39821da177e4SLinus Torvalds .readlink = generic_readlink, 39831da177e4SLinus Torvalds .follow_link = page_follow_link_light, 39841da177e4SLinus Torvalds .put_link = page_put_link, 39851da177e4SLinus Torvalds }; 39861da177e4SLinus Torvalds 39872d8f3038SAl Viro EXPORT_SYMBOL(user_path_at); 3988cc53ce53SDavid Howells EXPORT_SYMBOL(follow_down_one); 39891da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down); 39901da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up); 3991f6d2ac5cSAl Viro EXPORT_SYMBOL(get_write_access); /* nfsd */ 39921da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename); 39931da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len); 39941da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light); 39951da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link); 39961da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink); 39970adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink); 39981da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink); 39991da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations); 4000d1811465SAl Viro EXPORT_SYMBOL(kern_path); 400116f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup); 4002f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission); 40031da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename); 40041da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create); 40051da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link); 40061da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link); 40071da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir); 40081da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod); 40091da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission); 40101da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink); 40111da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename); 40121da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir); 40131da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink); 40141da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink); 40151da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash); 40161da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink); 4017