11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/fs/namei.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 1991, 1992 Linus Torvalds 51da177e4SLinus Torvalds */ 61da177e4SLinus Torvalds 71da177e4SLinus Torvalds /* 81da177e4SLinus Torvalds * Some corrections by tytso. 91da177e4SLinus Torvalds */ 101da177e4SLinus Torvalds 111da177e4SLinus Torvalds /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname 121da177e4SLinus Torvalds * lookup logic. 131da177e4SLinus Torvalds */ 141da177e4SLinus Torvalds /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture. 151da177e4SLinus Torvalds */ 161da177e4SLinus Torvalds 171da177e4SLinus Torvalds #include <linux/init.h> 18630d9c47SPaul Gortmaker #include <linux/export.h> 1944696908SDavid S. Miller #include <linux/kernel.h> 201da177e4SLinus Torvalds #include <linux/slab.h> 211da177e4SLinus Torvalds #include <linux/fs.h> 221da177e4SLinus Torvalds #include <linux/namei.h> 231da177e4SLinus Torvalds #include <linux/pagemap.h> 240eeca283SRobert Love #include <linux/fsnotify.h> 251da177e4SLinus Torvalds #include <linux/personality.h> 261da177e4SLinus Torvalds #include <linux/security.h> 276146f0d5SMimi Zohar #include <linux/ima.h> 281da177e4SLinus Torvalds #include <linux/syscalls.h> 291da177e4SLinus Torvalds #include <linux/mount.h> 301da177e4SLinus Torvalds #include <linux/audit.h> 3116f7e0feSRandy Dunlap #include <linux/capability.h> 32834f2a4aSTrond Myklebust #include <linux/file.h> 335590ff0dSUlrich Drepper #include <linux/fcntl.h> 3408ce5f16SSerge E. Hallyn #include <linux/device_cgroup.h> 355ad4e53bSAl Viro #include <linux/fs_struct.h> 36e77819e5SLinus Torvalds #include <linux/posix_acl.h> 371da177e4SLinus Torvalds #include <asm/uaccess.h> 381da177e4SLinus Torvalds 39e81e3f4dSEric Paris #include "internal.h" 40c7105365SAl Viro #include "mount.h" 41e81e3f4dSEric Paris 421da177e4SLinus Torvalds /* [Feb-1997 T. Schoebel-Theuer] 431da177e4SLinus Torvalds * Fundamental changes in the pathname lookup mechanisms (namei) 441da177e4SLinus Torvalds * were necessary because of omirr. The reason is that omirr needs 451da177e4SLinus Torvalds * to know the _real_ pathname, not the user-supplied one, in case 461da177e4SLinus Torvalds * of symlinks (and also when transname replacements occur). 471da177e4SLinus Torvalds * 481da177e4SLinus Torvalds * The new code replaces the old recursive symlink resolution with 491da177e4SLinus Torvalds * an iterative one (in case of non-nested symlink chains). It does 501da177e4SLinus Torvalds * this with calls to <fs>_follow_link(). 511da177e4SLinus Torvalds * As a side effect, dir_namei(), _namei() and follow_link() are now 521da177e4SLinus Torvalds * replaced with a single function lookup_dentry() that can handle all 531da177e4SLinus Torvalds * the special cases of the former code. 541da177e4SLinus Torvalds * 551da177e4SLinus Torvalds * With the new dcache, the pathname is stored at each inode, at least as 561da177e4SLinus Torvalds * long as the refcount of the inode is positive. As a side effect, the 571da177e4SLinus Torvalds * size of the dcache depends on the inode cache and thus is dynamic. 581da177e4SLinus Torvalds * 591da177e4SLinus Torvalds * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink 601da177e4SLinus Torvalds * resolution to correspond with current state of the code. 611da177e4SLinus Torvalds * 621da177e4SLinus Torvalds * Note that the symlink resolution is not *completely* iterative. 631da177e4SLinus Torvalds * There is still a significant amount of tail- and mid- recursion in 641da177e4SLinus Torvalds * the algorithm. Also, note that <fs>_readlink() is not used in 651da177e4SLinus Torvalds * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink() 661da177e4SLinus Torvalds * may return different results than <fs>_follow_link(). Many virtual 671da177e4SLinus Torvalds * filesystems (including /proc) exhibit this behavior. 681da177e4SLinus Torvalds */ 691da177e4SLinus Torvalds 701da177e4SLinus Torvalds /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation: 711da177e4SLinus Torvalds * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL 721da177e4SLinus Torvalds * and the name already exists in form of a symlink, try to create the new 731da177e4SLinus Torvalds * name indicated by the symlink. The old code always complained that the 741da177e4SLinus Torvalds * name already exists, due to not following the symlink even if its target 751da177e4SLinus Torvalds * is nonexistent. The new semantics affects also mknod() and link() when 7625985edcSLucas De Marchi * the name is a symlink pointing to a non-existent name. 771da177e4SLinus Torvalds * 781da177e4SLinus Torvalds * I don't know which semantics is the right one, since I have no access 791da177e4SLinus Torvalds * to standards. But I found by trial that HP-UX 9.0 has the full "new" 801da177e4SLinus Torvalds * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the 811da177e4SLinus Torvalds * "old" one. Personally, I think the new semantics is much more logical. 821da177e4SLinus Torvalds * Note that "ln old new" where "new" is a symlink pointing to a non-existing 831da177e4SLinus Torvalds * file does succeed in both HP-UX and SunOs, but not in Solaris 841da177e4SLinus Torvalds * and in the old Linux semantics. 851da177e4SLinus Torvalds */ 861da177e4SLinus Torvalds 871da177e4SLinus Torvalds /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink 881da177e4SLinus Torvalds * semantics. See the comments in "open_namei" and "do_link" below. 891da177e4SLinus Torvalds * 901da177e4SLinus Torvalds * [10-Sep-98 Alan Modra] Another symlink change. 911da177e4SLinus Torvalds */ 921da177e4SLinus Torvalds 931da177e4SLinus Torvalds /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks: 941da177e4SLinus Torvalds * inside the path - always follow. 951da177e4SLinus Torvalds * in the last component in creation/removal/renaming - never follow. 961da177e4SLinus Torvalds * if LOOKUP_FOLLOW passed - follow. 971da177e4SLinus Torvalds * if the pathname has trailing slashes - follow. 981da177e4SLinus Torvalds * otherwise - don't follow. 991da177e4SLinus Torvalds * (applied in that order). 1001da177e4SLinus Torvalds * 1011da177e4SLinus Torvalds * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT 1021da177e4SLinus Torvalds * restored for 2.4. This is the last surviving part of old 4.2BSD bug. 1031da177e4SLinus Torvalds * During the 2.4 we need to fix the userland stuff depending on it - 1041da177e4SLinus Torvalds * hopefully we will be able to get rid of that wart in 2.5. So far only 1051da177e4SLinus Torvalds * XEmacs seems to be relying on it... 1061da177e4SLinus Torvalds */ 1071da177e4SLinus Torvalds /* 1081da177e4SLinus Torvalds * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland) 109a11f3a05SArjan van de Ven * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives 1101da177e4SLinus Torvalds * any extra contention... 1111da177e4SLinus Torvalds */ 1121da177e4SLinus Torvalds 1131da177e4SLinus Torvalds /* In order to reduce some races, while at the same time doing additional 1141da177e4SLinus Torvalds * checking and hopefully speeding things up, we copy filenames to the 1151da177e4SLinus Torvalds * kernel data space before using them.. 1161da177e4SLinus Torvalds * 1171da177e4SLinus Torvalds * POSIX.1 2.4: an empty pathname is invalid (ENOENT). 1181da177e4SLinus Torvalds * PATH_MAX includes the nul terminator --RR. 1191da177e4SLinus Torvalds */ 12091a27b2aSJeff Layton void final_putname(struct filename *name) 1211da177e4SLinus Torvalds { 1227950e385SJeff Layton if (name->separate) { 12391a27b2aSJeff Layton __putname(name->name); 12491a27b2aSJeff Layton kfree(name); 1257950e385SJeff Layton } else { 1267950e385SJeff Layton __putname(name); 12791a27b2aSJeff Layton } 1287950e385SJeff Layton } 1297950e385SJeff Layton 1307950e385SJeff Layton #define EMBEDDED_NAME_MAX (PATH_MAX - sizeof(struct filename)) 13191a27b2aSJeff Layton 13291a27b2aSJeff Layton static struct filename * 13391a27b2aSJeff Layton getname_flags(const char __user *filename, int flags, int *empty) 13491a27b2aSJeff Layton { 13591a27b2aSJeff Layton struct filename *result, *err; 1363f9f0aa6SLinus Torvalds int len; 1377950e385SJeff Layton long max; 1387950e385SJeff Layton char *kname; 1391da177e4SLinus Torvalds 1407ac86265SJeff Layton result = audit_reusename(filename); 1417ac86265SJeff Layton if (result) 1427ac86265SJeff Layton return result; 1437ac86265SJeff Layton 1447950e385SJeff Layton result = __getname(); 1453f9f0aa6SLinus Torvalds if (unlikely(!result)) 1464043cde8SEric Paris return ERR_PTR(-ENOMEM); 1471da177e4SLinus Torvalds 1487950e385SJeff Layton /* 1497950e385SJeff Layton * First, try to embed the struct filename inside the names_cache 1507950e385SJeff Layton * allocation 1517950e385SJeff Layton */ 1527950e385SJeff Layton kname = (char *)result + sizeof(*result); 15391a27b2aSJeff Layton result->name = kname; 1547950e385SJeff Layton result->separate = false; 1557950e385SJeff Layton max = EMBEDDED_NAME_MAX; 1567950e385SJeff Layton 1577950e385SJeff Layton recopy: 1587950e385SJeff Layton len = strncpy_from_user(kname, filename, max); 15991a27b2aSJeff Layton if (unlikely(len < 0)) { 1603f9f0aa6SLinus Torvalds err = ERR_PTR(len); 1613f9f0aa6SLinus Torvalds goto error; 16291a27b2aSJeff Layton } 1633f9f0aa6SLinus Torvalds 1647950e385SJeff Layton /* 1657950e385SJeff Layton * Uh-oh. We have a name that's approaching PATH_MAX. Allocate a 1667950e385SJeff Layton * separate struct filename so we can dedicate the entire 1677950e385SJeff Layton * names_cache allocation for the pathname, and re-do the copy from 1687950e385SJeff Layton * userland. 1697950e385SJeff Layton */ 1707950e385SJeff Layton if (len == EMBEDDED_NAME_MAX && max == EMBEDDED_NAME_MAX) { 1717950e385SJeff Layton kname = (char *)result; 1727950e385SJeff Layton 1737950e385SJeff Layton result = kzalloc(sizeof(*result), GFP_KERNEL); 1747950e385SJeff Layton if (!result) { 1757950e385SJeff Layton err = ERR_PTR(-ENOMEM); 1767950e385SJeff Layton result = (struct filename *)kname; 1777950e385SJeff Layton goto error; 1787950e385SJeff Layton } 1797950e385SJeff Layton result->name = kname; 1807950e385SJeff Layton result->separate = true; 1817950e385SJeff Layton max = PATH_MAX; 1827950e385SJeff Layton goto recopy; 1837950e385SJeff Layton } 1847950e385SJeff Layton 1853f9f0aa6SLinus Torvalds /* The empty path is special. */ 1863f9f0aa6SLinus Torvalds if (unlikely(!len)) { 1873f9f0aa6SLinus Torvalds if (empty) 1881fa1e7f6SAndy Whitcroft *empty = 1; 1893f9f0aa6SLinus Torvalds err = ERR_PTR(-ENOENT); 1903f9f0aa6SLinus Torvalds if (!(flags & LOOKUP_EMPTY)) 1913f9f0aa6SLinus Torvalds goto error; 1921da177e4SLinus Torvalds } 1933f9f0aa6SLinus Torvalds 1943f9f0aa6SLinus Torvalds err = ERR_PTR(-ENAMETOOLONG); 1957950e385SJeff Layton if (unlikely(len >= PATH_MAX)) 1967950e385SJeff Layton goto error; 1977950e385SJeff Layton 1987950e385SJeff Layton result->uptr = filename; 199c4ad8f98SLinus Torvalds result->aname = NULL; 2001da177e4SLinus Torvalds audit_getname(result); 2011da177e4SLinus Torvalds return result; 2021da177e4SLinus Torvalds 2033f9f0aa6SLinus Torvalds error: 2047950e385SJeff Layton final_putname(result); 2053f9f0aa6SLinus Torvalds return err; 2063f9f0aa6SLinus Torvalds } 2073f9f0aa6SLinus Torvalds 20891a27b2aSJeff Layton struct filename * 20991a27b2aSJeff Layton getname(const char __user * filename) 210f52e0c11SAl Viro { 211f7493e5dSLinus Torvalds return getname_flags(filename, 0, NULL); 212f52e0c11SAl Viro } 213f52e0c11SAl Viro 214c4ad8f98SLinus Torvalds /* 215c4ad8f98SLinus Torvalds * The "getname_kernel()" interface doesn't do pathnames longer 216c4ad8f98SLinus Torvalds * than EMBEDDED_NAME_MAX. Deal with it - you're a kernel user. 217c4ad8f98SLinus Torvalds */ 218c4ad8f98SLinus Torvalds struct filename * 219c4ad8f98SLinus Torvalds getname_kernel(const char * filename) 220c4ad8f98SLinus Torvalds { 221c4ad8f98SLinus Torvalds struct filename *result; 222c4ad8f98SLinus Torvalds char *kname; 223c4ad8f98SLinus Torvalds int len; 224c4ad8f98SLinus Torvalds 225c4ad8f98SLinus Torvalds len = strlen(filename); 226c4ad8f98SLinus Torvalds if (len >= EMBEDDED_NAME_MAX) 227c4ad8f98SLinus Torvalds return ERR_PTR(-ENAMETOOLONG); 228c4ad8f98SLinus Torvalds 229c4ad8f98SLinus Torvalds result = __getname(); 230c4ad8f98SLinus Torvalds if (unlikely(!result)) 231c4ad8f98SLinus Torvalds return ERR_PTR(-ENOMEM); 232c4ad8f98SLinus Torvalds 233c4ad8f98SLinus Torvalds kname = (char *)result + sizeof(*result); 234c4ad8f98SLinus Torvalds result->name = kname; 235c4ad8f98SLinus Torvalds result->uptr = NULL; 236c4ad8f98SLinus Torvalds result->aname = NULL; 237c4ad8f98SLinus Torvalds result->separate = false; 238c4ad8f98SLinus Torvalds 239c4ad8f98SLinus Torvalds strlcpy(kname, filename, EMBEDDED_NAME_MAX); 240c4ad8f98SLinus Torvalds return result; 241c4ad8f98SLinus Torvalds } 242c4ad8f98SLinus Torvalds 2431da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL 24491a27b2aSJeff Layton void putname(struct filename *name) 2451da177e4SLinus Torvalds { 2465ac3a9c2SAl Viro if (unlikely(!audit_dummy_context())) 24791a27b2aSJeff Layton return audit_putname(name); 24891a27b2aSJeff Layton final_putname(name); 2491da177e4SLinus Torvalds } 2501da177e4SLinus Torvalds #endif 2511da177e4SLinus Torvalds 252e77819e5SLinus Torvalds static int check_acl(struct inode *inode, int mask) 253e77819e5SLinus Torvalds { 25484635d68SLinus Torvalds #ifdef CONFIG_FS_POSIX_ACL 255e77819e5SLinus Torvalds struct posix_acl *acl; 256e77819e5SLinus Torvalds 257e77819e5SLinus Torvalds if (mask & MAY_NOT_BLOCK) { 2583567866bSAl Viro acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS); 2593567866bSAl Viro if (!acl) 260e77819e5SLinus Torvalds return -EAGAIN; 2613567866bSAl Viro /* no ->get_acl() calls in RCU mode... */ 2623567866bSAl Viro if (acl == ACL_NOT_CACHED) 263e77819e5SLinus Torvalds return -ECHILD; 264206b1d09SAri Savolainen return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK); 265e77819e5SLinus Torvalds } 266e77819e5SLinus Torvalds 2672982baa2SChristoph Hellwig acl = get_acl(inode, ACL_TYPE_ACCESS); 2684e34e719SChristoph Hellwig if (IS_ERR(acl)) 2694e34e719SChristoph Hellwig return PTR_ERR(acl); 270e77819e5SLinus Torvalds if (acl) { 271e77819e5SLinus Torvalds int error = posix_acl_permission(inode, acl, mask); 272e77819e5SLinus Torvalds posix_acl_release(acl); 273e77819e5SLinus Torvalds return error; 274e77819e5SLinus Torvalds } 27584635d68SLinus Torvalds #endif 276e77819e5SLinus Torvalds 277e77819e5SLinus Torvalds return -EAGAIN; 278e77819e5SLinus Torvalds } 279e77819e5SLinus Torvalds 2805909ccaaSLinus Torvalds /* 281948409c7SAndreas Gruenbacher * This does the basic permission checking 2825909ccaaSLinus Torvalds */ 2837e40145eSAl Viro static int acl_permission_check(struct inode *inode, int mask) 2845909ccaaSLinus Torvalds { 28526cf46beSLinus Torvalds unsigned int mode = inode->i_mode; 2865909ccaaSLinus Torvalds 2878e96e3b7SEric W. Biederman if (likely(uid_eq(current_fsuid(), inode->i_uid))) 2885909ccaaSLinus Torvalds mode >>= 6; 2895909ccaaSLinus Torvalds else { 290e77819e5SLinus Torvalds if (IS_POSIXACL(inode) && (mode & S_IRWXG)) { 2917e40145eSAl Viro int error = check_acl(inode, mask); 2925909ccaaSLinus Torvalds if (error != -EAGAIN) 2935909ccaaSLinus Torvalds return error; 2945909ccaaSLinus Torvalds } 2955909ccaaSLinus Torvalds 2965909ccaaSLinus Torvalds if (in_group_p(inode->i_gid)) 2975909ccaaSLinus Torvalds mode >>= 3; 2985909ccaaSLinus Torvalds } 2995909ccaaSLinus Torvalds 3005909ccaaSLinus Torvalds /* 3015909ccaaSLinus Torvalds * If the DACs are ok we don't need any capability check. 3025909ccaaSLinus Torvalds */ 3039c2c7039SAl Viro if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0) 3045909ccaaSLinus Torvalds return 0; 3055909ccaaSLinus Torvalds return -EACCES; 3065909ccaaSLinus Torvalds } 3071da177e4SLinus Torvalds 3081da177e4SLinus Torvalds /** 3091da177e4SLinus Torvalds * generic_permission - check for access rights on a Posix-like filesystem 3101da177e4SLinus Torvalds * @inode: inode to check access rights for 3118fd90c8dSAndreas Gruenbacher * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...) 3121da177e4SLinus Torvalds * 3131da177e4SLinus Torvalds * Used to check for read/write/execute permissions on a file. 3141da177e4SLinus Torvalds * We use "fsuid" for this, letting us set arbitrary permissions 3151da177e4SLinus Torvalds * for filesystem access without changing the "normal" uids which 316b74c79e9SNick Piggin * are used for other things. 317b74c79e9SNick Piggin * 318b74c79e9SNick Piggin * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk 319b74c79e9SNick Piggin * request cannot be satisfied (eg. requires blocking or too much complexity). 320b74c79e9SNick Piggin * It would then be called again in ref-walk mode. 3211da177e4SLinus Torvalds */ 3222830ba7fSAl Viro int generic_permission(struct inode *inode, int mask) 3231da177e4SLinus Torvalds { 3245909ccaaSLinus Torvalds int ret; 3251da177e4SLinus Torvalds 3261da177e4SLinus Torvalds /* 327948409c7SAndreas Gruenbacher * Do the basic permission checks. 3281da177e4SLinus Torvalds */ 3297e40145eSAl Viro ret = acl_permission_check(inode, mask); 3305909ccaaSLinus Torvalds if (ret != -EACCES) 3315909ccaaSLinus Torvalds return ret; 3321da177e4SLinus Torvalds 333d594e7ecSAl Viro if (S_ISDIR(inode->i_mode)) { 334d594e7ecSAl Viro /* DACs are overridable for directories */ 3351a48e2acSEric W. Biederman if (inode_capable(inode, CAP_DAC_OVERRIDE)) 336d594e7ecSAl Viro return 0; 337d594e7ecSAl Viro if (!(mask & MAY_WRITE)) 3381a48e2acSEric W. Biederman if (inode_capable(inode, CAP_DAC_READ_SEARCH)) 339d594e7ecSAl Viro return 0; 340d594e7ecSAl Viro return -EACCES; 341d594e7ecSAl Viro } 3421da177e4SLinus Torvalds /* 3431da177e4SLinus Torvalds * Read/write DACs are always overridable. 344d594e7ecSAl Viro * Executable DACs are overridable when there is 345d594e7ecSAl Viro * at least one exec bit set. 3461da177e4SLinus Torvalds */ 347d594e7ecSAl Viro if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO)) 3481a48e2acSEric W. Biederman if (inode_capable(inode, CAP_DAC_OVERRIDE)) 3491da177e4SLinus Torvalds return 0; 3501da177e4SLinus Torvalds 3511da177e4SLinus Torvalds /* 3521da177e4SLinus Torvalds * Searching includes executable on directories, else just read. 3531da177e4SLinus Torvalds */ 3547ea66001SSerge E. Hallyn mask &= MAY_READ | MAY_WRITE | MAY_EXEC; 355d594e7ecSAl Viro if (mask == MAY_READ) 3561a48e2acSEric W. Biederman if (inode_capable(inode, CAP_DAC_READ_SEARCH)) 3571da177e4SLinus Torvalds return 0; 3581da177e4SLinus Torvalds 3591da177e4SLinus Torvalds return -EACCES; 3601da177e4SLinus Torvalds } 3614d359507SAl Viro EXPORT_SYMBOL(generic_permission); 3621da177e4SLinus Torvalds 3633ddcd056SLinus Torvalds /* 3643ddcd056SLinus Torvalds * We _really_ want to just do "generic_permission()" without 3653ddcd056SLinus Torvalds * even looking at the inode->i_op values. So we keep a cache 3663ddcd056SLinus Torvalds * flag in inode->i_opflags, that says "this has not special 3673ddcd056SLinus Torvalds * permission function, use the fast case". 3683ddcd056SLinus Torvalds */ 3693ddcd056SLinus Torvalds static inline int do_inode_permission(struct inode *inode, int mask) 3703ddcd056SLinus Torvalds { 3713ddcd056SLinus Torvalds if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) { 3723ddcd056SLinus Torvalds if (likely(inode->i_op->permission)) 3733ddcd056SLinus Torvalds return inode->i_op->permission(inode, mask); 3743ddcd056SLinus Torvalds 3753ddcd056SLinus Torvalds /* This gets set once for the inode lifetime */ 3763ddcd056SLinus Torvalds spin_lock(&inode->i_lock); 3773ddcd056SLinus Torvalds inode->i_opflags |= IOP_FASTPERM; 3783ddcd056SLinus Torvalds spin_unlock(&inode->i_lock); 3793ddcd056SLinus Torvalds } 3803ddcd056SLinus Torvalds return generic_permission(inode, mask); 3813ddcd056SLinus Torvalds } 3823ddcd056SLinus Torvalds 383cb23beb5SChristoph Hellwig /** 3840bdaea90SDavid Howells * __inode_permission - Check for access rights to a given inode 3850bdaea90SDavid Howells * @inode: Inode to check permission on 3860bdaea90SDavid Howells * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) 387cb23beb5SChristoph Hellwig * 3880bdaea90SDavid Howells * Check for read/write/execute permissions on an inode. 389948409c7SAndreas Gruenbacher * 390948409c7SAndreas Gruenbacher * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask. 3910bdaea90SDavid Howells * 3920bdaea90SDavid Howells * This does not check for a read-only file system. You probably want 3930bdaea90SDavid Howells * inode_permission(). 394cb23beb5SChristoph Hellwig */ 3950bdaea90SDavid Howells int __inode_permission(struct inode *inode, int mask) 3961da177e4SLinus Torvalds { 397e6305c43SAl Viro int retval; 3981da177e4SLinus Torvalds 3993ddcd056SLinus Torvalds if (unlikely(mask & MAY_WRITE)) { 4001da177e4SLinus Torvalds /* 4011da177e4SLinus Torvalds * Nobody gets write access to an immutable file. 4021da177e4SLinus Torvalds */ 4031da177e4SLinus Torvalds if (IS_IMMUTABLE(inode)) 4041da177e4SLinus Torvalds return -EACCES; 4051da177e4SLinus Torvalds } 4061da177e4SLinus Torvalds 4073ddcd056SLinus Torvalds retval = do_inode_permission(inode, mask); 4081da177e4SLinus Torvalds if (retval) 4091da177e4SLinus Torvalds return retval; 4101da177e4SLinus Torvalds 41108ce5f16SSerge E. Hallyn retval = devcgroup_inode_permission(inode, mask); 41208ce5f16SSerge E. Hallyn if (retval) 41308ce5f16SSerge E. Hallyn return retval; 41408ce5f16SSerge E. Hallyn 415d09ca739SEric Paris return security_inode_permission(inode, mask); 4161da177e4SLinus Torvalds } 4171da177e4SLinus Torvalds 418f4d6ff89SAl Viro /** 4190bdaea90SDavid Howells * sb_permission - Check superblock-level permissions 4200bdaea90SDavid Howells * @sb: Superblock of inode to check permission on 42155852635SRandy Dunlap * @inode: Inode to check permission on 4220bdaea90SDavid Howells * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) 4230bdaea90SDavid Howells * 4240bdaea90SDavid Howells * Separate out file-system wide checks from inode-specific permission checks. 4250bdaea90SDavid Howells */ 4260bdaea90SDavid Howells static int sb_permission(struct super_block *sb, struct inode *inode, int mask) 4270bdaea90SDavid Howells { 4280bdaea90SDavid Howells if (unlikely(mask & MAY_WRITE)) { 4290bdaea90SDavid Howells umode_t mode = inode->i_mode; 4300bdaea90SDavid Howells 4310bdaea90SDavid Howells /* Nobody gets write access to a read-only fs. */ 4320bdaea90SDavid Howells if ((sb->s_flags & MS_RDONLY) && 4330bdaea90SDavid Howells (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) 4340bdaea90SDavid Howells return -EROFS; 4350bdaea90SDavid Howells } 4360bdaea90SDavid Howells return 0; 4370bdaea90SDavid Howells } 4380bdaea90SDavid Howells 4390bdaea90SDavid Howells /** 4400bdaea90SDavid Howells * inode_permission - Check for access rights to a given inode 4410bdaea90SDavid Howells * @inode: Inode to check permission on 4420bdaea90SDavid Howells * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) 4430bdaea90SDavid Howells * 4440bdaea90SDavid Howells * Check for read/write/execute permissions on an inode. We use fs[ug]id for 4450bdaea90SDavid Howells * this, letting us set arbitrary permissions for filesystem access without 4460bdaea90SDavid Howells * changing the "normal" UIDs which are used for other things. 4470bdaea90SDavid Howells * 4480bdaea90SDavid Howells * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask. 4490bdaea90SDavid Howells */ 4500bdaea90SDavid Howells int inode_permission(struct inode *inode, int mask) 4510bdaea90SDavid Howells { 4520bdaea90SDavid Howells int retval; 4530bdaea90SDavid Howells 4540bdaea90SDavid Howells retval = sb_permission(inode->i_sb, inode, mask); 4550bdaea90SDavid Howells if (retval) 4560bdaea90SDavid Howells return retval; 4570bdaea90SDavid Howells return __inode_permission(inode, mask); 4580bdaea90SDavid Howells } 4594d359507SAl Viro EXPORT_SYMBOL(inode_permission); 4600bdaea90SDavid Howells 4610bdaea90SDavid Howells /** 4625dd784d0SJan Blunck * path_get - get a reference to a path 4635dd784d0SJan Blunck * @path: path to get the reference to 4645dd784d0SJan Blunck * 4655dd784d0SJan Blunck * Given a path increment the reference count to the dentry and the vfsmount. 4665dd784d0SJan Blunck */ 467dcf787f3SAl Viro void path_get(const struct path *path) 4685dd784d0SJan Blunck { 4695dd784d0SJan Blunck mntget(path->mnt); 4705dd784d0SJan Blunck dget(path->dentry); 4715dd784d0SJan Blunck } 4725dd784d0SJan Blunck EXPORT_SYMBOL(path_get); 4735dd784d0SJan Blunck 4745dd784d0SJan Blunck /** 4751d957f9bSJan Blunck * path_put - put a reference to a path 4761d957f9bSJan Blunck * @path: path to put the reference to 4771d957f9bSJan Blunck * 4781d957f9bSJan Blunck * Given a path decrement the reference count to the dentry and the vfsmount. 4791d957f9bSJan Blunck */ 480dcf787f3SAl Viro void path_put(const struct path *path) 4811da177e4SLinus Torvalds { 4821d957f9bSJan Blunck dput(path->dentry); 4831d957f9bSJan Blunck mntput(path->mnt); 4841da177e4SLinus Torvalds } 4851d957f9bSJan Blunck EXPORT_SYMBOL(path_put); 4861da177e4SLinus Torvalds 48719660af7SAl Viro /* 48831e6b01fSNick Piggin * Path walking has 2 modes, rcu-walk and ref-walk (see 48919660af7SAl Viro * Documentation/filesystems/path-lookup.txt). In situations when we can't 49019660af7SAl Viro * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab 49119660af7SAl Viro * normal reference counts on dentries and vfsmounts to transition to rcu-walk 49219660af7SAl Viro * mode. Refcounts are grabbed at the last known good point before rcu-walk 49319660af7SAl Viro * got stuck, so ref-walk may continue from there. If this is not successful 49419660af7SAl Viro * (eg. a seqcount has changed), then failure is returned and it's up to caller 49519660af7SAl Viro * to restart the path walk from the beginning in ref-walk mode. 49631e6b01fSNick Piggin */ 49731e6b01fSNick Piggin 49831e6b01fSNick Piggin /** 49919660af7SAl Viro * unlazy_walk - try to switch to ref-walk mode. 50019660af7SAl Viro * @nd: nameidata pathwalk data 50119660af7SAl Viro * @dentry: child of nd->path.dentry or NULL 50239191628SRandy Dunlap * Returns: 0 on success, -ECHILD on failure 50331e6b01fSNick Piggin * 50419660af7SAl Viro * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry 50519660af7SAl Viro * for ref-walk mode. @dentry must be a path found by a do_lookup call on 50619660af7SAl Viro * @nd or NULL. Must be called from rcu-walk context. 50731e6b01fSNick Piggin */ 50819660af7SAl Viro static int unlazy_walk(struct nameidata *nd, struct dentry *dentry) 50931e6b01fSNick Piggin { 51031e6b01fSNick Piggin struct fs_struct *fs = current->fs; 51131e6b01fSNick Piggin struct dentry *parent = nd->path.dentry; 51231e6b01fSNick Piggin 51331e6b01fSNick Piggin BUG_ON(!(nd->flags & LOOKUP_RCU)); 514e5c832d5SLinus Torvalds 515e5c832d5SLinus Torvalds /* 51648a066e7SAl Viro * After legitimizing the bastards, terminate_walk() 51748a066e7SAl Viro * will do the right thing for non-RCU mode, and all our 51848a066e7SAl Viro * subsequent exit cases should rcu_read_unlock() 51948a066e7SAl Viro * before returning. Do vfsmount first; if dentry 52048a066e7SAl Viro * can't be legitimized, just set nd->path.dentry to NULL 52148a066e7SAl Viro * and rely on dput(NULL) being a no-op. 522e5c832d5SLinus Torvalds */ 52348a066e7SAl Viro if (!legitimize_mnt(nd->path.mnt, nd->m_seq)) 524e5c832d5SLinus Torvalds return -ECHILD; 525e5c832d5SLinus Torvalds nd->flags &= ~LOOKUP_RCU; 52615570086SLinus Torvalds 52748a066e7SAl Viro if (!lockref_get_not_dead(&parent->d_lockref)) { 52848a066e7SAl Viro nd->path.dentry = NULL; 529d870b4a1SAl Viro goto out; 53048a066e7SAl Viro } 53148a066e7SAl Viro 53215570086SLinus Torvalds /* 53315570086SLinus Torvalds * For a negative lookup, the lookup sequence point is the parents 53415570086SLinus Torvalds * sequence point, and it only needs to revalidate the parent dentry. 53515570086SLinus Torvalds * 53615570086SLinus Torvalds * For a positive lookup, we need to move both the parent and the 53715570086SLinus Torvalds * dentry from the RCU domain to be properly refcounted. And the 53815570086SLinus Torvalds * sequence number in the dentry validates *both* dentry counters, 53915570086SLinus Torvalds * since we checked the sequence number of the parent after we got 54015570086SLinus Torvalds * the child sequence number. So we know the parent must still 54115570086SLinus Torvalds * be valid if the child sequence number is still valid. 54215570086SLinus Torvalds */ 54319660af7SAl Viro if (!dentry) { 544e5c832d5SLinus Torvalds if (read_seqcount_retry(&parent->d_seq, nd->seq)) 545e5c832d5SLinus Torvalds goto out; 54619660af7SAl Viro BUG_ON(nd->inode != parent->d_inode); 54719660af7SAl Viro } else { 548e5c832d5SLinus Torvalds if (!lockref_get_not_dead(&dentry->d_lockref)) 549e5c832d5SLinus Torvalds goto out; 550e5c832d5SLinus Torvalds if (read_seqcount_retry(&dentry->d_seq, nd->seq)) 551e5c832d5SLinus Torvalds goto drop_dentry; 55219660af7SAl Viro } 553e5c832d5SLinus Torvalds 554e5c832d5SLinus Torvalds /* 555e5c832d5SLinus Torvalds * Sequence counts matched. Now make sure that the root is 556e5c832d5SLinus Torvalds * still valid and get it if required. 557e5c832d5SLinus Torvalds */ 558e5c832d5SLinus Torvalds if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) { 559e5c832d5SLinus Torvalds spin_lock(&fs->lock); 560e5c832d5SLinus Torvalds if (nd->root.mnt != fs->root.mnt || nd->root.dentry != fs->root.dentry) 561e5c832d5SLinus Torvalds goto unlock_and_drop_dentry; 56231e6b01fSNick Piggin path_get(&nd->root); 56331e6b01fSNick Piggin spin_unlock(&fs->lock); 56431e6b01fSNick Piggin } 56531e6b01fSNick Piggin 5668b61e74fSAl Viro rcu_read_unlock(); 56731e6b01fSNick Piggin return 0; 56819660af7SAl Viro 569e5c832d5SLinus Torvalds unlock_and_drop_dentry: 57031e6b01fSNick Piggin spin_unlock(&fs->lock); 571e5c832d5SLinus Torvalds drop_dentry: 5728b61e74fSAl Viro rcu_read_unlock(); 573e5c832d5SLinus Torvalds dput(dentry); 574d0d27277SLinus Torvalds goto drop_root_mnt; 575e5c832d5SLinus Torvalds out: 5768b61e74fSAl Viro rcu_read_unlock(); 577d0d27277SLinus Torvalds drop_root_mnt: 578d0d27277SLinus Torvalds if (!(nd->flags & LOOKUP_ROOT)) 579d0d27277SLinus Torvalds nd->root.mnt = NULL; 58031e6b01fSNick Piggin return -ECHILD; 58131e6b01fSNick Piggin } 58231e6b01fSNick Piggin 5834ce16ef3SAl Viro static inline int d_revalidate(struct dentry *dentry, unsigned int flags) 58434286d66SNick Piggin { 5854ce16ef3SAl Viro return dentry->d_op->d_revalidate(dentry, flags); 58634286d66SNick Piggin } 58734286d66SNick Piggin 5889f1fafeeSAl Viro /** 5899f1fafeeSAl Viro * complete_walk - successful completion of path walk 5909f1fafeeSAl Viro * @nd: pointer nameidata 59139159de2SJeff Layton * 5929f1fafeeSAl Viro * If we had been in RCU mode, drop out of it and legitimize nd->path. 5939f1fafeeSAl Viro * Revalidate the final result, unless we'd already done that during 5949f1fafeeSAl Viro * the path walk or the filesystem doesn't ask for it. Return 0 on 5959f1fafeeSAl Viro * success, -error on failure. In case of failure caller does not 5969f1fafeeSAl Viro * need to drop nd->path. 59739159de2SJeff Layton */ 5989f1fafeeSAl Viro static int complete_walk(struct nameidata *nd) 59939159de2SJeff Layton { 60016c2cd71SAl Viro struct dentry *dentry = nd->path.dentry; 60139159de2SJeff Layton int status; 60239159de2SJeff Layton 6039f1fafeeSAl Viro if (nd->flags & LOOKUP_RCU) { 6049f1fafeeSAl Viro nd->flags &= ~LOOKUP_RCU; 6059f1fafeeSAl Viro if (!(nd->flags & LOOKUP_ROOT)) 6069f1fafeeSAl Viro nd->root.mnt = NULL; 60715570086SLinus Torvalds 60848a066e7SAl Viro if (!legitimize_mnt(nd->path.mnt, nd->m_seq)) { 6098b61e74fSAl Viro rcu_read_unlock(); 61048a066e7SAl Viro return -ECHILD; 61148a066e7SAl Viro } 612e5c832d5SLinus Torvalds if (unlikely(!lockref_get_not_dead(&dentry->d_lockref))) { 6138b61e74fSAl Viro rcu_read_unlock(); 61448a066e7SAl Viro mntput(nd->path.mnt); 6159f1fafeeSAl Viro return -ECHILD; 6169f1fafeeSAl Viro } 617e5c832d5SLinus Torvalds if (read_seqcount_retry(&dentry->d_seq, nd->seq)) { 6188b61e74fSAl Viro rcu_read_unlock(); 619e5c832d5SLinus Torvalds dput(dentry); 62048a066e7SAl Viro mntput(nd->path.mnt); 621e5c832d5SLinus Torvalds return -ECHILD; 622e5c832d5SLinus Torvalds } 6238b61e74fSAl Viro rcu_read_unlock(); 6249f1fafeeSAl Viro } 6259f1fafeeSAl Viro 62616c2cd71SAl Viro if (likely(!(nd->flags & LOOKUP_JUMPED))) 62739159de2SJeff Layton return 0; 62839159de2SJeff Layton 629ecf3d1f1SJeff Layton if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE))) 63016c2cd71SAl Viro return 0; 63116c2cd71SAl Viro 632ecf3d1f1SJeff Layton status = dentry->d_op->d_weak_revalidate(dentry, nd->flags); 63339159de2SJeff Layton if (status > 0) 63439159de2SJeff Layton return 0; 63539159de2SJeff Layton 63616c2cd71SAl Viro if (!status) 63739159de2SJeff Layton status = -ESTALE; 63816c2cd71SAl Viro 6399f1fafeeSAl Viro path_put(&nd->path); 64039159de2SJeff Layton return status; 64139159de2SJeff Layton } 64239159de2SJeff Layton 6432a737871SAl Viro static __always_inline void set_root(struct nameidata *nd) 6442a737871SAl Viro { 645f7ad3c6bSMiklos Szeredi if (!nd->root.mnt) 646f7ad3c6bSMiklos Szeredi get_fs_root(current->fs, &nd->root); 6472a737871SAl Viro } 6482a737871SAl Viro 6496de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *); 6506de88d72SAl Viro 65131e6b01fSNick Piggin static __always_inline void set_root_rcu(struct nameidata *nd) 65231e6b01fSNick Piggin { 65331e6b01fSNick Piggin if (!nd->root.mnt) { 65431e6b01fSNick Piggin struct fs_struct *fs = current->fs; 655c28cc364SNick Piggin unsigned seq; 656c28cc364SNick Piggin 657c28cc364SNick Piggin do { 658c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 65931e6b01fSNick Piggin nd->root = fs->root; 660c1530019STim Chen nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq); 661c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 66231e6b01fSNick Piggin } 66331e6b01fSNick Piggin } 66431e6b01fSNick Piggin 6651d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd) 666051d3812SIan Kent { 667051d3812SIan Kent dput(path->dentry); 6684ac91378SJan Blunck if (path->mnt != nd->path.mnt) 669051d3812SIan Kent mntput(path->mnt); 670051d3812SIan Kent } 671051d3812SIan Kent 6727b9337aaSNick Piggin static inline void path_to_nameidata(const struct path *path, 6737b9337aaSNick Piggin struct nameidata *nd) 674051d3812SIan Kent { 67531e6b01fSNick Piggin if (!(nd->flags & LOOKUP_RCU)) { 6764ac91378SJan Blunck dput(nd->path.dentry); 67731e6b01fSNick Piggin if (nd->path.mnt != path->mnt) 6784ac91378SJan Blunck mntput(nd->path.mnt); 6799a229683SHuang Shijie } 68031e6b01fSNick Piggin nd->path.mnt = path->mnt; 6814ac91378SJan Blunck nd->path.dentry = path->dentry; 682051d3812SIan Kent } 683051d3812SIan Kent 684b5fb63c1SChristoph Hellwig /* 685b5fb63c1SChristoph Hellwig * Helper to directly jump to a known parsed path from ->follow_link, 686b5fb63c1SChristoph Hellwig * caller must have taken a reference to path beforehand. 687b5fb63c1SChristoph Hellwig */ 688b5fb63c1SChristoph Hellwig void nd_jump_link(struct nameidata *nd, struct path *path) 689b5fb63c1SChristoph Hellwig { 690b5fb63c1SChristoph Hellwig path_put(&nd->path); 691b5fb63c1SChristoph Hellwig 692b5fb63c1SChristoph Hellwig nd->path = *path; 693b5fb63c1SChristoph Hellwig nd->inode = nd->path.dentry->d_inode; 694b5fb63c1SChristoph Hellwig nd->flags |= LOOKUP_JUMPED; 695b5fb63c1SChristoph Hellwig } 696b5fb63c1SChristoph Hellwig 697574197e0SAl Viro static inline void put_link(struct nameidata *nd, struct path *link, void *cookie) 698574197e0SAl Viro { 699574197e0SAl Viro struct inode *inode = link->dentry->d_inode; 7006d7b5aaeSAl Viro if (inode->i_op->put_link) 701574197e0SAl Viro inode->i_op->put_link(link->dentry, nd, cookie); 702574197e0SAl Viro path_put(link); 703574197e0SAl Viro } 704574197e0SAl Viro 705561ec64aSLinus Torvalds int sysctl_protected_symlinks __read_mostly = 0; 706561ec64aSLinus Torvalds int sysctl_protected_hardlinks __read_mostly = 0; 707800179c9SKees Cook 708800179c9SKees Cook /** 709800179c9SKees Cook * may_follow_link - Check symlink following for unsafe situations 710800179c9SKees Cook * @link: The path of the symlink 71155852635SRandy Dunlap * @nd: nameidata pathwalk data 712800179c9SKees Cook * 713800179c9SKees Cook * In the case of the sysctl_protected_symlinks sysctl being enabled, 714800179c9SKees Cook * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is 715800179c9SKees Cook * in a sticky world-writable directory. This is to protect privileged 716800179c9SKees Cook * processes from failing races against path names that may change out 717800179c9SKees Cook * from under them by way of other users creating malicious symlinks. 718800179c9SKees Cook * It will permit symlinks to be followed only when outside a sticky 719800179c9SKees Cook * world-writable directory, or when the uid of the symlink and follower 720800179c9SKees Cook * match, or when the directory owner matches the symlink's owner. 721800179c9SKees Cook * 722800179c9SKees Cook * Returns 0 if following the symlink is allowed, -ve on error. 723800179c9SKees Cook */ 724800179c9SKees Cook static inline int may_follow_link(struct path *link, struct nameidata *nd) 725800179c9SKees Cook { 726800179c9SKees Cook const struct inode *inode; 727800179c9SKees Cook const struct inode *parent; 728800179c9SKees Cook 729800179c9SKees Cook if (!sysctl_protected_symlinks) 730800179c9SKees Cook return 0; 731800179c9SKees Cook 732800179c9SKees Cook /* Allowed if owner and follower match. */ 733800179c9SKees Cook inode = link->dentry->d_inode; 73481abe27bSEric W. Biederman if (uid_eq(current_cred()->fsuid, inode->i_uid)) 735800179c9SKees Cook return 0; 736800179c9SKees Cook 737800179c9SKees Cook /* Allowed if parent directory not sticky and world-writable. */ 738800179c9SKees Cook parent = nd->path.dentry->d_inode; 739800179c9SKees Cook if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH)) 740800179c9SKees Cook return 0; 741800179c9SKees Cook 742800179c9SKees Cook /* Allowed if parent directory and link owner match. */ 74381abe27bSEric W. Biederman if (uid_eq(parent->i_uid, inode->i_uid)) 744800179c9SKees Cook return 0; 745800179c9SKees Cook 746ffd8d101SSasha Levin audit_log_link_denied("follow_link", link); 747800179c9SKees Cook path_put_conditional(link, nd); 748800179c9SKees Cook path_put(&nd->path); 749800179c9SKees Cook return -EACCES; 750800179c9SKees Cook } 751800179c9SKees Cook 752800179c9SKees Cook /** 753800179c9SKees Cook * safe_hardlink_source - Check for safe hardlink conditions 754800179c9SKees Cook * @inode: the source inode to hardlink from 755800179c9SKees Cook * 756800179c9SKees Cook * Return false if at least one of the following conditions: 757800179c9SKees Cook * - inode is not a regular file 758800179c9SKees Cook * - inode is setuid 759800179c9SKees Cook * - inode is setgid and group-exec 760800179c9SKees Cook * - access failure for read and write 761800179c9SKees Cook * 762800179c9SKees Cook * Otherwise returns true. 763800179c9SKees Cook */ 764800179c9SKees Cook static bool safe_hardlink_source(struct inode *inode) 765800179c9SKees Cook { 766800179c9SKees Cook umode_t mode = inode->i_mode; 767800179c9SKees Cook 768800179c9SKees Cook /* Special files should not get pinned to the filesystem. */ 769800179c9SKees Cook if (!S_ISREG(mode)) 770800179c9SKees Cook return false; 771800179c9SKees Cook 772800179c9SKees Cook /* Setuid files should not get pinned to the filesystem. */ 773800179c9SKees Cook if (mode & S_ISUID) 774800179c9SKees Cook return false; 775800179c9SKees Cook 776800179c9SKees Cook /* Executable setgid files should not get pinned to the filesystem. */ 777800179c9SKees Cook if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) 778800179c9SKees Cook return false; 779800179c9SKees Cook 780800179c9SKees Cook /* Hardlinking to unreadable or unwritable sources is dangerous. */ 781800179c9SKees Cook if (inode_permission(inode, MAY_READ | MAY_WRITE)) 782800179c9SKees Cook return false; 783800179c9SKees Cook 784800179c9SKees Cook return true; 785800179c9SKees Cook } 786800179c9SKees Cook 787800179c9SKees Cook /** 788800179c9SKees Cook * may_linkat - Check permissions for creating a hardlink 789800179c9SKees Cook * @link: the source to hardlink from 790800179c9SKees Cook * 791800179c9SKees Cook * Block hardlink when all of: 792800179c9SKees Cook * - sysctl_protected_hardlinks enabled 793800179c9SKees Cook * - fsuid does not match inode 794800179c9SKees Cook * - hardlink source is unsafe (see safe_hardlink_source() above) 795800179c9SKees Cook * - not CAP_FOWNER 796800179c9SKees Cook * 797800179c9SKees Cook * Returns 0 if successful, -ve on error. 798800179c9SKees Cook */ 799800179c9SKees Cook static int may_linkat(struct path *link) 800800179c9SKees Cook { 801800179c9SKees Cook const struct cred *cred; 802800179c9SKees Cook struct inode *inode; 803800179c9SKees Cook 804800179c9SKees Cook if (!sysctl_protected_hardlinks) 805800179c9SKees Cook return 0; 806800179c9SKees Cook 807800179c9SKees Cook cred = current_cred(); 808800179c9SKees Cook inode = link->dentry->d_inode; 809800179c9SKees Cook 810800179c9SKees Cook /* Source inode owner (or CAP_FOWNER) can hardlink all they like, 811800179c9SKees Cook * otherwise, it must be a safe source. 812800179c9SKees Cook */ 81381abe27bSEric W. Biederman if (uid_eq(cred->fsuid, inode->i_uid) || safe_hardlink_source(inode) || 814800179c9SKees Cook capable(CAP_FOWNER)) 815800179c9SKees Cook return 0; 816800179c9SKees Cook 817a51d9eaaSKees Cook audit_log_link_denied("linkat", link); 818800179c9SKees Cook return -EPERM; 819800179c9SKees Cook } 820800179c9SKees Cook 821def4af30SAl Viro static __always_inline int 822574197e0SAl Viro follow_link(struct path *link, struct nameidata *nd, void **p) 8231da177e4SLinus Torvalds { 8247b9337aaSNick Piggin struct dentry *dentry = link->dentry; 8256d7b5aaeSAl Viro int error; 8266d7b5aaeSAl Viro char *s; 8271da177e4SLinus Torvalds 828844a3917SAl Viro BUG_ON(nd->flags & LOOKUP_RCU); 829844a3917SAl Viro 8300e794589SAl Viro if (link->mnt == nd->path.mnt) 8310e794589SAl Viro mntget(link->mnt); 8320e794589SAl Viro 8336d7b5aaeSAl Viro error = -ELOOP; 8346d7b5aaeSAl Viro if (unlikely(current->total_link_count >= 40)) 8356d7b5aaeSAl Viro goto out_put_nd_path; 8366d7b5aaeSAl Viro 837574197e0SAl Viro cond_resched(); 838574197e0SAl Viro current->total_link_count++; 839574197e0SAl Viro 84068ac1234SAl Viro touch_atime(link); 8411da177e4SLinus Torvalds nd_set_link(nd, NULL); 842cd4e91d3SAl Viro 84336f3b4f6SAl Viro error = security_inode_follow_link(link->dentry, nd); 8446d7b5aaeSAl Viro if (error) 8456d7b5aaeSAl Viro goto out_put_nd_path; 84636f3b4f6SAl Viro 84786acdca1SAl Viro nd->last_type = LAST_BIND; 848def4af30SAl Viro *p = dentry->d_inode->i_op->follow_link(dentry, nd); 849def4af30SAl Viro error = PTR_ERR(*p); 8506d7b5aaeSAl Viro if (IS_ERR(*p)) 851408ef013SChristoph Hellwig goto out_put_nd_path; 8526d7b5aaeSAl Viro 853cc314eefSLinus Torvalds error = 0; 8546d7b5aaeSAl Viro s = nd_get_link(nd); 8556d7b5aaeSAl Viro if (s) { 856443ed254SAl Viro if (unlikely(IS_ERR(s))) { 857443ed254SAl Viro path_put(&nd->path); 858443ed254SAl Viro put_link(nd, link, *p); 859443ed254SAl Viro return PTR_ERR(s); 860443ed254SAl Viro } 861443ed254SAl Viro if (*s == '/') { 862443ed254SAl Viro set_root(nd); 863443ed254SAl Viro path_put(&nd->path); 864443ed254SAl Viro nd->path = nd->root; 865443ed254SAl Viro path_get(&nd->root); 866443ed254SAl Viro nd->flags |= LOOKUP_JUMPED; 867443ed254SAl Viro } 868443ed254SAl Viro nd->inode = nd->path.dentry->d_inode; 869443ed254SAl Viro error = link_path_walk(s, nd); 8706d7b5aaeSAl Viro if (unlikely(error)) 8716d7b5aaeSAl Viro put_link(nd, link, *p); 872b5fb63c1SChristoph Hellwig } 8736d7b5aaeSAl Viro 8746d7b5aaeSAl Viro return error; 8756d7b5aaeSAl Viro 8766d7b5aaeSAl Viro out_put_nd_path: 87798f6ef64SArnd Bergmann *p = NULL; 8786d7b5aaeSAl Viro path_put(&nd->path); 8796d7b5aaeSAl Viro path_put(link); 8801da177e4SLinus Torvalds return error; 8811da177e4SLinus Torvalds } 8821da177e4SLinus Torvalds 88331e6b01fSNick Piggin static int follow_up_rcu(struct path *path) 88431e6b01fSNick Piggin { 8850714a533SAl Viro struct mount *mnt = real_mount(path->mnt); 8860714a533SAl Viro struct mount *parent; 88731e6b01fSNick Piggin struct dentry *mountpoint; 88831e6b01fSNick Piggin 8890714a533SAl Viro parent = mnt->mnt_parent; 8900714a533SAl Viro if (&parent->mnt == path->mnt) 89131e6b01fSNick Piggin return 0; 892a73324daSAl Viro mountpoint = mnt->mnt_mountpoint; 89331e6b01fSNick Piggin path->dentry = mountpoint; 8940714a533SAl Viro path->mnt = &parent->mnt; 89531e6b01fSNick Piggin return 1; 89631e6b01fSNick Piggin } 89731e6b01fSNick Piggin 898f015f126SDavid Howells /* 899f015f126SDavid Howells * follow_up - Find the mountpoint of path's vfsmount 900f015f126SDavid Howells * 901f015f126SDavid Howells * Given a path, find the mountpoint of its source file system. 902f015f126SDavid Howells * Replace @path with the path of the mountpoint in the parent mount. 903f015f126SDavid Howells * Up is towards /. 904f015f126SDavid Howells * 905f015f126SDavid Howells * Return 1 if we went up a level and 0 if we were already at the 906f015f126SDavid Howells * root. 907f015f126SDavid Howells */ 908bab77ebfSAl Viro int follow_up(struct path *path) 9091da177e4SLinus Torvalds { 9100714a533SAl Viro struct mount *mnt = real_mount(path->mnt); 9110714a533SAl Viro struct mount *parent; 9121da177e4SLinus Torvalds struct dentry *mountpoint; 91399b7db7bSNick Piggin 91448a066e7SAl Viro read_seqlock_excl(&mount_lock); 9150714a533SAl Viro parent = mnt->mnt_parent; 9163c0a6163SAl Viro if (parent == mnt) { 91748a066e7SAl Viro read_sequnlock_excl(&mount_lock); 9181da177e4SLinus Torvalds return 0; 9191da177e4SLinus Torvalds } 9200714a533SAl Viro mntget(&parent->mnt); 921a73324daSAl Viro mountpoint = dget(mnt->mnt_mountpoint); 92248a066e7SAl Viro read_sequnlock_excl(&mount_lock); 923bab77ebfSAl Viro dput(path->dentry); 924bab77ebfSAl Viro path->dentry = mountpoint; 925bab77ebfSAl Viro mntput(path->mnt); 9260714a533SAl Viro path->mnt = &parent->mnt; 9271da177e4SLinus Torvalds return 1; 9281da177e4SLinus Torvalds } 9294d359507SAl Viro EXPORT_SYMBOL(follow_up); 9301da177e4SLinus Torvalds 931b5c84bf6SNick Piggin /* 9329875cf80SDavid Howells * Perform an automount 9339875cf80SDavid Howells * - return -EISDIR to tell follow_managed() to stop and return the path we 9349875cf80SDavid Howells * were called with. 9351da177e4SLinus Torvalds */ 9369875cf80SDavid Howells static int follow_automount(struct path *path, unsigned flags, 9379875cf80SDavid Howells bool *need_mntput) 93831e6b01fSNick Piggin { 9399875cf80SDavid Howells struct vfsmount *mnt; 940ea5b778aSDavid Howells int err; 9419875cf80SDavid Howells 9429875cf80SDavid Howells if (!path->dentry->d_op || !path->dentry->d_op->d_automount) 9439875cf80SDavid Howells return -EREMOTE; 9449875cf80SDavid Howells 9450ec26fd0SMiklos Szeredi /* We don't want to mount if someone's just doing a stat - 9460ec26fd0SMiklos Szeredi * unless they're stat'ing a directory and appended a '/' to 9470ec26fd0SMiklos Szeredi * the name. 9480ec26fd0SMiklos Szeredi * 9490ec26fd0SMiklos Szeredi * We do, however, want to mount if someone wants to open or 9505a30d8a2SDavid Howells * create a file of any type under the mountpoint, wants to 9510ec26fd0SMiklos Szeredi * traverse through the mountpoint or wants to open the 9520ec26fd0SMiklos Szeredi * mounted directory. Also, autofs may mark negative dentries 9530ec26fd0SMiklos Szeredi * as being automount points. These will need the attentions 9540ec26fd0SMiklos Szeredi * of the daemon to instantiate them before they can be used. 9555a30d8a2SDavid Howells */ 9565a30d8a2SDavid Howells if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY | 957d94c177bSLinus Torvalds LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) && 9585a30d8a2SDavid Howells path->dentry->d_inode) 9599875cf80SDavid Howells return -EISDIR; 9600ec26fd0SMiklos Szeredi 9619875cf80SDavid Howells current->total_link_count++; 9629875cf80SDavid Howells if (current->total_link_count >= 40) 9639875cf80SDavid Howells return -ELOOP; 9649875cf80SDavid Howells 9659875cf80SDavid Howells mnt = path->dentry->d_op->d_automount(path); 9669875cf80SDavid Howells if (IS_ERR(mnt)) { 9679875cf80SDavid Howells /* 9689875cf80SDavid Howells * The filesystem is allowed to return -EISDIR here to indicate 9699875cf80SDavid Howells * it doesn't want to automount. For instance, autofs would do 9709875cf80SDavid Howells * this so that its userspace daemon can mount on this dentry. 9719875cf80SDavid Howells * 9729875cf80SDavid Howells * However, we can only permit this if it's a terminal point in 9739875cf80SDavid Howells * the path being looked up; if it wasn't then the remainder of 9749875cf80SDavid Howells * the path is inaccessible and we should say so. 9759875cf80SDavid Howells */ 97649084c3bSAl Viro if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT)) 9779875cf80SDavid Howells return -EREMOTE; 9789875cf80SDavid Howells return PTR_ERR(mnt); 97931e6b01fSNick Piggin } 980ea5b778aSDavid Howells 9819875cf80SDavid Howells if (!mnt) /* mount collision */ 9829875cf80SDavid Howells return 0; 9839875cf80SDavid Howells 9848aef1884SAl Viro if (!*need_mntput) { 9858aef1884SAl Viro /* lock_mount() may release path->mnt on error */ 9868aef1884SAl Viro mntget(path->mnt); 9878aef1884SAl Viro *need_mntput = true; 9888aef1884SAl Viro } 98919a167afSAl Viro err = finish_automount(mnt, path); 990ea5b778aSDavid Howells 991ea5b778aSDavid Howells switch (err) { 992ea5b778aSDavid Howells case -EBUSY: 993ea5b778aSDavid Howells /* Someone else made a mount here whilst we were busy */ 99419a167afSAl Viro return 0; 995ea5b778aSDavid Howells case 0: 9968aef1884SAl Viro path_put(path); 9979875cf80SDavid Howells path->mnt = mnt; 9989875cf80SDavid Howells path->dentry = dget(mnt->mnt_root); 9999875cf80SDavid Howells return 0; 100019a167afSAl Viro default: 100119a167afSAl Viro return err; 10029875cf80SDavid Howells } 100319a167afSAl Viro 1004ea5b778aSDavid Howells } 10059875cf80SDavid Howells 10069875cf80SDavid Howells /* 10079875cf80SDavid Howells * Handle a dentry that is managed in some way. 1008cc53ce53SDavid Howells * - Flagged for transit management (autofs) 10099875cf80SDavid Howells * - Flagged as mountpoint 10109875cf80SDavid Howells * - Flagged as automount point 10119875cf80SDavid Howells * 10129875cf80SDavid Howells * This may only be called in refwalk mode. 10139875cf80SDavid Howells * 10149875cf80SDavid Howells * Serialization is taken care of in namespace.c 10159875cf80SDavid Howells */ 10169875cf80SDavid Howells static int follow_managed(struct path *path, unsigned flags) 10179875cf80SDavid Howells { 10188aef1884SAl Viro struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */ 10199875cf80SDavid Howells unsigned managed; 10209875cf80SDavid Howells bool need_mntput = false; 10218aef1884SAl Viro int ret = 0; 10229875cf80SDavid Howells 10239875cf80SDavid Howells /* Given that we're not holding a lock here, we retain the value in a 10249875cf80SDavid Howells * local variable for each dentry as we look at it so that we don't see 10259875cf80SDavid Howells * the components of that value change under us */ 10269875cf80SDavid Howells while (managed = ACCESS_ONCE(path->dentry->d_flags), 10279875cf80SDavid Howells managed &= DCACHE_MANAGED_DENTRY, 10289875cf80SDavid Howells unlikely(managed != 0)) { 1029cc53ce53SDavid Howells /* Allow the filesystem to manage the transit without i_mutex 1030cc53ce53SDavid Howells * being held. */ 1031cc53ce53SDavid Howells if (managed & DCACHE_MANAGE_TRANSIT) { 1032cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op); 1033cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op->d_manage); 10341aed3e42SAl Viro ret = path->dentry->d_op->d_manage(path->dentry, false); 1035cc53ce53SDavid Howells if (ret < 0) 10368aef1884SAl Viro break; 1037cc53ce53SDavid Howells } 1038cc53ce53SDavid Howells 10399875cf80SDavid Howells /* Transit to a mounted filesystem. */ 10409875cf80SDavid Howells if (managed & DCACHE_MOUNTED) { 10419875cf80SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 10429875cf80SDavid Howells if (mounted) { 10439875cf80SDavid Howells dput(path->dentry); 10449875cf80SDavid Howells if (need_mntput) 1045463ffb2eSAl Viro mntput(path->mnt); 1046463ffb2eSAl Viro path->mnt = mounted; 1047463ffb2eSAl Viro path->dentry = dget(mounted->mnt_root); 10489875cf80SDavid Howells need_mntput = true; 10499875cf80SDavid Howells continue; 1050463ffb2eSAl Viro } 1051463ffb2eSAl Viro 10529875cf80SDavid Howells /* Something is mounted on this dentry in another 10539875cf80SDavid Howells * namespace and/or whatever was mounted there in this 105448a066e7SAl Viro * namespace got unmounted before lookup_mnt() could 105548a066e7SAl Viro * get it */ 10561da177e4SLinus Torvalds } 10579875cf80SDavid Howells 10589875cf80SDavid Howells /* Handle an automount point */ 10599875cf80SDavid Howells if (managed & DCACHE_NEED_AUTOMOUNT) { 10609875cf80SDavid Howells ret = follow_automount(path, flags, &need_mntput); 10619875cf80SDavid Howells if (ret < 0) 10628aef1884SAl Viro break; 10639875cf80SDavid Howells continue; 10649875cf80SDavid Howells } 10659875cf80SDavid Howells 10669875cf80SDavid Howells /* We didn't change the current path point */ 10679875cf80SDavid Howells break; 10689875cf80SDavid Howells } 10698aef1884SAl Viro 10708aef1884SAl Viro if (need_mntput && path->mnt == mnt) 10718aef1884SAl Viro mntput(path->mnt); 10728aef1884SAl Viro if (ret == -EISDIR) 10738aef1884SAl Viro ret = 0; 1074a3fbbde7SAl Viro return ret < 0 ? ret : need_mntput; 10751da177e4SLinus Torvalds } 10761da177e4SLinus Torvalds 1077cc53ce53SDavid Howells int follow_down_one(struct path *path) 10781da177e4SLinus Torvalds { 10791da177e4SLinus Torvalds struct vfsmount *mounted; 10801da177e4SLinus Torvalds 10811c755af4SAl Viro mounted = lookup_mnt(path); 10821da177e4SLinus Torvalds if (mounted) { 10839393bd07SAl Viro dput(path->dentry); 10849393bd07SAl Viro mntput(path->mnt); 10859393bd07SAl Viro path->mnt = mounted; 10869393bd07SAl Viro path->dentry = dget(mounted->mnt_root); 10871da177e4SLinus Torvalds return 1; 10881da177e4SLinus Torvalds } 10891da177e4SLinus Torvalds return 0; 10901da177e4SLinus Torvalds } 10914d359507SAl Viro EXPORT_SYMBOL(follow_down_one); 10921da177e4SLinus Torvalds 109362a7375eSIan Kent static inline bool managed_dentry_might_block(struct dentry *dentry) 109462a7375eSIan Kent { 109562a7375eSIan Kent return (dentry->d_flags & DCACHE_MANAGE_TRANSIT && 109662a7375eSIan Kent dentry->d_op->d_manage(dentry, true) < 0); 109762a7375eSIan Kent } 109862a7375eSIan Kent 10999875cf80SDavid Howells /* 1100287548e4SAl Viro * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if 1101287548e4SAl Viro * we meet a managed dentry that would need blocking. 11029875cf80SDavid Howells */ 11039875cf80SDavid Howells static bool __follow_mount_rcu(struct nameidata *nd, struct path *path, 1104287548e4SAl Viro struct inode **inode) 11059875cf80SDavid Howells { 110662a7375eSIan Kent for (;;) { 1107c7105365SAl Viro struct mount *mounted; 110862a7375eSIan Kent /* 110962a7375eSIan Kent * Don't forget we might have a non-mountpoint managed dentry 111062a7375eSIan Kent * that wants to block transit. 111162a7375eSIan Kent */ 1112287548e4SAl Viro if (unlikely(managed_dentry_might_block(path->dentry))) 1113ab90911fSDavid Howells return false; 111462a7375eSIan Kent 111562a7375eSIan Kent if (!d_mountpoint(path->dentry)) 1116b37199e6SAl Viro return true; 111762a7375eSIan Kent 1118474279dcSAl Viro mounted = __lookup_mnt(path->mnt, path->dentry); 11199875cf80SDavid Howells if (!mounted) 11209875cf80SDavid Howells break; 1121c7105365SAl Viro path->mnt = &mounted->mnt; 1122c7105365SAl Viro path->dentry = mounted->mnt.mnt_root; 1123a3fbbde7SAl Viro nd->flags |= LOOKUP_JUMPED; 11249875cf80SDavid Howells nd->seq = read_seqcount_begin(&path->dentry->d_seq); 112559430262SLinus Torvalds /* 112659430262SLinus Torvalds * Update the inode too. We don't need to re-check the 112759430262SLinus Torvalds * dentry sequence number here after this d_inode read, 112859430262SLinus Torvalds * because a mount-point is always pinned. 112959430262SLinus Torvalds */ 113059430262SLinus Torvalds *inode = path->dentry->d_inode; 11319875cf80SDavid Howells } 1132b37199e6SAl Viro return read_seqretry(&mount_lock, nd->m_seq); 1133287548e4SAl Viro } 1134287548e4SAl Viro 113531e6b01fSNick Piggin static int follow_dotdot_rcu(struct nameidata *nd) 113631e6b01fSNick Piggin { 113731e6b01fSNick Piggin set_root_rcu(nd); 113831e6b01fSNick Piggin 113931e6b01fSNick Piggin while (1) { 114031e6b01fSNick Piggin if (nd->path.dentry == nd->root.dentry && 114131e6b01fSNick Piggin nd->path.mnt == nd->root.mnt) { 114231e6b01fSNick Piggin break; 114331e6b01fSNick Piggin } 114431e6b01fSNick Piggin if (nd->path.dentry != nd->path.mnt->mnt_root) { 114531e6b01fSNick Piggin struct dentry *old = nd->path.dentry; 114631e6b01fSNick Piggin struct dentry *parent = old->d_parent; 114731e6b01fSNick Piggin unsigned seq; 114831e6b01fSNick Piggin 114931e6b01fSNick Piggin seq = read_seqcount_begin(&parent->d_seq); 115031e6b01fSNick Piggin if (read_seqcount_retry(&old->d_seq, nd->seq)) 1151ef7562d5SAl Viro goto failed; 115231e6b01fSNick Piggin nd->path.dentry = parent; 115331e6b01fSNick Piggin nd->seq = seq; 115431e6b01fSNick Piggin break; 115531e6b01fSNick Piggin } 115631e6b01fSNick Piggin if (!follow_up_rcu(&nd->path)) 115731e6b01fSNick Piggin break; 115831e6b01fSNick Piggin nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); 115931e6b01fSNick Piggin } 1160b37199e6SAl Viro while (d_mountpoint(nd->path.dentry)) { 1161b37199e6SAl Viro struct mount *mounted; 1162b37199e6SAl Viro mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry); 1163b37199e6SAl Viro if (!mounted) 1164b37199e6SAl Viro break; 1165b37199e6SAl Viro nd->path.mnt = &mounted->mnt; 1166b37199e6SAl Viro nd->path.dentry = mounted->mnt.mnt_root; 1167b37199e6SAl Viro nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); 1168b37199e6SAl Viro if (!read_seqretry(&mount_lock, nd->m_seq)) 1169b37199e6SAl Viro goto failed; 1170b37199e6SAl Viro } 1171dea39376SAl Viro nd->inode = nd->path.dentry->d_inode; 117231e6b01fSNick Piggin return 0; 1173ef7562d5SAl Viro 1174ef7562d5SAl Viro failed: 1175ef7562d5SAl Viro nd->flags &= ~LOOKUP_RCU; 11765b6ca027SAl Viro if (!(nd->flags & LOOKUP_ROOT)) 1177ef7562d5SAl Viro nd->root.mnt = NULL; 11788b61e74fSAl Viro rcu_read_unlock(); 1179ef7562d5SAl Viro return -ECHILD; 118031e6b01fSNick Piggin } 118131e6b01fSNick Piggin 11829875cf80SDavid Howells /* 1183cc53ce53SDavid Howells * Follow down to the covering mount currently visible to userspace. At each 1184cc53ce53SDavid Howells * point, the filesystem owning that dentry may be queried as to whether the 1185cc53ce53SDavid Howells * caller is permitted to proceed or not. 1186cc53ce53SDavid Howells */ 11877cc90cc3SAl Viro int follow_down(struct path *path) 1188cc53ce53SDavid Howells { 1189cc53ce53SDavid Howells unsigned managed; 1190cc53ce53SDavid Howells int ret; 1191cc53ce53SDavid Howells 1192cc53ce53SDavid Howells while (managed = ACCESS_ONCE(path->dentry->d_flags), 1193cc53ce53SDavid Howells unlikely(managed & DCACHE_MANAGED_DENTRY)) { 1194cc53ce53SDavid Howells /* Allow the filesystem to manage the transit without i_mutex 1195cc53ce53SDavid Howells * being held. 1196cc53ce53SDavid Howells * 1197cc53ce53SDavid Howells * We indicate to the filesystem if someone is trying to mount 1198cc53ce53SDavid Howells * something here. This gives autofs the chance to deny anyone 1199cc53ce53SDavid Howells * other than its daemon the right to mount on its 1200cc53ce53SDavid Howells * superstructure. 1201cc53ce53SDavid Howells * 1202cc53ce53SDavid Howells * The filesystem may sleep at this point. 1203cc53ce53SDavid Howells */ 1204cc53ce53SDavid Howells if (managed & DCACHE_MANAGE_TRANSIT) { 1205cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op); 1206cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op->d_manage); 1207ab90911fSDavid Howells ret = path->dentry->d_op->d_manage( 12081aed3e42SAl Viro path->dentry, false); 1209cc53ce53SDavid Howells if (ret < 0) 1210cc53ce53SDavid Howells return ret == -EISDIR ? 0 : ret; 1211cc53ce53SDavid Howells } 1212cc53ce53SDavid Howells 1213cc53ce53SDavid Howells /* Transit to a mounted filesystem. */ 1214cc53ce53SDavid Howells if (managed & DCACHE_MOUNTED) { 1215cc53ce53SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 1216cc53ce53SDavid Howells if (!mounted) 1217cc53ce53SDavid Howells break; 1218cc53ce53SDavid Howells dput(path->dentry); 1219cc53ce53SDavid Howells mntput(path->mnt); 1220cc53ce53SDavid Howells path->mnt = mounted; 1221cc53ce53SDavid Howells path->dentry = dget(mounted->mnt_root); 1222cc53ce53SDavid Howells continue; 1223cc53ce53SDavid Howells } 1224cc53ce53SDavid Howells 1225cc53ce53SDavid Howells /* Don't handle automount points here */ 1226cc53ce53SDavid Howells break; 1227cc53ce53SDavid Howells } 1228cc53ce53SDavid Howells return 0; 1229cc53ce53SDavid Howells } 12304d359507SAl Viro EXPORT_SYMBOL(follow_down); 1231cc53ce53SDavid Howells 1232cc53ce53SDavid Howells /* 12339875cf80SDavid Howells * Skip to top of mountpoint pile in refwalk mode for follow_dotdot() 12349875cf80SDavid Howells */ 12359875cf80SDavid Howells static void follow_mount(struct path *path) 12369875cf80SDavid Howells { 12379875cf80SDavid Howells while (d_mountpoint(path->dentry)) { 12389875cf80SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 12399875cf80SDavid Howells if (!mounted) 12409875cf80SDavid Howells break; 12419875cf80SDavid Howells dput(path->dentry); 12429875cf80SDavid Howells mntput(path->mnt); 12439875cf80SDavid Howells path->mnt = mounted; 12449875cf80SDavid Howells path->dentry = dget(mounted->mnt_root); 12459875cf80SDavid Howells } 12469875cf80SDavid Howells } 12479875cf80SDavid Howells 124831e6b01fSNick Piggin static void follow_dotdot(struct nameidata *nd) 12491da177e4SLinus Torvalds { 12502a737871SAl Viro set_root(nd); 1251e518ddb7SAndreas Mohr 12521da177e4SLinus Torvalds while(1) { 12534ac91378SJan Blunck struct dentry *old = nd->path.dentry; 12541da177e4SLinus Torvalds 12552a737871SAl Viro if (nd->path.dentry == nd->root.dentry && 12562a737871SAl Viro nd->path.mnt == nd->root.mnt) { 12571da177e4SLinus Torvalds break; 12581da177e4SLinus Torvalds } 12594ac91378SJan Blunck if (nd->path.dentry != nd->path.mnt->mnt_root) { 12603088dd70SAl Viro /* rare case of legitimate dget_parent()... */ 12613088dd70SAl Viro nd->path.dentry = dget_parent(nd->path.dentry); 12621da177e4SLinus Torvalds dput(old); 12631da177e4SLinus Torvalds break; 12641da177e4SLinus Torvalds } 12653088dd70SAl Viro if (!follow_up(&nd->path)) 12661da177e4SLinus Torvalds break; 12671da177e4SLinus Torvalds } 126879ed0226SAl Viro follow_mount(&nd->path); 126931e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 12701da177e4SLinus Torvalds } 12711da177e4SLinus Torvalds 12721da177e4SLinus Torvalds /* 1273bad61189SMiklos Szeredi * This looks up the name in dcache, possibly revalidates the old dentry and 1274bad61189SMiklos Szeredi * allocates a new one if not found or not valid. In the need_lookup argument 1275bad61189SMiklos Szeredi * returns whether i_op->lookup is necessary. 1276bad61189SMiklos Szeredi * 1277bad61189SMiklos Szeredi * dir->d_inode->i_mutex must be held 1278baa03890SNick Piggin */ 1279bad61189SMiklos Szeredi static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir, 1280201f956eSAl Viro unsigned int flags, bool *need_lookup) 1281baa03890SNick Piggin { 1282baa03890SNick Piggin struct dentry *dentry; 1283bad61189SMiklos Szeredi int error; 1284baa03890SNick Piggin 1285bad61189SMiklos Szeredi *need_lookup = false; 1286bad61189SMiklos Szeredi dentry = d_lookup(dir, name); 1287bad61189SMiklos Szeredi if (dentry) { 128839e3c955SJeff Layton if (dentry->d_flags & DCACHE_OP_REVALIDATE) { 1289201f956eSAl Viro error = d_revalidate(dentry, flags); 1290bad61189SMiklos Szeredi if (unlikely(error <= 0)) { 1291bad61189SMiklos Szeredi if (error < 0) { 1292bad61189SMiklos Szeredi dput(dentry); 1293bad61189SMiklos Szeredi return ERR_PTR(error); 1294bad61189SMiklos Szeredi } else if (!d_invalidate(dentry)) { 1295bad61189SMiklos Szeredi dput(dentry); 1296bad61189SMiklos Szeredi dentry = NULL; 1297bad61189SMiklos Szeredi } 1298bad61189SMiklos Szeredi } 1299bad61189SMiklos Szeredi } 1300bad61189SMiklos Szeredi } 1301baa03890SNick Piggin 1302bad61189SMiklos Szeredi if (!dentry) { 1303bad61189SMiklos Szeredi dentry = d_alloc(dir, name); 1304baa03890SNick Piggin if (unlikely(!dentry)) 1305baa03890SNick Piggin return ERR_PTR(-ENOMEM); 1306baa03890SNick Piggin 1307bad61189SMiklos Szeredi *need_lookup = true; 1308baa03890SNick Piggin } 1309baa03890SNick Piggin return dentry; 1310baa03890SNick Piggin } 1311baa03890SNick Piggin 1312baa03890SNick Piggin /* 131313a2c3beSJ. Bruce Fields * Call i_op->lookup on the dentry. The dentry must be negative and 131413a2c3beSJ. Bruce Fields * unhashed. 1315bad61189SMiklos Szeredi * 1316bad61189SMiklos Szeredi * dir->d_inode->i_mutex must be held 131744396f4bSJosef Bacik */ 1318bad61189SMiklos Szeredi static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry, 131972bd866aSAl Viro unsigned int flags) 132044396f4bSJosef Bacik { 132144396f4bSJosef Bacik struct dentry *old; 132244396f4bSJosef Bacik 132344396f4bSJosef Bacik /* Don't create child dentry for a dead directory. */ 1324bad61189SMiklos Szeredi if (unlikely(IS_DEADDIR(dir))) { 1325e188dc02SMiklos Szeredi dput(dentry); 132644396f4bSJosef Bacik return ERR_PTR(-ENOENT); 1327e188dc02SMiklos Szeredi } 132844396f4bSJosef Bacik 132972bd866aSAl Viro old = dir->i_op->lookup(dir, dentry, flags); 133044396f4bSJosef Bacik if (unlikely(old)) { 133144396f4bSJosef Bacik dput(dentry); 133244396f4bSJosef Bacik dentry = old; 133344396f4bSJosef Bacik } 133444396f4bSJosef Bacik return dentry; 133544396f4bSJosef Bacik } 133644396f4bSJosef Bacik 1337a3255546SAl Viro static struct dentry *__lookup_hash(struct qstr *name, 133872bd866aSAl Viro struct dentry *base, unsigned int flags) 1339a3255546SAl Viro { 1340bad61189SMiklos Szeredi bool need_lookup; 1341a3255546SAl Viro struct dentry *dentry; 1342a3255546SAl Viro 134372bd866aSAl Viro dentry = lookup_dcache(name, base, flags, &need_lookup); 1344bad61189SMiklos Szeredi if (!need_lookup) 1345a3255546SAl Viro return dentry; 1346bad61189SMiklos Szeredi 134772bd866aSAl Viro return lookup_real(base->d_inode, dentry, flags); 1348a3255546SAl Viro } 1349a3255546SAl Viro 135044396f4bSJosef Bacik /* 13511da177e4SLinus Torvalds * It's more convoluted than I'd like it to be, but... it's still fairly 13521da177e4SLinus Torvalds * small and for now I'd prefer to have fast path as straight as possible. 13531da177e4SLinus Torvalds * It _is_ time-critical. 13541da177e4SLinus Torvalds */ 1355e97cdc87SAl Viro static int lookup_fast(struct nameidata *nd, 135631e6b01fSNick Piggin struct path *path, struct inode **inode) 13571da177e4SLinus Torvalds { 13584ac91378SJan Blunck struct vfsmount *mnt = nd->path.mnt; 135931e6b01fSNick Piggin struct dentry *dentry, *parent = nd->path.dentry; 13605a18fff2SAl Viro int need_reval = 1; 13615a18fff2SAl Viro int status = 1; 13629875cf80SDavid Howells int err; 13639875cf80SDavid Howells 13643cac260aSAl Viro /* 1365b04f784eSNick Piggin * Rename seqlock is not required here because in the off chance 1366b04f784eSNick Piggin * of a false negative due to a concurrent rename, we're going to 1367b04f784eSNick Piggin * do the non-racy lookup, below. 1368b04f784eSNick Piggin */ 136931e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) { 137031e6b01fSNick Piggin unsigned seq; 1371da53be12SLinus Torvalds dentry = __d_lookup_rcu(parent, &nd->last, &seq); 13725a18fff2SAl Viro if (!dentry) 13735a18fff2SAl Viro goto unlazy; 13745a18fff2SAl Viro 137512f8ad4bSLinus Torvalds /* 137612f8ad4bSLinus Torvalds * This sequence count validates that the inode matches 137712f8ad4bSLinus Torvalds * the dentry name information from lookup. 137812f8ad4bSLinus Torvalds */ 137912f8ad4bSLinus Torvalds *inode = dentry->d_inode; 138012f8ad4bSLinus Torvalds if (read_seqcount_retry(&dentry->d_seq, seq)) 138112f8ad4bSLinus Torvalds return -ECHILD; 138212f8ad4bSLinus Torvalds 138312f8ad4bSLinus Torvalds /* 138412f8ad4bSLinus Torvalds * This sequence count validates that the parent had no 138512f8ad4bSLinus Torvalds * changes while we did the lookup of the dentry above. 138612f8ad4bSLinus Torvalds * 138712f8ad4bSLinus Torvalds * The memory barrier in read_seqcount_begin of child is 138812f8ad4bSLinus Torvalds * enough, we can use __read_seqcount_retry here. 138912f8ad4bSLinus Torvalds */ 139031e6b01fSNick Piggin if (__read_seqcount_retry(&parent->d_seq, nd->seq)) 139131e6b01fSNick Piggin return -ECHILD; 139231e6b01fSNick Piggin nd->seq = seq; 13935a18fff2SAl Viro 139424643087SAl Viro if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) { 13954ce16ef3SAl Viro status = d_revalidate(dentry, nd->flags); 13965a18fff2SAl Viro if (unlikely(status <= 0)) { 13975a18fff2SAl Viro if (status != -ECHILD) 13985a18fff2SAl Viro need_reval = 0; 13995a18fff2SAl Viro goto unlazy; 14005a18fff2SAl Viro } 140124643087SAl Viro } 140231e6b01fSNick Piggin path->mnt = mnt; 140331e6b01fSNick Piggin path->dentry = dentry; 1404d6e9bd25SAl Viro if (unlikely(!__follow_mount_rcu(nd, path, inode))) 1405d6e9bd25SAl Viro goto unlazy; 1406d6e9bd25SAl Viro if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT)) 1407d6e9bd25SAl Viro goto unlazy; 14089875cf80SDavid Howells return 0; 14095a18fff2SAl Viro unlazy: 141019660af7SAl Viro if (unlazy_walk(nd, dentry)) 14115a18fff2SAl Viro return -ECHILD; 14125a18fff2SAl Viro } else { 1413e97cdc87SAl Viro dentry = __d_lookup(parent, &nd->last); 141424643087SAl Viro } 14155a18fff2SAl Viro 141681e6f520SAl Viro if (unlikely(!dentry)) 141781e6f520SAl Viro goto need_lookup; 14185a18fff2SAl Viro 14195a18fff2SAl Viro if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval) 14204ce16ef3SAl Viro status = d_revalidate(dentry, nd->flags); 14215a18fff2SAl Viro if (unlikely(status <= 0)) { 14225a18fff2SAl Viro if (status < 0) { 14235a18fff2SAl Viro dput(dentry); 14245a18fff2SAl Viro return status; 14255a18fff2SAl Viro } 14265a18fff2SAl Viro if (!d_invalidate(dentry)) { 14275a18fff2SAl Viro dput(dentry); 142881e6f520SAl Viro goto need_lookup; 14295a18fff2SAl Viro } 14305a18fff2SAl Viro } 1431697f514dSMiklos Szeredi 14321da177e4SLinus Torvalds path->mnt = mnt; 14331da177e4SLinus Torvalds path->dentry = dentry; 14349875cf80SDavid Howells err = follow_managed(path, nd->flags); 143589312214SIan Kent if (unlikely(err < 0)) { 143689312214SIan Kent path_put_conditional(path, nd); 14379875cf80SDavid Howells return err; 143889312214SIan Kent } 1439a3fbbde7SAl Viro if (err) 1440a3fbbde7SAl Viro nd->flags |= LOOKUP_JUMPED; 144131e6b01fSNick Piggin *inode = path->dentry->d_inode; 14421da177e4SLinus Torvalds return 0; 144381e6f520SAl Viro 144481e6f520SAl Viro need_lookup: 1445697f514dSMiklos Szeredi return 1; 1446697f514dSMiklos Szeredi } 1447697f514dSMiklos Szeredi 1448697f514dSMiklos Szeredi /* Fast lookup failed, do it the slow way */ 1449cc2a5271SAl Viro static int lookup_slow(struct nameidata *nd, struct path *path) 1450697f514dSMiklos Szeredi { 1451697f514dSMiklos Szeredi struct dentry *dentry, *parent; 1452697f514dSMiklos Szeredi int err; 1453697f514dSMiklos Szeredi 1454697f514dSMiklos Szeredi parent = nd->path.dentry; 145581e6f520SAl Viro BUG_ON(nd->inode != parent->d_inode); 145681e6f520SAl Viro 145781e6f520SAl Viro mutex_lock(&parent->d_inode->i_mutex); 1458cc2a5271SAl Viro dentry = __lookup_hash(&nd->last, parent, nd->flags); 145981e6f520SAl Viro mutex_unlock(&parent->d_inode->i_mutex); 146081e6f520SAl Viro if (IS_ERR(dentry)) 146181e6f520SAl Viro return PTR_ERR(dentry); 1462697f514dSMiklos Szeredi path->mnt = nd->path.mnt; 1463697f514dSMiklos Szeredi path->dentry = dentry; 1464697f514dSMiklos Szeredi err = follow_managed(path, nd->flags); 1465697f514dSMiklos Szeredi if (unlikely(err < 0)) { 1466697f514dSMiklos Szeredi path_put_conditional(path, nd); 1467697f514dSMiklos Szeredi return err; 1468697f514dSMiklos Szeredi } 1469697f514dSMiklos Szeredi if (err) 1470697f514dSMiklos Szeredi nd->flags |= LOOKUP_JUMPED; 1471697f514dSMiklos Szeredi return 0; 14721da177e4SLinus Torvalds } 14731da177e4SLinus Torvalds 147452094c8aSAl Viro static inline int may_lookup(struct nameidata *nd) 147552094c8aSAl Viro { 147652094c8aSAl Viro if (nd->flags & LOOKUP_RCU) { 14774ad5abb3SAl Viro int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK); 147852094c8aSAl Viro if (err != -ECHILD) 147952094c8aSAl Viro return err; 148019660af7SAl Viro if (unlazy_walk(nd, NULL)) 148152094c8aSAl Viro return -ECHILD; 148252094c8aSAl Viro } 14834ad5abb3SAl Viro return inode_permission(nd->inode, MAY_EXEC); 148452094c8aSAl Viro } 148552094c8aSAl Viro 14869856fa1bSAl Viro static inline int handle_dots(struct nameidata *nd, int type) 14879856fa1bSAl Viro { 14889856fa1bSAl Viro if (type == LAST_DOTDOT) { 14899856fa1bSAl Viro if (nd->flags & LOOKUP_RCU) { 14909856fa1bSAl Viro if (follow_dotdot_rcu(nd)) 14919856fa1bSAl Viro return -ECHILD; 14929856fa1bSAl Viro } else 14939856fa1bSAl Viro follow_dotdot(nd); 14949856fa1bSAl Viro } 14959856fa1bSAl Viro return 0; 14969856fa1bSAl Viro } 14979856fa1bSAl Viro 1498951361f9SAl Viro static void terminate_walk(struct nameidata *nd) 1499951361f9SAl Viro { 1500951361f9SAl Viro if (!(nd->flags & LOOKUP_RCU)) { 1501951361f9SAl Viro path_put(&nd->path); 1502951361f9SAl Viro } else { 1503951361f9SAl Viro nd->flags &= ~LOOKUP_RCU; 15045b6ca027SAl Viro if (!(nd->flags & LOOKUP_ROOT)) 1505951361f9SAl Viro nd->root.mnt = NULL; 15068b61e74fSAl Viro rcu_read_unlock(); 1507951361f9SAl Viro } 1508951361f9SAl Viro } 1509951361f9SAl Viro 15103ddcd056SLinus Torvalds /* 15113ddcd056SLinus Torvalds * Do we need to follow links? We _really_ want to be able 15123ddcd056SLinus Torvalds * to do this check without having to look at inode->i_op, 15133ddcd056SLinus Torvalds * so we keep a cache of "no, this doesn't need follow_link" 15143ddcd056SLinus Torvalds * for the common case. 15153ddcd056SLinus Torvalds */ 1516b18825a7SDavid Howells static inline int should_follow_link(struct dentry *dentry, int follow) 15173ddcd056SLinus Torvalds { 1518b18825a7SDavid Howells return unlikely(d_is_symlink(dentry)) ? follow : 0; 15193ddcd056SLinus Torvalds } 15203ddcd056SLinus Torvalds 1521ce57dfc1SAl Viro static inline int walk_component(struct nameidata *nd, struct path *path, 152221b9b073SAl Viro int follow) 1523ce57dfc1SAl Viro { 1524ce57dfc1SAl Viro struct inode *inode; 1525ce57dfc1SAl Viro int err; 1526ce57dfc1SAl Viro /* 1527ce57dfc1SAl Viro * "." and ".." are special - ".." especially so because it has 1528ce57dfc1SAl Viro * to be able to know about the current root directory and 1529ce57dfc1SAl Viro * parent relationships. 1530ce57dfc1SAl Viro */ 153121b9b073SAl Viro if (unlikely(nd->last_type != LAST_NORM)) 153221b9b073SAl Viro return handle_dots(nd, nd->last_type); 1533e97cdc87SAl Viro err = lookup_fast(nd, path, &inode); 1534ce57dfc1SAl Viro if (unlikely(err)) { 1535697f514dSMiklos Szeredi if (err < 0) 1536697f514dSMiklos Szeredi goto out_err; 1537697f514dSMiklos Szeredi 1538cc2a5271SAl Viro err = lookup_slow(nd, path); 1539697f514dSMiklos Szeredi if (err < 0) 1540697f514dSMiklos Szeredi goto out_err; 1541697f514dSMiklos Szeredi 1542697f514dSMiklos Szeredi inode = path->dentry->d_inode; 1543ce57dfc1SAl Viro } 1544697f514dSMiklos Szeredi err = -ENOENT; 1545697f514dSMiklos Szeredi if (!inode) 1546697f514dSMiklos Szeredi goto out_path_put; 1547697f514dSMiklos Szeredi 1548b18825a7SDavid Howells if (should_follow_link(path->dentry, follow)) { 154919660af7SAl Viro if (nd->flags & LOOKUP_RCU) { 155019660af7SAl Viro if (unlikely(unlazy_walk(nd, path->dentry))) { 1551697f514dSMiklos Szeredi err = -ECHILD; 1552697f514dSMiklos Szeredi goto out_err; 155319660af7SAl Viro } 155419660af7SAl Viro } 1555ce57dfc1SAl Viro BUG_ON(inode != path->dentry->d_inode); 1556ce57dfc1SAl Viro return 1; 1557ce57dfc1SAl Viro } 1558ce57dfc1SAl Viro path_to_nameidata(path, nd); 1559ce57dfc1SAl Viro nd->inode = inode; 1560ce57dfc1SAl Viro return 0; 1561697f514dSMiklos Szeredi 1562697f514dSMiklos Szeredi out_path_put: 1563697f514dSMiklos Szeredi path_to_nameidata(path, nd); 1564697f514dSMiklos Szeredi out_err: 1565697f514dSMiklos Szeredi terminate_walk(nd); 1566697f514dSMiklos Szeredi return err; 1567ce57dfc1SAl Viro } 1568ce57dfc1SAl Viro 15691da177e4SLinus Torvalds /* 1570b356379aSAl Viro * This limits recursive symlink follows to 8, while 1571b356379aSAl Viro * limiting consecutive symlinks to 40. 1572b356379aSAl Viro * 1573b356379aSAl Viro * Without that kind of total limit, nasty chains of consecutive 1574b356379aSAl Viro * symlinks can cause almost arbitrarily long lookups. 1575b356379aSAl Viro */ 1576b356379aSAl Viro static inline int nested_symlink(struct path *path, struct nameidata *nd) 1577b356379aSAl Viro { 1578b356379aSAl Viro int res; 1579b356379aSAl Viro 1580b356379aSAl Viro if (unlikely(current->link_count >= MAX_NESTED_LINKS)) { 1581b356379aSAl Viro path_put_conditional(path, nd); 1582b356379aSAl Viro path_put(&nd->path); 1583b356379aSAl Viro return -ELOOP; 1584b356379aSAl Viro } 15851a4022f8SErez Zadok BUG_ON(nd->depth >= MAX_NESTED_LINKS); 1586b356379aSAl Viro 1587b356379aSAl Viro nd->depth++; 1588b356379aSAl Viro current->link_count++; 1589b356379aSAl Viro 1590b356379aSAl Viro do { 1591b356379aSAl Viro struct path link = *path; 1592b356379aSAl Viro void *cookie; 1593574197e0SAl Viro 1594574197e0SAl Viro res = follow_link(&link, nd, &cookie); 15956d7b5aaeSAl Viro if (res) 15966d7b5aaeSAl Viro break; 159721b9b073SAl Viro res = walk_component(nd, path, LOOKUP_FOLLOW); 1598574197e0SAl Viro put_link(nd, &link, cookie); 1599b356379aSAl Viro } while (res > 0); 1600b356379aSAl Viro 1601b356379aSAl Viro current->link_count--; 1602b356379aSAl Viro nd->depth--; 1603b356379aSAl Viro return res; 1604b356379aSAl Viro } 1605b356379aSAl Viro 1606b356379aSAl Viro /* 1607bfcfaa77SLinus Torvalds * We can do the critical dentry name comparison and hashing 1608bfcfaa77SLinus Torvalds * operations one word at a time, but we are limited to: 1609bfcfaa77SLinus Torvalds * 1610bfcfaa77SLinus Torvalds * - Architectures with fast unaligned word accesses. We could 1611bfcfaa77SLinus Torvalds * do a "get_unaligned()" if this helps and is sufficiently 1612bfcfaa77SLinus Torvalds * fast. 1613bfcfaa77SLinus Torvalds * 1614bfcfaa77SLinus Torvalds * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we 1615bfcfaa77SLinus Torvalds * do not trap on the (extremely unlikely) case of a page 1616bfcfaa77SLinus Torvalds * crossing operation. 1617bfcfaa77SLinus Torvalds * 1618bfcfaa77SLinus Torvalds * - Furthermore, we need an efficient 64-bit compile for the 1619bfcfaa77SLinus Torvalds * 64-bit case in order to generate the "number of bytes in 1620bfcfaa77SLinus Torvalds * the final mask". Again, that could be replaced with a 1621bfcfaa77SLinus Torvalds * efficient population count instruction or similar. 1622bfcfaa77SLinus Torvalds */ 1623bfcfaa77SLinus Torvalds #ifdef CONFIG_DCACHE_WORD_ACCESS 1624bfcfaa77SLinus Torvalds 1625f68e556eSLinus Torvalds #include <asm/word-at-a-time.h> 1626bfcfaa77SLinus Torvalds 1627f68e556eSLinus Torvalds #ifdef CONFIG_64BIT 1628bfcfaa77SLinus Torvalds 1629bfcfaa77SLinus Torvalds static inline unsigned int fold_hash(unsigned long hash) 1630bfcfaa77SLinus Torvalds { 1631bfcfaa77SLinus Torvalds hash += hash >> (8*sizeof(int)); 1632bfcfaa77SLinus Torvalds return hash; 1633bfcfaa77SLinus Torvalds } 1634bfcfaa77SLinus Torvalds 1635bfcfaa77SLinus Torvalds #else /* 32-bit case */ 1636bfcfaa77SLinus Torvalds 1637bfcfaa77SLinus Torvalds #define fold_hash(x) (x) 1638bfcfaa77SLinus Torvalds 1639bfcfaa77SLinus Torvalds #endif 1640bfcfaa77SLinus Torvalds 1641bfcfaa77SLinus Torvalds unsigned int full_name_hash(const unsigned char *name, unsigned int len) 1642bfcfaa77SLinus Torvalds { 1643bfcfaa77SLinus Torvalds unsigned long a, mask; 1644bfcfaa77SLinus Torvalds unsigned long hash = 0; 1645bfcfaa77SLinus Torvalds 1646bfcfaa77SLinus Torvalds for (;;) { 1647e419b4ccSLinus Torvalds a = load_unaligned_zeropad(name); 1648bfcfaa77SLinus Torvalds if (len < sizeof(unsigned long)) 1649bfcfaa77SLinus Torvalds break; 1650bfcfaa77SLinus Torvalds hash += a; 1651f132c5beSAl Viro hash *= 9; 1652bfcfaa77SLinus Torvalds name += sizeof(unsigned long); 1653bfcfaa77SLinus Torvalds len -= sizeof(unsigned long); 1654bfcfaa77SLinus Torvalds if (!len) 1655bfcfaa77SLinus Torvalds goto done; 1656bfcfaa77SLinus Torvalds } 1657a5c21dceSWill Deacon mask = bytemask_from_count(len); 1658bfcfaa77SLinus Torvalds hash += mask & a; 1659bfcfaa77SLinus Torvalds done: 1660bfcfaa77SLinus Torvalds return fold_hash(hash); 1661bfcfaa77SLinus Torvalds } 1662bfcfaa77SLinus Torvalds EXPORT_SYMBOL(full_name_hash); 1663bfcfaa77SLinus Torvalds 1664bfcfaa77SLinus Torvalds /* 1665bfcfaa77SLinus Torvalds * Calculate the length and hash of the path component, and 1666bfcfaa77SLinus Torvalds * return the length of the component; 1667bfcfaa77SLinus Torvalds */ 1668bfcfaa77SLinus Torvalds static inline unsigned long hash_name(const char *name, unsigned int *hashp) 1669bfcfaa77SLinus Torvalds { 167036126f8fSLinus Torvalds unsigned long a, b, adata, bdata, mask, hash, len; 167136126f8fSLinus Torvalds const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS; 1672bfcfaa77SLinus Torvalds 1673bfcfaa77SLinus Torvalds hash = a = 0; 1674bfcfaa77SLinus Torvalds len = -sizeof(unsigned long); 1675bfcfaa77SLinus Torvalds do { 1676bfcfaa77SLinus Torvalds hash = (hash + a) * 9; 1677bfcfaa77SLinus Torvalds len += sizeof(unsigned long); 1678e419b4ccSLinus Torvalds a = load_unaligned_zeropad(name+len); 167936126f8fSLinus Torvalds b = a ^ REPEAT_BYTE('/'); 168036126f8fSLinus Torvalds } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants))); 1681bfcfaa77SLinus Torvalds 168236126f8fSLinus Torvalds adata = prep_zero_mask(a, adata, &constants); 168336126f8fSLinus Torvalds bdata = prep_zero_mask(b, bdata, &constants); 168436126f8fSLinus Torvalds 168536126f8fSLinus Torvalds mask = create_zero_mask(adata | bdata); 168636126f8fSLinus Torvalds 168736126f8fSLinus Torvalds hash += a & zero_bytemask(mask); 1688bfcfaa77SLinus Torvalds *hashp = fold_hash(hash); 1689bfcfaa77SLinus Torvalds 169036126f8fSLinus Torvalds return len + find_zero(mask); 1691bfcfaa77SLinus Torvalds } 1692bfcfaa77SLinus Torvalds 1693bfcfaa77SLinus Torvalds #else 1694bfcfaa77SLinus Torvalds 16950145acc2SLinus Torvalds unsigned int full_name_hash(const unsigned char *name, unsigned int len) 16960145acc2SLinus Torvalds { 16970145acc2SLinus Torvalds unsigned long hash = init_name_hash(); 16980145acc2SLinus Torvalds while (len--) 16990145acc2SLinus Torvalds hash = partial_name_hash(*name++, hash); 17000145acc2SLinus Torvalds return end_name_hash(hash); 17010145acc2SLinus Torvalds } 1702ae942ae7SLinus Torvalds EXPORT_SYMBOL(full_name_hash); 17030145acc2SLinus Torvalds 17043ddcd056SLinus Torvalds /* 1705200e9ef7SLinus Torvalds * We know there's a real path component here of at least 1706200e9ef7SLinus Torvalds * one character. 1707200e9ef7SLinus Torvalds */ 1708200e9ef7SLinus Torvalds static inline unsigned long hash_name(const char *name, unsigned int *hashp) 1709200e9ef7SLinus Torvalds { 1710200e9ef7SLinus Torvalds unsigned long hash = init_name_hash(); 1711200e9ef7SLinus Torvalds unsigned long len = 0, c; 1712200e9ef7SLinus Torvalds 1713200e9ef7SLinus Torvalds c = (unsigned char)*name; 1714200e9ef7SLinus Torvalds do { 1715200e9ef7SLinus Torvalds len++; 1716200e9ef7SLinus Torvalds hash = partial_name_hash(c, hash); 1717200e9ef7SLinus Torvalds c = (unsigned char)name[len]; 1718200e9ef7SLinus Torvalds } while (c && c != '/'); 1719200e9ef7SLinus Torvalds *hashp = end_name_hash(hash); 1720200e9ef7SLinus Torvalds return len; 1721200e9ef7SLinus Torvalds } 1722200e9ef7SLinus Torvalds 1723bfcfaa77SLinus Torvalds #endif 1724bfcfaa77SLinus Torvalds 1725200e9ef7SLinus Torvalds /* 17261da177e4SLinus Torvalds * Name resolution. 1727ea3834d9SPrasanna Meda * This is the basic name resolution function, turning a pathname into 1728ea3834d9SPrasanna Meda * the final dentry. We expect 'base' to be positive and a directory. 17291da177e4SLinus Torvalds * 1730ea3834d9SPrasanna Meda * Returns 0 and nd will have valid dentry and mnt on success. 1731ea3834d9SPrasanna Meda * Returns error and drops reference to input namei data on failure. 17321da177e4SLinus Torvalds */ 17336de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd) 17341da177e4SLinus Torvalds { 17351da177e4SLinus Torvalds struct path next; 17361da177e4SLinus Torvalds int err; 17371da177e4SLinus Torvalds 17381da177e4SLinus Torvalds while (*name=='/') 17391da177e4SLinus Torvalds name++; 17401da177e4SLinus Torvalds if (!*name) 1741086e183aSAl Viro return 0; 17421da177e4SLinus Torvalds 17431da177e4SLinus Torvalds /* At this point we know we have a real path component. */ 17441da177e4SLinus Torvalds for(;;) { 17451da177e4SLinus Torvalds struct qstr this; 1746200e9ef7SLinus Torvalds long len; 1747fe479a58SAl Viro int type; 17481da177e4SLinus Torvalds 174952094c8aSAl Viro err = may_lookup(nd); 17501da177e4SLinus Torvalds if (err) 17511da177e4SLinus Torvalds break; 17521da177e4SLinus Torvalds 1753200e9ef7SLinus Torvalds len = hash_name(name, &this.hash); 17541da177e4SLinus Torvalds this.name = name; 1755200e9ef7SLinus Torvalds this.len = len; 17561da177e4SLinus Torvalds 1757fe479a58SAl Viro type = LAST_NORM; 1758200e9ef7SLinus Torvalds if (name[0] == '.') switch (len) { 1759fe479a58SAl Viro case 2: 1760200e9ef7SLinus Torvalds if (name[1] == '.') { 1761fe479a58SAl Viro type = LAST_DOTDOT; 176216c2cd71SAl Viro nd->flags |= LOOKUP_JUMPED; 176316c2cd71SAl Viro } 1764fe479a58SAl Viro break; 1765fe479a58SAl Viro case 1: 1766fe479a58SAl Viro type = LAST_DOT; 1767fe479a58SAl Viro } 17685a202bcdSAl Viro if (likely(type == LAST_NORM)) { 17695a202bcdSAl Viro struct dentry *parent = nd->path.dentry; 177016c2cd71SAl Viro nd->flags &= ~LOOKUP_JUMPED; 17715a202bcdSAl Viro if (unlikely(parent->d_flags & DCACHE_OP_HASH)) { 1772da53be12SLinus Torvalds err = parent->d_op->d_hash(parent, &this); 17735a202bcdSAl Viro if (err < 0) 17745a202bcdSAl Viro break; 17755a202bcdSAl Viro } 17765a202bcdSAl Viro } 1777fe479a58SAl Viro 17785f4a6a69SAl Viro nd->last = this; 17795f4a6a69SAl Viro nd->last_type = type; 17805f4a6a69SAl Viro 1781200e9ef7SLinus Torvalds if (!name[len]) 17825f4a6a69SAl Viro return 0; 1783200e9ef7SLinus Torvalds /* 1784200e9ef7SLinus Torvalds * If it wasn't NUL, we know it was '/'. Skip that 1785200e9ef7SLinus Torvalds * slash, and continue until no more slashes. 1786200e9ef7SLinus Torvalds */ 1787200e9ef7SLinus Torvalds do { 1788200e9ef7SLinus Torvalds len++; 1789200e9ef7SLinus Torvalds } while (unlikely(name[len] == '/')); 1790200e9ef7SLinus Torvalds if (!name[len]) 17915f4a6a69SAl Viro return 0; 17925f4a6a69SAl Viro 1793200e9ef7SLinus Torvalds name += len; 17941da177e4SLinus Torvalds 179521b9b073SAl Viro err = walk_component(nd, &next, LOOKUP_FOLLOW); 1796ce57dfc1SAl Viro if (err < 0) 1797ce57dfc1SAl Viro return err; 1798fe479a58SAl Viro 1799ce57dfc1SAl Viro if (err) { 1800b356379aSAl Viro err = nested_symlink(&next, nd); 18011da177e4SLinus Torvalds if (err) 1802a7472babSAl Viro return err; 180331e6b01fSNick Piggin } 1804b18825a7SDavid Howells if (!d_is_directory(nd->path.dentry)) { 18053ddcd056SLinus Torvalds err = -ENOTDIR; 18063ddcd056SLinus Torvalds break; 18075f4a6a69SAl Viro } 1808ce57dfc1SAl Viro } 1809951361f9SAl Viro terminate_walk(nd); 18101da177e4SLinus Torvalds return err; 18111da177e4SLinus Torvalds } 18121da177e4SLinus Torvalds 181370e9b357SAl Viro static int path_init(int dfd, const char *name, unsigned int flags, 181470e9b357SAl Viro struct nameidata *nd, struct file **fp) 181531e6b01fSNick Piggin { 181631e6b01fSNick Piggin int retval = 0; 181731e6b01fSNick Piggin 181831e6b01fSNick Piggin nd->last_type = LAST_ROOT; /* if there are only slashes... */ 181916c2cd71SAl Viro nd->flags = flags | LOOKUP_JUMPED; 182031e6b01fSNick Piggin nd->depth = 0; 18215b6ca027SAl Viro if (flags & LOOKUP_ROOT) { 1822b18825a7SDavid Howells struct dentry *root = nd->root.dentry; 1823b18825a7SDavid Howells struct inode *inode = root->d_inode; 182473d049a4SAl Viro if (*name) { 1825b18825a7SDavid Howells if (!d_is_directory(root)) 18265b6ca027SAl Viro return -ENOTDIR; 18275b6ca027SAl Viro retval = inode_permission(inode, MAY_EXEC); 18285b6ca027SAl Viro if (retval) 18295b6ca027SAl Viro return retval; 183073d049a4SAl Viro } 18315b6ca027SAl Viro nd->path = nd->root; 18325b6ca027SAl Viro nd->inode = inode; 18335b6ca027SAl Viro if (flags & LOOKUP_RCU) { 18348b61e74fSAl Viro rcu_read_lock(); 18355b6ca027SAl Viro nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 183648a066e7SAl Viro nd->m_seq = read_seqbegin(&mount_lock); 18375b6ca027SAl Viro } else { 18385b6ca027SAl Viro path_get(&nd->path); 18395b6ca027SAl Viro } 18405b6ca027SAl Viro return 0; 18415b6ca027SAl Viro } 18425b6ca027SAl Viro 184331e6b01fSNick Piggin nd->root.mnt = NULL; 184431e6b01fSNick Piggin 184548a066e7SAl Viro nd->m_seq = read_seqbegin(&mount_lock); 184631e6b01fSNick Piggin if (*name=='/') { 1847e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 18488b61e74fSAl Viro rcu_read_lock(); 1849e41f7d4eSAl Viro set_root_rcu(nd); 1850e41f7d4eSAl Viro } else { 1851e41f7d4eSAl Viro set_root(nd); 1852e41f7d4eSAl Viro path_get(&nd->root); 1853e41f7d4eSAl Viro } 185431e6b01fSNick Piggin nd->path = nd->root; 185531e6b01fSNick Piggin } else if (dfd == AT_FDCWD) { 1856e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 185731e6b01fSNick Piggin struct fs_struct *fs = current->fs; 1858c28cc364SNick Piggin unsigned seq; 185931e6b01fSNick Piggin 18608b61e74fSAl Viro rcu_read_lock(); 186131e6b01fSNick Piggin 1862c28cc364SNick Piggin do { 1863c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 186431e6b01fSNick Piggin nd->path = fs->pwd; 1865c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 1866c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 1867e41f7d4eSAl Viro } else { 1868e41f7d4eSAl Viro get_fs_pwd(current->fs, &nd->path); 1869e41f7d4eSAl Viro } 187031e6b01fSNick Piggin } else { 1871582aa64aSJeff Layton /* Caller must check execute permissions on the starting path component */ 18722903ff01SAl Viro struct fd f = fdget_raw(dfd); 187331e6b01fSNick Piggin struct dentry *dentry; 187431e6b01fSNick Piggin 18752903ff01SAl Viro if (!f.file) 18762903ff01SAl Viro return -EBADF; 187731e6b01fSNick Piggin 18782903ff01SAl Viro dentry = f.file->f_path.dentry; 187931e6b01fSNick Piggin 1880f52e0c11SAl Viro if (*name) { 1881b18825a7SDavid Howells if (!d_is_directory(dentry)) { 18822903ff01SAl Viro fdput(f); 18832903ff01SAl Viro return -ENOTDIR; 1884f52e0c11SAl Viro } 18852903ff01SAl Viro } 18862903ff01SAl Viro 18872903ff01SAl Viro nd->path = f.file->f_path; 1888e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 18899c225f26SLinus Torvalds if (f.flags & FDPUT_FPUT) 18902903ff01SAl Viro *fp = f.file; 1891c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 18928b61e74fSAl Viro rcu_read_lock(); 18935590ff0dSUlrich Drepper } else { 18942903ff01SAl Viro path_get(&nd->path); 18952903ff01SAl Viro fdput(f); 18961da177e4SLinus Torvalds } 1897e41f7d4eSAl Viro } 1898e41f7d4eSAl Viro 189931e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 19009b4a9b14SAl Viro return 0; 19019b4a9b14SAl Viro } 19029b4a9b14SAl Viro 1903bd92d7feSAl Viro static inline int lookup_last(struct nameidata *nd, struct path *path) 1904bd92d7feSAl Viro { 1905bd92d7feSAl Viro if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len]) 1906bd92d7feSAl Viro nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 1907bd92d7feSAl Viro 1908bd92d7feSAl Viro nd->flags &= ~LOOKUP_PARENT; 190921b9b073SAl Viro return walk_component(nd, path, nd->flags & LOOKUP_FOLLOW); 1910bd92d7feSAl Viro } 1911bd92d7feSAl Viro 19129b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */ 1913ee0827cdSAl Viro static int path_lookupat(int dfd, const char *name, 19149b4a9b14SAl Viro unsigned int flags, struct nameidata *nd) 19159b4a9b14SAl Viro { 191670e9b357SAl Viro struct file *base = NULL; 1917bd92d7feSAl Viro struct path path; 1918bd92d7feSAl Viro int err; 191931e6b01fSNick Piggin 192031e6b01fSNick Piggin /* 192131e6b01fSNick Piggin * Path walking is largely split up into 2 different synchronisation 192231e6b01fSNick Piggin * schemes, rcu-walk and ref-walk (explained in 192331e6b01fSNick Piggin * Documentation/filesystems/path-lookup.txt). These share much of the 192431e6b01fSNick Piggin * path walk code, but some things particularly setup, cleanup, and 192531e6b01fSNick Piggin * following mounts are sufficiently divergent that functions are 192631e6b01fSNick Piggin * duplicated. Typically there is a function foo(), and its RCU 192731e6b01fSNick Piggin * analogue, foo_rcu(). 192831e6b01fSNick Piggin * 192931e6b01fSNick Piggin * -ECHILD is the error number of choice (just to avoid clashes) that 193031e6b01fSNick Piggin * is returned if some aspect of an rcu-walk fails. Such an error must 193131e6b01fSNick Piggin * be handled by restarting a traditional ref-walk (which will always 193231e6b01fSNick Piggin * be able to complete). 193331e6b01fSNick Piggin */ 1934bd92d7feSAl Viro err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base); 1935ee0827cdSAl Viro 1936bd92d7feSAl Viro if (unlikely(err)) 1937bd92d7feSAl Viro return err; 1938ee0827cdSAl Viro 1939ee0827cdSAl Viro current->total_link_count = 0; 1940bd92d7feSAl Viro err = link_path_walk(name, nd); 1941bd92d7feSAl Viro 1942bd92d7feSAl Viro if (!err && !(flags & LOOKUP_PARENT)) { 1943bd92d7feSAl Viro err = lookup_last(nd, &path); 1944bd92d7feSAl Viro while (err > 0) { 1945bd92d7feSAl Viro void *cookie; 1946bd92d7feSAl Viro struct path link = path; 1947800179c9SKees Cook err = may_follow_link(&link, nd); 1948800179c9SKees Cook if (unlikely(err)) 1949800179c9SKees Cook break; 1950bd92d7feSAl Viro nd->flags |= LOOKUP_PARENT; 1951574197e0SAl Viro err = follow_link(&link, nd, &cookie); 19526d7b5aaeSAl Viro if (err) 19536d7b5aaeSAl Viro break; 1954bd92d7feSAl Viro err = lookup_last(nd, &path); 1955574197e0SAl Viro put_link(nd, &link, cookie); 1956bd92d7feSAl Viro } 1957bd92d7feSAl Viro } 1958ee0827cdSAl Viro 19599f1fafeeSAl Viro if (!err) 19609f1fafeeSAl Viro err = complete_walk(nd); 1961bd92d7feSAl Viro 1962bd92d7feSAl Viro if (!err && nd->flags & LOOKUP_DIRECTORY) { 1963b18825a7SDavid Howells if (!d_is_directory(nd->path.dentry)) { 1964bd92d7feSAl Viro path_put(&nd->path); 1965bd23a539SAl Viro err = -ENOTDIR; 1966bd92d7feSAl Viro } 1967bd92d7feSAl Viro } 196816c2cd71SAl Viro 196970e9b357SAl Viro if (base) 197070e9b357SAl Viro fput(base); 1971ee0827cdSAl Viro 19725b6ca027SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) { 197331e6b01fSNick Piggin path_put(&nd->root); 197431e6b01fSNick Piggin nd->root.mnt = NULL; 197531e6b01fSNick Piggin } 1976bd92d7feSAl Viro return err; 197731e6b01fSNick Piggin } 197831e6b01fSNick Piggin 1979873f1eedSJeff Layton static int filename_lookup(int dfd, struct filename *name, 1980873f1eedSJeff Layton unsigned int flags, struct nameidata *nd) 1981873f1eedSJeff Layton { 1982873f1eedSJeff Layton int retval = path_lookupat(dfd, name->name, flags | LOOKUP_RCU, nd); 1983873f1eedSJeff Layton if (unlikely(retval == -ECHILD)) 1984873f1eedSJeff Layton retval = path_lookupat(dfd, name->name, flags, nd); 1985873f1eedSJeff Layton if (unlikely(retval == -ESTALE)) 1986873f1eedSJeff Layton retval = path_lookupat(dfd, name->name, 1987873f1eedSJeff Layton flags | LOOKUP_REVAL, nd); 1988873f1eedSJeff Layton 1989873f1eedSJeff Layton if (likely(!retval)) 1990adb5c247SJeff Layton audit_inode(name, nd->path.dentry, flags & LOOKUP_PARENT); 1991873f1eedSJeff Layton return retval; 1992873f1eedSJeff Layton } 1993873f1eedSJeff Layton 1994ee0827cdSAl Viro static int do_path_lookup(int dfd, const char *name, 1995ee0827cdSAl Viro unsigned int flags, struct nameidata *nd) 1996ee0827cdSAl Viro { 1997873f1eedSJeff Layton struct filename filename = { .name = name }; 1998ee0827cdSAl Viro 1999873f1eedSJeff Layton return filename_lookup(dfd, &filename, flags, nd); 20001da177e4SLinus Torvalds } 20011da177e4SLinus Torvalds 200279714f72SAl Viro /* does lookup, returns the object with parent locked */ 200379714f72SAl Viro struct dentry *kern_path_locked(const char *name, struct path *path) 20045590ff0dSUlrich Drepper { 200579714f72SAl Viro struct nameidata nd; 200679714f72SAl Viro struct dentry *d; 200779714f72SAl Viro int err = do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, &nd); 200879714f72SAl Viro if (err) 200979714f72SAl Viro return ERR_PTR(err); 201079714f72SAl Viro if (nd.last_type != LAST_NORM) { 201179714f72SAl Viro path_put(&nd.path); 201279714f72SAl Viro return ERR_PTR(-EINVAL); 201379714f72SAl Viro } 201479714f72SAl Viro mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 20151e0ea001SAl Viro d = __lookup_hash(&nd.last, nd.path.dentry, 0); 201679714f72SAl Viro if (IS_ERR(d)) { 201779714f72SAl Viro mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 201879714f72SAl Viro path_put(&nd.path); 201979714f72SAl Viro return d; 202079714f72SAl Viro } 202179714f72SAl Viro *path = nd.path; 202279714f72SAl Viro return d; 20235590ff0dSUlrich Drepper } 20245590ff0dSUlrich Drepper 2025d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path) 2026d1811465SAl Viro { 2027d1811465SAl Viro struct nameidata nd; 2028d1811465SAl Viro int res = do_path_lookup(AT_FDCWD, name, flags, &nd); 2029d1811465SAl Viro if (!res) 2030d1811465SAl Viro *path = nd.path; 2031d1811465SAl Viro return res; 2032d1811465SAl Viro } 20334d359507SAl Viro EXPORT_SYMBOL(kern_path); 2034d1811465SAl Viro 203516f18200SJosef 'Jeff' Sipek /** 203616f18200SJosef 'Jeff' Sipek * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair 203716f18200SJosef 'Jeff' Sipek * @dentry: pointer to dentry of the base directory 203816f18200SJosef 'Jeff' Sipek * @mnt: pointer to vfs mount of the base directory 203916f18200SJosef 'Jeff' Sipek * @name: pointer to file name 204016f18200SJosef 'Jeff' Sipek * @flags: lookup flags 2041e0a01249SAl Viro * @path: pointer to struct path to fill 204216f18200SJosef 'Jeff' Sipek */ 204316f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt, 204416f18200SJosef 'Jeff' Sipek const char *name, unsigned int flags, 2045e0a01249SAl Viro struct path *path) 204616f18200SJosef 'Jeff' Sipek { 2047e0a01249SAl Viro struct nameidata nd; 2048e0a01249SAl Viro int err; 2049e0a01249SAl Viro nd.root.dentry = dentry; 2050e0a01249SAl Viro nd.root.mnt = mnt; 2051e0a01249SAl Viro BUG_ON(flags & LOOKUP_PARENT); 20525b6ca027SAl Viro /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */ 2053e0a01249SAl Viro err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd); 2054e0a01249SAl Viro if (!err) 2055e0a01249SAl Viro *path = nd.path; 2056e0a01249SAl Viro return err; 205716f18200SJosef 'Jeff' Sipek } 20584d359507SAl Viro EXPORT_SYMBOL(vfs_path_lookup); 205916f18200SJosef 'Jeff' Sipek 2060057f6c01SJames Morris /* 2061057f6c01SJames Morris * Restricted form of lookup. Doesn't follow links, single-component only, 2062057f6c01SJames Morris * needs parent already locked. Doesn't follow mounts. 2063057f6c01SJames Morris * SMP-safe. 2064057f6c01SJames Morris */ 2065a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd) 20661da177e4SLinus Torvalds { 206772bd866aSAl Viro return __lookup_hash(&nd->last, nd->path.dentry, nd->flags); 20681da177e4SLinus Torvalds } 20691da177e4SLinus Torvalds 2070eead1911SChristoph Hellwig /** 2071a6b91919SRandy Dunlap * lookup_one_len - filesystem helper to lookup single pathname component 2072eead1911SChristoph Hellwig * @name: pathname component to lookup 2073eead1911SChristoph Hellwig * @base: base directory to lookup from 2074eead1911SChristoph Hellwig * @len: maximum length @len should be interpreted to 2075eead1911SChristoph Hellwig * 2076a6b91919SRandy Dunlap * Note that this routine is purely a helper for filesystem usage and should 2077a6b91919SRandy Dunlap * not be called by generic code. Also note that by using this function the 2078eead1911SChristoph Hellwig * nameidata argument is passed to the filesystem methods and a filesystem 2079eead1911SChristoph Hellwig * using this helper needs to be prepared for that. 2080eead1911SChristoph Hellwig */ 2081057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len) 2082057f6c01SJames Morris { 2083057f6c01SJames Morris struct qstr this; 20846a96ba54SAl Viro unsigned int c; 2085cda309deSMiklos Szeredi int err; 2086057f6c01SJames Morris 20872f9092e1SDavid Woodhouse WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex)); 20882f9092e1SDavid Woodhouse 20896a96ba54SAl Viro this.name = name; 20906a96ba54SAl Viro this.len = len; 20910145acc2SLinus Torvalds this.hash = full_name_hash(name, len); 20926a96ba54SAl Viro if (!len) 20936a96ba54SAl Viro return ERR_PTR(-EACCES); 20946a96ba54SAl Viro 209521d8a15aSAl Viro if (unlikely(name[0] == '.')) { 209621d8a15aSAl Viro if (len < 2 || (len == 2 && name[1] == '.')) 209721d8a15aSAl Viro return ERR_PTR(-EACCES); 209821d8a15aSAl Viro } 209921d8a15aSAl Viro 21006a96ba54SAl Viro while (len--) { 21016a96ba54SAl Viro c = *(const unsigned char *)name++; 21026a96ba54SAl Viro if (c == '/' || c == '\0') 21036a96ba54SAl Viro return ERR_PTR(-EACCES); 21046a96ba54SAl Viro } 21055a202bcdSAl Viro /* 21065a202bcdSAl Viro * See if the low-level filesystem might want 21075a202bcdSAl Viro * to use its own hash.. 21085a202bcdSAl Viro */ 21095a202bcdSAl Viro if (base->d_flags & DCACHE_OP_HASH) { 2110da53be12SLinus Torvalds int err = base->d_op->d_hash(base, &this); 21115a202bcdSAl Viro if (err < 0) 21125a202bcdSAl Viro return ERR_PTR(err); 21135a202bcdSAl Viro } 2114eead1911SChristoph Hellwig 2115cda309deSMiklos Szeredi err = inode_permission(base->d_inode, MAY_EXEC); 2116cda309deSMiklos Szeredi if (err) 2117cda309deSMiklos Szeredi return ERR_PTR(err); 2118cda309deSMiklos Szeredi 211972bd866aSAl Viro return __lookup_hash(&this, base, 0); 2120057f6c01SJames Morris } 21214d359507SAl Viro EXPORT_SYMBOL(lookup_one_len); 2122057f6c01SJames Morris 21231fa1e7f6SAndy Whitcroft int user_path_at_empty(int dfd, const char __user *name, unsigned flags, 21241fa1e7f6SAndy Whitcroft struct path *path, int *empty) 21251da177e4SLinus Torvalds { 21262d8f3038SAl Viro struct nameidata nd; 212791a27b2aSJeff Layton struct filename *tmp = getname_flags(name, flags, empty); 21281da177e4SLinus Torvalds int err = PTR_ERR(tmp); 21291da177e4SLinus Torvalds if (!IS_ERR(tmp)) { 21302d8f3038SAl Viro 21312d8f3038SAl Viro BUG_ON(flags & LOOKUP_PARENT); 21322d8f3038SAl Viro 2133873f1eedSJeff Layton err = filename_lookup(dfd, tmp, flags, &nd); 21341da177e4SLinus Torvalds putname(tmp); 21352d8f3038SAl Viro if (!err) 21362d8f3038SAl Viro *path = nd.path; 21371da177e4SLinus Torvalds } 21381da177e4SLinus Torvalds return err; 21391da177e4SLinus Torvalds } 21401da177e4SLinus Torvalds 21411fa1e7f6SAndy Whitcroft int user_path_at(int dfd, const char __user *name, unsigned flags, 21421fa1e7f6SAndy Whitcroft struct path *path) 21431fa1e7f6SAndy Whitcroft { 2144f7493e5dSLinus Torvalds return user_path_at_empty(dfd, name, flags, path, NULL); 21451fa1e7f6SAndy Whitcroft } 21464d359507SAl Viro EXPORT_SYMBOL(user_path_at); 21471fa1e7f6SAndy Whitcroft 2148873f1eedSJeff Layton /* 2149873f1eedSJeff Layton * NB: most callers don't do anything directly with the reference to the 2150873f1eedSJeff Layton * to struct filename, but the nd->last pointer points into the name string 2151873f1eedSJeff Layton * allocated by getname. So we must hold the reference to it until all 2152873f1eedSJeff Layton * path-walking is complete. 2153873f1eedSJeff Layton */ 215491a27b2aSJeff Layton static struct filename * 21559e790bd6SJeff Layton user_path_parent(int dfd, const char __user *path, struct nameidata *nd, 21569e790bd6SJeff Layton unsigned int flags) 21572ad94ae6SAl Viro { 215891a27b2aSJeff Layton struct filename *s = getname(path); 21592ad94ae6SAl Viro int error; 21602ad94ae6SAl Viro 21619e790bd6SJeff Layton /* only LOOKUP_REVAL is allowed in extra flags */ 21629e790bd6SJeff Layton flags &= LOOKUP_REVAL; 21639e790bd6SJeff Layton 21642ad94ae6SAl Viro if (IS_ERR(s)) 216591a27b2aSJeff Layton return s; 21662ad94ae6SAl Viro 21679e790bd6SJeff Layton error = filename_lookup(dfd, s, flags | LOOKUP_PARENT, nd); 216891a27b2aSJeff Layton if (error) { 21692ad94ae6SAl Viro putname(s); 217091a27b2aSJeff Layton return ERR_PTR(error); 217191a27b2aSJeff Layton } 21722ad94ae6SAl Viro 217391a27b2aSJeff Layton return s; 21742ad94ae6SAl Viro } 21752ad94ae6SAl Viro 21768033426eSJeff Layton /** 2177197df04cSAl Viro * mountpoint_last - look up last component for umount 21788033426eSJeff Layton * @nd: pathwalk nameidata - currently pointing at parent directory of "last" 21798033426eSJeff Layton * @path: pointer to container for result 21808033426eSJeff Layton * 21818033426eSJeff Layton * This is a special lookup_last function just for umount. In this case, we 21828033426eSJeff Layton * need to resolve the path without doing any revalidation. 21838033426eSJeff Layton * 21848033426eSJeff Layton * The nameidata should be the result of doing a LOOKUP_PARENT pathwalk. Since 21858033426eSJeff Layton * mountpoints are always pinned in the dcache, their ancestors are too. Thus, 21868033426eSJeff Layton * in almost all cases, this lookup will be served out of the dcache. The only 21878033426eSJeff Layton * cases where it won't are if nd->last refers to a symlink or the path is 21888033426eSJeff Layton * bogus and it doesn't exist. 21898033426eSJeff Layton * 21908033426eSJeff Layton * Returns: 21918033426eSJeff Layton * -error: if there was an error during lookup. This includes -ENOENT if the 21928033426eSJeff Layton * lookup found a negative dentry. The nd->path reference will also be 21938033426eSJeff Layton * put in this case. 21948033426eSJeff Layton * 21958033426eSJeff Layton * 0: if we successfully resolved nd->path and found it to not to be a 21968033426eSJeff Layton * symlink that needs to be followed. "path" will also be populated. 21978033426eSJeff Layton * The nd->path reference will also be put. 21988033426eSJeff Layton * 21998033426eSJeff Layton * 1: if we successfully resolved nd->last and found it to be a symlink 22008033426eSJeff Layton * that needs to be followed. "path" will be populated with the path 22018033426eSJeff Layton * to the link, and nd->path will *not* be put. 22028033426eSJeff Layton */ 22038033426eSJeff Layton static int 2204197df04cSAl Viro mountpoint_last(struct nameidata *nd, struct path *path) 22058033426eSJeff Layton { 22068033426eSJeff Layton int error = 0; 22078033426eSJeff Layton struct dentry *dentry; 22088033426eSJeff Layton struct dentry *dir = nd->path.dentry; 22098033426eSJeff Layton 221035759521SAl Viro /* If we're in rcuwalk, drop out of it to handle last component */ 221135759521SAl Viro if (nd->flags & LOOKUP_RCU) { 221235759521SAl Viro if (unlazy_walk(nd, NULL)) { 22138033426eSJeff Layton error = -ECHILD; 221435759521SAl Viro goto out; 221535759521SAl Viro } 22168033426eSJeff Layton } 22178033426eSJeff Layton 22188033426eSJeff Layton nd->flags &= ~LOOKUP_PARENT; 22198033426eSJeff Layton 22208033426eSJeff Layton if (unlikely(nd->last_type != LAST_NORM)) { 22218033426eSJeff Layton error = handle_dots(nd, nd->last_type); 222235759521SAl Viro if (error) 222335759521SAl Viro goto out; 22248033426eSJeff Layton dentry = dget(nd->path.dentry); 222535759521SAl Viro goto done; 22268033426eSJeff Layton } 22278033426eSJeff Layton 22288033426eSJeff Layton mutex_lock(&dir->d_inode->i_mutex); 22298033426eSJeff Layton dentry = d_lookup(dir, &nd->last); 22308033426eSJeff Layton if (!dentry) { 22318033426eSJeff Layton /* 22328033426eSJeff Layton * No cached dentry. Mounted dentries are pinned in the cache, 22338033426eSJeff Layton * so that means that this dentry is probably a symlink or the 22348033426eSJeff Layton * path doesn't actually point to a mounted dentry. 22358033426eSJeff Layton */ 22368033426eSJeff Layton dentry = d_alloc(dir, &nd->last); 22378033426eSJeff Layton if (!dentry) { 22388033426eSJeff Layton error = -ENOMEM; 2239bcceeebaSDave Jones mutex_unlock(&dir->d_inode->i_mutex); 224035759521SAl Viro goto out; 22418033426eSJeff Layton } 224235759521SAl Viro dentry = lookup_real(dir->d_inode, dentry, nd->flags); 224335759521SAl Viro error = PTR_ERR(dentry); 2244bcceeebaSDave Jones if (IS_ERR(dentry)) { 2245bcceeebaSDave Jones mutex_unlock(&dir->d_inode->i_mutex); 224635759521SAl Viro goto out; 22478033426eSJeff Layton } 2248bcceeebaSDave Jones } 22498033426eSJeff Layton mutex_unlock(&dir->d_inode->i_mutex); 22508033426eSJeff Layton 225135759521SAl Viro done: 22528033426eSJeff Layton if (!dentry->d_inode) { 22538033426eSJeff Layton error = -ENOENT; 22548033426eSJeff Layton dput(dentry); 225535759521SAl Viro goto out; 225635759521SAl Viro } 22578033426eSJeff Layton path->dentry = dentry; 22588033426eSJeff Layton path->mnt = mntget(nd->path.mnt); 2259b18825a7SDavid Howells if (should_follow_link(dentry, nd->flags & LOOKUP_FOLLOW)) 22608033426eSJeff Layton return 1; 22618033426eSJeff Layton follow_mount(path); 226235759521SAl Viro error = 0; 226335759521SAl Viro out: 22648033426eSJeff Layton terminate_walk(nd); 22658033426eSJeff Layton return error; 22668033426eSJeff Layton } 22678033426eSJeff Layton 22688033426eSJeff Layton /** 2269197df04cSAl Viro * path_mountpoint - look up a path to be umounted 22708033426eSJeff Layton * @dfd: directory file descriptor to start walk from 22718033426eSJeff Layton * @name: full pathname to walk 2272606d6fe3SRandy Dunlap * @path: pointer to container for result 22738033426eSJeff Layton * @flags: lookup flags 22748033426eSJeff Layton * 22758033426eSJeff Layton * Look up the given name, but don't attempt to revalidate the last component. 2276606d6fe3SRandy Dunlap * Returns 0 and "path" will be valid on success; Returns error otherwise. 22778033426eSJeff Layton */ 22788033426eSJeff Layton static int 2279197df04cSAl Viro path_mountpoint(int dfd, const char *name, struct path *path, unsigned int flags) 22808033426eSJeff Layton { 22818033426eSJeff Layton struct file *base = NULL; 22828033426eSJeff Layton struct nameidata nd; 22838033426eSJeff Layton int err; 22848033426eSJeff Layton 22858033426eSJeff Layton err = path_init(dfd, name, flags | LOOKUP_PARENT, &nd, &base); 22868033426eSJeff Layton if (unlikely(err)) 22878033426eSJeff Layton return err; 22888033426eSJeff Layton 22898033426eSJeff Layton current->total_link_count = 0; 22908033426eSJeff Layton err = link_path_walk(name, &nd); 22918033426eSJeff Layton if (err) 22928033426eSJeff Layton goto out; 22938033426eSJeff Layton 2294197df04cSAl Viro err = mountpoint_last(&nd, path); 22958033426eSJeff Layton while (err > 0) { 22968033426eSJeff Layton void *cookie; 22978033426eSJeff Layton struct path link = *path; 22988033426eSJeff Layton err = may_follow_link(&link, &nd); 22998033426eSJeff Layton if (unlikely(err)) 23008033426eSJeff Layton break; 23018033426eSJeff Layton nd.flags |= LOOKUP_PARENT; 23028033426eSJeff Layton err = follow_link(&link, &nd, &cookie); 23038033426eSJeff Layton if (err) 23048033426eSJeff Layton break; 2305197df04cSAl Viro err = mountpoint_last(&nd, path); 23068033426eSJeff Layton put_link(&nd, &link, cookie); 23078033426eSJeff Layton } 23088033426eSJeff Layton out: 23098033426eSJeff Layton if (base) 23108033426eSJeff Layton fput(base); 23118033426eSJeff Layton 23128033426eSJeff Layton if (nd.root.mnt && !(nd.flags & LOOKUP_ROOT)) 23138033426eSJeff Layton path_put(&nd.root); 23148033426eSJeff Layton 23158033426eSJeff Layton return err; 23168033426eSJeff Layton } 23178033426eSJeff Layton 23182d864651SAl Viro static int 23192d864651SAl Viro filename_mountpoint(int dfd, struct filename *s, struct path *path, 23202d864651SAl Viro unsigned int flags) 23212d864651SAl Viro { 23222d864651SAl Viro int error = path_mountpoint(dfd, s->name, path, flags | LOOKUP_RCU); 23232d864651SAl Viro if (unlikely(error == -ECHILD)) 23242d864651SAl Viro error = path_mountpoint(dfd, s->name, path, flags); 23252d864651SAl Viro if (unlikely(error == -ESTALE)) 23262d864651SAl Viro error = path_mountpoint(dfd, s->name, path, flags | LOOKUP_REVAL); 23272d864651SAl Viro if (likely(!error)) 23282d864651SAl Viro audit_inode(s, path->dentry, 0); 23292d864651SAl Viro return error; 23302d864651SAl Viro } 23312d864651SAl Viro 23328033426eSJeff Layton /** 2333197df04cSAl Viro * user_path_mountpoint_at - lookup a path from userland in order to umount it 23348033426eSJeff Layton * @dfd: directory file descriptor 23358033426eSJeff Layton * @name: pathname from userland 23368033426eSJeff Layton * @flags: lookup flags 23378033426eSJeff Layton * @path: pointer to container to hold result 23388033426eSJeff Layton * 23398033426eSJeff Layton * A umount is a special case for path walking. We're not actually interested 23408033426eSJeff Layton * in the inode in this situation, and ESTALE errors can be a problem. We 23418033426eSJeff Layton * simply want track down the dentry and vfsmount attached at the mountpoint 23428033426eSJeff Layton * and avoid revalidating the last component. 23438033426eSJeff Layton * 23448033426eSJeff Layton * Returns 0 and populates "path" on success. 23458033426eSJeff Layton */ 23468033426eSJeff Layton int 2347197df04cSAl Viro user_path_mountpoint_at(int dfd, const char __user *name, unsigned int flags, 23488033426eSJeff Layton struct path *path) 23498033426eSJeff Layton { 23508033426eSJeff Layton struct filename *s = getname(name); 23518033426eSJeff Layton int error; 23528033426eSJeff Layton if (IS_ERR(s)) 23538033426eSJeff Layton return PTR_ERR(s); 23542d864651SAl Viro error = filename_mountpoint(dfd, s, path, flags); 23558033426eSJeff Layton putname(s); 23568033426eSJeff Layton return error; 23578033426eSJeff Layton } 23588033426eSJeff Layton 23592d864651SAl Viro int 23602d864651SAl Viro kern_path_mountpoint(int dfd, const char *name, struct path *path, 23612d864651SAl Viro unsigned int flags) 23622d864651SAl Viro { 23632d864651SAl Viro struct filename s = {.name = name}; 23642d864651SAl Viro return filename_mountpoint(dfd, &s, path, flags); 23652d864651SAl Viro } 23662d864651SAl Viro EXPORT_SYMBOL(kern_path_mountpoint); 23672d864651SAl Viro 23681da177e4SLinus Torvalds /* 23691da177e4SLinus Torvalds * It's inline, so penalty for filesystems that don't use sticky bit is 23701da177e4SLinus Torvalds * minimal. 23711da177e4SLinus Torvalds */ 23721da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode) 23731da177e4SLinus Torvalds { 23748e96e3b7SEric W. Biederman kuid_t fsuid = current_fsuid(); 2375da9592edSDavid Howells 23761da177e4SLinus Torvalds if (!(dir->i_mode & S_ISVTX)) 23771da177e4SLinus Torvalds return 0; 23788e96e3b7SEric W. Biederman if (uid_eq(inode->i_uid, fsuid)) 23791da177e4SLinus Torvalds return 0; 23808e96e3b7SEric W. Biederman if (uid_eq(dir->i_uid, fsuid)) 23811da177e4SLinus Torvalds return 0; 23821a48e2acSEric W. Biederman return !inode_capable(inode, CAP_FOWNER); 23831da177e4SLinus Torvalds } 23841da177e4SLinus Torvalds 23851da177e4SLinus Torvalds /* 23861da177e4SLinus Torvalds * Check whether we can remove a link victim from directory dir, check 23871da177e4SLinus Torvalds * whether the type of victim is right. 23881da177e4SLinus Torvalds * 1. We can't do it if dir is read-only (done in permission()) 23891da177e4SLinus Torvalds * 2. We should have write and exec permissions on dir 23901da177e4SLinus Torvalds * 3. We can't remove anything from append-only dir 23911da177e4SLinus Torvalds * 4. We can't do anything with immutable dir (done in permission()) 23921da177e4SLinus Torvalds * 5. If the sticky bit on dir is set we should either 23931da177e4SLinus Torvalds * a. be owner of dir, or 23941da177e4SLinus Torvalds * b. be owner of victim, or 23951da177e4SLinus Torvalds * c. have CAP_FOWNER capability 23961da177e4SLinus Torvalds * 6. If the victim is append-only or immutable we can't do antyhing with 23971da177e4SLinus Torvalds * links pointing to it. 23981da177e4SLinus Torvalds * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. 23991da177e4SLinus Torvalds * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. 24001da177e4SLinus Torvalds * 9. We can't remove a root or mountpoint. 24011da177e4SLinus Torvalds * 10. We don't allow removal of NFS sillyrenamed files; it's handled by 24021da177e4SLinus Torvalds * nfs_async_unlink(). 24031da177e4SLinus Torvalds */ 2404b18825a7SDavid Howells static int may_delete(struct inode *dir, struct dentry *victim, bool isdir) 24051da177e4SLinus Torvalds { 2406b18825a7SDavid Howells struct inode *inode = victim->d_inode; 24071da177e4SLinus Torvalds int error; 24081da177e4SLinus Torvalds 2409b18825a7SDavid Howells if (d_is_negative(victim)) 24101da177e4SLinus Torvalds return -ENOENT; 2411b18825a7SDavid Howells BUG_ON(!inode); 24121da177e4SLinus Torvalds 24131da177e4SLinus Torvalds BUG_ON(victim->d_parent->d_inode != dir); 24144fa6b5ecSJeff Layton audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE); 24151da177e4SLinus Torvalds 2416f419a2e3SAl Viro error = inode_permission(dir, MAY_WRITE | MAY_EXEC); 24171da177e4SLinus Torvalds if (error) 24181da177e4SLinus Torvalds return error; 24191da177e4SLinus Torvalds if (IS_APPEND(dir)) 24201da177e4SLinus Torvalds return -EPERM; 2421b18825a7SDavid Howells 2422b18825a7SDavid Howells if (check_sticky(dir, inode) || IS_APPEND(inode) || 2423b18825a7SDavid Howells IS_IMMUTABLE(inode) || IS_SWAPFILE(inode)) 24241da177e4SLinus Torvalds return -EPERM; 24251da177e4SLinus Torvalds if (isdir) { 2426b18825a7SDavid Howells if (!d_is_directory(victim) && !d_is_autodir(victim)) 24271da177e4SLinus Torvalds return -ENOTDIR; 24281da177e4SLinus Torvalds if (IS_ROOT(victim)) 24291da177e4SLinus Torvalds return -EBUSY; 2430b18825a7SDavid Howells } else if (d_is_directory(victim) || d_is_autodir(victim)) 24311da177e4SLinus Torvalds return -EISDIR; 24321da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 24331da177e4SLinus Torvalds return -ENOENT; 24341da177e4SLinus Torvalds if (victim->d_flags & DCACHE_NFSFS_RENAMED) 24351da177e4SLinus Torvalds return -EBUSY; 24361da177e4SLinus Torvalds return 0; 24371da177e4SLinus Torvalds } 24381da177e4SLinus Torvalds 24391da177e4SLinus Torvalds /* Check whether we can create an object with dentry child in directory 24401da177e4SLinus Torvalds * dir. 24411da177e4SLinus Torvalds * 1. We can't do it if child already exists (open has special treatment for 24421da177e4SLinus Torvalds * this case, but since we are inlined it's OK) 24431da177e4SLinus Torvalds * 2. We can't do it if dir is read-only (done in permission()) 24441da177e4SLinus Torvalds * 3. We should have write and exec permissions on dir 24451da177e4SLinus Torvalds * 4. We can't do it if dir is immutable (done in permission()) 24461da177e4SLinus Torvalds */ 2447a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child) 24481da177e4SLinus Torvalds { 244914e972b4SJeff Layton audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE); 24501da177e4SLinus Torvalds if (child->d_inode) 24511da177e4SLinus Torvalds return -EEXIST; 24521da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 24531da177e4SLinus Torvalds return -ENOENT; 2454f419a2e3SAl Viro return inode_permission(dir, MAY_WRITE | MAY_EXEC); 24551da177e4SLinus Torvalds } 24561da177e4SLinus Torvalds 24571da177e4SLinus Torvalds /* 24581da177e4SLinus Torvalds * p1 and p2 should be directories on the same fs. 24591da177e4SLinus Torvalds */ 24601da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2) 24611da177e4SLinus Torvalds { 24621da177e4SLinus Torvalds struct dentry *p; 24631da177e4SLinus Torvalds 24641da177e4SLinus Torvalds if (p1 == p2) { 2465f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 24661da177e4SLinus Torvalds return NULL; 24671da177e4SLinus Torvalds } 24681da177e4SLinus Torvalds 2469a11f3a05SArjan van de Ven mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 24701da177e4SLinus Torvalds 2471e2761a11SOGAWA Hirofumi p = d_ancestor(p2, p1); 2472e2761a11SOGAWA Hirofumi if (p) { 2473f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT); 2474f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD); 24751da177e4SLinus Torvalds return p; 24761da177e4SLinus Torvalds } 24771da177e4SLinus Torvalds 2478e2761a11SOGAWA Hirofumi p = d_ancestor(p1, p2); 2479e2761a11SOGAWA Hirofumi if (p) { 2480f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 2481f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 24821da177e4SLinus Torvalds return p; 24831da177e4SLinus Torvalds } 24841da177e4SLinus Torvalds 2485f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 2486f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 24871da177e4SLinus Torvalds return NULL; 24881da177e4SLinus Torvalds } 24894d359507SAl Viro EXPORT_SYMBOL(lock_rename); 24901da177e4SLinus Torvalds 24911da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2) 24921da177e4SLinus Torvalds { 24931b1dcc1bSJes Sorensen mutex_unlock(&p1->d_inode->i_mutex); 24941da177e4SLinus Torvalds if (p1 != p2) { 24951b1dcc1bSJes Sorensen mutex_unlock(&p2->d_inode->i_mutex); 2496a11f3a05SArjan van de Ven mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 24971da177e4SLinus Torvalds } 24981da177e4SLinus Torvalds } 24994d359507SAl Viro EXPORT_SYMBOL(unlock_rename); 25001da177e4SLinus Torvalds 25014acdaf27SAl Viro int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode, 2502312b63fbSAl Viro bool want_excl) 25031da177e4SLinus Torvalds { 2504a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 25051da177e4SLinus Torvalds if (error) 25061da177e4SLinus Torvalds return error; 25071da177e4SLinus Torvalds 2508acfa4380SAl Viro if (!dir->i_op->create) 25091da177e4SLinus Torvalds return -EACCES; /* shouldn't it be ENOSYS? */ 25101da177e4SLinus Torvalds mode &= S_IALLUGO; 25111da177e4SLinus Torvalds mode |= S_IFREG; 25121da177e4SLinus Torvalds error = security_inode_create(dir, dentry, mode); 25131da177e4SLinus Torvalds if (error) 25141da177e4SLinus Torvalds return error; 2515312b63fbSAl Viro error = dir->i_op->create(dir, dentry, mode, want_excl); 2516a74574aaSStephen Smalley if (!error) 2517f38aa942SAmy Griffis fsnotify_create(dir, dentry); 25181da177e4SLinus Torvalds return error; 25191da177e4SLinus Torvalds } 25204d359507SAl Viro EXPORT_SYMBOL(vfs_create); 25211da177e4SLinus Torvalds 252273d049a4SAl Viro static int may_open(struct path *path, int acc_mode, int flag) 25231da177e4SLinus Torvalds { 25243fb64190SChristoph Hellwig struct dentry *dentry = path->dentry; 25251da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 25261da177e4SLinus Torvalds int error; 25271da177e4SLinus Torvalds 2528bcda7652SAl Viro /* O_PATH? */ 2529bcda7652SAl Viro if (!acc_mode) 2530bcda7652SAl Viro return 0; 2531bcda7652SAl Viro 25321da177e4SLinus Torvalds if (!inode) 25331da177e4SLinus Torvalds return -ENOENT; 25341da177e4SLinus Torvalds 2535c8fe8f30SChristoph Hellwig switch (inode->i_mode & S_IFMT) { 2536c8fe8f30SChristoph Hellwig case S_IFLNK: 25371da177e4SLinus Torvalds return -ELOOP; 2538c8fe8f30SChristoph Hellwig case S_IFDIR: 2539c8fe8f30SChristoph Hellwig if (acc_mode & MAY_WRITE) 25401da177e4SLinus Torvalds return -EISDIR; 2541c8fe8f30SChristoph Hellwig break; 2542c8fe8f30SChristoph Hellwig case S_IFBLK: 2543c8fe8f30SChristoph Hellwig case S_IFCHR: 25443fb64190SChristoph Hellwig if (path->mnt->mnt_flags & MNT_NODEV) 25451da177e4SLinus Torvalds return -EACCES; 2546c8fe8f30SChristoph Hellwig /*FALLTHRU*/ 2547c8fe8f30SChristoph Hellwig case S_IFIFO: 2548c8fe8f30SChristoph Hellwig case S_IFSOCK: 25491da177e4SLinus Torvalds flag &= ~O_TRUNC; 2550c8fe8f30SChristoph Hellwig break; 25514a3fd211SDave Hansen } 2552b41572e9SDave Hansen 25533fb64190SChristoph Hellwig error = inode_permission(inode, acc_mode); 2554b41572e9SDave Hansen if (error) 2555b41572e9SDave Hansen return error; 25566146f0d5SMimi Zohar 25571da177e4SLinus Torvalds /* 25581da177e4SLinus Torvalds * An append-only file must be opened in append mode for writing. 25591da177e4SLinus Torvalds */ 25601da177e4SLinus Torvalds if (IS_APPEND(inode)) { 25618737c930SAl Viro if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND)) 25627715b521SAl Viro return -EPERM; 25631da177e4SLinus Torvalds if (flag & O_TRUNC) 25647715b521SAl Viro return -EPERM; 25651da177e4SLinus Torvalds } 25661da177e4SLinus Torvalds 25671da177e4SLinus Torvalds /* O_NOATIME can only be set by the owner or superuser */ 25682e149670SSerge E. Hallyn if (flag & O_NOATIME && !inode_owner_or_capable(inode)) 25697715b521SAl Viro return -EPERM; 25701da177e4SLinus Torvalds 2571f3c7691eSJ. Bruce Fields return 0; 25727715b521SAl Viro } 25737715b521SAl Viro 2574e1181ee6SJeff Layton static int handle_truncate(struct file *filp) 25757715b521SAl Viro { 2576e1181ee6SJeff Layton struct path *path = &filp->f_path; 25777715b521SAl Viro struct inode *inode = path->dentry->d_inode; 25787715b521SAl Viro int error = get_write_access(inode); 25791da177e4SLinus Torvalds if (error) 25807715b521SAl Viro return error; 25811da177e4SLinus Torvalds /* 25821da177e4SLinus Torvalds * Refuse to truncate files with mandatory locks held on them. 25831da177e4SLinus Torvalds */ 25841da177e4SLinus Torvalds error = locks_verify_locked(inode); 2585be6d3e56SKentaro Takeda if (!error) 2586ea0d3ab2STetsuo Handa error = security_path_truncate(path); 25871da177e4SLinus Torvalds if (!error) { 25887715b521SAl Viro error = do_truncate(path->dentry, 0, 2589d139d7ffSMiklos Szeredi ATTR_MTIME|ATTR_CTIME|ATTR_OPEN, 2590e1181ee6SJeff Layton filp); 25911da177e4SLinus Torvalds } 25921da177e4SLinus Torvalds put_write_access(inode); 2593acd0c935SMimi Zohar return error; 25941da177e4SLinus Torvalds } 25951da177e4SLinus Torvalds 2596d57999e1SDave Hansen static inline int open_to_namei_flags(int flag) 2597d57999e1SDave Hansen { 25988a5e929dSAl Viro if ((flag & O_ACCMODE) == 3) 25998a5e929dSAl Viro flag--; 2600d57999e1SDave Hansen return flag; 2601d57999e1SDave Hansen } 2602d57999e1SDave Hansen 2603d18e9008SMiklos Szeredi static int may_o_create(struct path *dir, struct dentry *dentry, umode_t mode) 2604d18e9008SMiklos Szeredi { 2605d18e9008SMiklos Szeredi int error = security_path_mknod(dir, dentry, mode, 0); 2606d18e9008SMiklos Szeredi if (error) 2607d18e9008SMiklos Szeredi return error; 2608d18e9008SMiklos Szeredi 2609d18e9008SMiklos Szeredi error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC); 2610d18e9008SMiklos Szeredi if (error) 2611d18e9008SMiklos Szeredi return error; 2612d18e9008SMiklos Szeredi 2613d18e9008SMiklos Szeredi return security_inode_create(dir->dentry->d_inode, dentry, mode); 2614d18e9008SMiklos Szeredi } 2615d18e9008SMiklos Szeredi 26161acf0af9SDavid Howells /* 26171acf0af9SDavid Howells * Attempt to atomically look up, create and open a file from a negative 26181acf0af9SDavid Howells * dentry. 26191acf0af9SDavid Howells * 26201acf0af9SDavid Howells * Returns 0 if successful. The file will have been created and attached to 26211acf0af9SDavid Howells * @file by the filesystem calling finish_open(). 26221acf0af9SDavid Howells * 26231acf0af9SDavid Howells * Returns 1 if the file was looked up only or didn't need creating. The 26241acf0af9SDavid Howells * caller will need to perform the open themselves. @path will have been 26251acf0af9SDavid Howells * updated to point to the new dentry. This may be negative. 26261acf0af9SDavid Howells * 26271acf0af9SDavid Howells * Returns an error code otherwise. 26281acf0af9SDavid Howells */ 26292675a4ebSAl Viro static int atomic_open(struct nameidata *nd, struct dentry *dentry, 263030d90494SAl Viro struct path *path, struct file *file, 2631015c3bbcSMiklos Szeredi const struct open_flags *op, 263264894cf8SAl Viro bool got_write, bool need_lookup, 263347237687SAl Viro int *opened) 2634d18e9008SMiklos Szeredi { 2635d18e9008SMiklos Szeredi struct inode *dir = nd->path.dentry->d_inode; 2636d18e9008SMiklos Szeredi unsigned open_flag = open_to_namei_flags(op->open_flag); 2637d18e9008SMiklos Szeredi umode_t mode; 2638d18e9008SMiklos Szeredi int error; 2639d18e9008SMiklos Szeredi int acc_mode; 2640d18e9008SMiklos Szeredi int create_error = 0; 2641d18e9008SMiklos Szeredi struct dentry *const DENTRY_NOT_SET = (void *) -1UL; 2642116cc022SMiklos Szeredi bool excl; 2643d18e9008SMiklos Szeredi 2644d18e9008SMiklos Szeredi BUG_ON(dentry->d_inode); 2645d18e9008SMiklos Szeredi 2646d18e9008SMiklos Szeredi /* Don't create child dentry for a dead directory. */ 2647d18e9008SMiklos Szeredi if (unlikely(IS_DEADDIR(dir))) { 26482675a4ebSAl Viro error = -ENOENT; 2649d18e9008SMiklos Szeredi goto out; 2650d18e9008SMiklos Szeredi } 2651d18e9008SMiklos Szeredi 265262b259d8SMiklos Szeredi mode = op->mode; 2653d18e9008SMiklos Szeredi if ((open_flag & O_CREAT) && !IS_POSIXACL(dir)) 2654d18e9008SMiklos Szeredi mode &= ~current_umask(); 2655d18e9008SMiklos Szeredi 2656116cc022SMiklos Szeredi excl = (open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT); 2657116cc022SMiklos Szeredi if (excl) 2658d18e9008SMiklos Szeredi open_flag &= ~O_TRUNC; 2659d18e9008SMiklos Szeredi 2660d18e9008SMiklos Szeredi /* 2661d18e9008SMiklos Szeredi * Checking write permission is tricky, bacuse we don't know if we are 2662d18e9008SMiklos Szeredi * going to actually need it: O_CREAT opens should work as long as the 2663d18e9008SMiklos Szeredi * file exists. But checking existence breaks atomicity. The trick is 2664d18e9008SMiklos Szeredi * to check access and if not granted clear O_CREAT from the flags. 2665d18e9008SMiklos Szeredi * 2666d18e9008SMiklos Szeredi * Another problem is returing the "right" error value (e.g. for an 2667d18e9008SMiklos Szeredi * O_EXCL open we want to return EEXIST not EROFS). 2668d18e9008SMiklos Szeredi */ 266964894cf8SAl Viro if (((open_flag & (O_CREAT | O_TRUNC)) || 267064894cf8SAl Viro (open_flag & O_ACCMODE) != O_RDONLY) && unlikely(!got_write)) { 267164894cf8SAl Viro if (!(open_flag & O_CREAT)) { 2672d18e9008SMiklos Szeredi /* 2673d18e9008SMiklos Szeredi * No O_CREATE -> atomicity not a requirement -> fall 2674d18e9008SMiklos Szeredi * back to lookup + open 2675d18e9008SMiklos Szeredi */ 2676d18e9008SMiklos Szeredi goto no_open; 2677d18e9008SMiklos Szeredi } else if (open_flag & (O_EXCL | O_TRUNC)) { 2678d18e9008SMiklos Szeredi /* Fall back and fail with the right error */ 267964894cf8SAl Viro create_error = -EROFS; 2680d18e9008SMiklos Szeredi goto no_open; 2681d18e9008SMiklos Szeredi } else { 2682d18e9008SMiklos Szeredi /* No side effects, safe to clear O_CREAT */ 268364894cf8SAl Viro create_error = -EROFS; 2684d18e9008SMiklos Szeredi open_flag &= ~O_CREAT; 2685d18e9008SMiklos Szeredi } 2686d18e9008SMiklos Szeredi } 2687d18e9008SMiklos Szeredi 2688d18e9008SMiklos Szeredi if (open_flag & O_CREAT) { 268938227f78SMiklos Szeredi error = may_o_create(&nd->path, dentry, mode); 2690d18e9008SMiklos Szeredi if (error) { 2691d18e9008SMiklos Szeredi create_error = error; 2692d18e9008SMiklos Szeredi if (open_flag & O_EXCL) 2693d18e9008SMiklos Szeredi goto no_open; 2694d18e9008SMiklos Szeredi open_flag &= ~O_CREAT; 2695d18e9008SMiklos Szeredi } 2696d18e9008SMiklos Szeredi } 2697d18e9008SMiklos Szeredi 2698d18e9008SMiklos Szeredi if (nd->flags & LOOKUP_DIRECTORY) 2699d18e9008SMiklos Szeredi open_flag |= O_DIRECTORY; 2700d18e9008SMiklos Szeredi 270130d90494SAl Viro file->f_path.dentry = DENTRY_NOT_SET; 270230d90494SAl Viro file->f_path.mnt = nd->path.mnt; 270330d90494SAl Viro error = dir->i_op->atomic_open(dir, dentry, file, open_flag, mode, 270447237687SAl Viro opened); 2705d9585277SAl Viro if (error < 0) { 2706d9585277SAl Viro if (create_error && error == -ENOENT) 2707d9585277SAl Viro error = create_error; 2708d18e9008SMiklos Szeredi goto out; 2709d18e9008SMiklos Szeredi } 2710d18e9008SMiklos Szeredi 2711d9585277SAl Viro if (error) { /* returned 1, that is */ 271230d90494SAl Viro if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) { 27132675a4ebSAl Viro error = -EIO; 2714d18e9008SMiklos Szeredi goto out; 2715d18e9008SMiklos Szeredi } 271630d90494SAl Viro if (file->f_path.dentry) { 2717d18e9008SMiklos Szeredi dput(dentry); 271830d90494SAl Viro dentry = file->f_path.dentry; 2719d18e9008SMiklos Szeredi } 272003da633aSAl Viro if (*opened & FILE_CREATED) 272103da633aSAl Viro fsnotify_create(dir, dentry); 272203da633aSAl Viro if (!dentry->d_inode) { 272303da633aSAl Viro WARN_ON(*opened & FILE_CREATED); 272403da633aSAl Viro if (create_error) { 272562b2ce96SSage Weil error = create_error; 272662b2ce96SSage Weil goto out; 272762b2ce96SSage Weil } 272803da633aSAl Viro } else { 272903da633aSAl Viro if (excl && !(*opened & FILE_CREATED)) { 273003da633aSAl Viro error = -EEXIST; 273103da633aSAl Viro goto out; 273203da633aSAl Viro } 273303da633aSAl Viro } 2734d18e9008SMiklos Szeredi goto looked_up; 2735d18e9008SMiklos Szeredi } 2736d18e9008SMiklos Szeredi 2737d18e9008SMiklos Szeredi /* 2738d18e9008SMiklos Szeredi * We didn't have the inode before the open, so check open permission 2739d18e9008SMiklos Szeredi * here. 2740d18e9008SMiklos Szeredi */ 274103da633aSAl Viro acc_mode = op->acc_mode; 274203da633aSAl Viro if (*opened & FILE_CREATED) { 274303da633aSAl Viro WARN_ON(!(open_flag & O_CREAT)); 274403da633aSAl Viro fsnotify_create(dir, dentry); 274503da633aSAl Viro acc_mode = MAY_OPEN; 274603da633aSAl Viro } 27472675a4ebSAl Viro error = may_open(&file->f_path, acc_mode, open_flag); 27482675a4ebSAl Viro if (error) 27492675a4ebSAl Viro fput(file); 2750d18e9008SMiklos Szeredi 2751d18e9008SMiklos Szeredi out: 2752d18e9008SMiklos Szeredi dput(dentry); 27532675a4ebSAl Viro return error; 2754d18e9008SMiklos Szeredi 2755d18e9008SMiklos Szeredi no_open: 2756d18e9008SMiklos Szeredi if (need_lookup) { 275772bd866aSAl Viro dentry = lookup_real(dir, dentry, nd->flags); 2758d18e9008SMiklos Szeredi if (IS_ERR(dentry)) 27592675a4ebSAl Viro return PTR_ERR(dentry); 2760d18e9008SMiklos Szeredi 2761d18e9008SMiklos Szeredi if (create_error) { 2762d18e9008SMiklos Szeredi int open_flag = op->open_flag; 2763d18e9008SMiklos Szeredi 27642675a4ebSAl Viro error = create_error; 2765d18e9008SMiklos Szeredi if ((open_flag & O_EXCL)) { 2766d18e9008SMiklos Szeredi if (!dentry->d_inode) 2767d18e9008SMiklos Szeredi goto out; 2768d18e9008SMiklos Szeredi } else if (!dentry->d_inode) { 2769d18e9008SMiklos Szeredi goto out; 2770d18e9008SMiklos Szeredi } else if ((open_flag & O_TRUNC) && 2771d18e9008SMiklos Szeredi S_ISREG(dentry->d_inode->i_mode)) { 2772d18e9008SMiklos Szeredi goto out; 2773d18e9008SMiklos Szeredi } 2774d18e9008SMiklos Szeredi /* will fail later, go on to get the right error */ 2775d18e9008SMiklos Szeredi } 2776d18e9008SMiklos Szeredi } 2777d18e9008SMiklos Szeredi looked_up: 2778d18e9008SMiklos Szeredi path->dentry = dentry; 2779d18e9008SMiklos Szeredi path->mnt = nd->path.mnt; 27802675a4ebSAl Viro return 1; 2781d18e9008SMiklos Szeredi } 2782d18e9008SMiklos Szeredi 278331e6b01fSNick Piggin /* 27841acf0af9SDavid Howells * Look up and maybe create and open the last component. 2785d58ffd35SMiklos Szeredi * 2786d58ffd35SMiklos Szeredi * Must be called with i_mutex held on parent. 2787d58ffd35SMiklos Szeredi * 27881acf0af9SDavid Howells * Returns 0 if the file was successfully atomically created (if necessary) and 27891acf0af9SDavid Howells * opened. In this case the file will be returned attached to @file. 27901acf0af9SDavid Howells * 27911acf0af9SDavid Howells * Returns 1 if the file was not completely opened at this time, though lookups 27921acf0af9SDavid Howells * and creations will have been performed and the dentry returned in @path will 27931acf0af9SDavid Howells * be positive upon return if O_CREAT was specified. If O_CREAT wasn't 27941acf0af9SDavid Howells * specified then a negative dentry may be returned. 27951acf0af9SDavid Howells * 27961acf0af9SDavid Howells * An error code is returned otherwise. 27971acf0af9SDavid Howells * 27981acf0af9SDavid Howells * FILE_CREATE will be set in @*opened if the dentry was created and will be 27991acf0af9SDavid Howells * cleared otherwise prior to returning. 2800d58ffd35SMiklos Szeredi */ 28012675a4ebSAl Viro static int lookup_open(struct nameidata *nd, struct path *path, 280230d90494SAl Viro struct file *file, 2803d58ffd35SMiklos Szeredi const struct open_flags *op, 280464894cf8SAl Viro bool got_write, int *opened) 2805d58ffd35SMiklos Szeredi { 2806d58ffd35SMiklos Szeredi struct dentry *dir = nd->path.dentry; 280754ef4872SMiklos Szeredi struct inode *dir_inode = dir->d_inode; 2808d58ffd35SMiklos Szeredi struct dentry *dentry; 2809d58ffd35SMiklos Szeredi int error; 281054ef4872SMiklos Szeredi bool need_lookup; 2811d58ffd35SMiklos Szeredi 281247237687SAl Viro *opened &= ~FILE_CREATED; 2813201f956eSAl Viro dentry = lookup_dcache(&nd->last, dir, nd->flags, &need_lookup); 2814d58ffd35SMiklos Szeredi if (IS_ERR(dentry)) 28152675a4ebSAl Viro return PTR_ERR(dentry); 2816d58ffd35SMiklos Szeredi 2817d18e9008SMiklos Szeredi /* Cached positive dentry: will open in f_op->open */ 2818d18e9008SMiklos Szeredi if (!need_lookup && dentry->d_inode) 2819d18e9008SMiklos Szeredi goto out_no_open; 2820d18e9008SMiklos Szeredi 2821d18e9008SMiklos Szeredi if ((nd->flags & LOOKUP_OPEN) && dir_inode->i_op->atomic_open) { 282264894cf8SAl Viro return atomic_open(nd, dentry, path, file, op, got_write, 282347237687SAl Viro need_lookup, opened); 2824d18e9008SMiklos Szeredi } 2825d18e9008SMiklos Szeredi 282654ef4872SMiklos Szeredi if (need_lookup) { 282754ef4872SMiklos Szeredi BUG_ON(dentry->d_inode); 282854ef4872SMiklos Szeredi 282972bd866aSAl Viro dentry = lookup_real(dir_inode, dentry, nd->flags); 283054ef4872SMiklos Szeredi if (IS_ERR(dentry)) 28312675a4ebSAl Viro return PTR_ERR(dentry); 283254ef4872SMiklos Szeredi } 283354ef4872SMiklos Szeredi 2834d58ffd35SMiklos Szeredi /* Negative dentry, just create the file */ 2835d58ffd35SMiklos Szeredi if (!dentry->d_inode && (op->open_flag & O_CREAT)) { 2836d58ffd35SMiklos Szeredi umode_t mode = op->mode; 2837d58ffd35SMiklos Szeredi if (!IS_POSIXACL(dir->d_inode)) 2838d58ffd35SMiklos Szeredi mode &= ~current_umask(); 2839d58ffd35SMiklos Szeredi /* 2840d58ffd35SMiklos Szeredi * This write is needed to ensure that a 2841d58ffd35SMiklos Szeredi * rw->ro transition does not occur between 2842d58ffd35SMiklos Szeredi * the time when the file is created and when 2843d58ffd35SMiklos Szeredi * a permanent write count is taken through 2844015c3bbcSMiklos Szeredi * the 'struct file' in finish_open(). 2845d58ffd35SMiklos Szeredi */ 284664894cf8SAl Viro if (!got_write) { 284764894cf8SAl Viro error = -EROFS; 2848d58ffd35SMiklos Szeredi goto out_dput; 284964894cf8SAl Viro } 285047237687SAl Viro *opened |= FILE_CREATED; 2851d58ffd35SMiklos Szeredi error = security_path_mknod(&nd->path, dentry, mode, 0); 2852d58ffd35SMiklos Szeredi if (error) 2853d58ffd35SMiklos Szeredi goto out_dput; 2854312b63fbSAl Viro error = vfs_create(dir->d_inode, dentry, mode, 2855312b63fbSAl Viro nd->flags & LOOKUP_EXCL); 2856d58ffd35SMiklos Szeredi if (error) 2857d58ffd35SMiklos Szeredi goto out_dput; 2858d58ffd35SMiklos Szeredi } 2859d18e9008SMiklos Szeredi out_no_open: 2860d58ffd35SMiklos Szeredi path->dentry = dentry; 2861d58ffd35SMiklos Szeredi path->mnt = nd->path.mnt; 28622675a4ebSAl Viro return 1; 2863d58ffd35SMiklos Szeredi 2864d58ffd35SMiklos Szeredi out_dput: 2865d58ffd35SMiklos Szeredi dput(dentry); 28662675a4ebSAl Viro return error; 2867d58ffd35SMiklos Szeredi } 2868d58ffd35SMiklos Szeredi 2869d58ffd35SMiklos Szeredi /* 2870fe2d35ffSAl Viro * Handle the last step of open() 287131e6b01fSNick Piggin */ 28722675a4ebSAl Viro static int do_last(struct nameidata *nd, struct path *path, 287330d90494SAl Viro struct file *file, const struct open_flags *op, 2874669abf4eSJeff Layton int *opened, struct filename *name) 2875fb1cc555SAl Viro { 2876a1e28038SAl Viro struct dentry *dir = nd->path.dentry; 2877ca344a89SAl Viro int open_flag = op->open_flag; 287877d660a8SMiklos Szeredi bool will_truncate = (open_flag & O_TRUNC) != 0; 287964894cf8SAl Viro bool got_write = false; 2880bcda7652SAl Viro int acc_mode = op->acc_mode; 2881a1eb3315SMiklos Szeredi struct inode *inode; 288277d660a8SMiklos Szeredi bool symlink_ok = false; 288316b1c1cdSMiklos Szeredi struct path save_parent = { .dentry = NULL, .mnt = NULL }; 288416b1c1cdSMiklos Szeredi bool retried = false; 288516c2cd71SAl Viro int error; 2886fb1cc555SAl Viro 2887c3e380b0SAl Viro nd->flags &= ~LOOKUP_PARENT; 2888c3e380b0SAl Viro nd->flags |= op->intent; 2889c3e380b0SAl Viro 2890bc77daa7SAl Viro if (nd->last_type != LAST_NORM) { 2891fe2d35ffSAl Viro error = handle_dots(nd, nd->last_type); 2892fe2d35ffSAl Viro if (error) 28932675a4ebSAl Viro return error; 2894e83db167SMiklos Szeredi goto finish_open; 28951f36f774SAl Viro } 2896a2c36b45SAl Viro 2897ca344a89SAl Viro if (!(open_flag & O_CREAT)) { 2898fe2d35ffSAl Viro if (nd->last.name[nd->last.len]) 2899fe2d35ffSAl Viro nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 2900bcda7652SAl Viro if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW)) 290177d660a8SMiklos Szeredi symlink_ok = true; 2902fe2d35ffSAl Viro /* we _can_ be in RCU mode here */ 2903e97cdc87SAl Viro error = lookup_fast(nd, path, &inode); 290471574865SMiklos Szeredi if (likely(!error)) 290571574865SMiklos Szeredi goto finish_lookup; 290671574865SMiklos Szeredi 2907ce57dfc1SAl Viro if (error < 0) 29082675a4ebSAl Viro goto out; 2909a1eb3315SMiklos Szeredi 291037d7fffcSMiklos Szeredi BUG_ON(nd->inode != dir->d_inode); 2911b6183df7SMiklos Szeredi } else { 2912fe2d35ffSAl Viro /* create side of things */ 2913a3fbbde7SAl Viro /* 2914b6183df7SMiklos Szeredi * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED 2915b6183df7SMiklos Szeredi * has been cleared when we got to the last component we are 2916b6183df7SMiklos Szeredi * about to look up 2917a3fbbde7SAl Viro */ 29189f1fafeeSAl Viro error = complete_walk(nd); 29199f1fafeeSAl Viro if (error) 29202675a4ebSAl Viro return error; 2921fe2d35ffSAl Viro 292233e2208aSJeff Layton audit_inode(name, dir, LOOKUP_PARENT); 292316c2cd71SAl Viro error = -EISDIR; 29241f36f774SAl Viro /* trailing slashes? */ 292531e6b01fSNick Piggin if (nd->last.name[nd->last.len]) 29262675a4ebSAl Viro goto out; 2927b6183df7SMiklos Szeredi } 29281f36f774SAl Viro 292916b1c1cdSMiklos Szeredi retry_lookup: 293064894cf8SAl Viro if (op->open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) { 293164894cf8SAl Viro error = mnt_want_write(nd->path.mnt); 293264894cf8SAl Viro if (!error) 293364894cf8SAl Viro got_write = true; 293464894cf8SAl Viro /* 293564894cf8SAl Viro * do _not_ fail yet - we might not need that or fail with 293664894cf8SAl Viro * a different error; let lookup_open() decide; we'll be 293764894cf8SAl Viro * dropping this one anyway. 293864894cf8SAl Viro */ 293964894cf8SAl Viro } 2940a1e28038SAl Viro mutex_lock(&dir->d_inode->i_mutex); 294164894cf8SAl Viro error = lookup_open(nd, path, file, op, got_write, opened); 2942fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2943fb1cc555SAl Viro 29442675a4ebSAl Viro if (error <= 0) { 29452675a4ebSAl Viro if (error) 2946d58ffd35SMiklos Szeredi goto out; 29476c0d46c4SAl Viro 294847237687SAl Viro if ((*opened & FILE_CREATED) || 2949496ad9aaSAl Viro !S_ISREG(file_inode(file)->i_mode)) 295077d660a8SMiklos Szeredi will_truncate = false; 2951d18e9008SMiklos Szeredi 2952adb5c247SJeff Layton audit_inode(name, file->f_path.dentry, 0); 2953d18e9008SMiklos Szeredi goto opened; 2954d18e9008SMiklos Szeredi } 2955d18e9008SMiklos Szeredi 295647237687SAl Viro if (*opened & FILE_CREATED) { 29579b44f1b3SAl Viro /* Don't check for write permission, don't truncate */ 2958ca344a89SAl Viro open_flag &= ~O_TRUNC; 295977d660a8SMiklos Szeredi will_truncate = false; 2960bcda7652SAl Viro acc_mode = MAY_OPEN; 2961d58ffd35SMiklos Szeredi path_to_nameidata(path, nd); 2962e83db167SMiklos Szeredi goto finish_open_created; 2963fb1cc555SAl Viro } 2964fb1cc555SAl Viro 2965fb1cc555SAl Viro /* 29663134f37eSJeff Layton * create/update audit record if it already exists. 2967fb1cc555SAl Viro */ 2968b18825a7SDavid Howells if (d_is_positive(path->dentry)) 2969adb5c247SJeff Layton audit_inode(name, path->dentry, 0); 2970fb1cc555SAl Viro 2971d18e9008SMiklos Szeredi /* 2972d18e9008SMiklos Szeredi * If atomic_open() acquired write access it is dropped now due to 2973d18e9008SMiklos Szeredi * possible mount and symlink following (this might be optimized away if 2974d18e9008SMiklos Szeredi * necessary...) 2975d18e9008SMiklos Szeredi */ 297664894cf8SAl Viro if (got_write) { 2977d18e9008SMiklos Szeredi mnt_drop_write(nd->path.mnt); 297864894cf8SAl Viro got_write = false; 2979d18e9008SMiklos Szeredi } 2980d18e9008SMiklos Szeredi 2981fb1cc555SAl Viro error = -EEXIST; 2982f8310c59SAl Viro if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT)) 2983fb1cc555SAl Viro goto exit_dput; 2984fb1cc555SAl Viro 29859875cf80SDavid Howells error = follow_managed(path, nd->flags); 29869875cf80SDavid Howells if (error < 0) 2987fb1cc555SAl Viro goto exit_dput; 2988fb1cc555SAl Viro 2989a3fbbde7SAl Viro if (error) 2990a3fbbde7SAl Viro nd->flags |= LOOKUP_JUMPED; 2991a3fbbde7SAl Viro 2992decf3400SMiklos Szeredi BUG_ON(nd->flags & LOOKUP_RCU); 2993decf3400SMiklos Szeredi inode = path->dentry->d_inode; 29945f5daac1SMiklos Szeredi finish_lookup: 29955f5daac1SMiklos Szeredi /* we _can_ be in RCU mode here */ 2996fb1cc555SAl Viro error = -ENOENT; 2997b18825a7SDavid Howells if (d_is_negative(path->dentry)) { 299854c33e7fSMiklos Szeredi path_to_nameidata(path, nd); 29992675a4ebSAl Viro goto out; 300054c33e7fSMiklos Szeredi } 30019e67f361SAl Viro 3002b18825a7SDavid Howells if (should_follow_link(path->dentry, !symlink_ok)) { 3003d45ea867SMiklos Szeredi if (nd->flags & LOOKUP_RCU) { 3004d45ea867SMiklos Szeredi if (unlikely(unlazy_walk(nd, path->dentry))) { 3005d45ea867SMiklos Szeredi error = -ECHILD; 30062675a4ebSAl Viro goto out; 3007d45ea867SMiklos Szeredi } 3008d45ea867SMiklos Szeredi } 3009d45ea867SMiklos Szeredi BUG_ON(inode != path->dentry->d_inode); 30102675a4ebSAl Viro return 1; 3011d45ea867SMiklos Szeredi } 3012fb1cc555SAl Viro 301316b1c1cdSMiklos Szeredi if ((nd->flags & LOOKUP_RCU) || nd->path.mnt != path->mnt) { 3014fb1cc555SAl Viro path_to_nameidata(path, nd); 301516b1c1cdSMiklos Szeredi } else { 301616b1c1cdSMiklos Szeredi save_parent.dentry = nd->path.dentry; 301716b1c1cdSMiklos Szeredi save_parent.mnt = mntget(path->mnt); 301816b1c1cdSMiklos Szeredi nd->path.dentry = path->dentry; 301916b1c1cdSMiklos Szeredi 302016b1c1cdSMiklos Szeredi } 3021decf3400SMiklos Szeredi nd->inode = inode; 3022a3fbbde7SAl Viro /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */ 3023bc77daa7SAl Viro finish_open: 3024a3fbbde7SAl Viro error = complete_walk(nd); 302516b1c1cdSMiklos Szeredi if (error) { 302616b1c1cdSMiklos Szeredi path_put(&save_parent); 30272675a4ebSAl Viro return error; 302816b1c1cdSMiklos Szeredi } 3029bc77daa7SAl Viro audit_inode(name, nd->path.dentry, 0); 3030fb1cc555SAl Viro error = -EISDIR; 3031b18825a7SDavid Howells if ((open_flag & O_CREAT) && 3032b18825a7SDavid Howells (d_is_directory(nd->path.dentry) || d_is_autodir(nd->path.dentry))) 30332675a4ebSAl Viro goto out; 3034af2f5542SMiklos Szeredi error = -ENOTDIR; 3035b18825a7SDavid Howells if ((nd->flags & LOOKUP_DIRECTORY) && !d_is_directory(nd->path.dentry)) 30362675a4ebSAl Viro goto out; 30376c0d46c4SAl Viro if (!S_ISREG(nd->inode->i_mode)) 303877d660a8SMiklos Szeredi will_truncate = false; 30396c0d46c4SAl Viro 30400f9d1a10SAl Viro if (will_truncate) { 30410f9d1a10SAl Viro error = mnt_want_write(nd->path.mnt); 30420f9d1a10SAl Viro if (error) 30432675a4ebSAl Viro goto out; 304464894cf8SAl Viro got_write = true; 30450f9d1a10SAl Viro } 3046e83db167SMiklos Szeredi finish_open_created: 3047bcda7652SAl Viro error = may_open(&nd->path, acc_mode, open_flag); 3048ca344a89SAl Viro if (error) 30492675a4ebSAl Viro goto out; 305030d90494SAl Viro file->f_path.mnt = nd->path.mnt; 305130d90494SAl Viro error = finish_open(file, nd->path.dentry, NULL, opened); 305230d90494SAl Viro if (error) { 305330d90494SAl Viro if (error == -EOPENSTALE) 3054f60dc3dbSMiklos Szeredi goto stale_open; 3055015c3bbcSMiklos Szeredi goto out; 3056f60dc3dbSMiklos Szeredi } 3057a8277b9bSMiklos Szeredi opened: 30582675a4ebSAl Viro error = open_check_o_direct(file); 3059015c3bbcSMiklos Szeredi if (error) 3060015c3bbcSMiklos Szeredi goto exit_fput; 30612675a4ebSAl Viro error = ima_file_check(file, op->acc_mode); 3062aa4caadbSMiklos Szeredi if (error) 3063aa4caadbSMiklos Szeredi goto exit_fput; 3064aa4caadbSMiklos Szeredi 30650f9d1a10SAl Viro if (will_truncate) { 30662675a4ebSAl Viro error = handle_truncate(file); 3067aa4caadbSMiklos Szeredi if (error) 3068aa4caadbSMiklos Szeredi goto exit_fput; 30690f9d1a10SAl Viro } 3070ca344a89SAl Viro out: 307164894cf8SAl Viro if (got_write) 30720f9d1a10SAl Viro mnt_drop_write(nd->path.mnt); 307316b1c1cdSMiklos Szeredi path_put(&save_parent); 3074e276ae67SMiklos Szeredi terminate_walk(nd); 30752675a4ebSAl Viro return error; 3076fb1cc555SAl Viro 3077fb1cc555SAl Viro exit_dput: 3078fb1cc555SAl Viro path_put_conditional(path, nd); 3079ca344a89SAl Viro goto out; 3080015c3bbcSMiklos Szeredi exit_fput: 30812675a4ebSAl Viro fput(file); 30822675a4ebSAl Viro goto out; 3083015c3bbcSMiklos Szeredi 3084f60dc3dbSMiklos Szeredi stale_open: 3085f60dc3dbSMiklos Szeredi /* If no saved parent or already retried then can't retry */ 3086f60dc3dbSMiklos Szeredi if (!save_parent.dentry || retried) 3087f60dc3dbSMiklos Szeredi goto out; 3088f60dc3dbSMiklos Szeredi 3089f60dc3dbSMiklos Szeredi BUG_ON(save_parent.dentry != dir); 3090f60dc3dbSMiklos Szeredi path_put(&nd->path); 3091f60dc3dbSMiklos Szeredi nd->path = save_parent; 3092f60dc3dbSMiklos Szeredi nd->inode = dir->d_inode; 3093f60dc3dbSMiklos Szeredi save_parent.mnt = NULL; 3094f60dc3dbSMiklos Szeredi save_parent.dentry = NULL; 309564894cf8SAl Viro if (got_write) { 3096f60dc3dbSMiklos Szeredi mnt_drop_write(nd->path.mnt); 309764894cf8SAl Viro got_write = false; 3098f60dc3dbSMiklos Szeredi } 3099f60dc3dbSMiklos Szeredi retried = true; 3100f60dc3dbSMiklos Szeredi goto retry_lookup; 3101fb1cc555SAl Viro } 3102fb1cc555SAl Viro 310360545d0dSAl Viro static int do_tmpfile(int dfd, struct filename *pathname, 310460545d0dSAl Viro struct nameidata *nd, int flags, 310560545d0dSAl Viro const struct open_flags *op, 310660545d0dSAl Viro struct file *file, int *opened) 310760545d0dSAl Viro { 310860545d0dSAl Viro static const struct qstr name = QSTR_INIT("/", 1); 310960545d0dSAl Viro struct dentry *dentry, *child; 311060545d0dSAl Viro struct inode *dir; 311160545d0dSAl Viro int error = path_lookupat(dfd, pathname->name, 311260545d0dSAl Viro flags | LOOKUP_DIRECTORY, nd); 311360545d0dSAl Viro if (unlikely(error)) 311460545d0dSAl Viro return error; 311560545d0dSAl Viro error = mnt_want_write(nd->path.mnt); 311660545d0dSAl Viro if (unlikely(error)) 311760545d0dSAl Viro goto out; 311860545d0dSAl Viro /* we want directory to be writable */ 311960545d0dSAl Viro error = inode_permission(nd->inode, MAY_WRITE | MAY_EXEC); 312060545d0dSAl Viro if (error) 312160545d0dSAl Viro goto out2; 312260545d0dSAl Viro dentry = nd->path.dentry; 312360545d0dSAl Viro dir = dentry->d_inode; 312460545d0dSAl Viro if (!dir->i_op->tmpfile) { 312560545d0dSAl Viro error = -EOPNOTSUPP; 312660545d0dSAl Viro goto out2; 312760545d0dSAl Viro } 312860545d0dSAl Viro child = d_alloc(dentry, &name); 312960545d0dSAl Viro if (unlikely(!child)) { 313060545d0dSAl Viro error = -ENOMEM; 313160545d0dSAl Viro goto out2; 313260545d0dSAl Viro } 313360545d0dSAl Viro nd->flags &= ~LOOKUP_DIRECTORY; 313460545d0dSAl Viro nd->flags |= op->intent; 313560545d0dSAl Viro dput(nd->path.dentry); 313660545d0dSAl Viro nd->path.dentry = child; 313760545d0dSAl Viro error = dir->i_op->tmpfile(dir, nd->path.dentry, op->mode); 313860545d0dSAl Viro if (error) 313960545d0dSAl Viro goto out2; 314060545d0dSAl Viro audit_inode(pathname, nd->path.dentry, 0); 314160545d0dSAl Viro error = may_open(&nd->path, op->acc_mode, op->open_flag); 314260545d0dSAl Viro if (error) 314360545d0dSAl Viro goto out2; 314460545d0dSAl Viro file->f_path.mnt = nd->path.mnt; 314560545d0dSAl Viro error = finish_open(file, nd->path.dentry, NULL, opened); 314660545d0dSAl Viro if (error) 314760545d0dSAl Viro goto out2; 314860545d0dSAl Viro error = open_check_o_direct(file); 3149f4e0c30cSAl Viro if (error) { 315060545d0dSAl Viro fput(file); 3151f4e0c30cSAl Viro } else if (!(op->open_flag & O_EXCL)) { 3152f4e0c30cSAl Viro struct inode *inode = file_inode(file); 3153f4e0c30cSAl Viro spin_lock(&inode->i_lock); 3154f4e0c30cSAl Viro inode->i_state |= I_LINKABLE; 3155f4e0c30cSAl Viro spin_unlock(&inode->i_lock); 3156f4e0c30cSAl Viro } 315760545d0dSAl Viro out2: 315860545d0dSAl Viro mnt_drop_write(nd->path.mnt); 315960545d0dSAl Viro out: 316060545d0dSAl Viro path_put(&nd->path); 316160545d0dSAl Viro return error; 316260545d0dSAl Viro } 316360545d0dSAl Viro 3164669abf4eSJeff Layton static struct file *path_openat(int dfd, struct filename *pathname, 316573d049a4SAl Viro struct nameidata *nd, const struct open_flags *op, int flags) 31661da177e4SLinus Torvalds { 3167fe2d35ffSAl Viro struct file *base = NULL; 316830d90494SAl Viro struct file *file; 31699850c056SAl Viro struct path path; 317047237687SAl Viro int opened = 0; 317113aab428SAl Viro int error; 317231e6b01fSNick Piggin 317330d90494SAl Viro file = get_empty_filp(); 31741afc99beSAl Viro if (IS_ERR(file)) 31751afc99beSAl Viro return file; 317631e6b01fSNick Piggin 317730d90494SAl Viro file->f_flags = op->open_flag; 317831e6b01fSNick Piggin 3179bb458c64SAl Viro if (unlikely(file->f_flags & __O_TMPFILE)) { 318060545d0dSAl Viro error = do_tmpfile(dfd, pathname, nd, flags, op, file, &opened); 318160545d0dSAl Viro goto out; 318260545d0dSAl Viro } 318360545d0dSAl Viro 3184669abf4eSJeff Layton error = path_init(dfd, pathname->name, flags | LOOKUP_PARENT, nd, &base); 318531e6b01fSNick Piggin if (unlikely(error)) 31862675a4ebSAl Viro goto out; 318731e6b01fSNick Piggin 3188fe2d35ffSAl Viro current->total_link_count = 0; 3189669abf4eSJeff Layton error = link_path_walk(pathname->name, nd); 319031e6b01fSNick Piggin if (unlikely(error)) 31912675a4ebSAl Viro goto out; 31921da177e4SLinus Torvalds 31932675a4ebSAl Viro error = do_last(nd, &path, file, op, &opened, pathname); 31942675a4ebSAl Viro while (unlikely(error > 0)) { /* trailing symlink */ 31957b9337aaSNick Piggin struct path link = path; 3196def4af30SAl Viro void *cookie; 3197574197e0SAl Viro if (!(nd->flags & LOOKUP_FOLLOW)) { 319873d049a4SAl Viro path_put_conditional(&path, nd); 319973d049a4SAl Viro path_put(&nd->path); 32002675a4ebSAl Viro error = -ELOOP; 320140b39136SAl Viro break; 320240b39136SAl Viro } 3203800179c9SKees Cook error = may_follow_link(&link, nd); 3204800179c9SKees Cook if (unlikely(error)) 3205800179c9SKees Cook break; 320673d049a4SAl Viro nd->flags |= LOOKUP_PARENT; 320773d049a4SAl Viro nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL); 3208574197e0SAl Viro error = follow_link(&link, nd, &cookie); 3209c3e380b0SAl Viro if (unlikely(error)) 32102675a4ebSAl Viro break; 32112675a4ebSAl Viro error = do_last(nd, &path, file, op, &opened, pathname); 3212574197e0SAl Viro put_link(nd, &link, cookie); 3213806b681cSAl Viro } 321410fa8e62SAl Viro out: 321573d049a4SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) 321673d049a4SAl Viro path_put(&nd->root); 3217fe2d35ffSAl Viro if (base) 3218fe2d35ffSAl Viro fput(base); 32192675a4ebSAl Viro if (!(opened & FILE_OPENED)) { 32202675a4ebSAl Viro BUG_ON(!error); 322130d90494SAl Viro put_filp(file); 3222015c3bbcSMiklos Szeredi } 32232675a4ebSAl Viro if (unlikely(error)) { 32242675a4ebSAl Viro if (error == -EOPENSTALE) { 32252675a4ebSAl Viro if (flags & LOOKUP_RCU) 32262675a4ebSAl Viro error = -ECHILD; 32272675a4ebSAl Viro else 32282675a4ebSAl Viro error = -ESTALE; 32292675a4ebSAl Viro } 32302675a4ebSAl Viro file = ERR_PTR(error); 32312675a4ebSAl Viro } 32322675a4ebSAl Viro return file; 3233de459215SKirill Korotaev } 32341da177e4SLinus Torvalds 3235669abf4eSJeff Layton struct file *do_filp_open(int dfd, struct filename *pathname, 3236f9652e10SAl Viro const struct open_flags *op) 323713aab428SAl Viro { 323873d049a4SAl Viro struct nameidata nd; 3239f9652e10SAl Viro int flags = op->lookup_flags; 324013aab428SAl Viro struct file *filp; 324113aab428SAl Viro 324273d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU); 324313aab428SAl Viro if (unlikely(filp == ERR_PTR(-ECHILD))) 324473d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags); 324513aab428SAl Viro if (unlikely(filp == ERR_PTR(-ESTALE))) 324673d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL); 324713aab428SAl Viro return filp; 324813aab428SAl Viro } 324913aab428SAl Viro 325073d049a4SAl Viro struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt, 3251f9652e10SAl Viro const char *name, const struct open_flags *op) 325273d049a4SAl Viro { 325373d049a4SAl Viro struct nameidata nd; 325473d049a4SAl Viro struct file *file; 3255669abf4eSJeff Layton struct filename filename = { .name = name }; 3256f9652e10SAl Viro int flags = op->lookup_flags | LOOKUP_ROOT; 325773d049a4SAl Viro 325873d049a4SAl Viro nd.root.mnt = mnt; 325973d049a4SAl Viro nd.root.dentry = dentry; 326073d049a4SAl Viro 3261b18825a7SDavid Howells if (d_is_symlink(dentry) && op->intent & LOOKUP_OPEN) 326273d049a4SAl Viro return ERR_PTR(-ELOOP); 326373d049a4SAl Viro 3264669abf4eSJeff Layton file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_RCU); 326573d049a4SAl Viro if (unlikely(file == ERR_PTR(-ECHILD))) 3266669abf4eSJeff Layton file = path_openat(-1, &filename, &nd, op, flags); 326773d049a4SAl Viro if (unlikely(file == ERR_PTR(-ESTALE))) 3268669abf4eSJeff Layton file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_REVAL); 326973d049a4SAl Viro return file; 327073d049a4SAl Viro } 327173d049a4SAl Viro 32721ac12b4bSJeff Layton struct dentry *kern_path_create(int dfd, const char *pathname, 32731ac12b4bSJeff Layton struct path *path, unsigned int lookup_flags) 32741da177e4SLinus Torvalds { 3275c663e5d8SChristoph Hellwig struct dentry *dentry = ERR_PTR(-EEXIST); 3276ed75e95dSAl Viro struct nameidata nd; 3277c30dabfeSJan Kara int err2; 32781ac12b4bSJeff Layton int error; 32791ac12b4bSJeff Layton bool is_dir = (lookup_flags & LOOKUP_DIRECTORY); 32801ac12b4bSJeff Layton 32811ac12b4bSJeff Layton /* 32821ac12b4bSJeff Layton * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any 32831ac12b4bSJeff Layton * other flags passed in are ignored! 32841ac12b4bSJeff Layton */ 32851ac12b4bSJeff Layton lookup_flags &= LOOKUP_REVAL; 32861ac12b4bSJeff Layton 32871ac12b4bSJeff Layton error = do_path_lookup(dfd, pathname, LOOKUP_PARENT|lookup_flags, &nd); 3288ed75e95dSAl Viro if (error) 3289ed75e95dSAl Viro return ERR_PTR(error); 32901da177e4SLinus Torvalds 3291c663e5d8SChristoph Hellwig /* 3292c663e5d8SChristoph Hellwig * Yucky last component or no last component at all? 3293c663e5d8SChristoph Hellwig * (foo/., foo/.., /////) 3294c663e5d8SChristoph Hellwig */ 3295ed75e95dSAl Viro if (nd.last_type != LAST_NORM) 3296ed75e95dSAl Viro goto out; 3297ed75e95dSAl Viro nd.flags &= ~LOOKUP_PARENT; 3298ed75e95dSAl Viro nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL; 3299c663e5d8SChristoph Hellwig 3300c30dabfeSJan Kara /* don't fail immediately if it's r/o, at least try to report other errors */ 3301c30dabfeSJan Kara err2 = mnt_want_write(nd.path.mnt); 3302c663e5d8SChristoph Hellwig /* 3303c663e5d8SChristoph Hellwig * Do the final lookup. 3304c663e5d8SChristoph Hellwig */ 3305ed75e95dSAl Viro mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 3306ed75e95dSAl Viro dentry = lookup_hash(&nd); 33071da177e4SLinus Torvalds if (IS_ERR(dentry)) 3308a8104a9fSAl Viro goto unlock; 3309c663e5d8SChristoph Hellwig 3310a8104a9fSAl Viro error = -EEXIST; 3311b18825a7SDavid Howells if (d_is_positive(dentry)) 3312a8104a9fSAl Viro goto fail; 3313b18825a7SDavid Howells 3314c663e5d8SChristoph Hellwig /* 3315c663e5d8SChristoph Hellwig * Special case - lookup gave negative, but... we had foo/bar/ 3316c663e5d8SChristoph Hellwig * From the vfs_mknod() POV we just have a negative dentry - 3317c663e5d8SChristoph Hellwig * all is fine. Let's be bastards - you had / on the end, you've 3318c663e5d8SChristoph Hellwig * been asking for (non-existent) directory. -ENOENT for you. 3319c663e5d8SChristoph Hellwig */ 3320ed75e95dSAl Viro if (unlikely(!is_dir && nd.last.name[nd.last.len])) { 3321a8104a9fSAl Viro error = -ENOENT; 3322ed75e95dSAl Viro goto fail; 3323e9baf6e5SAl Viro } 3324c30dabfeSJan Kara if (unlikely(err2)) { 3325c30dabfeSJan Kara error = err2; 3326a8104a9fSAl Viro goto fail; 3327c30dabfeSJan Kara } 3328ed75e95dSAl Viro *path = nd.path; 3329e9baf6e5SAl Viro return dentry; 33301da177e4SLinus Torvalds fail: 3331a8104a9fSAl Viro dput(dentry); 3332a8104a9fSAl Viro dentry = ERR_PTR(error); 3333a8104a9fSAl Viro unlock: 3334dae6ad8fSAl Viro mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 3335c30dabfeSJan Kara if (!err2) 3336c30dabfeSJan Kara mnt_drop_write(nd.path.mnt); 3337ed75e95dSAl Viro out: 3338dae6ad8fSAl Viro path_put(&nd.path); 3339ed75e95dSAl Viro return dentry; 3340dae6ad8fSAl Viro } 3341dae6ad8fSAl Viro EXPORT_SYMBOL(kern_path_create); 3342dae6ad8fSAl Viro 3343921a1650SAl Viro void done_path_create(struct path *path, struct dentry *dentry) 3344921a1650SAl Viro { 3345921a1650SAl Viro dput(dentry); 3346921a1650SAl Viro mutex_unlock(&path->dentry->d_inode->i_mutex); 3347a8104a9fSAl Viro mnt_drop_write(path->mnt); 3348921a1650SAl Viro path_put(path); 3349921a1650SAl Viro } 3350921a1650SAl Viro EXPORT_SYMBOL(done_path_create); 3351921a1650SAl Viro 33521ac12b4bSJeff Layton struct dentry *user_path_create(int dfd, const char __user *pathname, 33531ac12b4bSJeff Layton struct path *path, unsigned int lookup_flags) 3354dae6ad8fSAl Viro { 335591a27b2aSJeff Layton struct filename *tmp = getname(pathname); 3356dae6ad8fSAl Viro struct dentry *res; 3357dae6ad8fSAl Viro if (IS_ERR(tmp)) 3358dae6ad8fSAl Viro return ERR_CAST(tmp); 33591ac12b4bSJeff Layton res = kern_path_create(dfd, tmp->name, path, lookup_flags); 3360dae6ad8fSAl Viro putname(tmp); 3361dae6ad8fSAl Viro return res; 3362dae6ad8fSAl Viro } 3363dae6ad8fSAl Viro EXPORT_SYMBOL(user_path_create); 3364dae6ad8fSAl Viro 33651a67aafbSAl Viro int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev) 33661da177e4SLinus Torvalds { 3367a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 33681da177e4SLinus Torvalds 33691da177e4SLinus Torvalds if (error) 33701da177e4SLinus Torvalds return error; 33711da177e4SLinus Torvalds 3372975d6b39SEric W. Biederman if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD)) 33731da177e4SLinus Torvalds return -EPERM; 33741da177e4SLinus Torvalds 3375acfa4380SAl Viro if (!dir->i_op->mknod) 33761da177e4SLinus Torvalds return -EPERM; 33771da177e4SLinus Torvalds 337808ce5f16SSerge E. Hallyn error = devcgroup_inode_mknod(mode, dev); 337908ce5f16SSerge E. Hallyn if (error) 338008ce5f16SSerge E. Hallyn return error; 338108ce5f16SSerge E. Hallyn 33821da177e4SLinus Torvalds error = security_inode_mknod(dir, dentry, mode, dev); 33831da177e4SLinus Torvalds if (error) 33841da177e4SLinus Torvalds return error; 33851da177e4SLinus Torvalds 33861da177e4SLinus Torvalds error = dir->i_op->mknod(dir, dentry, mode, dev); 3387a74574aaSStephen Smalley if (!error) 3388f38aa942SAmy Griffis fsnotify_create(dir, dentry); 33891da177e4SLinus Torvalds return error; 33901da177e4SLinus Torvalds } 33914d359507SAl Viro EXPORT_SYMBOL(vfs_mknod); 33921da177e4SLinus Torvalds 3393f69aac00SAl Viro static int may_mknod(umode_t mode) 3394463c3197SDave Hansen { 3395463c3197SDave Hansen switch (mode & S_IFMT) { 3396463c3197SDave Hansen case S_IFREG: 3397463c3197SDave Hansen case S_IFCHR: 3398463c3197SDave Hansen case S_IFBLK: 3399463c3197SDave Hansen case S_IFIFO: 3400463c3197SDave Hansen case S_IFSOCK: 3401463c3197SDave Hansen case 0: /* zero mode translates to S_IFREG */ 3402463c3197SDave Hansen return 0; 3403463c3197SDave Hansen case S_IFDIR: 3404463c3197SDave Hansen return -EPERM; 3405463c3197SDave Hansen default: 3406463c3197SDave Hansen return -EINVAL; 3407463c3197SDave Hansen } 3408463c3197SDave Hansen } 3409463c3197SDave Hansen 34108208a22bSAl Viro SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode, 34112e4d0924SHeiko Carstens unsigned, dev) 34121da177e4SLinus Torvalds { 34131da177e4SLinus Torvalds struct dentry *dentry; 3414dae6ad8fSAl Viro struct path path; 3415dae6ad8fSAl Viro int error; 3416972567f1SJeff Layton unsigned int lookup_flags = 0; 34171da177e4SLinus Torvalds 34188e4bfca1SAl Viro error = may_mknod(mode); 34198e4bfca1SAl Viro if (error) 34208e4bfca1SAl Viro return error; 3421972567f1SJeff Layton retry: 3422972567f1SJeff Layton dentry = user_path_create(dfd, filename, &path, lookup_flags); 3423dae6ad8fSAl Viro if (IS_ERR(dentry)) 3424dae6ad8fSAl Viro return PTR_ERR(dentry); 34252ad94ae6SAl Viro 3426dae6ad8fSAl Viro if (!IS_POSIXACL(path.dentry->d_inode)) 3427ce3b0f8dSAl Viro mode &= ~current_umask(); 3428dae6ad8fSAl Viro error = security_path_mknod(&path, dentry, mode, dev); 3429be6d3e56SKentaro Takeda if (error) 3430a8104a9fSAl Viro goto out; 34311da177e4SLinus Torvalds switch (mode & S_IFMT) { 34321da177e4SLinus Torvalds case 0: case S_IFREG: 3433312b63fbSAl Viro error = vfs_create(path.dentry->d_inode,dentry,mode,true); 34341da177e4SLinus Torvalds break; 34351da177e4SLinus Torvalds case S_IFCHR: case S_IFBLK: 3436dae6ad8fSAl Viro error = vfs_mknod(path.dentry->d_inode,dentry,mode, 34371da177e4SLinus Torvalds new_decode_dev(dev)); 34381da177e4SLinus Torvalds break; 34391da177e4SLinus Torvalds case S_IFIFO: case S_IFSOCK: 3440dae6ad8fSAl Viro error = vfs_mknod(path.dentry->d_inode,dentry,mode,0); 34411da177e4SLinus Torvalds break; 34421da177e4SLinus Torvalds } 3443a8104a9fSAl Viro out: 3444921a1650SAl Viro done_path_create(&path, dentry); 3445972567f1SJeff Layton if (retry_estale(error, lookup_flags)) { 3446972567f1SJeff Layton lookup_flags |= LOOKUP_REVAL; 3447972567f1SJeff Layton goto retry; 3448972567f1SJeff Layton } 34491da177e4SLinus Torvalds return error; 34501da177e4SLinus Torvalds } 34511da177e4SLinus Torvalds 34528208a22bSAl Viro SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev) 34535590ff0dSUlrich Drepper { 34545590ff0dSUlrich Drepper return sys_mknodat(AT_FDCWD, filename, mode, dev); 34555590ff0dSUlrich Drepper } 34565590ff0dSUlrich Drepper 345718bb1db3SAl Viro int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) 34581da177e4SLinus Torvalds { 3459a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 34608de52778SAl Viro unsigned max_links = dir->i_sb->s_max_links; 34611da177e4SLinus Torvalds 34621da177e4SLinus Torvalds if (error) 34631da177e4SLinus Torvalds return error; 34641da177e4SLinus Torvalds 3465acfa4380SAl Viro if (!dir->i_op->mkdir) 34661da177e4SLinus Torvalds return -EPERM; 34671da177e4SLinus Torvalds 34681da177e4SLinus Torvalds mode &= (S_IRWXUGO|S_ISVTX); 34691da177e4SLinus Torvalds error = security_inode_mkdir(dir, dentry, mode); 34701da177e4SLinus Torvalds if (error) 34711da177e4SLinus Torvalds return error; 34721da177e4SLinus Torvalds 34738de52778SAl Viro if (max_links && dir->i_nlink >= max_links) 34748de52778SAl Viro return -EMLINK; 34758de52778SAl Viro 34761da177e4SLinus Torvalds error = dir->i_op->mkdir(dir, dentry, mode); 3477a74574aaSStephen Smalley if (!error) 3478f38aa942SAmy Griffis fsnotify_mkdir(dir, dentry); 34791da177e4SLinus Torvalds return error; 34801da177e4SLinus Torvalds } 34814d359507SAl Viro EXPORT_SYMBOL(vfs_mkdir); 34821da177e4SLinus Torvalds 3483a218d0fdSAl Viro SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode) 34841da177e4SLinus Torvalds { 34856902d925SDave Hansen struct dentry *dentry; 3486dae6ad8fSAl Viro struct path path; 3487dae6ad8fSAl Viro int error; 3488b76d8b82SJeff Layton unsigned int lookup_flags = LOOKUP_DIRECTORY; 34891da177e4SLinus Torvalds 3490b76d8b82SJeff Layton retry: 3491b76d8b82SJeff Layton dentry = user_path_create(dfd, pathname, &path, lookup_flags); 34926902d925SDave Hansen if (IS_ERR(dentry)) 3493dae6ad8fSAl Viro return PTR_ERR(dentry); 34946902d925SDave Hansen 3495dae6ad8fSAl Viro if (!IS_POSIXACL(path.dentry->d_inode)) 3496ce3b0f8dSAl Viro mode &= ~current_umask(); 3497dae6ad8fSAl Viro error = security_path_mkdir(&path, dentry, mode); 3498a8104a9fSAl Viro if (!error) 3499dae6ad8fSAl Viro error = vfs_mkdir(path.dentry->d_inode, dentry, mode); 3500921a1650SAl Viro done_path_create(&path, dentry); 3501b76d8b82SJeff Layton if (retry_estale(error, lookup_flags)) { 3502b76d8b82SJeff Layton lookup_flags |= LOOKUP_REVAL; 3503b76d8b82SJeff Layton goto retry; 3504b76d8b82SJeff Layton } 35051da177e4SLinus Torvalds return error; 35061da177e4SLinus Torvalds } 35071da177e4SLinus Torvalds 3508a218d0fdSAl Viro SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode) 35095590ff0dSUlrich Drepper { 35105590ff0dSUlrich Drepper return sys_mkdirat(AT_FDCWD, pathname, mode); 35115590ff0dSUlrich Drepper } 35125590ff0dSUlrich Drepper 35131da177e4SLinus Torvalds /* 3514a71905f0SSage Weil * The dentry_unhash() helper will try to drop the dentry early: we 3515c0d02594SJ. Bruce Fields * should have a usage count of 1 if we're the only user of this 3516a71905f0SSage Weil * dentry, and if that is true (possibly after pruning the dcache), 3517a71905f0SSage Weil * then we drop the dentry now. 35181da177e4SLinus Torvalds * 35191da177e4SLinus Torvalds * A low-level filesystem can, if it choses, legally 35201da177e4SLinus Torvalds * do a 35211da177e4SLinus Torvalds * 35221da177e4SLinus Torvalds * if (!d_unhashed(dentry)) 35231da177e4SLinus Torvalds * return -EBUSY; 35241da177e4SLinus Torvalds * 35251da177e4SLinus Torvalds * if it cannot handle the case of removing a directory 35261da177e4SLinus Torvalds * that is still in use by something else.. 35271da177e4SLinus Torvalds */ 35281da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry) 35291da177e4SLinus Torvalds { 35301da177e4SLinus Torvalds shrink_dcache_parent(dentry); 35311da177e4SLinus Torvalds spin_lock(&dentry->d_lock); 353298474236SWaiman Long if (dentry->d_lockref.count == 1) 35331da177e4SLinus Torvalds __d_drop(dentry); 35341da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 35351da177e4SLinus Torvalds } 35364d359507SAl Viro EXPORT_SYMBOL(dentry_unhash); 35371da177e4SLinus Torvalds 35381da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry) 35391da177e4SLinus Torvalds { 35401da177e4SLinus Torvalds int error = may_delete(dir, dentry, 1); 35411da177e4SLinus Torvalds 35421da177e4SLinus Torvalds if (error) 35431da177e4SLinus Torvalds return error; 35441da177e4SLinus Torvalds 3545acfa4380SAl Viro if (!dir->i_op->rmdir) 35461da177e4SLinus Torvalds return -EPERM; 35471da177e4SLinus Torvalds 35481d2ef590SAl Viro dget(dentry); 35491b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 3550912dbc15SSage Weil 35511da177e4SLinus Torvalds error = -EBUSY; 3552912dbc15SSage Weil if (d_mountpoint(dentry)) 3553912dbc15SSage Weil goto out; 3554912dbc15SSage Weil 35551da177e4SLinus Torvalds error = security_inode_rmdir(dir, dentry); 3556912dbc15SSage Weil if (error) 3557912dbc15SSage Weil goto out; 3558912dbc15SSage Weil 35593cebde24SSage Weil shrink_dcache_parent(dentry); 35601da177e4SLinus Torvalds error = dir->i_op->rmdir(dir, dentry); 3561912dbc15SSage Weil if (error) 3562912dbc15SSage Weil goto out; 3563912dbc15SSage Weil 35641da177e4SLinus Torvalds dentry->d_inode->i_flags |= S_DEAD; 3565d83c49f3SAl Viro dont_mount(dentry); 35661da177e4SLinus Torvalds 3567912dbc15SSage Weil out: 3568912dbc15SSage Weil mutex_unlock(&dentry->d_inode->i_mutex); 35691d2ef590SAl Viro dput(dentry); 3570912dbc15SSage Weil if (!error) 3571912dbc15SSage Weil d_delete(dentry); 35721da177e4SLinus Torvalds return error; 35731da177e4SLinus Torvalds } 35744d359507SAl Viro EXPORT_SYMBOL(vfs_rmdir); 35751da177e4SLinus Torvalds 35765590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname) 35771da177e4SLinus Torvalds { 35781da177e4SLinus Torvalds int error = 0; 357991a27b2aSJeff Layton struct filename *name; 35801da177e4SLinus Torvalds struct dentry *dentry; 35811da177e4SLinus Torvalds struct nameidata nd; 3582c6ee9206SJeff Layton unsigned int lookup_flags = 0; 3583c6ee9206SJeff Layton retry: 3584c6ee9206SJeff Layton name = user_path_parent(dfd, pathname, &nd, lookup_flags); 358591a27b2aSJeff Layton if (IS_ERR(name)) 358691a27b2aSJeff Layton return PTR_ERR(name); 35871da177e4SLinus Torvalds 35881da177e4SLinus Torvalds switch(nd.last_type) { 35891da177e4SLinus Torvalds case LAST_DOTDOT: 35901da177e4SLinus Torvalds error = -ENOTEMPTY; 35911da177e4SLinus Torvalds goto exit1; 35921da177e4SLinus Torvalds case LAST_DOT: 35931da177e4SLinus Torvalds error = -EINVAL; 35941da177e4SLinus Torvalds goto exit1; 35951da177e4SLinus Torvalds case LAST_ROOT: 35961da177e4SLinus Torvalds error = -EBUSY; 35971da177e4SLinus Torvalds goto exit1; 35981da177e4SLinus Torvalds } 35990612d9fbSOGAWA Hirofumi 36000612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 3601c30dabfeSJan Kara error = mnt_want_write(nd.path.mnt); 3602c30dabfeSJan Kara if (error) 3603c30dabfeSJan Kara goto exit1; 36040612d9fbSOGAWA Hirofumi 36054ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 360649705b77SChristoph Hellwig dentry = lookup_hash(&nd); 36071da177e4SLinus Torvalds error = PTR_ERR(dentry); 36086902d925SDave Hansen if (IS_ERR(dentry)) 36096902d925SDave Hansen goto exit2; 3610e6bc45d6STheodore Ts'o if (!dentry->d_inode) { 3611e6bc45d6STheodore Ts'o error = -ENOENT; 3612e6bc45d6STheodore Ts'o goto exit3; 3613e6bc45d6STheodore Ts'o } 3614be6d3e56SKentaro Takeda error = security_path_rmdir(&nd.path, dentry); 3615be6d3e56SKentaro Takeda if (error) 3616c30dabfeSJan Kara goto exit3; 36174ac91378SJan Blunck error = vfs_rmdir(nd.path.dentry->d_inode, dentry); 36180622753bSDave Hansen exit3: 36191da177e4SLinus Torvalds dput(dentry); 36206902d925SDave Hansen exit2: 36214ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 3622c30dabfeSJan Kara mnt_drop_write(nd.path.mnt); 36231da177e4SLinus Torvalds exit1: 36241d957f9bSJan Blunck path_put(&nd.path); 36251da177e4SLinus Torvalds putname(name); 3626c6ee9206SJeff Layton if (retry_estale(error, lookup_flags)) { 3627c6ee9206SJeff Layton lookup_flags |= LOOKUP_REVAL; 3628c6ee9206SJeff Layton goto retry; 3629c6ee9206SJeff Layton } 36301da177e4SLinus Torvalds return error; 36311da177e4SLinus Torvalds } 36321da177e4SLinus Torvalds 36333cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname) 36345590ff0dSUlrich Drepper { 36355590ff0dSUlrich Drepper return do_rmdir(AT_FDCWD, pathname); 36365590ff0dSUlrich Drepper } 36375590ff0dSUlrich Drepper 3638b21996e3SJ. Bruce Fields /** 3639b21996e3SJ. Bruce Fields * vfs_unlink - unlink a filesystem object 3640b21996e3SJ. Bruce Fields * @dir: parent directory 3641b21996e3SJ. Bruce Fields * @dentry: victim 3642b21996e3SJ. Bruce Fields * @delegated_inode: returns victim inode, if the inode is delegated. 3643b21996e3SJ. Bruce Fields * 3644b21996e3SJ. Bruce Fields * The caller must hold dir->i_mutex. 3645b21996e3SJ. Bruce Fields * 3646b21996e3SJ. Bruce Fields * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and 3647b21996e3SJ. Bruce Fields * return a reference to the inode in delegated_inode. The caller 3648b21996e3SJ. Bruce Fields * should then break the delegation on that inode and retry. Because 3649b21996e3SJ. Bruce Fields * breaking a delegation may take a long time, the caller should drop 3650b21996e3SJ. Bruce Fields * dir->i_mutex before doing so. 3651b21996e3SJ. Bruce Fields * 3652b21996e3SJ. Bruce Fields * Alternatively, a caller may pass NULL for delegated_inode. This may 3653b21996e3SJ. Bruce Fields * be appropriate for callers that expect the underlying filesystem not 3654b21996e3SJ. Bruce Fields * to be NFS exported. 3655b21996e3SJ. Bruce Fields */ 3656b21996e3SJ. Bruce Fields int vfs_unlink(struct inode *dir, struct dentry *dentry, struct inode **delegated_inode) 36571da177e4SLinus Torvalds { 36589accbb97SJ. Bruce Fields struct inode *target = dentry->d_inode; 36591da177e4SLinus Torvalds int error = may_delete(dir, dentry, 0); 36601da177e4SLinus Torvalds 36611da177e4SLinus Torvalds if (error) 36621da177e4SLinus Torvalds return error; 36631da177e4SLinus Torvalds 3664acfa4380SAl Viro if (!dir->i_op->unlink) 36651da177e4SLinus Torvalds return -EPERM; 36661da177e4SLinus Torvalds 36679accbb97SJ. Bruce Fields mutex_lock(&target->i_mutex); 36681da177e4SLinus Torvalds if (d_mountpoint(dentry)) 36691da177e4SLinus Torvalds error = -EBUSY; 36701da177e4SLinus Torvalds else { 36711da177e4SLinus Torvalds error = security_inode_unlink(dir, dentry); 3672bec1052eSAl Viro if (!error) { 36735a14696cSJ. Bruce Fields error = try_break_deleg(target, delegated_inode); 36745a14696cSJ. Bruce Fields if (error) 3675b21996e3SJ. Bruce Fields goto out; 36761da177e4SLinus Torvalds error = dir->i_op->unlink(dir, dentry); 3677bec1052eSAl Viro if (!error) 3678d83c49f3SAl Viro dont_mount(dentry); 3679bec1052eSAl Viro } 36801da177e4SLinus Torvalds } 3681b21996e3SJ. Bruce Fields out: 36829accbb97SJ. Bruce Fields mutex_unlock(&target->i_mutex); 36831da177e4SLinus Torvalds 36841da177e4SLinus Torvalds /* We don't d_delete() NFS sillyrenamed files--they still exist. */ 36851da177e4SLinus Torvalds if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) { 36869accbb97SJ. Bruce Fields fsnotify_link_count(target); 36871da177e4SLinus Torvalds d_delete(dentry); 36881da177e4SLinus Torvalds } 36890eeca283SRobert Love 36901da177e4SLinus Torvalds return error; 36911da177e4SLinus Torvalds } 36924d359507SAl Viro EXPORT_SYMBOL(vfs_unlink); 36931da177e4SLinus Torvalds 36941da177e4SLinus Torvalds /* 36951da177e4SLinus Torvalds * Make sure that the actual truncation of the file will occur outside its 36961b1dcc1bSJes Sorensen * directory's i_mutex. Truncate can take a long time if there is a lot of 36971da177e4SLinus Torvalds * writeout happening, and we don't want to prevent access to the directory 36981da177e4SLinus Torvalds * while waiting on the I/O. 36991da177e4SLinus Torvalds */ 37005590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname) 37011da177e4SLinus Torvalds { 37022ad94ae6SAl Viro int error; 370391a27b2aSJeff Layton struct filename *name; 37041da177e4SLinus Torvalds struct dentry *dentry; 37051da177e4SLinus Torvalds struct nameidata nd; 37061da177e4SLinus Torvalds struct inode *inode = NULL; 3707b21996e3SJ. Bruce Fields struct inode *delegated_inode = NULL; 37085d18f813SJeff Layton unsigned int lookup_flags = 0; 37095d18f813SJeff Layton retry: 37105d18f813SJeff Layton name = user_path_parent(dfd, pathname, &nd, lookup_flags); 371191a27b2aSJeff Layton if (IS_ERR(name)) 371291a27b2aSJeff Layton return PTR_ERR(name); 37132ad94ae6SAl Viro 37141da177e4SLinus Torvalds error = -EISDIR; 37151da177e4SLinus Torvalds if (nd.last_type != LAST_NORM) 37161da177e4SLinus Torvalds goto exit1; 37170612d9fbSOGAWA Hirofumi 37180612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 3719c30dabfeSJan Kara error = mnt_want_write(nd.path.mnt); 3720c30dabfeSJan Kara if (error) 3721c30dabfeSJan Kara goto exit1; 3722b21996e3SJ. Bruce Fields retry_deleg: 37234ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 372449705b77SChristoph Hellwig dentry = lookup_hash(&nd); 37251da177e4SLinus Torvalds error = PTR_ERR(dentry); 37261da177e4SLinus Torvalds if (!IS_ERR(dentry)) { 37271da177e4SLinus Torvalds /* Why not before? Because we want correct error value */ 372850338b88STörök Edwin if (nd.last.name[nd.last.len]) 372950338b88STörök Edwin goto slashes; 37301da177e4SLinus Torvalds inode = dentry->d_inode; 3731b18825a7SDavid Howells if (d_is_negative(dentry)) 3732e6bc45d6STheodore Ts'o goto slashes; 37337de9c6eeSAl Viro ihold(inode); 3734be6d3e56SKentaro Takeda error = security_path_unlink(&nd.path, dentry); 3735be6d3e56SKentaro Takeda if (error) 3736c30dabfeSJan Kara goto exit2; 3737b21996e3SJ. Bruce Fields error = vfs_unlink(nd.path.dentry->d_inode, dentry, &delegated_inode); 37381da177e4SLinus Torvalds exit2: 37391da177e4SLinus Torvalds dput(dentry); 37401da177e4SLinus Torvalds } 37414ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 37421da177e4SLinus Torvalds if (inode) 37431da177e4SLinus Torvalds iput(inode); /* truncate the inode here */ 3744b21996e3SJ. Bruce Fields inode = NULL; 3745b21996e3SJ. Bruce Fields if (delegated_inode) { 37465a14696cSJ. Bruce Fields error = break_deleg_wait(&delegated_inode); 3747b21996e3SJ. Bruce Fields if (!error) 3748b21996e3SJ. Bruce Fields goto retry_deleg; 3749b21996e3SJ. Bruce Fields } 3750c30dabfeSJan Kara mnt_drop_write(nd.path.mnt); 37511da177e4SLinus Torvalds exit1: 37521d957f9bSJan Blunck path_put(&nd.path); 37531da177e4SLinus Torvalds putname(name); 37545d18f813SJeff Layton if (retry_estale(error, lookup_flags)) { 37555d18f813SJeff Layton lookup_flags |= LOOKUP_REVAL; 37565d18f813SJeff Layton inode = NULL; 37575d18f813SJeff Layton goto retry; 37585d18f813SJeff Layton } 37591da177e4SLinus Torvalds return error; 37601da177e4SLinus Torvalds 37611da177e4SLinus Torvalds slashes: 3762b18825a7SDavid Howells if (d_is_negative(dentry)) 3763b18825a7SDavid Howells error = -ENOENT; 3764b18825a7SDavid Howells else if (d_is_directory(dentry) || d_is_autodir(dentry)) 3765b18825a7SDavid Howells error = -EISDIR; 3766b18825a7SDavid Howells else 3767b18825a7SDavid Howells error = -ENOTDIR; 37681da177e4SLinus Torvalds goto exit2; 37691da177e4SLinus Torvalds } 37701da177e4SLinus Torvalds 37712e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag) 37725590ff0dSUlrich Drepper { 37735590ff0dSUlrich Drepper if ((flag & ~AT_REMOVEDIR) != 0) 37745590ff0dSUlrich Drepper return -EINVAL; 37755590ff0dSUlrich Drepper 37765590ff0dSUlrich Drepper if (flag & AT_REMOVEDIR) 37775590ff0dSUlrich Drepper return do_rmdir(dfd, pathname); 37785590ff0dSUlrich Drepper 37795590ff0dSUlrich Drepper return do_unlinkat(dfd, pathname); 37805590ff0dSUlrich Drepper } 37815590ff0dSUlrich Drepper 37823480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname) 37835590ff0dSUlrich Drepper { 37845590ff0dSUlrich Drepper return do_unlinkat(AT_FDCWD, pathname); 37855590ff0dSUlrich Drepper } 37865590ff0dSUlrich Drepper 3787db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname) 37881da177e4SLinus Torvalds { 3789a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 37901da177e4SLinus Torvalds 37911da177e4SLinus Torvalds if (error) 37921da177e4SLinus Torvalds return error; 37931da177e4SLinus Torvalds 3794acfa4380SAl Viro if (!dir->i_op->symlink) 37951da177e4SLinus Torvalds return -EPERM; 37961da177e4SLinus Torvalds 37971da177e4SLinus Torvalds error = security_inode_symlink(dir, dentry, oldname); 37981da177e4SLinus Torvalds if (error) 37991da177e4SLinus Torvalds return error; 38001da177e4SLinus Torvalds 38011da177e4SLinus Torvalds error = dir->i_op->symlink(dir, dentry, oldname); 3802a74574aaSStephen Smalley if (!error) 3803f38aa942SAmy Griffis fsnotify_create(dir, dentry); 38041da177e4SLinus Torvalds return error; 38051da177e4SLinus Torvalds } 38064d359507SAl Viro EXPORT_SYMBOL(vfs_symlink); 38071da177e4SLinus Torvalds 38082e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname, 38092e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 38101da177e4SLinus Torvalds { 38112ad94ae6SAl Viro int error; 381291a27b2aSJeff Layton struct filename *from; 38136902d925SDave Hansen struct dentry *dentry; 3814dae6ad8fSAl Viro struct path path; 3815f46d3567SJeff Layton unsigned int lookup_flags = 0; 38161da177e4SLinus Torvalds 38171da177e4SLinus Torvalds from = getname(oldname); 38181da177e4SLinus Torvalds if (IS_ERR(from)) 38191da177e4SLinus Torvalds return PTR_ERR(from); 3820f46d3567SJeff Layton retry: 3821f46d3567SJeff Layton dentry = user_path_create(newdfd, newname, &path, lookup_flags); 38221da177e4SLinus Torvalds error = PTR_ERR(dentry); 38236902d925SDave Hansen if (IS_ERR(dentry)) 3824dae6ad8fSAl Viro goto out_putname; 38256902d925SDave Hansen 382691a27b2aSJeff Layton error = security_path_symlink(&path, dentry, from->name); 3827a8104a9fSAl Viro if (!error) 382891a27b2aSJeff Layton error = vfs_symlink(path.dentry->d_inode, dentry, from->name); 3829921a1650SAl Viro done_path_create(&path, dentry); 3830f46d3567SJeff Layton if (retry_estale(error, lookup_flags)) { 3831f46d3567SJeff Layton lookup_flags |= LOOKUP_REVAL; 3832f46d3567SJeff Layton goto retry; 3833f46d3567SJeff Layton } 38346902d925SDave Hansen out_putname: 38351da177e4SLinus Torvalds putname(from); 38361da177e4SLinus Torvalds return error; 38371da177e4SLinus Torvalds } 38381da177e4SLinus Torvalds 38393480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname) 38405590ff0dSUlrich Drepper { 38415590ff0dSUlrich Drepper return sys_symlinkat(oldname, AT_FDCWD, newname); 38425590ff0dSUlrich Drepper } 38435590ff0dSUlrich Drepper 3844146a8595SJ. Bruce Fields /** 3845146a8595SJ. Bruce Fields * vfs_link - create a new link 3846146a8595SJ. Bruce Fields * @old_dentry: object to be linked 3847146a8595SJ. Bruce Fields * @dir: new parent 3848146a8595SJ. Bruce Fields * @new_dentry: where to create the new link 3849146a8595SJ. Bruce Fields * @delegated_inode: returns inode needing a delegation break 3850146a8595SJ. Bruce Fields * 3851146a8595SJ. Bruce Fields * The caller must hold dir->i_mutex 3852146a8595SJ. Bruce Fields * 3853146a8595SJ. Bruce Fields * If vfs_link discovers a delegation on the to-be-linked file in need 3854146a8595SJ. Bruce Fields * of breaking, it will return -EWOULDBLOCK and return a reference to the 3855146a8595SJ. Bruce Fields * inode in delegated_inode. The caller should then break the delegation 3856146a8595SJ. Bruce Fields * and retry. Because breaking a delegation may take a long time, the 3857146a8595SJ. Bruce Fields * caller should drop the i_mutex before doing so. 3858146a8595SJ. Bruce Fields * 3859146a8595SJ. Bruce Fields * Alternatively, a caller may pass NULL for delegated_inode. This may 3860146a8595SJ. Bruce Fields * be appropriate for callers that expect the underlying filesystem not 3861146a8595SJ. Bruce Fields * to be NFS exported. 3862146a8595SJ. Bruce Fields */ 3863146a8595SJ. Bruce Fields int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry, struct inode **delegated_inode) 38641da177e4SLinus Torvalds { 38651da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 38668de52778SAl Viro unsigned max_links = dir->i_sb->s_max_links; 38671da177e4SLinus Torvalds int error; 38681da177e4SLinus Torvalds 38691da177e4SLinus Torvalds if (!inode) 38701da177e4SLinus Torvalds return -ENOENT; 38711da177e4SLinus Torvalds 3872a95164d9SMiklos Szeredi error = may_create(dir, new_dentry); 38731da177e4SLinus Torvalds if (error) 38741da177e4SLinus Torvalds return error; 38751da177e4SLinus Torvalds 38761da177e4SLinus Torvalds if (dir->i_sb != inode->i_sb) 38771da177e4SLinus Torvalds return -EXDEV; 38781da177e4SLinus Torvalds 38791da177e4SLinus Torvalds /* 38801da177e4SLinus Torvalds * A link to an append-only or immutable file cannot be created. 38811da177e4SLinus Torvalds */ 38821da177e4SLinus Torvalds if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) 38831da177e4SLinus Torvalds return -EPERM; 3884acfa4380SAl Viro if (!dir->i_op->link) 38851da177e4SLinus Torvalds return -EPERM; 38867e79eedbSTetsuo Handa if (S_ISDIR(inode->i_mode)) 38871da177e4SLinus Torvalds return -EPERM; 38881da177e4SLinus Torvalds 38891da177e4SLinus Torvalds error = security_inode_link(old_dentry, dir, new_dentry); 38901da177e4SLinus Torvalds if (error) 38911da177e4SLinus Torvalds return error; 38921da177e4SLinus Torvalds 38937e79eedbSTetsuo Handa mutex_lock(&inode->i_mutex); 3894aae8a97dSAneesh Kumar K.V /* Make sure we don't allow creating hardlink to an unlinked file */ 3895f4e0c30cSAl Viro if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE)) 3896aae8a97dSAneesh Kumar K.V error = -ENOENT; 38978de52778SAl Viro else if (max_links && inode->i_nlink >= max_links) 38988de52778SAl Viro error = -EMLINK; 3899146a8595SJ. Bruce Fields else { 3900146a8595SJ. Bruce Fields error = try_break_deleg(inode, delegated_inode); 3901146a8595SJ. Bruce Fields if (!error) 39021da177e4SLinus Torvalds error = dir->i_op->link(old_dentry, dir, new_dentry); 3903146a8595SJ. Bruce Fields } 3904f4e0c30cSAl Viro 3905f4e0c30cSAl Viro if (!error && (inode->i_state & I_LINKABLE)) { 3906f4e0c30cSAl Viro spin_lock(&inode->i_lock); 3907f4e0c30cSAl Viro inode->i_state &= ~I_LINKABLE; 3908f4e0c30cSAl Viro spin_unlock(&inode->i_lock); 3909f4e0c30cSAl Viro } 39107e79eedbSTetsuo Handa mutex_unlock(&inode->i_mutex); 3911e31e14ecSStephen Smalley if (!error) 39127e79eedbSTetsuo Handa fsnotify_link(dir, inode, new_dentry); 39131da177e4SLinus Torvalds return error; 39141da177e4SLinus Torvalds } 39154d359507SAl Viro EXPORT_SYMBOL(vfs_link); 39161da177e4SLinus Torvalds 39171da177e4SLinus Torvalds /* 39181da177e4SLinus Torvalds * Hardlinks are often used in delicate situations. We avoid 39191da177e4SLinus Torvalds * security-related surprises by not following symlinks on the 39201da177e4SLinus Torvalds * newname. --KAB 39211da177e4SLinus Torvalds * 39221da177e4SLinus Torvalds * We don't follow them on the oldname either to be compatible 39231da177e4SLinus Torvalds * with linux 2.0, and to avoid hard-linking to directories 39241da177e4SLinus Torvalds * and other special files. --ADM 39251da177e4SLinus Torvalds */ 39262e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname, 39272e4d0924SHeiko Carstens int, newdfd, const char __user *, newname, int, flags) 39281da177e4SLinus Torvalds { 39291da177e4SLinus Torvalds struct dentry *new_dentry; 3930dae6ad8fSAl Viro struct path old_path, new_path; 3931146a8595SJ. Bruce Fields struct inode *delegated_inode = NULL; 393211a7b371SAneesh Kumar K.V int how = 0; 39331da177e4SLinus Torvalds int error; 39341da177e4SLinus Torvalds 393511a7b371SAneesh Kumar K.V if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0) 3936c04030e1SUlrich Drepper return -EINVAL; 393711a7b371SAneesh Kumar K.V /* 3938f0cc6ffbSLinus Torvalds * To use null names we require CAP_DAC_READ_SEARCH 3939f0cc6ffbSLinus Torvalds * This ensures that not everyone will be able to create 3940f0cc6ffbSLinus Torvalds * handlink using the passed filedescriptor. 394111a7b371SAneesh Kumar K.V */ 3942f0cc6ffbSLinus Torvalds if (flags & AT_EMPTY_PATH) { 3943f0cc6ffbSLinus Torvalds if (!capable(CAP_DAC_READ_SEARCH)) 3944f0cc6ffbSLinus Torvalds return -ENOENT; 394511a7b371SAneesh Kumar K.V how = LOOKUP_EMPTY; 3946f0cc6ffbSLinus Torvalds } 3947c04030e1SUlrich Drepper 394811a7b371SAneesh Kumar K.V if (flags & AT_SYMLINK_FOLLOW) 394911a7b371SAneesh Kumar K.V how |= LOOKUP_FOLLOW; 3950442e31caSJeff Layton retry: 395111a7b371SAneesh Kumar K.V error = user_path_at(olddfd, oldname, how, &old_path); 39521da177e4SLinus Torvalds if (error) 39532ad94ae6SAl Viro return error; 39542ad94ae6SAl Viro 3955442e31caSJeff Layton new_dentry = user_path_create(newdfd, newname, &new_path, 3956442e31caSJeff Layton (how & LOOKUP_REVAL)); 39571da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 39586902d925SDave Hansen if (IS_ERR(new_dentry)) 3959dae6ad8fSAl Viro goto out; 3960dae6ad8fSAl Viro 3961dae6ad8fSAl Viro error = -EXDEV; 3962dae6ad8fSAl Viro if (old_path.mnt != new_path.mnt) 3963dae6ad8fSAl Viro goto out_dput; 3964800179c9SKees Cook error = may_linkat(&old_path); 3965800179c9SKees Cook if (unlikely(error)) 3966800179c9SKees Cook goto out_dput; 3967dae6ad8fSAl Viro error = security_path_link(old_path.dentry, &new_path, new_dentry); 3968be6d3e56SKentaro Takeda if (error) 3969a8104a9fSAl Viro goto out_dput; 3970146a8595SJ. Bruce Fields error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry, &delegated_inode); 397175c3f29dSDave Hansen out_dput: 3972921a1650SAl Viro done_path_create(&new_path, new_dentry); 3973146a8595SJ. Bruce Fields if (delegated_inode) { 3974146a8595SJ. Bruce Fields error = break_deleg_wait(&delegated_inode); 3975d22e6338SOleg Drokin if (!error) { 3976d22e6338SOleg Drokin path_put(&old_path); 3977146a8595SJ. Bruce Fields goto retry; 3978146a8595SJ. Bruce Fields } 3979d22e6338SOleg Drokin } 3980442e31caSJeff Layton if (retry_estale(error, how)) { 3981d22e6338SOleg Drokin path_put(&old_path); 3982442e31caSJeff Layton how |= LOOKUP_REVAL; 3983442e31caSJeff Layton goto retry; 3984442e31caSJeff Layton } 39851da177e4SLinus Torvalds out: 39862d8f3038SAl Viro path_put(&old_path); 39871da177e4SLinus Torvalds 39881da177e4SLinus Torvalds return error; 39891da177e4SLinus Torvalds } 39901da177e4SLinus Torvalds 39913480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname) 39925590ff0dSUlrich Drepper { 3993c04030e1SUlrich Drepper return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0); 39945590ff0dSUlrich Drepper } 39955590ff0dSUlrich Drepper 39961da177e4SLinus Torvalds /* 39971da177e4SLinus Torvalds * The worst of all namespace operations - renaming directory. "Perverted" 39981da177e4SLinus Torvalds * doesn't even start to describe it. Somebody in UCB had a heck of a trip... 39991da177e4SLinus Torvalds * Problems: 40001da177e4SLinus Torvalds * a) we can get into loop creation. Check is done in is_subdir(). 40011da177e4SLinus Torvalds * b) race potential - two innocent renames can create a loop together. 40021da177e4SLinus Torvalds * That's where 4.4 screws up. Current fix: serialization on 4003a11f3a05SArjan van de Ven * sb->s_vfs_rename_mutex. We might be more accurate, but that's another 40041da177e4SLinus Torvalds * story. 40056cedba89SJ. Bruce Fields * c) we have to lock _four_ objects - parents and victim (if it exists), 40066cedba89SJ. Bruce Fields * and source (if it is not a directory). 40071b1dcc1bSJes Sorensen * And that - after we got ->i_mutex on parents (until then we don't know 40081da177e4SLinus Torvalds * whether the target exists). Solution: try to be smart with locking 40091da177e4SLinus Torvalds * order for inodes. We rely on the fact that tree topology may change 4010a11f3a05SArjan van de Ven * only under ->s_vfs_rename_mutex _and_ that parent of the object we 40111da177e4SLinus Torvalds * move will be locked. Thus we can rank directories by the tree 40121da177e4SLinus Torvalds * (ancestors first) and rank all non-directories after them. 40131da177e4SLinus Torvalds * That works since everybody except rename does "lock parent, lookup, 4014a11f3a05SArjan van de Ven * lock child" and rename is under ->s_vfs_rename_mutex. 40151da177e4SLinus Torvalds * HOWEVER, it relies on the assumption that any object with ->lookup() 40161da177e4SLinus Torvalds * has no more than 1 dentry. If "hybrid" objects will ever appear, 40171da177e4SLinus Torvalds * we'd better make sure that there's no link(2) for them. 4018e4eaac06SSage Weil * d) conversion from fhandle to dentry may come in the wrong moment - when 40191b1dcc1bSJes Sorensen * we are removing the target. Solution: we will have to grab ->i_mutex 40201da177e4SLinus Torvalds * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on 4021c41b20e7SAdam Buchbinder * ->i_mutex on parents, which works but leads to some truly excessive 40221da177e4SLinus Torvalds * locking]. 40231da177e4SLinus Torvalds */ 402475c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry, 40251da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 40261da177e4SLinus Torvalds { 40271da177e4SLinus Torvalds int error = 0; 40289055cba7SSage Weil struct inode *target = new_dentry->d_inode; 40298de52778SAl Viro unsigned max_links = new_dir->i_sb->s_max_links; 40301da177e4SLinus Torvalds 40311da177e4SLinus Torvalds /* 40321da177e4SLinus Torvalds * If we are going to change the parent - check write permissions, 40331da177e4SLinus Torvalds * we'll need to flip '..'. 40341da177e4SLinus Torvalds */ 40351da177e4SLinus Torvalds if (new_dir != old_dir) { 4036f419a2e3SAl Viro error = inode_permission(old_dentry->d_inode, MAY_WRITE); 40371da177e4SLinus Torvalds if (error) 40381da177e4SLinus Torvalds return error; 40391da177e4SLinus Torvalds } 40401da177e4SLinus Torvalds 40411da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 40421da177e4SLinus Torvalds if (error) 40431da177e4SLinus Torvalds return error; 40441da177e4SLinus Torvalds 40451d2ef590SAl Viro dget(new_dentry); 4046d83c49f3SAl Viro if (target) 40471b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 40489055cba7SSage Weil 40491da177e4SLinus Torvalds error = -EBUSY; 40509055cba7SSage Weil if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry)) 40519055cba7SSage Weil goto out; 40529055cba7SSage Weil 40538de52778SAl Viro error = -EMLINK; 40548de52778SAl Viro if (max_links && !target && new_dir != old_dir && 40558de52778SAl Viro new_dir->i_nlink >= max_links) 40568de52778SAl Viro goto out; 40578de52778SAl Viro 40583cebde24SSage Weil if (target) 40593cebde24SSage Weil shrink_dcache_parent(new_dentry); 40601da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 40619055cba7SSage Weil if (error) 40629055cba7SSage Weil goto out; 40639055cba7SSage Weil 40641da177e4SLinus Torvalds if (target) { 40651da177e4SLinus Torvalds target->i_flags |= S_DEAD; 4066d83c49f3SAl Viro dont_mount(new_dentry); 4067d83c49f3SAl Viro } 40689055cba7SSage Weil out: 40699055cba7SSage Weil if (target) 40701b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 40711d2ef590SAl Viro dput(new_dentry); 4072e31e14ecSStephen Smalley if (!error) 4073349457ccSMark Fasheh if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) 40741da177e4SLinus Torvalds d_move(old_dentry,new_dentry); 40751da177e4SLinus Torvalds return error; 40761da177e4SLinus Torvalds } 40771da177e4SLinus Torvalds 407875c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry, 40798e6d782cSJ. Bruce Fields struct inode *new_dir, struct dentry *new_dentry, 40808e6d782cSJ. Bruce Fields struct inode **delegated_inode) 40811da177e4SLinus Torvalds { 408251892bbbSSage Weil struct inode *target = new_dentry->d_inode; 40836cedba89SJ. Bruce Fields struct inode *source = old_dentry->d_inode; 40841da177e4SLinus Torvalds int error; 40851da177e4SLinus Torvalds 40861da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 40871da177e4SLinus Torvalds if (error) 40881da177e4SLinus Torvalds return error; 40891da177e4SLinus Torvalds 40901da177e4SLinus Torvalds dget(new_dentry); 40916cedba89SJ. Bruce Fields lock_two_nondirectories(source, target); 409251892bbbSSage Weil 40931da177e4SLinus Torvalds error = -EBUSY; 409451892bbbSSage Weil if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry)) 409551892bbbSSage Weil goto out; 409651892bbbSSage Weil 40978e6d782cSJ. Bruce Fields error = try_break_deleg(source, delegated_inode); 40988e6d782cSJ. Bruce Fields if (error) 40998e6d782cSJ. Bruce Fields goto out; 41008e6d782cSJ. Bruce Fields if (target) { 41018e6d782cSJ. Bruce Fields error = try_break_deleg(target, delegated_inode); 41028e6d782cSJ. Bruce Fields if (error) 41038e6d782cSJ. Bruce Fields goto out; 41048e6d782cSJ. Bruce Fields } 41051da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 410651892bbbSSage Weil if (error) 410751892bbbSSage Weil goto out; 410851892bbbSSage Weil 4109bec1052eSAl Viro if (target) 4110d83c49f3SAl Viro dont_mount(new_dentry); 4111349457ccSMark Fasheh if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) 41121da177e4SLinus Torvalds d_move(old_dentry, new_dentry); 411351892bbbSSage Weil out: 41146cedba89SJ. Bruce Fields unlock_two_nondirectories(source, target); 41151da177e4SLinus Torvalds dput(new_dentry); 41161da177e4SLinus Torvalds return error; 41171da177e4SLinus Torvalds } 41181da177e4SLinus Torvalds 41198e6d782cSJ. Bruce Fields /** 41208e6d782cSJ. Bruce Fields * vfs_rename - rename a filesystem object 41218e6d782cSJ. Bruce Fields * @old_dir: parent of source 41228e6d782cSJ. Bruce Fields * @old_dentry: source 41238e6d782cSJ. Bruce Fields * @new_dir: parent of destination 41248e6d782cSJ. Bruce Fields * @new_dentry: destination 41258e6d782cSJ. Bruce Fields * @delegated_inode: returns an inode needing a delegation break 41268e6d782cSJ. Bruce Fields * 41278e6d782cSJ. Bruce Fields * The caller must hold multiple mutexes--see lock_rename()). 41288e6d782cSJ. Bruce Fields * 41298e6d782cSJ. Bruce Fields * If vfs_rename discovers a delegation in need of breaking at either 41308e6d782cSJ. Bruce Fields * the source or destination, it will return -EWOULDBLOCK and return a 41318e6d782cSJ. Bruce Fields * reference to the inode in delegated_inode. The caller should then 41328e6d782cSJ. Bruce Fields * break the delegation and retry. Because breaking a delegation may 41338e6d782cSJ. Bruce Fields * take a long time, the caller should drop all locks before doing 41348e6d782cSJ. Bruce Fields * so. 41358e6d782cSJ. Bruce Fields * 41368e6d782cSJ. Bruce Fields * Alternatively, a caller may pass NULL for delegated_inode. This may 41378e6d782cSJ. Bruce Fields * be appropriate for callers that expect the underlying filesystem not 41388e6d782cSJ. Bruce Fields * to be NFS exported. 41398e6d782cSJ. Bruce Fields */ 41401da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry, 41418e6d782cSJ. Bruce Fields struct inode *new_dir, struct dentry *new_dentry, 41428e6d782cSJ. Bruce Fields struct inode **delegated_inode) 41431da177e4SLinus Torvalds { 41441da177e4SLinus Torvalds int error; 4145b18825a7SDavid Howells int is_dir = d_is_directory(old_dentry) || d_is_autodir(old_dentry); 414659b0df21SEric Paris const unsigned char *old_name; 41471da177e4SLinus Torvalds 41481da177e4SLinus Torvalds if (old_dentry->d_inode == new_dentry->d_inode) 41491da177e4SLinus Torvalds return 0; 41501da177e4SLinus Torvalds 41511da177e4SLinus Torvalds error = may_delete(old_dir, old_dentry, is_dir); 41521da177e4SLinus Torvalds if (error) 41531da177e4SLinus Torvalds return error; 41541da177e4SLinus Torvalds 41551da177e4SLinus Torvalds if (!new_dentry->d_inode) 4156a95164d9SMiklos Szeredi error = may_create(new_dir, new_dentry); 41571da177e4SLinus Torvalds else 41581da177e4SLinus Torvalds error = may_delete(new_dir, new_dentry, is_dir); 41591da177e4SLinus Torvalds if (error) 41601da177e4SLinus Torvalds return error; 41611da177e4SLinus Torvalds 4162acfa4380SAl Viro if (!old_dir->i_op->rename) 41631da177e4SLinus Torvalds return -EPERM; 41641da177e4SLinus Torvalds 41650eeca283SRobert Love old_name = fsnotify_oldname_init(old_dentry->d_name.name); 41660eeca283SRobert Love 41671da177e4SLinus Torvalds if (is_dir) 41681da177e4SLinus Torvalds error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry); 41691da177e4SLinus Torvalds else 41708e6d782cSJ. Bruce Fields error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry,delegated_inode); 4171123df294SAl Viro if (!error) 4172123df294SAl Viro fsnotify_move(old_dir, new_dir, old_name, is_dir, 41735a190ae6SAl Viro new_dentry->d_inode, old_dentry); 41740eeca283SRobert Love fsnotify_oldname_free(old_name); 41750eeca283SRobert Love 41761da177e4SLinus Torvalds return error; 41771da177e4SLinus Torvalds } 41784d359507SAl Viro EXPORT_SYMBOL(vfs_rename); 41791da177e4SLinus Torvalds 41802e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname, 41812e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 41821da177e4SLinus Torvalds { 41831da177e4SLinus Torvalds struct dentry *old_dir, *new_dir; 41841da177e4SLinus Torvalds struct dentry *old_dentry, *new_dentry; 41851da177e4SLinus Torvalds struct dentry *trap; 41861da177e4SLinus Torvalds struct nameidata oldnd, newnd; 41878e6d782cSJ. Bruce Fields struct inode *delegated_inode = NULL; 418891a27b2aSJeff Layton struct filename *from; 418991a27b2aSJeff Layton struct filename *to; 4190c6a94284SJeff Layton unsigned int lookup_flags = 0; 4191c6a94284SJeff Layton bool should_retry = false; 41922ad94ae6SAl Viro int error; 4193c6a94284SJeff Layton retry: 4194c6a94284SJeff Layton from = user_path_parent(olddfd, oldname, &oldnd, lookup_flags); 419591a27b2aSJeff Layton if (IS_ERR(from)) { 419691a27b2aSJeff Layton error = PTR_ERR(from); 41971da177e4SLinus Torvalds goto exit; 419891a27b2aSJeff Layton } 41991da177e4SLinus Torvalds 4200c6a94284SJeff Layton to = user_path_parent(newdfd, newname, &newnd, lookup_flags); 420191a27b2aSJeff Layton if (IS_ERR(to)) { 420291a27b2aSJeff Layton error = PTR_ERR(to); 42031da177e4SLinus Torvalds goto exit1; 420491a27b2aSJeff Layton } 42051da177e4SLinus Torvalds 42061da177e4SLinus Torvalds error = -EXDEV; 42074ac91378SJan Blunck if (oldnd.path.mnt != newnd.path.mnt) 42081da177e4SLinus Torvalds goto exit2; 42091da177e4SLinus Torvalds 42104ac91378SJan Blunck old_dir = oldnd.path.dentry; 42111da177e4SLinus Torvalds error = -EBUSY; 42121da177e4SLinus Torvalds if (oldnd.last_type != LAST_NORM) 42131da177e4SLinus Torvalds goto exit2; 42141da177e4SLinus Torvalds 42154ac91378SJan Blunck new_dir = newnd.path.dentry; 42161da177e4SLinus Torvalds if (newnd.last_type != LAST_NORM) 42171da177e4SLinus Torvalds goto exit2; 42181da177e4SLinus Torvalds 4219c30dabfeSJan Kara error = mnt_want_write(oldnd.path.mnt); 4220c30dabfeSJan Kara if (error) 4221c30dabfeSJan Kara goto exit2; 4222c30dabfeSJan Kara 42230612d9fbSOGAWA Hirofumi oldnd.flags &= ~LOOKUP_PARENT; 42240612d9fbSOGAWA Hirofumi newnd.flags &= ~LOOKUP_PARENT; 42254e9ed2f8SOGAWA Hirofumi newnd.flags |= LOOKUP_RENAME_TARGET; 42260612d9fbSOGAWA Hirofumi 42278e6d782cSJ. Bruce Fields retry_deleg: 42281da177e4SLinus Torvalds trap = lock_rename(new_dir, old_dir); 42291da177e4SLinus Torvalds 423049705b77SChristoph Hellwig old_dentry = lookup_hash(&oldnd); 42311da177e4SLinus Torvalds error = PTR_ERR(old_dentry); 42321da177e4SLinus Torvalds if (IS_ERR(old_dentry)) 42331da177e4SLinus Torvalds goto exit3; 42341da177e4SLinus Torvalds /* source must exist */ 42351da177e4SLinus Torvalds error = -ENOENT; 4236b18825a7SDavid Howells if (d_is_negative(old_dentry)) 42371da177e4SLinus Torvalds goto exit4; 42381da177e4SLinus Torvalds /* unless the source is a directory trailing slashes give -ENOTDIR */ 4239b18825a7SDavid Howells if (!d_is_directory(old_dentry) && !d_is_autodir(old_dentry)) { 42401da177e4SLinus Torvalds error = -ENOTDIR; 42411da177e4SLinus Torvalds if (oldnd.last.name[oldnd.last.len]) 42421da177e4SLinus Torvalds goto exit4; 42431da177e4SLinus Torvalds if (newnd.last.name[newnd.last.len]) 42441da177e4SLinus Torvalds goto exit4; 42451da177e4SLinus Torvalds } 42461da177e4SLinus Torvalds /* source should not be ancestor of target */ 42471da177e4SLinus Torvalds error = -EINVAL; 42481da177e4SLinus Torvalds if (old_dentry == trap) 42491da177e4SLinus Torvalds goto exit4; 425049705b77SChristoph Hellwig new_dentry = lookup_hash(&newnd); 42511da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 42521da177e4SLinus Torvalds if (IS_ERR(new_dentry)) 42531da177e4SLinus Torvalds goto exit4; 42541da177e4SLinus Torvalds /* target should not be an ancestor of source */ 42551da177e4SLinus Torvalds error = -ENOTEMPTY; 42561da177e4SLinus Torvalds if (new_dentry == trap) 42571da177e4SLinus Torvalds goto exit5; 42581da177e4SLinus Torvalds 4259be6d3e56SKentaro Takeda error = security_path_rename(&oldnd.path, old_dentry, 4260be6d3e56SKentaro Takeda &newnd.path, new_dentry); 4261be6d3e56SKentaro Takeda if (error) 4262c30dabfeSJan Kara goto exit5; 42631da177e4SLinus Torvalds error = vfs_rename(old_dir->d_inode, old_dentry, 42648e6d782cSJ. Bruce Fields new_dir->d_inode, new_dentry, 42658e6d782cSJ. Bruce Fields &delegated_inode); 42661da177e4SLinus Torvalds exit5: 42671da177e4SLinus Torvalds dput(new_dentry); 42681da177e4SLinus Torvalds exit4: 42691da177e4SLinus Torvalds dput(old_dentry); 42701da177e4SLinus Torvalds exit3: 42711da177e4SLinus Torvalds unlock_rename(new_dir, old_dir); 42728e6d782cSJ. Bruce Fields if (delegated_inode) { 42738e6d782cSJ. Bruce Fields error = break_deleg_wait(&delegated_inode); 42748e6d782cSJ. Bruce Fields if (!error) 42758e6d782cSJ. Bruce Fields goto retry_deleg; 42768e6d782cSJ. Bruce Fields } 4277c30dabfeSJan Kara mnt_drop_write(oldnd.path.mnt); 42781da177e4SLinus Torvalds exit2: 4279c6a94284SJeff Layton if (retry_estale(error, lookup_flags)) 4280c6a94284SJeff Layton should_retry = true; 42811d957f9bSJan Blunck path_put(&newnd.path); 42822ad94ae6SAl Viro putname(to); 42831da177e4SLinus Torvalds exit1: 42841d957f9bSJan Blunck path_put(&oldnd.path); 42851da177e4SLinus Torvalds putname(from); 4286c6a94284SJeff Layton if (should_retry) { 4287c6a94284SJeff Layton should_retry = false; 4288c6a94284SJeff Layton lookup_flags |= LOOKUP_REVAL; 4289c6a94284SJeff Layton goto retry; 4290c6a94284SJeff Layton } 42912ad94ae6SAl Viro exit: 42921da177e4SLinus Torvalds return error; 42931da177e4SLinus Torvalds } 42941da177e4SLinus Torvalds 4295a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname) 42965590ff0dSUlrich Drepper { 42975590ff0dSUlrich Drepper return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname); 42985590ff0dSUlrich Drepper } 42995590ff0dSUlrich Drepper 43005d826c84SAl Viro int readlink_copy(char __user *buffer, int buflen, const char *link) 43011da177e4SLinus Torvalds { 43025d826c84SAl Viro int len = PTR_ERR(link); 43031da177e4SLinus Torvalds if (IS_ERR(link)) 43041da177e4SLinus Torvalds goto out; 43051da177e4SLinus Torvalds 43061da177e4SLinus Torvalds len = strlen(link); 43071da177e4SLinus Torvalds if (len > (unsigned) buflen) 43081da177e4SLinus Torvalds len = buflen; 43091da177e4SLinus Torvalds if (copy_to_user(buffer, link, len)) 43101da177e4SLinus Torvalds len = -EFAULT; 43111da177e4SLinus Torvalds out: 43121da177e4SLinus Torvalds return len; 43131da177e4SLinus Torvalds } 43145d826c84SAl Viro EXPORT_SYMBOL(readlink_copy); 43151da177e4SLinus Torvalds 43161da177e4SLinus Torvalds /* 43171da177e4SLinus Torvalds * A helper for ->readlink(). This should be used *ONLY* for symlinks that 43181da177e4SLinus Torvalds * have ->follow_link() touching nd only in nd_set_link(). Using (or not 43191da177e4SLinus Torvalds * using) it for any given inode is up to filesystem. 43201da177e4SLinus Torvalds */ 43211da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen) 43221da177e4SLinus Torvalds { 43231da177e4SLinus Torvalds struct nameidata nd; 4324cc314eefSLinus Torvalds void *cookie; 4325694a1764SMarcin Slusarz int res; 4326cc314eefSLinus Torvalds 43271da177e4SLinus Torvalds nd.depth = 0; 4328cc314eefSLinus Torvalds cookie = dentry->d_inode->i_op->follow_link(dentry, &nd); 4329694a1764SMarcin Slusarz if (IS_ERR(cookie)) 4330694a1764SMarcin Slusarz return PTR_ERR(cookie); 4331694a1764SMarcin Slusarz 43325d826c84SAl Viro res = readlink_copy(buffer, buflen, nd_get_link(&nd)); 43331da177e4SLinus Torvalds if (dentry->d_inode->i_op->put_link) 4334cc314eefSLinus Torvalds dentry->d_inode->i_op->put_link(dentry, &nd, cookie); 4335694a1764SMarcin Slusarz return res; 43361da177e4SLinus Torvalds } 43374d359507SAl Viro EXPORT_SYMBOL(generic_readlink); 43381da177e4SLinus Torvalds 43391da177e4SLinus Torvalds /* get the link contents into pagecache */ 43401da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage) 43411da177e4SLinus Torvalds { 4342ebd09abbSDuane Griffin char *kaddr; 43431da177e4SLinus Torvalds struct page *page; 43441da177e4SLinus Torvalds struct address_space *mapping = dentry->d_inode->i_mapping; 4345090d2b18SPekka Enberg page = read_mapping_page(mapping, 0, NULL); 43461da177e4SLinus Torvalds if (IS_ERR(page)) 43476fe6900eSNick Piggin return (char*)page; 43481da177e4SLinus Torvalds *ppage = page; 4349ebd09abbSDuane Griffin kaddr = kmap(page); 4350ebd09abbSDuane Griffin nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1); 4351ebd09abbSDuane Griffin return kaddr; 43521da177e4SLinus Torvalds } 43531da177e4SLinus Torvalds 43541da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen) 43551da177e4SLinus Torvalds { 43561da177e4SLinus Torvalds struct page *page = NULL; 43575d826c84SAl Viro int res = readlink_copy(buffer, buflen, page_getlink(dentry, &page)); 43581da177e4SLinus Torvalds if (page) { 43591da177e4SLinus Torvalds kunmap(page); 43601da177e4SLinus Torvalds page_cache_release(page); 43611da177e4SLinus Torvalds } 43621da177e4SLinus Torvalds return res; 43631da177e4SLinus Torvalds } 43644d359507SAl Viro EXPORT_SYMBOL(page_readlink); 43651da177e4SLinus Torvalds 4366cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd) 43671da177e4SLinus Torvalds { 4368cc314eefSLinus Torvalds struct page *page = NULL; 43691da177e4SLinus Torvalds nd_set_link(nd, page_getlink(dentry, &page)); 4370cc314eefSLinus Torvalds return page; 43711da177e4SLinus Torvalds } 43724d359507SAl Viro EXPORT_SYMBOL(page_follow_link_light); 43731da177e4SLinus Torvalds 4374cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie) 43751da177e4SLinus Torvalds { 4376cc314eefSLinus Torvalds struct page *page = cookie; 4377cc314eefSLinus Torvalds 4378cc314eefSLinus Torvalds if (page) { 43791da177e4SLinus Torvalds kunmap(page); 43801da177e4SLinus Torvalds page_cache_release(page); 43811da177e4SLinus Torvalds } 43821da177e4SLinus Torvalds } 43834d359507SAl Viro EXPORT_SYMBOL(page_put_link); 43841da177e4SLinus Torvalds 438554566b2cSNick Piggin /* 438654566b2cSNick Piggin * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS 438754566b2cSNick Piggin */ 438854566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs) 43891da177e4SLinus Torvalds { 43901da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 43910adb25d2SKirill Korotaev struct page *page; 4392afddba49SNick Piggin void *fsdata; 4393beb497abSDmitriy Monakhov int err; 43941da177e4SLinus Torvalds char *kaddr; 439554566b2cSNick Piggin unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE; 439654566b2cSNick Piggin if (nofs) 439754566b2cSNick Piggin flags |= AOP_FLAG_NOFS; 43981da177e4SLinus Torvalds 43997e53cac4SNeilBrown retry: 4400afddba49SNick Piggin err = pagecache_write_begin(NULL, mapping, 0, len-1, 440154566b2cSNick Piggin flags, &page, &fsdata); 44021da177e4SLinus Torvalds if (err) 4403afddba49SNick Piggin goto fail; 4404afddba49SNick Piggin 4405e8e3c3d6SCong Wang kaddr = kmap_atomic(page); 44061da177e4SLinus Torvalds memcpy(kaddr, symname, len-1); 4407e8e3c3d6SCong Wang kunmap_atomic(kaddr); 4408afddba49SNick Piggin 4409afddba49SNick Piggin err = pagecache_write_end(NULL, mapping, 0, len-1, len-1, 4410afddba49SNick Piggin page, fsdata); 44111da177e4SLinus Torvalds if (err < 0) 44121da177e4SLinus Torvalds goto fail; 4413afddba49SNick Piggin if (err < len-1) 4414afddba49SNick Piggin goto retry; 4415afddba49SNick Piggin 44161da177e4SLinus Torvalds mark_inode_dirty(inode); 44171da177e4SLinus Torvalds return 0; 44181da177e4SLinus Torvalds fail: 44191da177e4SLinus Torvalds return err; 44201da177e4SLinus Torvalds } 44214d359507SAl Viro EXPORT_SYMBOL(__page_symlink); 44221da177e4SLinus Torvalds 44230adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len) 44240adb25d2SKirill Korotaev { 44250adb25d2SKirill Korotaev return __page_symlink(inode, symname, len, 442654566b2cSNick Piggin !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS)); 44270adb25d2SKirill Korotaev } 44284d359507SAl Viro EXPORT_SYMBOL(page_symlink); 44290adb25d2SKirill Korotaev 443092e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = { 44311da177e4SLinus Torvalds .readlink = generic_readlink, 44321da177e4SLinus Torvalds .follow_link = page_follow_link_light, 44331da177e4SLinus Torvalds .put_link = page_put_link, 44341da177e4SLinus Torvalds }; 44351da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations); 4436