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> 181da177e4SLinus Torvalds #include <linux/module.h> 191da177e4SLinus Torvalds #include <linux/slab.h> 201da177e4SLinus Torvalds #include <linux/fs.h> 211da177e4SLinus Torvalds #include <linux/namei.h> 221da177e4SLinus Torvalds #include <linux/pagemap.h> 230eeca283SRobert Love #include <linux/fsnotify.h> 241da177e4SLinus Torvalds #include <linux/personality.h> 251da177e4SLinus Torvalds #include <linux/security.h> 266146f0d5SMimi Zohar #include <linux/ima.h> 271da177e4SLinus Torvalds #include <linux/syscalls.h> 281da177e4SLinus Torvalds #include <linux/mount.h> 291da177e4SLinus Torvalds #include <linux/audit.h> 3016f7e0feSRandy Dunlap #include <linux/capability.h> 31834f2a4aSTrond Myklebust #include <linux/file.h> 325590ff0dSUlrich Drepper #include <linux/fcntl.h> 3308ce5f16SSerge E. Hallyn #include <linux/device_cgroup.h> 345ad4e53bSAl Viro #include <linux/fs_struct.h> 351da177e4SLinus Torvalds #include <asm/uaccess.h> 361da177e4SLinus Torvalds 37e81e3f4dSEric Paris #include "internal.h" 38e81e3f4dSEric Paris 391da177e4SLinus Torvalds /* [Feb-1997 T. Schoebel-Theuer] 401da177e4SLinus Torvalds * Fundamental changes in the pathname lookup mechanisms (namei) 411da177e4SLinus Torvalds * were necessary because of omirr. The reason is that omirr needs 421da177e4SLinus Torvalds * to know the _real_ pathname, not the user-supplied one, in case 431da177e4SLinus Torvalds * of symlinks (and also when transname replacements occur). 441da177e4SLinus Torvalds * 451da177e4SLinus Torvalds * The new code replaces the old recursive symlink resolution with 461da177e4SLinus Torvalds * an iterative one (in case of non-nested symlink chains). It does 471da177e4SLinus Torvalds * this with calls to <fs>_follow_link(). 481da177e4SLinus Torvalds * As a side effect, dir_namei(), _namei() and follow_link() are now 491da177e4SLinus Torvalds * replaced with a single function lookup_dentry() that can handle all 501da177e4SLinus Torvalds * the special cases of the former code. 511da177e4SLinus Torvalds * 521da177e4SLinus Torvalds * With the new dcache, the pathname is stored at each inode, at least as 531da177e4SLinus Torvalds * long as the refcount of the inode is positive. As a side effect, the 541da177e4SLinus Torvalds * size of the dcache depends on the inode cache and thus is dynamic. 551da177e4SLinus Torvalds * 561da177e4SLinus Torvalds * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink 571da177e4SLinus Torvalds * resolution to correspond with current state of the code. 581da177e4SLinus Torvalds * 591da177e4SLinus Torvalds * Note that the symlink resolution is not *completely* iterative. 601da177e4SLinus Torvalds * There is still a significant amount of tail- and mid- recursion in 611da177e4SLinus Torvalds * the algorithm. Also, note that <fs>_readlink() is not used in 621da177e4SLinus Torvalds * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink() 631da177e4SLinus Torvalds * may return different results than <fs>_follow_link(). Many virtual 641da177e4SLinus Torvalds * filesystems (including /proc) exhibit this behavior. 651da177e4SLinus Torvalds */ 661da177e4SLinus Torvalds 671da177e4SLinus Torvalds /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation: 681da177e4SLinus Torvalds * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL 691da177e4SLinus Torvalds * and the name already exists in form of a symlink, try to create the new 701da177e4SLinus Torvalds * name indicated by the symlink. The old code always complained that the 711da177e4SLinus Torvalds * name already exists, due to not following the symlink even if its target 721da177e4SLinus Torvalds * is nonexistent. The new semantics affects also mknod() and link() when 7325985edcSLucas De Marchi * the name is a symlink pointing to a non-existent name. 741da177e4SLinus Torvalds * 751da177e4SLinus Torvalds * I don't know which semantics is the right one, since I have no access 761da177e4SLinus Torvalds * to standards. But I found by trial that HP-UX 9.0 has the full "new" 771da177e4SLinus Torvalds * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the 781da177e4SLinus Torvalds * "old" one. Personally, I think the new semantics is much more logical. 791da177e4SLinus Torvalds * Note that "ln old new" where "new" is a symlink pointing to a non-existing 801da177e4SLinus Torvalds * file does succeed in both HP-UX and SunOs, but not in Solaris 811da177e4SLinus Torvalds * and in the old Linux semantics. 821da177e4SLinus Torvalds */ 831da177e4SLinus Torvalds 841da177e4SLinus Torvalds /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink 851da177e4SLinus Torvalds * semantics. See the comments in "open_namei" and "do_link" below. 861da177e4SLinus Torvalds * 871da177e4SLinus Torvalds * [10-Sep-98 Alan Modra] Another symlink change. 881da177e4SLinus Torvalds */ 891da177e4SLinus Torvalds 901da177e4SLinus Torvalds /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks: 911da177e4SLinus Torvalds * inside the path - always follow. 921da177e4SLinus Torvalds * in the last component in creation/removal/renaming - never follow. 931da177e4SLinus Torvalds * if LOOKUP_FOLLOW passed - follow. 941da177e4SLinus Torvalds * if the pathname has trailing slashes - follow. 951da177e4SLinus Torvalds * otherwise - don't follow. 961da177e4SLinus Torvalds * (applied in that order). 971da177e4SLinus Torvalds * 981da177e4SLinus Torvalds * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT 991da177e4SLinus Torvalds * restored for 2.4. This is the last surviving part of old 4.2BSD bug. 1001da177e4SLinus Torvalds * During the 2.4 we need to fix the userland stuff depending on it - 1011da177e4SLinus Torvalds * hopefully we will be able to get rid of that wart in 2.5. So far only 1021da177e4SLinus Torvalds * XEmacs seems to be relying on it... 1031da177e4SLinus Torvalds */ 1041da177e4SLinus Torvalds /* 1051da177e4SLinus Torvalds * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland) 106a11f3a05SArjan van de Ven * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives 1071da177e4SLinus Torvalds * any extra contention... 1081da177e4SLinus Torvalds */ 1091da177e4SLinus Torvalds 1101da177e4SLinus Torvalds /* In order to reduce some races, while at the same time doing additional 1111da177e4SLinus Torvalds * checking and hopefully speeding things up, we copy filenames to the 1121da177e4SLinus Torvalds * kernel data space before using them.. 1131da177e4SLinus Torvalds * 1141da177e4SLinus Torvalds * POSIX.1 2.4: an empty pathname is invalid (ENOENT). 1151da177e4SLinus Torvalds * PATH_MAX includes the nul terminator --RR. 1161da177e4SLinus Torvalds */ 117858119e1SArjan van de Ven static int do_getname(const char __user *filename, char *page) 1181da177e4SLinus Torvalds { 1191da177e4SLinus Torvalds int retval; 1201da177e4SLinus Torvalds unsigned long len = PATH_MAX; 1211da177e4SLinus Torvalds 1221da177e4SLinus Torvalds if (!segment_eq(get_fs(), KERNEL_DS)) { 1231da177e4SLinus Torvalds if ((unsigned long) filename >= TASK_SIZE) 1241da177e4SLinus Torvalds return -EFAULT; 1251da177e4SLinus Torvalds if (TASK_SIZE - (unsigned long) filename < PATH_MAX) 1261da177e4SLinus Torvalds len = TASK_SIZE - (unsigned long) filename; 1271da177e4SLinus Torvalds } 1281da177e4SLinus Torvalds 1291da177e4SLinus Torvalds retval = strncpy_from_user(page, filename, len); 1301da177e4SLinus Torvalds if (retval > 0) { 1311da177e4SLinus Torvalds if (retval < len) 1321da177e4SLinus Torvalds return 0; 1331da177e4SLinus Torvalds return -ENAMETOOLONG; 1341da177e4SLinus Torvalds } else if (!retval) 1351da177e4SLinus Torvalds retval = -ENOENT; 1361da177e4SLinus Torvalds return retval; 1371da177e4SLinus Torvalds } 1381da177e4SLinus Torvalds 139f52e0c11SAl Viro static char *getname_flags(const char __user * filename, int flags) 1401da177e4SLinus Torvalds { 1411da177e4SLinus Torvalds char *tmp, *result; 1421da177e4SLinus Torvalds 1431da177e4SLinus Torvalds result = ERR_PTR(-ENOMEM); 1441da177e4SLinus Torvalds tmp = __getname(); 1451da177e4SLinus Torvalds if (tmp) { 1461da177e4SLinus Torvalds int retval = do_getname(filename, tmp); 1471da177e4SLinus Torvalds 1481da177e4SLinus Torvalds result = tmp; 1491da177e4SLinus Torvalds if (retval < 0) { 150f52e0c11SAl Viro if (retval != -ENOENT || !(flags & LOOKUP_EMPTY)) { 1511da177e4SLinus Torvalds __putname(tmp); 1521da177e4SLinus Torvalds result = ERR_PTR(retval); 1531da177e4SLinus Torvalds } 1541da177e4SLinus Torvalds } 155f52e0c11SAl Viro } 1561da177e4SLinus Torvalds audit_getname(result); 1571da177e4SLinus Torvalds return result; 1581da177e4SLinus Torvalds } 1591da177e4SLinus Torvalds 160f52e0c11SAl Viro char *getname(const char __user * filename) 161f52e0c11SAl Viro { 162f52e0c11SAl Viro return getname_flags(filename, 0); 163f52e0c11SAl Viro } 164f52e0c11SAl Viro 1651da177e4SLinus Torvalds #ifdef CONFIG_AUDITSYSCALL 1661da177e4SLinus Torvalds void putname(const char *name) 1671da177e4SLinus Torvalds { 1685ac3a9c2SAl Viro if (unlikely(!audit_dummy_context())) 1691da177e4SLinus Torvalds audit_putname(name); 1701da177e4SLinus Torvalds else 1711da177e4SLinus Torvalds __putname(name); 1721da177e4SLinus Torvalds } 1731da177e4SLinus Torvalds EXPORT_SYMBOL(putname); 1741da177e4SLinus Torvalds #endif 1751da177e4SLinus Torvalds 1765909ccaaSLinus Torvalds /* 1775909ccaaSLinus Torvalds * This does basic POSIX ACL permission checking 1785909ccaaSLinus Torvalds */ 1797e40145eSAl Viro static int acl_permission_check(struct inode *inode, int mask) 1805909ccaaSLinus Torvalds { 1817e40145eSAl Viro int (*check_acl)(struct inode *inode, int mask); 18226cf46beSLinus Torvalds unsigned int mode = inode->i_mode; 1835909ccaaSLinus Torvalds 1849c2c7039SAl Viro mask &= MAY_READ | MAY_WRITE | MAY_EXEC | MAY_NOT_BLOCK; 1855909ccaaSLinus Torvalds 186e795b717SSerge E. Hallyn if (current_user_ns() != inode_userns(inode)) 187e795b717SSerge E. Hallyn goto other_perms; 188e795b717SSerge E. Hallyn 1895909ccaaSLinus Torvalds if (current_fsuid() == inode->i_uid) 1905909ccaaSLinus Torvalds mode >>= 6; 1915909ccaaSLinus Torvalds else { 192178ea735SAl Viro check_acl = inode->i_op->check_acl; 1935909ccaaSLinus Torvalds if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) { 1947e40145eSAl Viro int error = check_acl(inode, mask); 1955909ccaaSLinus Torvalds if (error != -EAGAIN) 1965909ccaaSLinus Torvalds return error; 1975909ccaaSLinus Torvalds } 1985909ccaaSLinus Torvalds 1995909ccaaSLinus Torvalds if (in_group_p(inode->i_gid)) 2005909ccaaSLinus Torvalds mode >>= 3; 2015909ccaaSLinus Torvalds } 2025909ccaaSLinus Torvalds 203e795b717SSerge E. Hallyn other_perms: 2045909ccaaSLinus Torvalds /* 2055909ccaaSLinus Torvalds * If the DACs are ok we don't need any capability check. 2065909ccaaSLinus Torvalds */ 2079c2c7039SAl Viro if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0) 2085909ccaaSLinus Torvalds return 0; 2095909ccaaSLinus Torvalds return -EACCES; 2105909ccaaSLinus Torvalds } 2111da177e4SLinus Torvalds 2121da177e4SLinus Torvalds /** 2131da177e4SLinus Torvalds * generic_permission - check for access rights on a Posix-like filesystem 2141da177e4SLinus Torvalds * @inode: inode to check access rights for 2151da177e4SLinus Torvalds * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) 21639191628SRandy Dunlap * @flags: IPERM_FLAG_ flags. 2171da177e4SLinus Torvalds * 2181da177e4SLinus Torvalds * Used to check for read/write/execute permissions on a file. 2191da177e4SLinus Torvalds * We use "fsuid" for this, letting us set arbitrary permissions 2201da177e4SLinus Torvalds * for filesystem access without changing the "normal" uids which 221b74c79e9SNick Piggin * are used for other things. 222b74c79e9SNick Piggin * 223b74c79e9SNick Piggin * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk 224b74c79e9SNick Piggin * request cannot be satisfied (eg. requires blocking or too much complexity). 225b74c79e9SNick Piggin * It would then be called again in ref-walk mode. 2261da177e4SLinus Torvalds */ 2272830ba7fSAl Viro int generic_permission(struct inode *inode, int mask) 2281da177e4SLinus Torvalds { 2295909ccaaSLinus Torvalds int ret; 2301da177e4SLinus Torvalds 2311da177e4SLinus Torvalds /* 2325909ccaaSLinus Torvalds * Do the basic POSIX ACL permission checks. 2331da177e4SLinus Torvalds */ 2347e40145eSAl Viro ret = acl_permission_check(inode, mask); 2355909ccaaSLinus Torvalds if (ret != -EACCES) 2365909ccaaSLinus Torvalds return ret; 2371da177e4SLinus Torvalds 238d594e7ecSAl Viro if (S_ISDIR(inode->i_mode)) { 239d594e7ecSAl Viro /* DACs are overridable for directories */ 240d594e7ecSAl Viro if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE)) 241d594e7ecSAl Viro return 0; 242d594e7ecSAl Viro if (!(mask & MAY_WRITE)) 243d594e7ecSAl Viro if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH)) 244d594e7ecSAl Viro return 0; 245d594e7ecSAl Viro return -EACCES; 246d594e7ecSAl Viro } 2471da177e4SLinus Torvalds /* 2481da177e4SLinus Torvalds * Read/write DACs are always overridable. 249d594e7ecSAl Viro * Executable DACs are overridable when there is 250d594e7ecSAl Viro * at least one exec bit set. 2511da177e4SLinus Torvalds */ 252d594e7ecSAl Viro if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO)) 253e795b717SSerge E. Hallyn if (ns_capable(inode_userns(inode), CAP_DAC_OVERRIDE)) 2541da177e4SLinus Torvalds return 0; 2551da177e4SLinus Torvalds 2561da177e4SLinus Torvalds /* 2571da177e4SLinus Torvalds * Searching includes executable on directories, else just read. 2581da177e4SLinus Torvalds */ 2597ea66001SSerge E. Hallyn mask &= MAY_READ | MAY_WRITE | MAY_EXEC; 260d594e7ecSAl Viro if (mask == MAY_READ) 261e795b717SSerge E. Hallyn if (ns_capable(inode_userns(inode), CAP_DAC_READ_SEARCH)) 2621da177e4SLinus Torvalds return 0; 2631da177e4SLinus Torvalds 2641da177e4SLinus Torvalds return -EACCES; 2651da177e4SLinus Torvalds } 2661da177e4SLinus Torvalds 267cb23beb5SChristoph Hellwig /** 268cb23beb5SChristoph Hellwig * inode_permission - check for access rights to a given inode 269cb23beb5SChristoph Hellwig * @inode: inode to check permission on 270cb23beb5SChristoph Hellwig * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) 271cb23beb5SChristoph Hellwig * 272cb23beb5SChristoph Hellwig * Used to check for read/write/execute permissions on an inode. 273cb23beb5SChristoph Hellwig * We use "fsuid" for this, letting us set arbitrary permissions 274cb23beb5SChristoph Hellwig * for filesystem access without changing the "normal" uids which 275cb23beb5SChristoph Hellwig * are used for other things. 276cb23beb5SChristoph Hellwig */ 277f419a2e3SAl Viro int inode_permission(struct inode *inode, int mask) 2781da177e4SLinus Torvalds { 279e6305c43SAl Viro int retval; 2801da177e4SLinus Torvalds 2811da177e4SLinus Torvalds if (mask & MAY_WRITE) { 28222590e41SMiklos Szeredi umode_t mode = inode->i_mode; 2831da177e4SLinus Torvalds 2841da177e4SLinus Torvalds /* 2851da177e4SLinus Torvalds * Nobody gets write access to a read-only fs. 2861da177e4SLinus Torvalds */ 2871da177e4SLinus Torvalds if (IS_RDONLY(inode) && 2881da177e4SLinus Torvalds (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) 2891da177e4SLinus Torvalds return -EROFS; 2901da177e4SLinus Torvalds 2911da177e4SLinus Torvalds /* 2921da177e4SLinus Torvalds * Nobody gets write access to an immutable file. 2931da177e4SLinus Torvalds */ 2941da177e4SLinus Torvalds if (IS_IMMUTABLE(inode)) 2951da177e4SLinus Torvalds return -EACCES; 2961da177e4SLinus Torvalds } 2971da177e4SLinus Torvalds 298acfa4380SAl Viro if (inode->i_op->permission) 29910556cb2SAl Viro retval = inode->i_op->permission(inode, mask); 300f696a365SMiklos Szeredi else 3012830ba7fSAl Viro retval = generic_permission(inode, mask); 302f696a365SMiklos Szeredi 3031da177e4SLinus Torvalds if (retval) 3041da177e4SLinus Torvalds return retval; 3051da177e4SLinus Torvalds 30608ce5f16SSerge E. Hallyn retval = devcgroup_inode_permission(inode, mask); 30708ce5f16SSerge E. Hallyn if (retval) 30808ce5f16SSerge E. Hallyn return retval; 30908ce5f16SSerge E. Hallyn 310d09ca739SEric Paris return security_inode_permission(inode, mask); 3111da177e4SLinus Torvalds } 3121da177e4SLinus Torvalds 313f4d6ff89SAl Viro /** 3145dd784d0SJan Blunck * path_get - get a reference to a path 3155dd784d0SJan Blunck * @path: path to get the reference to 3165dd784d0SJan Blunck * 3175dd784d0SJan Blunck * Given a path increment the reference count to the dentry and the vfsmount. 3185dd784d0SJan Blunck */ 3195dd784d0SJan Blunck void path_get(struct path *path) 3205dd784d0SJan Blunck { 3215dd784d0SJan Blunck mntget(path->mnt); 3225dd784d0SJan Blunck dget(path->dentry); 3235dd784d0SJan Blunck } 3245dd784d0SJan Blunck EXPORT_SYMBOL(path_get); 3255dd784d0SJan Blunck 3265dd784d0SJan Blunck /** 3271d957f9bSJan Blunck * path_put - put a reference to a path 3281d957f9bSJan Blunck * @path: path to put the reference to 3291d957f9bSJan Blunck * 3301d957f9bSJan Blunck * Given a path decrement the reference count to the dentry and the vfsmount. 3311d957f9bSJan Blunck */ 3321d957f9bSJan Blunck void path_put(struct path *path) 3331da177e4SLinus Torvalds { 3341d957f9bSJan Blunck dput(path->dentry); 3351d957f9bSJan Blunck mntput(path->mnt); 3361da177e4SLinus Torvalds } 3371d957f9bSJan Blunck EXPORT_SYMBOL(path_put); 3381da177e4SLinus Torvalds 33919660af7SAl Viro /* 34031e6b01fSNick Piggin * Path walking has 2 modes, rcu-walk and ref-walk (see 34119660af7SAl Viro * Documentation/filesystems/path-lookup.txt). In situations when we can't 34219660af7SAl Viro * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab 34319660af7SAl Viro * normal reference counts on dentries and vfsmounts to transition to rcu-walk 34419660af7SAl Viro * mode. Refcounts are grabbed at the last known good point before rcu-walk 34519660af7SAl Viro * got stuck, so ref-walk may continue from there. If this is not successful 34619660af7SAl Viro * (eg. a seqcount has changed), then failure is returned and it's up to caller 34719660af7SAl Viro * to restart the path walk from the beginning in ref-walk mode. 34831e6b01fSNick Piggin */ 34931e6b01fSNick Piggin 35031e6b01fSNick Piggin /** 35119660af7SAl Viro * unlazy_walk - try to switch to ref-walk mode. 35219660af7SAl Viro * @nd: nameidata pathwalk data 35319660af7SAl Viro * @dentry: child of nd->path.dentry or NULL 35439191628SRandy Dunlap * Returns: 0 on success, -ECHILD on failure 35531e6b01fSNick Piggin * 35619660af7SAl Viro * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry 35719660af7SAl Viro * for ref-walk mode. @dentry must be a path found by a do_lookup call on 35819660af7SAl Viro * @nd or NULL. Must be called from rcu-walk context. 35931e6b01fSNick Piggin */ 36019660af7SAl Viro static int unlazy_walk(struct nameidata *nd, struct dentry *dentry) 36131e6b01fSNick Piggin { 36231e6b01fSNick Piggin struct fs_struct *fs = current->fs; 36331e6b01fSNick Piggin struct dentry *parent = nd->path.dentry; 3645b6ca027SAl Viro int want_root = 0; 36531e6b01fSNick Piggin 36631e6b01fSNick Piggin BUG_ON(!(nd->flags & LOOKUP_RCU)); 3675b6ca027SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) { 3685b6ca027SAl Viro want_root = 1; 36931e6b01fSNick Piggin spin_lock(&fs->lock); 37031e6b01fSNick Piggin if (nd->root.mnt != fs->root.mnt || 37131e6b01fSNick Piggin nd->root.dentry != fs->root.dentry) 37231e6b01fSNick Piggin goto err_root; 37331e6b01fSNick Piggin } 37431e6b01fSNick Piggin spin_lock(&parent->d_lock); 37519660af7SAl Viro if (!dentry) { 37619660af7SAl Viro if (!__d_rcu_to_refcount(parent, nd->seq)) 37719660af7SAl Viro goto err_parent; 37819660af7SAl Viro BUG_ON(nd->inode != parent->d_inode); 37919660af7SAl Viro } else { 38094c0d4ecSAl Viro if (dentry->d_parent != parent) 38194c0d4ecSAl Viro goto err_parent; 38231e6b01fSNick Piggin spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); 38331e6b01fSNick Piggin if (!__d_rcu_to_refcount(dentry, nd->seq)) 38419660af7SAl Viro goto err_child; 38531e6b01fSNick Piggin /* 38619660af7SAl Viro * If the sequence check on the child dentry passed, then 38719660af7SAl Viro * the child has not been removed from its parent. This 38819660af7SAl Viro * means the parent dentry must be valid and able to take 38919660af7SAl Viro * a reference at this point. 39031e6b01fSNick Piggin */ 39131e6b01fSNick Piggin BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent); 39231e6b01fSNick Piggin BUG_ON(!parent->d_count); 39331e6b01fSNick Piggin parent->d_count++; 39431e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 39519660af7SAl Viro } 39631e6b01fSNick Piggin spin_unlock(&parent->d_lock); 3975b6ca027SAl Viro if (want_root) { 39831e6b01fSNick Piggin path_get(&nd->root); 39931e6b01fSNick Piggin spin_unlock(&fs->lock); 40031e6b01fSNick Piggin } 40131e6b01fSNick Piggin mntget(nd->path.mnt); 40231e6b01fSNick Piggin 40331e6b01fSNick Piggin rcu_read_unlock(); 40431e6b01fSNick Piggin br_read_unlock(vfsmount_lock); 40531e6b01fSNick Piggin nd->flags &= ~LOOKUP_RCU; 40631e6b01fSNick Piggin return 0; 40719660af7SAl Viro 40819660af7SAl Viro err_child: 40931e6b01fSNick Piggin spin_unlock(&dentry->d_lock); 41019660af7SAl Viro err_parent: 41131e6b01fSNick Piggin spin_unlock(&parent->d_lock); 41231e6b01fSNick Piggin err_root: 4135b6ca027SAl Viro if (want_root) 41431e6b01fSNick Piggin spin_unlock(&fs->lock); 41531e6b01fSNick Piggin return -ECHILD; 41631e6b01fSNick Piggin } 41731e6b01fSNick Piggin 41831e6b01fSNick Piggin /** 419834f2a4aSTrond Myklebust * release_open_intent - free up open intent resources 420834f2a4aSTrond Myklebust * @nd: pointer to nameidata 421834f2a4aSTrond Myklebust */ 422834f2a4aSTrond Myklebust void release_open_intent(struct nameidata *nd) 423834f2a4aSTrond Myklebust { 4242dab5974SLinus Torvalds struct file *file = nd->intent.open.file; 4252dab5974SLinus Torvalds 4262dab5974SLinus Torvalds if (file && !IS_ERR(file)) { 4272dab5974SLinus Torvalds if (file->f_path.dentry == NULL) 4282dab5974SLinus Torvalds put_filp(file); 429834f2a4aSTrond Myklebust else 4302dab5974SLinus Torvalds fput(file); 4312dab5974SLinus Torvalds } 432834f2a4aSTrond Myklebust } 433834f2a4aSTrond Myklebust 434f60aef7eSAl Viro static inline int d_revalidate(struct dentry *dentry, struct nameidata *nd) 43534286d66SNick Piggin { 436f60aef7eSAl Viro return dentry->d_op->d_revalidate(dentry, nd); 43734286d66SNick Piggin } 43834286d66SNick Piggin 4399f1fafeeSAl Viro /** 4409f1fafeeSAl Viro * complete_walk - successful completion of path walk 4419f1fafeeSAl Viro * @nd: pointer nameidata 44239159de2SJeff Layton * 4439f1fafeeSAl Viro * If we had been in RCU mode, drop out of it and legitimize nd->path. 4449f1fafeeSAl Viro * Revalidate the final result, unless we'd already done that during 4459f1fafeeSAl Viro * the path walk or the filesystem doesn't ask for it. Return 0 on 4469f1fafeeSAl Viro * success, -error on failure. In case of failure caller does not 4479f1fafeeSAl Viro * need to drop nd->path. 44839159de2SJeff Layton */ 4499f1fafeeSAl Viro static int complete_walk(struct nameidata *nd) 45039159de2SJeff Layton { 45116c2cd71SAl Viro struct dentry *dentry = nd->path.dentry; 45239159de2SJeff Layton int status; 45339159de2SJeff Layton 4549f1fafeeSAl Viro if (nd->flags & LOOKUP_RCU) { 4559f1fafeeSAl Viro nd->flags &= ~LOOKUP_RCU; 4569f1fafeeSAl Viro if (!(nd->flags & LOOKUP_ROOT)) 4579f1fafeeSAl Viro nd->root.mnt = NULL; 4589f1fafeeSAl Viro spin_lock(&dentry->d_lock); 4599f1fafeeSAl Viro if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) { 4609f1fafeeSAl Viro spin_unlock(&dentry->d_lock); 4619f1fafeeSAl Viro rcu_read_unlock(); 4629f1fafeeSAl Viro br_read_unlock(vfsmount_lock); 4639f1fafeeSAl Viro return -ECHILD; 4649f1fafeeSAl Viro } 4659f1fafeeSAl Viro BUG_ON(nd->inode != dentry->d_inode); 4669f1fafeeSAl Viro spin_unlock(&dentry->d_lock); 4679f1fafeeSAl Viro mntget(nd->path.mnt); 4689f1fafeeSAl Viro rcu_read_unlock(); 4699f1fafeeSAl Viro br_read_unlock(vfsmount_lock); 4709f1fafeeSAl Viro } 4719f1fafeeSAl Viro 47216c2cd71SAl Viro if (likely(!(nd->flags & LOOKUP_JUMPED))) 47339159de2SJeff Layton return 0; 47439159de2SJeff Layton 47516c2cd71SAl Viro if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE))) 47616c2cd71SAl Viro return 0; 47716c2cd71SAl Viro 47816c2cd71SAl Viro if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT))) 47916c2cd71SAl Viro return 0; 48016c2cd71SAl Viro 48116c2cd71SAl Viro /* Note: we do not d_invalidate() */ 48234286d66SNick Piggin status = d_revalidate(dentry, nd); 48339159de2SJeff Layton if (status > 0) 48439159de2SJeff Layton return 0; 48539159de2SJeff Layton 48616c2cd71SAl Viro if (!status) 48739159de2SJeff Layton status = -ESTALE; 48816c2cd71SAl Viro 4899f1fafeeSAl Viro path_put(&nd->path); 49039159de2SJeff Layton return status; 49139159de2SJeff Layton } 49239159de2SJeff Layton 4932a737871SAl Viro static __always_inline void set_root(struct nameidata *nd) 4942a737871SAl Viro { 495f7ad3c6bSMiklos Szeredi if (!nd->root.mnt) 496f7ad3c6bSMiklos Szeredi get_fs_root(current->fs, &nd->root); 4972a737871SAl Viro } 4982a737871SAl Viro 4996de88d72SAl Viro static int link_path_walk(const char *, struct nameidata *); 5006de88d72SAl Viro 50131e6b01fSNick Piggin static __always_inline void set_root_rcu(struct nameidata *nd) 50231e6b01fSNick Piggin { 50331e6b01fSNick Piggin if (!nd->root.mnt) { 50431e6b01fSNick Piggin struct fs_struct *fs = current->fs; 505c28cc364SNick Piggin unsigned seq; 506c28cc364SNick Piggin 507c28cc364SNick Piggin do { 508c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 50931e6b01fSNick Piggin nd->root = fs->root; 510c1530019STim Chen nd->seq = __read_seqcount_begin(&nd->root.dentry->d_seq); 511c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 51231e6b01fSNick Piggin } 51331e6b01fSNick Piggin } 51431e6b01fSNick Piggin 515f1662356SArjan van de Ven static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link) 5161da177e4SLinus Torvalds { 51731e6b01fSNick Piggin int ret; 51831e6b01fSNick Piggin 5191da177e4SLinus Torvalds if (IS_ERR(link)) 5201da177e4SLinus Torvalds goto fail; 5211da177e4SLinus Torvalds 5221da177e4SLinus Torvalds if (*link == '/') { 5232a737871SAl Viro set_root(nd); 5241d957f9bSJan Blunck path_put(&nd->path); 5252a737871SAl Viro nd->path = nd->root; 5262a737871SAl Viro path_get(&nd->root); 52716c2cd71SAl Viro nd->flags |= LOOKUP_JUMPED; 5281da177e4SLinus Torvalds } 52931e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 530b4091d5fSChristoph Hellwig 53131e6b01fSNick Piggin ret = link_path_walk(link, nd); 53231e6b01fSNick Piggin return ret; 5331da177e4SLinus Torvalds fail: 5341d957f9bSJan Blunck path_put(&nd->path); 5351da177e4SLinus Torvalds return PTR_ERR(link); 5361da177e4SLinus Torvalds } 5371da177e4SLinus Torvalds 5381d957f9bSJan Blunck static void path_put_conditional(struct path *path, struct nameidata *nd) 539051d3812SIan Kent { 540051d3812SIan Kent dput(path->dentry); 5414ac91378SJan Blunck if (path->mnt != nd->path.mnt) 542051d3812SIan Kent mntput(path->mnt); 543051d3812SIan Kent } 544051d3812SIan Kent 5457b9337aaSNick Piggin static inline void path_to_nameidata(const struct path *path, 5467b9337aaSNick Piggin struct nameidata *nd) 547051d3812SIan Kent { 54831e6b01fSNick Piggin if (!(nd->flags & LOOKUP_RCU)) { 5494ac91378SJan Blunck dput(nd->path.dentry); 55031e6b01fSNick Piggin if (nd->path.mnt != path->mnt) 5514ac91378SJan Blunck mntput(nd->path.mnt); 5529a229683SHuang Shijie } 55331e6b01fSNick Piggin nd->path.mnt = path->mnt; 5544ac91378SJan Blunck nd->path.dentry = path->dentry; 555051d3812SIan Kent } 556051d3812SIan Kent 557574197e0SAl Viro static inline void put_link(struct nameidata *nd, struct path *link, void *cookie) 558574197e0SAl Viro { 559574197e0SAl Viro struct inode *inode = link->dentry->d_inode; 560574197e0SAl Viro if (!IS_ERR(cookie) && inode->i_op->put_link) 561574197e0SAl Viro inode->i_op->put_link(link->dentry, nd, cookie); 562574197e0SAl Viro path_put(link); 563574197e0SAl Viro } 564574197e0SAl Viro 565def4af30SAl Viro static __always_inline int 566574197e0SAl Viro follow_link(struct path *link, struct nameidata *nd, void **p) 5671da177e4SLinus Torvalds { 5681da177e4SLinus Torvalds int error; 5697b9337aaSNick Piggin struct dentry *dentry = link->dentry; 5701da177e4SLinus Torvalds 571844a3917SAl Viro BUG_ON(nd->flags & LOOKUP_RCU); 572844a3917SAl Viro 5730e794589SAl Viro if (link->mnt == nd->path.mnt) 5740e794589SAl Viro mntget(link->mnt); 5750e794589SAl Viro 576574197e0SAl Viro if (unlikely(current->total_link_count >= 40)) { 577574197e0SAl Viro *p = ERR_PTR(-ELOOP); /* no ->put_link(), please */ 578574197e0SAl Viro path_put(&nd->path); 579574197e0SAl Viro return -ELOOP; 580574197e0SAl Viro } 581574197e0SAl Viro cond_resched(); 582574197e0SAl Viro current->total_link_count++; 583574197e0SAl Viro 5847b9337aaSNick Piggin touch_atime(link->mnt, dentry); 5851da177e4SLinus Torvalds nd_set_link(nd, NULL); 586cd4e91d3SAl Viro 58736f3b4f6SAl Viro error = security_inode_follow_link(link->dentry, nd); 58836f3b4f6SAl Viro if (error) { 58936f3b4f6SAl Viro *p = ERR_PTR(error); /* no ->put_link(), please */ 59036f3b4f6SAl Viro path_put(&nd->path); 59136f3b4f6SAl Viro return error; 59236f3b4f6SAl Viro } 59336f3b4f6SAl Viro 59486acdca1SAl Viro nd->last_type = LAST_BIND; 595def4af30SAl Viro *p = dentry->d_inode->i_op->follow_link(dentry, nd); 596def4af30SAl Viro error = PTR_ERR(*p); 597def4af30SAl Viro if (!IS_ERR(*p)) { 5981da177e4SLinus Torvalds char *s = nd_get_link(nd); 599cc314eefSLinus Torvalds error = 0; 6001da177e4SLinus Torvalds if (s) 6011da177e4SLinus Torvalds error = __vfs_follow_link(nd, s); 602bcda7652SAl Viro else if (nd->last_type == LAST_BIND) { 60316c2cd71SAl Viro nd->flags |= LOOKUP_JUMPED; 604b21041d0SAl Viro nd->inode = nd->path.dentry->d_inode; 605b21041d0SAl Viro if (nd->inode->i_op->follow_link) { 606bcda7652SAl Viro /* stepped on a _really_ weird one */ 607bcda7652SAl Viro path_put(&nd->path); 608bcda7652SAl Viro error = -ELOOP; 609bcda7652SAl Viro } 610bcda7652SAl Viro } 6111da177e4SLinus Torvalds } 6121da177e4SLinus Torvalds return error; 6131da177e4SLinus Torvalds } 6141da177e4SLinus Torvalds 61531e6b01fSNick Piggin static int follow_up_rcu(struct path *path) 61631e6b01fSNick Piggin { 61731e6b01fSNick Piggin struct vfsmount *parent; 61831e6b01fSNick Piggin struct dentry *mountpoint; 61931e6b01fSNick Piggin 62031e6b01fSNick Piggin parent = path->mnt->mnt_parent; 62131e6b01fSNick Piggin if (parent == path->mnt) 62231e6b01fSNick Piggin return 0; 62331e6b01fSNick Piggin mountpoint = path->mnt->mnt_mountpoint; 62431e6b01fSNick Piggin path->dentry = mountpoint; 62531e6b01fSNick Piggin path->mnt = parent; 62631e6b01fSNick Piggin return 1; 62731e6b01fSNick Piggin } 62831e6b01fSNick Piggin 629bab77ebfSAl Viro int follow_up(struct path *path) 6301da177e4SLinus Torvalds { 6311da177e4SLinus Torvalds struct vfsmount *parent; 6321da177e4SLinus Torvalds struct dentry *mountpoint; 63399b7db7bSNick Piggin 63499b7db7bSNick Piggin br_read_lock(vfsmount_lock); 635bab77ebfSAl Viro parent = path->mnt->mnt_parent; 636bab77ebfSAl Viro if (parent == path->mnt) { 63799b7db7bSNick Piggin br_read_unlock(vfsmount_lock); 6381da177e4SLinus Torvalds return 0; 6391da177e4SLinus Torvalds } 6401da177e4SLinus Torvalds mntget(parent); 641bab77ebfSAl Viro mountpoint = dget(path->mnt->mnt_mountpoint); 64299b7db7bSNick Piggin br_read_unlock(vfsmount_lock); 643bab77ebfSAl Viro dput(path->dentry); 644bab77ebfSAl Viro path->dentry = mountpoint; 645bab77ebfSAl Viro mntput(path->mnt); 646bab77ebfSAl Viro path->mnt = parent; 6471da177e4SLinus Torvalds return 1; 6481da177e4SLinus Torvalds } 6491da177e4SLinus Torvalds 650b5c84bf6SNick Piggin /* 6519875cf80SDavid Howells * Perform an automount 6529875cf80SDavid Howells * - return -EISDIR to tell follow_managed() to stop and return the path we 6539875cf80SDavid Howells * were called with. 6541da177e4SLinus Torvalds */ 6559875cf80SDavid Howells static int follow_automount(struct path *path, unsigned flags, 6569875cf80SDavid Howells bool *need_mntput) 65731e6b01fSNick Piggin { 6589875cf80SDavid Howells struct vfsmount *mnt; 659ea5b778aSDavid Howells int err; 6609875cf80SDavid Howells 6619875cf80SDavid Howells if (!path->dentry->d_op || !path->dentry->d_op->d_automount) 6629875cf80SDavid Howells return -EREMOTE; 6639875cf80SDavid Howells 6646f45b656SDavid Howells /* We don't want to mount if someone supplied AT_NO_AUTOMOUNT 6656f45b656SDavid Howells * and this is the terminal part of the path. 6666f45b656SDavid Howells */ 66749084c3bSAl Viro if ((flags & LOOKUP_NO_AUTOMOUNT) && !(flags & LOOKUP_PARENT)) 6686f45b656SDavid Howells return -EISDIR; /* we actually want to stop here */ 6696f45b656SDavid Howells 6709875cf80SDavid Howells /* We want to mount if someone is trying to open/create a file of any 6719875cf80SDavid Howells * type under the mountpoint, wants to traverse through the mountpoint 6729875cf80SDavid Howells * or wants to open the mounted directory. 6739875cf80SDavid Howells * 6749875cf80SDavid Howells * We don't want to mount if someone's just doing a stat and they've 6759875cf80SDavid Howells * set AT_SYMLINK_NOFOLLOW - unless they're stat'ing a directory and 6769875cf80SDavid Howells * appended a '/' to the name. 6779875cf80SDavid Howells */ 6789875cf80SDavid Howells if (!(flags & LOOKUP_FOLLOW) && 67949084c3bSAl Viro !(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY | 6809875cf80SDavid Howells LOOKUP_OPEN | LOOKUP_CREATE))) 6819875cf80SDavid Howells return -EISDIR; 6829875cf80SDavid Howells 6839875cf80SDavid Howells current->total_link_count++; 6849875cf80SDavid Howells if (current->total_link_count >= 40) 6859875cf80SDavid Howells return -ELOOP; 6869875cf80SDavid Howells 6879875cf80SDavid Howells mnt = path->dentry->d_op->d_automount(path); 6889875cf80SDavid Howells if (IS_ERR(mnt)) { 6899875cf80SDavid Howells /* 6909875cf80SDavid Howells * The filesystem is allowed to return -EISDIR here to indicate 6919875cf80SDavid Howells * it doesn't want to automount. For instance, autofs would do 6929875cf80SDavid Howells * this so that its userspace daemon can mount on this dentry. 6939875cf80SDavid Howells * 6949875cf80SDavid Howells * However, we can only permit this if it's a terminal point in 6959875cf80SDavid Howells * the path being looked up; if it wasn't then the remainder of 6969875cf80SDavid Howells * the path is inaccessible and we should say so. 6979875cf80SDavid Howells */ 69849084c3bSAl Viro if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT)) 6999875cf80SDavid Howells return -EREMOTE; 7009875cf80SDavid Howells return PTR_ERR(mnt); 70131e6b01fSNick Piggin } 702ea5b778aSDavid Howells 7039875cf80SDavid Howells if (!mnt) /* mount collision */ 7049875cf80SDavid Howells return 0; 7059875cf80SDavid Howells 7068aef1884SAl Viro if (!*need_mntput) { 7078aef1884SAl Viro /* lock_mount() may release path->mnt on error */ 7088aef1884SAl Viro mntget(path->mnt); 7098aef1884SAl Viro *need_mntput = true; 7108aef1884SAl Viro } 71119a167afSAl Viro err = finish_automount(mnt, path); 712ea5b778aSDavid Howells 713ea5b778aSDavid Howells switch (err) { 714ea5b778aSDavid Howells case -EBUSY: 715ea5b778aSDavid Howells /* Someone else made a mount here whilst we were busy */ 71619a167afSAl Viro return 0; 717ea5b778aSDavid Howells case 0: 7188aef1884SAl Viro path_put(path); 7199875cf80SDavid Howells path->mnt = mnt; 7209875cf80SDavid Howells path->dentry = dget(mnt->mnt_root); 7219875cf80SDavid Howells return 0; 72219a167afSAl Viro default: 72319a167afSAl Viro return err; 7249875cf80SDavid Howells } 72519a167afSAl Viro 726ea5b778aSDavid Howells } 7279875cf80SDavid Howells 7289875cf80SDavid Howells /* 7299875cf80SDavid Howells * Handle a dentry that is managed in some way. 730cc53ce53SDavid Howells * - Flagged for transit management (autofs) 7319875cf80SDavid Howells * - Flagged as mountpoint 7329875cf80SDavid Howells * - Flagged as automount point 7339875cf80SDavid Howells * 7349875cf80SDavid Howells * This may only be called in refwalk mode. 7359875cf80SDavid Howells * 7369875cf80SDavid Howells * Serialization is taken care of in namespace.c 7379875cf80SDavid Howells */ 7389875cf80SDavid Howells static int follow_managed(struct path *path, unsigned flags) 7399875cf80SDavid Howells { 7408aef1884SAl Viro struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */ 7419875cf80SDavid Howells unsigned managed; 7429875cf80SDavid Howells bool need_mntput = false; 7438aef1884SAl Viro int ret = 0; 7449875cf80SDavid Howells 7459875cf80SDavid Howells /* Given that we're not holding a lock here, we retain the value in a 7469875cf80SDavid Howells * local variable for each dentry as we look at it so that we don't see 7479875cf80SDavid Howells * the components of that value change under us */ 7489875cf80SDavid Howells while (managed = ACCESS_ONCE(path->dentry->d_flags), 7499875cf80SDavid Howells managed &= DCACHE_MANAGED_DENTRY, 7509875cf80SDavid Howells unlikely(managed != 0)) { 751cc53ce53SDavid Howells /* Allow the filesystem to manage the transit without i_mutex 752cc53ce53SDavid Howells * being held. */ 753cc53ce53SDavid Howells if (managed & DCACHE_MANAGE_TRANSIT) { 754cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op); 755cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op->d_manage); 7561aed3e42SAl Viro ret = path->dentry->d_op->d_manage(path->dentry, false); 757cc53ce53SDavid Howells if (ret < 0) 7588aef1884SAl Viro break; 759cc53ce53SDavid Howells } 760cc53ce53SDavid Howells 7619875cf80SDavid Howells /* Transit to a mounted filesystem. */ 7629875cf80SDavid Howells if (managed & DCACHE_MOUNTED) { 7639875cf80SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 7649875cf80SDavid Howells if (mounted) { 7659875cf80SDavid Howells dput(path->dentry); 7669875cf80SDavid Howells if (need_mntput) 767463ffb2eSAl Viro mntput(path->mnt); 768463ffb2eSAl Viro path->mnt = mounted; 769463ffb2eSAl Viro path->dentry = dget(mounted->mnt_root); 7709875cf80SDavid Howells need_mntput = true; 7719875cf80SDavid Howells continue; 772463ffb2eSAl Viro } 773463ffb2eSAl Viro 7749875cf80SDavid Howells /* Something is mounted on this dentry in another 7759875cf80SDavid Howells * namespace and/or whatever was mounted there in this 7769875cf80SDavid Howells * namespace got unmounted before we managed to get the 7779875cf80SDavid Howells * vfsmount_lock */ 7781da177e4SLinus Torvalds } 7799875cf80SDavid Howells 7809875cf80SDavid Howells /* Handle an automount point */ 7819875cf80SDavid Howells if (managed & DCACHE_NEED_AUTOMOUNT) { 7829875cf80SDavid Howells ret = follow_automount(path, flags, &need_mntput); 7839875cf80SDavid Howells if (ret < 0) 7848aef1884SAl Viro break; 7859875cf80SDavid Howells continue; 7869875cf80SDavid Howells } 7879875cf80SDavid Howells 7889875cf80SDavid Howells /* We didn't change the current path point */ 7899875cf80SDavid Howells break; 7909875cf80SDavid Howells } 7918aef1884SAl Viro 7928aef1884SAl Viro if (need_mntput && path->mnt == mnt) 7938aef1884SAl Viro mntput(path->mnt); 7948aef1884SAl Viro if (ret == -EISDIR) 7958aef1884SAl Viro ret = 0; 7968aef1884SAl Viro return ret; 7971da177e4SLinus Torvalds } 7981da177e4SLinus Torvalds 799cc53ce53SDavid Howells int follow_down_one(struct path *path) 8001da177e4SLinus Torvalds { 8011da177e4SLinus Torvalds struct vfsmount *mounted; 8021da177e4SLinus Torvalds 8031c755af4SAl Viro mounted = lookup_mnt(path); 8041da177e4SLinus Torvalds if (mounted) { 8059393bd07SAl Viro dput(path->dentry); 8069393bd07SAl Viro mntput(path->mnt); 8079393bd07SAl Viro path->mnt = mounted; 8089393bd07SAl Viro path->dentry = dget(mounted->mnt_root); 8091da177e4SLinus Torvalds return 1; 8101da177e4SLinus Torvalds } 8111da177e4SLinus Torvalds return 0; 8121da177e4SLinus Torvalds } 8131da177e4SLinus Torvalds 81462a7375eSIan Kent static inline bool managed_dentry_might_block(struct dentry *dentry) 81562a7375eSIan Kent { 81662a7375eSIan Kent return (dentry->d_flags & DCACHE_MANAGE_TRANSIT && 81762a7375eSIan Kent dentry->d_op->d_manage(dentry, true) < 0); 81862a7375eSIan Kent } 81962a7375eSIan Kent 8209875cf80SDavid Howells /* 821287548e4SAl Viro * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if 822287548e4SAl Viro * we meet a managed dentry that would need blocking. 8239875cf80SDavid Howells */ 8249875cf80SDavid Howells static bool __follow_mount_rcu(struct nameidata *nd, struct path *path, 825287548e4SAl Viro struct inode **inode) 8269875cf80SDavid Howells { 82762a7375eSIan Kent for (;;) { 8289875cf80SDavid Howells struct vfsmount *mounted; 82962a7375eSIan Kent /* 83062a7375eSIan Kent * Don't forget we might have a non-mountpoint managed dentry 83162a7375eSIan Kent * that wants to block transit. 83262a7375eSIan Kent */ 833287548e4SAl Viro if (unlikely(managed_dentry_might_block(path->dentry))) 834ab90911fSDavid Howells return false; 83562a7375eSIan Kent 83662a7375eSIan Kent if (!d_mountpoint(path->dentry)) 83762a7375eSIan Kent break; 83862a7375eSIan Kent 8399875cf80SDavid Howells mounted = __lookup_mnt(path->mnt, path->dentry, 1); 8409875cf80SDavid Howells if (!mounted) 8419875cf80SDavid Howells break; 8429875cf80SDavid Howells path->mnt = mounted; 8439875cf80SDavid Howells path->dentry = mounted->mnt_root; 8449875cf80SDavid Howells nd->seq = read_seqcount_begin(&path->dentry->d_seq); 84559430262SLinus Torvalds /* 84659430262SLinus Torvalds * Update the inode too. We don't need to re-check the 84759430262SLinus Torvalds * dentry sequence number here after this d_inode read, 84859430262SLinus Torvalds * because a mount-point is always pinned. 84959430262SLinus Torvalds */ 85059430262SLinus Torvalds *inode = path->dentry->d_inode; 8519875cf80SDavid Howells } 8529875cf80SDavid Howells return true; 8539875cf80SDavid Howells } 8549875cf80SDavid Howells 855dea39376SAl Viro static void follow_mount_rcu(struct nameidata *nd) 856287548e4SAl Viro { 857dea39376SAl Viro while (d_mountpoint(nd->path.dentry)) { 858287548e4SAl Viro struct vfsmount *mounted; 859dea39376SAl Viro mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1); 860287548e4SAl Viro if (!mounted) 861287548e4SAl Viro break; 862dea39376SAl Viro nd->path.mnt = mounted; 863dea39376SAl Viro nd->path.dentry = mounted->mnt_root; 864dea39376SAl Viro nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); 865287548e4SAl Viro } 866287548e4SAl Viro } 867287548e4SAl Viro 86831e6b01fSNick Piggin static int follow_dotdot_rcu(struct nameidata *nd) 86931e6b01fSNick Piggin { 87031e6b01fSNick Piggin set_root_rcu(nd); 87131e6b01fSNick Piggin 87231e6b01fSNick Piggin while (1) { 87331e6b01fSNick Piggin if (nd->path.dentry == nd->root.dentry && 87431e6b01fSNick Piggin nd->path.mnt == nd->root.mnt) { 87531e6b01fSNick Piggin break; 87631e6b01fSNick Piggin } 87731e6b01fSNick Piggin if (nd->path.dentry != nd->path.mnt->mnt_root) { 87831e6b01fSNick Piggin struct dentry *old = nd->path.dentry; 87931e6b01fSNick Piggin struct dentry *parent = old->d_parent; 88031e6b01fSNick Piggin unsigned seq; 88131e6b01fSNick Piggin 88231e6b01fSNick Piggin seq = read_seqcount_begin(&parent->d_seq); 88331e6b01fSNick Piggin if (read_seqcount_retry(&old->d_seq, nd->seq)) 884ef7562d5SAl Viro goto failed; 88531e6b01fSNick Piggin nd->path.dentry = parent; 88631e6b01fSNick Piggin nd->seq = seq; 88731e6b01fSNick Piggin break; 88831e6b01fSNick Piggin } 88931e6b01fSNick Piggin if (!follow_up_rcu(&nd->path)) 89031e6b01fSNick Piggin break; 89131e6b01fSNick Piggin nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); 89231e6b01fSNick Piggin } 893dea39376SAl Viro follow_mount_rcu(nd); 894dea39376SAl Viro nd->inode = nd->path.dentry->d_inode; 89531e6b01fSNick Piggin return 0; 896ef7562d5SAl Viro 897ef7562d5SAl Viro failed: 898ef7562d5SAl Viro nd->flags &= ~LOOKUP_RCU; 8995b6ca027SAl Viro if (!(nd->flags & LOOKUP_ROOT)) 900ef7562d5SAl Viro nd->root.mnt = NULL; 901ef7562d5SAl Viro rcu_read_unlock(); 902ef7562d5SAl Viro br_read_unlock(vfsmount_lock); 903ef7562d5SAl Viro return -ECHILD; 90431e6b01fSNick Piggin } 90531e6b01fSNick Piggin 9069875cf80SDavid Howells /* 907cc53ce53SDavid Howells * Follow down to the covering mount currently visible to userspace. At each 908cc53ce53SDavid Howells * point, the filesystem owning that dentry may be queried as to whether the 909cc53ce53SDavid Howells * caller is permitted to proceed or not. 910cc53ce53SDavid Howells */ 9117cc90cc3SAl Viro int follow_down(struct path *path) 912cc53ce53SDavid Howells { 913cc53ce53SDavid Howells unsigned managed; 914cc53ce53SDavid Howells int ret; 915cc53ce53SDavid Howells 916cc53ce53SDavid Howells while (managed = ACCESS_ONCE(path->dentry->d_flags), 917cc53ce53SDavid Howells unlikely(managed & DCACHE_MANAGED_DENTRY)) { 918cc53ce53SDavid Howells /* Allow the filesystem to manage the transit without i_mutex 919cc53ce53SDavid Howells * being held. 920cc53ce53SDavid Howells * 921cc53ce53SDavid Howells * We indicate to the filesystem if someone is trying to mount 922cc53ce53SDavid Howells * something here. This gives autofs the chance to deny anyone 923cc53ce53SDavid Howells * other than its daemon the right to mount on its 924cc53ce53SDavid Howells * superstructure. 925cc53ce53SDavid Howells * 926cc53ce53SDavid Howells * The filesystem may sleep at this point. 927cc53ce53SDavid Howells */ 928cc53ce53SDavid Howells if (managed & DCACHE_MANAGE_TRANSIT) { 929cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op); 930cc53ce53SDavid Howells BUG_ON(!path->dentry->d_op->d_manage); 931ab90911fSDavid Howells ret = path->dentry->d_op->d_manage( 9321aed3e42SAl Viro path->dentry, false); 933cc53ce53SDavid Howells if (ret < 0) 934cc53ce53SDavid Howells return ret == -EISDIR ? 0 : ret; 935cc53ce53SDavid Howells } 936cc53ce53SDavid Howells 937cc53ce53SDavid Howells /* Transit to a mounted filesystem. */ 938cc53ce53SDavid Howells if (managed & DCACHE_MOUNTED) { 939cc53ce53SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 940cc53ce53SDavid Howells if (!mounted) 941cc53ce53SDavid Howells break; 942cc53ce53SDavid Howells dput(path->dentry); 943cc53ce53SDavid Howells mntput(path->mnt); 944cc53ce53SDavid Howells path->mnt = mounted; 945cc53ce53SDavid Howells path->dentry = dget(mounted->mnt_root); 946cc53ce53SDavid Howells continue; 947cc53ce53SDavid Howells } 948cc53ce53SDavid Howells 949cc53ce53SDavid Howells /* Don't handle automount points here */ 950cc53ce53SDavid Howells break; 951cc53ce53SDavid Howells } 952cc53ce53SDavid Howells return 0; 953cc53ce53SDavid Howells } 954cc53ce53SDavid Howells 955cc53ce53SDavid Howells /* 9569875cf80SDavid Howells * Skip to top of mountpoint pile in refwalk mode for follow_dotdot() 9579875cf80SDavid Howells */ 9589875cf80SDavid Howells static void follow_mount(struct path *path) 9599875cf80SDavid Howells { 9609875cf80SDavid Howells while (d_mountpoint(path->dentry)) { 9619875cf80SDavid Howells struct vfsmount *mounted = lookup_mnt(path); 9629875cf80SDavid Howells if (!mounted) 9639875cf80SDavid Howells break; 9649875cf80SDavid Howells dput(path->dentry); 9659875cf80SDavid Howells mntput(path->mnt); 9669875cf80SDavid Howells path->mnt = mounted; 9679875cf80SDavid Howells path->dentry = dget(mounted->mnt_root); 9689875cf80SDavid Howells } 9699875cf80SDavid Howells } 9709875cf80SDavid Howells 97131e6b01fSNick Piggin static void follow_dotdot(struct nameidata *nd) 9721da177e4SLinus Torvalds { 9732a737871SAl Viro set_root(nd); 974e518ddb7SAndreas Mohr 9751da177e4SLinus Torvalds while(1) { 9764ac91378SJan Blunck struct dentry *old = nd->path.dentry; 9771da177e4SLinus Torvalds 9782a737871SAl Viro if (nd->path.dentry == nd->root.dentry && 9792a737871SAl Viro nd->path.mnt == nd->root.mnt) { 9801da177e4SLinus Torvalds break; 9811da177e4SLinus Torvalds } 9824ac91378SJan Blunck if (nd->path.dentry != nd->path.mnt->mnt_root) { 9833088dd70SAl Viro /* rare case of legitimate dget_parent()... */ 9843088dd70SAl Viro nd->path.dentry = dget_parent(nd->path.dentry); 9851da177e4SLinus Torvalds dput(old); 9861da177e4SLinus Torvalds break; 9871da177e4SLinus Torvalds } 9883088dd70SAl Viro if (!follow_up(&nd->path)) 9891da177e4SLinus Torvalds break; 9901da177e4SLinus Torvalds } 99179ed0226SAl Viro follow_mount(&nd->path); 99231e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 9931da177e4SLinus Torvalds } 9941da177e4SLinus Torvalds 9951da177e4SLinus Torvalds /* 996baa03890SNick Piggin * Allocate a dentry with name and parent, and perform a parent 997baa03890SNick Piggin * directory ->lookup on it. Returns the new dentry, or ERR_PTR 998baa03890SNick Piggin * on error. parent->d_inode->i_mutex must be held. d_lookup must 999baa03890SNick Piggin * have verified that no child exists while under i_mutex. 1000baa03890SNick Piggin */ 1001baa03890SNick Piggin static struct dentry *d_alloc_and_lookup(struct dentry *parent, 1002baa03890SNick Piggin struct qstr *name, struct nameidata *nd) 1003baa03890SNick Piggin { 1004baa03890SNick Piggin struct inode *inode = parent->d_inode; 1005baa03890SNick Piggin struct dentry *dentry; 1006baa03890SNick Piggin struct dentry *old; 1007baa03890SNick Piggin 1008baa03890SNick Piggin /* Don't create child dentry for a dead directory. */ 1009baa03890SNick Piggin if (unlikely(IS_DEADDIR(inode))) 1010baa03890SNick Piggin return ERR_PTR(-ENOENT); 1011baa03890SNick Piggin 1012baa03890SNick Piggin dentry = d_alloc(parent, name); 1013baa03890SNick Piggin if (unlikely(!dentry)) 1014baa03890SNick Piggin return ERR_PTR(-ENOMEM); 1015baa03890SNick Piggin 1016baa03890SNick Piggin old = inode->i_op->lookup(inode, dentry, nd); 1017baa03890SNick Piggin if (unlikely(old)) { 1018baa03890SNick Piggin dput(dentry); 1019baa03890SNick Piggin dentry = old; 1020baa03890SNick Piggin } 1021baa03890SNick Piggin return dentry; 1022baa03890SNick Piggin } 1023baa03890SNick Piggin 1024baa03890SNick Piggin /* 102544396f4bSJosef Bacik * We already have a dentry, but require a lookup to be performed on the parent 102644396f4bSJosef Bacik * directory to fill in d_inode. Returns the new dentry, or ERR_PTR on error. 102744396f4bSJosef Bacik * parent->d_inode->i_mutex must be held. d_lookup must have verified that no 102844396f4bSJosef Bacik * child exists while under i_mutex. 102944396f4bSJosef Bacik */ 103044396f4bSJosef Bacik static struct dentry *d_inode_lookup(struct dentry *parent, struct dentry *dentry, 103144396f4bSJosef Bacik struct nameidata *nd) 103244396f4bSJosef Bacik { 103344396f4bSJosef Bacik struct inode *inode = parent->d_inode; 103444396f4bSJosef Bacik struct dentry *old; 103544396f4bSJosef Bacik 103644396f4bSJosef Bacik /* Don't create child dentry for a dead directory. */ 103744396f4bSJosef Bacik if (unlikely(IS_DEADDIR(inode))) 103844396f4bSJosef Bacik return ERR_PTR(-ENOENT); 103944396f4bSJosef Bacik 104044396f4bSJosef Bacik old = inode->i_op->lookup(inode, dentry, nd); 104144396f4bSJosef Bacik if (unlikely(old)) { 104244396f4bSJosef Bacik dput(dentry); 104344396f4bSJosef Bacik dentry = old; 104444396f4bSJosef Bacik } 104544396f4bSJosef Bacik return dentry; 104644396f4bSJosef Bacik } 104744396f4bSJosef Bacik 104844396f4bSJosef Bacik /* 10491da177e4SLinus Torvalds * It's more convoluted than I'd like it to be, but... it's still fairly 10501da177e4SLinus Torvalds * small and for now I'd prefer to have fast path as straight as possible. 10511da177e4SLinus Torvalds * It _is_ time-critical. 10521da177e4SLinus Torvalds */ 10531da177e4SLinus Torvalds static int do_lookup(struct nameidata *nd, struct qstr *name, 105431e6b01fSNick Piggin struct path *path, struct inode **inode) 10551da177e4SLinus Torvalds { 10564ac91378SJan Blunck struct vfsmount *mnt = nd->path.mnt; 105731e6b01fSNick Piggin struct dentry *dentry, *parent = nd->path.dentry; 10585a18fff2SAl Viro int need_reval = 1; 10595a18fff2SAl Viro int status = 1; 10609875cf80SDavid Howells int err; 10619875cf80SDavid Howells 10623cac260aSAl Viro /* 1063b04f784eSNick Piggin * Rename seqlock is not required here because in the off chance 1064b04f784eSNick Piggin * of a false negative due to a concurrent rename, we're going to 1065b04f784eSNick Piggin * do the non-racy lookup, below. 1066b04f784eSNick Piggin */ 106731e6b01fSNick Piggin if (nd->flags & LOOKUP_RCU) { 106831e6b01fSNick Piggin unsigned seq; 106931e6b01fSNick Piggin *inode = nd->inode; 107031e6b01fSNick Piggin dentry = __d_lookup_rcu(parent, name, &seq, inode); 10715a18fff2SAl Viro if (!dentry) 10725a18fff2SAl Viro goto unlazy; 10735a18fff2SAl Viro 107431e6b01fSNick Piggin /* Memory barrier in read_seqcount_begin of child is enough */ 107531e6b01fSNick Piggin if (__read_seqcount_retry(&parent->d_seq, nd->seq)) 107631e6b01fSNick Piggin return -ECHILD; 107731e6b01fSNick Piggin nd->seq = seq; 10785a18fff2SAl Viro 107924643087SAl Viro if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) { 10805a18fff2SAl Viro status = d_revalidate(dentry, nd); 10815a18fff2SAl Viro if (unlikely(status <= 0)) { 10825a18fff2SAl Viro if (status != -ECHILD) 10835a18fff2SAl Viro need_reval = 0; 10845a18fff2SAl Viro goto unlazy; 10855a18fff2SAl Viro } 108624643087SAl Viro } 108744396f4bSJosef Bacik if (unlikely(d_need_lookup(dentry))) 108844396f4bSJosef Bacik goto unlazy; 108931e6b01fSNick Piggin path->mnt = mnt; 109031e6b01fSNick Piggin path->dentry = dentry; 1091d6e9bd25SAl Viro if (unlikely(!__follow_mount_rcu(nd, path, inode))) 1092d6e9bd25SAl Viro goto unlazy; 1093d6e9bd25SAl Viro if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT)) 1094d6e9bd25SAl Viro goto unlazy; 10959875cf80SDavid Howells return 0; 10965a18fff2SAl Viro unlazy: 109719660af7SAl Viro if (unlazy_walk(nd, dentry)) 10985a18fff2SAl Viro return -ECHILD; 10995a18fff2SAl Viro } else { 110031e6b01fSNick Piggin dentry = __d_lookup(parent, name); 110124643087SAl Viro } 11025a18fff2SAl Viro 110344396f4bSJosef Bacik if (dentry && unlikely(d_need_lookup(dentry))) { 110444396f4bSJosef Bacik dput(dentry); 110544396f4bSJosef Bacik dentry = NULL; 110644396f4bSJosef Bacik } 11075a18fff2SAl Viro retry: 11085a18fff2SAl Viro if (unlikely(!dentry)) { 11095a18fff2SAl Viro struct inode *dir = parent->d_inode; 11105a18fff2SAl Viro BUG_ON(nd->inode != dir); 11115a18fff2SAl Viro 11125a18fff2SAl Viro mutex_lock(&dir->i_mutex); 11135a18fff2SAl Viro dentry = d_lookup(parent, name); 11145a18fff2SAl Viro if (likely(!dentry)) { 11155a18fff2SAl Viro dentry = d_alloc_and_lookup(parent, name, nd); 11165a18fff2SAl Viro if (IS_ERR(dentry)) { 11175a18fff2SAl Viro mutex_unlock(&dir->i_mutex); 11185a18fff2SAl Viro return PTR_ERR(dentry); 11195a18fff2SAl Viro } 11205a18fff2SAl Viro /* known good */ 11215a18fff2SAl Viro need_reval = 0; 11225a18fff2SAl Viro status = 1; 112344396f4bSJosef Bacik } else if (unlikely(d_need_lookup(dentry))) { 112444396f4bSJosef Bacik dentry = d_inode_lookup(parent, dentry, nd); 112544396f4bSJosef Bacik if (IS_ERR(dentry)) { 112644396f4bSJosef Bacik mutex_unlock(&dir->i_mutex); 112744396f4bSJosef Bacik return PTR_ERR(dentry); 112844396f4bSJosef Bacik } 112944396f4bSJosef Bacik /* known good */ 113044396f4bSJosef Bacik need_reval = 0; 113144396f4bSJosef Bacik status = 1; 11325a18fff2SAl Viro } 11335a18fff2SAl Viro mutex_unlock(&dir->i_mutex); 11345a18fff2SAl Viro } 11355a18fff2SAl Viro if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval) 11365a18fff2SAl Viro status = d_revalidate(dentry, nd); 11375a18fff2SAl Viro if (unlikely(status <= 0)) { 11385a18fff2SAl Viro if (status < 0) { 11395a18fff2SAl Viro dput(dentry); 11405a18fff2SAl Viro return status; 11415a18fff2SAl Viro } 11425a18fff2SAl Viro if (!d_invalidate(dentry)) { 11435a18fff2SAl Viro dput(dentry); 11445a18fff2SAl Viro dentry = NULL; 11455a18fff2SAl Viro need_reval = 1; 11465a18fff2SAl Viro goto retry; 11475a18fff2SAl Viro } 11485a18fff2SAl Viro } 11495a18fff2SAl Viro 11501da177e4SLinus Torvalds path->mnt = mnt; 11511da177e4SLinus Torvalds path->dentry = dentry; 11529875cf80SDavid Howells err = follow_managed(path, nd->flags); 115389312214SIan Kent if (unlikely(err < 0)) { 115489312214SIan Kent path_put_conditional(path, nd); 11559875cf80SDavid Howells return err; 115689312214SIan Kent } 115731e6b01fSNick Piggin *inode = path->dentry->d_inode; 11581da177e4SLinus Torvalds return 0; 11591da177e4SLinus Torvalds } 11601da177e4SLinus Torvalds 116152094c8aSAl Viro static inline int may_lookup(struct nameidata *nd) 116252094c8aSAl Viro { 116352094c8aSAl Viro if (nd->flags & LOOKUP_RCU) { 11644ad5abb3SAl Viro int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK); 116552094c8aSAl Viro if (err != -ECHILD) 116652094c8aSAl Viro return err; 116719660af7SAl Viro if (unlazy_walk(nd, NULL)) 116852094c8aSAl Viro return -ECHILD; 116952094c8aSAl Viro } 11704ad5abb3SAl Viro return inode_permission(nd->inode, MAY_EXEC); 117152094c8aSAl Viro } 117252094c8aSAl Viro 11739856fa1bSAl Viro static inline int handle_dots(struct nameidata *nd, int type) 11749856fa1bSAl Viro { 11759856fa1bSAl Viro if (type == LAST_DOTDOT) { 11769856fa1bSAl Viro if (nd->flags & LOOKUP_RCU) { 11779856fa1bSAl Viro if (follow_dotdot_rcu(nd)) 11789856fa1bSAl Viro return -ECHILD; 11799856fa1bSAl Viro } else 11809856fa1bSAl Viro follow_dotdot(nd); 11819856fa1bSAl Viro } 11829856fa1bSAl Viro return 0; 11839856fa1bSAl Viro } 11849856fa1bSAl Viro 1185951361f9SAl Viro static void terminate_walk(struct nameidata *nd) 1186951361f9SAl Viro { 1187951361f9SAl Viro if (!(nd->flags & LOOKUP_RCU)) { 1188951361f9SAl Viro path_put(&nd->path); 1189951361f9SAl Viro } else { 1190951361f9SAl Viro nd->flags &= ~LOOKUP_RCU; 11915b6ca027SAl Viro if (!(nd->flags & LOOKUP_ROOT)) 1192951361f9SAl Viro nd->root.mnt = NULL; 1193951361f9SAl Viro rcu_read_unlock(); 1194951361f9SAl Viro br_read_unlock(vfsmount_lock); 1195951361f9SAl Viro } 1196951361f9SAl Viro } 1197951361f9SAl Viro 1198ce57dfc1SAl Viro static inline int walk_component(struct nameidata *nd, struct path *path, 1199ce57dfc1SAl Viro struct qstr *name, int type, int follow) 1200ce57dfc1SAl Viro { 1201ce57dfc1SAl Viro struct inode *inode; 1202ce57dfc1SAl Viro int err; 1203ce57dfc1SAl Viro /* 1204ce57dfc1SAl Viro * "." and ".." are special - ".." especially so because it has 1205ce57dfc1SAl Viro * to be able to know about the current root directory and 1206ce57dfc1SAl Viro * parent relationships. 1207ce57dfc1SAl Viro */ 1208ce57dfc1SAl Viro if (unlikely(type != LAST_NORM)) 1209ce57dfc1SAl Viro return handle_dots(nd, type); 1210ce57dfc1SAl Viro err = do_lookup(nd, name, path, &inode); 1211ce57dfc1SAl Viro if (unlikely(err)) { 1212ce57dfc1SAl Viro terminate_walk(nd); 1213ce57dfc1SAl Viro return err; 1214ce57dfc1SAl Viro } 1215ce57dfc1SAl Viro if (!inode) { 1216ce57dfc1SAl Viro path_to_nameidata(path, nd); 1217ce57dfc1SAl Viro terminate_walk(nd); 1218ce57dfc1SAl Viro return -ENOENT; 1219ce57dfc1SAl Viro } 1220ce57dfc1SAl Viro if (unlikely(inode->i_op->follow_link) && follow) { 122119660af7SAl Viro if (nd->flags & LOOKUP_RCU) { 122219660af7SAl Viro if (unlikely(unlazy_walk(nd, path->dentry))) { 122319660af7SAl Viro terminate_walk(nd); 1224ce57dfc1SAl Viro return -ECHILD; 122519660af7SAl Viro } 122619660af7SAl Viro } 1227ce57dfc1SAl Viro BUG_ON(inode != path->dentry->d_inode); 1228ce57dfc1SAl Viro return 1; 1229ce57dfc1SAl Viro } 1230ce57dfc1SAl Viro path_to_nameidata(path, nd); 1231ce57dfc1SAl Viro nd->inode = inode; 1232ce57dfc1SAl Viro return 0; 1233ce57dfc1SAl Viro } 1234ce57dfc1SAl Viro 12351da177e4SLinus Torvalds /* 1236b356379aSAl Viro * This limits recursive symlink follows to 8, while 1237b356379aSAl Viro * limiting consecutive symlinks to 40. 1238b356379aSAl Viro * 1239b356379aSAl Viro * Without that kind of total limit, nasty chains of consecutive 1240b356379aSAl Viro * symlinks can cause almost arbitrarily long lookups. 1241b356379aSAl Viro */ 1242b356379aSAl Viro static inline int nested_symlink(struct path *path, struct nameidata *nd) 1243b356379aSAl Viro { 1244b356379aSAl Viro int res; 1245b356379aSAl Viro 1246b356379aSAl Viro if (unlikely(current->link_count >= MAX_NESTED_LINKS)) { 1247b356379aSAl Viro path_put_conditional(path, nd); 1248b356379aSAl Viro path_put(&nd->path); 1249b356379aSAl Viro return -ELOOP; 1250b356379aSAl Viro } 12511a4022f8SErez Zadok BUG_ON(nd->depth >= MAX_NESTED_LINKS); 1252b356379aSAl Viro 1253b356379aSAl Viro nd->depth++; 1254b356379aSAl Viro current->link_count++; 1255b356379aSAl Viro 1256b356379aSAl Viro do { 1257b356379aSAl Viro struct path link = *path; 1258b356379aSAl Viro void *cookie; 1259574197e0SAl Viro 1260574197e0SAl Viro res = follow_link(&link, nd, &cookie); 1261b356379aSAl Viro if (!res) 1262b356379aSAl Viro res = walk_component(nd, path, &nd->last, 1263b356379aSAl Viro nd->last_type, LOOKUP_FOLLOW); 1264574197e0SAl Viro put_link(nd, &link, cookie); 1265b356379aSAl Viro } while (res > 0); 1266b356379aSAl Viro 1267b356379aSAl Viro current->link_count--; 1268b356379aSAl Viro nd->depth--; 1269b356379aSAl Viro return res; 1270b356379aSAl Viro } 1271b356379aSAl Viro 1272b356379aSAl Viro /* 12731da177e4SLinus Torvalds * Name resolution. 1274ea3834d9SPrasanna Meda * This is the basic name resolution function, turning a pathname into 1275ea3834d9SPrasanna Meda * the final dentry. We expect 'base' to be positive and a directory. 12761da177e4SLinus Torvalds * 1277ea3834d9SPrasanna Meda * Returns 0 and nd will have valid dentry and mnt on success. 1278ea3834d9SPrasanna Meda * Returns error and drops reference to input namei data on failure. 12791da177e4SLinus Torvalds */ 12806de88d72SAl Viro static int link_path_walk(const char *name, struct nameidata *nd) 12811da177e4SLinus Torvalds { 12821da177e4SLinus Torvalds struct path next; 12831da177e4SLinus Torvalds int err; 12841da177e4SLinus Torvalds 12851da177e4SLinus Torvalds while (*name=='/') 12861da177e4SLinus Torvalds name++; 12871da177e4SLinus Torvalds if (!*name) 1288086e183aSAl Viro return 0; 12891da177e4SLinus Torvalds 12901da177e4SLinus Torvalds /* At this point we know we have a real path component. */ 12911da177e4SLinus Torvalds for(;;) { 12921da177e4SLinus Torvalds unsigned long hash; 12931da177e4SLinus Torvalds struct qstr this; 12941da177e4SLinus Torvalds unsigned int c; 1295fe479a58SAl Viro int type; 12961da177e4SLinus Torvalds 129752094c8aSAl Viro err = may_lookup(nd); 12981da177e4SLinus Torvalds if (err) 12991da177e4SLinus Torvalds break; 13001da177e4SLinus Torvalds 13011da177e4SLinus Torvalds this.name = name; 13021da177e4SLinus Torvalds c = *(const unsigned char *)name; 13031da177e4SLinus Torvalds 13041da177e4SLinus Torvalds hash = init_name_hash(); 13051da177e4SLinus Torvalds do { 13061da177e4SLinus Torvalds name++; 13071da177e4SLinus Torvalds hash = partial_name_hash(c, hash); 13081da177e4SLinus Torvalds c = *(const unsigned char *)name; 13091da177e4SLinus Torvalds } while (c && (c != '/')); 13101da177e4SLinus Torvalds this.len = name - (const char *) this.name; 13111da177e4SLinus Torvalds this.hash = end_name_hash(hash); 13121da177e4SLinus Torvalds 1313fe479a58SAl Viro type = LAST_NORM; 1314fe479a58SAl Viro if (this.name[0] == '.') switch (this.len) { 1315fe479a58SAl Viro case 2: 131616c2cd71SAl Viro if (this.name[1] == '.') { 1317fe479a58SAl Viro type = LAST_DOTDOT; 131816c2cd71SAl Viro nd->flags |= LOOKUP_JUMPED; 131916c2cd71SAl Viro } 1320fe479a58SAl Viro break; 1321fe479a58SAl Viro case 1: 1322fe479a58SAl Viro type = LAST_DOT; 1323fe479a58SAl Viro } 13245a202bcdSAl Viro if (likely(type == LAST_NORM)) { 13255a202bcdSAl Viro struct dentry *parent = nd->path.dentry; 132616c2cd71SAl Viro nd->flags &= ~LOOKUP_JUMPED; 13275a202bcdSAl Viro if (unlikely(parent->d_flags & DCACHE_OP_HASH)) { 13285a202bcdSAl Viro err = parent->d_op->d_hash(parent, nd->inode, 13295a202bcdSAl Viro &this); 13305a202bcdSAl Viro if (err < 0) 13315a202bcdSAl Viro break; 13325a202bcdSAl Viro } 13335a202bcdSAl Viro } 1334fe479a58SAl Viro 13351da177e4SLinus Torvalds /* remove trailing slashes? */ 13361da177e4SLinus Torvalds if (!c) 13371da177e4SLinus Torvalds goto last_component; 13381da177e4SLinus Torvalds while (*++name == '/'); 13391da177e4SLinus Torvalds if (!*name) 1340b356379aSAl Viro goto last_component; 13411da177e4SLinus Torvalds 1342ce57dfc1SAl Viro err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW); 1343ce57dfc1SAl Viro if (err < 0) 1344ce57dfc1SAl Viro return err; 1345fe479a58SAl Viro 1346ce57dfc1SAl Viro if (err) { 1347b356379aSAl Viro err = nested_symlink(&next, nd); 13481da177e4SLinus Torvalds if (err) 1349a7472babSAl Viro return err; 135031e6b01fSNick Piggin } 13511da177e4SLinus Torvalds err = -ENOTDIR; 135231e6b01fSNick Piggin if (!nd->inode->i_op->lookup) 13531da177e4SLinus Torvalds break; 13541da177e4SLinus Torvalds continue; 13551da177e4SLinus Torvalds /* here ends the main loop */ 13561da177e4SLinus Torvalds 13571da177e4SLinus Torvalds last_component: 1358ce57dfc1SAl Viro nd->last = this; 1359ce57dfc1SAl Viro nd->last_type = type; 1360ce57dfc1SAl Viro return 0; 1361ce57dfc1SAl Viro } 1362951361f9SAl Viro terminate_walk(nd); 13631da177e4SLinus Torvalds return err; 13641da177e4SLinus Torvalds } 13651da177e4SLinus Torvalds 136670e9b357SAl Viro static int path_init(int dfd, const char *name, unsigned int flags, 136770e9b357SAl Viro struct nameidata *nd, struct file **fp) 136831e6b01fSNick Piggin { 136931e6b01fSNick Piggin int retval = 0; 137031e6b01fSNick Piggin int fput_needed; 137131e6b01fSNick Piggin struct file *file; 137231e6b01fSNick Piggin 137331e6b01fSNick Piggin nd->last_type = LAST_ROOT; /* if there are only slashes... */ 137416c2cd71SAl Viro nd->flags = flags | LOOKUP_JUMPED; 137531e6b01fSNick Piggin nd->depth = 0; 13765b6ca027SAl Viro if (flags & LOOKUP_ROOT) { 13775b6ca027SAl Viro struct inode *inode = nd->root.dentry->d_inode; 137873d049a4SAl Viro if (*name) { 13795b6ca027SAl Viro if (!inode->i_op->lookup) 13805b6ca027SAl Viro return -ENOTDIR; 13815b6ca027SAl Viro retval = inode_permission(inode, MAY_EXEC); 13825b6ca027SAl Viro if (retval) 13835b6ca027SAl Viro return retval; 138473d049a4SAl Viro } 13855b6ca027SAl Viro nd->path = nd->root; 13865b6ca027SAl Viro nd->inode = inode; 13875b6ca027SAl Viro if (flags & LOOKUP_RCU) { 13885b6ca027SAl Viro br_read_lock(vfsmount_lock); 13895b6ca027SAl Viro rcu_read_lock(); 13905b6ca027SAl Viro nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 13915b6ca027SAl Viro } else { 13925b6ca027SAl Viro path_get(&nd->path); 13935b6ca027SAl Viro } 13945b6ca027SAl Viro return 0; 13955b6ca027SAl Viro } 13965b6ca027SAl Viro 139731e6b01fSNick Piggin nd->root.mnt = NULL; 139831e6b01fSNick Piggin 139931e6b01fSNick Piggin if (*name=='/') { 1400e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 140131e6b01fSNick Piggin br_read_lock(vfsmount_lock); 140231e6b01fSNick Piggin rcu_read_lock(); 1403e41f7d4eSAl Viro set_root_rcu(nd); 1404e41f7d4eSAl Viro } else { 1405e41f7d4eSAl Viro set_root(nd); 1406e41f7d4eSAl Viro path_get(&nd->root); 1407e41f7d4eSAl Viro } 140831e6b01fSNick Piggin nd->path = nd->root; 140931e6b01fSNick Piggin } else if (dfd == AT_FDCWD) { 1410e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 141131e6b01fSNick Piggin struct fs_struct *fs = current->fs; 1412c28cc364SNick Piggin unsigned seq; 141331e6b01fSNick Piggin 141431e6b01fSNick Piggin br_read_lock(vfsmount_lock); 141531e6b01fSNick Piggin rcu_read_lock(); 141631e6b01fSNick Piggin 1417c28cc364SNick Piggin do { 1418c28cc364SNick Piggin seq = read_seqcount_begin(&fs->seq); 141931e6b01fSNick Piggin nd->path = fs->pwd; 1420c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 1421c28cc364SNick Piggin } while (read_seqcount_retry(&fs->seq, seq)); 1422e41f7d4eSAl Viro } else { 1423e41f7d4eSAl Viro get_fs_pwd(current->fs, &nd->path); 1424e41f7d4eSAl Viro } 142531e6b01fSNick Piggin } else { 142631e6b01fSNick Piggin struct dentry *dentry; 142731e6b01fSNick Piggin 14281abf0c71SAl Viro file = fget_raw_light(dfd, &fput_needed); 142931e6b01fSNick Piggin retval = -EBADF; 143031e6b01fSNick Piggin if (!file) 143131e6b01fSNick Piggin goto out_fail; 143231e6b01fSNick Piggin 143331e6b01fSNick Piggin dentry = file->f_path.dentry; 143431e6b01fSNick Piggin 1435f52e0c11SAl Viro if (*name) { 143631e6b01fSNick Piggin retval = -ENOTDIR; 143731e6b01fSNick Piggin if (!S_ISDIR(dentry->d_inode->i_mode)) 143831e6b01fSNick Piggin goto fput_fail; 143931e6b01fSNick Piggin 14404ad5abb3SAl Viro retval = inode_permission(dentry->d_inode, MAY_EXEC); 144131e6b01fSNick Piggin if (retval) 144231e6b01fSNick Piggin goto fput_fail; 1443f52e0c11SAl Viro } 144431e6b01fSNick Piggin 144531e6b01fSNick Piggin nd->path = file->f_path; 1446e41f7d4eSAl Viro if (flags & LOOKUP_RCU) { 144731e6b01fSNick Piggin if (fput_needed) 144870e9b357SAl Viro *fp = file; 1449c28cc364SNick Piggin nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); 145031e6b01fSNick Piggin br_read_lock(vfsmount_lock); 145131e6b01fSNick Piggin rcu_read_lock(); 14525590ff0dSUlrich Drepper } else { 14535dd784d0SJan Blunck path_get(&file->f_path); 14545590ff0dSUlrich Drepper fput_light(file, fput_needed); 14551da177e4SLinus Torvalds } 1456e41f7d4eSAl Viro } 1457e41f7d4eSAl Viro 145831e6b01fSNick Piggin nd->inode = nd->path.dentry->d_inode; 14599b4a9b14SAl Viro return 0; 14602dfdd266SJosef 'Jeff' Sipek 14619b4a9b14SAl Viro fput_fail: 14629b4a9b14SAl Viro fput_light(file, fput_needed); 14639b4a9b14SAl Viro out_fail: 14649b4a9b14SAl Viro return retval; 14659b4a9b14SAl Viro } 14669b4a9b14SAl Viro 1467bd92d7feSAl Viro static inline int lookup_last(struct nameidata *nd, struct path *path) 1468bd92d7feSAl Viro { 1469bd92d7feSAl Viro if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len]) 1470bd92d7feSAl Viro nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 1471bd92d7feSAl Viro 1472bd92d7feSAl Viro nd->flags &= ~LOOKUP_PARENT; 1473bd92d7feSAl Viro return walk_component(nd, path, &nd->last, nd->last_type, 1474bd92d7feSAl Viro nd->flags & LOOKUP_FOLLOW); 1475bd92d7feSAl Viro } 1476bd92d7feSAl Viro 14779b4a9b14SAl Viro /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */ 1478ee0827cdSAl Viro static int path_lookupat(int dfd, const char *name, 14799b4a9b14SAl Viro unsigned int flags, struct nameidata *nd) 14809b4a9b14SAl Viro { 148170e9b357SAl Viro struct file *base = NULL; 1482bd92d7feSAl Viro struct path path; 1483bd92d7feSAl Viro int err; 148431e6b01fSNick Piggin 148531e6b01fSNick Piggin /* 148631e6b01fSNick Piggin * Path walking is largely split up into 2 different synchronisation 148731e6b01fSNick Piggin * schemes, rcu-walk and ref-walk (explained in 148831e6b01fSNick Piggin * Documentation/filesystems/path-lookup.txt). These share much of the 148931e6b01fSNick Piggin * path walk code, but some things particularly setup, cleanup, and 149031e6b01fSNick Piggin * following mounts are sufficiently divergent that functions are 149131e6b01fSNick Piggin * duplicated. Typically there is a function foo(), and its RCU 149231e6b01fSNick Piggin * analogue, foo_rcu(). 149331e6b01fSNick Piggin * 149431e6b01fSNick Piggin * -ECHILD is the error number of choice (just to avoid clashes) that 149531e6b01fSNick Piggin * is returned if some aspect of an rcu-walk fails. Such an error must 149631e6b01fSNick Piggin * be handled by restarting a traditional ref-walk (which will always 149731e6b01fSNick Piggin * be able to complete). 149831e6b01fSNick Piggin */ 1499bd92d7feSAl Viro err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base); 1500ee0827cdSAl Viro 1501bd92d7feSAl Viro if (unlikely(err)) 1502bd92d7feSAl Viro return err; 1503ee0827cdSAl Viro 1504ee0827cdSAl Viro current->total_link_count = 0; 1505bd92d7feSAl Viro err = link_path_walk(name, nd); 1506bd92d7feSAl Viro 1507bd92d7feSAl Viro if (!err && !(flags & LOOKUP_PARENT)) { 1508bd92d7feSAl Viro err = lookup_last(nd, &path); 1509bd92d7feSAl Viro while (err > 0) { 1510bd92d7feSAl Viro void *cookie; 1511bd92d7feSAl Viro struct path link = path; 1512bd92d7feSAl Viro nd->flags |= LOOKUP_PARENT; 1513574197e0SAl Viro err = follow_link(&link, nd, &cookie); 1514bd92d7feSAl Viro if (!err) 1515bd92d7feSAl Viro err = lookup_last(nd, &path); 1516574197e0SAl Viro put_link(nd, &link, cookie); 1517bd92d7feSAl Viro } 1518bd92d7feSAl Viro } 1519ee0827cdSAl Viro 15209f1fafeeSAl Viro if (!err) 15219f1fafeeSAl Viro err = complete_walk(nd); 1522bd92d7feSAl Viro 1523bd92d7feSAl Viro if (!err && nd->flags & LOOKUP_DIRECTORY) { 1524bd92d7feSAl Viro if (!nd->inode->i_op->lookup) { 1525bd92d7feSAl Viro path_put(&nd->path); 1526bd23a539SAl Viro err = -ENOTDIR; 1527bd92d7feSAl Viro } 1528bd92d7feSAl Viro } 152916c2cd71SAl Viro 153070e9b357SAl Viro if (base) 153170e9b357SAl Viro fput(base); 1532ee0827cdSAl Viro 15335b6ca027SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) { 153431e6b01fSNick Piggin path_put(&nd->root); 153531e6b01fSNick Piggin nd->root.mnt = NULL; 153631e6b01fSNick Piggin } 1537bd92d7feSAl Viro return err; 153831e6b01fSNick Piggin } 153931e6b01fSNick Piggin 1540ee0827cdSAl Viro static int do_path_lookup(int dfd, const char *name, 1541ee0827cdSAl Viro unsigned int flags, struct nameidata *nd) 1542ee0827cdSAl Viro { 1543ee0827cdSAl Viro int retval = path_lookupat(dfd, name, flags | LOOKUP_RCU, nd); 1544ee0827cdSAl Viro if (unlikely(retval == -ECHILD)) 1545ee0827cdSAl Viro retval = path_lookupat(dfd, name, flags, nd); 1546ee0827cdSAl Viro if (unlikely(retval == -ESTALE)) 1547ee0827cdSAl Viro retval = path_lookupat(dfd, name, flags | LOOKUP_REVAL, nd); 1548ee0827cdSAl Viro 154931e6b01fSNick Piggin if (likely(!retval)) { 155031e6b01fSNick Piggin if (unlikely(!audit_dummy_context())) { 155131e6b01fSNick Piggin if (nd->path.dentry && nd->inode) 155231e6b01fSNick Piggin audit_inode(name, nd->path.dentry); 155331e6b01fSNick Piggin } 155431e6b01fSNick Piggin } 1555170aa3d0SUlrich Drepper return retval; 15561da177e4SLinus Torvalds } 15571da177e4SLinus Torvalds 1558c9c6cac0SAl Viro int kern_path_parent(const char *name, struct nameidata *nd) 15595590ff0dSUlrich Drepper { 1560c9c6cac0SAl Viro return do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, nd); 15615590ff0dSUlrich Drepper } 15625590ff0dSUlrich Drepper 1563d1811465SAl Viro int kern_path(const char *name, unsigned int flags, struct path *path) 1564d1811465SAl Viro { 1565d1811465SAl Viro struct nameidata nd; 1566d1811465SAl Viro int res = do_path_lookup(AT_FDCWD, name, flags, &nd); 1567d1811465SAl Viro if (!res) 1568d1811465SAl Viro *path = nd.path; 1569d1811465SAl Viro return res; 1570d1811465SAl Viro } 1571d1811465SAl Viro 157216f18200SJosef 'Jeff' Sipek /** 157316f18200SJosef 'Jeff' Sipek * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair 157416f18200SJosef 'Jeff' Sipek * @dentry: pointer to dentry of the base directory 157516f18200SJosef 'Jeff' Sipek * @mnt: pointer to vfs mount of the base directory 157616f18200SJosef 'Jeff' Sipek * @name: pointer to file name 157716f18200SJosef 'Jeff' Sipek * @flags: lookup flags 1578e0a01249SAl Viro * @path: pointer to struct path to fill 157916f18200SJosef 'Jeff' Sipek */ 158016f18200SJosef 'Jeff' Sipek int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt, 158116f18200SJosef 'Jeff' Sipek const char *name, unsigned int flags, 1582e0a01249SAl Viro struct path *path) 158316f18200SJosef 'Jeff' Sipek { 1584e0a01249SAl Viro struct nameidata nd; 1585e0a01249SAl Viro int err; 1586e0a01249SAl Viro nd.root.dentry = dentry; 1587e0a01249SAl Viro nd.root.mnt = mnt; 1588e0a01249SAl Viro BUG_ON(flags & LOOKUP_PARENT); 15895b6ca027SAl Viro /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */ 1590e0a01249SAl Viro err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd); 1591e0a01249SAl Viro if (!err) 1592e0a01249SAl Viro *path = nd.path; 1593e0a01249SAl Viro return err; 159416f18200SJosef 'Jeff' Sipek } 159516f18200SJosef 'Jeff' Sipek 1596eead1911SChristoph Hellwig static struct dentry *__lookup_hash(struct qstr *name, 1597eead1911SChristoph Hellwig struct dentry *base, struct nameidata *nd) 15981da177e4SLinus Torvalds { 159981fca444SChristoph Hellwig struct inode *inode = base->d_inode; 16001da177e4SLinus Torvalds struct dentry *dentry; 16011da177e4SLinus Torvalds int err; 16021da177e4SLinus Torvalds 16034ad5abb3SAl Viro err = inode_permission(inode, MAY_EXEC); 160481fca444SChristoph Hellwig if (err) 160581fca444SChristoph Hellwig return ERR_PTR(err); 16061da177e4SLinus Torvalds 16071da177e4SLinus Torvalds /* 1608b04f784eSNick Piggin * Don't bother with __d_lookup: callers are for creat as 1609b04f784eSNick Piggin * well as unlink, so a lot of the time it would cost 1610b04f784eSNick Piggin * a double lookup. 16116e6b1bd1SAl Viro */ 16126e6b1bd1SAl Viro dentry = d_lookup(base, name); 16136e6b1bd1SAl Viro 161444396f4bSJosef Bacik if (dentry && d_need_lookup(dentry)) { 161544396f4bSJosef Bacik /* 161644396f4bSJosef Bacik * __lookup_hash is called with the parent dir's i_mutex already 161744396f4bSJosef Bacik * held, so we are good to go here. 161844396f4bSJosef Bacik */ 161944396f4bSJosef Bacik dentry = d_inode_lookup(base, dentry, nd); 162044396f4bSJosef Bacik if (IS_ERR(dentry)) 162144396f4bSJosef Bacik return dentry; 162244396f4bSJosef Bacik } 162344396f4bSJosef Bacik 1624d2d9e9fbSAl Viro if (dentry && (dentry->d_flags & DCACHE_OP_REVALIDATE)) { 1625d2d9e9fbSAl Viro int status = d_revalidate(dentry, nd); 1626d2d9e9fbSAl Viro if (unlikely(status <= 0)) { 1627d2d9e9fbSAl Viro /* 1628d2d9e9fbSAl Viro * The dentry failed validation. 1629d2d9e9fbSAl Viro * If d_revalidate returned 0 attempt to invalidate 1630d2d9e9fbSAl Viro * the dentry otherwise d_revalidate is asking us 1631d2d9e9fbSAl Viro * to return a fail status. 1632d2d9e9fbSAl Viro */ 1633d2d9e9fbSAl Viro if (status < 0) { 1634d2d9e9fbSAl Viro dput(dentry); 1635d2d9e9fbSAl Viro return ERR_PTR(status); 1636d2d9e9fbSAl Viro } else if (!d_invalidate(dentry)) { 1637d2d9e9fbSAl Viro dput(dentry); 1638d2d9e9fbSAl Viro dentry = NULL; 1639d2d9e9fbSAl Viro } 1640d2d9e9fbSAl Viro } 1641d2d9e9fbSAl Viro } 16426e6b1bd1SAl Viro 16431da177e4SLinus Torvalds if (!dentry) 1644baa03890SNick Piggin dentry = d_alloc_and_lookup(base, name, nd); 16455a202bcdSAl Viro 16461da177e4SLinus Torvalds return dentry; 16471da177e4SLinus Torvalds } 16481da177e4SLinus Torvalds 1649057f6c01SJames Morris /* 1650057f6c01SJames Morris * Restricted form of lookup. Doesn't follow links, single-component only, 1651057f6c01SJames Morris * needs parent already locked. Doesn't follow mounts. 1652057f6c01SJames Morris * SMP-safe. 1653057f6c01SJames Morris */ 1654a244e169SAdrian Bunk static struct dentry *lookup_hash(struct nameidata *nd) 16551da177e4SLinus Torvalds { 16564ac91378SJan Blunck return __lookup_hash(&nd->last, nd->path.dentry, nd); 16571da177e4SLinus Torvalds } 16581da177e4SLinus Torvalds 1659eead1911SChristoph Hellwig /** 1660a6b91919SRandy Dunlap * lookup_one_len - filesystem helper to lookup single pathname component 1661eead1911SChristoph Hellwig * @name: pathname component to lookup 1662eead1911SChristoph Hellwig * @base: base directory to lookup from 1663eead1911SChristoph Hellwig * @len: maximum length @len should be interpreted to 1664eead1911SChristoph Hellwig * 1665a6b91919SRandy Dunlap * Note that this routine is purely a helper for filesystem usage and should 1666a6b91919SRandy Dunlap * not be called by generic code. Also note that by using this function the 1667eead1911SChristoph Hellwig * nameidata argument is passed to the filesystem methods and a filesystem 1668eead1911SChristoph Hellwig * using this helper needs to be prepared for that. 1669eead1911SChristoph Hellwig */ 1670057f6c01SJames Morris struct dentry *lookup_one_len(const char *name, struct dentry *base, int len) 1671057f6c01SJames Morris { 1672057f6c01SJames Morris struct qstr this; 16736a96ba54SAl Viro unsigned long hash; 16746a96ba54SAl Viro unsigned int c; 1675057f6c01SJames Morris 16762f9092e1SDavid Woodhouse WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex)); 16772f9092e1SDavid Woodhouse 16786a96ba54SAl Viro this.name = name; 16796a96ba54SAl Viro this.len = len; 16806a96ba54SAl Viro if (!len) 16816a96ba54SAl Viro return ERR_PTR(-EACCES); 16826a96ba54SAl Viro 16836a96ba54SAl Viro hash = init_name_hash(); 16846a96ba54SAl Viro while (len--) { 16856a96ba54SAl Viro c = *(const unsigned char *)name++; 16866a96ba54SAl Viro if (c == '/' || c == '\0') 16876a96ba54SAl Viro return ERR_PTR(-EACCES); 16886a96ba54SAl Viro hash = partial_name_hash(c, hash); 16896a96ba54SAl Viro } 16906a96ba54SAl Viro this.hash = end_name_hash(hash); 16915a202bcdSAl Viro /* 16925a202bcdSAl Viro * See if the low-level filesystem might want 16935a202bcdSAl Viro * to use its own hash.. 16945a202bcdSAl Viro */ 16955a202bcdSAl Viro if (base->d_flags & DCACHE_OP_HASH) { 16965a202bcdSAl Viro int err = base->d_op->d_hash(base, base->d_inode, &this); 16975a202bcdSAl Viro if (err < 0) 16985a202bcdSAl Viro return ERR_PTR(err); 16995a202bcdSAl Viro } 1700eead1911SChristoph Hellwig 170149705b77SChristoph Hellwig return __lookup_hash(&this, base, NULL); 1702057f6c01SJames Morris } 1703057f6c01SJames Morris 17042d8f3038SAl Viro int user_path_at(int dfd, const char __user *name, unsigned flags, 17052d8f3038SAl Viro struct path *path) 17061da177e4SLinus Torvalds { 17072d8f3038SAl Viro struct nameidata nd; 1708f52e0c11SAl Viro char *tmp = getname_flags(name, flags); 17091da177e4SLinus Torvalds int err = PTR_ERR(tmp); 17101da177e4SLinus Torvalds if (!IS_ERR(tmp)) { 17112d8f3038SAl Viro 17122d8f3038SAl Viro BUG_ON(flags & LOOKUP_PARENT); 17132d8f3038SAl Viro 17142d8f3038SAl Viro err = do_path_lookup(dfd, tmp, flags, &nd); 17151da177e4SLinus Torvalds putname(tmp); 17162d8f3038SAl Viro if (!err) 17172d8f3038SAl Viro *path = nd.path; 17181da177e4SLinus Torvalds } 17191da177e4SLinus Torvalds return err; 17201da177e4SLinus Torvalds } 17211da177e4SLinus Torvalds 17222ad94ae6SAl Viro static int user_path_parent(int dfd, const char __user *path, 17232ad94ae6SAl Viro struct nameidata *nd, char **name) 17242ad94ae6SAl Viro { 17252ad94ae6SAl Viro char *s = getname(path); 17262ad94ae6SAl Viro int error; 17272ad94ae6SAl Viro 17282ad94ae6SAl Viro if (IS_ERR(s)) 17292ad94ae6SAl Viro return PTR_ERR(s); 17302ad94ae6SAl Viro 17312ad94ae6SAl Viro error = do_path_lookup(dfd, s, LOOKUP_PARENT, nd); 17322ad94ae6SAl Viro if (error) 17332ad94ae6SAl Viro putname(s); 17342ad94ae6SAl Viro else 17352ad94ae6SAl Viro *name = s; 17362ad94ae6SAl Viro 17372ad94ae6SAl Viro return error; 17382ad94ae6SAl Viro } 17392ad94ae6SAl Viro 17401da177e4SLinus Torvalds /* 17411da177e4SLinus Torvalds * It's inline, so penalty for filesystems that don't use sticky bit is 17421da177e4SLinus Torvalds * minimal. 17431da177e4SLinus Torvalds */ 17441da177e4SLinus Torvalds static inline int check_sticky(struct inode *dir, struct inode *inode) 17451da177e4SLinus Torvalds { 1746da9592edSDavid Howells uid_t fsuid = current_fsuid(); 1747da9592edSDavid Howells 17481da177e4SLinus Torvalds if (!(dir->i_mode & S_ISVTX)) 17491da177e4SLinus Torvalds return 0; 1750e795b717SSerge E. Hallyn if (current_user_ns() != inode_userns(inode)) 1751e795b717SSerge E. Hallyn goto other_userns; 1752da9592edSDavid Howells if (inode->i_uid == fsuid) 17531da177e4SLinus Torvalds return 0; 1754da9592edSDavid Howells if (dir->i_uid == fsuid) 17551da177e4SLinus Torvalds return 0; 1756e795b717SSerge E. Hallyn 1757e795b717SSerge E. Hallyn other_userns: 1758e795b717SSerge E. Hallyn return !ns_capable(inode_userns(inode), CAP_FOWNER); 17591da177e4SLinus Torvalds } 17601da177e4SLinus Torvalds 17611da177e4SLinus Torvalds /* 17621da177e4SLinus Torvalds * Check whether we can remove a link victim from directory dir, check 17631da177e4SLinus Torvalds * whether the type of victim is right. 17641da177e4SLinus Torvalds * 1. We can't do it if dir is read-only (done in permission()) 17651da177e4SLinus Torvalds * 2. We should have write and exec permissions on dir 17661da177e4SLinus Torvalds * 3. We can't remove anything from append-only dir 17671da177e4SLinus Torvalds * 4. We can't do anything with immutable dir (done in permission()) 17681da177e4SLinus Torvalds * 5. If the sticky bit on dir is set we should either 17691da177e4SLinus Torvalds * a. be owner of dir, or 17701da177e4SLinus Torvalds * b. be owner of victim, or 17711da177e4SLinus Torvalds * c. have CAP_FOWNER capability 17721da177e4SLinus Torvalds * 6. If the victim is append-only or immutable we can't do antyhing with 17731da177e4SLinus Torvalds * links pointing to it. 17741da177e4SLinus Torvalds * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. 17751da177e4SLinus Torvalds * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. 17761da177e4SLinus Torvalds * 9. We can't remove a root or mountpoint. 17771da177e4SLinus Torvalds * 10. We don't allow removal of NFS sillyrenamed files; it's handled by 17781da177e4SLinus Torvalds * nfs_async_unlink(). 17791da177e4SLinus Torvalds */ 1780858119e1SArjan van de Ven static int may_delete(struct inode *dir,struct dentry *victim,int isdir) 17811da177e4SLinus Torvalds { 17821da177e4SLinus Torvalds int error; 17831da177e4SLinus Torvalds 17841da177e4SLinus Torvalds if (!victim->d_inode) 17851da177e4SLinus Torvalds return -ENOENT; 17861da177e4SLinus Torvalds 17871da177e4SLinus Torvalds BUG_ON(victim->d_parent->d_inode != dir); 1788cccc6bbaSAl Viro audit_inode_child(victim, dir); 17891da177e4SLinus Torvalds 1790f419a2e3SAl Viro error = inode_permission(dir, MAY_WRITE | MAY_EXEC); 17911da177e4SLinus Torvalds if (error) 17921da177e4SLinus Torvalds return error; 17931da177e4SLinus Torvalds if (IS_APPEND(dir)) 17941da177e4SLinus Torvalds return -EPERM; 17951da177e4SLinus Torvalds if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)|| 1796f9454548SHugh Dickins IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode)) 17971da177e4SLinus Torvalds return -EPERM; 17981da177e4SLinus Torvalds if (isdir) { 17991da177e4SLinus Torvalds if (!S_ISDIR(victim->d_inode->i_mode)) 18001da177e4SLinus Torvalds return -ENOTDIR; 18011da177e4SLinus Torvalds if (IS_ROOT(victim)) 18021da177e4SLinus Torvalds return -EBUSY; 18031da177e4SLinus Torvalds } else if (S_ISDIR(victim->d_inode->i_mode)) 18041da177e4SLinus Torvalds return -EISDIR; 18051da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 18061da177e4SLinus Torvalds return -ENOENT; 18071da177e4SLinus Torvalds if (victim->d_flags & DCACHE_NFSFS_RENAMED) 18081da177e4SLinus Torvalds return -EBUSY; 18091da177e4SLinus Torvalds return 0; 18101da177e4SLinus Torvalds } 18111da177e4SLinus Torvalds 18121da177e4SLinus Torvalds /* Check whether we can create an object with dentry child in directory 18131da177e4SLinus Torvalds * dir. 18141da177e4SLinus Torvalds * 1. We can't do it if child already exists (open has special treatment for 18151da177e4SLinus Torvalds * this case, but since we are inlined it's OK) 18161da177e4SLinus Torvalds * 2. We can't do it if dir is read-only (done in permission()) 18171da177e4SLinus Torvalds * 3. We should have write and exec permissions on dir 18181da177e4SLinus Torvalds * 4. We can't do it if dir is immutable (done in permission()) 18191da177e4SLinus Torvalds */ 1820a95164d9SMiklos Szeredi static inline int may_create(struct inode *dir, struct dentry *child) 18211da177e4SLinus Torvalds { 18221da177e4SLinus Torvalds if (child->d_inode) 18231da177e4SLinus Torvalds return -EEXIST; 18241da177e4SLinus Torvalds if (IS_DEADDIR(dir)) 18251da177e4SLinus Torvalds return -ENOENT; 1826f419a2e3SAl Viro return inode_permission(dir, MAY_WRITE | MAY_EXEC); 18271da177e4SLinus Torvalds } 18281da177e4SLinus Torvalds 18291da177e4SLinus Torvalds /* 18301da177e4SLinus Torvalds * p1 and p2 should be directories on the same fs. 18311da177e4SLinus Torvalds */ 18321da177e4SLinus Torvalds struct dentry *lock_rename(struct dentry *p1, struct dentry *p2) 18331da177e4SLinus Torvalds { 18341da177e4SLinus Torvalds struct dentry *p; 18351da177e4SLinus Torvalds 18361da177e4SLinus Torvalds if (p1 == p2) { 1837f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 18381da177e4SLinus Torvalds return NULL; 18391da177e4SLinus Torvalds } 18401da177e4SLinus Torvalds 1841a11f3a05SArjan van de Ven mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 18421da177e4SLinus Torvalds 1843e2761a11SOGAWA Hirofumi p = d_ancestor(p2, p1); 1844e2761a11SOGAWA Hirofumi if (p) { 1845f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT); 1846f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD); 18471da177e4SLinus Torvalds return p; 18481da177e4SLinus Torvalds } 18491da177e4SLinus Torvalds 1850e2761a11SOGAWA Hirofumi p = d_ancestor(p1, p2); 1851e2761a11SOGAWA Hirofumi if (p) { 1852f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 1853f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 18541da177e4SLinus Torvalds return p; 18551da177e4SLinus Torvalds } 18561da177e4SLinus Torvalds 1857f2eace23SIngo Molnar mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); 1858f2eace23SIngo Molnar mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); 18591da177e4SLinus Torvalds return NULL; 18601da177e4SLinus Torvalds } 18611da177e4SLinus Torvalds 18621da177e4SLinus Torvalds void unlock_rename(struct dentry *p1, struct dentry *p2) 18631da177e4SLinus Torvalds { 18641b1dcc1bSJes Sorensen mutex_unlock(&p1->d_inode->i_mutex); 18651da177e4SLinus Torvalds if (p1 != p2) { 18661b1dcc1bSJes Sorensen mutex_unlock(&p2->d_inode->i_mutex); 1867a11f3a05SArjan van de Ven mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex); 18681da177e4SLinus Torvalds } 18691da177e4SLinus Torvalds } 18701da177e4SLinus Torvalds 18711da177e4SLinus Torvalds int vfs_create(struct inode *dir, struct dentry *dentry, int mode, 18721da177e4SLinus Torvalds struct nameidata *nd) 18731da177e4SLinus Torvalds { 1874a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 18751da177e4SLinus Torvalds 18761da177e4SLinus Torvalds if (error) 18771da177e4SLinus Torvalds return error; 18781da177e4SLinus Torvalds 1879acfa4380SAl Viro if (!dir->i_op->create) 18801da177e4SLinus Torvalds return -EACCES; /* shouldn't it be ENOSYS? */ 18811da177e4SLinus Torvalds mode &= S_IALLUGO; 18821da177e4SLinus Torvalds mode |= S_IFREG; 18831da177e4SLinus Torvalds error = security_inode_create(dir, dentry, mode); 18841da177e4SLinus Torvalds if (error) 18851da177e4SLinus Torvalds return error; 18861da177e4SLinus Torvalds error = dir->i_op->create(dir, dentry, mode, nd); 1887a74574aaSStephen Smalley if (!error) 1888f38aa942SAmy Griffis fsnotify_create(dir, dentry); 18891da177e4SLinus Torvalds return error; 18901da177e4SLinus Torvalds } 18911da177e4SLinus Torvalds 189273d049a4SAl Viro static int may_open(struct path *path, int acc_mode, int flag) 18931da177e4SLinus Torvalds { 18943fb64190SChristoph Hellwig struct dentry *dentry = path->dentry; 18951da177e4SLinus Torvalds struct inode *inode = dentry->d_inode; 18961da177e4SLinus Torvalds int error; 18971da177e4SLinus Torvalds 1898bcda7652SAl Viro /* O_PATH? */ 1899bcda7652SAl Viro if (!acc_mode) 1900bcda7652SAl Viro return 0; 1901bcda7652SAl Viro 19021da177e4SLinus Torvalds if (!inode) 19031da177e4SLinus Torvalds return -ENOENT; 19041da177e4SLinus Torvalds 1905c8fe8f30SChristoph Hellwig switch (inode->i_mode & S_IFMT) { 1906c8fe8f30SChristoph Hellwig case S_IFLNK: 19071da177e4SLinus Torvalds return -ELOOP; 1908c8fe8f30SChristoph Hellwig case S_IFDIR: 1909c8fe8f30SChristoph Hellwig if (acc_mode & MAY_WRITE) 19101da177e4SLinus Torvalds return -EISDIR; 1911c8fe8f30SChristoph Hellwig break; 1912c8fe8f30SChristoph Hellwig case S_IFBLK: 1913c8fe8f30SChristoph Hellwig case S_IFCHR: 19143fb64190SChristoph Hellwig if (path->mnt->mnt_flags & MNT_NODEV) 19151da177e4SLinus Torvalds return -EACCES; 1916c8fe8f30SChristoph Hellwig /*FALLTHRU*/ 1917c8fe8f30SChristoph Hellwig case S_IFIFO: 1918c8fe8f30SChristoph Hellwig case S_IFSOCK: 19191da177e4SLinus Torvalds flag &= ~O_TRUNC; 1920c8fe8f30SChristoph Hellwig break; 19214a3fd211SDave Hansen } 1922b41572e9SDave Hansen 19233fb64190SChristoph Hellwig error = inode_permission(inode, acc_mode); 1924b41572e9SDave Hansen if (error) 1925b41572e9SDave Hansen return error; 19266146f0d5SMimi Zohar 19271da177e4SLinus Torvalds /* 19281da177e4SLinus Torvalds * An append-only file must be opened in append mode for writing. 19291da177e4SLinus Torvalds */ 19301da177e4SLinus Torvalds if (IS_APPEND(inode)) { 19318737c930SAl Viro if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND)) 19327715b521SAl Viro return -EPERM; 19331da177e4SLinus Torvalds if (flag & O_TRUNC) 19347715b521SAl Viro return -EPERM; 19351da177e4SLinus Torvalds } 19361da177e4SLinus Torvalds 19371da177e4SLinus Torvalds /* O_NOATIME can only be set by the owner or superuser */ 19382e149670SSerge E. Hallyn if (flag & O_NOATIME && !inode_owner_or_capable(inode)) 19397715b521SAl Viro return -EPERM; 19401da177e4SLinus Torvalds 19411da177e4SLinus Torvalds /* 19421da177e4SLinus Torvalds * Ensure there are no outstanding leases on the file. 19431da177e4SLinus Torvalds */ 1944b65a9cfcSAl Viro return break_lease(inode, flag); 19457715b521SAl Viro } 19467715b521SAl Viro 1947e1181ee6SJeff Layton static int handle_truncate(struct file *filp) 19487715b521SAl Viro { 1949e1181ee6SJeff Layton struct path *path = &filp->f_path; 19507715b521SAl Viro struct inode *inode = path->dentry->d_inode; 19517715b521SAl Viro int error = get_write_access(inode); 19521da177e4SLinus Torvalds if (error) 19537715b521SAl Viro return error; 19541da177e4SLinus Torvalds /* 19551da177e4SLinus Torvalds * Refuse to truncate files with mandatory locks held on them. 19561da177e4SLinus Torvalds */ 19571da177e4SLinus Torvalds error = locks_verify_locked(inode); 1958be6d3e56SKentaro Takeda if (!error) 1959ea0d3ab2STetsuo Handa error = security_path_truncate(path); 19601da177e4SLinus Torvalds if (!error) { 19617715b521SAl Viro error = do_truncate(path->dentry, 0, 1962d139d7ffSMiklos Szeredi ATTR_MTIME|ATTR_CTIME|ATTR_OPEN, 1963e1181ee6SJeff Layton filp); 19641da177e4SLinus Torvalds } 19651da177e4SLinus Torvalds put_write_access(inode); 1966acd0c935SMimi Zohar return error; 19671da177e4SLinus Torvalds } 19681da177e4SLinus Torvalds 1969d57999e1SDave Hansen static inline int open_to_namei_flags(int flag) 1970d57999e1SDave Hansen { 19718a5e929dSAl Viro if ((flag & O_ACCMODE) == 3) 19728a5e929dSAl Viro flag--; 1973d57999e1SDave Hansen return flag; 1974d57999e1SDave Hansen } 1975d57999e1SDave Hansen 197631e6b01fSNick Piggin /* 1977fe2d35ffSAl Viro * Handle the last step of open() 197831e6b01fSNick Piggin */ 1979fb1cc555SAl Viro static struct file *do_last(struct nameidata *nd, struct path *path, 1980c3e380b0SAl Viro const struct open_flags *op, const char *pathname) 1981fb1cc555SAl Viro { 1982a1e28038SAl Viro struct dentry *dir = nd->path.dentry; 19836c0d46c4SAl Viro struct dentry *dentry; 1984ca344a89SAl Viro int open_flag = op->open_flag; 19856c0d46c4SAl Viro int will_truncate = open_flag & O_TRUNC; 1986ca344a89SAl Viro int want_write = 0; 1987bcda7652SAl Viro int acc_mode = op->acc_mode; 1988fb1cc555SAl Viro struct file *filp; 198916c2cd71SAl Viro int error; 1990fb1cc555SAl Viro 1991c3e380b0SAl Viro nd->flags &= ~LOOKUP_PARENT; 1992c3e380b0SAl Viro nd->flags |= op->intent; 1993c3e380b0SAl Viro 19941f36f774SAl Viro switch (nd->last_type) { 19951f36f774SAl Viro case LAST_DOTDOT: 1996176306f5SNeil Brown case LAST_DOT: 1997fe2d35ffSAl Viro error = handle_dots(nd, nd->last_type); 1998fe2d35ffSAl Viro if (error) 1999fe2d35ffSAl Viro return ERR_PTR(error); 20001f36f774SAl Viro /* fallthrough */ 20011f36f774SAl Viro case LAST_ROOT: 20029f1fafeeSAl Viro error = complete_walk(nd); 200316c2cd71SAl Viro if (error) 20049f1fafeeSAl Viro return ERR_PTR(error); 2005fe2d35ffSAl Viro audit_inode(pathname, nd->path.dentry); 2006ca344a89SAl Viro if (open_flag & O_CREAT) { 200716c2cd71SAl Viro error = -EISDIR; 20081f36f774SAl Viro goto exit; 2009fe2d35ffSAl Viro } 2010fe2d35ffSAl Viro goto ok; 20111f36f774SAl Viro case LAST_BIND: 20129f1fafeeSAl Viro error = complete_walk(nd); 201316c2cd71SAl Viro if (error) 20149f1fafeeSAl Viro return ERR_PTR(error); 20151f36f774SAl Viro audit_inode(pathname, dir); 20161f36f774SAl Viro goto ok; 20171f36f774SAl Viro } 2018a2c36b45SAl Viro 2019ca344a89SAl Viro if (!(open_flag & O_CREAT)) { 2020bcda7652SAl Viro int symlink_ok = 0; 2021fe2d35ffSAl Viro if (nd->last.name[nd->last.len]) 2022fe2d35ffSAl Viro nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; 2023bcda7652SAl Viro if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW)) 2024bcda7652SAl Viro symlink_ok = 1; 2025fe2d35ffSAl Viro /* we _can_ be in RCU mode here */ 2026ce57dfc1SAl Viro error = walk_component(nd, path, &nd->last, LAST_NORM, 2027ce57dfc1SAl Viro !symlink_ok); 2028ce57dfc1SAl Viro if (error < 0) 2029fe2d35ffSAl Viro return ERR_PTR(error); 2030ce57dfc1SAl Viro if (error) /* symlink */ 2031fe2d35ffSAl Viro return NULL; 2032fe2d35ffSAl Viro /* sayonara */ 20339f1fafeeSAl Viro error = complete_walk(nd); 20349f1fafeeSAl Viro if (error) 2035fe2d35ffSAl Viro return ERR_PTR(-ECHILD); 2036fe2d35ffSAl Viro 2037fe2d35ffSAl Viro error = -ENOTDIR; 2038fe2d35ffSAl Viro if (nd->flags & LOOKUP_DIRECTORY) { 2039ce57dfc1SAl Viro if (!nd->inode->i_op->lookup) 2040fe2d35ffSAl Viro goto exit; 2041fe2d35ffSAl Viro } 2042fe2d35ffSAl Viro audit_inode(pathname, nd->path.dentry); 2043fe2d35ffSAl Viro goto ok; 2044fe2d35ffSAl Viro } 2045fe2d35ffSAl Viro 2046fe2d35ffSAl Viro /* create side of things */ 20479f1fafeeSAl Viro error = complete_walk(nd); 20489f1fafeeSAl Viro if (error) 20499f1fafeeSAl Viro return ERR_PTR(error); 2050fe2d35ffSAl Viro 2051fe2d35ffSAl Viro audit_inode(pathname, dir); 205216c2cd71SAl Viro error = -EISDIR; 20531f36f774SAl Viro /* trailing slashes? */ 205431e6b01fSNick Piggin if (nd->last.name[nd->last.len]) 20551f36f774SAl Viro goto exit; 20561f36f774SAl Viro 2057a1e28038SAl Viro mutex_lock(&dir->d_inode->i_mutex); 2058a1e28038SAl Viro 20596c0d46c4SAl Viro dentry = lookup_hash(nd); 20606c0d46c4SAl Viro error = PTR_ERR(dentry); 20616c0d46c4SAl Viro if (IS_ERR(dentry)) { 2062fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2063fb1cc555SAl Viro goto exit; 2064fb1cc555SAl Viro } 2065fb1cc555SAl Viro 20666c0d46c4SAl Viro path->dentry = dentry; 20676c0d46c4SAl Viro path->mnt = nd->path.mnt; 20686c0d46c4SAl Viro 2069fb1cc555SAl Viro /* Negative dentry, just create the file */ 20706c0d46c4SAl Viro if (!dentry->d_inode) { 20716c0d46c4SAl Viro int mode = op->mode; 20726c0d46c4SAl Viro if (!IS_POSIXACL(dir->d_inode)) 20736c0d46c4SAl Viro mode &= ~current_umask(); 2074fb1cc555SAl Viro /* 2075fb1cc555SAl Viro * This write is needed to ensure that a 20766c0d46c4SAl Viro * rw->ro transition does not occur between 2077fb1cc555SAl Viro * the time when the file is created and when 2078fb1cc555SAl Viro * a permanent write count is taken through 2079fb1cc555SAl Viro * the 'struct file' in nameidata_to_filp(). 2080fb1cc555SAl Viro */ 2081fb1cc555SAl Viro error = mnt_want_write(nd->path.mnt); 2082fb1cc555SAl Viro if (error) 2083fb1cc555SAl Viro goto exit_mutex_unlock; 2084ca344a89SAl Viro want_write = 1; 20859b44f1b3SAl Viro /* Don't check for write permission, don't truncate */ 2086ca344a89SAl Viro open_flag &= ~O_TRUNC; 20876c0d46c4SAl Viro will_truncate = 0; 2088bcda7652SAl Viro acc_mode = MAY_OPEN; 20896c0d46c4SAl Viro error = security_path_mknod(&nd->path, dentry, mode, 0); 20906c0d46c4SAl Viro if (error) 20916c0d46c4SAl Viro goto exit_mutex_unlock; 20926c0d46c4SAl Viro error = vfs_create(dir->d_inode, dentry, mode, nd); 20936c0d46c4SAl Viro if (error) 20946c0d46c4SAl Viro goto exit_mutex_unlock; 20956c0d46c4SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 20966c0d46c4SAl Viro dput(nd->path.dentry); 20976c0d46c4SAl Viro nd->path.dentry = dentry; 2098ca344a89SAl Viro goto common; 2099fb1cc555SAl Viro } 2100fb1cc555SAl Viro 2101fb1cc555SAl Viro /* 2102fb1cc555SAl Viro * It already exists. 2103fb1cc555SAl Viro */ 2104fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2105fb1cc555SAl Viro audit_inode(pathname, path->dentry); 2106fb1cc555SAl Viro 2107fb1cc555SAl Viro error = -EEXIST; 2108ca344a89SAl Viro if (open_flag & O_EXCL) 2109fb1cc555SAl Viro goto exit_dput; 2110fb1cc555SAl Viro 21119875cf80SDavid Howells error = follow_managed(path, nd->flags); 21129875cf80SDavid Howells if (error < 0) 2113fb1cc555SAl Viro goto exit_dput; 2114fb1cc555SAl Viro 2115fb1cc555SAl Viro error = -ENOENT; 2116fb1cc555SAl Viro if (!path->dentry->d_inode) 2117fb1cc555SAl Viro goto exit_dput; 21189e67f361SAl Viro 21199e67f361SAl Viro if (path->dentry->d_inode->i_op->follow_link) 2120fb1cc555SAl Viro return NULL; 2121fb1cc555SAl Viro 2122fb1cc555SAl Viro path_to_nameidata(path, nd); 212331e6b01fSNick Piggin nd->inode = path->dentry->d_inode; 2124fb1cc555SAl Viro error = -EISDIR; 212531e6b01fSNick Piggin if (S_ISDIR(nd->inode->i_mode)) 2126fb1cc555SAl Viro goto exit; 212767ee3ad2SAl Viro ok: 21286c0d46c4SAl Viro if (!S_ISREG(nd->inode->i_mode)) 21296c0d46c4SAl Viro will_truncate = 0; 21306c0d46c4SAl Viro 21310f9d1a10SAl Viro if (will_truncate) { 21320f9d1a10SAl Viro error = mnt_want_write(nd->path.mnt); 21330f9d1a10SAl Viro if (error) 21340f9d1a10SAl Viro goto exit; 2135ca344a89SAl Viro want_write = 1; 21360f9d1a10SAl Viro } 2137ca344a89SAl Viro common: 2138bcda7652SAl Viro error = may_open(&nd->path, acc_mode, open_flag); 2139ca344a89SAl Viro if (error) 21400f9d1a10SAl Viro goto exit; 21410f9d1a10SAl Viro filp = nameidata_to_filp(nd); 21420f9d1a10SAl Viro if (!IS_ERR(filp)) { 21430f9d1a10SAl Viro error = ima_file_check(filp, op->acc_mode); 21440f9d1a10SAl Viro if (error) { 21450f9d1a10SAl Viro fput(filp); 21460f9d1a10SAl Viro filp = ERR_PTR(error); 21470f9d1a10SAl Viro } 21480f9d1a10SAl Viro } 21490f9d1a10SAl Viro if (!IS_ERR(filp)) { 21500f9d1a10SAl Viro if (will_truncate) { 21510f9d1a10SAl Viro error = handle_truncate(filp); 21520f9d1a10SAl Viro if (error) { 21530f9d1a10SAl Viro fput(filp); 21540f9d1a10SAl Viro filp = ERR_PTR(error); 21550f9d1a10SAl Viro } 21560f9d1a10SAl Viro } 21570f9d1a10SAl Viro } 2158ca344a89SAl Viro out: 2159ca344a89SAl Viro if (want_write) 21600f9d1a10SAl Viro mnt_drop_write(nd->path.mnt); 21610f9d1a10SAl Viro path_put(&nd->path); 2162fb1cc555SAl Viro return filp; 2163fb1cc555SAl Viro 2164fb1cc555SAl Viro exit_mutex_unlock: 2165fb1cc555SAl Viro mutex_unlock(&dir->d_inode->i_mutex); 2166fb1cc555SAl Viro exit_dput: 2167fb1cc555SAl Viro path_put_conditional(path, nd); 2168fb1cc555SAl Viro exit: 2169ca344a89SAl Viro filp = ERR_PTR(error); 2170ca344a89SAl Viro goto out; 2171fb1cc555SAl Viro } 2172fb1cc555SAl Viro 217313aab428SAl Viro static struct file *path_openat(int dfd, const char *pathname, 217473d049a4SAl Viro struct nameidata *nd, const struct open_flags *op, int flags) 21751da177e4SLinus Torvalds { 2176fe2d35ffSAl Viro struct file *base = NULL; 21774a3fd211SDave Hansen struct file *filp; 21789850c056SAl Viro struct path path; 217913aab428SAl Viro int error; 218031e6b01fSNick Piggin 218131e6b01fSNick Piggin filp = get_empty_filp(); 218231e6b01fSNick Piggin if (!filp) 218331e6b01fSNick Piggin return ERR_PTR(-ENFILE); 218431e6b01fSNick Piggin 218547c805dcSAl Viro filp->f_flags = op->open_flag; 218673d049a4SAl Viro nd->intent.open.file = filp; 218773d049a4SAl Viro nd->intent.open.flags = open_to_namei_flags(op->open_flag); 218873d049a4SAl Viro nd->intent.open.create_mode = op->mode; 218931e6b01fSNick Piggin 219073d049a4SAl Viro error = path_init(dfd, pathname, flags | LOOKUP_PARENT, nd, &base); 219131e6b01fSNick Piggin if (unlikely(error)) 219213aab428SAl Viro goto out_filp; 219331e6b01fSNick Piggin 2194fe2d35ffSAl Viro current->total_link_count = 0; 219573d049a4SAl Viro error = link_path_walk(pathname, nd); 219631e6b01fSNick Piggin if (unlikely(error)) 219731e6b01fSNick Piggin goto out_filp; 21981da177e4SLinus Torvalds 219973d049a4SAl Viro filp = do_last(nd, &path, op, pathname); 2200806b681cSAl Viro while (unlikely(!filp)) { /* trailing symlink */ 22017b9337aaSNick Piggin struct path link = path; 2202def4af30SAl Viro void *cookie; 2203574197e0SAl Viro if (!(nd->flags & LOOKUP_FOLLOW)) { 220473d049a4SAl Viro path_put_conditional(&path, nd); 220573d049a4SAl Viro path_put(&nd->path); 220640b39136SAl Viro filp = ERR_PTR(-ELOOP); 220740b39136SAl Viro break; 220840b39136SAl Viro } 220973d049a4SAl Viro nd->flags |= LOOKUP_PARENT; 221073d049a4SAl Viro nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL); 2211574197e0SAl Viro error = follow_link(&link, nd, &cookie); 2212c3e380b0SAl Viro if (unlikely(error)) 2213f1afe9efSAl Viro filp = ERR_PTR(error); 2214c3e380b0SAl Viro else 221573d049a4SAl Viro filp = do_last(nd, &path, op, pathname); 2216574197e0SAl Viro put_link(nd, &link, cookie); 2217806b681cSAl Viro } 221810fa8e62SAl Viro out: 221973d049a4SAl Viro if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) 222073d049a4SAl Viro path_put(&nd->root); 2221fe2d35ffSAl Viro if (base) 2222fe2d35ffSAl Viro fput(base); 222373d049a4SAl Viro release_open_intent(nd); 222410fa8e62SAl Viro return filp; 22251da177e4SLinus Torvalds 222631e6b01fSNick Piggin out_filp: 222710fa8e62SAl Viro filp = ERR_PTR(error); 222810fa8e62SAl Viro goto out; 2229de459215SKirill Korotaev } 22301da177e4SLinus Torvalds 223113aab428SAl Viro struct file *do_filp_open(int dfd, const char *pathname, 223213aab428SAl Viro const struct open_flags *op, int flags) 223313aab428SAl Viro { 223473d049a4SAl Viro struct nameidata nd; 223513aab428SAl Viro struct file *filp; 223613aab428SAl Viro 223773d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU); 223813aab428SAl Viro if (unlikely(filp == ERR_PTR(-ECHILD))) 223973d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags); 224013aab428SAl Viro if (unlikely(filp == ERR_PTR(-ESTALE))) 224173d049a4SAl Viro filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL); 224213aab428SAl Viro return filp; 224313aab428SAl Viro } 224413aab428SAl Viro 224573d049a4SAl Viro struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt, 224673d049a4SAl Viro const char *name, const struct open_flags *op, int flags) 224773d049a4SAl Viro { 224873d049a4SAl Viro struct nameidata nd; 224973d049a4SAl Viro struct file *file; 225073d049a4SAl Viro 225173d049a4SAl Viro nd.root.mnt = mnt; 225273d049a4SAl Viro nd.root.dentry = dentry; 225373d049a4SAl Viro 225473d049a4SAl Viro flags |= LOOKUP_ROOT; 225573d049a4SAl Viro 2256bcda7652SAl Viro if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN) 225773d049a4SAl Viro return ERR_PTR(-ELOOP); 225873d049a4SAl Viro 225973d049a4SAl Viro file = path_openat(-1, name, &nd, op, flags | LOOKUP_RCU); 226073d049a4SAl Viro if (unlikely(file == ERR_PTR(-ECHILD))) 226173d049a4SAl Viro file = path_openat(-1, name, &nd, op, flags); 226273d049a4SAl Viro if (unlikely(file == ERR_PTR(-ESTALE))) 226373d049a4SAl Viro file = path_openat(-1, name, &nd, op, flags | LOOKUP_REVAL); 226473d049a4SAl Viro return file; 226573d049a4SAl Viro } 226673d049a4SAl Viro 2267ed75e95dSAl Viro struct dentry *kern_path_create(int dfd, const char *pathname, struct path *path, int is_dir) 22681da177e4SLinus Torvalds { 2269c663e5d8SChristoph Hellwig struct dentry *dentry = ERR_PTR(-EEXIST); 2270ed75e95dSAl Viro struct nameidata nd; 2271ed75e95dSAl Viro int error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd); 2272ed75e95dSAl Viro if (error) 2273ed75e95dSAl Viro return ERR_PTR(error); 22741da177e4SLinus Torvalds 2275c663e5d8SChristoph Hellwig /* 2276c663e5d8SChristoph Hellwig * Yucky last component or no last component at all? 2277c663e5d8SChristoph Hellwig * (foo/., foo/.., /////) 2278c663e5d8SChristoph Hellwig */ 2279ed75e95dSAl Viro if (nd.last_type != LAST_NORM) 2280ed75e95dSAl Viro goto out; 2281ed75e95dSAl Viro nd.flags &= ~LOOKUP_PARENT; 2282ed75e95dSAl Viro nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL; 2283ed75e95dSAl Viro nd.intent.open.flags = O_EXCL; 2284c663e5d8SChristoph Hellwig 2285c663e5d8SChristoph Hellwig /* 2286c663e5d8SChristoph Hellwig * Do the final lookup. 2287c663e5d8SChristoph Hellwig */ 2288ed75e95dSAl Viro mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 2289ed75e95dSAl Viro dentry = lookup_hash(&nd); 22901da177e4SLinus Torvalds if (IS_ERR(dentry)) 22911da177e4SLinus Torvalds goto fail; 2292c663e5d8SChristoph Hellwig 2293e9baf6e5SAl Viro if (dentry->d_inode) 2294e9baf6e5SAl Viro goto eexist; 2295c663e5d8SChristoph Hellwig /* 2296c663e5d8SChristoph Hellwig * Special case - lookup gave negative, but... we had foo/bar/ 2297c663e5d8SChristoph Hellwig * From the vfs_mknod() POV we just have a negative dentry - 2298c663e5d8SChristoph Hellwig * all is fine. Let's be bastards - you had / on the end, you've 2299c663e5d8SChristoph Hellwig * been asking for (non-existent) directory. -ENOENT for you. 2300c663e5d8SChristoph Hellwig */ 2301ed75e95dSAl Viro if (unlikely(!is_dir && nd.last.name[nd.last.len])) { 23021da177e4SLinus Torvalds dput(dentry); 23031da177e4SLinus Torvalds dentry = ERR_PTR(-ENOENT); 2304ed75e95dSAl Viro goto fail; 2305e9baf6e5SAl Viro } 2306ed75e95dSAl Viro *path = nd.path; 2307e9baf6e5SAl Viro return dentry; 2308e9baf6e5SAl Viro eexist: 2309e9baf6e5SAl Viro dput(dentry); 2310e9baf6e5SAl Viro dentry = ERR_PTR(-EEXIST); 23111da177e4SLinus Torvalds fail: 2312dae6ad8fSAl Viro mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 2313ed75e95dSAl Viro out: 2314dae6ad8fSAl Viro path_put(&nd.path); 2315ed75e95dSAl Viro return dentry; 2316dae6ad8fSAl Viro } 2317dae6ad8fSAl Viro EXPORT_SYMBOL(kern_path_create); 2318dae6ad8fSAl Viro 2319dae6ad8fSAl Viro struct dentry *user_path_create(int dfd, const char __user *pathname, struct path *path, int is_dir) 2320dae6ad8fSAl Viro { 2321dae6ad8fSAl Viro char *tmp = getname(pathname); 2322dae6ad8fSAl Viro struct dentry *res; 2323dae6ad8fSAl Viro if (IS_ERR(tmp)) 2324dae6ad8fSAl Viro return ERR_CAST(tmp); 2325dae6ad8fSAl Viro res = kern_path_create(dfd, tmp, path, is_dir); 2326dae6ad8fSAl Viro putname(tmp); 2327dae6ad8fSAl Viro return res; 2328dae6ad8fSAl Viro } 2329dae6ad8fSAl Viro EXPORT_SYMBOL(user_path_create); 2330dae6ad8fSAl Viro 23311da177e4SLinus Torvalds int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev) 23321da177e4SLinus Torvalds { 2333a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 23341da177e4SLinus Torvalds 23351da177e4SLinus Torvalds if (error) 23361da177e4SLinus Torvalds return error; 23371da177e4SLinus Torvalds 2338e795b717SSerge E. Hallyn if ((S_ISCHR(mode) || S_ISBLK(mode)) && 2339e795b717SSerge E. Hallyn !ns_capable(inode_userns(dir), CAP_MKNOD)) 23401da177e4SLinus Torvalds return -EPERM; 23411da177e4SLinus Torvalds 2342acfa4380SAl Viro if (!dir->i_op->mknod) 23431da177e4SLinus Torvalds return -EPERM; 23441da177e4SLinus Torvalds 234508ce5f16SSerge E. Hallyn error = devcgroup_inode_mknod(mode, dev); 234608ce5f16SSerge E. Hallyn if (error) 234708ce5f16SSerge E. Hallyn return error; 234808ce5f16SSerge E. Hallyn 23491da177e4SLinus Torvalds error = security_inode_mknod(dir, dentry, mode, dev); 23501da177e4SLinus Torvalds if (error) 23511da177e4SLinus Torvalds return error; 23521da177e4SLinus Torvalds 23531da177e4SLinus Torvalds error = dir->i_op->mknod(dir, dentry, mode, dev); 2354a74574aaSStephen Smalley if (!error) 2355f38aa942SAmy Griffis fsnotify_create(dir, dentry); 23561da177e4SLinus Torvalds return error; 23571da177e4SLinus Torvalds } 23581da177e4SLinus Torvalds 2359463c3197SDave Hansen static int may_mknod(mode_t mode) 2360463c3197SDave Hansen { 2361463c3197SDave Hansen switch (mode & S_IFMT) { 2362463c3197SDave Hansen case S_IFREG: 2363463c3197SDave Hansen case S_IFCHR: 2364463c3197SDave Hansen case S_IFBLK: 2365463c3197SDave Hansen case S_IFIFO: 2366463c3197SDave Hansen case S_IFSOCK: 2367463c3197SDave Hansen case 0: /* zero mode translates to S_IFREG */ 2368463c3197SDave Hansen return 0; 2369463c3197SDave Hansen case S_IFDIR: 2370463c3197SDave Hansen return -EPERM; 2371463c3197SDave Hansen default: 2372463c3197SDave Hansen return -EINVAL; 2373463c3197SDave Hansen } 2374463c3197SDave Hansen } 2375463c3197SDave Hansen 23762e4d0924SHeiko Carstens SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, int, mode, 23772e4d0924SHeiko Carstens unsigned, dev) 23781da177e4SLinus Torvalds { 23791da177e4SLinus Torvalds struct dentry *dentry; 2380dae6ad8fSAl Viro struct path path; 2381dae6ad8fSAl Viro int error; 23821da177e4SLinus Torvalds 23831da177e4SLinus Torvalds if (S_ISDIR(mode)) 23841da177e4SLinus Torvalds return -EPERM; 23851da177e4SLinus Torvalds 2386dae6ad8fSAl Viro dentry = user_path_create(dfd, filename, &path, 0); 2387dae6ad8fSAl Viro if (IS_ERR(dentry)) 2388dae6ad8fSAl Viro return PTR_ERR(dentry); 23892ad94ae6SAl Viro 2390dae6ad8fSAl Viro if (!IS_POSIXACL(path.dentry->d_inode)) 2391ce3b0f8dSAl Viro mode &= ~current_umask(); 2392463c3197SDave Hansen error = may_mknod(mode); 2393463c3197SDave Hansen if (error) 2394463c3197SDave Hansen goto out_dput; 2395dae6ad8fSAl Viro error = mnt_want_write(path.mnt); 2396463c3197SDave Hansen if (error) 2397463c3197SDave Hansen goto out_dput; 2398dae6ad8fSAl Viro error = security_path_mknod(&path, dentry, mode, dev); 2399be6d3e56SKentaro Takeda if (error) 2400be6d3e56SKentaro Takeda goto out_drop_write; 24011da177e4SLinus Torvalds switch (mode & S_IFMT) { 24021da177e4SLinus Torvalds case 0: case S_IFREG: 2403dae6ad8fSAl Viro error = vfs_create(path.dentry->d_inode,dentry,mode,NULL); 24041da177e4SLinus Torvalds break; 24051da177e4SLinus Torvalds case S_IFCHR: case S_IFBLK: 2406dae6ad8fSAl Viro error = vfs_mknod(path.dentry->d_inode,dentry,mode, 24071da177e4SLinus Torvalds new_decode_dev(dev)); 24081da177e4SLinus Torvalds break; 24091da177e4SLinus Torvalds case S_IFIFO: case S_IFSOCK: 2410dae6ad8fSAl Viro error = vfs_mknod(path.dentry->d_inode,dentry,mode,0); 24111da177e4SLinus Torvalds break; 24121da177e4SLinus Torvalds } 2413be6d3e56SKentaro Takeda out_drop_write: 2414dae6ad8fSAl Viro mnt_drop_write(path.mnt); 2415463c3197SDave Hansen out_dput: 24161da177e4SLinus Torvalds dput(dentry); 2417dae6ad8fSAl Viro mutex_unlock(&path.dentry->d_inode->i_mutex); 2418dae6ad8fSAl Viro path_put(&path); 24191da177e4SLinus Torvalds 24201da177e4SLinus Torvalds return error; 24211da177e4SLinus Torvalds } 24221da177e4SLinus Torvalds 24233480b257SHeiko Carstens SYSCALL_DEFINE3(mknod, const char __user *, filename, int, mode, unsigned, dev) 24245590ff0dSUlrich Drepper { 24255590ff0dSUlrich Drepper return sys_mknodat(AT_FDCWD, filename, mode, dev); 24265590ff0dSUlrich Drepper } 24275590ff0dSUlrich Drepper 24281da177e4SLinus Torvalds int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) 24291da177e4SLinus Torvalds { 2430a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 24311da177e4SLinus Torvalds 24321da177e4SLinus Torvalds if (error) 24331da177e4SLinus Torvalds return error; 24341da177e4SLinus Torvalds 2435acfa4380SAl Viro if (!dir->i_op->mkdir) 24361da177e4SLinus Torvalds return -EPERM; 24371da177e4SLinus Torvalds 24381da177e4SLinus Torvalds mode &= (S_IRWXUGO|S_ISVTX); 24391da177e4SLinus Torvalds error = security_inode_mkdir(dir, dentry, mode); 24401da177e4SLinus Torvalds if (error) 24411da177e4SLinus Torvalds return error; 24421da177e4SLinus Torvalds 24431da177e4SLinus Torvalds error = dir->i_op->mkdir(dir, dentry, mode); 2444a74574aaSStephen Smalley if (!error) 2445f38aa942SAmy Griffis fsnotify_mkdir(dir, dentry); 24461da177e4SLinus Torvalds return error; 24471da177e4SLinus Torvalds } 24481da177e4SLinus Torvalds 24492e4d0924SHeiko Carstens SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, int, mode) 24501da177e4SLinus Torvalds { 24516902d925SDave Hansen struct dentry *dentry; 2452dae6ad8fSAl Viro struct path path; 2453dae6ad8fSAl Viro int error; 24541da177e4SLinus Torvalds 2455dae6ad8fSAl Viro dentry = user_path_create(dfd, pathname, &path, 1); 24566902d925SDave Hansen if (IS_ERR(dentry)) 2457dae6ad8fSAl Viro return PTR_ERR(dentry); 24586902d925SDave Hansen 2459dae6ad8fSAl Viro if (!IS_POSIXACL(path.dentry->d_inode)) 2460ce3b0f8dSAl Viro mode &= ~current_umask(); 2461dae6ad8fSAl Viro error = mnt_want_write(path.mnt); 2462463c3197SDave Hansen if (error) 2463463c3197SDave Hansen goto out_dput; 2464dae6ad8fSAl Viro error = security_path_mkdir(&path, dentry, mode); 2465be6d3e56SKentaro Takeda if (error) 2466be6d3e56SKentaro Takeda goto out_drop_write; 2467dae6ad8fSAl Viro error = vfs_mkdir(path.dentry->d_inode, dentry, mode); 2468be6d3e56SKentaro Takeda out_drop_write: 2469dae6ad8fSAl Viro mnt_drop_write(path.mnt); 2470463c3197SDave Hansen out_dput: 24711da177e4SLinus Torvalds dput(dentry); 2472dae6ad8fSAl Viro mutex_unlock(&path.dentry->d_inode->i_mutex); 2473dae6ad8fSAl Viro path_put(&path); 24741da177e4SLinus Torvalds return error; 24751da177e4SLinus Torvalds } 24761da177e4SLinus Torvalds 24773cdad428SHeiko Carstens SYSCALL_DEFINE2(mkdir, const char __user *, pathname, int, mode) 24785590ff0dSUlrich Drepper { 24795590ff0dSUlrich Drepper return sys_mkdirat(AT_FDCWD, pathname, mode); 24805590ff0dSUlrich Drepper } 24815590ff0dSUlrich Drepper 24821da177e4SLinus Torvalds /* 2483a71905f0SSage Weil * The dentry_unhash() helper will try to drop the dentry early: we 2484a71905f0SSage Weil * should have a usage count of 2 if we're the only user of this 2485a71905f0SSage Weil * dentry, and if that is true (possibly after pruning the dcache), 2486a71905f0SSage Weil * then we drop the dentry now. 24871da177e4SLinus Torvalds * 24881da177e4SLinus Torvalds * A low-level filesystem can, if it choses, legally 24891da177e4SLinus Torvalds * do a 24901da177e4SLinus Torvalds * 24911da177e4SLinus Torvalds * if (!d_unhashed(dentry)) 24921da177e4SLinus Torvalds * return -EBUSY; 24931da177e4SLinus Torvalds * 24941da177e4SLinus Torvalds * if it cannot handle the case of removing a directory 24951da177e4SLinus Torvalds * that is still in use by something else.. 24961da177e4SLinus Torvalds */ 24971da177e4SLinus Torvalds void dentry_unhash(struct dentry *dentry) 24981da177e4SLinus Torvalds { 24991da177e4SLinus Torvalds shrink_dcache_parent(dentry); 25001da177e4SLinus Torvalds spin_lock(&dentry->d_lock); 250164252c75SSage Weil if (dentry->d_count == 1) 25021da177e4SLinus Torvalds __d_drop(dentry); 25031da177e4SLinus Torvalds spin_unlock(&dentry->d_lock); 25041da177e4SLinus Torvalds } 25051da177e4SLinus Torvalds 25061da177e4SLinus Torvalds int vfs_rmdir(struct inode *dir, struct dentry *dentry) 25071da177e4SLinus Torvalds { 25081da177e4SLinus Torvalds int error = may_delete(dir, dentry, 1); 25091da177e4SLinus Torvalds 25101da177e4SLinus Torvalds if (error) 25111da177e4SLinus Torvalds return error; 25121da177e4SLinus Torvalds 2513acfa4380SAl Viro if (!dir->i_op->rmdir) 25141da177e4SLinus Torvalds return -EPERM; 25151da177e4SLinus Torvalds 25161b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 2517912dbc15SSage Weil 25181da177e4SLinus Torvalds error = -EBUSY; 2519912dbc15SSage Weil if (d_mountpoint(dentry)) 2520912dbc15SSage Weil goto out; 2521912dbc15SSage Weil 25221da177e4SLinus Torvalds error = security_inode_rmdir(dir, dentry); 2523912dbc15SSage Weil if (error) 2524912dbc15SSage Weil goto out; 2525912dbc15SSage Weil 25263cebde24SSage Weil shrink_dcache_parent(dentry); 25271da177e4SLinus Torvalds error = dir->i_op->rmdir(dir, dentry); 2528912dbc15SSage Weil if (error) 2529912dbc15SSage Weil goto out; 2530912dbc15SSage Weil 25311da177e4SLinus Torvalds dentry->d_inode->i_flags |= S_DEAD; 2532d83c49f3SAl Viro dont_mount(dentry); 25331da177e4SLinus Torvalds 2534912dbc15SSage Weil out: 2535912dbc15SSage Weil mutex_unlock(&dentry->d_inode->i_mutex); 2536912dbc15SSage Weil if (!error) 2537912dbc15SSage Weil d_delete(dentry); 25381da177e4SLinus Torvalds return error; 25391da177e4SLinus Torvalds } 25401da177e4SLinus Torvalds 25415590ff0dSUlrich Drepper static long do_rmdir(int dfd, const char __user *pathname) 25421da177e4SLinus Torvalds { 25431da177e4SLinus Torvalds int error = 0; 25441da177e4SLinus Torvalds char * name; 25451da177e4SLinus Torvalds struct dentry *dentry; 25461da177e4SLinus Torvalds struct nameidata nd; 25471da177e4SLinus Torvalds 25482ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &name); 25491da177e4SLinus Torvalds if (error) 25502ad94ae6SAl Viro return error; 25511da177e4SLinus Torvalds 25521da177e4SLinus Torvalds switch(nd.last_type) { 25531da177e4SLinus Torvalds case LAST_DOTDOT: 25541da177e4SLinus Torvalds error = -ENOTEMPTY; 25551da177e4SLinus Torvalds goto exit1; 25561da177e4SLinus Torvalds case LAST_DOT: 25571da177e4SLinus Torvalds error = -EINVAL; 25581da177e4SLinus Torvalds goto exit1; 25591da177e4SLinus Torvalds case LAST_ROOT: 25601da177e4SLinus Torvalds error = -EBUSY; 25611da177e4SLinus Torvalds goto exit1; 25621da177e4SLinus Torvalds } 25630612d9fbSOGAWA Hirofumi 25640612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 25650612d9fbSOGAWA Hirofumi 25664ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 256749705b77SChristoph Hellwig dentry = lookup_hash(&nd); 25681da177e4SLinus Torvalds error = PTR_ERR(dentry); 25696902d925SDave Hansen if (IS_ERR(dentry)) 25706902d925SDave Hansen goto exit2; 2571e6bc45d6STheodore Ts'o if (!dentry->d_inode) { 2572e6bc45d6STheodore Ts'o error = -ENOENT; 2573e6bc45d6STheodore Ts'o goto exit3; 2574e6bc45d6STheodore Ts'o } 25750622753bSDave Hansen error = mnt_want_write(nd.path.mnt); 25760622753bSDave Hansen if (error) 25770622753bSDave Hansen goto exit3; 2578be6d3e56SKentaro Takeda error = security_path_rmdir(&nd.path, dentry); 2579be6d3e56SKentaro Takeda if (error) 2580be6d3e56SKentaro Takeda goto exit4; 25814ac91378SJan Blunck error = vfs_rmdir(nd.path.dentry->d_inode, dentry); 2582be6d3e56SKentaro Takeda exit4: 25830622753bSDave Hansen mnt_drop_write(nd.path.mnt); 25840622753bSDave Hansen exit3: 25851da177e4SLinus Torvalds dput(dentry); 25866902d925SDave Hansen exit2: 25874ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 25881da177e4SLinus Torvalds exit1: 25891d957f9bSJan Blunck path_put(&nd.path); 25901da177e4SLinus Torvalds putname(name); 25911da177e4SLinus Torvalds return error; 25921da177e4SLinus Torvalds } 25931da177e4SLinus Torvalds 25943cdad428SHeiko Carstens SYSCALL_DEFINE1(rmdir, const char __user *, pathname) 25955590ff0dSUlrich Drepper { 25965590ff0dSUlrich Drepper return do_rmdir(AT_FDCWD, pathname); 25975590ff0dSUlrich Drepper } 25985590ff0dSUlrich Drepper 25991da177e4SLinus Torvalds int vfs_unlink(struct inode *dir, struct dentry *dentry) 26001da177e4SLinus Torvalds { 26011da177e4SLinus Torvalds int error = may_delete(dir, dentry, 0); 26021da177e4SLinus Torvalds 26031da177e4SLinus Torvalds if (error) 26041da177e4SLinus Torvalds return error; 26051da177e4SLinus Torvalds 2606acfa4380SAl Viro if (!dir->i_op->unlink) 26071da177e4SLinus Torvalds return -EPERM; 26081da177e4SLinus Torvalds 26091b1dcc1bSJes Sorensen mutex_lock(&dentry->d_inode->i_mutex); 26101da177e4SLinus Torvalds if (d_mountpoint(dentry)) 26111da177e4SLinus Torvalds error = -EBUSY; 26121da177e4SLinus Torvalds else { 26131da177e4SLinus Torvalds error = security_inode_unlink(dir, dentry); 2614bec1052eSAl Viro if (!error) { 26151da177e4SLinus Torvalds error = dir->i_op->unlink(dir, dentry); 2616bec1052eSAl Viro if (!error) 2617d83c49f3SAl Viro dont_mount(dentry); 2618bec1052eSAl Viro } 26191da177e4SLinus Torvalds } 26201b1dcc1bSJes Sorensen mutex_unlock(&dentry->d_inode->i_mutex); 26211da177e4SLinus Torvalds 26221da177e4SLinus Torvalds /* We don't d_delete() NFS sillyrenamed files--they still exist. */ 26231da177e4SLinus Torvalds if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) { 2624ece95912SJan Kara fsnotify_link_count(dentry->d_inode); 26251da177e4SLinus Torvalds d_delete(dentry); 26261da177e4SLinus Torvalds } 26270eeca283SRobert Love 26281da177e4SLinus Torvalds return error; 26291da177e4SLinus Torvalds } 26301da177e4SLinus Torvalds 26311da177e4SLinus Torvalds /* 26321da177e4SLinus Torvalds * Make sure that the actual truncation of the file will occur outside its 26331b1dcc1bSJes Sorensen * directory's i_mutex. Truncate can take a long time if there is a lot of 26341da177e4SLinus Torvalds * writeout happening, and we don't want to prevent access to the directory 26351da177e4SLinus Torvalds * while waiting on the I/O. 26361da177e4SLinus Torvalds */ 26375590ff0dSUlrich Drepper static long do_unlinkat(int dfd, const char __user *pathname) 26381da177e4SLinus Torvalds { 26392ad94ae6SAl Viro int error; 26401da177e4SLinus Torvalds char *name; 26411da177e4SLinus Torvalds struct dentry *dentry; 26421da177e4SLinus Torvalds struct nameidata nd; 26431da177e4SLinus Torvalds struct inode *inode = NULL; 26441da177e4SLinus Torvalds 26452ad94ae6SAl Viro error = user_path_parent(dfd, pathname, &nd, &name); 26461da177e4SLinus Torvalds if (error) 26472ad94ae6SAl Viro return error; 26482ad94ae6SAl Viro 26491da177e4SLinus Torvalds error = -EISDIR; 26501da177e4SLinus Torvalds if (nd.last_type != LAST_NORM) 26511da177e4SLinus Torvalds goto exit1; 26520612d9fbSOGAWA Hirofumi 26530612d9fbSOGAWA Hirofumi nd.flags &= ~LOOKUP_PARENT; 26540612d9fbSOGAWA Hirofumi 26554ac91378SJan Blunck mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); 265649705b77SChristoph Hellwig dentry = lookup_hash(&nd); 26571da177e4SLinus Torvalds error = PTR_ERR(dentry); 26581da177e4SLinus Torvalds if (!IS_ERR(dentry)) { 26591da177e4SLinus Torvalds /* Why not before? Because we want correct error value */ 266050338b88STörök Edwin if (nd.last.name[nd.last.len]) 266150338b88STörök Edwin goto slashes; 26621da177e4SLinus Torvalds inode = dentry->d_inode; 266350338b88STörök Edwin if (!inode) 2664e6bc45d6STheodore Ts'o goto slashes; 26657de9c6eeSAl Viro ihold(inode); 26660622753bSDave Hansen error = mnt_want_write(nd.path.mnt); 26670622753bSDave Hansen if (error) 26680622753bSDave Hansen goto exit2; 2669be6d3e56SKentaro Takeda error = security_path_unlink(&nd.path, dentry); 2670be6d3e56SKentaro Takeda if (error) 2671be6d3e56SKentaro Takeda goto exit3; 26724ac91378SJan Blunck error = vfs_unlink(nd.path.dentry->d_inode, dentry); 2673be6d3e56SKentaro Takeda exit3: 26740622753bSDave Hansen mnt_drop_write(nd.path.mnt); 26751da177e4SLinus Torvalds exit2: 26761da177e4SLinus Torvalds dput(dentry); 26771da177e4SLinus Torvalds } 26784ac91378SJan Blunck mutex_unlock(&nd.path.dentry->d_inode->i_mutex); 26791da177e4SLinus Torvalds if (inode) 26801da177e4SLinus Torvalds iput(inode); /* truncate the inode here */ 26811da177e4SLinus Torvalds exit1: 26821d957f9bSJan Blunck path_put(&nd.path); 26831da177e4SLinus Torvalds putname(name); 26841da177e4SLinus Torvalds return error; 26851da177e4SLinus Torvalds 26861da177e4SLinus Torvalds slashes: 26871da177e4SLinus Torvalds error = !dentry->d_inode ? -ENOENT : 26881da177e4SLinus Torvalds S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR; 26891da177e4SLinus Torvalds goto exit2; 26901da177e4SLinus Torvalds } 26911da177e4SLinus Torvalds 26922e4d0924SHeiko Carstens SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag) 26935590ff0dSUlrich Drepper { 26945590ff0dSUlrich Drepper if ((flag & ~AT_REMOVEDIR) != 0) 26955590ff0dSUlrich Drepper return -EINVAL; 26965590ff0dSUlrich Drepper 26975590ff0dSUlrich Drepper if (flag & AT_REMOVEDIR) 26985590ff0dSUlrich Drepper return do_rmdir(dfd, pathname); 26995590ff0dSUlrich Drepper 27005590ff0dSUlrich Drepper return do_unlinkat(dfd, pathname); 27015590ff0dSUlrich Drepper } 27025590ff0dSUlrich Drepper 27033480b257SHeiko Carstens SYSCALL_DEFINE1(unlink, const char __user *, pathname) 27045590ff0dSUlrich Drepper { 27055590ff0dSUlrich Drepper return do_unlinkat(AT_FDCWD, pathname); 27065590ff0dSUlrich Drepper } 27075590ff0dSUlrich Drepper 2708db2e747bSMiklos Szeredi int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname) 27091da177e4SLinus Torvalds { 2710a95164d9SMiklos Szeredi int error = may_create(dir, dentry); 27111da177e4SLinus Torvalds 27121da177e4SLinus Torvalds if (error) 27131da177e4SLinus Torvalds return error; 27141da177e4SLinus Torvalds 2715acfa4380SAl Viro if (!dir->i_op->symlink) 27161da177e4SLinus Torvalds return -EPERM; 27171da177e4SLinus Torvalds 27181da177e4SLinus Torvalds error = security_inode_symlink(dir, dentry, oldname); 27191da177e4SLinus Torvalds if (error) 27201da177e4SLinus Torvalds return error; 27211da177e4SLinus Torvalds 27221da177e4SLinus Torvalds error = dir->i_op->symlink(dir, dentry, oldname); 2723a74574aaSStephen Smalley if (!error) 2724f38aa942SAmy Griffis fsnotify_create(dir, dentry); 27251da177e4SLinus Torvalds return error; 27261da177e4SLinus Torvalds } 27271da177e4SLinus Torvalds 27282e4d0924SHeiko Carstens SYSCALL_DEFINE3(symlinkat, const char __user *, oldname, 27292e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 27301da177e4SLinus Torvalds { 27312ad94ae6SAl Viro int error; 27321da177e4SLinus Torvalds char *from; 27336902d925SDave Hansen struct dentry *dentry; 2734dae6ad8fSAl Viro struct path path; 27351da177e4SLinus Torvalds 27361da177e4SLinus Torvalds from = getname(oldname); 27371da177e4SLinus Torvalds if (IS_ERR(from)) 27381da177e4SLinus Torvalds return PTR_ERR(from); 27392ad94ae6SAl Viro 2740dae6ad8fSAl Viro dentry = user_path_create(newdfd, newname, &path, 0); 27411da177e4SLinus Torvalds error = PTR_ERR(dentry); 27426902d925SDave Hansen if (IS_ERR(dentry)) 2743dae6ad8fSAl Viro goto out_putname; 27446902d925SDave Hansen 2745dae6ad8fSAl Viro error = mnt_want_write(path.mnt); 274675c3f29dSDave Hansen if (error) 274775c3f29dSDave Hansen goto out_dput; 2748dae6ad8fSAl Viro error = security_path_symlink(&path, dentry, from); 2749be6d3e56SKentaro Takeda if (error) 2750be6d3e56SKentaro Takeda goto out_drop_write; 2751dae6ad8fSAl Viro error = vfs_symlink(path.dentry->d_inode, dentry, from); 2752be6d3e56SKentaro Takeda out_drop_write: 2753dae6ad8fSAl Viro mnt_drop_write(path.mnt); 275475c3f29dSDave Hansen out_dput: 27551da177e4SLinus Torvalds dput(dentry); 2756dae6ad8fSAl Viro mutex_unlock(&path.dentry->d_inode->i_mutex); 2757dae6ad8fSAl Viro path_put(&path); 27586902d925SDave Hansen out_putname: 27591da177e4SLinus Torvalds putname(from); 27601da177e4SLinus Torvalds return error; 27611da177e4SLinus Torvalds } 27621da177e4SLinus Torvalds 27633480b257SHeiko Carstens SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname) 27645590ff0dSUlrich Drepper { 27655590ff0dSUlrich Drepper return sys_symlinkat(oldname, AT_FDCWD, newname); 27665590ff0dSUlrich Drepper } 27675590ff0dSUlrich Drepper 27681da177e4SLinus Torvalds int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) 27691da177e4SLinus Torvalds { 27701da177e4SLinus Torvalds struct inode *inode = old_dentry->d_inode; 27711da177e4SLinus Torvalds int error; 27721da177e4SLinus Torvalds 27731da177e4SLinus Torvalds if (!inode) 27741da177e4SLinus Torvalds return -ENOENT; 27751da177e4SLinus Torvalds 2776a95164d9SMiklos Szeredi error = may_create(dir, new_dentry); 27771da177e4SLinus Torvalds if (error) 27781da177e4SLinus Torvalds return error; 27791da177e4SLinus Torvalds 27801da177e4SLinus Torvalds if (dir->i_sb != inode->i_sb) 27811da177e4SLinus Torvalds return -EXDEV; 27821da177e4SLinus Torvalds 27831da177e4SLinus Torvalds /* 27841da177e4SLinus Torvalds * A link to an append-only or immutable file cannot be created. 27851da177e4SLinus Torvalds */ 27861da177e4SLinus Torvalds if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) 27871da177e4SLinus Torvalds return -EPERM; 2788acfa4380SAl Viro if (!dir->i_op->link) 27891da177e4SLinus Torvalds return -EPERM; 27907e79eedbSTetsuo Handa if (S_ISDIR(inode->i_mode)) 27911da177e4SLinus Torvalds return -EPERM; 27921da177e4SLinus Torvalds 27931da177e4SLinus Torvalds error = security_inode_link(old_dentry, dir, new_dentry); 27941da177e4SLinus Torvalds if (error) 27951da177e4SLinus Torvalds return error; 27961da177e4SLinus Torvalds 27977e79eedbSTetsuo Handa mutex_lock(&inode->i_mutex); 2798aae8a97dSAneesh Kumar K.V /* Make sure we don't allow creating hardlink to an unlinked file */ 2799aae8a97dSAneesh Kumar K.V if (inode->i_nlink == 0) 2800aae8a97dSAneesh Kumar K.V error = -ENOENT; 2801aae8a97dSAneesh Kumar K.V else 28021da177e4SLinus Torvalds error = dir->i_op->link(old_dentry, dir, new_dentry); 28037e79eedbSTetsuo Handa mutex_unlock(&inode->i_mutex); 2804e31e14ecSStephen Smalley if (!error) 28057e79eedbSTetsuo Handa fsnotify_link(dir, inode, new_dentry); 28061da177e4SLinus Torvalds return error; 28071da177e4SLinus Torvalds } 28081da177e4SLinus Torvalds 28091da177e4SLinus Torvalds /* 28101da177e4SLinus Torvalds * Hardlinks are often used in delicate situations. We avoid 28111da177e4SLinus Torvalds * security-related surprises by not following symlinks on the 28121da177e4SLinus Torvalds * newname. --KAB 28131da177e4SLinus Torvalds * 28141da177e4SLinus Torvalds * We don't follow them on the oldname either to be compatible 28151da177e4SLinus Torvalds * with linux 2.0, and to avoid hard-linking to directories 28161da177e4SLinus Torvalds * and other special files. --ADM 28171da177e4SLinus Torvalds */ 28182e4d0924SHeiko Carstens SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname, 28192e4d0924SHeiko Carstens int, newdfd, const char __user *, newname, int, flags) 28201da177e4SLinus Torvalds { 28211da177e4SLinus Torvalds struct dentry *new_dentry; 2822dae6ad8fSAl Viro struct path old_path, new_path; 282311a7b371SAneesh Kumar K.V int how = 0; 28241da177e4SLinus Torvalds int error; 28251da177e4SLinus Torvalds 282611a7b371SAneesh Kumar K.V if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0) 2827c04030e1SUlrich Drepper return -EINVAL; 282811a7b371SAneesh Kumar K.V /* 282911a7b371SAneesh Kumar K.V * To use null names we require CAP_DAC_READ_SEARCH 283011a7b371SAneesh Kumar K.V * This ensures that not everyone will be able to create 283111a7b371SAneesh Kumar K.V * handlink using the passed filedescriptor. 283211a7b371SAneesh Kumar K.V */ 283311a7b371SAneesh Kumar K.V if (flags & AT_EMPTY_PATH) { 283411a7b371SAneesh Kumar K.V if (!capable(CAP_DAC_READ_SEARCH)) 283511a7b371SAneesh Kumar K.V return -ENOENT; 283611a7b371SAneesh Kumar K.V how = LOOKUP_EMPTY; 283711a7b371SAneesh Kumar K.V } 2838c04030e1SUlrich Drepper 283911a7b371SAneesh Kumar K.V if (flags & AT_SYMLINK_FOLLOW) 284011a7b371SAneesh Kumar K.V how |= LOOKUP_FOLLOW; 284111a7b371SAneesh Kumar K.V 284211a7b371SAneesh Kumar K.V error = user_path_at(olddfd, oldname, how, &old_path); 28431da177e4SLinus Torvalds if (error) 28442ad94ae6SAl Viro return error; 28452ad94ae6SAl Viro 2846dae6ad8fSAl Viro new_dentry = user_path_create(newdfd, newname, &new_path, 0); 28471da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 28486902d925SDave Hansen if (IS_ERR(new_dentry)) 2849dae6ad8fSAl Viro goto out; 2850dae6ad8fSAl Viro 2851dae6ad8fSAl Viro error = -EXDEV; 2852dae6ad8fSAl Viro if (old_path.mnt != new_path.mnt) 2853dae6ad8fSAl Viro goto out_dput; 2854dae6ad8fSAl Viro error = mnt_want_write(new_path.mnt); 285575c3f29dSDave Hansen if (error) 285675c3f29dSDave Hansen goto out_dput; 2857dae6ad8fSAl Viro error = security_path_link(old_path.dentry, &new_path, new_dentry); 2858be6d3e56SKentaro Takeda if (error) 2859be6d3e56SKentaro Takeda goto out_drop_write; 2860dae6ad8fSAl Viro error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry); 2861be6d3e56SKentaro Takeda out_drop_write: 2862dae6ad8fSAl Viro mnt_drop_write(new_path.mnt); 286375c3f29dSDave Hansen out_dput: 28641da177e4SLinus Torvalds dput(new_dentry); 2865dae6ad8fSAl Viro mutex_unlock(&new_path.dentry->d_inode->i_mutex); 2866dae6ad8fSAl Viro path_put(&new_path); 28671da177e4SLinus Torvalds out: 28682d8f3038SAl Viro path_put(&old_path); 28691da177e4SLinus Torvalds 28701da177e4SLinus Torvalds return error; 28711da177e4SLinus Torvalds } 28721da177e4SLinus Torvalds 28733480b257SHeiko Carstens SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname) 28745590ff0dSUlrich Drepper { 2875c04030e1SUlrich Drepper return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0); 28765590ff0dSUlrich Drepper } 28775590ff0dSUlrich Drepper 28781da177e4SLinus Torvalds /* 28791da177e4SLinus Torvalds * The worst of all namespace operations - renaming directory. "Perverted" 28801da177e4SLinus Torvalds * doesn't even start to describe it. Somebody in UCB had a heck of a trip... 28811da177e4SLinus Torvalds * Problems: 28821da177e4SLinus Torvalds * a) we can get into loop creation. Check is done in is_subdir(). 28831da177e4SLinus Torvalds * b) race potential - two innocent renames can create a loop together. 28841da177e4SLinus Torvalds * That's where 4.4 screws up. Current fix: serialization on 2885a11f3a05SArjan van de Ven * sb->s_vfs_rename_mutex. We might be more accurate, but that's another 28861da177e4SLinus Torvalds * story. 28871da177e4SLinus Torvalds * c) we have to lock _three_ objects - parents and victim (if it exists). 28881b1dcc1bSJes Sorensen * And that - after we got ->i_mutex on parents (until then we don't know 28891da177e4SLinus Torvalds * whether the target exists). Solution: try to be smart with locking 28901da177e4SLinus Torvalds * order for inodes. We rely on the fact that tree topology may change 2891a11f3a05SArjan van de Ven * only under ->s_vfs_rename_mutex _and_ that parent of the object we 28921da177e4SLinus Torvalds * move will be locked. Thus we can rank directories by the tree 28931da177e4SLinus Torvalds * (ancestors first) and rank all non-directories after them. 28941da177e4SLinus Torvalds * That works since everybody except rename does "lock parent, lookup, 2895a11f3a05SArjan van de Ven * lock child" and rename is under ->s_vfs_rename_mutex. 28961da177e4SLinus Torvalds * HOWEVER, it relies on the assumption that any object with ->lookup() 28971da177e4SLinus Torvalds * has no more than 1 dentry. If "hybrid" objects will ever appear, 28981da177e4SLinus Torvalds * we'd better make sure that there's no link(2) for them. 2899e4eaac06SSage Weil * d) conversion from fhandle to dentry may come in the wrong moment - when 29001b1dcc1bSJes Sorensen * we are removing the target. Solution: we will have to grab ->i_mutex 29011da177e4SLinus Torvalds * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on 2902c41b20e7SAdam Buchbinder * ->i_mutex on parents, which works but leads to some truly excessive 29031da177e4SLinus Torvalds * locking]. 29041da177e4SLinus Torvalds */ 290575c96f85SAdrian Bunk static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry, 29061da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 29071da177e4SLinus Torvalds { 29081da177e4SLinus Torvalds int error = 0; 29099055cba7SSage Weil struct inode *target = new_dentry->d_inode; 29101da177e4SLinus Torvalds 29111da177e4SLinus Torvalds /* 29121da177e4SLinus Torvalds * If we are going to change the parent - check write permissions, 29131da177e4SLinus Torvalds * we'll need to flip '..'. 29141da177e4SLinus Torvalds */ 29151da177e4SLinus Torvalds if (new_dir != old_dir) { 2916f419a2e3SAl Viro error = inode_permission(old_dentry->d_inode, MAY_WRITE); 29171da177e4SLinus Torvalds if (error) 29181da177e4SLinus Torvalds return error; 29191da177e4SLinus Torvalds } 29201da177e4SLinus Torvalds 29211da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 29221da177e4SLinus Torvalds if (error) 29231da177e4SLinus Torvalds return error; 29241da177e4SLinus Torvalds 2925d83c49f3SAl Viro if (target) 29261b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 29279055cba7SSage Weil 29281da177e4SLinus Torvalds error = -EBUSY; 29299055cba7SSage Weil if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry)) 29309055cba7SSage Weil goto out; 29319055cba7SSage Weil 29323cebde24SSage Weil if (target) 29333cebde24SSage Weil shrink_dcache_parent(new_dentry); 29341da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 29359055cba7SSage Weil if (error) 29369055cba7SSage Weil goto out; 29379055cba7SSage Weil 29381da177e4SLinus Torvalds if (target) { 29391da177e4SLinus Torvalds target->i_flags |= S_DEAD; 2940d83c49f3SAl Viro dont_mount(new_dentry); 2941d83c49f3SAl Viro } 29429055cba7SSage Weil out: 29439055cba7SSage Weil if (target) 29441b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 2945e31e14ecSStephen Smalley if (!error) 2946349457ccSMark Fasheh if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) 29471da177e4SLinus Torvalds d_move(old_dentry,new_dentry); 29481da177e4SLinus Torvalds return error; 29491da177e4SLinus Torvalds } 29501da177e4SLinus Torvalds 295175c96f85SAdrian Bunk static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry, 29521da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 29531da177e4SLinus Torvalds { 295451892bbbSSage Weil struct inode *target = new_dentry->d_inode; 29551da177e4SLinus Torvalds int error; 29561da177e4SLinus Torvalds 29571da177e4SLinus Torvalds error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); 29581da177e4SLinus Torvalds if (error) 29591da177e4SLinus Torvalds return error; 29601da177e4SLinus Torvalds 29611da177e4SLinus Torvalds dget(new_dentry); 29621da177e4SLinus Torvalds if (target) 29631b1dcc1bSJes Sorensen mutex_lock(&target->i_mutex); 296451892bbbSSage Weil 29651da177e4SLinus Torvalds error = -EBUSY; 296651892bbbSSage Weil if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry)) 296751892bbbSSage Weil goto out; 296851892bbbSSage Weil 29691da177e4SLinus Torvalds error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); 297051892bbbSSage Weil if (error) 297151892bbbSSage Weil goto out; 297251892bbbSSage Weil 2973bec1052eSAl Viro if (target) 2974d83c49f3SAl Viro dont_mount(new_dentry); 2975349457ccSMark Fasheh if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) 29761da177e4SLinus Torvalds d_move(old_dentry, new_dentry); 297751892bbbSSage Weil out: 29781da177e4SLinus Torvalds if (target) 29791b1dcc1bSJes Sorensen mutex_unlock(&target->i_mutex); 29801da177e4SLinus Torvalds dput(new_dentry); 29811da177e4SLinus Torvalds return error; 29821da177e4SLinus Torvalds } 29831da177e4SLinus Torvalds 29841da177e4SLinus Torvalds int vfs_rename(struct inode *old_dir, struct dentry *old_dentry, 29851da177e4SLinus Torvalds struct inode *new_dir, struct dentry *new_dentry) 29861da177e4SLinus Torvalds { 29871da177e4SLinus Torvalds int error; 29881da177e4SLinus Torvalds int is_dir = S_ISDIR(old_dentry->d_inode->i_mode); 298959b0df21SEric Paris const unsigned char *old_name; 29901da177e4SLinus Torvalds 29911da177e4SLinus Torvalds if (old_dentry->d_inode == new_dentry->d_inode) 29921da177e4SLinus Torvalds return 0; 29931da177e4SLinus Torvalds 29941da177e4SLinus Torvalds error = may_delete(old_dir, old_dentry, is_dir); 29951da177e4SLinus Torvalds if (error) 29961da177e4SLinus Torvalds return error; 29971da177e4SLinus Torvalds 29981da177e4SLinus Torvalds if (!new_dentry->d_inode) 2999a95164d9SMiklos Szeredi error = may_create(new_dir, new_dentry); 30001da177e4SLinus Torvalds else 30011da177e4SLinus Torvalds error = may_delete(new_dir, new_dentry, is_dir); 30021da177e4SLinus Torvalds if (error) 30031da177e4SLinus Torvalds return error; 30041da177e4SLinus Torvalds 3005acfa4380SAl Viro if (!old_dir->i_op->rename) 30061da177e4SLinus Torvalds return -EPERM; 30071da177e4SLinus Torvalds 30080eeca283SRobert Love old_name = fsnotify_oldname_init(old_dentry->d_name.name); 30090eeca283SRobert Love 30101da177e4SLinus Torvalds if (is_dir) 30111da177e4SLinus Torvalds error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry); 30121da177e4SLinus Torvalds else 30131da177e4SLinus Torvalds error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry); 3014123df294SAl Viro if (!error) 3015123df294SAl Viro fsnotify_move(old_dir, new_dir, old_name, is_dir, 30165a190ae6SAl Viro new_dentry->d_inode, old_dentry); 30170eeca283SRobert Love fsnotify_oldname_free(old_name); 30180eeca283SRobert Love 30191da177e4SLinus Torvalds return error; 30201da177e4SLinus Torvalds } 30211da177e4SLinus Torvalds 30222e4d0924SHeiko Carstens SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname, 30232e4d0924SHeiko Carstens int, newdfd, const char __user *, newname) 30241da177e4SLinus Torvalds { 30251da177e4SLinus Torvalds struct dentry *old_dir, *new_dir; 30261da177e4SLinus Torvalds struct dentry *old_dentry, *new_dentry; 30271da177e4SLinus Torvalds struct dentry *trap; 30281da177e4SLinus Torvalds struct nameidata oldnd, newnd; 30292ad94ae6SAl Viro char *from; 30302ad94ae6SAl Viro char *to; 30312ad94ae6SAl Viro int error; 30321da177e4SLinus Torvalds 30332ad94ae6SAl Viro error = user_path_parent(olddfd, oldname, &oldnd, &from); 30341da177e4SLinus Torvalds if (error) 30351da177e4SLinus Torvalds goto exit; 30361da177e4SLinus Torvalds 30372ad94ae6SAl Viro error = user_path_parent(newdfd, newname, &newnd, &to); 30381da177e4SLinus Torvalds if (error) 30391da177e4SLinus Torvalds goto exit1; 30401da177e4SLinus Torvalds 30411da177e4SLinus Torvalds error = -EXDEV; 30424ac91378SJan Blunck if (oldnd.path.mnt != newnd.path.mnt) 30431da177e4SLinus Torvalds goto exit2; 30441da177e4SLinus Torvalds 30454ac91378SJan Blunck old_dir = oldnd.path.dentry; 30461da177e4SLinus Torvalds error = -EBUSY; 30471da177e4SLinus Torvalds if (oldnd.last_type != LAST_NORM) 30481da177e4SLinus Torvalds goto exit2; 30491da177e4SLinus Torvalds 30504ac91378SJan Blunck new_dir = newnd.path.dentry; 30511da177e4SLinus Torvalds if (newnd.last_type != LAST_NORM) 30521da177e4SLinus Torvalds goto exit2; 30531da177e4SLinus Torvalds 30540612d9fbSOGAWA Hirofumi oldnd.flags &= ~LOOKUP_PARENT; 30550612d9fbSOGAWA Hirofumi newnd.flags &= ~LOOKUP_PARENT; 30564e9ed2f8SOGAWA Hirofumi newnd.flags |= LOOKUP_RENAME_TARGET; 30570612d9fbSOGAWA Hirofumi 30581da177e4SLinus Torvalds trap = lock_rename(new_dir, old_dir); 30591da177e4SLinus Torvalds 306049705b77SChristoph Hellwig old_dentry = lookup_hash(&oldnd); 30611da177e4SLinus Torvalds error = PTR_ERR(old_dentry); 30621da177e4SLinus Torvalds if (IS_ERR(old_dentry)) 30631da177e4SLinus Torvalds goto exit3; 30641da177e4SLinus Torvalds /* source must exist */ 30651da177e4SLinus Torvalds error = -ENOENT; 30661da177e4SLinus Torvalds if (!old_dentry->d_inode) 30671da177e4SLinus Torvalds goto exit4; 30681da177e4SLinus Torvalds /* unless the source is a directory trailing slashes give -ENOTDIR */ 30691da177e4SLinus Torvalds if (!S_ISDIR(old_dentry->d_inode->i_mode)) { 30701da177e4SLinus Torvalds error = -ENOTDIR; 30711da177e4SLinus Torvalds if (oldnd.last.name[oldnd.last.len]) 30721da177e4SLinus Torvalds goto exit4; 30731da177e4SLinus Torvalds if (newnd.last.name[newnd.last.len]) 30741da177e4SLinus Torvalds goto exit4; 30751da177e4SLinus Torvalds } 30761da177e4SLinus Torvalds /* source should not be ancestor of target */ 30771da177e4SLinus Torvalds error = -EINVAL; 30781da177e4SLinus Torvalds if (old_dentry == trap) 30791da177e4SLinus Torvalds goto exit4; 308049705b77SChristoph Hellwig new_dentry = lookup_hash(&newnd); 30811da177e4SLinus Torvalds error = PTR_ERR(new_dentry); 30821da177e4SLinus Torvalds if (IS_ERR(new_dentry)) 30831da177e4SLinus Torvalds goto exit4; 30841da177e4SLinus Torvalds /* target should not be an ancestor of source */ 30851da177e4SLinus Torvalds error = -ENOTEMPTY; 30861da177e4SLinus Torvalds if (new_dentry == trap) 30871da177e4SLinus Torvalds goto exit5; 30881da177e4SLinus Torvalds 30899079b1ebSDave Hansen error = mnt_want_write(oldnd.path.mnt); 30909079b1ebSDave Hansen if (error) 30919079b1ebSDave Hansen goto exit5; 3092be6d3e56SKentaro Takeda error = security_path_rename(&oldnd.path, old_dentry, 3093be6d3e56SKentaro Takeda &newnd.path, new_dentry); 3094be6d3e56SKentaro Takeda if (error) 3095be6d3e56SKentaro Takeda goto exit6; 30961da177e4SLinus Torvalds error = vfs_rename(old_dir->d_inode, old_dentry, 30971da177e4SLinus Torvalds new_dir->d_inode, new_dentry); 3098be6d3e56SKentaro Takeda exit6: 30999079b1ebSDave Hansen mnt_drop_write(oldnd.path.mnt); 31001da177e4SLinus Torvalds exit5: 31011da177e4SLinus Torvalds dput(new_dentry); 31021da177e4SLinus Torvalds exit4: 31031da177e4SLinus Torvalds dput(old_dentry); 31041da177e4SLinus Torvalds exit3: 31051da177e4SLinus Torvalds unlock_rename(new_dir, old_dir); 31061da177e4SLinus Torvalds exit2: 31071d957f9bSJan Blunck path_put(&newnd.path); 31082ad94ae6SAl Viro putname(to); 31091da177e4SLinus Torvalds exit1: 31101d957f9bSJan Blunck path_put(&oldnd.path); 31111da177e4SLinus Torvalds putname(from); 31122ad94ae6SAl Viro exit: 31131da177e4SLinus Torvalds return error; 31141da177e4SLinus Torvalds } 31151da177e4SLinus Torvalds 3116a26eab24SHeiko Carstens SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname) 31175590ff0dSUlrich Drepper { 31185590ff0dSUlrich Drepper return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname); 31195590ff0dSUlrich Drepper } 31205590ff0dSUlrich Drepper 31211da177e4SLinus Torvalds int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link) 31221da177e4SLinus Torvalds { 31231da177e4SLinus Torvalds int len; 31241da177e4SLinus Torvalds 31251da177e4SLinus Torvalds len = PTR_ERR(link); 31261da177e4SLinus Torvalds if (IS_ERR(link)) 31271da177e4SLinus Torvalds goto out; 31281da177e4SLinus Torvalds 31291da177e4SLinus Torvalds len = strlen(link); 31301da177e4SLinus Torvalds if (len > (unsigned) buflen) 31311da177e4SLinus Torvalds len = buflen; 31321da177e4SLinus Torvalds if (copy_to_user(buffer, link, len)) 31331da177e4SLinus Torvalds len = -EFAULT; 31341da177e4SLinus Torvalds out: 31351da177e4SLinus Torvalds return len; 31361da177e4SLinus Torvalds } 31371da177e4SLinus Torvalds 31381da177e4SLinus Torvalds /* 31391da177e4SLinus Torvalds * A helper for ->readlink(). This should be used *ONLY* for symlinks that 31401da177e4SLinus Torvalds * have ->follow_link() touching nd only in nd_set_link(). Using (or not 31411da177e4SLinus Torvalds * using) it for any given inode is up to filesystem. 31421da177e4SLinus Torvalds */ 31431da177e4SLinus Torvalds int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen) 31441da177e4SLinus Torvalds { 31451da177e4SLinus Torvalds struct nameidata nd; 3146cc314eefSLinus Torvalds void *cookie; 3147694a1764SMarcin Slusarz int res; 3148cc314eefSLinus Torvalds 31491da177e4SLinus Torvalds nd.depth = 0; 3150cc314eefSLinus Torvalds cookie = dentry->d_inode->i_op->follow_link(dentry, &nd); 3151694a1764SMarcin Slusarz if (IS_ERR(cookie)) 3152694a1764SMarcin Slusarz return PTR_ERR(cookie); 3153694a1764SMarcin Slusarz 3154694a1764SMarcin Slusarz res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd)); 31551da177e4SLinus Torvalds if (dentry->d_inode->i_op->put_link) 3156cc314eefSLinus Torvalds dentry->d_inode->i_op->put_link(dentry, &nd, cookie); 3157694a1764SMarcin Slusarz return res; 31581da177e4SLinus Torvalds } 31591da177e4SLinus Torvalds 31601da177e4SLinus Torvalds int vfs_follow_link(struct nameidata *nd, const char *link) 31611da177e4SLinus Torvalds { 31621da177e4SLinus Torvalds return __vfs_follow_link(nd, link); 31631da177e4SLinus Torvalds } 31641da177e4SLinus Torvalds 31651da177e4SLinus Torvalds /* get the link contents into pagecache */ 31661da177e4SLinus Torvalds static char *page_getlink(struct dentry * dentry, struct page **ppage) 31671da177e4SLinus Torvalds { 3168ebd09abbSDuane Griffin char *kaddr; 31691da177e4SLinus Torvalds struct page *page; 31701da177e4SLinus Torvalds struct address_space *mapping = dentry->d_inode->i_mapping; 3171090d2b18SPekka Enberg page = read_mapping_page(mapping, 0, NULL); 31721da177e4SLinus Torvalds if (IS_ERR(page)) 31736fe6900eSNick Piggin return (char*)page; 31741da177e4SLinus Torvalds *ppage = page; 3175ebd09abbSDuane Griffin kaddr = kmap(page); 3176ebd09abbSDuane Griffin nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1); 3177ebd09abbSDuane Griffin return kaddr; 31781da177e4SLinus Torvalds } 31791da177e4SLinus Torvalds 31801da177e4SLinus Torvalds int page_readlink(struct dentry *dentry, char __user *buffer, int buflen) 31811da177e4SLinus Torvalds { 31821da177e4SLinus Torvalds struct page *page = NULL; 31831da177e4SLinus Torvalds char *s = page_getlink(dentry, &page); 31841da177e4SLinus Torvalds int res = vfs_readlink(dentry,buffer,buflen,s); 31851da177e4SLinus Torvalds if (page) { 31861da177e4SLinus Torvalds kunmap(page); 31871da177e4SLinus Torvalds page_cache_release(page); 31881da177e4SLinus Torvalds } 31891da177e4SLinus Torvalds return res; 31901da177e4SLinus Torvalds } 31911da177e4SLinus Torvalds 3192cc314eefSLinus Torvalds void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd) 31931da177e4SLinus Torvalds { 3194cc314eefSLinus Torvalds struct page *page = NULL; 31951da177e4SLinus Torvalds nd_set_link(nd, page_getlink(dentry, &page)); 3196cc314eefSLinus Torvalds return page; 31971da177e4SLinus Torvalds } 31981da177e4SLinus Torvalds 3199cc314eefSLinus Torvalds void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie) 32001da177e4SLinus Torvalds { 3201cc314eefSLinus Torvalds struct page *page = cookie; 3202cc314eefSLinus Torvalds 3203cc314eefSLinus Torvalds if (page) { 32041da177e4SLinus Torvalds kunmap(page); 32051da177e4SLinus Torvalds page_cache_release(page); 32061da177e4SLinus Torvalds } 32071da177e4SLinus Torvalds } 32081da177e4SLinus Torvalds 320954566b2cSNick Piggin /* 321054566b2cSNick Piggin * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS 321154566b2cSNick Piggin */ 321254566b2cSNick Piggin int __page_symlink(struct inode *inode, const char *symname, int len, int nofs) 32131da177e4SLinus Torvalds { 32141da177e4SLinus Torvalds struct address_space *mapping = inode->i_mapping; 32150adb25d2SKirill Korotaev struct page *page; 3216afddba49SNick Piggin void *fsdata; 3217beb497abSDmitriy Monakhov int err; 32181da177e4SLinus Torvalds char *kaddr; 321954566b2cSNick Piggin unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE; 322054566b2cSNick Piggin if (nofs) 322154566b2cSNick Piggin flags |= AOP_FLAG_NOFS; 32221da177e4SLinus Torvalds 32237e53cac4SNeilBrown retry: 3224afddba49SNick Piggin err = pagecache_write_begin(NULL, mapping, 0, len-1, 322554566b2cSNick Piggin flags, &page, &fsdata); 32261da177e4SLinus Torvalds if (err) 3227afddba49SNick Piggin goto fail; 3228afddba49SNick Piggin 32291da177e4SLinus Torvalds kaddr = kmap_atomic(page, KM_USER0); 32301da177e4SLinus Torvalds memcpy(kaddr, symname, len-1); 32311da177e4SLinus Torvalds kunmap_atomic(kaddr, KM_USER0); 3232afddba49SNick Piggin 3233afddba49SNick Piggin err = pagecache_write_end(NULL, mapping, 0, len-1, len-1, 3234afddba49SNick Piggin page, fsdata); 32351da177e4SLinus Torvalds if (err < 0) 32361da177e4SLinus Torvalds goto fail; 3237afddba49SNick Piggin if (err < len-1) 3238afddba49SNick Piggin goto retry; 3239afddba49SNick Piggin 32401da177e4SLinus Torvalds mark_inode_dirty(inode); 32411da177e4SLinus Torvalds return 0; 32421da177e4SLinus Torvalds fail: 32431da177e4SLinus Torvalds return err; 32441da177e4SLinus Torvalds } 32451da177e4SLinus Torvalds 32460adb25d2SKirill Korotaev int page_symlink(struct inode *inode, const char *symname, int len) 32470adb25d2SKirill Korotaev { 32480adb25d2SKirill Korotaev return __page_symlink(inode, symname, len, 324954566b2cSNick Piggin !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS)); 32500adb25d2SKirill Korotaev } 32510adb25d2SKirill Korotaev 325292e1d5beSArjan van de Ven const struct inode_operations page_symlink_inode_operations = { 32531da177e4SLinus Torvalds .readlink = generic_readlink, 32541da177e4SLinus Torvalds .follow_link = page_follow_link_light, 32551da177e4SLinus Torvalds .put_link = page_put_link, 32561da177e4SLinus Torvalds }; 32571da177e4SLinus Torvalds 32582d8f3038SAl Viro EXPORT_SYMBOL(user_path_at); 3259cc53ce53SDavid Howells EXPORT_SYMBOL(follow_down_one); 32601da177e4SLinus Torvalds EXPORT_SYMBOL(follow_down); 32611da177e4SLinus Torvalds EXPORT_SYMBOL(follow_up); 32621da177e4SLinus Torvalds EXPORT_SYMBOL(get_write_access); /* binfmt_aout */ 32631da177e4SLinus Torvalds EXPORT_SYMBOL(getname); 32641da177e4SLinus Torvalds EXPORT_SYMBOL(lock_rename); 32651da177e4SLinus Torvalds EXPORT_SYMBOL(lookup_one_len); 32661da177e4SLinus Torvalds EXPORT_SYMBOL(page_follow_link_light); 32671da177e4SLinus Torvalds EXPORT_SYMBOL(page_put_link); 32681da177e4SLinus Torvalds EXPORT_SYMBOL(page_readlink); 32690adb25d2SKirill Korotaev EXPORT_SYMBOL(__page_symlink); 32701da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink); 32711da177e4SLinus Torvalds EXPORT_SYMBOL(page_symlink_inode_operations); 3272c9c6cac0SAl Viro EXPORT_SYMBOL(kern_path_parent); 3273d1811465SAl Viro EXPORT_SYMBOL(kern_path); 327416f18200SJosef 'Jeff' Sipek EXPORT_SYMBOL(vfs_path_lookup); 3275f419a2e3SAl Viro EXPORT_SYMBOL(inode_permission); 32761da177e4SLinus Torvalds EXPORT_SYMBOL(unlock_rename); 32771da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_create); 32781da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_follow_link); 32791da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_link); 32801da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mkdir); 32811da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_mknod); 32821da177e4SLinus Torvalds EXPORT_SYMBOL(generic_permission); 32831da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_readlink); 32841da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rename); 32851da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_rmdir); 32861da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_symlink); 32871da177e4SLinus Torvalds EXPORT_SYMBOL(vfs_unlink); 32881da177e4SLinus Torvalds EXPORT_SYMBOL(dentry_unhash); 32891da177e4SLinus Torvalds EXPORT_SYMBOL(generic_readlink); 3290