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 1337ac86265SJeff Layton result = audit_reusename(filename); 1347ac86265SJeff Layton if (result) 1357ac86265SJeff Layton return result; 1367ac86265SJeff Layton 13791a27b2aSJeff Layton /* FIXME: create dedicated slabcache? */ 13891a27b2aSJeff Layton result = kzalloc(sizeof(*result), GFP_KERNEL); 1393f9f0aa6SLinus Torvalds if (unlikely(!result)) 1404043cde8SEric Paris return ERR_PTR(-ENOMEM); 1411da177e4SLinus Torvalds 14291a27b2aSJeff Layton kname = __getname(); 14391a27b2aSJeff Layton if (unlikely(!kname)) { 14491a27b2aSJeff Layton err = ERR_PTR(-ENOMEM); 14591a27b2aSJeff Layton goto error_free_name; 14691a27b2aSJeff Layton } 14791a27b2aSJeff Layton 14891a27b2aSJeff Layton result->name = kname; 14991a27b2aSJeff Layton result->uptr = filename; 15091a27b2aSJeff Layton len = strncpy_from_user(kname, filename, PATH_MAX); 15191a27b2aSJeff Layton if (unlikely(len < 0)) { 1523f9f0aa6SLinus Torvalds err = ERR_PTR(len); 1533f9f0aa6SLinus Torvalds goto error; 15491a27b2aSJeff Layton } 1553f9f0aa6SLinus Torvalds 1563f9f0aa6SLinus Torvalds /* The empty path is special. */ 1573f9f0aa6SLinus Torvalds if (unlikely(!len)) { 1583f9f0aa6SLinus Torvalds if (empty) 1591fa1e7f6SAndy Whitcroft *empty = 1; 1603f9f0aa6SLinus Torvalds err = ERR_PTR(-ENOENT); 1613f9f0aa6SLinus Torvalds if (!(flags & LOOKUP_EMPTY)) 1623f9f0aa6SLinus Torvalds goto error; 1631da177e4SLinus Torvalds } 1643f9f0aa6SLinus Torvalds 1653f9f0aa6SLinus Torvalds err = ERR_PTR(-ENAMETOOLONG); 1663f9f0aa6SLinus Torvalds if (likely(len < PATH_MAX)) { 1671da177e4SLinus Torvalds audit_getname(result); 1681da177e4SLinus Torvalds return result; 1691da177e4SLinus Torvalds } 1701da177e4SLinus Torvalds 1713f9f0aa6SLinus Torvalds error: 17291a27b2aSJeff Layton __putname(kname); 17391a27b2aSJeff Layton error_free_name: 17491a27b2aSJeff Layton kfree(result); 1753f9f0aa6SLinus Torvalds return err; 1763f9f0aa6SLinus Torvalds } 1773f9f0aa6SLinus Torvalds 17891a27b2aSJeff Layton struct filename * 17991a27b2aSJeff Layton getname(const char __user * filename) 180f52e0c11SAl Viro { 181f7493e5dSLinus Torvalds return getname_flags(filename, 0, NULL); 182f52e0c11SAl Viro } 18391a27b2aSJeff Layton EXPORT_SYMBOL(getname); 184f52e0c11SAl Viro 1851da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL 18691a27b2aSJeff Layton void putname(struct filename *name) 1871da177e4SLinus Torvalds { 1885ac3a9c2SAl Viro if (unlikely(!audit_dummy_context())) 18991a27b2aSJeff Layton return audit_putname(name); 19091a27b2aSJeff Layton final_putname(name); 1911da177e4SLinus Torvalds } 1921da177e4SLinus Torvalds #endif 1931da177e4SLinus Torvalds 194e77819e5SLinus Torvalds static int check_acl(struct inode *inode, int mask) 195e77819e5SLinus Torvalds { 19684635d68SLinus Torvalds #ifdef CONFIG_FS_POSIX_ACL 197e77819e5SLinus Torvalds struct posix_acl *acl; 198e77819e5SLinus Torvalds 199e77819e5SLinus Torvalds if (mask & MAY_NOT_BLOCK) { 2003567866bSAl Viro acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS); 2013567866bSAl Viro if (!acl) 202e77819e5SLinus Torvalds return -EAGAIN; 2033567866bSAl Viro /* no ->get_acl() calls in RCU mode... */ 2043567866bSAl Viro if (acl == ACL_NOT_CACHED) 205e77819e5SLinus Torvalds return -ECHILD; 206206b1d09SAri Savolainen return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK); 207e77819e5SLinus Torvalds } 208e77819e5SLinus Torvalds 209e77819e5SLinus Torvalds acl = get_cached_acl(inode, ACL_TYPE_ACCESS); 210e77819e5SLinus Torvalds 211e77819e5SLinus Torvalds /* 2124e34e719SChristoph Hellwig * A filesystem can force a ACL callback by just never filling the 2134e34e719SChristoph Hellwig * ACL cache. But normally you'd fill the cache either at inode 2144e34e719SChristoph Hellwig * instantiation time, or on the first ->get_acl call. 215e77819e5SLinus Torvalds * 2164e34e719SChristoph Hellwig * If the filesystem doesn't have a get_acl() function at all, we'll 2174e34e719SChristoph Hellwig * just create the negative cache entry. 218e77819e5SLinus Torvalds */ 219e77819e5SLinus Torvalds if (acl == ACL_NOT_CACHED) { 2204e34e719SChristoph Hellwig if (inode->i_op->get_acl) { 2214e34e719SChristoph Hellwig acl = inode->i_op->get_acl(inode, ACL_TYPE_ACCESS); 2224e34e719SChristoph Hellwig if (IS_ERR(acl)) 2234e34e719SChristoph Hellwig return PTR_ERR(acl); 2244e34e719SChristoph Hellwig } else { 225e77819e5SLinus Torvalds set_cached_acl(inode, ACL_TYPE_ACCESS, NULL); 226e77819e5SLinus Torvalds return -EAGAIN; 227e77819e5SLinus Torvalds } 2284e34e719SChristoph Hellwig } 229e77819e5SLinus Torvalds 230e77819e5SLinus Torvalds if (acl) { 231e77819e5SLinus Torvalds int error = posix_acl_permission(inode, acl, mask); 232e77819e5SLinus Torvalds posix_acl_release(acl); 233e77819e5SLinus Torvalds return error; 234e77819e5SLinus Torvalds } 23584635d68SLinus Torvalds #endif 236e77819e5SLinus Torvalds 237e77819e5SLinus Torvalds return -EAGAIN; 238e77819e5SLinus Torvalds } 239e77819e5SLinus Torvalds 2405909ccaaSLinus Torvalds /* 241948409c7SAndreas Gruenbacher * This does the basic permission checking 2425909ccaaSLinus Torvalds */ 2437e40145eSAl Viro static int acl_permission_check(struct inode *inode, int mask) 2445909ccaaSLinus Torvalds { 24526cf46beSLinus Torvalds unsigned int mode = inode->i_mode; 2465909ccaaSLinus Torvalds 2478e96e3b7SEric W. Biederman if (likely(uid_eq(current_fsuid(), inode->i_uid))) 2485909ccaaSLinus Torvalds mode >>= 6; 2495909ccaaSLinus Torvalds else { 250e77819e5SLinus Torvalds if (IS_POSIXACL(inode) && (mode & S_IRWXG)) { 2517e40145eSAl Viro int error = check_acl(inode, mask); 2525909ccaaSLinus Torvalds if (error != -EAGAIN) 2535909ccaaSLinus Torvalds return error; 2545909ccaaSLinus Torvalds } 2555909ccaaSLinus Torvalds 2565909ccaaSLinus Torvalds if (in_group_p(inode->i_gid)) 2575909ccaaSLinus Torvalds mode >>= 3; 2585909ccaaSLinus Torvalds } 2595909ccaaSLinus Torvalds 2605909ccaaSLinus Torvalds /* 2615909ccaaSLinus Torvalds * If the DACs are ok we don't need any capability check. 2625909ccaaSLinus Torvalds */ 2639c2c7039SAl Viro if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0) 2645909ccaaSLinus Torvalds return 0; 2655909ccaaSLinus Torvalds return -EACCES; 2665909ccaaSLinus Torvalds } 2671da177e4SLinus Torvalds 2681da177e4SLinus Torvalds /** 2691da177e4SLinus Torvalds * generic_permission - check for access rights on a Posix-like filesystem 2701da177e4SLinus Torvalds * @inode: inode to check access rights for 2718fd90c8dSAndreas Gruenbacher * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...) 2721da177e4SLinus Torvalds * 2731da177e4SLinus Torvalds * Used to check for read/write/execute permissions on a file. 2741da177e4SLinus Torvalds * We use "fsuid" for this, letting us set arbitrary permissions 2751da177e4SLinus Torvalds * for filesystem access without changing the "normal" uids which 276b74c79e9SNick Piggin * are used for other things. 277b74c79e9SNick Piggin * 278b74c79e9SNick Piggin * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk 279b74c79e9SNick Piggin * request cannot be satisfied (eg. requires blocking or too much complexity). 280b74c79e9SNick Piggin * It would then be called again in ref-walk mode. 2811da177e4SLinus Torvalds */ 2822830ba7fSAl Viro int generic_permission(struct inode *inode, int mask) 2831da177e4SLinus Torvalds { 2845909ccaaSLinus Torvalds int ret; 2851da177e4SLinus Torvalds 2861da177e4SLinus Torvalds /* 287948409c7SAndreas Gruenbacher * Do the basic permission checks. 2881da177e4SLinus Torvalds */ 2897e40145eSAl Viro ret = acl_permission_check(inode, mask); 2905909ccaaSLinus Torvalds if (ret != -EACCES) 2915909ccaaSLinus Torvalds return ret; 2921da177e4SLinus Torvalds 293d594e7ecSAl Viro if (S_ISDIR(inode->i_mode)) { 294d594e7ecSAl Viro /* DACs are overridable for directories */ 2951a48e2acSEric W. Biederman if (inode_capable(inode, CAP_DAC_OVERRIDE)) 296d594e7ecSAl Viro return 0; 297d594e7ecSAl Viro if (!(mask & MAY_WRITE)) 2981a48e2acSEric W. Biederman if (inode_capable(inode, CAP_DAC_READ_SEARCH)) 299d594e7ecSAl Viro return 0; 300d594e7ecSAl Viro return -EACCES; 301d594e7ecSAl Viro } 3021da177e4SLinus Torvalds /* 3031da177e4SLinus Torvalds * Read/write DACs are always overridable. 304d594e7ecSAl Viro * Executable DACs are overridable when there is 305d594e7ecSAl Viro * at least one exec bit set. 3061da177e4SLinus Torvalds */ 307d594e7ecSAl Viro if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO)) 3081a48e2acSEric W. Biederman if (inode_capable(inode, CAP_DAC_OVERRIDE)) 3091da177e4SLinus Torvalds return 0; 3101da177e4SLinus Torvalds 3111da177e4SLinus Torvalds /* 3121da177e4SLinus Torvalds * Searching includes executable on directories, else just read. 3131da177e4SLinus Torvalds */ 3147ea66001SSerge E. Hallyn mask &= MAY_READ | MAY_WRITE | MAY_EXEC; 315d594e7ecSAl Viro if (mask == MAY_READ) 3161a48e2acSEric W. Biederman if (inode_capable(inode, CAP_DAC_READ_SEARCH)) 3171da177e4SLinus Torvalds return 0; 3181da177e4SLinus Torvalds 3191da177e4SLinus Torvalds return -EACCES; 3201da177e4SLinus Torvalds } 3211da177e4SLinus Torvalds 3223ddcd056SLinus Torvalds /* 3233ddcd056SLinus Torvalds * We _really_ want to just do "generic_permission()" without 3243ddcd056SLinus Torvalds * even looking at the inode->i_op values. So we keep a cache 3253ddcd056SLinus Torvalds * flag in inode->i_opflags, that says "this has not special 3263ddcd056SLinus Torvalds * permission function, use the fast case". 3273ddcd056SLinus Torvalds */ 3283ddcd056SLinus Torvalds static inline int do_inode_permission(struct inode *inode, int mask) 3293ddcd056SLinus Torvalds { 3303ddcd056SLinus Torvalds if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) { 3313ddcd056SLinus Torvalds if (likely(inode->i_op->permission)) 3323ddcd056SLinus Torvalds return inode->i_op->permission(inode, mask); 3333ddcd056SLinus Torvalds 3343ddcd056SLinus Torvalds /* This gets set once for the inode lifetime */ 3353ddcd056SLinus Torvalds spin_lock(&inode->i_lock); 3363ddcd056SLinus Torvalds inode->i_opflags |= IOP_FASTPERM; 3373ddcd056SLinus Torvalds spin_unlock(&inode->i_lock); 3383ddcd056SLinus Torvalds } 3393ddcd056SLinus Torvalds return generic_permission(inode, mask); 3403ddcd056SLinus Torvalds } 3413ddcd056SLinus Torvalds 342cb23beb5SChristoph Hellwig /** 3430bdaea90SDavid Howells * __inode_permission - Check for access rights to a given inode 3440bdaea90SDavid Howells * @inode: Inode to check permission on 3450bdaea90SDavid Howells * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) 346cb23beb5SChristoph Hellwig * 3470bdaea90SDavid Howells * Check for read/write/execute permissions on an inode. 348948409c7SAndreas Gruenbacher * 349948409c7SAndreas Gruenbacher * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask. 3500bdaea90SDavid Howells * 3510bdaea90SDavid Howells * This does not check for a read-only file system. You probably want 3520bdaea90SDavid Howells * inode_permission(). 353cb23beb5SChristoph Hellwig */ 3540bdaea90SDavid Howells int __inode_permission(struct inode *inode, int mask) 3551da177e4SLinus Torvalds { 356e6305c43SAl Viro int retval; 3571da177e4SLinus Torvalds 3583ddcd056SLinus Torvalds if (unlikely(mask & MAY_WRITE)) { 3591da177e4SLinus Torvalds /* 3601da177e4SLinus Torvalds * Nobody gets write access to an immutable file. 3611da177e4SLinus Torvalds */ 3621da177e4SLinus Torvalds if (IS_IMMUTABLE(inode)) 3631da177e4SLinus Torvalds return -EACCES; 3641da177e4SLinus Torvalds } 3651da177e4SLinus Torvalds 3663ddcd056SLinus Torvalds retval = do_inode_permission(inode, mask); 3671da177e4SLinus Torvalds if (retval) 3681da177e4SLinus Torvalds return retval; 3691da177e4SLinus Torvalds 37008ce5f16SSerge E. Hallyn retval = devcgroup_inode_permission(inode, mask); 37108ce5f16SSerge E. Hallyn if (retval) 37208ce5f16SSerge E. Hallyn return retval; 37308ce5f16SSerge E. Hallyn 374d09ca739SEric Paris return security_inode_permission(inode, mask); 3751da177e4SLinus Torvalds } 3761da177e4SLinus Torvalds 377f4d6ff89SAl Viro /** 3780bdaea90SDavid Howells * sb_permission - Check superblock-level permissions 3790bdaea90SDavid Howells * @sb: Superblock of inode to check permission on 38055852635SRandy Dunlap * @inode: Inode to check permission on 3810bdaea90SDavid Howells * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) 3820bdaea90SDavid Howells * 3830bdaea90SDavid Howells * Separate out file-system wide checks from inode-specific permission checks. 3840bdaea90SDavid Howells */ 3850bdaea90SDavid Howells static int sb_permission(struct super_block *sb, struct inode *inode, int mask) 3860bdaea90SDavid Howells { 3870bdaea90SDavid Howells if (unlikely(mask & MAY_WRITE)) { 3880bdaea90SDavid Howells umode_t mode = inode->i_mode; 3890bdaea90SDavid Howells 3900bdaea90SDavid Howells /* Nobody gets write access to a read-only fs. */ 3910bdaea90SDavid Howells if ((sb->s_flags & MS_RDONLY) && 3920bdaea90SDavid Howells (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) 3930bdaea90SDavid Howells return -EROFS; 3940bdaea90SDavid Howells } 3950bdaea90SDavid Howells return 0; 3960bdaea90SDavid Howells } 3970bdaea90SDavid Howells 3980bdaea90SDavid Howells /** 3990bdaea90SDavid Howells * inode_permission - Check for access rights to a given inode 4000bdaea90SDavid Howells * @inode: Inode to check permission on 4010bdaea90SDavid Howells * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) 4020bdaea90SDavid Howells * 4030bdaea90SDavid Howells * Check for read/write/execute permissions on an inode. We use fs[ug]id for 4040bdaea90SDavid Howells * this, letting us set arbitrary permissions for filesystem access without 4050bdaea90SDavid Howells * changing the "normal" UIDs which are used for other things. 4060bdaea90SDavid Howells * 4070bdaea90SDavid Howells * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask. 4080bdaea90SDavid Howells */ 4090bdaea90SDavid Howells int inode_permission(struct inode *inode, int mask) 4100bdaea90SDavid Howells { 4110bdaea90SDavid Howells int retval; 4120bdaea90SDavid Howells 4130bdaea90SDavid Howells retval = sb_permission(inode->i_sb, inode, mask); 4140bdaea90SDavid Howells if (retval) 4150bdaea90SDavid Howells return retval; 4160bdaea90SDavid Howells return __inode_permission(inode, mask); 4170bdaea90SDavid Howells } 4180bdaea90SDavid Howells 4190bdaea90SDavid Howells /** 4205dd784d0SJan Blunck * path_get - get a reference to a path 4215dd784d0SJan Blunck * @path: path to get the reference to 4225dd784d0SJan Blunck * 4235dd784d0SJan Blunck * Given a path increment the reference count to the dentry and the vfsmount. 4245dd784d0SJan Blunck */ 4255dd784d0SJan Blunck void path_get(struct path *path) 4265dd784d0SJan Blunck { 4275dd784d0SJan Blunck mntget(path->mnt); 4285dd784d0SJan Blunck dget(path->dentry); 4295dd784d0SJan Blunck } 4305dd784d0SJan Blunck EXPORT_SYMBOL(path_get); 4315dd784d0SJan Blunck 4325dd784d0SJan Blunck /** 4331d957f9bSJan Blunck * path_put - put a reference to a path 4341d957f9bSJan Blunck * @path: path to put the reference to 4351d957f9bSJan Blunck * 4361d957f9bSJan Blunck * Given a path decrement the reference count to the dentry and the vfsmount. 4371d957f9bSJan Blunck */ 4381d957f9bSJan Blunck void path_put(struct path *path) 4391da177e4SLinus Torvalds { 4401d957f9bSJan Blunck dput(path->dentry); 4411d957f9bSJan Blunck mntput(path->mnt); 4421da177e4SLinus Torvalds } 4431d957f9bSJan Blunck EXPORT_SYMBOL(path_put); 4441da177e4SLinus Torvalds 44519660af7SAl Viro /* 44631e6b01fSNick Piggin * Path walking has 2 modes, rcu-walk and ref-walk (see 44719660af7SAl Viro * Documentation/filesystems/path-lookup.txt). In situations when we can't 44819660af7SAl Viro * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab 44919660af7SAl Viro * normal reference counts on dentries and vfsmounts to transition to rcu-walk 45019660af7SAl Viro * mode. Refcounts are grabbed at the last known good point before rcu-walk 45119660af7SAl Viro * got stuck, so ref-walk may continue from there. If this is not successful 45219660af7SAl Viro * (eg. a seqcount has changed), then failure is returned and it's up to caller 45319660af7SAl Viro * to restart the path walk from the beginning in ref-walk mode. 45431e6b01fSNick Piggin */ 45531e6b01fSNick Piggin 45632a7991bSAl Viro static inline void lock_rcu_walk(void) 45732a7991bSAl Viro { 45832a7991bSAl Viro br_read_lock(&vfsmount_lock); 45932a7991bSAl Viro rcu_read_lock(); 46032a7991bSAl Viro } 46132a7991bSAl Viro 46232a7991bSAl Viro static inline void unlock_rcu_walk(void) 46332a7991bSAl Viro { 46432a7991bSAl Viro rcu_read_unlock(); 46532a7991bSAl Viro br_read_unlock(&vfsmount_lock); 46632a7991bSAl Viro } 46732a7991bSAl Viro 46831e6b01fSNick Piggin /** 46919660af7SAl Viro * unlazy_walk - try to switch to ref-walk mode. 47019660af7SAl Viro * @nd: nameidata pathwalk data 47119660af7SAl Viro * @dentry: child of nd->path.dentry or NULL 47239191628SRandy Dunlap * Returns: 0 on success, -ECHILD on failure 47331e6b01fSNick Piggin * 47419660af7SAl Viro * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry 47519660af7SAl Viro * for ref-walk mode. @dentry must be a path found by a do_lookup call on 47619660af7SAl Viro * @nd or NULL. Must be called from rcu-walk context. 47731e6b01fSNick Piggin */ 47819660af7SAl Viro static int unlazy_walk(struct nameidata *nd, struct dentry *dentry) 47931e6b01fSNick Piggin { 48031e6b01fSNick Piggin struct fs_struct *fs = current->fs; 48131e6b01fSNick Piggin struct dentry *parent = nd->path.dentry; 4825b6ca027SAl Viro int want_root = 0; 48331e6b01fSNick Piggin 48431e6b01fSNick Piggin BUG_ON(!(nd->flags & LOOKUP_RCU)); 4855b6ca027SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) { 4865b6ca027SAl Viro want_root = 1; 48731e6b01fSNick Piggin spin_lock(&fs->lock); 48831e6b01fSNick Piggin if (nd->root.mnt != fs->root.mnt || 48931e6b01fSNick Piggin nd->root.dentry != fs->root.dentry) 49031e6b01fSNick Piggin goto err_root; 49131e6b01fSNick Piggin } 49231e6b01fSNick Piggin spin_lock(&parent->d_lock); 49319660af7SAl Viro if (!dentry) { 49419660af7SAl Viro if (!__d_rcu_to_refcount(parent, nd->seq)) 49519660af7SAl Viro goto err_parent; 49619660af7SAl Viro BUG_ON(nd->inode != parent->d_inode); 49719660af7SAl Viro } else { 49894c0d4ecSAl Viro if (dentry->d_parent != parent) 49994c0d4ecSAl Viro goto err_parent; 50031e6b01fSNick Piggin spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); 50131e6b01fSNick Piggin if (!__d_rcu_to_refcount(dentry, nd->seq)) 50219660af7SAl Viro goto err_child; 50331e6b01fSNick Piggin /* 50419660af7SAl Viro * If the sequence check on the child dentry passed, then 50519660af7SAl Viro * the child has not been removed from its parent. This 50619660af7SAl Viro * means the parent dentry must be valid and able to take 50719660af7SAl Viro * a reference at this point. 50831e6b01fSNick Piggin */ 50931e6b01fSNick Piggin BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent); 51031e6b01fSNick Piggin BUG_ON(!parent->d_count); 51131e6b01fSNick Piggin parent->d_count++; 51231e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 51319660af7SAl Viro } 51431e6b01fSNick Piggin spin_unlock(&parent->d_lock); 5155b6ca027SAl Viro if (want_root) { 51631e6b01fSNick Piggin path_get(&nd->root); 51731e6b01fSNick Piggin spin_unlock(&fs->lock); 51831e6b01fSNick Piggin } 51931e6b01fSNick Piggin mntget(nd->path.mnt); 52031e6b01fSNick Piggin 52132a7991bSAl Viro unlock_rcu_walk(); 52231e6b01fSNick Piggin nd->flags &= ~LOOKUP_RCU; 52331e6b01fSNick Piggin return 0; 52419660af7SAl Viro 52519660af7SAl Viro err_child: 52631e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 52719660af7SAl Viro err_parent: 52831e6b01fSNick Piggin spin_unlock(&parent->d_lock); 52931e6b01fSNick Piggin err_root: 5305b6ca027SAl Viro if (want_root) 53131e6b01fSNick Piggin spin_unlock(&fs->lock); 53231e6b01fSNick Piggin return -ECHILD; 53331e6b01fSNick Piggin } 53431e6b01fSNick Piggin 5354ce16ef3SAl Viro static inline int d_revalidate(struct dentry *dentry, unsigned int flags) 53634286d66SNick Piggin { 5374ce16ef3SAl Viro return dentry->d_op->d_revalidate(dentry, flags); 53834286d66SNick Piggin } 53934286d66SNick Piggin 5409f1fafeeSAl Viro /** 5419f1fafeeSAl Viro * complete_walk - successful completion of path walk 5429f1fafeeSAl Viro * @nd: pointer nameidata 54339159de2SJeff Layton * 5449f1fafeeSAl Viro * If we had been in RCU mode, drop out of it and legitimize nd->path. 5459f1fafeeSAl Viro * Revalidate the final result, unless we'd already done that during 5469f1fafeeSAl Viro * the path walk or the filesystem doesn't ask for it. Return 0 on 5479f1fafeeSAl Viro * success, -error on failure. In case of failure caller does not 5489f1fafeeSAl Viro * need to drop nd->path. 54939159de2SJeff Layton */ 5509f1fafeeSAl Viro static int complete_walk(struct nameidata *nd) 55139159de2SJeff Layton { 55216c2cd71SAl Viro struct dentry *dentry = nd->path.dentry; 55339159de2SJeff Layton int status; 55439159de2SJeff Layton 5559f1fafeeSAl Viro if (nd->flags & LOOKUP_RCU) { 5569f1fafeeSAl Viro nd->flags &= ~LOOKUP_RCU; 5579f1fafeeSAl Viro if (!(nd->flags & LOOKUP_ROOT)) 5589f1fafeeSAl Viro nd->root.mnt = NULL; 5599f1fafeeSAl Viro spin_lock(&dentry->d_lock); 5609f1fafeeSAl Viro if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) { 5619f1fafeeSAl Viro spin_unlock(&dentry->d_lock); 56232a7991bSAl Viro unlock_rcu_walk(); 5639f1fafeeSAl Viro return -ECHILD; 5649f1fafeeSAl Viro } 5659f1fafeeSAl Viro BUG_ON(nd->inode != dentry->d_inode); 5669f1fafeeSAl Viro spin_unlock(&dentry->d_lock); 5679f1fafeeSAl Viro mntget(nd->path.mnt); 56832a7991bSAl Viro unlock_rcu_walk(); 5699f1fafeeSAl Viro } 5709f1fafeeSAl Viro 57116c2cd71SAl Viro if (likely(!(nd->flags & LOOKUP_JUMPED))) 57239159de2SJeff Layton return 0; 57339159de2SJeff Layton 57416c2cd71SAl Viro if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE))) 57516c2cd71SAl Viro return 0; 57616c2cd71SAl Viro 57716c2cd71SAl Viro if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT))) 57816c2cd71SAl Viro return 0; 57916c2cd71SAl Viro 58016c2cd71SAl Viro /* Note: we do not d_invalidate() */ 5814ce16ef3SAl Viro status = d_revalidate(dentry, nd->flags); 58239159de2SJeff Layton if (status > 0) 58339159de2SJeff Layton return 0; 58439159de2SJeff Layton 58516c2cd71SAl Viro if (!status) 58639159de2SJeff Layton status = -ESTALE; 58716c2cd71SAl Viro 5889f1fafeeSAl Viro path_put(&nd->path); 58939159de2SJeff Layton return status; 59039159de2SJeff Layton } 59139159de2SJeff Layton 5922a737871SAl Viro static __always_inline void set_root(struct nameidata *nd) 5932a737871SAl Viro { 594f7ad3c6bSMiklos Szeredi if (!nd->root.mnt) 595f7ad3c6bSMiklos Szeredi get_fs_root(current->fs, &nd->root); 5962a737871SAl Viro } 5972a737871SAl Viro 5986de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *); 5996de88d72SAl Viro 60031e6b01fSNick Piggin static __always_inline void set_root_rcu(struct nameidata *nd) 60131e6b01fSNick Piggin { 60231e6b01fSNick Piggin if (!nd->root.mnt) { 60331e6b01fSNick Piggin struct fs_struct *fs = current->fs; 604c28cc364SNick Piggin unsigned seq; 605c28cc364SNick Piggin 606c28cc364SNick Piggin do { 607c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 60831e6b01fSNick Piggin nd->root = fs->root; 609c1530019STim Chen nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq); 610c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 61131e6b01fSNick Piggin } 61231e6b01fSNick Piggin } 61331e6b01fSNick Piggin 614f1662356SArjan van de Ven static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link) 6151da177e4SLinus Torvalds { 61631e6b01fSNick Piggin int ret; 61731e6b01fSNick Piggin 6181da177e4SLinus Torvalds if (IS_ERR(link)) 6191da177e4SLinus Torvalds goto fail; 6201da177e4SLinus Torvalds 6211da177e4SLinus Torvalds if (*link == '/') { 6222a737871SAl Viro set_root(nd); 6231d957f9bSJan Blunck path_put(&nd->path); 6242a737871SAl Viro nd->path = nd->root; 6252a737871SAl Viro path_get(&nd->root); 62616c2cd71SAl Viro nd->flags |= LOOKUP_JUMPED; 6271da177e4SLinus Torvalds } 62831e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 629b4091d5fSChristoph Hellwig 63031e6b01fSNick Piggin ret = link_path_walk(link, nd); 63131e6b01fSNick Piggin return ret; 6321da177e4SLinus Torvalds fail: 6331d957f9bSJan Blunck path_put(&nd->path); 6341da177e4SLinus Torvalds return PTR_ERR(link); 6351da177e4SLinus Torvalds } 6361da177e4SLinus Torvalds 6371d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd) 638051d3812SIan Kent { 639051d3812SIan Kent dput(path->dentry); 6404ac91378SJan Blunck if (path->mnt != nd->path.mnt) 641051d3812SIan Kent mntput(path->mnt); 642051d3812SIan Kent } 643051d3812SIan Kent 6447b9337aaSNick Piggin static inline void path_to_nameidata(const struct path *path, 6457b9337aaSNick Piggin struct nameidata *nd) 646051d3812SIan Kent { 64731e6b01fSNick Piggin if (!(nd->flags & LOOKUP_RCU)) { 6484ac91378SJan Blunck dput(nd->path.dentry); 64931e6b01fSNick Piggin if (nd->path.mnt != path->mnt) 6504ac91378SJan Blunck mntput(nd->path.mnt); 6519a229683SHuang Shijie } 65231e6b01fSNick Piggin nd->path.mnt = path->mnt; 6534ac91378SJan Blunck nd->path.dentry = path->dentry; 654051d3812SIan Kent } 655051d3812SIan Kent 656b5fb63c1SChristoph Hellwig /* 657b5fb63c1SChristoph Hellwig * Helper to directly jump to a known parsed path from ->follow_link, 658b5fb63c1SChristoph Hellwig * caller must have taken a reference to path beforehand. 659b5fb63c1SChristoph Hellwig */ 660b5fb63c1SChristoph Hellwig void nd_jump_link(struct nameidata *nd, struct path *path) 661b5fb63c1SChristoph Hellwig { 662b5fb63c1SChristoph Hellwig path_put(&nd->path); 663b5fb63c1SChristoph Hellwig 664b5fb63c1SChristoph Hellwig nd->path = *path; 665b5fb63c1SChristoph Hellwig nd->inode = nd->path.dentry->d_inode; 666b5fb63c1SChristoph Hellwig nd->flags |= LOOKUP_JUMPED; 667b5fb63c1SChristoph Hellwig 668b5fb63c1SChristoph Hellwig BUG_ON(nd->inode->i_op->follow_link); 669b5fb63c1SChristoph Hellwig } 670b5fb63c1SChristoph Hellwig 671574197e0SAl Viro static inline void put_link(struct nameidata *nd, struct path *link, void *cookie) 672574197e0SAl Viro { 673574197e0SAl Viro struct inode *inode = link->dentry->d_inode; 6746d7b5aaeSAl Viro if (inode->i_op->put_link) 675574197e0SAl Viro inode->i_op->put_link(link->dentry, nd, cookie); 676574197e0SAl Viro path_put(link); 677574197e0SAl Viro } 678574197e0SAl Viro 679800179c9SKees Cook int sysctl_protected_symlinks __read_mostly = 1; 680800179c9SKees Cook int sysctl_protected_hardlinks __read_mostly = 1; 681800179c9SKees Cook 682800179c9SKees Cook /** 683800179c9SKees Cook * may_follow_link - Check symlink following for unsafe situations 684800179c9SKees Cook * @link: The path of the symlink 68555852635SRandy Dunlap * @nd: nameidata pathwalk data 686800179c9SKees Cook * 687800179c9SKees Cook * In the case of the sysctl_protected_symlinks sysctl being enabled, 688800179c9SKees Cook * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is 689800179c9SKees Cook * in a sticky world-writable directory. This is to protect privileged 690800179c9SKees Cook * processes from failing races against path names that may change out 691800179c9SKees Cook * from under them by way of other users creating malicious symlinks. 692800179c9SKees Cook * It will permit symlinks to be followed only when outside a sticky 693800179c9SKees Cook * world-writable directory, or when the uid of the symlink and follower 694800179c9SKees Cook * match, or when the directory owner matches the symlink's owner. 695800179c9SKees Cook * 696800179c9SKees Cook * Returns 0 if following the symlink is allowed, -ve on error. 697800179c9SKees Cook */ 698800179c9SKees Cook static inline int may_follow_link(struct path *link, struct nameidata *nd) 699800179c9SKees Cook { 700800179c9SKees Cook const struct inode *inode; 701800179c9SKees Cook const struct inode *parent; 702800179c9SKees Cook 703800179c9SKees Cook if (!sysctl_protected_symlinks) 704800179c9SKees Cook return 0; 705800179c9SKees Cook 706800179c9SKees Cook /* Allowed if owner and follower match. */ 707800179c9SKees Cook inode = link->dentry->d_inode; 70881abe27bSEric W. Biederman if (uid_eq(current_cred()->fsuid, inode->i_uid)) 709800179c9SKees Cook return 0; 710800179c9SKees Cook 711800179c9SKees Cook /* Allowed if parent directory not sticky and world-writable. */ 712800179c9SKees Cook parent = nd->path.dentry->d_inode; 713800179c9SKees Cook if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH)) 714800179c9SKees Cook return 0; 715800179c9SKees Cook 716800179c9SKees Cook /* Allowed if parent directory and link owner match. */ 71781abe27bSEric W. Biederman if (uid_eq(parent->i_uid, inode->i_uid)) 718800179c9SKees Cook return 0; 719800179c9SKees Cook 720ffd8d101SSasha Levin audit_log_link_denied("follow_link", link); 721800179c9SKees Cook path_put_conditional(link, nd); 722800179c9SKees Cook path_put(&nd->path); 723800179c9SKees Cook return -EACCES; 724800179c9SKees Cook } 725800179c9SKees Cook 726800179c9SKees Cook /** 727800179c9SKees Cook * safe_hardlink_source - Check for safe hardlink conditions 728800179c9SKees Cook * @inode: the source inode to hardlink from 729800179c9SKees Cook * 730800179c9SKees Cook * Return false if at least one of the following conditions: 731800179c9SKees Cook * - inode is not a regular file 732800179c9SKees Cook * - inode is setuid 733800179c9SKees Cook * - inode is setgid and group-exec 734800179c9SKees Cook * - access failure for read and write 735800179c9SKees Cook * 736800179c9SKees Cook * Otherwise returns true. 737800179c9SKees Cook */ 738800179c9SKees Cook static bool safe_hardlink_source(struct inode *inode) 739800179c9SKees Cook { 740800179c9SKees Cook umode_t mode = inode->i_mode; 741800179c9SKees Cook 742800179c9SKees Cook /* Special files should not get pinned to the filesystem. */ 743800179c9SKees Cook if (!S_ISREG(mode)) 744800179c9SKees Cook return false; 745800179c9SKees Cook 746800179c9SKees Cook /* Setuid files should not get pinned to the filesystem. */ 747800179c9SKees Cook if (mode & S_ISUID) 748800179c9SKees Cook return false; 749800179c9SKees Cook 750800179c9SKees Cook /* Executable setgid files should not get pinned to the filesystem. */ 751800179c9SKees Cook if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) 752800179c9SKees Cook return false; 753800179c9SKees Cook 754800179c9SKees Cook /* Hardlinking to unreadable or unwritable sources is dangerous. */ 755800179c9SKees Cook if (inode_permission(inode, MAY_READ | MAY_WRITE)) 756800179c9SKees Cook return false; 757800179c9SKees Cook 758800179c9SKees Cook return true; 759800179c9SKees Cook } 760800179c9SKees Cook 761800179c9SKees Cook /** 762800179c9SKees Cook * may_linkat - Check permissions for creating a hardlink 763800179c9SKees Cook * @link: the source to hardlink from 764800179c9SKees Cook * 765800179c9SKees Cook * Block hardlink when all of: 766800179c9SKees Cook * - sysctl_protected_hardlinks enabled 767800179c9SKees Cook * - fsuid does not match inode 768800179c9SKees Cook * - hardlink source is unsafe (see safe_hardlink_source() above) 769800179c9SKees Cook * - not CAP_FOWNER 770800179c9SKees Cook * 771800179c9SKees Cook * Returns 0 if successful, -ve on error. 772800179c9SKees Cook */ 773800179c9SKees Cook static int may_linkat(struct path *link) 774800179c9SKees Cook { 775800179c9SKees Cook const struct cred *cred; 776800179c9SKees Cook struct inode *inode; 777800179c9SKees Cook 778800179c9SKees Cook if (!sysctl_protected_hardlinks) 779800179c9SKees Cook return 0; 780800179c9SKees Cook 781800179c9SKees Cook cred = current_cred(); 782800179c9SKees Cook inode = link->dentry->d_inode; 783800179c9SKees Cook 784800179c9SKees Cook /* Source inode owner (or CAP_FOWNER) can hardlink all they like, 785800179c9SKees Cook * otherwise, it must be a safe source. 786800179c9SKees Cook */ 78781abe27bSEric W. Biederman if (uid_eq(cred->fsuid, inode->i_uid) || safe_hardlink_source(inode) || 788800179c9SKees Cook capable(CAP_FOWNER)) 789800179c9SKees Cook return 0; 790800179c9SKees Cook 791a51d9eaaSKees Cook audit_log_link_denied("linkat", link); 792800179c9SKees Cook return -EPERM; 793800179c9SKees Cook } 794800179c9SKees Cook 795def4af30SAl Viro static __always_inline int 796574197e0SAl Viro follow_link(struct path *link, struct nameidata *nd, void **p) 7971da177e4SLinus Torvalds { 7987b9337aaSNick Piggin struct dentry *dentry = link->dentry; 7996d7b5aaeSAl Viro int error; 8006d7b5aaeSAl Viro char *s; 8011da177e4SLinus Torvalds 802844a3917SAl Viro BUG_ON(nd->flags & LOOKUP_RCU); 803844a3917SAl Viro 8040e794589SAl Viro if (link->mnt == nd->path.mnt) 8050e794589SAl Viro mntget(link->mnt); 8060e794589SAl Viro 8076d7b5aaeSAl Viro error = -ELOOP; 8086d7b5aaeSAl Viro if (unlikely(current->total_link_count >= 40)) 8096d7b5aaeSAl Viro goto out_put_nd_path; 8106d7b5aaeSAl Viro 811574197e0SAl Viro cond_resched(); 812574197e0SAl Viro current->total_link_count++; 813574197e0SAl Viro 81468ac1234SAl Viro touch_atime(link); 8151da177e4SLinus Torvalds nd_set_link(nd, NULL); 816cd4e91d3SAl Viro 81736f3b4f6SAl Viro error = security_inode_follow_link(link->dentry, nd); 8186d7b5aaeSAl Viro if (error) 8196d7b5aaeSAl Viro goto out_put_nd_path; 82036f3b4f6SAl Viro 82186acdca1SAl Viro nd->last_type = LAST_BIND; 822def4af30SAl Viro *p = dentry->d_inode->i_op->follow_link(dentry, nd); 823def4af30SAl Viro error = PTR_ERR(*p); 8246d7b5aaeSAl Viro if (IS_ERR(*p)) 825408ef013SChristoph Hellwig goto out_put_nd_path; 8266d7b5aaeSAl Viro 827cc314eefSLinus Torvalds error = 0; 8286d7b5aaeSAl Viro s = nd_get_link(nd); 8296d7b5aaeSAl Viro if (s) { 8301da177e4SLinus Torvalds error = __vfs_follow_link(nd, s); 8316d7b5aaeSAl Viro if (unlikely(error)) 8326d7b5aaeSAl Viro put_link(nd, link, *p); 833b5fb63c1SChristoph Hellwig } 8346d7b5aaeSAl Viro 8356d7b5aaeSAl Viro return error; 8366d7b5aaeSAl Viro 8376d7b5aaeSAl Viro out_put_nd_path: 83898f6ef64SArnd Bergmann *p = NULL; 8396d7b5aaeSAl Viro path_put(&nd->path); 8406d7b5aaeSAl Viro path_put(link); 8411da177e4SLinus Torvalds return error; 8421da177e4SLinus Torvalds } 8431da177e4SLinus Torvalds 84431e6b01fSNick Piggin static int follow_up_rcu(struct path *path) 84531e6b01fSNick Piggin { 8460714a533SAl Viro struct mount *mnt = real_mount(path->mnt); 8470714a533SAl Viro struct mount *parent; 84831e6b01fSNick Piggin struct dentry *mountpoint; 84931e6b01fSNick Piggin 8500714a533SAl Viro parent = mnt->mnt_parent; 8510714a533SAl Viro if (&parent->mnt == path->mnt) 85231e6b01fSNick Piggin return 0; 853a73324daSAl Viro mountpoint = mnt->mnt_mountpoint; 85431e6b01fSNick Piggin path->dentry = mountpoint; 8550714a533SAl Viro path->mnt = &parent->mnt; 85631e6b01fSNick Piggin return 1; 85731e6b01fSNick Piggin } 85831e6b01fSNick Piggin 859f015f126SDavid Howells /* 860f015f126SDavid Howells * follow_up - Find the mountpoint of path's vfsmount 861f015f126SDavid Howells * 862f015f126SDavid Howells * Given a path, find the mountpoint of its source file system. 863f015f126SDavid Howells * Replace @path with the path of the mountpoint in the parent mount. 864f015f126SDavid Howells * Up is towards /. 865f015f126SDavid Howells * 866f015f126SDavid Howells * Return 1 if we went up a level and 0 if we were already at the 867f015f126SDavid Howells * root. 868f015f126SDavid Howells */ 869bab77ebfSAl Viro int follow_up(struct path *path) 8701da177e4SLinus Torvalds { 8710714a533SAl Viro struct mount *mnt = real_mount(path->mnt); 8720714a533SAl Viro struct mount *parent; 8731da177e4SLinus Torvalds struct dentry *mountpoint; 87499b7db7bSNick Piggin 875962830dfSAndi Kleen br_read_lock(&vfsmount_lock); 8760714a533SAl Viro parent = mnt->mnt_parent; 8773c0a6163SAl Viro if (parent == mnt) { 878962830dfSAndi Kleen br_read_unlock(&vfsmount_lock); 8791da177e4SLinus Torvalds return 0; 8801da177e4SLinus Torvalds } 8810714a533SAl Viro mntget(&parent->mnt); 882a73324daSAl Viro mountpoint = dget(mnt->mnt_mountpoint); 883962830dfSAndi Kleen br_read_unlock(&vfsmount_lock); 884bab77ebfSAl Viro dput(path->dentry); 885bab77ebfSAl Viro path->dentry = mountpoint; 886bab77ebfSAl Viro mntput(path->mnt); 8870714a533SAl Viro path->mnt = &parent->mnt; 8881da177e4SLinus Torvalds return 1; 8891da177e4SLinus Torvalds } 8901da177e4SLinus Torvalds 891b5c84bf6SNick Piggin /* 8929875cf80SDavid Howells * Perform an automount 8939875cf80SDavid Howells * - return -EISDIR to tell follow_managed() to stop and return the path we 8949875cf80SDavid Howells * were called with. 8951da177e4SLinus Torvalds */ 8969875cf80SDavid Howells static int follow_automount(struct path *path, unsigned flags, 8979875cf80SDavid Howells bool *need_mntput) 89831e6b01fSNick Piggin { 8999875cf80SDavid Howells struct vfsmount *mnt; 900ea5b778aSDavid Howells int err; 9019875cf80SDavid Howells 9029875cf80SDavid Howells if (!path->dentry->d_op || !path->dentry->d_op->d_automount) 9039875cf80SDavid Howells return -EREMOTE; 9049875cf80SDavid Howells 9050ec26fd0SMiklos Szeredi /* We don't want to mount if someone's just doing a stat - 9060ec26fd0SMiklos Szeredi * unless they're stat'ing a directory and appended a '/' to 9070ec26fd0SMiklos Szeredi * the name. 9080ec26fd0SMiklos Szeredi * 9090ec26fd0SMiklos Szeredi * We do, however, want to mount if someone wants to open or 9105a30d8a2SDavid Howells * create a file of any type under the mountpoint, wants to 9110ec26fd0SMiklos Szeredi * traverse through the mountpoint or wants to open the 9120ec26fd0SMiklos Szeredi * mounted directory. Also, autofs may mark negative dentries 9130ec26fd0SMiklos Szeredi * as being automount points. These will need the attentions 9140ec26fd0SMiklos Szeredi * of the daemon to instantiate them before they can be used. 9155a30d8a2SDavid Howells */ 9165a30d8a2SDavid Howells if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY | 917d94c177bSLinus Torvalds LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) && 9185a30d8a2SDavid Howells path->dentry->d_inode) 9199875cf80SDavid Howells return -EISDIR; 9200ec26fd0SMiklos Szeredi 9219875cf80SDavid Howells current->total_link_count++; 9229875cf80SDavid Howells if (current->total_link_count >= 40) 9239875cf80SDavid Howells return -ELOOP; 9249875cf80SDavid Howells 9259875cf80SDavid Howells mnt = path->dentry->d_op->d_automount(path); 9269875cf80SDavid Howells if (IS_ERR(mnt)) { 9279875cf80SDavid Howells /* 9289875cf80SDavid Howells * The filesystem is allowed to return -EISDIR here to indicate 9299875cf80SDavid Howells * it doesn't want to automount. For instance, autofs would do 9309875cf80SDavid Howells * this so that its userspace daemon can mount on this dentry. 9319875cf80SDavid Howells * 9329875cf80SDavid Howells * However, we can only permit this if it's a terminal point in 9339875cf80SDavid Howells * the path being looked up; if it wasn't then the remainder of 9349875cf80SDavid Howells * the path is inaccessible and we should say so. 9359875cf80SDavid Howells */ 93649084c3bSAl Viro if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT)) 9379875cf80SDavid Howells return -EREMOTE; 9389875cf80SDavid Howells return PTR_ERR(mnt); 93931e6b01fSNick Piggin } 940ea5b778aSDavid Howells 9419875cf80SDavid Howells if (!mnt) /* mount collision */ 9429875cf80SDavid Howells return 0; 9439875cf80SDavid Howells 9448aef1884SAl Viro if (!*need_mntput) { 9458aef1884SAl Viro /* lock_mount() may release path->mnt on error */ 9468aef1884SAl Viro mntget(path->mnt); 9478aef1884SAl Viro *need_mntput = true; 9488aef1884SAl Viro } 94919a167afSAl Viro err = finish_automount(mnt, path); 950ea5b778aSDavid Howells 951ea5b778aSDavid Howells switch (err) { 952ea5b778aSDavid Howells case -EBUSY: 953ea5b778aSDavid Howells /* Someone else made a mount here whilst we were busy */ 95419a167afSAl Viro return 0; 955ea5b778aSDavid Howells case 0: 9568aef1884SAl Viro path_put(path); 9579875cf80SDavid Howells path->mnt = mnt; 9589875cf80SDavid Howells path->dentry = dget(mnt->mnt_root); 9599875cf80SDavid Howells return 0; 96019a167afSAl Viro default: 96119a167afSAl Viro return err; 9629875cf80SDavid Howells } 96319a167afSAl Viro 964ea5b778aSDavid Howells } 9659875cf80SDavid Howells 9669875cf80SDavid Howells /* 9679875cf80SDavid Howells * Handle a dentry that is managed in some way. 968cc53ce53SDavid Howells * - Flagged for transit management (autofs) 9699875cf80SDavid Howells * - Flagged as mountpoint 9709875cf80SDavid Howells * - Flagged as automount point 9719875cf80SDavid Howells * 9729875cf80SDavid Howells * This may only be called in refwalk mode. 9739875cf80SDavid Howells * 9749875cf80SDavid Howells * Serialization is taken care of in namespace.c 9759875cf80SDavid Howells */ 9769875cf80SDavid Howells static int follow_managed(struct path *path, unsigned flags) 9779875cf80SDavid Howells { 9788aef1884SAl Viro struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */ 9799875cf80SDavid Howells unsigned managed; 9809875cf80SDavid Howells bool need_mntput = false; 9818aef1884SAl Viro int ret = 0; 9829875cf80SDavid Howells 9839875cf80SDavid Howells /* Given that we're not holding a lock here, we retain the value in a 9849875cf80SDavid Howells * local variable for each dentry as we look at it so that we don't see 9859875cf80SDavid Howells * the components of that value change under us */ 9869875cf80SDavid Howells while (managed = ACCESS_ONCE(path->dentry->d_flags), 9879875cf80SDavid Howells managed &= DCACHE_MANAGED_DENTRY, 9889875cf80SDavid Howells unlikely(managed != 0)) { 989cc53ce53SDavid Howells /* Allow the filesystem to manage the transit without i_mutex 990cc53ce53SDavid Howells * being held. */ 991cc53ce53SDavid Howells if (managed & DCACHE_MANAGE_TRANSIT) { 992cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op); 993cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op->d_manage); 9941aed3e42SAl Viro ret = path->dentry->d_op->d_manage(path->dentry, false); 995cc53ce53SDavid Howells if (ret < 0) 9968aef1884SAl Viro break; 997cc53ce53SDavid Howells } 998cc53ce53SDavid Howells 9999875cf80SDavid Howells /* Transit to a mounted filesystem. */ 10009875cf80SDavid Howells if (managed & DCACHE_MOUNTED) { 10019875cf80SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 10029875cf80SDavid Howells if (mounted) { 10039875cf80SDavid Howells dput(path->dentry); 10049875cf80SDavid Howells if (need_mntput) 1005463ffb2eSAl Viro mntput(path->mnt); 1006463ffb2eSAl Viro path->mnt = mounted; 1007463ffb2eSAl Viro path->dentry = dget(mounted->mnt_root); 10089875cf80SDavid Howells need_mntput = true; 10099875cf80SDavid Howells continue; 1010463ffb2eSAl Viro } 1011463ffb2eSAl Viro 10129875cf80SDavid Howells /* Something is mounted on this dentry in another 10139875cf80SDavid Howells * namespace and/or whatever was mounted there in this 10149875cf80SDavid Howells * namespace got unmounted before we managed to get the 10159875cf80SDavid Howells * vfsmount_lock */ 10161da177e4SLinus Torvalds } 10179875cf80SDavid Howells 10189875cf80SDavid Howells /* Handle an automount point */ 10199875cf80SDavid Howells if (managed & DCACHE_NEED_AUTOMOUNT) { 10209875cf80SDavid Howells ret = follow_automount(path, flags, &need_mntput); 10219875cf80SDavid Howells if (ret < 0) 10228aef1884SAl Viro break; 10239875cf80SDavid Howells continue; 10249875cf80SDavid Howells } 10259875cf80SDavid Howells 10269875cf80SDavid Howells /* We didn't change the current path point */ 10279875cf80SDavid Howells break; 10289875cf80SDavid Howells } 10298aef1884SAl Viro 10308aef1884SAl Viro if (need_mntput && path->mnt == mnt) 10318aef1884SAl Viro mntput(path->mnt); 10328aef1884SAl Viro if (ret == -EISDIR) 10338aef1884SAl Viro ret = 0; 1034a3fbbde7SAl Viro return ret < 0 ? ret : need_mntput; 10351da177e4SLinus Torvalds } 10361da177e4SLinus Torvalds 1037cc53ce53SDavid Howells int follow_down_one(struct path *path) 10381da177e4SLinus Torvalds { 10391da177e4SLinus Torvalds struct vfsmount *mounted; 10401da177e4SLinus Torvalds 10411c755af4SAl Viro mounted = lookup_mnt(path); 10421da177e4SLinus Torvalds if (mounted) { 10439393bd07SAl Viro dput(path->dentry); 10449393bd07SAl Viro mntput(path->mnt); 10459393bd07SAl Viro path->mnt = mounted; 10469393bd07SAl Viro path->dentry = dget(mounted->mnt_root); 10471da177e4SLinus Torvalds return 1; 10481da177e4SLinus Torvalds } 10491da177e4SLinus Torvalds return 0; 10501da177e4SLinus Torvalds } 10511da177e4SLinus Torvalds 105262a7375eSIan Kent static inline bool managed_dentry_might_block(struct dentry *dentry) 105362a7375eSIan Kent { 105462a7375eSIan Kent return (dentry->d_flags & DCACHE_MANAGE_TRANSIT && 105562a7375eSIan Kent dentry->d_op->d_manage(dentry, true) < 0); 105662a7375eSIan Kent } 105762a7375eSIan Kent 10589875cf80SDavid Howells /* 1059287548e4SAl Viro * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if 1060287548e4SAl Viro * we meet a managed dentry that would need blocking. 10619875cf80SDavid Howells */ 10629875cf80SDavid Howells static bool __follow_mount_rcu(struct nameidata *nd, struct path *path, 1063287548e4SAl Viro struct inode **inode) 10649875cf80SDavid Howells { 106562a7375eSIan Kent for (;;) { 1066c7105365SAl Viro struct mount *mounted; 106762a7375eSIan Kent /* 106862a7375eSIan Kent * Don't forget we might have a non-mountpoint managed dentry 106962a7375eSIan Kent * that wants to block transit. 107062a7375eSIan Kent */ 1071287548e4SAl Viro if (unlikely(managed_dentry_might_block(path->dentry))) 1072ab90911fSDavid Howells return false; 107362a7375eSIan Kent 107462a7375eSIan Kent if (!d_mountpoint(path->dentry)) 107562a7375eSIan Kent break; 107662a7375eSIan Kent 10779875cf80SDavid Howells mounted = __lookup_mnt(path->mnt, path->dentry, 1); 10789875cf80SDavid Howells if (!mounted) 10799875cf80SDavid Howells break; 1080c7105365SAl Viro path->mnt = &mounted->mnt; 1081c7105365SAl Viro path->dentry = mounted->mnt.mnt_root; 1082a3fbbde7SAl Viro nd->flags |= LOOKUP_JUMPED; 10839875cf80SDavid Howells nd->seq = read_seqcount_begin(&path->dentry->d_seq); 108459430262SLinus Torvalds /* 108559430262SLinus Torvalds * Update the inode too. We don't need to re-check the 108659430262SLinus Torvalds * dentry sequence number here after this d_inode read, 108759430262SLinus Torvalds * because a mount-point is always pinned. 108859430262SLinus Torvalds */ 108959430262SLinus Torvalds *inode = path->dentry->d_inode; 10909875cf80SDavid Howells } 10919875cf80SDavid Howells return true; 10929875cf80SDavid Howells } 10939875cf80SDavid Howells 1094dea39376SAl Viro static void follow_mount_rcu(struct nameidata *nd) 1095287548e4SAl Viro { 1096dea39376SAl Viro while (d_mountpoint(nd->path.dentry)) { 1097c7105365SAl Viro struct mount *mounted; 1098dea39376SAl Viro mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1); 1099287548e4SAl Viro if (!mounted) 1100287548e4SAl Viro break; 1101c7105365SAl Viro nd->path.mnt = &mounted->mnt; 1102c7105365SAl Viro nd->path.dentry = mounted->mnt.mnt_root; 1103dea39376SAl Viro nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); 1104287548e4SAl Viro } 1105287548e4SAl Viro } 1106287548e4SAl Viro 110731e6b01fSNick Piggin static int follow_dotdot_rcu(struct nameidata *nd) 110831e6b01fSNick Piggin { 110931e6b01fSNick Piggin set_root_rcu(nd); 111031e6b01fSNick Piggin 111131e6b01fSNick Piggin while (1) { 111231e6b01fSNick Piggin if (nd->path.dentry == nd->root.dentry && 111331e6b01fSNick Piggin nd->path.mnt == nd->root.mnt) { 111431e6b01fSNick Piggin break; 111531e6b01fSNick Piggin } 111631e6b01fSNick Piggin if (nd->path.dentry != nd->path.mnt->mnt_root) { 111731e6b01fSNick Piggin struct dentry *old = nd->path.dentry; 111831e6b01fSNick Piggin struct dentry *parent = old->d_parent; 111931e6b01fSNick Piggin unsigned seq; 112031e6b01fSNick Piggin 112131e6b01fSNick Piggin seq = read_seqcount_begin(&parent->d_seq); 112231e6b01fSNick Piggin if (read_seqcount_retry(&old->d_seq, nd->seq)) 1123ef7562d5SAl Viro goto failed; 112431e6b01fSNick Piggin nd->path.dentry = parent; 112531e6b01fSNick Piggin nd->seq = seq; 112631e6b01fSNick Piggin break; 112731e6b01fSNick Piggin } 112831e6b01fSNick Piggin if (!follow_up_rcu(&nd->path)) 112931e6b01fSNick Piggin break; 113031e6b01fSNick Piggin nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); 113131e6b01fSNick Piggin } 1132dea39376SAl Viro follow_mount_rcu(nd); 1133dea39376SAl Viro nd->inode = nd->path.dentry->d_inode; 113431e6b01fSNick Piggin return 0; 1135ef7562d5SAl Viro 1136ef7562d5SAl Viro failed: 1137ef7562d5SAl Viro nd->flags &= ~LOOKUP_RCU; 11385b6ca027SAl Viro if (!(nd->flags & LOOKUP_ROOT)) 1139ef7562d5SAl Viro nd->root.mnt = NULL; 114032a7991bSAl Viro unlock_rcu_walk(); 1141ef7562d5SAl Viro return -ECHILD; 114231e6b01fSNick Piggin } 114331e6b01fSNick Piggin 11449875cf80SDavid Howells /* 1145cc53ce53SDavid Howells * Follow down to the covering mount currently visible to userspace. At each 1146cc53ce53SDavid Howells * point, the filesystem owning that dentry may be queried as to whether the 1147cc53ce53SDavid Howells * caller is permitted to proceed or not. 1148cc53ce53SDavid Howells */ 11497cc90cc3SAl Viro int follow_down(struct path *path) 1150cc53ce53SDavid Howells { 1151cc53ce53SDavid Howells unsigned managed; 1152cc53ce53SDavid Howells int ret; 1153cc53ce53SDavid Howells 1154cc53ce53SDavid Howells while (managed = ACCESS_ONCE(path->dentry->d_flags), 1155cc53ce53SDavid Howells unlikely(managed & DCACHE_MANAGED_DENTRY)) { 1156cc53ce53SDavid Howells /* Allow the filesystem to manage the transit without i_mutex 1157cc53ce53SDavid Howells * being held. 1158cc53ce53SDavid Howells * 1159cc53ce53SDavid Howells * We indicate to the filesystem if someone is trying to mount 1160cc53ce53SDavid Howells * something here. This gives autofs the chance to deny anyone 1161cc53ce53SDavid Howells * other than its daemon the right to mount on its 1162cc53ce53SDavid Howells * superstructure. 1163cc53ce53SDavid Howells * 1164cc53ce53SDavid Howells * The filesystem may sleep at this point. 1165cc53ce53SDavid Howells */ 1166cc53ce53SDavid Howells if (managed & DCACHE_MANAGE_TRANSIT) { 1167cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op); 1168cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op->d_manage); 1169ab90911fSDavid Howells ret = path->dentry->d_op->d_manage( 11701aed3e42SAl Viro path->dentry, false); 1171cc53ce53SDavid Howells if (ret < 0) 1172cc53ce53SDavid Howells return ret == -EISDIR ? 0 : ret; 1173cc53ce53SDavid Howells } 1174cc53ce53SDavid Howells 1175cc53ce53SDavid Howells /* Transit to a mounted filesystem. */ 1176cc53ce53SDavid Howells if (managed & DCACHE_MOUNTED) { 1177cc53ce53SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 1178cc53ce53SDavid Howells if (!mounted) 1179cc53ce53SDavid Howells break; 1180cc53ce53SDavid Howells dput(path->dentry); 1181cc53ce53SDavid Howells mntput(path->mnt); 1182cc53ce53SDavid Howells path->mnt = mounted; 1183cc53ce53SDavid Howells path->dentry = dget(mounted->mnt_root); 1184cc53ce53SDavid Howells continue; 1185cc53ce53SDavid Howells } 1186cc53ce53SDavid Howells 1187cc53ce53SDavid Howells /* Don't handle automount points here */ 1188cc53ce53SDavid Howells break; 1189cc53ce53SDavid Howells } 1190cc53ce53SDavid Howells return 0; 1191cc53ce53SDavid Howells } 1192cc53ce53SDavid Howells 1193cc53ce53SDavid Howells /* 11949875cf80SDavid Howells * Skip to top of mountpoint pile in refwalk mode for follow_dotdot() 11959875cf80SDavid Howells */ 11969875cf80SDavid Howells static void follow_mount(struct path *path) 11979875cf80SDavid Howells { 11989875cf80SDavid Howells while (d_mountpoint(path->dentry)) { 11999875cf80SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 12009875cf80SDavid Howells if (!mounted) 12019875cf80SDavid Howells break; 12029875cf80SDavid Howells dput(path->dentry); 12039875cf80SDavid Howells mntput(path->mnt); 12049875cf80SDavid Howells path->mnt = mounted; 12059875cf80SDavid Howells path->dentry = dget(mounted->mnt_root); 12069875cf80SDavid Howells } 12079875cf80SDavid Howells } 12089875cf80SDavid Howells 120931e6b01fSNick Piggin static void follow_dotdot(struct nameidata *nd) 12101da177e4SLinus Torvalds { 12112a737871SAl Viro set_root(nd); 1212e518ddb7SAndreas Mohr 12131da177e4SLinus Torvalds while(1) { 12144ac91378SJan Blunck struct dentry *old = nd->path.dentry; 12151da177e4SLinus Torvalds 12162a737871SAl Viro if (nd->path.dentry == nd->root.dentry && 12172a737871SAl Viro nd->path.mnt == nd->root.mnt) { 12181da177e4SLinus Torvalds break; 12191da177e4SLinus Torvalds } 12204ac91378SJan Blunck if (nd->path.dentry != nd->path.mnt->mnt_root) { 12213088dd70SAl Viro /* rare case of legitimate dget_parent()... */ 12223088dd70SAl Viro nd->path.dentry = dget_parent(nd->path.dentry); 12231da177e4SLinus Torvalds dput(old); 12241da177e4SLinus Torvalds break; 12251da177e4SLinus Torvalds } 12263088dd70SAl Viro if (!follow_up(&nd->path)) 12271da177e4SLinus Torvalds break; 12281da177e4SLinus Torvalds } 122979ed0226SAl Viro follow_mount(&nd->path); 123031e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 12311da177e4SLinus Torvalds } 12321da177e4SLinus Torvalds 12331da177e4SLinus Torvalds /* 1234bad61189SMiklos Szeredi * This looks up the name in dcache, possibly revalidates the old dentry and 1235bad61189SMiklos Szeredi * allocates a new one if not found or not valid. In the need_lookup argument 1236bad61189SMiklos Szeredi * returns whether i_op->lookup is necessary. 1237bad61189SMiklos Szeredi * 1238bad61189SMiklos Szeredi * dir->d_inode->i_mutex must be held 1239baa03890SNick Piggin */ 1240bad61189SMiklos Szeredi static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir, 1241201f956eSAl Viro unsigned int flags, bool *need_lookup) 1242baa03890SNick Piggin { 1243baa03890SNick Piggin struct dentry *dentry; 1244bad61189SMiklos Szeredi int error; 1245baa03890SNick Piggin 1246bad61189SMiklos Szeredi *need_lookup = false; 1247bad61189SMiklos Szeredi dentry = d_lookup(dir, name); 1248bad61189SMiklos Szeredi if (dentry) { 1249bad61189SMiklos Szeredi if (d_need_lookup(dentry)) { 1250bad61189SMiklos Szeredi *need_lookup = true; 1251bad61189SMiklos Szeredi } else if (dentry->d_flags & DCACHE_OP_REVALIDATE) { 1252201f956eSAl Viro error = d_revalidate(dentry, flags); 1253bad61189SMiklos Szeredi if (unlikely(error <= 0)) { 1254bad61189SMiklos Szeredi if (error < 0) { 1255bad61189SMiklos Szeredi dput(dentry); 1256bad61189SMiklos Szeredi return ERR_PTR(error); 1257bad61189SMiklos Szeredi } else if (!d_invalidate(dentry)) { 1258bad61189SMiklos Szeredi dput(dentry); 1259bad61189SMiklos Szeredi dentry = NULL; 1260bad61189SMiklos Szeredi } 1261bad61189SMiklos Szeredi } 1262bad61189SMiklos Szeredi } 1263bad61189SMiklos Szeredi } 1264baa03890SNick Piggin 1265bad61189SMiklos Szeredi if (!dentry) { 1266bad61189SMiklos Szeredi dentry = d_alloc(dir, name); 1267baa03890SNick Piggin if (unlikely(!dentry)) 1268baa03890SNick Piggin return ERR_PTR(-ENOMEM); 1269baa03890SNick Piggin 1270bad61189SMiklos Szeredi *need_lookup = true; 1271baa03890SNick Piggin } 1272baa03890SNick Piggin return dentry; 1273baa03890SNick Piggin } 1274baa03890SNick Piggin 1275baa03890SNick Piggin /* 1276bad61189SMiklos Szeredi * Call i_op->lookup on the dentry. The dentry must be negative but may be 1277bad61189SMiklos Szeredi * hashed if it was pouplated with DCACHE_NEED_LOOKUP. 1278bad61189SMiklos Szeredi * 1279bad61189SMiklos Szeredi * dir->d_inode->i_mutex must be held 128044396f4bSJosef Bacik */ 1281bad61189SMiklos Szeredi static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry, 128272bd866aSAl Viro unsigned int flags) 128344396f4bSJosef Bacik { 128444396f4bSJosef Bacik struct dentry *old; 128544396f4bSJosef Bacik 128644396f4bSJosef Bacik /* Don't create child dentry for a dead directory. */ 1287bad61189SMiklos Szeredi if (unlikely(IS_DEADDIR(dir))) { 1288e188dc02SMiklos Szeredi dput(dentry); 128944396f4bSJosef Bacik return ERR_PTR(-ENOENT); 1290e188dc02SMiklos Szeredi } 129144396f4bSJosef Bacik 129272bd866aSAl Viro old = dir->i_op->lookup(dir, dentry, flags); 129344396f4bSJosef Bacik if (unlikely(old)) { 129444396f4bSJosef Bacik dput(dentry); 129544396f4bSJosef Bacik dentry = old; 129644396f4bSJosef Bacik } 129744396f4bSJosef Bacik return dentry; 129844396f4bSJosef Bacik } 129944396f4bSJosef Bacik 1300a3255546SAl Viro static struct dentry *__lookup_hash(struct qstr *name, 130172bd866aSAl Viro struct dentry *base, unsigned int flags) 1302a3255546SAl Viro { 1303bad61189SMiklos Szeredi bool need_lookup; 1304a3255546SAl Viro struct dentry *dentry; 1305a3255546SAl Viro 130672bd866aSAl Viro dentry = lookup_dcache(name, base, flags, &need_lookup); 1307bad61189SMiklos Szeredi if (!need_lookup) 1308a3255546SAl Viro return dentry; 1309bad61189SMiklos Szeredi 131072bd866aSAl Viro return lookup_real(base->d_inode, dentry, flags); 1311a3255546SAl Viro } 1312a3255546SAl Viro 131344396f4bSJosef Bacik /* 13141da177e4SLinus Torvalds * It's more convoluted than I'd like it to be, but... it's still fairly 13151da177e4SLinus Torvalds * small and for now I'd prefer to have fast path as straight as possible. 13161da177e4SLinus Torvalds * It _is_ time-critical. 13171da177e4SLinus Torvalds */ 1318697f514dSMiklos Szeredi static int lookup_fast(struct nameidata *nd, struct qstr *name, 131931e6b01fSNick Piggin struct path *path, struct inode **inode) 13201da177e4SLinus Torvalds { 13214ac91378SJan Blunck struct vfsmount *mnt = nd->path.mnt; 132231e6b01fSNick Piggin struct dentry *dentry, *parent = nd->path.dentry; 13235a18fff2SAl Viro int need_reval = 1; 13245a18fff2SAl Viro int status = 1; 13259875cf80SDavid Howells int err; 13269875cf80SDavid Howells 13273cac260aSAl Viro /* 1328b04f784eSNick Piggin * Rename seqlock is not required here because in the off chance 1329b04f784eSNick Piggin * of a false negative due to a concurrent rename, we're going to 1330b04f784eSNick Piggin * do the non-racy lookup, below. 1331b04f784eSNick Piggin */ 133231e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) { 133331e6b01fSNick Piggin unsigned seq; 133412f8ad4bSLinus Torvalds dentry = __d_lookup_rcu(parent, name, &seq, nd->inode); 13355a18fff2SAl Viro if (!dentry) 13365a18fff2SAl Viro goto unlazy; 13375a18fff2SAl Viro 133812f8ad4bSLinus Torvalds /* 133912f8ad4bSLinus Torvalds * This sequence count validates that the inode matches 134012f8ad4bSLinus Torvalds * the dentry name information from lookup. 134112f8ad4bSLinus Torvalds */ 134212f8ad4bSLinus Torvalds *inode = dentry->d_inode; 134312f8ad4bSLinus Torvalds if (read_seqcount_retry(&dentry->d_seq, seq)) 134412f8ad4bSLinus Torvalds return -ECHILD; 134512f8ad4bSLinus Torvalds 134612f8ad4bSLinus Torvalds /* 134712f8ad4bSLinus Torvalds * This sequence count validates that the parent had no 134812f8ad4bSLinus Torvalds * changes while we did the lookup of the dentry above. 134912f8ad4bSLinus Torvalds * 135012f8ad4bSLinus Torvalds * The memory barrier in read_seqcount_begin of child is 135112f8ad4bSLinus Torvalds * enough, we can use __read_seqcount_retry here. 135212f8ad4bSLinus Torvalds */ 135331e6b01fSNick Piggin if (__read_seqcount_retry(&parent->d_seq, nd->seq)) 135431e6b01fSNick Piggin return -ECHILD; 135531e6b01fSNick Piggin nd->seq = seq; 13565a18fff2SAl Viro 1357fa4ee159SMiklos Szeredi if (unlikely(d_need_lookup(dentry))) 1358fa4ee159SMiklos Szeredi goto unlazy; 135924643087SAl Viro if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) { 13604ce16ef3SAl Viro status = d_revalidate(dentry, nd->flags); 13615a18fff2SAl Viro if (unlikely(status <= 0)) { 13625a18fff2SAl Viro if (status != -ECHILD) 13635a18fff2SAl Viro need_reval = 0; 13645a18fff2SAl Viro goto unlazy; 13655a18fff2SAl Viro } 136624643087SAl Viro } 136731e6b01fSNick Piggin path->mnt = mnt; 136831e6b01fSNick Piggin path->dentry = dentry; 1369d6e9bd25SAl Viro if (unlikely(!__follow_mount_rcu(nd, path, inode))) 1370d6e9bd25SAl Viro goto unlazy; 1371d6e9bd25SAl Viro if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT)) 1372d6e9bd25SAl Viro goto unlazy; 13739875cf80SDavid Howells return 0; 13745a18fff2SAl Viro unlazy: 137519660af7SAl Viro if (unlazy_walk(nd, dentry)) 13765a18fff2SAl Viro return -ECHILD; 13775a18fff2SAl Viro } else { 137831e6b01fSNick Piggin dentry = __d_lookup(parent, name); 137924643087SAl Viro } 13805a18fff2SAl Viro 138181e6f520SAl Viro if (unlikely(!dentry)) 138281e6f520SAl Viro goto need_lookup; 13835a18fff2SAl Viro 138481e6f520SAl Viro if (unlikely(d_need_lookup(dentry))) { 138581e6f520SAl Viro dput(dentry); 138681e6f520SAl Viro goto need_lookup; 13875a18fff2SAl Viro } 138881e6f520SAl Viro 13895a18fff2SAl Viro if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval) 13904ce16ef3SAl Viro status = d_revalidate(dentry, nd->flags); 13915a18fff2SAl Viro if (unlikely(status <= 0)) { 13925a18fff2SAl Viro if (status < 0) { 13935a18fff2SAl Viro dput(dentry); 13945a18fff2SAl Viro return status; 13955a18fff2SAl Viro } 13965a18fff2SAl Viro if (!d_invalidate(dentry)) { 13975a18fff2SAl Viro dput(dentry); 139881e6f520SAl Viro goto need_lookup; 13995a18fff2SAl Viro } 14005a18fff2SAl Viro } 1401697f514dSMiklos Szeredi 14021da177e4SLinus Torvalds path->mnt = mnt; 14031da177e4SLinus Torvalds path->dentry = dentry; 14049875cf80SDavid Howells err = follow_managed(path, nd->flags); 140589312214SIan Kent if (unlikely(err < 0)) { 140689312214SIan Kent path_put_conditional(path, nd); 14079875cf80SDavid Howells return err; 140889312214SIan Kent } 1409a3fbbde7SAl Viro if (err) 1410a3fbbde7SAl Viro nd->flags |= LOOKUP_JUMPED; 141131e6b01fSNick Piggin *inode = path->dentry->d_inode; 14121da177e4SLinus Torvalds return 0; 141381e6f520SAl Viro 141481e6f520SAl Viro need_lookup: 1415697f514dSMiklos Szeredi return 1; 1416697f514dSMiklos Szeredi } 1417697f514dSMiklos Szeredi 1418697f514dSMiklos Szeredi /* Fast lookup failed, do it the slow way */ 1419697f514dSMiklos Szeredi static int lookup_slow(struct nameidata *nd, struct qstr *name, 1420697f514dSMiklos Szeredi struct path *path) 1421697f514dSMiklos Szeredi { 1422697f514dSMiklos Szeredi struct dentry *dentry, *parent; 1423697f514dSMiklos Szeredi int err; 1424697f514dSMiklos Szeredi 1425697f514dSMiklos Szeredi parent = nd->path.dentry; 142681e6f520SAl Viro BUG_ON(nd->inode != parent->d_inode); 142781e6f520SAl Viro 142881e6f520SAl Viro mutex_lock(&parent->d_inode->i_mutex); 142972bd866aSAl Viro dentry = __lookup_hash(name, parent, nd->flags); 143081e6f520SAl Viro mutex_unlock(&parent->d_inode->i_mutex); 143181e6f520SAl Viro if (IS_ERR(dentry)) 143281e6f520SAl Viro return PTR_ERR(dentry); 1433697f514dSMiklos Szeredi path->mnt = nd->path.mnt; 1434697f514dSMiklos Szeredi path->dentry = dentry; 1435697f514dSMiklos Szeredi err = follow_managed(path, nd->flags); 1436697f514dSMiklos Szeredi if (unlikely(err < 0)) { 1437697f514dSMiklos Szeredi path_put_conditional(path, nd); 1438697f514dSMiklos Szeredi return err; 1439697f514dSMiklos Szeredi } 1440697f514dSMiklos Szeredi if (err) 1441697f514dSMiklos Szeredi nd->flags |= LOOKUP_JUMPED; 1442697f514dSMiklos Szeredi return 0; 14431da177e4SLinus Torvalds } 14441da177e4SLinus Torvalds 144552094c8aSAl Viro static inline int may_lookup(struct nameidata *nd) 144652094c8aSAl Viro { 144752094c8aSAl Viro if (nd->flags & LOOKUP_RCU) { 14484ad5abb3SAl Viro int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK); 144952094c8aSAl Viro if (err != -ECHILD) 145052094c8aSAl Viro return err; 145119660af7SAl Viro if (unlazy_walk(nd, NULL)) 145252094c8aSAl Viro return -ECHILD; 145352094c8aSAl Viro } 14544ad5abb3SAl Viro return inode_permission(nd->inode, MAY_EXEC); 145552094c8aSAl Viro } 145652094c8aSAl Viro 14579856fa1bSAl Viro static inline int handle_dots(struct nameidata *nd, int type) 14589856fa1bSAl Viro { 14599856fa1bSAl Viro if (type == LAST_DOTDOT) { 14609856fa1bSAl Viro if (nd->flags & LOOKUP_RCU) { 14619856fa1bSAl Viro if (follow_dotdot_rcu(nd)) 14629856fa1bSAl Viro return -ECHILD; 14639856fa1bSAl Viro } else 14649856fa1bSAl Viro follow_dotdot(nd); 14659856fa1bSAl Viro } 14669856fa1bSAl Viro return 0; 14679856fa1bSAl Viro } 14689856fa1bSAl Viro 1469951361f9SAl Viro static void terminate_walk(struct nameidata *nd) 1470951361f9SAl Viro { 1471951361f9SAl Viro if (!(nd->flags & LOOKUP_RCU)) { 1472951361f9SAl Viro path_put(&nd->path); 1473951361f9SAl Viro } else { 1474951361f9SAl Viro nd->flags &= ~LOOKUP_RCU; 14755b6ca027SAl Viro if (!(nd->flags & LOOKUP_ROOT)) 1476951361f9SAl Viro nd->root.mnt = NULL; 147732a7991bSAl Viro unlock_rcu_walk(); 1478951361f9SAl Viro } 1479951361f9SAl Viro } 1480951361f9SAl Viro 14813ddcd056SLinus Torvalds /* 14823ddcd056SLinus Torvalds * Do we need to follow links? We _really_ want to be able 14833ddcd056SLinus Torvalds * to do this check without having to look at inode->i_op, 14843ddcd056SLinus Torvalds * so we keep a cache of "no, this doesn't need follow_link" 14853ddcd056SLinus Torvalds * for the common case. 14863ddcd056SLinus Torvalds */ 14877813b94aSLinus Torvalds static inline int should_follow_link(struct inode *inode, int follow) 14883ddcd056SLinus Torvalds { 14893ddcd056SLinus Torvalds if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) { 14903ddcd056SLinus Torvalds if (likely(inode->i_op->follow_link)) 14913ddcd056SLinus Torvalds return follow; 14923ddcd056SLinus Torvalds 14933ddcd056SLinus Torvalds /* This gets set once for the inode lifetime */ 14943ddcd056SLinus Torvalds spin_lock(&inode->i_lock); 14953ddcd056SLinus Torvalds inode->i_opflags |= IOP_NOFOLLOW; 14963ddcd056SLinus Torvalds spin_unlock(&inode->i_lock); 14973ddcd056SLinus Torvalds } 14983ddcd056SLinus Torvalds return 0; 14993ddcd056SLinus Torvalds } 15003ddcd056SLinus Torvalds 1501ce57dfc1SAl Viro static inline int walk_component(struct nameidata *nd, struct path *path, 1502ce57dfc1SAl Viro struct qstr *name, int type, int follow) 1503ce57dfc1SAl Viro { 1504ce57dfc1SAl Viro struct inode *inode; 1505ce57dfc1SAl Viro int err; 1506ce57dfc1SAl Viro /* 1507ce57dfc1SAl Viro * "." and ".." are special - ".." especially so because it has 1508ce57dfc1SAl Viro * to be able to know about the current root directory and 1509ce57dfc1SAl Viro * parent relationships. 1510ce57dfc1SAl Viro */ 1511ce57dfc1SAl Viro if (unlikely(type != LAST_NORM)) 1512ce57dfc1SAl Viro return handle_dots(nd, type); 1513697f514dSMiklos Szeredi err = lookup_fast(nd, name, path, &inode); 1514ce57dfc1SAl Viro if (unlikely(err)) { 1515697f514dSMiklos Szeredi if (err < 0) 1516697f514dSMiklos Szeredi goto out_err; 1517697f514dSMiklos Szeredi 1518697f514dSMiklos Szeredi err = lookup_slow(nd, name, path); 1519697f514dSMiklos Szeredi if (err < 0) 1520697f514dSMiklos Szeredi goto out_err; 1521697f514dSMiklos Szeredi 1522697f514dSMiklos Szeredi inode = path->dentry->d_inode; 1523ce57dfc1SAl Viro } 1524697f514dSMiklos Szeredi err = -ENOENT; 1525697f514dSMiklos Szeredi if (!inode) 1526697f514dSMiklos Szeredi goto out_path_put; 1527697f514dSMiklos Szeredi 15287813b94aSLinus Torvalds if (should_follow_link(inode, follow)) { 152919660af7SAl Viro if (nd->flags & LOOKUP_RCU) { 153019660af7SAl Viro if (unlikely(unlazy_walk(nd, path->dentry))) { 1531697f514dSMiklos Szeredi err = -ECHILD; 1532697f514dSMiklos Szeredi goto out_err; 153319660af7SAl Viro } 153419660af7SAl Viro } 1535ce57dfc1SAl Viro BUG_ON(inode != path->dentry->d_inode); 1536ce57dfc1SAl Viro return 1; 1537ce57dfc1SAl Viro } 1538ce57dfc1SAl Viro path_to_nameidata(path, nd); 1539ce57dfc1SAl Viro nd->inode = inode; 1540ce57dfc1SAl Viro return 0; 1541697f514dSMiklos Szeredi 1542697f514dSMiklos Szeredi out_path_put: 1543697f514dSMiklos Szeredi path_to_nameidata(path, nd); 1544697f514dSMiklos Szeredi out_err: 1545697f514dSMiklos Szeredi terminate_walk(nd); 1546697f514dSMiklos Szeredi return err; 1547ce57dfc1SAl Viro } 1548ce57dfc1SAl Viro 15491da177e4SLinus Torvalds /* 1550b356379aSAl Viro * This limits recursive symlink follows to 8, while 1551b356379aSAl Viro * limiting consecutive symlinks to 40. 1552b356379aSAl Viro * 1553b356379aSAl Viro * Without that kind of total limit, nasty chains of consecutive 1554b356379aSAl Viro * symlinks can cause almost arbitrarily long lookups. 1555b356379aSAl Viro */ 1556b356379aSAl Viro static inline int nested_symlink(struct path *path, struct nameidata *nd) 1557b356379aSAl Viro { 1558b356379aSAl Viro int res; 1559b356379aSAl Viro 1560b356379aSAl Viro if (unlikely(current->link_count >= MAX_NESTED_LINKS)) { 1561b356379aSAl Viro path_put_conditional(path, nd); 1562b356379aSAl Viro path_put(&nd->path); 1563b356379aSAl Viro return -ELOOP; 1564b356379aSAl Viro } 15651a4022f8SErez Zadok BUG_ON(nd->depth >= MAX_NESTED_LINKS); 1566b356379aSAl Viro 1567b356379aSAl Viro nd->depth++; 1568b356379aSAl Viro current->link_count++; 1569b356379aSAl Viro 1570b356379aSAl Viro do { 1571b356379aSAl Viro struct path link = *path; 1572b356379aSAl Viro void *cookie; 1573574197e0SAl Viro 1574574197e0SAl Viro res = follow_link(&link, nd, &cookie); 15756d7b5aaeSAl Viro if (res) 15766d7b5aaeSAl Viro break; 1577b356379aSAl Viro res = walk_component(nd, path, &nd->last, 1578b356379aSAl Viro nd->last_type, LOOKUP_FOLLOW); 1579574197e0SAl Viro put_link(nd, &link, cookie); 1580b356379aSAl Viro } while (res > 0); 1581b356379aSAl Viro 1582b356379aSAl Viro current->link_count--; 1583b356379aSAl Viro nd->depth--; 1584b356379aSAl Viro return res; 1585b356379aSAl Viro } 1586b356379aSAl Viro 1587b356379aSAl Viro /* 15883ddcd056SLinus Torvalds * We really don't want to look at inode->i_op->lookup 15893ddcd056SLinus Torvalds * when we don't have to. So we keep a cache bit in 15903ddcd056SLinus Torvalds * the inode ->i_opflags field that says "yes, we can 15913ddcd056SLinus Torvalds * do lookup on this inode". 15923ddcd056SLinus Torvalds */ 15933ddcd056SLinus Torvalds static inline int can_lookup(struct inode *inode) 15943ddcd056SLinus Torvalds { 15953ddcd056SLinus Torvalds if (likely(inode->i_opflags & IOP_LOOKUP)) 15963ddcd056SLinus Torvalds return 1; 15973ddcd056SLinus Torvalds if (likely(!inode->i_op->lookup)) 15983ddcd056SLinus Torvalds return 0; 15993ddcd056SLinus Torvalds 16003ddcd056SLinus Torvalds /* We do this once for the lifetime of the inode */ 16013ddcd056SLinus Torvalds spin_lock(&inode->i_lock); 16023ddcd056SLinus Torvalds inode->i_opflags |= IOP_LOOKUP; 16033ddcd056SLinus Torvalds spin_unlock(&inode->i_lock); 16043ddcd056SLinus Torvalds return 1; 16053ddcd056SLinus Torvalds } 16063ddcd056SLinus Torvalds 1607bfcfaa77SLinus Torvalds /* 1608bfcfaa77SLinus Torvalds * We can do the critical dentry name comparison and hashing 1609bfcfaa77SLinus Torvalds * operations one word at a time, but we are limited to: 1610bfcfaa77SLinus Torvalds * 1611bfcfaa77SLinus Torvalds * - Architectures with fast unaligned word accesses. We could 1612bfcfaa77SLinus Torvalds * do a "get_unaligned()" if this helps and is sufficiently 1613bfcfaa77SLinus Torvalds * fast. 1614bfcfaa77SLinus Torvalds * 1615bfcfaa77SLinus Torvalds * - Little-endian machines (so that we can generate the mask 1616bfcfaa77SLinus Torvalds * of low bytes efficiently). Again, we *could* do a byte 1617bfcfaa77SLinus Torvalds * swapping load on big-endian architectures if that is not 1618bfcfaa77SLinus Torvalds * expensive enough to make the optimization worthless. 1619bfcfaa77SLinus Torvalds * 1620bfcfaa77SLinus Torvalds * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we 1621bfcfaa77SLinus Torvalds * do not trap on the (extremely unlikely) case of a page 1622bfcfaa77SLinus Torvalds * crossing operation. 1623bfcfaa77SLinus Torvalds * 1624bfcfaa77SLinus Torvalds * - Furthermore, we need an efficient 64-bit compile for the 1625bfcfaa77SLinus Torvalds * 64-bit case in order to generate the "number of bytes in 1626bfcfaa77SLinus Torvalds * the final mask". Again, that could be replaced with a 1627bfcfaa77SLinus Torvalds * efficient population count instruction or similar. 1628bfcfaa77SLinus Torvalds */ 1629bfcfaa77SLinus Torvalds #ifdef CONFIG_DCACHE_WORD_ACCESS 1630bfcfaa77SLinus Torvalds 1631f68e556eSLinus Torvalds #include <asm/word-at-a-time.h> 1632bfcfaa77SLinus Torvalds 1633f68e556eSLinus Torvalds #ifdef CONFIG_64BIT 1634bfcfaa77SLinus Torvalds 1635bfcfaa77SLinus Torvalds static inline unsigned int fold_hash(unsigned long hash) 1636bfcfaa77SLinus Torvalds { 1637bfcfaa77SLinus Torvalds hash += hash >> (8*sizeof(int)); 1638bfcfaa77SLinus Torvalds return hash; 1639bfcfaa77SLinus Torvalds } 1640bfcfaa77SLinus Torvalds 1641bfcfaa77SLinus Torvalds #else /* 32-bit case */ 1642bfcfaa77SLinus Torvalds 1643bfcfaa77SLinus Torvalds #define fold_hash(x) (x) 1644bfcfaa77SLinus Torvalds 1645bfcfaa77SLinus Torvalds #endif 1646bfcfaa77SLinus Torvalds 1647bfcfaa77SLinus Torvalds unsigned int full_name_hash(const unsigned char *name, unsigned int len) 1648bfcfaa77SLinus Torvalds { 1649bfcfaa77SLinus Torvalds unsigned long a, mask; 1650bfcfaa77SLinus Torvalds unsigned long hash = 0; 1651bfcfaa77SLinus Torvalds 1652bfcfaa77SLinus Torvalds for (;;) { 1653e419b4ccSLinus Torvalds a = load_unaligned_zeropad(name); 1654bfcfaa77SLinus Torvalds if (len < sizeof(unsigned long)) 1655bfcfaa77SLinus Torvalds break; 1656bfcfaa77SLinus Torvalds hash += a; 1657f132c5beSAl Viro hash *= 9; 1658bfcfaa77SLinus Torvalds name += sizeof(unsigned long); 1659bfcfaa77SLinus Torvalds len -= sizeof(unsigned long); 1660bfcfaa77SLinus Torvalds if (!len) 1661bfcfaa77SLinus Torvalds goto done; 1662bfcfaa77SLinus Torvalds } 1663bfcfaa77SLinus Torvalds mask = ~(~0ul << len*8); 1664bfcfaa77SLinus Torvalds hash += mask & a; 1665bfcfaa77SLinus Torvalds done: 1666bfcfaa77SLinus Torvalds return fold_hash(hash); 1667bfcfaa77SLinus Torvalds } 1668bfcfaa77SLinus Torvalds EXPORT_SYMBOL(full_name_hash); 1669bfcfaa77SLinus Torvalds 1670bfcfaa77SLinus Torvalds /* 1671bfcfaa77SLinus Torvalds * Calculate the length and hash of the path component, and 1672bfcfaa77SLinus Torvalds * return the length of the component; 1673bfcfaa77SLinus Torvalds */ 1674bfcfaa77SLinus Torvalds static inline unsigned long hash_name(const char *name, unsigned int *hashp) 1675bfcfaa77SLinus Torvalds { 167636126f8fSLinus Torvalds unsigned long a, b, adata, bdata, mask, hash, len; 167736126f8fSLinus Torvalds const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS; 1678bfcfaa77SLinus Torvalds 1679bfcfaa77SLinus Torvalds hash = a = 0; 1680bfcfaa77SLinus Torvalds len = -sizeof(unsigned long); 1681bfcfaa77SLinus Torvalds do { 1682bfcfaa77SLinus Torvalds hash = (hash + a) * 9; 1683bfcfaa77SLinus Torvalds len += sizeof(unsigned long); 1684e419b4ccSLinus Torvalds a = load_unaligned_zeropad(name+len); 168536126f8fSLinus Torvalds b = a ^ REPEAT_BYTE('/'); 168636126f8fSLinus Torvalds } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants))); 1687bfcfaa77SLinus Torvalds 168836126f8fSLinus Torvalds adata = prep_zero_mask(a, adata, &constants); 168936126f8fSLinus Torvalds bdata = prep_zero_mask(b, bdata, &constants); 169036126f8fSLinus Torvalds 169136126f8fSLinus Torvalds mask = create_zero_mask(adata | bdata); 169236126f8fSLinus Torvalds 169336126f8fSLinus Torvalds hash += a & zero_bytemask(mask); 1694bfcfaa77SLinus Torvalds *hashp = fold_hash(hash); 1695bfcfaa77SLinus Torvalds 169636126f8fSLinus Torvalds return len + find_zero(mask); 1697bfcfaa77SLinus Torvalds } 1698bfcfaa77SLinus Torvalds 1699bfcfaa77SLinus Torvalds #else 1700bfcfaa77SLinus Torvalds 17010145acc2SLinus Torvalds unsigned int full_name_hash(const unsigned char *name, unsigned int len) 17020145acc2SLinus Torvalds { 17030145acc2SLinus Torvalds unsigned long hash = init_name_hash(); 17040145acc2SLinus Torvalds while (len--) 17050145acc2SLinus Torvalds hash = partial_name_hash(*name++, hash); 17060145acc2SLinus Torvalds return end_name_hash(hash); 17070145acc2SLinus Torvalds } 1708ae942ae7SLinus Torvalds EXPORT_SYMBOL(full_name_hash); 17090145acc2SLinus Torvalds 17103ddcd056SLinus Torvalds /* 1711200e9ef7SLinus Torvalds * We know there's a real path component here of at least 1712200e9ef7SLinus Torvalds * one character. 1713200e9ef7SLinus Torvalds */ 1714200e9ef7SLinus Torvalds static inline unsigned long hash_name(const char *name, unsigned int *hashp) 1715200e9ef7SLinus Torvalds { 1716200e9ef7SLinus Torvalds unsigned long hash = init_name_hash(); 1717200e9ef7SLinus Torvalds unsigned long len = 0, c; 1718200e9ef7SLinus Torvalds 1719200e9ef7SLinus Torvalds c = (unsigned char)*name; 1720200e9ef7SLinus Torvalds do { 1721200e9ef7SLinus Torvalds len++; 1722200e9ef7SLinus Torvalds hash = partial_name_hash(c, hash); 1723200e9ef7SLinus Torvalds c = (unsigned char)name[len]; 1724200e9ef7SLinus Torvalds } while (c && c != '/'); 1725200e9ef7SLinus Torvalds *hashp = end_name_hash(hash); 1726200e9ef7SLinus Torvalds return len; 1727200e9ef7SLinus Torvalds } 1728200e9ef7SLinus Torvalds 1729bfcfaa77SLinus Torvalds #endif 1730bfcfaa77SLinus Torvalds 1731200e9ef7SLinus Torvalds /* 17321da177e4SLinus Torvalds * Name resolution. 1733ea3834d9SPrasanna Meda * This is the basic name resolution function, turning a pathname into 1734ea3834d9SPrasanna Meda * the final dentry. We expect 'base' to be positive and a directory. 17351da177e4SLinus Torvalds * 1736ea3834d9SPrasanna Meda * Returns 0 and nd will have valid dentry and mnt on success. 1737ea3834d9SPrasanna Meda * Returns error and drops reference to input namei data on failure. 17381da177e4SLinus Torvalds */ 17396de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd) 17401da177e4SLinus Torvalds { 17411da177e4SLinus Torvalds struct path next; 17421da177e4SLinus Torvalds int err; 17431da177e4SLinus Torvalds 17441da177e4SLinus Torvalds while (*name=='/') 17451da177e4SLinus Torvalds name++; 17461da177e4SLinus Torvalds if (!*name) 1747086e183aSAl Viro return 0; 17481da177e4SLinus Torvalds 17491da177e4SLinus Torvalds /* At this point we know we have a real path component. */ 17501da177e4SLinus Torvalds for(;;) { 17511da177e4SLinus Torvalds struct qstr this; 1752200e9ef7SLinus Torvalds long len; 1753fe479a58SAl Viro int type; 17541da177e4SLinus Torvalds 175552094c8aSAl Viro err = may_lookup(nd); 17561da177e4SLinus Torvalds if (err) 17571da177e4SLinus Torvalds break; 17581da177e4SLinus Torvalds 1759200e9ef7SLinus Torvalds len = hash_name(name, &this.hash); 17601da177e4SLinus Torvalds this.name = name; 1761200e9ef7SLinus Torvalds this.len = len; 17621da177e4SLinus Torvalds 1763fe479a58SAl Viro type = LAST_NORM; 1764200e9ef7SLinus Torvalds if (name[0] == '.') switch (len) { 1765fe479a58SAl Viro case 2: 1766200e9ef7SLinus Torvalds if (name[1] == '.') { 1767fe479a58SAl Viro type = LAST_DOTDOT; 176816c2cd71SAl Viro nd->flags |= LOOKUP_JUMPED; 176916c2cd71SAl Viro } 1770fe479a58SAl Viro break; 1771fe479a58SAl Viro case 1: 1772fe479a58SAl Viro type = LAST_DOT; 1773fe479a58SAl Viro } 17745a202bcdSAl Viro if (likely(type == LAST_NORM)) { 17755a202bcdSAl Viro struct dentry *parent = nd->path.dentry; 177616c2cd71SAl Viro nd->flags &= ~LOOKUP_JUMPED; 17775a202bcdSAl Viro if (unlikely(parent->d_flags & DCACHE_OP_HASH)) { 17785a202bcdSAl Viro err = parent->d_op->d_hash(parent, nd->inode, 17795a202bcdSAl Viro &this); 17805a202bcdSAl Viro if (err < 0) 17815a202bcdSAl Viro break; 17825a202bcdSAl Viro } 17835a202bcdSAl Viro } 1784fe479a58SAl Viro 1785200e9ef7SLinus Torvalds if (!name[len]) 17861da177e4SLinus Torvalds goto last_component; 1787200e9ef7SLinus Torvalds /* 1788200e9ef7SLinus Torvalds * If it wasn't NUL, we know it was '/'. Skip that 1789200e9ef7SLinus Torvalds * slash, and continue until no more slashes. 1790200e9ef7SLinus Torvalds */ 1791200e9ef7SLinus Torvalds do { 1792200e9ef7SLinus Torvalds len++; 1793200e9ef7SLinus Torvalds } while (unlikely(name[len] == '/')); 1794200e9ef7SLinus Torvalds if (!name[len]) 1795b356379aSAl Viro goto last_component; 1796200e9ef7SLinus Torvalds name += len; 17971da177e4SLinus Torvalds 1798ce57dfc1SAl Viro err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW); 1799ce57dfc1SAl Viro if (err < 0) 1800ce57dfc1SAl Viro return err; 1801fe479a58SAl Viro 1802ce57dfc1SAl Viro if (err) { 1803b356379aSAl Viro err = nested_symlink(&next, nd); 18041da177e4SLinus Torvalds if (err) 1805a7472babSAl Viro return err; 180631e6b01fSNick Piggin } 18073ddcd056SLinus Torvalds if (can_lookup(nd->inode)) 18081da177e4SLinus Torvalds continue; 18093ddcd056SLinus Torvalds err = -ENOTDIR; 18103ddcd056SLinus Torvalds break; 18111da177e4SLinus Torvalds /* here ends the main loop */ 18121da177e4SLinus Torvalds 18131da177e4SLinus Torvalds last_component: 1814ce57dfc1SAl Viro nd->last = this; 1815ce57dfc1SAl Viro nd->last_type = type; 1816ce57dfc1SAl Viro return 0; 1817ce57dfc1SAl Viro } 1818951361f9SAl Viro terminate_walk(nd); 18191da177e4SLinus Torvalds return err; 18201da177e4SLinus Torvalds } 18211da177e4SLinus Torvalds 182270e9b357SAl Viro static int path_init(int dfd, const char *name, unsigned int flags, 182370e9b357SAl Viro struct nameidata *nd, struct file **fp) 182431e6b01fSNick Piggin { 182531e6b01fSNick Piggin int retval = 0; 182631e6b01fSNick Piggin 182731e6b01fSNick Piggin nd->last_type = LAST_ROOT; /* if there are only slashes... */ 182816c2cd71SAl Viro nd->flags = flags | LOOKUP_JUMPED; 182931e6b01fSNick Piggin nd->depth = 0; 18305b6ca027SAl Viro if (flags & LOOKUP_ROOT) { 18315b6ca027SAl Viro struct inode *inode = nd->root.dentry->d_inode; 183273d049a4SAl Viro if (*name) { 18335b6ca027SAl Viro if (!inode->i_op->lookup) 18345b6ca027SAl Viro return -ENOTDIR; 18355b6ca027SAl Viro retval = inode_permission(inode, MAY_EXEC); 18365b6ca027SAl Viro if (retval) 18375b6ca027SAl Viro return retval; 183873d049a4SAl Viro } 18395b6ca027SAl Viro nd->path = nd->root; 18405b6ca027SAl Viro nd->inode = inode; 18415b6ca027SAl Viro if (flags & LOOKUP_RCU) { 184232a7991bSAl Viro lock_rcu_walk(); 18435b6ca027SAl Viro nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 18445b6ca027SAl Viro } else { 18455b6ca027SAl Viro path_get(&nd->path); 18465b6ca027SAl Viro } 18475b6ca027SAl Viro return 0; 18485b6ca027SAl Viro } 18495b6ca027SAl Viro 185031e6b01fSNick Piggin nd->root.mnt = NULL; 185131e6b01fSNick Piggin 185231e6b01fSNick Piggin if (*name=='/') { 1853e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 185432a7991bSAl Viro lock_rcu_walk(); 1855e41f7d4eSAl Viro set_root_rcu(nd); 1856e41f7d4eSAl Viro } else { 1857e41f7d4eSAl Viro set_root(nd); 1858e41f7d4eSAl Viro path_get(&nd->root); 1859e41f7d4eSAl Viro } 186031e6b01fSNick Piggin nd->path = nd->root; 186131e6b01fSNick Piggin } else if (dfd == AT_FDCWD) { 1862e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 186331e6b01fSNick Piggin struct fs_struct *fs = current->fs; 1864c28cc364SNick Piggin unsigned seq; 186531e6b01fSNick Piggin 186632a7991bSAl Viro lock_rcu_walk(); 186731e6b01fSNick Piggin 1868c28cc364SNick Piggin do { 1869c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 187031e6b01fSNick Piggin nd->path = fs->pwd; 1871c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 1872c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 1873e41f7d4eSAl Viro } else { 1874e41f7d4eSAl Viro get_fs_pwd(current->fs, &nd->path); 1875e41f7d4eSAl Viro } 187631e6b01fSNick Piggin } else { 18772903ff01SAl Viro struct fd f = fdget_raw(dfd); 187831e6b01fSNick Piggin struct dentry *dentry; 187931e6b01fSNick Piggin 18802903ff01SAl Viro if (!f.file) 18812903ff01SAl Viro return -EBADF; 188231e6b01fSNick Piggin 18832903ff01SAl Viro dentry = f.file->f_path.dentry; 188431e6b01fSNick Piggin 1885f52e0c11SAl Viro if (*name) { 18862903ff01SAl Viro if (!S_ISDIR(dentry->d_inode->i_mode)) { 18872903ff01SAl Viro fdput(f); 18882903ff01SAl Viro return -ENOTDIR; 1889f52e0c11SAl Viro } 189031e6b01fSNick Piggin 18912903ff01SAl Viro retval = inode_permission(dentry->d_inode, MAY_EXEC); 18922903ff01SAl Viro if (retval) { 18932903ff01SAl Viro fdput(f); 18942903ff01SAl Viro return retval; 18952903ff01SAl Viro } 18962903ff01SAl Viro } 18972903ff01SAl Viro 18982903ff01SAl Viro nd->path = f.file->f_path; 1899e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 19002903ff01SAl Viro if (f.need_put) 19012903ff01SAl Viro *fp = f.file; 1902c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 190332a7991bSAl Viro lock_rcu_walk(); 19045590ff0dSUlrich Drepper } else { 19052903ff01SAl Viro path_get(&nd->path); 19062903ff01SAl Viro fdput(f); 19071da177e4SLinus Torvalds } 1908e41f7d4eSAl Viro } 1909e41f7d4eSAl Viro 191031e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 19119b4a9b14SAl Viro return 0; 19129b4a9b14SAl Viro } 19139b4a9b14SAl Viro 1914bd92d7feSAl Viro static inline int lookup_last(struct nameidata *nd, struct path *path) 1915bd92d7feSAl Viro { 1916bd92d7feSAl Viro if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len]) 1917bd92d7feSAl Viro nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 1918bd92d7feSAl Viro 1919bd92d7feSAl Viro nd->flags &= ~LOOKUP_PARENT; 1920bd92d7feSAl Viro return walk_component(nd, path, &nd->last, nd->last_type, 1921bd92d7feSAl Viro nd->flags & LOOKUP_FOLLOW); 1922bd92d7feSAl Viro } 1923bd92d7feSAl Viro 19249b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */ 1925ee0827cdSAl Viro static int path_lookupat(int dfd, const char *name, 19269b4a9b14SAl Viro unsigned int flags, struct nameidata *nd) 19279b4a9b14SAl Viro { 192870e9b357SAl Viro struct file *base = NULL; 1929bd92d7feSAl Viro struct path path; 1930bd92d7feSAl Viro int err; 193131e6b01fSNick Piggin 193231e6b01fSNick Piggin /* 193331e6b01fSNick Piggin * Path walking is largely split up into 2 different synchronisation 193431e6b01fSNick Piggin * schemes, rcu-walk and ref-walk (explained in 193531e6b01fSNick Piggin * Documentation/filesystems/path-lookup.txt). These share much of the 193631e6b01fSNick Piggin * path walk code, but some things particularly setup, cleanup, and 193731e6b01fSNick Piggin * following mounts are sufficiently divergent that functions are 193831e6b01fSNick Piggin * duplicated. Typically there is a function foo(), and its RCU 193931e6b01fSNick Piggin * analogue, foo_rcu(). 194031e6b01fSNick Piggin * 194131e6b01fSNick Piggin * -ECHILD is the error number of choice (just to avoid clashes) that 194231e6b01fSNick Piggin * is returned if some aspect of an rcu-walk fails. Such an error must 194331e6b01fSNick Piggin * be handled by restarting a traditional ref-walk (which will always 194431e6b01fSNick Piggin * be able to complete). 194531e6b01fSNick Piggin */ 1946bd92d7feSAl Viro err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base); 1947ee0827cdSAl Viro 1948bd92d7feSAl Viro if (unlikely(err)) 1949bd92d7feSAl Viro return err; 1950ee0827cdSAl Viro 1951ee0827cdSAl Viro current->total_link_count = 0; 1952bd92d7feSAl Viro err = link_path_walk(name, nd); 1953bd92d7feSAl Viro 1954bd92d7feSAl Viro if (!err && !(flags & LOOKUP_PARENT)) { 1955bd92d7feSAl Viro err = lookup_last(nd, &path); 1956bd92d7feSAl Viro while (err > 0) { 1957bd92d7feSAl Viro void *cookie; 1958bd92d7feSAl Viro struct path link = path; 1959800179c9SKees Cook err = may_follow_link(&link, nd); 1960800179c9SKees Cook if (unlikely(err)) 1961800179c9SKees Cook break; 1962bd92d7feSAl Viro nd->flags |= LOOKUP_PARENT; 1963574197e0SAl Viro err = follow_link(&link, nd, &cookie); 19646d7b5aaeSAl Viro if (err) 19656d7b5aaeSAl Viro break; 1966bd92d7feSAl Viro err = lookup_last(nd, &path); 1967574197e0SAl Viro put_link(nd, &link, cookie); 1968bd92d7feSAl Viro } 1969bd92d7feSAl Viro } 1970ee0827cdSAl Viro 19719f1fafeeSAl Viro if (!err) 19729f1fafeeSAl Viro err = complete_walk(nd); 1973bd92d7feSAl Viro 1974bd92d7feSAl Viro if (!err && nd->flags & LOOKUP_DIRECTORY) { 1975bd92d7feSAl Viro if (!nd->inode->i_op->lookup) { 1976bd92d7feSAl Viro path_put(&nd->path); 1977bd23a539SAl Viro err = -ENOTDIR; 1978bd92d7feSAl Viro } 1979bd92d7feSAl Viro } 198016c2cd71SAl Viro 198170e9b357SAl Viro if (base) 198270e9b357SAl Viro fput(base); 1983ee0827cdSAl Viro 19845b6ca027SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) { 198531e6b01fSNick Piggin path_put(&nd->root); 198631e6b01fSNick Piggin nd->root.mnt = NULL; 198731e6b01fSNick Piggin } 1988bd92d7feSAl Viro return err; 198931e6b01fSNick Piggin } 199031e6b01fSNick Piggin 1991ee0827cdSAl Viro static int do_path_lookup(int dfd, const char *name, 1992ee0827cdSAl Viro unsigned int flags, struct nameidata *nd) 1993ee0827cdSAl Viro { 1994ee0827cdSAl Viro int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd); 1995ee0827cdSAl Viro if (unlikely(retval == -ECHILD)) 1996ee0827cdSAl Viro retval = path_lookupat(dfd, name, flags, nd); 1997ee0827cdSAl Viro if (unlikely(retval == -ESTALE)) 1998ee0827cdSAl Viro retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd); 1999ee0827cdSAl Viro 2000f78570ddSJeff Layton if (likely(!retval)) 2001bfcec708SJeff Layton audit_inode(name, nd->path.dentry, flags & LOOKUP_PARENT); 2002170aa3d0SUlrich Drepper return retval; 20031da177e4SLinus Torvalds } 20041da177e4SLinus Torvalds 200579714f72SAl Viro /* does lookup, returns the object with parent locked */ 200679714f72SAl Viro struct dentry *kern_path_locked(const char *name, struct path *path) 20075590ff0dSUlrich Drepper { 200879714f72SAl Viro struct nameidata nd; 200979714f72SAl Viro struct dentry *d; 201079714f72SAl Viro int err = do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, &nd); 201179714f72SAl Viro if (err) 201279714f72SAl Viro return ERR_PTR(err); 201379714f72SAl Viro if (nd.last_type != LAST_NORM) { 201479714f72SAl Viro path_put(&nd.path); 201579714f72SAl Viro return ERR_PTR(-EINVAL); 201679714f72SAl Viro } 201779714f72SAl Viro mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 20181e0ea001SAl Viro d = __lookup_hash(&nd.last, nd.path.dentry, 0); 201979714f72SAl Viro if (IS_ERR(d)) { 202079714f72SAl Viro mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 202179714f72SAl Viro path_put(&nd.path); 202279714f72SAl Viro return d; 202379714f72SAl Viro } 202479714f72SAl Viro *path = nd.path; 202579714f72SAl Viro return d; 20265590ff0dSUlrich Drepper } 20275590ff0dSUlrich Drepper 2028d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path) 2029d1811465SAl Viro { 2030d1811465SAl Viro struct nameidata nd; 2031d1811465SAl Viro int res = do_path_lookup(AT_FDCWD, name, flags, &nd); 2032d1811465SAl Viro if (!res) 2033d1811465SAl Viro *path = nd.path; 2034d1811465SAl Viro return res; 2035d1811465SAl Viro } 2036d1811465SAl Viro 203716f18200SJosef 'Jeff' Sipek /** 203816f18200SJosef 'Jeff' Sipek * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair 203916f18200SJosef 'Jeff' Sipek * @dentry: pointer to dentry of the base directory 204016f18200SJosef 'Jeff' Sipek * @mnt: pointer to vfs mount of the base directory 204116f18200SJosef 'Jeff' Sipek * @name: pointer to file name 204216f18200SJosef 'Jeff' Sipek * @flags: lookup flags 2043e0a01249SAl Viro * @path: pointer to struct path to fill 204416f18200SJosef 'Jeff' Sipek */ 204516f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt, 204616f18200SJosef 'Jeff' Sipek const char *name, unsigned int flags, 2047e0a01249SAl Viro struct path *path) 204816f18200SJosef 'Jeff' Sipek { 2049e0a01249SAl Viro struct nameidata nd; 2050e0a01249SAl Viro int err; 2051e0a01249SAl Viro nd.root.dentry = dentry; 2052e0a01249SAl Viro nd.root.mnt = mnt; 2053e0a01249SAl Viro BUG_ON(flags & LOOKUP_PARENT); 20545b6ca027SAl Viro /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */ 2055e0a01249SAl Viro err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd); 2056e0a01249SAl Viro if (!err) 2057e0a01249SAl Viro *path = nd.path; 2058e0a01249SAl Viro return err; 205916f18200SJosef 'Jeff' Sipek } 206016f18200SJosef 'Jeff' Sipek 2061057f6c01SJames Morris /* 2062057f6c01SJames Morris * Restricted form of lookup. Doesn't follow links, single-component only, 2063057f6c01SJames Morris * needs parent already locked. Doesn't follow mounts. 2064057f6c01SJames Morris * SMP-safe. 2065057f6c01SJames Morris */ 2066a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd) 20671da177e4SLinus Torvalds { 206872bd866aSAl Viro return __lookup_hash(&nd->last, nd->path.dentry, nd->flags); 20691da177e4SLinus Torvalds } 20701da177e4SLinus Torvalds 2071eead1911SChristoph Hellwig /** 2072a6b91919SRandy Dunlap * lookup_one_len - filesystem helper to lookup single pathname component 2073eead1911SChristoph Hellwig * @name: pathname component to lookup 2074eead1911SChristoph Hellwig * @base: base directory to lookup from 2075eead1911SChristoph Hellwig * @len: maximum length @len should be interpreted to 2076eead1911SChristoph Hellwig * 2077a6b91919SRandy Dunlap * Note that this routine is purely a helper for filesystem usage and should 2078a6b91919SRandy Dunlap * not be called by generic code. Also note that by using this function the 2079eead1911SChristoph Hellwig * nameidata argument is passed to the filesystem methods and a filesystem 2080eead1911SChristoph Hellwig * using this helper needs to be prepared for that. 2081eead1911SChristoph Hellwig */ 2082057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len) 2083057f6c01SJames Morris { 2084057f6c01SJames Morris struct qstr this; 20856a96ba54SAl Viro unsigned int c; 2086cda309deSMiklos Szeredi int err; 2087057f6c01SJames Morris 20882f9092e1SDavid Woodhouse WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex)); 20892f9092e1SDavid Woodhouse 20906a96ba54SAl Viro this.name = name; 20916a96ba54SAl Viro this.len = len; 20920145acc2SLinus Torvalds this.hash = full_name_hash(name, len); 20936a96ba54SAl Viro if (!len) 20946a96ba54SAl Viro return ERR_PTR(-EACCES); 20956a96ba54SAl Viro 20966a96ba54SAl Viro while (len--) { 20976a96ba54SAl Viro c = *(const unsigned char *)name++; 20986a96ba54SAl Viro if (c == '/' || c == '\0') 20996a96ba54SAl Viro return ERR_PTR(-EACCES); 21006a96ba54SAl Viro } 21015a202bcdSAl Viro /* 21025a202bcdSAl Viro * See if the low-level filesystem might want 21035a202bcdSAl Viro * to use its own hash.. 21045a202bcdSAl Viro */ 21055a202bcdSAl Viro if (base->d_flags & DCACHE_OP_HASH) { 21065a202bcdSAl Viro int err = base->d_op->d_hash(base, base->d_inode, &this); 21075a202bcdSAl Viro if (err < 0) 21085a202bcdSAl Viro return ERR_PTR(err); 21095a202bcdSAl Viro } 2110eead1911SChristoph Hellwig 2111cda309deSMiklos Szeredi err = inode_permission(base->d_inode, MAY_EXEC); 2112cda309deSMiklos Szeredi if (err) 2113cda309deSMiklos Szeredi return ERR_PTR(err); 2114cda309deSMiklos Szeredi 211572bd866aSAl Viro return __lookup_hash(&this, base, 0); 2116057f6c01SJames Morris } 2117057f6c01SJames Morris 21181fa1e7f6SAndy Whitcroft int user_path_at_empty(int dfd, const char __user *name, unsigned flags, 21191fa1e7f6SAndy Whitcroft struct path *path, int *empty) 21201da177e4SLinus Torvalds { 21212d8f3038SAl Viro struct nameidata nd; 212291a27b2aSJeff Layton struct filename *tmp = getname_flags(name, flags, empty); 21231da177e4SLinus Torvalds int err = PTR_ERR(tmp); 21241da177e4SLinus Torvalds if (!IS_ERR(tmp)) { 21252d8f3038SAl Viro 21262d8f3038SAl Viro BUG_ON(flags & LOOKUP_PARENT); 21272d8f3038SAl Viro 212891a27b2aSJeff Layton err = do_path_lookup(dfd, tmp->name, flags, &nd); 21291da177e4SLinus Torvalds putname(tmp); 21302d8f3038SAl Viro if (!err) 21312d8f3038SAl Viro *path = nd.path; 21321da177e4SLinus Torvalds } 21331da177e4SLinus Torvalds return err; 21341da177e4SLinus Torvalds } 21351da177e4SLinus Torvalds 21361fa1e7f6SAndy Whitcroft int user_path_at(int dfd, const char __user *name, unsigned flags, 21371fa1e7f6SAndy Whitcroft struct path *path) 21381fa1e7f6SAndy Whitcroft { 2139f7493e5dSLinus Torvalds return user_path_at_empty(dfd, name, flags, path, NULL); 21401fa1e7f6SAndy Whitcroft } 21411fa1e7f6SAndy Whitcroft 214291a27b2aSJeff Layton static struct filename * 214391a27b2aSJeff Layton user_path_parent(int dfd, const char __user *path, struct nameidata *nd) 21442ad94ae6SAl Viro { 214591a27b2aSJeff Layton struct filename *s = getname(path); 21462ad94ae6SAl Viro int error; 21472ad94ae6SAl Viro 21482ad94ae6SAl Viro if (IS_ERR(s)) 214991a27b2aSJeff Layton return s; 21502ad94ae6SAl Viro 215191a27b2aSJeff Layton error = do_path_lookup(dfd, s->name, LOOKUP_PARENT, nd); 215291a27b2aSJeff Layton if (error) { 21532ad94ae6SAl Viro putname(s); 215491a27b2aSJeff Layton return ERR_PTR(error); 215591a27b2aSJeff Layton } 21562ad94ae6SAl Viro 215791a27b2aSJeff Layton return s; 21582ad94ae6SAl Viro } 21592ad94ae6SAl Viro 21601da177e4SLinus Torvalds /* 21611da177e4SLinus Torvalds * It's inline, so penalty for filesystems that don't use sticky bit is 21621da177e4SLinus Torvalds * minimal. 21631da177e4SLinus Torvalds */ 21641da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode) 21651da177e4SLinus Torvalds { 21668e96e3b7SEric W. Biederman kuid_t fsuid = current_fsuid(); 2167da9592edSDavid Howells 21681da177e4SLinus Torvalds if (!(dir->i_mode & S_ISVTX)) 21691da177e4SLinus Torvalds return 0; 21708e96e3b7SEric W. Biederman if (uid_eq(inode->i_uid, fsuid)) 21711da177e4SLinus Torvalds return 0; 21728e96e3b7SEric W. Biederman if (uid_eq(dir->i_uid, fsuid)) 21731da177e4SLinus Torvalds return 0; 21741a48e2acSEric W. Biederman return !inode_capable(inode, CAP_FOWNER); 21751da177e4SLinus Torvalds } 21761da177e4SLinus Torvalds 21771da177e4SLinus Torvalds /* 21781da177e4SLinus Torvalds * Check whether we can remove a link victim from directory dir, check 21791da177e4SLinus Torvalds * whether the type of victim is right. 21801da177e4SLinus Torvalds * 1. We can't do it if dir is read-only (done in permission()) 21811da177e4SLinus Torvalds * 2. We should have write and exec permissions on dir 21821da177e4SLinus Torvalds * 3. We can't remove anything from append-only dir 21831da177e4SLinus Torvalds * 4. We can't do anything with immutable dir (done in permission()) 21841da177e4SLinus Torvalds * 5. If the sticky bit on dir is set we should either 21851da177e4SLinus Torvalds * a. be owner of dir, or 21861da177e4SLinus Torvalds * b. be owner of victim, or 21871da177e4SLinus Torvalds * c. have CAP_FOWNER capability 21881da177e4SLinus Torvalds * 6. If the victim is append-only or immutable we can't do antyhing with 21891da177e4SLinus Torvalds * links pointing to it. 21901da177e4SLinus Torvalds * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. 21911da177e4SLinus Torvalds * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. 21921da177e4SLinus Torvalds * 9. We can't remove a root or mountpoint. 21931da177e4SLinus Torvalds * 10. We don't allow removal of NFS sillyrenamed files; it's handled by 21941da177e4SLinus Torvalds * nfs_async_unlink(). 21951da177e4SLinus Torvalds */ 2196858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir) 21971da177e4SLinus Torvalds { 21981da177e4SLinus Torvalds int error; 21991da177e4SLinus Torvalds 22001da177e4SLinus Torvalds if (!victim->d_inode) 22011da177e4SLinus Torvalds return -ENOENT; 22021da177e4SLinus Torvalds 22031da177e4SLinus Torvalds BUG_ON(victim->d_parent->d_inode != dir); 22044fa6b5ecSJeff Layton audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE); 22051da177e4SLinus Torvalds 2206f419a2e3SAl Viro error = inode_permission(dir, MAY_WRITE | MAY_EXEC); 22071da177e4SLinus Torvalds if (error) 22081da177e4SLinus Torvalds return error; 22091da177e4SLinus Torvalds if (IS_APPEND(dir)) 22101da177e4SLinus Torvalds return -EPERM; 22111da177e4SLinus Torvalds if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)|| 2212f9454548SHugh Dickins IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode)) 22131da177e4SLinus Torvalds return -EPERM; 22141da177e4SLinus Torvalds if (isdir) { 22151da177e4SLinus Torvalds if (!S_ISDIR(victim->d_inode->i_mode)) 22161da177e4SLinus Torvalds return -ENOTDIR; 22171da177e4SLinus Torvalds if (IS_ROOT(victim)) 22181da177e4SLinus Torvalds return -EBUSY; 22191da177e4SLinus Torvalds } else if (S_ISDIR(victim->d_inode->i_mode)) 22201da177e4SLinus Torvalds return -EISDIR; 22211da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 22221da177e4SLinus Torvalds return -ENOENT; 22231da177e4SLinus Torvalds if (victim->d_flags & DCACHE_NFSFS_RENAMED) 22241da177e4SLinus Torvalds return -EBUSY; 22251da177e4SLinus Torvalds return 0; 22261da177e4SLinus Torvalds } 22271da177e4SLinus Torvalds 22281da177e4SLinus Torvalds /* Check whether we can create an object with dentry child in directory 22291da177e4SLinus Torvalds * dir. 22301da177e4SLinus Torvalds * 1. We can't do it if child already exists (open has special treatment for 22311da177e4SLinus Torvalds * this case, but since we are inlined it's OK) 22321da177e4SLinus Torvalds * 2. We can't do it if dir is read-only (done in permission()) 22331da177e4SLinus Torvalds * 3. We should have write and exec permissions on dir 22341da177e4SLinus Torvalds * 4. We can't do it if dir is immutable (done in permission()) 22351da177e4SLinus Torvalds */ 2236a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child) 22371da177e4SLinus Torvalds { 22381da177e4SLinus Torvalds if (child->d_inode) 22391da177e4SLinus Torvalds return -EEXIST; 22401da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 22411da177e4SLinus Torvalds return -ENOENT; 2242f419a2e3SAl Viro return inode_permission(dir, MAY_WRITE | MAY_EXEC); 22431da177e4SLinus Torvalds } 22441da177e4SLinus Torvalds 22451da177e4SLinus Torvalds /* 22461da177e4SLinus Torvalds * p1 and p2 should be directories on the same fs. 22471da177e4SLinus Torvalds */ 22481da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2) 22491da177e4SLinus Torvalds { 22501da177e4SLinus Torvalds struct dentry *p; 22511da177e4SLinus Torvalds 22521da177e4SLinus Torvalds if (p1 == p2) { 2253f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 22541da177e4SLinus Torvalds return NULL; 22551da177e4SLinus Torvalds } 22561da177e4SLinus Torvalds 2257a11f3a05SArjan van de Ven mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 22581da177e4SLinus Torvalds 2259e2761a11SOGAWA Hirofumi p = d_ancestor(p2, p1); 2260e2761a11SOGAWA Hirofumi if (p) { 2261f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT); 2262f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD); 22631da177e4SLinus Torvalds return p; 22641da177e4SLinus Torvalds } 22651da177e4SLinus Torvalds 2266e2761a11SOGAWA Hirofumi p = d_ancestor(p1, p2); 2267e2761a11SOGAWA Hirofumi if (p) { 2268f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 2269f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 22701da177e4SLinus Torvalds return p; 22711da177e4SLinus Torvalds } 22721da177e4SLinus Torvalds 2273f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 2274f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 22751da177e4SLinus Torvalds return NULL; 22761da177e4SLinus Torvalds } 22771da177e4SLinus Torvalds 22781da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2) 22791da177e4SLinus Torvalds { 22801b1dcc1bSJes Sorensen mutex_unlock(&p1->d_inode->i_mutex); 22811da177e4SLinus Torvalds if (p1 != p2) { 22821b1dcc1bSJes Sorensen mutex_unlock(&p2->d_inode->i_mutex); 2283a11f3a05SArjan van de Ven mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 22841da177e4SLinus Torvalds } 22851da177e4SLinus Torvalds } 22861da177e4SLinus Torvalds 22874acdaf27SAl Viro int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode, 2288312b63fbSAl Viro bool want_excl) 22891da177e4SLinus Torvalds { 2290a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 22911da177e4SLinus Torvalds if (error) 22921da177e4SLinus Torvalds return error; 22931da177e4SLinus Torvalds 2294acfa4380SAl Viro if (!dir->i_op->create) 22951da177e4SLinus Torvalds return -EACCES; /* shouldn't it be ENOSYS? */ 22961da177e4SLinus Torvalds mode &= S_IALLUGO; 22971da177e4SLinus Torvalds mode |= S_IFREG; 22981da177e4SLinus Torvalds error = security_inode_create(dir, dentry, mode); 22991da177e4SLinus Torvalds if (error) 23001da177e4SLinus Torvalds return error; 2301312b63fbSAl Viro error = dir->i_op->create(dir, dentry, mode, want_excl); 2302a74574aaSStephen Smalley if (!error) 2303f38aa942SAmy Griffis fsnotify_create(dir, dentry); 23041da177e4SLinus Torvalds return error; 23051da177e4SLinus Torvalds } 23061da177e4SLinus Torvalds 230773d049a4SAl Viro static int may_open(struct path *path, int acc_mode, int flag) 23081da177e4SLinus Torvalds { 23093fb64190SChristoph Hellwig struct dentry *dentry = path->dentry; 23101da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 23111da177e4SLinus Torvalds int error; 23121da177e4SLinus Torvalds 2313bcda7652SAl Viro /* O_PATH? */ 2314bcda7652SAl Viro if (!acc_mode) 2315bcda7652SAl Viro return 0; 2316bcda7652SAl Viro 23171da177e4SLinus Torvalds if (!inode) 23181da177e4SLinus Torvalds return -ENOENT; 23191da177e4SLinus Torvalds 2320c8fe8f30SChristoph Hellwig switch (inode->i_mode & S_IFMT) { 2321c8fe8f30SChristoph Hellwig case S_IFLNK: 23221da177e4SLinus Torvalds return -ELOOP; 2323c8fe8f30SChristoph Hellwig case S_IFDIR: 2324c8fe8f30SChristoph Hellwig if (acc_mode & MAY_WRITE) 23251da177e4SLinus Torvalds return -EISDIR; 2326c8fe8f30SChristoph Hellwig break; 2327c8fe8f30SChristoph Hellwig case S_IFBLK: 2328c8fe8f30SChristoph Hellwig case S_IFCHR: 23293fb64190SChristoph Hellwig if (path->mnt->mnt_flags & MNT_NODEV) 23301da177e4SLinus Torvalds return -EACCES; 2331c8fe8f30SChristoph Hellwig /*FALLTHRU*/ 2332c8fe8f30SChristoph Hellwig case S_IFIFO: 2333c8fe8f30SChristoph Hellwig case S_IFSOCK: 23341da177e4SLinus Torvalds flag &= ~O_TRUNC; 2335c8fe8f30SChristoph Hellwig break; 23364a3fd211SDave Hansen } 2337b41572e9SDave Hansen 23383fb64190SChristoph Hellwig error = inode_permission(inode, acc_mode); 2339b41572e9SDave Hansen if (error) 2340b41572e9SDave Hansen return error; 23416146f0d5SMimi Zohar 23421da177e4SLinus Torvalds /* 23431da177e4SLinus Torvalds * An append-only file must be opened in append mode for writing. 23441da177e4SLinus Torvalds */ 23451da177e4SLinus Torvalds if (IS_APPEND(inode)) { 23468737c930SAl Viro if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND)) 23477715b521SAl Viro return -EPERM; 23481da177e4SLinus Torvalds if (flag & O_TRUNC) 23497715b521SAl Viro return -EPERM; 23501da177e4SLinus Torvalds } 23511da177e4SLinus Torvalds 23521da177e4SLinus Torvalds /* O_NOATIME can only be set by the owner or superuser */ 23532e149670SSerge E. Hallyn if (flag & O_NOATIME && !inode_owner_or_capable(inode)) 23547715b521SAl Viro return -EPERM; 23551da177e4SLinus Torvalds 2356f3c7691eSJ. Bruce Fields return 0; 23577715b521SAl Viro } 23587715b521SAl Viro 2359e1181ee6SJeff Layton static int handle_truncate(struct file *filp) 23607715b521SAl Viro { 2361e1181ee6SJeff Layton struct path *path = &filp->f_path; 23627715b521SAl Viro struct inode *inode = path->dentry->d_inode; 23637715b521SAl Viro int error = get_write_access(inode); 23641da177e4SLinus Torvalds if (error) 23657715b521SAl Viro return error; 23661da177e4SLinus Torvalds /* 23671da177e4SLinus Torvalds * Refuse to truncate files with mandatory locks held on them. 23681da177e4SLinus Torvalds */ 23691da177e4SLinus Torvalds error = locks_verify_locked(inode); 2370be6d3e56SKentaro Takeda if (!error) 2371ea0d3ab2STetsuo Handa error = security_path_truncate(path); 23721da177e4SLinus Torvalds if (!error) { 23737715b521SAl Viro error = do_truncate(path->dentry, 0, 2374d139d7ffSMiklos Szeredi ATTR_MTIME|ATTR_CTIME|ATTR_OPEN, 2375e1181ee6SJeff Layton filp); 23761da177e4SLinus Torvalds } 23771da177e4SLinus Torvalds put_write_access(inode); 2378acd0c935SMimi Zohar return error; 23791da177e4SLinus Torvalds } 23801da177e4SLinus Torvalds 2381d57999e1SDave Hansen static inline int open_to_namei_flags(int flag) 2382d57999e1SDave Hansen { 23838a5e929dSAl Viro if ((flag & O_ACCMODE) == 3) 23848a5e929dSAl Viro flag--; 2385d57999e1SDave Hansen return flag; 2386d57999e1SDave Hansen } 2387d57999e1SDave Hansen 2388d18e9008SMiklos Szeredi static int may_o_create(struct path *dir, struct dentry *dentry, umode_t mode) 2389d18e9008SMiklos Szeredi { 2390d18e9008SMiklos Szeredi int error = security_path_mknod(dir, dentry, mode, 0); 2391d18e9008SMiklos Szeredi if (error) 2392d18e9008SMiklos Szeredi return error; 2393d18e9008SMiklos Szeredi 2394d18e9008SMiklos Szeredi error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC); 2395d18e9008SMiklos Szeredi if (error) 2396d18e9008SMiklos Szeredi return error; 2397d18e9008SMiklos Szeredi 2398d18e9008SMiklos Szeredi return security_inode_create(dir->dentry->d_inode, dentry, mode); 2399d18e9008SMiklos Szeredi } 2400d18e9008SMiklos Szeredi 24011acf0af9SDavid Howells /* 24021acf0af9SDavid Howells * Attempt to atomically look up, create and open a file from a negative 24031acf0af9SDavid Howells * dentry. 24041acf0af9SDavid Howells * 24051acf0af9SDavid Howells * Returns 0 if successful. The file will have been created and attached to 24061acf0af9SDavid Howells * @file by the filesystem calling finish_open(). 24071acf0af9SDavid Howells * 24081acf0af9SDavid Howells * Returns 1 if the file was looked up only or didn't need creating. The 24091acf0af9SDavid Howells * caller will need to perform the open themselves. @path will have been 24101acf0af9SDavid Howells * updated to point to the new dentry. This may be negative. 24111acf0af9SDavid Howells * 24121acf0af9SDavid Howells * Returns an error code otherwise. 24131acf0af9SDavid Howells */ 24142675a4ebSAl Viro static int atomic_open(struct nameidata *nd, struct dentry *dentry, 241530d90494SAl Viro struct path *path, struct file *file, 2416015c3bbcSMiklos Szeredi const struct open_flags *op, 241764894cf8SAl Viro bool got_write, bool need_lookup, 241847237687SAl Viro int *opened) 2419d18e9008SMiklos Szeredi { 2420d18e9008SMiklos Szeredi struct inode *dir = nd->path.dentry->d_inode; 2421d18e9008SMiklos Szeredi unsigned open_flag = open_to_namei_flags(op->open_flag); 2422d18e9008SMiklos Szeredi umode_t mode; 2423d18e9008SMiklos Szeredi int error; 2424d18e9008SMiklos Szeredi int acc_mode; 2425d18e9008SMiklos Szeredi int create_error = 0; 2426d18e9008SMiklos Szeredi struct dentry *const DENTRY_NOT_SET = (void *) -1UL; 2427d18e9008SMiklos Szeredi 2428d18e9008SMiklos Szeredi BUG_ON(dentry->d_inode); 2429d18e9008SMiklos Szeredi 2430d18e9008SMiklos Szeredi /* Don't create child dentry for a dead directory. */ 2431d18e9008SMiklos Szeredi if (unlikely(IS_DEADDIR(dir))) { 24322675a4ebSAl Viro error = -ENOENT; 2433d18e9008SMiklos Szeredi goto out; 2434d18e9008SMiklos Szeredi } 2435d18e9008SMiklos Szeredi 243662b259d8SMiklos Szeredi mode = op->mode; 2437d18e9008SMiklos Szeredi if ((open_flag & O_CREAT) && !IS_POSIXACL(dir)) 2438d18e9008SMiklos Szeredi mode &= ~current_umask(); 2439d18e9008SMiklos Szeredi 2440f8310c59SAl Viro if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT)) { 2441d18e9008SMiklos Szeredi open_flag &= ~O_TRUNC; 244247237687SAl Viro *opened |= FILE_CREATED; 2443d18e9008SMiklos Szeredi } 2444d18e9008SMiklos Szeredi 2445d18e9008SMiklos Szeredi /* 2446d18e9008SMiklos Szeredi * Checking write permission is tricky, bacuse we don't know if we are 2447d18e9008SMiklos Szeredi * going to actually need it: O_CREAT opens should work as long as the 2448d18e9008SMiklos Szeredi * file exists. But checking existence breaks atomicity. The trick is 2449d18e9008SMiklos Szeredi * to check access and if not granted clear O_CREAT from the flags. 2450d18e9008SMiklos Szeredi * 2451d18e9008SMiklos Szeredi * Another problem is returing the "right" error value (e.g. for an 2452d18e9008SMiklos Szeredi * O_EXCL open we want to return EEXIST not EROFS). 2453d18e9008SMiklos Szeredi */ 245464894cf8SAl Viro if (((open_flag & (O_CREAT | O_TRUNC)) || 245564894cf8SAl Viro (open_flag & O_ACCMODE) != O_RDONLY) && unlikely(!got_write)) { 245664894cf8SAl Viro if (!(open_flag & O_CREAT)) { 2457d18e9008SMiklos Szeredi /* 2458d18e9008SMiklos Szeredi * No O_CREATE -> atomicity not a requirement -> fall 2459d18e9008SMiklos Szeredi * back to lookup + open 2460d18e9008SMiklos Szeredi */ 2461d18e9008SMiklos Szeredi goto no_open; 2462d18e9008SMiklos Szeredi } else if (open_flag & (O_EXCL | O_TRUNC)) { 2463d18e9008SMiklos Szeredi /* Fall back and fail with the right error */ 246464894cf8SAl Viro create_error = -EROFS; 2465d18e9008SMiklos Szeredi goto no_open; 2466d18e9008SMiklos Szeredi } else { 2467d18e9008SMiklos Szeredi /* No side effects, safe to clear O_CREAT */ 246864894cf8SAl Viro create_error = -EROFS; 2469d18e9008SMiklos Szeredi open_flag &= ~O_CREAT; 2470d18e9008SMiklos Szeredi } 2471d18e9008SMiklos Szeredi } 2472d18e9008SMiklos Szeredi 2473d18e9008SMiklos Szeredi if (open_flag & O_CREAT) { 247438227f78SMiklos Szeredi error = may_o_create(&nd->path, dentry, mode); 2475d18e9008SMiklos Szeredi if (error) { 2476d18e9008SMiklos Szeredi create_error = error; 2477d18e9008SMiklos Szeredi if (open_flag & O_EXCL) 2478d18e9008SMiklos Szeredi goto no_open; 2479d18e9008SMiklos Szeredi open_flag &= ~O_CREAT; 2480d18e9008SMiklos Szeredi } 2481d18e9008SMiklos Szeredi } 2482d18e9008SMiklos Szeredi 2483d18e9008SMiklos Szeredi if (nd->flags & LOOKUP_DIRECTORY) 2484d18e9008SMiklos Szeredi open_flag |= O_DIRECTORY; 2485d18e9008SMiklos Szeredi 248630d90494SAl Viro file->f_path.dentry = DENTRY_NOT_SET; 248730d90494SAl Viro file->f_path.mnt = nd->path.mnt; 248830d90494SAl Viro error = dir->i_op->atomic_open(dir, dentry, file, open_flag, mode, 248947237687SAl Viro opened); 2490d9585277SAl Viro if (error < 0) { 2491d9585277SAl Viro if (create_error && error == -ENOENT) 2492d9585277SAl Viro error = create_error; 2493d18e9008SMiklos Szeredi goto out; 2494d18e9008SMiklos Szeredi } 2495d18e9008SMiklos Szeredi 2496d18e9008SMiklos Szeredi acc_mode = op->acc_mode; 249747237687SAl Viro if (*opened & FILE_CREATED) { 2498d18e9008SMiklos Szeredi fsnotify_create(dir, dentry); 2499d18e9008SMiklos Szeredi acc_mode = MAY_OPEN; 2500d18e9008SMiklos Szeredi } 2501d18e9008SMiklos Szeredi 2502d9585277SAl Viro if (error) { /* returned 1, that is */ 250330d90494SAl Viro if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) { 25042675a4ebSAl Viro error = -EIO; 2505d18e9008SMiklos Szeredi goto out; 2506d18e9008SMiklos Szeredi } 250730d90494SAl Viro if (file->f_path.dentry) { 2508d18e9008SMiklos Szeredi dput(dentry); 250930d90494SAl Viro dentry = file->f_path.dentry; 2510d18e9008SMiklos Szeredi } 251162b2ce96SSage Weil if (create_error && dentry->d_inode == NULL) { 251262b2ce96SSage Weil error = create_error; 251362b2ce96SSage Weil goto out; 251462b2ce96SSage Weil } 2515d18e9008SMiklos Szeredi goto looked_up; 2516d18e9008SMiklos Szeredi } 2517d18e9008SMiklos Szeredi 2518d18e9008SMiklos Szeredi /* 2519d18e9008SMiklos Szeredi * We didn't have the inode before the open, so check open permission 2520d18e9008SMiklos Szeredi * here. 2521d18e9008SMiklos Szeredi */ 25222675a4ebSAl Viro error = may_open(&file->f_path, acc_mode, open_flag); 25232675a4ebSAl Viro if (error) 25242675a4ebSAl Viro fput(file); 2525d18e9008SMiklos Szeredi 2526d18e9008SMiklos Szeredi out: 2527d18e9008SMiklos Szeredi dput(dentry); 25282675a4ebSAl Viro return error; 2529d18e9008SMiklos Szeredi 2530d18e9008SMiklos Szeredi no_open: 2531d18e9008SMiklos Szeredi if (need_lookup) { 253272bd866aSAl Viro dentry = lookup_real(dir, dentry, nd->flags); 2533d18e9008SMiklos Szeredi if (IS_ERR(dentry)) 25342675a4ebSAl Viro return PTR_ERR(dentry); 2535d18e9008SMiklos Szeredi 2536d18e9008SMiklos Szeredi if (create_error) { 2537d18e9008SMiklos Szeredi int open_flag = op->open_flag; 2538d18e9008SMiklos Szeredi 25392675a4ebSAl Viro error = create_error; 2540d18e9008SMiklos Szeredi if ((open_flag & O_EXCL)) { 2541d18e9008SMiklos Szeredi if (!dentry->d_inode) 2542d18e9008SMiklos Szeredi goto out; 2543d18e9008SMiklos Szeredi } else if (!dentry->d_inode) { 2544d18e9008SMiklos Szeredi goto out; 2545d18e9008SMiklos Szeredi } else if ((open_flag & O_TRUNC) && 2546d18e9008SMiklos Szeredi S_ISREG(dentry->d_inode->i_mode)) { 2547d18e9008SMiklos Szeredi goto out; 2548d18e9008SMiklos Szeredi } 2549d18e9008SMiklos Szeredi /* will fail later, go on to get the right error */ 2550d18e9008SMiklos Szeredi } 2551d18e9008SMiklos Szeredi } 2552d18e9008SMiklos Szeredi looked_up: 2553d18e9008SMiklos Szeredi path->dentry = dentry; 2554d18e9008SMiklos Szeredi path->mnt = nd->path.mnt; 25552675a4ebSAl Viro return 1; 2556d18e9008SMiklos Szeredi } 2557d18e9008SMiklos Szeredi 255831e6b01fSNick Piggin /* 25591acf0af9SDavid Howells * Look up and maybe create and open the last component. 2560d58ffd35SMiklos Szeredi * 2561d58ffd35SMiklos Szeredi * Must be called with i_mutex held on parent. 2562d58ffd35SMiklos Szeredi * 25631acf0af9SDavid Howells * Returns 0 if the file was successfully atomically created (if necessary) and 25641acf0af9SDavid Howells * opened. In this case the file will be returned attached to @file. 25651acf0af9SDavid Howells * 25661acf0af9SDavid Howells * Returns 1 if the file was not completely opened at this time, though lookups 25671acf0af9SDavid Howells * and creations will have been performed and the dentry returned in @path will 25681acf0af9SDavid Howells * be positive upon return if O_CREAT was specified. If O_CREAT wasn't 25691acf0af9SDavid Howells * specified then a negative dentry may be returned. 25701acf0af9SDavid Howells * 25711acf0af9SDavid Howells * An error code is returned otherwise. 25721acf0af9SDavid Howells * 25731acf0af9SDavid Howells * FILE_CREATE will be set in @*opened if the dentry was created and will be 25741acf0af9SDavid Howells * cleared otherwise prior to returning. 2575d58ffd35SMiklos Szeredi */ 25762675a4ebSAl Viro static int lookup_open(struct nameidata *nd, struct path *path, 257730d90494SAl Viro struct file *file, 2578d58ffd35SMiklos Szeredi const struct open_flags *op, 257964894cf8SAl Viro bool got_write, int *opened) 2580d58ffd35SMiklos Szeredi { 2581d58ffd35SMiklos Szeredi struct dentry *dir = nd->path.dentry; 258254ef4872SMiklos Szeredi struct inode *dir_inode = dir->d_inode; 2583d58ffd35SMiklos Szeredi struct dentry *dentry; 2584d58ffd35SMiklos Szeredi int error; 258554ef4872SMiklos Szeredi bool need_lookup; 2586d58ffd35SMiklos Szeredi 258747237687SAl Viro *opened &= ~FILE_CREATED; 2588201f956eSAl Viro dentry = lookup_dcache(&nd->last, dir, nd->flags, &need_lookup); 2589d58ffd35SMiklos Szeredi if (IS_ERR(dentry)) 25902675a4ebSAl Viro return PTR_ERR(dentry); 2591d58ffd35SMiklos Szeredi 2592d18e9008SMiklos Szeredi /* Cached positive dentry: will open in f_op->open */ 2593d18e9008SMiklos Szeredi if (!need_lookup && dentry->d_inode) 2594d18e9008SMiklos Szeredi goto out_no_open; 2595d18e9008SMiklos Szeredi 2596d18e9008SMiklos Szeredi if ((nd->flags & LOOKUP_OPEN) && dir_inode->i_op->atomic_open) { 259764894cf8SAl Viro return atomic_open(nd, dentry, path, file, op, got_write, 259847237687SAl Viro need_lookup, opened); 2599d18e9008SMiklos Szeredi } 2600d18e9008SMiklos Szeredi 260154ef4872SMiklos Szeredi if (need_lookup) { 260254ef4872SMiklos Szeredi BUG_ON(dentry->d_inode); 260354ef4872SMiklos Szeredi 260472bd866aSAl Viro dentry = lookup_real(dir_inode, dentry, nd->flags); 260554ef4872SMiklos Szeredi if (IS_ERR(dentry)) 26062675a4ebSAl Viro return PTR_ERR(dentry); 260754ef4872SMiklos Szeredi } 260854ef4872SMiklos Szeredi 2609d58ffd35SMiklos Szeredi /* Negative dentry, just create the file */ 2610d58ffd35SMiklos Szeredi if (!dentry->d_inode && (op->open_flag & O_CREAT)) { 2611d58ffd35SMiklos Szeredi umode_t mode = op->mode; 2612d58ffd35SMiklos Szeredi if (!IS_POSIXACL(dir->d_inode)) 2613d58ffd35SMiklos Szeredi mode &= ~current_umask(); 2614d58ffd35SMiklos Szeredi /* 2615d58ffd35SMiklos Szeredi * This write is needed to ensure that a 2616d58ffd35SMiklos Szeredi * rw->ro transition does not occur between 2617d58ffd35SMiklos Szeredi * the time when the file is created and when 2618d58ffd35SMiklos Szeredi * a permanent write count is taken through 2619015c3bbcSMiklos Szeredi * the 'struct file' in finish_open(). 2620d58ffd35SMiklos Szeredi */ 262164894cf8SAl Viro if (!got_write) { 262264894cf8SAl Viro error = -EROFS; 2623d58ffd35SMiklos Szeredi goto out_dput; 262464894cf8SAl Viro } 262547237687SAl Viro *opened |= FILE_CREATED; 2626d58ffd35SMiklos Szeredi error = security_path_mknod(&nd->path, dentry, mode, 0); 2627d58ffd35SMiklos Szeredi if (error) 2628d58ffd35SMiklos Szeredi goto out_dput; 2629312b63fbSAl Viro error = vfs_create(dir->d_inode, dentry, mode, 2630312b63fbSAl Viro nd->flags & LOOKUP_EXCL); 2631d58ffd35SMiklos Szeredi if (error) 2632d58ffd35SMiklos Szeredi goto out_dput; 2633d58ffd35SMiklos Szeredi } 2634d18e9008SMiklos Szeredi out_no_open: 2635d58ffd35SMiklos Szeredi path->dentry = dentry; 2636d58ffd35SMiklos Szeredi path->mnt = nd->path.mnt; 26372675a4ebSAl Viro return 1; 2638d58ffd35SMiklos Szeredi 2639d58ffd35SMiklos Szeredi out_dput: 2640d58ffd35SMiklos Szeredi dput(dentry); 26412675a4ebSAl Viro return error; 2642d58ffd35SMiklos Szeredi } 2643d58ffd35SMiklos Szeredi 2644d58ffd35SMiklos Szeredi /* 2645fe2d35ffSAl Viro * Handle the last step of open() 264631e6b01fSNick Piggin */ 26472675a4ebSAl Viro static int do_last(struct nameidata *nd, struct path *path, 264830d90494SAl Viro struct file *file, const struct open_flags *op, 264947237687SAl Viro int *opened, const char *pathname) 2650fb1cc555SAl Viro { 2651a1e28038SAl Viro struct dentry *dir = nd->path.dentry; 2652ca344a89SAl Viro int open_flag = op->open_flag; 265377d660a8SMiklos Szeredi bool will_truncate = (open_flag & O_TRUNC) != 0; 265464894cf8SAl Viro bool got_write = false; 2655bcda7652SAl Viro int acc_mode = op->acc_mode; 2656a1eb3315SMiklos Szeredi struct inode *inode; 265777d660a8SMiklos Szeredi bool symlink_ok = false; 265816b1c1cdSMiklos Szeredi struct path save_parent = { .dentry = NULL, .mnt = NULL }; 265916b1c1cdSMiklos Szeredi bool retried = false; 266016c2cd71SAl Viro int error; 2661fb1cc555SAl Viro 2662c3e380b0SAl Viro nd->flags &= ~LOOKUP_PARENT; 2663c3e380b0SAl Viro nd->flags |= op->intent; 2664c3e380b0SAl Viro 26651f36f774SAl Viro switch (nd->last_type) { 26661f36f774SAl Viro case LAST_DOTDOT: 2667176306f5SNeil Brown case LAST_DOT: 2668fe2d35ffSAl Viro error = handle_dots(nd, nd->last_type); 2669fe2d35ffSAl Viro if (error) 26702675a4ebSAl Viro return error; 26711f36f774SAl Viro /* fallthrough */ 26721f36f774SAl Viro case LAST_ROOT: 26739f1fafeeSAl Viro error = complete_walk(nd); 267416c2cd71SAl Viro if (error) 26752675a4ebSAl Viro return error; 2676bfcec708SJeff Layton audit_inode(pathname, nd->path.dentry, 0); 2677ca344a89SAl Viro if (open_flag & O_CREAT) { 267816c2cd71SAl Viro error = -EISDIR; 26792675a4ebSAl Viro goto out; 2680fe2d35ffSAl Viro } 2681e83db167SMiklos Szeredi goto finish_open; 26821f36f774SAl Viro case LAST_BIND: 26839f1fafeeSAl Viro error = complete_walk(nd); 268416c2cd71SAl Viro if (error) 26852675a4ebSAl Viro return error; 2686bfcec708SJeff Layton audit_inode(pathname, dir, 0); 2687e83db167SMiklos Szeredi goto finish_open; 26881f36f774SAl Viro } 2689a2c36b45SAl Viro 2690ca344a89SAl Viro if (!(open_flag & O_CREAT)) { 2691fe2d35ffSAl Viro if (nd->last.name[nd->last.len]) 2692fe2d35ffSAl Viro nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 2693bcda7652SAl Viro if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW)) 269477d660a8SMiklos Szeredi symlink_ok = true; 2695fe2d35ffSAl Viro /* we _can_ be in RCU mode here */ 2696a1eb3315SMiklos Szeredi error = lookup_fast(nd, &nd->last, path, &inode); 269771574865SMiklos Szeredi if (likely(!error)) 269871574865SMiklos Szeredi goto finish_lookup; 269971574865SMiklos Szeredi 2700ce57dfc1SAl Viro if (error < 0) 27012675a4ebSAl Viro goto out; 2702a1eb3315SMiklos Szeredi 270337d7fffcSMiklos Szeredi BUG_ON(nd->inode != dir->d_inode); 2704b6183df7SMiklos Szeredi } else { 2705fe2d35ffSAl Viro /* create side of things */ 2706a3fbbde7SAl Viro /* 2707b6183df7SMiklos Szeredi * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED 2708b6183df7SMiklos Szeredi * has been cleared when we got to the last component we are 2709b6183df7SMiklos Szeredi * about to look up 2710a3fbbde7SAl Viro */ 27119f1fafeeSAl Viro error = complete_walk(nd); 27129f1fafeeSAl Viro if (error) 27132675a4ebSAl Viro return error; 2714fe2d35ffSAl Viro 2715bfcec708SJeff Layton audit_inode(pathname, dir, 0); 271616c2cd71SAl Viro error = -EISDIR; 27171f36f774SAl Viro /* trailing slashes? */ 271831e6b01fSNick Piggin if (nd->last.name[nd->last.len]) 27192675a4ebSAl Viro goto out; 2720b6183df7SMiklos Szeredi } 27211f36f774SAl Viro 272216b1c1cdSMiklos Szeredi retry_lookup: 272364894cf8SAl Viro if (op->open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) { 272464894cf8SAl Viro error = mnt_want_write(nd->path.mnt); 272564894cf8SAl Viro if (!error) 272664894cf8SAl Viro got_write = true; 272764894cf8SAl Viro /* 272864894cf8SAl Viro * do _not_ fail yet - we might not need that or fail with 272964894cf8SAl Viro * a different error; let lookup_open() decide; we'll be 273064894cf8SAl Viro * dropping this one anyway. 273164894cf8SAl Viro */ 273264894cf8SAl Viro } 2733a1e28038SAl Viro mutex_lock(&dir->d_inode->i_mutex); 273464894cf8SAl Viro error = lookup_open(nd, path, file, op, got_write, opened); 2735fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2736fb1cc555SAl Viro 27372675a4ebSAl Viro if (error <= 0) { 27382675a4ebSAl Viro if (error) 2739d58ffd35SMiklos Szeredi goto out; 27406c0d46c4SAl Viro 274147237687SAl Viro if ((*opened & FILE_CREATED) || 27422675a4ebSAl Viro !S_ISREG(file->f_path.dentry->d_inode->i_mode)) 274377d660a8SMiklos Szeredi will_truncate = false; 2744d18e9008SMiklos Szeredi 2745bfcec708SJeff Layton audit_inode(pathname, file->f_path.dentry, 0); 2746d18e9008SMiklos Szeredi goto opened; 2747d18e9008SMiklos Szeredi } 2748d18e9008SMiklos Szeredi 274947237687SAl Viro if (*opened & FILE_CREATED) { 27509b44f1b3SAl Viro /* Don't check for write permission, don't truncate */ 2751ca344a89SAl Viro open_flag &= ~O_TRUNC; 275277d660a8SMiklos Szeredi will_truncate = false; 2753bcda7652SAl Viro acc_mode = MAY_OPEN; 2754d58ffd35SMiklos Szeredi path_to_nameidata(path, nd); 2755e83db167SMiklos Szeredi goto finish_open_created; 2756fb1cc555SAl Viro } 2757fb1cc555SAl Viro 2758fb1cc555SAl Viro /* 27593134f37eSJeff Layton * create/update audit record if it already exists. 2760fb1cc555SAl Viro */ 27613134f37eSJeff Layton if (path->dentry->d_inode) 2762bfcec708SJeff Layton audit_inode(pathname, path->dentry, 0); 2763fb1cc555SAl Viro 2764d18e9008SMiklos Szeredi /* 2765d18e9008SMiklos Szeredi * If atomic_open() acquired write access it is dropped now due to 2766d18e9008SMiklos Szeredi * possible mount and symlink following (this might be optimized away if 2767d18e9008SMiklos Szeredi * necessary...) 2768d18e9008SMiklos Szeredi */ 276964894cf8SAl Viro if (got_write) { 2770d18e9008SMiklos Szeredi mnt_drop_write(nd->path.mnt); 277164894cf8SAl Viro got_write = false; 2772d18e9008SMiklos Szeredi } 2773d18e9008SMiklos Szeredi 2774fb1cc555SAl Viro error = -EEXIST; 2775f8310c59SAl Viro if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT)) 2776fb1cc555SAl Viro goto exit_dput; 2777fb1cc555SAl Viro 27789875cf80SDavid Howells error = follow_managed(path, nd->flags); 27799875cf80SDavid Howells if (error < 0) 2780fb1cc555SAl Viro goto exit_dput; 2781fb1cc555SAl Viro 2782a3fbbde7SAl Viro if (error) 2783a3fbbde7SAl Viro nd->flags |= LOOKUP_JUMPED; 2784a3fbbde7SAl Viro 2785decf3400SMiklos Szeredi BUG_ON(nd->flags & LOOKUP_RCU); 2786decf3400SMiklos Szeredi inode = path->dentry->d_inode; 27875f5daac1SMiklos Szeredi finish_lookup: 27885f5daac1SMiklos Szeredi /* we _can_ be in RCU mode here */ 2789fb1cc555SAl Viro error = -ENOENT; 279054c33e7fSMiklos Szeredi if (!inode) { 279154c33e7fSMiklos Szeredi path_to_nameidata(path, nd); 27922675a4ebSAl Viro goto out; 279354c33e7fSMiklos Szeredi } 27949e67f361SAl Viro 2795d45ea867SMiklos Szeredi if (should_follow_link(inode, !symlink_ok)) { 2796d45ea867SMiklos Szeredi if (nd->flags & LOOKUP_RCU) { 2797d45ea867SMiklos Szeredi if (unlikely(unlazy_walk(nd, path->dentry))) { 2798d45ea867SMiklos Szeredi error = -ECHILD; 27992675a4ebSAl Viro goto out; 2800d45ea867SMiklos Szeredi } 2801d45ea867SMiklos Szeredi } 2802d45ea867SMiklos Szeredi BUG_ON(inode != path->dentry->d_inode); 28032675a4ebSAl Viro return 1; 2804d45ea867SMiklos Szeredi } 2805fb1cc555SAl Viro 280616b1c1cdSMiklos Szeredi if ((nd->flags & LOOKUP_RCU) || nd->path.mnt != path->mnt) { 2807fb1cc555SAl Viro path_to_nameidata(path, nd); 280816b1c1cdSMiklos Szeredi } else { 280916b1c1cdSMiklos Szeredi save_parent.dentry = nd->path.dentry; 281016b1c1cdSMiklos Szeredi save_parent.mnt = mntget(path->mnt); 281116b1c1cdSMiklos Szeredi nd->path.dentry = path->dentry; 281216b1c1cdSMiklos Szeredi 281316b1c1cdSMiklos Szeredi } 2814decf3400SMiklos Szeredi nd->inode = inode; 2815a3fbbde7SAl Viro /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */ 2816a3fbbde7SAl Viro error = complete_walk(nd); 281716b1c1cdSMiklos Szeredi if (error) { 281816b1c1cdSMiklos Szeredi path_put(&save_parent); 28192675a4ebSAl Viro return error; 282016b1c1cdSMiklos Szeredi } 2821fb1cc555SAl Viro error = -EISDIR; 2822050ac841SMiklos Szeredi if ((open_flag & O_CREAT) && S_ISDIR(nd->inode->i_mode)) 28232675a4ebSAl Viro goto out; 2824af2f5542SMiklos Szeredi error = -ENOTDIR; 2825af2f5542SMiklos Szeredi if ((nd->flags & LOOKUP_DIRECTORY) && !nd->inode->i_op->lookup) 28262675a4ebSAl Viro goto out; 2827bfcec708SJeff Layton audit_inode(pathname, nd->path.dentry, 0); 2828e83db167SMiklos Szeredi finish_open: 28296c0d46c4SAl Viro if (!S_ISREG(nd->inode->i_mode)) 283077d660a8SMiklos Szeredi will_truncate = false; 28316c0d46c4SAl Viro 28320f9d1a10SAl Viro if (will_truncate) { 28330f9d1a10SAl Viro error = mnt_want_write(nd->path.mnt); 28340f9d1a10SAl Viro if (error) 28352675a4ebSAl Viro goto out; 283664894cf8SAl Viro got_write = true; 28370f9d1a10SAl Viro } 2838e83db167SMiklos Szeredi finish_open_created: 2839bcda7652SAl Viro error = may_open(&nd->path, acc_mode, open_flag); 2840ca344a89SAl Viro if (error) 28412675a4ebSAl Viro goto out; 284230d90494SAl Viro file->f_path.mnt = nd->path.mnt; 284330d90494SAl Viro error = finish_open(file, nd->path.dentry, NULL, opened); 284430d90494SAl Viro if (error) { 284530d90494SAl Viro if (error == -EOPENSTALE) 2846f60dc3dbSMiklos Szeredi goto stale_open; 2847015c3bbcSMiklos Szeredi goto out; 2848f60dc3dbSMiklos Szeredi } 2849a8277b9bSMiklos Szeredi opened: 28502675a4ebSAl Viro error = open_check_o_direct(file); 2851015c3bbcSMiklos Szeredi if (error) 2852015c3bbcSMiklos Szeredi goto exit_fput; 28532675a4ebSAl Viro error = ima_file_check(file, op->acc_mode); 2854aa4caadbSMiklos Szeredi if (error) 2855aa4caadbSMiklos Szeredi goto exit_fput; 2856aa4caadbSMiklos Szeredi 28570f9d1a10SAl Viro if (will_truncate) { 28582675a4ebSAl Viro error = handle_truncate(file); 2859aa4caadbSMiklos Szeredi if (error) 2860aa4caadbSMiklos Szeredi goto exit_fput; 28610f9d1a10SAl Viro } 2862ca344a89SAl Viro out: 286364894cf8SAl Viro if (got_write) 28640f9d1a10SAl Viro mnt_drop_write(nd->path.mnt); 286516b1c1cdSMiklos Szeredi path_put(&save_parent); 2866e276ae67SMiklos Szeredi terminate_walk(nd); 28672675a4ebSAl Viro return error; 2868fb1cc555SAl Viro 2869fb1cc555SAl Viro exit_dput: 2870fb1cc555SAl Viro path_put_conditional(path, nd); 2871ca344a89SAl Viro goto out; 2872015c3bbcSMiklos Szeredi exit_fput: 28732675a4ebSAl Viro fput(file); 28742675a4ebSAl Viro goto out; 2875015c3bbcSMiklos Szeredi 2876f60dc3dbSMiklos Szeredi stale_open: 2877f60dc3dbSMiklos Szeredi /* If no saved parent or already retried then can't retry */ 2878f60dc3dbSMiklos Szeredi if (!save_parent.dentry || retried) 2879f60dc3dbSMiklos Szeredi goto out; 2880f60dc3dbSMiklos Szeredi 2881f60dc3dbSMiklos Szeredi BUG_ON(save_parent.dentry != dir); 2882f60dc3dbSMiklos Szeredi path_put(&nd->path); 2883f60dc3dbSMiklos Szeredi nd->path = save_parent; 2884f60dc3dbSMiklos Szeredi nd->inode = dir->d_inode; 2885f60dc3dbSMiklos Szeredi save_parent.mnt = NULL; 2886f60dc3dbSMiklos Szeredi save_parent.dentry = NULL; 288764894cf8SAl Viro if (got_write) { 2888f60dc3dbSMiklos Szeredi mnt_drop_write(nd->path.mnt); 288964894cf8SAl Viro got_write = false; 2890f60dc3dbSMiklos Szeredi } 2891f60dc3dbSMiklos Szeredi retried = true; 2892f60dc3dbSMiklos Szeredi goto retry_lookup; 2893fb1cc555SAl Viro } 2894fb1cc555SAl Viro 289513aab428SAl Viro static struct file *path_openat(int dfd, const char *pathname, 289673d049a4SAl Viro struct nameidata *nd, const struct open_flags *op, int flags) 28971da177e4SLinus Torvalds { 2898fe2d35ffSAl Viro struct file *base = NULL; 289930d90494SAl Viro struct file *file; 29009850c056SAl Viro struct path path; 290147237687SAl Viro int opened = 0; 290213aab428SAl Viro int error; 290331e6b01fSNick Piggin 290430d90494SAl Viro file = get_empty_filp(); 290530d90494SAl Viro if (!file) 290631e6b01fSNick Piggin return ERR_PTR(-ENFILE); 290731e6b01fSNick Piggin 290830d90494SAl Viro file->f_flags = op->open_flag; 290931e6b01fSNick Piggin 291073d049a4SAl Viro error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base); 291131e6b01fSNick Piggin if (unlikely(error)) 29122675a4ebSAl Viro goto out; 291331e6b01fSNick Piggin 2914fe2d35ffSAl Viro current->total_link_count = 0; 291573d049a4SAl Viro error = link_path_walk(pathname, nd); 291631e6b01fSNick Piggin if (unlikely(error)) 29172675a4ebSAl Viro goto out; 29181da177e4SLinus Torvalds 29192675a4ebSAl Viro error = do_last(nd, &path, file, op, &opened, pathname); 29202675a4ebSAl Viro while (unlikely(error > 0)) { /* trailing symlink */ 29217b9337aaSNick Piggin struct path link = path; 2922def4af30SAl Viro void *cookie; 2923574197e0SAl Viro if (!(nd->flags & LOOKUP_FOLLOW)) { 292473d049a4SAl Viro path_put_conditional(&path, nd); 292573d049a4SAl Viro path_put(&nd->path); 29262675a4ebSAl Viro error = -ELOOP; 292740b39136SAl Viro break; 292840b39136SAl Viro } 2929800179c9SKees Cook error = may_follow_link(&link, nd); 2930800179c9SKees Cook if (unlikely(error)) 2931800179c9SKees Cook break; 293273d049a4SAl Viro nd->flags |= LOOKUP_PARENT; 293373d049a4SAl Viro nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL); 2934574197e0SAl Viro error = follow_link(&link, nd, &cookie); 2935c3e380b0SAl Viro if (unlikely(error)) 29362675a4ebSAl Viro break; 29372675a4ebSAl Viro error = do_last(nd, &path, file, op, &opened, pathname); 2938574197e0SAl Viro put_link(nd, &link, cookie); 2939806b681cSAl Viro } 294010fa8e62SAl Viro out: 294173d049a4SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) 294273d049a4SAl Viro path_put(&nd->root); 2943fe2d35ffSAl Viro if (base) 2944fe2d35ffSAl Viro fput(base); 29452675a4ebSAl Viro if (!(opened & FILE_OPENED)) { 29462675a4ebSAl Viro BUG_ON(!error); 294730d90494SAl Viro put_filp(file); 2948015c3bbcSMiklos Szeredi } 29492675a4ebSAl Viro if (unlikely(error)) { 29502675a4ebSAl Viro if (error == -EOPENSTALE) { 29512675a4ebSAl Viro if (flags & LOOKUP_RCU) 29522675a4ebSAl Viro error = -ECHILD; 29532675a4ebSAl Viro else 29542675a4ebSAl Viro error = -ESTALE; 29552675a4ebSAl Viro } 29562675a4ebSAl Viro file = ERR_PTR(error); 29572675a4ebSAl Viro } 29582675a4ebSAl Viro return file; 2959de459215SKirill Korotaev } 29601da177e4SLinus Torvalds 296113aab428SAl Viro struct file *do_filp_open(int dfd, const char *pathname, 296213aab428SAl Viro const struct open_flags *op, int flags) 296313aab428SAl Viro { 296473d049a4SAl Viro struct nameidata nd; 296513aab428SAl Viro struct file *filp; 296613aab428SAl Viro 296773d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU); 296813aab428SAl Viro if (unlikely(filp == ERR_PTR(-ECHILD))) 296973d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags); 297013aab428SAl Viro if (unlikely(filp == ERR_PTR(-ESTALE))) 297173d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL); 297213aab428SAl Viro return filp; 297313aab428SAl Viro } 297413aab428SAl Viro 297573d049a4SAl Viro struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt, 297673d049a4SAl Viro const char *name, const struct open_flags *op, int flags) 297773d049a4SAl Viro { 297873d049a4SAl Viro struct nameidata nd; 297973d049a4SAl Viro struct file *file; 298073d049a4SAl Viro 298173d049a4SAl Viro nd.root.mnt = mnt; 298273d049a4SAl Viro nd.root.dentry = dentry; 298373d049a4SAl Viro 298473d049a4SAl Viro flags |= LOOKUP_ROOT; 298573d049a4SAl Viro 2986bcda7652SAl Viro if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN) 298773d049a4SAl Viro return ERR_PTR(-ELOOP); 298873d049a4SAl Viro 298973d049a4SAl Viro file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU); 299073d049a4SAl Viro if (unlikely(file == ERR_PTR(-ECHILD))) 299173d049a4SAl Viro file = path_openat(-1, name, &nd, op, flags); 299273d049a4SAl Viro if (unlikely(file == ERR_PTR(-ESTALE))) 299373d049a4SAl Viro file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL); 299473d049a4SAl Viro return file; 299573d049a4SAl Viro } 299673d049a4SAl Viro 2997ed75e95dSAl Viro struct dentry *kern_path_create(int dfd, const char *pathname, struct path *path, int is_dir) 29981da177e4SLinus Torvalds { 2999c663e5d8SChristoph Hellwig struct dentry *dentry = ERR_PTR(-EEXIST); 3000ed75e95dSAl Viro struct nameidata nd; 3001c30dabfeSJan Kara int err2; 3002ed75e95dSAl Viro int error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd); 3003ed75e95dSAl Viro if (error) 3004ed75e95dSAl Viro return ERR_PTR(error); 30051da177e4SLinus Torvalds 3006c663e5d8SChristoph Hellwig /* 3007c663e5d8SChristoph Hellwig * Yucky last component or no last component at all? 3008c663e5d8SChristoph Hellwig * (foo/., foo/.., /////) 3009c663e5d8SChristoph Hellwig */ 3010ed75e95dSAl Viro if (nd.last_type != LAST_NORM) 3011ed75e95dSAl Viro goto out; 3012ed75e95dSAl Viro nd.flags &= ~LOOKUP_PARENT; 3013ed75e95dSAl Viro nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL; 3014c663e5d8SChristoph Hellwig 3015c30dabfeSJan Kara /* don't fail immediately if it's r/o, at least try to report other errors */ 3016c30dabfeSJan Kara err2 = mnt_want_write(nd.path.mnt); 3017c663e5d8SChristoph Hellwig /* 3018c663e5d8SChristoph Hellwig * Do the final lookup. 3019c663e5d8SChristoph Hellwig */ 3020ed75e95dSAl Viro mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 3021ed75e95dSAl Viro dentry = lookup_hash(&nd); 30221da177e4SLinus Torvalds if (IS_ERR(dentry)) 3023a8104a9fSAl Viro goto unlock; 3024c663e5d8SChristoph Hellwig 3025a8104a9fSAl Viro error = -EEXIST; 3026e9baf6e5SAl Viro if (dentry->d_inode) 3027a8104a9fSAl Viro goto fail; 3028c663e5d8SChristoph Hellwig /* 3029c663e5d8SChristoph Hellwig * Special case - lookup gave negative, but... we had foo/bar/ 3030c663e5d8SChristoph Hellwig * From the vfs_mknod() POV we just have a negative dentry - 3031c663e5d8SChristoph Hellwig * all is fine. Let's be bastards - you had / on the end, you've 3032c663e5d8SChristoph Hellwig * been asking for (non-existent) directory. -ENOENT for you. 3033c663e5d8SChristoph Hellwig */ 3034ed75e95dSAl Viro if (unlikely(!is_dir && nd.last.name[nd.last.len])) { 3035a8104a9fSAl Viro error = -ENOENT; 3036ed75e95dSAl Viro goto fail; 3037e9baf6e5SAl Viro } 3038c30dabfeSJan Kara if (unlikely(err2)) { 3039c30dabfeSJan Kara error = err2; 3040a8104a9fSAl Viro goto fail; 3041c30dabfeSJan Kara } 3042ed75e95dSAl Viro *path = nd.path; 3043e9baf6e5SAl Viro return dentry; 30441da177e4SLinus Torvalds fail: 3045a8104a9fSAl Viro dput(dentry); 3046a8104a9fSAl Viro dentry = ERR_PTR(error); 3047a8104a9fSAl Viro unlock: 3048dae6ad8fSAl Viro mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 3049c30dabfeSJan Kara if (!err2) 3050c30dabfeSJan Kara mnt_drop_write(nd.path.mnt); 3051ed75e95dSAl Viro out: 3052dae6ad8fSAl Viro path_put(&nd.path); 3053ed75e95dSAl Viro return dentry; 3054dae6ad8fSAl Viro } 3055dae6ad8fSAl Viro EXPORT_SYMBOL(kern_path_create); 3056dae6ad8fSAl Viro 3057921a1650SAl Viro void done_path_create(struct path *path, struct dentry *dentry) 3058921a1650SAl Viro { 3059921a1650SAl Viro dput(dentry); 3060921a1650SAl Viro mutex_unlock(&path->dentry->d_inode->i_mutex); 3061a8104a9fSAl Viro mnt_drop_write(path->mnt); 3062921a1650SAl Viro path_put(path); 3063921a1650SAl Viro } 3064921a1650SAl Viro EXPORT_SYMBOL(done_path_create); 3065921a1650SAl Viro 3066dae6ad8fSAl Viro struct dentry *user_path_create(int dfd, const char __user *pathname, struct path *path, int is_dir) 3067dae6ad8fSAl Viro { 306891a27b2aSJeff Layton struct filename *tmp = getname(pathname); 3069dae6ad8fSAl Viro struct dentry *res; 3070dae6ad8fSAl Viro if (IS_ERR(tmp)) 3071dae6ad8fSAl Viro return ERR_CAST(tmp); 307291a27b2aSJeff Layton res = kern_path_create(dfd, tmp->name, path, is_dir); 3073dae6ad8fSAl Viro putname(tmp); 3074dae6ad8fSAl Viro return res; 3075dae6ad8fSAl Viro } 3076dae6ad8fSAl Viro EXPORT_SYMBOL(user_path_create); 3077dae6ad8fSAl Viro 30781a67aafbSAl Viro int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev) 30791da177e4SLinus Torvalds { 3080a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 30811da177e4SLinus Torvalds 30821da177e4SLinus Torvalds if (error) 30831da177e4SLinus Torvalds return error; 30841da177e4SLinus Torvalds 3085975d6b39SEric W. Biederman if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD)) 30861da177e4SLinus Torvalds return -EPERM; 30871da177e4SLinus Torvalds 3088acfa4380SAl Viro if (!dir->i_op->mknod) 30891da177e4SLinus Torvalds return -EPERM; 30901da177e4SLinus Torvalds 309108ce5f16SSerge E. Hallyn error = devcgroup_inode_mknod(mode, dev); 309208ce5f16SSerge E. Hallyn if (error) 309308ce5f16SSerge E. Hallyn return error; 309408ce5f16SSerge E. Hallyn 30951da177e4SLinus Torvalds error = security_inode_mknod(dir, dentry, mode, dev); 30961da177e4SLinus Torvalds if (error) 30971da177e4SLinus Torvalds return error; 30981da177e4SLinus Torvalds 30991da177e4SLinus Torvalds error = dir->i_op->mknod(dir, dentry, mode, dev); 3100a74574aaSStephen Smalley if (!error) 3101f38aa942SAmy Griffis fsnotify_create(dir, dentry); 31021da177e4SLinus Torvalds return error; 31031da177e4SLinus Torvalds } 31041da177e4SLinus Torvalds 3105f69aac00SAl Viro static int may_mknod(umode_t mode) 3106463c3197SDave Hansen { 3107463c3197SDave Hansen switch (mode & S_IFMT) { 3108463c3197SDave Hansen case S_IFREG: 3109463c3197SDave Hansen case S_IFCHR: 3110463c3197SDave Hansen case S_IFBLK: 3111463c3197SDave Hansen case S_IFIFO: 3112463c3197SDave Hansen case S_IFSOCK: 3113463c3197SDave Hansen case 0: /* zero mode translates to S_IFREG */ 3114463c3197SDave Hansen return 0; 3115463c3197SDave Hansen case S_IFDIR: 3116463c3197SDave Hansen return -EPERM; 3117463c3197SDave Hansen default: 3118463c3197SDave Hansen return -EINVAL; 3119463c3197SDave Hansen } 3120463c3197SDave Hansen } 3121463c3197SDave Hansen 31228208a22bSAl Viro SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode, 31232e4d0924SHeiko Carstens unsigned, dev) 31241da177e4SLinus Torvalds { 31251da177e4SLinus Torvalds struct dentry *dentry; 3126dae6ad8fSAl Viro struct path path; 3127dae6ad8fSAl Viro int error; 31281da177e4SLinus Torvalds 31298e4bfca1SAl Viro error = may_mknod(mode); 31308e4bfca1SAl Viro if (error) 31318e4bfca1SAl Viro return error; 31321da177e4SLinus Torvalds 3133dae6ad8fSAl Viro dentry = user_path_create(dfd, filename, &path, 0); 3134dae6ad8fSAl Viro if (IS_ERR(dentry)) 3135dae6ad8fSAl Viro return PTR_ERR(dentry); 31362ad94ae6SAl Viro 3137dae6ad8fSAl Viro if (!IS_POSIXACL(path.dentry->d_inode)) 3138ce3b0f8dSAl Viro mode &= ~current_umask(); 3139dae6ad8fSAl Viro error = security_path_mknod(&path, dentry, mode, dev); 3140be6d3e56SKentaro Takeda if (error) 3141a8104a9fSAl Viro goto out; 31421da177e4SLinus Torvalds switch (mode & S_IFMT) { 31431da177e4SLinus Torvalds case 0: case S_IFREG: 3144312b63fbSAl Viro error = vfs_create(path.dentry->d_inode,dentry,mode,true); 31451da177e4SLinus Torvalds break; 31461da177e4SLinus Torvalds case S_IFCHR: case S_IFBLK: 3147dae6ad8fSAl Viro error = vfs_mknod(path.dentry->d_inode,dentry,mode, 31481da177e4SLinus Torvalds new_decode_dev(dev)); 31491da177e4SLinus Torvalds break; 31501da177e4SLinus Torvalds case S_IFIFO: case S_IFSOCK: 3151dae6ad8fSAl Viro error = vfs_mknod(path.dentry->d_inode,dentry,mode,0); 31521da177e4SLinus Torvalds break; 31531da177e4SLinus Torvalds } 3154a8104a9fSAl Viro out: 3155921a1650SAl Viro done_path_create(&path, dentry); 31561da177e4SLinus Torvalds return error; 31571da177e4SLinus Torvalds } 31581da177e4SLinus Torvalds 31598208a22bSAl Viro SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev) 31605590ff0dSUlrich Drepper { 31615590ff0dSUlrich Drepper return sys_mknodat(AT_FDCWD, filename, mode, dev); 31625590ff0dSUlrich Drepper } 31635590ff0dSUlrich Drepper 316418bb1db3SAl Viro int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) 31651da177e4SLinus Torvalds { 3166a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 31678de52778SAl Viro unsigned max_links = dir->i_sb->s_max_links; 31681da177e4SLinus Torvalds 31691da177e4SLinus Torvalds if (error) 31701da177e4SLinus Torvalds return error; 31711da177e4SLinus Torvalds 3172acfa4380SAl Viro if (!dir->i_op->mkdir) 31731da177e4SLinus Torvalds return -EPERM; 31741da177e4SLinus Torvalds 31751da177e4SLinus Torvalds mode &= (S_IRWXUGO|S_ISVTX); 31761da177e4SLinus Torvalds error = security_inode_mkdir(dir, dentry, mode); 31771da177e4SLinus Torvalds if (error) 31781da177e4SLinus Torvalds return error; 31791da177e4SLinus Torvalds 31808de52778SAl Viro if (max_links && dir->i_nlink >= max_links) 31818de52778SAl Viro return -EMLINK; 31828de52778SAl Viro 31831da177e4SLinus Torvalds error = dir->i_op->mkdir(dir, dentry, mode); 3184a74574aaSStephen Smalley if (!error) 3185f38aa942SAmy Griffis fsnotify_mkdir(dir, dentry); 31861da177e4SLinus Torvalds return error; 31871da177e4SLinus Torvalds } 31881da177e4SLinus Torvalds 3189a218d0fdSAl Viro SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode) 31901da177e4SLinus Torvalds { 31916902d925SDave Hansen struct dentry *dentry; 3192dae6ad8fSAl Viro struct path path; 3193dae6ad8fSAl Viro int error; 31941da177e4SLinus Torvalds 3195dae6ad8fSAl Viro dentry = user_path_create(dfd, pathname, &path, 1); 31966902d925SDave Hansen if (IS_ERR(dentry)) 3197dae6ad8fSAl Viro return PTR_ERR(dentry); 31986902d925SDave Hansen 3199dae6ad8fSAl Viro if (!IS_POSIXACL(path.dentry->d_inode)) 3200ce3b0f8dSAl Viro mode &= ~current_umask(); 3201dae6ad8fSAl Viro error = security_path_mkdir(&path, dentry, mode); 3202a8104a9fSAl Viro if (!error) 3203dae6ad8fSAl Viro error = vfs_mkdir(path.dentry->d_inode, dentry, mode); 3204921a1650SAl Viro done_path_create(&path, dentry); 32051da177e4SLinus Torvalds return error; 32061da177e4SLinus Torvalds } 32071da177e4SLinus Torvalds 3208a218d0fdSAl Viro SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode) 32095590ff0dSUlrich Drepper { 32105590ff0dSUlrich Drepper return sys_mkdirat(AT_FDCWD, pathname, mode); 32115590ff0dSUlrich Drepper } 32125590ff0dSUlrich Drepper 32131da177e4SLinus Torvalds /* 3214a71905f0SSage Weil * The dentry_unhash() helper will try to drop the dentry early: we 3215c0d02594SJ. Bruce Fields * should have a usage count of 1 if we're the only user of this 3216a71905f0SSage Weil * dentry, and if that is true (possibly after pruning the dcache), 3217a71905f0SSage Weil * then we drop the dentry now. 32181da177e4SLinus Torvalds * 32191da177e4SLinus Torvalds * A low-level filesystem can, if it choses, legally 32201da177e4SLinus Torvalds * do a 32211da177e4SLinus Torvalds * 32221da177e4SLinus Torvalds * if (!d_unhashed(dentry)) 32231da177e4SLinus Torvalds * return -EBUSY; 32241da177e4SLinus Torvalds * 32251da177e4SLinus Torvalds * if it cannot handle the case of removing a directory 32261da177e4SLinus Torvalds * that is still in use by something else.. 32271da177e4SLinus Torvalds */ 32281da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry) 32291da177e4SLinus Torvalds { 32301da177e4SLinus Torvalds shrink_dcache_parent(dentry); 32311da177e4SLinus Torvalds spin_lock(&dentry->d_lock); 323264252c75SSage Weil if (dentry->d_count == 1) 32331da177e4SLinus Torvalds __d_drop(dentry); 32341da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 32351da177e4SLinus Torvalds } 32361da177e4SLinus Torvalds 32371da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry) 32381da177e4SLinus Torvalds { 32391da177e4SLinus Torvalds int error = may_delete(dir, dentry, 1); 32401da177e4SLinus Torvalds 32411da177e4SLinus Torvalds if (error) 32421da177e4SLinus Torvalds return error; 32431da177e4SLinus Torvalds 3244acfa4380SAl Viro if (!dir->i_op->rmdir) 32451da177e4SLinus Torvalds return -EPERM; 32461da177e4SLinus Torvalds 32471d2ef590SAl Viro dget(dentry); 32481b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 3249912dbc15SSage Weil 32501da177e4SLinus Torvalds error = -EBUSY; 3251912dbc15SSage Weil if (d_mountpoint(dentry)) 3252912dbc15SSage Weil goto out; 3253912dbc15SSage Weil 32541da177e4SLinus Torvalds error = security_inode_rmdir(dir, dentry); 3255912dbc15SSage Weil if (error) 3256912dbc15SSage Weil goto out; 3257912dbc15SSage Weil 32583cebde24SSage Weil shrink_dcache_parent(dentry); 32591da177e4SLinus Torvalds error = dir->i_op->rmdir(dir, dentry); 3260912dbc15SSage Weil if (error) 3261912dbc15SSage Weil goto out; 3262912dbc15SSage Weil 32631da177e4SLinus Torvalds dentry->d_inode->i_flags |= S_DEAD; 3264d83c49f3SAl Viro dont_mount(dentry); 32651da177e4SLinus Torvalds 3266912dbc15SSage Weil out: 3267912dbc15SSage Weil mutex_unlock(&dentry->d_inode->i_mutex); 32681d2ef590SAl Viro dput(dentry); 3269912dbc15SSage Weil if (!error) 3270912dbc15SSage Weil d_delete(dentry); 32711da177e4SLinus Torvalds return error; 32721da177e4SLinus Torvalds } 32731da177e4SLinus Torvalds 32745590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname) 32751da177e4SLinus Torvalds { 32761da177e4SLinus Torvalds int error = 0; 327791a27b2aSJeff Layton struct filename *name; 32781da177e4SLinus Torvalds struct dentry *dentry; 32791da177e4SLinus Torvalds struct nameidata nd; 32801da177e4SLinus Torvalds 328191a27b2aSJeff Layton name = user_path_parent(dfd, pathname, &nd); 328291a27b2aSJeff Layton if (IS_ERR(name)) 328391a27b2aSJeff Layton return PTR_ERR(name); 32841da177e4SLinus Torvalds 32851da177e4SLinus Torvalds switch(nd.last_type) { 32861da177e4SLinus Torvalds case LAST_DOTDOT: 32871da177e4SLinus Torvalds error = -ENOTEMPTY; 32881da177e4SLinus Torvalds goto exit1; 32891da177e4SLinus Torvalds case LAST_DOT: 32901da177e4SLinus Torvalds error = -EINVAL; 32911da177e4SLinus Torvalds goto exit1; 32921da177e4SLinus Torvalds case LAST_ROOT: 32931da177e4SLinus Torvalds error = -EBUSY; 32941da177e4SLinus Torvalds goto exit1; 32951da177e4SLinus Torvalds } 32960612d9fbSOGAWA Hirofumi 32970612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 3298c30dabfeSJan Kara error = mnt_want_write(nd.path.mnt); 3299c30dabfeSJan Kara if (error) 3300c30dabfeSJan Kara goto exit1; 33010612d9fbSOGAWA Hirofumi 33024ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 330349705b77SChristoph Hellwig dentry = lookup_hash(&nd); 33041da177e4SLinus Torvalds error = PTR_ERR(dentry); 33056902d925SDave Hansen if (IS_ERR(dentry)) 33066902d925SDave Hansen goto exit2; 3307e6bc45d6STheodore Ts'o if (!dentry->d_inode) { 3308e6bc45d6STheodore Ts'o error = -ENOENT; 3309e6bc45d6STheodore Ts'o goto exit3; 3310e6bc45d6STheodore Ts'o } 3311be6d3e56SKentaro Takeda error = security_path_rmdir(&nd.path, dentry); 3312be6d3e56SKentaro Takeda if (error) 3313c30dabfeSJan Kara goto exit3; 33144ac91378SJan Blunck error = vfs_rmdir(nd.path.dentry->d_inode, dentry); 33150622753bSDave Hansen exit3: 33161da177e4SLinus Torvalds dput(dentry); 33176902d925SDave Hansen exit2: 33184ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 3319c30dabfeSJan Kara mnt_drop_write(nd.path.mnt); 33201da177e4SLinus Torvalds exit1: 33211d957f9bSJan Blunck path_put(&nd.path); 33221da177e4SLinus Torvalds putname(name); 33231da177e4SLinus Torvalds return error; 33241da177e4SLinus Torvalds } 33251da177e4SLinus Torvalds 33263cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname) 33275590ff0dSUlrich Drepper { 33285590ff0dSUlrich Drepper return do_rmdir(AT_FDCWD, pathname); 33295590ff0dSUlrich Drepper } 33305590ff0dSUlrich Drepper 33311da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry) 33321da177e4SLinus Torvalds { 33331da177e4SLinus Torvalds int error = may_delete(dir, dentry, 0); 33341da177e4SLinus Torvalds 33351da177e4SLinus Torvalds if (error) 33361da177e4SLinus Torvalds return error; 33371da177e4SLinus Torvalds 3338acfa4380SAl Viro if (!dir->i_op->unlink) 33391da177e4SLinus Torvalds return -EPERM; 33401da177e4SLinus Torvalds 33411b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 33421da177e4SLinus Torvalds if (d_mountpoint(dentry)) 33431da177e4SLinus Torvalds error = -EBUSY; 33441da177e4SLinus Torvalds else { 33451da177e4SLinus Torvalds error = security_inode_unlink(dir, dentry); 3346bec1052eSAl Viro if (!error) { 33471da177e4SLinus Torvalds error = dir->i_op->unlink(dir, dentry); 3348bec1052eSAl Viro if (!error) 3349d83c49f3SAl Viro dont_mount(dentry); 3350bec1052eSAl Viro } 33511da177e4SLinus Torvalds } 33521b1dcc1bSJes Sorensen mutex_unlock(&dentry->d_inode->i_mutex); 33531da177e4SLinus Torvalds 33541da177e4SLinus Torvalds /* We don't d_delete() NFS sillyrenamed files--they still exist. */ 33551da177e4SLinus Torvalds if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) { 3356ece95912SJan Kara fsnotify_link_count(dentry->d_inode); 33571da177e4SLinus Torvalds d_delete(dentry); 33581da177e4SLinus Torvalds } 33590eeca283SRobert Love 33601da177e4SLinus Torvalds return error; 33611da177e4SLinus Torvalds } 33621da177e4SLinus Torvalds 33631da177e4SLinus Torvalds /* 33641da177e4SLinus Torvalds * Make sure that the actual truncation of the file will occur outside its 33651b1dcc1bSJes Sorensen * directory's i_mutex. Truncate can take a long time if there is a lot of 33661da177e4SLinus Torvalds * writeout happening, and we don't want to prevent access to the directory 33671da177e4SLinus Torvalds * while waiting on the I/O. 33681da177e4SLinus Torvalds */ 33695590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname) 33701da177e4SLinus Torvalds { 33712ad94ae6SAl Viro int error; 337291a27b2aSJeff Layton struct filename *name; 33731da177e4SLinus Torvalds struct dentry *dentry; 33741da177e4SLinus Torvalds struct nameidata nd; 33751da177e4SLinus Torvalds struct inode *inode = NULL; 33761da177e4SLinus Torvalds 337791a27b2aSJeff Layton name = user_path_parent(dfd, pathname, &nd); 337891a27b2aSJeff Layton if (IS_ERR(name)) 337991a27b2aSJeff Layton return PTR_ERR(name); 33802ad94ae6SAl Viro 33811da177e4SLinus Torvalds error = -EISDIR; 33821da177e4SLinus Torvalds if (nd.last_type != LAST_NORM) 33831da177e4SLinus Torvalds goto exit1; 33840612d9fbSOGAWA Hirofumi 33850612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 3386c30dabfeSJan Kara error = mnt_want_write(nd.path.mnt); 3387c30dabfeSJan Kara if (error) 3388c30dabfeSJan Kara goto exit1; 33890612d9fbSOGAWA Hirofumi 33904ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 339149705b77SChristoph Hellwig dentry = lookup_hash(&nd); 33921da177e4SLinus Torvalds error = PTR_ERR(dentry); 33931da177e4SLinus Torvalds if (!IS_ERR(dentry)) { 33941da177e4SLinus Torvalds /* Why not before? Because we want correct error value */ 339550338b88STörök Edwin if (nd.last.name[nd.last.len]) 339650338b88STörök Edwin goto slashes; 33971da177e4SLinus Torvalds inode = dentry->d_inode; 339850338b88STörök Edwin if (!inode) 3399e6bc45d6STheodore Ts'o goto slashes; 34007de9c6eeSAl Viro ihold(inode); 3401be6d3e56SKentaro Takeda error = security_path_unlink(&nd.path, dentry); 3402be6d3e56SKentaro Takeda if (error) 3403c30dabfeSJan Kara goto exit2; 34044ac91378SJan Blunck error = vfs_unlink(nd.path.dentry->d_inode, dentry); 34051da177e4SLinus Torvalds exit2: 34061da177e4SLinus Torvalds dput(dentry); 34071da177e4SLinus Torvalds } 34084ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 34091da177e4SLinus Torvalds if (inode) 34101da177e4SLinus Torvalds iput(inode); /* truncate the inode here */ 3411c30dabfeSJan Kara mnt_drop_write(nd.path.mnt); 34121da177e4SLinus Torvalds exit1: 34131d957f9bSJan Blunck path_put(&nd.path); 34141da177e4SLinus Torvalds putname(name); 34151da177e4SLinus Torvalds return error; 34161da177e4SLinus Torvalds 34171da177e4SLinus Torvalds slashes: 34181da177e4SLinus Torvalds error = !dentry->d_inode ? -ENOENT : 34191da177e4SLinus Torvalds S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR; 34201da177e4SLinus Torvalds goto exit2; 34211da177e4SLinus Torvalds } 34221da177e4SLinus Torvalds 34232e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag) 34245590ff0dSUlrich Drepper { 34255590ff0dSUlrich Drepper if ((flag & ~AT_REMOVEDIR) != 0) 34265590ff0dSUlrich Drepper return -EINVAL; 34275590ff0dSUlrich Drepper 34285590ff0dSUlrich Drepper if (flag & AT_REMOVEDIR) 34295590ff0dSUlrich Drepper return do_rmdir(dfd, pathname); 34305590ff0dSUlrich Drepper 34315590ff0dSUlrich Drepper return do_unlinkat(dfd, pathname); 34325590ff0dSUlrich Drepper } 34335590ff0dSUlrich Drepper 34343480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname) 34355590ff0dSUlrich Drepper { 34365590ff0dSUlrich Drepper return do_unlinkat(AT_FDCWD, pathname); 34375590ff0dSUlrich Drepper } 34385590ff0dSUlrich Drepper 3439db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname) 34401da177e4SLinus Torvalds { 3441a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 34421da177e4SLinus Torvalds 34431da177e4SLinus Torvalds if (error) 34441da177e4SLinus Torvalds return error; 34451da177e4SLinus Torvalds 3446acfa4380SAl Viro if (!dir->i_op->symlink) 34471da177e4SLinus Torvalds return -EPERM; 34481da177e4SLinus Torvalds 34491da177e4SLinus Torvalds error = security_inode_symlink(dir, dentry, oldname); 34501da177e4SLinus Torvalds if (error) 34511da177e4SLinus Torvalds return error; 34521da177e4SLinus Torvalds 34531da177e4SLinus Torvalds error = dir->i_op->symlink(dir, dentry, oldname); 3454a74574aaSStephen Smalley if (!error) 3455f38aa942SAmy Griffis fsnotify_create(dir, dentry); 34561da177e4SLinus Torvalds return error; 34571da177e4SLinus Torvalds } 34581da177e4SLinus Torvalds 34592e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname, 34602e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 34611da177e4SLinus Torvalds { 34622ad94ae6SAl Viro int error; 346391a27b2aSJeff Layton struct filename *from; 34646902d925SDave Hansen struct dentry *dentry; 3465dae6ad8fSAl Viro struct path path; 34661da177e4SLinus Torvalds 34671da177e4SLinus Torvalds from = getname(oldname); 34681da177e4SLinus Torvalds if (IS_ERR(from)) 34691da177e4SLinus Torvalds return PTR_ERR(from); 34702ad94ae6SAl Viro 3471dae6ad8fSAl Viro dentry = user_path_create(newdfd, newname, &path, 0); 34721da177e4SLinus Torvalds error = PTR_ERR(dentry); 34736902d925SDave Hansen if (IS_ERR(dentry)) 3474dae6ad8fSAl Viro goto out_putname; 34756902d925SDave Hansen 347691a27b2aSJeff Layton error = security_path_symlink(&path, dentry, from->name); 3477a8104a9fSAl Viro if (!error) 347891a27b2aSJeff Layton error = vfs_symlink(path.dentry->d_inode, dentry, from->name); 3479921a1650SAl Viro done_path_create(&path, dentry); 34806902d925SDave Hansen out_putname: 34811da177e4SLinus Torvalds putname(from); 34821da177e4SLinus Torvalds return error; 34831da177e4SLinus Torvalds } 34841da177e4SLinus Torvalds 34853480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname) 34865590ff0dSUlrich Drepper { 34875590ff0dSUlrich Drepper return sys_symlinkat(oldname, AT_FDCWD, newname); 34885590ff0dSUlrich Drepper } 34895590ff0dSUlrich Drepper 34901da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) 34911da177e4SLinus Torvalds { 34921da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 34938de52778SAl Viro unsigned max_links = dir->i_sb->s_max_links; 34941da177e4SLinus Torvalds int error; 34951da177e4SLinus Torvalds 34961da177e4SLinus Torvalds if (!inode) 34971da177e4SLinus Torvalds return -ENOENT; 34981da177e4SLinus Torvalds 3499a95164d9SMiklos Szeredi error = may_create(dir, new_dentry); 35001da177e4SLinus Torvalds if (error) 35011da177e4SLinus Torvalds return error; 35021da177e4SLinus Torvalds 35031da177e4SLinus Torvalds if (dir->i_sb != inode->i_sb) 35041da177e4SLinus Torvalds return -EXDEV; 35051da177e4SLinus Torvalds 35061da177e4SLinus Torvalds /* 35071da177e4SLinus Torvalds * A link to an append-only or immutable file cannot be created. 35081da177e4SLinus Torvalds */ 35091da177e4SLinus Torvalds if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) 35101da177e4SLinus Torvalds return -EPERM; 3511acfa4380SAl Viro if (!dir->i_op->link) 35121da177e4SLinus Torvalds return -EPERM; 35137e79eedbSTetsuo Handa if (S_ISDIR(inode->i_mode)) 35141da177e4SLinus Torvalds return -EPERM; 35151da177e4SLinus Torvalds 35161da177e4SLinus Torvalds error = security_inode_link(old_dentry, dir, new_dentry); 35171da177e4SLinus Torvalds if (error) 35181da177e4SLinus Torvalds return error; 35191da177e4SLinus Torvalds 35207e79eedbSTetsuo Handa mutex_lock(&inode->i_mutex); 3521aae8a97dSAneesh Kumar K.V /* Make sure we don't allow creating hardlink to an unlinked file */ 3522aae8a97dSAneesh Kumar K.V if (inode->i_nlink == 0) 3523aae8a97dSAneesh Kumar K.V error = -ENOENT; 35248de52778SAl Viro else if (max_links && inode->i_nlink >= max_links) 35258de52778SAl Viro error = -EMLINK; 3526aae8a97dSAneesh Kumar K.V else 35271da177e4SLinus Torvalds error = dir->i_op->link(old_dentry, dir, new_dentry); 35287e79eedbSTetsuo Handa mutex_unlock(&inode->i_mutex); 3529e31e14ecSStephen Smalley if (!error) 35307e79eedbSTetsuo Handa fsnotify_link(dir, inode, new_dentry); 35311da177e4SLinus Torvalds return error; 35321da177e4SLinus Torvalds } 35331da177e4SLinus Torvalds 35341da177e4SLinus Torvalds /* 35351da177e4SLinus Torvalds * Hardlinks are often used in delicate situations. We avoid 35361da177e4SLinus Torvalds * security-related surprises by not following symlinks on the 35371da177e4SLinus Torvalds * newname. --KAB 35381da177e4SLinus Torvalds * 35391da177e4SLinus Torvalds * We don't follow them on the oldname either to be compatible 35401da177e4SLinus Torvalds * with linux 2.0, and to avoid hard-linking to directories 35411da177e4SLinus Torvalds * and other special files. --ADM 35421da177e4SLinus Torvalds */ 35432e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname, 35442e4d0924SHeiko Carstens int, newdfd, const char __user *, newname, int, flags) 35451da177e4SLinus Torvalds { 35461da177e4SLinus Torvalds struct dentry *new_dentry; 3547dae6ad8fSAl Viro struct path old_path, new_path; 354811a7b371SAneesh Kumar K.V int how = 0; 35491da177e4SLinus Torvalds int error; 35501da177e4SLinus Torvalds 355111a7b371SAneesh Kumar K.V if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0) 3552c04030e1SUlrich Drepper return -EINVAL; 355311a7b371SAneesh Kumar K.V /* 355411a7b371SAneesh Kumar K.V * To use null names we require CAP_DAC_READ_SEARCH 355511a7b371SAneesh Kumar K.V * This ensures that not everyone will be able to create 355611a7b371SAneesh Kumar K.V * handlink using the passed filedescriptor. 355711a7b371SAneesh Kumar K.V */ 355811a7b371SAneesh Kumar K.V if (flags & AT_EMPTY_PATH) { 355911a7b371SAneesh Kumar K.V if (!capable(CAP_DAC_READ_SEARCH)) 356011a7b371SAneesh Kumar K.V return -ENOENT; 356111a7b371SAneesh Kumar K.V how = LOOKUP_EMPTY; 356211a7b371SAneesh Kumar K.V } 3563c04030e1SUlrich Drepper 356411a7b371SAneesh Kumar K.V if (flags & AT_SYMLINK_FOLLOW) 356511a7b371SAneesh Kumar K.V how |= LOOKUP_FOLLOW; 356611a7b371SAneesh Kumar K.V 356711a7b371SAneesh Kumar K.V error = user_path_at(olddfd, oldname, how, &old_path); 35681da177e4SLinus Torvalds if (error) 35692ad94ae6SAl Viro return error; 35702ad94ae6SAl Viro 3571dae6ad8fSAl Viro new_dentry = user_path_create(newdfd, newname, &new_path, 0); 35721da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 35736902d925SDave Hansen if (IS_ERR(new_dentry)) 3574dae6ad8fSAl Viro goto out; 3575dae6ad8fSAl Viro 3576dae6ad8fSAl Viro error = -EXDEV; 3577dae6ad8fSAl Viro if (old_path.mnt != new_path.mnt) 3578dae6ad8fSAl Viro goto out_dput; 3579800179c9SKees Cook error = may_linkat(&old_path); 3580800179c9SKees Cook if (unlikely(error)) 3581800179c9SKees Cook goto out_dput; 3582dae6ad8fSAl Viro error = security_path_link(old_path.dentry, &new_path, new_dentry); 3583be6d3e56SKentaro Takeda if (error) 3584a8104a9fSAl Viro goto out_dput; 3585dae6ad8fSAl Viro error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry); 358675c3f29dSDave Hansen out_dput: 3587921a1650SAl Viro done_path_create(&new_path, new_dentry); 35881da177e4SLinus Torvalds out: 35892d8f3038SAl Viro path_put(&old_path); 35901da177e4SLinus Torvalds 35911da177e4SLinus Torvalds return error; 35921da177e4SLinus Torvalds } 35931da177e4SLinus Torvalds 35943480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname) 35955590ff0dSUlrich Drepper { 3596c04030e1SUlrich Drepper return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0); 35975590ff0dSUlrich Drepper } 35985590ff0dSUlrich Drepper 35991da177e4SLinus Torvalds /* 36001da177e4SLinus Torvalds * The worst of all namespace operations - renaming directory. "Perverted" 36011da177e4SLinus Torvalds * doesn't even start to describe it. Somebody in UCB had a heck of a trip... 36021da177e4SLinus Torvalds * Problems: 36031da177e4SLinus Torvalds * a) we can get into loop creation. Check is done in is_subdir(). 36041da177e4SLinus Torvalds * b) race potential - two innocent renames can create a loop together. 36051da177e4SLinus Torvalds * That's where 4.4 screws up. Current fix: serialization on 3606a11f3a05SArjan van de Ven * sb->s_vfs_rename_mutex. We might be more accurate, but that's another 36071da177e4SLinus Torvalds * story. 36081da177e4SLinus Torvalds * c) we have to lock _three_ objects - parents and victim (if it exists). 36091b1dcc1bSJes Sorensen * And that - after we got ->i_mutex on parents (until then we don't know 36101da177e4SLinus Torvalds * whether the target exists). Solution: try to be smart with locking 36111da177e4SLinus Torvalds * order for inodes. We rely on the fact that tree topology may change 3612a11f3a05SArjan van de Ven * only under ->s_vfs_rename_mutex _and_ that parent of the object we 36131da177e4SLinus Torvalds * move will be locked. Thus we can rank directories by the tree 36141da177e4SLinus Torvalds * (ancestors first) and rank all non-directories after them. 36151da177e4SLinus Torvalds * That works since everybody except rename does "lock parent, lookup, 3616a11f3a05SArjan van de Ven * lock child" and rename is under ->s_vfs_rename_mutex. 36171da177e4SLinus Torvalds * HOWEVER, it relies on the assumption that any object with ->lookup() 36181da177e4SLinus Torvalds * has no more than 1 dentry. If "hybrid" objects will ever appear, 36191da177e4SLinus Torvalds * we'd better make sure that there's no link(2) for them. 3620e4eaac06SSage Weil * d) conversion from fhandle to dentry may come in the wrong moment - when 36211b1dcc1bSJes Sorensen * we are removing the target. Solution: we will have to grab ->i_mutex 36221da177e4SLinus Torvalds * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on 3623c41b20e7SAdam Buchbinder * ->i_mutex on parents, which works but leads to some truly excessive 36241da177e4SLinus Torvalds * locking]. 36251da177e4SLinus Torvalds */ 362675c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry, 36271da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 36281da177e4SLinus Torvalds { 36291da177e4SLinus Torvalds int error = 0; 36309055cba7SSage Weil struct inode *target = new_dentry->d_inode; 36318de52778SAl Viro unsigned max_links = new_dir->i_sb->s_max_links; 36321da177e4SLinus Torvalds 36331da177e4SLinus Torvalds /* 36341da177e4SLinus Torvalds * If we are going to change the parent - check write permissions, 36351da177e4SLinus Torvalds * we'll need to flip '..'. 36361da177e4SLinus Torvalds */ 36371da177e4SLinus Torvalds if (new_dir != old_dir) { 3638f419a2e3SAl Viro error = inode_permission(old_dentry->d_inode, MAY_WRITE); 36391da177e4SLinus Torvalds if (error) 36401da177e4SLinus Torvalds return error; 36411da177e4SLinus Torvalds } 36421da177e4SLinus Torvalds 36431da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 36441da177e4SLinus Torvalds if (error) 36451da177e4SLinus Torvalds return error; 36461da177e4SLinus Torvalds 36471d2ef590SAl Viro dget(new_dentry); 3648d83c49f3SAl Viro if (target) 36491b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 36509055cba7SSage Weil 36511da177e4SLinus Torvalds error = -EBUSY; 36529055cba7SSage Weil if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry)) 36539055cba7SSage Weil goto out; 36549055cba7SSage Weil 36558de52778SAl Viro error = -EMLINK; 36568de52778SAl Viro if (max_links && !target && new_dir != old_dir && 36578de52778SAl Viro new_dir->i_nlink >= max_links) 36588de52778SAl Viro goto out; 36598de52778SAl Viro 36603cebde24SSage Weil if (target) 36613cebde24SSage Weil shrink_dcache_parent(new_dentry); 36621da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 36639055cba7SSage Weil if (error) 36649055cba7SSage Weil goto out; 36659055cba7SSage Weil 36661da177e4SLinus Torvalds if (target) { 36671da177e4SLinus Torvalds target->i_flags |= S_DEAD; 3668d83c49f3SAl Viro dont_mount(new_dentry); 3669d83c49f3SAl Viro } 36709055cba7SSage Weil out: 36719055cba7SSage Weil if (target) 36721b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 36731d2ef590SAl Viro dput(new_dentry); 3674e31e14ecSStephen Smalley if (!error) 3675349457ccSMark Fasheh if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) 36761da177e4SLinus Torvalds d_move(old_dentry,new_dentry); 36771da177e4SLinus Torvalds return error; 36781da177e4SLinus Torvalds } 36791da177e4SLinus Torvalds 368075c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry, 36811da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 36821da177e4SLinus Torvalds { 368351892bbbSSage Weil struct inode *target = new_dentry->d_inode; 36841da177e4SLinus Torvalds int error; 36851da177e4SLinus Torvalds 36861da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 36871da177e4SLinus Torvalds if (error) 36881da177e4SLinus Torvalds return error; 36891da177e4SLinus Torvalds 36901da177e4SLinus Torvalds dget(new_dentry); 36911da177e4SLinus Torvalds if (target) 36921b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 369351892bbbSSage Weil 36941da177e4SLinus Torvalds error = -EBUSY; 369551892bbbSSage Weil if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry)) 369651892bbbSSage Weil goto out; 369751892bbbSSage Weil 36981da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 369951892bbbSSage Weil if (error) 370051892bbbSSage Weil goto out; 370151892bbbSSage Weil 3702bec1052eSAl Viro if (target) 3703d83c49f3SAl Viro dont_mount(new_dentry); 3704349457ccSMark Fasheh if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) 37051da177e4SLinus Torvalds d_move(old_dentry, new_dentry); 370651892bbbSSage Weil out: 37071da177e4SLinus Torvalds if (target) 37081b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 37091da177e4SLinus Torvalds dput(new_dentry); 37101da177e4SLinus Torvalds return error; 37111da177e4SLinus Torvalds } 37121da177e4SLinus Torvalds 37131da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry, 37141da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 37151da177e4SLinus Torvalds { 37161da177e4SLinus Torvalds int error; 37171da177e4SLinus Torvalds int is_dir = S_ISDIR(old_dentry->d_inode->i_mode); 371859b0df21SEric Paris const unsigned char *old_name; 37191da177e4SLinus Torvalds 37201da177e4SLinus Torvalds if (old_dentry->d_inode == new_dentry->d_inode) 37211da177e4SLinus Torvalds return 0; 37221da177e4SLinus Torvalds 37231da177e4SLinus Torvalds error = may_delete(old_dir, old_dentry, is_dir); 37241da177e4SLinus Torvalds if (error) 37251da177e4SLinus Torvalds return error; 37261da177e4SLinus Torvalds 37271da177e4SLinus Torvalds if (!new_dentry->d_inode) 3728a95164d9SMiklos Szeredi error = may_create(new_dir, new_dentry); 37291da177e4SLinus Torvalds else 37301da177e4SLinus Torvalds error = may_delete(new_dir, new_dentry, is_dir); 37311da177e4SLinus Torvalds if (error) 37321da177e4SLinus Torvalds return error; 37331da177e4SLinus Torvalds 3734acfa4380SAl Viro if (!old_dir->i_op->rename) 37351da177e4SLinus Torvalds return -EPERM; 37361da177e4SLinus Torvalds 37370eeca283SRobert Love old_name = fsnotify_oldname_init(old_dentry->d_name.name); 37380eeca283SRobert Love 37391da177e4SLinus Torvalds if (is_dir) 37401da177e4SLinus Torvalds error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry); 37411da177e4SLinus Torvalds else 37421da177e4SLinus Torvalds error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry); 3743123df294SAl Viro if (!error) 3744123df294SAl Viro fsnotify_move(old_dir, new_dir, old_name, is_dir, 37455a190ae6SAl Viro new_dentry->d_inode, old_dentry); 37460eeca283SRobert Love fsnotify_oldname_free(old_name); 37470eeca283SRobert Love 37481da177e4SLinus Torvalds return error; 37491da177e4SLinus Torvalds } 37501da177e4SLinus Torvalds 37512e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname, 37522e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 37531da177e4SLinus Torvalds { 37541da177e4SLinus Torvalds struct dentry *old_dir, *new_dir; 37551da177e4SLinus Torvalds struct dentry *old_dentry, *new_dentry; 37561da177e4SLinus Torvalds struct dentry *trap; 37571da177e4SLinus Torvalds struct nameidata oldnd, newnd; 375891a27b2aSJeff Layton struct filename *from; 375991a27b2aSJeff Layton struct filename *to; 37602ad94ae6SAl Viro int error; 37611da177e4SLinus Torvalds 376291a27b2aSJeff Layton from = user_path_parent(olddfd, oldname, &oldnd); 376391a27b2aSJeff Layton if (IS_ERR(from)) { 376491a27b2aSJeff Layton error = PTR_ERR(from); 37651da177e4SLinus Torvalds goto exit; 376691a27b2aSJeff Layton } 37671da177e4SLinus Torvalds 376891a27b2aSJeff Layton to = user_path_parent(newdfd, newname, &newnd); 376991a27b2aSJeff Layton if (IS_ERR(to)) { 377091a27b2aSJeff Layton error = PTR_ERR(to); 37711da177e4SLinus Torvalds goto exit1; 377291a27b2aSJeff Layton } 37731da177e4SLinus Torvalds 37741da177e4SLinus Torvalds error = -EXDEV; 37754ac91378SJan Blunck if (oldnd.path.mnt != newnd.path.mnt) 37761da177e4SLinus Torvalds goto exit2; 37771da177e4SLinus Torvalds 37784ac91378SJan Blunck old_dir = oldnd.path.dentry; 37791da177e4SLinus Torvalds error = -EBUSY; 37801da177e4SLinus Torvalds if (oldnd.last_type != LAST_NORM) 37811da177e4SLinus Torvalds goto exit2; 37821da177e4SLinus Torvalds 37834ac91378SJan Blunck new_dir = newnd.path.dentry; 37841da177e4SLinus Torvalds if (newnd.last_type != LAST_NORM) 37851da177e4SLinus Torvalds goto exit2; 37861da177e4SLinus Torvalds 3787c30dabfeSJan Kara error = mnt_want_write(oldnd.path.mnt); 3788c30dabfeSJan Kara if (error) 3789c30dabfeSJan Kara goto exit2; 3790c30dabfeSJan Kara 37910612d9fbSOGAWA Hirofumi oldnd.flags &= ~LOOKUP_PARENT; 37920612d9fbSOGAWA Hirofumi newnd.flags &= ~LOOKUP_PARENT; 37934e9ed2f8SOGAWA Hirofumi newnd.flags |= LOOKUP_RENAME_TARGET; 37940612d9fbSOGAWA Hirofumi 37951da177e4SLinus Torvalds trap = lock_rename(new_dir, old_dir); 37961da177e4SLinus Torvalds 379749705b77SChristoph Hellwig old_dentry = lookup_hash(&oldnd); 37981da177e4SLinus Torvalds error = PTR_ERR(old_dentry); 37991da177e4SLinus Torvalds if (IS_ERR(old_dentry)) 38001da177e4SLinus Torvalds goto exit3; 38011da177e4SLinus Torvalds /* source must exist */ 38021da177e4SLinus Torvalds error = -ENOENT; 38031da177e4SLinus Torvalds if (!old_dentry->d_inode) 38041da177e4SLinus Torvalds goto exit4; 38051da177e4SLinus Torvalds /* unless the source is a directory trailing slashes give -ENOTDIR */ 38061da177e4SLinus Torvalds if (!S_ISDIR(old_dentry->d_inode->i_mode)) { 38071da177e4SLinus Torvalds error = -ENOTDIR; 38081da177e4SLinus Torvalds if (oldnd.last.name[oldnd.last.len]) 38091da177e4SLinus Torvalds goto exit4; 38101da177e4SLinus Torvalds if (newnd.last.name[newnd.last.len]) 38111da177e4SLinus Torvalds goto exit4; 38121da177e4SLinus Torvalds } 38131da177e4SLinus Torvalds /* source should not be ancestor of target */ 38141da177e4SLinus Torvalds error = -EINVAL; 38151da177e4SLinus Torvalds if (old_dentry == trap) 38161da177e4SLinus Torvalds goto exit4; 381749705b77SChristoph Hellwig new_dentry = lookup_hash(&newnd); 38181da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 38191da177e4SLinus Torvalds if (IS_ERR(new_dentry)) 38201da177e4SLinus Torvalds goto exit4; 38211da177e4SLinus Torvalds /* target should not be an ancestor of source */ 38221da177e4SLinus Torvalds error = -ENOTEMPTY; 38231da177e4SLinus Torvalds if (new_dentry == trap) 38241da177e4SLinus Torvalds goto exit5; 38251da177e4SLinus Torvalds 3826be6d3e56SKentaro Takeda error = security_path_rename(&oldnd.path, old_dentry, 3827be6d3e56SKentaro Takeda &newnd.path, new_dentry); 3828be6d3e56SKentaro Takeda if (error) 3829c30dabfeSJan Kara goto exit5; 38301da177e4SLinus Torvalds error = vfs_rename(old_dir->d_inode, old_dentry, 38311da177e4SLinus Torvalds new_dir->d_inode, new_dentry); 38321da177e4SLinus Torvalds exit5: 38331da177e4SLinus Torvalds dput(new_dentry); 38341da177e4SLinus Torvalds exit4: 38351da177e4SLinus Torvalds dput(old_dentry); 38361da177e4SLinus Torvalds exit3: 38371da177e4SLinus Torvalds unlock_rename(new_dir, old_dir); 3838c30dabfeSJan Kara mnt_drop_write(oldnd.path.mnt); 38391da177e4SLinus Torvalds exit2: 38401d957f9bSJan Blunck path_put(&newnd.path); 38412ad94ae6SAl Viro putname(to); 38421da177e4SLinus Torvalds exit1: 38431d957f9bSJan Blunck path_put(&oldnd.path); 38441da177e4SLinus Torvalds putname(from); 38452ad94ae6SAl Viro exit: 38461da177e4SLinus Torvalds return error; 38471da177e4SLinus Torvalds } 38481da177e4SLinus Torvalds 3849a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname) 38505590ff0dSUlrich Drepper { 38515590ff0dSUlrich Drepper return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname); 38525590ff0dSUlrich Drepper } 38535590ff0dSUlrich Drepper 38541da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link) 38551da177e4SLinus Torvalds { 38561da177e4SLinus Torvalds int len; 38571da177e4SLinus Torvalds 38581da177e4SLinus Torvalds len = PTR_ERR(link); 38591da177e4SLinus Torvalds if (IS_ERR(link)) 38601da177e4SLinus Torvalds goto out; 38611da177e4SLinus Torvalds 38621da177e4SLinus Torvalds len = strlen(link); 38631da177e4SLinus Torvalds if (len > (unsigned) buflen) 38641da177e4SLinus Torvalds len = buflen; 38651da177e4SLinus Torvalds if (copy_to_user(buffer, link, len)) 38661da177e4SLinus Torvalds len = -EFAULT; 38671da177e4SLinus Torvalds out: 38681da177e4SLinus Torvalds return len; 38691da177e4SLinus Torvalds } 38701da177e4SLinus Torvalds 38711da177e4SLinus Torvalds /* 38721da177e4SLinus Torvalds * A helper for ->readlink(). This should be used *ONLY* for symlinks that 38731da177e4SLinus Torvalds * have ->follow_link() touching nd only in nd_set_link(). Using (or not 38741da177e4SLinus Torvalds * using) it for any given inode is up to filesystem. 38751da177e4SLinus Torvalds */ 38761da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen) 38771da177e4SLinus Torvalds { 38781da177e4SLinus Torvalds struct nameidata nd; 3879cc314eefSLinus Torvalds void *cookie; 3880694a1764SMarcin Slusarz int res; 3881cc314eefSLinus Torvalds 38821da177e4SLinus Torvalds nd.depth = 0; 3883cc314eefSLinus Torvalds cookie = dentry->d_inode->i_op->follow_link(dentry, &nd); 3884694a1764SMarcin Slusarz if (IS_ERR(cookie)) 3885694a1764SMarcin Slusarz return PTR_ERR(cookie); 3886694a1764SMarcin Slusarz 3887694a1764SMarcin Slusarz res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd)); 38881da177e4SLinus Torvalds if (dentry->d_inode->i_op->put_link) 3889cc314eefSLinus Torvalds dentry->d_inode->i_op->put_link(dentry, &nd, cookie); 3890694a1764SMarcin Slusarz return res; 38911da177e4SLinus Torvalds } 38921da177e4SLinus Torvalds 38931da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link) 38941da177e4SLinus Torvalds { 38951da177e4SLinus Torvalds return __vfs_follow_link(nd, link); 38961da177e4SLinus Torvalds } 38971da177e4SLinus Torvalds 38981da177e4SLinus Torvalds /* get the link contents into pagecache */ 38991da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage) 39001da177e4SLinus Torvalds { 3901ebd09abbSDuane Griffin char *kaddr; 39021da177e4SLinus Torvalds struct page *page; 39031da177e4SLinus Torvalds struct address_space *mapping = dentry->d_inode->i_mapping; 3904090d2b18SPekka Enberg page = read_mapping_page(mapping, 0, NULL); 39051da177e4SLinus Torvalds if (IS_ERR(page)) 39066fe6900eSNick Piggin return (char*)page; 39071da177e4SLinus Torvalds *ppage = page; 3908ebd09abbSDuane Griffin kaddr = kmap(page); 3909ebd09abbSDuane Griffin nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1); 3910ebd09abbSDuane Griffin return kaddr; 39111da177e4SLinus Torvalds } 39121da177e4SLinus Torvalds 39131da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen) 39141da177e4SLinus Torvalds { 39151da177e4SLinus Torvalds struct page *page = NULL; 39161da177e4SLinus Torvalds char *s = page_getlink(dentry, &page); 39171da177e4SLinus Torvalds int res = vfs_readlink(dentry,buffer,buflen,s); 39181da177e4SLinus Torvalds if (page) { 39191da177e4SLinus Torvalds kunmap(page); 39201da177e4SLinus Torvalds page_cache_release(page); 39211da177e4SLinus Torvalds } 39221da177e4SLinus Torvalds return res; 39231da177e4SLinus Torvalds } 39241da177e4SLinus Torvalds 3925cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd) 39261da177e4SLinus Torvalds { 3927cc314eefSLinus Torvalds struct page *page = NULL; 39281da177e4SLinus Torvalds nd_set_link(nd, page_getlink(dentry, &page)); 3929cc314eefSLinus Torvalds return page; 39301da177e4SLinus Torvalds } 39311da177e4SLinus Torvalds 3932cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie) 39331da177e4SLinus Torvalds { 3934cc314eefSLinus Torvalds struct page *page = cookie; 3935cc314eefSLinus Torvalds 3936cc314eefSLinus Torvalds if (page) { 39371da177e4SLinus Torvalds kunmap(page); 39381da177e4SLinus Torvalds page_cache_release(page); 39391da177e4SLinus Torvalds } 39401da177e4SLinus Torvalds } 39411da177e4SLinus Torvalds 394254566b2cSNick Piggin /* 394354566b2cSNick Piggin * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS 394454566b2cSNick Piggin */ 394554566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs) 39461da177e4SLinus Torvalds { 39471da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 39480adb25d2SKirill Korotaev struct page *page; 3949afddba49SNick Piggin void *fsdata; 3950beb497abSDmitriy Monakhov int err; 39511da177e4SLinus Torvalds char *kaddr; 395254566b2cSNick Piggin unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE; 395354566b2cSNick Piggin if (nofs) 395454566b2cSNick Piggin flags |= AOP_FLAG_NOFS; 39551da177e4SLinus Torvalds 39567e53cac4SNeilBrown retry: 3957afddba49SNick Piggin err = pagecache_write_begin(NULL, mapping, 0, len-1, 395854566b2cSNick Piggin flags, &page, &fsdata); 39591da177e4SLinus Torvalds if (err) 3960afddba49SNick Piggin goto fail; 3961afddba49SNick Piggin 3962e8e3c3d6SCong Wang kaddr = kmap_atomic(page); 39631da177e4SLinus Torvalds memcpy(kaddr, symname, len-1); 3964e8e3c3d6SCong Wang kunmap_atomic(kaddr); 3965afddba49SNick Piggin 3966afddba49SNick Piggin err = pagecache_write_end(NULL, mapping, 0, len-1, len-1, 3967afddba49SNick Piggin page, fsdata); 39681da177e4SLinus Torvalds if (err < 0) 39691da177e4SLinus Torvalds goto fail; 3970afddba49SNick Piggin if (err < len-1) 3971afddba49SNick Piggin goto retry; 3972afddba49SNick Piggin 39731da177e4SLinus Torvalds mark_inode_dirty(inode); 39741da177e4SLinus Torvalds return 0; 39751da177e4SLinus Torvalds fail: 39761da177e4SLinus Torvalds return err; 39771da177e4SLinus Torvalds } 39781da177e4SLinus Torvalds 39790adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len) 39800adb25d2SKirill Korotaev { 39810adb25d2SKirill Korotaev return __page_symlink(inode, symname, len, 398254566b2cSNick Piggin !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS)); 39830adb25d2SKirill Korotaev } 39840adb25d2SKirill Korotaev 398592e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = { 39861da177e4SLinus Torvalds .readlink = generic_readlink, 39871da177e4SLinus Torvalds .follow_link = page_follow_link_light, 39881da177e4SLinus Torvalds .put_link = page_put_link, 39891da177e4SLinus Torvalds }; 39901da177e4SLinus Torvalds 39912d8f3038SAl Viro EXPORT_SYMBOL(user_path_at); 3992cc53ce53SDavid Howells EXPORT_SYMBOL(follow_down_one); 39931da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down); 39941da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up); 3995f6d2ac5cSAl Viro EXPORT_SYMBOL(get_write_access); /* nfsd */ 39961da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename); 39971da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len); 39981da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light); 39991da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link); 40001da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink); 40010adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink); 40021da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink); 40031da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations); 4004d1811465SAl Viro EXPORT_SYMBOL(kern_path); 400516f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup); 4006f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission); 40071da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename); 40081da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create); 40091da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link); 40101da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link); 40111da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir); 40121da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod); 40131da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission); 40141da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink); 40151da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename); 40161da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir); 40171da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink); 40181da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink); 40191da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash); 40201da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink); 4021