Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/namei.c |
| 3 | * |
| 4 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 5 | */ |
| 6 | |
| 7 | /* |
| 8 | * Some corrections by tytso. |
| 9 | */ |
| 10 | |
| 11 | /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname |
| 12 | * lookup logic. |
| 13 | */ |
| 14 | /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture. |
| 15 | */ |
| 16 | |
| 17 | #include <linux/init.h> |
Paul Gortmaker | 630d9c4 | 2011-11-16 23:57:37 -0500 | [diff] [blame] | 18 | #include <linux/export.h> |
David S. Miller | 91cd1b0 | 2012-05-23 20:12:50 -0700 | [diff] [blame] | 19 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/slab.h> |
| 21 | #include <linux/fs.h> |
| 22 | #include <linux/namei.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/pagemap.h> |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 24 | #include <linux/fsnotify.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/personality.h> |
| 26 | #include <linux/security.h> |
Mimi Zohar | 6146f0d | 2009-02-04 09:06:57 -0500 | [diff] [blame] | 27 | #include <linux/ima.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/syscalls.h> |
| 29 | #include <linux/mount.h> |
| 30 | #include <linux/audit.h> |
Randy Dunlap | 16f7e0f | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 31 | #include <linux/capability.h> |
Trond Myklebust | 834f2a4 | 2005-10-18 14:20:16 -0700 | [diff] [blame] | 32 | #include <linux/file.h> |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 33 | #include <linux/fcntl.h> |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 34 | #include <linux/device_cgroup.h> |
Al Viro | 5ad4e53 | 2009-03-29 19:50:06 -0400 | [diff] [blame] | 35 | #include <linux/fs_struct.h> |
Linus Torvalds | e77819e | 2011-07-22 19:30:19 -0700 | [diff] [blame] | 36 | #include <linux/posix_acl.h> |
Linus Torvalds | 07e997d | 2014-09-13 11:30:10 -0700 | [diff] [blame] | 37 | #include <linux/hash.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <asm/uaccess.h> |
| 39 | |
Eric Paris | e81e3f4 | 2009-12-04 15:47:36 -0500 | [diff] [blame] | 40 | #include "internal.h" |
Al Viro | c710536 | 2011-11-24 18:22:03 -0500 | [diff] [blame] | 41 | #include "mount.h" |
Eric Paris | e81e3f4 | 2009-12-04 15:47:36 -0500 | [diff] [blame] | 42 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | /* [Feb-1997 T. Schoebel-Theuer] |
| 44 | * Fundamental changes in the pathname lookup mechanisms (namei) |
| 45 | * were necessary because of omirr. The reason is that omirr needs |
| 46 | * to know the _real_ pathname, not the user-supplied one, in case |
| 47 | * of symlinks (and also when transname replacements occur). |
| 48 | * |
| 49 | * The new code replaces the old recursive symlink resolution with |
| 50 | * an iterative one (in case of non-nested symlink chains). It does |
| 51 | * this with calls to <fs>_follow_link(). |
| 52 | * As a side effect, dir_namei(), _namei() and follow_link() are now |
| 53 | * replaced with a single function lookup_dentry() that can handle all |
| 54 | * the special cases of the former code. |
| 55 | * |
| 56 | * With the new dcache, the pathname is stored at each inode, at least as |
| 57 | * long as the refcount of the inode is positive. As a side effect, the |
| 58 | * size of the dcache depends on the inode cache and thus is dynamic. |
| 59 | * |
| 60 | * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink |
| 61 | * resolution to correspond with current state of the code. |
| 62 | * |
| 63 | * Note that the symlink resolution is not *completely* iterative. |
| 64 | * There is still a significant amount of tail- and mid- recursion in |
| 65 | * the algorithm. Also, note that <fs>_readlink() is not used in |
| 66 | * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink() |
| 67 | * may return different results than <fs>_follow_link(). Many virtual |
| 68 | * filesystems (including /proc) exhibit this behavior. |
| 69 | */ |
| 70 | |
| 71 | /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation: |
| 72 | * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL |
| 73 | * and the name already exists in form of a symlink, try to create the new |
| 74 | * name indicated by the symlink. The old code always complained that the |
| 75 | * name already exists, due to not following the symlink even if its target |
| 76 | * is nonexistent. The new semantics affects also mknod() and link() when |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 77 | * the name is a symlink pointing to a non-existent name. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | * |
| 79 | * I don't know which semantics is the right one, since I have no access |
| 80 | * to standards. But I found by trial that HP-UX 9.0 has the full "new" |
| 81 | * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the |
| 82 | * "old" one. Personally, I think the new semantics is much more logical. |
| 83 | * Note that "ln old new" where "new" is a symlink pointing to a non-existing |
| 84 | * file does succeed in both HP-UX and SunOs, but not in Solaris |
| 85 | * and in the old Linux semantics. |
| 86 | */ |
| 87 | |
| 88 | /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink |
| 89 | * semantics. See the comments in "open_namei" and "do_link" below. |
| 90 | * |
| 91 | * [10-Sep-98 Alan Modra] Another symlink change. |
| 92 | */ |
| 93 | |
| 94 | /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks: |
| 95 | * inside the path - always follow. |
| 96 | * in the last component in creation/removal/renaming - never follow. |
| 97 | * if LOOKUP_FOLLOW passed - follow. |
| 98 | * if the pathname has trailing slashes - follow. |
| 99 | * otherwise - don't follow. |
| 100 | * (applied in that order). |
| 101 | * |
| 102 | * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT |
| 103 | * restored for 2.4. This is the last surviving part of old 4.2BSD bug. |
| 104 | * During the 2.4 we need to fix the userland stuff depending on it - |
| 105 | * hopefully we will be able to get rid of that wart in 2.5. So far only |
| 106 | * XEmacs seems to be relying on it... |
| 107 | */ |
| 108 | /* |
| 109 | * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland) |
Arjan van de Ven | a11f3a0 | 2006-03-23 03:00:33 -0800 | [diff] [blame] | 110 | * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | * any extra contention... |
| 112 | */ |
| 113 | |
| 114 | /* In order to reduce some races, while at the same time doing additional |
| 115 | * checking and hopefully speeding things up, we copy filenames to the |
| 116 | * kernel data space before using them.. |
| 117 | * |
| 118 | * POSIX.1 2.4: an empty pathname is invalid (ENOENT). |
| 119 | * PATH_MAX includes the nul terminator --RR. |
| 120 | */ |
Jeff Layton | 9cee5ca | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 121 | void final_putname(struct filename *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | { |
Jeff Layton | 869724e | 2012-10-10 16:43:13 -0400 | [diff] [blame] | 123 | if (name->separate) { |
| 124 | __putname(name->name); |
| 125 | kfree(name); |
| 126 | } else { |
| 127 | __putname(name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Jeff Layton | 869724e | 2012-10-10 16:43:13 -0400 | [diff] [blame] | 131 | #define EMBEDDED_NAME_MAX (PATH_MAX - sizeof(struct filename)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | |
Jeff Layton | 9cee5ca | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 133 | static struct filename * |
| 134 | getname_flags(const char __user *filename, int flags, int *empty) |
| 135 | { |
| 136 | struct filename *result, *err; |
Linus Torvalds | 256a73d | 2012-04-28 14:38:32 -0700 | [diff] [blame] | 137 | int len; |
Jeff Layton | 869724e | 2012-10-10 16:43:13 -0400 | [diff] [blame] | 138 | long max; |
| 139 | char *kname; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | |
Jeff Layton | 869724e | 2012-10-10 16:43:13 -0400 | [diff] [blame] | 141 | result = __getname(); |
Linus Torvalds | 256a73d | 2012-04-28 14:38:32 -0700 | [diff] [blame] | 142 | if (unlikely(!result)) |
Eric Paris | 4043cde | 2012-01-03 14:23:08 -0500 | [diff] [blame] | 143 | return ERR_PTR(-ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | |
Jeff Layton | 869724e | 2012-10-10 16:43:13 -0400 | [diff] [blame] | 145 | /* |
| 146 | * First, try to embed the struct filename inside the names_cache |
| 147 | * allocation |
| 148 | */ |
| 149 | kname = (char *)result + sizeof(*result); |
Jeff Layton | 9cee5ca | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 150 | result->name = kname; |
Jeff Layton | 869724e | 2012-10-10 16:43:13 -0400 | [diff] [blame] | 151 | result->separate = false; |
| 152 | max = EMBEDDED_NAME_MAX; |
| 153 | |
| 154 | recopy: |
| 155 | len = strncpy_from_user(kname, filename, max); |
Jeff Layton | 9cee5ca | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 156 | if (unlikely(len < 0)) { |
| 157 | err = ERR_PTR(len); |
Linus Torvalds | 256a73d | 2012-04-28 14:38:32 -0700 | [diff] [blame] | 158 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | } |
Linus Torvalds | 256a73d | 2012-04-28 14:38:32 -0700 | [diff] [blame] | 160 | |
Jeff Layton | 869724e | 2012-10-10 16:43:13 -0400 | [diff] [blame] | 161 | /* |
| 162 | * Uh-oh. We have a name that's approaching PATH_MAX. Allocate a |
| 163 | * separate struct filename so we can dedicate the entire |
| 164 | * names_cache allocation for the pathname, and re-do the copy from |
| 165 | * userland. |
| 166 | */ |
| 167 | if (len == EMBEDDED_NAME_MAX && max == EMBEDDED_NAME_MAX) { |
| 168 | kname = (char *)result; |
| 169 | |
| 170 | result = kzalloc(sizeof(*result), GFP_KERNEL); |
| 171 | if (!result) { |
| 172 | err = ERR_PTR(-ENOMEM); |
| 173 | result = (struct filename *)kname; |
| 174 | goto error; |
| 175 | } |
| 176 | result->name = kname; |
| 177 | result->separate = true; |
| 178 | max = PATH_MAX; |
| 179 | goto recopy; |
| 180 | } |
| 181 | |
Linus Torvalds | 256a73d | 2012-04-28 14:38:32 -0700 | [diff] [blame] | 182 | /* The empty path is special. */ |
| 183 | if (unlikely(!len)) { |
| 184 | if (empty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | *empty = 1; |
Linus Torvalds | 256a73d | 2012-04-28 14:38:32 -0700 | [diff] [blame] | 186 | err = ERR_PTR(-ENOENT); |
| 187 | if (!(flags & LOOKUP_EMPTY)) |
| 188 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | } |
Linus Torvalds | 256a73d | 2012-04-28 14:38:32 -0700 | [diff] [blame] | 190 | |
| 191 | err = ERR_PTR(-ENAMETOOLONG); |
Jeff Layton | 869724e | 2012-10-10 16:43:13 -0400 | [diff] [blame] | 192 | if (unlikely(len >= PATH_MAX)) |
| 193 | goto error; |
| 194 | |
| 195 | result->uptr = filename; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | audit_getname(result); |
| 197 | return result; |
Linus Torvalds | 256a73d | 2012-04-28 14:38:32 -0700 | [diff] [blame] | 198 | |
| 199 | error: |
Jeff Layton | 869724e | 2012-10-10 16:43:13 -0400 | [diff] [blame] | 200 | final_putname(result); |
Linus Torvalds | 256a73d | 2012-04-28 14:38:32 -0700 | [diff] [blame] | 201 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Jeff Layton | 9cee5ca | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 204 | struct filename * |
| 205 | getname(const char __user * filename) |
Al Viro | f52e0c1 | 2011-03-14 18:56:51 -0400 | [diff] [blame] | 206 | { |
Linus Torvalds | f7493e5 | 2012-03-22 16:10:40 -0700 | [diff] [blame] | 207 | return getname_flags(filename, 0, NULL); |
Al Viro | f52e0c1 | 2011-03-14 18:56:51 -0400 | [diff] [blame] | 208 | } |
Jeff Layton | 9cee5ca | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 209 | EXPORT_SYMBOL(getname); |
Al Viro | f52e0c1 | 2011-03-14 18:56:51 -0400 | [diff] [blame] | 210 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | #ifdef CONFIG_AUDITSYSCALL |
Jeff Layton | 9cee5ca | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 212 | void putname(struct filename *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | { |
Al Viro | 5ac3a9c | 2006-07-16 06:38:45 -0400 | [diff] [blame] | 214 | if (unlikely(!audit_dummy_context())) |
Jeff Layton | 9cee5ca | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 215 | return audit_putname(name); |
| 216 | final_putname(name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | #endif |
| 219 | |
Linus Torvalds | e77819e | 2011-07-22 19:30:19 -0700 | [diff] [blame] | 220 | static int check_acl(struct inode *inode, int mask) |
| 221 | { |
Linus Torvalds | 84635d6 | 2011-07-25 22:47:03 -0700 | [diff] [blame] | 222 | #ifdef CONFIG_FS_POSIX_ACL |
Linus Torvalds | e77819e | 2011-07-22 19:30:19 -0700 | [diff] [blame] | 223 | struct posix_acl *acl; |
| 224 | |
Linus Torvalds | e77819e | 2011-07-22 19:30:19 -0700 | [diff] [blame] | 225 | if (mask & MAY_NOT_BLOCK) { |
Al Viro | 3567866 | 2011-08-02 21:32:13 -0400 | [diff] [blame] | 226 | acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS); |
| 227 | if (!acl) |
Linus Torvalds | e77819e | 2011-07-22 19:30:19 -0700 | [diff] [blame] | 228 | return -EAGAIN; |
Al Viro | 3567866 | 2011-08-02 21:32:13 -0400 | [diff] [blame] | 229 | /* no ->get_acl() calls in RCU mode... */ |
| 230 | if (acl == ACL_NOT_CACHED) |
| 231 | return -ECHILD; |
Ari Savolainen | 206b1d0 | 2011-08-06 19:43:07 +0300 | [diff] [blame] | 232 | return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK); |
Linus Torvalds | e77819e | 2011-07-22 19:30:19 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | acl = get_cached_acl(inode, ACL_TYPE_ACCESS); |
| 236 | |
| 237 | /* |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 238 | * A filesystem can force a ACL callback by just never filling the |
| 239 | * ACL cache. But normally you'd fill the cache either at inode |
| 240 | * instantiation time, or on the first ->get_acl call. |
Linus Torvalds | e77819e | 2011-07-22 19:30:19 -0700 | [diff] [blame] | 241 | * |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 242 | * If the filesystem doesn't have a get_acl() function at all, we'll |
| 243 | * just create the negative cache entry. |
Linus Torvalds | e77819e | 2011-07-22 19:30:19 -0700 | [diff] [blame] | 244 | */ |
| 245 | if (acl == ACL_NOT_CACHED) { |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 246 | if (inode->i_op->get_acl) { |
| 247 | acl = inode->i_op->get_acl(inode, ACL_TYPE_ACCESS); |
| 248 | if (IS_ERR(acl)) |
| 249 | return PTR_ERR(acl); |
| 250 | } else { |
| 251 | set_cached_acl(inode, ACL_TYPE_ACCESS, NULL); |
| 252 | return -EAGAIN; |
| 253 | } |
Linus Torvalds | e77819e | 2011-07-22 19:30:19 -0700 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | if (acl) { |
| 257 | int error = posix_acl_permission(inode, acl, mask); |
| 258 | posix_acl_release(acl); |
| 259 | return error; |
| 260 | } |
Linus Torvalds | 84635d6 | 2011-07-25 22:47:03 -0700 | [diff] [blame] | 261 | #endif |
Linus Torvalds | e77819e | 2011-07-22 19:30:19 -0700 | [diff] [blame] | 262 | |
| 263 | return -EAGAIN; |
| 264 | } |
| 265 | |
Linus Torvalds | 5909ccaa | 2009-08-28 11:51:25 -0700 | [diff] [blame] | 266 | /* |
Andreas Gruenbacher | 948409c | 2011-10-23 23:13:33 +0530 | [diff] [blame] | 267 | * This does the basic permission checking |
Linus Torvalds | 5909ccaa | 2009-08-28 11:51:25 -0700 | [diff] [blame] | 268 | */ |
Al Viro | 7e40145 | 2011-06-20 19:12:17 -0400 | [diff] [blame] | 269 | static int acl_permission_check(struct inode *inode, int mask) |
Linus Torvalds | 5909ccaa | 2009-08-28 11:51:25 -0700 | [diff] [blame] | 270 | { |
Linus Torvalds | 26cf46b | 2011-05-13 11:51:01 -0700 | [diff] [blame] | 271 | unsigned int mode = inode->i_mode; |
Linus Torvalds | 5909ccaa | 2009-08-28 11:51:25 -0700 | [diff] [blame] | 272 | |
Linus Torvalds | 14067ff | 2011-07-25 19:55:52 -0700 | [diff] [blame] | 273 | if (likely(current_fsuid() == inode->i_uid)) |
Linus Torvalds | 5909ccaa | 2009-08-28 11:51:25 -0700 | [diff] [blame] | 274 | mode >>= 6; |
| 275 | else { |
Linus Torvalds | e77819e | 2011-07-22 19:30:19 -0700 | [diff] [blame] | 276 | if (IS_POSIXACL(inode) && (mode & S_IRWXG)) { |
Al Viro | 7e40145 | 2011-06-20 19:12:17 -0400 | [diff] [blame] | 277 | int error = check_acl(inode, mask); |
Nick Piggin | b74c79e | 2011-01-07 17:49:58 +1100 | [diff] [blame] | 278 | if (error != -EAGAIN) |
| 279 | return error; |
Linus Torvalds | 5909ccaa | 2009-08-28 11:51:25 -0700 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | if (in_group_p(inode->i_gid)) |
| 283 | mode >>= 3; |
| 284 | } |
| 285 | |
| 286 | /* |
| 287 | * If the DACs are ok we don't need any capability check. |
| 288 | */ |
Al Viro | 9c2c703 | 2011-06-20 19:06:22 -0400 | [diff] [blame] | 289 | if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0) |
Linus Torvalds | 5909ccaa | 2009-08-28 11:51:25 -0700 | [diff] [blame] | 290 | return 0; |
| 291 | return -EACCES; |
| 292 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | |
| 294 | /** |
Nick Piggin | b74c79e | 2011-01-07 17:49:58 +1100 | [diff] [blame] | 295 | * generic_permission - check for access rights on a Posix-like filesystem |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | * @inode: inode to check access rights for |
Andreas Gruenbacher | 8fd90c8 | 2011-10-23 23:13:30 +0530 | [diff] [blame] | 297 | * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | * |
| 299 | * Used to check for read/write/execute permissions on a file. |
| 300 | * We use "fsuid" for this, letting us set arbitrary permissions |
| 301 | * for filesystem access without changing the "normal" uids which |
Nick Piggin | b74c79e | 2011-01-07 17:49:58 +1100 | [diff] [blame] | 302 | * are used for other things. |
| 303 | * |
| 304 | * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk |
| 305 | * request cannot be satisfied (eg. requires blocking or too much complexity). |
| 306 | * It would then be called again in ref-walk mode. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | */ |
Al Viro | 2830ba7 | 2011-06-20 19:16:29 -0400 | [diff] [blame] | 308 | int generic_permission(struct inode *inode, int mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | { |
Linus Torvalds | 5909ccaa | 2009-08-28 11:51:25 -0700 | [diff] [blame] | 310 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | |
| 312 | /* |
Andreas Gruenbacher | 948409c | 2011-10-23 23:13:33 +0530 | [diff] [blame] | 313 | * Do the basic permission checks. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | */ |
Al Viro | 7e40145 | 2011-06-20 19:12:17 -0400 | [diff] [blame] | 315 | ret = acl_permission_check(inode, mask); |
Linus Torvalds | 5909ccaa | 2009-08-28 11:51:25 -0700 | [diff] [blame] | 316 | if (ret != -EACCES) |
| 317 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | |
Al Viro | d594e7e | 2011-06-20 19:55:42 -0400 | [diff] [blame] | 319 | if (S_ISDIR(inode->i_mode)) { |
| 320 | /* DACs are overridable for directories */ |
Eric W. Biederman | d6814e26 | 2011-11-14 16:24:06 -0800 | [diff] [blame] | 321 | if (inode_capable(inode, CAP_DAC_OVERRIDE)) |
Al Viro | d594e7e | 2011-06-20 19:55:42 -0400 | [diff] [blame] | 322 | return 0; |
| 323 | if (!(mask & MAY_WRITE)) |
Eric W. Biederman | d6814e26 | 2011-11-14 16:24:06 -0800 | [diff] [blame] | 324 | if (inode_capable(inode, CAP_DAC_READ_SEARCH)) |
Al Viro | d594e7e | 2011-06-20 19:55:42 -0400 | [diff] [blame] | 325 | return 0; |
| 326 | return -EACCES; |
| 327 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | /* |
| 329 | * Read/write DACs are always overridable. |
Al Viro | d594e7e | 2011-06-20 19:55:42 -0400 | [diff] [blame] | 330 | * Executable DACs are overridable when there is |
| 331 | * at least one exec bit set. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | */ |
Al Viro | d594e7e | 2011-06-20 19:55:42 -0400 | [diff] [blame] | 333 | if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO)) |
Eric W. Biederman | d6814e26 | 2011-11-14 16:24:06 -0800 | [diff] [blame] | 334 | if (inode_capable(inode, CAP_DAC_OVERRIDE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | return 0; |
| 336 | |
| 337 | /* |
| 338 | * Searching includes executable on directories, else just read. |
| 339 | */ |
Serge E. Hallyn | 7ea6600 | 2009-12-29 14:50:19 -0600 | [diff] [blame] | 340 | mask &= MAY_READ | MAY_WRITE | MAY_EXEC; |
Al Viro | d594e7e | 2011-06-20 19:55:42 -0400 | [diff] [blame] | 341 | if (mask == MAY_READ) |
Eric W. Biederman | d6814e26 | 2011-11-14 16:24:06 -0800 | [diff] [blame] | 342 | if (inode_capable(inode, CAP_DAC_READ_SEARCH)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | return 0; |
| 344 | |
| 345 | return -EACCES; |
| 346 | } |
| 347 | |
Linus Torvalds | 3ddcd05 | 2011-08-06 22:45:50 -0700 | [diff] [blame] | 348 | /* |
| 349 | * We _really_ want to just do "generic_permission()" without |
| 350 | * even looking at the inode->i_op values. So we keep a cache |
| 351 | * flag in inode->i_opflags, that says "this has not special |
| 352 | * permission function, use the fast case". |
| 353 | */ |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 354 | static inline int do_inode_permission(struct vfsmount *mnt, struct inode *inode, int mask) |
Linus Torvalds | 3ddcd05 | 2011-08-06 22:45:50 -0700 | [diff] [blame] | 355 | { |
| 356 | if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) { |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 357 | if (likely(mnt && inode->i_op->permission2)) |
| 358 | return inode->i_op->permission2(mnt, inode, mask); |
Linus Torvalds | 3ddcd05 | 2011-08-06 22:45:50 -0700 | [diff] [blame] | 359 | if (likely(inode->i_op->permission)) |
| 360 | return inode->i_op->permission(inode, mask); |
| 361 | |
| 362 | /* This gets set once for the inode lifetime */ |
| 363 | spin_lock(&inode->i_lock); |
| 364 | inode->i_opflags |= IOP_FASTPERM; |
| 365 | spin_unlock(&inode->i_lock); |
| 366 | } |
| 367 | return generic_permission(inode, mask); |
| 368 | } |
| 369 | |
Christoph Hellwig | cb23beb | 2008-10-24 09:59:29 +0200 | [diff] [blame] | 370 | /** |
| 371 | * inode_permission - check for access rights to a given inode |
| 372 | * @inode: inode to check permission on |
Andreas Gruenbacher | 8fd90c8 | 2011-10-23 23:13:30 +0530 | [diff] [blame] | 373 | * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...) |
Christoph Hellwig | cb23beb | 2008-10-24 09:59:29 +0200 | [diff] [blame] | 374 | * |
| 375 | * Used to check for read/write/execute permissions on an inode. |
| 376 | * We use "fsuid" for this, letting us set arbitrary permissions |
| 377 | * for filesystem access without changing the "normal" uids which |
| 378 | * are used for other things. |
Andreas Gruenbacher | 948409c | 2011-10-23 23:13:33 +0530 | [diff] [blame] | 379 | * |
| 380 | * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask. |
Christoph Hellwig | cb23beb | 2008-10-24 09:59:29 +0200 | [diff] [blame] | 381 | */ |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 382 | int inode_permission2(struct vfsmount *mnt, struct inode *inode, int mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | { |
Al Viro | e6305c4 | 2008-07-15 21:03:57 -0400 | [diff] [blame] | 384 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | |
Linus Torvalds | 3ddcd05 | 2011-08-06 22:45:50 -0700 | [diff] [blame] | 386 | if (unlikely(mask & MAY_WRITE)) { |
Miklos Szeredi | 22590e4 | 2007-10-16 23:27:08 -0700 | [diff] [blame] | 387 | umode_t mode = inode->i_mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | |
| 389 | /* |
| 390 | * Nobody gets write access to a read-only fs. |
| 391 | */ |
| 392 | if (IS_RDONLY(inode) && |
| 393 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) |
| 394 | return -EROFS; |
| 395 | |
| 396 | /* |
| 397 | * Nobody gets write access to an immutable file. |
| 398 | */ |
| 399 | if (IS_IMMUTABLE(inode)) |
| 400 | return -EACCES; |
| 401 | } |
| 402 | |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 403 | retval = do_inode_permission(mnt, inode, mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | if (retval) |
| 405 | return retval; |
| 406 | |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 407 | retval = devcgroup_inode_permission(inode, mask); |
| 408 | if (retval) |
| 409 | return retval; |
| 410 | |
Eric Paris | d09ca73 | 2010-07-23 11:43:57 -0400 | [diff] [blame] | 411 | return security_inode_permission(inode, mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | } |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 413 | EXPORT_SYMBOL(inode_permission2); |
| 414 | |
| 415 | int inode_permission(struct inode *inode, int mask) |
| 416 | { |
| 417 | return inode_permission2(NULL, inode, mask); |
| 418 | } |
| 419 | EXPORT_SYMBOL(inode_permission); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | |
Al Viro | f4d6ff8 | 2011-06-19 13:14:21 -0400 | [diff] [blame] | 421 | /** |
Jan Blunck | 5dd784d | 2008-02-14 19:34:38 -0800 | [diff] [blame] | 422 | * path_get - get a reference to a path |
| 423 | * @path: path to get the reference to |
| 424 | * |
| 425 | * Given a path increment the reference count to the dentry and the vfsmount. |
| 426 | */ |
| 427 | void path_get(struct path *path) |
| 428 | { |
| 429 | mntget(path->mnt); |
| 430 | dget(path->dentry); |
| 431 | } |
| 432 | EXPORT_SYMBOL(path_get); |
| 433 | |
| 434 | /** |
Jan Blunck | 1d957f9 | 2008-02-14 19:34:35 -0800 | [diff] [blame] | 435 | * path_put - put a reference to a path |
| 436 | * @path: path to put the reference to |
| 437 | * |
| 438 | * Given a path decrement the reference count to the dentry and the vfsmount. |
| 439 | */ |
| 440 | void path_put(struct path *path) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | { |
Jan Blunck | 1d957f9 | 2008-02-14 19:34:35 -0800 | [diff] [blame] | 442 | dput(path->dentry); |
| 443 | mntput(path->mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | } |
Jan Blunck | 1d957f9 | 2008-02-14 19:34:35 -0800 | [diff] [blame] | 445 | EXPORT_SYMBOL(path_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | |
Eric W. Biederman | bf2e9fa | 2015-08-15 20:27:13 -0500 | [diff] [blame] | 447 | /** |
| 448 | * path_connected - Verify that a path->dentry is below path->mnt.mnt_root |
| 449 | * @path: nameidate to verify |
| 450 | * |
| 451 | * Rename can sometimes move a file or directory outside of a bind |
| 452 | * mount, path_connected allows those cases to be detected. |
| 453 | */ |
| 454 | static bool path_connected(const struct path *path) |
| 455 | { |
| 456 | struct vfsmount *mnt = path->mnt; |
| 457 | |
| 458 | /* Only bind mounts can have disconnected paths */ |
| 459 | if (mnt->mnt_root == mnt->mnt_sb->s_root) |
| 460 | return true; |
| 461 | |
| 462 | return is_subdir(path->dentry, mnt->mnt_root); |
| 463 | } |
| 464 | |
Al Viro | 19660af | 2011-03-25 10:32:48 -0400 | [diff] [blame] | 465 | /* |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 466 | * Path walking has 2 modes, rcu-walk and ref-walk (see |
Al Viro | 19660af | 2011-03-25 10:32:48 -0400 | [diff] [blame] | 467 | * Documentation/filesystems/path-lookup.txt). In situations when we can't |
| 468 | * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab |
| 469 | * normal reference counts on dentries and vfsmounts to transition to rcu-walk |
| 470 | * mode. Refcounts are grabbed at the last known good point before rcu-walk |
| 471 | * got stuck, so ref-walk may continue from there. If this is not successful |
| 472 | * (eg. a seqcount has changed), then failure is returned and it's up to caller |
| 473 | * to restart the path walk from the beginning in ref-walk mode. |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 474 | */ |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 475 | |
Al Viro | 9caac00 | 2012-07-18 20:43:19 +0400 | [diff] [blame] | 476 | static inline void lock_rcu_walk(void) |
| 477 | { |
| 478 | br_read_lock(&vfsmount_lock); |
| 479 | rcu_read_lock(); |
| 480 | } |
| 481 | |
| 482 | static inline void unlock_rcu_walk(void) |
| 483 | { |
| 484 | rcu_read_unlock(); |
| 485 | br_read_unlock(&vfsmount_lock); |
| 486 | } |
| 487 | |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 488 | /** |
Al Viro | 19660af | 2011-03-25 10:32:48 -0400 | [diff] [blame] | 489 | * unlazy_walk - try to switch to ref-walk mode. |
| 490 | * @nd: nameidata pathwalk data |
| 491 | * @dentry: child of nd->path.dentry or NULL |
Randy Dunlap | 3919162 | 2011-01-08 19:36:21 -0800 | [diff] [blame] | 492 | * Returns: 0 on success, -ECHILD on failure |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 493 | * |
Al Viro | 19660af | 2011-03-25 10:32:48 -0400 | [diff] [blame] | 494 | * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry |
| 495 | * for ref-walk mode. @dentry must be a path found by a do_lookup call on |
| 496 | * @nd or NULL. Must be called from rcu-walk context. |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 497 | */ |
Al Viro | 19660af | 2011-03-25 10:32:48 -0400 | [diff] [blame] | 498 | static int unlazy_walk(struct nameidata *nd, struct dentry *dentry) |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 499 | { |
| 500 | struct fs_struct *fs = current->fs; |
| 501 | struct dentry *parent = nd->path.dentry; |
Al Viro | 5b6ca02 | 2011-03-09 23:04:47 -0500 | [diff] [blame] | 502 | int want_root = 0; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 503 | |
| 504 | BUG_ON(!(nd->flags & LOOKUP_RCU)); |
Al Viro | 5b6ca02 | 2011-03-09 23:04:47 -0500 | [diff] [blame] | 505 | if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) { |
| 506 | want_root = 1; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 507 | spin_lock(&fs->lock); |
| 508 | if (nd->root.mnt != fs->root.mnt || |
| 509 | nd->root.dentry != fs->root.dentry) |
| 510 | goto err_root; |
| 511 | } |
| 512 | spin_lock(&parent->d_lock); |
Al Viro | 19660af | 2011-03-25 10:32:48 -0400 | [diff] [blame] | 513 | if (!dentry) { |
| 514 | if (!__d_rcu_to_refcount(parent, nd->seq)) |
| 515 | goto err_parent; |
| 516 | BUG_ON(nd->inode != parent->d_inode); |
| 517 | } else { |
Al Viro | 94c0d4e | 2011-07-12 21:40:23 -0400 | [diff] [blame] | 518 | if (dentry->d_parent != parent) |
| 519 | goto err_parent; |
Al Viro | 19660af | 2011-03-25 10:32:48 -0400 | [diff] [blame] | 520 | spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED); |
| 521 | if (!__d_rcu_to_refcount(dentry, nd->seq)) |
| 522 | goto err_child; |
| 523 | /* |
| 524 | * If the sequence check on the child dentry passed, then |
| 525 | * the child has not been removed from its parent. This |
| 526 | * means the parent dentry must be valid and able to take |
| 527 | * a reference at this point. |
| 528 | */ |
| 529 | BUG_ON(!IS_ROOT(dentry) && dentry->d_parent != parent); |
| 530 | BUG_ON(!parent->d_count); |
| 531 | parent->d_count++; |
| 532 | spin_unlock(&dentry->d_lock); |
| 533 | } |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 534 | spin_unlock(&parent->d_lock); |
Al Viro | 5b6ca02 | 2011-03-09 23:04:47 -0500 | [diff] [blame] | 535 | if (want_root) { |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 536 | path_get(&nd->root); |
| 537 | spin_unlock(&fs->lock); |
| 538 | } |
| 539 | mntget(nd->path.mnt); |
| 540 | |
Al Viro | 9caac00 | 2012-07-18 20:43:19 +0400 | [diff] [blame] | 541 | unlock_rcu_walk(); |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 542 | nd->flags &= ~LOOKUP_RCU; |
| 543 | return 0; |
Al Viro | 19660af | 2011-03-25 10:32:48 -0400 | [diff] [blame] | 544 | |
| 545 | err_child: |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 546 | spin_unlock(&dentry->d_lock); |
Al Viro | 19660af | 2011-03-25 10:32:48 -0400 | [diff] [blame] | 547 | err_parent: |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 548 | spin_unlock(&parent->d_lock); |
| 549 | err_root: |
Al Viro | 5b6ca02 | 2011-03-09 23:04:47 -0500 | [diff] [blame] | 550 | if (want_root) |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 551 | spin_unlock(&fs->lock); |
| 552 | return -ECHILD; |
| 553 | } |
| 554 | |
Al Viro | 28ca326 | 2012-06-10 16:10:59 -0400 | [diff] [blame] | 555 | static inline int d_revalidate(struct dentry *dentry, unsigned int flags) |
Trond Myklebust | 834f2a4 | 2005-10-18 14:20:16 -0700 | [diff] [blame] | 556 | { |
Al Viro | 28ca326 | 2012-06-10 16:10:59 -0400 | [diff] [blame] | 557 | return dentry->d_op->d_revalidate(dentry, flags); |
Nick Piggin | 34286d6 | 2011-01-07 17:49:57 +1100 | [diff] [blame] | 558 | } |
| 559 | |
Al Viro | 9f1fafe | 2011-03-25 11:00:12 -0400 | [diff] [blame] | 560 | /** |
| 561 | * complete_walk - successful completion of path walk |
| 562 | * @nd: pointer nameidata |
Jeff Layton | 39159de | 2009-12-07 12:01:50 -0500 | [diff] [blame] | 563 | * |
Al Viro | 9f1fafe | 2011-03-25 11:00:12 -0400 | [diff] [blame] | 564 | * If we had been in RCU mode, drop out of it and legitimize nd->path. |
| 565 | * Revalidate the final result, unless we'd already done that during |
| 566 | * the path walk or the filesystem doesn't ask for it. Return 0 on |
| 567 | * success, -error on failure. In case of failure caller does not |
| 568 | * need to drop nd->path. |
Jeff Layton | 39159de | 2009-12-07 12:01:50 -0500 | [diff] [blame] | 569 | */ |
Al Viro | 9f1fafe | 2011-03-25 11:00:12 -0400 | [diff] [blame] | 570 | static int complete_walk(struct nameidata *nd) |
Jeff Layton | 39159de | 2009-12-07 12:01:50 -0500 | [diff] [blame] | 571 | { |
Al Viro | 16c2cd7 | 2011-02-22 15:50:10 -0500 | [diff] [blame] | 572 | struct dentry *dentry = nd->path.dentry; |
Jeff Layton | 39159de | 2009-12-07 12:01:50 -0500 | [diff] [blame] | 573 | int status; |
Jeff Layton | 39159de | 2009-12-07 12:01:50 -0500 | [diff] [blame] | 574 | |
Al Viro | 9f1fafe | 2011-03-25 11:00:12 -0400 | [diff] [blame] | 575 | if (nd->flags & LOOKUP_RCU) { |
| 576 | nd->flags &= ~LOOKUP_RCU; |
| 577 | if (!(nd->flags & LOOKUP_ROOT)) |
| 578 | nd->root.mnt = NULL; |
| 579 | spin_lock(&dentry->d_lock); |
| 580 | if (unlikely(!__d_rcu_to_refcount(dentry, nd->seq))) { |
| 581 | spin_unlock(&dentry->d_lock); |
Al Viro | 9caac00 | 2012-07-18 20:43:19 +0400 | [diff] [blame] | 582 | unlock_rcu_walk(); |
Al Viro | 9f1fafe | 2011-03-25 11:00:12 -0400 | [diff] [blame] | 583 | return -ECHILD; |
| 584 | } |
| 585 | BUG_ON(nd->inode != dentry->d_inode); |
| 586 | spin_unlock(&dentry->d_lock); |
| 587 | mntget(nd->path.mnt); |
Al Viro | 9caac00 | 2012-07-18 20:43:19 +0400 | [diff] [blame] | 588 | unlock_rcu_walk(); |
Al Viro | 9f1fafe | 2011-03-25 11:00:12 -0400 | [diff] [blame] | 589 | } |
| 590 | |
Al Viro | 16c2cd7 | 2011-02-22 15:50:10 -0500 | [diff] [blame] | 591 | if (likely(!(nd->flags & LOOKUP_JUMPED))) |
Jeff Layton | 39159de | 2009-12-07 12:01:50 -0500 | [diff] [blame] | 592 | return 0; |
| 593 | |
Al Viro | 16c2cd7 | 2011-02-22 15:50:10 -0500 | [diff] [blame] | 594 | if (likely(!(dentry->d_flags & DCACHE_OP_REVALIDATE))) |
| 595 | return 0; |
| 596 | |
| 597 | if (likely(!(dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT))) |
| 598 | return 0; |
| 599 | |
| 600 | /* Note: we do not d_invalidate() */ |
Al Viro | 28ca326 | 2012-06-10 16:10:59 -0400 | [diff] [blame] | 601 | status = d_revalidate(dentry, nd->flags); |
Jeff Layton | 39159de | 2009-12-07 12:01:50 -0500 | [diff] [blame] | 602 | if (status > 0) |
| 603 | return 0; |
| 604 | |
Al Viro | 16c2cd7 | 2011-02-22 15:50:10 -0500 | [diff] [blame] | 605 | if (!status) |
Jeff Layton | 39159de | 2009-12-07 12:01:50 -0500 | [diff] [blame] | 606 | status = -ESTALE; |
Al Viro | 16c2cd7 | 2011-02-22 15:50:10 -0500 | [diff] [blame] | 607 | |
Al Viro | 9f1fafe | 2011-03-25 11:00:12 -0400 | [diff] [blame] | 608 | path_put(&nd->path); |
Jeff Layton | 39159de | 2009-12-07 12:01:50 -0500 | [diff] [blame] | 609 | return status; |
| 610 | } |
| 611 | |
Al Viro | 2a73787 | 2009-04-07 11:49:53 -0400 | [diff] [blame] | 612 | static __always_inline void set_root(struct nameidata *nd) |
| 613 | { |
Al Viro | 589a49f | 2014-09-13 21:55:46 -0400 | [diff] [blame] | 614 | get_fs_root(current->fs, &nd->root); |
Al Viro | 2a73787 | 2009-04-07 11:49:53 -0400 | [diff] [blame] | 615 | } |
| 616 | |
Al Viro | 6de88d7 | 2009-08-09 01:41:57 +0400 | [diff] [blame] | 617 | static int link_path_walk(const char *, struct nameidata *); |
| 618 | |
Al Viro | 589a49f | 2014-09-13 21:55:46 -0400 | [diff] [blame] | 619 | static __always_inline unsigned set_root_rcu(struct nameidata *nd) |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 620 | { |
Al Viro | 589a49f | 2014-09-13 21:55:46 -0400 | [diff] [blame] | 621 | struct fs_struct *fs = current->fs; |
| 622 | unsigned seq, res; |
Nick Piggin | c28cc36 | 2011-01-07 17:49:53 +1100 | [diff] [blame] | 623 | |
Al Viro | 589a49f | 2014-09-13 21:55:46 -0400 | [diff] [blame] | 624 | do { |
| 625 | seq = read_seqcount_begin(&fs->seq); |
| 626 | nd->root = fs->root; |
| 627 | res = __read_seqcount_begin(&nd->root.dentry->d_seq); |
| 628 | } while (read_seqcount_retry(&fs->seq, seq)); |
| 629 | return res; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 630 | } |
| 631 | |
Arjan van de Ven | f166235 | 2006-01-14 13:21:31 -0800 | [diff] [blame] | 632 | static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | { |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 634 | int ret; |
| 635 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | if (IS_ERR(link)) |
| 637 | goto fail; |
| 638 | |
| 639 | if (*link == '/') { |
Al Viro | 9221426 | 2014-09-13 21:55:46 -0400 | [diff] [blame] | 640 | if (!nd->root.mnt) |
| 641 | set_root(nd); |
Jan Blunck | 1d957f9 | 2008-02-14 19:34:35 -0800 | [diff] [blame] | 642 | path_put(&nd->path); |
Al Viro | 2a73787 | 2009-04-07 11:49:53 -0400 | [diff] [blame] | 643 | nd->path = nd->root; |
| 644 | path_get(&nd->root); |
Al Viro | 16c2cd7 | 2011-02-22 15:50:10 -0500 | [diff] [blame] | 645 | nd->flags |= LOOKUP_JUMPED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | } |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 647 | nd->inode = nd->path.dentry->d_inode; |
Christoph Hellwig | b4091d5 | 2008-11-05 15:07:21 +0100 | [diff] [blame] | 648 | |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 649 | ret = link_path_walk(link, nd); |
| 650 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | fail: |
Jan Blunck | 1d957f9 | 2008-02-14 19:34:35 -0800 | [diff] [blame] | 652 | path_put(&nd->path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | return PTR_ERR(link); |
| 654 | } |
| 655 | |
Jan Blunck | 1d957f9 | 2008-02-14 19:34:35 -0800 | [diff] [blame] | 656 | static void path_put_conditional(struct path *path, struct nameidata *nd) |
Miklos Szeredi | 09dd17d | 2005-09-06 15:18:21 -0700 | [diff] [blame] | 657 | { |
| 658 | dput(path->dentry); |
Jan Blunck | 4ac9137 | 2008-02-14 19:34:32 -0800 | [diff] [blame] | 659 | if (path->mnt != nd->path.mnt) |
Miklos Szeredi | 09dd17d | 2005-09-06 15:18:21 -0700 | [diff] [blame] | 660 | mntput(path->mnt); |
| 661 | } |
| 662 | |
Nick Piggin | 7b9337a | 2011-01-14 08:42:43 +0000 | [diff] [blame] | 663 | static inline void path_to_nameidata(const struct path *path, |
| 664 | struct nameidata *nd) |
Miklos Szeredi | 09dd17d | 2005-09-06 15:18:21 -0700 | [diff] [blame] | 665 | { |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 666 | if (!(nd->flags & LOOKUP_RCU)) { |
| 667 | dput(nd->path.dentry); |
| 668 | if (nd->path.mnt != path->mnt) |
| 669 | mntput(nd->path.mnt); |
Huang Shijie | 9a22968 | 2010-04-02 17:37:13 +0800 | [diff] [blame] | 670 | } |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 671 | nd->path.mnt = path->mnt; |
Jan Blunck | 4ac9137 | 2008-02-14 19:34:32 -0800 | [diff] [blame] | 672 | nd->path.dentry = path->dentry; |
Miklos Szeredi | 09dd17d | 2005-09-06 15:18:21 -0700 | [diff] [blame] | 673 | } |
| 674 | |
Christoph Hellwig | fac2602 | 2012-06-18 10:47:04 -0400 | [diff] [blame] | 675 | /* |
| 676 | * Helper to directly jump to a known parsed path from ->follow_link, |
| 677 | * caller must have taken a reference to path beforehand. |
| 678 | */ |
| 679 | void nd_jump_link(struct nameidata *nd, struct path *path) |
| 680 | { |
| 681 | path_put(&nd->path); |
| 682 | |
| 683 | nd->path = *path; |
| 684 | nd->inode = nd->path.dentry->d_inode; |
| 685 | nd->flags |= LOOKUP_JUMPED; |
Christoph Hellwig | fac2602 | 2012-06-18 10:47:04 -0400 | [diff] [blame] | 686 | } |
| 687 | |
Al Viro | 574197e | 2011-03-14 22:20:34 -0400 | [diff] [blame] | 688 | static inline void put_link(struct nameidata *nd, struct path *link, void *cookie) |
| 689 | { |
| 690 | struct inode *inode = link->dentry->d_inode; |
Al Viro | 2c4d739 | 2012-06-10 04:15:17 -0400 | [diff] [blame] | 691 | if (inode->i_op->put_link) |
Al Viro | 574197e | 2011-03-14 22:20:34 -0400 | [diff] [blame] | 692 | inode->i_op->put_link(link->dentry, nd, cookie); |
| 693 | path_put(link); |
| 694 | } |
| 695 | |
Linus Torvalds | fcf25fb | 2012-10-26 10:05:07 -0700 | [diff] [blame] | 696 | int sysctl_protected_symlinks __read_mostly = 0; |
| 697 | int sysctl_protected_hardlinks __read_mostly = 0; |
Salvatore Mesoraca | 063d55a | 2018-08-23 17:00:35 -0700 | [diff] [blame] | 698 | int sysctl_protected_fifos __read_mostly; |
| 699 | int sysctl_protected_regular __read_mostly; |
Kees Cook | 5092e9c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 700 | |
| 701 | /** |
| 702 | * may_follow_link - Check symlink following for unsafe situations |
| 703 | * @link: The path of the symlink |
| 704 | * |
| 705 | * In the case of the sysctl_protected_symlinks sysctl being enabled, |
| 706 | * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is |
| 707 | * in a sticky world-writable directory. This is to protect privileged |
| 708 | * processes from failing races against path names that may change out |
| 709 | * from under them by way of other users creating malicious symlinks. |
| 710 | * It will permit symlinks to be followed only when outside a sticky |
| 711 | * world-writable directory, or when the uid of the symlink and follower |
| 712 | * match, or when the directory owner matches the symlink's owner. |
| 713 | * |
| 714 | * Returns 0 if following the symlink is allowed, -ve on error. |
| 715 | */ |
| 716 | static inline int may_follow_link(struct path *link, struct nameidata *nd) |
| 717 | { |
| 718 | const struct inode *inode; |
| 719 | const struct inode *parent; |
| 720 | |
| 721 | if (!sysctl_protected_symlinks) |
| 722 | return 0; |
| 723 | |
| 724 | /* Allowed if owner and follower match. */ |
| 725 | inode = link->dentry->d_inode; |
| 726 | if (current_cred()->fsuid == inode->i_uid) |
| 727 | return 0; |
| 728 | |
| 729 | /* Allowed if parent directory not sticky and world-writable. */ |
| 730 | parent = nd->path.dentry->d_inode; |
| 731 | if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH)) |
| 732 | return 0; |
| 733 | |
| 734 | /* Allowed if parent directory and link owner match. */ |
| 735 | if (parent->i_uid == inode->i_uid) |
| 736 | return 0; |
| 737 | |
Sasha Levin | d4def9b | 2012-10-04 19:56:40 -0400 | [diff] [blame] | 738 | audit_log_link_denied("follow_link", link); |
Kees Cook | 5092e9c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 739 | path_put_conditional(link, nd); |
| 740 | path_put(&nd->path); |
| 741 | return -EACCES; |
| 742 | } |
| 743 | |
| 744 | /** |
| 745 | * safe_hardlink_source - Check for safe hardlink conditions |
| 746 | * @inode: the source inode to hardlink from |
| 747 | * |
| 748 | * Return false if at least one of the following conditions: |
| 749 | * - inode is not a regular file |
| 750 | * - inode is setuid |
| 751 | * - inode is setgid and group-exec |
| 752 | * - access failure for read and write |
| 753 | * |
| 754 | * Otherwise returns true. |
| 755 | */ |
| 756 | static bool safe_hardlink_source(struct inode *inode) |
| 757 | { |
| 758 | umode_t mode = inode->i_mode; |
| 759 | |
| 760 | /* Special files should not get pinned to the filesystem. */ |
| 761 | if (!S_ISREG(mode)) |
| 762 | return false; |
| 763 | |
| 764 | /* Setuid files should not get pinned to the filesystem. */ |
| 765 | if (mode & S_ISUID) |
| 766 | return false; |
| 767 | |
| 768 | /* Executable setgid files should not get pinned to the filesystem. */ |
| 769 | if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) |
| 770 | return false; |
| 771 | |
| 772 | /* Hardlinking to unreadable or unwritable sources is dangerous. */ |
| 773 | if (inode_permission(inode, MAY_READ | MAY_WRITE)) |
| 774 | return false; |
| 775 | |
| 776 | return true; |
| 777 | } |
| 778 | |
| 779 | /** |
| 780 | * may_linkat - Check permissions for creating a hardlink |
| 781 | * @link: the source to hardlink from |
| 782 | * |
| 783 | * Block hardlink when all of: |
| 784 | * - sysctl_protected_hardlinks enabled |
| 785 | * - fsuid does not match inode |
| 786 | * - hardlink source is unsafe (see safe_hardlink_source() above) |
| 787 | * - not CAP_FOWNER |
| 788 | * |
| 789 | * Returns 0 if successful, -ve on error. |
| 790 | */ |
| 791 | static int may_linkat(struct path *link) |
| 792 | { |
| 793 | const struct cred *cred; |
| 794 | struct inode *inode; |
| 795 | |
| 796 | if (!sysctl_protected_hardlinks) |
| 797 | return 0; |
| 798 | |
| 799 | cred = current_cred(); |
| 800 | inode = link->dentry->d_inode; |
| 801 | |
| 802 | /* Source inode owner (or CAP_FOWNER) can hardlink all they like, |
| 803 | * otherwise, it must be a safe source. |
| 804 | */ |
| 805 | if (cred->fsuid == inode->i_uid || safe_hardlink_source(inode) || |
| 806 | capable(CAP_FOWNER)) |
| 807 | return 0; |
| 808 | |
Kees Cook | ccdd090 | 2012-07-25 17:29:08 -0700 | [diff] [blame] | 809 | audit_log_link_denied("linkat", link); |
Kees Cook | 5092e9c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 810 | return -EPERM; |
| 811 | } |
| 812 | |
Salvatore Mesoraca | 063d55a | 2018-08-23 17:00:35 -0700 | [diff] [blame] | 813 | /** |
| 814 | * may_create_in_sticky - Check whether an O_CREAT open in a sticky directory |
| 815 | * should be allowed, or not, on files that already |
| 816 | * exist. |
Al Viro | a243411 | 2020-01-26 09:29:34 -0500 | [diff] [blame] | 817 | * @dir_mode: mode bits of directory |
| 818 | * @dir_uid: owner of directory |
Salvatore Mesoraca | 063d55a | 2018-08-23 17:00:35 -0700 | [diff] [blame] | 819 | * @inode: the inode of the file to open |
| 820 | * |
| 821 | * Block an O_CREAT open of a FIFO (or a regular file) when: |
| 822 | * - sysctl_protected_fifos (or sysctl_protected_regular) is enabled |
| 823 | * - the file already exists |
| 824 | * - we are in a sticky directory |
| 825 | * - we don't own the file |
| 826 | * - the owner of the directory doesn't own the file |
| 827 | * - the directory is world writable |
| 828 | * If the sysctl_protected_fifos (or sysctl_protected_regular) is set to 2 |
| 829 | * the directory doesn't have to be world writable: being group writable will |
| 830 | * be enough. |
| 831 | * |
| 832 | * Returns 0 if the open is allowed, -ve on error. |
| 833 | */ |
Al Viro | a243411 | 2020-01-26 09:29:34 -0500 | [diff] [blame] | 834 | static int may_create_in_sticky(umode_t dir_mode, kuid_t dir_uid, |
Salvatore Mesoraca | 063d55a | 2018-08-23 17:00:35 -0700 | [diff] [blame] | 835 | struct inode * const inode) |
| 836 | { |
| 837 | if ((!sysctl_protected_fifos && S_ISFIFO(inode->i_mode)) || |
| 838 | (!sysctl_protected_regular && S_ISREG(inode->i_mode)) || |
Al Viro | a243411 | 2020-01-26 09:29:34 -0500 | [diff] [blame] | 839 | likely(!(dir_mode & S_ISVTX)) || |
| 840 | uid_eq(inode->i_uid, dir_uid) || |
Salvatore Mesoraca | 063d55a | 2018-08-23 17:00:35 -0700 | [diff] [blame] | 841 | uid_eq(current_fsuid(), inode->i_uid)) |
| 842 | return 0; |
| 843 | |
Al Viro | a243411 | 2020-01-26 09:29:34 -0500 | [diff] [blame] | 844 | if (likely(dir_mode & 0002) || |
| 845 | (dir_mode & 0020 && |
Salvatore Mesoraca | 063d55a | 2018-08-23 17:00:35 -0700 | [diff] [blame] | 846 | ((sysctl_protected_fifos >= 2 && S_ISFIFO(inode->i_mode)) || |
| 847 | (sysctl_protected_regular >= 2 && S_ISREG(inode->i_mode))))) { |
| 848 | return -EACCES; |
| 849 | } |
| 850 | return 0; |
| 851 | } |
| 852 | |
Al Viro | def4af3 | 2009-12-26 08:37:05 -0500 | [diff] [blame] | 853 | static __always_inline int |
Al Viro | 574197e | 2011-03-14 22:20:34 -0400 | [diff] [blame] | 854 | follow_link(struct path *link, struct nameidata *nd, void **p) |
Ian Kent | 051d381 | 2006-03-27 01:14:53 -0800 | [diff] [blame] | 855 | { |
Nick Piggin | 7b9337a | 2011-01-14 08:42:43 +0000 | [diff] [blame] | 856 | struct dentry *dentry = link->dentry; |
Al Viro | 2c4d739 | 2012-06-10 04:15:17 -0400 | [diff] [blame] | 857 | int error; |
| 858 | char *s; |
Ian Kent | 051d381 | 2006-03-27 01:14:53 -0800 | [diff] [blame] | 859 | |
Al Viro | 844a391 | 2011-02-15 00:38:26 -0500 | [diff] [blame] | 860 | BUG_ON(nd->flags & LOOKUP_RCU); |
| 861 | |
Al Viro | 0e79458 | 2011-03-16 02:45:02 -0400 | [diff] [blame] | 862 | if (link->mnt == nd->path.mnt) |
| 863 | mntget(link->mnt); |
| 864 | |
Al Viro | 2c4d739 | 2012-06-10 04:15:17 -0400 | [diff] [blame] | 865 | error = -ELOOP; |
| 866 | if (unlikely(current->total_link_count >= 40)) |
| 867 | goto out_put_nd_path; |
| 868 | |
Al Viro | 574197e | 2011-03-14 22:20:34 -0400 | [diff] [blame] | 869 | cond_resched(); |
| 870 | current->total_link_count++; |
| 871 | |
Al Viro | 68ac123 | 2012-03-15 08:21:57 -0400 | [diff] [blame] | 872 | touch_atime(link); |
Ian Kent | 051d381 | 2006-03-27 01:14:53 -0800 | [diff] [blame] | 873 | nd_set_link(nd, NULL); |
| 874 | |
Al Viro | 36f3b4f | 2011-02-22 21:24:38 -0500 | [diff] [blame] | 875 | error = security_inode_follow_link(link->dentry, nd); |
Al Viro | 2c4d739 | 2012-06-10 04:15:17 -0400 | [diff] [blame] | 876 | if (error) |
| 877 | goto out_put_nd_path; |
Al Viro | 36f3b4f | 2011-02-22 21:24:38 -0500 | [diff] [blame] | 878 | |
Al Viro | 86acdca1 | 2009-12-22 23:45:11 -0500 | [diff] [blame] | 879 | nd->last_type = LAST_BIND; |
Al Viro | def4af3 | 2009-12-26 08:37:05 -0500 | [diff] [blame] | 880 | *p = dentry->d_inode->i_op->follow_link(dentry, nd); |
| 881 | error = PTR_ERR(*p); |
Al Viro | 2c4d739 | 2012-06-10 04:15:17 -0400 | [diff] [blame] | 882 | if (IS_ERR(*p)) |
Christoph Hellwig | da51d39 | 2012-06-18 10:47:03 -0400 | [diff] [blame] | 883 | goto out_put_nd_path; |
Al Viro | 2c4d739 | 2012-06-10 04:15:17 -0400 | [diff] [blame] | 884 | |
| 885 | error = 0; |
| 886 | s = nd_get_link(nd); |
| 887 | if (s) { |
| 888 | error = __vfs_follow_link(nd, s); |
Christoph Hellwig | fac2602 | 2012-06-18 10:47:04 -0400 | [diff] [blame] | 889 | if (unlikely(error)) |
| 890 | put_link(nd, link, *p); |
Ian Kent | 051d381 | 2006-03-27 01:14:53 -0800 | [diff] [blame] | 891 | } |
Al Viro | 2c4d739 | 2012-06-10 04:15:17 -0400 | [diff] [blame] | 892 | |
| 893 | return error; |
| 894 | |
| 895 | out_put_nd_path: |
Arnd Bergmann | e1031ba | 2012-10-11 13:20:00 +0000 | [diff] [blame] | 896 | *p = NULL; |
Al Viro | 2c4d739 | 2012-06-10 04:15:17 -0400 | [diff] [blame] | 897 | path_put(&nd->path); |
Al Viro | 2c4d739 | 2012-06-10 04:15:17 -0400 | [diff] [blame] | 898 | path_put(link); |
Ian Kent | 051d381 | 2006-03-27 01:14:53 -0800 | [diff] [blame] | 899 | return error; |
| 900 | } |
| 901 | |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 902 | static int follow_up_rcu(struct path *path) |
| 903 | { |
Al Viro | 0714a53 | 2011-11-24 22:19:58 -0500 | [diff] [blame] | 904 | struct mount *mnt = real_mount(path->mnt); |
| 905 | struct mount *parent; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 906 | struct dentry *mountpoint; |
| 907 | |
Al Viro | 0714a53 | 2011-11-24 22:19:58 -0500 | [diff] [blame] | 908 | parent = mnt->mnt_parent; |
| 909 | if (&parent->mnt == path->mnt) |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 910 | return 0; |
Al Viro | a73324d | 2011-11-24 22:25:07 -0500 | [diff] [blame] | 911 | mountpoint = mnt->mnt_mountpoint; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 912 | path->dentry = mountpoint; |
Al Viro | 0714a53 | 2011-11-24 22:19:58 -0500 | [diff] [blame] | 913 | path->mnt = &parent->mnt; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 914 | return 1; |
| 915 | } |
| 916 | |
David Howells | 105a0cd | 2012-06-25 12:55:28 +0100 | [diff] [blame] | 917 | /* |
| 918 | * follow_up - Find the mountpoint of path's vfsmount |
| 919 | * |
| 920 | * Given a path, find the mountpoint of its source file system. |
| 921 | * Replace @path with the path of the mountpoint in the parent mount. |
| 922 | * Up is towards /. |
| 923 | * |
| 924 | * Return 1 if we went up a level and 0 if we were already at the |
| 925 | * root. |
| 926 | */ |
Al Viro | bab77eb | 2009-04-18 03:26:48 -0400 | [diff] [blame] | 927 | int follow_up(struct path *path) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | { |
Al Viro | 0714a53 | 2011-11-24 22:19:58 -0500 | [diff] [blame] | 929 | struct mount *mnt = real_mount(path->mnt); |
| 930 | struct mount *parent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | struct dentry *mountpoint; |
Nick Piggin | 99b7db7 | 2010-08-18 04:37:39 +1000 | [diff] [blame] | 932 | |
Andi Kleen | f076950 | 2012-05-08 13:32:02 +0930 | [diff] [blame] | 933 | br_read_lock(&vfsmount_lock); |
Al Viro | 0714a53 | 2011-11-24 22:19:58 -0500 | [diff] [blame] | 934 | parent = mnt->mnt_parent; |
Al Viro | dab53a7 | 2012-07-18 17:32:50 +0400 | [diff] [blame] | 935 | if (parent == mnt) { |
Andi Kleen | f076950 | 2012-05-08 13:32:02 +0930 | [diff] [blame] | 936 | br_read_unlock(&vfsmount_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | return 0; |
| 938 | } |
Al Viro | 0714a53 | 2011-11-24 22:19:58 -0500 | [diff] [blame] | 939 | mntget(&parent->mnt); |
Al Viro | a73324d | 2011-11-24 22:25:07 -0500 | [diff] [blame] | 940 | mountpoint = dget(mnt->mnt_mountpoint); |
Andi Kleen | f076950 | 2012-05-08 13:32:02 +0930 | [diff] [blame] | 941 | br_read_unlock(&vfsmount_lock); |
Al Viro | bab77eb | 2009-04-18 03:26:48 -0400 | [diff] [blame] | 942 | dput(path->dentry); |
| 943 | path->dentry = mountpoint; |
| 944 | mntput(path->mnt); |
Al Viro | 0714a53 | 2011-11-24 22:19:58 -0500 | [diff] [blame] | 945 | path->mnt = &parent->mnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | return 1; |
| 947 | } |
| 948 | |
Nick Piggin | b5c84bf | 2011-01-07 17:49:38 +1100 | [diff] [blame] | 949 | /* |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 950 | * Perform an automount |
| 951 | * - return -EISDIR to tell follow_managed() to stop and return the path we |
| 952 | * were called with. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | */ |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 954 | static int follow_automount(struct path *path, unsigned flags, |
| 955 | bool *need_mntput) |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 956 | { |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 957 | struct vfsmount *mnt; |
David Howells | ea5b778 | 2011-01-14 19:10:03 +0000 | [diff] [blame] | 958 | int err; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 959 | |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 960 | if (!path->dentry->d_op || !path->dentry->d_op->d_automount) |
| 961 | return -EREMOTE; |
Al Viro | 463ffb2 | 2005-06-06 13:36:05 -0700 | [diff] [blame] | 962 | |
Miklos Szeredi | 0ec26fd | 2011-09-05 18:06:26 +0200 | [diff] [blame] | 963 | /* We don't want to mount if someone's just doing a stat - |
| 964 | * unless they're stat'ing a directory and appended a '/' to |
| 965 | * the name. |
| 966 | * |
| 967 | * We do, however, want to mount if someone wants to open or |
| 968 | * create a file of any type under the mountpoint, wants to |
| 969 | * traverse through the mountpoint or wants to open the |
| 970 | * mounted directory. Also, autofs may mark negative dentries |
| 971 | * as being automount points. These will need the attentions |
| 972 | * of the daemon to instantiate them before they can be used. |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 973 | */ |
Miklos Szeredi | 0ec26fd | 2011-09-05 18:06:26 +0200 | [diff] [blame] | 974 | if (!(flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY | |
Linus Torvalds | d94c177 | 2011-09-26 17:44:55 -0700 | [diff] [blame] | 975 | LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) && |
Miklos Szeredi | 0ec26fd | 2011-09-05 18:06:26 +0200 | [diff] [blame] | 976 | path->dentry->d_inode) |
| 977 | return -EISDIR; |
| 978 | |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 979 | current->total_link_count++; |
| 980 | if (current->total_link_count >= 40) |
| 981 | return -ELOOP; |
| 982 | |
| 983 | mnt = path->dentry->d_op->d_automount(path); |
| 984 | if (IS_ERR(mnt)) { |
| 985 | /* |
| 986 | * The filesystem is allowed to return -EISDIR here to indicate |
| 987 | * it doesn't want to automount. For instance, autofs would do |
| 988 | * this so that its userspace daemon can mount on this dentry. |
| 989 | * |
| 990 | * However, we can only permit this if it's a terminal point in |
| 991 | * the path being looked up; if it wasn't then the remainder of |
| 992 | * the path is inaccessible and we should say so. |
| 993 | */ |
Al Viro | 49084c3 | 2011-06-25 21:59:52 -0400 | [diff] [blame] | 994 | if (PTR_ERR(mnt) == -EISDIR && (flags & LOOKUP_PARENT)) |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 995 | return -EREMOTE; |
| 996 | return PTR_ERR(mnt); |
| 997 | } |
David Howells | ea5b778 | 2011-01-14 19:10:03 +0000 | [diff] [blame] | 998 | |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 999 | if (!mnt) /* mount collision */ |
| 1000 | return 0; |
| 1001 | |
Al Viro | 8aef188 | 2011-06-16 15:10:06 +0100 | [diff] [blame] | 1002 | if (!*need_mntput) { |
| 1003 | /* lock_mount() may release path->mnt on error */ |
| 1004 | mntget(path->mnt); |
| 1005 | *need_mntput = true; |
| 1006 | } |
Al Viro | 19a167a | 2011-01-17 01:35:23 -0500 | [diff] [blame] | 1007 | err = finish_automount(mnt, path); |
David Howells | ea5b778 | 2011-01-14 19:10:03 +0000 | [diff] [blame] | 1008 | |
David Howells | ea5b778 | 2011-01-14 19:10:03 +0000 | [diff] [blame] | 1009 | switch (err) { |
| 1010 | case -EBUSY: |
| 1011 | /* Someone else made a mount here whilst we were busy */ |
Al Viro | 19a167a | 2011-01-17 01:35:23 -0500 | [diff] [blame] | 1012 | return 0; |
David Howells | ea5b778 | 2011-01-14 19:10:03 +0000 | [diff] [blame] | 1013 | case 0: |
Al Viro | 8aef188 | 2011-06-16 15:10:06 +0100 | [diff] [blame] | 1014 | path_put(path); |
David Howells | ea5b778 | 2011-01-14 19:10:03 +0000 | [diff] [blame] | 1015 | path->mnt = mnt; |
| 1016 | path->dentry = dget(mnt->mnt_root); |
David Howells | ea5b778 | 2011-01-14 19:10:03 +0000 | [diff] [blame] | 1017 | return 0; |
Al Viro | 19a167a | 2011-01-17 01:35:23 -0500 | [diff] [blame] | 1018 | default: |
| 1019 | return err; |
David Howells | ea5b778 | 2011-01-14 19:10:03 +0000 | [diff] [blame] | 1020 | } |
Al Viro | 19a167a | 2011-01-17 01:35:23 -0500 | [diff] [blame] | 1021 | |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1022 | } |
| 1023 | |
| 1024 | /* |
| 1025 | * Handle a dentry that is managed in some way. |
David Howells | cc53ce5 | 2011-01-14 18:45:26 +0000 | [diff] [blame] | 1026 | * - Flagged for transit management (autofs) |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1027 | * - Flagged as mountpoint |
| 1028 | * - Flagged as automount point |
| 1029 | * |
| 1030 | * This may only be called in refwalk mode. |
| 1031 | * |
| 1032 | * Serialization is taken care of in namespace.c |
| 1033 | */ |
| 1034 | static int follow_managed(struct path *path, unsigned flags) |
| 1035 | { |
Al Viro | 8aef188 | 2011-06-16 15:10:06 +0100 | [diff] [blame] | 1036 | struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */ |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1037 | unsigned managed; |
| 1038 | bool need_mntput = false; |
Al Viro | 8aef188 | 2011-06-16 15:10:06 +0100 | [diff] [blame] | 1039 | int ret = 0; |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1040 | |
| 1041 | /* Given that we're not holding a lock here, we retain the value in a |
| 1042 | * local variable for each dentry as we look at it so that we don't see |
| 1043 | * the components of that value change under us */ |
| 1044 | while (managed = ACCESS_ONCE(path->dentry->d_flags), |
| 1045 | managed &= DCACHE_MANAGED_DENTRY, |
| 1046 | unlikely(managed != 0)) { |
David Howells | cc53ce5 | 2011-01-14 18:45:26 +0000 | [diff] [blame] | 1047 | /* Allow the filesystem to manage the transit without i_mutex |
| 1048 | * being held. */ |
| 1049 | if (managed & DCACHE_MANAGE_TRANSIT) { |
| 1050 | BUG_ON(!path->dentry->d_op); |
| 1051 | BUG_ON(!path->dentry->d_op->d_manage); |
Al Viro | 1aed3e4 | 2011-03-18 09:09:02 -0400 | [diff] [blame] | 1052 | ret = path->dentry->d_op->d_manage(path->dentry, false); |
David Howells | cc53ce5 | 2011-01-14 18:45:26 +0000 | [diff] [blame] | 1053 | if (ret < 0) |
Al Viro | 8aef188 | 2011-06-16 15:10:06 +0100 | [diff] [blame] | 1054 | break; |
David Howells | cc53ce5 | 2011-01-14 18:45:26 +0000 | [diff] [blame] | 1055 | } |
| 1056 | |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1057 | /* Transit to a mounted filesystem. */ |
| 1058 | if (managed & DCACHE_MOUNTED) { |
| 1059 | struct vfsmount *mounted = lookup_mnt(path); |
| 1060 | if (mounted) { |
| 1061 | dput(path->dentry); |
| 1062 | if (need_mntput) |
| 1063 | mntput(path->mnt); |
| 1064 | path->mnt = mounted; |
| 1065 | path->dentry = dget(mounted->mnt_root); |
| 1066 | need_mntput = true; |
| 1067 | continue; |
| 1068 | } |
| 1069 | |
| 1070 | /* Something is mounted on this dentry in another |
| 1071 | * namespace and/or whatever was mounted there in this |
| 1072 | * namespace got unmounted before we managed to get the |
| 1073 | * vfsmount_lock */ |
| 1074 | } |
| 1075 | |
| 1076 | /* Handle an automount point */ |
| 1077 | if (managed & DCACHE_NEED_AUTOMOUNT) { |
| 1078 | ret = follow_automount(path, flags, &need_mntput); |
| 1079 | if (ret < 0) |
Al Viro | 8aef188 | 2011-06-16 15:10:06 +0100 | [diff] [blame] | 1080 | break; |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1081 | continue; |
| 1082 | } |
| 1083 | |
| 1084 | /* We didn't change the current path point */ |
| 1085 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1086 | } |
Al Viro | 8aef188 | 2011-06-16 15:10:06 +0100 | [diff] [blame] | 1087 | |
| 1088 | if (need_mntput && path->mnt == mnt) |
| 1089 | mntput(path->mnt); |
| 1090 | if (ret == -EISDIR) |
| 1091 | ret = 0; |
Al Viro | a3fbbde | 2011-11-07 21:21:26 +0000 | [diff] [blame] | 1092 | return ret < 0 ? ret : need_mntput; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | } |
| 1094 | |
David Howells | cc53ce5 | 2011-01-14 18:45:26 +0000 | [diff] [blame] | 1095 | int follow_down_one(struct path *path) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | { |
| 1097 | struct vfsmount *mounted; |
| 1098 | |
Al Viro | 1c755af | 2009-04-18 14:06:57 -0400 | [diff] [blame] | 1099 | mounted = lookup_mnt(path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | if (mounted) { |
Al Viro | 9393bd0 | 2009-04-18 13:58:15 -0400 | [diff] [blame] | 1101 | dput(path->dentry); |
| 1102 | mntput(path->mnt); |
| 1103 | path->mnt = mounted; |
| 1104 | path->dentry = dget(mounted->mnt_root); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1105 | return 1; |
| 1106 | } |
| 1107 | return 0; |
| 1108 | } |
| 1109 | |
Ian Kent | 62a7375 | 2011-03-25 01:51:02 +0800 | [diff] [blame] | 1110 | static inline bool managed_dentry_might_block(struct dentry *dentry) |
| 1111 | { |
| 1112 | return (dentry->d_flags & DCACHE_MANAGE_TRANSIT && |
| 1113 | dentry->d_op->d_manage(dentry, true) < 0); |
| 1114 | } |
| 1115 | |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1116 | /* |
Al Viro | 287548e | 2011-05-27 06:50:06 -0400 | [diff] [blame] | 1117 | * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if |
| 1118 | * we meet a managed dentry that would need blocking. |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1119 | */ |
| 1120 | static bool __follow_mount_rcu(struct nameidata *nd, struct path *path, |
Al Viro | 287548e | 2011-05-27 06:50:06 -0400 | [diff] [blame] | 1121 | struct inode **inode) |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1122 | { |
Ian Kent | 62a7375 | 2011-03-25 01:51:02 +0800 | [diff] [blame] | 1123 | for (;;) { |
Al Viro | c710536 | 2011-11-24 18:22:03 -0500 | [diff] [blame] | 1124 | struct mount *mounted; |
Ian Kent | 62a7375 | 2011-03-25 01:51:02 +0800 | [diff] [blame] | 1125 | /* |
| 1126 | * Don't forget we might have a non-mountpoint managed dentry |
| 1127 | * that wants to block transit. |
| 1128 | */ |
Al Viro | 287548e | 2011-05-27 06:50:06 -0400 | [diff] [blame] | 1129 | if (unlikely(managed_dentry_might_block(path->dentry))) |
David Howells | ab90911 | 2011-01-14 18:46:51 +0000 | [diff] [blame] | 1130 | return false; |
Ian Kent | 62a7375 | 2011-03-25 01:51:02 +0800 | [diff] [blame] | 1131 | |
| 1132 | if (!d_mountpoint(path->dentry)) |
| 1133 | break; |
| 1134 | |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1135 | mounted = __lookup_mnt(path->mnt, path->dentry, 1); |
| 1136 | if (!mounted) |
| 1137 | break; |
Al Viro | c710536 | 2011-11-24 18:22:03 -0500 | [diff] [blame] | 1138 | path->mnt = &mounted->mnt; |
| 1139 | path->dentry = mounted->mnt.mnt_root; |
Al Viro | a3fbbde | 2011-11-07 21:21:26 +0000 | [diff] [blame] | 1140 | nd->flags |= LOOKUP_JUMPED; |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1141 | nd->seq = read_seqcount_begin(&path->dentry->d_seq); |
Linus Torvalds | 5943026 | 2011-07-18 15:43:29 -0700 | [diff] [blame] | 1142 | /* |
| 1143 | * Update the inode too. We don't need to re-check the |
| 1144 | * dentry sequence number here after this d_inode read, |
| 1145 | * because a mount-point is always pinned. |
| 1146 | */ |
| 1147 | *inode = path->dentry->d_inode; |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1148 | } |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1149 | return true; |
| 1150 | } |
| 1151 | |
Al Viro | dea3937 | 2011-05-27 06:53:39 -0400 | [diff] [blame] | 1152 | static void follow_mount_rcu(struct nameidata *nd) |
Al Viro | 287548e | 2011-05-27 06:50:06 -0400 | [diff] [blame] | 1153 | { |
Al Viro | dea3937 | 2011-05-27 06:53:39 -0400 | [diff] [blame] | 1154 | while (d_mountpoint(nd->path.dentry)) { |
Al Viro | c710536 | 2011-11-24 18:22:03 -0500 | [diff] [blame] | 1155 | struct mount *mounted; |
Al Viro | dea3937 | 2011-05-27 06:53:39 -0400 | [diff] [blame] | 1156 | mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry, 1); |
Al Viro | 287548e | 2011-05-27 06:50:06 -0400 | [diff] [blame] | 1157 | if (!mounted) |
| 1158 | break; |
Al Viro | c710536 | 2011-11-24 18:22:03 -0500 | [diff] [blame] | 1159 | nd->path.mnt = &mounted->mnt; |
| 1160 | nd->path.dentry = mounted->mnt.mnt_root; |
Al Viro | dea3937 | 2011-05-27 06:53:39 -0400 | [diff] [blame] | 1161 | nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); |
Al Viro | 287548e | 2011-05-27 06:50:06 -0400 | [diff] [blame] | 1162 | } |
| 1163 | } |
| 1164 | |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1165 | static int follow_dotdot_rcu(struct nameidata *nd) |
| 1166 | { |
Al Viro | 589a49f | 2014-09-13 21:55:46 -0400 | [diff] [blame] | 1167 | if (!nd->root.mnt) |
| 1168 | set_root_rcu(nd); |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1169 | |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1170 | while (1) { |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1171 | if (nd->path.dentry == nd->root.dentry && |
| 1172 | nd->path.mnt == nd->root.mnt) { |
| 1173 | break; |
| 1174 | } |
| 1175 | if (nd->path.dentry != nd->path.mnt->mnt_root) { |
| 1176 | struct dentry *old = nd->path.dentry; |
| 1177 | struct dentry *parent = old->d_parent; |
| 1178 | unsigned seq; |
| 1179 | |
| 1180 | seq = read_seqcount_begin(&parent->d_seq); |
| 1181 | if (read_seqcount_retry(&old->d_seq, nd->seq)) |
Al Viro | ef7562d | 2011-03-04 14:35:59 -0500 | [diff] [blame] | 1182 | goto failed; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1183 | nd->path.dentry = parent; |
| 1184 | nd->seq = seq; |
Eric W. Biederman | bf2e9fa | 2015-08-15 20:27:13 -0500 | [diff] [blame] | 1185 | if (unlikely(!path_connected(&nd->path))) |
| 1186 | goto failed; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1187 | break; |
| 1188 | } |
| 1189 | if (!follow_up_rcu(&nd->path)) |
| 1190 | break; |
| 1191 | nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1192 | } |
Al Viro | dea3937 | 2011-05-27 06:53:39 -0400 | [diff] [blame] | 1193 | follow_mount_rcu(nd); |
| 1194 | nd->inode = nd->path.dentry->d_inode; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1195 | return 0; |
Al Viro | ef7562d | 2011-03-04 14:35:59 -0500 | [diff] [blame] | 1196 | |
| 1197 | failed: |
| 1198 | nd->flags &= ~LOOKUP_RCU; |
Al Viro | 5b6ca02 | 2011-03-09 23:04:47 -0500 | [diff] [blame] | 1199 | if (!(nd->flags & LOOKUP_ROOT)) |
| 1200 | nd->root.mnt = NULL; |
Al Viro | 9caac00 | 2012-07-18 20:43:19 +0400 | [diff] [blame] | 1201 | unlock_rcu_walk(); |
Al Viro | ef7562d | 2011-03-04 14:35:59 -0500 | [diff] [blame] | 1202 | return -ECHILD; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1203 | } |
| 1204 | |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1205 | /* |
David Howells | cc53ce5 | 2011-01-14 18:45:26 +0000 | [diff] [blame] | 1206 | * Follow down to the covering mount currently visible to userspace. At each |
| 1207 | * point, the filesystem owning that dentry may be queried as to whether the |
| 1208 | * caller is permitted to proceed or not. |
David Howells | cc53ce5 | 2011-01-14 18:45:26 +0000 | [diff] [blame] | 1209 | */ |
Al Viro | 7cc90cc | 2011-03-18 09:04:20 -0400 | [diff] [blame] | 1210 | int follow_down(struct path *path) |
David Howells | cc53ce5 | 2011-01-14 18:45:26 +0000 | [diff] [blame] | 1211 | { |
| 1212 | unsigned managed; |
| 1213 | int ret; |
| 1214 | |
| 1215 | while (managed = ACCESS_ONCE(path->dentry->d_flags), |
| 1216 | unlikely(managed & DCACHE_MANAGED_DENTRY)) { |
| 1217 | /* Allow the filesystem to manage the transit without i_mutex |
| 1218 | * being held. |
| 1219 | * |
| 1220 | * We indicate to the filesystem if someone is trying to mount |
| 1221 | * something here. This gives autofs the chance to deny anyone |
| 1222 | * other than its daemon the right to mount on its |
| 1223 | * superstructure. |
| 1224 | * |
| 1225 | * The filesystem may sleep at this point. |
| 1226 | */ |
| 1227 | if (managed & DCACHE_MANAGE_TRANSIT) { |
| 1228 | BUG_ON(!path->dentry->d_op); |
| 1229 | BUG_ON(!path->dentry->d_op->d_manage); |
David Howells | ab90911 | 2011-01-14 18:46:51 +0000 | [diff] [blame] | 1230 | ret = path->dentry->d_op->d_manage( |
Al Viro | 1aed3e4 | 2011-03-18 09:09:02 -0400 | [diff] [blame] | 1231 | path->dentry, false); |
David Howells | cc53ce5 | 2011-01-14 18:45:26 +0000 | [diff] [blame] | 1232 | if (ret < 0) |
| 1233 | return ret == -EISDIR ? 0 : ret; |
| 1234 | } |
| 1235 | |
| 1236 | /* Transit to a mounted filesystem. */ |
| 1237 | if (managed & DCACHE_MOUNTED) { |
| 1238 | struct vfsmount *mounted = lookup_mnt(path); |
| 1239 | if (!mounted) |
| 1240 | break; |
| 1241 | dput(path->dentry); |
| 1242 | mntput(path->mnt); |
| 1243 | path->mnt = mounted; |
| 1244 | path->dentry = dget(mounted->mnt_root); |
| 1245 | continue; |
| 1246 | } |
| 1247 | |
| 1248 | /* Don't handle automount points here */ |
| 1249 | break; |
| 1250 | } |
| 1251 | return 0; |
| 1252 | } |
| 1253 | |
| 1254 | /* |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1255 | * Skip to top of mountpoint pile in refwalk mode for follow_dotdot() |
| 1256 | */ |
| 1257 | static void follow_mount(struct path *path) |
| 1258 | { |
| 1259 | while (d_mountpoint(path->dentry)) { |
| 1260 | struct vfsmount *mounted = lookup_mnt(path); |
| 1261 | if (!mounted) |
| 1262 | break; |
| 1263 | dput(path->dentry); |
| 1264 | mntput(path->mnt); |
| 1265 | path->mnt = mounted; |
| 1266 | path->dentry = dget(mounted->mnt_root); |
| 1267 | } |
| 1268 | } |
| 1269 | |
Eric W. Biederman | bf2e9fa | 2015-08-15 20:27:13 -0500 | [diff] [blame] | 1270 | static int follow_dotdot(struct nameidata *nd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1271 | { |
Al Viro | 589a49f | 2014-09-13 21:55:46 -0400 | [diff] [blame] | 1272 | if (!nd->root.mnt) |
| 1273 | set_root(nd); |
Andreas Mohr | e518ddb | 2006-09-29 02:01:22 -0700 | [diff] [blame] | 1274 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1275 | while(1) { |
Jan Blunck | 4ac9137 | 2008-02-14 19:34:32 -0800 | [diff] [blame] | 1276 | struct dentry *old = nd->path.dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1277 | |
Al Viro | 2a73787 | 2009-04-07 11:49:53 -0400 | [diff] [blame] | 1278 | if (nd->path.dentry == nd->root.dentry && |
| 1279 | nd->path.mnt == nd->root.mnt) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1280 | break; |
| 1281 | } |
Jan Blunck | 4ac9137 | 2008-02-14 19:34:32 -0800 | [diff] [blame] | 1282 | if (nd->path.dentry != nd->path.mnt->mnt_root) { |
Al Viro | 3088dd7 | 2010-01-30 15:47:29 -0500 | [diff] [blame] | 1283 | /* rare case of legitimate dget_parent()... */ |
| 1284 | nd->path.dentry = dget_parent(nd->path.dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1285 | dput(old); |
Eric W. Biederman | bf2e9fa | 2015-08-15 20:27:13 -0500 | [diff] [blame] | 1286 | if (unlikely(!path_connected(&nd->path))) { |
| 1287 | path_put(&nd->path); |
| 1288 | return -ENOENT; |
| 1289 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1290 | break; |
| 1291 | } |
Al Viro | 3088dd7 | 2010-01-30 15:47:29 -0500 | [diff] [blame] | 1292 | if (!follow_up(&nd->path)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1293 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1294 | } |
Al Viro | 79ed022 | 2009-04-18 13:59:41 -0400 | [diff] [blame] | 1295 | follow_mount(&nd->path); |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1296 | nd->inode = nd->path.dentry->d_inode; |
Eric W. Biederman | bf2e9fa | 2015-08-15 20:27:13 -0500 | [diff] [blame] | 1297 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1298 | } |
| 1299 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | /* |
Miklos Szeredi | bad6118 | 2012-03-26 12:54:24 +0200 | [diff] [blame] | 1301 | * This looks up the name in dcache, possibly revalidates the old dentry and |
| 1302 | * allocates a new one if not found or not valid. In the need_lookup argument |
| 1303 | * returns whether i_op->lookup is necessary. |
| 1304 | * |
| 1305 | * dir->d_inode->i_mutex must be held |
Nick Piggin | baa0389 | 2010-08-18 04:37:31 +1000 | [diff] [blame] | 1306 | */ |
Miklos Szeredi | bad6118 | 2012-03-26 12:54:24 +0200 | [diff] [blame] | 1307 | static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir, |
Al Viro | 61b61da | 2012-06-22 12:42:10 +0400 | [diff] [blame] | 1308 | unsigned int flags, bool *need_lookup) |
Nick Piggin | baa0389 | 2010-08-18 04:37:31 +1000 | [diff] [blame] | 1309 | { |
Nick Piggin | baa0389 | 2010-08-18 04:37:31 +1000 | [diff] [blame] | 1310 | struct dentry *dentry; |
Miklos Szeredi | bad6118 | 2012-03-26 12:54:24 +0200 | [diff] [blame] | 1311 | int error; |
Nick Piggin | baa0389 | 2010-08-18 04:37:31 +1000 | [diff] [blame] | 1312 | |
Miklos Szeredi | bad6118 | 2012-03-26 12:54:24 +0200 | [diff] [blame] | 1313 | *need_lookup = false; |
| 1314 | dentry = d_lookup(dir, name); |
| 1315 | if (dentry) { |
| 1316 | if (d_need_lookup(dentry)) { |
| 1317 | *need_lookup = true; |
| 1318 | } else if (dentry->d_flags & DCACHE_OP_REVALIDATE) { |
Al Viro | 61b61da | 2012-06-22 12:42:10 +0400 | [diff] [blame] | 1319 | error = d_revalidate(dentry, flags); |
Miklos Szeredi | bad6118 | 2012-03-26 12:54:24 +0200 | [diff] [blame] | 1320 | if (unlikely(error <= 0)) { |
| 1321 | if (error < 0) { |
| 1322 | dput(dentry); |
| 1323 | return ERR_PTR(error); |
| 1324 | } else if (!d_invalidate(dentry)) { |
| 1325 | dput(dentry); |
| 1326 | dentry = NULL; |
| 1327 | } |
| 1328 | } |
| 1329 | } |
| 1330 | } |
Nick Piggin | baa0389 | 2010-08-18 04:37:31 +1000 | [diff] [blame] | 1331 | |
Miklos Szeredi | bad6118 | 2012-03-26 12:54:24 +0200 | [diff] [blame] | 1332 | if (!dentry) { |
| 1333 | dentry = d_alloc(dir, name); |
| 1334 | if (unlikely(!dentry)) |
| 1335 | return ERR_PTR(-ENOMEM); |
Nick Piggin | baa0389 | 2010-08-18 04:37:31 +1000 | [diff] [blame] | 1336 | |
Miklos Szeredi | bad6118 | 2012-03-26 12:54:24 +0200 | [diff] [blame] | 1337 | *need_lookup = true; |
Nick Piggin | baa0389 | 2010-08-18 04:37:31 +1000 | [diff] [blame] | 1338 | } |
| 1339 | return dentry; |
| 1340 | } |
| 1341 | |
| 1342 | /* |
Miklos Szeredi | bad6118 | 2012-03-26 12:54:24 +0200 | [diff] [blame] | 1343 | * Call i_op->lookup on the dentry. The dentry must be negative but may be |
| 1344 | * hashed if it was pouplated with DCACHE_NEED_LOOKUP. |
| 1345 | * |
| 1346 | * dir->d_inode->i_mutex must be held |
Josef Bacik | 44396f4 | 2011-05-31 11:58:49 -0400 | [diff] [blame] | 1347 | */ |
Miklos Szeredi | bad6118 | 2012-03-26 12:54:24 +0200 | [diff] [blame] | 1348 | static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry, |
Al Viro | 292d3af | 2012-06-10 17:17:17 -0400 | [diff] [blame] | 1349 | unsigned int flags) |
Josef Bacik | 44396f4 | 2011-05-31 11:58:49 -0400 | [diff] [blame] | 1350 | { |
Josef Bacik | 44396f4 | 2011-05-31 11:58:49 -0400 | [diff] [blame] | 1351 | struct dentry *old; |
| 1352 | |
| 1353 | /* Don't create child dentry for a dead directory. */ |
Miklos Szeredi | bad6118 | 2012-03-26 12:54:24 +0200 | [diff] [blame] | 1354 | if (unlikely(IS_DEADDIR(dir))) { |
Miklos Szeredi | e188dc0 | 2012-02-03 14:25:18 +0100 | [diff] [blame] | 1355 | dput(dentry); |
Josef Bacik | 44396f4 | 2011-05-31 11:58:49 -0400 | [diff] [blame] | 1356 | return ERR_PTR(-ENOENT); |
Miklos Szeredi | e188dc0 | 2012-02-03 14:25:18 +0100 | [diff] [blame] | 1357 | } |
Josef Bacik | 44396f4 | 2011-05-31 11:58:49 -0400 | [diff] [blame] | 1358 | |
Al Viro | 292d3af | 2012-06-10 17:17:17 -0400 | [diff] [blame] | 1359 | old = dir->i_op->lookup(dir, dentry, flags); |
Josef Bacik | 44396f4 | 2011-05-31 11:58:49 -0400 | [diff] [blame] | 1360 | if (unlikely(old)) { |
| 1361 | dput(dentry); |
| 1362 | dentry = old; |
| 1363 | } |
| 1364 | return dentry; |
| 1365 | } |
| 1366 | |
Al Viro | a325554 | 2012-03-30 14:41:51 -0400 | [diff] [blame] | 1367 | static struct dentry *__lookup_hash(struct qstr *name, |
Al Viro | 292d3af | 2012-06-10 17:17:17 -0400 | [diff] [blame] | 1368 | struct dentry *base, unsigned int flags) |
Al Viro | a325554 | 2012-03-30 14:41:51 -0400 | [diff] [blame] | 1369 | { |
Miklos Szeredi | bad6118 | 2012-03-26 12:54:24 +0200 | [diff] [blame] | 1370 | bool need_lookup; |
Al Viro | a325554 | 2012-03-30 14:41:51 -0400 | [diff] [blame] | 1371 | struct dentry *dentry; |
| 1372 | |
Al Viro | 292d3af | 2012-06-10 17:17:17 -0400 | [diff] [blame] | 1373 | dentry = lookup_dcache(name, base, flags, &need_lookup); |
Miklos Szeredi | bad6118 | 2012-03-26 12:54:24 +0200 | [diff] [blame] | 1374 | if (!need_lookup) |
| 1375 | return dentry; |
Al Viro | a325554 | 2012-03-30 14:41:51 -0400 | [diff] [blame] | 1376 | |
Al Viro | 292d3af | 2012-06-10 17:17:17 -0400 | [diff] [blame] | 1377 | return lookup_real(base->d_inode, dentry, flags); |
Al Viro | a325554 | 2012-03-30 14:41:51 -0400 | [diff] [blame] | 1378 | } |
| 1379 | |
Josef Bacik | 44396f4 | 2011-05-31 11:58:49 -0400 | [diff] [blame] | 1380 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1381 | * It's more convoluted than I'd like it to be, but... it's still fairly |
| 1382 | * small and for now I'd prefer to have fast path as straight as possible. |
| 1383 | * It _is_ time-critical. |
| 1384 | */ |
Miklos Szeredi | db77d76 | 2012-05-21 17:30:05 +0200 | [diff] [blame] | 1385 | static int lookup_fast(struct nameidata *nd, struct qstr *name, |
| 1386 | struct path *path, struct inode **inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1387 | { |
Jan Blunck | 4ac9137 | 2008-02-14 19:34:32 -0800 | [diff] [blame] | 1388 | struct vfsmount *mnt = nd->path.mnt; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1389 | struct dentry *dentry, *parent = nd->path.dentry; |
Al Viro | 5a18fff | 2011-03-11 04:44:53 -0500 | [diff] [blame] | 1390 | int need_reval = 1; |
| 1391 | int status = 1; |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1392 | int err; |
| 1393 | |
Al Viro | 3cac260 | 2009-08-13 18:27:43 +0400 | [diff] [blame] | 1394 | /* |
Nick Piggin | b04f784 | 2010-08-18 04:37:34 +1000 | [diff] [blame] | 1395 | * Rename seqlock is not required here because in the off chance |
| 1396 | * of a false negative due to a concurrent rename, we're going to |
| 1397 | * do the non-racy lookup, below. |
| 1398 | */ |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1399 | if (nd->flags & LOOKUP_RCU) { |
| 1400 | unsigned seq; |
Linus Torvalds | b7d3740d | 2012-05-04 14:59:14 -0700 | [diff] [blame] | 1401 | dentry = __d_lookup_rcu(parent, name, &seq, nd->inode); |
Al Viro | 5a18fff | 2011-03-11 04:44:53 -0500 | [diff] [blame] | 1402 | if (!dentry) |
| 1403 | goto unlazy; |
| 1404 | |
Linus Torvalds | b7d3740d | 2012-05-04 14:59:14 -0700 | [diff] [blame] | 1405 | /* |
| 1406 | * This sequence count validates that the inode matches |
| 1407 | * the dentry name information from lookup. |
| 1408 | */ |
| 1409 | *inode = dentry->d_inode; |
| 1410 | if (read_seqcount_retry(&dentry->d_seq, seq)) |
| 1411 | return -ECHILD; |
| 1412 | |
| 1413 | /* |
| 1414 | * This sequence count validates that the parent had no |
| 1415 | * changes while we did the lookup of the dentry above. |
| 1416 | * |
| 1417 | * The memory barrier in read_seqcount_begin of child is |
| 1418 | * enough, we can use __read_seqcount_retry here. |
| 1419 | */ |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1420 | if (__read_seqcount_retry(&parent->d_seq, nd->seq)) |
| 1421 | return -ECHILD; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1422 | nd->seq = seq; |
Al Viro | 5a18fff | 2011-03-11 04:44:53 -0500 | [diff] [blame] | 1423 | |
Miklos Szeredi | fa4ee15 | 2012-03-26 12:54:19 +0200 | [diff] [blame] | 1424 | if (unlikely(d_need_lookup(dentry))) |
| 1425 | goto unlazy; |
Al Viro | 2464308 | 2011-02-15 01:26:22 -0500 | [diff] [blame] | 1426 | if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) { |
Al Viro | 28ca326 | 2012-06-10 16:10:59 -0400 | [diff] [blame] | 1427 | status = d_revalidate(dentry, nd->flags); |
Al Viro | 5a18fff | 2011-03-11 04:44:53 -0500 | [diff] [blame] | 1428 | if (unlikely(status <= 0)) { |
| 1429 | if (status != -ECHILD) |
| 1430 | need_reval = 0; |
| 1431 | goto unlazy; |
| 1432 | } |
Al Viro | 2464308 | 2011-02-15 01:26:22 -0500 | [diff] [blame] | 1433 | } |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1434 | path->mnt = mnt; |
| 1435 | path->dentry = dentry; |
Al Viro | d6e9bd2 | 2011-05-27 07:03:15 -0400 | [diff] [blame] | 1436 | if (unlikely(!__follow_mount_rcu(nd, path, inode))) |
| 1437 | goto unlazy; |
| 1438 | if (unlikely(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT)) |
| 1439 | goto unlazy; |
| 1440 | return 0; |
Al Viro | 5a18fff | 2011-03-11 04:44:53 -0500 | [diff] [blame] | 1441 | unlazy: |
Al Viro | 19660af | 2011-03-25 10:32:48 -0400 | [diff] [blame] | 1442 | if (unlazy_walk(nd, dentry)) |
| 1443 | return -ECHILD; |
Al Viro | 5a18fff | 2011-03-11 04:44:53 -0500 | [diff] [blame] | 1444 | } else { |
| 1445 | dentry = __d_lookup(parent, name); |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1446 | } |
Al Viro | 5a18fff | 2011-03-11 04:44:53 -0500 | [diff] [blame] | 1447 | |
Al Viro | 81e6f52 | 2012-03-30 14:48:04 -0400 | [diff] [blame] | 1448 | if (unlikely(!dentry)) |
| 1449 | goto need_lookup; |
Al Viro | 5a18fff | 2011-03-11 04:44:53 -0500 | [diff] [blame] | 1450 | |
Al Viro | 81e6f52 | 2012-03-30 14:48:04 -0400 | [diff] [blame] | 1451 | if (unlikely(d_need_lookup(dentry))) { |
| 1452 | dput(dentry); |
| 1453 | goto need_lookup; |
Al Viro | 2464308 | 2011-02-15 01:26:22 -0500 | [diff] [blame] | 1454 | } |
Al Viro | 81e6f52 | 2012-03-30 14:48:04 -0400 | [diff] [blame] | 1455 | |
Al Viro | 5a18fff | 2011-03-11 04:44:53 -0500 | [diff] [blame] | 1456 | if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval) |
Al Viro | 28ca326 | 2012-06-10 16:10:59 -0400 | [diff] [blame] | 1457 | status = d_revalidate(dentry, nd->flags); |
Al Viro | 5a18fff | 2011-03-11 04:44:53 -0500 | [diff] [blame] | 1458 | if (unlikely(status <= 0)) { |
| 1459 | if (status < 0) { |
| 1460 | dput(dentry); |
| 1461 | return status; |
| 1462 | } |
| 1463 | if (!d_invalidate(dentry)) { |
| 1464 | dput(dentry); |
Al Viro | 81e6f52 | 2012-03-30 14:48:04 -0400 | [diff] [blame] | 1465 | goto need_lookup; |
Al Viro | 5a18fff | 2011-03-11 04:44:53 -0500 | [diff] [blame] | 1466 | } |
| 1467 | } |
Miklos Szeredi | db77d76 | 2012-05-21 17:30:05 +0200 | [diff] [blame] | 1468 | |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1469 | path->mnt = mnt; |
| 1470 | path->dentry = dentry; |
| 1471 | err = follow_managed(path, nd->flags); |
Ian Kent | 8931221 | 2011-01-18 12:06:10 +0800 | [diff] [blame] | 1472 | if (unlikely(err < 0)) { |
| 1473 | path_put_conditional(path, nd); |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1474 | return err; |
Ian Kent | 8931221 | 2011-01-18 12:06:10 +0800 | [diff] [blame] | 1475 | } |
Al Viro | a3fbbde | 2011-11-07 21:21:26 +0000 | [diff] [blame] | 1476 | if (err) |
| 1477 | nd->flags |= LOOKUP_JUMPED; |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 1478 | *inode = path->dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1479 | return 0; |
Al Viro | 81e6f52 | 2012-03-30 14:48:04 -0400 | [diff] [blame] | 1480 | |
| 1481 | need_lookup: |
Miklos Szeredi | db77d76 | 2012-05-21 17:30:05 +0200 | [diff] [blame] | 1482 | return 1; |
| 1483 | } |
| 1484 | |
| 1485 | /* Fast lookup failed, do it the slow way */ |
| 1486 | static int lookup_slow(struct nameidata *nd, struct qstr *name, |
| 1487 | struct path *path) |
| 1488 | { |
| 1489 | struct dentry *dentry, *parent; |
| 1490 | int err; |
| 1491 | |
| 1492 | parent = nd->path.dentry; |
Al Viro | 81e6f52 | 2012-03-30 14:48:04 -0400 | [diff] [blame] | 1493 | BUG_ON(nd->inode != parent->d_inode); |
| 1494 | |
| 1495 | mutex_lock(&parent->d_inode->i_mutex); |
Al Viro | 292d3af | 2012-06-10 17:17:17 -0400 | [diff] [blame] | 1496 | dentry = __lookup_hash(name, parent, nd->flags); |
Al Viro | 81e6f52 | 2012-03-30 14:48:04 -0400 | [diff] [blame] | 1497 | mutex_unlock(&parent->d_inode->i_mutex); |
| 1498 | if (IS_ERR(dentry)) |
| 1499 | return PTR_ERR(dentry); |
Miklos Szeredi | db77d76 | 2012-05-21 17:30:05 +0200 | [diff] [blame] | 1500 | path->mnt = nd->path.mnt; |
| 1501 | path->dentry = dentry; |
| 1502 | err = follow_managed(path, nd->flags); |
| 1503 | if (unlikely(err < 0)) { |
| 1504 | path_put_conditional(path, nd); |
| 1505 | return err; |
| 1506 | } |
| 1507 | if (err) |
| 1508 | nd->flags |= LOOKUP_JUMPED; |
| 1509 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1510 | } |
| 1511 | |
Al Viro | 52094c8 | 2011-02-21 21:34:47 -0500 | [diff] [blame] | 1512 | static inline int may_lookup(struct nameidata *nd) |
| 1513 | { |
| 1514 | if (nd->flags & LOOKUP_RCU) { |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 1515 | int err = inode_permission2(nd->path.mnt, nd->inode, MAY_EXEC|MAY_NOT_BLOCK); |
Al Viro | 52094c8 | 2011-02-21 21:34:47 -0500 | [diff] [blame] | 1516 | if (err != -ECHILD) |
| 1517 | return err; |
Al Viro | 19660af | 2011-03-25 10:32:48 -0400 | [diff] [blame] | 1518 | if (unlazy_walk(nd, NULL)) |
Al Viro | 52094c8 | 2011-02-21 21:34:47 -0500 | [diff] [blame] | 1519 | return -ECHILD; |
| 1520 | } |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 1521 | return inode_permission2(nd->path.mnt, nd->inode, MAY_EXEC); |
Al Viro | 52094c8 | 2011-02-21 21:34:47 -0500 | [diff] [blame] | 1522 | } |
| 1523 | |
Al Viro | 9856fa1 | 2011-03-04 14:22:06 -0500 | [diff] [blame] | 1524 | static inline int handle_dots(struct nameidata *nd, int type) |
| 1525 | { |
| 1526 | if (type == LAST_DOTDOT) { |
| 1527 | if (nd->flags & LOOKUP_RCU) { |
| 1528 | if (follow_dotdot_rcu(nd)) |
| 1529 | return -ECHILD; |
| 1530 | } else |
Eric W. Biederman | bf2e9fa | 2015-08-15 20:27:13 -0500 | [diff] [blame] | 1531 | return follow_dotdot(nd); |
Al Viro | 9856fa1 | 2011-03-04 14:22:06 -0500 | [diff] [blame] | 1532 | } |
| 1533 | return 0; |
| 1534 | } |
| 1535 | |
Al Viro | 951361f | 2011-03-04 14:44:37 -0500 | [diff] [blame] | 1536 | static void terminate_walk(struct nameidata *nd) |
| 1537 | { |
| 1538 | if (!(nd->flags & LOOKUP_RCU)) { |
| 1539 | path_put(&nd->path); |
| 1540 | } else { |
| 1541 | nd->flags &= ~LOOKUP_RCU; |
Al Viro | 5b6ca02 | 2011-03-09 23:04:47 -0500 | [diff] [blame] | 1542 | if (!(nd->flags & LOOKUP_ROOT)) |
| 1543 | nd->root.mnt = NULL; |
Al Viro | 9caac00 | 2012-07-18 20:43:19 +0400 | [diff] [blame] | 1544 | unlock_rcu_walk(); |
Al Viro | 951361f | 2011-03-04 14:44:37 -0500 | [diff] [blame] | 1545 | } |
| 1546 | } |
| 1547 | |
Linus Torvalds | 3ddcd05 | 2011-08-06 22:45:50 -0700 | [diff] [blame] | 1548 | /* |
| 1549 | * Do we need to follow links? We _really_ want to be able |
| 1550 | * to do this check without having to look at inode->i_op, |
| 1551 | * so we keep a cache of "no, this doesn't need follow_link" |
| 1552 | * for the common case. |
| 1553 | */ |
Linus Torvalds | 7813b94 | 2011-08-07 09:53:20 -0700 | [diff] [blame] | 1554 | static inline int should_follow_link(struct inode *inode, int follow) |
Linus Torvalds | 3ddcd05 | 2011-08-06 22:45:50 -0700 | [diff] [blame] | 1555 | { |
| 1556 | if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) { |
| 1557 | if (likely(inode->i_op->follow_link)) |
| 1558 | return follow; |
| 1559 | |
| 1560 | /* This gets set once for the inode lifetime */ |
| 1561 | spin_lock(&inode->i_lock); |
| 1562 | inode->i_opflags |= IOP_NOFOLLOW; |
| 1563 | spin_unlock(&inode->i_lock); |
| 1564 | } |
| 1565 | return 0; |
| 1566 | } |
| 1567 | |
Al Viro | ce57dfc | 2011-03-13 19:58:58 -0400 | [diff] [blame] | 1568 | static inline int walk_component(struct nameidata *nd, struct path *path, |
| 1569 | struct qstr *name, int type, int follow) |
| 1570 | { |
| 1571 | struct inode *inode; |
| 1572 | int err; |
| 1573 | /* |
| 1574 | * "." and ".." are special - ".." especially so because it has |
| 1575 | * to be able to know about the current root directory and |
| 1576 | * parent relationships. |
| 1577 | */ |
| 1578 | if (unlikely(type != LAST_NORM)) |
| 1579 | return handle_dots(nd, type); |
Miklos Szeredi | db77d76 | 2012-05-21 17:30:05 +0200 | [diff] [blame] | 1580 | err = lookup_fast(nd, name, path, &inode); |
Al Viro | ce57dfc | 2011-03-13 19:58:58 -0400 | [diff] [blame] | 1581 | if (unlikely(err)) { |
Miklos Szeredi | db77d76 | 2012-05-21 17:30:05 +0200 | [diff] [blame] | 1582 | if (err < 0) |
| 1583 | goto out_err; |
| 1584 | |
| 1585 | err = lookup_slow(nd, name, path); |
| 1586 | if (err < 0) |
| 1587 | goto out_err; |
| 1588 | |
| 1589 | inode = path->dentry->d_inode; |
Al Viro | ce57dfc | 2011-03-13 19:58:58 -0400 | [diff] [blame] | 1590 | } |
Miklos Szeredi | db77d76 | 2012-05-21 17:30:05 +0200 | [diff] [blame] | 1591 | err = -ENOENT; |
| 1592 | if (!inode) |
| 1593 | goto out_path_put; |
| 1594 | |
Linus Torvalds | 7813b94 | 2011-08-07 09:53:20 -0700 | [diff] [blame] | 1595 | if (should_follow_link(inode, follow)) { |
Al Viro | 19660af | 2011-03-25 10:32:48 -0400 | [diff] [blame] | 1596 | if (nd->flags & LOOKUP_RCU) { |
Al Viro | a377bbd | 2015-04-24 15:47:07 -0400 | [diff] [blame] | 1597 | if (unlikely(nd->path.mnt != path->mnt || |
| 1598 | unlazy_walk(nd, path->dentry))) { |
Miklos Szeredi | db77d76 | 2012-05-21 17:30:05 +0200 | [diff] [blame] | 1599 | err = -ECHILD; |
| 1600 | goto out_err; |
Al Viro | 19660af | 2011-03-25 10:32:48 -0400 | [diff] [blame] | 1601 | } |
| 1602 | } |
Al Viro | ce57dfc | 2011-03-13 19:58:58 -0400 | [diff] [blame] | 1603 | BUG_ON(inode != path->dentry->d_inode); |
| 1604 | return 1; |
| 1605 | } |
| 1606 | path_to_nameidata(path, nd); |
| 1607 | nd->inode = inode; |
| 1608 | return 0; |
Miklos Szeredi | db77d76 | 2012-05-21 17:30:05 +0200 | [diff] [blame] | 1609 | |
| 1610 | out_path_put: |
| 1611 | path_to_nameidata(path, nd); |
| 1612 | out_err: |
| 1613 | terminate_walk(nd); |
| 1614 | return err; |
Al Viro | ce57dfc | 2011-03-13 19:58:58 -0400 | [diff] [blame] | 1615 | } |
| 1616 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1617 | /* |
Al Viro | b356379 | 2011-03-14 21:54:55 -0400 | [diff] [blame] | 1618 | * This limits recursive symlink follows to 8, while |
| 1619 | * limiting consecutive symlinks to 40. |
| 1620 | * |
| 1621 | * Without that kind of total limit, nasty chains of consecutive |
| 1622 | * symlinks can cause almost arbitrarily long lookups. |
| 1623 | */ |
| 1624 | static inline int nested_symlink(struct path *path, struct nameidata *nd) |
| 1625 | { |
| 1626 | int res; |
| 1627 | |
Al Viro | b356379 | 2011-03-14 21:54:55 -0400 | [diff] [blame] | 1628 | if (unlikely(current->link_count >= MAX_NESTED_LINKS)) { |
| 1629 | path_put_conditional(path, nd); |
| 1630 | path_put(&nd->path); |
| 1631 | return -ELOOP; |
| 1632 | } |
Erez Zadok | 1a4022f | 2011-05-21 01:19:59 -0400 | [diff] [blame] | 1633 | BUG_ON(nd->depth >= MAX_NESTED_LINKS); |
Al Viro | b356379 | 2011-03-14 21:54:55 -0400 | [diff] [blame] | 1634 | |
| 1635 | nd->depth++; |
| 1636 | current->link_count++; |
| 1637 | |
| 1638 | do { |
| 1639 | struct path link = *path; |
| 1640 | void *cookie; |
Al Viro | 574197e | 2011-03-14 22:20:34 -0400 | [diff] [blame] | 1641 | |
| 1642 | res = follow_link(&link, nd, &cookie); |
Al Viro | 2c4d739 | 2012-06-10 04:15:17 -0400 | [diff] [blame] | 1643 | if (res) |
| 1644 | break; |
| 1645 | res = walk_component(nd, path, &nd->last, |
| 1646 | nd->last_type, LOOKUP_FOLLOW); |
Al Viro | 574197e | 2011-03-14 22:20:34 -0400 | [diff] [blame] | 1647 | put_link(nd, &link, cookie); |
Al Viro | b356379 | 2011-03-14 21:54:55 -0400 | [diff] [blame] | 1648 | } while (res > 0); |
| 1649 | |
| 1650 | current->link_count--; |
| 1651 | nd->depth--; |
| 1652 | return res; |
| 1653 | } |
| 1654 | |
| 1655 | /* |
Linus Torvalds | 3ddcd05 | 2011-08-06 22:45:50 -0700 | [diff] [blame] | 1656 | * We really don't want to look at inode->i_op->lookup |
| 1657 | * when we don't have to. So we keep a cache bit in |
| 1658 | * the inode ->i_opflags field that says "yes, we can |
| 1659 | * do lookup on this inode". |
| 1660 | */ |
| 1661 | static inline int can_lookup(struct inode *inode) |
| 1662 | { |
| 1663 | if (likely(inode->i_opflags & IOP_LOOKUP)) |
| 1664 | return 1; |
| 1665 | if (likely(!inode->i_op->lookup)) |
| 1666 | return 0; |
| 1667 | |
| 1668 | /* We do this once for the lifetime of the inode */ |
| 1669 | spin_lock(&inode->i_lock); |
| 1670 | inode->i_opflags |= IOP_LOOKUP; |
| 1671 | spin_unlock(&inode->i_lock); |
| 1672 | return 1; |
| 1673 | } |
| 1674 | |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 1675 | /* |
| 1676 | * We can do the critical dentry name comparison and hashing |
| 1677 | * operations one word at a time, but we are limited to: |
| 1678 | * |
| 1679 | * - Architectures with fast unaligned word accesses. We could |
| 1680 | * do a "get_unaligned()" if this helps and is sufficiently |
| 1681 | * fast. |
| 1682 | * |
| 1683 | * - Little-endian machines (so that we can generate the mask |
| 1684 | * of low bytes efficiently). Again, we *could* do a byte |
| 1685 | * swapping load on big-endian architectures if that is not |
| 1686 | * expensive enough to make the optimization worthless. |
| 1687 | * |
| 1688 | * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we |
| 1689 | * do not trap on the (extremely unlikely) case of a page |
| 1690 | * crossing operation. |
| 1691 | * |
| 1692 | * - Furthermore, we need an efficient 64-bit compile for the |
| 1693 | * 64-bit case in order to generate the "number of bytes in |
| 1694 | * the final mask". Again, that could be replaced with a |
| 1695 | * efficient population count instruction or similar. |
| 1696 | */ |
| 1697 | #ifdef CONFIG_DCACHE_WORD_ACCESS |
| 1698 | |
Linus Torvalds | f68e556 | 2012-04-06 13:54:56 -0700 | [diff] [blame] | 1699 | #include <asm/word-at-a-time.h> |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 1700 | |
Linus Torvalds | f68e556 | 2012-04-06 13:54:56 -0700 | [diff] [blame] | 1701 | #ifdef CONFIG_64BIT |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 1702 | |
| 1703 | static inline unsigned int fold_hash(unsigned long hash) |
| 1704 | { |
Linus Torvalds | 07e997d | 2014-09-13 11:30:10 -0700 | [diff] [blame] | 1705 | return hash_64(hash, 32); |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 1706 | } |
| 1707 | |
| 1708 | #else /* 32-bit case */ |
| 1709 | |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 1710 | #define fold_hash(x) (x) |
| 1711 | |
| 1712 | #endif |
| 1713 | |
| 1714 | unsigned int full_name_hash(const unsigned char *name, unsigned int len) |
| 1715 | { |
| 1716 | unsigned long a, mask; |
| 1717 | unsigned long hash = 0; |
| 1718 | |
| 1719 | for (;;) { |
Linus Torvalds | e419b4c | 2012-05-03 10:16:43 -0700 | [diff] [blame] | 1720 | a = load_unaligned_zeropad(name); |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 1721 | if (len < sizeof(unsigned long)) |
| 1722 | break; |
| 1723 | hash += a; |
Al Viro | f132c5b | 2012-03-22 21:59:52 +0000 | [diff] [blame] | 1724 | hash *= 9; |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 1725 | name += sizeof(unsigned long); |
| 1726 | len -= sizeof(unsigned long); |
| 1727 | if (!len) |
| 1728 | goto done; |
| 1729 | } |
| 1730 | mask = ~(~0ul << len*8); |
| 1731 | hash += mask & a; |
| 1732 | done: |
| 1733 | return fold_hash(hash); |
| 1734 | } |
| 1735 | EXPORT_SYMBOL(full_name_hash); |
| 1736 | |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 1737 | /* |
| 1738 | * Calculate the length and hash of the path component, and |
| 1739 | * return the length of the component; |
| 1740 | */ |
| 1741 | static inline unsigned long hash_name(const char *name, unsigned int *hashp) |
| 1742 | { |
| 1743 | unsigned long a, mask, hash, len; |
| 1744 | |
| 1745 | hash = a = 0; |
| 1746 | len = -sizeof(unsigned long); |
| 1747 | do { |
| 1748 | hash = (hash + a) * 9; |
| 1749 | len += sizeof(unsigned long); |
Linus Torvalds | e419b4c | 2012-05-03 10:16:43 -0700 | [diff] [blame] | 1750 | a = load_unaligned_zeropad(name+len); |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 1751 | /* Do we have any NUL or '/' bytes in this word? */ |
Linus Torvalds | f68e556 | 2012-04-06 13:54:56 -0700 | [diff] [blame] | 1752 | mask = has_zero(a) | has_zero(a ^ REPEAT_BYTE('/')); |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 1753 | } while (!mask); |
| 1754 | |
| 1755 | /* The mask *below* the first high bit set */ |
| 1756 | mask = (mask - 1) & ~mask; |
| 1757 | mask >>= 7; |
| 1758 | hash += a & mask; |
| 1759 | *hashp = fold_hash(hash); |
| 1760 | |
| 1761 | return len + count_masked_bytes(mask); |
| 1762 | } |
| 1763 | |
| 1764 | #else |
| 1765 | |
Linus Torvalds | 0145acc | 2012-03-02 14:32:59 -0800 | [diff] [blame] | 1766 | unsigned int full_name_hash(const unsigned char *name, unsigned int len) |
| 1767 | { |
| 1768 | unsigned long hash = init_name_hash(); |
| 1769 | while (len--) |
| 1770 | hash = partial_name_hash(*name++, hash); |
| 1771 | return end_name_hash(hash); |
| 1772 | } |
Linus Torvalds | ae942ae | 2012-03-02 19:40:57 -0800 | [diff] [blame] | 1773 | EXPORT_SYMBOL(full_name_hash); |
Linus Torvalds | 0145acc | 2012-03-02 14:32:59 -0800 | [diff] [blame] | 1774 | |
Linus Torvalds | 3ddcd05 | 2011-08-06 22:45:50 -0700 | [diff] [blame] | 1775 | /* |
Linus Torvalds | 200e9ef | 2012-03-02 14:49:24 -0800 | [diff] [blame] | 1776 | * We know there's a real path component here of at least |
| 1777 | * one character. |
| 1778 | */ |
| 1779 | static inline unsigned long hash_name(const char *name, unsigned int *hashp) |
| 1780 | { |
| 1781 | unsigned long hash = init_name_hash(); |
| 1782 | unsigned long len = 0, c; |
| 1783 | |
| 1784 | c = (unsigned char)*name; |
| 1785 | do { |
| 1786 | len++; |
| 1787 | hash = partial_name_hash(c, hash); |
| 1788 | c = (unsigned char)name[len]; |
| 1789 | } while (c && c != '/'); |
| 1790 | *hashp = end_name_hash(hash); |
| 1791 | return len; |
| 1792 | } |
| 1793 | |
Linus Torvalds | bfcfaa7 | 2012-03-06 11:16:17 -0800 | [diff] [blame] | 1794 | #endif |
| 1795 | |
Linus Torvalds | 200e9ef | 2012-03-02 14:49:24 -0800 | [diff] [blame] | 1796 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1797 | * Name resolution. |
Prasanna Meda | ea3834d | 2005-04-29 16:00:17 +0100 | [diff] [blame] | 1798 | * This is the basic name resolution function, turning a pathname into |
| 1799 | * the final dentry. We expect 'base' to be positive and a directory. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1800 | * |
Prasanna Meda | ea3834d | 2005-04-29 16:00:17 +0100 | [diff] [blame] | 1801 | * Returns 0 and nd will have valid dentry and mnt on success. |
| 1802 | * Returns error and drops reference to input namei data on failure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1803 | */ |
Al Viro | 6de88d7 | 2009-08-09 01:41:57 +0400 | [diff] [blame] | 1804 | static int link_path_walk(const char *name, struct nameidata *nd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1805 | { |
| 1806 | struct path next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1807 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1808 | |
| 1809 | while (*name=='/') |
| 1810 | name++; |
| 1811 | if (!*name) |
Al Viro | 086e183 | 2011-02-22 20:56:27 -0500 | [diff] [blame] | 1812 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1813 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1814 | /* At this point we know we have a real path component. */ |
| 1815 | for(;;) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1816 | struct qstr this; |
Linus Torvalds | 200e9ef | 2012-03-02 14:49:24 -0800 | [diff] [blame] | 1817 | long len; |
Al Viro | fe479a5 | 2011-02-22 15:10:03 -0500 | [diff] [blame] | 1818 | int type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1819 | |
Al Viro | 52094c8 | 2011-02-21 21:34:47 -0500 | [diff] [blame] | 1820 | err = may_lookup(nd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1821 | if (err) |
| 1822 | break; |
| 1823 | |
Linus Torvalds | 200e9ef | 2012-03-02 14:49:24 -0800 | [diff] [blame] | 1824 | len = hash_name(name, &this.hash); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1825 | this.name = name; |
Linus Torvalds | 200e9ef | 2012-03-02 14:49:24 -0800 | [diff] [blame] | 1826 | this.len = len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1827 | |
Al Viro | fe479a5 | 2011-02-22 15:10:03 -0500 | [diff] [blame] | 1828 | type = LAST_NORM; |
Linus Torvalds | 200e9ef | 2012-03-02 14:49:24 -0800 | [diff] [blame] | 1829 | if (name[0] == '.') switch (len) { |
Al Viro | fe479a5 | 2011-02-22 15:10:03 -0500 | [diff] [blame] | 1830 | case 2: |
Linus Torvalds | 200e9ef | 2012-03-02 14:49:24 -0800 | [diff] [blame] | 1831 | if (name[1] == '.') { |
Al Viro | fe479a5 | 2011-02-22 15:10:03 -0500 | [diff] [blame] | 1832 | type = LAST_DOTDOT; |
Al Viro | 16c2cd7 | 2011-02-22 15:50:10 -0500 | [diff] [blame] | 1833 | nd->flags |= LOOKUP_JUMPED; |
| 1834 | } |
Al Viro | fe479a5 | 2011-02-22 15:10:03 -0500 | [diff] [blame] | 1835 | break; |
| 1836 | case 1: |
| 1837 | type = LAST_DOT; |
| 1838 | } |
Al Viro | 5a202bc | 2011-03-08 14:17:44 -0500 | [diff] [blame] | 1839 | if (likely(type == LAST_NORM)) { |
| 1840 | struct dentry *parent = nd->path.dentry; |
Al Viro | 16c2cd7 | 2011-02-22 15:50:10 -0500 | [diff] [blame] | 1841 | nd->flags &= ~LOOKUP_JUMPED; |
Al Viro | 5a202bc | 2011-03-08 14:17:44 -0500 | [diff] [blame] | 1842 | if (unlikely(parent->d_flags & DCACHE_OP_HASH)) { |
| 1843 | err = parent->d_op->d_hash(parent, nd->inode, |
| 1844 | &this); |
| 1845 | if (err < 0) |
| 1846 | break; |
| 1847 | } |
| 1848 | } |
Al Viro | fe479a5 | 2011-02-22 15:10:03 -0500 | [diff] [blame] | 1849 | |
Linus Torvalds | 200e9ef | 2012-03-02 14:49:24 -0800 | [diff] [blame] | 1850 | if (!name[len]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1851 | goto last_component; |
Linus Torvalds | 200e9ef | 2012-03-02 14:49:24 -0800 | [diff] [blame] | 1852 | /* |
| 1853 | * If it wasn't NUL, we know it was '/'. Skip that |
| 1854 | * slash, and continue until no more slashes. |
| 1855 | */ |
| 1856 | do { |
| 1857 | len++; |
| 1858 | } while (unlikely(name[len] == '/')); |
| 1859 | if (!name[len]) |
Al Viro | b356379 | 2011-03-14 21:54:55 -0400 | [diff] [blame] | 1860 | goto last_component; |
Linus Torvalds | 200e9ef | 2012-03-02 14:49:24 -0800 | [diff] [blame] | 1861 | name += len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1862 | |
Al Viro | ce57dfc | 2011-03-13 19:58:58 -0400 | [diff] [blame] | 1863 | err = walk_component(nd, &next, &this, type, LOOKUP_FOLLOW); |
| 1864 | if (err < 0) |
| 1865 | return err; |
Al Viro | fe479a5 | 2011-02-22 15:10:03 -0500 | [diff] [blame] | 1866 | |
Al Viro | ce57dfc | 2011-03-13 19:58:58 -0400 | [diff] [blame] | 1867 | if (err) { |
Al Viro | b356379 | 2011-03-14 21:54:55 -0400 | [diff] [blame] | 1868 | err = nested_symlink(&next, nd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1869 | if (err) |
Al Viro | a7472ba | 2011-03-04 14:39:30 -0500 | [diff] [blame] | 1870 | return err; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1871 | } |
Linus Torvalds | 3ddcd05 | 2011-08-06 22:45:50 -0700 | [diff] [blame] | 1872 | if (can_lookup(nd->inode)) |
| 1873 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1874 | err = -ENOTDIR; |
Linus Torvalds | 3ddcd05 | 2011-08-06 22:45:50 -0700 | [diff] [blame] | 1875 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1876 | /* here ends the main loop */ |
| 1877 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1878 | last_component: |
Al Viro | b356379 | 2011-03-14 21:54:55 -0400 | [diff] [blame] | 1879 | nd->last = this; |
| 1880 | nd->last_type = type; |
Al Viro | 086e183 | 2011-02-22 20:56:27 -0500 | [diff] [blame] | 1881 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1882 | } |
Al Viro | 951361f | 2011-03-04 14:44:37 -0500 | [diff] [blame] | 1883 | terminate_walk(nd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1884 | return err; |
| 1885 | } |
| 1886 | |
Al Viro | 70e9b35 | 2011-03-05 21:12:22 -0500 | [diff] [blame] | 1887 | static int path_init(int dfd, const char *name, unsigned int flags, |
| 1888 | struct nameidata *nd, struct file **fp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1889 | { |
Prasanna Meda | ea3834d | 2005-04-29 16:00:17 +0100 | [diff] [blame] | 1890 | int retval = 0; |
Ulrich Drepper | 170aa3d | 2006-02-04 23:28:02 -0800 | [diff] [blame] | 1891 | int fput_needed; |
| 1892 | struct file *file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1893 | |
| 1894 | nd->last_type = LAST_ROOT; /* if there are only slashes... */ |
Al Viro | 16c2cd7 | 2011-02-22 15:50:10 -0500 | [diff] [blame] | 1895 | nd->flags = flags | LOOKUP_JUMPED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1896 | nd->depth = 0; |
Al Viro | 5b6ca02 | 2011-03-09 23:04:47 -0500 | [diff] [blame] | 1897 | if (flags & LOOKUP_ROOT) { |
| 1898 | struct inode *inode = nd->root.dentry->d_inode; |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 1899 | struct vfsmount *mnt = nd->root.mnt; |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 1900 | if (*name) { |
| 1901 | if (!inode->i_op->lookup) |
| 1902 | return -ENOTDIR; |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 1903 | retval = inode_permission2(mnt, inode, MAY_EXEC); |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 1904 | if (retval) |
| 1905 | return retval; |
| 1906 | } |
Al Viro | 5b6ca02 | 2011-03-09 23:04:47 -0500 | [diff] [blame] | 1907 | nd->path = nd->root; |
| 1908 | nd->inode = inode; |
| 1909 | if (flags & LOOKUP_RCU) { |
Al Viro | 9caac00 | 2012-07-18 20:43:19 +0400 | [diff] [blame] | 1910 | lock_rcu_walk(); |
Al Viro | 5b6ca02 | 2011-03-09 23:04:47 -0500 | [diff] [blame] | 1911 | nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); |
| 1912 | } else { |
| 1913 | path_get(&nd->path); |
| 1914 | } |
| 1915 | return 0; |
| 1916 | } |
| 1917 | |
Al Viro | 2a73787 | 2009-04-07 11:49:53 -0400 | [diff] [blame] | 1918 | nd->root.mnt = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1919 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1920 | if (*name=='/') { |
Al Viro | e41f7d4 | 2011-02-22 14:02:58 -0500 | [diff] [blame] | 1921 | if (flags & LOOKUP_RCU) { |
Al Viro | 9caac00 | 2012-07-18 20:43:19 +0400 | [diff] [blame] | 1922 | lock_rcu_walk(); |
Al Viro | 589a49f | 2014-09-13 21:55:46 -0400 | [diff] [blame] | 1923 | nd->seq = set_root_rcu(nd); |
Al Viro | e41f7d4 | 2011-02-22 14:02:58 -0500 | [diff] [blame] | 1924 | } else { |
| 1925 | set_root(nd); |
| 1926 | path_get(&nd->root); |
| 1927 | } |
Al Viro | 2a73787 | 2009-04-07 11:49:53 -0400 | [diff] [blame] | 1928 | nd->path = nd->root; |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 1929 | } else if (dfd == AT_FDCWD) { |
Al Viro | e41f7d4 | 2011-02-22 14:02:58 -0500 | [diff] [blame] | 1930 | if (flags & LOOKUP_RCU) { |
| 1931 | struct fs_struct *fs = current->fs; |
| 1932 | unsigned seq; |
| 1933 | |
Al Viro | 9caac00 | 2012-07-18 20:43:19 +0400 | [diff] [blame] | 1934 | lock_rcu_walk(); |
Al Viro | e41f7d4 | 2011-02-22 14:02:58 -0500 | [diff] [blame] | 1935 | |
| 1936 | do { |
| 1937 | seq = read_seqcount_begin(&fs->seq); |
| 1938 | nd->path = fs->pwd; |
| 1939 | nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); |
| 1940 | } while (read_seqcount_retry(&fs->seq, seq)); |
| 1941 | } else { |
| 1942 | get_fs_pwd(current->fs, &nd->path); |
| 1943 | } |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 1944 | } else { |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 1945 | struct dentry *dentry; |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 1946 | struct vfsmount *mnt; |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 1947 | |
Al Viro | 1abf0c7 | 2011-03-13 03:51:11 -0400 | [diff] [blame] | 1948 | file = fget_raw_light(dfd, &fput_needed); |
Ulrich Drepper | 170aa3d | 2006-02-04 23:28:02 -0800 | [diff] [blame] | 1949 | retval = -EBADF; |
| 1950 | if (!file) |
Trond Myklebust | 6d09bb6 | 2006-06-04 02:51:37 -0700 | [diff] [blame] | 1951 | goto out_fail; |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 1952 | |
Josef "Jeff" Sipek | 0f7fc9e | 2006-12-08 02:36:35 -0800 | [diff] [blame] | 1953 | dentry = file->f_path.dentry; |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 1954 | mnt = file->f_path.mnt; |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 1955 | |
Al Viro | f52e0c1 | 2011-03-14 18:56:51 -0400 | [diff] [blame] | 1956 | if (*name) { |
| 1957 | retval = -ENOTDIR; |
| 1958 | if (!S_ISDIR(dentry->d_inode->i_mode)) |
| 1959 | goto fput_fail; |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 1960 | |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 1961 | retval = inode_permission2(mnt, dentry->d_inode, MAY_EXEC); |
Al Viro | f52e0c1 | 2011-03-14 18:56:51 -0400 | [diff] [blame] | 1962 | if (retval) |
| 1963 | goto fput_fail; |
| 1964 | } |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 1965 | |
Jan Blunck | 5dd784d | 2008-02-14 19:34:38 -0800 | [diff] [blame] | 1966 | nd->path = file->f_path; |
Al Viro | e41f7d4 | 2011-02-22 14:02:58 -0500 | [diff] [blame] | 1967 | if (flags & LOOKUP_RCU) { |
| 1968 | if (fput_needed) |
Al Viro | 70e9b35 | 2011-03-05 21:12:22 -0500 | [diff] [blame] | 1969 | *fp = file; |
Al Viro | e41f7d4 | 2011-02-22 14:02:58 -0500 | [diff] [blame] | 1970 | nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); |
Al Viro | 9caac00 | 2012-07-18 20:43:19 +0400 | [diff] [blame] | 1971 | lock_rcu_walk(); |
Al Viro | e41f7d4 | 2011-02-22 14:02:58 -0500 | [diff] [blame] | 1972 | } else { |
| 1973 | path_get(&file->f_path); |
| 1974 | fput_light(file, fput_needed); |
| 1975 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1976 | } |
Al Viro | e41f7d4 | 2011-02-22 14:02:58 -0500 | [diff] [blame] | 1977 | |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 1978 | nd->inode = nd->path.dentry->d_inode; |
Al Viro | 9b4a9b1 | 2009-04-07 11:44:16 -0400 | [diff] [blame] | 1979 | return 0; |
Ulrich Drepper | 170aa3d | 2006-02-04 23:28:02 -0800 | [diff] [blame] | 1980 | |
Trond Myklebust | 6d09bb6 | 2006-06-04 02:51:37 -0700 | [diff] [blame] | 1981 | fput_fail: |
Ulrich Drepper | 170aa3d | 2006-02-04 23:28:02 -0800 | [diff] [blame] | 1982 | fput_light(file, fput_needed); |
Al Viro | 9b4a9b1 | 2009-04-07 11:44:16 -0400 | [diff] [blame] | 1983 | out_fail: |
| 1984 | return retval; |
| 1985 | } |
| 1986 | |
Al Viro | bd92d7f | 2011-03-14 19:54:59 -0400 | [diff] [blame] | 1987 | static inline int lookup_last(struct nameidata *nd, struct path *path) |
| 1988 | { |
| 1989 | if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len]) |
| 1990 | nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; |
| 1991 | |
| 1992 | nd->flags &= ~LOOKUP_PARENT; |
| 1993 | return walk_component(nd, path, &nd->last, nd->last_type, |
| 1994 | nd->flags & LOOKUP_FOLLOW); |
| 1995 | } |
| 1996 | |
Al Viro | 9b4a9b1 | 2009-04-07 11:44:16 -0400 | [diff] [blame] | 1997 | /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */ |
Al Viro | ee0827c | 2011-02-21 23:38:09 -0500 | [diff] [blame] | 1998 | static int path_lookupat(int dfd, const char *name, |
Al Viro | 9b4a9b1 | 2009-04-07 11:44:16 -0400 | [diff] [blame] | 1999 | unsigned int flags, struct nameidata *nd) |
| 2000 | { |
Al Viro | 70e9b35 | 2011-03-05 21:12:22 -0500 | [diff] [blame] | 2001 | struct file *base = NULL; |
Al Viro | bd92d7f | 2011-03-14 19:54:59 -0400 | [diff] [blame] | 2002 | struct path path; |
| 2003 | int err; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 2004 | |
| 2005 | /* |
| 2006 | * Path walking is largely split up into 2 different synchronisation |
| 2007 | * schemes, rcu-walk and ref-walk (explained in |
| 2008 | * Documentation/filesystems/path-lookup.txt). These share much of the |
| 2009 | * path walk code, but some things particularly setup, cleanup, and |
| 2010 | * following mounts are sufficiently divergent that functions are |
| 2011 | * duplicated. Typically there is a function foo(), and its RCU |
| 2012 | * analogue, foo_rcu(). |
| 2013 | * |
| 2014 | * -ECHILD is the error number of choice (just to avoid clashes) that |
| 2015 | * is returned if some aspect of an rcu-walk fails. Such an error must |
| 2016 | * be handled by restarting a traditional ref-walk (which will always |
| 2017 | * be able to complete). |
| 2018 | */ |
Al Viro | bd92d7f | 2011-03-14 19:54:59 -0400 | [diff] [blame] | 2019 | err = path_init(dfd, name, flags | LOOKUP_PARENT, nd, &base); |
Al Viro | ee0827c | 2011-02-21 23:38:09 -0500 | [diff] [blame] | 2020 | |
Al Viro | bd92d7f | 2011-03-14 19:54:59 -0400 | [diff] [blame] | 2021 | if (unlikely(err)) |
| 2022 | return err; |
Al Viro | ee0827c | 2011-02-21 23:38:09 -0500 | [diff] [blame] | 2023 | |
| 2024 | current->total_link_count = 0; |
Al Viro | bd92d7f | 2011-03-14 19:54:59 -0400 | [diff] [blame] | 2025 | err = link_path_walk(name, nd); |
| 2026 | |
| 2027 | if (!err && !(flags & LOOKUP_PARENT)) { |
Al Viro | bd92d7f | 2011-03-14 19:54:59 -0400 | [diff] [blame] | 2028 | err = lookup_last(nd, &path); |
| 2029 | while (err > 0) { |
| 2030 | void *cookie; |
| 2031 | struct path link = path; |
Kees Cook | 5092e9c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 2032 | err = may_follow_link(&link, nd); |
| 2033 | if (unlikely(err)) |
| 2034 | break; |
Al Viro | bd92d7f | 2011-03-14 19:54:59 -0400 | [diff] [blame] | 2035 | nd->flags |= LOOKUP_PARENT; |
Al Viro | 574197e | 2011-03-14 22:20:34 -0400 | [diff] [blame] | 2036 | err = follow_link(&link, nd, &cookie); |
Al Viro | 2c4d739 | 2012-06-10 04:15:17 -0400 | [diff] [blame] | 2037 | if (err) |
| 2038 | break; |
| 2039 | err = lookup_last(nd, &path); |
Al Viro | 574197e | 2011-03-14 22:20:34 -0400 | [diff] [blame] | 2040 | put_link(nd, &link, cookie); |
Al Viro | bd92d7f | 2011-03-14 19:54:59 -0400 | [diff] [blame] | 2041 | } |
| 2042 | } |
Al Viro | ee0827c | 2011-02-21 23:38:09 -0500 | [diff] [blame] | 2043 | |
Al Viro | 9f1fafe | 2011-03-25 11:00:12 -0400 | [diff] [blame] | 2044 | if (!err) |
| 2045 | err = complete_walk(nd); |
Al Viro | bd92d7f | 2011-03-14 19:54:59 -0400 | [diff] [blame] | 2046 | |
| 2047 | if (!err && nd->flags & LOOKUP_DIRECTORY) { |
Al Viro | a1e1263 | 2013-06-06 19:33:47 -0400 | [diff] [blame] | 2048 | if (!can_lookup(nd->inode)) { |
Al Viro | bd92d7f | 2011-03-14 19:54:59 -0400 | [diff] [blame] | 2049 | path_put(&nd->path); |
Al Viro | bd23a53 | 2011-03-23 09:56:30 -0400 | [diff] [blame] | 2050 | err = -ENOTDIR; |
Al Viro | bd92d7f | 2011-03-14 19:54:59 -0400 | [diff] [blame] | 2051 | } |
| 2052 | } |
Al Viro | 16c2cd7 | 2011-02-22 15:50:10 -0500 | [diff] [blame] | 2053 | |
Tom Marshall | 384972f | 2017-04-28 22:46:37 +0000 | [diff] [blame] | 2054 | if (!err) { |
| 2055 | struct super_block *sb = nd->inode->i_sb; |
| 2056 | if (sb->s_flags & MS_RDONLY) { |
Tom Marshall | 1a4cd21 | 2017-05-19 18:24:49 +0000 | [diff] [blame] | 2057 | if (d_is_su(nd->path.dentry) && !su_visible()) { |
| 2058 | path_put(&nd->path); |
Tom Marshall | 384972f | 2017-04-28 22:46:37 +0000 | [diff] [blame] | 2059 | err = -ENOENT; |
Tom Marshall | 1a4cd21 | 2017-05-19 18:24:49 +0000 | [diff] [blame] | 2060 | } |
Tom Marshall | 384972f | 2017-04-28 22:46:37 +0000 | [diff] [blame] | 2061 | } |
| 2062 | } |
| 2063 | |
Al Viro | 70e9b35 | 2011-03-05 21:12:22 -0500 | [diff] [blame] | 2064 | if (base) |
| 2065 | fput(base); |
Al Viro | ee0827c | 2011-02-21 23:38:09 -0500 | [diff] [blame] | 2066 | |
Al Viro | 5b6ca02 | 2011-03-09 23:04:47 -0500 | [diff] [blame] | 2067 | if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) { |
Al Viro | 2a73787 | 2009-04-07 11:49:53 -0400 | [diff] [blame] | 2068 | path_put(&nd->root); |
| 2069 | nd->root.mnt = NULL; |
| 2070 | } |
Al Viro | bd92d7f | 2011-03-14 19:54:59 -0400 | [diff] [blame] | 2071 | return err; |
Al Viro | ee0827c | 2011-02-21 23:38:09 -0500 | [diff] [blame] | 2072 | } |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 2073 | |
Jeff Layton | 3a9b94c | 2012-10-10 15:25:29 -0400 | [diff] [blame] | 2074 | static int filename_lookup(int dfd, struct filename *name, |
Al Viro | ee0827c | 2011-02-21 23:38:09 -0500 | [diff] [blame] | 2075 | unsigned int flags, struct nameidata *nd) |
| 2076 | { |
Jeff Layton | 3a9b94c | 2012-10-10 15:25:29 -0400 | [diff] [blame] | 2077 | int retval = path_lookupat(dfd, name->name, flags | LOOKUP_RCU, nd); |
Al Viro | ee0827c | 2011-02-21 23:38:09 -0500 | [diff] [blame] | 2078 | if (unlikely(retval == -ECHILD)) |
Jeff Layton | 3a9b94c | 2012-10-10 15:25:29 -0400 | [diff] [blame] | 2079 | retval = path_lookupat(dfd, name->name, flags, nd); |
Al Viro | ee0827c | 2011-02-21 23:38:09 -0500 | [diff] [blame] | 2080 | if (unlikely(retval == -ESTALE)) |
Jeff Layton | 3a9b94c | 2012-10-10 15:25:29 -0400 | [diff] [blame] | 2081 | retval = path_lookupat(dfd, name->name, |
| 2082 | flags | LOOKUP_REVAL, nd); |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 2083 | |
| 2084 | if (likely(!retval)) { |
| 2085 | if (unlikely(!audit_dummy_context())) { |
| 2086 | if (nd->path.dentry && nd->inode) |
Jeff Layton | 3a9b94c | 2012-10-10 15:25:29 -0400 | [diff] [blame] | 2087 | audit_inode(name->name, nd->path.dentry); |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 2088 | } |
| 2089 | } |
Al Viro | 9b4a9b1 | 2009-04-07 11:44:16 -0400 | [diff] [blame] | 2090 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2091 | } |
| 2092 | |
Jeff Layton | 3a9b94c | 2012-10-10 15:25:29 -0400 | [diff] [blame] | 2093 | static int do_path_lookup(int dfd, const char *name, |
| 2094 | unsigned int flags, struct nameidata *nd) |
| 2095 | { |
| 2096 | struct filename filename = { .name = name }; |
| 2097 | |
| 2098 | return filename_lookup(dfd, &filename, flags, nd); |
| 2099 | } |
| 2100 | |
Al Viro | 0613fb8 | 2012-06-15 03:01:42 +0400 | [diff] [blame] | 2101 | /* does lookup, returns the object with parent locked */ |
| 2102 | struct dentry *kern_path_locked(const char *name, struct path *path) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 2103 | { |
Al Viro | 0613fb8 | 2012-06-15 03:01:42 +0400 | [diff] [blame] | 2104 | struct nameidata nd; |
| 2105 | struct dentry *d; |
| 2106 | int err = do_path_lookup(AT_FDCWD, name, LOOKUP_PARENT, &nd); |
| 2107 | if (err) |
| 2108 | return ERR_PTR(err); |
| 2109 | if (nd.last_type != LAST_NORM) { |
| 2110 | path_put(&nd.path); |
| 2111 | return ERR_PTR(-EINVAL); |
| 2112 | } |
| 2113 | mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); |
Al Viro | 59efa86 | 2012-07-22 23:46:21 +0400 | [diff] [blame] | 2114 | d = __lookup_hash(&nd.last, nd.path.dentry, 0); |
Al Viro | 0613fb8 | 2012-06-15 03:01:42 +0400 | [diff] [blame] | 2115 | if (IS_ERR(d)) { |
| 2116 | mutex_unlock(&nd.path.dentry->d_inode->i_mutex); |
| 2117 | path_put(&nd.path); |
| 2118 | return d; |
| 2119 | } |
| 2120 | *path = nd.path; |
| 2121 | return d; |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 2122 | } |
| 2123 | |
Al Viro | d181146 | 2008-08-02 00:49:18 -0400 | [diff] [blame] | 2124 | int kern_path(const char *name, unsigned int flags, struct path *path) |
| 2125 | { |
| 2126 | struct nameidata nd; |
| 2127 | int res = do_path_lookup(AT_FDCWD, name, flags, &nd); |
| 2128 | if (!res) |
| 2129 | *path = nd.path; |
| 2130 | return res; |
| 2131 | } |
| 2132 | |
Josef 'Jeff' Sipek | 16f1820 | 2007-07-19 01:48:18 -0700 | [diff] [blame] | 2133 | /** |
| 2134 | * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair |
| 2135 | * @dentry: pointer to dentry of the base directory |
| 2136 | * @mnt: pointer to vfs mount of the base directory |
| 2137 | * @name: pointer to file name |
| 2138 | * @flags: lookup flags |
Al Viro | e0a0124 | 2011-06-27 17:00:37 -0400 | [diff] [blame] | 2139 | * @path: pointer to struct path to fill |
Josef 'Jeff' Sipek | 16f1820 | 2007-07-19 01:48:18 -0700 | [diff] [blame] | 2140 | */ |
| 2141 | int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt, |
| 2142 | const char *name, unsigned int flags, |
Al Viro | e0a0124 | 2011-06-27 17:00:37 -0400 | [diff] [blame] | 2143 | struct path *path) |
Josef 'Jeff' Sipek | 16f1820 | 2007-07-19 01:48:18 -0700 | [diff] [blame] | 2144 | { |
Al Viro | e0a0124 | 2011-06-27 17:00:37 -0400 | [diff] [blame] | 2145 | struct nameidata nd; |
| 2146 | int err; |
| 2147 | nd.root.dentry = dentry; |
| 2148 | nd.root.mnt = mnt; |
| 2149 | BUG_ON(flags & LOOKUP_PARENT); |
Al Viro | 5b6ca02 | 2011-03-09 23:04:47 -0500 | [diff] [blame] | 2150 | /* the first argument of do_path_lookup() is ignored with LOOKUP_ROOT */ |
Al Viro | e0a0124 | 2011-06-27 17:00:37 -0400 | [diff] [blame] | 2151 | err = do_path_lookup(AT_FDCWD, name, flags | LOOKUP_ROOT, &nd); |
| 2152 | if (!err) |
| 2153 | *path = nd.path; |
| 2154 | return err; |
Josef 'Jeff' Sipek | 16f1820 | 2007-07-19 01:48:18 -0700 | [diff] [blame] | 2155 | } |
| 2156 | |
James Morris | 057f6c0 | 2007-04-26 00:12:05 -0700 | [diff] [blame] | 2157 | /* |
| 2158 | * Restricted form of lookup. Doesn't follow links, single-component only, |
| 2159 | * needs parent already locked. Doesn't follow mounts. |
| 2160 | * SMP-safe. |
| 2161 | */ |
Adrian Bunk | a244e16 | 2006-03-31 02:32:11 -0800 | [diff] [blame] | 2162 | static struct dentry *lookup_hash(struct nameidata *nd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2163 | { |
Al Viro | 292d3af | 2012-06-10 17:17:17 -0400 | [diff] [blame] | 2164 | return __lookup_hash(&nd->last, nd->path.dentry, nd->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2165 | } |
| 2166 | |
Christoph Hellwig | eead191 | 2007-10-16 23:25:38 -0700 | [diff] [blame] | 2167 | /** |
Randy Dunlap | a6b9191 | 2008-03-19 17:01:00 -0700 | [diff] [blame] | 2168 | * lookup_one_len - filesystem helper to lookup single pathname component |
Christoph Hellwig | eead191 | 2007-10-16 23:25:38 -0700 | [diff] [blame] | 2169 | * @name: pathname component to lookup |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 2170 | * @mnt: mount we are looking up on |
Christoph Hellwig | eead191 | 2007-10-16 23:25:38 -0700 | [diff] [blame] | 2171 | * @base: base directory to lookup from |
| 2172 | * @len: maximum length @len should be interpreted to |
| 2173 | * |
Randy Dunlap | a6b9191 | 2008-03-19 17:01:00 -0700 | [diff] [blame] | 2174 | * Note that this routine is purely a helper for filesystem usage and should |
| 2175 | * not be called by generic code. Also note that by using this function the |
Christoph Hellwig | eead191 | 2007-10-16 23:25:38 -0700 | [diff] [blame] | 2176 | * nameidata argument is passed to the filesystem methods and a filesystem |
| 2177 | * using this helper needs to be prepared for that. |
| 2178 | */ |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 2179 | struct dentry *lookup_one_len2(const char *name, struct vfsmount *mnt, struct dentry *base, int len) |
James Morris | 057f6c0 | 2007-04-26 00:12:05 -0700 | [diff] [blame] | 2180 | { |
James Morris | 057f6c0 | 2007-04-26 00:12:05 -0700 | [diff] [blame] | 2181 | struct qstr this; |
Al Viro | 6a96ba5 | 2011-03-07 23:49:20 -0500 | [diff] [blame] | 2182 | unsigned int c; |
Miklos Szeredi | cda309d | 2012-03-26 12:54:21 +0200 | [diff] [blame] | 2183 | int err; |
James Morris | 057f6c0 | 2007-04-26 00:12:05 -0700 | [diff] [blame] | 2184 | |
David Woodhouse | 2f9092e | 2009-04-20 23:18:37 +0100 | [diff] [blame] | 2185 | WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex)); |
| 2186 | |
Al Viro | 6a96ba5 | 2011-03-07 23:49:20 -0500 | [diff] [blame] | 2187 | this.name = name; |
| 2188 | this.len = len; |
Linus Torvalds | 0145acc | 2012-03-02 14:32:59 -0800 | [diff] [blame] | 2189 | this.hash = full_name_hash(name, len); |
Al Viro | 6a96ba5 | 2011-03-07 23:49:20 -0500 | [diff] [blame] | 2190 | if (!len) |
| 2191 | return ERR_PTR(-EACCES); |
| 2192 | |
Al Viro | 04618fb | 2012-11-29 22:17:21 -0500 | [diff] [blame] | 2193 | if (unlikely(name[0] == '.')) { |
| 2194 | if (len < 2 || (len == 2 && name[1] == '.')) |
| 2195 | return ERR_PTR(-EACCES); |
| 2196 | } |
| 2197 | |
Al Viro | 6a96ba5 | 2011-03-07 23:49:20 -0500 | [diff] [blame] | 2198 | while (len--) { |
| 2199 | c = *(const unsigned char *)name++; |
| 2200 | if (c == '/' || c == '\0') |
| 2201 | return ERR_PTR(-EACCES); |
Al Viro | 6a96ba5 | 2011-03-07 23:49:20 -0500 | [diff] [blame] | 2202 | } |
Al Viro | 5a202bc | 2011-03-08 14:17:44 -0500 | [diff] [blame] | 2203 | /* |
| 2204 | * See if the low-level filesystem might want |
| 2205 | * to use its own hash.. |
| 2206 | */ |
| 2207 | if (base->d_flags & DCACHE_OP_HASH) { |
| 2208 | int err = base->d_op->d_hash(base, base->d_inode, &this); |
| 2209 | if (err < 0) |
| 2210 | return ERR_PTR(err); |
| 2211 | } |
Christoph Hellwig | eead191 | 2007-10-16 23:25:38 -0700 | [diff] [blame] | 2212 | |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 2213 | err = inode_permission2(mnt, base->d_inode, MAY_EXEC); |
Miklos Szeredi | cda309d | 2012-03-26 12:54:21 +0200 | [diff] [blame] | 2214 | if (err) |
| 2215 | return ERR_PTR(err); |
| 2216 | |
Al Viro | 292d3af | 2012-06-10 17:17:17 -0400 | [diff] [blame] | 2217 | return __lookup_hash(&this, base, 0); |
James Morris | 057f6c0 | 2007-04-26 00:12:05 -0700 | [diff] [blame] | 2218 | } |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 2219 | EXPORT_SYMBOL(lookup_one_len2); |
| 2220 | |
| 2221 | struct dentry *lookup_one_len(const char *name, struct dentry *base, int len) |
| 2222 | { |
| 2223 | return lookup_one_len2(name, NULL, base, len); |
| 2224 | } |
| 2225 | EXPORT_SYMBOL(lookup_one_len); |
James Morris | 057f6c0 | 2007-04-26 00:12:05 -0700 | [diff] [blame] | 2226 | |
Andy Whitcroft | 1fa1e7f | 2011-11-02 09:44:39 +0100 | [diff] [blame] | 2227 | int user_path_at_empty(int dfd, const char __user *name, unsigned flags, |
| 2228 | struct path *path, int *empty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2229 | { |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 2230 | struct nameidata nd; |
Jeff Layton | 9cee5ca | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 2231 | struct filename *tmp = getname_flags(name, flags, empty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2232 | int err = PTR_ERR(tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2233 | if (!IS_ERR(tmp)) { |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 2234 | |
| 2235 | BUG_ON(flags & LOOKUP_PARENT); |
| 2236 | |
Jeff Layton | 3a9b94c | 2012-10-10 15:25:29 -0400 | [diff] [blame] | 2237 | err = filename_lookup(dfd, tmp, flags, &nd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2238 | putname(tmp); |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 2239 | if (!err) |
| 2240 | *path = nd.path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2241 | } |
| 2242 | return err; |
| 2243 | } |
| 2244 | |
Andy Whitcroft | 1fa1e7f | 2011-11-02 09:44:39 +0100 | [diff] [blame] | 2245 | int user_path_at(int dfd, const char __user *name, unsigned flags, |
| 2246 | struct path *path) |
| 2247 | { |
Linus Torvalds | f7493e5 | 2012-03-22 16:10:40 -0700 | [diff] [blame] | 2248 | return user_path_at_empty(dfd, name, flags, path, NULL); |
Andy Whitcroft | 1fa1e7f | 2011-11-02 09:44:39 +0100 | [diff] [blame] | 2249 | } |
| 2250 | |
Jeff Layton | 3a9b94c | 2012-10-10 15:25:29 -0400 | [diff] [blame] | 2251 | /* |
| 2252 | * NB: most callers don't do anything directly with the reference to the |
| 2253 | * to struct filename, but the nd->last pointer points into the name string |
| 2254 | * allocated by getname. So we must hold the reference to it until all |
| 2255 | * path-walking is complete. |
| 2256 | */ |
Jeff Layton | 9cee5ca | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 2257 | static struct filename * |
| 2258 | user_path_parent(int dfd, const char __user *path, struct nameidata *nd) |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 2259 | { |
Jeff Layton | 9cee5ca | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 2260 | struct filename *s = getname(path); |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 2261 | int error; |
| 2262 | |
| 2263 | if (IS_ERR(s)) |
Jeff Layton | 9cee5ca | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 2264 | return s; |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 2265 | |
Jeff Layton | 3a9b94c | 2012-10-10 15:25:29 -0400 | [diff] [blame] | 2266 | error = filename_lookup(dfd, s, LOOKUP_PARENT, nd); |
Jeff Layton | 9cee5ca | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 2267 | if (error) { |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 2268 | putname(s); |
Jeff Layton | 9cee5ca | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 2269 | return ERR_PTR(error); |
| 2270 | } |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 2271 | |
Jeff Layton | 9cee5ca | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 2272 | return s; |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 2273 | } |
| 2274 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2275 | /* |
| 2276 | * It's inline, so penalty for filesystems that don't use sticky bit is |
| 2277 | * minimal. |
| 2278 | */ |
| 2279 | static inline int check_sticky(struct inode *dir, struct inode *inode) |
| 2280 | { |
David Howells | da9592e | 2008-11-14 10:39:05 +1100 | [diff] [blame] | 2281 | uid_t fsuid = current_fsuid(); |
| 2282 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2283 | if (!(dir->i_mode & S_ISVTX)) |
| 2284 | return 0; |
David Howells | da9592e | 2008-11-14 10:39:05 +1100 | [diff] [blame] | 2285 | if (inode->i_uid == fsuid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2286 | return 0; |
David Howells | da9592e | 2008-11-14 10:39:05 +1100 | [diff] [blame] | 2287 | if (dir->i_uid == fsuid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2288 | return 0; |
Eric W. Biederman | d6814e26 | 2011-11-14 16:24:06 -0800 | [diff] [blame] | 2289 | return !inode_capable(inode, CAP_FOWNER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2290 | } |
| 2291 | |
| 2292 | /* |
| 2293 | * Check whether we can remove a link victim from directory dir, check |
| 2294 | * whether the type of victim is right. |
| 2295 | * 1. We can't do it if dir is read-only (done in permission()) |
| 2296 | * 2. We should have write and exec permissions on dir |
| 2297 | * 3. We can't remove anything from append-only dir |
| 2298 | * 4. We can't do anything with immutable dir (done in permission()) |
| 2299 | * 5. If the sticky bit on dir is set we should either |
| 2300 | * a. be owner of dir, or |
| 2301 | * b. be owner of victim, or |
| 2302 | * c. have CAP_FOWNER capability |
| 2303 | * 6. If the victim is append-only or immutable we can't do antyhing with |
| 2304 | * links pointing to it. |
| 2305 | * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. |
| 2306 | * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. |
| 2307 | * 9. We can't remove a root or mountpoint. |
| 2308 | * 10. We don't allow removal of NFS sillyrenamed files; it's handled by |
| 2309 | * nfs_async_unlink(). |
| 2310 | */ |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 2311 | static int may_delete(struct vfsmount *mnt, struct inode *dir,struct dentry *victim,int isdir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2312 | { |
| 2313 | int error; |
| 2314 | |
| 2315 | if (!victim->d_inode) |
| 2316 | return -ENOENT; |
| 2317 | |
| 2318 | BUG_ON(victim->d_parent->d_inode != dir); |
Al Viro | cccc6bb | 2009-12-25 05:07:33 -0500 | [diff] [blame] | 2319 | audit_inode_child(victim, dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2320 | |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 2321 | error = inode_permission2(mnt, dir, MAY_WRITE | MAY_EXEC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2322 | if (error) |
| 2323 | return error; |
| 2324 | if (IS_APPEND(dir)) |
| 2325 | return -EPERM; |
| 2326 | if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)|| |
Hugh Dickins | f945454 | 2008-11-19 15:36:38 -0800 | [diff] [blame] | 2327 | IS_IMMUTABLE(victim->d_inode) || IS_SWAPFILE(victim->d_inode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2328 | return -EPERM; |
| 2329 | if (isdir) { |
| 2330 | if (!S_ISDIR(victim->d_inode->i_mode)) |
| 2331 | return -ENOTDIR; |
| 2332 | if (IS_ROOT(victim)) |
| 2333 | return -EBUSY; |
| 2334 | } else if (S_ISDIR(victim->d_inode->i_mode)) |
| 2335 | return -EISDIR; |
| 2336 | if (IS_DEADDIR(dir)) |
| 2337 | return -ENOENT; |
| 2338 | if (victim->d_flags & DCACHE_NFSFS_RENAMED) |
| 2339 | return -EBUSY; |
| 2340 | return 0; |
| 2341 | } |
| 2342 | |
| 2343 | /* Check whether we can create an object with dentry child in directory |
| 2344 | * dir. |
| 2345 | * 1. We can't do it if child already exists (open has special treatment for |
| 2346 | * this case, but since we are inlined it's OK) |
| 2347 | * 2. We can't do it if dir is read-only (done in permission()) |
| 2348 | * 3. We should have write and exec permissions on dir |
| 2349 | * 4. We can't do it if dir is immutable (done in permission()) |
| 2350 | */ |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 2351 | static inline int may_create(struct vfsmount *mnt, struct inode *dir, struct dentry *child) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2352 | { |
| 2353 | if (child->d_inode) |
| 2354 | return -EEXIST; |
| 2355 | if (IS_DEADDIR(dir)) |
| 2356 | return -ENOENT; |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 2357 | return inode_permission2(mnt, dir, MAY_WRITE | MAY_EXEC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2358 | } |
| 2359 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2360 | /* |
| 2361 | * p1 and p2 should be directories on the same fs. |
| 2362 | */ |
| 2363 | struct dentry *lock_rename(struct dentry *p1, struct dentry *p2) |
| 2364 | { |
| 2365 | struct dentry *p; |
| 2366 | |
| 2367 | if (p1 == p2) { |
Ingo Molnar | f2eace2 | 2006-07-03 00:25:05 -0700 | [diff] [blame] | 2368 | mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2369 | return NULL; |
| 2370 | } |
| 2371 | |
Arjan van de Ven | a11f3a0 | 2006-03-23 03:00:33 -0800 | [diff] [blame] | 2372 | mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2373 | |
OGAWA Hirofumi | e2761a1 | 2008-10-16 07:50:28 +0900 | [diff] [blame] | 2374 | p = d_ancestor(p2, p1); |
| 2375 | if (p) { |
| 2376 | mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT); |
| 2377 | mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD); |
| 2378 | return p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2379 | } |
| 2380 | |
OGAWA Hirofumi | e2761a1 | 2008-10-16 07:50:28 +0900 | [diff] [blame] | 2381 | p = d_ancestor(p1, p2); |
| 2382 | if (p) { |
| 2383 | mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); |
| 2384 | mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); |
| 2385 | return p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2386 | } |
| 2387 | |
Ingo Molnar | f2eace2 | 2006-07-03 00:25:05 -0700 | [diff] [blame] | 2388 | mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); |
| 2389 | mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2390 | return NULL; |
| 2391 | } |
| 2392 | |
| 2393 | void unlock_rename(struct dentry *p1, struct dentry *p2) |
| 2394 | { |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 2395 | mutex_unlock(&p1->d_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2396 | if (p1 != p2) { |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 2397 | mutex_unlock(&p2->d_inode->i_mutex); |
Arjan van de Ven | a11f3a0 | 2006-03-23 03:00:33 -0800 | [diff] [blame] | 2398 | mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2399 | } |
| 2400 | } |
| 2401 | |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 2402 | int vfs_create2(struct vfsmount *mnt, struct inode *dir, struct dentry *dentry, |
Al Viro | 8da0eaf | 2012-06-10 18:09:36 -0400 | [diff] [blame] | 2403 | umode_t mode, bool want_excl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2404 | { |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 2405 | int error = may_create(mnt, dir, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2406 | if (error) |
| 2407 | return error; |
| 2408 | |
Al Viro | acfa438 | 2008-12-04 10:06:33 -0500 | [diff] [blame] | 2409 | if (!dir->i_op->create) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2410 | return -EACCES; /* shouldn't it be ENOSYS? */ |
| 2411 | mode &= S_IALLUGO; |
| 2412 | mode |= S_IFREG; |
| 2413 | error = security_inode_create(dir, dentry, mode); |
| 2414 | if (error) |
| 2415 | return error; |
Al Viro | 8da0eaf | 2012-06-10 18:09:36 -0400 | [diff] [blame] | 2416 | error = dir->i_op->create(dir, dentry, mode, want_excl); |
Amir Samuelov | 6a22e46 | 2014-05-26 11:44:06 +0300 | [diff] [blame] | 2417 | if (error) |
| 2418 | return error; |
| 2419 | |
| 2420 | error = security_inode_post_create(dir, dentry, mode); |
| 2421 | if (error) |
| 2422 | return error; |
| 2423 | |
Stephen Smalley | a74574a | 2005-09-09 13:01:44 -0700 | [diff] [blame] | 2424 | if (!error) |
Amy Griffis | f38aa94 | 2005-11-03 15:57:06 +0000 | [diff] [blame] | 2425 | fsnotify_create(dir, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2426 | return error; |
| 2427 | } |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 2428 | EXPORT_SYMBOL(vfs_create2); |
| 2429 | |
| 2430 | int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode, |
Al Viro | 8da0eaf | 2012-06-10 18:09:36 -0400 | [diff] [blame] | 2431 | bool want_excl) |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 2432 | { |
Al Viro | 8da0eaf | 2012-06-10 18:09:36 -0400 | [diff] [blame] | 2433 | return vfs_create2(NULL, dir, dentry, mode, want_excl); |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 2434 | } |
| 2435 | EXPORT_SYMBOL(vfs_create); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2436 | |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 2437 | static int may_open(struct path *path, int acc_mode, int flag) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2438 | { |
Christoph Hellwig | 3fb6419 | 2008-10-24 09:58:10 +0200 | [diff] [blame] | 2439 | struct dentry *dentry = path->dentry; |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 2440 | struct vfsmount *mnt = path->mnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2441 | struct inode *inode = dentry->d_inode; |
| 2442 | int error; |
| 2443 | |
Al Viro | bcda765 | 2011-03-13 16:42:14 -0400 | [diff] [blame] | 2444 | /* O_PATH? */ |
| 2445 | if (!acc_mode) |
| 2446 | return 0; |
| 2447 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2448 | if (!inode) |
| 2449 | return -ENOENT; |
| 2450 | |
Christoph Hellwig | c8fe8f3 | 2009-01-05 19:27:23 +0100 | [diff] [blame] | 2451 | switch (inode->i_mode & S_IFMT) { |
| 2452 | case S_IFLNK: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2453 | return -ELOOP; |
Christoph Hellwig | c8fe8f3 | 2009-01-05 19:27:23 +0100 | [diff] [blame] | 2454 | case S_IFDIR: |
| 2455 | if (acc_mode & MAY_WRITE) |
| 2456 | return -EISDIR; |
| 2457 | break; |
| 2458 | case S_IFBLK: |
| 2459 | case S_IFCHR: |
Christoph Hellwig | 3fb6419 | 2008-10-24 09:58:10 +0200 | [diff] [blame] | 2460 | if (path->mnt->mnt_flags & MNT_NODEV) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2461 | return -EACCES; |
Christoph Hellwig | c8fe8f3 | 2009-01-05 19:27:23 +0100 | [diff] [blame] | 2462 | /*FALLTHRU*/ |
| 2463 | case S_IFIFO: |
| 2464 | case S_IFSOCK: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2465 | flag &= ~O_TRUNC; |
Christoph Hellwig | c8fe8f3 | 2009-01-05 19:27:23 +0100 | [diff] [blame] | 2466 | break; |
Dave Hansen | 4a3fd21 | 2008-02-15 14:37:48 -0800 | [diff] [blame] | 2467 | } |
Dave Hansen | b41572e | 2007-10-16 23:31:14 -0700 | [diff] [blame] | 2468 | |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 2469 | error = inode_permission2(mnt, inode, acc_mode); |
Dave Hansen | b41572e | 2007-10-16 23:31:14 -0700 | [diff] [blame] | 2470 | if (error) |
| 2471 | return error; |
Mimi Zohar | 6146f0d | 2009-02-04 09:06:57 -0500 | [diff] [blame] | 2472 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2473 | /* |
| 2474 | * An append-only file must be opened in append mode for writing. |
| 2475 | */ |
| 2476 | if (IS_APPEND(inode)) { |
Al Viro | 8737c93 | 2009-12-24 06:47:55 -0500 | [diff] [blame] | 2477 | if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND)) |
Al Viro | 7715b52 | 2009-12-16 03:54:00 -0500 | [diff] [blame] | 2478 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2479 | if (flag & O_TRUNC) |
Al Viro | 7715b52 | 2009-12-16 03:54:00 -0500 | [diff] [blame] | 2480 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2481 | } |
| 2482 | |
| 2483 | /* O_NOATIME can only be set by the owner or superuser */ |
Serge E. Hallyn | 2e14967 | 2011-03-23 16:43:26 -0700 | [diff] [blame] | 2484 | if (flag & O_NOATIME && !inode_owner_or_capable(inode)) |
Al Viro | 7715b52 | 2009-12-16 03:54:00 -0500 | [diff] [blame] | 2485 | return -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2486 | |
J. Bruce Fields | f3c7691e | 2011-09-21 10:58:13 -0400 | [diff] [blame] | 2487 | return 0; |
Al Viro | 7715b52 | 2009-12-16 03:54:00 -0500 | [diff] [blame] | 2488 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2489 | |
Jeff Layton | e1181ee | 2010-12-07 16:19:50 -0500 | [diff] [blame] | 2490 | static int handle_truncate(struct file *filp) |
Al Viro | 7715b52 | 2009-12-16 03:54:00 -0500 | [diff] [blame] | 2491 | { |
Jeff Layton | e1181ee | 2010-12-07 16:19:50 -0500 | [diff] [blame] | 2492 | struct path *path = &filp->f_path; |
Al Viro | 7715b52 | 2009-12-16 03:54:00 -0500 | [diff] [blame] | 2493 | struct inode *inode = path->dentry->d_inode; |
| 2494 | int error = get_write_access(inode); |
| 2495 | if (error) |
| 2496 | return error; |
| 2497 | /* |
| 2498 | * Refuse to truncate files with mandatory locks held on them. |
| 2499 | */ |
| 2500 | error = locks_verify_locked(inode); |
| 2501 | if (!error) |
Tetsuo Handa | ea0d3ab | 2010-06-02 13:24:43 +0900 | [diff] [blame] | 2502 | error = security_path_truncate(path); |
Al Viro | 7715b52 | 2009-12-16 03:54:00 -0500 | [diff] [blame] | 2503 | if (!error) { |
Daniel Rosenberg | a88a18f | 2016-12-27 16:36:40 -0800 | [diff] [blame] | 2504 | error = do_truncate2(path->mnt, path->dentry, 0, |
Al Viro | 7715b52 | 2009-12-16 03:54:00 -0500 | [diff] [blame] | 2505 | ATTR_MTIME|ATTR_CTIME|ATTR_OPEN, |
Jeff Layton | e1181ee | 2010-12-07 16:19:50 -0500 | [diff] [blame] | 2506 | filp); |
Al Viro | 7715b52 | 2009-12-16 03:54:00 -0500 | [diff] [blame] | 2507 | } |
| 2508 | put_write_access(inode); |
Mimi Zohar | acd0c93 | 2009-09-04 13:08:46 -0400 | [diff] [blame] | 2509 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2510 | } |
| 2511 | |
Dave Hansen | d57999e | 2008-02-15 14:37:27 -0800 | [diff] [blame] | 2512 | static inline int open_to_namei_flags(int flag) |
| 2513 | { |
Al Viro | 8a5e929 | 2011-06-25 19:15:54 -0400 | [diff] [blame] | 2514 | if ((flag & O_ACCMODE) == 3) |
| 2515 | flag--; |
Dave Hansen | d57999e | 2008-02-15 14:37:27 -0800 | [diff] [blame] | 2516 | return flag; |
| 2517 | } |
| 2518 | |
Miklos Szeredi | 48b105f | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2519 | static int may_o_create(struct path *dir, struct dentry *dentry, umode_t mode) |
| 2520 | { |
| 2521 | int error = security_path_mknod(dir, dentry, mode, 0); |
| 2522 | if (error) |
| 2523 | return error; |
| 2524 | |
| 2525 | error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC); |
| 2526 | if (error) |
| 2527 | return error; |
| 2528 | |
| 2529 | return security_inode_create(dir->dentry->d_inode, dentry, mode); |
| 2530 | } |
| 2531 | |
David Howells | 9898cc8 | 2012-06-14 16:13:46 +0100 | [diff] [blame] | 2532 | /* |
| 2533 | * Attempt to atomically look up, create and open a file from a negative |
| 2534 | * dentry. |
| 2535 | * |
| 2536 | * Returns 0 if successful. The file will have been created and attached to |
| 2537 | * @file by the filesystem calling finish_open(). |
| 2538 | * |
| 2539 | * Returns 1 if the file was looked up only or didn't need creating. The |
| 2540 | * caller will need to perform the open themselves. @path will have been |
| 2541 | * updated to point to the new dentry. This may be negative. |
| 2542 | * |
| 2543 | * Returns an error code otherwise. |
| 2544 | */ |
Al Viro | 70806c3 | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2545 | static int atomic_open(struct nameidata *nd, struct dentry *dentry, |
| 2546 | struct path *path, struct file *file, |
| 2547 | const struct open_flags *op, |
Al Viro | 8c162b9 | 2012-07-31 00:53:35 +0400 | [diff] [blame] | 2548 | bool got_write, bool need_lookup, |
Al Viro | 70806c3 | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2549 | int *opened) |
Miklos Szeredi | 48b105f | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2550 | { |
| 2551 | struct inode *dir = nd->path.dentry->d_inode; |
| 2552 | unsigned open_flag = open_to_namei_flags(op->open_flag); |
| 2553 | umode_t mode; |
| 2554 | int error; |
| 2555 | int acc_mode; |
Miklos Szeredi | 48b105f | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2556 | int create_error = 0; |
| 2557 | struct dentry *const DENTRY_NOT_SET = (void *) -1UL; |
| 2558 | |
| 2559 | BUG_ON(dentry->d_inode); |
| 2560 | |
| 2561 | /* Don't create child dentry for a dead directory. */ |
| 2562 | if (unlikely(IS_DEADDIR(dir))) { |
Al Viro | 70806c3 | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2563 | error = -ENOENT; |
Miklos Szeredi | 48b105f | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2564 | goto out; |
| 2565 | } |
| 2566 | |
Miklos Szeredi | 99fdd3a | 2012-08-15 13:01:24 +0200 | [diff] [blame] | 2567 | mode = op->mode; |
Miklos Szeredi | 48b105f | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2568 | if ((open_flag & O_CREAT) && !IS_POSIXACL(dir)) |
| 2569 | mode &= ~current_umask(); |
| 2570 | |
Al Viro | 5b2d9ff | 2012-07-30 11:50:30 +0400 | [diff] [blame] | 2571 | if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT)) { |
Miklos Szeredi | 48b105f | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2572 | open_flag &= ~O_TRUNC; |
Al Viro | 8915ef3 | 2012-06-10 05:01:45 -0400 | [diff] [blame] | 2573 | *opened |= FILE_CREATED; |
Miklos Szeredi | 48b105f | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2574 | } |
| 2575 | |
| 2576 | /* |
| 2577 | * Checking write permission is tricky, bacuse we don't know if we are |
| 2578 | * going to actually need it: O_CREAT opens should work as long as the |
| 2579 | * file exists. But checking existence breaks atomicity. The trick is |
| 2580 | * to check access and if not granted clear O_CREAT from the flags. |
| 2581 | * |
| 2582 | * Another problem is returing the "right" error value (e.g. for an |
| 2583 | * O_EXCL open we want to return EEXIST not EROFS). |
| 2584 | */ |
Al Viro | 8c162b9 | 2012-07-31 00:53:35 +0400 | [diff] [blame] | 2585 | if (((open_flag & (O_CREAT | O_TRUNC)) || |
| 2586 | (open_flag & O_ACCMODE) != O_RDONLY) && unlikely(!got_write)) { |
| 2587 | if (!(open_flag & O_CREAT)) { |
Miklos Szeredi | 48b105f | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2588 | /* |
| 2589 | * No O_CREATE -> atomicity not a requirement -> fall |
| 2590 | * back to lookup + open |
| 2591 | */ |
| 2592 | goto no_open; |
| 2593 | } else if (open_flag & (O_EXCL | O_TRUNC)) { |
| 2594 | /* Fall back and fail with the right error */ |
Al Viro | 8c162b9 | 2012-07-31 00:53:35 +0400 | [diff] [blame] | 2595 | create_error = -EROFS; |
Miklos Szeredi | 48b105f | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2596 | goto no_open; |
| 2597 | } else { |
| 2598 | /* No side effects, safe to clear O_CREAT */ |
Al Viro | 8c162b9 | 2012-07-31 00:53:35 +0400 | [diff] [blame] | 2599 | create_error = -EROFS; |
Miklos Szeredi | 48b105f | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2600 | open_flag &= ~O_CREAT; |
| 2601 | } |
| 2602 | } |
| 2603 | |
| 2604 | if (open_flag & O_CREAT) { |
Miklos Szeredi | 63bab5b | 2012-08-15 13:01:24 +0200 | [diff] [blame] | 2605 | error = may_o_create(&nd->path, dentry, mode); |
Miklos Szeredi | 48b105f | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2606 | if (error) { |
| 2607 | create_error = error; |
| 2608 | if (open_flag & O_EXCL) |
| 2609 | goto no_open; |
| 2610 | open_flag &= ~O_CREAT; |
| 2611 | } |
| 2612 | } |
| 2613 | |
| 2614 | if (nd->flags & LOOKUP_DIRECTORY) |
| 2615 | open_flag |= O_DIRECTORY; |
| 2616 | |
Al Viro | b2ebdc5 | 2012-06-22 12:40:19 +0400 | [diff] [blame] | 2617 | file->f_path.dentry = DENTRY_NOT_SET; |
| 2618 | file->f_path.mnt = nd->path.mnt; |
| 2619 | error = dir->i_op->atomic_open(dir, dentry, file, open_flag, mode, |
Al Viro | 8915ef3 | 2012-06-10 05:01:45 -0400 | [diff] [blame] | 2620 | opened); |
Al Viro | 50ed02d | 2012-06-22 12:39:14 +0400 | [diff] [blame] | 2621 | if (error < 0) { |
Al Viro | 50ed02d | 2012-06-22 12:39:14 +0400 | [diff] [blame] | 2622 | if (create_error && error == -ENOENT) |
| 2623 | error = create_error; |
Miklos Szeredi | 48b105f | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2624 | goto out; |
| 2625 | } |
| 2626 | |
| 2627 | acc_mode = op->acc_mode; |
Al Viro | 8915ef3 | 2012-06-10 05:01:45 -0400 | [diff] [blame] | 2628 | if (*opened & FILE_CREATED) { |
Miklos Szeredi | 48b105f | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2629 | fsnotify_create(dir, dentry); |
| 2630 | acc_mode = MAY_OPEN; |
| 2631 | } |
| 2632 | |
Al Viro | 50ed02d | 2012-06-22 12:39:14 +0400 | [diff] [blame] | 2633 | if (error) { /* returned 1, that is */ |
Al Viro | b2ebdc5 | 2012-06-22 12:40:19 +0400 | [diff] [blame] | 2634 | if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) { |
Al Viro | 70806c3 | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2635 | error = -EIO; |
Miklos Szeredi | 48b105f | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2636 | goto out; |
| 2637 | } |
Al Viro | b2ebdc5 | 2012-06-22 12:40:19 +0400 | [diff] [blame] | 2638 | if (file->f_path.dentry) { |
Miklos Szeredi | 48b105f | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2639 | dput(dentry); |
Al Viro | b2ebdc5 | 2012-06-22 12:40:19 +0400 | [diff] [blame] | 2640 | dentry = file->f_path.dentry; |
Miklos Szeredi | 48b105f | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2641 | } |
Sage Weil | b5e96de | 2012-08-15 13:30:12 -0700 | [diff] [blame] | 2642 | if (create_error && dentry->d_inode == NULL) { |
| 2643 | error = create_error; |
| 2644 | goto out; |
| 2645 | } |
Miklos Szeredi | 48b105f | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2646 | goto looked_up; |
| 2647 | } |
| 2648 | |
| 2649 | /* |
| 2650 | * We didn't have the inode before the open, so check open permission |
| 2651 | * here. |
| 2652 | */ |
Al Viro | 70806c3 | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2653 | error = may_open(&file->f_path, acc_mode, open_flag); |
| 2654 | if (error) |
| 2655 | fput(file); |
Miklos Szeredi | 48b105f | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2656 | |
| 2657 | out: |
| 2658 | dput(dentry); |
Al Viro | 70806c3 | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2659 | return error; |
Miklos Szeredi | 48b105f | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2660 | |
Miklos Szeredi | 48b105f | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2661 | no_open: |
| 2662 | if (need_lookup) { |
Al Viro | 292d3af | 2012-06-10 17:17:17 -0400 | [diff] [blame] | 2663 | dentry = lookup_real(dir, dentry, nd->flags); |
Miklos Szeredi | 48b105f | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2664 | if (IS_ERR(dentry)) |
Al Viro | 70806c3 | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2665 | return PTR_ERR(dentry); |
Miklos Szeredi | 48b105f | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2666 | |
| 2667 | if (create_error) { |
| 2668 | int open_flag = op->open_flag; |
| 2669 | |
Al Viro | 70806c3 | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2670 | error = create_error; |
Miklos Szeredi | 48b105f | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2671 | if ((open_flag & O_EXCL)) { |
| 2672 | if (!dentry->d_inode) |
| 2673 | goto out; |
| 2674 | } else if (!dentry->d_inode) { |
| 2675 | goto out; |
| 2676 | } else if ((open_flag & O_TRUNC) && |
| 2677 | S_ISREG(dentry->d_inode->i_mode)) { |
| 2678 | goto out; |
| 2679 | } |
| 2680 | /* will fail later, go on to get the right error */ |
| 2681 | } |
| 2682 | } |
| 2683 | looked_up: |
| 2684 | path->dentry = dentry; |
| 2685 | path->mnt = nd->path.mnt; |
Al Viro | 70806c3 | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2686 | return 1; |
Miklos Szeredi | 48b105f | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2687 | } |
| 2688 | |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 2689 | /* |
David Howells | 9898cc8 | 2012-06-14 16:13:46 +0100 | [diff] [blame] | 2690 | * Look up and maybe create and open the last component. |
Miklos Szeredi | 8632d4d | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 2691 | * |
| 2692 | * Must be called with i_mutex held on parent. |
| 2693 | * |
David Howells | 9898cc8 | 2012-06-14 16:13:46 +0100 | [diff] [blame] | 2694 | * Returns 0 if the file was successfully atomically created (if necessary) and |
| 2695 | * opened. In this case the file will be returned attached to @file. |
| 2696 | * |
| 2697 | * Returns 1 if the file was not completely opened at this time, though lookups |
| 2698 | * and creations will have been performed and the dentry returned in @path will |
| 2699 | * be positive upon return if O_CREAT was specified. If O_CREAT wasn't |
| 2700 | * specified then a negative dentry may be returned. |
| 2701 | * |
| 2702 | * An error code is returned otherwise. |
| 2703 | * |
| 2704 | * FILE_CREATE will be set in @*opened if the dentry was created and will be |
| 2705 | * cleared otherwise prior to returning. |
Miklos Szeredi | 8632d4d | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 2706 | */ |
Al Viro | 70806c3 | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2707 | static int lookup_open(struct nameidata *nd, struct path *path, |
| 2708 | struct file *file, |
| 2709 | const struct open_flags *op, |
Al Viro | 8c162b9 | 2012-07-31 00:53:35 +0400 | [diff] [blame] | 2710 | bool got_write, int *opened) |
Miklos Szeredi | 8632d4d | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 2711 | { |
| 2712 | struct dentry *dir = nd->path.dentry; |
Miklos Szeredi | 1060664 | 2012-06-05 15:10:16 +0200 | [diff] [blame] | 2713 | struct inode *dir_inode = dir->d_inode; |
Miklos Szeredi | 8632d4d | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 2714 | struct vfsmount *mnt = nd->path.mnt; |
| 2715 | struct dentry *dentry; |
| 2716 | int error; |
Miklos Szeredi | 1060664 | 2012-06-05 15:10:16 +0200 | [diff] [blame] | 2717 | bool need_lookup; |
Miklos Szeredi | 8632d4d | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 2718 | |
Al Viro | 8915ef3 | 2012-06-10 05:01:45 -0400 | [diff] [blame] | 2719 | *opened &= ~FILE_CREATED; |
Al Viro | 61b61da | 2012-06-22 12:42:10 +0400 | [diff] [blame] | 2720 | dentry = lookup_dcache(&nd->last, dir, nd->flags, &need_lookup); |
Miklos Szeredi | 8632d4d | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 2721 | if (IS_ERR(dentry)) |
Al Viro | 70806c3 | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2722 | return PTR_ERR(dentry); |
Miklos Szeredi | 8632d4d | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 2723 | |
Miklos Szeredi | 48b105f | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2724 | /* Cached positive dentry: will open in f_op->open */ |
| 2725 | if (!need_lookup && dentry->d_inode) |
| 2726 | goto out_no_open; |
| 2727 | |
| 2728 | if ((nd->flags & LOOKUP_OPEN) && dir_inode->i_op->atomic_open) { |
Al Viro | 8c162b9 | 2012-07-31 00:53:35 +0400 | [diff] [blame] | 2729 | return atomic_open(nd, dentry, path, file, op, got_write, |
Al Viro | 8915ef3 | 2012-06-10 05:01:45 -0400 | [diff] [blame] | 2730 | need_lookup, opened); |
Miklos Szeredi | 48b105f | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2731 | } |
| 2732 | |
Miklos Szeredi | 1060664 | 2012-06-05 15:10:16 +0200 | [diff] [blame] | 2733 | if (need_lookup) { |
| 2734 | BUG_ON(dentry->d_inode); |
| 2735 | |
Al Viro | 292d3af | 2012-06-10 17:17:17 -0400 | [diff] [blame] | 2736 | dentry = lookup_real(dir_inode, dentry, nd->flags); |
Miklos Szeredi | 1060664 | 2012-06-05 15:10:16 +0200 | [diff] [blame] | 2737 | if (IS_ERR(dentry)) |
Al Viro | 70806c3 | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2738 | return PTR_ERR(dentry); |
Miklos Szeredi | 1060664 | 2012-06-05 15:10:16 +0200 | [diff] [blame] | 2739 | } |
| 2740 | |
Miklos Szeredi | 8632d4d | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 2741 | /* Negative dentry, just create the file */ |
| 2742 | if (!dentry->d_inode && (op->open_flag & O_CREAT)) { |
| 2743 | umode_t mode = op->mode; |
| 2744 | if (!IS_POSIXACL(dir->d_inode)) |
| 2745 | mode &= ~current_umask(); |
| 2746 | /* |
| 2747 | * This write is needed to ensure that a |
| 2748 | * rw->ro transition does not occur between |
| 2749 | * the time when the file is created and when |
| 2750 | * a permanent write count is taken through |
Miklos Szeredi | 2f80ad3 | 2012-06-05 15:10:27 +0200 | [diff] [blame] | 2751 | * the 'struct file' in finish_open(). |
Miklos Szeredi | 8632d4d | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 2752 | */ |
Al Viro | 8c162b9 | 2012-07-31 00:53:35 +0400 | [diff] [blame] | 2753 | if (!got_write) { |
| 2754 | error = -EROFS; |
Miklos Szeredi | 8632d4d | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 2755 | goto out_dput; |
Al Viro | 8c162b9 | 2012-07-31 00:53:35 +0400 | [diff] [blame] | 2756 | } |
Al Viro | 8915ef3 | 2012-06-10 05:01:45 -0400 | [diff] [blame] | 2757 | *opened |= FILE_CREATED; |
Miklos Szeredi | 8632d4d | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 2758 | error = security_path_mknod(&nd->path, dentry, mode, 0); |
| 2759 | if (error) |
| 2760 | goto out_dput; |
Al Viro | 8da0eaf | 2012-06-10 18:09:36 -0400 | [diff] [blame] | 2761 | error = vfs_create2(mnt, dir->d_inode, dentry, mode, |
| 2762 | nd->flags & LOOKUP_EXCL); |
Miklos Szeredi | 8632d4d | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 2763 | if (error) |
| 2764 | goto out_dput; |
| 2765 | } |
Miklos Szeredi | 48b105f | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2766 | out_no_open: |
Miklos Szeredi | 8632d4d | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 2767 | path->dentry = dentry; |
| 2768 | path->mnt = nd->path.mnt; |
Al Viro | 70806c3 | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2769 | return 1; |
Miklos Szeredi | 8632d4d | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 2770 | |
| 2771 | out_dput: |
| 2772 | dput(dentry); |
Al Viro | 70806c3 | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2773 | return error; |
Miklos Szeredi | 8632d4d | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 2774 | } |
| 2775 | |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 2776 | /* |
Al Viro | fe2d35f | 2011-03-05 22:58:25 -0500 | [diff] [blame] | 2777 | * Handle the last step of open() |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 2778 | */ |
Al Viro | 70806c3 | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2779 | static int do_last(struct nameidata *nd, struct path *path, |
| 2780 | struct file *file, const struct open_flags *op, |
Jeff Layton | 631423f | 2012-10-10 16:43:10 -0400 | [diff] [blame] | 2781 | int *opened, struct filename *name) |
Al Viro | fb1cc55 | 2009-12-24 01:58:28 -0500 | [diff] [blame] | 2782 | { |
Al Viro | a1e2803 | 2009-12-24 02:12:06 -0500 | [diff] [blame] | 2783 | struct dentry *dir = nd->path.dentry; |
Al Viro | 8a14c34 | 2020-02-01 16:26:45 +0000 | [diff] [blame] | 2784 | kuid_t dir_uid = nd->inode->i_uid; |
| 2785 | umode_t dir_mode = nd->inode->i_mode; |
Al Viro | ca344a89 | 2011-03-09 00:36:45 -0500 | [diff] [blame] | 2786 | int open_flag = op->open_flag; |
Miklos Szeredi | d6a4ce3 | 2012-06-05 15:10:30 +0200 | [diff] [blame] | 2787 | bool will_truncate = (open_flag & O_TRUNC) != 0; |
Al Viro | 8c162b9 | 2012-07-31 00:53:35 +0400 | [diff] [blame] | 2788 | bool got_write = false; |
Al Viro | bcda765 | 2011-03-13 16:42:14 -0400 | [diff] [blame] | 2789 | int acc_mode = op->acc_mode; |
Miklos Szeredi | a5f84da | 2012-05-21 17:30:07 +0200 | [diff] [blame] | 2790 | struct inode *inode; |
Miklos Szeredi | d6a4ce3 | 2012-06-05 15:10:30 +0200 | [diff] [blame] | 2791 | bool symlink_ok = false; |
Miklos Szeredi | efbfa63 | 2012-05-21 17:30:19 +0200 | [diff] [blame] | 2792 | struct path save_parent = { .dentry = NULL, .mnt = NULL }; |
| 2793 | bool retried = false; |
Al Viro | 16c2cd7 | 2011-02-22 15:50:10 -0500 | [diff] [blame] | 2794 | int error; |
Jeff Layton | 631423f | 2012-10-10 16:43:10 -0400 | [diff] [blame] | 2795 | const char *pathname = name->name; |
Al Viro | fb1cc55 | 2009-12-24 01:58:28 -0500 | [diff] [blame] | 2796 | |
Al Viro | c3e380b | 2011-02-23 13:39:45 -0500 | [diff] [blame] | 2797 | nd->flags &= ~LOOKUP_PARENT; |
| 2798 | nd->flags |= op->intent; |
| 2799 | |
Al Viro | f910390 | 2013-06-06 09:12:33 -0400 | [diff] [blame] | 2800 | if (nd->last_type != LAST_NORM) { |
Al Viro | fe2d35f | 2011-03-05 22:58:25 -0500 | [diff] [blame] | 2801 | error = handle_dots(nd, nd->last_type); |
| 2802 | if (error) |
Al Viro | 70806c3 | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2803 | return error; |
Miklos Szeredi | 8d49e6e | 2012-06-05 15:10:29 +0200 | [diff] [blame] | 2804 | goto finish_open; |
Al Viro | 1f36f77 | 2009-12-26 10:56:19 -0500 | [diff] [blame] | 2805 | } |
Al Viro | 67ee3ad | 2009-12-26 07:01:01 -0500 | [diff] [blame] | 2806 | |
Al Viro | ca344a89 | 2011-03-09 00:36:45 -0500 | [diff] [blame] | 2807 | if (!(open_flag & O_CREAT)) { |
Al Viro | fe2d35f | 2011-03-05 22:58:25 -0500 | [diff] [blame] | 2808 | if (nd->last.name[nd->last.len]) |
| 2809 | nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; |
Al Viro | bcda765 | 2011-03-13 16:42:14 -0400 | [diff] [blame] | 2810 | if (open_flag & O_PATH && !(nd->flags & LOOKUP_FOLLOW)) |
Miklos Szeredi | d6a4ce3 | 2012-06-05 15:10:30 +0200 | [diff] [blame] | 2811 | symlink_ok = true; |
Al Viro | fe2d35f | 2011-03-05 22:58:25 -0500 | [diff] [blame] | 2812 | /* we _can_ be in RCU mode here */ |
Miklos Szeredi | a5f84da | 2012-05-21 17:30:07 +0200 | [diff] [blame] | 2813 | error = lookup_fast(nd, &nd->last, path, &inode); |
Miklos Szeredi | a2ab364 | 2012-06-05 15:10:14 +0200 | [diff] [blame] | 2814 | if (likely(!error)) |
| 2815 | goto finish_lookup; |
Miklos Szeredi | a5f84da | 2012-05-21 17:30:07 +0200 | [diff] [blame] | 2816 | |
Al Viro | ce57dfc | 2011-03-13 19:58:58 -0400 | [diff] [blame] | 2817 | if (error < 0) |
Al Viro | 70806c3 | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2818 | goto out; |
Miklos Szeredi | 513072f | 2012-06-05 15:10:12 +0200 | [diff] [blame] | 2819 | |
Miklos Szeredi | a2ab364 | 2012-06-05 15:10:14 +0200 | [diff] [blame] | 2820 | BUG_ON(nd->inode != dir->d_inode); |
Miklos Szeredi | 441a458 | 2012-06-05 15:10:13 +0200 | [diff] [blame] | 2821 | } else { |
| 2822 | /* create side of things */ |
| 2823 | /* |
| 2824 | * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED |
| 2825 | * has been cleared when we got to the last component we are |
| 2826 | * about to look up |
| 2827 | */ |
Al Viro | 9f1fafe | 2011-03-25 11:00:12 -0400 | [diff] [blame] | 2828 | error = complete_walk(nd); |
| 2829 | if (error) |
Al Viro | 70806c3 | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2830 | return error; |
Al Viro | fe2d35f | 2011-03-05 22:58:25 -0500 | [diff] [blame] | 2831 | |
Miklos Szeredi | 441a458 | 2012-06-05 15:10:13 +0200 | [diff] [blame] | 2832 | audit_inode(pathname, dir); |
| 2833 | error = -EISDIR; |
| 2834 | /* trailing slashes? */ |
| 2835 | if (nd->last.name[nd->last.len]) |
Al Viro | 70806c3 | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2836 | goto out; |
Al Viro | fe2d35f | 2011-03-05 22:58:25 -0500 | [diff] [blame] | 2837 | } |
| 2838 | |
Miklos Szeredi | efbfa63 | 2012-05-21 17:30:19 +0200 | [diff] [blame] | 2839 | retry_lookup: |
Al Viro | 8c162b9 | 2012-07-31 00:53:35 +0400 | [diff] [blame] | 2840 | if (op->open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) { |
Al Viro | fb1cc55 | 2009-12-24 01:58:28 -0500 | [diff] [blame] | 2841 | error = mnt_want_write(nd->path.mnt); |
Al Viro | 8c162b9 | 2012-07-31 00:53:35 +0400 | [diff] [blame] | 2842 | if (!error) |
| 2843 | got_write = true; |
| 2844 | /* |
| 2845 | * do _not_ fail yet - we might not need that or fail with |
| 2846 | * a different error; let lookup_open() decide; we'll be |
| 2847 | * dropping this one anyway. |
| 2848 | */ |
| 2849 | } |
Al Viro | fb1cc55 | 2009-12-24 01:58:28 -0500 | [diff] [blame] | 2850 | mutex_lock(&dir->d_inode->i_mutex); |
Al Viro | 8c162b9 | 2012-07-31 00:53:35 +0400 | [diff] [blame] | 2851 | error = lookup_open(nd, path, file, op, got_write, opened); |
Miklos Szeredi | 8632d4d | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 2852 | mutex_unlock(&dir->d_inode->i_mutex); |
Al Viro | fb1cc55 | 2009-12-24 01:58:28 -0500 | [diff] [blame] | 2853 | |
Al Viro | 70806c3 | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2854 | if (error <= 0) { |
Al Viro | fb1cc55 | 2009-12-24 01:58:28 -0500 | [diff] [blame] | 2855 | if (error) |
Miklos Szeredi | 48b105f | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2856 | goto out; |
| 2857 | |
Al Viro | 8915ef3 | 2012-06-10 05:01:45 -0400 | [diff] [blame] | 2858 | if ((*opened & FILE_CREATED) || |
Al Viro | 70806c3 | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2859 | !S_ISREG(file->f_path.dentry->d_inode->i_mode)) |
Miklos Szeredi | d6a4ce3 | 2012-06-05 15:10:30 +0200 | [diff] [blame] | 2860 | will_truncate = false; |
Miklos Szeredi | 48b105f | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2861 | |
Al Viro | 70806c3 | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2862 | audit_inode(pathname, file->f_path.dentry); |
Miklos Szeredi | 48b105f | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2863 | goto opened; |
| 2864 | } |
Al Viro | fb1cc55 | 2009-12-24 01:58:28 -0500 | [diff] [blame] | 2865 | |
Al Viro | 8915ef3 | 2012-06-10 05:01:45 -0400 | [diff] [blame] | 2866 | if (*opened & FILE_CREATED) { |
Al Viro | 9b44f1b | 2011-03-09 00:17:27 -0500 | [diff] [blame] | 2867 | /* Don't check for write permission, don't truncate */ |
Al Viro | ca344a89 | 2011-03-09 00:36:45 -0500 | [diff] [blame] | 2868 | open_flag &= ~O_TRUNC; |
Miklos Szeredi | d6a4ce3 | 2012-06-05 15:10:30 +0200 | [diff] [blame] | 2869 | will_truncate = false; |
Al Viro | bcda765 | 2011-03-13 16:42:14 -0400 | [diff] [blame] | 2870 | acc_mode = MAY_OPEN; |
Miklos Szeredi | 8632d4d | 2012-06-05 15:10:15 +0200 | [diff] [blame] | 2871 | path_to_nameidata(path, nd); |
Miklos Szeredi | 8d49e6e | 2012-06-05 15:10:29 +0200 | [diff] [blame] | 2872 | goto finish_open_created; |
Al Viro | fb1cc55 | 2009-12-24 01:58:28 -0500 | [diff] [blame] | 2873 | } |
| 2874 | |
| 2875 | /* |
Jeff Layton | 968c5ab | 2012-07-25 10:19:47 -0400 | [diff] [blame] | 2876 | * create/update audit record if it already exists. |
Al Viro | fb1cc55 | 2009-12-24 01:58:28 -0500 | [diff] [blame] | 2877 | */ |
Jeff Layton | 968c5ab | 2012-07-25 10:19:47 -0400 | [diff] [blame] | 2878 | if (path->dentry->d_inode) |
| 2879 | audit_inode(pathname, path->dentry); |
Al Viro | fb1cc55 | 2009-12-24 01:58:28 -0500 | [diff] [blame] | 2880 | |
Miklos Szeredi | 48b105f | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2881 | /* |
| 2882 | * If atomic_open() acquired write access it is dropped now due to |
| 2883 | * possible mount and symlink following (this might be optimized away if |
| 2884 | * necessary...) |
| 2885 | */ |
Al Viro | 8c162b9 | 2012-07-31 00:53:35 +0400 | [diff] [blame] | 2886 | if (got_write) { |
Miklos Szeredi | 48b105f | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2887 | mnt_drop_write(nd->path.mnt); |
Al Viro | 8c162b9 | 2012-07-31 00:53:35 +0400 | [diff] [blame] | 2888 | got_write = false; |
Miklos Szeredi | 48b105f | 2012-06-05 15:10:17 +0200 | [diff] [blame] | 2889 | } |
Al Viro | fb1cc55 | 2009-12-24 01:58:28 -0500 | [diff] [blame] | 2890 | |
| 2891 | error = -EEXIST; |
Al Viro | 5b2d9ff | 2012-07-30 11:50:30 +0400 | [diff] [blame] | 2892 | if ((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT)) |
Al Viro | fb1cc55 | 2009-12-24 01:58:28 -0500 | [diff] [blame] | 2893 | goto exit_dput; |
| 2894 | |
David Howells | 9875cf8 | 2011-01-14 18:45:21 +0000 | [diff] [blame] | 2895 | error = follow_managed(path, nd->flags); |
| 2896 | if (error < 0) |
| 2897 | goto exit_dput; |
Al Viro | fb1cc55 | 2009-12-24 01:58:28 -0500 | [diff] [blame] | 2898 | |
Al Viro | a3fbbde | 2011-11-07 21:21:26 +0000 | [diff] [blame] | 2899 | if (error) |
| 2900 | nd->flags |= LOOKUP_JUMPED; |
| 2901 | |
Miklos Szeredi | 5c7f451 | 2012-05-21 17:30:08 +0200 | [diff] [blame] | 2902 | BUG_ON(nd->flags & LOOKUP_RCU); |
| 2903 | inode = path->dentry->d_inode; |
Miklos Szeredi | 99ec980 | 2012-05-21 17:30:14 +0200 | [diff] [blame] | 2904 | finish_lookup: |
| 2905 | /* we _can_ be in RCU mode here */ |
Al Viro | fb1cc55 | 2009-12-24 01:58:28 -0500 | [diff] [blame] | 2906 | error = -ENOENT; |
Miklos Szeredi | 1678506 | 2012-05-21 17:30:10 +0200 | [diff] [blame] | 2907 | if (!inode) { |
| 2908 | path_to_nameidata(path, nd); |
Al Viro | 70806c3 | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2909 | goto out; |
Miklos Szeredi | 1678506 | 2012-05-21 17:30:10 +0200 | [diff] [blame] | 2910 | } |
Al Viro | 9e67f36 | 2009-12-26 07:04:50 -0500 | [diff] [blame] | 2911 | |
Miklos Szeredi | 458abf2 | 2012-05-21 17:30:09 +0200 | [diff] [blame] | 2912 | if (should_follow_link(inode, !symlink_ok)) { |
| 2913 | if (nd->flags & LOOKUP_RCU) { |
| 2914 | if (unlikely(unlazy_walk(nd, path->dentry))) { |
| 2915 | error = -ECHILD; |
Al Viro | 70806c3 | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2916 | goto out; |
Miklos Szeredi | 458abf2 | 2012-05-21 17:30:09 +0200 | [diff] [blame] | 2917 | } |
| 2918 | } |
| 2919 | BUG_ON(inode != path->dentry->d_inode); |
Al Viro | 70806c3 | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2920 | return 1; |
Miklos Szeredi | 458abf2 | 2012-05-21 17:30:09 +0200 | [diff] [blame] | 2921 | } |
Al Viro | fb1cc55 | 2009-12-24 01:58:28 -0500 | [diff] [blame] | 2922 | |
Miklos Szeredi | efbfa63 | 2012-05-21 17:30:19 +0200 | [diff] [blame] | 2923 | if ((nd->flags & LOOKUP_RCU) || nd->path.mnt != path->mnt) { |
| 2924 | path_to_nameidata(path, nd); |
| 2925 | } else { |
| 2926 | save_parent.dentry = nd->path.dentry; |
| 2927 | save_parent.mnt = mntget(path->mnt); |
| 2928 | nd->path.dentry = path->dentry; |
| 2929 | |
| 2930 | } |
Miklos Szeredi | 5c7f451 | 2012-05-21 17:30:08 +0200 | [diff] [blame] | 2931 | nd->inode = inode; |
Al Viro | a3fbbde | 2011-11-07 21:21:26 +0000 | [diff] [blame] | 2932 | /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */ |
Al Viro | f910390 | 2013-06-06 09:12:33 -0400 | [diff] [blame] | 2933 | finish_open: |
Al Viro | a3fbbde | 2011-11-07 21:21:26 +0000 | [diff] [blame] | 2934 | error = complete_walk(nd); |
Miklos Szeredi | efbfa63 | 2012-05-21 17:30:19 +0200 | [diff] [blame] | 2935 | if (error) { |
| 2936 | path_put(&save_parent); |
Al Viro | 70806c3 | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2937 | return error; |
Miklos Szeredi | efbfa63 | 2012-05-21 17:30:19 +0200 | [diff] [blame] | 2938 | } |
Al Viro | f910390 | 2013-06-06 09:12:33 -0400 | [diff] [blame] | 2939 | audit_inode(pathname, nd->path.dentry); |
Salvatore Mesoraca | 063d55a | 2018-08-23 17:00:35 -0700 | [diff] [blame] | 2940 | if (open_flag & O_CREAT) { |
| 2941 | error = -EISDIR; |
| 2942 | if (S_ISDIR(nd->inode->i_mode)) |
| 2943 | goto out; |
Al Viro | a243411 | 2020-01-26 09:29:34 -0500 | [diff] [blame] | 2944 | error = may_create_in_sticky(dir_mode, dir_uid, |
Salvatore Mesoraca | 063d55a | 2018-08-23 17:00:35 -0700 | [diff] [blame] | 2945 | d_backing_inode(nd->path.dentry)); |
| 2946 | if (unlikely(error)) |
| 2947 | goto out; |
| 2948 | } |
Miklos Szeredi | aba9dce | 2012-05-21 17:30:11 +0200 | [diff] [blame] | 2949 | error = -ENOTDIR; |
Al Viro | a1e1263 | 2013-06-06 19:33:47 -0400 | [diff] [blame] | 2950 | if ((nd->flags & LOOKUP_DIRECTORY) && !can_lookup(nd->inode)) |
Al Viro | 70806c3 | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2951 | goto out; |
Al Viro | 6c0d46c | 2011-03-09 00:59:59 -0500 | [diff] [blame] | 2952 | if (!S_ISREG(nd->inode->i_mode)) |
Miklos Szeredi | d6a4ce3 | 2012-06-05 15:10:30 +0200 | [diff] [blame] | 2953 | will_truncate = false; |
Al Viro | 6c0d46c | 2011-03-09 00:59:59 -0500 | [diff] [blame] | 2954 | |
Al Viro | 0f9d1a1 | 2011-03-09 00:13:14 -0500 | [diff] [blame] | 2955 | if (will_truncate) { |
| 2956 | error = mnt_want_write(nd->path.mnt); |
| 2957 | if (error) |
Al Viro | 70806c3 | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2958 | goto out; |
Al Viro | 8c162b9 | 2012-07-31 00:53:35 +0400 | [diff] [blame] | 2959 | got_write = true; |
Al Viro | 0f9d1a1 | 2011-03-09 00:13:14 -0500 | [diff] [blame] | 2960 | } |
Miklos Szeredi | 8d49e6e | 2012-06-05 15:10:29 +0200 | [diff] [blame] | 2961 | finish_open_created: |
Al Viro | bcda765 | 2011-03-13 16:42:14 -0400 | [diff] [blame] | 2962 | error = may_open(&nd->path, acc_mode, open_flag); |
Al Viro | ca344a89 | 2011-03-09 00:36:45 -0500 | [diff] [blame] | 2963 | if (error) |
Al Viro | 70806c3 | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2964 | goto out; |
Al Viro | b2ebdc5 | 2012-06-22 12:40:19 +0400 | [diff] [blame] | 2965 | file->f_path.mnt = nd->path.mnt; |
| 2966 | error = finish_open(file, nd->path.dentry, NULL, opened); |
| 2967 | if (error) { |
Al Viro | b2ebdc5 | 2012-06-22 12:40:19 +0400 | [diff] [blame] | 2968 | if (error == -EOPENSTALE) |
Miklos Szeredi | 36c2752 | 2012-06-05 15:10:31 +0200 | [diff] [blame] | 2969 | goto stale_open; |
Miklos Szeredi | 2f80ad3 | 2012-06-05 15:10:27 +0200 | [diff] [blame] | 2970 | goto out; |
Al Viro | 0f9d1a1 | 2011-03-09 00:13:14 -0500 | [diff] [blame] | 2971 | } |
Miklos Szeredi | 2de99c0 | 2012-06-05 15:10:32 +0200 | [diff] [blame] | 2972 | opened: |
Al Viro | 70806c3 | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2973 | error = open_check_o_direct(file); |
Miklos Szeredi | 2f80ad3 | 2012-06-05 15:10:27 +0200 | [diff] [blame] | 2974 | if (error) |
| 2975 | goto exit_fput; |
Al Viro | 70806c3 | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2976 | error = ima_file_check(file, op->acc_mode); |
Miklos Szeredi | 4233c21 | 2012-06-05 15:10:28 +0200 | [diff] [blame] | 2977 | if (error) |
| 2978 | goto exit_fput; |
| 2979 | |
| 2980 | if (will_truncate) { |
Al Viro | 70806c3 | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2981 | error = handle_truncate(file); |
Miklos Szeredi | 4233c21 | 2012-06-05 15:10:28 +0200 | [diff] [blame] | 2982 | if (error) |
| 2983 | goto exit_fput; |
Al Viro | 0f9d1a1 | 2011-03-09 00:13:14 -0500 | [diff] [blame] | 2984 | } |
Al Viro | ca344a89 | 2011-03-09 00:36:45 -0500 | [diff] [blame] | 2985 | out: |
Al Viro | 8c162b9 | 2012-07-31 00:53:35 +0400 | [diff] [blame] | 2986 | if (got_write) |
Al Viro | 0f9d1a1 | 2011-03-09 00:13:14 -0500 | [diff] [blame] | 2987 | mnt_drop_write(nd->path.mnt); |
Miklos Szeredi | efbfa63 | 2012-05-21 17:30:19 +0200 | [diff] [blame] | 2988 | path_put(&save_parent); |
Miklos Szeredi | 2aabf6d | 2012-05-21 17:30:06 +0200 | [diff] [blame] | 2989 | terminate_walk(nd); |
Al Viro | 70806c3 | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2990 | return error; |
Al Viro | fb1cc55 | 2009-12-24 01:58:28 -0500 | [diff] [blame] | 2991 | |
Al Viro | fb1cc55 | 2009-12-24 01:58:28 -0500 | [diff] [blame] | 2992 | exit_dput: |
| 2993 | path_put_conditional(path, nd); |
Al Viro | ca344a89 | 2011-03-09 00:36:45 -0500 | [diff] [blame] | 2994 | goto out; |
Miklos Szeredi | 2f80ad3 | 2012-06-05 15:10:27 +0200 | [diff] [blame] | 2995 | exit_fput: |
Al Viro | 70806c3 | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 2996 | fput(file); |
| 2997 | goto out; |
Miklos Szeredi | 2f80ad3 | 2012-06-05 15:10:27 +0200 | [diff] [blame] | 2998 | |
Miklos Szeredi | 36c2752 | 2012-06-05 15:10:31 +0200 | [diff] [blame] | 2999 | stale_open: |
| 3000 | /* If no saved parent or already retried then can't retry */ |
| 3001 | if (!save_parent.dentry || retried) |
| 3002 | goto out; |
| 3003 | |
| 3004 | BUG_ON(save_parent.dentry != dir); |
| 3005 | path_put(&nd->path); |
| 3006 | nd->path = save_parent; |
| 3007 | nd->inode = dir->d_inode; |
| 3008 | save_parent.mnt = NULL; |
| 3009 | save_parent.dentry = NULL; |
Al Viro | 8c162b9 | 2012-07-31 00:53:35 +0400 | [diff] [blame] | 3010 | if (got_write) { |
Miklos Szeredi | 36c2752 | 2012-06-05 15:10:31 +0200 | [diff] [blame] | 3011 | mnt_drop_write(nd->path.mnt); |
Al Viro | 8c162b9 | 2012-07-31 00:53:35 +0400 | [diff] [blame] | 3012 | got_write = false; |
Miklos Szeredi | 36c2752 | 2012-06-05 15:10:31 +0200 | [diff] [blame] | 3013 | } |
| 3014 | retried = true; |
| 3015 | goto retry_lookup; |
Al Viro | fb1cc55 | 2009-12-24 01:58:28 -0500 | [diff] [blame] | 3016 | } |
| 3017 | |
Al Viro | b0d5430 | 2013-06-07 01:20:27 -0400 | [diff] [blame] | 3018 | static int do_tmpfile(int dfd, struct filename *pathname, |
| 3019 | struct nameidata *nd, int flags, |
| 3020 | const struct open_flags *op, |
| 3021 | struct file *file, int *opened) |
| 3022 | { |
| 3023 | static const struct qstr name = QSTR_INIT("/", 1); |
| 3024 | struct dentry *dentry, *child; |
| 3025 | struct inode *dir; |
| 3026 | int error = path_lookupat(dfd, pathname->name, |
| 3027 | flags | LOOKUP_DIRECTORY, nd); |
| 3028 | if (unlikely(error)) |
| 3029 | return error; |
| 3030 | error = mnt_want_write(nd->path.mnt); |
| 3031 | if (unlikely(error)) |
| 3032 | goto out; |
| 3033 | /* we want directory to be writable */ |
| 3034 | error = inode_permission(nd->inode, MAY_WRITE | MAY_EXEC); |
| 3035 | if (error) |
| 3036 | goto out2; |
| 3037 | dentry = nd->path.dentry; |
| 3038 | dir = dentry->d_inode; |
| 3039 | if (!dir->i_op->tmpfile) { |
| 3040 | error = -EOPNOTSUPP; |
| 3041 | goto out2; |
| 3042 | } |
| 3043 | child = d_alloc(dentry, &name); |
| 3044 | if (unlikely(!child)) { |
| 3045 | error = -ENOMEM; |
| 3046 | goto out2; |
| 3047 | } |
| 3048 | nd->flags &= ~LOOKUP_DIRECTORY; |
| 3049 | nd->flags |= op->intent; |
| 3050 | dput(nd->path.dentry); |
| 3051 | nd->path.dentry = child; |
| 3052 | error = dir->i_op->tmpfile(dir, nd->path.dentry, op->mode); |
| 3053 | if (error) |
| 3054 | goto out2; |
| 3055 | audit_inode(pathname->name, nd->path.dentry); |
Eric Rannaud | 78cb8e0 | 2014-10-30 01:51:01 -0700 | [diff] [blame] | 3056 | /* Don't check for other permissions, the inode was just created */ |
| 3057 | error = may_open(&nd->path, MAY_OPEN, op->open_flag); |
Al Viro | b0d5430 | 2013-06-07 01:20:27 -0400 | [diff] [blame] | 3058 | if (error) |
| 3059 | goto out2; |
| 3060 | file->f_path.mnt = nd->path.mnt; |
| 3061 | error = finish_open(file, nd->path.dentry, NULL, opened); |
| 3062 | if (error) |
| 3063 | goto out2; |
| 3064 | error = open_check_o_direct(file); |
Al Viro | f6f3426 | 2013-06-11 08:34:36 +0400 | [diff] [blame] | 3065 | if (error) { |
Al Viro | b0d5430 | 2013-06-07 01:20:27 -0400 | [diff] [blame] | 3066 | fput(file); |
Al Viro | f6f3426 | 2013-06-11 08:34:36 +0400 | [diff] [blame] | 3067 | } else if (!(op->open_flag & O_EXCL)) { |
| 3068 | struct inode *inode = file->f_path.dentry->d_inode; |
| 3069 | spin_lock(&inode->i_lock); |
| 3070 | inode->i_state |= I_LINKABLE; |
| 3071 | spin_unlock(&inode->i_lock); |
| 3072 | } |
Al Viro | b0d5430 | 2013-06-07 01:20:27 -0400 | [diff] [blame] | 3073 | out2: |
| 3074 | mnt_drop_write(nd->path.mnt); |
| 3075 | out: |
| 3076 | path_put(&nd->path); |
| 3077 | return error; |
| 3078 | } |
| 3079 | |
Jeff Layton | 631423f | 2012-10-10 16:43:10 -0400 | [diff] [blame] | 3080 | static struct file *path_openat(int dfd, struct filename *pathname, |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 3081 | struct nameidata *nd, const struct open_flags *op, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3082 | { |
Al Viro | fe2d35f | 2011-03-05 22:58:25 -0500 | [diff] [blame] | 3083 | struct file *base = NULL; |
Al Viro | b2ebdc5 | 2012-06-22 12:40:19 +0400 | [diff] [blame] | 3084 | struct file *file; |
Al Viro | 9850c05 | 2010-01-13 15:01:15 -0500 | [diff] [blame] | 3085 | struct path path; |
Al Viro | 8915ef3 | 2012-06-10 05:01:45 -0400 | [diff] [blame] | 3086 | int opened = 0; |
Al Viro | 13aab42 | 2011-02-23 17:54:08 -0500 | [diff] [blame] | 3087 | int error; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 3088 | |
Al Viro | b2ebdc5 | 2012-06-22 12:40:19 +0400 | [diff] [blame] | 3089 | file = get_empty_filp(); |
| 3090 | if (!file) |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 3091 | return ERR_PTR(-ENFILE); |
| 3092 | |
Al Viro | b2ebdc5 | 2012-06-22 12:40:19 +0400 | [diff] [blame] | 3093 | file->f_flags = op->open_flag; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 3094 | |
Al Viro | d50bcfe | 2013-07-13 13:26:37 +0400 | [diff] [blame] | 3095 | if (unlikely(file->f_flags & __O_TMPFILE)) { |
Al Viro | b0d5430 | 2013-06-07 01:20:27 -0400 | [diff] [blame] | 3096 | error = do_tmpfile(dfd, pathname, nd, flags, op, file, &opened); |
Al Viro | 8b41137 | 2015-05-08 22:53:15 -0400 | [diff] [blame] | 3097 | goto out2; |
Al Viro | b0d5430 | 2013-06-07 01:20:27 -0400 | [diff] [blame] | 3098 | } |
| 3099 | |
Jeff Layton | 631423f | 2012-10-10 16:43:10 -0400 | [diff] [blame] | 3100 | error = path_init(dfd, pathname->name, flags | LOOKUP_PARENT, nd, &base); |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 3101 | if (unlikely(error)) |
Al Viro | 70806c3 | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 3102 | goto out; |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 3103 | |
Al Viro | fe2d35f | 2011-03-05 22:58:25 -0500 | [diff] [blame] | 3104 | current->total_link_count = 0; |
Jeff Layton | 631423f | 2012-10-10 16:43:10 -0400 | [diff] [blame] | 3105 | error = link_path_walk(pathname->name, nd); |
Nick Piggin | 31e6b01 | 2011-01-07 17:49:52 +1100 | [diff] [blame] | 3106 | if (unlikely(error)) |
Al Viro | 70806c3 | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 3107 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3108 | |
Al Viro | 70806c3 | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 3109 | error = do_last(nd, &path, file, op, &opened, pathname); |
| 3110 | while (unlikely(error > 0)) { /* trailing symlink */ |
Nick Piggin | 7b9337a | 2011-01-14 08:42:43 +0000 | [diff] [blame] | 3111 | struct path link = path; |
Al Viro | def4af3 | 2009-12-26 08:37:05 -0500 | [diff] [blame] | 3112 | void *cookie; |
Al Viro | 574197e | 2011-03-14 22:20:34 -0400 | [diff] [blame] | 3113 | if (!(nd->flags & LOOKUP_FOLLOW)) { |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 3114 | path_put_conditional(&path, nd); |
| 3115 | path_put(&nd->path); |
Al Viro | 70806c3 | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 3116 | error = -ELOOP; |
Al Viro | 40b3913 | 2011-03-09 16:22:18 -0500 | [diff] [blame] | 3117 | break; |
| 3118 | } |
Kees Cook | 5092e9c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 3119 | error = may_follow_link(&link, nd); |
| 3120 | if (unlikely(error)) |
| 3121 | break; |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 3122 | nd->flags |= LOOKUP_PARENT; |
| 3123 | nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL); |
Al Viro | 574197e | 2011-03-14 22:20:34 -0400 | [diff] [blame] | 3124 | error = follow_link(&link, nd, &cookie); |
Al Viro | c3e380b | 2011-02-23 13:39:45 -0500 | [diff] [blame] | 3125 | if (unlikely(error)) |
Al Viro | 70806c3 | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 3126 | break; |
| 3127 | error = do_last(nd, &path, file, op, &opened, pathname); |
Al Viro | 574197e | 2011-03-14 22:20:34 -0400 | [diff] [blame] | 3128 | put_link(nd, &link, cookie); |
Al Viro | 806b681 | 2009-12-26 07:16:40 -0500 | [diff] [blame] | 3129 | } |
Al Viro | 10fa8e6 | 2009-12-26 07:09:49 -0500 | [diff] [blame] | 3130 | out: |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 3131 | if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) |
| 3132 | path_put(&nd->root); |
Al Viro | fe2d35f | 2011-03-05 22:58:25 -0500 | [diff] [blame] | 3133 | if (base) |
| 3134 | fput(base); |
Al Viro | 8b41137 | 2015-05-08 22:53:15 -0400 | [diff] [blame] | 3135 | out2: |
Al Viro | 70806c3 | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 3136 | if (!(opened & FILE_OPENED)) { |
| 3137 | BUG_ON(!error); |
Al Viro | b2ebdc5 | 2012-06-22 12:40:19 +0400 | [diff] [blame] | 3138 | put_filp(file); |
Miklos Szeredi | 2f80ad3 | 2012-06-05 15:10:27 +0200 | [diff] [blame] | 3139 | } |
Al Viro | 70806c3 | 2012-06-22 12:41:10 +0400 | [diff] [blame] | 3140 | if (unlikely(error)) { |
| 3141 | if (error == -EOPENSTALE) { |
| 3142 | if (flags & LOOKUP_RCU) |
| 3143 | error = -ECHILD; |
| 3144 | else |
| 3145 | error = -ESTALE; |
| 3146 | } |
| 3147 | file = ERR_PTR(error); |
| 3148 | } |
| 3149 | return file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3150 | } |
| 3151 | |
Jeff Layton | 631423f | 2012-10-10 16:43:10 -0400 | [diff] [blame] | 3152 | struct file *do_filp_open(int dfd, struct filename *pathname, |
Al Viro | 13aab42 | 2011-02-23 17:54:08 -0500 | [diff] [blame] | 3153 | const struct open_flags *op, int flags) |
| 3154 | { |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 3155 | struct nameidata nd; |
Al Viro | 13aab42 | 2011-02-23 17:54:08 -0500 | [diff] [blame] | 3156 | struct file *filp; |
| 3157 | |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 3158 | filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_RCU); |
Al Viro | 13aab42 | 2011-02-23 17:54:08 -0500 | [diff] [blame] | 3159 | if (unlikely(filp == ERR_PTR(-ECHILD))) |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 3160 | filp = path_openat(dfd, pathname, &nd, op, flags); |
Al Viro | 13aab42 | 2011-02-23 17:54:08 -0500 | [diff] [blame] | 3161 | if (unlikely(filp == ERR_PTR(-ESTALE))) |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 3162 | filp = path_openat(dfd, pathname, &nd, op, flags | LOOKUP_REVAL); |
Al Viro | 13aab42 | 2011-02-23 17:54:08 -0500 | [diff] [blame] | 3163 | return filp; |
| 3164 | } |
| 3165 | |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 3166 | struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt, |
| 3167 | const char *name, const struct open_flags *op, int flags) |
| 3168 | { |
| 3169 | struct nameidata nd; |
| 3170 | struct file *file; |
Jeff Layton | 631423f | 2012-10-10 16:43:10 -0400 | [diff] [blame] | 3171 | struct filename filename = { .name = name }; |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 3172 | |
| 3173 | nd.root.mnt = mnt; |
| 3174 | nd.root.dentry = dentry; |
| 3175 | |
| 3176 | flags |= LOOKUP_ROOT; |
| 3177 | |
Al Viro | bcda765 | 2011-03-13 16:42:14 -0400 | [diff] [blame] | 3178 | if (dentry->d_inode->i_op->follow_link && op->intent & LOOKUP_OPEN) |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 3179 | return ERR_PTR(-ELOOP); |
| 3180 | |
Jeff Layton | 631423f | 2012-10-10 16:43:10 -0400 | [diff] [blame] | 3181 | file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_RCU); |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 3182 | if (unlikely(file == ERR_PTR(-ECHILD))) |
Jeff Layton | 631423f | 2012-10-10 16:43:10 -0400 | [diff] [blame] | 3183 | file = path_openat(-1, &filename, &nd, op, flags); |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 3184 | if (unlikely(file == ERR_PTR(-ESTALE))) |
Jeff Layton | 631423f | 2012-10-10 16:43:10 -0400 | [diff] [blame] | 3185 | file = path_openat(-1, &filename, &nd, op, flags | LOOKUP_REVAL); |
Al Viro | 73d049a | 2011-03-11 12:08:24 -0500 | [diff] [blame] | 3186 | return file; |
| 3187 | } |
| 3188 | |
Al Viro | ed75e95 | 2011-06-27 16:53:43 -0400 | [diff] [blame] | 3189 | struct dentry *kern_path_create(int dfd, const char *pathname, struct path *path, int is_dir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3190 | { |
Christoph Hellwig | c663e5d | 2005-06-23 00:09:49 -0700 | [diff] [blame] | 3191 | struct dentry *dentry = ERR_PTR(-EEXIST); |
Al Viro | ed75e95 | 2011-06-27 16:53:43 -0400 | [diff] [blame] | 3192 | struct nameidata nd; |
Jan Kara | 7d14039 | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3193 | int err2; |
Al Viro | ed75e95 | 2011-06-27 16:53:43 -0400 | [diff] [blame] | 3194 | int error = do_path_lookup(dfd, pathname, LOOKUP_PARENT, &nd); |
| 3195 | if (error) |
| 3196 | return ERR_PTR(error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3197 | |
Christoph Hellwig | c663e5d | 2005-06-23 00:09:49 -0700 | [diff] [blame] | 3198 | /* |
| 3199 | * Yucky last component or no last component at all? |
| 3200 | * (foo/., foo/.., /////) |
| 3201 | */ |
Al Viro | ed75e95 | 2011-06-27 16:53:43 -0400 | [diff] [blame] | 3202 | if (nd.last_type != LAST_NORM) |
| 3203 | goto out; |
| 3204 | nd.flags &= ~LOOKUP_PARENT; |
| 3205 | nd.flags |= LOOKUP_CREATE | LOOKUP_EXCL; |
Christoph Hellwig | c663e5d | 2005-06-23 00:09:49 -0700 | [diff] [blame] | 3206 | |
Jan Kara | 7d14039 | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3207 | /* don't fail immediately if it's r/o, at least try to report other errors */ |
| 3208 | err2 = mnt_want_write(nd.path.mnt); |
Christoph Hellwig | c663e5d | 2005-06-23 00:09:49 -0700 | [diff] [blame] | 3209 | /* |
| 3210 | * Do the final lookup. |
| 3211 | */ |
Al Viro | ed75e95 | 2011-06-27 16:53:43 -0400 | [diff] [blame] | 3212 | mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); |
| 3213 | dentry = lookup_hash(&nd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3214 | if (IS_ERR(dentry)) |
Al Viro | eb8b9ae | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3215 | goto unlock; |
Christoph Hellwig | c663e5d | 2005-06-23 00:09:49 -0700 | [diff] [blame] | 3216 | |
Al Viro | eb8b9ae | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3217 | error = -EEXIST; |
Al Viro | e9baf6e | 2008-05-15 04:49:12 -0400 | [diff] [blame] | 3218 | if (dentry->d_inode) |
Al Viro | eb8b9ae | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3219 | goto fail; |
Christoph Hellwig | c663e5d | 2005-06-23 00:09:49 -0700 | [diff] [blame] | 3220 | /* |
| 3221 | * Special case - lookup gave negative, but... we had foo/bar/ |
| 3222 | * From the vfs_mknod() POV we just have a negative dentry - |
| 3223 | * all is fine. Let's be bastards - you had / on the end, you've |
| 3224 | * been asking for (non-existent) directory. -ENOENT for you. |
| 3225 | */ |
Al Viro | ed75e95 | 2011-06-27 16:53:43 -0400 | [diff] [blame] | 3226 | if (unlikely(!is_dir && nd.last.name[nd.last.len])) { |
Al Viro | eb8b9ae | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3227 | error = -ENOENT; |
Al Viro | ed75e95 | 2011-06-27 16:53:43 -0400 | [diff] [blame] | 3228 | goto fail; |
Al Viro | e9baf6e | 2008-05-15 04:49:12 -0400 | [diff] [blame] | 3229 | } |
Jan Kara | 7d14039 | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3230 | if (unlikely(err2)) { |
| 3231 | error = err2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3232 | goto fail; |
| 3233 | } |
Al Viro | ed75e95 | 2011-06-27 16:53:43 -0400 | [diff] [blame] | 3234 | *path = nd.path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3235 | return dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3236 | fail: |
Al Viro | eb8b9ae | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3237 | dput(dentry); |
| 3238 | dentry = ERR_PTR(error); |
| 3239 | unlock: |
Al Viro | ed75e95 | 2011-06-27 16:53:43 -0400 | [diff] [blame] | 3240 | mutex_unlock(&nd.path.dentry->d_inode->i_mutex); |
Jan Kara | 7d14039 | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3241 | if (!err2) |
| 3242 | mnt_drop_write(nd.path.mnt); |
Al Viro | ed75e95 | 2011-06-27 16:53:43 -0400 | [diff] [blame] | 3243 | out: |
| 3244 | path_put(&nd.path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3245 | return dentry; |
| 3246 | } |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3247 | EXPORT_SYMBOL(kern_path_create); |
| 3248 | |
Al Viro | 58cc338 | 2012-07-20 01:15:31 +0400 | [diff] [blame] | 3249 | void done_path_create(struct path *path, struct dentry *dentry) |
| 3250 | { |
| 3251 | dput(dentry); |
| 3252 | mutex_unlock(&path->dentry->d_inode->i_mutex); |
Al Viro | eb8b9ae | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3253 | mnt_drop_write(path->mnt); |
Al Viro | 58cc338 | 2012-07-20 01:15:31 +0400 | [diff] [blame] | 3254 | path_put(path); |
| 3255 | } |
| 3256 | EXPORT_SYMBOL(done_path_create); |
| 3257 | |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3258 | struct dentry *user_path_create(int dfd, const char __user *pathname, struct path *path, int is_dir) |
| 3259 | { |
Jeff Layton | 9cee5ca | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 3260 | struct filename *tmp = getname(pathname); |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3261 | struct dentry *res; |
| 3262 | if (IS_ERR(tmp)) |
| 3263 | return ERR_CAST(tmp); |
Jeff Layton | 9cee5ca | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 3264 | res = kern_path_create(dfd, tmp->name, path, is_dir); |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3265 | putname(tmp); |
| 3266 | return res; |
| 3267 | } |
| 3268 | EXPORT_SYMBOL(user_path_create); |
| 3269 | |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 3270 | int vfs_mknod2(struct vfsmount *mnt, struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3271 | { |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 3272 | int error = may_create(mnt, dir, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3273 | |
| 3274 | if (error) |
| 3275 | return error; |
| 3276 | |
Eric W. Biederman | b95e212 | 2011-11-13 12:16:43 -0800 | [diff] [blame] | 3277 | if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3278 | return -EPERM; |
| 3279 | |
Al Viro | acfa438 | 2008-12-04 10:06:33 -0500 | [diff] [blame] | 3280 | if (!dir->i_op->mknod) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3281 | return -EPERM; |
| 3282 | |
Serge E. Hallyn | 08ce5f1 | 2008-04-29 01:00:10 -0700 | [diff] [blame] | 3283 | error = devcgroup_inode_mknod(mode, dev); |
| 3284 | if (error) |
| 3285 | return error; |
| 3286 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3287 | error = security_inode_mknod(dir, dentry, mode, dev); |
| 3288 | if (error) |
| 3289 | return error; |
| 3290 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3291 | error = dir->i_op->mknod(dir, dentry, mode, dev); |
Amir Samuelov | 6a22e46 | 2014-05-26 11:44:06 +0300 | [diff] [blame] | 3292 | if (error) |
| 3293 | return error; |
| 3294 | |
| 3295 | error = security_inode_post_create(dir, dentry, mode); |
| 3296 | if (error) |
| 3297 | return error; |
| 3298 | |
Stephen Smalley | a74574a | 2005-09-09 13:01:44 -0700 | [diff] [blame] | 3299 | if (!error) |
Amy Griffis | f38aa94 | 2005-11-03 15:57:06 +0000 | [diff] [blame] | 3300 | fsnotify_create(dir, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3301 | return error; |
| 3302 | } |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 3303 | EXPORT_SYMBOL(vfs_mknod2); |
| 3304 | |
| 3305 | int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev) |
| 3306 | { |
| 3307 | return vfs_mknod2(NULL, dir, dentry, mode, dev); |
| 3308 | } |
| 3309 | EXPORT_SYMBOL(vfs_mknod); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3310 | |
Al Viro | f69aac0 | 2011-07-26 04:31:40 -0400 | [diff] [blame] | 3311 | static int may_mknod(umode_t mode) |
Dave Hansen | 463c319 | 2008-02-15 14:37:57 -0800 | [diff] [blame] | 3312 | { |
| 3313 | switch (mode & S_IFMT) { |
| 3314 | case S_IFREG: |
| 3315 | case S_IFCHR: |
| 3316 | case S_IFBLK: |
| 3317 | case S_IFIFO: |
| 3318 | case S_IFSOCK: |
| 3319 | case 0: /* zero mode translates to S_IFREG */ |
| 3320 | return 0; |
| 3321 | case S_IFDIR: |
| 3322 | return -EPERM; |
| 3323 | default: |
| 3324 | return -EINVAL; |
| 3325 | } |
| 3326 | } |
| 3327 | |
Al Viro | 8208a22 | 2011-07-25 17:32:17 -0400 | [diff] [blame] | 3328 | SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode, |
Heiko Carstens | 2e4d092 | 2009-01-14 14:14:31 +0100 | [diff] [blame] | 3329 | unsigned, dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3330 | { |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 3331 | struct dentry *dentry; |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3332 | struct path path; |
| 3333 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3334 | |
Al Viro | 1db2b39 | 2012-07-20 01:17:26 +0400 | [diff] [blame] | 3335 | error = may_mknod(mode); |
| 3336 | if (error) |
| 3337 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3338 | |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3339 | dentry = user_path_create(dfd, filename, &path, 0); |
| 3340 | if (IS_ERR(dentry)) |
| 3341 | return PTR_ERR(dentry); |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 3342 | |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3343 | if (!IS_POSIXACL(path.dentry->d_inode)) |
Al Viro | ce3b0f8 | 2009-03-29 19:08:22 -0400 | [diff] [blame] | 3344 | mode &= ~current_umask(); |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3345 | error = security_path_mknod(&path, dentry, mode, dev); |
Kentaro Takeda | be6d3e5 | 2008-12-17 13:24:15 +0900 | [diff] [blame] | 3346 | if (error) |
Al Viro | eb8b9ae | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3347 | goto out; |
Dave Hansen | 463c319 | 2008-02-15 14:37:57 -0800 | [diff] [blame] | 3348 | switch (mode & S_IFMT) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3349 | case 0: case S_IFREG: |
Al Viro | 8da0eaf | 2012-06-10 18:09:36 -0400 | [diff] [blame] | 3350 | error = vfs_create2(path.mnt, path.dentry->d_inode,dentry,mode,true); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3351 | break; |
| 3352 | case S_IFCHR: case S_IFBLK: |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 3353 | error = vfs_mknod2(path.mnt, path.dentry->d_inode,dentry,mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3354 | new_decode_dev(dev)); |
| 3355 | break; |
| 3356 | case S_IFIFO: case S_IFSOCK: |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3357 | error = vfs_mknod(path.dentry->d_inode,dentry,mode,0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3358 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3359 | } |
Al Viro | eb8b9ae | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3360 | out: |
Al Viro | 58cc338 | 2012-07-20 01:15:31 +0400 | [diff] [blame] | 3361 | done_path_create(&path, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3362 | |
| 3363 | return error; |
| 3364 | } |
| 3365 | |
Al Viro | 8208a22 | 2011-07-25 17:32:17 -0400 | [diff] [blame] | 3366 | SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 3367 | { |
| 3368 | return sys_mknodat(AT_FDCWD, filename, mode, dev); |
| 3369 | } |
| 3370 | |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 3371 | int vfs_mkdir2(struct vfsmount *mnt, struct inode *dir, struct dentry *dentry, umode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3372 | { |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 3373 | int error = may_create(mnt, dir, dentry); |
Al Viro | 8de5277 | 2012-02-06 12:45:27 -0500 | [diff] [blame] | 3374 | unsigned max_links = dir->i_sb->s_max_links; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3375 | |
| 3376 | if (error) |
| 3377 | return error; |
| 3378 | |
Al Viro | acfa438 | 2008-12-04 10:06:33 -0500 | [diff] [blame] | 3379 | if (!dir->i_op->mkdir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3380 | return -EPERM; |
| 3381 | |
| 3382 | mode &= (S_IRWXUGO|S_ISVTX); |
| 3383 | error = security_inode_mkdir(dir, dentry, mode); |
| 3384 | if (error) |
| 3385 | return error; |
| 3386 | |
Al Viro | 8de5277 | 2012-02-06 12:45:27 -0500 | [diff] [blame] | 3387 | if (max_links && dir->i_nlink >= max_links) |
| 3388 | return -EMLINK; |
| 3389 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3390 | error = dir->i_op->mkdir(dir, dentry, mode); |
Stephen Smalley | a74574a | 2005-09-09 13:01:44 -0700 | [diff] [blame] | 3391 | if (!error) |
Amy Griffis | f38aa94 | 2005-11-03 15:57:06 +0000 | [diff] [blame] | 3392 | fsnotify_mkdir(dir, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3393 | return error; |
| 3394 | } |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 3395 | EXPORT_SYMBOL(vfs_mkdir2); |
| 3396 | |
| 3397 | int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) |
| 3398 | { |
| 3399 | return vfs_mkdir2(NULL, dir, dentry, mode); |
| 3400 | } |
| 3401 | EXPORT_SYMBOL(vfs_mkdir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3402 | |
Al Viro | a218d0f | 2011-11-21 14:59:34 -0500 | [diff] [blame] | 3403 | SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3404 | { |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 3405 | struct dentry *dentry; |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3406 | struct path path; |
| 3407 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3408 | |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3409 | dentry = user_path_create(dfd, pathname, &path, 1); |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 3410 | if (IS_ERR(dentry)) |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3411 | return PTR_ERR(dentry); |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 3412 | |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3413 | if (!IS_POSIXACL(path.dentry->d_inode)) |
Al Viro | ce3b0f8 | 2009-03-29 19:08:22 -0400 | [diff] [blame] | 3414 | mode &= ~current_umask(); |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3415 | error = security_path_mkdir(&path, dentry, mode); |
Al Viro | eb8b9ae | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3416 | if (!error) |
| 3417 | error = vfs_mkdir2(path.mnt, path.dentry->d_inode, dentry, mode); |
Al Viro | 58cc338 | 2012-07-20 01:15:31 +0400 | [diff] [blame] | 3418 | done_path_create(&path, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3419 | return error; |
| 3420 | } |
| 3421 | |
Al Viro | a218d0f | 2011-11-21 14:59:34 -0500 | [diff] [blame] | 3422 | SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 3423 | { |
| 3424 | return sys_mkdirat(AT_FDCWD, pathname, mode); |
| 3425 | } |
| 3426 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3427 | /* |
Sage Weil | a71905f | 2011-05-24 13:06:08 -0700 | [diff] [blame] | 3428 | * The dentry_unhash() helper will try to drop the dentry early: we |
J. Bruce Fields | c0d0259 | 2012-02-15 11:48:40 -0500 | [diff] [blame] | 3429 | * should have a usage count of 1 if we're the only user of this |
Sage Weil | a71905f | 2011-05-24 13:06:08 -0700 | [diff] [blame] | 3430 | * dentry, and if that is true (possibly after pruning the dcache), |
| 3431 | * then we drop the dentry now. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3432 | * |
| 3433 | * A low-level filesystem can, if it choses, legally |
| 3434 | * do a |
| 3435 | * |
| 3436 | * if (!d_unhashed(dentry)) |
| 3437 | * return -EBUSY; |
| 3438 | * |
| 3439 | * if it cannot handle the case of removing a directory |
| 3440 | * that is still in use by something else.. |
| 3441 | */ |
| 3442 | void dentry_unhash(struct dentry *dentry) |
| 3443 | { |
Vasily Averin | dc16842 | 2006-12-06 20:37:07 -0800 | [diff] [blame] | 3444 | shrink_dcache_parent(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3445 | spin_lock(&dentry->d_lock); |
Sage Weil | 64252c7 | 2011-05-24 13:06:05 -0700 | [diff] [blame] | 3446 | if (dentry->d_count == 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3447 | __d_drop(dentry); |
| 3448 | spin_unlock(&dentry->d_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3449 | } |
| 3450 | |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 3451 | int vfs_rmdir2(struct vfsmount *mnt, struct inode *dir, struct dentry *dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3452 | { |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 3453 | int error = may_delete(mnt, dir, dentry, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3454 | |
| 3455 | if (error) |
| 3456 | return error; |
| 3457 | |
Al Viro | acfa438 | 2008-12-04 10:06:33 -0500 | [diff] [blame] | 3458 | if (!dir->i_op->rmdir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3459 | return -EPERM; |
| 3460 | |
Al Viro | 1d2ef59 | 2011-09-14 18:55:41 +0100 | [diff] [blame] | 3461 | dget(dentry); |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 3462 | mutex_lock(&dentry->d_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3463 | |
Sage Weil | 912dbc1 | 2011-05-24 13:06:11 -0700 | [diff] [blame] | 3464 | error = -EBUSY; |
| 3465 | if (d_mountpoint(dentry)) |
| 3466 | goto out; |
| 3467 | |
| 3468 | error = security_inode_rmdir(dir, dentry); |
| 3469 | if (error) |
| 3470 | goto out; |
| 3471 | |
Sage Weil | 3cebde2 | 2011-05-29 21:20:59 -0700 | [diff] [blame] | 3472 | shrink_dcache_parent(dentry); |
Sage Weil | 912dbc1 | 2011-05-24 13:06:11 -0700 | [diff] [blame] | 3473 | error = dir->i_op->rmdir(dir, dentry); |
| 3474 | if (error) |
| 3475 | goto out; |
| 3476 | |
| 3477 | dentry->d_inode->i_flags |= S_DEAD; |
| 3478 | dont_mount(dentry); |
| 3479 | |
| 3480 | out: |
| 3481 | mutex_unlock(&dentry->d_inode->i_mutex); |
Al Viro | 1d2ef59 | 2011-09-14 18:55:41 +0100 | [diff] [blame] | 3482 | dput(dentry); |
Sage Weil | 912dbc1 | 2011-05-24 13:06:11 -0700 | [diff] [blame] | 3483 | if (!error) |
| 3484 | d_delete(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3485 | return error; |
| 3486 | } |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 3487 | EXPORT_SYMBOL(vfs_rmdir2); |
| 3488 | |
| 3489 | int vfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 3490 | { |
| 3491 | return vfs_rmdir2(NULL, dir, dentry); |
| 3492 | } |
| 3493 | EXPORT_SYMBOL(vfs_rmdir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3494 | |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 3495 | static long do_rmdir(int dfd, const char __user *pathname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3496 | { |
| 3497 | int error = 0; |
Jeff Layton | 9cee5ca | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 3498 | struct filename *name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3499 | struct dentry *dentry; |
| 3500 | struct nameidata nd; |
| 3501 | |
Jeff Layton | 9cee5ca | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 3502 | name = user_path_parent(dfd, pathname, &nd); |
| 3503 | if (IS_ERR(name)) |
| 3504 | return PTR_ERR(name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3505 | |
| 3506 | switch(nd.last_type) { |
OGAWA Hirofumi | 0612d9f | 2008-10-16 07:50:29 +0900 | [diff] [blame] | 3507 | case LAST_DOTDOT: |
| 3508 | error = -ENOTEMPTY; |
| 3509 | goto exit1; |
| 3510 | case LAST_DOT: |
| 3511 | error = -EINVAL; |
| 3512 | goto exit1; |
| 3513 | case LAST_ROOT: |
| 3514 | error = -EBUSY; |
| 3515 | goto exit1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3516 | } |
OGAWA Hirofumi | 0612d9f | 2008-10-16 07:50:29 +0900 | [diff] [blame] | 3517 | |
| 3518 | nd.flags &= ~LOOKUP_PARENT; |
Jan Kara | 7d14039 | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3519 | error = mnt_want_write(nd.path.mnt); |
| 3520 | if (error) |
| 3521 | goto exit1; |
OGAWA Hirofumi | 0612d9f | 2008-10-16 07:50:29 +0900 | [diff] [blame] | 3522 | |
Jan Blunck | 4ac9137 | 2008-02-14 19:34:32 -0800 | [diff] [blame] | 3523 | mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); |
Christoph Hellwig | 49705b7 | 2005-11-08 21:35:06 -0800 | [diff] [blame] | 3524 | dentry = lookup_hash(&nd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3525 | error = PTR_ERR(dentry); |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 3526 | if (IS_ERR(dentry)) |
| 3527 | goto exit2; |
Theodore Ts'o | e6bc45d | 2011-06-06 19:19:40 -0400 | [diff] [blame] | 3528 | if (!dentry->d_inode) { |
| 3529 | error = -ENOENT; |
| 3530 | goto exit3; |
| 3531 | } |
Kentaro Takeda | be6d3e5 | 2008-12-17 13:24:15 +0900 | [diff] [blame] | 3532 | error = security_path_rmdir(&nd.path, dentry); |
| 3533 | if (error) |
Jan Kara | 7d14039 | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3534 | goto exit3; |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 3535 | error = vfs_rmdir2(nd.path.mnt, nd.path.dentry->d_inode, dentry); |
Dave Hansen | 0622753 | 2008-02-15 14:37:34 -0800 | [diff] [blame] | 3536 | exit3: |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 3537 | dput(dentry); |
| 3538 | exit2: |
Jan Blunck | 4ac9137 | 2008-02-14 19:34:32 -0800 | [diff] [blame] | 3539 | mutex_unlock(&nd.path.dentry->d_inode->i_mutex); |
Jan Kara | 7d14039 | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3540 | mnt_drop_write(nd.path.mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3541 | exit1: |
Jan Blunck | 1d957f9 | 2008-02-14 19:34:35 -0800 | [diff] [blame] | 3542 | path_put(&nd.path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3543 | putname(name); |
| 3544 | return error; |
| 3545 | } |
| 3546 | |
Heiko Carstens | 3cdad42 | 2009-01-14 14:14:22 +0100 | [diff] [blame] | 3547 | SYSCALL_DEFINE1(rmdir, const char __user *, pathname) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 3548 | { |
| 3549 | return do_rmdir(AT_FDCWD, pathname); |
| 3550 | } |
| 3551 | |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 3552 | int vfs_unlink2(struct vfsmount *mnt, struct inode *dir, struct dentry *dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3553 | { |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 3554 | int error = may_delete(mnt, dir, dentry, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3555 | |
| 3556 | if (error) |
| 3557 | return error; |
| 3558 | |
Al Viro | acfa438 | 2008-12-04 10:06:33 -0500 | [diff] [blame] | 3559 | if (!dir->i_op->unlink) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3560 | return -EPERM; |
| 3561 | |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 3562 | mutex_lock(&dentry->d_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3563 | if (d_mountpoint(dentry)) |
| 3564 | error = -EBUSY; |
| 3565 | else { |
| 3566 | error = security_inode_unlink(dir, dentry); |
Al Viro | bec1052 | 2010-03-03 14:12:08 -0500 | [diff] [blame] | 3567 | if (!error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3568 | error = dir->i_op->unlink(dir, dentry); |
Al Viro | bec1052 | 2010-03-03 14:12:08 -0500 | [diff] [blame] | 3569 | if (!error) |
Al Viro | d83c49f | 2010-04-30 17:17:09 -0400 | [diff] [blame] | 3570 | dont_mount(dentry); |
Al Viro | bec1052 | 2010-03-03 14:12:08 -0500 | [diff] [blame] | 3571 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3572 | } |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 3573 | mutex_unlock(&dentry->d_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3574 | |
| 3575 | /* We don't d_delete() NFS sillyrenamed files--they still exist. */ |
| 3576 | if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) { |
Jan Kara | ece9591 | 2008-02-06 01:37:13 -0800 | [diff] [blame] | 3577 | fsnotify_link_count(dentry->d_inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3578 | d_delete(dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3579 | } |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 3580 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3581 | return error; |
| 3582 | } |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 3583 | EXPORT_SYMBOL(vfs_unlink2); |
| 3584 | |
| 3585 | int vfs_unlink(struct inode *dir, struct dentry *dentry) |
| 3586 | { |
| 3587 | return vfs_unlink2(NULL, dir, dentry); |
| 3588 | } |
| 3589 | EXPORT_SYMBOL(vfs_unlink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3590 | |
| 3591 | /* |
| 3592 | * Make sure that the actual truncation of the file will occur outside its |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 3593 | * directory's i_mutex. Truncate can take a long time if there is a lot of |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3594 | * writeout happening, and we don't want to prevent access to the directory |
| 3595 | * while waiting on the I/O. |
| 3596 | */ |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 3597 | static long do_unlinkat(int dfd, const char __user *pathname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3598 | { |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 3599 | int error; |
Jeff Layton | 9cee5ca | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 3600 | struct filename *name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3601 | struct dentry *dentry; |
| 3602 | struct nameidata nd; |
| 3603 | struct inode *inode = NULL; |
| 3604 | |
Jeff Layton | 9cee5ca | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 3605 | name = user_path_parent(dfd, pathname, &nd); |
| 3606 | if (IS_ERR(name)) |
| 3607 | return PTR_ERR(name); |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 3608 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3609 | error = -EISDIR; |
| 3610 | if (nd.last_type != LAST_NORM) |
| 3611 | goto exit1; |
OGAWA Hirofumi | 0612d9f | 2008-10-16 07:50:29 +0900 | [diff] [blame] | 3612 | |
| 3613 | nd.flags &= ~LOOKUP_PARENT; |
Jan Kara | 7d14039 | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3614 | error = mnt_want_write(nd.path.mnt); |
| 3615 | if (error) |
| 3616 | goto exit1; |
OGAWA Hirofumi | 0612d9f | 2008-10-16 07:50:29 +0900 | [diff] [blame] | 3617 | |
Jan Blunck | 4ac9137 | 2008-02-14 19:34:32 -0800 | [diff] [blame] | 3618 | mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); |
Christoph Hellwig | 49705b7 | 2005-11-08 21:35:06 -0800 | [diff] [blame] | 3619 | dentry = lookup_hash(&nd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3620 | error = PTR_ERR(dentry); |
| 3621 | if (!IS_ERR(dentry)) { |
| 3622 | /* Why not before? Because we want correct error value */ |
Török Edwin | 50338b8 | 2011-06-16 00:06:14 +0300 | [diff] [blame] | 3623 | if (nd.last.name[nd.last.len]) |
| 3624 | goto slashes; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3625 | inode = dentry->d_inode; |
Török Edwin | 50338b8 | 2011-06-16 00:06:14 +0300 | [diff] [blame] | 3626 | if (!inode) |
Theodore Ts'o | e6bc45d | 2011-06-06 19:19:40 -0400 | [diff] [blame] | 3627 | goto slashes; |
| 3628 | ihold(inode); |
Kentaro Takeda | be6d3e5 | 2008-12-17 13:24:15 +0900 | [diff] [blame] | 3629 | error = security_path_unlink(&nd.path, dentry); |
| 3630 | if (error) |
Jan Kara | 7d14039 | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3631 | goto exit2; |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 3632 | error = vfs_unlink2(nd.path.mnt, nd.path.dentry->d_inode, dentry); |
Jan Kara | 7d14039 | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3633 | exit2: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3634 | dput(dentry); |
| 3635 | } |
Jan Blunck | 4ac9137 | 2008-02-14 19:34:32 -0800 | [diff] [blame] | 3636 | mutex_unlock(&nd.path.dentry->d_inode->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3637 | if (inode) |
| 3638 | iput(inode); /* truncate the inode here */ |
Jan Kara | 7d14039 | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 3639 | mnt_drop_write(nd.path.mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3640 | exit1: |
Jan Blunck | 1d957f9 | 2008-02-14 19:34:35 -0800 | [diff] [blame] | 3641 | path_put(&nd.path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3642 | putname(name); |
| 3643 | return error; |
| 3644 | |
| 3645 | slashes: |
| 3646 | error = !dentry->d_inode ? -ENOENT : |
| 3647 | S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR; |
| 3648 | goto exit2; |
| 3649 | } |
| 3650 | |
Heiko Carstens | 2e4d092 | 2009-01-14 14:14:31 +0100 | [diff] [blame] | 3651 | SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 3652 | { |
| 3653 | if ((flag & ~AT_REMOVEDIR) != 0) |
| 3654 | return -EINVAL; |
| 3655 | |
| 3656 | if (flag & AT_REMOVEDIR) |
| 3657 | return do_rmdir(dfd, pathname); |
| 3658 | |
| 3659 | return do_unlinkat(dfd, pathname); |
| 3660 | } |
| 3661 | |
Heiko Carstens | 3480b25 | 2009-01-14 14:14:16 +0100 | [diff] [blame] | 3662 | SYSCALL_DEFINE1(unlink, const char __user *, pathname) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 3663 | { |
| 3664 | return do_unlinkat(AT_FDCWD, pathname); |
| 3665 | } |
| 3666 | |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 3667 | int vfs_symlink2(struct vfsmount *mnt, struct inode *dir, struct dentry *dentry, const char *oldname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3668 | { |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 3669 | int error = may_create(mnt, dir, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3670 | |
| 3671 | if (error) |
| 3672 | return error; |
| 3673 | |
Al Viro | acfa438 | 2008-12-04 10:06:33 -0500 | [diff] [blame] | 3674 | if (!dir->i_op->symlink) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3675 | return -EPERM; |
| 3676 | |
| 3677 | error = security_inode_symlink(dir, dentry, oldname); |
| 3678 | if (error) |
| 3679 | return error; |
| 3680 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3681 | error = dir->i_op->symlink(dir, dentry, oldname); |
Stephen Smalley | a74574a | 2005-09-09 13:01:44 -0700 | [diff] [blame] | 3682 | if (!error) |
Amy Griffis | f38aa94 | 2005-11-03 15:57:06 +0000 | [diff] [blame] | 3683 | fsnotify_create(dir, dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3684 | return error; |
| 3685 | } |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 3686 | EXPORT_SYMBOL(vfs_symlink2); |
| 3687 | |
| 3688 | int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname) |
| 3689 | { |
| 3690 | return vfs_symlink2(NULL, dir, dentry, oldname); |
| 3691 | } |
| 3692 | EXPORT_SYMBOL(vfs_symlink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3693 | |
Heiko Carstens | 2e4d092 | 2009-01-14 14:14:31 +0100 | [diff] [blame] | 3694 | SYSCALL_DEFINE3(symlinkat, const char __user *, oldname, |
| 3695 | int, newdfd, const char __user *, newname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3696 | { |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 3697 | int error; |
Jeff Layton | 9cee5ca | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 3698 | struct filename *from; |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 3699 | struct dentry *dentry; |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3700 | struct path path; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3701 | |
| 3702 | from = getname(oldname); |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 3703 | if (IS_ERR(from)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3704 | return PTR_ERR(from); |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 3705 | |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3706 | dentry = user_path_create(newdfd, newname, &path, 0); |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 3707 | error = PTR_ERR(dentry); |
| 3708 | if (IS_ERR(dentry)) |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3709 | goto out_putname; |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 3710 | |
Jeff Layton | 9cee5ca | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 3711 | error = security_path_symlink(&path, dentry, from->name); |
Al Viro | eb8b9ae | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3712 | if (!error) |
Jeff Layton | 9cee5ca | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 3713 | error = vfs_symlink2(path.mnt, path.dentry->d_inode, dentry, from->name); |
Al Viro | 58cc338 | 2012-07-20 01:15:31 +0400 | [diff] [blame] | 3714 | done_path_create(&path, dentry); |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 3715 | out_putname: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3716 | putname(from); |
| 3717 | return error; |
| 3718 | } |
| 3719 | |
Heiko Carstens | 3480b25 | 2009-01-14 14:14:16 +0100 | [diff] [blame] | 3720 | SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 3721 | { |
| 3722 | return sys_symlinkat(oldname, AT_FDCWD, newname); |
| 3723 | } |
| 3724 | |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 3725 | int vfs_link2(struct vfsmount *mnt, struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3726 | { |
| 3727 | struct inode *inode = old_dentry->d_inode; |
Al Viro | 8de5277 | 2012-02-06 12:45:27 -0500 | [diff] [blame] | 3728 | unsigned max_links = dir->i_sb->s_max_links; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3729 | int error; |
| 3730 | |
| 3731 | if (!inode) |
| 3732 | return -ENOENT; |
| 3733 | |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 3734 | error = may_create(mnt, dir, new_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3735 | if (error) |
| 3736 | return error; |
| 3737 | |
| 3738 | if (dir->i_sb != inode->i_sb) |
| 3739 | return -EXDEV; |
| 3740 | |
| 3741 | /* |
| 3742 | * A link to an append-only or immutable file cannot be created. |
| 3743 | */ |
| 3744 | if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) |
| 3745 | return -EPERM; |
Al Viro | acfa438 | 2008-12-04 10:06:33 -0500 | [diff] [blame] | 3746 | if (!dir->i_op->link) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3747 | return -EPERM; |
Tetsuo Handa | 7e79eed | 2008-06-24 16:50:15 +0200 | [diff] [blame] | 3748 | if (S_ISDIR(inode->i_mode)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3749 | return -EPERM; |
| 3750 | |
| 3751 | error = security_inode_link(old_dentry, dir, new_dentry); |
| 3752 | if (error) |
| 3753 | return error; |
| 3754 | |
Tetsuo Handa | 7e79eed | 2008-06-24 16:50:15 +0200 | [diff] [blame] | 3755 | mutex_lock(&inode->i_mutex); |
Aneesh Kumar K.V | aae8a97 | 2011-01-29 18:43:27 +0530 | [diff] [blame] | 3756 | /* Make sure we don't allow creating hardlink to an unlinked file */ |
Al Viro | f6f3426 | 2013-06-11 08:34:36 +0400 | [diff] [blame] | 3757 | if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE)) |
Aneesh Kumar K.V | aae8a97 | 2011-01-29 18:43:27 +0530 | [diff] [blame] | 3758 | error = -ENOENT; |
Al Viro | 8de5277 | 2012-02-06 12:45:27 -0500 | [diff] [blame] | 3759 | else if (max_links && inode->i_nlink >= max_links) |
| 3760 | error = -EMLINK; |
Aneesh Kumar K.V | aae8a97 | 2011-01-29 18:43:27 +0530 | [diff] [blame] | 3761 | else |
| 3762 | error = dir->i_op->link(old_dentry, dir, new_dentry); |
Al Viro | f6f3426 | 2013-06-11 08:34:36 +0400 | [diff] [blame] | 3763 | |
| 3764 | if (!error && (inode->i_state & I_LINKABLE)) { |
| 3765 | spin_lock(&inode->i_lock); |
| 3766 | inode->i_state &= ~I_LINKABLE; |
| 3767 | spin_unlock(&inode->i_lock); |
| 3768 | } |
Tetsuo Handa | 7e79eed | 2008-06-24 16:50:15 +0200 | [diff] [blame] | 3769 | mutex_unlock(&inode->i_mutex); |
Stephen Smalley | e31e14e | 2005-09-09 13:01:45 -0700 | [diff] [blame] | 3770 | if (!error) |
Tetsuo Handa | 7e79eed | 2008-06-24 16:50:15 +0200 | [diff] [blame] | 3771 | fsnotify_link(dir, inode, new_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3772 | return error; |
| 3773 | } |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 3774 | EXPORT_SYMBOL(vfs_link2); |
| 3775 | |
| 3776 | int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) |
| 3777 | { |
| 3778 | return vfs_link2(NULL, old_dentry, dir, new_dentry); |
| 3779 | } |
| 3780 | EXPORT_SYMBOL(vfs_link); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3781 | |
| 3782 | /* |
| 3783 | * Hardlinks are often used in delicate situations. We avoid |
| 3784 | * security-related surprises by not following symlinks on the |
| 3785 | * newname. --KAB |
| 3786 | * |
| 3787 | * We don't follow them on the oldname either to be compatible |
| 3788 | * with linux 2.0, and to avoid hard-linking to directories |
| 3789 | * and other special files. --ADM |
| 3790 | */ |
Heiko Carstens | 2e4d092 | 2009-01-14 14:14:31 +0100 | [diff] [blame] | 3791 | SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname, |
| 3792 | int, newdfd, const char __user *, newname, int, flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3793 | { |
| 3794 | struct dentry *new_dentry; |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3795 | struct path old_path, new_path; |
Aneesh Kumar K.V | 11a7b37 | 2011-01-29 18:43:42 +0530 | [diff] [blame] | 3796 | int how = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3797 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3798 | |
Aneesh Kumar K.V | 11a7b37 | 2011-01-29 18:43:42 +0530 | [diff] [blame] | 3799 | if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0) |
Ulrich Drepper | c04030e | 2006-02-24 13:04:21 -0800 | [diff] [blame] | 3800 | return -EINVAL; |
Aneesh Kumar K.V | 11a7b37 | 2011-01-29 18:43:42 +0530 | [diff] [blame] | 3801 | /* |
| 3802 | * To use null names we require CAP_DAC_READ_SEARCH |
| 3803 | * This ensures that not everyone will be able to create |
| 3804 | * handlink using the passed filedescriptor. |
| 3805 | */ |
| 3806 | if (flags & AT_EMPTY_PATH) { |
| 3807 | if (!capable(CAP_DAC_READ_SEARCH)) |
| 3808 | return -ENOENT; |
| 3809 | how = LOOKUP_EMPTY; |
| 3810 | } |
Ulrich Drepper | c04030e | 2006-02-24 13:04:21 -0800 | [diff] [blame] | 3811 | |
Aneesh Kumar K.V | 11a7b37 | 2011-01-29 18:43:42 +0530 | [diff] [blame] | 3812 | if (flags & AT_SYMLINK_FOLLOW) |
| 3813 | how |= LOOKUP_FOLLOW; |
| 3814 | |
| 3815 | error = user_path_at(olddfd, oldname, how, &old_path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3816 | if (error) |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 3817 | return error; |
| 3818 | |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3819 | new_dentry = user_path_create(newdfd, newname, &new_path, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3820 | error = PTR_ERR(new_dentry); |
Dave Hansen | 6902d92 | 2006-09-30 23:29:01 -0700 | [diff] [blame] | 3821 | if (IS_ERR(new_dentry)) |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3822 | goto out; |
| 3823 | |
| 3824 | error = -EXDEV; |
| 3825 | if (old_path.mnt != new_path.mnt) |
| 3826 | goto out_dput; |
Kees Cook | 5092e9c | 2012-07-25 17:29:07 -0700 | [diff] [blame] | 3827 | error = may_linkat(&old_path); |
| 3828 | if (unlikely(error)) |
Dave Hansen | 75c3f29 | 2008-02-15 14:37:45 -0800 | [diff] [blame] | 3829 | goto out_dput; |
Al Viro | dae6ad8 | 2011-06-26 11:50:15 -0400 | [diff] [blame] | 3830 | error = security_path_link(old_path.dentry, &new_path, new_dentry); |
Kentaro Takeda | be6d3e5 | 2008-12-17 13:24:15 +0900 | [diff] [blame] | 3831 | if (error) |
Al Viro | eb8b9ae | 2012-07-20 02:25:00 +0400 | [diff] [blame] | 3832 | goto out_dput; |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 3833 | error = vfs_link2(old_path.mnt, old_path.dentry, new_path.dentry->d_inode, new_dentry); |
Dave Hansen | 75c3f29 | 2008-02-15 14:37:45 -0800 | [diff] [blame] | 3834 | out_dput: |
Al Viro | 58cc338 | 2012-07-20 01:15:31 +0400 | [diff] [blame] | 3835 | done_path_create(&new_path, new_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3836 | out: |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 3837 | path_put(&old_path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3838 | |
| 3839 | return error; |
| 3840 | } |
| 3841 | |
Heiko Carstens | 3480b25 | 2009-01-14 14:14:16 +0100 | [diff] [blame] | 3842 | SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 3843 | { |
Ulrich Drepper | c04030e | 2006-02-24 13:04:21 -0800 | [diff] [blame] | 3844 | return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0); |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 3845 | } |
| 3846 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3847 | /* |
| 3848 | * The worst of all namespace operations - renaming directory. "Perverted" |
| 3849 | * doesn't even start to describe it. Somebody in UCB had a heck of a trip... |
| 3850 | * Problems: |
| 3851 | * a) we can get into loop creation. Check is done in is_subdir(). |
| 3852 | * b) race potential - two innocent renames can create a loop together. |
| 3853 | * That's where 4.4 screws up. Current fix: serialization on |
Arjan van de Ven | a11f3a0 | 2006-03-23 03:00:33 -0800 | [diff] [blame] | 3854 | * sb->s_vfs_rename_mutex. We might be more accurate, but that's another |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3855 | * story. |
| 3856 | * c) we have to lock _three_ objects - parents and victim (if it exists). |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 3857 | * And that - after we got ->i_mutex on parents (until then we don't know |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3858 | * whether the target exists). Solution: try to be smart with locking |
| 3859 | * order for inodes. We rely on the fact that tree topology may change |
Arjan van de Ven | a11f3a0 | 2006-03-23 03:00:33 -0800 | [diff] [blame] | 3860 | * only under ->s_vfs_rename_mutex _and_ that parent of the object we |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3861 | * move will be locked. Thus we can rank directories by the tree |
| 3862 | * (ancestors first) and rank all non-directories after them. |
| 3863 | * That works since everybody except rename does "lock parent, lookup, |
Arjan van de Ven | a11f3a0 | 2006-03-23 03:00:33 -0800 | [diff] [blame] | 3864 | * lock child" and rename is under ->s_vfs_rename_mutex. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3865 | * HOWEVER, it relies on the assumption that any object with ->lookup() |
| 3866 | * has no more than 1 dentry. If "hybrid" objects will ever appear, |
| 3867 | * we'd better make sure that there's no link(2) for them. |
Sage Weil | e4eaac0 | 2011-05-24 13:06:07 -0700 | [diff] [blame] | 3868 | * d) conversion from fhandle to dentry may come in the wrong moment - when |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 3869 | * we are removing the target. Solution: we will have to grab ->i_mutex |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3870 | * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on |
Adam Buchbinder | c41b20e | 2009-12-11 16:35:39 -0500 | [diff] [blame] | 3871 | * ->i_mutex on parents, which works but leads to some truly excessive |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3872 | * locking]. |
| 3873 | */ |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 3874 | static int vfs_rename_dir(struct vfsmount *mnt, |
| 3875 | struct inode *old_dir, struct dentry *old_dentry, |
| 3876 | struct inode *new_dir, struct dentry *new_dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3877 | { |
| 3878 | int error = 0; |
Sage Weil | 9055cba | 2011-05-24 13:06:12 -0700 | [diff] [blame] | 3879 | struct inode *target = new_dentry->d_inode; |
Al Viro | 8de5277 | 2012-02-06 12:45:27 -0500 | [diff] [blame] | 3880 | unsigned max_links = new_dir->i_sb->s_max_links; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3881 | |
| 3882 | /* |
| 3883 | * If we are going to change the parent - check write permissions, |
| 3884 | * we'll need to flip '..'. |
| 3885 | */ |
| 3886 | if (new_dir != old_dir) { |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 3887 | error = inode_permission2(mnt, old_dentry->d_inode, MAY_WRITE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3888 | if (error) |
| 3889 | return error; |
| 3890 | } |
| 3891 | |
| 3892 | error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); |
| 3893 | if (error) |
| 3894 | return error; |
| 3895 | |
Al Viro | 1d2ef59 | 2011-09-14 18:55:41 +0100 | [diff] [blame] | 3896 | dget(new_dentry); |
Al Viro | d83c49f | 2010-04-30 17:17:09 -0400 | [diff] [blame] | 3897 | if (target) |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 3898 | mutex_lock(&target->i_mutex); |
Sage Weil | 9055cba | 2011-05-24 13:06:12 -0700 | [diff] [blame] | 3899 | |
| 3900 | error = -EBUSY; |
| 3901 | if (d_mountpoint(old_dentry) || d_mountpoint(new_dentry)) |
| 3902 | goto out; |
| 3903 | |
Al Viro | 8de5277 | 2012-02-06 12:45:27 -0500 | [diff] [blame] | 3904 | error = -EMLINK; |
| 3905 | if (max_links && !target && new_dir != old_dir && |
| 3906 | new_dir->i_nlink >= max_links) |
| 3907 | goto out; |
| 3908 | |
Sage Weil | 3cebde2 | 2011-05-29 21:20:59 -0700 | [diff] [blame] | 3909 | if (target) |
| 3910 | shrink_dcache_parent(new_dentry); |
Sage Weil | 9055cba | 2011-05-24 13:06:12 -0700 | [diff] [blame] | 3911 | error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); |
| 3912 | if (error) |
| 3913 | goto out; |
| 3914 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3915 | if (target) { |
Sage Weil | 9055cba | 2011-05-24 13:06:12 -0700 | [diff] [blame] | 3916 | target->i_flags |= S_DEAD; |
| 3917 | dont_mount(new_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3918 | } |
Sage Weil | 9055cba | 2011-05-24 13:06:12 -0700 | [diff] [blame] | 3919 | out: |
| 3920 | if (target) |
| 3921 | mutex_unlock(&target->i_mutex); |
Al Viro | 1d2ef59 | 2011-09-14 18:55:41 +0100 | [diff] [blame] | 3922 | dput(new_dentry); |
Stephen Smalley | e31e14e | 2005-09-09 13:01:45 -0700 | [diff] [blame] | 3923 | if (!error) |
Mark Fasheh | 349457c | 2006-09-08 14:22:21 -0700 | [diff] [blame] | 3924 | if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) |
| 3925 | d_move(old_dentry,new_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3926 | return error; |
| 3927 | } |
| 3928 | |
Adrian Bunk | 75c96f8 | 2005-05-05 16:16:09 -0700 | [diff] [blame] | 3929 | static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry, |
| 3930 | struct inode *new_dir, struct dentry *new_dentry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3931 | { |
Sage Weil | 51892bb | 2011-05-24 13:06:13 -0700 | [diff] [blame] | 3932 | struct inode *target = new_dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3933 | int error; |
| 3934 | |
| 3935 | error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); |
| 3936 | if (error) |
| 3937 | return error; |
| 3938 | |
| 3939 | dget(new_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3940 | if (target) |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 3941 | mutex_lock(&target->i_mutex); |
Sage Weil | 51892bb | 2011-05-24 13:06:13 -0700 | [diff] [blame] | 3942 | |
| 3943 | error = -EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3944 | if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry)) |
Sage Weil | 51892bb | 2011-05-24 13:06:13 -0700 | [diff] [blame] | 3945 | goto out; |
| 3946 | |
| 3947 | error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); |
| 3948 | if (error) |
| 3949 | goto out; |
| 3950 | |
| 3951 | if (target) |
| 3952 | dont_mount(new_dentry); |
| 3953 | if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) |
| 3954 | d_move(old_dentry, new_dentry); |
| 3955 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3956 | if (target) |
Jes Sorensen | 1b1dcc1 | 2006-01-09 15:59:24 -0800 | [diff] [blame] | 3957 | mutex_unlock(&target->i_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3958 | dput(new_dentry); |
| 3959 | return error; |
| 3960 | } |
| 3961 | |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 3962 | int vfs_rename2(struct vfsmount *mnt, |
| 3963 | struct inode *old_dir, struct dentry *old_dentry, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3964 | struct inode *new_dir, struct dentry *new_dentry) |
| 3965 | { |
| 3966 | int error; |
| 3967 | int is_dir = S_ISDIR(old_dentry->d_inode->i_mode); |
Al Viro | 6e71d85 | 2017-07-07 14:51:19 -0400 | [diff] [blame] | 3968 | struct name_snapshot old_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3969 | |
| 3970 | if (old_dentry->d_inode == new_dentry->d_inode) |
| 3971 | return 0; |
| 3972 | |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 3973 | error = may_delete(mnt, old_dir, old_dentry, is_dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3974 | if (error) |
| 3975 | return error; |
| 3976 | |
| 3977 | if (!new_dentry->d_inode) |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 3978 | error = may_create(mnt, new_dir, new_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3979 | else |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 3980 | error = may_delete(mnt, new_dir, new_dentry, is_dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3981 | if (error) |
| 3982 | return error; |
| 3983 | |
Al Viro | acfa438 | 2008-12-04 10:06:33 -0500 | [diff] [blame] | 3984 | if (!old_dir->i_op->rename) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3985 | return -EPERM; |
| 3986 | |
Al Viro | 6e71d85 | 2017-07-07 14:51:19 -0400 | [diff] [blame] | 3987 | take_dentry_name_snapshot(&old_name, old_dentry); |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 3988 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3989 | if (is_dir) |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 3990 | error = vfs_rename_dir(mnt, old_dir,old_dentry,new_dir,new_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3991 | else |
| 3992 | error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry); |
Al Viro | 123df29 | 2009-12-25 04:57:57 -0500 | [diff] [blame] | 3993 | if (!error) |
Al Viro | 6e71d85 | 2017-07-07 14:51:19 -0400 | [diff] [blame] | 3994 | fsnotify_move(old_dir, new_dir, old_name.name, is_dir, |
Al Viro | 5a190ae | 2007-06-07 12:19:32 -0400 | [diff] [blame] | 3995 | new_dentry->d_inode, old_dentry); |
Al Viro | 6e71d85 | 2017-07-07 14:51:19 -0400 | [diff] [blame] | 3996 | release_dentry_name_snapshot(&old_name); |
Robert Love | 0eeca28 | 2005-07-12 17:06:03 -0400 | [diff] [blame] | 3997 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3998 | return error; |
| 3999 | } |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 4000 | EXPORT_SYMBOL(vfs_rename2); |
| 4001 | |
| 4002 | int vfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 4003 | struct inode *new_dir, struct dentry *new_dentry) |
| 4004 | { |
| 4005 | return vfs_rename2(NULL, old_dir, old_dentry, new_dir, new_dentry); |
| 4006 | } |
| 4007 | EXPORT_SYMBOL(vfs_rename); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4008 | |
Heiko Carstens | 2e4d092 | 2009-01-14 14:14:31 +0100 | [diff] [blame] | 4009 | SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname, |
| 4010 | int, newdfd, const char __user *, newname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4011 | { |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 4012 | struct dentry *old_dir, *new_dir; |
| 4013 | struct dentry *old_dentry, *new_dentry; |
| 4014 | struct dentry *trap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4015 | struct nameidata oldnd, newnd; |
Jeff Layton | 9cee5ca | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 4016 | struct filename *from; |
| 4017 | struct filename *to; |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 4018 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4019 | |
Jeff Layton | 9cee5ca | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 4020 | from = user_path_parent(olddfd, oldname, &oldnd); |
| 4021 | if (IS_ERR(from)) { |
| 4022 | error = PTR_ERR(from); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4023 | goto exit; |
Jeff Layton | 9cee5ca | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 4024 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4025 | |
Jeff Layton | 9cee5ca | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 4026 | to = user_path_parent(newdfd, newname, &newnd); |
| 4027 | if (IS_ERR(to)) { |
| 4028 | error = PTR_ERR(to); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4029 | goto exit1; |
Jeff Layton | 9cee5ca | 2012-10-10 15:25:28 -0400 | [diff] [blame] | 4030 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4031 | |
| 4032 | error = -EXDEV; |
Jan Blunck | 4ac9137 | 2008-02-14 19:34:32 -0800 | [diff] [blame] | 4033 | if (oldnd.path.mnt != newnd.path.mnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4034 | goto exit2; |
| 4035 | |
Jan Blunck | 4ac9137 | 2008-02-14 19:34:32 -0800 | [diff] [blame] | 4036 | old_dir = oldnd.path.dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4037 | error = -EBUSY; |
| 4038 | if (oldnd.last_type != LAST_NORM) |
| 4039 | goto exit2; |
| 4040 | |
Jan Blunck | 4ac9137 | 2008-02-14 19:34:32 -0800 | [diff] [blame] | 4041 | new_dir = newnd.path.dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4042 | if (newnd.last_type != LAST_NORM) |
| 4043 | goto exit2; |
| 4044 | |
Jan Kara | 7d14039 | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 4045 | error = mnt_want_write(oldnd.path.mnt); |
| 4046 | if (error) |
| 4047 | goto exit2; |
| 4048 | |
OGAWA Hirofumi | 0612d9f | 2008-10-16 07:50:29 +0900 | [diff] [blame] | 4049 | oldnd.flags &= ~LOOKUP_PARENT; |
| 4050 | newnd.flags &= ~LOOKUP_PARENT; |
OGAWA Hirofumi | 4e9ed2f | 2008-10-16 07:50:29 +0900 | [diff] [blame] | 4051 | newnd.flags |= LOOKUP_RENAME_TARGET; |
OGAWA Hirofumi | 0612d9f | 2008-10-16 07:50:29 +0900 | [diff] [blame] | 4052 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4053 | trap = lock_rename(new_dir, old_dir); |
| 4054 | |
Christoph Hellwig | 49705b7 | 2005-11-08 21:35:06 -0800 | [diff] [blame] | 4055 | old_dentry = lookup_hash(&oldnd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4056 | error = PTR_ERR(old_dentry); |
| 4057 | if (IS_ERR(old_dentry)) |
| 4058 | goto exit3; |
| 4059 | /* source must exist */ |
| 4060 | error = -ENOENT; |
| 4061 | if (!old_dentry->d_inode) |
| 4062 | goto exit4; |
| 4063 | /* unless the source is a directory trailing slashes give -ENOTDIR */ |
| 4064 | if (!S_ISDIR(old_dentry->d_inode->i_mode)) { |
| 4065 | error = -ENOTDIR; |
| 4066 | if (oldnd.last.name[oldnd.last.len]) |
| 4067 | goto exit4; |
| 4068 | if (newnd.last.name[newnd.last.len]) |
| 4069 | goto exit4; |
| 4070 | } |
| 4071 | /* source should not be ancestor of target */ |
| 4072 | error = -EINVAL; |
| 4073 | if (old_dentry == trap) |
| 4074 | goto exit4; |
Christoph Hellwig | 49705b7 | 2005-11-08 21:35:06 -0800 | [diff] [blame] | 4075 | new_dentry = lookup_hash(&newnd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4076 | error = PTR_ERR(new_dentry); |
| 4077 | if (IS_ERR(new_dentry)) |
| 4078 | goto exit4; |
| 4079 | /* target should not be an ancestor of source */ |
| 4080 | error = -ENOTEMPTY; |
| 4081 | if (new_dentry == trap) |
| 4082 | goto exit5; |
| 4083 | |
Kentaro Takeda | be6d3e5 | 2008-12-17 13:24:15 +0900 | [diff] [blame] | 4084 | error = security_path_rename(&oldnd.path, old_dentry, |
| 4085 | &newnd.path, new_dentry); |
| 4086 | if (error) |
Jan Kara | 7d14039 | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 4087 | goto exit5; |
Daniel Rosenberg | 1728511 | 2016-10-26 16:27:45 -0700 | [diff] [blame] | 4088 | error = vfs_rename2(oldnd.path.mnt, old_dir->d_inode, old_dentry, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4089 | new_dir->d_inode, new_dentry); |
| 4090 | exit5: |
| 4091 | dput(new_dentry); |
| 4092 | exit4: |
| 4093 | dput(old_dentry); |
| 4094 | exit3: |
| 4095 | unlock_rename(new_dir, old_dir); |
Jan Kara | 7d14039 | 2012-06-12 16:20:30 +0200 | [diff] [blame] | 4096 | mnt_drop_write(oldnd.path.mnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4097 | exit2: |
Jan Blunck | 1d957f9 | 2008-02-14 19:34:35 -0800 | [diff] [blame] | 4098 | path_put(&newnd.path); |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 4099 | putname(to); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4100 | exit1: |
Jan Blunck | 1d957f9 | 2008-02-14 19:34:35 -0800 | [diff] [blame] | 4101 | path_put(&oldnd.path); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4102 | putname(from); |
Al Viro | 2ad94ae | 2008-07-21 09:32:51 -0400 | [diff] [blame] | 4103 | exit: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4104 | return error; |
| 4105 | } |
| 4106 | |
Heiko Carstens | a26eab2 | 2009-01-14 14:14:17 +0100 | [diff] [blame] | 4107 | SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname) |
Ulrich Drepper | 5590ff0 | 2006-01-18 17:43:53 -0800 | [diff] [blame] | 4108 | { |
| 4109 | return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname); |
| 4110 | } |
| 4111 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4112 | int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link) |
| 4113 | { |
| 4114 | int len; |
| 4115 | |
| 4116 | len = PTR_ERR(link); |
| 4117 | if (IS_ERR(link)) |
| 4118 | goto out; |
| 4119 | |
| 4120 | len = strlen(link); |
| 4121 | if (len > (unsigned) buflen) |
| 4122 | len = buflen; |
| 4123 | if (copy_to_user(buffer, link, len)) |
| 4124 | len = -EFAULT; |
| 4125 | out: |
| 4126 | return len; |
| 4127 | } |
| 4128 | |
| 4129 | /* |
| 4130 | * A helper for ->readlink(). This should be used *ONLY* for symlinks that |
| 4131 | * have ->follow_link() touching nd only in nd_set_link(). Using (or not |
| 4132 | * using) it for any given inode is up to filesystem. |
| 4133 | */ |
| 4134 | int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen) |
| 4135 | { |
| 4136 | struct nameidata nd; |
Linus Torvalds | cc314ee | 2005-08-19 18:02:56 -0700 | [diff] [blame] | 4137 | void *cookie; |
Marcin Slusarz | 694a176 | 2008-06-09 16:40:37 -0700 | [diff] [blame] | 4138 | int res; |
Linus Torvalds | cc314ee | 2005-08-19 18:02:56 -0700 | [diff] [blame] | 4139 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4140 | nd.depth = 0; |
Linus Torvalds | cc314ee | 2005-08-19 18:02:56 -0700 | [diff] [blame] | 4141 | cookie = dentry->d_inode->i_op->follow_link(dentry, &nd); |
Marcin Slusarz | 694a176 | 2008-06-09 16:40:37 -0700 | [diff] [blame] | 4142 | if (IS_ERR(cookie)) |
| 4143 | return PTR_ERR(cookie); |
| 4144 | |
| 4145 | res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd)); |
| 4146 | if (dentry->d_inode->i_op->put_link) |
| 4147 | dentry->d_inode->i_op->put_link(dentry, &nd, cookie); |
| 4148 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4149 | } |
| 4150 | |
| 4151 | int vfs_follow_link(struct nameidata *nd, const char *link) |
| 4152 | { |
| 4153 | return __vfs_follow_link(nd, link); |
| 4154 | } |
| 4155 | |
| 4156 | /* get the link contents into pagecache */ |
| 4157 | static char *page_getlink(struct dentry * dentry, struct page **ppage) |
| 4158 | { |
Duane Griffin | ebd09ab | 2008-12-19 20:47:12 +0000 | [diff] [blame] | 4159 | char *kaddr; |
| 4160 | struct page *page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4161 | struct address_space *mapping = dentry->d_inode->i_mapping; |
Pekka Enberg | 090d2b1 | 2006-06-23 02:05:08 -0700 | [diff] [blame] | 4162 | page = read_mapping_page(mapping, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4163 | if (IS_ERR(page)) |
Nick Piggin | 6fe6900 | 2007-05-06 14:49:04 -0700 | [diff] [blame] | 4164 | return (char*)page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4165 | *ppage = page; |
Duane Griffin | ebd09ab | 2008-12-19 20:47:12 +0000 | [diff] [blame] | 4166 | kaddr = kmap(page); |
| 4167 | nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1); |
| 4168 | return kaddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4169 | } |
| 4170 | |
| 4171 | int page_readlink(struct dentry *dentry, char __user *buffer, int buflen) |
| 4172 | { |
| 4173 | struct page *page = NULL; |
| 4174 | char *s = page_getlink(dentry, &page); |
| 4175 | int res = vfs_readlink(dentry,buffer,buflen,s); |
| 4176 | if (page) { |
| 4177 | kunmap(page); |
| 4178 | page_cache_release(page); |
| 4179 | } |
| 4180 | return res; |
| 4181 | } |
| 4182 | |
Linus Torvalds | cc314ee | 2005-08-19 18:02:56 -0700 | [diff] [blame] | 4183 | void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4184 | { |
Linus Torvalds | cc314ee | 2005-08-19 18:02:56 -0700 | [diff] [blame] | 4185 | struct page *page = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4186 | nd_set_link(nd, page_getlink(dentry, &page)); |
Linus Torvalds | cc314ee | 2005-08-19 18:02:56 -0700 | [diff] [blame] | 4187 | return page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4188 | } |
| 4189 | |
Linus Torvalds | cc314ee | 2005-08-19 18:02:56 -0700 | [diff] [blame] | 4190 | void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4191 | { |
Linus Torvalds | cc314ee | 2005-08-19 18:02:56 -0700 | [diff] [blame] | 4192 | struct page *page = cookie; |
| 4193 | |
| 4194 | if (page) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4195 | kunmap(page); |
| 4196 | page_cache_release(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4197 | } |
| 4198 | } |
| 4199 | |
Nick Piggin | 54566b2 | 2009-01-04 12:00:53 -0800 | [diff] [blame] | 4200 | /* |
| 4201 | * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS |
| 4202 | */ |
| 4203 | int __page_symlink(struct inode *inode, const char *symname, int len, int nofs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4204 | { |
| 4205 | struct address_space *mapping = inode->i_mapping; |
Kirill Korotaev | 0adb25d | 2006-03-11 03:27:13 -0800 | [diff] [blame] | 4206 | struct page *page; |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 4207 | void *fsdata; |
Dmitriy Monakhov | beb497a | 2007-02-16 01:27:18 -0800 | [diff] [blame] | 4208 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4209 | char *kaddr; |
Nick Piggin | 54566b2 | 2009-01-04 12:00:53 -0800 | [diff] [blame] | 4210 | unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE; |
| 4211 | if (nofs) |
| 4212 | flags |= AOP_FLAG_NOFS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4213 | |
NeilBrown | 7e53cac | 2006-03-25 03:07:57 -0800 | [diff] [blame] | 4214 | retry: |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 4215 | err = pagecache_write_begin(NULL, mapping, 0, len-1, |
Nick Piggin | 54566b2 | 2009-01-04 12:00:53 -0800 | [diff] [blame] | 4216 | flags, &page, &fsdata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4217 | if (err) |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 4218 | goto fail; |
| 4219 | |
Cong Wang | e8e3c3d | 2011-11-25 23:14:27 +0800 | [diff] [blame] | 4220 | kaddr = kmap_atomic(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4221 | memcpy(kaddr, symname, len-1); |
Cong Wang | e8e3c3d | 2011-11-25 23:14:27 +0800 | [diff] [blame] | 4222 | kunmap_atomic(kaddr); |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 4223 | |
| 4224 | err = pagecache_write_end(NULL, mapping, 0, len-1, len-1, |
| 4225 | page, fsdata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4226 | if (err < 0) |
| 4227 | goto fail; |
Nick Piggin | afddba4 | 2007-10-16 01:25:01 -0700 | [diff] [blame] | 4228 | if (err < len-1) |
| 4229 | goto retry; |
| 4230 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4231 | mark_inode_dirty(inode); |
| 4232 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4233 | fail: |
| 4234 | return err; |
| 4235 | } |
| 4236 | |
Kirill Korotaev | 0adb25d | 2006-03-11 03:27:13 -0800 | [diff] [blame] | 4237 | int page_symlink(struct inode *inode, const char *symname, int len) |
| 4238 | { |
| 4239 | return __page_symlink(inode, symname, len, |
Nick Piggin | 54566b2 | 2009-01-04 12:00:53 -0800 | [diff] [blame] | 4240 | !(mapping_gfp_mask(inode->i_mapping) & __GFP_FS)); |
Kirill Korotaev | 0adb25d | 2006-03-11 03:27:13 -0800 | [diff] [blame] | 4241 | } |
| 4242 | |
Arjan van de Ven | 92e1d5b | 2007-02-12 00:55:39 -0800 | [diff] [blame] | 4243 | const struct inode_operations page_symlink_inode_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4244 | .readlink = generic_readlink, |
| 4245 | .follow_link = page_follow_link_light, |
| 4246 | .put_link = page_put_link, |
| 4247 | }; |
| 4248 | |
Al Viro | 2d8f303 | 2008-07-22 09:59:21 -0400 | [diff] [blame] | 4249 | EXPORT_SYMBOL(user_path_at); |
David Howells | cc53ce5 | 2011-01-14 18:45:26 +0000 | [diff] [blame] | 4250 | EXPORT_SYMBOL(follow_down_one); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4251 | EXPORT_SYMBOL(follow_down); |
| 4252 | EXPORT_SYMBOL(follow_up); |
Al Viro | 66cc1a2 | 2012-08-26 12:55:54 -0400 | [diff] [blame] | 4253 | EXPORT_SYMBOL(get_write_access); /* nfsd */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4254 | EXPORT_SYMBOL(lock_rename); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4255 | EXPORT_SYMBOL(page_follow_link_light); |
| 4256 | EXPORT_SYMBOL(page_put_link); |
| 4257 | EXPORT_SYMBOL(page_readlink); |
Kirill Korotaev | 0adb25d | 2006-03-11 03:27:13 -0800 | [diff] [blame] | 4258 | EXPORT_SYMBOL(__page_symlink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4259 | EXPORT_SYMBOL(page_symlink); |
| 4260 | EXPORT_SYMBOL(page_symlink_inode_operations); |
Al Viro | d181146 | 2008-08-02 00:49:18 -0400 | [diff] [blame] | 4261 | EXPORT_SYMBOL(kern_path); |
Josef 'Jeff' Sipek | 16f1820 | 2007-07-19 01:48:18 -0700 | [diff] [blame] | 4262 | EXPORT_SYMBOL(vfs_path_lookup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4263 | EXPORT_SYMBOL(unlock_rename); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4264 | EXPORT_SYMBOL(vfs_follow_link); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4265 | EXPORT_SYMBOL(generic_permission); |
| 4266 | EXPORT_SYMBOL(vfs_readlink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4267 | EXPORT_SYMBOL(dentry_unhash); |
| 4268 | EXPORT_SYMBOL(generic_readlink); |