11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/fs/namei.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 1991, 1992 Linus Torvalds 51da177e4SLinus Torvalds */ 61da177e4SLinus Torvalds 71da177e4SLinus Torvalds /* 81da177e4SLinus Torvalds * Some corrections by tytso. 91da177e4SLinus Torvalds */ 101da177e4SLinus Torvalds 111da177e4SLinus Torvalds /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname 121da177e4SLinus Torvalds * lookup logic. 131da177e4SLinus Torvalds */ 141da177e4SLinus Torvalds /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture. 151da177e4SLinus Torvalds */ 161da177e4SLinus Torvalds 171da177e4SLinus Torvalds #include <linux/init.h> 18630d9c47SPaul Gortmaker #include <linux/export.h> 1944696908SDavid S. Miller #include <linux/kernel.h> 201da177e4SLinus Torvalds #include <linux/slab.h> 211da177e4SLinus Torvalds #include <linux/fs.h> 221da177e4SLinus Torvalds #include <linux/namei.h> 231da177e4SLinus Torvalds #include <linux/pagemap.h> 240eeca283SRobert Love #include <linux/fsnotify.h> 251da177e4SLinus Torvalds #include <linux/personality.h> 261da177e4SLinus Torvalds #include <linux/security.h> 276146f0d5SMimi Zohar #include <linux/ima.h> 281da177e4SLinus Torvalds #include <linux/syscalls.h> 291da177e4SLinus Torvalds #include <linux/mount.h> 301da177e4SLinus Torvalds #include <linux/audit.h> 3116f7e0feSRandy Dunlap #include <linux/capability.h> 32834f2a4aSTrond Myklebust #include <linux/file.h> 335590ff0dSUlrich Drepper #include <linux/fcntl.h> 3408ce5f16SSerge E. Hallyn #include <linux/device_cgroup.h> 355ad4e53bSAl Viro #include <linux/fs_struct.h> 36e77819e5SLinus Torvalds #include <linux/posix_acl.h> 371da177e4SLinus Torvalds #include <asm/uaccess.h> 381da177e4SLinus Torvalds 39e81e3f4dSEric Paris #include "internal.h" 40c7105365SAl Viro #include "mount.h" 41e81e3f4dSEric Paris 421da177e4SLinus Torvalds /* [Feb-1997 T. Schoebel-Theuer] 431da177e4SLinus Torvalds * Fundamental changes in the pathname lookup mechanisms (namei) 441da177e4SLinus Torvalds * were necessary because of omirr. The reason is that omirr needs 451da177e4SLinus Torvalds * to know the _real_ pathname, not the user-supplied one, in case 461da177e4SLinus Torvalds * of symlinks (and also when transname replacements occur). 471da177e4SLinus Torvalds * 481da177e4SLinus Torvalds * The new code replaces the old recursive symlink resolution with 491da177e4SLinus Torvalds * an iterative one (in case of non-nested symlink chains). It does 501da177e4SLinus Torvalds * this with calls to <fs>_follow_link(). 511da177e4SLinus Torvalds * As a side effect, dir_namei(), _namei() and follow_link() are now 521da177e4SLinus Torvalds * replaced with a single function lookup_dentry() that can handle all 531da177e4SLinus Torvalds * the special cases of the former code. 541da177e4SLinus Torvalds * 551da177e4SLinus Torvalds * With the new dcache, the pathname is stored at each inode, at least as 561da177e4SLinus Torvalds * long as the refcount of the inode is positive. As a side effect, the 571da177e4SLinus Torvalds * size of the dcache depends on the inode cache and thus is dynamic. 581da177e4SLinus Torvalds * 591da177e4SLinus Torvalds * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink 601da177e4SLinus Torvalds * resolution to correspond with current state of the code. 611da177e4SLinus Torvalds * 621da177e4SLinus Torvalds * Note that the symlink resolution is not *completely* iterative. 631da177e4SLinus Torvalds * There is still a significant amount of tail- and mid- recursion in 641da177e4SLinus Torvalds * the algorithm. Also, note that <fs>_readlink() is not used in 651da177e4SLinus Torvalds * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink() 661da177e4SLinus Torvalds * may return different results than <fs>_follow_link(). Many virtual 671da177e4SLinus Torvalds * filesystems (including /proc) exhibit this behavior. 681da177e4SLinus Torvalds */ 691da177e4SLinus Torvalds 701da177e4SLinus Torvalds /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation: 711da177e4SLinus Torvalds * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL 721da177e4SLinus Torvalds * and the name already exists in form of a symlink, try to create the new 731da177e4SLinus Torvalds * name indicated by the symlink. The old code always complained that the 741da177e4SLinus Torvalds * name already exists, due to not following the symlink even if its target 751da177e4SLinus Torvalds * is nonexistent. The new semantics affects also mknod() and link() when 7625985edcSLucas De Marchi * the name is a symlink pointing to a non-existent name. 771da177e4SLinus Torvalds * 781da177e4SLinus Torvalds * I don't know which semantics is the right one, since I have no access 791da177e4SLinus Torvalds * to standards. But I found by trial that HP-UX 9.0 has the full "new" 801da177e4SLinus Torvalds * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the 811da177e4SLinus Torvalds * "old" one. Personally, I think the new semantics is much more logical. 821da177e4SLinus Torvalds * Note that "ln old new" where "new" is a symlink pointing to a non-existing 831da177e4SLinus Torvalds * file does succeed in both HP-UX and SunOs, but not in Solaris 841da177e4SLinus Torvalds * and in the old Linux semantics. 851da177e4SLinus Torvalds */ 861da177e4SLinus Torvalds 871da177e4SLinus Torvalds /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink 881da177e4SLinus Torvalds * semantics. See the comments in "open_namei" and "do_link" below. 891da177e4SLinus Torvalds * 901da177e4SLinus Torvalds * [10-Sep-98 Alan Modra] Another symlink change. 911da177e4SLinus Torvalds */ 921da177e4SLinus Torvalds 931da177e4SLinus Torvalds /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks: 941da177e4SLinus Torvalds * inside the path - always follow. 951da177e4SLinus Torvalds * in the last component in creation/removal/renaming - never follow. 961da177e4SLinus Torvalds * if LOOKUP_FOLLOW passed - follow. 971da177e4SLinus Torvalds * if the pathname has trailing slashes - follow. 981da177e4SLinus Torvalds * otherwise - don't follow. 991da177e4SLinus Torvalds * (applied in that order). 1001da177e4SLinus Torvalds * 1011da177e4SLinus Torvalds * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT 1021da177e4SLinus Torvalds * restored for 2.4. This is the last surviving part of old 4.2BSD bug. 1031da177e4SLinus Torvalds * During the 2.4 we need to fix the userland stuff depending on it - 1041da177e4SLinus Torvalds * hopefully we will be able to get rid of that wart in 2.5. So far only 1051da177e4SLinus Torvalds * XEmacs seems to be relying on it... 1061da177e4SLinus Torvalds */ 1071da177e4SLinus Torvalds /* 1081da177e4SLinus Torvalds * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland) 109a11f3a05SArjan van de Ven * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives 1101da177e4SLinus Torvalds * any extra contention... 1111da177e4SLinus Torvalds */ 1121da177e4SLinus Torvalds 1131da177e4SLinus Torvalds /* In order to reduce some races, while at the same time doing additional 1141da177e4SLinus Torvalds * checking and hopefully speeding things up, we copy filenames to the 1151da177e4SLinus Torvalds * kernel data space before using them.. 1161da177e4SLinus Torvalds * 1171da177e4SLinus Torvalds * POSIX.1 2.4: an empty pathname is invalid (ENOENT). 1181da177e4SLinus Torvalds * PATH_MAX includes the nul terminator --RR. 1191da177e4SLinus Torvalds */ 1201fa1e7f6SAndy Whitcroft static char *getname_flags(const char __user *filename, int flags, int *empty) 1211da177e4SLinus Torvalds { 1223f9f0aa6SLinus Torvalds char *result = __getname(), *err; 1233f9f0aa6SLinus Torvalds int len; 1241da177e4SLinus Torvalds 1253f9f0aa6SLinus Torvalds if (unlikely(!result)) 1264043cde8SEric Paris return ERR_PTR(-ENOMEM); 1271da177e4SLinus Torvalds 1283f9f0aa6SLinus Torvalds len = strncpy_from_user(result, filename, PATH_MAX); 1293f9f0aa6SLinus Torvalds err = ERR_PTR(len); 1303f9f0aa6SLinus Torvalds if (unlikely(len < 0)) 1313f9f0aa6SLinus Torvalds goto error; 1323f9f0aa6SLinus Torvalds 1333f9f0aa6SLinus Torvalds /* The empty path is special. */ 1343f9f0aa6SLinus Torvalds if (unlikely(!len)) { 1353f9f0aa6SLinus Torvalds if (empty) 1361fa1e7f6SAndy Whitcroft *empty = 1; 1373f9f0aa6SLinus Torvalds err = ERR_PTR(-ENOENT); 1383f9f0aa6SLinus Torvalds if (!(flags & LOOKUP_EMPTY)) 1393f9f0aa6SLinus Torvalds goto error; 1401da177e4SLinus Torvalds } 1413f9f0aa6SLinus Torvalds 1423f9f0aa6SLinus Torvalds err = ERR_PTR(-ENAMETOOLONG); 1433f9f0aa6SLinus Torvalds if (likely(len < PATH_MAX)) { 1441da177e4SLinus Torvalds audit_getname(result); 1451da177e4SLinus Torvalds return result; 1461da177e4SLinus Torvalds } 1471da177e4SLinus Torvalds 1483f9f0aa6SLinus Torvalds error: 1493f9f0aa6SLinus Torvalds __putname(result); 1503f9f0aa6SLinus Torvalds return err; 1513f9f0aa6SLinus Torvalds } 1523f9f0aa6SLinus Torvalds 153f52e0c11SAl Viro char *getname(const char __user * filename) 154f52e0c11SAl Viro { 155f7493e5dSLinus Torvalds return getname_flags(filename, 0, NULL); 156f52e0c11SAl Viro } 157f52e0c11SAl Viro 1581da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL 1591da177e4SLinus Torvalds void putname(const char *name) 1601da177e4SLinus Torvalds { 1615ac3a9c2SAl Viro if (unlikely(!audit_dummy_context())) 1621da177e4SLinus Torvalds audit_putname(name); 1631da177e4SLinus Torvalds else 1641da177e4SLinus Torvalds __putname(name); 1651da177e4SLinus Torvalds } 1661da177e4SLinus Torvalds EXPORT_SYMBOL(putname); 1671da177e4SLinus Torvalds #endif 1681da177e4SLinus Torvalds 169e77819e5SLinus Torvalds static int check_acl(struct inode *inode, int mask) 170e77819e5SLinus Torvalds { 17184635d68SLinus Torvalds #ifdef CONFIG_FS_POSIX_ACL 172e77819e5SLinus Torvalds struct posix_acl *acl; 173e77819e5SLinus Torvalds 174e77819e5SLinus Torvalds if (mask & MAY_NOT_BLOCK) { 1753567866bSAl Viro acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS); 1763567866bSAl Viro if (!acl) 177e77819e5SLinus Torvalds return -EAGAIN; 1783567866bSAl Viro /* no ->get_acl() calls in RCU mode... */ 1793567866bSAl Viro if (acl == ACL_NOT_CACHED) 180e77819e5SLinus Torvalds return -ECHILD; 181206b1d09SAri Savolainen return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK); 182e77819e5SLinus Torvalds } 183e77819e5SLinus Torvalds 184e77819e5SLinus Torvalds acl = get_cached_acl(inode, ACL_TYPE_ACCESS); 185e77819e5SLinus Torvalds 186e77819e5SLinus Torvalds /* 1874e34e719SChristoph Hellwig * A filesystem can force a ACL callback by just never filling the 1884e34e719SChristoph Hellwig * ACL cache. But normally you'd fill the cache either at inode 1894e34e719SChristoph Hellwig * instantiation time, or on the first ->get_acl call. 190e77819e5SLinus Torvalds * 1914e34e719SChristoph Hellwig * If the filesystem doesn't have a get_acl() function at all, we'll 1924e34e719SChristoph Hellwig * just create the negative cache entry. 193e77819e5SLinus Torvalds */ 194e77819e5SLinus Torvalds if (acl == ACL_NOT_CACHED) { 1954e34e719SChristoph Hellwig if (inode->i_op->get_acl) { 1964e34e719SChristoph Hellwig acl = inode->i_op->get_acl(inode, ACL_TYPE_ACCESS); 1974e34e719SChristoph Hellwig if (IS_ERR(acl)) 1984e34e719SChristoph Hellwig return PTR_ERR(acl); 1994e34e719SChristoph Hellwig } else { 200e77819e5SLinus Torvalds set_cached_acl(inode, ACL_TYPE_ACCESS, NULL); 201e77819e5SLinus Torvalds return -EAGAIN; 202e77819e5SLinus Torvalds } 2034e34e719SChristoph Hellwig } 204e77819e5SLinus Torvalds 205e77819e5SLinus Torvalds if (acl) { 206e77819e5SLinus Torvalds int error = posix_acl_permission(inode, acl, mask); 207e77819e5SLinus Torvalds posix_acl_release(acl); 208e77819e5SLinus Torvalds return error; 209e77819e5SLinus Torvalds } 21084635d68SLinus Torvalds #endif 211e77819e5SLinus Torvalds 212e77819e5SLinus Torvalds return -EAGAIN; 213e77819e5SLinus Torvalds } 214e77819e5SLinus Torvalds 2155909ccaaSLinus Torvalds /* 216948409c7SAndreas Gruenbacher * This does the basic permission checking 2175909ccaaSLinus Torvalds */ 2187e40145eSAl Viro static int acl_permission_check(struct inode *inode, int mask) 2195909ccaaSLinus Torvalds { 22026cf46beSLinus Torvalds unsigned int mode = inode->i_mode; 2215909ccaaSLinus Torvalds 222e795b717SSerge E. Hallyn if (current_user_ns() != inode_userns(inode)) 223e795b717SSerge E. Hallyn goto other_perms; 224e795b717SSerge E. Hallyn 22514067ff5SLinus Torvalds if (likely(current_fsuid() == inode->i_uid)) 2265909ccaaSLinus Torvalds mode >>= 6; 2275909ccaaSLinus Torvalds else { 228e77819e5SLinus Torvalds if (IS_POSIXACL(inode) && (mode & S_IRWXG)) { 2297e40145eSAl Viro int error = check_acl(inode, mask); 2305909ccaaSLinus Torvalds if (error != -EAGAIN) 2315909ccaaSLinus Torvalds return error; 2325909ccaaSLinus Torvalds } 2335909ccaaSLinus Torvalds 2345909ccaaSLinus Torvalds if (in_group_p(inode->i_gid)) 2355909ccaaSLinus Torvalds mode >>= 3; 2365909ccaaSLinus Torvalds } 2375909ccaaSLinus Torvalds 238e795b717SSerge E. Hallyn other_perms: 2395909ccaaSLinus Torvalds /* 2405909ccaaSLinus Torvalds * If the DACs are ok we don't need any capability check. 2415909ccaaSLinus Torvalds */ 2429c2c7039SAl Viro if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0) 2435909ccaaSLinus Torvalds return 0; 2445909ccaaSLinus Torvalds return -EACCES; 2455909ccaaSLinus Torvalds } 2461da177e4SLinus Torvalds 2471da177e4SLinus Torvalds /** 2481da177e4SLinus Torvalds * generic_permission - check for access rights on a Posix-like filesystem 2491da177e4SLinus Torvalds * @inode: inode to check access rights for 2508fd90c8dSAndreas Gruenbacher * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...) 2511da177e4SLinus Torvalds * 2521da177e4SLinus Torvalds * Used to check for read/write/execute permissions on a file. 2531da177e4SLinus Torvalds * We use "fsuid" for this, letting us set arbitrary permissions 2541da177e4SLinus Torvalds * for filesystem access without changing the "normal" uids which 255b74c79e9SNick Piggin * are used for other things. 256b74c79e9SNick Piggin * 257b74c79e9SNick Piggin * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk 258b74c79e9SNick Piggin * request cannot be satisfied (eg. requires blocking or too much complexity). 259b74c79e9SNick Piggin * It would then be called again in ref-walk mode. 2601da177e4SLinus Torvalds */ 2612830ba7fSAl Viro int generic_permission(struct inode *inode, int mask) 2621da177e4SLinus Torvalds { 2635909ccaaSLinus Torvalds int ret; 2641da177e4SLinus Torvalds 2651da177e4SLinus Torvalds /* 266948409c7SAndreas Gruenbacher * Do the basic permission checks. 2671da177e4SLinus Torvalds */ 2687e40145eSAl Viro ret = acl_permission_check(inode, mask); 2695909ccaaSLinus Torvalds if (ret != -EACCES) 2705909ccaaSLinus Torvalds return ret; 2711da177e4SLinus Torvalds 272d594e7ecSAl Viro if (S_ISDIR(inode->i_mode)) { 273d594e7ecSAl Viro /* DACs are overridable for directories */ 274d594e7ecSAl Viro if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE)) 275d594e7ecSAl Viro return 0; 276d594e7ecSAl Viro if (!(mask & MAY_WRITE)) 277d594e7ecSAl Viro if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH)) 278d594e7ecSAl Viro return 0; 279d594e7ecSAl Viro return -EACCES; 280d594e7ecSAl Viro } 2811da177e4SLinus Torvalds /* 2821da177e4SLinus Torvalds * Read/write DACs are always overridable. 283d594e7ecSAl Viro * Executable DACs are overridable when there is 284d594e7ecSAl Viro * at least one exec bit set. 2851da177e4SLinus Torvalds */ 286d594e7ecSAl Viro if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO)) 287e795b717SSerge E. Hallyn if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE)) 2881da177e4SLinus Torvalds return 0; 2891da177e4SLinus Torvalds 2901da177e4SLinus Torvalds /* 2911da177e4SLinus Torvalds * Searching includes executable on directories, else just read. 2921da177e4SLinus Torvalds */ 2937ea66001SSerge E. Hallyn mask &= MAY_READ | MAY_WRITE | MAY_EXEC; 294d594e7ecSAl Viro if (mask == MAY_READ) 295e795b717SSerge E. Hallyn if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH)) 2961da177e4SLinus Torvalds return 0; 2971da177e4SLinus Torvalds 2981da177e4SLinus Torvalds return -EACCES; 2991da177e4SLinus Torvalds } 3001da177e4SLinus Torvalds 3013ddcd056SLinus Torvalds /* 3023ddcd056SLinus Torvalds * We _really_ want to just do "generic_permission()" without 3033ddcd056SLinus Torvalds * even looking at the inode->i_op values. So we keep a cache 3043ddcd056SLinus Torvalds * flag in inode->i_opflags, that says "this has not special 3053ddcd056SLinus Torvalds * permission function, use the fast case". 3063ddcd056SLinus Torvalds */ 3073ddcd056SLinus Torvalds static inline int do_inode_permission(struct inode *inode, int mask) 3083ddcd056SLinus Torvalds { 3093ddcd056SLinus Torvalds if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) { 3103ddcd056SLinus Torvalds if (likely(inode->i_op->permission)) 3113ddcd056SLinus Torvalds return inode->i_op->permission(inode, mask); 3123ddcd056SLinus Torvalds 3133ddcd056SLinus Torvalds /* This gets set once for the inode lifetime */ 3143ddcd056SLinus Torvalds spin_lock(&inode->i_lock); 3153ddcd056SLinus Torvalds inode->i_opflags |= IOP_FASTPERM; 3163ddcd056SLinus Torvalds spin_unlock(&inode->i_lock); 3173ddcd056SLinus Torvalds } 3183ddcd056SLinus Torvalds return generic_permission(inode, mask); 3193ddcd056SLinus Torvalds } 3203ddcd056SLinus Torvalds 321cb23beb5SChristoph Hellwig /** 322cb23beb5SChristoph Hellwig * inode_permission - check for access rights to a given inode 323cb23beb5SChristoph Hellwig * @inode: inode to check permission on 3248fd90c8dSAndreas Gruenbacher * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...) 325cb23beb5SChristoph Hellwig * 326cb23beb5SChristoph Hellwig * Used to check for read/write/execute permissions on an inode. 327cb23beb5SChristoph Hellwig * We use "fsuid" for this, letting us set arbitrary permissions 328cb23beb5SChristoph Hellwig * for filesystem access without changing the "normal" uids which 329cb23beb5SChristoph Hellwig * are used for other things. 330948409c7SAndreas Gruenbacher * 331948409c7SAndreas Gruenbacher * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask. 332cb23beb5SChristoph Hellwig */ 333f419a2e3SAl Viro int inode_permission(struct inode *inode, int mask) 3341da177e4SLinus Torvalds { 335e6305c43SAl Viro int retval; 3361da177e4SLinus Torvalds 3373ddcd056SLinus Torvalds if (unlikely(mask & MAY_WRITE)) { 33822590e41SMiklos Szeredi umode_t mode = inode->i_mode; 3391da177e4SLinus Torvalds 3401da177e4SLinus Torvalds /* 3411da177e4SLinus Torvalds * Nobody gets write access to a read-only fs. 3421da177e4SLinus Torvalds */ 3431da177e4SLinus Torvalds if (IS_RDONLY(inode) && 3441da177e4SLinus Torvalds (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) 3451da177e4SLinus Torvalds return -EROFS; 3461da177e4SLinus Torvalds 3471da177e4SLinus Torvalds /* 3481da177e4SLinus Torvalds * Nobody gets write access to an immutable file. 3491da177e4SLinus Torvalds */ 3501da177e4SLinus Torvalds if (IS_IMMUTABLE(inode)) 3511da177e4SLinus Torvalds return -EACCES; 3521da177e4SLinus Torvalds } 3531da177e4SLinus Torvalds 3543ddcd056SLinus Torvalds retval = do_inode_permission(inode, mask); 3551da177e4SLinus Torvalds if (retval) 3561da177e4SLinus Torvalds return retval; 3571da177e4SLinus Torvalds 35808ce5f16SSerge E. Hallyn retval = devcgroup_inode_permission(inode, mask); 35908ce5f16SSerge E. Hallyn if (retval) 36008ce5f16SSerge E. Hallyn return retval; 36108ce5f16SSerge E. Hallyn 362d09ca739SEric Paris return security_inode_permission(inode, mask); 3631da177e4SLinus Torvalds } 3641da177e4SLinus Torvalds 365f4d6ff89SAl Viro /** 3665dd784d0SJan Blunck * path_get - get a reference to a path 3675dd784d0SJan Blunck * @path: path to get the reference to 3685dd784d0SJan Blunck * 3695dd784d0SJan Blunck * Given a path increment the reference count to the dentry and the vfsmount. 3705dd784d0SJan Blunck */ 3715dd784d0SJan Blunck void path_get(struct path *path) 3725dd784d0SJan Blunck { 3735dd784d0SJan Blunck mntget(path->mnt); 3745dd784d0SJan Blunck dget(path->dentry); 3755dd784d0SJan Blunck } 3765dd784d0SJan Blunck EXPORT_SYMBOL(path_get); 3775dd784d0SJan Blunck 3785dd784d0SJan Blunck /** 3791d957f9bSJan Blunck * path_put - put a reference to a path 3801d957f9bSJan Blunck * @path: path to put the reference to 3811d957f9bSJan Blunck * 3821d957f9bSJan Blunck * Given a path decrement the reference count to the dentry and the vfsmount. 3831d957f9bSJan Blunck */ 3841d957f9bSJan Blunck void path_put(struct path *path) 3851da177e4SLinus Torvalds { 3861d957f9bSJan Blunck dput(path->dentry); 3871d957f9bSJan Blunck mntput(path->mnt); 3881da177e4SLinus Torvalds } 3891d957f9bSJan Blunck EXPORT_SYMBOL(path_put); 3901da177e4SLinus Torvalds 39119660af7SAl Viro /* 39231e6b01fSNick Piggin * Path walking has 2 modes, rcu-walk and ref-walk (see 39319660af7SAl Viro * Documentation/filesystems/path-lookup.txt). In situations when we can't 39419660af7SAl Viro * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab 39519660af7SAl Viro * normal reference counts on dentries and vfsmounts to transition to rcu-walk 39619660af7SAl Viro * mode. Refcounts are grabbed at the last known good point before rcu-walk 39719660af7SAl Viro * got stuck, so ref-walk may continue from there. If this is not successful 39819660af7SAl Viro * (eg. a seqcount has changed), then failure is returned and it's up to caller 39919660af7SAl Viro * to restart the path walk from the beginning in ref-walk mode. 40031e6b01fSNick Piggin */ 40131e6b01fSNick Piggin 40231e6b01fSNick Piggin /** 40319660af7SAl Viro * unlazy_walk - try to switch to ref-walk mode. 40419660af7SAl Viro * @nd: nameidata pathwalk data 40519660af7SAl Viro * @dentry: child of nd->path.dentry or NULL 40639191628SRandy Dunlap * Returns: 0 on success, -ECHILD on failure 40731e6b01fSNick Piggin * 40819660af7SAl Viro * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry 40919660af7SAl Viro * for ref-walk mode. @dentry must be a path found by a do_lookup call on 41019660af7SAl Viro * @nd or NULL. Must be called from rcu-walk context. 41131e6b01fSNick Piggin */ 41219660af7SAl Viro static int unlazy_walk(struct nameidata *nd, struct dentry *dentry) 41331e6b01fSNick Piggin { 41431e6b01fSNick Piggin struct fs_struct *fs = current->fs; 41531e6b01fSNick Piggin struct dentry *parent = nd->path.dentry; 4165b6ca027SAl Viro int want_root = 0; 41731e6b01fSNick Piggin 41831e6b01fSNick Piggin BUG_ON(!(nd->flags & LOOKUP_RCU)); 4195b6ca027SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) { 4205b6ca027SAl Viro want_root = 1; 42131e6b01fSNick Piggin spin_lock(&fs->lock); 42231e6b01fSNick Piggin if (nd->root.mnt != fs->root.mnt || 42331e6b01fSNick Piggin nd->root.dentry != fs->root.dentry) 42431e6b01fSNick Piggin goto err_root; 42531e6b01fSNick Piggin } 42631e6b01fSNick Piggin spin_lock(&parent->d_lock); 42719660af7SAl Viro if (!dentry) { 42819660af7SAl Viro if (!__d_rcu_to_refcount(parent, nd->seq)) 42919660af7SAl Viro goto err_parent; 43019660af7SAl Viro BUG_ON(nd->inode != parent->d_inode); 43119660af7SAl Viro } else { 43294c0d4ecSAl Viro if (dentry->d_parent != parent) 43394c0d4ecSAl Viro goto err_parent; 43431e6b01fSNick Piggin spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); 43531e6b01fSNick Piggin if (!__d_rcu_to_refcount(dentry, nd->seq)) 43619660af7SAl Viro goto err_child; 43731e6b01fSNick Piggin /* 43819660af7SAl Viro * If the sequence check on the child dentry passed, then 43919660af7SAl Viro * the child has not been removed from its parent. This 44019660af7SAl Viro * means the parent dentry must be valid and able to take 44119660af7SAl Viro * a reference at this point. 44231e6b01fSNick Piggin */ 44331e6b01fSNick Piggin BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent); 44431e6b01fSNick Piggin BUG_ON(!parent->d_count); 44531e6b01fSNick Piggin parent->d_count++; 44631e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 44719660af7SAl Viro } 44831e6b01fSNick Piggin spin_unlock(&parent->d_lock); 4495b6ca027SAl Viro if (want_root) { 45031e6b01fSNick Piggin path_get(&nd->root); 45131e6b01fSNick Piggin spin_unlock(&fs->lock); 45231e6b01fSNick Piggin } 45331e6b01fSNick Piggin mntget(nd->path.mnt); 45431e6b01fSNick Piggin 45531e6b01fSNick Piggin rcu_read_unlock(); 45631e6b01fSNick Piggin br_read_unlock(vfsmount_lock); 45731e6b01fSNick Piggin nd->flags &= ~LOOKUP_RCU; 45831e6b01fSNick Piggin return 0; 45919660af7SAl Viro 46019660af7SAl Viro err_child: 46131e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 46219660af7SAl Viro err_parent: 46331e6b01fSNick Piggin spin_unlock(&parent->d_lock); 46431e6b01fSNick Piggin err_root: 4655b6ca027SAl Viro if (want_root) 46631e6b01fSNick Piggin spin_unlock(&fs->lock); 46731e6b01fSNick Piggin return -ECHILD; 46831e6b01fSNick Piggin } 46931e6b01fSNick Piggin 47031e6b01fSNick Piggin /** 471834f2a4aSTrond Myklebust * release_open_intent - free up open intent resources 472834f2a4aSTrond Myklebust * @nd: pointer to nameidata 473834f2a4aSTrond Myklebust */ 474834f2a4aSTrond Myklebust void release_open_intent(struct nameidata *nd) 475834f2a4aSTrond Myklebust { 4762dab5974SLinus Torvalds struct file *file = nd->intent.open.file; 4772dab5974SLinus Torvalds 4782dab5974SLinus Torvalds if (file && !IS_ERR(file)) { 4792dab5974SLinus Torvalds if (file->f_path.dentry == NULL) 4802dab5974SLinus Torvalds put_filp(file); 481834f2a4aSTrond Myklebust else 4822dab5974SLinus Torvalds fput(file); 4832dab5974SLinus Torvalds } 484834f2a4aSTrond Myklebust } 485834f2a4aSTrond Myklebust 486f60aef7eSAl Viro static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd) 48734286d66SNick Piggin { 488f60aef7eSAl Viro return dentry->d_op->d_revalidate(dentry, nd); 48934286d66SNick Piggin } 49034286d66SNick Piggin 4919f1fafeeSAl Viro /** 4929f1fafeeSAl Viro * complete_walk - successful completion of path walk 4939f1fafeeSAl Viro * @nd: pointer nameidata 49439159de2SJeff Layton * 4959f1fafeeSAl Viro * If we had been in RCU mode, drop out of it and legitimize nd->path. 4969f1fafeeSAl Viro * Revalidate the final result, unless we'd already done that during 4979f1fafeeSAl Viro * the path walk or the filesystem doesn't ask for it. Return 0 on 4989f1fafeeSAl Viro * success, -error on failure. In case of failure caller does not 4999f1fafeeSAl Viro * need to drop nd->path. 50039159de2SJeff Layton */ 5019f1fafeeSAl Viro static int complete_walk(struct nameidata *nd) 50239159de2SJeff Layton { 50316c2cd71SAl Viro struct dentry *dentry = nd->path.dentry; 50439159de2SJeff Layton int status; 50539159de2SJeff Layton 5069f1fafeeSAl Viro if (nd->flags & LOOKUP_RCU) { 5079f1fafeeSAl Viro nd->flags &= ~LOOKUP_RCU; 5089f1fafeeSAl Viro if (!(nd->flags & LOOKUP_ROOT)) 5099f1fafeeSAl Viro nd->root.mnt = NULL; 5109f1fafeeSAl Viro spin_lock(&dentry->d_lock); 5119f1fafeeSAl Viro if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) { 5129f1fafeeSAl Viro spin_unlock(&dentry->d_lock); 5139f1fafeeSAl Viro rcu_read_unlock(); 5149f1fafeeSAl Viro br_read_unlock(vfsmount_lock); 5159f1fafeeSAl Viro return -ECHILD; 5169f1fafeeSAl Viro } 5179f1fafeeSAl Viro BUG_ON(nd->inode != dentry->d_inode); 5189f1fafeeSAl Viro spin_unlock(&dentry->d_lock); 5199f1fafeeSAl Viro mntget(nd->path.mnt); 5209f1fafeeSAl Viro rcu_read_unlock(); 5219f1fafeeSAl Viro br_read_unlock(vfsmount_lock); 5229f1fafeeSAl Viro } 5239f1fafeeSAl Viro 52416c2cd71SAl Viro if (likely(!(nd->flags & LOOKUP_JUMPED))) 52539159de2SJeff Layton return 0; 52639159de2SJeff Layton 52716c2cd71SAl Viro if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE))) 52816c2cd71SAl Viro return 0; 52916c2cd71SAl Viro 53016c2cd71SAl Viro if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT))) 53116c2cd71SAl Viro return 0; 53216c2cd71SAl Viro 53316c2cd71SAl Viro /* Note: we do not d_invalidate() */ 53434286d66SNick Piggin status = d_revalidate(dentry, nd); 53539159de2SJeff Layton if (status > 0) 53639159de2SJeff Layton return 0; 53739159de2SJeff Layton 53816c2cd71SAl Viro if (!status) 53939159de2SJeff Layton status = -ESTALE; 54016c2cd71SAl Viro 5419f1fafeeSAl Viro path_put(&nd->path); 54239159de2SJeff Layton return status; 54339159de2SJeff Layton } 54439159de2SJeff Layton 5452a737871SAl Viro static __always_inline void set_root(struct nameidata *nd) 5462a737871SAl Viro { 547f7ad3c6bSMiklos Szeredi if (!nd->root.mnt) 548f7ad3c6bSMiklos Szeredi get_fs_root(current->fs, &nd->root); 5492a737871SAl Viro } 5502a737871SAl Viro 5516de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *); 5526de88d72SAl Viro 55331e6b01fSNick Piggin static __always_inline void set_root_rcu(struct nameidata *nd) 55431e6b01fSNick Piggin { 55531e6b01fSNick Piggin if (!nd->root.mnt) { 55631e6b01fSNick Piggin struct fs_struct *fs = current->fs; 557c28cc364SNick Piggin unsigned seq; 558c28cc364SNick Piggin 559c28cc364SNick Piggin do { 560c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 56131e6b01fSNick Piggin nd->root = fs->root; 562c1530019STim Chen nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq); 563c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 56431e6b01fSNick Piggin } 56531e6b01fSNick Piggin } 56631e6b01fSNick Piggin 567f1662356SArjan van de Ven static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link) 5681da177e4SLinus Torvalds { 56931e6b01fSNick Piggin int ret; 57031e6b01fSNick Piggin 5711da177e4SLinus Torvalds if (IS_ERR(link)) 5721da177e4SLinus Torvalds goto fail; 5731da177e4SLinus Torvalds 5741da177e4SLinus Torvalds if (*link == '/') { 5752a737871SAl Viro set_root(nd); 5761d957f9bSJan Blunck path_put(&nd->path); 5772a737871SAl Viro nd->path = nd->root; 5782a737871SAl Viro path_get(&nd->root); 57916c2cd71SAl Viro nd->flags |= LOOKUP_JUMPED; 5801da177e4SLinus Torvalds } 58131e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 582b4091d5fSChristoph Hellwig 58331e6b01fSNick Piggin ret = link_path_walk(link, nd); 58431e6b01fSNick Piggin return ret; 5851da177e4SLinus Torvalds fail: 5861d957f9bSJan Blunck path_put(&nd->path); 5871da177e4SLinus Torvalds return PTR_ERR(link); 5881da177e4SLinus Torvalds } 5891da177e4SLinus Torvalds 5901d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd) 591051d3812SIan Kent { 592051d3812SIan Kent dput(path->dentry); 5934ac91378SJan Blunck if (path->mnt != nd->path.mnt) 594051d3812SIan Kent mntput(path->mnt); 595051d3812SIan Kent } 596051d3812SIan Kent 5977b9337aaSNick Piggin static inline void path_to_nameidata(const struct path *path, 5987b9337aaSNick Piggin struct nameidata *nd) 599051d3812SIan Kent { 60031e6b01fSNick Piggin if (!(nd->flags & LOOKUP_RCU)) { 6014ac91378SJan Blunck dput(nd->path.dentry); 60231e6b01fSNick Piggin if (nd->path.mnt != path->mnt) 6034ac91378SJan Blunck mntput(nd->path.mnt); 6049a229683SHuang Shijie } 60531e6b01fSNick Piggin nd->path.mnt = path->mnt; 6064ac91378SJan Blunck nd->path.dentry = path->dentry; 607051d3812SIan Kent } 608051d3812SIan Kent 609574197e0SAl Viro static inline void put_link(struct nameidata *nd, struct path *link, void *cookie) 610574197e0SAl Viro { 611574197e0SAl Viro struct inode *inode = link->dentry->d_inode; 612574197e0SAl Viro if (!IS_ERR(cookie) && inode->i_op->put_link) 613574197e0SAl Viro inode->i_op->put_link(link->dentry, nd, cookie); 614574197e0SAl Viro path_put(link); 615574197e0SAl Viro } 616574197e0SAl Viro 617def4af30SAl Viro static __always_inline int 618574197e0SAl Viro follow_link(struct path *link, struct nameidata *nd, void **p) 6191da177e4SLinus Torvalds { 6201da177e4SLinus Torvalds int error; 6217b9337aaSNick Piggin struct dentry *dentry = link->dentry; 6221da177e4SLinus Torvalds 623844a3917SAl Viro BUG_ON(nd->flags & LOOKUP_RCU); 624844a3917SAl Viro 6250e794589SAl Viro if (link->mnt == nd->path.mnt) 6260e794589SAl Viro mntget(link->mnt); 6270e794589SAl Viro 628574197e0SAl Viro if (unlikely(current->total_link_count >= 40)) { 629574197e0SAl Viro *p = ERR_PTR(-ELOOP); /* no ->put_link(), please */ 630574197e0SAl Viro path_put(&nd->path); 631574197e0SAl Viro return -ELOOP; 632574197e0SAl Viro } 633574197e0SAl Viro cond_resched(); 634574197e0SAl Viro current->total_link_count++; 635574197e0SAl Viro 63668ac1234SAl Viro touch_atime(link); 6371da177e4SLinus Torvalds nd_set_link(nd, NULL); 638cd4e91d3SAl Viro 63936f3b4f6SAl Viro error = security_inode_follow_link(link->dentry, nd); 64036f3b4f6SAl Viro if (error) { 64136f3b4f6SAl Viro *p = ERR_PTR(error); /* no ->put_link(), please */ 64236f3b4f6SAl Viro path_put(&nd->path); 64336f3b4f6SAl Viro return error; 64436f3b4f6SAl Viro } 64536f3b4f6SAl Viro 64686acdca1SAl Viro nd->last_type = LAST_BIND; 647def4af30SAl Viro *p = dentry->d_inode->i_op->follow_link(dentry, nd); 648def4af30SAl Viro error = PTR_ERR(*p); 649def4af30SAl Viro if (!IS_ERR(*p)) { 6501da177e4SLinus Torvalds char *s = nd_get_link(nd); 651cc314eefSLinus Torvalds error = 0; 6521da177e4SLinus Torvalds if (s) 6531da177e4SLinus Torvalds error = __vfs_follow_link(nd, s); 654bcda7652SAl Viro else if (nd->last_type == LAST_BIND) { 65516c2cd71SAl Viro nd->flags |= LOOKUP_JUMPED; 656b21041d0SAl Viro nd->inode = nd->path.dentry->d_inode; 657b21041d0SAl Viro if (nd->inode->i_op->follow_link) { 658bcda7652SAl Viro /* stepped on a _really_ weird one */ 659bcda7652SAl Viro path_put(&nd->path); 660bcda7652SAl Viro error = -ELOOP; 661bcda7652SAl Viro } 662bcda7652SAl Viro } 6631da177e4SLinus Torvalds } 6641da177e4SLinus Torvalds return error; 6651da177e4SLinus Torvalds } 6661da177e4SLinus Torvalds 66731e6b01fSNick Piggin static int follow_up_rcu(struct path *path) 66831e6b01fSNick Piggin { 6690714a533SAl Viro struct mount *mnt = real_mount(path->mnt); 6700714a533SAl Viro struct mount *parent; 67131e6b01fSNick Piggin struct dentry *mountpoint; 67231e6b01fSNick Piggin 6730714a533SAl Viro parent = mnt->mnt_parent; 6740714a533SAl Viro if (&parent->mnt == path->mnt) 67531e6b01fSNick Piggin return 0; 676a73324daSAl Viro mountpoint = mnt->mnt_mountpoint; 67731e6b01fSNick Piggin path->dentry = mountpoint; 6780714a533SAl Viro path->mnt = &parent->mnt; 67931e6b01fSNick Piggin return 1; 68031e6b01fSNick Piggin } 68131e6b01fSNick Piggin 682bab77ebfSAl Viro int follow_up(struct path *path) 6831da177e4SLinus Torvalds { 6840714a533SAl Viro struct mount *mnt = real_mount(path->mnt); 6850714a533SAl Viro struct mount *parent; 6861da177e4SLinus Torvalds struct dentry *mountpoint; 68799b7db7bSNick Piggin 68899b7db7bSNick Piggin br_read_lock(vfsmount_lock); 6890714a533SAl Viro parent = mnt->mnt_parent; 6900714a533SAl Viro if (&parent->mnt == path->mnt) { 69199b7db7bSNick Piggin br_read_unlock(vfsmount_lock); 6921da177e4SLinus Torvalds return 0; 6931da177e4SLinus Torvalds } 6940714a533SAl Viro mntget(&parent->mnt); 695a73324daSAl Viro mountpoint = dget(mnt->mnt_mountpoint); 69699b7db7bSNick Piggin br_read_unlock(vfsmount_lock); 697bab77ebfSAl Viro dput(path->dentry); 698bab77ebfSAl Viro path->dentry = mountpoint; 699bab77ebfSAl Viro mntput(path->mnt); 7000714a533SAl Viro path->mnt = &parent->mnt; 7011da177e4SLinus Torvalds return 1; 7021da177e4SLinus Torvalds } 7031da177e4SLinus Torvalds 704b5c84bf6SNick Piggin /* 7059875cf80SDavid Howells * Perform an automount 7069875cf80SDavid Howells * - return -EISDIR to tell follow_managed() to stop and return the path we 7079875cf80SDavid Howells * were called with. 7081da177e4SLinus Torvalds */ 7099875cf80SDavid Howells static int follow_automount(struct path *path, unsigned flags, 7109875cf80SDavid Howells bool *need_mntput) 71131e6b01fSNick Piggin { 7129875cf80SDavid Howells struct vfsmount *mnt; 713ea5b778aSDavid Howells int err; 7149875cf80SDavid Howells 7159875cf80SDavid Howells if (!path->dentry->d_op || !path->dentry->d_op->d_automount) 7169875cf80SDavid Howells return -EREMOTE; 7179875cf80SDavid Howells 7180ec26fd0SMiklos Szeredi /* We don't want to mount if someone's just doing a stat - 7190ec26fd0SMiklos Szeredi * unless they're stat'ing a directory and appended a '/' to 7200ec26fd0SMiklos Szeredi * the name. 7210ec26fd0SMiklos Szeredi * 7220ec26fd0SMiklos Szeredi * We do, however, want to mount if someone wants to open or 7235a30d8a2SDavid Howells * create a file of any type under the mountpoint, wants to 7240ec26fd0SMiklos Szeredi * traverse through the mountpoint or wants to open the 7250ec26fd0SMiklos Szeredi * mounted directory. Also, autofs may mark negative dentries 7260ec26fd0SMiklos Szeredi * as being automount points. These will need the attentions 7270ec26fd0SMiklos Szeredi * of the daemon to instantiate them before they can be used. 7285a30d8a2SDavid Howells */ 7295a30d8a2SDavid Howells if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY | 730d94c177bSLinus Torvalds LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) && 7315a30d8a2SDavid Howells path->dentry->d_inode) 7329875cf80SDavid Howells return -EISDIR; 7330ec26fd0SMiklos Szeredi 7349875cf80SDavid Howells current->total_link_count++; 7359875cf80SDavid Howells if (current->total_link_count >= 40) 7369875cf80SDavid Howells return -ELOOP; 7379875cf80SDavid Howells 7389875cf80SDavid Howells mnt = path->dentry->d_op->d_automount(path); 7399875cf80SDavid Howells if (IS_ERR(mnt)) { 7409875cf80SDavid Howells /* 7419875cf80SDavid Howells * The filesystem is allowed to return -EISDIR here to indicate 7429875cf80SDavid Howells * it doesn't want to automount. For instance, autofs would do 7439875cf80SDavid Howells * this so that its userspace daemon can mount on this dentry. 7449875cf80SDavid Howells * 7459875cf80SDavid Howells * However, we can only permit this if it's a terminal point in 7469875cf80SDavid Howells * the path being looked up; if it wasn't then the remainder of 7479875cf80SDavid Howells * the path is inaccessible and we should say so. 7489875cf80SDavid Howells */ 74949084c3bSAl Viro if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT)) 7509875cf80SDavid Howells return -EREMOTE; 7519875cf80SDavid Howells return PTR_ERR(mnt); 75231e6b01fSNick Piggin } 753ea5b778aSDavid Howells 7549875cf80SDavid Howells if (!mnt) /* mount collision */ 7559875cf80SDavid Howells return 0; 7569875cf80SDavid Howells 7578aef1884SAl Viro if (!*need_mntput) { 7588aef1884SAl Viro /* lock_mount() may release path->mnt on error */ 7598aef1884SAl Viro mntget(path->mnt); 7608aef1884SAl Viro *need_mntput = true; 7618aef1884SAl Viro } 76219a167afSAl Viro err = finish_automount(mnt, path); 763ea5b778aSDavid Howells 764ea5b778aSDavid Howells switch (err) { 765ea5b778aSDavid Howells case -EBUSY: 766ea5b778aSDavid Howells /* Someone else made a mount here whilst we were busy */ 76719a167afSAl Viro return 0; 768ea5b778aSDavid Howells case 0: 7698aef1884SAl Viro path_put(path); 7709875cf80SDavid Howells path->mnt = mnt; 7719875cf80SDavid Howells path->dentry = dget(mnt->mnt_root); 7729875cf80SDavid Howells return 0; 77319a167afSAl Viro default: 77419a167afSAl Viro return err; 7759875cf80SDavid Howells } 77619a167afSAl Viro 777ea5b778aSDavid Howells } 7789875cf80SDavid Howells 7799875cf80SDavid Howells /* 7809875cf80SDavid Howells * Handle a dentry that is managed in some way. 781cc53ce53SDavid Howells * - Flagged for transit management (autofs) 7829875cf80SDavid Howells * - Flagged as mountpoint 7839875cf80SDavid Howells * - Flagged as automount point 7849875cf80SDavid Howells * 7859875cf80SDavid Howells * This may only be called in refwalk mode. 7869875cf80SDavid Howells * 7879875cf80SDavid Howells * Serialization is taken care of in namespace.c 7889875cf80SDavid Howells */ 7899875cf80SDavid Howells static int follow_managed(struct path *path, unsigned flags) 7909875cf80SDavid Howells { 7918aef1884SAl Viro struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */ 7929875cf80SDavid Howells unsigned managed; 7939875cf80SDavid Howells bool need_mntput = false; 7948aef1884SAl Viro int ret = 0; 7959875cf80SDavid Howells 7969875cf80SDavid Howells /* Given that we're not holding a lock here, we retain the value in a 7979875cf80SDavid Howells * local variable for each dentry as we look at it so that we don't see 7989875cf80SDavid Howells * the components of that value change under us */ 7999875cf80SDavid Howells while (managed = ACCESS_ONCE(path->dentry->d_flags), 8009875cf80SDavid Howells managed &= DCACHE_MANAGED_DENTRY, 8019875cf80SDavid Howells unlikely(managed != 0)) { 802cc53ce53SDavid Howells /* Allow the filesystem to manage the transit without i_mutex 803cc53ce53SDavid Howells * being held. */ 804cc53ce53SDavid Howells if (managed & DCACHE_MANAGE_TRANSIT) { 805cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op); 806cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op->d_manage); 8071aed3e42SAl Viro ret = path->dentry->d_op->d_manage(path->dentry, false); 808cc53ce53SDavid Howells if (ret < 0) 8098aef1884SAl Viro break; 810cc53ce53SDavid Howells } 811cc53ce53SDavid Howells 8129875cf80SDavid Howells /* Transit to a mounted filesystem. */ 8139875cf80SDavid Howells if (managed & DCACHE_MOUNTED) { 8149875cf80SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 8159875cf80SDavid Howells if (mounted) { 8169875cf80SDavid Howells dput(path->dentry); 8179875cf80SDavid Howells if (need_mntput) 818463ffb2eSAl Viro mntput(path->mnt); 819463ffb2eSAl Viro path->mnt = mounted; 820463ffb2eSAl Viro path->dentry = dget(mounted->mnt_root); 8219875cf80SDavid Howells need_mntput = true; 8229875cf80SDavid Howells continue; 823463ffb2eSAl Viro } 824463ffb2eSAl Viro 8259875cf80SDavid Howells /* Something is mounted on this dentry in another 8269875cf80SDavid Howells * namespace and/or whatever was mounted there in this 8279875cf80SDavid Howells * namespace got unmounted before we managed to get the 8289875cf80SDavid Howells * vfsmount_lock */ 8291da177e4SLinus Torvalds } 8309875cf80SDavid Howells 8319875cf80SDavid Howells /* Handle an automount point */ 8329875cf80SDavid Howells if (managed & DCACHE_NEED_AUTOMOUNT) { 8339875cf80SDavid Howells ret = follow_automount(path, flags, &need_mntput); 8349875cf80SDavid Howells if (ret < 0) 8358aef1884SAl Viro break; 8369875cf80SDavid Howells continue; 8379875cf80SDavid Howells } 8389875cf80SDavid Howells 8399875cf80SDavid Howells /* We didn't change the current path point */ 8409875cf80SDavid Howells break; 8419875cf80SDavid Howells } 8428aef1884SAl Viro 8438aef1884SAl Viro if (need_mntput && path->mnt == mnt) 8448aef1884SAl Viro mntput(path->mnt); 8458aef1884SAl Viro if (ret == -EISDIR) 8468aef1884SAl Viro ret = 0; 847a3fbbde7SAl Viro return ret < 0 ? ret : need_mntput; 8481da177e4SLinus Torvalds } 8491da177e4SLinus Torvalds 850cc53ce53SDavid Howells int follow_down_one(struct path *path) 8511da177e4SLinus Torvalds { 8521da177e4SLinus Torvalds struct vfsmount *mounted; 8531da177e4SLinus Torvalds 8541c755af4SAl Viro mounted = lookup_mnt(path); 8551da177e4SLinus Torvalds if (mounted) { 8569393bd07SAl Viro dput(path->dentry); 8579393bd07SAl Viro mntput(path->mnt); 8589393bd07SAl Viro path->mnt = mounted; 8599393bd07SAl Viro path->dentry = dget(mounted->mnt_root); 8601da177e4SLinus Torvalds return 1; 8611da177e4SLinus Torvalds } 8621da177e4SLinus Torvalds return 0; 8631da177e4SLinus Torvalds } 8641da177e4SLinus Torvalds 86562a7375eSIan Kent static inline bool managed_dentry_might_block(struct dentry *dentry) 86662a7375eSIan Kent { 86762a7375eSIan Kent return (dentry->d_flags & DCACHE_MANAGE_TRANSIT && 86862a7375eSIan Kent dentry->d_op->d_manage(dentry, true) < 0); 86962a7375eSIan Kent } 87062a7375eSIan Kent 8719875cf80SDavid Howells /* 872287548e4SAl Viro * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if 873287548e4SAl Viro * we meet a managed dentry that would need blocking. 8749875cf80SDavid Howells */ 8759875cf80SDavid Howells static bool __follow_mount_rcu(struct nameidata *nd, struct path *path, 876287548e4SAl Viro struct inode **inode) 8779875cf80SDavid Howells { 87862a7375eSIan Kent for (;;) { 879c7105365SAl Viro struct mount *mounted; 88062a7375eSIan Kent /* 88162a7375eSIan Kent * Don't forget we might have a non-mountpoint managed dentry 88262a7375eSIan Kent * that wants to block transit. 88362a7375eSIan Kent */ 884287548e4SAl Viro if (unlikely(managed_dentry_might_block(path->dentry))) 885ab90911fSDavid Howells return false; 88662a7375eSIan Kent 88762a7375eSIan Kent if (!d_mountpoint(path->dentry)) 88862a7375eSIan Kent break; 88962a7375eSIan Kent 8909875cf80SDavid Howells mounted = __lookup_mnt(path->mnt, path->dentry, 1); 8919875cf80SDavid Howells if (!mounted) 8929875cf80SDavid Howells break; 893c7105365SAl Viro path->mnt = &mounted->mnt; 894c7105365SAl Viro path->dentry = mounted->mnt.mnt_root; 895a3fbbde7SAl Viro nd->flags |= LOOKUP_JUMPED; 8969875cf80SDavid Howells nd->seq = read_seqcount_begin(&path->dentry->d_seq); 89759430262SLinus Torvalds /* 89859430262SLinus Torvalds * Update the inode too. We don't need to re-check the 89959430262SLinus Torvalds * dentry sequence number here after this d_inode read, 90059430262SLinus Torvalds * because a mount-point is always pinned. 90159430262SLinus Torvalds */ 90259430262SLinus Torvalds *inode = path->dentry->d_inode; 9039875cf80SDavid Howells } 9049875cf80SDavid Howells return true; 9059875cf80SDavid Howells } 9069875cf80SDavid Howells 907dea39376SAl Viro static void follow_mount_rcu(struct nameidata *nd) 908287548e4SAl Viro { 909dea39376SAl Viro while (d_mountpoint(nd->path.dentry)) { 910c7105365SAl Viro struct mount *mounted; 911dea39376SAl Viro mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1); 912287548e4SAl Viro if (!mounted) 913287548e4SAl Viro break; 914c7105365SAl Viro nd->path.mnt = &mounted->mnt; 915c7105365SAl Viro nd->path.dentry = mounted->mnt.mnt_root; 916dea39376SAl Viro nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); 917287548e4SAl Viro } 918287548e4SAl Viro } 919287548e4SAl Viro 92031e6b01fSNick Piggin static int follow_dotdot_rcu(struct nameidata *nd) 92131e6b01fSNick Piggin { 92231e6b01fSNick Piggin set_root_rcu(nd); 92331e6b01fSNick Piggin 92431e6b01fSNick Piggin while (1) { 92531e6b01fSNick Piggin if (nd->path.dentry == nd->root.dentry && 92631e6b01fSNick Piggin nd->path.mnt == nd->root.mnt) { 92731e6b01fSNick Piggin break; 92831e6b01fSNick Piggin } 92931e6b01fSNick Piggin if (nd->path.dentry != nd->path.mnt->mnt_root) { 93031e6b01fSNick Piggin struct dentry *old = nd->path.dentry; 93131e6b01fSNick Piggin struct dentry *parent = old->d_parent; 93231e6b01fSNick Piggin unsigned seq; 93331e6b01fSNick Piggin 93431e6b01fSNick Piggin seq = read_seqcount_begin(&parent->d_seq); 93531e6b01fSNick Piggin if (read_seqcount_retry(&old->d_seq, nd->seq)) 936ef7562d5SAl Viro goto failed; 93731e6b01fSNick Piggin nd->path.dentry = parent; 93831e6b01fSNick Piggin nd->seq = seq; 93931e6b01fSNick Piggin break; 94031e6b01fSNick Piggin } 94131e6b01fSNick Piggin if (!follow_up_rcu(&nd->path)) 94231e6b01fSNick Piggin break; 94331e6b01fSNick Piggin nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); 94431e6b01fSNick Piggin } 945dea39376SAl Viro follow_mount_rcu(nd); 946dea39376SAl Viro nd->inode = nd->path.dentry->d_inode; 94731e6b01fSNick Piggin return 0; 948ef7562d5SAl Viro 949ef7562d5SAl Viro failed: 950ef7562d5SAl Viro nd->flags &= ~LOOKUP_RCU; 9515b6ca027SAl Viro if (!(nd->flags & LOOKUP_ROOT)) 952ef7562d5SAl Viro nd->root.mnt = NULL; 953ef7562d5SAl Viro rcu_read_unlock(); 954ef7562d5SAl Viro br_read_unlock(vfsmount_lock); 955ef7562d5SAl Viro return -ECHILD; 95631e6b01fSNick Piggin } 95731e6b01fSNick Piggin 9589875cf80SDavid Howells /* 959cc53ce53SDavid Howells * Follow down to the covering mount currently visible to userspace. At each 960cc53ce53SDavid Howells * point, the filesystem owning that dentry may be queried as to whether the 961cc53ce53SDavid Howells * caller is permitted to proceed or not. 962cc53ce53SDavid Howells */ 9637cc90cc3SAl Viro int follow_down(struct path *path) 964cc53ce53SDavid Howells { 965cc53ce53SDavid Howells unsigned managed; 966cc53ce53SDavid Howells int ret; 967cc53ce53SDavid Howells 968cc53ce53SDavid Howells while (managed = ACCESS_ONCE(path->dentry->d_flags), 969cc53ce53SDavid Howells unlikely(managed & DCACHE_MANAGED_DENTRY)) { 970cc53ce53SDavid Howells /* Allow the filesystem to manage the transit without i_mutex 971cc53ce53SDavid Howells * being held. 972cc53ce53SDavid Howells * 973cc53ce53SDavid Howells * We indicate to the filesystem if someone is trying to mount 974cc53ce53SDavid Howells * something here. This gives autofs the chance to deny anyone 975cc53ce53SDavid Howells * other than its daemon the right to mount on its 976cc53ce53SDavid Howells * superstructure. 977cc53ce53SDavid Howells * 978cc53ce53SDavid Howells * The filesystem may sleep at this point. 979cc53ce53SDavid Howells */ 980cc53ce53SDavid Howells if (managed & DCACHE_MANAGE_TRANSIT) { 981cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op); 982cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op->d_manage); 983ab90911fSDavid Howells ret = path->dentry->d_op->d_manage( 9841aed3e42SAl Viro path->dentry, false); 985cc53ce53SDavid Howells if (ret < 0) 986cc53ce53SDavid Howells return ret == -EISDIR ? 0 : ret; 987cc53ce53SDavid Howells } 988cc53ce53SDavid Howells 989cc53ce53SDavid Howells /* Transit to a mounted filesystem. */ 990cc53ce53SDavid Howells if (managed & DCACHE_MOUNTED) { 991cc53ce53SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 992cc53ce53SDavid Howells if (!mounted) 993cc53ce53SDavid Howells break; 994cc53ce53SDavid Howells dput(path->dentry); 995cc53ce53SDavid Howells mntput(path->mnt); 996cc53ce53SDavid Howells path->mnt = mounted; 997cc53ce53SDavid Howells path->dentry = dget(mounted->mnt_root); 998cc53ce53SDavid Howells continue; 999cc53ce53SDavid Howells } 1000cc53ce53SDavid Howells 1001cc53ce53SDavid Howells /* Don't handle automount points here */ 1002cc53ce53SDavid Howells break; 1003cc53ce53SDavid Howells } 1004cc53ce53SDavid Howells return 0; 1005cc53ce53SDavid Howells } 1006cc53ce53SDavid Howells 1007cc53ce53SDavid Howells /* 10089875cf80SDavid Howells * Skip to top of mountpoint pile in refwalk mode for follow_dotdot() 10099875cf80SDavid Howells */ 10109875cf80SDavid Howells static void follow_mount(struct path *path) 10119875cf80SDavid Howells { 10129875cf80SDavid Howells while (d_mountpoint(path->dentry)) { 10139875cf80SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 10149875cf80SDavid Howells if (!mounted) 10159875cf80SDavid Howells break; 10169875cf80SDavid Howells dput(path->dentry); 10179875cf80SDavid Howells mntput(path->mnt); 10189875cf80SDavid Howells path->mnt = mounted; 10199875cf80SDavid Howells path->dentry = dget(mounted->mnt_root); 10209875cf80SDavid Howells } 10219875cf80SDavid Howells } 10229875cf80SDavid Howells 102331e6b01fSNick Piggin static void follow_dotdot(struct nameidata *nd) 10241da177e4SLinus Torvalds { 10252a737871SAl Viro set_root(nd); 1026e518ddb7SAndreas Mohr 10271da177e4SLinus Torvalds while(1) { 10284ac91378SJan Blunck struct dentry *old = nd->path.dentry; 10291da177e4SLinus Torvalds 10302a737871SAl Viro if (nd->path.dentry == nd->root.dentry && 10312a737871SAl Viro nd->path.mnt == nd->root.mnt) { 10321da177e4SLinus Torvalds break; 10331da177e4SLinus Torvalds } 10344ac91378SJan Blunck if (nd->path.dentry != nd->path.mnt->mnt_root) { 10353088dd70SAl Viro /* rare case of legitimate dget_parent()... */ 10363088dd70SAl Viro nd->path.dentry = dget_parent(nd->path.dentry); 10371da177e4SLinus Torvalds dput(old); 10381da177e4SLinus Torvalds break; 10391da177e4SLinus Torvalds } 10403088dd70SAl Viro if (!follow_up(&nd->path)) 10411da177e4SLinus Torvalds break; 10421da177e4SLinus Torvalds } 104379ed0226SAl Viro follow_mount(&nd->path); 104431e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 10451da177e4SLinus Torvalds } 10461da177e4SLinus Torvalds 10471da177e4SLinus Torvalds /* 1048bad61189SMiklos Szeredi * This looks up the name in dcache, possibly revalidates the old dentry and 1049bad61189SMiklos Szeredi * allocates a new one if not found or not valid. In the need_lookup argument 1050bad61189SMiklos Szeredi * returns whether i_op->lookup is necessary. 1051bad61189SMiklos Szeredi * 1052bad61189SMiklos Szeredi * dir->d_inode->i_mutex must be held 1053baa03890SNick Piggin */ 1054bad61189SMiklos Szeredi static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir, 1055bad61189SMiklos Szeredi struct nameidata *nd, bool *need_lookup) 1056baa03890SNick Piggin { 1057baa03890SNick Piggin struct dentry *dentry; 1058bad61189SMiklos Szeredi int error; 1059baa03890SNick Piggin 1060bad61189SMiklos Szeredi *need_lookup = false; 1061bad61189SMiklos Szeredi dentry = d_lookup(dir, name); 1062bad61189SMiklos Szeredi if (dentry) { 1063bad61189SMiklos Szeredi if (d_need_lookup(dentry)) { 1064bad61189SMiklos Szeredi *need_lookup = true; 1065bad61189SMiklos Szeredi } else if (dentry->d_flags & DCACHE_OP_REVALIDATE) { 1066bad61189SMiklos Szeredi error = d_revalidate(dentry, nd); 1067bad61189SMiklos Szeredi if (unlikely(error <= 0)) { 1068bad61189SMiklos Szeredi if (error < 0) { 1069bad61189SMiklos Szeredi dput(dentry); 1070bad61189SMiklos Szeredi return ERR_PTR(error); 1071bad61189SMiklos Szeredi } else if (!d_invalidate(dentry)) { 1072bad61189SMiklos Szeredi dput(dentry); 1073bad61189SMiklos Szeredi dentry = NULL; 1074bad61189SMiklos Szeredi } 1075bad61189SMiklos Szeredi } 1076bad61189SMiklos Szeredi } 1077bad61189SMiklos Szeredi } 1078baa03890SNick Piggin 1079bad61189SMiklos Szeredi if (!dentry) { 1080bad61189SMiklos Szeredi dentry = d_alloc(dir, name); 1081baa03890SNick Piggin if (unlikely(!dentry)) 1082baa03890SNick Piggin return ERR_PTR(-ENOMEM); 1083baa03890SNick Piggin 1084bad61189SMiklos Szeredi *need_lookup = true; 1085baa03890SNick Piggin } 1086baa03890SNick Piggin return dentry; 1087baa03890SNick Piggin } 1088baa03890SNick Piggin 1089baa03890SNick Piggin /* 1090bad61189SMiklos Szeredi * Call i_op->lookup on the dentry. The dentry must be negative but may be 1091bad61189SMiklos Szeredi * hashed if it was pouplated with DCACHE_NEED_LOOKUP. 1092bad61189SMiklos Szeredi * 1093bad61189SMiklos Szeredi * dir->d_inode->i_mutex must be held 109444396f4bSJosef Bacik */ 1095bad61189SMiklos Szeredi static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry, 109644396f4bSJosef Bacik struct nameidata *nd) 109744396f4bSJosef Bacik { 109844396f4bSJosef Bacik struct dentry *old; 109944396f4bSJosef Bacik 110044396f4bSJosef Bacik /* Don't create child dentry for a dead directory. */ 1101bad61189SMiklos Szeredi if (unlikely(IS_DEADDIR(dir))) { 1102e188dc02SMiklos Szeredi dput(dentry); 110344396f4bSJosef Bacik return ERR_PTR(-ENOENT); 1104e188dc02SMiklos Szeredi } 110544396f4bSJosef Bacik 1106bad61189SMiklos Szeredi old = dir->i_op->lookup(dir, dentry, nd); 110744396f4bSJosef Bacik if (unlikely(old)) { 110844396f4bSJosef Bacik dput(dentry); 110944396f4bSJosef Bacik dentry = old; 111044396f4bSJosef Bacik } 111144396f4bSJosef Bacik return dentry; 111244396f4bSJosef Bacik } 111344396f4bSJosef Bacik 1114a3255546SAl Viro static struct dentry *__lookup_hash(struct qstr *name, 1115a3255546SAl Viro struct dentry *base, struct nameidata *nd) 1116a3255546SAl Viro { 1117bad61189SMiklos Szeredi bool need_lookup; 1118a3255546SAl Viro struct dentry *dentry; 1119a3255546SAl Viro 1120bad61189SMiklos Szeredi dentry = lookup_dcache(name, base, nd, &need_lookup); 1121bad61189SMiklos Szeredi if (!need_lookup) 1122a3255546SAl Viro return dentry; 1123bad61189SMiklos Szeredi 1124bad61189SMiklos Szeredi return lookup_real(base->d_inode, dentry, nd); 1125a3255546SAl Viro } 1126a3255546SAl Viro 112744396f4bSJosef Bacik /* 11281da177e4SLinus Torvalds * It's more convoluted than I'd like it to be, but... it's still fairly 11291da177e4SLinus Torvalds * small and for now I'd prefer to have fast path as straight as possible. 11301da177e4SLinus Torvalds * It _is_ time-critical. 11311da177e4SLinus Torvalds */ 11321da177e4SLinus Torvalds static int do_lookup(struct nameidata *nd, struct qstr *name, 113331e6b01fSNick Piggin struct path *path, struct inode **inode) 11341da177e4SLinus Torvalds { 11354ac91378SJan Blunck struct vfsmount *mnt = nd->path.mnt; 113631e6b01fSNick Piggin struct dentry *dentry, *parent = nd->path.dentry; 11375a18fff2SAl Viro int need_reval = 1; 11385a18fff2SAl Viro int status = 1; 11399875cf80SDavid Howells int err; 11409875cf80SDavid Howells 11413cac260aSAl Viro /* 1142b04f784eSNick Piggin * Rename seqlock is not required here because in the off chance 1143b04f784eSNick Piggin * of a false negative due to a concurrent rename, we're going to 1144b04f784eSNick Piggin * do the non-racy lookup, below. 1145b04f784eSNick Piggin */ 114631e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) { 114731e6b01fSNick Piggin unsigned seq; 114812f8ad4bSLinus Torvalds dentry = __d_lookup_rcu(parent, name, &seq, nd->inode); 11495a18fff2SAl Viro if (!dentry) 11505a18fff2SAl Viro goto unlazy; 11515a18fff2SAl Viro 115212f8ad4bSLinus Torvalds /* 115312f8ad4bSLinus Torvalds * This sequence count validates that the inode matches 115412f8ad4bSLinus Torvalds * the dentry name information from lookup. 115512f8ad4bSLinus Torvalds */ 115612f8ad4bSLinus Torvalds *inode = dentry->d_inode; 115712f8ad4bSLinus Torvalds if (read_seqcount_retry(&dentry->d_seq, seq)) 115812f8ad4bSLinus Torvalds return -ECHILD; 115912f8ad4bSLinus Torvalds 116012f8ad4bSLinus Torvalds /* 116112f8ad4bSLinus Torvalds * This sequence count validates that the parent had no 116212f8ad4bSLinus Torvalds * changes while we did the lookup of the dentry above. 116312f8ad4bSLinus Torvalds * 116412f8ad4bSLinus Torvalds * The memory barrier in read_seqcount_begin of child is 116512f8ad4bSLinus Torvalds * enough, we can use __read_seqcount_retry here. 116612f8ad4bSLinus Torvalds */ 116731e6b01fSNick Piggin if (__read_seqcount_retry(&parent->d_seq, nd->seq)) 116831e6b01fSNick Piggin return -ECHILD; 116931e6b01fSNick Piggin nd->seq = seq; 11705a18fff2SAl Viro 1171fa4ee159SMiklos Szeredi if (unlikely(d_need_lookup(dentry))) 1172fa4ee159SMiklos Szeredi goto unlazy; 117324643087SAl Viro if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) { 11745a18fff2SAl Viro status = d_revalidate(dentry, nd); 11755a18fff2SAl Viro if (unlikely(status <= 0)) { 11765a18fff2SAl Viro if (status != -ECHILD) 11775a18fff2SAl Viro need_reval = 0; 11785a18fff2SAl Viro goto unlazy; 11795a18fff2SAl Viro } 118024643087SAl Viro } 118131e6b01fSNick Piggin path->mnt = mnt; 118231e6b01fSNick Piggin path->dentry = dentry; 1183d6e9bd25SAl Viro if (unlikely(!__follow_mount_rcu(nd, path, inode))) 1184d6e9bd25SAl Viro goto unlazy; 1185d6e9bd25SAl Viro if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT)) 1186d6e9bd25SAl Viro goto unlazy; 11879875cf80SDavid Howells return 0; 11885a18fff2SAl Viro unlazy: 118919660af7SAl Viro if (unlazy_walk(nd, dentry)) 11905a18fff2SAl Viro return -ECHILD; 11915a18fff2SAl Viro } else { 119231e6b01fSNick Piggin dentry = __d_lookup(parent, name); 119324643087SAl Viro } 11945a18fff2SAl Viro 119581e6f520SAl Viro if (unlikely(!dentry)) 119681e6f520SAl Viro goto need_lookup; 11975a18fff2SAl Viro 119881e6f520SAl Viro if (unlikely(d_need_lookup(dentry))) { 119981e6f520SAl Viro dput(dentry); 120081e6f520SAl Viro goto need_lookup; 12015a18fff2SAl Viro } 120281e6f520SAl Viro 12035a18fff2SAl Viro if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval) 12045a18fff2SAl Viro status = d_revalidate(dentry, nd); 12055a18fff2SAl Viro if (unlikely(status <= 0)) { 12065a18fff2SAl Viro if (status < 0) { 12075a18fff2SAl Viro dput(dentry); 12085a18fff2SAl Viro return status; 12095a18fff2SAl Viro } 12105a18fff2SAl Viro if (!d_invalidate(dentry)) { 12115a18fff2SAl Viro dput(dentry); 121281e6f520SAl Viro goto need_lookup; 12135a18fff2SAl Viro } 12145a18fff2SAl Viro } 12153f6c7c71SAl Viro done: 12161da177e4SLinus Torvalds path->mnt = mnt; 12171da177e4SLinus Torvalds path->dentry = dentry; 12189875cf80SDavid Howells err = follow_managed(path, nd->flags); 121989312214SIan Kent if (unlikely(err < 0)) { 122089312214SIan Kent path_put_conditional(path, nd); 12219875cf80SDavid Howells return err; 122289312214SIan Kent } 1223a3fbbde7SAl Viro if (err) 1224a3fbbde7SAl Viro nd->flags |= LOOKUP_JUMPED; 122531e6b01fSNick Piggin *inode = path->dentry->d_inode; 12261da177e4SLinus Torvalds return 0; 122781e6f520SAl Viro 122881e6f520SAl Viro need_lookup: 122981e6f520SAl Viro BUG_ON(nd->inode != parent->d_inode); 123081e6f520SAl Viro 123181e6f520SAl Viro mutex_lock(&parent->d_inode->i_mutex); 123281e6f520SAl Viro dentry = __lookup_hash(name, parent, nd); 123381e6f520SAl Viro mutex_unlock(&parent->d_inode->i_mutex); 123481e6f520SAl Viro if (IS_ERR(dentry)) 123581e6f520SAl Viro return PTR_ERR(dentry); 123681e6f520SAl Viro goto done; 12371da177e4SLinus Torvalds } 12381da177e4SLinus Torvalds 123952094c8aSAl Viro static inline int may_lookup(struct nameidata *nd) 124052094c8aSAl Viro { 124152094c8aSAl Viro if (nd->flags & LOOKUP_RCU) { 12424ad5abb3SAl Viro int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK); 124352094c8aSAl Viro if (err != -ECHILD) 124452094c8aSAl Viro return err; 124519660af7SAl Viro if (unlazy_walk(nd, NULL)) 124652094c8aSAl Viro return -ECHILD; 124752094c8aSAl Viro } 12484ad5abb3SAl Viro return inode_permission(nd->inode, MAY_EXEC); 124952094c8aSAl Viro } 125052094c8aSAl Viro 12519856fa1bSAl Viro static inline int handle_dots(struct nameidata *nd, int type) 12529856fa1bSAl Viro { 12539856fa1bSAl Viro if (type == LAST_DOTDOT) { 12549856fa1bSAl Viro if (nd->flags & LOOKUP_RCU) { 12559856fa1bSAl Viro if (follow_dotdot_rcu(nd)) 12569856fa1bSAl Viro return -ECHILD; 12579856fa1bSAl Viro } else 12589856fa1bSAl Viro follow_dotdot(nd); 12599856fa1bSAl Viro } 12609856fa1bSAl Viro return 0; 12619856fa1bSAl Viro } 12629856fa1bSAl Viro 1263951361f9SAl Viro static void terminate_walk(struct nameidata *nd) 1264951361f9SAl Viro { 1265951361f9SAl Viro if (!(nd->flags & LOOKUP_RCU)) { 1266951361f9SAl Viro path_put(&nd->path); 1267951361f9SAl Viro } else { 1268951361f9SAl Viro nd->flags &= ~LOOKUP_RCU; 12695b6ca027SAl Viro if (!(nd->flags & LOOKUP_ROOT)) 1270951361f9SAl Viro nd->root.mnt = NULL; 1271951361f9SAl Viro rcu_read_unlock(); 1272951361f9SAl Viro br_read_unlock(vfsmount_lock); 1273951361f9SAl Viro } 1274951361f9SAl Viro } 1275951361f9SAl Viro 12763ddcd056SLinus Torvalds /* 12773ddcd056SLinus Torvalds * Do we need to follow links? We _really_ want to be able 12783ddcd056SLinus Torvalds * to do this check without having to look at inode->i_op, 12793ddcd056SLinus Torvalds * so we keep a cache of "no, this doesn't need follow_link" 12803ddcd056SLinus Torvalds * for the common case. 12813ddcd056SLinus Torvalds */ 12827813b94aSLinus Torvalds static inline int should_follow_link(struct inode *inode, int follow) 12833ddcd056SLinus Torvalds { 12843ddcd056SLinus Torvalds if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) { 12853ddcd056SLinus Torvalds if (likely(inode->i_op->follow_link)) 12863ddcd056SLinus Torvalds return follow; 12873ddcd056SLinus Torvalds 12883ddcd056SLinus Torvalds /* This gets set once for the inode lifetime */ 12893ddcd056SLinus Torvalds spin_lock(&inode->i_lock); 12903ddcd056SLinus Torvalds inode->i_opflags |= IOP_NOFOLLOW; 12913ddcd056SLinus Torvalds spin_unlock(&inode->i_lock); 12923ddcd056SLinus Torvalds } 12933ddcd056SLinus Torvalds return 0; 12943ddcd056SLinus Torvalds } 12953ddcd056SLinus Torvalds 1296ce57dfc1SAl Viro static inline int walk_component(struct nameidata *nd, struct path *path, 1297ce57dfc1SAl Viro struct qstr *name, int type, int follow) 1298ce57dfc1SAl Viro { 1299ce57dfc1SAl Viro struct inode *inode; 1300ce57dfc1SAl Viro int err; 1301ce57dfc1SAl Viro /* 1302ce57dfc1SAl Viro * "." and ".." are special - ".." especially so because it has 1303ce57dfc1SAl Viro * to be able to know about the current root directory and 1304ce57dfc1SAl Viro * parent relationships. 1305ce57dfc1SAl Viro */ 1306ce57dfc1SAl Viro if (unlikely(type != LAST_NORM)) 1307ce57dfc1SAl Viro return handle_dots(nd, type); 1308ce57dfc1SAl Viro err = do_lookup(nd, name, path, &inode); 1309ce57dfc1SAl Viro if (unlikely(err)) { 1310ce57dfc1SAl Viro terminate_walk(nd); 1311ce57dfc1SAl Viro return err; 1312ce57dfc1SAl Viro } 1313ce57dfc1SAl Viro if (!inode) { 1314ce57dfc1SAl Viro path_to_nameidata(path, nd); 1315ce57dfc1SAl Viro terminate_walk(nd); 1316ce57dfc1SAl Viro return -ENOENT; 1317ce57dfc1SAl Viro } 13187813b94aSLinus Torvalds if (should_follow_link(inode, follow)) { 131919660af7SAl Viro if (nd->flags & LOOKUP_RCU) { 132019660af7SAl Viro if (unlikely(unlazy_walk(nd, path->dentry))) { 132119660af7SAl Viro terminate_walk(nd); 1322ce57dfc1SAl Viro return -ECHILD; 132319660af7SAl Viro } 132419660af7SAl Viro } 1325ce57dfc1SAl Viro BUG_ON(inode != path->dentry->d_inode); 1326ce57dfc1SAl Viro return 1; 1327ce57dfc1SAl Viro } 1328ce57dfc1SAl Viro path_to_nameidata(path, nd); 1329ce57dfc1SAl Viro nd->inode = inode; 1330ce57dfc1SAl Viro return 0; 1331ce57dfc1SAl Viro } 1332ce57dfc1SAl Viro 13331da177e4SLinus Torvalds /* 1334b356379aSAl Viro * This limits recursive symlink follows to 8, while 1335b356379aSAl Viro * limiting consecutive symlinks to 40. 1336b356379aSAl Viro * 1337b356379aSAl Viro * Without that kind of total limit, nasty chains of consecutive 1338b356379aSAl Viro * symlinks can cause almost arbitrarily long lookups. 1339b356379aSAl Viro */ 1340b356379aSAl Viro static inline int nested_symlink(struct path *path, struct nameidata *nd) 1341b356379aSAl Viro { 1342b356379aSAl Viro int res; 1343b356379aSAl Viro 1344b356379aSAl Viro if (unlikely(current->link_count >= MAX_NESTED_LINKS)) { 1345b356379aSAl Viro path_put_conditional(path, nd); 1346b356379aSAl Viro path_put(&nd->path); 1347b356379aSAl Viro return -ELOOP; 1348b356379aSAl Viro } 13491a4022f8SErez Zadok BUG_ON(nd->depth >= MAX_NESTED_LINKS); 1350b356379aSAl Viro 1351b356379aSAl Viro nd->depth++; 1352b356379aSAl Viro current->link_count++; 1353b356379aSAl Viro 1354b356379aSAl Viro do { 1355b356379aSAl Viro struct path link = *path; 1356b356379aSAl Viro void *cookie; 1357574197e0SAl Viro 1358574197e0SAl Viro res = follow_link(&link, nd, &cookie); 1359b356379aSAl Viro if (!res) 1360b356379aSAl Viro res = walk_component(nd, path, &nd->last, 1361b356379aSAl Viro nd->last_type, LOOKUP_FOLLOW); 1362574197e0SAl Viro put_link(nd, &link, cookie); 1363b356379aSAl Viro } while (res > 0); 1364b356379aSAl Viro 1365b356379aSAl Viro current->link_count--; 1366b356379aSAl Viro nd->depth--; 1367b356379aSAl Viro return res; 1368b356379aSAl Viro } 1369b356379aSAl Viro 1370b356379aSAl Viro /* 13713ddcd056SLinus Torvalds * We really don't want to look at inode->i_op->lookup 13723ddcd056SLinus Torvalds * when we don't have to. So we keep a cache bit in 13733ddcd056SLinus Torvalds * the inode ->i_opflags field that says "yes, we can 13743ddcd056SLinus Torvalds * do lookup on this inode". 13753ddcd056SLinus Torvalds */ 13763ddcd056SLinus Torvalds static inline int can_lookup(struct inode *inode) 13773ddcd056SLinus Torvalds { 13783ddcd056SLinus Torvalds if (likely(inode->i_opflags & IOP_LOOKUP)) 13793ddcd056SLinus Torvalds return 1; 13803ddcd056SLinus Torvalds if (likely(!inode->i_op->lookup)) 13813ddcd056SLinus Torvalds return 0; 13823ddcd056SLinus Torvalds 13833ddcd056SLinus Torvalds /* We do this once for the lifetime of the inode */ 13843ddcd056SLinus Torvalds spin_lock(&inode->i_lock); 13853ddcd056SLinus Torvalds inode->i_opflags |= IOP_LOOKUP; 13863ddcd056SLinus Torvalds spin_unlock(&inode->i_lock); 13873ddcd056SLinus Torvalds return 1; 13883ddcd056SLinus Torvalds } 13893ddcd056SLinus Torvalds 1390bfcfaa77SLinus Torvalds /* 1391bfcfaa77SLinus Torvalds * We can do the critical dentry name comparison and hashing 1392bfcfaa77SLinus Torvalds * operations one word at a time, but we are limited to: 1393bfcfaa77SLinus Torvalds * 1394bfcfaa77SLinus Torvalds * - Architectures with fast unaligned word accesses. We could 1395bfcfaa77SLinus Torvalds * do a "get_unaligned()" if this helps and is sufficiently 1396bfcfaa77SLinus Torvalds * fast. 1397bfcfaa77SLinus Torvalds * 1398bfcfaa77SLinus Torvalds * - Little-endian machines (so that we can generate the mask 1399bfcfaa77SLinus Torvalds * of low bytes efficiently). Again, we *could* do a byte 1400bfcfaa77SLinus Torvalds * swapping load on big-endian architectures if that is not 1401bfcfaa77SLinus Torvalds * expensive enough to make the optimization worthless. 1402bfcfaa77SLinus Torvalds * 1403bfcfaa77SLinus Torvalds * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we 1404bfcfaa77SLinus Torvalds * do not trap on the (extremely unlikely) case of a page 1405bfcfaa77SLinus Torvalds * crossing operation. 1406bfcfaa77SLinus Torvalds * 1407bfcfaa77SLinus Torvalds * - Furthermore, we need an efficient 64-bit compile for the 1408bfcfaa77SLinus Torvalds * 64-bit case in order to generate the "number of bytes in 1409bfcfaa77SLinus Torvalds * the final mask". Again, that could be replaced with a 1410bfcfaa77SLinus Torvalds * efficient population count instruction or similar. 1411bfcfaa77SLinus Torvalds */ 1412bfcfaa77SLinus Torvalds #ifdef CONFIG_DCACHE_WORD_ACCESS 1413bfcfaa77SLinus Torvalds 1414f68e556eSLinus Torvalds #include <asm/word-at-a-time.h> 1415bfcfaa77SLinus Torvalds 1416f68e556eSLinus Torvalds #ifdef CONFIG_64BIT 1417bfcfaa77SLinus Torvalds 1418bfcfaa77SLinus Torvalds static inline unsigned int fold_hash(unsigned long hash) 1419bfcfaa77SLinus Torvalds { 1420bfcfaa77SLinus Torvalds hash += hash >> (8*sizeof(int)); 1421bfcfaa77SLinus Torvalds return hash; 1422bfcfaa77SLinus Torvalds } 1423bfcfaa77SLinus Torvalds 1424bfcfaa77SLinus Torvalds #else /* 32-bit case */ 1425bfcfaa77SLinus Torvalds 1426bfcfaa77SLinus Torvalds #define fold_hash(x) (x) 1427bfcfaa77SLinus Torvalds 1428bfcfaa77SLinus Torvalds #endif 1429bfcfaa77SLinus Torvalds 1430bfcfaa77SLinus Torvalds unsigned int full_name_hash(const unsigned char *name, unsigned int len) 1431bfcfaa77SLinus Torvalds { 1432bfcfaa77SLinus Torvalds unsigned long a, mask; 1433bfcfaa77SLinus Torvalds unsigned long hash = 0; 1434bfcfaa77SLinus Torvalds 1435bfcfaa77SLinus Torvalds for (;;) { 1436e419b4ccSLinus Torvalds a = load_unaligned_zeropad(name); 1437bfcfaa77SLinus Torvalds if (len < sizeof(unsigned long)) 1438bfcfaa77SLinus Torvalds break; 1439bfcfaa77SLinus Torvalds hash += a; 1440f132c5beSAl Viro hash *= 9; 1441bfcfaa77SLinus Torvalds name += sizeof(unsigned long); 1442bfcfaa77SLinus Torvalds len -= sizeof(unsigned long); 1443bfcfaa77SLinus Torvalds if (!len) 1444bfcfaa77SLinus Torvalds goto done; 1445bfcfaa77SLinus Torvalds } 1446bfcfaa77SLinus Torvalds mask = ~(~0ul << len*8); 1447bfcfaa77SLinus Torvalds hash += mask & a; 1448bfcfaa77SLinus Torvalds done: 1449bfcfaa77SLinus Torvalds return fold_hash(hash); 1450bfcfaa77SLinus Torvalds } 1451bfcfaa77SLinus Torvalds EXPORT_SYMBOL(full_name_hash); 1452bfcfaa77SLinus Torvalds 1453bfcfaa77SLinus Torvalds /* 1454bfcfaa77SLinus Torvalds * Calculate the length and hash of the path component, and 1455bfcfaa77SLinus Torvalds * return the length of the component; 1456bfcfaa77SLinus Torvalds */ 1457bfcfaa77SLinus Torvalds static inline unsigned long hash_name(const char *name, unsigned int *hashp) 1458bfcfaa77SLinus Torvalds { 1459bfcfaa77SLinus Torvalds unsigned long a, mask, hash, len; 1460bfcfaa77SLinus Torvalds 1461bfcfaa77SLinus Torvalds hash = a = 0; 1462bfcfaa77SLinus Torvalds len = -sizeof(unsigned long); 1463bfcfaa77SLinus Torvalds do { 1464bfcfaa77SLinus Torvalds hash = (hash + a) * 9; 1465bfcfaa77SLinus Torvalds len += sizeof(unsigned long); 1466e419b4ccSLinus Torvalds a = load_unaligned_zeropad(name+len); 1467bfcfaa77SLinus Torvalds /* Do we have any NUL or '/' bytes in this word? */ 1468f68e556eSLinus Torvalds mask = has_zero(a) | has_zero(a ^ REPEAT_BYTE('/')); 1469bfcfaa77SLinus Torvalds } while (!mask); 1470bfcfaa77SLinus Torvalds 1471bfcfaa77SLinus Torvalds /* The mask *below* the first high bit set */ 1472bfcfaa77SLinus Torvalds mask = (mask - 1) & ~mask; 1473bfcfaa77SLinus Torvalds mask >>= 7; 1474bfcfaa77SLinus Torvalds hash += a & mask; 1475bfcfaa77SLinus Torvalds *hashp = fold_hash(hash); 1476bfcfaa77SLinus Torvalds 1477bfcfaa77SLinus Torvalds return len + count_masked_bytes(mask); 1478bfcfaa77SLinus Torvalds } 1479bfcfaa77SLinus Torvalds 1480bfcfaa77SLinus Torvalds #else 1481bfcfaa77SLinus Torvalds 14820145acc2SLinus Torvalds unsigned int full_name_hash(const unsigned char *name, unsigned int len) 14830145acc2SLinus Torvalds { 14840145acc2SLinus Torvalds unsigned long hash = init_name_hash(); 14850145acc2SLinus Torvalds while (len--) 14860145acc2SLinus Torvalds hash = partial_name_hash(*name++, hash); 14870145acc2SLinus Torvalds return end_name_hash(hash); 14880145acc2SLinus Torvalds } 1489ae942ae7SLinus Torvalds EXPORT_SYMBOL(full_name_hash); 14900145acc2SLinus Torvalds 14913ddcd056SLinus Torvalds /* 1492200e9ef7SLinus Torvalds * We know there's a real path component here of at least 1493200e9ef7SLinus Torvalds * one character. 1494200e9ef7SLinus Torvalds */ 1495200e9ef7SLinus Torvalds static inline unsigned long hash_name(const char *name, unsigned int *hashp) 1496200e9ef7SLinus Torvalds { 1497200e9ef7SLinus Torvalds unsigned long hash = init_name_hash(); 1498200e9ef7SLinus Torvalds unsigned long len = 0, c; 1499200e9ef7SLinus Torvalds 1500200e9ef7SLinus Torvalds c = (unsigned char)*name; 1501200e9ef7SLinus Torvalds do { 1502200e9ef7SLinus Torvalds len++; 1503200e9ef7SLinus Torvalds hash = partial_name_hash(c, hash); 1504200e9ef7SLinus Torvalds c = (unsigned char)name[len]; 1505200e9ef7SLinus Torvalds } while (c && c != '/'); 1506200e9ef7SLinus Torvalds *hashp = end_name_hash(hash); 1507200e9ef7SLinus Torvalds return len; 1508200e9ef7SLinus Torvalds } 1509200e9ef7SLinus Torvalds 1510bfcfaa77SLinus Torvalds #endif 1511bfcfaa77SLinus Torvalds 1512200e9ef7SLinus Torvalds /* 15131da177e4SLinus Torvalds * Name resolution. 1514ea3834d9SPrasanna Meda * This is the basic name resolution function, turning a pathname into 1515ea3834d9SPrasanna Meda * the final dentry. We expect 'base' to be positive and a directory. 15161da177e4SLinus Torvalds * 1517ea3834d9SPrasanna Meda * Returns 0 and nd will have valid dentry and mnt on success. 1518ea3834d9SPrasanna Meda * Returns error and drops reference to input namei data on failure. 15191da177e4SLinus Torvalds */ 15206de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd) 15211da177e4SLinus Torvalds { 15221da177e4SLinus Torvalds struct path next; 15231da177e4SLinus Torvalds int err; 15241da177e4SLinus Torvalds 15251da177e4SLinus Torvalds while (*name=='/') 15261da177e4SLinus Torvalds name++; 15271da177e4SLinus Torvalds if (!*name) 1528086e183aSAl Viro return 0; 15291da177e4SLinus Torvalds 15301da177e4SLinus Torvalds /* At this point we know we have a real path component. */ 15311da177e4SLinus Torvalds for(;;) { 15321da177e4SLinus Torvalds struct qstr this; 1533200e9ef7SLinus Torvalds long len; 1534fe479a58SAl Viro int type; 15351da177e4SLinus Torvalds 153652094c8aSAl Viro err = may_lookup(nd); 15371da177e4SLinus Torvalds if (err) 15381da177e4SLinus Torvalds break; 15391da177e4SLinus Torvalds 1540200e9ef7SLinus Torvalds len = hash_name(name, &this.hash); 15411da177e4SLinus Torvalds this.name = name; 1542200e9ef7SLinus Torvalds this.len = len; 15431da177e4SLinus Torvalds 1544fe479a58SAl Viro type = LAST_NORM; 1545200e9ef7SLinus Torvalds if (name[0] == '.') switch (len) { 1546fe479a58SAl Viro case 2: 1547200e9ef7SLinus Torvalds if (name[1] == '.') { 1548fe479a58SAl Viro type = LAST_DOTDOT; 154916c2cd71SAl Viro nd->flags |= LOOKUP_JUMPED; 155016c2cd71SAl Viro } 1551fe479a58SAl Viro break; 1552fe479a58SAl Viro case 1: 1553fe479a58SAl Viro type = LAST_DOT; 1554fe479a58SAl Viro } 15555a202bcdSAl Viro if (likely(type == LAST_NORM)) { 15565a202bcdSAl Viro struct dentry *parent = nd->path.dentry; 155716c2cd71SAl Viro nd->flags &= ~LOOKUP_JUMPED; 15585a202bcdSAl Viro if (unlikely(parent->d_flags & DCACHE_OP_HASH)) { 15595a202bcdSAl Viro err = parent->d_op->d_hash(parent, nd->inode, 15605a202bcdSAl Viro &this); 15615a202bcdSAl Viro if (err < 0) 15625a202bcdSAl Viro break; 15635a202bcdSAl Viro } 15645a202bcdSAl Viro } 1565fe479a58SAl Viro 1566200e9ef7SLinus Torvalds if (!name[len]) 15671da177e4SLinus Torvalds goto last_component; 1568200e9ef7SLinus Torvalds /* 1569200e9ef7SLinus Torvalds * If it wasn't NUL, we know it was '/'. Skip that 1570200e9ef7SLinus Torvalds * slash, and continue until no more slashes. 1571200e9ef7SLinus Torvalds */ 1572200e9ef7SLinus Torvalds do { 1573200e9ef7SLinus Torvalds len++; 1574200e9ef7SLinus Torvalds } while (unlikely(name[len] == '/')); 1575200e9ef7SLinus Torvalds if (!name[len]) 1576b356379aSAl Viro goto last_component; 1577200e9ef7SLinus Torvalds name += len; 15781da177e4SLinus Torvalds 1579ce57dfc1SAl Viro err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW); 1580ce57dfc1SAl Viro if (err < 0) 1581ce57dfc1SAl Viro return err; 1582fe479a58SAl Viro 1583ce57dfc1SAl Viro if (err) { 1584b356379aSAl Viro err = nested_symlink(&next, nd); 15851da177e4SLinus Torvalds if (err) 1586a7472babSAl Viro return err; 158731e6b01fSNick Piggin } 15883ddcd056SLinus Torvalds if (can_lookup(nd->inode)) 15891da177e4SLinus Torvalds continue; 15903ddcd056SLinus Torvalds err = -ENOTDIR; 15913ddcd056SLinus Torvalds break; 15921da177e4SLinus Torvalds /* here ends the main loop */ 15931da177e4SLinus Torvalds 15941da177e4SLinus Torvalds last_component: 1595ce57dfc1SAl Viro nd->last = this; 1596ce57dfc1SAl Viro nd->last_type = type; 1597ce57dfc1SAl Viro return 0; 1598ce57dfc1SAl Viro } 1599951361f9SAl Viro terminate_walk(nd); 16001da177e4SLinus Torvalds return err; 16011da177e4SLinus Torvalds } 16021da177e4SLinus Torvalds 160370e9b357SAl Viro static int path_init(int dfd, const char *name, unsigned int flags, 160470e9b357SAl Viro struct nameidata *nd, struct file **fp) 160531e6b01fSNick Piggin { 160631e6b01fSNick Piggin int retval = 0; 160731e6b01fSNick Piggin int fput_needed; 160831e6b01fSNick Piggin struct file *file; 160931e6b01fSNick Piggin 161031e6b01fSNick Piggin nd->last_type = LAST_ROOT; /* if there are only slashes... */ 161116c2cd71SAl Viro nd->flags = flags | LOOKUP_JUMPED; 161231e6b01fSNick Piggin nd->depth = 0; 16135b6ca027SAl Viro if (flags & LOOKUP_ROOT) { 16145b6ca027SAl Viro struct inode *inode = nd->root.dentry->d_inode; 161573d049a4SAl Viro if (*name) { 16165b6ca027SAl Viro if (!inode->i_op->lookup) 16175b6ca027SAl Viro return -ENOTDIR; 16185b6ca027SAl Viro retval = inode_permission(inode, MAY_EXEC); 16195b6ca027SAl Viro if (retval) 16205b6ca027SAl Viro return retval; 162173d049a4SAl Viro } 16225b6ca027SAl Viro nd->path = nd->root; 16235b6ca027SAl Viro nd->inode = inode; 16245b6ca027SAl Viro if (flags & LOOKUP_RCU) { 16255b6ca027SAl Viro br_read_lock(vfsmount_lock); 16265b6ca027SAl Viro rcu_read_lock(); 16275b6ca027SAl Viro nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 16285b6ca027SAl Viro } else { 16295b6ca027SAl Viro path_get(&nd->path); 16305b6ca027SAl Viro } 16315b6ca027SAl Viro return 0; 16325b6ca027SAl Viro } 16335b6ca027SAl Viro 163431e6b01fSNick Piggin nd->root.mnt = NULL; 163531e6b01fSNick Piggin 163631e6b01fSNick Piggin if (*name=='/') { 1637e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 163831e6b01fSNick Piggin br_read_lock(vfsmount_lock); 163931e6b01fSNick Piggin rcu_read_lock(); 1640e41f7d4eSAl Viro set_root_rcu(nd); 1641e41f7d4eSAl Viro } else { 1642e41f7d4eSAl Viro set_root(nd); 1643e41f7d4eSAl Viro path_get(&nd->root); 1644e41f7d4eSAl Viro } 164531e6b01fSNick Piggin nd->path = nd->root; 164631e6b01fSNick Piggin } else if (dfd == AT_FDCWD) { 1647e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 164831e6b01fSNick Piggin struct fs_struct *fs = current->fs; 1649c28cc364SNick Piggin unsigned seq; 165031e6b01fSNick Piggin 165131e6b01fSNick Piggin br_read_lock(vfsmount_lock); 165231e6b01fSNick Piggin rcu_read_lock(); 165331e6b01fSNick Piggin 1654c28cc364SNick Piggin do { 1655c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 165631e6b01fSNick Piggin nd->path = fs->pwd; 1657c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 1658c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 1659e41f7d4eSAl Viro } else { 1660e41f7d4eSAl Viro get_fs_pwd(current->fs, &nd->path); 1661e41f7d4eSAl Viro } 166231e6b01fSNick Piggin } else { 166331e6b01fSNick Piggin struct dentry *dentry; 166431e6b01fSNick Piggin 16651abf0c71SAl Viro file = fget_raw_light(dfd, &fput_needed); 166631e6b01fSNick Piggin retval = -EBADF; 166731e6b01fSNick Piggin if (!file) 166831e6b01fSNick Piggin goto out_fail; 166931e6b01fSNick Piggin 167031e6b01fSNick Piggin dentry = file->f_path.dentry; 167131e6b01fSNick Piggin 1672f52e0c11SAl Viro if (*name) { 167331e6b01fSNick Piggin retval = -ENOTDIR; 167431e6b01fSNick Piggin if (!S_ISDIR(dentry->d_inode->i_mode)) 167531e6b01fSNick Piggin goto fput_fail; 167631e6b01fSNick Piggin 16774ad5abb3SAl Viro retval = inode_permission(dentry->d_inode, MAY_EXEC); 167831e6b01fSNick Piggin if (retval) 167931e6b01fSNick Piggin goto fput_fail; 1680f52e0c11SAl Viro } 168131e6b01fSNick Piggin 168231e6b01fSNick Piggin nd->path = file->f_path; 1683e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 168431e6b01fSNick Piggin if (fput_needed) 168570e9b357SAl Viro *fp = file; 1686c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 168731e6b01fSNick Piggin br_read_lock(vfsmount_lock); 168831e6b01fSNick Piggin rcu_read_lock(); 16895590ff0dSUlrich Drepper } else { 16905dd784d0SJan Blunck path_get(&file->f_path); 16915590ff0dSUlrich Drepper fput_light(file, fput_needed); 16921da177e4SLinus Torvalds } 1693e41f7d4eSAl Viro } 1694e41f7d4eSAl Viro 169531e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 16969b4a9b14SAl Viro return 0; 16972dfdd266SJosef 'Jeff' Sipek 16989b4a9b14SAl Viro fput_fail: 16999b4a9b14SAl Viro fput_light(file, fput_needed); 17009b4a9b14SAl Viro out_fail: 17019b4a9b14SAl Viro return retval; 17029b4a9b14SAl Viro } 17039b4a9b14SAl Viro 1704bd92d7feSAl Viro static inline int lookup_last(struct nameidata *nd, struct path *path) 1705bd92d7feSAl Viro { 1706bd92d7feSAl Viro if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len]) 1707bd92d7feSAl Viro nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 1708bd92d7feSAl Viro 1709bd92d7feSAl Viro nd->flags &= ~LOOKUP_PARENT; 1710bd92d7feSAl Viro return walk_component(nd, path, &nd->last, nd->last_type, 1711bd92d7feSAl Viro nd->flags & LOOKUP_FOLLOW); 1712bd92d7feSAl Viro } 1713bd92d7feSAl Viro 17149b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */ 1715ee0827cdSAl Viro static int path_lookupat(int dfd, const char *name, 17169b4a9b14SAl Viro unsigned int flags, struct nameidata *nd) 17179b4a9b14SAl Viro { 171870e9b357SAl Viro struct file *base = NULL; 1719bd92d7feSAl Viro struct path path; 1720bd92d7feSAl Viro int err; 172131e6b01fSNick Piggin 172231e6b01fSNick Piggin /* 172331e6b01fSNick Piggin * Path walking is largely split up into 2 different synchronisation 172431e6b01fSNick Piggin * schemes, rcu-walk and ref-walk (explained in 172531e6b01fSNick Piggin * Documentation/filesystems/path-lookup.txt). These share much of the 172631e6b01fSNick Piggin * path walk code, but some things particularly setup, cleanup, and 172731e6b01fSNick Piggin * following mounts are sufficiently divergent that functions are 172831e6b01fSNick Piggin * duplicated. Typically there is a function foo(), and its RCU 172931e6b01fSNick Piggin * analogue, foo_rcu(). 173031e6b01fSNick Piggin * 173131e6b01fSNick Piggin * -ECHILD is the error number of choice (just to avoid clashes) that 173231e6b01fSNick Piggin * is returned if some aspect of an rcu-walk fails. Such an error must 173331e6b01fSNick Piggin * be handled by restarting a traditional ref-walk (which will always 173431e6b01fSNick Piggin * be able to complete). 173531e6b01fSNick Piggin */ 1736bd92d7feSAl Viro err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base); 1737ee0827cdSAl Viro 1738bd92d7feSAl Viro if (unlikely(err)) 1739bd92d7feSAl Viro return err; 1740ee0827cdSAl Viro 1741ee0827cdSAl Viro current->total_link_count = 0; 1742bd92d7feSAl Viro err = link_path_walk(name, nd); 1743bd92d7feSAl Viro 1744bd92d7feSAl Viro if (!err && !(flags & LOOKUP_PARENT)) { 1745bd92d7feSAl Viro err = lookup_last(nd, &path); 1746bd92d7feSAl Viro while (err > 0) { 1747bd92d7feSAl Viro void *cookie; 1748bd92d7feSAl Viro struct path link = path; 1749bd92d7feSAl Viro nd->flags |= LOOKUP_PARENT; 1750574197e0SAl Viro err = follow_link(&link, nd, &cookie); 1751bd92d7feSAl Viro if (!err) 1752bd92d7feSAl Viro err = lookup_last(nd, &path); 1753574197e0SAl Viro put_link(nd, &link, cookie); 1754bd92d7feSAl Viro } 1755bd92d7feSAl Viro } 1756ee0827cdSAl Viro 17579f1fafeeSAl Viro if (!err) 17589f1fafeeSAl Viro err = complete_walk(nd); 1759bd92d7feSAl Viro 1760bd92d7feSAl Viro if (!err && nd->flags & LOOKUP_DIRECTORY) { 1761bd92d7feSAl Viro if (!nd->inode->i_op->lookup) { 1762bd92d7feSAl Viro path_put(&nd->path); 1763bd23a539SAl Viro err = -ENOTDIR; 1764bd92d7feSAl Viro } 1765bd92d7feSAl Viro } 176616c2cd71SAl Viro 176770e9b357SAl Viro if (base) 176870e9b357SAl Viro fput(base); 1769ee0827cdSAl Viro 17705b6ca027SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) { 177131e6b01fSNick Piggin path_put(&nd->root); 177231e6b01fSNick Piggin nd->root.mnt = NULL; 177331e6b01fSNick Piggin } 1774bd92d7feSAl Viro return err; 177531e6b01fSNick Piggin } 177631e6b01fSNick Piggin 1777ee0827cdSAl Viro static int do_path_lookup(int dfd, const char *name, 1778ee0827cdSAl Viro unsigned int flags, struct nameidata *nd) 1779ee0827cdSAl Viro { 1780ee0827cdSAl Viro int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd); 1781ee0827cdSAl Viro if (unlikely(retval == -ECHILD)) 1782ee0827cdSAl Viro retval = path_lookupat(dfd, name, flags, nd); 1783ee0827cdSAl Viro if (unlikely(retval == -ESTALE)) 1784ee0827cdSAl Viro retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd); 1785ee0827cdSAl Viro 178631e6b01fSNick Piggin if (likely(!retval)) { 178731e6b01fSNick Piggin if (unlikely(!audit_dummy_context())) { 178831e6b01fSNick Piggin if (nd->path.dentry && nd->inode) 178931e6b01fSNick Piggin audit_inode(name, nd->path.dentry); 179031e6b01fSNick Piggin } 179131e6b01fSNick Piggin } 1792170aa3d0SUlrich Drepper return retval; 17931da177e4SLinus Torvalds } 17941da177e4SLinus Torvalds 1795c9c6cac0SAl Viro int kern_path_parent(const char *name, struct nameidata *nd) 17965590ff0dSUlrich Drepper { 1797c9c6cac0SAl Viro return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd); 17985590ff0dSUlrich Drepper } 17995590ff0dSUlrich Drepper 1800d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path) 1801d1811465SAl Viro { 1802d1811465SAl Viro struct nameidata nd; 1803d1811465SAl Viro int res = do_path_lookup(AT_FDCWD, name, flags, &nd); 1804d1811465SAl Viro if (!res) 1805d1811465SAl Viro *path = nd.path; 1806d1811465SAl Viro return res; 1807d1811465SAl Viro } 1808d1811465SAl Viro 180916f18200SJosef 'Jeff' Sipek /** 181016f18200SJosef 'Jeff' Sipek * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair 181116f18200SJosef 'Jeff' Sipek * @dentry: pointer to dentry of the base directory 181216f18200SJosef 'Jeff' Sipek * @mnt: pointer to vfs mount of the base directory 181316f18200SJosef 'Jeff' Sipek * @name: pointer to file name 181416f18200SJosef 'Jeff' Sipek * @flags: lookup flags 1815e0a01249SAl Viro * @path: pointer to struct path to fill 181616f18200SJosef 'Jeff' Sipek */ 181716f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt, 181816f18200SJosef 'Jeff' Sipek const char *name, unsigned int flags, 1819e0a01249SAl Viro struct path *path) 182016f18200SJosef 'Jeff' Sipek { 1821e0a01249SAl Viro struct nameidata nd; 1822e0a01249SAl Viro int err; 1823e0a01249SAl Viro nd.root.dentry = dentry; 1824e0a01249SAl Viro nd.root.mnt = mnt; 1825e0a01249SAl Viro BUG_ON(flags & LOOKUP_PARENT); 18265b6ca027SAl Viro /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */ 1827e0a01249SAl Viro err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd); 1828e0a01249SAl Viro if (!err) 1829e0a01249SAl Viro *path = nd.path; 1830e0a01249SAl Viro return err; 183116f18200SJosef 'Jeff' Sipek } 183216f18200SJosef 'Jeff' Sipek 1833057f6c01SJames Morris /* 1834057f6c01SJames Morris * Restricted form of lookup. Doesn't follow links, single-component only, 1835057f6c01SJames Morris * needs parent already locked. Doesn't follow mounts. 1836057f6c01SJames Morris * SMP-safe. 1837057f6c01SJames Morris */ 1838a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd) 18391da177e4SLinus Torvalds { 18404ac91378SJan Blunck return __lookup_hash(&nd->last, nd->path.dentry, nd); 18411da177e4SLinus Torvalds } 18421da177e4SLinus Torvalds 1843eead1911SChristoph Hellwig /** 1844a6b91919SRandy Dunlap * lookup_one_len - filesystem helper to lookup single pathname component 1845eead1911SChristoph Hellwig * @name: pathname component to lookup 1846eead1911SChristoph Hellwig * @base: base directory to lookup from 1847eead1911SChristoph Hellwig * @len: maximum length @len should be interpreted to 1848eead1911SChristoph Hellwig * 1849a6b91919SRandy Dunlap * Note that this routine is purely a helper for filesystem usage and should 1850a6b91919SRandy Dunlap * not be called by generic code. Also note that by using this function the 1851eead1911SChristoph Hellwig * nameidata argument is passed to the filesystem methods and a filesystem 1852eead1911SChristoph Hellwig * using this helper needs to be prepared for that. 1853eead1911SChristoph Hellwig */ 1854057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len) 1855057f6c01SJames Morris { 1856057f6c01SJames Morris struct qstr this; 18576a96ba54SAl Viro unsigned int c; 1858cda309deSMiklos Szeredi int err; 1859057f6c01SJames Morris 18602f9092e1SDavid Woodhouse WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex)); 18612f9092e1SDavid Woodhouse 18626a96ba54SAl Viro this.name = name; 18636a96ba54SAl Viro this.len = len; 18640145acc2SLinus Torvalds this.hash = full_name_hash(name, len); 18656a96ba54SAl Viro if (!len) 18666a96ba54SAl Viro return ERR_PTR(-EACCES); 18676a96ba54SAl Viro 18686a96ba54SAl Viro while (len--) { 18696a96ba54SAl Viro c = *(const unsigned char *)name++; 18706a96ba54SAl Viro if (c == '/' || c == '\0') 18716a96ba54SAl Viro return ERR_PTR(-EACCES); 18726a96ba54SAl Viro } 18735a202bcdSAl Viro /* 18745a202bcdSAl Viro * See if the low-level filesystem might want 18755a202bcdSAl Viro * to use its own hash.. 18765a202bcdSAl Viro */ 18775a202bcdSAl Viro if (base->d_flags & DCACHE_OP_HASH) { 18785a202bcdSAl Viro int err = base->d_op->d_hash(base, base->d_inode, &this); 18795a202bcdSAl Viro if (err < 0) 18805a202bcdSAl Viro return ERR_PTR(err); 18815a202bcdSAl Viro } 1882eead1911SChristoph Hellwig 1883cda309deSMiklos Szeredi err = inode_permission(base->d_inode, MAY_EXEC); 1884cda309deSMiklos Szeredi if (err) 1885cda309deSMiklos Szeredi return ERR_PTR(err); 1886cda309deSMiklos Szeredi 188749705b77SChristoph Hellwig return __lookup_hash(&this, base, NULL); 1888057f6c01SJames Morris } 1889057f6c01SJames Morris 18901fa1e7f6SAndy Whitcroft int user_path_at_empty(int dfd, const char __user *name, unsigned flags, 18911fa1e7f6SAndy Whitcroft struct path *path, int *empty) 18921da177e4SLinus Torvalds { 18932d8f3038SAl Viro struct nameidata nd; 18941fa1e7f6SAndy Whitcroft char *tmp = getname_flags(name, flags, empty); 18951da177e4SLinus Torvalds int err = PTR_ERR(tmp); 18961da177e4SLinus Torvalds if (!IS_ERR(tmp)) { 18972d8f3038SAl Viro 18982d8f3038SAl Viro BUG_ON(flags & LOOKUP_PARENT); 18992d8f3038SAl Viro 19002d8f3038SAl Viro err = do_path_lookup(dfd, tmp, flags, &nd); 19011da177e4SLinus Torvalds putname(tmp); 19022d8f3038SAl Viro if (!err) 19032d8f3038SAl Viro *path = nd.path; 19041da177e4SLinus Torvalds } 19051da177e4SLinus Torvalds return err; 19061da177e4SLinus Torvalds } 19071da177e4SLinus Torvalds 19081fa1e7f6SAndy Whitcroft int user_path_at(int dfd, const char __user *name, unsigned flags, 19091fa1e7f6SAndy Whitcroft struct path *path) 19101fa1e7f6SAndy Whitcroft { 1911f7493e5dSLinus Torvalds return user_path_at_empty(dfd, name, flags, path, NULL); 19121fa1e7f6SAndy Whitcroft } 19131fa1e7f6SAndy Whitcroft 19142ad94ae6SAl Viro static int user_path_parent(int dfd, const char __user *path, 19152ad94ae6SAl Viro struct nameidata *nd, char **name) 19162ad94ae6SAl Viro { 19172ad94ae6SAl Viro char *s = getname(path); 19182ad94ae6SAl Viro int error; 19192ad94ae6SAl Viro 19202ad94ae6SAl Viro if (IS_ERR(s)) 19212ad94ae6SAl Viro return PTR_ERR(s); 19222ad94ae6SAl Viro 19232ad94ae6SAl Viro error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd); 19242ad94ae6SAl Viro if (error) 19252ad94ae6SAl Viro putname(s); 19262ad94ae6SAl Viro else 19272ad94ae6SAl Viro *name = s; 19282ad94ae6SAl Viro 19292ad94ae6SAl Viro return error; 19302ad94ae6SAl Viro } 19312ad94ae6SAl Viro 19321da177e4SLinus Torvalds /* 19331da177e4SLinus Torvalds * It's inline, so penalty for filesystems that don't use sticky bit is 19341da177e4SLinus Torvalds * minimal. 19351da177e4SLinus Torvalds */ 19361da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode) 19371da177e4SLinus Torvalds { 1938da9592edSDavid Howells uid_t fsuid = current_fsuid(); 1939da9592edSDavid Howells 19401da177e4SLinus Torvalds if (!(dir->i_mode & S_ISVTX)) 19411da177e4SLinus Torvalds return 0; 1942e795b717SSerge E. Hallyn if (current_user_ns() != inode_userns(inode)) 1943e795b717SSerge E. Hallyn goto other_userns; 1944da9592edSDavid Howells if (inode->i_uid == fsuid) 19451da177e4SLinus Torvalds return 0; 1946da9592edSDavid Howells if (dir->i_uid == fsuid) 19471da177e4SLinus Torvalds return 0; 1948e795b717SSerge E. Hallyn 1949e795b717SSerge E. Hallyn other_userns: 1950e795b717SSerge E. Hallyn return !ns_capable(inode_userns(inode), CAP_FOWNER); 19511da177e4SLinus Torvalds } 19521da177e4SLinus Torvalds 19531da177e4SLinus Torvalds /* 19541da177e4SLinus Torvalds * Check whether we can remove a link victim from directory dir, check 19551da177e4SLinus Torvalds * whether the type of victim is right. 19561da177e4SLinus Torvalds * 1. We can't do it if dir is read-only (done in permission()) 19571da177e4SLinus Torvalds * 2. We should have write and exec permissions on dir 19581da177e4SLinus Torvalds * 3. We can't remove anything from append-only dir 19591da177e4SLinus Torvalds * 4. We can't do anything with immutable dir (done in permission()) 19601da177e4SLinus Torvalds * 5. If the sticky bit on dir is set we should either 19611da177e4SLinus Torvalds * a. be owner of dir, or 19621da177e4SLinus Torvalds * b. be owner of victim, or 19631da177e4SLinus Torvalds * c. have CAP_FOWNER capability 19641da177e4SLinus Torvalds * 6. If the victim is append-only or immutable we can't do antyhing with 19651da177e4SLinus Torvalds * links pointing to it. 19661da177e4SLinus Torvalds * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. 19671da177e4SLinus Torvalds * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. 19681da177e4SLinus Torvalds * 9. We can't remove a root or mountpoint. 19691da177e4SLinus Torvalds * 10. We don't allow removal of NFS sillyrenamed files; it's handled by 19701da177e4SLinus Torvalds * nfs_async_unlink(). 19711da177e4SLinus Torvalds */ 1972858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir) 19731da177e4SLinus Torvalds { 19741da177e4SLinus Torvalds int error; 19751da177e4SLinus Torvalds 19761da177e4SLinus Torvalds if (!victim->d_inode) 19771da177e4SLinus Torvalds return -ENOENT; 19781da177e4SLinus Torvalds 19791da177e4SLinus Torvalds BUG_ON(victim->d_parent->d_inode != dir); 1980cccc6bbaSAl Viro audit_inode_child(victim, dir); 19811da177e4SLinus Torvalds 1982f419a2e3SAl Viro error = inode_permission(dir, MAY_WRITE | MAY_EXEC); 19831da177e4SLinus Torvalds if (error) 19841da177e4SLinus Torvalds return error; 19851da177e4SLinus Torvalds if (IS_APPEND(dir)) 19861da177e4SLinus Torvalds return -EPERM; 19871da177e4SLinus Torvalds if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)|| 1988f9454548SHugh Dickins IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode)) 19891da177e4SLinus Torvalds return -EPERM; 19901da177e4SLinus Torvalds if (isdir) { 19911da177e4SLinus Torvalds if (!S_ISDIR(victim->d_inode->i_mode)) 19921da177e4SLinus Torvalds return -ENOTDIR; 19931da177e4SLinus Torvalds if (IS_ROOT(victim)) 19941da177e4SLinus Torvalds return -EBUSY; 19951da177e4SLinus Torvalds } else if (S_ISDIR(victim->d_inode->i_mode)) 19961da177e4SLinus Torvalds return -EISDIR; 19971da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 19981da177e4SLinus Torvalds return -ENOENT; 19991da177e4SLinus Torvalds if (victim->d_flags & DCACHE_NFSFS_RENAMED) 20001da177e4SLinus Torvalds return -EBUSY; 20011da177e4SLinus Torvalds return 0; 20021da177e4SLinus Torvalds } 20031da177e4SLinus Torvalds 20041da177e4SLinus Torvalds /* Check whether we can create an object with dentry child in directory 20051da177e4SLinus Torvalds * dir. 20061da177e4SLinus Torvalds * 1. We can't do it if child already exists (open has special treatment for 20071da177e4SLinus Torvalds * this case, but since we are inlined it's OK) 20081da177e4SLinus Torvalds * 2. We can't do it if dir is read-only (done in permission()) 20091da177e4SLinus Torvalds * 3. We should have write and exec permissions on dir 20101da177e4SLinus Torvalds * 4. We can't do it if dir is immutable (done in permission()) 20111da177e4SLinus Torvalds */ 2012a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child) 20131da177e4SLinus Torvalds { 20141da177e4SLinus Torvalds if (child->d_inode) 20151da177e4SLinus Torvalds return -EEXIST; 20161da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 20171da177e4SLinus Torvalds return -ENOENT; 2018f419a2e3SAl Viro return inode_permission(dir, MAY_WRITE | MAY_EXEC); 20191da177e4SLinus Torvalds } 20201da177e4SLinus Torvalds 20211da177e4SLinus Torvalds /* 20221da177e4SLinus Torvalds * p1 and p2 should be directories on the same fs. 20231da177e4SLinus Torvalds */ 20241da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2) 20251da177e4SLinus Torvalds { 20261da177e4SLinus Torvalds struct dentry *p; 20271da177e4SLinus Torvalds 20281da177e4SLinus Torvalds if (p1 == p2) { 2029f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 20301da177e4SLinus Torvalds return NULL; 20311da177e4SLinus Torvalds } 20321da177e4SLinus Torvalds 2033a11f3a05SArjan van de Ven mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 20341da177e4SLinus Torvalds 2035e2761a11SOGAWA Hirofumi p = d_ancestor(p2, p1); 2036e2761a11SOGAWA Hirofumi if (p) { 2037f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT); 2038f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD); 20391da177e4SLinus Torvalds return p; 20401da177e4SLinus Torvalds } 20411da177e4SLinus Torvalds 2042e2761a11SOGAWA Hirofumi p = d_ancestor(p1, p2); 2043e2761a11SOGAWA Hirofumi if (p) { 2044f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 2045f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 20461da177e4SLinus Torvalds return p; 20471da177e4SLinus Torvalds } 20481da177e4SLinus Torvalds 2049f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 2050f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 20511da177e4SLinus Torvalds return NULL; 20521da177e4SLinus Torvalds } 20531da177e4SLinus Torvalds 20541da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2) 20551da177e4SLinus Torvalds { 20561b1dcc1bSJes Sorensen mutex_unlock(&p1->d_inode->i_mutex); 20571da177e4SLinus Torvalds if (p1 != p2) { 20581b1dcc1bSJes Sorensen mutex_unlock(&p2->d_inode->i_mutex); 2059a11f3a05SArjan van de Ven mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 20601da177e4SLinus Torvalds } 20611da177e4SLinus Torvalds } 20621da177e4SLinus Torvalds 20634acdaf27SAl Viro int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode, 20641da177e4SLinus Torvalds struct nameidata *nd) 20651da177e4SLinus Torvalds { 2066a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 20671da177e4SLinus Torvalds 20681da177e4SLinus Torvalds if (error) 20691da177e4SLinus Torvalds return error; 20701da177e4SLinus Torvalds 2071acfa4380SAl Viro if (!dir->i_op->create) 20721da177e4SLinus Torvalds return -EACCES; /* shouldn't it be ENOSYS? */ 20731da177e4SLinus Torvalds mode &= S_IALLUGO; 20741da177e4SLinus Torvalds mode |= S_IFREG; 20751da177e4SLinus Torvalds error = security_inode_create(dir, dentry, mode); 20761da177e4SLinus Torvalds if (error) 20771da177e4SLinus Torvalds return error; 20781da177e4SLinus Torvalds error = dir->i_op->create(dir, dentry, mode, nd); 2079a74574aaSStephen Smalley if (!error) 2080f38aa942SAmy Griffis fsnotify_create(dir, dentry); 20811da177e4SLinus Torvalds return error; 20821da177e4SLinus Torvalds } 20831da177e4SLinus Torvalds 208473d049a4SAl Viro static int may_open(struct path *path, int acc_mode, int flag) 20851da177e4SLinus Torvalds { 20863fb64190SChristoph Hellwig struct dentry *dentry = path->dentry; 20871da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 20881da177e4SLinus Torvalds int error; 20891da177e4SLinus Torvalds 2090bcda7652SAl Viro /* O_PATH? */ 2091bcda7652SAl Viro if (!acc_mode) 2092bcda7652SAl Viro return 0; 2093bcda7652SAl Viro 20941da177e4SLinus Torvalds if (!inode) 20951da177e4SLinus Torvalds return -ENOENT; 20961da177e4SLinus Torvalds 2097c8fe8f30SChristoph Hellwig switch (inode->i_mode & S_IFMT) { 2098c8fe8f30SChristoph Hellwig case S_IFLNK: 20991da177e4SLinus Torvalds return -ELOOP; 2100c8fe8f30SChristoph Hellwig case S_IFDIR: 2101c8fe8f30SChristoph Hellwig if (acc_mode & MAY_WRITE) 21021da177e4SLinus Torvalds return -EISDIR; 2103c8fe8f30SChristoph Hellwig break; 2104c8fe8f30SChristoph Hellwig case S_IFBLK: 2105c8fe8f30SChristoph Hellwig case S_IFCHR: 21063fb64190SChristoph Hellwig if (path->mnt->mnt_flags & MNT_NODEV) 21071da177e4SLinus Torvalds return -EACCES; 2108c8fe8f30SChristoph Hellwig /*FALLTHRU*/ 2109c8fe8f30SChristoph Hellwig case S_IFIFO: 2110c8fe8f30SChristoph Hellwig case S_IFSOCK: 21111da177e4SLinus Torvalds flag &= ~O_TRUNC; 2112c8fe8f30SChristoph Hellwig break; 21134a3fd211SDave Hansen } 2114b41572e9SDave Hansen 21153fb64190SChristoph Hellwig error = inode_permission(inode, acc_mode); 2116b41572e9SDave Hansen if (error) 2117b41572e9SDave Hansen return error; 21186146f0d5SMimi Zohar 21191da177e4SLinus Torvalds /* 21201da177e4SLinus Torvalds * An append-only file must be opened in append mode for writing. 21211da177e4SLinus Torvalds */ 21221da177e4SLinus Torvalds if (IS_APPEND(inode)) { 21238737c930SAl Viro if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND)) 21247715b521SAl Viro return -EPERM; 21251da177e4SLinus Torvalds if (flag & O_TRUNC) 21267715b521SAl Viro return -EPERM; 21271da177e4SLinus Torvalds } 21281da177e4SLinus Torvalds 21291da177e4SLinus Torvalds /* O_NOATIME can only be set by the owner or superuser */ 21302e149670SSerge E. Hallyn if (flag & O_NOATIME && !inode_owner_or_capable(inode)) 21317715b521SAl Viro return -EPERM; 21321da177e4SLinus Torvalds 2133f3c7691eSJ. Bruce Fields return 0; 21347715b521SAl Viro } 21357715b521SAl Viro 2136e1181ee6SJeff Layton static int handle_truncate(struct file *filp) 21377715b521SAl Viro { 2138e1181ee6SJeff Layton struct path *path = &filp->f_path; 21397715b521SAl Viro struct inode *inode = path->dentry->d_inode; 21407715b521SAl Viro int error = get_write_access(inode); 21411da177e4SLinus Torvalds if (error) 21427715b521SAl Viro return error; 21431da177e4SLinus Torvalds /* 21441da177e4SLinus Torvalds * Refuse to truncate files with mandatory locks held on them. 21451da177e4SLinus Torvalds */ 21461da177e4SLinus Torvalds error = locks_verify_locked(inode); 2147be6d3e56SKentaro Takeda if (!error) 2148ea0d3ab2STetsuo Handa error = security_path_truncate(path); 21491da177e4SLinus Torvalds if (!error) { 21507715b521SAl Viro error = do_truncate(path->dentry, 0, 2151d139d7ffSMiklos Szeredi ATTR_MTIME|ATTR_CTIME|ATTR_OPEN, 2152e1181ee6SJeff Layton filp); 21531da177e4SLinus Torvalds } 21541da177e4SLinus Torvalds put_write_access(inode); 2155acd0c935SMimi Zohar return error; 21561da177e4SLinus Torvalds } 21571da177e4SLinus Torvalds 2158d57999e1SDave Hansen static inline int open_to_namei_flags(int flag) 2159d57999e1SDave Hansen { 21608a5e929dSAl Viro if ((flag & O_ACCMODE) == 3) 21618a5e929dSAl Viro flag--; 2162d57999e1SDave Hansen return flag; 2163d57999e1SDave Hansen } 2164d57999e1SDave Hansen 216531e6b01fSNick Piggin /* 2166fe2d35ffSAl Viro * Handle the last step of open() 216731e6b01fSNick Piggin */ 2168fb1cc555SAl Viro static struct file *do_last(struct nameidata *nd, struct path *path, 2169c3e380b0SAl Viro const struct open_flags *op, const char *pathname) 2170fb1cc555SAl Viro { 2171a1e28038SAl Viro struct dentry *dir = nd->path.dentry; 21726c0d46c4SAl Viro struct dentry *dentry; 2173ca344a89SAl Viro int open_flag = op->open_flag; 21746c0d46c4SAl Viro int will_truncate = open_flag & O_TRUNC; 2175ca344a89SAl Viro int want_write = 0; 2176bcda7652SAl Viro int acc_mode = op->acc_mode; 2177fb1cc555SAl Viro struct file *filp; 217816c2cd71SAl Viro int error; 2179fb1cc555SAl Viro 2180c3e380b0SAl Viro nd->flags &= ~LOOKUP_PARENT; 2181c3e380b0SAl Viro nd->flags |= op->intent; 2182c3e380b0SAl Viro 21831f36f774SAl Viro switch (nd->last_type) { 21841f36f774SAl Viro case LAST_DOTDOT: 2185176306f5SNeil Brown case LAST_DOT: 2186fe2d35ffSAl Viro error = handle_dots(nd, nd->last_type); 2187fe2d35ffSAl Viro if (error) 2188fe2d35ffSAl Viro return ERR_PTR(error); 21891f36f774SAl Viro /* fallthrough */ 21901f36f774SAl Viro case LAST_ROOT: 21919f1fafeeSAl Viro error = complete_walk(nd); 219216c2cd71SAl Viro if (error) 21939f1fafeeSAl Viro return ERR_PTR(error); 2194fe2d35ffSAl Viro audit_inode(pathname, nd->path.dentry); 2195ca344a89SAl Viro if (open_flag & O_CREAT) { 219616c2cd71SAl Viro error = -EISDIR; 21971f36f774SAl Viro goto exit; 2198fe2d35ffSAl Viro } 2199fe2d35ffSAl Viro goto ok; 22001f36f774SAl Viro case LAST_BIND: 22019f1fafeeSAl Viro error = complete_walk(nd); 220216c2cd71SAl Viro if (error) 22039f1fafeeSAl Viro return ERR_PTR(error); 22041f36f774SAl Viro audit_inode(pathname, dir); 22051f36f774SAl Viro goto ok; 22061f36f774SAl Viro } 2207a2c36b45SAl Viro 2208ca344a89SAl Viro if (!(open_flag & O_CREAT)) { 2209bcda7652SAl Viro int symlink_ok = 0; 2210fe2d35ffSAl Viro if (nd->last.name[nd->last.len]) 2211fe2d35ffSAl Viro nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 2212bcda7652SAl Viro if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW)) 2213bcda7652SAl Viro symlink_ok = 1; 2214fe2d35ffSAl Viro /* we _can_ be in RCU mode here */ 2215ce57dfc1SAl Viro error = walk_component(nd, path, &nd->last, LAST_NORM, 2216ce57dfc1SAl Viro !symlink_ok); 2217ce57dfc1SAl Viro if (error < 0) 2218fe2d35ffSAl Viro return ERR_PTR(error); 2219ce57dfc1SAl Viro if (error) /* symlink */ 2220fe2d35ffSAl Viro return NULL; 2221fe2d35ffSAl Viro /* sayonara */ 22229f1fafeeSAl Viro error = complete_walk(nd); 22239f1fafeeSAl Viro if (error) 22247f6c7e62SMiklos Szeredi return ERR_PTR(error); 2225fe2d35ffSAl Viro 2226fe2d35ffSAl Viro error = -ENOTDIR; 2227fe2d35ffSAl Viro if (nd->flags & LOOKUP_DIRECTORY) { 2228ce57dfc1SAl Viro if (!nd->inode->i_op->lookup) 2229fe2d35ffSAl Viro goto exit; 2230fe2d35ffSAl Viro } 2231fe2d35ffSAl Viro audit_inode(pathname, nd->path.dentry); 2232fe2d35ffSAl Viro goto ok; 2233fe2d35ffSAl Viro } 2234fe2d35ffSAl Viro 2235fe2d35ffSAl Viro /* create side of things */ 2236a3fbbde7SAl Viro /* 2237a3fbbde7SAl Viro * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED has been 2238a3fbbde7SAl Viro * cleared when we got to the last component we are about to look up 2239a3fbbde7SAl Viro */ 22409f1fafeeSAl Viro error = complete_walk(nd); 22419f1fafeeSAl Viro if (error) 22429f1fafeeSAl Viro return ERR_PTR(error); 2243fe2d35ffSAl Viro 2244fe2d35ffSAl Viro audit_inode(pathname, dir); 224516c2cd71SAl Viro error = -EISDIR; 22461f36f774SAl Viro /* trailing slashes? */ 224731e6b01fSNick Piggin if (nd->last.name[nd->last.len]) 22481f36f774SAl Viro goto exit; 22491f36f774SAl Viro 2250a1e28038SAl Viro mutex_lock(&dir->d_inode->i_mutex); 2251a1e28038SAl Viro 22526c0d46c4SAl Viro dentry = lookup_hash(nd); 22536c0d46c4SAl Viro error = PTR_ERR(dentry); 22546c0d46c4SAl Viro if (IS_ERR(dentry)) { 2255fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2256fb1cc555SAl Viro goto exit; 2257fb1cc555SAl Viro } 2258fb1cc555SAl Viro 22596c0d46c4SAl Viro path->dentry = dentry; 22606c0d46c4SAl Viro path->mnt = nd->path.mnt; 22616c0d46c4SAl Viro 2262fb1cc555SAl Viro /* Negative dentry, just create the file */ 22636c0d46c4SAl Viro if (!dentry->d_inode) { 2264a218d0fdSAl Viro umode_t mode = op->mode; 22656c0d46c4SAl Viro if (!IS_POSIXACL(dir->d_inode)) 22666c0d46c4SAl Viro mode &= ~current_umask(); 2267fb1cc555SAl Viro /* 2268fb1cc555SAl Viro * This write is needed to ensure that a 22696c0d46c4SAl Viro * rw->ro transition does not occur between 2270fb1cc555SAl Viro * the time when the file is created and when 2271fb1cc555SAl Viro * a permanent write count is taken through 2272fb1cc555SAl Viro * the 'struct file' in nameidata_to_filp(). 2273fb1cc555SAl Viro */ 2274fb1cc555SAl Viro error = mnt_want_write(nd->path.mnt); 2275fb1cc555SAl Viro if (error) 2276fb1cc555SAl Viro goto exit_mutex_unlock; 2277ca344a89SAl Viro want_write = 1; 22789b44f1b3SAl Viro /* Don't check for write permission, don't truncate */ 2279ca344a89SAl Viro open_flag &= ~O_TRUNC; 22806c0d46c4SAl Viro will_truncate = 0; 2281bcda7652SAl Viro acc_mode = MAY_OPEN; 22826c0d46c4SAl Viro error = security_path_mknod(&nd->path, dentry, mode, 0); 22836c0d46c4SAl Viro if (error) 22846c0d46c4SAl Viro goto exit_mutex_unlock; 22856c0d46c4SAl Viro error = vfs_create(dir->d_inode, dentry, mode, nd); 22866c0d46c4SAl Viro if (error) 22876c0d46c4SAl Viro goto exit_mutex_unlock; 22886c0d46c4SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 22896c0d46c4SAl Viro dput(nd->path.dentry); 22906c0d46c4SAl Viro nd->path.dentry = dentry; 2291ca344a89SAl Viro goto common; 2292fb1cc555SAl Viro } 2293fb1cc555SAl Viro 2294fb1cc555SAl Viro /* 2295fb1cc555SAl Viro * It already exists. 2296fb1cc555SAl Viro */ 2297fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2298fb1cc555SAl Viro audit_inode(pathname, path->dentry); 2299fb1cc555SAl Viro 2300fb1cc555SAl Viro error = -EEXIST; 2301ca344a89SAl Viro if (open_flag & O_EXCL) 2302fb1cc555SAl Viro goto exit_dput; 2303fb1cc555SAl Viro 23049875cf80SDavid Howells error = follow_managed(path, nd->flags); 23059875cf80SDavid Howells if (error < 0) 2306fb1cc555SAl Viro goto exit_dput; 2307fb1cc555SAl Viro 2308a3fbbde7SAl Viro if (error) 2309a3fbbde7SAl Viro nd->flags |= LOOKUP_JUMPED; 2310a3fbbde7SAl Viro 2311fb1cc555SAl Viro error = -ENOENT; 2312fb1cc555SAl Viro if (!path->dentry->d_inode) 2313fb1cc555SAl Viro goto exit_dput; 23149e67f361SAl Viro 23159e67f361SAl Viro if (path->dentry->d_inode->i_op->follow_link) 2316fb1cc555SAl Viro return NULL; 2317fb1cc555SAl Viro 2318fb1cc555SAl Viro path_to_nameidata(path, nd); 231931e6b01fSNick Piggin nd->inode = path->dentry->d_inode; 2320a3fbbde7SAl Viro /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */ 2321a3fbbde7SAl Viro error = complete_walk(nd); 2322a3fbbde7SAl Viro if (error) 2323097b180cSMiklos Szeredi return ERR_PTR(error); 2324fb1cc555SAl Viro error = -EISDIR; 232531e6b01fSNick Piggin if (S_ISDIR(nd->inode->i_mode)) 2326fb1cc555SAl Viro goto exit; 232767ee3ad2SAl Viro ok: 23286c0d46c4SAl Viro if (!S_ISREG(nd->inode->i_mode)) 23296c0d46c4SAl Viro will_truncate = 0; 23306c0d46c4SAl Viro 23310f9d1a10SAl Viro if (will_truncate) { 23320f9d1a10SAl Viro error = mnt_want_write(nd->path.mnt); 23330f9d1a10SAl Viro if (error) 23340f9d1a10SAl Viro goto exit; 2335ca344a89SAl Viro want_write = 1; 23360f9d1a10SAl Viro } 2337ca344a89SAl Viro common: 2338bcda7652SAl Viro error = may_open(&nd->path, acc_mode, open_flag); 2339ca344a89SAl Viro if (error) 23400f9d1a10SAl Viro goto exit; 23410f9d1a10SAl Viro filp = nameidata_to_filp(nd); 23420f9d1a10SAl Viro if (!IS_ERR(filp)) { 23430f9d1a10SAl Viro error = ima_file_check(filp, op->acc_mode); 23440f9d1a10SAl Viro if (error) { 23450f9d1a10SAl Viro fput(filp); 23460f9d1a10SAl Viro filp = ERR_PTR(error); 23470f9d1a10SAl Viro } 23480f9d1a10SAl Viro } 23490f9d1a10SAl Viro if (!IS_ERR(filp)) { 23500f9d1a10SAl Viro if (will_truncate) { 23510f9d1a10SAl Viro error = handle_truncate(filp); 23520f9d1a10SAl Viro if (error) { 23530f9d1a10SAl Viro fput(filp); 23540f9d1a10SAl Viro filp = ERR_PTR(error); 23550f9d1a10SAl Viro } 23560f9d1a10SAl Viro } 23570f9d1a10SAl Viro } 2358ca344a89SAl Viro out: 2359ca344a89SAl Viro if (want_write) 23600f9d1a10SAl Viro mnt_drop_write(nd->path.mnt); 23610f9d1a10SAl Viro path_put(&nd->path); 2362fb1cc555SAl Viro return filp; 2363fb1cc555SAl Viro 2364fb1cc555SAl Viro exit_mutex_unlock: 2365fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2366fb1cc555SAl Viro exit_dput: 2367fb1cc555SAl Viro path_put_conditional(path, nd); 2368fb1cc555SAl Viro exit: 2369ca344a89SAl Viro filp = ERR_PTR(error); 2370ca344a89SAl Viro goto out; 2371fb1cc555SAl Viro } 2372fb1cc555SAl Viro 237313aab428SAl Viro static struct file *path_openat(int dfd, const char *pathname, 237473d049a4SAl Viro struct nameidata *nd, const struct open_flags *op, int flags) 23751da177e4SLinus Torvalds { 2376fe2d35ffSAl Viro struct file *base = NULL; 23774a3fd211SDave Hansen struct file *filp; 23789850c056SAl Viro struct path path; 237913aab428SAl Viro int error; 238031e6b01fSNick Piggin 238131e6b01fSNick Piggin filp = get_empty_filp(); 238231e6b01fSNick Piggin if (!filp) 238331e6b01fSNick Piggin return ERR_PTR(-ENFILE); 238431e6b01fSNick Piggin 238547c805dcSAl Viro filp->f_flags = op->open_flag; 238673d049a4SAl Viro nd->intent.open.file = filp; 238773d049a4SAl Viro nd->intent.open.flags = open_to_namei_flags(op->open_flag); 238873d049a4SAl Viro nd->intent.open.create_mode = op->mode; 238931e6b01fSNick Piggin 239073d049a4SAl Viro error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base); 239131e6b01fSNick Piggin if (unlikely(error)) 239213aab428SAl Viro goto out_filp; 239331e6b01fSNick Piggin 2394fe2d35ffSAl Viro current->total_link_count = 0; 239573d049a4SAl Viro error = link_path_walk(pathname, nd); 239631e6b01fSNick Piggin if (unlikely(error)) 239731e6b01fSNick Piggin goto out_filp; 23981da177e4SLinus Torvalds 239973d049a4SAl Viro filp = do_last(nd, &path, op, pathname); 2400806b681cSAl Viro while (unlikely(!filp)) { /* trailing symlink */ 24017b9337aaSNick Piggin struct path link = path; 2402def4af30SAl Viro void *cookie; 2403574197e0SAl Viro if (!(nd->flags & LOOKUP_FOLLOW)) { 240473d049a4SAl Viro path_put_conditional(&path, nd); 240573d049a4SAl Viro path_put(&nd->path); 240640b39136SAl Viro filp = ERR_PTR(-ELOOP); 240740b39136SAl Viro break; 240840b39136SAl Viro } 240973d049a4SAl Viro nd->flags |= LOOKUP_PARENT; 241073d049a4SAl Viro nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL); 2411574197e0SAl Viro error = follow_link(&link, nd, &cookie); 2412c3e380b0SAl Viro if (unlikely(error)) 2413f1afe9efSAl Viro filp = ERR_PTR(error); 2414c3e380b0SAl Viro else 241573d049a4SAl Viro filp = do_last(nd, &path, op, pathname); 2416574197e0SAl Viro put_link(nd, &link, cookie); 2417806b681cSAl Viro } 241810fa8e62SAl Viro out: 241973d049a4SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) 242073d049a4SAl Viro path_put(&nd->root); 2421fe2d35ffSAl Viro if (base) 2422fe2d35ffSAl Viro fput(base); 242373d049a4SAl Viro release_open_intent(nd); 242410fa8e62SAl Viro return filp; 24251da177e4SLinus Torvalds 242631e6b01fSNick Piggin out_filp: 242710fa8e62SAl Viro filp = ERR_PTR(error); 242810fa8e62SAl Viro goto out; 2429de459215SKirill Korotaev } 24301da177e4SLinus Torvalds 243113aab428SAl Viro struct file *do_filp_open(int dfd, const char *pathname, 243213aab428SAl Viro const struct open_flags *op, int flags) 243313aab428SAl Viro { 243473d049a4SAl Viro struct nameidata nd; 243513aab428SAl Viro struct file *filp; 243613aab428SAl Viro 243773d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU); 243813aab428SAl Viro if (unlikely(filp == ERR_PTR(-ECHILD))) 243973d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags); 244013aab428SAl Viro if (unlikely(filp == ERR_PTR(-ESTALE))) 244173d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL); 244213aab428SAl Viro return filp; 244313aab428SAl Viro } 244413aab428SAl Viro 244573d049a4SAl Viro struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt, 244673d049a4SAl Viro const char *name, const struct open_flags *op, int flags) 244773d049a4SAl Viro { 244873d049a4SAl Viro struct nameidata nd; 244973d049a4SAl Viro struct file *file; 245073d049a4SAl Viro 245173d049a4SAl Viro nd.root.mnt = mnt; 245273d049a4SAl Viro nd.root.dentry = dentry; 245373d049a4SAl Viro 245473d049a4SAl Viro flags |= LOOKUP_ROOT; 245573d049a4SAl Viro 2456bcda7652SAl Viro if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN) 245773d049a4SAl Viro return ERR_PTR(-ELOOP); 245873d049a4SAl Viro 245973d049a4SAl Viro file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU); 246073d049a4SAl Viro if (unlikely(file == ERR_PTR(-ECHILD))) 246173d049a4SAl Viro file = path_openat(-1, name, &nd, op, flags); 246273d049a4SAl Viro if (unlikely(file == ERR_PTR(-ESTALE))) 246373d049a4SAl Viro file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL); 246473d049a4SAl Viro return file; 246573d049a4SAl Viro } 246673d049a4SAl Viro 2467ed75e95dSAl Viro struct dentry *kern_path_create(int dfd, const char *pathname, struct path *path, int is_dir) 24681da177e4SLinus Torvalds { 2469c663e5d8SChristoph Hellwig struct dentry *dentry = ERR_PTR(-EEXIST); 2470ed75e95dSAl Viro struct nameidata nd; 2471ed75e95dSAl Viro int error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd); 2472ed75e95dSAl Viro if (error) 2473ed75e95dSAl Viro return ERR_PTR(error); 24741da177e4SLinus Torvalds 2475c663e5d8SChristoph Hellwig /* 2476c663e5d8SChristoph Hellwig * Yucky last component or no last component at all? 2477c663e5d8SChristoph Hellwig * (foo/., foo/.., /////) 2478c663e5d8SChristoph Hellwig */ 2479ed75e95dSAl Viro if (nd.last_type != LAST_NORM) 2480ed75e95dSAl Viro goto out; 2481ed75e95dSAl Viro nd.flags &= ~LOOKUP_PARENT; 2482ed75e95dSAl Viro nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL; 2483ed75e95dSAl Viro nd.intent.open.flags = O_EXCL; 2484c663e5d8SChristoph Hellwig 2485c663e5d8SChristoph Hellwig /* 2486c663e5d8SChristoph Hellwig * Do the final lookup. 2487c663e5d8SChristoph Hellwig */ 2488ed75e95dSAl Viro mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 2489ed75e95dSAl Viro dentry = lookup_hash(&nd); 24901da177e4SLinus Torvalds if (IS_ERR(dentry)) 24911da177e4SLinus Torvalds goto fail; 2492c663e5d8SChristoph Hellwig 2493e9baf6e5SAl Viro if (dentry->d_inode) 2494e9baf6e5SAl Viro goto eexist; 2495c663e5d8SChristoph Hellwig /* 2496c663e5d8SChristoph Hellwig * Special case - lookup gave negative, but... we had foo/bar/ 2497c663e5d8SChristoph Hellwig * From the vfs_mknod() POV we just have a negative dentry - 2498c663e5d8SChristoph Hellwig * all is fine. Let's be bastards - you had / on the end, you've 2499c663e5d8SChristoph Hellwig * been asking for (non-existent) directory. -ENOENT for you. 2500c663e5d8SChristoph Hellwig */ 2501ed75e95dSAl Viro if (unlikely(!is_dir && nd.last.name[nd.last.len])) { 25021da177e4SLinus Torvalds dput(dentry); 25031da177e4SLinus Torvalds dentry = ERR_PTR(-ENOENT); 2504ed75e95dSAl Viro goto fail; 2505e9baf6e5SAl Viro } 2506ed75e95dSAl Viro *path = nd.path; 2507e9baf6e5SAl Viro return dentry; 2508e9baf6e5SAl Viro eexist: 2509e9baf6e5SAl Viro dput(dentry); 2510e9baf6e5SAl Viro dentry = ERR_PTR(-EEXIST); 25111da177e4SLinus Torvalds fail: 2512dae6ad8fSAl Viro mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 2513ed75e95dSAl Viro out: 2514dae6ad8fSAl Viro path_put(&nd.path); 2515ed75e95dSAl Viro return dentry; 2516dae6ad8fSAl Viro } 2517dae6ad8fSAl Viro EXPORT_SYMBOL(kern_path_create); 2518dae6ad8fSAl Viro 2519dae6ad8fSAl Viro struct dentry *user_path_create(int dfd, const char __user *pathname, struct path *path, int is_dir) 2520dae6ad8fSAl Viro { 2521dae6ad8fSAl Viro char *tmp = getname(pathname); 2522dae6ad8fSAl Viro struct dentry *res; 2523dae6ad8fSAl Viro if (IS_ERR(tmp)) 2524dae6ad8fSAl Viro return ERR_CAST(tmp); 2525dae6ad8fSAl Viro res = kern_path_create(dfd, tmp, path, is_dir); 2526dae6ad8fSAl Viro putname(tmp); 2527dae6ad8fSAl Viro return res; 2528dae6ad8fSAl Viro } 2529dae6ad8fSAl Viro EXPORT_SYMBOL(user_path_create); 2530dae6ad8fSAl Viro 25311a67aafbSAl Viro int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev) 25321da177e4SLinus Torvalds { 2533a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 25341da177e4SLinus Torvalds 25351da177e4SLinus Torvalds if (error) 25361da177e4SLinus Torvalds return error; 25371da177e4SLinus Torvalds 2538e795b717SSerge E. Hallyn if ((S_ISCHR(mode) || S_ISBLK(mode)) && 2539e795b717SSerge E. Hallyn !ns_capable(inode_userns(dir), CAP_MKNOD)) 25401da177e4SLinus Torvalds return -EPERM; 25411da177e4SLinus Torvalds 2542acfa4380SAl Viro if (!dir->i_op->mknod) 25431da177e4SLinus Torvalds return -EPERM; 25441da177e4SLinus Torvalds 254508ce5f16SSerge E. Hallyn error = devcgroup_inode_mknod(mode, dev); 254608ce5f16SSerge E. Hallyn if (error) 254708ce5f16SSerge E. Hallyn return error; 254808ce5f16SSerge E. Hallyn 25491da177e4SLinus Torvalds error = security_inode_mknod(dir, dentry, mode, dev); 25501da177e4SLinus Torvalds if (error) 25511da177e4SLinus Torvalds return error; 25521da177e4SLinus Torvalds 25531da177e4SLinus Torvalds error = dir->i_op->mknod(dir, dentry, mode, dev); 2554a74574aaSStephen Smalley if (!error) 2555f38aa942SAmy Griffis fsnotify_create(dir, dentry); 25561da177e4SLinus Torvalds return error; 25571da177e4SLinus Torvalds } 25581da177e4SLinus Torvalds 2559f69aac00SAl Viro static int may_mknod(umode_t mode) 2560463c3197SDave Hansen { 2561463c3197SDave Hansen switch (mode & S_IFMT) { 2562463c3197SDave Hansen case S_IFREG: 2563463c3197SDave Hansen case S_IFCHR: 2564463c3197SDave Hansen case S_IFBLK: 2565463c3197SDave Hansen case S_IFIFO: 2566463c3197SDave Hansen case S_IFSOCK: 2567463c3197SDave Hansen case 0: /* zero mode translates to S_IFREG */ 2568463c3197SDave Hansen return 0; 2569463c3197SDave Hansen case S_IFDIR: 2570463c3197SDave Hansen return -EPERM; 2571463c3197SDave Hansen default: 2572463c3197SDave Hansen return -EINVAL; 2573463c3197SDave Hansen } 2574463c3197SDave Hansen } 2575463c3197SDave Hansen 25768208a22bSAl Viro SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode, 25772e4d0924SHeiko Carstens unsigned, dev) 25781da177e4SLinus Torvalds { 25791da177e4SLinus Torvalds struct dentry *dentry; 2580dae6ad8fSAl Viro struct path path; 2581dae6ad8fSAl Viro int error; 25821da177e4SLinus Torvalds 25831da177e4SLinus Torvalds if (S_ISDIR(mode)) 25841da177e4SLinus Torvalds return -EPERM; 25851da177e4SLinus Torvalds 2586dae6ad8fSAl Viro dentry = user_path_create(dfd, filename, &path, 0); 2587dae6ad8fSAl Viro if (IS_ERR(dentry)) 2588dae6ad8fSAl Viro return PTR_ERR(dentry); 25892ad94ae6SAl Viro 2590dae6ad8fSAl Viro if (!IS_POSIXACL(path.dentry->d_inode)) 2591ce3b0f8dSAl Viro mode &= ~current_umask(); 2592463c3197SDave Hansen error = may_mknod(mode); 2593463c3197SDave Hansen if (error) 2594463c3197SDave Hansen goto out_dput; 2595dae6ad8fSAl Viro error = mnt_want_write(path.mnt); 2596463c3197SDave Hansen if (error) 2597463c3197SDave Hansen goto out_dput; 2598dae6ad8fSAl Viro error = security_path_mknod(&path, dentry, mode, dev); 2599be6d3e56SKentaro Takeda if (error) 2600be6d3e56SKentaro Takeda goto out_drop_write; 26011da177e4SLinus Torvalds switch (mode & S_IFMT) { 26021da177e4SLinus Torvalds case 0: case S_IFREG: 2603dae6ad8fSAl Viro error = vfs_create(path.dentry->d_inode,dentry,mode,NULL); 26041da177e4SLinus Torvalds break; 26051da177e4SLinus Torvalds case S_IFCHR: case S_IFBLK: 2606dae6ad8fSAl Viro error = vfs_mknod(path.dentry->d_inode,dentry,mode, 26071da177e4SLinus Torvalds new_decode_dev(dev)); 26081da177e4SLinus Torvalds break; 26091da177e4SLinus Torvalds case S_IFIFO: case S_IFSOCK: 2610dae6ad8fSAl Viro error = vfs_mknod(path.dentry->d_inode,dentry,mode,0); 26111da177e4SLinus Torvalds break; 26121da177e4SLinus Torvalds } 2613be6d3e56SKentaro Takeda out_drop_write: 2614dae6ad8fSAl Viro mnt_drop_write(path.mnt); 2615463c3197SDave Hansen out_dput: 26161da177e4SLinus Torvalds dput(dentry); 2617dae6ad8fSAl Viro mutex_unlock(&path.dentry->d_inode->i_mutex); 2618dae6ad8fSAl Viro path_put(&path); 26191da177e4SLinus Torvalds 26201da177e4SLinus Torvalds return error; 26211da177e4SLinus Torvalds } 26221da177e4SLinus Torvalds 26238208a22bSAl Viro SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev) 26245590ff0dSUlrich Drepper { 26255590ff0dSUlrich Drepper return sys_mknodat(AT_FDCWD, filename, mode, dev); 26265590ff0dSUlrich Drepper } 26275590ff0dSUlrich Drepper 262818bb1db3SAl Viro int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) 26291da177e4SLinus Torvalds { 2630a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 26318de52778SAl Viro unsigned max_links = dir->i_sb->s_max_links; 26321da177e4SLinus Torvalds 26331da177e4SLinus Torvalds if (error) 26341da177e4SLinus Torvalds return error; 26351da177e4SLinus Torvalds 2636acfa4380SAl Viro if (!dir->i_op->mkdir) 26371da177e4SLinus Torvalds return -EPERM; 26381da177e4SLinus Torvalds 26391da177e4SLinus Torvalds mode &= (S_IRWXUGO|S_ISVTX); 26401da177e4SLinus Torvalds error = security_inode_mkdir(dir, dentry, mode); 26411da177e4SLinus Torvalds if (error) 26421da177e4SLinus Torvalds return error; 26431da177e4SLinus Torvalds 26448de52778SAl Viro if (max_links && dir->i_nlink >= max_links) 26458de52778SAl Viro return -EMLINK; 26468de52778SAl Viro 26471da177e4SLinus Torvalds error = dir->i_op->mkdir(dir, dentry, mode); 2648a74574aaSStephen Smalley if (!error) 2649f38aa942SAmy Griffis fsnotify_mkdir(dir, dentry); 26501da177e4SLinus Torvalds return error; 26511da177e4SLinus Torvalds } 26521da177e4SLinus Torvalds 2653a218d0fdSAl Viro SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode) 26541da177e4SLinus Torvalds { 26556902d925SDave Hansen struct dentry *dentry; 2656dae6ad8fSAl Viro struct path path; 2657dae6ad8fSAl Viro int error; 26581da177e4SLinus Torvalds 2659dae6ad8fSAl Viro dentry = user_path_create(dfd, pathname, &path, 1); 26606902d925SDave Hansen if (IS_ERR(dentry)) 2661dae6ad8fSAl Viro return PTR_ERR(dentry); 26626902d925SDave Hansen 2663dae6ad8fSAl Viro if (!IS_POSIXACL(path.dentry->d_inode)) 2664ce3b0f8dSAl Viro mode &= ~current_umask(); 2665dae6ad8fSAl Viro error = mnt_want_write(path.mnt); 2666463c3197SDave Hansen if (error) 2667463c3197SDave Hansen goto out_dput; 2668dae6ad8fSAl Viro error = security_path_mkdir(&path, dentry, mode); 2669be6d3e56SKentaro Takeda if (error) 2670be6d3e56SKentaro Takeda goto out_drop_write; 2671dae6ad8fSAl Viro error = vfs_mkdir(path.dentry->d_inode, dentry, mode); 2672be6d3e56SKentaro Takeda out_drop_write: 2673dae6ad8fSAl Viro mnt_drop_write(path.mnt); 2674463c3197SDave Hansen out_dput: 26751da177e4SLinus Torvalds dput(dentry); 2676dae6ad8fSAl Viro mutex_unlock(&path.dentry->d_inode->i_mutex); 2677dae6ad8fSAl Viro path_put(&path); 26781da177e4SLinus Torvalds return error; 26791da177e4SLinus Torvalds } 26801da177e4SLinus Torvalds 2681a218d0fdSAl Viro SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode) 26825590ff0dSUlrich Drepper { 26835590ff0dSUlrich Drepper return sys_mkdirat(AT_FDCWD, pathname, mode); 26845590ff0dSUlrich Drepper } 26855590ff0dSUlrich Drepper 26861da177e4SLinus Torvalds /* 2687a71905f0SSage Weil * The dentry_unhash() helper will try to drop the dentry early: we 2688c0d02594SJ. Bruce Fields * should have a usage count of 1 if we're the only user of this 2689a71905f0SSage Weil * dentry, and if that is true (possibly after pruning the dcache), 2690a71905f0SSage Weil * then we drop the dentry now. 26911da177e4SLinus Torvalds * 26921da177e4SLinus Torvalds * A low-level filesystem can, if it choses, legally 26931da177e4SLinus Torvalds * do a 26941da177e4SLinus Torvalds * 26951da177e4SLinus Torvalds * if (!d_unhashed(dentry)) 26961da177e4SLinus Torvalds * return -EBUSY; 26971da177e4SLinus Torvalds * 26981da177e4SLinus Torvalds * if it cannot handle the case of removing a directory 26991da177e4SLinus Torvalds * that is still in use by something else.. 27001da177e4SLinus Torvalds */ 27011da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry) 27021da177e4SLinus Torvalds { 27031da177e4SLinus Torvalds shrink_dcache_parent(dentry); 27041da177e4SLinus Torvalds spin_lock(&dentry->d_lock); 270564252c75SSage Weil if (dentry->d_count == 1) 27061da177e4SLinus Torvalds __d_drop(dentry); 27071da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 27081da177e4SLinus Torvalds } 27091da177e4SLinus Torvalds 27101da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry) 27111da177e4SLinus Torvalds { 27121da177e4SLinus Torvalds int error = may_delete(dir, dentry, 1); 27131da177e4SLinus Torvalds 27141da177e4SLinus Torvalds if (error) 27151da177e4SLinus Torvalds return error; 27161da177e4SLinus Torvalds 2717acfa4380SAl Viro if (!dir->i_op->rmdir) 27181da177e4SLinus Torvalds return -EPERM; 27191da177e4SLinus Torvalds 27201d2ef590SAl Viro dget(dentry); 27211b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 2722912dbc15SSage Weil 27231da177e4SLinus Torvalds error = -EBUSY; 2724912dbc15SSage Weil if (d_mountpoint(dentry)) 2725912dbc15SSage Weil goto out; 2726912dbc15SSage Weil 27271da177e4SLinus Torvalds error = security_inode_rmdir(dir, dentry); 2728912dbc15SSage Weil if (error) 2729912dbc15SSage Weil goto out; 2730912dbc15SSage Weil 27313cebde24SSage Weil shrink_dcache_parent(dentry); 27321da177e4SLinus Torvalds error = dir->i_op->rmdir(dir, dentry); 2733912dbc15SSage Weil if (error) 2734912dbc15SSage Weil goto out; 2735912dbc15SSage Weil 27361da177e4SLinus Torvalds dentry->d_inode->i_flags |= S_DEAD; 2737d83c49f3SAl Viro dont_mount(dentry); 27381da177e4SLinus Torvalds 2739912dbc15SSage Weil out: 2740912dbc15SSage Weil mutex_unlock(&dentry->d_inode->i_mutex); 27411d2ef590SAl Viro dput(dentry); 2742912dbc15SSage Weil if (!error) 2743912dbc15SSage Weil d_delete(dentry); 27441da177e4SLinus Torvalds return error; 27451da177e4SLinus Torvalds } 27461da177e4SLinus Torvalds 27475590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname) 27481da177e4SLinus Torvalds { 27491da177e4SLinus Torvalds int error = 0; 27501da177e4SLinus Torvalds char * name; 27511da177e4SLinus Torvalds struct dentry *dentry; 27521da177e4SLinus Torvalds struct nameidata nd; 27531da177e4SLinus Torvalds 27542ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &name); 27551da177e4SLinus Torvalds if (error) 27562ad94ae6SAl Viro return error; 27571da177e4SLinus Torvalds 27581da177e4SLinus Torvalds switch(nd.last_type) { 27591da177e4SLinus Torvalds case LAST_DOTDOT: 27601da177e4SLinus Torvalds error = -ENOTEMPTY; 27611da177e4SLinus Torvalds goto exit1; 27621da177e4SLinus Torvalds case LAST_DOT: 27631da177e4SLinus Torvalds error = -EINVAL; 27641da177e4SLinus Torvalds goto exit1; 27651da177e4SLinus Torvalds case LAST_ROOT: 27661da177e4SLinus Torvalds error = -EBUSY; 27671da177e4SLinus Torvalds goto exit1; 27681da177e4SLinus Torvalds } 27690612d9fbSOGAWA Hirofumi 27700612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 27710612d9fbSOGAWA Hirofumi 27724ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 277349705b77SChristoph Hellwig dentry = lookup_hash(&nd); 27741da177e4SLinus Torvalds error = PTR_ERR(dentry); 27756902d925SDave Hansen if (IS_ERR(dentry)) 27766902d925SDave Hansen goto exit2; 2777e6bc45d6STheodore Ts'o if (!dentry->d_inode) { 2778e6bc45d6STheodore Ts'o error = -ENOENT; 2779e6bc45d6STheodore Ts'o goto exit3; 2780e6bc45d6STheodore Ts'o } 27810622753bSDave Hansen error = mnt_want_write(nd.path.mnt); 27820622753bSDave Hansen if (error) 27830622753bSDave Hansen goto exit3; 2784be6d3e56SKentaro Takeda error = security_path_rmdir(&nd.path, dentry); 2785be6d3e56SKentaro Takeda if (error) 2786be6d3e56SKentaro Takeda goto exit4; 27874ac91378SJan Blunck error = vfs_rmdir(nd.path.dentry->d_inode, dentry); 2788be6d3e56SKentaro Takeda exit4: 27890622753bSDave Hansen mnt_drop_write(nd.path.mnt); 27900622753bSDave Hansen exit3: 27911da177e4SLinus Torvalds dput(dentry); 27926902d925SDave Hansen exit2: 27934ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 27941da177e4SLinus Torvalds exit1: 27951d957f9bSJan Blunck path_put(&nd.path); 27961da177e4SLinus Torvalds putname(name); 27971da177e4SLinus Torvalds return error; 27981da177e4SLinus Torvalds } 27991da177e4SLinus Torvalds 28003cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname) 28015590ff0dSUlrich Drepper { 28025590ff0dSUlrich Drepper return do_rmdir(AT_FDCWD, pathname); 28035590ff0dSUlrich Drepper } 28045590ff0dSUlrich Drepper 28051da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry) 28061da177e4SLinus Torvalds { 28071da177e4SLinus Torvalds int error = may_delete(dir, dentry, 0); 28081da177e4SLinus Torvalds 28091da177e4SLinus Torvalds if (error) 28101da177e4SLinus Torvalds return error; 28111da177e4SLinus Torvalds 2812acfa4380SAl Viro if (!dir->i_op->unlink) 28131da177e4SLinus Torvalds return -EPERM; 28141da177e4SLinus Torvalds 28151b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 28161da177e4SLinus Torvalds if (d_mountpoint(dentry)) 28171da177e4SLinus Torvalds error = -EBUSY; 28181da177e4SLinus Torvalds else { 28191da177e4SLinus Torvalds error = security_inode_unlink(dir, dentry); 2820bec1052eSAl Viro if (!error) { 28211da177e4SLinus Torvalds error = dir->i_op->unlink(dir, dentry); 2822bec1052eSAl Viro if (!error) 2823d83c49f3SAl Viro dont_mount(dentry); 2824bec1052eSAl Viro } 28251da177e4SLinus Torvalds } 28261b1dcc1bSJes Sorensen mutex_unlock(&dentry->d_inode->i_mutex); 28271da177e4SLinus Torvalds 28281da177e4SLinus Torvalds /* We don't d_delete() NFS sillyrenamed files--they still exist. */ 28291da177e4SLinus Torvalds if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) { 2830ece95912SJan Kara fsnotify_link_count(dentry->d_inode); 28311da177e4SLinus Torvalds d_delete(dentry); 28321da177e4SLinus Torvalds } 28330eeca283SRobert Love 28341da177e4SLinus Torvalds return error; 28351da177e4SLinus Torvalds } 28361da177e4SLinus Torvalds 28371da177e4SLinus Torvalds /* 28381da177e4SLinus Torvalds * Make sure that the actual truncation of the file will occur outside its 28391b1dcc1bSJes Sorensen * directory's i_mutex. Truncate can take a long time if there is a lot of 28401da177e4SLinus Torvalds * writeout happening, and we don't want to prevent access to the directory 28411da177e4SLinus Torvalds * while waiting on the I/O. 28421da177e4SLinus Torvalds */ 28435590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname) 28441da177e4SLinus Torvalds { 28452ad94ae6SAl Viro int error; 28461da177e4SLinus Torvalds char *name; 28471da177e4SLinus Torvalds struct dentry *dentry; 28481da177e4SLinus Torvalds struct nameidata nd; 28491da177e4SLinus Torvalds struct inode *inode = NULL; 28501da177e4SLinus Torvalds 28512ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &name); 28521da177e4SLinus Torvalds if (error) 28532ad94ae6SAl Viro return error; 28542ad94ae6SAl Viro 28551da177e4SLinus Torvalds error = -EISDIR; 28561da177e4SLinus Torvalds if (nd.last_type != LAST_NORM) 28571da177e4SLinus Torvalds goto exit1; 28580612d9fbSOGAWA Hirofumi 28590612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 28600612d9fbSOGAWA Hirofumi 28614ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 286249705b77SChristoph Hellwig dentry = lookup_hash(&nd); 28631da177e4SLinus Torvalds error = PTR_ERR(dentry); 28641da177e4SLinus Torvalds if (!IS_ERR(dentry)) { 28651da177e4SLinus Torvalds /* Why not before? Because we want correct error value */ 286650338b88STörök Edwin if (nd.last.name[nd.last.len]) 286750338b88STörök Edwin goto slashes; 28681da177e4SLinus Torvalds inode = dentry->d_inode; 286950338b88STörök Edwin if (!inode) 2870e6bc45d6STheodore Ts'o goto slashes; 28717de9c6eeSAl Viro ihold(inode); 28720622753bSDave Hansen error = mnt_want_write(nd.path.mnt); 28730622753bSDave Hansen if (error) 28740622753bSDave Hansen goto exit2; 2875be6d3e56SKentaro Takeda error = security_path_unlink(&nd.path, dentry); 2876be6d3e56SKentaro Takeda if (error) 2877be6d3e56SKentaro Takeda goto exit3; 28784ac91378SJan Blunck error = vfs_unlink(nd.path.dentry->d_inode, dentry); 2879be6d3e56SKentaro Takeda exit3: 28800622753bSDave Hansen mnt_drop_write(nd.path.mnt); 28811da177e4SLinus Torvalds exit2: 28821da177e4SLinus Torvalds dput(dentry); 28831da177e4SLinus Torvalds } 28844ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 28851da177e4SLinus Torvalds if (inode) 28861da177e4SLinus Torvalds iput(inode); /* truncate the inode here */ 28871da177e4SLinus Torvalds exit1: 28881d957f9bSJan Blunck path_put(&nd.path); 28891da177e4SLinus Torvalds putname(name); 28901da177e4SLinus Torvalds return error; 28911da177e4SLinus Torvalds 28921da177e4SLinus Torvalds slashes: 28931da177e4SLinus Torvalds error = !dentry->d_inode ? -ENOENT : 28941da177e4SLinus Torvalds S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR; 28951da177e4SLinus Torvalds goto exit2; 28961da177e4SLinus Torvalds } 28971da177e4SLinus Torvalds 28982e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag) 28995590ff0dSUlrich Drepper { 29005590ff0dSUlrich Drepper if ((flag & ~AT_REMOVEDIR) != 0) 29015590ff0dSUlrich Drepper return -EINVAL; 29025590ff0dSUlrich Drepper 29035590ff0dSUlrich Drepper if (flag & AT_REMOVEDIR) 29045590ff0dSUlrich Drepper return do_rmdir(dfd, pathname); 29055590ff0dSUlrich Drepper 29065590ff0dSUlrich Drepper return do_unlinkat(dfd, pathname); 29075590ff0dSUlrich Drepper } 29085590ff0dSUlrich Drepper 29093480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname) 29105590ff0dSUlrich Drepper { 29115590ff0dSUlrich Drepper return do_unlinkat(AT_FDCWD, pathname); 29125590ff0dSUlrich Drepper } 29135590ff0dSUlrich Drepper 2914db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname) 29151da177e4SLinus Torvalds { 2916a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 29171da177e4SLinus Torvalds 29181da177e4SLinus Torvalds if (error) 29191da177e4SLinus Torvalds return error; 29201da177e4SLinus Torvalds 2921acfa4380SAl Viro if (!dir->i_op->symlink) 29221da177e4SLinus Torvalds return -EPERM; 29231da177e4SLinus Torvalds 29241da177e4SLinus Torvalds error = security_inode_symlink(dir, dentry, oldname); 29251da177e4SLinus Torvalds if (error) 29261da177e4SLinus Torvalds return error; 29271da177e4SLinus Torvalds 29281da177e4SLinus Torvalds error = dir->i_op->symlink(dir, dentry, oldname); 2929a74574aaSStephen Smalley if (!error) 2930f38aa942SAmy Griffis fsnotify_create(dir, dentry); 29311da177e4SLinus Torvalds return error; 29321da177e4SLinus Torvalds } 29331da177e4SLinus Torvalds 29342e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname, 29352e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 29361da177e4SLinus Torvalds { 29372ad94ae6SAl Viro int error; 29381da177e4SLinus Torvalds char *from; 29396902d925SDave Hansen struct dentry *dentry; 2940dae6ad8fSAl Viro struct path path; 29411da177e4SLinus Torvalds 29421da177e4SLinus Torvalds from = getname(oldname); 29431da177e4SLinus Torvalds if (IS_ERR(from)) 29441da177e4SLinus Torvalds return PTR_ERR(from); 29452ad94ae6SAl Viro 2946dae6ad8fSAl Viro dentry = user_path_create(newdfd, newname, &path, 0); 29471da177e4SLinus Torvalds error = PTR_ERR(dentry); 29486902d925SDave Hansen if (IS_ERR(dentry)) 2949dae6ad8fSAl Viro goto out_putname; 29506902d925SDave Hansen 2951dae6ad8fSAl Viro error = mnt_want_write(path.mnt); 295275c3f29dSDave Hansen if (error) 295375c3f29dSDave Hansen goto out_dput; 2954dae6ad8fSAl Viro error = security_path_symlink(&path, dentry, from); 2955be6d3e56SKentaro Takeda if (error) 2956be6d3e56SKentaro Takeda goto out_drop_write; 2957dae6ad8fSAl Viro error = vfs_symlink(path.dentry->d_inode, dentry, from); 2958be6d3e56SKentaro Takeda out_drop_write: 2959dae6ad8fSAl Viro mnt_drop_write(path.mnt); 296075c3f29dSDave Hansen out_dput: 29611da177e4SLinus Torvalds dput(dentry); 2962dae6ad8fSAl Viro mutex_unlock(&path.dentry->d_inode->i_mutex); 2963dae6ad8fSAl Viro path_put(&path); 29646902d925SDave Hansen out_putname: 29651da177e4SLinus Torvalds putname(from); 29661da177e4SLinus Torvalds return error; 29671da177e4SLinus Torvalds } 29681da177e4SLinus Torvalds 29693480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname) 29705590ff0dSUlrich Drepper { 29715590ff0dSUlrich Drepper return sys_symlinkat(oldname, AT_FDCWD, newname); 29725590ff0dSUlrich Drepper } 29735590ff0dSUlrich Drepper 29741da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) 29751da177e4SLinus Torvalds { 29761da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 29778de52778SAl Viro unsigned max_links = dir->i_sb->s_max_links; 29781da177e4SLinus Torvalds int error; 29791da177e4SLinus Torvalds 29801da177e4SLinus Torvalds if (!inode) 29811da177e4SLinus Torvalds return -ENOENT; 29821da177e4SLinus Torvalds 2983a95164d9SMiklos Szeredi error = may_create(dir, new_dentry); 29841da177e4SLinus Torvalds if (error) 29851da177e4SLinus Torvalds return error; 29861da177e4SLinus Torvalds 29871da177e4SLinus Torvalds if (dir->i_sb != inode->i_sb) 29881da177e4SLinus Torvalds return -EXDEV; 29891da177e4SLinus Torvalds 29901da177e4SLinus Torvalds /* 29911da177e4SLinus Torvalds * A link to an append-only or immutable file cannot be created. 29921da177e4SLinus Torvalds */ 29931da177e4SLinus Torvalds if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) 29941da177e4SLinus Torvalds return -EPERM; 2995acfa4380SAl Viro if (!dir->i_op->link) 29961da177e4SLinus Torvalds return -EPERM; 29977e79eedbSTetsuo Handa if (S_ISDIR(inode->i_mode)) 29981da177e4SLinus Torvalds return -EPERM; 29991da177e4SLinus Torvalds 30001da177e4SLinus Torvalds error = security_inode_link(old_dentry, dir, new_dentry); 30011da177e4SLinus Torvalds if (error) 30021da177e4SLinus Torvalds return error; 30031da177e4SLinus Torvalds 30047e79eedbSTetsuo Handa mutex_lock(&inode->i_mutex); 3005aae8a97dSAneesh Kumar K.V /* Make sure we don't allow creating hardlink to an unlinked file */ 3006aae8a97dSAneesh Kumar K.V if (inode->i_nlink == 0) 3007aae8a97dSAneesh Kumar K.V error = -ENOENT; 30088de52778SAl Viro else if (max_links && inode->i_nlink >= max_links) 30098de52778SAl Viro error = -EMLINK; 3010aae8a97dSAneesh Kumar K.V else 30111da177e4SLinus Torvalds error = dir->i_op->link(old_dentry, dir, new_dentry); 30127e79eedbSTetsuo Handa mutex_unlock(&inode->i_mutex); 3013e31e14ecSStephen Smalley if (!error) 30147e79eedbSTetsuo Handa fsnotify_link(dir, inode, new_dentry); 30151da177e4SLinus Torvalds return error; 30161da177e4SLinus Torvalds } 30171da177e4SLinus Torvalds 30181da177e4SLinus Torvalds /* 30191da177e4SLinus Torvalds * Hardlinks are often used in delicate situations. We avoid 30201da177e4SLinus Torvalds * security-related surprises by not following symlinks on the 30211da177e4SLinus Torvalds * newname. --KAB 30221da177e4SLinus Torvalds * 30231da177e4SLinus Torvalds * We don't follow them on the oldname either to be compatible 30241da177e4SLinus Torvalds * with linux 2.0, and to avoid hard-linking to directories 30251da177e4SLinus Torvalds * and other special files. --ADM 30261da177e4SLinus Torvalds */ 30272e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname, 30282e4d0924SHeiko Carstens int, newdfd, const char __user *, newname, int, flags) 30291da177e4SLinus Torvalds { 30301da177e4SLinus Torvalds struct dentry *new_dentry; 3031dae6ad8fSAl Viro struct path old_path, new_path; 303211a7b371SAneesh Kumar K.V int how = 0; 30331da177e4SLinus Torvalds int error; 30341da177e4SLinus Torvalds 303511a7b371SAneesh Kumar K.V if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0) 3036c04030e1SUlrich Drepper return -EINVAL; 303711a7b371SAneesh Kumar K.V /* 303811a7b371SAneesh Kumar K.V * To use null names we require CAP_DAC_READ_SEARCH 303911a7b371SAneesh Kumar K.V * This ensures that not everyone will be able to create 304011a7b371SAneesh Kumar K.V * handlink using the passed filedescriptor. 304111a7b371SAneesh Kumar K.V */ 304211a7b371SAneesh Kumar K.V if (flags & AT_EMPTY_PATH) { 304311a7b371SAneesh Kumar K.V if (!capable(CAP_DAC_READ_SEARCH)) 304411a7b371SAneesh Kumar K.V return -ENOENT; 304511a7b371SAneesh Kumar K.V how = LOOKUP_EMPTY; 304611a7b371SAneesh Kumar K.V } 3047c04030e1SUlrich Drepper 304811a7b371SAneesh Kumar K.V if (flags & AT_SYMLINK_FOLLOW) 304911a7b371SAneesh Kumar K.V how |= LOOKUP_FOLLOW; 305011a7b371SAneesh Kumar K.V 305111a7b371SAneesh Kumar K.V error = user_path_at(olddfd, oldname, how, &old_path); 30521da177e4SLinus Torvalds if (error) 30532ad94ae6SAl Viro return error; 30542ad94ae6SAl Viro 3055dae6ad8fSAl Viro new_dentry = user_path_create(newdfd, newname, &new_path, 0); 30561da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 30576902d925SDave Hansen if (IS_ERR(new_dentry)) 3058dae6ad8fSAl Viro goto out; 3059dae6ad8fSAl Viro 3060dae6ad8fSAl Viro error = -EXDEV; 3061dae6ad8fSAl Viro if (old_path.mnt != new_path.mnt) 3062dae6ad8fSAl Viro goto out_dput; 3063dae6ad8fSAl Viro error = mnt_want_write(new_path.mnt); 306475c3f29dSDave Hansen if (error) 306575c3f29dSDave Hansen goto out_dput; 3066dae6ad8fSAl Viro error = security_path_link(old_path.dentry, &new_path, new_dentry); 3067be6d3e56SKentaro Takeda if (error) 3068be6d3e56SKentaro Takeda goto out_drop_write; 3069dae6ad8fSAl Viro error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry); 3070be6d3e56SKentaro Takeda out_drop_write: 3071dae6ad8fSAl Viro mnt_drop_write(new_path.mnt); 307275c3f29dSDave Hansen out_dput: 30731da177e4SLinus Torvalds dput(new_dentry); 3074dae6ad8fSAl Viro mutex_unlock(&new_path.dentry->d_inode->i_mutex); 3075dae6ad8fSAl Viro path_put(&new_path); 30761da177e4SLinus Torvalds out: 30772d8f3038SAl Viro path_put(&old_path); 30781da177e4SLinus Torvalds 30791da177e4SLinus Torvalds return error; 30801da177e4SLinus Torvalds } 30811da177e4SLinus Torvalds 30823480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname) 30835590ff0dSUlrich Drepper { 3084c04030e1SUlrich Drepper return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0); 30855590ff0dSUlrich Drepper } 30865590ff0dSUlrich Drepper 30871da177e4SLinus Torvalds /* 30881da177e4SLinus Torvalds * The worst of all namespace operations - renaming directory. "Perverted" 30891da177e4SLinus Torvalds * doesn't even start to describe it. Somebody in UCB had a heck of a trip... 30901da177e4SLinus Torvalds * Problems: 30911da177e4SLinus Torvalds * a) we can get into loop creation. Check is done in is_subdir(). 30921da177e4SLinus Torvalds * b) race potential - two innocent renames can create a loop together. 30931da177e4SLinus Torvalds * That's where 4.4 screws up. Current fix: serialization on 3094a11f3a05SArjan van de Ven * sb->s_vfs_rename_mutex. We might be more accurate, but that's another 30951da177e4SLinus Torvalds * story. 30961da177e4SLinus Torvalds * c) we have to lock _three_ objects - parents and victim (if it exists). 30971b1dcc1bSJes Sorensen * And that - after we got ->i_mutex on parents (until then we don't know 30981da177e4SLinus Torvalds * whether the target exists). Solution: try to be smart with locking 30991da177e4SLinus Torvalds * order for inodes. We rely on the fact that tree topology may change 3100a11f3a05SArjan van de Ven * only under ->s_vfs_rename_mutex _and_ that parent of the object we 31011da177e4SLinus Torvalds * move will be locked. Thus we can rank directories by the tree 31021da177e4SLinus Torvalds * (ancestors first) and rank all non-directories after them. 31031da177e4SLinus Torvalds * That works since everybody except rename does "lock parent, lookup, 3104a11f3a05SArjan van de Ven * lock child" and rename is under ->s_vfs_rename_mutex. 31051da177e4SLinus Torvalds * HOWEVER, it relies on the assumption that any object with ->lookup() 31061da177e4SLinus Torvalds * has no more than 1 dentry. If "hybrid" objects will ever appear, 31071da177e4SLinus Torvalds * we'd better make sure that there's no link(2) for them. 3108e4eaac06SSage Weil * d) conversion from fhandle to dentry may come in the wrong moment - when 31091b1dcc1bSJes Sorensen * we are removing the target. Solution: we will have to grab ->i_mutex 31101da177e4SLinus Torvalds * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on 3111c41b20e7SAdam Buchbinder * ->i_mutex on parents, which works but leads to some truly excessive 31121da177e4SLinus Torvalds * locking]. 31131da177e4SLinus Torvalds */ 311475c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry, 31151da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 31161da177e4SLinus Torvalds { 31171da177e4SLinus Torvalds int error = 0; 31189055cba7SSage Weil struct inode *target = new_dentry->d_inode; 31198de52778SAl Viro unsigned max_links = new_dir->i_sb->s_max_links; 31201da177e4SLinus Torvalds 31211da177e4SLinus Torvalds /* 31221da177e4SLinus Torvalds * If we are going to change the parent - check write permissions, 31231da177e4SLinus Torvalds * we'll need to flip '..'. 31241da177e4SLinus Torvalds */ 31251da177e4SLinus Torvalds if (new_dir != old_dir) { 3126f419a2e3SAl Viro error = inode_permission(old_dentry->d_inode, MAY_WRITE); 31271da177e4SLinus Torvalds if (error) 31281da177e4SLinus Torvalds return error; 31291da177e4SLinus Torvalds } 31301da177e4SLinus Torvalds 31311da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 31321da177e4SLinus Torvalds if (error) 31331da177e4SLinus Torvalds return error; 31341da177e4SLinus Torvalds 31351d2ef590SAl Viro dget(new_dentry); 3136d83c49f3SAl Viro if (target) 31371b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 31389055cba7SSage Weil 31391da177e4SLinus Torvalds error = -EBUSY; 31409055cba7SSage Weil if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry)) 31419055cba7SSage Weil goto out; 31429055cba7SSage Weil 31438de52778SAl Viro error = -EMLINK; 31448de52778SAl Viro if (max_links && !target && new_dir != old_dir && 31458de52778SAl Viro new_dir->i_nlink >= max_links) 31468de52778SAl Viro goto out; 31478de52778SAl Viro 31483cebde24SSage Weil if (target) 31493cebde24SSage Weil shrink_dcache_parent(new_dentry); 31501da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 31519055cba7SSage Weil if (error) 31529055cba7SSage Weil goto out; 31539055cba7SSage Weil 31541da177e4SLinus Torvalds if (target) { 31551da177e4SLinus Torvalds target->i_flags |= S_DEAD; 3156d83c49f3SAl Viro dont_mount(new_dentry); 3157d83c49f3SAl Viro } 31589055cba7SSage Weil out: 31599055cba7SSage Weil if (target) 31601b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 31611d2ef590SAl Viro dput(new_dentry); 3162e31e14ecSStephen Smalley if (!error) 3163349457ccSMark Fasheh if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) 31641da177e4SLinus Torvalds d_move(old_dentry,new_dentry); 31651da177e4SLinus Torvalds return error; 31661da177e4SLinus Torvalds } 31671da177e4SLinus Torvalds 316875c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry, 31691da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 31701da177e4SLinus Torvalds { 317151892bbbSSage Weil struct inode *target = new_dentry->d_inode; 31721da177e4SLinus Torvalds int error; 31731da177e4SLinus Torvalds 31741da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 31751da177e4SLinus Torvalds if (error) 31761da177e4SLinus Torvalds return error; 31771da177e4SLinus Torvalds 31781da177e4SLinus Torvalds dget(new_dentry); 31791da177e4SLinus Torvalds if (target) 31801b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 318151892bbbSSage Weil 31821da177e4SLinus Torvalds error = -EBUSY; 318351892bbbSSage Weil if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry)) 318451892bbbSSage Weil goto out; 318551892bbbSSage Weil 31861da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 318751892bbbSSage Weil if (error) 318851892bbbSSage Weil goto out; 318951892bbbSSage Weil 3190bec1052eSAl Viro if (target) 3191d83c49f3SAl Viro dont_mount(new_dentry); 3192349457ccSMark Fasheh if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) 31931da177e4SLinus Torvalds d_move(old_dentry, new_dentry); 319451892bbbSSage Weil out: 31951da177e4SLinus Torvalds if (target) 31961b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 31971da177e4SLinus Torvalds dput(new_dentry); 31981da177e4SLinus Torvalds return error; 31991da177e4SLinus Torvalds } 32001da177e4SLinus Torvalds 32011da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry, 32021da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 32031da177e4SLinus Torvalds { 32041da177e4SLinus Torvalds int error; 32051da177e4SLinus Torvalds int is_dir = S_ISDIR(old_dentry->d_inode->i_mode); 320659b0df21SEric Paris const unsigned char *old_name; 32071da177e4SLinus Torvalds 32081da177e4SLinus Torvalds if (old_dentry->d_inode == new_dentry->d_inode) 32091da177e4SLinus Torvalds return 0; 32101da177e4SLinus Torvalds 32111da177e4SLinus Torvalds error = may_delete(old_dir, old_dentry, is_dir); 32121da177e4SLinus Torvalds if (error) 32131da177e4SLinus Torvalds return error; 32141da177e4SLinus Torvalds 32151da177e4SLinus Torvalds if (!new_dentry->d_inode) 3216a95164d9SMiklos Szeredi error = may_create(new_dir, new_dentry); 32171da177e4SLinus Torvalds else 32181da177e4SLinus Torvalds error = may_delete(new_dir, new_dentry, is_dir); 32191da177e4SLinus Torvalds if (error) 32201da177e4SLinus Torvalds return error; 32211da177e4SLinus Torvalds 3222acfa4380SAl Viro if (!old_dir->i_op->rename) 32231da177e4SLinus Torvalds return -EPERM; 32241da177e4SLinus Torvalds 32250eeca283SRobert Love old_name = fsnotify_oldname_init(old_dentry->d_name.name); 32260eeca283SRobert Love 32271da177e4SLinus Torvalds if (is_dir) 32281da177e4SLinus Torvalds error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry); 32291da177e4SLinus Torvalds else 32301da177e4SLinus Torvalds error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry); 3231123df294SAl Viro if (!error) 3232123df294SAl Viro fsnotify_move(old_dir, new_dir, old_name, is_dir, 32335a190ae6SAl Viro new_dentry->d_inode, old_dentry); 32340eeca283SRobert Love fsnotify_oldname_free(old_name); 32350eeca283SRobert Love 32361da177e4SLinus Torvalds return error; 32371da177e4SLinus Torvalds } 32381da177e4SLinus Torvalds 32392e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname, 32402e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 32411da177e4SLinus Torvalds { 32421da177e4SLinus Torvalds struct dentry *old_dir, *new_dir; 32431da177e4SLinus Torvalds struct dentry *old_dentry, *new_dentry; 32441da177e4SLinus Torvalds struct dentry *trap; 32451da177e4SLinus Torvalds struct nameidata oldnd, newnd; 32462ad94ae6SAl Viro char *from; 32472ad94ae6SAl Viro char *to; 32482ad94ae6SAl Viro int error; 32491da177e4SLinus Torvalds 32502ad94ae6SAl Viro error = user_path_parent(olddfd, oldname, &oldnd, &from); 32511da177e4SLinus Torvalds if (error) 32521da177e4SLinus Torvalds goto exit; 32531da177e4SLinus Torvalds 32542ad94ae6SAl Viro error = user_path_parent(newdfd, newname, &newnd, &to); 32551da177e4SLinus Torvalds if (error) 32561da177e4SLinus Torvalds goto exit1; 32571da177e4SLinus Torvalds 32581da177e4SLinus Torvalds error = -EXDEV; 32594ac91378SJan Blunck if (oldnd.path.mnt != newnd.path.mnt) 32601da177e4SLinus Torvalds goto exit2; 32611da177e4SLinus Torvalds 32624ac91378SJan Blunck old_dir = oldnd.path.dentry; 32631da177e4SLinus Torvalds error = -EBUSY; 32641da177e4SLinus Torvalds if (oldnd.last_type != LAST_NORM) 32651da177e4SLinus Torvalds goto exit2; 32661da177e4SLinus Torvalds 32674ac91378SJan Blunck new_dir = newnd.path.dentry; 32681da177e4SLinus Torvalds if (newnd.last_type != LAST_NORM) 32691da177e4SLinus Torvalds goto exit2; 32701da177e4SLinus Torvalds 32710612d9fbSOGAWA Hirofumi oldnd.flags &= ~LOOKUP_PARENT; 32720612d9fbSOGAWA Hirofumi newnd.flags &= ~LOOKUP_PARENT; 32734e9ed2f8SOGAWA Hirofumi newnd.flags |= LOOKUP_RENAME_TARGET; 32740612d9fbSOGAWA Hirofumi 32751da177e4SLinus Torvalds trap = lock_rename(new_dir, old_dir); 32761da177e4SLinus Torvalds 327749705b77SChristoph Hellwig old_dentry = lookup_hash(&oldnd); 32781da177e4SLinus Torvalds error = PTR_ERR(old_dentry); 32791da177e4SLinus Torvalds if (IS_ERR(old_dentry)) 32801da177e4SLinus Torvalds goto exit3; 32811da177e4SLinus Torvalds /* source must exist */ 32821da177e4SLinus Torvalds error = -ENOENT; 32831da177e4SLinus Torvalds if (!old_dentry->d_inode) 32841da177e4SLinus Torvalds goto exit4; 32851da177e4SLinus Torvalds /* unless the source is a directory trailing slashes give -ENOTDIR */ 32861da177e4SLinus Torvalds if (!S_ISDIR(old_dentry->d_inode->i_mode)) { 32871da177e4SLinus Torvalds error = -ENOTDIR; 32881da177e4SLinus Torvalds if (oldnd.last.name[oldnd.last.len]) 32891da177e4SLinus Torvalds goto exit4; 32901da177e4SLinus Torvalds if (newnd.last.name[newnd.last.len]) 32911da177e4SLinus Torvalds goto exit4; 32921da177e4SLinus Torvalds } 32931da177e4SLinus Torvalds /* source should not be ancestor of target */ 32941da177e4SLinus Torvalds error = -EINVAL; 32951da177e4SLinus Torvalds if (old_dentry == trap) 32961da177e4SLinus Torvalds goto exit4; 329749705b77SChristoph Hellwig new_dentry = lookup_hash(&newnd); 32981da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 32991da177e4SLinus Torvalds if (IS_ERR(new_dentry)) 33001da177e4SLinus Torvalds goto exit4; 33011da177e4SLinus Torvalds /* target should not be an ancestor of source */ 33021da177e4SLinus Torvalds error = -ENOTEMPTY; 33031da177e4SLinus Torvalds if (new_dentry == trap) 33041da177e4SLinus Torvalds goto exit5; 33051da177e4SLinus Torvalds 33069079b1ebSDave Hansen error = mnt_want_write(oldnd.path.mnt); 33079079b1ebSDave Hansen if (error) 33089079b1ebSDave Hansen goto exit5; 3309be6d3e56SKentaro Takeda error = security_path_rename(&oldnd.path, old_dentry, 3310be6d3e56SKentaro Takeda &newnd.path, new_dentry); 3311be6d3e56SKentaro Takeda if (error) 3312be6d3e56SKentaro Takeda goto exit6; 33131da177e4SLinus Torvalds error = vfs_rename(old_dir->d_inode, old_dentry, 33141da177e4SLinus Torvalds new_dir->d_inode, new_dentry); 3315be6d3e56SKentaro Takeda exit6: 33169079b1ebSDave Hansen mnt_drop_write(oldnd.path.mnt); 33171da177e4SLinus Torvalds exit5: 33181da177e4SLinus Torvalds dput(new_dentry); 33191da177e4SLinus Torvalds exit4: 33201da177e4SLinus Torvalds dput(old_dentry); 33211da177e4SLinus Torvalds exit3: 33221da177e4SLinus Torvalds unlock_rename(new_dir, old_dir); 33231da177e4SLinus Torvalds exit2: 33241d957f9bSJan Blunck path_put(&newnd.path); 33252ad94ae6SAl Viro putname(to); 33261da177e4SLinus Torvalds exit1: 33271d957f9bSJan Blunck path_put(&oldnd.path); 33281da177e4SLinus Torvalds putname(from); 33292ad94ae6SAl Viro exit: 33301da177e4SLinus Torvalds return error; 33311da177e4SLinus Torvalds } 33321da177e4SLinus Torvalds 3333a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname) 33345590ff0dSUlrich Drepper { 33355590ff0dSUlrich Drepper return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname); 33365590ff0dSUlrich Drepper } 33375590ff0dSUlrich Drepper 33381da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link) 33391da177e4SLinus Torvalds { 33401da177e4SLinus Torvalds int len; 33411da177e4SLinus Torvalds 33421da177e4SLinus Torvalds len = PTR_ERR(link); 33431da177e4SLinus Torvalds if (IS_ERR(link)) 33441da177e4SLinus Torvalds goto out; 33451da177e4SLinus Torvalds 33461da177e4SLinus Torvalds len = strlen(link); 33471da177e4SLinus Torvalds if (len > (unsigned) buflen) 33481da177e4SLinus Torvalds len = buflen; 33491da177e4SLinus Torvalds if (copy_to_user(buffer, link, len)) 33501da177e4SLinus Torvalds len = -EFAULT; 33511da177e4SLinus Torvalds out: 33521da177e4SLinus Torvalds return len; 33531da177e4SLinus Torvalds } 33541da177e4SLinus Torvalds 33551da177e4SLinus Torvalds /* 33561da177e4SLinus Torvalds * A helper for ->readlink(). This should be used *ONLY* for symlinks that 33571da177e4SLinus Torvalds * have ->follow_link() touching nd only in nd_set_link(). Using (or not 33581da177e4SLinus Torvalds * using) it for any given inode is up to filesystem. 33591da177e4SLinus Torvalds */ 33601da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen) 33611da177e4SLinus Torvalds { 33621da177e4SLinus Torvalds struct nameidata nd; 3363cc314eefSLinus Torvalds void *cookie; 3364694a1764SMarcin Slusarz int res; 3365cc314eefSLinus Torvalds 33661da177e4SLinus Torvalds nd.depth = 0; 3367cc314eefSLinus Torvalds cookie = dentry->d_inode->i_op->follow_link(dentry, &nd); 3368694a1764SMarcin Slusarz if (IS_ERR(cookie)) 3369694a1764SMarcin Slusarz return PTR_ERR(cookie); 3370694a1764SMarcin Slusarz 3371694a1764SMarcin Slusarz res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd)); 33721da177e4SLinus Torvalds if (dentry->d_inode->i_op->put_link) 3373cc314eefSLinus Torvalds dentry->d_inode->i_op->put_link(dentry, &nd, cookie); 3374694a1764SMarcin Slusarz return res; 33751da177e4SLinus Torvalds } 33761da177e4SLinus Torvalds 33771da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link) 33781da177e4SLinus Torvalds { 33791da177e4SLinus Torvalds return __vfs_follow_link(nd, link); 33801da177e4SLinus Torvalds } 33811da177e4SLinus Torvalds 33821da177e4SLinus Torvalds /* get the link contents into pagecache */ 33831da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage) 33841da177e4SLinus Torvalds { 3385ebd09abbSDuane Griffin char *kaddr; 33861da177e4SLinus Torvalds struct page *page; 33871da177e4SLinus Torvalds struct address_space *mapping = dentry->d_inode->i_mapping; 3388090d2b18SPekka Enberg page = read_mapping_page(mapping, 0, NULL); 33891da177e4SLinus Torvalds if (IS_ERR(page)) 33906fe6900eSNick Piggin return (char*)page; 33911da177e4SLinus Torvalds *ppage = page; 3392ebd09abbSDuane Griffin kaddr = kmap(page); 3393ebd09abbSDuane Griffin nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1); 3394ebd09abbSDuane Griffin return kaddr; 33951da177e4SLinus Torvalds } 33961da177e4SLinus Torvalds 33971da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen) 33981da177e4SLinus Torvalds { 33991da177e4SLinus Torvalds struct page *page = NULL; 34001da177e4SLinus Torvalds char *s = page_getlink(dentry, &page); 34011da177e4SLinus Torvalds int res = vfs_readlink(dentry,buffer,buflen,s); 34021da177e4SLinus Torvalds if (page) { 34031da177e4SLinus Torvalds kunmap(page); 34041da177e4SLinus Torvalds page_cache_release(page); 34051da177e4SLinus Torvalds } 34061da177e4SLinus Torvalds return res; 34071da177e4SLinus Torvalds } 34081da177e4SLinus Torvalds 3409cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd) 34101da177e4SLinus Torvalds { 3411cc314eefSLinus Torvalds struct page *page = NULL; 34121da177e4SLinus Torvalds nd_set_link(nd, page_getlink(dentry, &page)); 3413cc314eefSLinus Torvalds return page; 34141da177e4SLinus Torvalds } 34151da177e4SLinus Torvalds 3416cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie) 34171da177e4SLinus Torvalds { 3418cc314eefSLinus Torvalds struct page *page = cookie; 3419cc314eefSLinus Torvalds 3420cc314eefSLinus Torvalds if (page) { 34211da177e4SLinus Torvalds kunmap(page); 34221da177e4SLinus Torvalds page_cache_release(page); 34231da177e4SLinus Torvalds } 34241da177e4SLinus Torvalds } 34251da177e4SLinus Torvalds 342654566b2cSNick Piggin /* 342754566b2cSNick Piggin * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS 342854566b2cSNick Piggin */ 342954566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs) 34301da177e4SLinus Torvalds { 34311da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 34320adb25d2SKirill Korotaev struct page *page; 3433afddba49SNick Piggin void *fsdata; 3434beb497abSDmitriy Monakhov int err; 34351da177e4SLinus Torvalds char *kaddr; 343654566b2cSNick Piggin unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE; 343754566b2cSNick Piggin if (nofs) 343854566b2cSNick Piggin flags |= AOP_FLAG_NOFS; 34391da177e4SLinus Torvalds 34407e53cac4SNeilBrown retry: 3441afddba49SNick Piggin err = pagecache_write_begin(NULL, mapping, 0, len-1, 344254566b2cSNick Piggin flags, &page, &fsdata); 34431da177e4SLinus Torvalds if (err) 3444afddba49SNick Piggin goto fail; 3445afddba49SNick Piggin 3446e8e3c3d6SCong Wang kaddr = kmap_atomic(page); 34471da177e4SLinus Torvalds memcpy(kaddr, symname, len-1); 3448e8e3c3d6SCong Wang kunmap_atomic(kaddr); 3449afddba49SNick Piggin 3450afddba49SNick Piggin err = pagecache_write_end(NULL, mapping, 0, len-1, len-1, 3451afddba49SNick Piggin page, fsdata); 34521da177e4SLinus Torvalds if (err < 0) 34531da177e4SLinus Torvalds goto fail; 3454afddba49SNick Piggin if (err < len-1) 3455afddba49SNick Piggin goto retry; 3456afddba49SNick Piggin 34571da177e4SLinus Torvalds mark_inode_dirty(inode); 34581da177e4SLinus Torvalds return 0; 34591da177e4SLinus Torvalds fail: 34601da177e4SLinus Torvalds return err; 34611da177e4SLinus Torvalds } 34621da177e4SLinus Torvalds 34630adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len) 34640adb25d2SKirill Korotaev { 34650adb25d2SKirill Korotaev return __page_symlink(inode, symname, len, 346654566b2cSNick Piggin !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS)); 34670adb25d2SKirill Korotaev } 34680adb25d2SKirill Korotaev 346992e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = { 34701da177e4SLinus Torvalds .readlink = generic_readlink, 34711da177e4SLinus Torvalds .follow_link = page_follow_link_light, 34721da177e4SLinus Torvalds .put_link = page_put_link, 34731da177e4SLinus Torvalds }; 34741da177e4SLinus Torvalds 34752d8f3038SAl Viro EXPORT_SYMBOL(user_path_at); 3476cc53ce53SDavid Howells EXPORT_SYMBOL(follow_down_one); 34771da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down); 34781da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up); 34791da177e4SLinus Torvalds EXPORT_SYMBOL(get_write_access); /* binfmt_aout */ 34801da177e4SLinus Torvalds EXPORT_SYMBOL(getname); 34811da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename); 34821da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len); 34831da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light); 34841da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link); 34851da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink); 34860adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink); 34871da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink); 34881da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations); 3489d1811465SAl Viro EXPORT_SYMBOL(kern_path); 349016f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup); 3491f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission); 34921da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename); 34931da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create); 34941da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link); 34951da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link); 34961da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir); 34971da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod); 34981da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission); 34991da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink); 35001da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename); 35011da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir); 35021da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink); 35031da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink); 35041da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash); 35051da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink); 3506