Amir Goldstein | 8ed5eec | 2017-07-12 14:17:16 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Overlayfs NFS export support. |
| 3 | * |
| 4 | * Amir Goldstein <amir73il@gmail.com> |
| 5 | * |
| 6 | * Copyright (C) 2017-2018 CTERA Networks. All Rights Reserved. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License version 2 as published by |
| 10 | * the Free Software Foundation. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/fs.h> |
| 14 | #include <linux/cred.h> |
| 15 | #include <linux/mount.h> |
| 16 | #include <linux/namei.h> |
| 17 | #include <linux/xattr.h> |
| 18 | #include <linux/exportfs.h> |
| 19 | #include <linux/ratelimit.h> |
| 20 | #include "overlayfs.h" |
| 21 | |
Amir Goldstein | b305e84 | 2018-01-18 13:14:55 +0200 | [diff] [blame] | 22 | /* |
| 23 | * We only need to encode origin if there is a chance that the same object was |
| 24 | * encoded pre copy up and then we need to stay consistent with the same |
| 25 | * encoding also after copy up. If non-pure upper is not indexed, then it was |
| 26 | * copied up before NFS export was enabled. In that case we don't need to worry |
| 27 | * about staying consistent with pre copy up encoding and we encode an upper |
| 28 | * file handle. Overlay root dentry is a private case of non-indexed upper. |
| 29 | * |
| 30 | * The following table summarizes the different file handle encodings used for |
| 31 | * different overlay object types: |
| 32 | * |
| 33 | * Object type | Encoding |
| 34 | * -------------------------------- |
| 35 | * Pure upper | U |
| 36 | * Non-indexed upper | U |
Amir Goldstein | 05e1f11 | 2018-01-18 13:15:26 +0200 | [diff] [blame^] | 37 | * Indexed upper | L (*) |
| 38 | * Non-upper | L (*) |
Amir Goldstein | b305e84 | 2018-01-18 13:14:55 +0200 | [diff] [blame] | 39 | * |
| 40 | * U = upper file handle |
| 41 | * L = lower file handle |
Amir Goldstein | 05e1f11 | 2018-01-18 13:15:26 +0200 | [diff] [blame^] | 42 | * |
| 43 | * (*) Connecting an overlay dir from real lower dentry is not always |
| 44 | * possible when there are redirects in lower layers. To mitigate this case, |
| 45 | * we copy up the lower dir first and then encode an upper dir file handle. |
Amir Goldstein | b305e84 | 2018-01-18 13:14:55 +0200 | [diff] [blame] | 46 | */ |
| 47 | static bool ovl_should_encode_origin(struct dentry *dentry) |
| 48 | { |
Amir Goldstein | 05e1f11 | 2018-01-18 13:15:26 +0200 | [diff] [blame^] | 49 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; |
| 50 | |
Amir Goldstein | b305e84 | 2018-01-18 13:14:55 +0200 | [diff] [blame] | 51 | if (!ovl_dentry_lower(dentry)) |
| 52 | return false; |
| 53 | |
Amir Goldstein | 05e1f11 | 2018-01-18 13:15:26 +0200 | [diff] [blame^] | 54 | /* |
| 55 | * Decoding a merge dir, whose origin's parent is under a redirected |
| 56 | * lower dir is not always possible. As a simple aproximation, we do |
| 57 | * not encode lower dir file handles when overlay has multiple lower |
| 58 | * layers and origin is below the topmost lower layer. |
| 59 | * |
| 60 | * TODO: copy up only the parent that is under redirected lower. |
| 61 | */ |
| 62 | if (d_is_dir(dentry) && ofs->upper_mnt && |
| 63 | OVL_E(dentry)->lowerstack[0].layer->idx > 1) |
| 64 | return false; |
| 65 | |
Amir Goldstein | b305e84 | 2018-01-18 13:14:55 +0200 | [diff] [blame] | 66 | /* Decoding a non-indexed upper from origin is not implemented */ |
| 67 | if (ovl_dentry_upper(dentry) && |
| 68 | !ovl_test_flag(OVL_INDEX, d_inode(dentry))) |
| 69 | return false; |
| 70 | |
| 71 | return true; |
| 72 | } |
| 73 | |
Amir Goldstein | 05e1f11 | 2018-01-18 13:15:26 +0200 | [diff] [blame^] | 74 | static int ovl_encode_maybe_copy_up(struct dentry *dentry) |
| 75 | { |
| 76 | int err; |
| 77 | |
| 78 | if (ovl_dentry_upper(dentry)) |
| 79 | return 0; |
| 80 | |
| 81 | err = ovl_want_write(dentry); |
| 82 | if (err) |
| 83 | return err; |
| 84 | |
| 85 | err = ovl_copy_up(dentry); |
| 86 | |
| 87 | ovl_drop_write(dentry); |
| 88 | return err; |
| 89 | } |
| 90 | |
Amir Goldstein | 8ed5eec | 2017-07-12 14:17:16 +0300 | [diff] [blame] | 91 | static int ovl_d_to_fh(struct dentry *dentry, char *buf, int buflen) |
| 92 | { |
Amir Goldstein | 05e1f11 | 2018-01-18 13:15:26 +0200 | [diff] [blame^] | 93 | struct dentry *upper; |
Amir Goldstein | 8ed5eec | 2017-07-12 14:17:16 +0300 | [diff] [blame] | 94 | struct dentry *origin = ovl_dentry_lower(dentry); |
| 95 | struct ovl_fh *fh = NULL; |
| 96 | int err; |
| 97 | |
Amir Goldstein | 05e1f11 | 2018-01-18 13:15:26 +0200 | [diff] [blame^] | 98 | /* |
| 99 | * If we should not encode a lower dir file handle, copy up and encode |
| 100 | * an upper dir file handle. |
| 101 | */ |
| 102 | if (!ovl_should_encode_origin(dentry)) { |
| 103 | err = ovl_encode_maybe_copy_up(dentry); |
| 104 | if (err) |
| 105 | goto fail; |
Amir Goldstein | 8ed5eec | 2017-07-12 14:17:16 +0300 | [diff] [blame] | 106 | |
Amir Goldstein | 05e1f11 | 2018-01-18 13:15:26 +0200 | [diff] [blame^] | 107 | origin = NULL; |
| 108 | } |
| 109 | |
| 110 | upper = ovl_dentry_upper(dentry); |
Amir Goldstein | 8ed5eec | 2017-07-12 14:17:16 +0300 | [diff] [blame] | 111 | err = -EACCES; |
| 112 | if (!upper || origin) |
| 113 | goto fail; |
| 114 | |
| 115 | /* TODO: encode non pure-upper by origin */ |
| 116 | fh = ovl_encode_fh(upper, true); |
| 117 | |
| 118 | err = -EOVERFLOW; |
| 119 | if (fh->len > buflen) |
| 120 | goto fail; |
| 121 | |
| 122 | memcpy(buf, (char *)fh, fh->len); |
| 123 | err = fh->len; |
| 124 | |
| 125 | out: |
| 126 | kfree(fh); |
| 127 | return err; |
| 128 | |
| 129 | fail: |
| 130 | pr_warn_ratelimited("overlayfs: failed to encode file handle (%pd2, err=%i, buflen=%d, len=%d, type=%d)\n", |
| 131 | dentry, err, buflen, fh ? (int)fh->len : 0, |
| 132 | fh ? fh->type : 0); |
| 133 | goto out; |
| 134 | } |
| 135 | |
| 136 | static int ovl_dentry_to_fh(struct dentry *dentry, u32 *fid, int *max_len) |
| 137 | { |
| 138 | int res, len = *max_len << 2; |
| 139 | |
| 140 | res = ovl_d_to_fh(dentry, (char *)fid, len); |
| 141 | if (res <= 0) |
| 142 | return FILEID_INVALID; |
| 143 | |
| 144 | len = res; |
| 145 | |
| 146 | /* Round up to dwords */ |
| 147 | *max_len = (len + 3) >> 2; |
| 148 | return OVL_FILEID; |
| 149 | } |
| 150 | |
| 151 | static int ovl_encode_inode_fh(struct inode *inode, u32 *fid, int *max_len, |
| 152 | struct inode *parent) |
| 153 | { |
| 154 | struct dentry *dentry; |
| 155 | int type; |
| 156 | |
| 157 | /* TODO: encode connectable file handles */ |
| 158 | if (parent) |
| 159 | return FILEID_INVALID; |
| 160 | |
| 161 | dentry = d_find_any_alias(inode); |
| 162 | if (WARN_ON(!dentry)) |
| 163 | return FILEID_INVALID; |
| 164 | |
| 165 | type = ovl_dentry_to_fh(dentry, fid, max_len); |
| 166 | |
| 167 | dput(dentry); |
| 168 | return type; |
| 169 | } |
| 170 | |
Amir Goldstein | 8556a42 | 2018-01-19 01:03:23 +0200 | [diff] [blame] | 171 | /* |
| 172 | * Find or instantiate an overlay dentry from real dentries. |
| 173 | */ |
| 174 | static struct dentry *ovl_obtain_alias(struct super_block *sb, |
| 175 | struct dentry *upper, |
| 176 | struct ovl_path *lowerpath) |
| 177 | { |
| 178 | struct inode *inode; |
| 179 | struct dentry *dentry; |
| 180 | struct ovl_entry *oe; |
| 181 | void *fsdata = &oe; |
| 182 | |
| 183 | /* TODO: obtain non pure-upper */ |
| 184 | if (lowerpath) |
| 185 | return ERR_PTR(-EIO); |
| 186 | |
| 187 | inode = ovl_get_inode(sb, dget(upper), NULL, NULL, 0); |
| 188 | if (IS_ERR(inode)) { |
| 189 | dput(upper); |
| 190 | return ERR_CAST(inode); |
| 191 | } |
| 192 | |
| 193 | dentry = d_find_any_alias(inode); |
| 194 | if (!dentry) { |
| 195 | dentry = d_alloc_anon(inode->i_sb); |
| 196 | if (!dentry) |
| 197 | goto nomem; |
| 198 | oe = ovl_alloc_entry(0); |
| 199 | if (!oe) |
| 200 | goto nomem; |
| 201 | |
| 202 | dentry->d_fsdata = oe; |
| 203 | ovl_dentry_set_upper_alias(dentry); |
| 204 | } |
| 205 | |
| 206 | return d_instantiate_anon(dentry, inode); |
| 207 | |
| 208 | nomem: |
| 209 | iput(inode); |
| 210 | dput(dentry); |
| 211 | return ERR_PTR(-ENOMEM); |
| 212 | } |
| 213 | |
Amir Goldstein | 3985b70 | 2017-12-28 18:36:16 +0200 | [diff] [blame] | 214 | /* |
| 215 | * Lookup a child overlay dentry to get a connected overlay dentry whose real |
| 216 | * dentry is @real. If @real is on upper layer, we lookup a child overlay |
| 217 | * dentry with the same name as the real dentry. Otherwise, we need to consult |
| 218 | * index for lookup. |
| 219 | */ |
| 220 | static struct dentry *ovl_lookup_real_one(struct dentry *connected, |
| 221 | struct dentry *real, |
| 222 | struct ovl_layer *layer) |
| 223 | { |
| 224 | struct inode *dir = d_inode(connected); |
| 225 | struct dentry *this, *parent = NULL; |
| 226 | struct name_snapshot name; |
| 227 | int err; |
| 228 | |
| 229 | /* TODO: lookup by lower real dentry */ |
| 230 | if (layer->idx) |
| 231 | return ERR_PTR(-EACCES); |
| 232 | |
| 233 | /* |
| 234 | * Lookup child overlay dentry by real name. The dir mutex protects us |
| 235 | * from racing with overlay rename. If the overlay dentry that is above |
| 236 | * real has already been moved to a parent that is not under the |
| 237 | * connected overlay dir, we return -ECHILD and restart the lookup of |
| 238 | * connected real path from the top. |
| 239 | */ |
| 240 | inode_lock_nested(dir, I_MUTEX_PARENT); |
| 241 | err = -ECHILD; |
| 242 | parent = dget_parent(real); |
| 243 | if (ovl_dentry_upper(connected) != parent) |
| 244 | goto fail; |
| 245 | |
| 246 | /* |
| 247 | * We also need to take a snapshot of real dentry name to protect us |
| 248 | * from racing with underlying layer rename. In this case, we don't |
| 249 | * care about returning ESTALE, only from dereferencing a free name |
| 250 | * pointer because we hold no lock on the real dentry. |
| 251 | */ |
| 252 | take_dentry_name_snapshot(&name, real); |
| 253 | this = lookup_one_len(name.name, connected, strlen(name.name)); |
| 254 | err = PTR_ERR(this); |
| 255 | if (IS_ERR(this)) { |
| 256 | goto fail; |
| 257 | } else if (!this || !this->d_inode) { |
| 258 | dput(this); |
| 259 | err = -ENOENT; |
| 260 | goto fail; |
| 261 | } else if (ovl_dentry_upper(this) != real) { |
| 262 | dput(this); |
| 263 | err = -ESTALE; |
| 264 | goto fail; |
| 265 | } |
| 266 | |
| 267 | out: |
| 268 | release_dentry_name_snapshot(&name); |
| 269 | dput(parent); |
| 270 | inode_unlock(dir); |
| 271 | return this; |
| 272 | |
| 273 | fail: |
| 274 | pr_warn_ratelimited("overlayfs: failed to lookup one by real (%pd2, layer=%d, connected=%pd2, err=%i)\n", |
| 275 | real, layer->idx, connected, err); |
| 276 | this = ERR_PTR(err); |
| 277 | goto out; |
| 278 | } |
| 279 | |
| 280 | /* |
| 281 | * Lookup a connected overlay dentry whose real dentry is @real. |
| 282 | * If @real is on upper layer, we lookup a child overlay dentry with the same |
| 283 | * path the real dentry. Otherwise, we need to consult index for lookup. |
| 284 | */ |
| 285 | static struct dentry *ovl_lookup_real(struct super_block *sb, |
| 286 | struct dentry *real, |
| 287 | struct ovl_layer *layer) |
| 288 | { |
| 289 | struct dentry *connected; |
| 290 | int err = 0; |
| 291 | |
| 292 | /* TODO: use index when looking up by lower real dentry */ |
| 293 | if (layer->idx) |
| 294 | return ERR_PTR(-EACCES); |
| 295 | |
| 296 | connected = dget(sb->s_root); |
| 297 | while (!err) { |
| 298 | struct dentry *next, *this; |
| 299 | struct dentry *parent = NULL; |
| 300 | struct dentry *real_connected = ovl_dentry_upper(connected); |
| 301 | |
| 302 | if (real_connected == real) |
| 303 | break; |
| 304 | |
| 305 | /* Find the topmost dentry not yet connected */ |
| 306 | next = dget(real); |
| 307 | for (;;) { |
| 308 | parent = dget_parent(next); |
| 309 | |
| 310 | if (parent == real_connected) |
| 311 | break; |
| 312 | |
| 313 | /* |
| 314 | * If real has been moved out of 'real_connected', |
| 315 | * we will not find 'real_connected' and hit the layer |
| 316 | * root. In that case, we need to restart connecting. |
| 317 | * This game can go on forever in the worst case. We |
| 318 | * may want to consider taking s_vfs_rename_mutex if |
| 319 | * this happens more than once. |
| 320 | */ |
| 321 | if (parent == layer->mnt->mnt_root) { |
| 322 | dput(connected); |
| 323 | connected = dget(sb->s_root); |
| 324 | break; |
| 325 | } |
| 326 | |
| 327 | /* |
| 328 | * If real file has been moved out of the layer root |
| 329 | * directory, we will eventully hit the real fs root. |
| 330 | * This cannot happen by legit overlay rename, so we |
| 331 | * return error in that case. |
| 332 | */ |
| 333 | if (parent == next) { |
| 334 | err = -EXDEV; |
| 335 | break; |
| 336 | } |
| 337 | |
| 338 | dput(next); |
| 339 | next = parent; |
| 340 | } |
| 341 | |
| 342 | if (!err) { |
| 343 | this = ovl_lookup_real_one(connected, next, layer); |
| 344 | if (IS_ERR(this)) |
| 345 | err = PTR_ERR(this); |
| 346 | |
| 347 | /* |
| 348 | * Lookup of child in overlay can fail when racing with |
| 349 | * overlay rename of child away from 'connected' parent. |
| 350 | * In this case, we need to restart the lookup from the |
| 351 | * top, because we cannot trust that 'real_connected' is |
| 352 | * still an ancestor of 'real'. |
| 353 | */ |
| 354 | if (err == -ECHILD) { |
| 355 | this = dget(sb->s_root); |
| 356 | err = 0; |
| 357 | } |
| 358 | if (!err) { |
| 359 | dput(connected); |
| 360 | connected = this; |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | dput(parent); |
| 365 | dput(next); |
| 366 | } |
| 367 | |
| 368 | if (err) |
| 369 | goto fail; |
| 370 | |
| 371 | return connected; |
| 372 | |
| 373 | fail: |
| 374 | pr_warn_ratelimited("overlayfs: failed to lookup by real (%pd2, layer=%d, connected=%pd2, err=%i)\n", |
| 375 | real, layer->idx, connected, err); |
| 376 | dput(connected); |
| 377 | return ERR_PTR(err); |
| 378 | } |
| 379 | |
| 380 | /* |
| 381 | * Get an overlay dentry from upper/lower real dentries. |
| 382 | */ |
| 383 | static struct dentry *ovl_get_dentry(struct super_block *sb, |
| 384 | struct dentry *upper, |
| 385 | struct ovl_path *lowerpath) |
| 386 | { |
| 387 | struct ovl_fs *ofs = sb->s_fs_info; |
| 388 | struct ovl_layer upper_layer = { .mnt = ofs->upper_mnt }; |
| 389 | |
| 390 | /* TODO: get non-upper dentry */ |
| 391 | if (!upper) |
| 392 | return ERR_PTR(-EACCES); |
| 393 | |
| 394 | /* |
| 395 | * Obtain a disconnected overlay dentry from a non-dir real upper |
| 396 | * dentry. |
| 397 | */ |
| 398 | if (!d_is_dir(upper)) |
| 399 | return ovl_obtain_alias(sb, upper, NULL); |
| 400 | |
| 401 | /* Removed empty directory? */ |
| 402 | if ((upper->d_flags & DCACHE_DISCONNECTED) || d_unhashed(upper)) |
| 403 | return ERR_PTR(-ENOENT); |
| 404 | |
| 405 | /* |
| 406 | * If real upper dentry is connected and hashed, get a connected |
| 407 | * overlay dentry with the same path as the real upper dentry. |
| 408 | */ |
| 409 | return ovl_lookup_real(sb, upper, &upper_layer); |
| 410 | } |
| 411 | |
Amir Goldstein | 8556a42 | 2018-01-19 01:03:23 +0200 | [diff] [blame] | 412 | static struct dentry *ovl_upper_fh_to_d(struct super_block *sb, |
| 413 | struct ovl_fh *fh) |
| 414 | { |
| 415 | struct ovl_fs *ofs = sb->s_fs_info; |
| 416 | struct dentry *dentry; |
| 417 | struct dentry *upper; |
| 418 | |
| 419 | if (!ofs->upper_mnt) |
| 420 | return ERR_PTR(-EACCES); |
| 421 | |
| 422 | upper = ovl_decode_fh(fh, ofs->upper_mnt); |
| 423 | if (IS_ERR_OR_NULL(upper)) |
| 424 | return upper; |
| 425 | |
Amir Goldstein | 3985b70 | 2017-12-28 18:36:16 +0200 | [diff] [blame] | 426 | dentry = ovl_get_dentry(sb, upper, NULL); |
Amir Goldstein | 8556a42 | 2018-01-19 01:03:23 +0200 | [diff] [blame] | 427 | dput(upper); |
| 428 | |
| 429 | return dentry; |
| 430 | } |
| 431 | |
| 432 | static struct dentry *ovl_fh_to_dentry(struct super_block *sb, struct fid *fid, |
| 433 | int fh_len, int fh_type) |
| 434 | { |
| 435 | struct dentry *dentry = NULL; |
| 436 | struct ovl_fh *fh = (struct ovl_fh *) fid; |
| 437 | int len = fh_len << 2; |
| 438 | unsigned int flags = 0; |
| 439 | int err; |
| 440 | |
| 441 | err = -EINVAL; |
| 442 | if (fh_type != OVL_FILEID) |
| 443 | goto out_err; |
| 444 | |
| 445 | err = ovl_check_fh_len(fh, len); |
| 446 | if (err) |
| 447 | goto out_err; |
| 448 | |
| 449 | /* TODO: decode non-upper */ |
| 450 | flags = fh->flags; |
| 451 | if (flags & OVL_FH_FLAG_PATH_UPPER) |
| 452 | dentry = ovl_upper_fh_to_d(sb, fh); |
| 453 | err = PTR_ERR(dentry); |
| 454 | if (IS_ERR(dentry) && err != -ESTALE) |
| 455 | goto out_err; |
| 456 | |
| 457 | return dentry; |
| 458 | |
| 459 | out_err: |
| 460 | pr_warn_ratelimited("overlayfs: failed to decode file handle (len=%d, type=%d, flags=%x, err=%i)\n", |
| 461 | len, fh_type, flags, err); |
| 462 | return ERR_PTR(err); |
| 463 | } |
| 464 | |
Amir Goldstein | 3985b70 | 2017-12-28 18:36:16 +0200 | [diff] [blame] | 465 | static struct dentry *ovl_fh_to_parent(struct super_block *sb, struct fid *fid, |
| 466 | int fh_len, int fh_type) |
| 467 | { |
| 468 | pr_warn_ratelimited("overlayfs: connectable file handles not supported; use 'no_subtree_check' exportfs option.\n"); |
| 469 | return ERR_PTR(-EACCES); |
| 470 | } |
| 471 | |
| 472 | static int ovl_get_name(struct dentry *parent, char *name, |
| 473 | struct dentry *child) |
| 474 | { |
| 475 | /* |
| 476 | * ovl_fh_to_dentry() returns connected dir overlay dentries and |
| 477 | * ovl_fh_to_parent() is not implemented, so we should not get here. |
| 478 | */ |
| 479 | WARN_ON_ONCE(1); |
| 480 | return -EIO; |
| 481 | } |
| 482 | |
| 483 | static struct dentry *ovl_get_parent(struct dentry *dentry) |
| 484 | { |
| 485 | /* |
| 486 | * ovl_fh_to_dentry() returns connected dir overlay dentries, so we |
| 487 | * should not get here. |
| 488 | */ |
| 489 | WARN_ON_ONCE(1); |
| 490 | return ERR_PTR(-EIO); |
| 491 | } |
| 492 | |
Amir Goldstein | 8ed5eec | 2017-07-12 14:17:16 +0300 | [diff] [blame] | 493 | const struct export_operations ovl_export_operations = { |
| 494 | .encode_fh = ovl_encode_inode_fh, |
Amir Goldstein | 8556a42 | 2018-01-19 01:03:23 +0200 | [diff] [blame] | 495 | .fh_to_dentry = ovl_fh_to_dentry, |
Amir Goldstein | 3985b70 | 2017-12-28 18:36:16 +0200 | [diff] [blame] | 496 | .fh_to_parent = ovl_fh_to_parent, |
| 497 | .get_name = ovl_get_name, |
| 498 | .get_parent = ovl_get_parent, |
Amir Goldstein | 8ed5eec | 2017-07-12 14:17:16 +0300 | [diff] [blame] | 499 | }; |