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> 191da177e4SLinus Torvalds #include <linux/slab.h> 201da177e4SLinus Torvalds #include <linux/fs.h> 211da177e4SLinus Torvalds #include <linux/namei.h> 221da177e4SLinus Torvalds #include <linux/pagemap.h> 230eeca283SRobert Love #include <linux/fsnotify.h> 241da177e4SLinus Torvalds #include <linux/personality.h> 251da177e4SLinus Torvalds #include <linux/security.h> 266146f0d5SMimi Zohar #include <linux/ima.h> 271da177e4SLinus Torvalds #include <linux/syscalls.h> 281da177e4SLinus Torvalds #include <linux/mount.h> 291da177e4SLinus Torvalds #include <linux/audit.h> 3016f7e0feSRandy Dunlap #include <linux/capability.h> 31834f2a4aSTrond Myklebust #include <linux/file.h> 325590ff0dSUlrich Drepper #include <linux/fcntl.h> 3308ce5f16SSerge E. Hallyn #include <linux/device_cgroup.h> 345ad4e53bSAl Viro #include <linux/fs_struct.h> 35e77819e5SLinus Torvalds #include <linux/posix_acl.h> 361da177e4SLinus Torvalds #include <asm/uaccess.h> 371da177e4SLinus Torvalds 38e81e3f4dSEric Paris #include "internal.h" 39c7105365SAl Viro #include "mount.h" 40e81e3f4dSEric Paris 411da177e4SLinus Torvalds /* [Feb-1997 T. Schoebel-Theuer] 421da177e4SLinus Torvalds * Fundamental changes in the pathname lookup mechanisms (namei) 431da177e4SLinus Torvalds * were necessary because of omirr. The reason is that omirr needs 441da177e4SLinus Torvalds * to know the _real_ pathname, not the user-supplied one, in case 451da177e4SLinus Torvalds * of symlinks (and also when transname replacements occur). 461da177e4SLinus Torvalds * 471da177e4SLinus Torvalds * The new code replaces the old recursive symlink resolution with 481da177e4SLinus Torvalds * an iterative one (in case of non-nested symlink chains). It does 491da177e4SLinus Torvalds * this with calls to <fs>_follow_link(). 501da177e4SLinus Torvalds * As a side effect, dir_namei(), _namei() and follow_link() are now 511da177e4SLinus Torvalds * replaced with a single function lookup_dentry() that can handle all 521da177e4SLinus Torvalds * the special cases of the former code. 531da177e4SLinus Torvalds * 541da177e4SLinus Torvalds * With the new dcache, the pathname is stored at each inode, at least as 551da177e4SLinus Torvalds * long as the refcount of the inode is positive. As a side effect, the 561da177e4SLinus Torvalds * size of the dcache depends on the inode cache and thus is dynamic. 571da177e4SLinus Torvalds * 581da177e4SLinus Torvalds * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink 591da177e4SLinus Torvalds * resolution to correspond with current state of the code. 601da177e4SLinus Torvalds * 611da177e4SLinus Torvalds * Note that the symlink resolution is not *completely* iterative. 621da177e4SLinus Torvalds * There is still a significant amount of tail- and mid- recursion in 631da177e4SLinus Torvalds * the algorithm. Also, note that <fs>_readlink() is not used in 641da177e4SLinus Torvalds * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink() 651da177e4SLinus Torvalds * may return different results than <fs>_follow_link(). Many virtual 661da177e4SLinus Torvalds * filesystems (including /proc) exhibit this behavior. 671da177e4SLinus Torvalds */ 681da177e4SLinus Torvalds 691da177e4SLinus Torvalds /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation: 701da177e4SLinus Torvalds * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL 711da177e4SLinus Torvalds * and the name already exists in form of a symlink, try to create the new 721da177e4SLinus Torvalds * name indicated by the symlink. The old code always complained that the 731da177e4SLinus Torvalds * name already exists, due to not following the symlink even if its target 741da177e4SLinus Torvalds * is nonexistent. The new semantics affects also mknod() and link() when 7525985edcSLucas De Marchi * the name is a symlink pointing to a non-existent name. 761da177e4SLinus Torvalds * 771da177e4SLinus Torvalds * I don't know which semantics is the right one, since I have no access 781da177e4SLinus Torvalds * to standards. But I found by trial that HP-UX 9.0 has the full "new" 791da177e4SLinus Torvalds * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the 801da177e4SLinus Torvalds * "old" one. Personally, I think the new semantics is much more logical. 811da177e4SLinus Torvalds * Note that "ln old new" where "new" is a symlink pointing to a non-existing 821da177e4SLinus Torvalds * file does succeed in both HP-UX and SunOs, but not in Solaris 831da177e4SLinus Torvalds * and in the old Linux semantics. 841da177e4SLinus Torvalds */ 851da177e4SLinus Torvalds 861da177e4SLinus Torvalds /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink 871da177e4SLinus Torvalds * semantics. See the comments in "open_namei" and "do_link" below. 881da177e4SLinus Torvalds * 891da177e4SLinus Torvalds * [10-Sep-98 Alan Modra] Another symlink change. 901da177e4SLinus Torvalds */ 911da177e4SLinus Torvalds 921da177e4SLinus Torvalds /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks: 931da177e4SLinus Torvalds * inside the path - always follow. 941da177e4SLinus Torvalds * in the last component in creation/removal/renaming - never follow. 951da177e4SLinus Torvalds * if LOOKUP_FOLLOW passed - follow. 961da177e4SLinus Torvalds * if the pathname has trailing slashes - follow. 971da177e4SLinus Torvalds * otherwise - don't follow. 981da177e4SLinus Torvalds * (applied in that order). 991da177e4SLinus Torvalds * 1001da177e4SLinus Torvalds * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT 1011da177e4SLinus Torvalds * restored for 2.4. This is the last surviving part of old 4.2BSD bug. 1021da177e4SLinus Torvalds * During the 2.4 we need to fix the userland stuff depending on it - 1031da177e4SLinus Torvalds * hopefully we will be able to get rid of that wart in 2.5. So far only 1041da177e4SLinus Torvalds * XEmacs seems to be relying on it... 1051da177e4SLinus Torvalds */ 1061da177e4SLinus Torvalds /* 1071da177e4SLinus Torvalds * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland) 108a11f3a05SArjan van de Ven * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives 1091da177e4SLinus Torvalds * any extra contention... 1101da177e4SLinus Torvalds */ 1111da177e4SLinus Torvalds 1121da177e4SLinus Torvalds /* In order to reduce some races, while at the same time doing additional 1131da177e4SLinus Torvalds * checking and hopefully speeding things up, we copy filenames to the 1141da177e4SLinus Torvalds * kernel data space before using them.. 1151da177e4SLinus Torvalds * 1161da177e4SLinus Torvalds * POSIX.1 2.4: an empty pathname is invalid (ENOENT). 1171da177e4SLinus Torvalds * PATH_MAX includes the nul terminator --RR. 1181da177e4SLinus Torvalds */ 1191fa1e7f6SAndy Whitcroft static char *getname_flags(const char __user *filename, int flags, int *empty) 1201da177e4SLinus Torvalds { 1213f9f0aa6SLinus Torvalds char *result = __getname(), *err; 1223f9f0aa6SLinus Torvalds int len; 1231da177e4SLinus Torvalds 1243f9f0aa6SLinus Torvalds if (unlikely(!result)) 1254043cde8SEric Paris return ERR_PTR(-ENOMEM); 1261da177e4SLinus Torvalds 1273f9f0aa6SLinus Torvalds len = strncpy_from_user(result, filename, PATH_MAX); 1283f9f0aa6SLinus Torvalds err = ERR_PTR(len); 1293f9f0aa6SLinus Torvalds if (unlikely(len < 0)) 1303f9f0aa6SLinus Torvalds goto error; 1313f9f0aa6SLinus Torvalds 1323f9f0aa6SLinus Torvalds /* The empty path is special. */ 1333f9f0aa6SLinus Torvalds if (unlikely(!len)) { 1343f9f0aa6SLinus Torvalds if (empty) 1351fa1e7f6SAndy Whitcroft *empty = 1; 1363f9f0aa6SLinus Torvalds err = ERR_PTR(-ENOENT); 1373f9f0aa6SLinus Torvalds if (!(flags & LOOKUP_EMPTY)) 1383f9f0aa6SLinus Torvalds goto error; 1391da177e4SLinus Torvalds } 1403f9f0aa6SLinus Torvalds 1413f9f0aa6SLinus Torvalds err = ERR_PTR(-ENAMETOOLONG); 1423f9f0aa6SLinus Torvalds if (likely(len < PATH_MAX)) { 1431da177e4SLinus Torvalds audit_getname(result); 1441da177e4SLinus Torvalds return result; 1451da177e4SLinus Torvalds } 1461da177e4SLinus Torvalds 1473f9f0aa6SLinus Torvalds error: 1483f9f0aa6SLinus Torvalds __putname(result); 1493f9f0aa6SLinus Torvalds return err; 1503f9f0aa6SLinus Torvalds } 1513f9f0aa6SLinus Torvalds 152f52e0c11SAl Viro char *getname(const char __user * filename) 153f52e0c11SAl Viro { 154f7493e5dSLinus Torvalds return getname_flags(filename, 0, NULL); 155f52e0c11SAl Viro } 156f52e0c11SAl Viro 1571da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL 1581da177e4SLinus Torvalds void putname(const char *name) 1591da177e4SLinus Torvalds { 1605ac3a9c2SAl Viro if (unlikely(!audit_dummy_context())) 1611da177e4SLinus Torvalds audit_putname(name); 1621da177e4SLinus Torvalds else 1631da177e4SLinus Torvalds __putname(name); 1641da177e4SLinus Torvalds } 1651da177e4SLinus Torvalds EXPORT_SYMBOL(putname); 1661da177e4SLinus Torvalds #endif 1671da177e4SLinus Torvalds 168e77819e5SLinus Torvalds static int check_acl(struct inode *inode, int mask) 169e77819e5SLinus Torvalds { 17084635d68SLinus Torvalds #ifdef CONFIG_FS_POSIX_ACL 171e77819e5SLinus Torvalds struct posix_acl *acl; 172e77819e5SLinus Torvalds 173e77819e5SLinus Torvalds if (mask & MAY_NOT_BLOCK) { 1743567866bSAl Viro acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS); 1753567866bSAl Viro if (!acl) 176e77819e5SLinus Torvalds return -EAGAIN; 1773567866bSAl Viro /* no ->get_acl() calls in RCU mode... */ 1783567866bSAl Viro if (acl == ACL_NOT_CACHED) 179e77819e5SLinus Torvalds return -ECHILD; 180206b1d09SAri Savolainen return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK); 181e77819e5SLinus Torvalds } 182e77819e5SLinus Torvalds 183e77819e5SLinus Torvalds acl = get_cached_acl(inode, ACL_TYPE_ACCESS); 184e77819e5SLinus Torvalds 185e77819e5SLinus Torvalds /* 1864e34e719SChristoph Hellwig * A filesystem can force a ACL callback by just never filling the 1874e34e719SChristoph Hellwig * ACL cache. But normally you'd fill the cache either at inode 1884e34e719SChristoph Hellwig * instantiation time, or on the first ->get_acl call. 189e77819e5SLinus Torvalds * 1904e34e719SChristoph Hellwig * If the filesystem doesn't have a get_acl() function at all, we'll 1914e34e719SChristoph Hellwig * just create the negative cache entry. 192e77819e5SLinus Torvalds */ 193e77819e5SLinus Torvalds if (acl == ACL_NOT_CACHED) { 1944e34e719SChristoph Hellwig if (inode->i_op->get_acl) { 1954e34e719SChristoph Hellwig acl = inode->i_op->get_acl(inode, ACL_TYPE_ACCESS); 1964e34e719SChristoph Hellwig if (IS_ERR(acl)) 1974e34e719SChristoph Hellwig return PTR_ERR(acl); 1984e34e719SChristoph Hellwig } else { 199e77819e5SLinus Torvalds set_cached_acl(inode, ACL_TYPE_ACCESS, NULL); 200e77819e5SLinus Torvalds return -EAGAIN; 201e77819e5SLinus Torvalds } 2024e34e719SChristoph Hellwig } 203e77819e5SLinus Torvalds 204e77819e5SLinus Torvalds if (acl) { 205e77819e5SLinus Torvalds int error = posix_acl_permission(inode, acl, mask); 206e77819e5SLinus Torvalds posix_acl_release(acl); 207e77819e5SLinus Torvalds return error; 208e77819e5SLinus Torvalds } 20984635d68SLinus Torvalds #endif 210e77819e5SLinus Torvalds 211e77819e5SLinus Torvalds return -EAGAIN; 212e77819e5SLinus Torvalds } 213e77819e5SLinus Torvalds 2145909ccaaSLinus Torvalds /* 215948409c7SAndreas Gruenbacher * This does the basic permission checking 2165909ccaaSLinus Torvalds */ 2177e40145eSAl Viro static int acl_permission_check(struct inode *inode, int mask) 2185909ccaaSLinus Torvalds { 21926cf46beSLinus Torvalds unsigned int mode = inode->i_mode; 2205909ccaaSLinus Torvalds 221e795b717SSerge E. Hallyn if (current_user_ns() != inode_userns(inode)) 222e795b717SSerge E. Hallyn goto other_perms; 223e795b717SSerge E. Hallyn 22414067ff5SLinus Torvalds if (likely(current_fsuid() == inode->i_uid)) 2255909ccaaSLinus Torvalds mode >>= 6; 2265909ccaaSLinus Torvalds else { 227e77819e5SLinus Torvalds if (IS_POSIXACL(inode) && (mode & S_IRWXG)) { 2287e40145eSAl Viro int error = check_acl(inode, mask); 2295909ccaaSLinus Torvalds if (error != -EAGAIN) 2305909ccaaSLinus Torvalds return error; 2315909ccaaSLinus Torvalds } 2325909ccaaSLinus Torvalds 2335909ccaaSLinus Torvalds if (in_group_p(inode->i_gid)) 2345909ccaaSLinus Torvalds mode >>= 3; 2355909ccaaSLinus Torvalds } 2365909ccaaSLinus Torvalds 237e795b717SSerge E. Hallyn other_perms: 2385909ccaaSLinus Torvalds /* 2395909ccaaSLinus Torvalds * If the DACs are ok we don't need any capability check. 2405909ccaaSLinus Torvalds */ 2419c2c7039SAl Viro if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0) 2425909ccaaSLinus Torvalds return 0; 2435909ccaaSLinus Torvalds return -EACCES; 2445909ccaaSLinus Torvalds } 2451da177e4SLinus Torvalds 2461da177e4SLinus Torvalds /** 2471da177e4SLinus Torvalds * generic_permission - check for access rights on a Posix-like filesystem 2481da177e4SLinus Torvalds * @inode: inode to check access rights for 2498fd90c8dSAndreas Gruenbacher * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...) 2501da177e4SLinus Torvalds * 2511da177e4SLinus Torvalds * Used to check for read/write/execute permissions on a file. 2521da177e4SLinus Torvalds * We use "fsuid" for this, letting us set arbitrary permissions 2531da177e4SLinus Torvalds * for filesystem access without changing the "normal" uids which 254b74c79e9SNick Piggin * are used for other things. 255b74c79e9SNick Piggin * 256b74c79e9SNick Piggin * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk 257b74c79e9SNick Piggin * request cannot be satisfied (eg. requires blocking or too much complexity). 258b74c79e9SNick Piggin * It would then be called again in ref-walk mode. 2591da177e4SLinus Torvalds */ 2602830ba7fSAl Viro int generic_permission(struct inode *inode, int mask) 2611da177e4SLinus Torvalds { 2625909ccaaSLinus Torvalds int ret; 2631da177e4SLinus Torvalds 2641da177e4SLinus Torvalds /* 265948409c7SAndreas Gruenbacher * Do the basic permission checks. 2661da177e4SLinus Torvalds */ 2677e40145eSAl Viro ret = acl_permission_check(inode, mask); 2685909ccaaSLinus Torvalds if (ret != -EACCES) 2695909ccaaSLinus Torvalds return ret; 2701da177e4SLinus Torvalds 271d594e7ecSAl Viro if (S_ISDIR(inode->i_mode)) { 272d594e7ecSAl Viro /* DACs are overridable for directories */ 273d594e7ecSAl Viro if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE)) 274d594e7ecSAl Viro return 0; 275d594e7ecSAl Viro if (!(mask & MAY_WRITE)) 276d594e7ecSAl Viro if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH)) 277d594e7ecSAl Viro return 0; 278d594e7ecSAl Viro return -EACCES; 279d594e7ecSAl Viro } 2801da177e4SLinus Torvalds /* 2811da177e4SLinus Torvalds * Read/write DACs are always overridable. 282d594e7ecSAl Viro * Executable DACs are overridable when there is 283d594e7ecSAl Viro * at least one exec bit set. 2841da177e4SLinus Torvalds */ 285d594e7ecSAl Viro if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO)) 286e795b717SSerge E. Hallyn if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE)) 2871da177e4SLinus Torvalds return 0; 2881da177e4SLinus Torvalds 2891da177e4SLinus Torvalds /* 2901da177e4SLinus Torvalds * Searching includes executable on directories, else just read. 2911da177e4SLinus Torvalds */ 2927ea66001SSerge E. Hallyn mask &= MAY_READ | MAY_WRITE | MAY_EXEC; 293d594e7ecSAl Viro if (mask == MAY_READ) 294e795b717SSerge E. Hallyn if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH)) 2951da177e4SLinus Torvalds return 0; 2961da177e4SLinus Torvalds 2971da177e4SLinus Torvalds return -EACCES; 2981da177e4SLinus Torvalds } 2991da177e4SLinus Torvalds 3003ddcd056SLinus Torvalds /* 3013ddcd056SLinus Torvalds * We _really_ want to just do "generic_permission()" without 3023ddcd056SLinus Torvalds * even looking at the inode->i_op values. So we keep a cache 3033ddcd056SLinus Torvalds * flag in inode->i_opflags, that says "this has not special 3043ddcd056SLinus Torvalds * permission function, use the fast case". 3053ddcd056SLinus Torvalds */ 3063ddcd056SLinus Torvalds static inline int do_inode_permission(struct inode *inode, int mask) 3073ddcd056SLinus Torvalds { 3083ddcd056SLinus Torvalds if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) { 3093ddcd056SLinus Torvalds if (likely(inode->i_op->permission)) 3103ddcd056SLinus Torvalds return inode->i_op->permission(inode, mask); 3113ddcd056SLinus Torvalds 3123ddcd056SLinus Torvalds /* This gets set once for the inode lifetime */ 3133ddcd056SLinus Torvalds spin_lock(&inode->i_lock); 3143ddcd056SLinus Torvalds inode->i_opflags |= IOP_FASTPERM; 3153ddcd056SLinus Torvalds spin_unlock(&inode->i_lock); 3163ddcd056SLinus Torvalds } 3173ddcd056SLinus Torvalds return generic_permission(inode, mask); 3183ddcd056SLinus Torvalds } 3193ddcd056SLinus Torvalds 320cb23beb5SChristoph Hellwig /** 321cb23beb5SChristoph Hellwig * inode_permission - check for access rights to a given inode 322cb23beb5SChristoph Hellwig * @inode: inode to check permission on 3238fd90c8dSAndreas Gruenbacher * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...) 324cb23beb5SChristoph Hellwig * 325cb23beb5SChristoph Hellwig * Used to check for read/write/execute permissions on an inode. 326cb23beb5SChristoph Hellwig * We use "fsuid" for this, letting us set arbitrary permissions 327cb23beb5SChristoph Hellwig * for filesystem access without changing the "normal" uids which 328cb23beb5SChristoph Hellwig * are used for other things. 329948409c7SAndreas Gruenbacher * 330948409c7SAndreas Gruenbacher * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask. 331cb23beb5SChristoph Hellwig */ 332f419a2e3SAl Viro int inode_permission(struct inode *inode, int mask) 3331da177e4SLinus Torvalds { 334e6305c43SAl Viro int retval; 3351da177e4SLinus Torvalds 3363ddcd056SLinus Torvalds if (unlikely(mask & MAY_WRITE)) { 33722590e41SMiklos Szeredi umode_t mode = inode->i_mode; 3381da177e4SLinus Torvalds 3391da177e4SLinus Torvalds /* 3401da177e4SLinus Torvalds * Nobody gets write access to a read-only fs. 3411da177e4SLinus Torvalds */ 3421da177e4SLinus Torvalds if (IS_RDONLY(inode) && 3431da177e4SLinus Torvalds (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) 3441da177e4SLinus Torvalds return -EROFS; 3451da177e4SLinus Torvalds 3461da177e4SLinus Torvalds /* 3471da177e4SLinus Torvalds * Nobody gets write access to an immutable file. 3481da177e4SLinus Torvalds */ 3491da177e4SLinus Torvalds if (IS_IMMUTABLE(inode)) 3501da177e4SLinus Torvalds return -EACCES; 3511da177e4SLinus Torvalds } 3521da177e4SLinus Torvalds 3533ddcd056SLinus Torvalds retval = do_inode_permission(inode, mask); 3541da177e4SLinus Torvalds if (retval) 3551da177e4SLinus Torvalds return retval; 3561da177e4SLinus Torvalds 35708ce5f16SSerge E. Hallyn retval = devcgroup_inode_permission(inode, mask); 35808ce5f16SSerge E. Hallyn if (retval) 35908ce5f16SSerge E. Hallyn return retval; 36008ce5f16SSerge E. Hallyn 361d09ca739SEric Paris return security_inode_permission(inode, mask); 3621da177e4SLinus Torvalds } 3631da177e4SLinus Torvalds 364f4d6ff89SAl Viro /** 3655dd784d0SJan Blunck * path_get - get a reference to a path 3665dd784d0SJan Blunck * @path: path to get the reference to 3675dd784d0SJan Blunck * 3685dd784d0SJan Blunck * Given a path increment the reference count to the dentry and the vfsmount. 3695dd784d0SJan Blunck */ 3705dd784d0SJan Blunck void path_get(struct path *path) 3715dd784d0SJan Blunck { 3725dd784d0SJan Blunck mntget(path->mnt); 3735dd784d0SJan Blunck dget(path->dentry); 3745dd784d0SJan Blunck } 3755dd784d0SJan Blunck EXPORT_SYMBOL(path_get); 3765dd784d0SJan Blunck 3775dd784d0SJan Blunck /** 3781d957f9bSJan Blunck * path_put - put a reference to a path 3791d957f9bSJan Blunck * @path: path to put the reference to 3801d957f9bSJan Blunck * 3811d957f9bSJan Blunck * Given a path decrement the reference count to the dentry and the vfsmount. 3821d957f9bSJan Blunck */ 3831d957f9bSJan Blunck void path_put(struct path *path) 3841da177e4SLinus Torvalds { 3851d957f9bSJan Blunck dput(path->dentry); 3861d957f9bSJan Blunck mntput(path->mnt); 3871da177e4SLinus Torvalds } 3881d957f9bSJan Blunck EXPORT_SYMBOL(path_put); 3891da177e4SLinus Torvalds 39019660af7SAl Viro /* 39131e6b01fSNick Piggin * Path walking has 2 modes, rcu-walk and ref-walk (see 39219660af7SAl Viro * Documentation/filesystems/path-lookup.txt). In situations when we can't 39319660af7SAl Viro * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab 39419660af7SAl Viro * normal reference counts on dentries and vfsmounts to transition to rcu-walk 39519660af7SAl Viro * mode. Refcounts are grabbed at the last known good point before rcu-walk 39619660af7SAl Viro * got stuck, so ref-walk may continue from there. If this is not successful 39719660af7SAl Viro * (eg. a seqcount has changed), then failure is returned and it's up to caller 39819660af7SAl Viro * to restart the path walk from the beginning in ref-walk mode. 39931e6b01fSNick Piggin */ 40031e6b01fSNick Piggin 40131e6b01fSNick Piggin /** 40219660af7SAl Viro * unlazy_walk - try to switch to ref-walk mode. 40319660af7SAl Viro * @nd: nameidata pathwalk data 40419660af7SAl Viro * @dentry: child of nd->path.dentry or NULL 40539191628SRandy Dunlap * Returns: 0 on success, -ECHILD on failure 40631e6b01fSNick Piggin * 40719660af7SAl Viro * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry 40819660af7SAl Viro * for ref-walk mode. @dentry must be a path found by a do_lookup call on 40919660af7SAl Viro * @nd or NULL. Must be called from rcu-walk context. 41031e6b01fSNick Piggin */ 41119660af7SAl Viro static int unlazy_walk(struct nameidata *nd, struct dentry *dentry) 41231e6b01fSNick Piggin { 41331e6b01fSNick Piggin struct fs_struct *fs = current->fs; 41431e6b01fSNick Piggin struct dentry *parent = nd->path.dentry; 4155b6ca027SAl Viro int want_root = 0; 41631e6b01fSNick Piggin 41731e6b01fSNick Piggin BUG_ON(!(nd->flags & LOOKUP_RCU)); 4185b6ca027SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) { 4195b6ca027SAl Viro want_root = 1; 42031e6b01fSNick Piggin spin_lock(&fs->lock); 42131e6b01fSNick Piggin if (nd->root.mnt != fs->root.mnt || 42231e6b01fSNick Piggin nd->root.dentry != fs->root.dentry) 42331e6b01fSNick Piggin goto err_root; 42431e6b01fSNick Piggin } 42531e6b01fSNick Piggin spin_lock(&parent->d_lock); 42619660af7SAl Viro if (!dentry) { 42719660af7SAl Viro if (!__d_rcu_to_refcount(parent, nd->seq)) 42819660af7SAl Viro goto err_parent; 42919660af7SAl Viro BUG_ON(nd->inode != parent->d_inode); 43019660af7SAl Viro } else { 43194c0d4ecSAl Viro if (dentry->d_parent != parent) 43294c0d4ecSAl Viro goto err_parent; 43331e6b01fSNick Piggin spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); 43431e6b01fSNick Piggin if (!__d_rcu_to_refcount(dentry, nd->seq)) 43519660af7SAl Viro goto err_child; 43631e6b01fSNick Piggin /* 43719660af7SAl Viro * If the sequence check on the child dentry passed, then 43819660af7SAl Viro * the child has not been removed from its parent. This 43919660af7SAl Viro * means the parent dentry must be valid and able to take 44019660af7SAl Viro * a reference at this point. 44131e6b01fSNick Piggin */ 44231e6b01fSNick Piggin BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent); 44331e6b01fSNick Piggin BUG_ON(!parent->d_count); 44431e6b01fSNick Piggin parent->d_count++; 44531e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 44619660af7SAl Viro } 44731e6b01fSNick Piggin spin_unlock(&parent->d_lock); 4485b6ca027SAl Viro if (want_root) { 44931e6b01fSNick Piggin path_get(&nd->root); 45031e6b01fSNick Piggin spin_unlock(&fs->lock); 45131e6b01fSNick Piggin } 45231e6b01fSNick Piggin mntget(nd->path.mnt); 45331e6b01fSNick Piggin 45431e6b01fSNick Piggin rcu_read_unlock(); 45531e6b01fSNick Piggin br_read_unlock(vfsmount_lock); 45631e6b01fSNick Piggin nd->flags &= ~LOOKUP_RCU; 45731e6b01fSNick Piggin return 0; 45819660af7SAl Viro 45919660af7SAl Viro err_child: 46031e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 46119660af7SAl Viro err_parent: 46231e6b01fSNick Piggin spin_unlock(&parent->d_lock); 46331e6b01fSNick Piggin err_root: 4645b6ca027SAl Viro if (want_root) 46531e6b01fSNick Piggin spin_unlock(&fs->lock); 46631e6b01fSNick Piggin return -ECHILD; 46731e6b01fSNick Piggin } 46831e6b01fSNick Piggin 46931e6b01fSNick Piggin /** 470834f2a4aSTrond Myklebust * release_open_intent - free up open intent resources 471834f2a4aSTrond Myklebust * @nd: pointer to nameidata 472834f2a4aSTrond Myklebust */ 473834f2a4aSTrond Myklebust void release_open_intent(struct nameidata *nd) 474834f2a4aSTrond Myklebust { 4752dab5974SLinus Torvalds struct file *file = nd->intent.open.file; 4762dab5974SLinus Torvalds 4772dab5974SLinus Torvalds if (file && !IS_ERR(file)) { 4782dab5974SLinus Torvalds if (file->f_path.dentry == NULL) 4792dab5974SLinus Torvalds put_filp(file); 480834f2a4aSTrond Myklebust else 4812dab5974SLinus Torvalds fput(file); 4822dab5974SLinus Torvalds } 483834f2a4aSTrond Myklebust } 484834f2a4aSTrond Myklebust 485f60aef7eSAl Viro static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd) 48634286d66SNick Piggin { 487f60aef7eSAl Viro return dentry->d_op->d_revalidate(dentry, nd); 48834286d66SNick Piggin } 48934286d66SNick Piggin 4909f1fafeeSAl Viro /** 4919f1fafeeSAl Viro * complete_walk - successful completion of path walk 4929f1fafeeSAl Viro * @nd: pointer nameidata 49339159de2SJeff Layton * 4949f1fafeeSAl Viro * If we had been in RCU mode, drop out of it and legitimize nd->path. 4959f1fafeeSAl Viro * Revalidate the final result, unless we'd already done that during 4969f1fafeeSAl Viro * the path walk or the filesystem doesn't ask for it. Return 0 on 4979f1fafeeSAl Viro * success, -error on failure. In case of failure caller does not 4989f1fafeeSAl Viro * need to drop nd->path. 49939159de2SJeff Layton */ 5009f1fafeeSAl Viro static int complete_walk(struct nameidata *nd) 50139159de2SJeff Layton { 50216c2cd71SAl Viro struct dentry *dentry = nd->path.dentry; 50339159de2SJeff Layton int status; 50439159de2SJeff Layton 5059f1fafeeSAl Viro if (nd->flags & LOOKUP_RCU) { 5069f1fafeeSAl Viro nd->flags &= ~LOOKUP_RCU; 5079f1fafeeSAl Viro if (!(nd->flags & LOOKUP_ROOT)) 5089f1fafeeSAl Viro nd->root.mnt = NULL; 5099f1fafeeSAl Viro spin_lock(&dentry->d_lock); 5109f1fafeeSAl Viro if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) { 5119f1fafeeSAl Viro spin_unlock(&dentry->d_lock); 5129f1fafeeSAl Viro rcu_read_unlock(); 5139f1fafeeSAl Viro br_read_unlock(vfsmount_lock); 5149f1fafeeSAl Viro return -ECHILD; 5159f1fafeeSAl Viro } 5169f1fafeeSAl Viro BUG_ON(nd->inode != dentry->d_inode); 5179f1fafeeSAl Viro spin_unlock(&dentry->d_lock); 5189f1fafeeSAl Viro mntget(nd->path.mnt); 5199f1fafeeSAl Viro rcu_read_unlock(); 5209f1fafeeSAl Viro br_read_unlock(vfsmount_lock); 5219f1fafeeSAl Viro } 5229f1fafeeSAl Viro 52316c2cd71SAl Viro if (likely(!(nd->flags & LOOKUP_JUMPED))) 52439159de2SJeff Layton return 0; 52539159de2SJeff Layton 52616c2cd71SAl Viro if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE))) 52716c2cd71SAl Viro return 0; 52816c2cd71SAl Viro 52916c2cd71SAl Viro if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT))) 53016c2cd71SAl Viro return 0; 53116c2cd71SAl Viro 53216c2cd71SAl Viro /* Note: we do not d_invalidate() */ 53334286d66SNick Piggin status = d_revalidate(dentry, nd); 53439159de2SJeff Layton if (status > 0) 53539159de2SJeff Layton return 0; 53639159de2SJeff Layton 53716c2cd71SAl Viro if (!status) 53839159de2SJeff Layton status = -ESTALE; 53916c2cd71SAl Viro 5409f1fafeeSAl Viro path_put(&nd->path); 54139159de2SJeff Layton return status; 54239159de2SJeff Layton } 54339159de2SJeff Layton 5442a737871SAl Viro static __always_inline void set_root(struct nameidata *nd) 5452a737871SAl Viro { 546f7ad3c6bSMiklos Szeredi if (!nd->root.mnt) 547f7ad3c6bSMiklos Szeredi get_fs_root(current->fs, &nd->root); 5482a737871SAl Viro } 5492a737871SAl Viro 5506de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *); 5516de88d72SAl Viro 55231e6b01fSNick Piggin static __always_inline void set_root_rcu(struct nameidata *nd) 55331e6b01fSNick Piggin { 55431e6b01fSNick Piggin if (!nd->root.mnt) { 55531e6b01fSNick Piggin struct fs_struct *fs = current->fs; 556c28cc364SNick Piggin unsigned seq; 557c28cc364SNick Piggin 558c28cc364SNick Piggin do { 559c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 56031e6b01fSNick Piggin nd->root = fs->root; 561c1530019STim Chen nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq); 562c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 56331e6b01fSNick Piggin } 56431e6b01fSNick Piggin } 56531e6b01fSNick Piggin 566f1662356SArjan van de Ven static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link) 5671da177e4SLinus Torvalds { 56831e6b01fSNick Piggin int ret; 56931e6b01fSNick Piggin 5701da177e4SLinus Torvalds if (IS_ERR(link)) 5711da177e4SLinus Torvalds goto fail; 5721da177e4SLinus Torvalds 5731da177e4SLinus Torvalds if (*link == '/') { 5742a737871SAl Viro set_root(nd); 5751d957f9bSJan Blunck path_put(&nd->path); 5762a737871SAl Viro nd->path = nd->root; 5772a737871SAl Viro path_get(&nd->root); 57816c2cd71SAl Viro nd->flags |= LOOKUP_JUMPED; 5791da177e4SLinus Torvalds } 58031e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 581b4091d5fSChristoph Hellwig 58231e6b01fSNick Piggin ret = link_path_walk(link, nd); 58331e6b01fSNick Piggin return ret; 5841da177e4SLinus Torvalds fail: 5851d957f9bSJan Blunck path_put(&nd->path); 5861da177e4SLinus Torvalds return PTR_ERR(link); 5871da177e4SLinus Torvalds } 5881da177e4SLinus Torvalds 5891d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd) 590051d3812SIan Kent { 591051d3812SIan Kent dput(path->dentry); 5924ac91378SJan Blunck if (path->mnt != nd->path.mnt) 593051d3812SIan Kent mntput(path->mnt); 594051d3812SIan Kent } 595051d3812SIan Kent 5967b9337aaSNick Piggin static inline void path_to_nameidata(const struct path *path, 5977b9337aaSNick Piggin struct nameidata *nd) 598051d3812SIan Kent { 59931e6b01fSNick Piggin if (!(nd->flags & LOOKUP_RCU)) { 6004ac91378SJan Blunck dput(nd->path.dentry); 60131e6b01fSNick Piggin if (nd->path.mnt != path->mnt) 6024ac91378SJan Blunck mntput(nd->path.mnt); 6039a229683SHuang Shijie } 60431e6b01fSNick Piggin nd->path.mnt = path->mnt; 6054ac91378SJan Blunck nd->path.dentry = path->dentry; 606051d3812SIan Kent } 607051d3812SIan Kent 608574197e0SAl Viro static inline void put_link(struct nameidata *nd, struct path *link, void *cookie) 609574197e0SAl Viro { 610574197e0SAl Viro struct inode *inode = link->dentry->d_inode; 611574197e0SAl Viro if (!IS_ERR(cookie) && inode->i_op->put_link) 612574197e0SAl Viro inode->i_op->put_link(link->dentry, nd, cookie); 613574197e0SAl Viro path_put(link); 614574197e0SAl Viro } 615574197e0SAl Viro 616def4af30SAl Viro static __always_inline int 617574197e0SAl Viro follow_link(struct path *link, struct nameidata *nd, void **p) 6181da177e4SLinus Torvalds { 6191da177e4SLinus Torvalds int error; 6207b9337aaSNick Piggin struct dentry *dentry = link->dentry; 6211da177e4SLinus Torvalds 622844a3917SAl Viro BUG_ON(nd->flags & LOOKUP_RCU); 623844a3917SAl Viro 6240e794589SAl Viro if (link->mnt == nd->path.mnt) 6250e794589SAl Viro mntget(link->mnt); 6260e794589SAl Viro 627574197e0SAl Viro if (unlikely(current->total_link_count >= 40)) { 628574197e0SAl Viro *p = ERR_PTR(-ELOOP); /* no ->put_link(), please */ 629574197e0SAl Viro path_put(&nd->path); 630574197e0SAl Viro return -ELOOP; 631574197e0SAl Viro } 632574197e0SAl Viro cond_resched(); 633574197e0SAl Viro current->total_link_count++; 634574197e0SAl Viro 63568ac1234SAl Viro touch_atime(link); 6361da177e4SLinus Torvalds nd_set_link(nd, NULL); 637cd4e91d3SAl Viro 63836f3b4f6SAl Viro error = security_inode_follow_link(link->dentry, nd); 63936f3b4f6SAl Viro if (error) { 64036f3b4f6SAl Viro *p = ERR_PTR(error); /* no ->put_link(), please */ 64136f3b4f6SAl Viro path_put(&nd->path); 64236f3b4f6SAl Viro return error; 64336f3b4f6SAl Viro } 64436f3b4f6SAl Viro 64586acdca1SAl Viro nd->last_type = LAST_BIND; 646def4af30SAl Viro *p = dentry->d_inode->i_op->follow_link(dentry, nd); 647def4af30SAl Viro error = PTR_ERR(*p); 648def4af30SAl Viro if (!IS_ERR(*p)) { 6491da177e4SLinus Torvalds char *s = nd_get_link(nd); 650cc314eefSLinus Torvalds error = 0; 6511da177e4SLinus Torvalds if (s) 6521da177e4SLinus Torvalds error = __vfs_follow_link(nd, s); 653bcda7652SAl Viro else if (nd->last_type == LAST_BIND) { 65416c2cd71SAl Viro nd->flags |= LOOKUP_JUMPED; 655b21041d0SAl Viro nd->inode = nd->path.dentry->d_inode; 656b21041d0SAl Viro if (nd->inode->i_op->follow_link) { 657bcda7652SAl Viro /* stepped on a _really_ weird one */ 658bcda7652SAl Viro path_put(&nd->path); 659bcda7652SAl Viro error = -ELOOP; 660bcda7652SAl Viro } 661bcda7652SAl Viro } 6621da177e4SLinus Torvalds } 6631da177e4SLinus Torvalds return error; 6641da177e4SLinus Torvalds } 6651da177e4SLinus Torvalds 66631e6b01fSNick Piggin static int follow_up_rcu(struct path *path) 66731e6b01fSNick Piggin { 6680714a533SAl Viro struct mount *mnt = real_mount(path->mnt); 6690714a533SAl Viro struct mount *parent; 67031e6b01fSNick Piggin struct dentry *mountpoint; 67131e6b01fSNick Piggin 6720714a533SAl Viro parent = mnt->mnt_parent; 6730714a533SAl Viro if (&parent->mnt == path->mnt) 67431e6b01fSNick Piggin return 0; 675a73324daSAl Viro mountpoint = mnt->mnt_mountpoint; 67631e6b01fSNick Piggin path->dentry = mountpoint; 6770714a533SAl Viro path->mnt = &parent->mnt; 67831e6b01fSNick Piggin return 1; 67931e6b01fSNick Piggin } 68031e6b01fSNick Piggin 681bab77ebfSAl Viro int follow_up(struct path *path) 6821da177e4SLinus Torvalds { 6830714a533SAl Viro struct mount *mnt = real_mount(path->mnt); 6840714a533SAl Viro struct mount *parent; 6851da177e4SLinus Torvalds struct dentry *mountpoint; 68699b7db7bSNick Piggin 68799b7db7bSNick Piggin br_read_lock(vfsmount_lock); 6880714a533SAl Viro parent = mnt->mnt_parent; 6890714a533SAl Viro if (&parent->mnt == path->mnt) { 69099b7db7bSNick Piggin br_read_unlock(vfsmount_lock); 6911da177e4SLinus Torvalds return 0; 6921da177e4SLinus Torvalds } 6930714a533SAl Viro mntget(&parent->mnt); 694a73324daSAl Viro mountpoint = dget(mnt->mnt_mountpoint); 69599b7db7bSNick Piggin br_read_unlock(vfsmount_lock); 696bab77ebfSAl Viro dput(path->dentry); 697bab77ebfSAl Viro path->dentry = mountpoint; 698bab77ebfSAl Viro mntput(path->mnt); 6990714a533SAl Viro path->mnt = &parent->mnt; 7001da177e4SLinus Torvalds return 1; 7011da177e4SLinus Torvalds } 7021da177e4SLinus Torvalds 703b5c84bf6SNick Piggin /* 7049875cf80SDavid Howells * Perform an automount 7059875cf80SDavid Howells * - return -EISDIR to tell follow_managed() to stop and return the path we 7069875cf80SDavid Howells * were called with. 7071da177e4SLinus Torvalds */ 7089875cf80SDavid Howells static int follow_automount(struct path *path, unsigned flags, 7099875cf80SDavid Howells bool *need_mntput) 71031e6b01fSNick Piggin { 7119875cf80SDavid Howells struct vfsmount *mnt; 712ea5b778aSDavid Howells int err; 7139875cf80SDavid Howells 7149875cf80SDavid Howells if (!path->dentry->d_op || !path->dentry->d_op->d_automount) 7159875cf80SDavid Howells return -EREMOTE; 7169875cf80SDavid Howells 7170ec26fd0SMiklos Szeredi /* We don't want to mount if someone's just doing a stat - 7180ec26fd0SMiklos Szeredi * unless they're stat'ing a directory and appended a '/' to 7190ec26fd0SMiklos Szeredi * the name. 7200ec26fd0SMiklos Szeredi * 7210ec26fd0SMiklos Szeredi * We do, however, want to mount if someone wants to open or 7225a30d8a2SDavid Howells * create a file of any type under the mountpoint, wants to 7230ec26fd0SMiklos Szeredi * traverse through the mountpoint or wants to open the 7240ec26fd0SMiklos Szeredi * mounted directory. Also, autofs may mark negative dentries 7250ec26fd0SMiklos Szeredi * as being automount points. These will need the attentions 7260ec26fd0SMiklos Szeredi * of the daemon to instantiate them before they can be used. 7275a30d8a2SDavid Howells */ 7285a30d8a2SDavid Howells if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY | 729d94c177bSLinus Torvalds LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) && 7305a30d8a2SDavid Howells path->dentry->d_inode) 7319875cf80SDavid Howells return -EISDIR; 7320ec26fd0SMiklos Szeredi 7339875cf80SDavid Howells current->total_link_count++; 7349875cf80SDavid Howells if (current->total_link_count >= 40) 7359875cf80SDavid Howells return -ELOOP; 7369875cf80SDavid Howells 7379875cf80SDavid Howells mnt = path->dentry->d_op->d_automount(path); 7389875cf80SDavid Howells if (IS_ERR(mnt)) { 7399875cf80SDavid Howells /* 7409875cf80SDavid Howells * The filesystem is allowed to return -EISDIR here to indicate 7419875cf80SDavid Howells * it doesn't want to automount. For instance, autofs would do 7429875cf80SDavid Howells * this so that its userspace daemon can mount on this dentry. 7439875cf80SDavid Howells * 7449875cf80SDavid Howells * However, we can only permit this if it's a terminal point in 7459875cf80SDavid Howells * the path being looked up; if it wasn't then the remainder of 7469875cf80SDavid Howells * the path is inaccessible and we should say so. 7479875cf80SDavid Howells */ 74849084c3bSAl Viro if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT)) 7499875cf80SDavid Howells return -EREMOTE; 7509875cf80SDavid Howells return PTR_ERR(mnt); 75131e6b01fSNick Piggin } 752ea5b778aSDavid Howells 7539875cf80SDavid Howells if (!mnt) /* mount collision */ 7549875cf80SDavid Howells return 0; 7559875cf80SDavid Howells 7568aef1884SAl Viro if (!*need_mntput) { 7578aef1884SAl Viro /* lock_mount() may release path->mnt on error */ 7588aef1884SAl Viro mntget(path->mnt); 7598aef1884SAl Viro *need_mntput = true; 7608aef1884SAl Viro } 76119a167afSAl Viro err = finish_automount(mnt, path); 762ea5b778aSDavid Howells 763ea5b778aSDavid Howells switch (err) { 764ea5b778aSDavid Howells case -EBUSY: 765ea5b778aSDavid Howells /* Someone else made a mount here whilst we were busy */ 76619a167afSAl Viro return 0; 767ea5b778aSDavid Howells case 0: 7688aef1884SAl Viro path_put(path); 7699875cf80SDavid Howells path->mnt = mnt; 7709875cf80SDavid Howells path->dentry = dget(mnt->mnt_root); 7719875cf80SDavid Howells return 0; 77219a167afSAl Viro default: 77319a167afSAl Viro return err; 7749875cf80SDavid Howells } 77519a167afSAl Viro 776ea5b778aSDavid Howells } 7779875cf80SDavid Howells 7789875cf80SDavid Howells /* 7799875cf80SDavid Howells * Handle a dentry that is managed in some way. 780cc53ce53SDavid Howells * - Flagged for transit management (autofs) 7819875cf80SDavid Howells * - Flagged as mountpoint 7829875cf80SDavid Howells * - Flagged as automount point 7839875cf80SDavid Howells * 7849875cf80SDavid Howells * This may only be called in refwalk mode. 7859875cf80SDavid Howells * 7869875cf80SDavid Howells * Serialization is taken care of in namespace.c 7879875cf80SDavid Howells */ 7889875cf80SDavid Howells static int follow_managed(struct path *path, unsigned flags) 7899875cf80SDavid Howells { 7908aef1884SAl Viro struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */ 7919875cf80SDavid Howells unsigned managed; 7929875cf80SDavid Howells bool need_mntput = false; 7938aef1884SAl Viro int ret = 0; 7949875cf80SDavid Howells 7959875cf80SDavid Howells /* Given that we're not holding a lock here, we retain the value in a 7969875cf80SDavid Howells * local variable for each dentry as we look at it so that we don't see 7979875cf80SDavid Howells * the components of that value change under us */ 7989875cf80SDavid Howells while (managed = ACCESS_ONCE(path->dentry->d_flags), 7999875cf80SDavid Howells managed &= DCACHE_MANAGED_DENTRY, 8009875cf80SDavid Howells unlikely(managed != 0)) { 801cc53ce53SDavid Howells /* Allow the filesystem to manage the transit without i_mutex 802cc53ce53SDavid Howells * being held. */ 803cc53ce53SDavid Howells if (managed & DCACHE_MANAGE_TRANSIT) { 804cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op); 805cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op->d_manage); 8061aed3e42SAl Viro ret = path->dentry->d_op->d_manage(path->dentry, false); 807cc53ce53SDavid Howells if (ret < 0) 8088aef1884SAl Viro break; 809cc53ce53SDavid Howells } 810cc53ce53SDavid Howells 8119875cf80SDavid Howells /* Transit to a mounted filesystem. */ 8129875cf80SDavid Howells if (managed & DCACHE_MOUNTED) { 8139875cf80SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 8149875cf80SDavid Howells if (mounted) { 8159875cf80SDavid Howells dput(path->dentry); 8169875cf80SDavid Howells if (need_mntput) 817463ffb2eSAl Viro mntput(path->mnt); 818463ffb2eSAl Viro path->mnt = mounted; 819463ffb2eSAl Viro path->dentry = dget(mounted->mnt_root); 8209875cf80SDavid Howells need_mntput = true; 8219875cf80SDavid Howells continue; 822463ffb2eSAl Viro } 823463ffb2eSAl Viro 8249875cf80SDavid Howells /* Something is mounted on this dentry in another 8259875cf80SDavid Howells * namespace and/or whatever was mounted there in this 8269875cf80SDavid Howells * namespace got unmounted before we managed to get the 8279875cf80SDavid Howells * vfsmount_lock */ 8281da177e4SLinus Torvalds } 8299875cf80SDavid Howells 8309875cf80SDavid Howells /* Handle an automount point */ 8319875cf80SDavid Howells if (managed & DCACHE_NEED_AUTOMOUNT) { 8329875cf80SDavid Howells ret = follow_automount(path, flags, &need_mntput); 8339875cf80SDavid Howells if (ret < 0) 8348aef1884SAl Viro break; 8359875cf80SDavid Howells continue; 8369875cf80SDavid Howells } 8379875cf80SDavid Howells 8389875cf80SDavid Howells /* We didn't change the current path point */ 8399875cf80SDavid Howells break; 8409875cf80SDavid Howells } 8418aef1884SAl Viro 8428aef1884SAl Viro if (need_mntput && path->mnt == mnt) 8438aef1884SAl Viro mntput(path->mnt); 8448aef1884SAl Viro if (ret == -EISDIR) 8458aef1884SAl Viro ret = 0; 846a3fbbde7SAl Viro return ret < 0 ? ret : need_mntput; 8471da177e4SLinus Torvalds } 8481da177e4SLinus Torvalds 849cc53ce53SDavid Howells int follow_down_one(struct path *path) 8501da177e4SLinus Torvalds { 8511da177e4SLinus Torvalds struct vfsmount *mounted; 8521da177e4SLinus Torvalds 8531c755af4SAl Viro mounted = lookup_mnt(path); 8541da177e4SLinus Torvalds if (mounted) { 8559393bd07SAl Viro dput(path->dentry); 8569393bd07SAl Viro mntput(path->mnt); 8579393bd07SAl Viro path->mnt = mounted; 8589393bd07SAl Viro path->dentry = dget(mounted->mnt_root); 8591da177e4SLinus Torvalds return 1; 8601da177e4SLinus Torvalds } 8611da177e4SLinus Torvalds return 0; 8621da177e4SLinus Torvalds } 8631da177e4SLinus Torvalds 86462a7375eSIan Kent static inline bool managed_dentry_might_block(struct dentry *dentry) 86562a7375eSIan Kent { 86662a7375eSIan Kent return (dentry->d_flags & DCACHE_MANAGE_TRANSIT && 86762a7375eSIan Kent dentry->d_op->d_manage(dentry, true) < 0); 86862a7375eSIan Kent } 86962a7375eSIan Kent 8709875cf80SDavid Howells /* 871287548e4SAl Viro * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if 872287548e4SAl Viro * we meet a managed dentry that would need blocking. 8739875cf80SDavid Howells */ 8749875cf80SDavid Howells static bool __follow_mount_rcu(struct nameidata *nd, struct path *path, 875287548e4SAl Viro struct inode **inode) 8769875cf80SDavid Howells { 87762a7375eSIan Kent for (;;) { 878c7105365SAl Viro struct mount *mounted; 87962a7375eSIan Kent /* 88062a7375eSIan Kent * Don't forget we might have a non-mountpoint managed dentry 88162a7375eSIan Kent * that wants to block transit. 88262a7375eSIan Kent */ 883287548e4SAl Viro if (unlikely(managed_dentry_might_block(path->dentry))) 884ab90911fSDavid Howells return false; 88562a7375eSIan Kent 88662a7375eSIan Kent if (!d_mountpoint(path->dentry)) 88762a7375eSIan Kent break; 88862a7375eSIan Kent 8899875cf80SDavid Howells mounted = __lookup_mnt(path->mnt, path->dentry, 1); 8909875cf80SDavid Howells if (!mounted) 8919875cf80SDavid Howells break; 892c7105365SAl Viro path->mnt = &mounted->mnt; 893c7105365SAl Viro path->dentry = mounted->mnt.mnt_root; 894a3fbbde7SAl Viro nd->flags |= LOOKUP_JUMPED; 8959875cf80SDavid Howells nd->seq = read_seqcount_begin(&path->dentry->d_seq); 89659430262SLinus Torvalds /* 89759430262SLinus Torvalds * Update the inode too. We don't need to re-check the 89859430262SLinus Torvalds * dentry sequence number here after this d_inode read, 89959430262SLinus Torvalds * because a mount-point is always pinned. 90059430262SLinus Torvalds */ 90159430262SLinus Torvalds *inode = path->dentry->d_inode; 9029875cf80SDavid Howells } 9039875cf80SDavid Howells return true; 9049875cf80SDavid Howells } 9059875cf80SDavid Howells 906dea39376SAl Viro static void follow_mount_rcu(struct nameidata *nd) 907287548e4SAl Viro { 908dea39376SAl Viro while (d_mountpoint(nd->path.dentry)) { 909c7105365SAl Viro struct mount *mounted; 910dea39376SAl Viro mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1); 911287548e4SAl Viro if (!mounted) 912287548e4SAl Viro break; 913c7105365SAl Viro nd->path.mnt = &mounted->mnt; 914c7105365SAl Viro nd->path.dentry = mounted->mnt.mnt_root; 915dea39376SAl Viro nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); 916287548e4SAl Viro } 917287548e4SAl Viro } 918287548e4SAl Viro 91931e6b01fSNick Piggin static int follow_dotdot_rcu(struct nameidata *nd) 92031e6b01fSNick Piggin { 92131e6b01fSNick Piggin set_root_rcu(nd); 92231e6b01fSNick Piggin 92331e6b01fSNick Piggin while (1) { 92431e6b01fSNick Piggin if (nd->path.dentry == nd->root.dentry && 92531e6b01fSNick Piggin nd->path.mnt == nd->root.mnt) { 92631e6b01fSNick Piggin break; 92731e6b01fSNick Piggin } 92831e6b01fSNick Piggin if (nd->path.dentry != nd->path.mnt->mnt_root) { 92931e6b01fSNick Piggin struct dentry *old = nd->path.dentry; 93031e6b01fSNick Piggin struct dentry *parent = old->d_parent; 93131e6b01fSNick Piggin unsigned seq; 93231e6b01fSNick Piggin 93331e6b01fSNick Piggin seq = read_seqcount_begin(&parent->d_seq); 93431e6b01fSNick Piggin if (read_seqcount_retry(&old->d_seq, nd->seq)) 935ef7562d5SAl Viro goto failed; 93631e6b01fSNick Piggin nd->path.dentry = parent; 93731e6b01fSNick Piggin nd->seq = seq; 93831e6b01fSNick Piggin break; 93931e6b01fSNick Piggin } 94031e6b01fSNick Piggin if (!follow_up_rcu(&nd->path)) 94131e6b01fSNick Piggin break; 94231e6b01fSNick Piggin nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); 94331e6b01fSNick Piggin } 944dea39376SAl Viro follow_mount_rcu(nd); 945dea39376SAl Viro nd->inode = nd->path.dentry->d_inode; 94631e6b01fSNick Piggin return 0; 947ef7562d5SAl Viro 948ef7562d5SAl Viro failed: 949ef7562d5SAl Viro nd->flags &= ~LOOKUP_RCU; 9505b6ca027SAl Viro if (!(nd->flags & LOOKUP_ROOT)) 951ef7562d5SAl Viro nd->root.mnt = NULL; 952ef7562d5SAl Viro rcu_read_unlock(); 953ef7562d5SAl Viro br_read_unlock(vfsmount_lock); 954ef7562d5SAl Viro return -ECHILD; 95531e6b01fSNick Piggin } 95631e6b01fSNick Piggin 9579875cf80SDavid Howells /* 958cc53ce53SDavid Howells * Follow down to the covering mount currently visible to userspace. At each 959cc53ce53SDavid Howells * point, the filesystem owning that dentry may be queried as to whether the 960cc53ce53SDavid Howells * caller is permitted to proceed or not. 961cc53ce53SDavid Howells */ 9627cc90cc3SAl Viro int follow_down(struct path *path) 963cc53ce53SDavid Howells { 964cc53ce53SDavid Howells unsigned managed; 965cc53ce53SDavid Howells int ret; 966cc53ce53SDavid Howells 967cc53ce53SDavid Howells while (managed = ACCESS_ONCE(path->dentry->d_flags), 968cc53ce53SDavid Howells unlikely(managed & DCACHE_MANAGED_DENTRY)) { 969cc53ce53SDavid Howells /* Allow the filesystem to manage the transit without i_mutex 970cc53ce53SDavid Howells * being held. 971cc53ce53SDavid Howells * 972cc53ce53SDavid Howells * We indicate to the filesystem if someone is trying to mount 973cc53ce53SDavid Howells * something here. This gives autofs the chance to deny anyone 974cc53ce53SDavid Howells * other than its daemon the right to mount on its 975cc53ce53SDavid Howells * superstructure. 976cc53ce53SDavid Howells * 977cc53ce53SDavid Howells * The filesystem may sleep at this point. 978cc53ce53SDavid Howells */ 979cc53ce53SDavid Howells if (managed & DCACHE_MANAGE_TRANSIT) { 980cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op); 981cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op->d_manage); 982ab90911fSDavid Howells ret = path->dentry->d_op->d_manage( 9831aed3e42SAl Viro path->dentry, false); 984cc53ce53SDavid Howells if (ret < 0) 985cc53ce53SDavid Howells return ret == -EISDIR ? 0 : ret; 986cc53ce53SDavid Howells } 987cc53ce53SDavid Howells 988cc53ce53SDavid Howells /* Transit to a mounted filesystem. */ 989cc53ce53SDavid Howells if (managed & DCACHE_MOUNTED) { 990cc53ce53SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 991cc53ce53SDavid Howells if (!mounted) 992cc53ce53SDavid Howells break; 993cc53ce53SDavid Howells dput(path->dentry); 994cc53ce53SDavid Howells mntput(path->mnt); 995cc53ce53SDavid Howells path->mnt = mounted; 996cc53ce53SDavid Howells path->dentry = dget(mounted->mnt_root); 997cc53ce53SDavid Howells continue; 998cc53ce53SDavid Howells } 999cc53ce53SDavid Howells 1000cc53ce53SDavid Howells /* Don't handle automount points here */ 1001cc53ce53SDavid Howells break; 1002cc53ce53SDavid Howells } 1003cc53ce53SDavid Howells return 0; 1004cc53ce53SDavid Howells } 1005cc53ce53SDavid Howells 1006cc53ce53SDavid Howells /* 10079875cf80SDavid Howells * Skip to top of mountpoint pile in refwalk mode for follow_dotdot() 10089875cf80SDavid Howells */ 10099875cf80SDavid Howells static void follow_mount(struct path *path) 10109875cf80SDavid Howells { 10119875cf80SDavid Howells while (d_mountpoint(path->dentry)) { 10129875cf80SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 10139875cf80SDavid Howells if (!mounted) 10149875cf80SDavid Howells break; 10159875cf80SDavid Howells dput(path->dentry); 10169875cf80SDavid Howells mntput(path->mnt); 10179875cf80SDavid Howells path->mnt = mounted; 10189875cf80SDavid Howells path->dentry = dget(mounted->mnt_root); 10199875cf80SDavid Howells } 10209875cf80SDavid Howells } 10219875cf80SDavid Howells 102231e6b01fSNick Piggin static void follow_dotdot(struct nameidata *nd) 10231da177e4SLinus Torvalds { 10242a737871SAl Viro set_root(nd); 1025e518ddb7SAndreas Mohr 10261da177e4SLinus Torvalds while(1) { 10274ac91378SJan Blunck struct dentry *old = nd->path.dentry; 10281da177e4SLinus Torvalds 10292a737871SAl Viro if (nd->path.dentry == nd->root.dentry && 10302a737871SAl Viro nd->path.mnt == nd->root.mnt) { 10311da177e4SLinus Torvalds break; 10321da177e4SLinus Torvalds } 10334ac91378SJan Blunck if (nd->path.dentry != nd->path.mnt->mnt_root) { 10343088dd70SAl Viro /* rare case of legitimate dget_parent()... */ 10353088dd70SAl Viro nd->path.dentry = dget_parent(nd->path.dentry); 10361da177e4SLinus Torvalds dput(old); 10371da177e4SLinus Torvalds break; 10381da177e4SLinus Torvalds } 10393088dd70SAl Viro if (!follow_up(&nd->path)) 10401da177e4SLinus Torvalds break; 10411da177e4SLinus Torvalds } 104279ed0226SAl Viro follow_mount(&nd->path); 104331e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 10441da177e4SLinus Torvalds } 10451da177e4SLinus Torvalds 10461da177e4SLinus Torvalds /* 1047bad61189SMiklos Szeredi * This looks up the name in dcache, possibly revalidates the old dentry and 1048bad61189SMiklos Szeredi * allocates a new one if not found or not valid. In the need_lookup argument 1049bad61189SMiklos Szeredi * returns whether i_op->lookup is necessary. 1050bad61189SMiklos Szeredi * 1051bad61189SMiklos Szeredi * dir->d_inode->i_mutex must be held 1052baa03890SNick Piggin */ 1053bad61189SMiklos Szeredi static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir, 1054bad61189SMiklos Szeredi struct nameidata *nd, bool *need_lookup) 1055baa03890SNick Piggin { 1056baa03890SNick Piggin struct dentry *dentry; 1057bad61189SMiklos Szeredi int error; 1058baa03890SNick Piggin 1059bad61189SMiklos Szeredi *need_lookup = false; 1060bad61189SMiklos Szeredi dentry = d_lookup(dir, name); 1061bad61189SMiklos Szeredi if (dentry) { 1062bad61189SMiklos Szeredi if (d_need_lookup(dentry)) { 1063bad61189SMiklos Szeredi *need_lookup = true; 1064bad61189SMiklos Szeredi } else if (dentry->d_flags & DCACHE_OP_REVALIDATE) { 1065bad61189SMiklos Szeredi error = d_revalidate(dentry, nd); 1066bad61189SMiklos Szeredi if (unlikely(error <= 0)) { 1067bad61189SMiklos Szeredi if (error < 0) { 1068bad61189SMiklos Szeredi dput(dentry); 1069bad61189SMiklos Szeredi return ERR_PTR(error); 1070bad61189SMiklos Szeredi } else if (!d_invalidate(dentry)) { 1071bad61189SMiklos Szeredi dput(dentry); 1072bad61189SMiklos Szeredi dentry = NULL; 1073bad61189SMiklos Szeredi } 1074bad61189SMiklos Szeredi } 1075bad61189SMiklos Szeredi } 1076bad61189SMiklos Szeredi } 1077baa03890SNick Piggin 1078bad61189SMiklos Szeredi if (!dentry) { 1079bad61189SMiklos Szeredi dentry = d_alloc(dir, name); 1080baa03890SNick Piggin if (unlikely(!dentry)) 1081baa03890SNick Piggin return ERR_PTR(-ENOMEM); 1082baa03890SNick Piggin 1083bad61189SMiklos Szeredi *need_lookup = true; 1084baa03890SNick Piggin } 1085baa03890SNick Piggin return dentry; 1086baa03890SNick Piggin } 1087baa03890SNick Piggin 1088baa03890SNick Piggin /* 1089bad61189SMiklos Szeredi * Call i_op->lookup on the dentry. The dentry must be negative but may be 1090bad61189SMiklos Szeredi * hashed if it was pouplated with DCACHE_NEED_LOOKUP. 1091bad61189SMiklos Szeredi * 1092bad61189SMiklos Szeredi * dir->d_inode->i_mutex must be held 109344396f4bSJosef Bacik */ 1094bad61189SMiklos Szeredi static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry, 109544396f4bSJosef Bacik struct nameidata *nd) 109644396f4bSJosef Bacik { 109744396f4bSJosef Bacik struct dentry *old; 109844396f4bSJosef Bacik 109944396f4bSJosef Bacik /* Don't create child dentry for a dead directory. */ 1100bad61189SMiklos Szeredi if (unlikely(IS_DEADDIR(dir))) { 1101e188dc02SMiklos Szeredi dput(dentry); 110244396f4bSJosef Bacik return ERR_PTR(-ENOENT); 1103e188dc02SMiklos Szeredi } 110444396f4bSJosef Bacik 1105bad61189SMiklos Szeredi old = dir->i_op->lookup(dir, dentry, nd); 110644396f4bSJosef Bacik if (unlikely(old)) { 110744396f4bSJosef Bacik dput(dentry); 110844396f4bSJosef Bacik dentry = old; 110944396f4bSJosef Bacik } 111044396f4bSJosef Bacik return dentry; 111144396f4bSJosef Bacik } 111244396f4bSJosef Bacik 1113a3255546SAl Viro static struct dentry *__lookup_hash(struct qstr *name, 1114a3255546SAl Viro struct dentry *base, struct nameidata *nd) 1115a3255546SAl Viro { 1116bad61189SMiklos Szeredi bool need_lookup; 1117a3255546SAl Viro struct dentry *dentry; 1118a3255546SAl Viro 1119bad61189SMiklos Szeredi dentry = lookup_dcache(name, base, nd, &need_lookup); 1120bad61189SMiklos Szeredi if (!need_lookup) 1121a3255546SAl Viro return dentry; 1122bad61189SMiklos Szeredi 1123bad61189SMiklos Szeredi return lookup_real(base->d_inode, dentry, nd); 1124a3255546SAl Viro } 1125a3255546SAl Viro 112644396f4bSJosef Bacik /* 11271da177e4SLinus Torvalds * It's more convoluted than I'd like it to be, but... it's still fairly 11281da177e4SLinus Torvalds * small and for now I'd prefer to have fast path as straight as possible. 11291da177e4SLinus Torvalds * It _is_ time-critical. 11301da177e4SLinus Torvalds */ 11311da177e4SLinus Torvalds static int do_lookup(struct nameidata *nd, struct qstr *name, 113231e6b01fSNick Piggin struct path *path, struct inode **inode) 11331da177e4SLinus Torvalds { 11344ac91378SJan Blunck struct vfsmount *mnt = nd->path.mnt; 113531e6b01fSNick Piggin struct dentry *dentry, *parent = nd->path.dentry; 11365a18fff2SAl Viro int need_reval = 1; 11375a18fff2SAl Viro int status = 1; 11389875cf80SDavid Howells int err; 11399875cf80SDavid Howells 11403cac260aSAl Viro /* 1141b04f784eSNick Piggin * Rename seqlock is not required here because in the off chance 1142b04f784eSNick Piggin * of a false negative due to a concurrent rename, we're going to 1143b04f784eSNick Piggin * do the non-racy lookup, below. 1144b04f784eSNick Piggin */ 114531e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) { 114631e6b01fSNick Piggin unsigned seq; 114731e6b01fSNick Piggin *inode = nd->inode; 114831e6b01fSNick Piggin dentry = __d_lookup_rcu(parent, name, &seq, inode); 11495a18fff2SAl Viro if (!dentry) 11505a18fff2SAl Viro goto unlazy; 11515a18fff2SAl Viro 115231e6b01fSNick Piggin /* Memory barrier in read_seqcount_begin of child is enough */ 115331e6b01fSNick Piggin if (__read_seqcount_retry(&parent->d_seq, nd->seq)) 115431e6b01fSNick Piggin return -ECHILD; 115531e6b01fSNick Piggin nd->seq = seq; 11565a18fff2SAl Viro 1157fa4ee159SMiklos Szeredi if (unlikely(d_need_lookup(dentry))) 1158fa4ee159SMiklos Szeredi goto unlazy; 115924643087SAl Viro if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) { 11605a18fff2SAl Viro status = d_revalidate(dentry, nd); 11615a18fff2SAl Viro if (unlikely(status <= 0)) { 11625a18fff2SAl Viro if (status != -ECHILD) 11635a18fff2SAl Viro need_reval = 0; 11645a18fff2SAl Viro goto unlazy; 11655a18fff2SAl Viro } 116624643087SAl Viro } 116731e6b01fSNick Piggin path->mnt = mnt; 116831e6b01fSNick Piggin path->dentry = dentry; 1169d6e9bd25SAl Viro if (unlikely(!__follow_mount_rcu(nd, path, inode))) 1170d6e9bd25SAl Viro goto unlazy; 1171d6e9bd25SAl Viro if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT)) 1172d6e9bd25SAl Viro goto unlazy; 11739875cf80SDavid Howells return 0; 11745a18fff2SAl Viro unlazy: 117519660af7SAl Viro if (unlazy_walk(nd, dentry)) 11765a18fff2SAl Viro return -ECHILD; 11775a18fff2SAl Viro } else { 117831e6b01fSNick Piggin dentry = __d_lookup(parent, name); 117924643087SAl Viro } 11805a18fff2SAl Viro 118181e6f520SAl Viro if (unlikely(!dentry)) 118281e6f520SAl Viro goto need_lookup; 11835a18fff2SAl Viro 118481e6f520SAl Viro if (unlikely(d_need_lookup(dentry))) { 118581e6f520SAl Viro dput(dentry); 118681e6f520SAl Viro goto need_lookup; 11875a18fff2SAl Viro } 118881e6f520SAl Viro 11895a18fff2SAl Viro if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval) 11905a18fff2SAl Viro status = d_revalidate(dentry, nd); 11915a18fff2SAl Viro if (unlikely(status <= 0)) { 11925a18fff2SAl Viro if (status < 0) { 11935a18fff2SAl Viro dput(dentry); 11945a18fff2SAl Viro return status; 11955a18fff2SAl Viro } 11965a18fff2SAl Viro if (!d_invalidate(dentry)) { 11975a18fff2SAl Viro dput(dentry); 119881e6f520SAl Viro goto need_lookup; 11995a18fff2SAl Viro } 12005a18fff2SAl Viro } 12013f6c7c71SAl Viro done: 12021da177e4SLinus Torvalds path->mnt = mnt; 12031da177e4SLinus Torvalds path->dentry = dentry; 12049875cf80SDavid Howells err = follow_managed(path, nd->flags); 120589312214SIan Kent if (unlikely(err < 0)) { 120689312214SIan Kent path_put_conditional(path, nd); 12079875cf80SDavid Howells return err; 120889312214SIan Kent } 1209a3fbbde7SAl Viro if (err) 1210a3fbbde7SAl Viro nd->flags |= LOOKUP_JUMPED; 121131e6b01fSNick Piggin *inode = path->dentry->d_inode; 12121da177e4SLinus Torvalds return 0; 121381e6f520SAl Viro 121481e6f520SAl Viro need_lookup: 121581e6f520SAl Viro BUG_ON(nd->inode != parent->d_inode); 121681e6f520SAl Viro 121781e6f520SAl Viro mutex_lock(&parent->d_inode->i_mutex); 121881e6f520SAl Viro dentry = __lookup_hash(name, parent, nd); 121981e6f520SAl Viro mutex_unlock(&parent->d_inode->i_mutex); 122081e6f520SAl Viro if (IS_ERR(dentry)) 122181e6f520SAl Viro return PTR_ERR(dentry); 122281e6f520SAl Viro goto done; 12231da177e4SLinus Torvalds } 12241da177e4SLinus Torvalds 122552094c8aSAl Viro static inline int may_lookup(struct nameidata *nd) 122652094c8aSAl Viro { 122752094c8aSAl Viro if (nd->flags & LOOKUP_RCU) { 12284ad5abb3SAl Viro int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK); 122952094c8aSAl Viro if (err != -ECHILD) 123052094c8aSAl Viro return err; 123119660af7SAl Viro if (unlazy_walk(nd, NULL)) 123252094c8aSAl Viro return -ECHILD; 123352094c8aSAl Viro } 12344ad5abb3SAl Viro return inode_permission(nd->inode, MAY_EXEC); 123552094c8aSAl Viro } 123652094c8aSAl Viro 12379856fa1bSAl Viro static inline int handle_dots(struct nameidata *nd, int type) 12389856fa1bSAl Viro { 12399856fa1bSAl Viro if (type == LAST_DOTDOT) { 12409856fa1bSAl Viro if (nd->flags & LOOKUP_RCU) { 12419856fa1bSAl Viro if (follow_dotdot_rcu(nd)) 12429856fa1bSAl Viro return -ECHILD; 12439856fa1bSAl Viro } else 12449856fa1bSAl Viro follow_dotdot(nd); 12459856fa1bSAl Viro } 12469856fa1bSAl Viro return 0; 12479856fa1bSAl Viro } 12489856fa1bSAl Viro 1249951361f9SAl Viro static void terminate_walk(struct nameidata *nd) 1250951361f9SAl Viro { 1251951361f9SAl Viro if (!(nd->flags & LOOKUP_RCU)) { 1252951361f9SAl Viro path_put(&nd->path); 1253951361f9SAl Viro } else { 1254951361f9SAl Viro nd->flags &= ~LOOKUP_RCU; 12555b6ca027SAl Viro if (!(nd->flags & LOOKUP_ROOT)) 1256951361f9SAl Viro nd->root.mnt = NULL; 1257951361f9SAl Viro rcu_read_unlock(); 1258951361f9SAl Viro br_read_unlock(vfsmount_lock); 1259951361f9SAl Viro } 1260951361f9SAl Viro } 1261951361f9SAl Viro 12623ddcd056SLinus Torvalds /* 12633ddcd056SLinus Torvalds * Do we need to follow links? We _really_ want to be able 12643ddcd056SLinus Torvalds * to do this check without having to look at inode->i_op, 12653ddcd056SLinus Torvalds * so we keep a cache of "no, this doesn't need follow_link" 12663ddcd056SLinus Torvalds * for the common case. 12673ddcd056SLinus Torvalds */ 12687813b94aSLinus Torvalds static inline int should_follow_link(struct inode *inode, int follow) 12693ddcd056SLinus Torvalds { 12703ddcd056SLinus Torvalds if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) { 12713ddcd056SLinus Torvalds if (likely(inode->i_op->follow_link)) 12723ddcd056SLinus Torvalds return follow; 12733ddcd056SLinus Torvalds 12743ddcd056SLinus Torvalds /* This gets set once for the inode lifetime */ 12753ddcd056SLinus Torvalds spin_lock(&inode->i_lock); 12763ddcd056SLinus Torvalds inode->i_opflags |= IOP_NOFOLLOW; 12773ddcd056SLinus Torvalds spin_unlock(&inode->i_lock); 12783ddcd056SLinus Torvalds } 12793ddcd056SLinus Torvalds return 0; 12803ddcd056SLinus Torvalds } 12813ddcd056SLinus Torvalds 1282ce57dfc1SAl Viro static inline int walk_component(struct nameidata *nd, struct path *path, 1283ce57dfc1SAl Viro struct qstr *name, int type, int follow) 1284ce57dfc1SAl Viro { 1285ce57dfc1SAl Viro struct inode *inode; 1286ce57dfc1SAl Viro int err; 1287ce57dfc1SAl Viro /* 1288ce57dfc1SAl Viro * "." and ".." are special - ".." especially so because it has 1289ce57dfc1SAl Viro * to be able to know about the current root directory and 1290ce57dfc1SAl Viro * parent relationships. 1291ce57dfc1SAl Viro */ 1292ce57dfc1SAl Viro if (unlikely(type != LAST_NORM)) 1293ce57dfc1SAl Viro return handle_dots(nd, type); 1294ce57dfc1SAl Viro err = do_lookup(nd, name, path, &inode); 1295ce57dfc1SAl Viro if (unlikely(err)) { 1296ce57dfc1SAl Viro terminate_walk(nd); 1297ce57dfc1SAl Viro return err; 1298ce57dfc1SAl Viro } 1299ce57dfc1SAl Viro if (!inode) { 1300ce57dfc1SAl Viro path_to_nameidata(path, nd); 1301ce57dfc1SAl Viro terminate_walk(nd); 1302ce57dfc1SAl Viro return -ENOENT; 1303ce57dfc1SAl Viro } 13047813b94aSLinus Torvalds if (should_follow_link(inode, follow)) { 130519660af7SAl Viro if (nd->flags & LOOKUP_RCU) { 130619660af7SAl Viro if (unlikely(unlazy_walk(nd, path->dentry))) { 130719660af7SAl Viro terminate_walk(nd); 1308ce57dfc1SAl Viro return -ECHILD; 130919660af7SAl Viro } 131019660af7SAl Viro } 1311ce57dfc1SAl Viro BUG_ON(inode != path->dentry->d_inode); 1312ce57dfc1SAl Viro return 1; 1313ce57dfc1SAl Viro } 1314ce57dfc1SAl Viro path_to_nameidata(path, nd); 1315ce57dfc1SAl Viro nd->inode = inode; 1316ce57dfc1SAl Viro return 0; 1317ce57dfc1SAl Viro } 1318ce57dfc1SAl Viro 13191da177e4SLinus Torvalds /* 1320b356379aSAl Viro * This limits recursive symlink follows to 8, while 1321b356379aSAl Viro * limiting consecutive symlinks to 40. 1322b356379aSAl Viro * 1323b356379aSAl Viro * Without that kind of total limit, nasty chains of consecutive 1324b356379aSAl Viro * symlinks can cause almost arbitrarily long lookups. 1325b356379aSAl Viro */ 1326b356379aSAl Viro static inline int nested_symlink(struct path *path, struct nameidata *nd) 1327b356379aSAl Viro { 1328b356379aSAl Viro int res; 1329b356379aSAl Viro 1330b356379aSAl Viro if (unlikely(current->link_count >= MAX_NESTED_LINKS)) { 1331b356379aSAl Viro path_put_conditional(path, nd); 1332b356379aSAl Viro path_put(&nd->path); 1333b356379aSAl Viro return -ELOOP; 1334b356379aSAl Viro } 13351a4022f8SErez Zadok BUG_ON(nd->depth >= MAX_NESTED_LINKS); 1336b356379aSAl Viro 1337b356379aSAl Viro nd->depth++; 1338b356379aSAl Viro current->link_count++; 1339b356379aSAl Viro 1340b356379aSAl Viro do { 1341b356379aSAl Viro struct path link = *path; 1342b356379aSAl Viro void *cookie; 1343574197e0SAl Viro 1344574197e0SAl Viro res = follow_link(&link, nd, &cookie); 1345b356379aSAl Viro if (!res) 1346b356379aSAl Viro res = walk_component(nd, path, &nd->last, 1347b356379aSAl Viro nd->last_type, LOOKUP_FOLLOW); 1348574197e0SAl Viro put_link(nd, &link, cookie); 1349b356379aSAl Viro } while (res > 0); 1350b356379aSAl Viro 1351b356379aSAl Viro current->link_count--; 1352b356379aSAl Viro nd->depth--; 1353b356379aSAl Viro return res; 1354b356379aSAl Viro } 1355b356379aSAl Viro 1356b356379aSAl Viro /* 13573ddcd056SLinus Torvalds * We really don't want to look at inode->i_op->lookup 13583ddcd056SLinus Torvalds * when we don't have to. So we keep a cache bit in 13593ddcd056SLinus Torvalds * the inode ->i_opflags field that says "yes, we can 13603ddcd056SLinus Torvalds * do lookup on this inode". 13613ddcd056SLinus Torvalds */ 13623ddcd056SLinus Torvalds static inline int can_lookup(struct inode *inode) 13633ddcd056SLinus Torvalds { 13643ddcd056SLinus Torvalds if (likely(inode->i_opflags & IOP_LOOKUP)) 13653ddcd056SLinus Torvalds return 1; 13663ddcd056SLinus Torvalds if (likely(!inode->i_op->lookup)) 13673ddcd056SLinus Torvalds return 0; 13683ddcd056SLinus Torvalds 13693ddcd056SLinus Torvalds /* We do this once for the lifetime of the inode */ 13703ddcd056SLinus Torvalds spin_lock(&inode->i_lock); 13713ddcd056SLinus Torvalds inode->i_opflags |= IOP_LOOKUP; 13723ddcd056SLinus Torvalds spin_unlock(&inode->i_lock); 13733ddcd056SLinus Torvalds return 1; 13743ddcd056SLinus Torvalds } 13753ddcd056SLinus Torvalds 1376bfcfaa77SLinus Torvalds /* 1377bfcfaa77SLinus Torvalds * We can do the critical dentry name comparison and hashing 1378bfcfaa77SLinus Torvalds * operations one word at a time, but we are limited to: 1379bfcfaa77SLinus Torvalds * 1380bfcfaa77SLinus Torvalds * - Architectures with fast unaligned word accesses. We could 1381bfcfaa77SLinus Torvalds * do a "get_unaligned()" if this helps and is sufficiently 1382bfcfaa77SLinus Torvalds * fast. 1383bfcfaa77SLinus Torvalds * 1384bfcfaa77SLinus Torvalds * - Little-endian machines (so that we can generate the mask 1385bfcfaa77SLinus Torvalds * of low bytes efficiently). Again, we *could* do a byte 1386bfcfaa77SLinus Torvalds * swapping load on big-endian architectures if that is not 1387bfcfaa77SLinus Torvalds * expensive enough to make the optimization worthless. 1388bfcfaa77SLinus Torvalds * 1389bfcfaa77SLinus Torvalds * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we 1390bfcfaa77SLinus Torvalds * do not trap on the (extremely unlikely) case of a page 1391bfcfaa77SLinus Torvalds * crossing operation. 1392bfcfaa77SLinus Torvalds * 1393bfcfaa77SLinus Torvalds * - Furthermore, we need an efficient 64-bit compile for the 1394bfcfaa77SLinus Torvalds * 64-bit case in order to generate the "number of bytes in 1395bfcfaa77SLinus Torvalds * the final mask". Again, that could be replaced with a 1396bfcfaa77SLinus Torvalds * efficient population count instruction or similar. 1397bfcfaa77SLinus Torvalds */ 1398bfcfaa77SLinus Torvalds #ifdef CONFIG_DCACHE_WORD_ACCESS 1399bfcfaa77SLinus Torvalds 1400f68e556eSLinus Torvalds #include <asm/word-at-a-time.h> 1401bfcfaa77SLinus Torvalds 1402f68e556eSLinus Torvalds #ifdef CONFIG_64BIT 1403bfcfaa77SLinus Torvalds 1404bfcfaa77SLinus Torvalds static inline unsigned int fold_hash(unsigned long hash) 1405bfcfaa77SLinus Torvalds { 1406bfcfaa77SLinus Torvalds hash += hash >> (8*sizeof(int)); 1407bfcfaa77SLinus Torvalds return hash; 1408bfcfaa77SLinus Torvalds } 1409bfcfaa77SLinus Torvalds 1410bfcfaa77SLinus Torvalds #else /* 32-bit case */ 1411bfcfaa77SLinus Torvalds 1412bfcfaa77SLinus Torvalds #define fold_hash(x) (x) 1413bfcfaa77SLinus Torvalds 1414bfcfaa77SLinus Torvalds #endif 1415bfcfaa77SLinus Torvalds 1416bfcfaa77SLinus Torvalds unsigned int full_name_hash(const unsigned char *name, unsigned int len) 1417bfcfaa77SLinus Torvalds { 1418bfcfaa77SLinus Torvalds unsigned long a, mask; 1419bfcfaa77SLinus Torvalds unsigned long hash = 0; 1420bfcfaa77SLinus Torvalds 1421bfcfaa77SLinus Torvalds for (;;) { 1422bfcfaa77SLinus Torvalds a = *(unsigned long *)name; 1423bfcfaa77SLinus Torvalds if (len < sizeof(unsigned long)) 1424bfcfaa77SLinus Torvalds break; 1425bfcfaa77SLinus Torvalds hash += a; 1426f132c5beSAl Viro hash *= 9; 1427bfcfaa77SLinus Torvalds name += sizeof(unsigned long); 1428bfcfaa77SLinus Torvalds len -= sizeof(unsigned long); 1429bfcfaa77SLinus Torvalds if (!len) 1430bfcfaa77SLinus Torvalds goto done; 1431bfcfaa77SLinus Torvalds } 1432bfcfaa77SLinus Torvalds mask = ~(~0ul << len*8); 1433bfcfaa77SLinus Torvalds hash += mask & a; 1434bfcfaa77SLinus Torvalds done: 1435bfcfaa77SLinus Torvalds return fold_hash(hash); 1436bfcfaa77SLinus Torvalds } 1437bfcfaa77SLinus Torvalds EXPORT_SYMBOL(full_name_hash); 1438bfcfaa77SLinus Torvalds 1439bfcfaa77SLinus Torvalds /* 1440bfcfaa77SLinus Torvalds * Calculate the length and hash of the path component, and 1441bfcfaa77SLinus Torvalds * return the length of the component; 1442bfcfaa77SLinus Torvalds */ 1443bfcfaa77SLinus Torvalds static inline unsigned long hash_name(const char *name, unsigned int *hashp) 1444bfcfaa77SLinus Torvalds { 1445bfcfaa77SLinus Torvalds unsigned long a, mask, hash, len; 1446bfcfaa77SLinus Torvalds 1447bfcfaa77SLinus Torvalds hash = a = 0; 1448bfcfaa77SLinus Torvalds len = -sizeof(unsigned long); 1449bfcfaa77SLinus Torvalds do { 1450bfcfaa77SLinus Torvalds hash = (hash + a) * 9; 1451bfcfaa77SLinus Torvalds len += sizeof(unsigned long); 1452bfcfaa77SLinus Torvalds a = *(unsigned long *)(name+len); 1453bfcfaa77SLinus Torvalds /* Do we have any NUL or '/' bytes in this word? */ 1454f68e556eSLinus Torvalds mask = has_zero(a) | has_zero(a ^ REPEAT_BYTE('/')); 1455bfcfaa77SLinus Torvalds } while (!mask); 1456bfcfaa77SLinus Torvalds 1457bfcfaa77SLinus Torvalds /* The mask *below* the first high bit set */ 1458bfcfaa77SLinus Torvalds mask = (mask - 1) & ~mask; 1459bfcfaa77SLinus Torvalds mask >>= 7; 1460bfcfaa77SLinus Torvalds hash += a & mask; 1461bfcfaa77SLinus Torvalds *hashp = fold_hash(hash); 1462bfcfaa77SLinus Torvalds 1463bfcfaa77SLinus Torvalds return len + count_masked_bytes(mask); 1464bfcfaa77SLinus Torvalds } 1465bfcfaa77SLinus Torvalds 1466bfcfaa77SLinus Torvalds #else 1467bfcfaa77SLinus Torvalds 14680145acc2SLinus Torvalds unsigned int full_name_hash(const unsigned char *name, unsigned int len) 14690145acc2SLinus Torvalds { 14700145acc2SLinus Torvalds unsigned long hash = init_name_hash(); 14710145acc2SLinus Torvalds while (len--) 14720145acc2SLinus Torvalds hash = partial_name_hash(*name++, hash); 14730145acc2SLinus Torvalds return end_name_hash(hash); 14740145acc2SLinus Torvalds } 1475ae942ae7SLinus Torvalds EXPORT_SYMBOL(full_name_hash); 14760145acc2SLinus Torvalds 14773ddcd056SLinus Torvalds /* 1478200e9ef7SLinus Torvalds * We know there's a real path component here of at least 1479200e9ef7SLinus Torvalds * one character. 1480200e9ef7SLinus Torvalds */ 1481200e9ef7SLinus Torvalds static inline unsigned long hash_name(const char *name, unsigned int *hashp) 1482200e9ef7SLinus Torvalds { 1483200e9ef7SLinus Torvalds unsigned long hash = init_name_hash(); 1484200e9ef7SLinus Torvalds unsigned long len = 0, c; 1485200e9ef7SLinus Torvalds 1486200e9ef7SLinus Torvalds c = (unsigned char)*name; 1487200e9ef7SLinus Torvalds do { 1488200e9ef7SLinus Torvalds len++; 1489200e9ef7SLinus Torvalds hash = partial_name_hash(c, hash); 1490200e9ef7SLinus Torvalds c = (unsigned char)name[len]; 1491200e9ef7SLinus Torvalds } while (c && c != '/'); 1492200e9ef7SLinus Torvalds *hashp = end_name_hash(hash); 1493200e9ef7SLinus Torvalds return len; 1494200e9ef7SLinus Torvalds } 1495200e9ef7SLinus Torvalds 1496bfcfaa77SLinus Torvalds #endif 1497bfcfaa77SLinus Torvalds 1498200e9ef7SLinus Torvalds /* 14991da177e4SLinus Torvalds * Name resolution. 1500ea3834d9SPrasanna Meda * This is the basic name resolution function, turning a pathname into 1501ea3834d9SPrasanna Meda * the final dentry. We expect 'base' to be positive and a directory. 15021da177e4SLinus Torvalds * 1503ea3834d9SPrasanna Meda * Returns 0 and nd will have valid dentry and mnt on success. 1504ea3834d9SPrasanna Meda * Returns error and drops reference to input namei data on failure. 15051da177e4SLinus Torvalds */ 15066de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd) 15071da177e4SLinus Torvalds { 15081da177e4SLinus Torvalds struct path next; 15091da177e4SLinus Torvalds int err; 15101da177e4SLinus Torvalds 15111da177e4SLinus Torvalds while (*name=='/') 15121da177e4SLinus Torvalds name++; 15131da177e4SLinus Torvalds if (!*name) 1514086e183aSAl Viro return 0; 15151da177e4SLinus Torvalds 15161da177e4SLinus Torvalds /* At this point we know we have a real path component. */ 15171da177e4SLinus Torvalds for(;;) { 15181da177e4SLinus Torvalds struct qstr this; 1519200e9ef7SLinus Torvalds long len; 1520fe479a58SAl Viro int type; 15211da177e4SLinus Torvalds 152252094c8aSAl Viro err = may_lookup(nd); 15231da177e4SLinus Torvalds if (err) 15241da177e4SLinus Torvalds break; 15251da177e4SLinus Torvalds 1526200e9ef7SLinus Torvalds len = hash_name(name, &this.hash); 15271da177e4SLinus Torvalds this.name = name; 1528200e9ef7SLinus Torvalds this.len = len; 15291da177e4SLinus Torvalds 1530fe479a58SAl Viro type = LAST_NORM; 1531200e9ef7SLinus Torvalds if (name[0] == '.') switch (len) { 1532fe479a58SAl Viro case 2: 1533200e9ef7SLinus Torvalds if (name[1] == '.') { 1534fe479a58SAl Viro type = LAST_DOTDOT; 153516c2cd71SAl Viro nd->flags |= LOOKUP_JUMPED; 153616c2cd71SAl Viro } 1537fe479a58SAl Viro break; 1538fe479a58SAl Viro case 1: 1539fe479a58SAl Viro type = LAST_DOT; 1540fe479a58SAl Viro } 15415a202bcdSAl Viro if (likely(type == LAST_NORM)) { 15425a202bcdSAl Viro struct dentry *parent = nd->path.dentry; 154316c2cd71SAl Viro nd->flags &= ~LOOKUP_JUMPED; 15445a202bcdSAl Viro if (unlikely(parent->d_flags & DCACHE_OP_HASH)) { 15455a202bcdSAl Viro err = parent->d_op->d_hash(parent, nd->inode, 15465a202bcdSAl Viro &this); 15475a202bcdSAl Viro if (err < 0) 15485a202bcdSAl Viro break; 15495a202bcdSAl Viro } 15505a202bcdSAl Viro } 1551fe479a58SAl Viro 1552200e9ef7SLinus Torvalds if (!name[len]) 15531da177e4SLinus Torvalds goto last_component; 1554200e9ef7SLinus Torvalds /* 1555200e9ef7SLinus Torvalds * If it wasn't NUL, we know it was '/'. Skip that 1556200e9ef7SLinus Torvalds * slash, and continue until no more slashes. 1557200e9ef7SLinus Torvalds */ 1558200e9ef7SLinus Torvalds do { 1559200e9ef7SLinus Torvalds len++; 1560200e9ef7SLinus Torvalds } while (unlikely(name[len] == '/')); 1561200e9ef7SLinus Torvalds if (!name[len]) 1562b356379aSAl Viro goto last_component; 1563200e9ef7SLinus Torvalds name += len; 15641da177e4SLinus Torvalds 1565ce57dfc1SAl Viro err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW); 1566ce57dfc1SAl Viro if (err < 0) 1567ce57dfc1SAl Viro return err; 1568fe479a58SAl Viro 1569ce57dfc1SAl Viro if (err) { 1570b356379aSAl Viro err = nested_symlink(&next, nd); 15711da177e4SLinus Torvalds if (err) 1572a7472babSAl Viro return err; 157331e6b01fSNick Piggin } 15743ddcd056SLinus Torvalds if (can_lookup(nd->inode)) 15751da177e4SLinus Torvalds continue; 15763ddcd056SLinus Torvalds err = -ENOTDIR; 15773ddcd056SLinus Torvalds break; 15781da177e4SLinus Torvalds /* here ends the main loop */ 15791da177e4SLinus Torvalds 15801da177e4SLinus Torvalds last_component: 1581ce57dfc1SAl Viro nd->last = this; 1582ce57dfc1SAl Viro nd->last_type = type; 1583ce57dfc1SAl Viro return 0; 1584ce57dfc1SAl Viro } 1585951361f9SAl Viro terminate_walk(nd); 15861da177e4SLinus Torvalds return err; 15871da177e4SLinus Torvalds } 15881da177e4SLinus Torvalds 158970e9b357SAl Viro static int path_init(int dfd, const char *name, unsigned int flags, 159070e9b357SAl Viro struct nameidata *nd, struct file **fp) 159131e6b01fSNick Piggin { 159231e6b01fSNick Piggin int retval = 0; 159331e6b01fSNick Piggin int fput_needed; 159431e6b01fSNick Piggin struct file *file; 159531e6b01fSNick Piggin 159631e6b01fSNick Piggin nd->last_type = LAST_ROOT; /* if there are only slashes... */ 159716c2cd71SAl Viro nd->flags = flags | LOOKUP_JUMPED; 159831e6b01fSNick Piggin nd->depth = 0; 15995b6ca027SAl Viro if (flags & LOOKUP_ROOT) { 16005b6ca027SAl Viro struct inode *inode = nd->root.dentry->d_inode; 160173d049a4SAl Viro if (*name) { 16025b6ca027SAl Viro if (!inode->i_op->lookup) 16035b6ca027SAl Viro return -ENOTDIR; 16045b6ca027SAl Viro retval = inode_permission(inode, MAY_EXEC); 16055b6ca027SAl Viro if (retval) 16065b6ca027SAl Viro return retval; 160773d049a4SAl Viro } 16085b6ca027SAl Viro nd->path = nd->root; 16095b6ca027SAl Viro nd->inode = inode; 16105b6ca027SAl Viro if (flags & LOOKUP_RCU) { 16115b6ca027SAl Viro br_read_lock(vfsmount_lock); 16125b6ca027SAl Viro rcu_read_lock(); 16135b6ca027SAl Viro nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 16145b6ca027SAl Viro } else { 16155b6ca027SAl Viro path_get(&nd->path); 16165b6ca027SAl Viro } 16175b6ca027SAl Viro return 0; 16185b6ca027SAl Viro } 16195b6ca027SAl Viro 162031e6b01fSNick Piggin nd->root.mnt = NULL; 162131e6b01fSNick Piggin 162231e6b01fSNick Piggin if (*name=='/') { 1623e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 162431e6b01fSNick Piggin br_read_lock(vfsmount_lock); 162531e6b01fSNick Piggin rcu_read_lock(); 1626e41f7d4eSAl Viro set_root_rcu(nd); 1627e41f7d4eSAl Viro } else { 1628e41f7d4eSAl Viro set_root(nd); 1629e41f7d4eSAl Viro path_get(&nd->root); 1630e41f7d4eSAl Viro } 163131e6b01fSNick Piggin nd->path = nd->root; 163231e6b01fSNick Piggin } else if (dfd == AT_FDCWD) { 1633e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 163431e6b01fSNick Piggin struct fs_struct *fs = current->fs; 1635c28cc364SNick Piggin unsigned seq; 163631e6b01fSNick Piggin 163731e6b01fSNick Piggin br_read_lock(vfsmount_lock); 163831e6b01fSNick Piggin rcu_read_lock(); 163931e6b01fSNick Piggin 1640c28cc364SNick Piggin do { 1641c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 164231e6b01fSNick Piggin nd->path = fs->pwd; 1643c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 1644c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 1645e41f7d4eSAl Viro } else { 1646e41f7d4eSAl Viro get_fs_pwd(current->fs, &nd->path); 1647e41f7d4eSAl Viro } 164831e6b01fSNick Piggin } else { 164931e6b01fSNick Piggin struct dentry *dentry; 165031e6b01fSNick Piggin 16511abf0c71SAl Viro file = fget_raw_light(dfd, &fput_needed); 165231e6b01fSNick Piggin retval = -EBADF; 165331e6b01fSNick Piggin if (!file) 165431e6b01fSNick Piggin goto out_fail; 165531e6b01fSNick Piggin 165631e6b01fSNick Piggin dentry = file->f_path.dentry; 165731e6b01fSNick Piggin 1658f52e0c11SAl Viro if (*name) { 165931e6b01fSNick Piggin retval = -ENOTDIR; 166031e6b01fSNick Piggin if (!S_ISDIR(dentry->d_inode->i_mode)) 166131e6b01fSNick Piggin goto fput_fail; 166231e6b01fSNick Piggin 16634ad5abb3SAl Viro retval = inode_permission(dentry->d_inode, MAY_EXEC); 166431e6b01fSNick Piggin if (retval) 166531e6b01fSNick Piggin goto fput_fail; 1666f52e0c11SAl Viro } 166731e6b01fSNick Piggin 166831e6b01fSNick Piggin nd->path = file->f_path; 1669e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 167031e6b01fSNick Piggin if (fput_needed) 167170e9b357SAl Viro *fp = file; 1672c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 167331e6b01fSNick Piggin br_read_lock(vfsmount_lock); 167431e6b01fSNick Piggin rcu_read_lock(); 16755590ff0dSUlrich Drepper } else { 16765dd784d0SJan Blunck path_get(&file->f_path); 16775590ff0dSUlrich Drepper fput_light(file, fput_needed); 16781da177e4SLinus Torvalds } 1679e41f7d4eSAl Viro } 1680e41f7d4eSAl Viro 168131e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 16829b4a9b14SAl Viro return 0; 16832dfdd266SJosef 'Jeff' Sipek 16849b4a9b14SAl Viro fput_fail: 16859b4a9b14SAl Viro fput_light(file, fput_needed); 16869b4a9b14SAl Viro out_fail: 16879b4a9b14SAl Viro return retval; 16889b4a9b14SAl Viro } 16899b4a9b14SAl Viro 1690bd92d7feSAl Viro static inline int lookup_last(struct nameidata *nd, struct path *path) 1691bd92d7feSAl Viro { 1692bd92d7feSAl Viro if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len]) 1693bd92d7feSAl Viro nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 1694bd92d7feSAl Viro 1695bd92d7feSAl Viro nd->flags &= ~LOOKUP_PARENT; 1696bd92d7feSAl Viro return walk_component(nd, path, &nd->last, nd->last_type, 1697bd92d7feSAl Viro nd->flags & LOOKUP_FOLLOW); 1698bd92d7feSAl Viro } 1699bd92d7feSAl Viro 17009b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */ 1701ee0827cdSAl Viro static int path_lookupat(int dfd, const char *name, 17029b4a9b14SAl Viro unsigned int flags, struct nameidata *nd) 17039b4a9b14SAl Viro { 170470e9b357SAl Viro struct file *base = NULL; 1705bd92d7feSAl Viro struct path path; 1706bd92d7feSAl Viro int err; 170731e6b01fSNick Piggin 170831e6b01fSNick Piggin /* 170931e6b01fSNick Piggin * Path walking is largely split up into 2 different synchronisation 171031e6b01fSNick Piggin * schemes, rcu-walk and ref-walk (explained in 171131e6b01fSNick Piggin * Documentation/filesystems/path-lookup.txt). These share much of the 171231e6b01fSNick Piggin * path walk code, but some things particularly setup, cleanup, and 171331e6b01fSNick Piggin * following mounts are sufficiently divergent that functions are 171431e6b01fSNick Piggin * duplicated. Typically there is a function foo(), and its RCU 171531e6b01fSNick Piggin * analogue, foo_rcu(). 171631e6b01fSNick Piggin * 171731e6b01fSNick Piggin * -ECHILD is the error number of choice (just to avoid clashes) that 171831e6b01fSNick Piggin * is returned if some aspect of an rcu-walk fails. Such an error must 171931e6b01fSNick Piggin * be handled by restarting a traditional ref-walk (which will always 172031e6b01fSNick Piggin * be able to complete). 172131e6b01fSNick Piggin */ 1722bd92d7feSAl Viro err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base); 1723ee0827cdSAl Viro 1724bd92d7feSAl Viro if (unlikely(err)) 1725bd92d7feSAl Viro return err; 1726ee0827cdSAl Viro 1727ee0827cdSAl Viro current->total_link_count = 0; 1728bd92d7feSAl Viro err = link_path_walk(name, nd); 1729bd92d7feSAl Viro 1730bd92d7feSAl Viro if (!err && !(flags & LOOKUP_PARENT)) { 1731bd92d7feSAl Viro err = lookup_last(nd, &path); 1732bd92d7feSAl Viro while (err > 0) { 1733bd92d7feSAl Viro void *cookie; 1734bd92d7feSAl Viro struct path link = path; 1735bd92d7feSAl Viro nd->flags |= LOOKUP_PARENT; 1736574197e0SAl Viro err = follow_link(&link, nd, &cookie); 1737bd92d7feSAl Viro if (!err) 1738bd92d7feSAl Viro err = lookup_last(nd, &path); 1739574197e0SAl Viro put_link(nd, &link, cookie); 1740bd92d7feSAl Viro } 1741bd92d7feSAl Viro } 1742ee0827cdSAl Viro 17439f1fafeeSAl Viro if (!err) 17449f1fafeeSAl Viro err = complete_walk(nd); 1745bd92d7feSAl Viro 1746bd92d7feSAl Viro if (!err && nd->flags & LOOKUP_DIRECTORY) { 1747bd92d7feSAl Viro if (!nd->inode->i_op->lookup) { 1748bd92d7feSAl Viro path_put(&nd->path); 1749bd23a539SAl Viro err = -ENOTDIR; 1750bd92d7feSAl Viro } 1751bd92d7feSAl Viro } 175216c2cd71SAl Viro 175370e9b357SAl Viro if (base) 175470e9b357SAl Viro fput(base); 1755ee0827cdSAl Viro 17565b6ca027SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) { 175731e6b01fSNick Piggin path_put(&nd->root); 175831e6b01fSNick Piggin nd->root.mnt = NULL; 175931e6b01fSNick Piggin } 1760bd92d7feSAl Viro return err; 176131e6b01fSNick Piggin } 176231e6b01fSNick Piggin 1763ee0827cdSAl Viro static int do_path_lookup(int dfd, const char *name, 1764ee0827cdSAl Viro unsigned int flags, struct nameidata *nd) 1765ee0827cdSAl Viro { 1766ee0827cdSAl Viro int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd); 1767ee0827cdSAl Viro if (unlikely(retval == -ECHILD)) 1768ee0827cdSAl Viro retval = path_lookupat(dfd, name, flags, nd); 1769ee0827cdSAl Viro if (unlikely(retval == -ESTALE)) 1770ee0827cdSAl Viro retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd); 1771ee0827cdSAl Viro 177231e6b01fSNick Piggin if (likely(!retval)) { 177331e6b01fSNick Piggin if (unlikely(!audit_dummy_context())) { 177431e6b01fSNick Piggin if (nd->path.dentry && nd->inode) 177531e6b01fSNick Piggin audit_inode(name, nd->path.dentry); 177631e6b01fSNick Piggin } 177731e6b01fSNick Piggin } 1778170aa3d0SUlrich Drepper return retval; 17791da177e4SLinus Torvalds } 17801da177e4SLinus Torvalds 1781c9c6cac0SAl Viro int kern_path_parent(const char *name, struct nameidata *nd) 17825590ff0dSUlrich Drepper { 1783c9c6cac0SAl Viro return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd); 17845590ff0dSUlrich Drepper } 17855590ff0dSUlrich Drepper 1786d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path) 1787d1811465SAl Viro { 1788d1811465SAl Viro struct nameidata nd; 1789d1811465SAl Viro int res = do_path_lookup(AT_FDCWD, name, flags, &nd); 1790d1811465SAl Viro if (!res) 1791d1811465SAl Viro *path = nd.path; 1792d1811465SAl Viro return res; 1793d1811465SAl Viro } 1794d1811465SAl Viro 179516f18200SJosef 'Jeff' Sipek /** 179616f18200SJosef 'Jeff' Sipek * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair 179716f18200SJosef 'Jeff' Sipek * @dentry: pointer to dentry of the base directory 179816f18200SJosef 'Jeff' Sipek * @mnt: pointer to vfs mount of the base directory 179916f18200SJosef 'Jeff' Sipek * @name: pointer to file name 180016f18200SJosef 'Jeff' Sipek * @flags: lookup flags 1801e0a01249SAl Viro * @path: pointer to struct path to fill 180216f18200SJosef 'Jeff' Sipek */ 180316f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt, 180416f18200SJosef 'Jeff' Sipek const char *name, unsigned int flags, 1805e0a01249SAl Viro struct path *path) 180616f18200SJosef 'Jeff' Sipek { 1807e0a01249SAl Viro struct nameidata nd; 1808e0a01249SAl Viro int err; 1809e0a01249SAl Viro nd.root.dentry = dentry; 1810e0a01249SAl Viro nd.root.mnt = mnt; 1811e0a01249SAl Viro BUG_ON(flags & LOOKUP_PARENT); 18125b6ca027SAl Viro /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */ 1813e0a01249SAl Viro err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd); 1814e0a01249SAl Viro if (!err) 1815e0a01249SAl Viro *path = nd.path; 1816e0a01249SAl Viro return err; 181716f18200SJosef 'Jeff' Sipek } 181816f18200SJosef 'Jeff' Sipek 1819057f6c01SJames Morris /* 1820057f6c01SJames Morris * Restricted form of lookup. Doesn't follow links, single-component only, 1821057f6c01SJames Morris * needs parent already locked. Doesn't follow mounts. 1822057f6c01SJames Morris * SMP-safe. 1823057f6c01SJames Morris */ 1824a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd) 18251da177e4SLinus Torvalds { 18264ac91378SJan Blunck return __lookup_hash(&nd->last, nd->path.dentry, nd); 18271da177e4SLinus Torvalds } 18281da177e4SLinus Torvalds 1829eead1911SChristoph Hellwig /** 1830a6b91919SRandy Dunlap * lookup_one_len - filesystem helper to lookup single pathname component 1831eead1911SChristoph Hellwig * @name: pathname component to lookup 1832eead1911SChristoph Hellwig * @base: base directory to lookup from 1833eead1911SChristoph Hellwig * @len: maximum length @len should be interpreted to 1834eead1911SChristoph Hellwig * 1835a6b91919SRandy Dunlap * Note that this routine is purely a helper for filesystem usage and should 1836a6b91919SRandy Dunlap * not be called by generic code. Also note that by using this function the 1837eead1911SChristoph Hellwig * nameidata argument is passed to the filesystem methods and a filesystem 1838eead1911SChristoph Hellwig * using this helper needs to be prepared for that. 1839eead1911SChristoph Hellwig */ 1840057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len) 1841057f6c01SJames Morris { 1842057f6c01SJames Morris struct qstr this; 18436a96ba54SAl Viro unsigned int c; 1844cda309deSMiklos Szeredi int err; 1845057f6c01SJames Morris 18462f9092e1SDavid Woodhouse WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex)); 18472f9092e1SDavid Woodhouse 18486a96ba54SAl Viro this.name = name; 18496a96ba54SAl Viro this.len = len; 18500145acc2SLinus Torvalds this.hash = full_name_hash(name, len); 18516a96ba54SAl Viro if (!len) 18526a96ba54SAl Viro return ERR_PTR(-EACCES); 18536a96ba54SAl Viro 18546a96ba54SAl Viro while (len--) { 18556a96ba54SAl Viro c = *(const unsigned char *)name++; 18566a96ba54SAl Viro if (c == '/' || c == '\0') 18576a96ba54SAl Viro return ERR_PTR(-EACCES); 18586a96ba54SAl Viro } 18595a202bcdSAl Viro /* 18605a202bcdSAl Viro * See if the low-level filesystem might want 18615a202bcdSAl Viro * to use its own hash.. 18625a202bcdSAl Viro */ 18635a202bcdSAl Viro if (base->d_flags & DCACHE_OP_HASH) { 18645a202bcdSAl Viro int err = base->d_op->d_hash(base, base->d_inode, &this); 18655a202bcdSAl Viro if (err < 0) 18665a202bcdSAl Viro return ERR_PTR(err); 18675a202bcdSAl Viro } 1868eead1911SChristoph Hellwig 1869cda309deSMiklos Szeredi err = inode_permission(base->d_inode, MAY_EXEC); 1870cda309deSMiklos Szeredi if (err) 1871cda309deSMiklos Szeredi return ERR_PTR(err); 1872cda309deSMiklos Szeredi 187349705b77SChristoph Hellwig return __lookup_hash(&this, base, NULL); 1874057f6c01SJames Morris } 1875057f6c01SJames Morris 18761fa1e7f6SAndy Whitcroft int user_path_at_empty(int dfd, const char __user *name, unsigned flags, 18771fa1e7f6SAndy Whitcroft struct path *path, int *empty) 18781da177e4SLinus Torvalds { 18792d8f3038SAl Viro struct nameidata nd; 18801fa1e7f6SAndy Whitcroft char *tmp = getname_flags(name, flags, empty); 18811da177e4SLinus Torvalds int err = PTR_ERR(tmp); 18821da177e4SLinus Torvalds if (!IS_ERR(tmp)) { 18832d8f3038SAl Viro 18842d8f3038SAl Viro BUG_ON(flags & LOOKUP_PARENT); 18852d8f3038SAl Viro 18862d8f3038SAl Viro err = do_path_lookup(dfd, tmp, flags, &nd); 18871da177e4SLinus Torvalds putname(tmp); 18882d8f3038SAl Viro if (!err) 18892d8f3038SAl Viro *path = nd.path; 18901da177e4SLinus Torvalds } 18911da177e4SLinus Torvalds return err; 18921da177e4SLinus Torvalds } 18931da177e4SLinus Torvalds 18941fa1e7f6SAndy Whitcroft int user_path_at(int dfd, const char __user *name, unsigned flags, 18951fa1e7f6SAndy Whitcroft struct path *path) 18961fa1e7f6SAndy Whitcroft { 1897f7493e5dSLinus Torvalds return user_path_at_empty(dfd, name, flags, path, NULL); 18981fa1e7f6SAndy Whitcroft } 18991fa1e7f6SAndy Whitcroft 19002ad94ae6SAl Viro static int user_path_parent(int dfd, const char __user *path, 19012ad94ae6SAl Viro struct nameidata *nd, char **name) 19022ad94ae6SAl Viro { 19032ad94ae6SAl Viro char *s = getname(path); 19042ad94ae6SAl Viro int error; 19052ad94ae6SAl Viro 19062ad94ae6SAl Viro if (IS_ERR(s)) 19072ad94ae6SAl Viro return PTR_ERR(s); 19082ad94ae6SAl Viro 19092ad94ae6SAl Viro error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd); 19102ad94ae6SAl Viro if (error) 19112ad94ae6SAl Viro putname(s); 19122ad94ae6SAl Viro else 19132ad94ae6SAl Viro *name = s; 19142ad94ae6SAl Viro 19152ad94ae6SAl Viro return error; 19162ad94ae6SAl Viro } 19172ad94ae6SAl Viro 19181da177e4SLinus Torvalds /* 19191da177e4SLinus Torvalds * It's inline, so penalty for filesystems that don't use sticky bit is 19201da177e4SLinus Torvalds * minimal. 19211da177e4SLinus Torvalds */ 19221da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode) 19231da177e4SLinus Torvalds { 1924da9592edSDavid Howells uid_t fsuid = current_fsuid(); 1925da9592edSDavid Howells 19261da177e4SLinus Torvalds if (!(dir->i_mode & S_ISVTX)) 19271da177e4SLinus Torvalds return 0; 1928e795b717SSerge E. Hallyn if (current_user_ns() != inode_userns(inode)) 1929e795b717SSerge E. Hallyn goto other_userns; 1930da9592edSDavid Howells if (inode->i_uid == fsuid) 19311da177e4SLinus Torvalds return 0; 1932da9592edSDavid Howells if (dir->i_uid == fsuid) 19331da177e4SLinus Torvalds return 0; 1934e795b717SSerge E. Hallyn 1935e795b717SSerge E. Hallyn other_userns: 1936e795b717SSerge E. Hallyn return !ns_capable(inode_userns(inode), CAP_FOWNER); 19371da177e4SLinus Torvalds } 19381da177e4SLinus Torvalds 19391da177e4SLinus Torvalds /* 19401da177e4SLinus Torvalds * Check whether we can remove a link victim from directory dir, check 19411da177e4SLinus Torvalds * whether the type of victim is right. 19421da177e4SLinus Torvalds * 1. We can't do it if dir is read-only (done in permission()) 19431da177e4SLinus Torvalds * 2. We should have write and exec permissions on dir 19441da177e4SLinus Torvalds * 3. We can't remove anything from append-only dir 19451da177e4SLinus Torvalds * 4. We can't do anything with immutable dir (done in permission()) 19461da177e4SLinus Torvalds * 5. If the sticky bit on dir is set we should either 19471da177e4SLinus Torvalds * a. be owner of dir, or 19481da177e4SLinus Torvalds * b. be owner of victim, or 19491da177e4SLinus Torvalds * c. have CAP_FOWNER capability 19501da177e4SLinus Torvalds * 6. If the victim is append-only or immutable we can't do antyhing with 19511da177e4SLinus Torvalds * links pointing to it. 19521da177e4SLinus Torvalds * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. 19531da177e4SLinus Torvalds * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. 19541da177e4SLinus Torvalds * 9. We can't remove a root or mountpoint. 19551da177e4SLinus Torvalds * 10. We don't allow removal of NFS sillyrenamed files; it's handled by 19561da177e4SLinus Torvalds * nfs_async_unlink(). 19571da177e4SLinus Torvalds */ 1958858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir) 19591da177e4SLinus Torvalds { 19601da177e4SLinus Torvalds int error; 19611da177e4SLinus Torvalds 19621da177e4SLinus Torvalds if (!victim->d_inode) 19631da177e4SLinus Torvalds return -ENOENT; 19641da177e4SLinus Torvalds 19651da177e4SLinus Torvalds BUG_ON(victim->d_parent->d_inode != dir); 1966cccc6bbaSAl Viro audit_inode_child(victim, dir); 19671da177e4SLinus Torvalds 1968f419a2e3SAl Viro error = inode_permission(dir, MAY_WRITE | MAY_EXEC); 19691da177e4SLinus Torvalds if (error) 19701da177e4SLinus Torvalds return error; 19711da177e4SLinus Torvalds if (IS_APPEND(dir)) 19721da177e4SLinus Torvalds return -EPERM; 19731da177e4SLinus Torvalds if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)|| 1974f9454548SHugh Dickins IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode)) 19751da177e4SLinus Torvalds return -EPERM; 19761da177e4SLinus Torvalds if (isdir) { 19771da177e4SLinus Torvalds if (!S_ISDIR(victim->d_inode->i_mode)) 19781da177e4SLinus Torvalds return -ENOTDIR; 19791da177e4SLinus Torvalds if (IS_ROOT(victim)) 19801da177e4SLinus Torvalds return -EBUSY; 19811da177e4SLinus Torvalds } else if (S_ISDIR(victim->d_inode->i_mode)) 19821da177e4SLinus Torvalds return -EISDIR; 19831da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 19841da177e4SLinus Torvalds return -ENOENT; 19851da177e4SLinus Torvalds if (victim->d_flags & DCACHE_NFSFS_RENAMED) 19861da177e4SLinus Torvalds return -EBUSY; 19871da177e4SLinus Torvalds return 0; 19881da177e4SLinus Torvalds } 19891da177e4SLinus Torvalds 19901da177e4SLinus Torvalds /* Check whether we can create an object with dentry child in directory 19911da177e4SLinus Torvalds * dir. 19921da177e4SLinus Torvalds * 1. We can't do it if child already exists (open has special treatment for 19931da177e4SLinus Torvalds * this case, but since we are inlined it's OK) 19941da177e4SLinus Torvalds * 2. We can't do it if dir is read-only (done in permission()) 19951da177e4SLinus Torvalds * 3. We should have write and exec permissions on dir 19961da177e4SLinus Torvalds * 4. We can't do it if dir is immutable (done in permission()) 19971da177e4SLinus Torvalds */ 1998a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child) 19991da177e4SLinus Torvalds { 20001da177e4SLinus Torvalds if (child->d_inode) 20011da177e4SLinus Torvalds return -EEXIST; 20021da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 20031da177e4SLinus Torvalds return -ENOENT; 2004f419a2e3SAl Viro return inode_permission(dir, MAY_WRITE | MAY_EXEC); 20051da177e4SLinus Torvalds } 20061da177e4SLinus Torvalds 20071da177e4SLinus Torvalds /* 20081da177e4SLinus Torvalds * p1 and p2 should be directories on the same fs. 20091da177e4SLinus Torvalds */ 20101da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2) 20111da177e4SLinus Torvalds { 20121da177e4SLinus Torvalds struct dentry *p; 20131da177e4SLinus Torvalds 20141da177e4SLinus Torvalds if (p1 == p2) { 2015f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 20161da177e4SLinus Torvalds return NULL; 20171da177e4SLinus Torvalds } 20181da177e4SLinus Torvalds 2019a11f3a05SArjan van de Ven mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 20201da177e4SLinus Torvalds 2021e2761a11SOGAWA Hirofumi p = d_ancestor(p2, p1); 2022e2761a11SOGAWA Hirofumi if (p) { 2023f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT); 2024f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD); 20251da177e4SLinus Torvalds return p; 20261da177e4SLinus Torvalds } 20271da177e4SLinus Torvalds 2028e2761a11SOGAWA Hirofumi p = d_ancestor(p1, p2); 2029e2761a11SOGAWA Hirofumi if (p) { 2030f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 2031f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 20321da177e4SLinus Torvalds return p; 20331da177e4SLinus Torvalds } 20341da177e4SLinus Torvalds 2035f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 2036f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 20371da177e4SLinus Torvalds return NULL; 20381da177e4SLinus Torvalds } 20391da177e4SLinus Torvalds 20401da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2) 20411da177e4SLinus Torvalds { 20421b1dcc1bSJes Sorensen mutex_unlock(&p1->d_inode->i_mutex); 20431da177e4SLinus Torvalds if (p1 != p2) { 20441b1dcc1bSJes Sorensen mutex_unlock(&p2->d_inode->i_mutex); 2045a11f3a05SArjan van de Ven mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 20461da177e4SLinus Torvalds } 20471da177e4SLinus Torvalds } 20481da177e4SLinus Torvalds 20494acdaf27SAl Viro int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode, 20501da177e4SLinus Torvalds struct nameidata *nd) 20511da177e4SLinus Torvalds { 2052a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 20531da177e4SLinus Torvalds 20541da177e4SLinus Torvalds if (error) 20551da177e4SLinus Torvalds return error; 20561da177e4SLinus Torvalds 2057acfa4380SAl Viro if (!dir->i_op->create) 20581da177e4SLinus Torvalds return -EACCES; /* shouldn't it be ENOSYS? */ 20591da177e4SLinus Torvalds mode &= S_IALLUGO; 20601da177e4SLinus Torvalds mode |= S_IFREG; 20611da177e4SLinus Torvalds error = security_inode_create(dir, dentry, mode); 20621da177e4SLinus Torvalds if (error) 20631da177e4SLinus Torvalds return error; 20641da177e4SLinus Torvalds error = dir->i_op->create(dir, dentry, mode, nd); 2065a74574aaSStephen Smalley if (!error) 2066f38aa942SAmy Griffis fsnotify_create(dir, dentry); 20671da177e4SLinus Torvalds return error; 20681da177e4SLinus Torvalds } 20691da177e4SLinus Torvalds 207073d049a4SAl Viro static int may_open(struct path *path, int acc_mode, int flag) 20711da177e4SLinus Torvalds { 20723fb64190SChristoph Hellwig struct dentry *dentry = path->dentry; 20731da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 20741da177e4SLinus Torvalds int error; 20751da177e4SLinus Torvalds 2076bcda7652SAl Viro /* O_PATH? */ 2077bcda7652SAl Viro if (!acc_mode) 2078bcda7652SAl Viro return 0; 2079bcda7652SAl Viro 20801da177e4SLinus Torvalds if (!inode) 20811da177e4SLinus Torvalds return -ENOENT; 20821da177e4SLinus Torvalds 2083c8fe8f30SChristoph Hellwig switch (inode->i_mode & S_IFMT) { 2084c8fe8f30SChristoph Hellwig case S_IFLNK: 20851da177e4SLinus Torvalds return -ELOOP; 2086c8fe8f30SChristoph Hellwig case S_IFDIR: 2087c8fe8f30SChristoph Hellwig if (acc_mode & MAY_WRITE) 20881da177e4SLinus Torvalds return -EISDIR; 2089c8fe8f30SChristoph Hellwig break; 2090c8fe8f30SChristoph Hellwig case S_IFBLK: 2091c8fe8f30SChristoph Hellwig case S_IFCHR: 20923fb64190SChristoph Hellwig if (path->mnt->mnt_flags & MNT_NODEV) 20931da177e4SLinus Torvalds return -EACCES; 2094c8fe8f30SChristoph Hellwig /*FALLTHRU*/ 2095c8fe8f30SChristoph Hellwig case S_IFIFO: 2096c8fe8f30SChristoph Hellwig case S_IFSOCK: 20971da177e4SLinus Torvalds flag &= ~O_TRUNC; 2098c8fe8f30SChristoph Hellwig break; 20994a3fd211SDave Hansen } 2100b41572e9SDave Hansen 21013fb64190SChristoph Hellwig error = inode_permission(inode, acc_mode); 2102b41572e9SDave Hansen if (error) 2103b41572e9SDave Hansen return error; 21046146f0d5SMimi Zohar 21051da177e4SLinus Torvalds /* 21061da177e4SLinus Torvalds * An append-only file must be opened in append mode for writing. 21071da177e4SLinus Torvalds */ 21081da177e4SLinus Torvalds if (IS_APPEND(inode)) { 21098737c930SAl Viro if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND)) 21107715b521SAl Viro return -EPERM; 21111da177e4SLinus Torvalds if (flag & O_TRUNC) 21127715b521SAl Viro return -EPERM; 21131da177e4SLinus Torvalds } 21141da177e4SLinus Torvalds 21151da177e4SLinus Torvalds /* O_NOATIME can only be set by the owner or superuser */ 21162e149670SSerge E. Hallyn if (flag & O_NOATIME && !inode_owner_or_capable(inode)) 21177715b521SAl Viro return -EPERM; 21181da177e4SLinus Torvalds 2119f3c7691eSJ. Bruce Fields return 0; 21207715b521SAl Viro } 21217715b521SAl Viro 2122e1181ee6SJeff Layton static int handle_truncate(struct file *filp) 21237715b521SAl Viro { 2124e1181ee6SJeff Layton struct path *path = &filp->f_path; 21257715b521SAl Viro struct inode *inode = path->dentry->d_inode; 21267715b521SAl Viro int error = get_write_access(inode); 21271da177e4SLinus Torvalds if (error) 21287715b521SAl Viro return error; 21291da177e4SLinus Torvalds /* 21301da177e4SLinus Torvalds * Refuse to truncate files with mandatory locks held on them. 21311da177e4SLinus Torvalds */ 21321da177e4SLinus Torvalds error = locks_verify_locked(inode); 2133be6d3e56SKentaro Takeda if (!error) 2134ea0d3ab2STetsuo Handa error = security_path_truncate(path); 21351da177e4SLinus Torvalds if (!error) { 21367715b521SAl Viro error = do_truncate(path->dentry, 0, 2137d139d7ffSMiklos Szeredi ATTR_MTIME|ATTR_CTIME|ATTR_OPEN, 2138e1181ee6SJeff Layton filp); 21391da177e4SLinus Torvalds } 21401da177e4SLinus Torvalds put_write_access(inode); 2141acd0c935SMimi Zohar return error; 21421da177e4SLinus Torvalds } 21431da177e4SLinus Torvalds 2144d57999e1SDave Hansen static inline int open_to_namei_flags(int flag) 2145d57999e1SDave Hansen { 21468a5e929dSAl Viro if ((flag & O_ACCMODE) == 3) 21478a5e929dSAl Viro flag--; 2148d57999e1SDave Hansen return flag; 2149d57999e1SDave Hansen } 2150d57999e1SDave Hansen 215131e6b01fSNick Piggin /* 2152fe2d35ffSAl Viro * Handle the last step of open() 215331e6b01fSNick Piggin */ 2154fb1cc555SAl Viro static struct file *do_last(struct nameidata *nd, struct path *path, 2155c3e380b0SAl Viro const struct open_flags *op, const char *pathname) 2156fb1cc555SAl Viro { 2157a1e28038SAl Viro struct dentry *dir = nd->path.dentry; 21586c0d46c4SAl Viro struct dentry *dentry; 2159ca344a89SAl Viro int open_flag = op->open_flag; 21606c0d46c4SAl Viro int will_truncate = open_flag & O_TRUNC; 2161ca344a89SAl Viro int want_write = 0; 2162bcda7652SAl Viro int acc_mode = op->acc_mode; 2163fb1cc555SAl Viro struct file *filp; 216416c2cd71SAl Viro int error; 2165fb1cc555SAl Viro 2166c3e380b0SAl Viro nd->flags &= ~LOOKUP_PARENT; 2167c3e380b0SAl Viro nd->flags |= op->intent; 2168c3e380b0SAl Viro 21691f36f774SAl Viro switch (nd->last_type) { 21701f36f774SAl Viro case LAST_DOTDOT: 2171176306f5SNeil Brown case LAST_DOT: 2172fe2d35ffSAl Viro error = handle_dots(nd, nd->last_type); 2173fe2d35ffSAl Viro if (error) 2174fe2d35ffSAl Viro return ERR_PTR(error); 21751f36f774SAl Viro /* fallthrough */ 21761f36f774SAl Viro case LAST_ROOT: 21779f1fafeeSAl Viro error = complete_walk(nd); 217816c2cd71SAl Viro if (error) 21799f1fafeeSAl Viro return ERR_PTR(error); 2180fe2d35ffSAl Viro audit_inode(pathname, nd->path.dentry); 2181ca344a89SAl Viro if (open_flag & O_CREAT) { 218216c2cd71SAl Viro error = -EISDIR; 21831f36f774SAl Viro goto exit; 2184fe2d35ffSAl Viro } 2185fe2d35ffSAl Viro goto ok; 21861f36f774SAl Viro case LAST_BIND: 21879f1fafeeSAl Viro error = complete_walk(nd); 218816c2cd71SAl Viro if (error) 21899f1fafeeSAl Viro return ERR_PTR(error); 21901f36f774SAl Viro audit_inode(pathname, dir); 21911f36f774SAl Viro goto ok; 21921f36f774SAl Viro } 2193a2c36b45SAl Viro 2194ca344a89SAl Viro if (!(open_flag & O_CREAT)) { 2195bcda7652SAl Viro int symlink_ok = 0; 2196fe2d35ffSAl Viro if (nd->last.name[nd->last.len]) 2197fe2d35ffSAl Viro nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 2198bcda7652SAl Viro if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW)) 2199bcda7652SAl Viro symlink_ok = 1; 2200fe2d35ffSAl Viro /* we _can_ be in RCU mode here */ 2201ce57dfc1SAl Viro error = walk_component(nd, path, &nd->last, LAST_NORM, 2202ce57dfc1SAl Viro !symlink_ok); 2203ce57dfc1SAl Viro if (error < 0) 2204fe2d35ffSAl Viro return ERR_PTR(error); 2205ce57dfc1SAl Viro if (error) /* symlink */ 2206fe2d35ffSAl Viro return NULL; 2207fe2d35ffSAl Viro /* sayonara */ 22089f1fafeeSAl Viro error = complete_walk(nd); 22099f1fafeeSAl Viro if (error) 22107f6c7e62SMiklos Szeredi return ERR_PTR(error); 2211fe2d35ffSAl Viro 2212fe2d35ffSAl Viro error = -ENOTDIR; 2213fe2d35ffSAl Viro if (nd->flags & LOOKUP_DIRECTORY) { 2214ce57dfc1SAl Viro if (!nd->inode->i_op->lookup) 2215fe2d35ffSAl Viro goto exit; 2216fe2d35ffSAl Viro } 2217fe2d35ffSAl Viro audit_inode(pathname, nd->path.dentry); 2218fe2d35ffSAl Viro goto ok; 2219fe2d35ffSAl Viro } 2220fe2d35ffSAl Viro 2221fe2d35ffSAl Viro /* create side of things */ 2222a3fbbde7SAl Viro /* 2223a3fbbde7SAl Viro * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED has been 2224a3fbbde7SAl Viro * cleared when we got to the last component we are about to look up 2225a3fbbde7SAl Viro */ 22269f1fafeeSAl Viro error = complete_walk(nd); 22279f1fafeeSAl Viro if (error) 22289f1fafeeSAl Viro return ERR_PTR(error); 2229fe2d35ffSAl Viro 2230fe2d35ffSAl Viro audit_inode(pathname, dir); 223116c2cd71SAl Viro error = -EISDIR; 22321f36f774SAl Viro /* trailing slashes? */ 223331e6b01fSNick Piggin if (nd->last.name[nd->last.len]) 22341f36f774SAl Viro goto exit; 22351f36f774SAl Viro 2236a1e28038SAl Viro mutex_lock(&dir->d_inode->i_mutex); 2237a1e28038SAl Viro 22386c0d46c4SAl Viro dentry = lookup_hash(nd); 22396c0d46c4SAl Viro error = PTR_ERR(dentry); 22406c0d46c4SAl Viro if (IS_ERR(dentry)) { 2241fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2242fb1cc555SAl Viro goto exit; 2243fb1cc555SAl Viro } 2244fb1cc555SAl Viro 22456c0d46c4SAl Viro path->dentry = dentry; 22466c0d46c4SAl Viro path->mnt = nd->path.mnt; 22476c0d46c4SAl Viro 2248fb1cc555SAl Viro /* Negative dentry, just create the file */ 22496c0d46c4SAl Viro if (!dentry->d_inode) { 2250a218d0fdSAl Viro umode_t mode = op->mode; 22516c0d46c4SAl Viro if (!IS_POSIXACL(dir->d_inode)) 22526c0d46c4SAl Viro mode &= ~current_umask(); 2253fb1cc555SAl Viro /* 2254fb1cc555SAl Viro * This write is needed to ensure that a 22556c0d46c4SAl Viro * rw->ro transition does not occur between 2256fb1cc555SAl Viro * the time when the file is created and when 2257fb1cc555SAl Viro * a permanent write count is taken through 2258fb1cc555SAl Viro * the 'struct file' in nameidata_to_filp(). 2259fb1cc555SAl Viro */ 2260fb1cc555SAl Viro error = mnt_want_write(nd->path.mnt); 2261fb1cc555SAl Viro if (error) 2262fb1cc555SAl Viro goto exit_mutex_unlock; 2263ca344a89SAl Viro want_write = 1; 22649b44f1b3SAl Viro /* Don't check for write permission, don't truncate */ 2265ca344a89SAl Viro open_flag &= ~O_TRUNC; 22666c0d46c4SAl Viro will_truncate = 0; 2267bcda7652SAl Viro acc_mode = MAY_OPEN; 22686c0d46c4SAl Viro error = security_path_mknod(&nd->path, dentry, mode, 0); 22696c0d46c4SAl Viro if (error) 22706c0d46c4SAl Viro goto exit_mutex_unlock; 22716c0d46c4SAl Viro error = vfs_create(dir->d_inode, dentry, mode, nd); 22726c0d46c4SAl Viro if (error) 22736c0d46c4SAl Viro goto exit_mutex_unlock; 22746c0d46c4SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 22756c0d46c4SAl Viro dput(nd->path.dentry); 22766c0d46c4SAl Viro nd->path.dentry = dentry; 2277ca344a89SAl Viro goto common; 2278fb1cc555SAl Viro } 2279fb1cc555SAl Viro 2280fb1cc555SAl Viro /* 2281fb1cc555SAl Viro * It already exists. 2282fb1cc555SAl Viro */ 2283fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2284fb1cc555SAl Viro audit_inode(pathname, path->dentry); 2285fb1cc555SAl Viro 2286fb1cc555SAl Viro error = -EEXIST; 2287ca344a89SAl Viro if (open_flag & O_EXCL) 2288fb1cc555SAl Viro goto exit_dput; 2289fb1cc555SAl Viro 22909875cf80SDavid Howells error = follow_managed(path, nd->flags); 22919875cf80SDavid Howells if (error < 0) 2292fb1cc555SAl Viro goto exit_dput; 2293fb1cc555SAl Viro 2294a3fbbde7SAl Viro if (error) 2295a3fbbde7SAl Viro nd->flags |= LOOKUP_JUMPED; 2296a3fbbde7SAl Viro 2297fb1cc555SAl Viro error = -ENOENT; 2298fb1cc555SAl Viro if (!path->dentry->d_inode) 2299fb1cc555SAl Viro goto exit_dput; 23009e67f361SAl Viro 23019e67f361SAl Viro if (path->dentry->d_inode->i_op->follow_link) 2302fb1cc555SAl Viro return NULL; 2303fb1cc555SAl Viro 2304fb1cc555SAl Viro path_to_nameidata(path, nd); 230531e6b01fSNick Piggin nd->inode = path->dentry->d_inode; 2306a3fbbde7SAl Viro /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */ 2307a3fbbde7SAl Viro error = complete_walk(nd); 2308a3fbbde7SAl Viro if (error) 2309097b180cSMiklos Szeredi return ERR_PTR(error); 2310fb1cc555SAl Viro error = -EISDIR; 231131e6b01fSNick Piggin if (S_ISDIR(nd->inode->i_mode)) 2312fb1cc555SAl Viro goto exit; 231367ee3ad2SAl Viro ok: 23146c0d46c4SAl Viro if (!S_ISREG(nd->inode->i_mode)) 23156c0d46c4SAl Viro will_truncate = 0; 23166c0d46c4SAl Viro 23170f9d1a10SAl Viro if (will_truncate) { 23180f9d1a10SAl Viro error = mnt_want_write(nd->path.mnt); 23190f9d1a10SAl Viro if (error) 23200f9d1a10SAl Viro goto exit; 2321ca344a89SAl Viro want_write = 1; 23220f9d1a10SAl Viro } 2323ca344a89SAl Viro common: 2324bcda7652SAl Viro error = may_open(&nd->path, acc_mode, open_flag); 2325ca344a89SAl Viro if (error) 23260f9d1a10SAl Viro goto exit; 23270f9d1a10SAl Viro filp = nameidata_to_filp(nd); 23280f9d1a10SAl Viro if (!IS_ERR(filp)) { 23290f9d1a10SAl Viro error = ima_file_check(filp, op->acc_mode); 23300f9d1a10SAl Viro if (error) { 23310f9d1a10SAl Viro fput(filp); 23320f9d1a10SAl Viro filp = ERR_PTR(error); 23330f9d1a10SAl Viro } 23340f9d1a10SAl Viro } 23350f9d1a10SAl Viro if (!IS_ERR(filp)) { 23360f9d1a10SAl Viro if (will_truncate) { 23370f9d1a10SAl Viro error = handle_truncate(filp); 23380f9d1a10SAl Viro if (error) { 23390f9d1a10SAl Viro fput(filp); 23400f9d1a10SAl Viro filp = ERR_PTR(error); 23410f9d1a10SAl Viro } 23420f9d1a10SAl Viro } 23430f9d1a10SAl Viro } 2344ca344a89SAl Viro out: 2345ca344a89SAl Viro if (want_write) 23460f9d1a10SAl Viro mnt_drop_write(nd->path.mnt); 23470f9d1a10SAl Viro path_put(&nd->path); 2348fb1cc555SAl Viro return filp; 2349fb1cc555SAl Viro 2350fb1cc555SAl Viro exit_mutex_unlock: 2351fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2352fb1cc555SAl Viro exit_dput: 2353fb1cc555SAl Viro path_put_conditional(path, nd); 2354fb1cc555SAl Viro exit: 2355ca344a89SAl Viro filp = ERR_PTR(error); 2356ca344a89SAl Viro goto out; 2357fb1cc555SAl Viro } 2358fb1cc555SAl Viro 235913aab428SAl Viro static struct file *path_openat(int dfd, const char *pathname, 236073d049a4SAl Viro struct nameidata *nd, const struct open_flags *op, int flags) 23611da177e4SLinus Torvalds { 2362fe2d35ffSAl Viro struct file *base = NULL; 23634a3fd211SDave Hansen struct file *filp; 23649850c056SAl Viro struct path path; 236513aab428SAl Viro int error; 236631e6b01fSNick Piggin 236731e6b01fSNick Piggin filp = get_empty_filp(); 236831e6b01fSNick Piggin if (!filp) 236931e6b01fSNick Piggin return ERR_PTR(-ENFILE); 237031e6b01fSNick Piggin 237147c805dcSAl Viro filp->f_flags = op->open_flag; 237273d049a4SAl Viro nd->intent.open.file = filp; 237373d049a4SAl Viro nd->intent.open.flags = open_to_namei_flags(op->open_flag); 237473d049a4SAl Viro nd->intent.open.create_mode = op->mode; 237531e6b01fSNick Piggin 237673d049a4SAl Viro error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base); 237731e6b01fSNick Piggin if (unlikely(error)) 237813aab428SAl Viro goto out_filp; 237931e6b01fSNick Piggin 2380fe2d35ffSAl Viro current->total_link_count = 0; 238173d049a4SAl Viro error = link_path_walk(pathname, nd); 238231e6b01fSNick Piggin if (unlikely(error)) 238331e6b01fSNick Piggin goto out_filp; 23841da177e4SLinus Torvalds 238573d049a4SAl Viro filp = do_last(nd, &path, op, pathname); 2386806b681cSAl Viro while (unlikely(!filp)) { /* trailing symlink */ 23877b9337aaSNick Piggin struct path link = path; 2388def4af30SAl Viro void *cookie; 2389574197e0SAl Viro if (!(nd->flags & LOOKUP_FOLLOW)) { 239073d049a4SAl Viro path_put_conditional(&path, nd); 239173d049a4SAl Viro path_put(&nd->path); 239240b39136SAl Viro filp = ERR_PTR(-ELOOP); 239340b39136SAl Viro break; 239440b39136SAl Viro } 239573d049a4SAl Viro nd->flags |= LOOKUP_PARENT; 239673d049a4SAl Viro nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL); 2397574197e0SAl Viro error = follow_link(&link, nd, &cookie); 2398c3e380b0SAl Viro if (unlikely(error)) 2399f1afe9efSAl Viro filp = ERR_PTR(error); 2400c3e380b0SAl Viro else 240173d049a4SAl Viro filp = do_last(nd, &path, op, pathname); 2402574197e0SAl Viro put_link(nd, &link, cookie); 2403806b681cSAl Viro } 240410fa8e62SAl Viro out: 240573d049a4SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) 240673d049a4SAl Viro path_put(&nd->root); 2407fe2d35ffSAl Viro if (base) 2408fe2d35ffSAl Viro fput(base); 240973d049a4SAl Viro release_open_intent(nd); 241010fa8e62SAl Viro return filp; 24111da177e4SLinus Torvalds 241231e6b01fSNick Piggin out_filp: 241310fa8e62SAl Viro filp = ERR_PTR(error); 241410fa8e62SAl Viro goto out; 2415de459215SKirill Korotaev } 24161da177e4SLinus Torvalds 241713aab428SAl Viro struct file *do_filp_open(int dfd, const char *pathname, 241813aab428SAl Viro const struct open_flags *op, int flags) 241913aab428SAl Viro { 242073d049a4SAl Viro struct nameidata nd; 242113aab428SAl Viro struct file *filp; 242213aab428SAl Viro 242373d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU); 242413aab428SAl Viro if (unlikely(filp == ERR_PTR(-ECHILD))) 242573d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags); 242613aab428SAl Viro if (unlikely(filp == ERR_PTR(-ESTALE))) 242773d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL); 242813aab428SAl Viro return filp; 242913aab428SAl Viro } 243013aab428SAl Viro 243173d049a4SAl Viro struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt, 243273d049a4SAl Viro const char *name, const struct open_flags *op, int flags) 243373d049a4SAl Viro { 243473d049a4SAl Viro struct nameidata nd; 243573d049a4SAl Viro struct file *file; 243673d049a4SAl Viro 243773d049a4SAl Viro nd.root.mnt = mnt; 243873d049a4SAl Viro nd.root.dentry = dentry; 243973d049a4SAl Viro 244073d049a4SAl Viro flags |= LOOKUP_ROOT; 244173d049a4SAl Viro 2442bcda7652SAl Viro if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN) 244373d049a4SAl Viro return ERR_PTR(-ELOOP); 244473d049a4SAl Viro 244573d049a4SAl Viro file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU); 244673d049a4SAl Viro if (unlikely(file == ERR_PTR(-ECHILD))) 244773d049a4SAl Viro file = path_openat(-1, name, &nd, op, flags); 244873d049a4SAl Viro if (unlikely(file == ERR_PTR(-ESTALE))) 244973d049a4SAl Viro file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL); 245073d049a4SAl Viro return file; 245173d049a4SAl Viro } 245273d049a4SAl Viro 2453ed75e95dSAl Viro struct dentry *kern_path_create(int dfd, const char *pathname, struct path *path, int is_dir) 24541da177e4SLinus Torvalds { 2455c663e5d8SChristoph Hellwig struct dentry *dentry = ERR_PTR(-EEXIST); 2456ed75e95dSAl Viro struct nameidata nd; 2457ed75e95dSAl Viro int error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd); 2458ed75e95dSAl Viro if (error) 2459ed75e95dSAl Viro return ERR_PTR(error); 24601da177e4SLinus Torvalds 2461c663e5d8SChristoph Hellwig /* 2462c663e5d8SChristoph Hellwig * Yucky last component or no last component at all? 2463c663e5d8SChristoph Hellwig * (foo/., foo/.., /////) 2464c663e5d8SChristoph Hellwig */ 2465ed75e95dSAl Viro if (nd.last_type != LAST_NORM) 2466ed75e95dSAl Viro goto out; 2467ed75e95dSAl Viro nd.flags &= ~LOOKUP_PARENT; 2468ed75e95dSAl Viro nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL; 2469ed75e95dSAl Viro nd.intent.open.flags = O_EXCL; 2470c663e5d8SChristoph Hellwig 2471c663e5d8SChristoph Hellwig /* 2472c663e5d8SChristoph Hellwig * Do the final lookup. 2473c663e5d8SChristoph Hellwig */ 2474ed75e95dSAl Viro mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 2475ed75e95dSAl Viro dentry = lookup_hash(&nd); 24761da177e4SLinus Torvalds if (IS_ERR(dentry)) 24771da177e4SLinus Torvalds goto fail; 2478c663e5d8SChristoph Hellwig 2479e9baf6e5SAl Viro if (dentry->d_inode) 2480e9baf6e5SAl Viro goto eexist; 2481c663e5d8SChristoph Hellwig /* 2482c663e5d8SChristoph Hellwig * Special case - lookup gave negative, but... we had foo/bar/ 2483c663e5d8SChristoph Hellwig * From the vfs_mknod() POV we just have a negative dentry - 2484c663e5d8SChristoph Hellwig * all is fine. Let's be bastards - you had / on the end, you've 2485c663e5d8SChristoph Hellwig * been asking for (non-existent) directory. -ENOENT for you. 2486c663e5d8SChristoph Hellwig */ 2487ed75e95dSAl Viro if (unlikely(!is_dir && nd.last.name[nd.last.len])) { 24881da177e4SLinus Torvalds dput(dentry); 24891da177e4SLinus Torvalds dentry = ERR_PTR(-ENOENT); 2490ed75e95dSAl Viro goto fail; 2491e9baf6e5SAl Viro } 2492ed75e95dSAl Viro *path = nd.path; 2493e9baf6e5SAl Viro return dentry; 2494e9baf6e5SAl Viro eexist: 2495e9baf6e5SAl Viro dput(dentry); 2496e9baf6e5SAl Viro dentry = ERR_PTR(-EEXIST); 24971da177e4SLinus Torvalds fail: 2498dae6ad8fSAl Viro mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 2499ed75e95dSAl Viro out: 2500dae6ad8fSAl Viro path_put(&nd.path); 2501ed75e95dSAl Viro return dentry; 2502dae6ad8fSAl Viro } 2503dae6ad8fSAl Viro EXPORT_SYMBOL(kern_path_create); 2504dae6ad8fSAl Viro 2505dae6ad8fSAl Viro struct dentry *user_path_create(int dfd, const char __user *pathname, struct path *path, int is_dir) 2506dae6ad8fSAl Viro { 2507dae6ad8fSAl Viro char *tmp = getname(pathname); 2508dae6ad8fSAl Viro struct dentry *res; 2509dae6ad8fSAl Viro if (IS_ERR(tmp)) 2510dae6ad8fSAl Viro return ERR_CAST(tmp); 2511dae6ad8fSAl Viro res = kern_path_create(dfd, tmp, path, is_dir); 2512dae6ad8fSAl Viro putname(tmp); 2513dae6ad8fSAl Viro return res; 2514dae6ad8fSAl Viro } 2515dae6ad8fSAl Viro EXPORT_SYMBOL(user_path_create); 2516dae6ad8fSAl Viro 25171a67aafbSAl Viro int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev) 25181da177e4SLinus Torvalds { 2519a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 25201da177e4SLinus Torvalds 25211da177e4SLinus Torvalds if (error) 25221da177e4SLinus Torvalds return error; 25231da177e4SLinus Torvalds 2524e795b717SSerge E. Hallyn if ((S_ISCHR(mode) || S_ISBLK(mode)) && 2525e795b717SSerge E. Hallyn !ns_capable(inode_userns(dir), CAP_MKNOD)) 25261da177e4SLinus Torvalds return -EPERM; 25271da177e4SLinus Torvalds 2528acfa4380SAl Viro if (!dir->i_op->mknod) 25291da177e4SLinus Torvalds return -EPERM; 25301da177e4SLinus Torvalds 253108ce5f16SSerge E. Hallyn error = devcgroup_inode_mknod(mode, dev); 253208ce5f16SSerge E. Hallyn if (error) 253308ce5f16SSerge E. Hallyn return error; 253408ce5f16SSerge E. Hallyn 25351da177e4SLinus Torvalds error = security_inode_mknod(dir, dentry, mode, dev); 25361da177e4SLinus Torvalds if (error) 25371da177e4SLinus Torvalds return error; 25381da177e4SLinus Torvalds 25391da177e4SLinus Torvalds error = dir->i_op->mknod(dir, dentry, mode, dev); 2540a74574aaSStephen Smalley if (!error) 2541f38aa942SAmy Griffis fsnotify_create(dir, dentry); 25421da177e4SLinus Torvalds return error; 25431da177e4SLinus Torvalds } 25441da177e4SLinus Torvalds 2545f69aac00SAl Viro static int may_mknod(umode_t mode) 2546463c3197SDave Hansen { 2547463c3197SDave Hansen switch (mode & S_IFMT) { 2548463c3197SDave Hansen case S_IFREG: 2549463c3197SDave Hansen case S_IFCHR: 2550463c3197SDave Hansen case S_IFBLK: 2551463c3197SDave Hansen case S_IFIFO: 2552463c3197SDave Hansen case S_IFSOCK: 2553463c3197SDave Hansen case 0: /* zero mode translates to S_IFREG */ 2554463c3197SDave Hansen return 0; 2555463c3197SDave Hansen case S_IFDIR: 2556463c3197SDave Hansen return -EPERM; 2557463c3197SDave Hansen default: 2558463c3197SDave Hansen return -EINVAL; 2559463c3197SDave Hansen } 2560463c3197SDave Hansen } 2561463c3197SDave Hansen 25628208a22bSAl Viro SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode, 25632e4d0924SHeiko Carstens unsigned, dev) 25641da177e4SLinus Torvalds { 25651da177e4SLinus Torvalds struct dentry *dentry; 2566dae6ad8fSAl Viro struct path path; 2567dae6ad8fSAl Viro int error; 25681da177e4SLinus Torvalds 25691da177e4SLinus Torvalds if (S_ISDIR(mode)) 25701da177e4SLinus Torvalds return -EPERM; 25711da177e4SLinus Torvalds 2572dae6ad8fSAl Viro dentry = user_path_create(dfd, filename, &path, 0); 2573dae6ad8fSAl Viro if (IS_ERR(dentry)) 2574dae6ad8fSAl Viro return PTR_ERR(dentry); 25752ad94ae6SAl Viro 2576dae6ad8fSAl Viro if (!IS_POSIXACL(path.dentry->d_inode)) 2577ce3b0f8dSAl Viro mode &= ~current_umask(); 2578463c3197SDave Hansen error = may_mknod(mode); 2579463c3197SDave Hansen if (error) 2580463c3197SDave Hansen goto out_dput; 2581dae6ad8fSAl Viro error = mnt_want_write(path.mnt); 2582463c3197SDave Hansen if (error) 2583463c3197SDave Hansen goto out_dput; 2584dae6ad8fSAl Viro error = security_path_mknod(&path, dentry, mode, dev); 2585be6d3e56SKentaro Takeda if (error) 2586be6d3e56SKentaro Takeda goto out_drop_write; 25871da177e4SLinus Torvalds switch (mode & S_IFMT) { 25881da177e4SLinus Torvalds case 0: case S_IFREG: 2589dae6ad8fSAl Viro error = vfs_create(path.dentry->d_inode,dentry,mode,NULL); 25901da177e4SLinus Torvalds break; 25911da177e4SLinus Torvalds case S_IFCHR: case S_IFBLK: 2592dae6ad8fSAl Viro error = vfs_mknod(path.dentry->d_inode,dentry,mode, 25931da177e4SLinus Torvalds new_decode_dev(dev)); 25941da177e4SLinus Torvalds break; 25951da177e4SLinus Torvalds case S_IFIFO: case S_IFSOCK: 2596dae6ad8fSAl Viro error = vfs_mknod(path.dentry->d_inode,dentry,mode,0); 25971da177e4SLinus Torvalds break; 25981da177e4SLinus Torvalds } 2599be6d3e56SKentaro Takeda out_drop_write: 2600dae6ad8fSAl Viro mnt_drop_write(path.mnt); 2601463c3197SDave Hansen out_dput: 26021da177e4SLinus Torvalds dput(dentry); 2603dae6ad8fSAl Viro mutex_unlock(&path.dentry->d_inode->i_mutex); 2604dae6ad8fSAl Viro path_put(&path); 26051da177e4SLinus Torvalds 26061da177e4SLinus Torvalds return error; 26071da177e4SLinus Torvalds } 26081da177e4SLinus Torvalds 26098208a22bSAl Viro SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev) 26105590ff0dSUlrich Drepper { 26115590ff0dSUlrich Drepper return sys_mknodat(AT_FDCWD, filename, mode, dev); 26125590ff0dSUlrich Drepper } 26135590ff0dSUlrich Drepper 261418bb1db3SAl Viro int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) 26151da177e4SLinus Torvalds { 2616a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 26178de52778SAl Viro unsigned max_links = dir->i_sb->s_max_links; 26181da177e4SLinus Torvalds 26191da177e4SLinus Torvalds if (error) 26201da177e4SLinus Torvalds return error; 26211da177e4SLinus Torvalds 2622acfa4380SAl Viro if (!dir->i_op->mkdir) 26231da177e4SLinus Torvalds return -EPERM; 26241da177e4SLinus Torvalds 26251da177e4SLinus Torvalds mode &= (S_IRWXUGO|S_ISVTX); 26261da177e4SLinus Torvalds error = security_inode_mkdir(dir, dentry, mode); 26271da177e4SLinus Torvalds if (error) 26281da177e4SLinus Torvalds return error; 26291da177e4SLinus Torvalds 26308de52778SAl Viro if (max_links && dir->i_nlink >= max_links) 26318de52778SAl Viro return -EMLINK; 26328de52778SAl Viro 26331da177e4SLinus Torvalds error = dir->i_op->mkdir(dir, dentry, mode); 2634a74574aaSStephen Smalley if (!error) 2635f38aa942SAmy Griffis fsnotify_mkdir(dir, dentry); 26361da177e4SLinus Torvalds return error; 26371da177e4SLinus Torvalds } 26381da177e4SLinus Torvalds 2639a218d0fdSAl Viro SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode) 26401da177e4SLinus Torvalds { 26416902d925SDave Hansen struct dentry *dentry; 2642dae6ad8fSAl Viro struct path path; 2643dae6ad8fSAl Viro int error; 26441da177e4SLinus Torvalds 2645dae6ad8fSAl Viro dentry = user_path_create(dfd, pathname, &path, 1); 26466902d925SDave Hansen if (IS_ERR(dentry)) 2647dae6ad8fSAl Viro return PTR_ERR(dentry); 26486902d925SDave Hansen 2649dae6ad8fSAl Viro if (!IS_POSIXACL(path.dentry->d_inode)) 2650ce3b0f8dSAl Viro mode &= ~current_umask(); 2651dae6ad8fSAl Viro error = mnt_want_write(path.mnt); 2652463c3197SDave Hansen if (error) 2653463c3197SDave Hansen goto out_dput; 2654dae6ad8fSAl Viro error = security_path_mkdir(&path, dentry, mode); 2655be6d3e56SKentaro Takeda if (error) 2656be6d3e56SKentaro Takeda goto out_drop_write; 2657dae6ad8fSAl Viro error = vfs_mkdir(path.dentry->d_inode, dentry, mode); 2658be6d3e56SKentaro Takeda out_drop_write: 2659dae6ad8fSAl Viro mnt_drop_write(path.mnt); 2660463c3197SDave Hansen out_dput: 26611da177e4SLinus Torvalds dput(dentry); 2662dae6ad8fSAl Viro mutex_unlock(&path.dentry->d_inode->i_mutex); 2663dae6ad8fSAl Viro path_put(&path); 26641da177e4SLinus Torvalds return error; 26651da177e4SLinus Torvalds } 26661da177e4SLinus Torvalds 2667a218d0fdSAl Viro SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode) 26685590ff0dSUlrich Drepper { 26695590ff0dSUlrich Drepper return sys_mkdirat(AT_FDCWD, pathname, mode); 26705590ff0dSUlrich Drepper } 26715590ff0dSUlrich Drepper 26721da177e4SLinus Torvalds /* 2673a71905f0SSage Weil * The dentry_unhash() helper will try to drop the dentry early: we 2674c0d02594SJ. Bruce Fields * should have a usage count of 1 if we're the only user of this 2675a71905f0SSage Weil * dentry, and if that is true (possibly after pruning the dcache), 2676a71905f0SSage Weil * then we drop the dentry now. 26771da177e4SLinus Torvalds * 26781da177e4SLinus Torvalds * A low-level filesystem can, if it choses, legally 26791da177e4SLinus Torvalds * do a 26801da177e4SLinus Torvalds * 26811da177e4SLinus Torvalds * if (!d_unhashed(dentry)) 26821da177e4SLinus Torvalds * return -EBUSY; 26831da177e4SLinus Torvalds * 26841da177e4SLinus Torvalds * if it cannot handle the case of removing a directory 26851da177e4SLinus Torvalds * that is still in use by something else.. 26861da177e4SLinus Torvalds */ 26871da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry) 26881da177e4SLinus Torvalds { 26891da177e4SLinus Torvalds shrink_dcache_parent(dentry); 26901da177e4SLinus Torvalds spin_lock(&dentry->d_lock); 269164252c75SSage Weil if (dentry->d_count == 1) 26921da177e4SLinus Torvalds __d_drop(dentry); 26931da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 26941da177e4SLinus Torvalds } 26951da177e4SLinus Torvalds 26961da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry) 26971da177e4SLinus Torvalds { 26981da177e4SLinus Torvalds int error = may_delete(dir, dentry, 1); 26991da177e4SLinus Torvalds 27001da177e4SLinus Torvalds if (error) 27011da177e4SLinus Torvalds return error; 27021da177e4SLinus Torvalds 2703acfa4380SAl Viro if (!dir->i_op->rmdir) 27041da177e4SLinus Torvalds return -EPERM; 27051da177e4SLinus Torvalds 27061d2ef590SAl Viro dget(dentry); 27071b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 2708912dbc15SSage Weil 27091da177e4SLinus Torvalds error = -EBUSY; 2710912dbc15SSage Weil if (d_mountpoint(dentry)) 2711912dbc15SSage Weil goto out; 2712912dbc15SSage Weil 27131da177e4SLinus Torvalds error = security_inode_rmdir(dir, dentry); 2714912dbc15SSage Weil if (error) 2715912dbc15SSage Weil goto out; 2716912dbc15SSage Weil 27173cebde24SSage Weil shrink_dcache_parent(dentry); 27181da177e4SLinus Torvalds error = dir->i_op->rmdir(dir, dentry); 2719912dbc15SSage Weil if (error) 2720912dbc15SSage Weil goto out; 2721912dbc15SSage Weil 27221da177e4SLinus Torvalds dentry->d_inode->i_flags |= S_DEAD; 2723d83c49f3SAl Viro dont_mount(dentry); 27241da177e4SLinus Torvalds 2725912dbc15SSage Weil out: 2726912dbc15SSage Weil mutex_unlock(&dentry->d_inode->i_mutex); 27271d2ef590SAl Viro dput(dentry); 2728912dbc15SSage Weil if (!error) 2729912dbc15SSage Weil d_delete(dentry); 27301da177e4SLinus Torvalds return error; 27311da177e4SLinus Torvalds } 27321da177e4SLinus Torvalds 27335590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname) 27341da177e4SLinus Torvalds { 27351da177e4SLinus Torvalds int error = 0; 27361da177e4SLinus Torvalds char * name; 27371da177e4SLinus Torvalds struct dentry *dentry; 27381da177e4SLinus Torvalds struct nameidata nd; 27391da177e4SLinus Torvalds 27402ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &name); 27411da177e4SLinus Torvalds if (error) 27422ad94ae6SAl Viro return error; 27431da177e4SLinus Torvalds 27441da177e4SLinus Torvalds switch(nd.last_type) { 27451da177e4SLinus Torvalds case LAST_DOTDOT: 27461da177e4SLinus Torvalds error = -ENOTEMPTY; 27471da177e4SLinus Torvalds goto exit1; 27481da177e4SLinus Torvalds case LAST_DOT: 27491da177e4SLinus Torvalds error = -EINVAL; 27501da177e4SLinus Torvalds goto exit1; 27511da177e4SLinus Torvalds case LAST_ROOT: 27521da177e4SLinus Torvalds error = -EBUSY; 27531da177e4SLinus Torvalds goto exit1; 27541da177e4SLinus Torvalds } 27550612d9fbSOGAWA Hirofumi 27560612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 27570612d9fbSOGAWA Hirofumi 27584ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 275949705b77SChristoph Hellwig dentry = lookup_hash(&nd); 27601da177e4SLinus Torvalds error = PTR_ERR(dentry); 27616902d925SDave Hansen if (IS_ERR(dentry)) 27626902d925SDave Hansen goto exit2; 2763e6bc45d6STheodore Ts'o if (!dentry->d_inode) { 2764e6bc45d6STheodore Ts'o error = -ENOENT; 2765e6bc45d6STheodore Ts'o goto exit3; 2766e6bc45d6STheodore Ts'o } 27670622753bSDave Hansen error = mnt_want_write(nd.path.mnt); 27680622753bSDave Hansen if (error) 27690622753bSDave Hansen goto exit3; 2770be6d3e56SKentaro Takeda error = security_path_rmdir(&nd.path, dentry); 2771be6d3e56SKentaro Takeda if (error) 2772be6d3e56SKentaro Takeda goto exit4; 27734ac91378SJan Blunck error = vfs_rmdir(nd.path.dentry->d_inode, dentry); 2774be6d3e56SKentaro Takeda exit4: 27750622753bSDave Hansen mnt_drop_write(nd.path.mnt); 27760622753bSDave Hansen exit3: 27771da177e4SLinus Torvalds dput(dentry); 27786902d925SDave Hansen exit2: 27794ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 27801da177e4SLinus Torvalds exit1: 27811d957f9bSJan Blunck path_put(&nd.path); 27821da177e4SLinus Torvalds putname(name); 27831da177e4SLinus Torvalds return error; 27841da177e4SLinus Torvalds } 27851da177e4SLinus Torvalds 27863cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname) 27875590ff0dSUlrich Drepper { 27885590ff0dSUlrich Drepper return do_rmdir(AT_FDCWD, pathname); 27895590ff0dSUlrich Drepper } 27905590ff0dSUlrich Drepper 27911da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry) 27921da177e4SLinus Torvalds { 27931da177e4SLinus Torvalds int error = may_delete(dir, dentry, 0); 27941da177e4SLinus Torvalds 27951da177e4SLinus Torvalds if (error) 27961da177e4SLinus Torvalds return error; 27971da177e4SLinus Torvalds 2798acfa4380SAl Viro if (!dir->i_op->unlink) 27991da177e4SLinus Torvalds return -EPERM; 28001da177e4SLinus Torvalds 28011b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 28021da177e4SLinus Torvalds if (d_mountpoint(dentry)) 28031da177e4SLinus Torvalds error = -EBUSY; 28041da177e4SLinus Torvalds else { 28051da177e4SLinus Torvalds error = security_inode_unlink(dir, dentry); 2806bec1052eSAl Viro if (!error) { 28071da177e4SLinus Torvalds error = dir->i_op->unlink(dir, dentry); 2808bec1052eSAl Viro if (!error) 2809d83c49f3SAl Viro dont_mount(dentry); 2810bec1052eSAl Viro } 28111da177e4SLinus Torvalds } 28121b1dcc1bSJes Sorensen mutex_unlock(&dentry->d_inode->i_mutex); 28131da177e4SLinus Torvalds 28141da177e4SLinus Torvalds /* We don't d_delete() NFS sillyrenamed files--they still exist. */ 28151da177e4SLinus Torvalds if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) { 2816ece95912SJan Kara fsnotify_link_count(dentry->d_inode); 28171da177e4SLinus Torvalds d_delete(dentry); 28181da177e4SLinus Torvalds } 28190eeca283SRobert Love 28201da177e4SLinus Torvalds return error; 28211da177e4SLinus Torvalds } 28221da177e4SLinus Torvalds 28231da177e4SLinus Torvalds /* 28241da177e4SLinus Torvalds * Make sure that the actual truncation of the file will occur outside its 28251b1dcc1bSJes Sorensen * directory's i_mutex. Truncate can take a long time if there is a lot of 28261da177e4SLinus Torvalds * writeout happening, and we don't want to prevent access to the directory 28271da177e4SLinus Torvalds * while waiting on the I/O. 28281da177e4SLinus Torvalds */ 28295590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname) 28301da177e4SLinus Torvalds { 28312ad94ae6SAl Viro int error; 28321da177e4SLinus Torvalds char *name; 28331da177e4SLinus Torvalds struct dentry *dentry; 28341da177e4SLinus Torvalds struct nameidata nd; 28351da177e4SLinus Torvalds struct inode *inode = NULL; 28361da177e4SLinus Torvalds 28372ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &name); 28381da177e4SLinus Torvalds if (error) 28392ad94ae6SAl Viro return error; 28402ad94ae6SAl Viro 28411da177e4SLinus Torvalds error = -EISDIR; 28421da177e4SLinus Torvalds if (nd.last_type != LAST_NORM) 28431da177e4SLinus Torvalds goto exit1; 28440612d9fbSOGAWA Hirofumi 28450612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 28460612d9fbSOGAWA Hirofumi 28474ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 284849705b77SChristoph Hellwig dentry = lookup_hash(&nd); 28491da177e4SLinus Torvalds error = PTR_ERR(dentry); 28501da177e4SLinus Torvalds if (!IS_ERR(dentry)) { 28511da177e4SLinus Torvalds /* Why not before? Because we want correct error value */ 285250338b88STörök Edwin if (nd.last.name[nd.last.len]) 285350338b88STörök Edwin goto slashes; 28541da177e4SLinus Torvalds inode = dentry->d_inode; 285550338b88STörök Edwin if (!inode) 2856e6bc45d6STheodore Ts'o goto slashes; 28577de9c6eeSAl Viro ihold(inode); 28580622753bSDave Hansen error = mnt_want_write(nd.path.mnt); 28590622753bSDave Hansen if (error) 28600622753bSDave Hansen goto exit2; 2861be6d3e56SKentaro Takeda error = security_path_unlink(&nd.path, dentry); 2862be6d3e56SKentaro Takeda if (error) 2863be6d3e56SKentaro Takeda goto exit3; 28644ac91378SJan Blunck error = vfs_unlink(nd.path.dentry->d_inode, dentry); 2865be6d3e56SKentaro Takeda exit3: 28660622753bSDave Hansen mnt_drop_write(nd.path.mnt); 28671da177e4SLinus Torvalds exit2: 28681da177e4SLinus Torvalds dput(dentry); 28691da177e4SLinus Torvalds } 28704ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 28711da177e4SLinus Torvalds if (inode) 28721da177e4SLinus Torvalds iput(inode); /* truncate the inode here */ 28731da177e4SLinus Torvalds exit1: 28741d957f9bSJan Blunck path_put(&nd.path); 28751da177e4SLinus Torvalds putname(name); 28761da177e4SLinus Torvalds return error; 28771da177e4SLinus Torvalds 28781da177e4SLinus Torvalds slashes: 28791da177e4SLinus Torvalds error = !dentry->d_inode ? -ENOENT : 28801da177e4SLinus Torvalds S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR; 28811da177e4SLinus Torvalds goto exit2; 28821da177e4SLinus Torvalds } 28831da177e4SLinus Torvalds 28842e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag) 28855590ff0dSUlrich Drepper { 28865590ff0dSUlrich Drepper if ((flag & ~AT_REMOVEDIR) != 0) 28875590ff0dSUlrich Drepper return -EINVAL; 28885590ff0dSUlrich Drepper 28895590ff0dSUlrich Drepper if (flag & AT_REMOVEDIR) 28905590ff0dSUlrich Drepper return do_rmdir(dfd, pathname); 28915590ff0dSUlrich Drepper 28925590ff0dSUlrich Drepper return do_unlinkat(dfd, pathname); 28935590ff0dSUlrich Drepper } 28945590ff0dSUlrich Drepper 28953480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname) 28965590ff0dSUlrich Drepper { 28975590ff0dSUlrich Drepper return do_unlinkat(AT_FDCWD, pathname); 28985590ff0dSUlrich Drepper } 28995590ff0dSUlrich Drepper 2900db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname) 29011da177e4SLinus Torvalds { 2902a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 29031da177e4SLinus Torvalds 29041da177e4SLinus Torvalds if (error) 29051da177e4SLinus Torvalds return error; 29061da177e4SLinus Torvalds 2907acfa4380SAl Viro if (!dir->i_op->symlink) 29081da177e4SLinus Torvalds return -EPERM; 29091da177e4SLinus Torvalds 29101da177e4SLinus Torvalds error = security_inode_symlink(dir, dentry, oldname); 29111da177e4SLinus Torvalds if (error) 29121da177e4SLinus Torvalds return error; 29131da177e4SLinus Torvalds 29141da177e4SLinus Torvalds error = dir->i_op->symlink(dir, dentry, oldname); 2915a74574aaSStephen Smalley if (!error) 2916f38aa942SAmy Griffis fsnotify_create(dir, dentry); 29171da177e4SLinus Torvalds return error; 29181da177e4SLinus Torvalds } 29191da177e4SLinus Torvalds 29202e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname, 29212e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 29221da177e4SLinus Torvalds { 29232ad94ae6SAl Viro int error; 29241da177e4SLinus Torvalds char *from; 29256902d925SDave Hansen struct dentry *dentry; 2926dae6ad8fSAl Viro struct path path; 29271da177e4SLinus Torvalds 29281da177e4SLinus Torvalds from = getname(oldname); 29291da177e4SLinus Torvalds if (IS_ERR(from)) 29301da177e4SLinus Torvalds return PTR_ERR(from); 29312ad94ae6SAl Viro 2932dae6ad8fSAl Viro dentry = user_path_create(newdfd, newname, &path, 0); 29331da177e4SLinus Torvalds error = PTR_ERR(dentry); 29346902d925SDave Hansen if (IS_ERR(dentry)) 2935dae6ad8fSAl Viro goto out_putname; 29366902d925SDave Hansen 2937dae6ad8fSAl Viro error = mnt_want_write(path.mnt); 293875c3f29dSDave Hansen if (error) 293975c3f29dSDave Hansen goto out_dput; 2940dae6ad8fSAl Viro error = security_path_symlink(&path, dentry, from); 2941be6d3e56SKentaro Takeda if (error) 2942be6d3e56SKentaro Takeda goto out_drop_write; 2943dae6ad8fSAl Viro error = vfs_symlink(path.dentry->d_inode, dentry, from); 2944be6d3e56SKentaro Takeda out_drop_write: 2945dae6ad8fSAl Viro mnt_drop_write(path.mnt); 294675c3f29dSDave Hansen out_dput: 29471da177e4SLinus Torvalds dput(dentry); 2948dae6ad8fSAl Viro mutex_unlock(&path.dentry->d_inode->i_mutex); 2949dae6ad8fSAl Viro path_put(&path); 29506902d925SDave Hansen out_putname: 29511da177e4SLinus Torvalds putname(from); 29521da177e4SLinus Torvalds return error; 29531da177e4SLinus Torvalds } 29541da177e4SLinus Torvalds 29553480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname) 29565590ff0dSUlrich Drepper { 29575590ff0dSUlrich Drepper return sys_symlinkat(oldname, AT_FDCWD, newname); 29585590ff0dSUlrich Drepper } 29595590ff0dSUlrich Drepper 29601da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) 29611da177e4SLinus Torvalds { 29621da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 29638de52778SAl Viro unsigned max_links = dir->i_sb->s_max_links; 29641da177e4SLinus Torvalds int error; 29651da177e4SLinus Torvalds 29661da177e4SLinus Torvalds if (!inode) 29671da177e4SLinus Torvalds return -ENOENT; 29681da177e4SLinus Torvalds 2969a95164d9SMiklos Szeredi error = may_create(dir, new_dentry); 29701da177e4SLinus Torvalds if (error) 29711da177e4SLinus Torvalds return error; 29721da177e4SLinus Torvalds 29731da177e4SLinus Torvalds if (dir->i_sb != inode->i_sb) 29741da177e4SLinus Torvalds return -EXDEV; 29751da177e4SLinus Torvalds 29761da177e4SLinus Torvalds /* 29771da177e4SLinus Torvalds * A link to an append-only or immutable file cannot be created. 29781da177e4SLinus Torvalds */ 29791da177e4SLinus Torvalds if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) 29801da177e4SLinus Torvalds return -EPERM; 2981acfa4380SAl Viro if (!dir->i_op->link) 29821da177e4SLinus Torvalds return -EPERM; 29837e79eedbSTetsuo Handa if (S_ISDIR(inode->i_mode)) 29841da177e4SLinus Torvalds return -EPERM; 29851da177e4SLinus Torvalds 29861da177e4SLinus Torvalds error = security_inode_link(old_dentry, dir, new_dentry); 29871da177e4SLinus Torvalds if (error) 29881da177e4SLinus Torvalds return error; 29891da177e4SLinus Torvalds 29907e79eedbSTetsuo Handa mutex_lock(&inode->i_mutex); 2991aae8a97dSAneesh Kumar K.V /* Make sure we don't allow creating hardlink to an unlinked file */ 2992aae8a97dSAneesh Kumar K.V if (inode->i_nlink == 0) 2993aae8a97dSAneesh Kumar K.V error = -ENOENT; 29948de52778SAl Viro else if (max_links && inode->i_nlink >= max_links) 29958de52778SAl Viro error = -EMLINK; 2996aae8a97dSAneesh Kumar K.V else 29971da177e4SLinus Torvalds error = dir->i_op->link(old_dentry, dir, new_dentry); 29987e79eedbSTetsuo Handa mutex_unlock(&inode->i_mutex); 2999e31e14ecSStephen Smalley if (!error) 30007e79eedbSTetsuo Handa fsnotify_link(dir, inode, new_dentry); 30011da177e4SLinus Torvalds return error; 30021da177e4SLinus Torvalds } 30031da177e4SLinus Torvalds 30041da177e4SLinus Torvalds /* 30051da177e4SLinus Torvalds * Hardlinks are often used in delicate situations. We avoid 30061da177e4SLinus Torvalds * security-related surprises by not following symlinks on the 30071da177e4SLinus Torvalds * newname. --KAB 30081da177e4SLinus Torvalds * 30091da177e4SLinus Torvalds * We don't follow them on the oldname either to be compatible 30101da177e4SLinus Torvalds * with linux 2.0, and to avoid hard-linking to directories 30111da177e4SLinus Torvalds * and other special files. --ADM 30121da177e4SLinus Torvalds */ 30132e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname, 30142e4d0924SHeiko Carstens int, newdfd, const char __user *, newname, int, flags) 30151da177e4SLinus Torvalds { 30161da177e4SLinus Torvalds struct dentry *new_dentry; 3017dae6ad8fSAl Viro struct path old_path, new_path; 301811a7b371SAneesh Kumar K.V int how = 0; 30191da177e4SLinus Torvalds int error; 30201da177e4SLinus Torvalds 302111a7b371SAneesh Kumar K.V if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0) 3022c04030e1SUlrich Drepper return -EINVAL; 302311a7b371SAneesh Kumar K.V /* 302411a7b371SAneesh Kumar K.V * To use null names we require CAP_DAC_READ_SEARCH 302511a7b371SAneesh Kumar K.V * This ensures that not everyone will be able to create 302611a7b371SAneesh Kumar K.V * handlink using the passed filedescriptor. 302711a7b371SAneesh Kumar K.V */ 302811a7b371SAneesh Kumar K.V if (flags & AT_EMPTY_PATH) { 302911a7b371SAneesh Kumar K.V if (!capable(CAP_DAC_READ_SEARCH)) 303011a7b371SAneesh Kumar K.V return -ENOENT; 303111a7b371SAneesh Kumar K.V how = LOOKUP_EMPTY; 303211a7b371SAneesh Kumar K.V } 3033c04030e1SUlrich Drepper 303411a7b371SAneesh Kumar K.V if (flags & AT_SYMLINK_FOLLOW) 303511a7b371SAneesh Kumar K.V how |= LOOKUP_FOLLOW; 303611a7b371SAneesh Kumar K.V 303711a7b371SAneesh Kumar K.V error = user_path_at(olddfd, oldname, how, &old_path); 30381da177e4SLinus Torvalds if (error) 30392ad94ae6SAl Viro return error; 30402ad94ae6SAl Viro 3041dae6ad8fSAl Viro new_dentry = user_path_create(newdfd, newname, &new_path, 0); 30421da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 30436902d925SDave Hansen if (IS_ERR(new_dentry)) 3044dae6ad8fSAl Viro goto out; 3045dae6ad8fSAl Viro 3046dae6ad8fSAl Viro error = -EXDEV; 3047dae6ad8fSAl Viro if (old_path.mnt != new_path.mnt) 3048dae6ad8fSAl Viro goto out_dput; 3049dae6ad8fSAl Viro error = mnt_want_write(new_path.mnt); 305075c3f29dSDave Hansen if (error) 305175c3f29dSDave Hansen goto out_dput; 3052dae6ad8fSAl Viro error = security_path_link(old_path.dentry, &new_path, new_dentry); 3053be6d3e56SKentaro Takeda if (error) 3054be6d3e56SKentaro Takeda goto out_drop_write; 3055dae6ad8fSAl Viro error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry); 3056be6d3e56SKentaro Takeda out_drop_write: 3057dae6ad8fSAl Viro mnt_drop_write(new_path.mnt); 305875c3f29dSDave Hansen out_dput: 30591da177e4SLinus Torvalds dput(new_dentry); 3060dae6ad8fSAl Viro mutex_unlock(&new_path.dentry->d_inode->i_mutex); 3061dae6ad8fSAl Viro path_put(&new_path); 30621da177e4SLinus Torvalds out: 30632d8f3038SAl Viro path_put(&old_path); 30641da177e4SLinus Torvalds 30651da177e4SLinus Torvalds return error; 30661da177e4SLinus Torvalds } 30671da177e4SLinus Torvalds 30683480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname) 30695590ff0dSUlrich Drepper { 3070c04030e1SUlrich Drepper return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0); 30715590ff0dSUlrich Drepper } 30725590ff0dSUlrich Drepper 30731da177e4SLinus Torvalds /* 30741da177e4SLinus Torvalds * The worst of all namespace operations - renaming directory. "Perverted" 30751da177e4SLinus Torvalds * doesn't even start to describe it. Somebody in UCB had a heck of a trip... 30761da177e4SLinus Torvalds * Problems: 30771da177e4SLinus Torvalds * a) we can get into loop creation. Check is done in is_subdir(). 30781da177e4SLinus Torvalds * b) race potential - two innocent renames can create a loop together. 30791da177e4SLinus Torvalds * That's where 4.4 screws up. Current fix: serialization on 3080a11f3a05SArjan van de Ven * sb->s_vfs_rename_mutex. We might be more accurate, but that's another 30811da177e4SLinus Torvalds * story. 30821da177e4SLinus Torvalds * c) we have to lock _three_ objects - parents and victim (if it exists). 30831b1dcc1bSJes Sorensen * And that - after we got ->i_mutex on parents (until then we don't know 30841da177e4SLinus Torvalds * whether the target exists). Solution: try to be smart with locking 30851da177e4SLinus Torvalds * order for inodes. We rely on the fact that tree topology may change 3086a11f3a05SArjan van de Ven * only under ->s_vfs_rename_mutex _and_ that parent of the object we 30871da177e4SLinus Torvalds * move will be locked. Thus we can rank directories by the tree 30881da177e4SLinus Torvalds * (ancestors first) and rank all non-directories after them. 30891da177e4SLinus Torvalds * That works since everybody except rename does "lock parent, lookup, 3090a11f3a05SArjan van de Ven * lock child" and rename is under ->s_vfs_rename_mutex. 30911da177e4SLinus Torvalds * HOWEVER, it relies on the assumption that any object with ->lookup() 30921da177e4SLinus Torvalds * has no more than 1 dentry. If "hybrid" objects will ever appear, 30931da177e4SLinus Torvalds * we'd better make sure that there's no link(2) for them. 3094e4eaac06SSage Weil * d) conversion from fhandle to dentry may come in the wrong moment - when 30951b1dcc1bSJes Sorensen * we are removing the target. Solution: we will have to grab ->i_mutex 30961da177e4SLinus Torvalds * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on 3097c41b20e7SAdam Buchbinder * ->i_mutex on parents, which works but leads to some truly excessive 30981da177e4SLinus Torvalds * locking]. 30991da177e4SLinus Torvalds */ 310075c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry, 31011da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 31021da177e4SLinus Torvalds { 31031da177e4SLinus Torvalds int error = 0; 31049055cba7SSage Weil struct inode *target = new_dentry->d_inode; 31058de52778SAl Viro unsigned max_links = new_dir->i_sb->s_max_links; 31061da177e4SLinus Torvalds 31071da177e4SLinus Torvalds /* 31081da177e4SLinus Torvalds * If we are going to change the parent - check write permissions, 31091da177e4SLinus Torvalds * we'll need to flip '..'. 31101da177e4SLinus Torvalds */ 31111da177e4SLinus Torvalds if (new_dir != old_dir) { 3112f419a2e3SAl Viro error = inode_permission(old_dentry->d_inode, MAY_WRITE); 31131da177e4SLinus Torvalds if (error) 31141da177e4SLinus Torvalds return error; 31151da177e4SLinus Torvalds } 31161da177e4SLinus Torvalds 31171da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 31181da177e4SLinus Torvalds if (error) 31191da177e4SLinus Torvalds return error; 31201da177e4SLinus Torvalds 31211d2ef590SAl Viro dget(new_dentry); 3122d83c49f3SAl Viro if (target) 31231b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 31249055cba7SSage Weil 31251da177e4SLinus Torvalds error = -EBUSY; 31269055cba7SSage Weil if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry)) 31279055cba7SSage Weil goto out; 31289055cba7SSage Weil 31298de52778SAl Viro error = -EMLINK; 31308de52778SAl Viro if (max_links && !target && new_dir != old_dir && 31318de52778SAl Viro new_dir->i_nlink >= max_links) 31328de52778SAl Viro goto out; 31338de52778SAl Viro 31343cebde24SSage Weil if (target) 31353cebde24SSage Weil shrink_dcache_parent(new_dentry); 31361da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 31379055cba7SSage Weil if (error) 31389055cba7SSage Weil goto out; 31399055cba7SSage Weil 31401da177e4SLinus Torvalds if (target) { 31411da177e4SLinus Torvalds target->i_flags |= S_DEAD; 3142d83c49f3SAl Viro dont_mount(new_dentry); 3143d83c49f3SAl Viro } 31449055cba7SSage Weil out: 31459055cba7SSage Weil if (target) 31461b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 31471d2ef590SAl Viro dput(new_dentry); 3148e31e14ecSStephen Smalley if (!error) 3149349457ccSMark Fasheh if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) 31501da177e4SLinus Torvalds d_move(old_dentry,new_dentry); 31511da177e4SLinus Torvalds return error; 31521da177e4SLinus Torvalds } 31531da177e4SLinus Torvalds 315475c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry, 31551da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 31561da177e4SLinus Torvalds { 315751892bbbSSage Weil struct inode *target = new_dentry->d_inode; 31581da177e4SLinus Torvalds int error; 31591da177e4SLinus Torvalds 31601da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 31611da177e4SLinus Torvalds if (error) 31621da177e4SLinus Torvalds return error; 31631da177e4SLinus Torvalds 31641da177e4SLinus Torvalds dget(new_dentry); 31651da177e4SLinus Torvalds if (target) 31661b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 316751892bbbSSage Weil 31681da177e4SLinus Torvalds error = -EBUSY; 316951892bbbSSage Weil if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry)) 317051892bbbSSage Weil goto out; 317151892bbbSSage Weil 31721da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 317351892bbbSSage Weil if (error) 317451892bbbSSage Weil goto out; 317551892bbbSSage Weil 3176bec1052eSAl Viro if (target) 3177d83c49f3SAl Viro dont_mount(new_dentry); 3178349457ccSMark Fasheh if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) 31791da177e4SLinus Torvalds d_move(old_dentry, new_dentry); 318051892bbbSSage Weil out: 31811da177e4SLinus Torvalds if (target) 31821b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 31831da177e4SLinus Torvalds dput(new_dentry); 31841da177e4SLinus Torvalds return error; 31851da177e4SLinus Torvalds } 31861da177e4SLinus Torvalds 31871da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry, 31881da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 31891da177e4SLinus Torvalds { 31901da177e4SLinus Torvalds int error; 31911da177e4SLinus Torvalds int is_dir = S_ISDIR(old_dentry->d_inode->i_mode); 319259b0df21SEric Paris const unsigned char *old_name; 31931da177e4SLinus Torvalds 31941da177e4SLinus Torvalds if (old_dentry->d_inode == new_dentry->d_inode) 31951da177e4SLinus Torvalds return 0; 31961da177e4SLinus Torvalds 31971da177e4SLinus Torvalds error = may_delete(old_dir, old_dentry, is_dir); 31981da177e4SLinus Torvalds if (error) 31991da177e4SLinus Torvalds return error; 32001da177e4SLinus Torvalds 32011da177e4SLinus Torvalds if (!new_dentry->d_inode) 3202a95164d9SMiklos Szeredi error = may_create(new_dir, new_dentry); 32031da177e4SLinus Torvalds else 32041da177e4SLinus Torvalds error = may_delete(new_dir, new_dentry, is_dir); 32051da177e4SLinus Torvalds if (error) 32061da177e4SLinus Torvalds return error; 32071da177e4SLinus Torvalds 3208acfa4380SAl Viro if (!old_dir->i_op->rename) 32091da177e4SLinus Torvalds return -EPERM; 32101da177e4SLinus Torvalds 32110eeca283SRobert Love old_name = fsnotify_oldname_init(old_dentry->d_name.name); 32120eeca283SRobert Love 32131da177e4SLinus Torvalds if (is_dir) 32141da177e4SLinus Torvalds error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry); 32151da177e4SLinus Torvalds else 32161da177e4SLinus Torvalds error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry); 3217123df294SAl Viro if (!error) 3218123df294SAl Viro fsnotify_move(old_dir, new_dir, old_name, is_dir, 32195a190ae6SAl Viro new_dentry->d_inode, old_dentry); 32200eeca283SRobert Love fsnotify_oldname_free(old_name); 32210eeca283SRobert Love 32221da177e4SLinus Torvalds return error; 32231da177e4SLinus Torvalds } 32241da177e4SLinus Torvalds 32252e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname, 32262e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 32271da177e4SLinus Torvalds { 32281da177e4SLinus Torvalds struct dentry *old_dir, *new_dir; 32291da177e4SLinus Torvalds struct dentry *old_dentry, *new_dentry; 32301da177e4SLinus Torvalds struct dentry *trap; 32311da177e4SLinus Torvalds struct nameidata oldnd, newnd; 32322ad94ae6SAl Viro char *from; 32332ad94ae6SAl Viro char *to; 32342ad94ae6SAl Viro int error; 32351da177e4SLinus Torvalds 32362ad94ae6SAl Viro error = user_path_parent(olddfd, oldname, &oldnd, &from); 32371da177e4SLinus Torvalds if (error) 32381da177e4SLinus Torvalds goto exit; 32391da177e4SLinus Torvalds 32402ad94ae6SAl Viro error = user_path_parent(newdfd, newname, &newnd, &to); 32411da177e4SLinus Torvalds if (error) 32421da177e4SLinus Torvalds goto exit1; 32431da177e4SLinus Torvalds 32441da177e4SLinus Torvalds error = -EXDEV; 32454ac91378SJan Blunck if (oldnd.path.mnt != newnd.path.mnt) 32461da177e4SLinus Torvalds goto exit2; 32471da177e4SLinus Torvalds 32484ac91378SJan Blunck old_dir = oldnd.path.dentry; 32491da177e4SLinus Torvalds error = -EBUSY; 32501da177e4SLinus Torvalds if (oldnd.last_type != LAST_NORM) 32511da177e4SLinus Torvalds goto exit2; 32521da177e4SLinus Torvalds 32534ac91378SJan Blunck new_dir = newnd.path.dentry; 32541da177e4SLinus Torvalds if (newnd.last_type != LAST_NORM) 32551da177e4SLinus Torvalds goto exit2; 32561da177e4SLinus Torvalds 32570612d9fbSOGAWA Hirofumi oldnd.flags &= ~LOOKUP_PARENT; 32580612d9fbSOGAWA Hirofumi newnd.flags &= ~LOOKUP_PARENT; 32594e9ed2f8SOGAWA Hirofumi newnd.flags |= LOOKUP_RENAME_TARGET; 32600612d9fbSOGAWA Hirofumi 32611da177e4SLinus Torvalds trap = lock_rename(new_dir, old_dir); 32621da177e4SLinus Torvalds 326349705b77SChristoph Hellwig old_dentry = lookup_hash(&oldnd); 32641da177e4SLinus Torvalds error = PTR_ERR(old_dentry); 32651da177e4SLinus Torvalds if (IS_ERR(old_dentry)) 32661da177e4SLinus Torvalds goto exit3; 32671da177e4SLinus Torvalds /* source must exist */ 32681da177e4SLinus Torvalds error = -ENOENT; 32691da177e4SLinus Torvalds if (!old_dentry->d_inode) 32701da177e4SLinus Torvalds goto exit4; 32711da177e4SLinus Torvalds /* unless the source is a directory trailing slashes give -ENOTDIR */ 32721da177e4SLinus Torvalds if (!S_ISDIR(old_dentry->d_inode->i_mode)) { 32731da177e4SLinus Torvalds error = -ENOTDIR; 32741da177e4SLinus Torvalds if (oldnd.last.name[oldnd.last.len]) 32751da177e4SLinus Torvalds goto exit4; 32761da177e4SLinus Torvalds if (newnd.last.name[newnd.last.len]) 32771da177e4SLinus Torvalds goto exit4; 32781da177e4SLinus Torvalds } 32791da177e4SLinus Torvalds /* source should not be ancestor of target */ 32801da177e4SLinus Torvalds error = -EINVAL; 32811da177e4SLinus Torvalds if (old_dentry == trap) 32821da177e4SLinus Torvalds goto exit4; 328349705b77SChristoph Hellwig new_dentry = lookup_hash(&newnd); 32841da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 32851da177e4SLinus Torvalds if (IS_ERR(new_dentry)) 32861da177e4SLinus Torvalds goto exit4; 32871da177e4SLinus Torvalds /* target should not be an ancestor of source */ 32881da177e4SLinus Torvalds error = -ENOTEMPTY; 32891da177e4SLinus Torvalds if (new_dentry == trap) 32901da177e4SLinus Torvalds goto exit5; 32911da177e4SLinus Torvalds 32929079b1ebSDave Hansen error = mnt_want_write(oldnd.path.mnt); 32939079b1ebSDave Hansen if (error) 32949079b1ebSDave Hansen goto exit5; 3295be6d3e56SKentaro Takeda error = security_path_rename(&oldnd.path, old_dentry, 3296be6d3e56SKentaro Takeda &newnd.path, new_dentry); 3297be6d3e56SKentaro Takeda if (error) 3298be6d3e56SKentaro Takeda goto exit6; 32991da177e4SLinus Torvalds error = vfs_rename(old_dir->d_inode, old_dentry, 33001da177e4SLinus Torvalds new_dir->d_inode, new_dentry); 3301be6d3e56SKentaro Takeda exit6: 33029079b1ebSDave Hansen mnt_drop_write(oldnd.path.mnt); 33031da177e4SLinus Torvalds exit5: 33041da177e4SLinus Torvalds dput(new_dentry); 33051da177e4SLinus Torvalds exit4: 33061da177e4SLinus Torvalds dput(old_dentry); 33071da177e4SLinus Torvalds exit3: 33081da177e4SLinus Torvalds unlock_rename(new_dir, old_dir); 33091da177e4SLinus Torvalds exit2: 33101d957f9bSJan Blunck path_put(&newnd.path); 33112ad94ae6SAl Viro putname(to); 33121da177e4SLinus Torvalds exit1: 33131d957f9bSJan Blunck path_put(&oldnd.path); 33141da177e4SLinus Torvalds putname(from); 33152ad94ae6SAl Viro exit: 33161da177e4SLinus Torvalds return error; 33171da177e4SLinus Torvalds } 33181da177e4SLinus Torvalds 3319a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname) 33205590ff0dSUlrich Drepper { 33215590ff0dSUlrich Drepper return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname); 33225590ff0dSUlrich Drepper } 33235590ff0dSUlrich Drepper 33241da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link) 33251da177e4SLinus Torvalds { 33261da177e4SLinus Torvalds int len; 33271da177e4SLinus Torvalds 33281da177e4SLinus Torvalds len = PTR_ERR(link); 33291da177e4SLinus Torvalds if (IS_ERR(link)) 33301da177e4SLinus Torvalds goto out; 33311da177e4SLinus Torvalds 33321da177e4SLinus Torvalds len = strlen(link); 33331da177e4SLinus Torvalds if (len > (unsigned) buflen) 33341da177e4SLinus Torvalds len = buflen; 33351da177e4SLinus Torvalds if (copy_to_user(buffer, link, len)) 33361da177e4SLinus Torvalds len = -EFAULT; 33371da177e4SLinus Torvalds out: 33381da177e4SLinus Torvalds return len; 33391da177e4SLinus Torvalds } 33401da177e4SLinus Torvalds 33411da177e4SLinus Torvalds /* 33421da177e4SLinus Torvalds * A helper for ->readlink(). This should be used *ONLY* for symlinks that 33431da177e4SLinus Torvalds * have ->follow_link() touching nd only in nd_set_link(). Using (or not 33441da177e4SLinus Torvalds * using) it for any given inode is up to filesystem. 33451da177e4SLinus Torvalds */ 33461da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen) 33471da177e4SLinus Torvalds { 33481da177e4SLinus Torvalds struct nameidata nd; 3349cc314eefSLinus Torvalds void *cookie; 3350694a1764SMarcin Slusarz int res; 3351cc314eefSLinus Torvalds 33521da177e4SLinus Torvalds nd.depth = 0; 3353cc314eefSLinus Torvalds cookie = dentry->d_inode->i_op->follow_link(dentry, &nd); 3354694a1764SMarcin Slusarz if (IS_ERR(cookie)) 3355694a1764SMarcin Slusarz return PTR_ERR(cookie); 3356694a1764SMarcin Slusarz 3357694a1764SMarcin Slusarz res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd)); 33581da177e4SLinus Torvalds if (dentry->d_inode->i_op->put_link) 3359cc314eefSLinus Torvalds dentry->d_inode->i_op->put_link(dentry, &nd, cookie); 3360694a1764SMarcin Slusarz return res; 33611da177e4SLinus Torvalds } 33621da177e4SLinus Torvalds 33631da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link) 33641da177e4SLinus Torvalds { 33651da177e4SLinus Torvalds return __vfs_follow_link(nd, link); 33661da177e4SLinus Torvalds } 33671da177e4SLinus Torvalds 33681da177e4SLinus Torvalds /* get the link contents into pagecache */ 33691da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage) 33701da177e4SLinus Torvalds { 3371ebd09abbSDuane Griffin char *kaddr; 33721da177e4SLinus Torvalds struct page *page; 33731da177e4SLinus Torvalds struct address_space *mapping = dentry->d_inode->i_mapping; 3374090d2b18SPekka Enberg page = read_mapping_page(mapping, 0, NULL); 33751da177e4SLinus Torvalds if (IS_ERR(page)) 33766fe6900eSNick Piggin return (char*)page; 33771da177e4SLinus Torvalds *ppage = page; 3378ebd09abbSDuane Griffin kaddr = kmap(page); 3379ebd09abbSDuane Griffin nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1); 3380ebd09abbSDuane Griffin return kaddr; 33811da177e4SLinus Torvalds } 33821da177e4SLinus Torvalds 33831da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen) 33841da177e4SLinus Torvalds { 33851da177e4SLinus Torvalds struct page *page = NULL; 33861da177e4SLinus Torvalds char *s = page_getlink(dentry, &page); 33871da177e4SLinus Torvalds int res = vfs_readlink(dentry,buffer,buflen,s); 33881da177e4SLinus Torvalds if (page) { 33891da177e4SLinus Torvalds kunmap(page); 33901da177e4SLinus Torvalds page_cache_release(page); 33911da177e4SLinus Torvalds } 33921da177e4SLinus Torvalds return res; 33931da177e4SLinus Torvalds } 33941da177e4SLinus Torvalds 3395cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd) 33961da177e4SLinus Torvalds { 3397cc314eefSLinus Torvalds struct page *page = NULL; 33981da177e4SLinus Torvalds nd_set_link(nd, page_getlink(dentry, &page)); 3399cc314eefSLinus Torvalds return page; 34001da177e4SLinus Torvalds } 34011da177e4SLinus Torvalds 3402cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie) 34031da177e4SLinus Torvalds { 3404cc314eefSLinus Torvalds struct page *page = cookie; 3405cc314eefSLinus Torvalds 3406cc314eefSLinus Torvalds if (page) { 34071da177e4SLinus Torvalds kunmap(page); 34081da177e4SLinus Torvalds page_cache_release(page); 34091da177e4SLinus Torvalds } 34101da177e4SLinus Torvalds } 34111da177e4SLinus Torvalds 341254566b2cSNick Piggin /* 341354566b2cSNick Piggin * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS 341454566b2cSNick Piggin */ 341554566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs) 34161da177e4SLinus Torvalds { 34171da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 34180adb25d2SKirill Korotaev struct page *page; 3419afddba49SNick Piggin void *fsdata; 3420beb497abSDmitriy Monakhov int err; 34211da177e4SLinus Torvalds char *kaddr; 342254566b2cSNick Piggin unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE; 342354566b2cSNick Piggin if (nofs) 342454566b2cSNick Piggin flags |= AOP_FLAG_NOFS; 34251da177e4SLinus Torvalds 34267e53cac4SNeilBrown retry: 3427afddba49SNick Piggin err = pagecache_write_begin(NULL, mapping, 0, len-1, 342854566b2cSNick Piggin flags, &page, &fsdata); 34291da177e4SLinus Torvalds if (err) 3430afddba49SNick Piggin goto fail; 3431afddba49SNick Piggin 3432e8e3c3d6SCong Wang kaddr = kmap_atomic(page); 34331da177e4SLinus Torvalds memcpy(kaddr, symname, len-1); 3434e8e3c3d6SCong Wang kunmap_atomic(kaddr); 3435afddba49SNick Piggin 3436afddba49SNick Piggin err = pagecache_write_end(NULL, mapping, 0, len-1, len-1, 3437afddba49SNick Piggin page, fsdata); 34381da177e4SLinus Torvalds if (err < 0) 34391da177e4SLinus Torvalds goto fail; 3440afddba49SNick Piggin if (err < len-1) 3441afddba49SNick Piggin goto retry; 3442afddba49SNick Piggin 34431da177e4SLinus Torvalds mark_inode_dirty(inode); 34441da177e4SLinus Torvalds return 0; 34451da177e4SLinus Torvalds fail: 34461da177e4SLinus Torvalds return err; 34471da177e4SLinus Torvalds } 34481da177e4SLinus Torvalds 34490adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len) 34500adb25d2SKirill Korotaev { 34510adb25d2SKirill Korotaev return __page_symlink(inode, symname, len, 345254566b2cSNick Piggin !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS)); 34530adb25d2SKirill Korotaev } 34540adb25d2SKirill Korotaev 345592e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = { 34561da177e4SLinus Torvalds .readlink = generic_readlink, 34571da177e4SLinus Torvalds .follow_link = page_follow_link_light, 34581da177e4SLinus Torvalds .put_link = page_put_link, 34591da177e4SLinus Torvalds }; 34601da177e4SLinus Torvalds 34612d8f3038SAl Viro EXPORT_SYMBOL(user_path_at); 3462cc53ce53SDavid Howells EXPORT_SYMBOL(follow_down_one); 34631da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down); 34641da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up); 34651da177e4SLinus Torvalds EXPORT_SYMBOL(get_write_access); /* binfmt_aout */ 34661da177e4SLinus Torvalds EXPORT_SYMBOL(getname); 34671da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename); 34681da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len); 34691da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light); 34701da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link); 34711da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink); 34720adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink); 34731da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink); 34741da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations); 3475d1811465SAl Viro EXPORT_SYMBOL(kern_path); 347616f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup); 3477f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission); 34781da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename); 34791da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create); 34801da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link); 34811da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link); 34821da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir); 34831da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod); 34841da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission); 34851da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink); 34861da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename); 34871da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir); 34881da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink); 34891da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink); 34901da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash); 34911da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink); 3492