1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * 4 * Copyright (C) 2011 Novell Inc. 5 */ 6 7 #include <uapi/linux/magic.h> 8 #include <linux/fs.h> 9 #include <linux/namei.h> 10 #include <linux/xattr.h> 11 #include <linux/mount.h> 12 #include <linux/parser.h> 13 #include <linux/module.h> 14 #include <linux/statfs.h> 15 #include <linux/seq_file.h> 16 #include <linux/posix_acl_xattr.h> 17 #include <linux/exportfs.h> 18 #include <linux/file.h> 19 #include <linux/fs_context.h> 20 #include <linux/fs_parser.h> 21 #include "overlayfs.h" 22 #include "params.h" 23 24 MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>"); 25 MODULE_DESCRIPTION("Overlay filesystem"); 26 MODULE_LICENSE("GPL"); 27 28 29 struct ovl_dir_cache; 30 31 static struct dentry *ovl_d_real(struct dentry *dentry, enum d_real_type type) 32 { 33 struct dentry *upper, *lower; 34 int err; 35 36 switch (type) { 37 case D_REAL_DATA: 38 case D_REAL_METADATA: 39 break; 40 default: 41 goto bug; 42 } 43 44 if (!d_is_reg(dentry)) { 45 /* d_real_inode() is only relevant for regular files */ 46 return dentry; 47 } 48 49 upper = ovl_dentry_upper(dentry); 50 if (upper && (type == D_REAL_METADATA || 51 ovl_has_upperdata(d_inode(dentry)))) 52 return upper; 53 54 if (type == D_REAL_METADATA) { 55 lower = ovl_dentry_lower(dentry); 56 goto real_lower; 57 } 58 59 /* 60 * Best effort lazy lookup of lowerdata for D_REAL_DATA case to return 61 * the real lowerdata dentry. The only current caller of d_real() with 62 * D_REAL_DATA is d_real_inode() from trace_uprobe and this caller is 63 * likely going to be followed reading from the file, before placing 64 * uprobes on offset within the file, so lowerdata should be available 65 * when setting the uprobe. 66 */ 67 err = ovl_verify_lowerdata(dentry); 68 if (err) 69 goto bug; 70 lower = ovl_dentry_lowerdata(dentry); 71 if (!lower) 72 goto bug; 73 74 real_lower: 75 /* Handle recursion into stacked lower fs */ 76 return d_real(lower, type); 77 78 bug: 79 WARN(1, "%s(%pd4, %d): real dentry not found\n", __func__, dentry, type); 80 return dentry; 81 } 82 83 static int ovl_revalidate_real(struct dentry *d, unsigned int flags, bool weak) 84 { 85 int ret = 1; 86 87 if (!d) 88 return 1; 89 90 if (weak) { 91 if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE) 92 ret = d->d_op->d_weak_revalidate(d, flags); 93 } else if (d->d_flags & DCACHE_OP_REVALIDATE) { 94 struct dentry *parent; 95 struct inode *dir; 96 struct name_snapshot n; 97 98 if (flags & LOOKUP_RCU) { 99 parent = READ_ONCE(d->d_parent); 100 dir = d_inode_rcu(parent); 101 if (!dir) 102 return -ECHILD; 103 } else { 104 parent = dget_parent(d); 105 dir = d_inode(parent); 106 } 107 take_dentry_name_snapshot(&n, d); 108 ret = d->d_op->d_revalidate(dir, &n.name, d, flags); 109 release_dentry_name_snapshot(&n); 110 if (!(flags & LOOKUP_RCU)) 111 dput(parent); 112 if (!ret) { 113 if (!(flags & LOOKUP_RCU)) 114 d_invalidate(d); 115 ret = -ESTALE; 116 } 117 } 118 return ret; 119 } 120 121 static int ovl_dentry_revalidate_common(struct dentry *dentry, 122 unsigned int flags, bool weak) 123 { 124 struct ovl_entry *oe; 125 struct ovl_path *lowerstack; 126 struct inode *inode = d_inode_rcu(dentry); 127 struct dentry *upper; 128 unsigned int i; 129 int ret = 1; 130 131 /* Careful in RCU mode */ 132 if (!inode) 133 return -ECHILD; 134 135 oe = OVL_I_E(inode); 136 lowerstack = ovl_lowerstack(oe); 137 upper = ovl_i_dentry_upper(inode); 138 if (upper) 139 ret = ovl_revalidate_real(upper, flags, weak); 140 141 for (i = 0; ret > 0 && i < ovl_numlower(oe); i++) 142 ret = ovl_revalidate_real(lowerstack[i].dentry, flags, weak); 143 144 return ret; 145 } 146 147 static int ovl_dentry_revalidate(struct inode *dir, const struct qstr *name, 148 struct dentry *dentry, unsigned int flags) 149 { 150 return ovl_dentry_revalidate_common(dentry, flags, false); 151 } 152 153 static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags) 154 { 155 return ovl_dentry_revalidate_common(dentry, flags, true); 156 } 157 158 static const struct dentry_operations ovl_dentry_operations = { 159 .d_real = ovl_d_real, 160 .d_revalidate = ovl_dentry_revalidate, 161 .d_weak_revalidate = ovl_dentry_weak_revalidate, 162 }; 163 164 #if IS_ENABLED(CONFIG_UNICODE) 165 static const struct dentry_operations ovl_dentry_ci_operations = { 166 .d_real = ovl_d_real, 167 .d_revalidate = ovl_dentry_revalidate, 168 .d_weak_revalidate = ovl_dentry_weak_revalidate, 169 .d_hash = generic_ci_d_hash, 170 .d_compare = generic_ci_d_compare, 171 }; 172 #endif 173 174 static struct kmem_cache *ovl_inode_cachep; 175 176 static struct inode *ovl_alloc_inode(struct super_block *sb) 177 { 178 struct ovl_inode *oi = alloc_inode_sb(sb, ovl_inode_cachep, GFP_KERNEL); 179 180 if (!oi) 181 return NULL; 182 183 oi->cache = NULL; 184 oi->redirect = NULL; 185 oi->version = 0; 186 oi->flags = 0; 187 oi->__upperdentry = NULL; 188 oi->lowerdata_redirect = NULL; 189 oi->oe = NULL; 190 mutex_init(&oi->lock); 191 192 return &oi->vfs_inode; 193 } 194 195 static void ovl_free_inode(struct inode *inode) 196 { 197 struct ovl_inode *oi = OVL_I(inode); 198 199 kfree(oi->redirect); 200 kfree(oi->oe); 201 mutex_destroy(&oi->lock); 202 kmem_cache_free(ovl_inode_cachep, oi); 203 } 204 205 static void ovl_destroy_inode(struct inode *inode) 206 { 207 struct ovl_inode *oi = OVL_I(inode); 208 209 dput(oi->__upperdentry); 210 ovl_stack_put(ovl_lowerstack(oi->oe), ovl_numlower(oi->oe)); 211 if (S_ISDIR(inode->i_mode)) 212 ovl_dir_cache_free(inode); 213 else 214 kfree(oi->lowerdata_redirect); 215 } 216 217 static void ovl_put_super(struct super_block *sb) 218 { 219 struct ovl_fs *ofs = OVL_FS(sb); 220 221 if (ofs) 222 ovl_free_fs(ofs); 223 } 224 225 /* Sync real dirty inodes in upper filesystem (if it exists) */ 226 static int ovl_sync_fs(struct super_block *sb, int wait) 227 { 228 struct ovl_fs *ofs = OVL_FS(sb); 229 struct super_block *upper_sb; 230 int ret; 231 232 ret = ovl_sync_status(ofs); 233 234 if (ret < 0) 235 return -EIO; 236 237 if (!ret) 238 return ret; 239 240 /* 241 * Not called for sync(2) call or an emergency sync (SB_I_SKIP_SYNC). 242 * All the super blocks will be iterated, including upper_sb. 243 * 244 * If this is a syncfs(2) call, then we do need to call 245 * sync_filesystem() on upper_sb, but enough if we do it when being 246 * called with wait == 1. 247 */ 248 if (!wait) 249 return 0; 250 251 upper_sb = ovl_upper_mnt(ofs)->mnt_sb; 252 253 down_read(&upper_sb->s_umount); 254 ret = sync_filesystem(upper_sb); 255 up_read(&upper_sb->s_umount); 256 257 return ret; 258 } 259 260 /** 261 * ovl_statfs 262 * @dentry: The dentry to query 263 * @buf: The struct kstatfs to fill in with stats 264 * 265 * Get the filesystem statistics. As writes always target the upper layer 266 * filesystem pass the statfs to the upper filesystem (if it exists) 267 */ 268 static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf) 269 { 270 struct super_block *sb = dentry->d_sb; 271 struct ovl_fs *ofs = OVL_FS(sb); 272 struct dentry *root_dentry = sb->s_root; 273 struct path path; 274 int err; 275 276 ovl_path_real(root_dentry, &path); 277 278 err = vfs_statfs(&path, buf); 279 if (!err) { 280 buf->f_namelen = ofs->namelen; 281 buf->f_type = OVERLAYFS_SUPER_MAGIC; 282 if (ovl_has_fsid(ofs)) 283 buf->f_fsid = uuid_to_fsid(sb->s_uuid.b); 284 } 285 286 return err; 287 } 288 289 static const struct super_operations ovl_super_operations = { 290 .alloc_inode = ovl_alloc_inode, 291 .free_inode = ovl_free_inode, 292 .destroy_inode = ovl_destroy_inode, 293 .drop_inode = inode_just_drop, 294 .put_super = ovl_put_super, 295 .sync_fs = ovl_sync_fs, 296 .statfs = ovl_statfs, 297 .show_options = ovl_show_options, 298 }; 299 300 #define OVL_WORKDIR_NAME "work" 301 #define OVL_INDEXDIR_NAME "index" 302 303 static struct dentry *ovl_workdir_create(struct ovl_fs *ofs, 304 const char *name, bool persist) 305 { 306 struct inode *dir = ofs->workbasedir->d_inode; 307 struct vfsmount *mnt = ovl_upper_mnt(ofs); 308 struct dentry *work; 309 int err; 310 bool retried = false; 311 312 retry: 313 work = ovl_start_creating_upper(ofs, ofs->workbasedir, &QSTR(name)); 314 315 if (!IS_ERR(work)) { 316 struct iattr attr = { 317 .ia_valid = ATTR_MODE, 318 .ia_mode = S_IFDIR | 0, 319 }; 320 321 if (work->d_inode) { 322 end_creating_keep(work); 323 if (persist) 324 return work; 325 err = -EEXIST; 326 if (retried) 327 goto out_dput; 328 retried = true; 329 err = ovl_workdir_cleanup(ofs, ofs->workbasedir, mnt, work, 0); 330 dput(work); 331 if (err == -EINVAL) 332 return ERR_PTR(err); 333 334 goto retry; 335 } 336 337 work = ovl_do_mkdir(ofs, dir, work, attr.ia_mode); 338 end_creating_keep(work); 339 err = PTR_ERR(work); 340 if (IS_ERR(work)) 341 goto out_err; 342 343 /* Weird filesystem returning with hashed negative (kernfs)? */ 344 err = -EINVAL; 345 if (d_really_is_negative(work)) 346 goto out_dput; 347 348 /* 349 * Try to remove POSIX ACL xattrs from workdir. We are good if: 350 * 351 * a) success (there was a POSIX ACL xattr and was removed) 352 * b) -ENODATA (there was no POSIX ACL xattr) 353 * c) -EOPNOTSUPP (POSIX ACL xattrs are not supported) 354 * 355 * There are various other error values that could effectively 356 * mean that the xattr doesn't exist (e.g. -ERANGE is returned 357 * if the xattr name is too long), but the set of filesystems 358 * allowed as upper are limited to "normal" ones, where checking 359 * for the above two errors is sufficient. 360 */ 361 err = ovl_do_remove_acl(ofs, work, XATTR_NAME_POSIX_ACL_DEFAULT); 362 if (err && err != -ENODATA && err != -EOPNOTSUPP) 363 goto out_dput; 364 365 err = ovl_do_remove_acl(ofs, work, XATTR_NAME_POSIX_ACL_ACCESS); 366 if (err && err != -ENODATA && err != -EOPNOTSUPP) 367 goto out_dput; 368 369 /* Clear any inherited mode bits */ 370 inode_lock(work->d_inode); 371 err = ovl_do_notify_change(ofs, work, &attr); 372 inode_unlock(work->d_inode); 373 if (err) 374 goto out_dput; 375 } else { 376 err = PTR_ERR(work); 377 goto out_err; 378 } 379 return work; 380 381 out_dput: 382 dput(work); 383 out_err: 384 pr_warn("failed to create directory %s/%s (errno: %i); mounting read-only\n", 385 ofs->config.workdir, name, -err); 386 return NULL; 387 } 388 389 static int ovl_check_namelen(const struct path *path, struct ovl_fs *ofs, 390 const char *name) 391 { 392 struct kstatfs statfs; 393 int err = vfs_statfs(path, &statfs); 394 395 if (err) 396 pr_err("statfs failed on '%s'\n", name); 397 else 398 ofs->namelen = max(ofs->namelen, statfs.f_namelen); 399 400 return err; 401 } 402 403 static int ovl_lower_dir(const char *name, const struct path *path, 404 struct ovl_fs *ofs, int *stack_depth) 405 { 406 int fh_type; 407 int err; 408 409 err = ovl_check_namelen(path, ofs, name); 410 if (err) 411 return err; 412 413 *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth); 414 415 /* 416 * The inodes index feature and NFS export need to encode and decode 417 * file handles, so they require that all layers support them. 418 */ 419 fh_type = ovl_can_decode_fh(path->dentry->d_sb); 420 if ((ofs->config.nfs_export || 421 (ofs->config.index && ofs->config.upperdir)) && !fh_type) { 422 ofs->config.index = false; 423 ofs->config.nfs_export = false; 424 pr_warn("fs on '%s' does not support file handles, falling back to index=off,nfs_export=off.\n", 425 name); 426 } 427 ofs->nofh |= !fh_type; 428 /* 429 * Decoding origin file handle is required for persistent st_ino. 430 * Without persistent st_ino, xino=auto falls back to xino=off. 431 */ 432 if (ofs->config.xino == OVL_XINO_AUTO && 433 ofs->config.upperdir && !fh_type) { 434 ofs->config.xino = OVL_XINO_OFF; 435 pr_warn("fs on '%s' does not support file handles, falling back to xino=off.\n", 436 name); 437 } 438 439 /* Check if lower fs has 32bit inode numbers */ 440 if (fh_type != FILEID_INO32_GEN) 441 ofs->xino_mode = -1; 442 443 return 0; 444 } 445 446 /* Workdir should not be subdir of upperdir and vice versa */ 447 static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir) 448 { 449 bool ok = false; 450 451 if (workdir != upperdir) { 452 struct dentry *trap = lock_rename(workdir, upperdir); 453 if (!IS_ERR(trap)) 454 unlock_rename(workdir, upperdir); 455 ok = (trap == NULL); 456 } 457 return ok; 458 } 459 460 static int ovl_setup_trap(struct super_block *sb, struct dentry *dir, 461 struct inode **ptrap, const char *name) 462 { 463 struct inode *trap; 464 int err; 465 466 trap = ovl_get_trap_inode(sb, dir); 467 err = PTR_ERR_OR_ZERO(trap); 468 if (err) { 469 if (err == -ELOOP) 470 pr_err("conflicting %s path\n", name); 471 return err; 472 } 473 474 *ptrap = trap; 475 return 0; 476 } 477 478 /* 479 * Determine how we treat concurrent use of upperdir/workdir based on the 480 * index feature. This is papering over mount leaks of container runtimes, 481 * for example, an old overlay mount is leaked and now its upperdir is 482 * attempted to be used as a lower layer in a new overlay mount. 483 */ 484 static int ovl_report_in_use(struct ovl_fs *ofs, const char *name) 485 { 486 if (ofs->config.index) { 487 pr_err("%s is in-use as upperdir/workdir of another mount, mount with '-o index=off' to override exclusive upperdir protection.\n", 488 name); 489 return -EBUSY; 490 } else { 491 pr_warn("%s is in-use as upperdir/workdir of another mount, accessing files from both mounts will result in undefined behavior.\n", 492 name); 493 return 0; 494 } 495 } 496 497 static int ovl_get_upper(struct super_block *sb, struct ovl_fs *ofs, 498 struct ovl_layer *upper_layer, 499 const struct path *upperpath) 500 { 501 struct vfsmount *upper_mnt; 502 int err; 503 504 /* Upperdir path should not be r/o */ 505 if (__mnt_is_readonly(upperpath->mnt)) { 506 pr_err("upper fs is r/o, try multi-lower layers mount\n"); 507 err = -EINVAL; 508 goto out; 509 } 510 511 err = ovl_check_namelen(upperpath, ofs, ofs->config.upperdir); 512 if (err) 513 goto out; 514 515 err = ovl_setup_trap(sb, upperpath->dentry, &upper_layer->trap, 516 "upperdir"); 517 if (err) 518 goto out; 519 520 upper_mnt = clone_private_mount(upperpath); 521 err = PTR_ERR(upper_mnt); 522 if (IS_ERR(upper_mnt)) { 523 pr_err("failed to clone upperpath\n"); 524 goto out; 525 } 526 527 /* Don't inherit atime flags */ 528 upper_mnt->mnt_flags &= ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME); 529 upper_layer->mnt = upper_mnt; 530 upper_layer->idx = 0; 531 upper_layer->fsid = 0; 532 533 /* 534 * Inherit SB_NOSEC flag from upperdir. 535 * 536 * This optimization changes behavior when a security related attribute 537 * (suid/sgid/security.*) is changed on an underlying layer. This is 538 * okay because we don't yet have guarantees in that case, but it will 539 * need careful treatment once we want to honour changes to underlying 540 * filesystems. 541 */ 542 if (upper_mnt->mnt_sb->s_flags & SB_NOSEC) 543 sb->s_flags |= SB_NOSEC; 544 545 if (ovl_inuse_trylock(ovl_upper_mnt(ofs)->mnt_root)) { 546 ofs->upperdir_locked = true; 547 } else { 548 err = ovl_report_in_use(ofs, "upperdir"); 549 if (err) 550 goto out; 551 } 552 553 err = 0; 554 out: 555 return err; 556 } 557 558 /* 559 * Returns 1 if RENAME_WHITEOUT is supported, 0 if not supported and 560 * negative values if error is encountered. 561 */ 562 static int ovl_check_rename_whiteout(struct ovl_fs *ofs) 563 { 564 struct dentry *workdir = ofs->workdir; 565 struct dentry *temp; 566 struct dentry *whiteout; 567 struct name_snapshot name; 568 struct renamedata rd = {}; 569 char name2[OVL_TEMPNAME_SIZE]; 570 int err; 571 572 temp = ovl_create_temp(ofs, workdir, OVL_CATTR(S_IFREG | 0)); 573 err = PTR_ERR(temp); 574 if (IS_ERR(temp)) 575 return err; 576 577 rd.mnt_idmap = ovl_upper_mnt_idmap(ofs); 578 rd.old_parent = workdir; 579 rd.new_parent = workdir; 580 rd.flags = RENAME_WHITEOUT; 581 ovl_tempname(name2); 582 err = start_renaming_dentry(&rd, 0, temp, &QSTR(name2)); 583 if (err) { 584 dput(temp); 585 return err; 586 } 587 588 /* Name is inline and stable - using snapshot as a copy helper */ 589 take_dentry_name_snapshot(&name, temp); 590 err = ovl_do_rename_rd(&rd); 591 end_renaming(&rd); 592 if (err) { 593 if (err == -EINVAL) 594 err = 0; 595 goto cleanup_temp; 596 } 597 598 whiteout = ovl_lookup_upper_unlocked(ofs, name.name.name, 599 workdir, name.name.len); 600 err = PTR_ERR(whiteout); 601 if (IS_ERR(whiteout)) 602 goto cleanup_temp; 603 604 err = ovl_upper_is_whiteout(ofs, whiteout); 605 606 /* Best effort cleanup of whiteout and temp file */ 607 if (err) 608 ovl_cleanup(ofs, workdir, whiteout); 609 dput(whiteout); 610 611 cleanup_temp: 612 ovl_cleanup(ofs, workdir, temp); 613 release_dentry_name_snapshot(&name); 614 dput(temp); 615 616 return err; 617 } 618 619 static struct dentry *ovl_lookup_or_create(struct ovl_fs *ofs, 620 struct dentry *parent, 621 const char *name, umode_t mode) 622 { 623 struct dentry *child; 624 625 child = ovl_start_creating_upper(ofs, parent, &QSTR(name)); 626 if (!IS_ERR(child)) { 627 if (!child->d_inode) 628 child = ovl_create_real(ofs, parent, child, 629 OVL_CATTR(mode)); 630 end_creating_keep(child); 631 } 632 dput(parent); 633 634 return child; 635 } 636 637 /* 638 * Creates $workdir/work/incompat/volatile/dirty file if it is not already 639 * present. 640 */ 641 static int ovl_create_volatile_dirty(struct ovl_fs *ofs) 642 { 643 unsigned int ctr; 644 struct dentry *d = dget(ofs->workbasedir); 645 static const char *const volatile_path[] = { 646 OVL_WORKDIR_NAME, "incompat", "volatile", "dirty" 647 }; 648 const char *const *name = volatile_path; 649 650 for (ctr = ARRAY_SIZE(volatile_path); ctr; ctr--, name++) { 651 d = ovl_lookup_or_create(ofs, d, *name, ctr > 1 ? S_IFDIR : S_IFREG); 652 if (IS_ERR(d)) 653 return PTR_ERR(d); 654 } 655 dput(d); 656 return 0; 657 } 658 659 static int ovl_make_workdir(struct super_block *sb, struct ovl_fs *ofs, 660 const struct path *workpath) 661 { 662 struct vfsmount *mnt = ovl_upper_mnt(ofs); 663 struct dentry *workdir; 664 struct file *tmpfile; 665 bool rename_whiteout; 666 bool d_type; 667 int fh_type; 668 int err; 669 670 err = mnt_want_write(mnt); 671 if (err) 672 return err; 673 674 workdir = ovl_workdir_create(ofs, OVL_WORKDIR_NAME, false); 675 err = PTR_ERR(workdir); 676 if (IS_ERR_OR_NULL(workdir)) 677 goto out; 678 679 ofs->workdir = workdir; 680 681 err = ovl_setup_trap(sb, ofs->workdir, &ofs->workdir_trap, "workdir"); 682 if (err) 683 goto out; 684 685 /* 686 * Upper should support d_type, else whiteouts are visible. Given 687 * workdir and upper are on same fs, we can do iterate_dir() on 688 * workdir. This check requires successful creation of workdir in 689 * previous step. 690 */ 691 err = ovl_check_d_type_supported(workpath); 692 if (err < 0) 693 goto out; 694 695 d_type = err; 696 if (!d_type) 697 pr_warn("upper fs needs to support d_type.\n"); 698 699 /* Check if upper/work fs supports O_TMPFILE */ 700 tmpfile = ovl_do_tmpfile(ofs, ofs->workdir, S_IFREG | 0); 701 ofs->tmpfile = !IS_ERR(tmpfile); 702 if (ofs->tmpfile) 703 fput(tmpfile); 704 else 705 pr_warn("upper fs does not support tmpfile.\n"); 706 707 708 /* Check if upper/work fs supports RENAME_WHITEOUT */ 709 err = ovl_check_rename_whiteout(ofs); 710 if (err < 0) 711 goto out; 712 713 rename_whiteout = err; 714 if (!rename_whiteout) 715 pr_warn("upper fs does not support RENAME_WHITEOUT.\n"); 716 717 /* 718 * Check if upper/work fs supports (trusted|user).overlay.* xattr 719 */ 720 err = ovl_setxattr(ofs, ofs->workdir, OVL_XATTR_OPAQUE, "0", 1); 721 if (err) { 722 pr_warn("failed to set xattr on upper\n"); 723 ofs->noxattr = true; 724 if (ovl_redirect_follow(ofs)) { 725 ofs->config.redirect_mode = OVL_REDIRECT_NOFOLLOW; 726 pr_warn("...falling back to redirect_dir=nofollow.\n"); 727 } 728 if (ofs->config.metacopy) { 729 ofs->config.metacopy = false; 730 pr_warn("...falling back to metacopy=off.\n"); 731 } 732 if (ofs->config.index) { 733 ofs->config.index = false; 734 pr_warn("...falling back to index=off.\n"); 735 } 736 if (ovl_has_fsid(ofs)) { 737 ofs->config.uuid = OVL_UUID_NULL; 738 pr_warn("...falling back to uuid=null.\n"); 739 } 740 /* 741 * xattr support is required for persistent st_ino. 742 * Without persistent st_ino, xino=auto falls back to xino=off. 743 */ 744 if (ofs->config.xino == OVL_XINO_AUTO) { 745 ofs->config.xino = OVL_XINO_OFF; 746 pr_warn("...falling back to xino=off.\n"); 747 } 748 if (err == -EPERM && !ofs->config.userxattr) 749 pr_info("try mounting with 'userxattr' option\n"); 750 err = 0; 751 } else { 752 ovl_removexattr(ofs, ofs->workdir, OVL_XATTR_OPAQUE); 753 } 754 755 /* 756 * We allowed sub-optimal upper fs configuration and don't want to break 757 * users over kernel upgrade, but we never allowed remote upper fs, so 758 * we can enforce strict requirements for remote upper fs. 759 */ 760 if (ovl_dentry_remote(ofs->workdir) && 761 (!d_type || !rename_whiteout || ofs->noxattr)) { 762 pr_err("upper fs missing required features.\n"); 763 err = -EINVAL; 764 goto out; 765 } 766 767 /* 768 * For volatile mount, create a incompat/volatile/dirty file to keep 769 * track of it. 770 */ 771 if (ofs->config.ovl_volatile) { 772 err = ovl_create_volatile_dirty(ofs); 773 if (err < 0) { 774 pr_err("Failed to create volatile/dirty file.\n"); 775 goto out; 776 } 777 } 778 779 /* Check if upper/work fs supports file handles */ 780 fh_type = ovl_can_decode_fh(ofs->workdir->d_sb); 781 if (ofs->config.index && !fh_type) { 782 ofs->config.index = false; 783 pr_warn("upper fs does not support file handles, falling back to index=off.\n"); 784 } 785 ofs->nofh |= !fh_type; 786 787 /* Check if upper fs has 32bit inode numbers */ 788 if (fh_type != FILEID_INO32_GEN) 789 ofs->xino_mode = -1; 790 791 /* NFS export of r/w mount depends on index */ 792 if (ofs->config.nfs_export && !ofs->config.index) { 793 pr_warn("NFS export requires \"index=on\", falling back to nfs_export=off.\n"); 794 ofs->config.nfs_export = false; 795 } 796 out: 797 mnt_drop_write(mnt); 798 return err; 799 } 800 801 static int ovl_get_workdir(struct super_block *sb, struct ovl_fs *ofs, 802 const struct path *upperpath, 803 const struct path *workpath) 804 { 805 int err; 806 807 err = -EINVAL; 808 if (upperpath->mnt != workpath->mnt) { 809 pr_err("workdir and upperdir must reside under the same mount\n"); 810 return err; 811 } 812 if (!ovl_workdir_ok(workpath->dentry, upperpath->dentry)) { 813 pr_err("workdir and upperdir must be separate subtrees\n"); 814 return err; 815 } 816 817 ofs->workbasedir = dget(workpath->dentry); 818 819 if (ovl_inuse_trylock(ofs->workbasedir)) { 820 ofs->workdir_locked = true; 821 } else { 822 err = ovl_report_in_use(ofs, "workdir"); 823 if (err) 824 return err; 825 } 826 827 err = ovl_setup_trap(sb, ofs->workbasedir, &ofs->workbasedir_trap, 828 "workdir"); 829 if (err) 830 return err; 831 832 return ovl_make_workdir(sb, ofs, workpath); 833 } 834 835 static int ovl_get_indexdir(struct super_block *sb, struct ovl_fs *ofs, 836 struct ovl_entry *oe, const struct path *upperpath) 837 { 838 struct vfsmount *mnt = ovl_upper_mnt(ofs); 839 struct dentry *indexdir; 840 struct dentry *origin = ovl_lowerstack(oe)->dentry; 841 const struct ovl_fh *fh; 842 int err; 843 844 fh = ovl_get_origin_fh(ofs, origin); 845 if (IS_ERR(fh)) 846 return PTR_ERR(fh); 847 848 err = mnt_want_write(mnt); 849 if (err) 850 goto out_free_fh; 851 852 /* Verify lower root is upper root origin */ 853 err = ovl_verify_origin_fh(ofs, upperpath->dentry, fh, true); 854 if (err) { 855 pr_err("failed to verify upper root origin\n"); 856 goto out; 857 } 858 859 /* index dir will act also as workdir */ 860 iput(ofs->workdir_trap); 861 ofs->workdir_trap = NULL; 862 dput(ofs->workdir); 863 ofs->workdir = NULL; 864 indexdir = ovl_workdir_create(ofs, OVL_INDEXDIR_NAME, true); 865 if (IS_ERR(indexdir)) { 866 err = PTR_ERR(indexdir); 867 } else if (indexdir) { 868 ofs->workdir = indexdir; 869 err = ovl_setup_trap(sb, indexdir, &ofs->workdir_trap, 870 "indexdir"); 871 if (err) 872 goto out; 873 874 /* 875 * Verify upper root is exclusively associated with index dir. 876 * Older kernels stored upper fh in ".overlay.origin" 877 * xattr. If that xattr exists, verify that it is a match to 878 * upper dir file handle. In any case, verify or set xattr 879 * ".overlay.upper" to indicate that index may have 880 * directory entries. 881 */ 882 if (ovl_check_origin_xattr(ofs, indexdir)) { 883 err = ovl_verify_origin_xattr(ofs, indexdir, 884 OVL_XATTR_ORIGIN, 885 upperpath->dentry, true, 886 false); 887 if (err) 888 pr_err("failed to verify index dir 'origin' xattr\n"); 889 } 890 err = ovl_verify_upper(ofs, indexdir, upperpath->dentry, true); 891 if (err) 892 pr_err("failed to verify index dir 'upper' xattr\n"); 893 894 /* Cleanup bad/stale/orphan index entries */ 895 if (!err) 896 err = ovl_indexdir_cleanup(ofs); 897 } 898 if (err || !indexdir) 899 pr_warn("try deleting index dir or mounting with '-o index=off' to disable inodes index.\n"); 900 901 out: 902 mnt_drop_write(mnt); 903 out_free_fh: 904 kfree(fh); 905 return err; 906 } 907 908 static bool ovl_lower_uuid_ok(struct ovl_fs *ofs, const uuid_t *uuid) 909 { 910 unsigned int i; 911 912 if (!ofs->config.nfs_export && !ovl_upper_mnt(ofs)) 913 return true; 914 915 /* 916 * We allow using single lower with null uuid for index and nfs_export 917 * for example to support those features with single lower squashfs. 918 * To avoid regressions in setups of overlay with re-formatted lower 919 * squashfs, do not allow decoding origin with lower null uuid unless 920 * user opted-in to one of the new features that require following the 921 * lower inode of non-dir upper. 922 */ 923 if (ovl_allow_offline_changes(ofs) && uuid_is_null(uuid)) 924 return false; 925 926 for (i = 0; i < ofs->numfs; i++) { 927 /* 928 * We use uuid to associate an overlay lower file handle with a 929 * lower layer, so we can accept lower fs with null uuid as long 930 * as all lower layers with null uuid are on the same fs. 931 * if we detect multiple lower fs with the same uuid, we 932 * disable lower file handle decoding on all of them. 933 */ 934 if (ofs->fs[i].is_lower && 935 uuid_equal(&ofs->fs[i].sb->s_uuid, uuid)) { 936 ofs->fs[i].bad_uuid = true; 937 return false; 938 } 939 } 940 return true; 941 } 942 943 /* Get a unique fsid for the layer */ 944 static int ovl_get_fsid(struct ovl_fs *ofs, const struct path *path) 945 { 946 struct super_block *sb = path->mnt->mnt_sb; 947 unsigned int i; 948 dev_t dev; 949 int err; 950 bool bad_uuid = false; 951 bool warn = false; 952 953 for (i = 0; i < ofs->numfs; i++) { 954 if (ofs->fs[i].sb == sb) 955 return i; 956 } 957 958 if (!ovl_lower_uuid_ok(ofs, &sb->s_uuid)) { 959 bad_uuid = true; 960 if (ofs->config.xino == OVL_XINO_AUTO) { 961 ofs->config.xino = OVL_XINO_OFF; 962 warn = true; 963 } 964 if (ofs->config.index || ofs->config.nfs_export) { 965 ofs->config.index = false; 966 ofs->config.nfs_export = false; 967 warn = true; 968 } 969 if (warn) { 970 pr_warn("%s uuid detected in lower fs '%pd2', falling back to xino=%s,index=off,nfs_export=off.\n", 971 uuid_is_null(&sb->s_uuid) ? "null" : 972 "conflicting", 973 path->dentry, ovl_xino_mode(&ofs->config)); 974 } 975 } 976 977 err = get_anon_bdev(&dev); 978 if (err) { 979 pr_err("failed to get anonymous bdev for lowerpath\n"); 980 return err; 981 } 982 983 ofs->fs[ofs->numfs].sb = sb; 984 ofs->fs[ofs->numfs].pseudo_dev = dev; 985 ofs->fs[ofs->numfs].bad_uuid = bad_uuid; 986 987 return ofs->numfs++; 988 } 989 990 /* 991 * The fsid after the last lower fsid is used for the data layers. 992 * It is a "null fs" with a null sb, null uuid, and no pseudo dev. 993 */ 994 static int ovl_get_data_fsid(struct ovl_fs *ofs) 995 { 996 return ofs->numfs; 997 } 998 999 /* 1000 * Set the ovl sb encoding as the same one used by the first layer 1001 */ 1002 static int ovl_set_encoding(struct super_block *sb, struct super_block *fs_sb) 1003 { 1004 if (!sb_has_encoding(fs_sb)) 1005 return 0; 1006 1007 #if IS_ENABLED(CONFIG_UNICODE) 1008 if (sb_has_strict_encoding(fs_sb)) { 1009 pr_err("strict encoding not supported\n"); 1010 return -EINVAL; 1011 } 1012 1013 sb->s_encoding = fs_sb->s_encoding; 1014 sb->s_encoding_flags = fs_sb->s_encoding_flags; 1015 #endif 1016 return 0; 1017 } 1018 1019 static int ovl_get_layers(struct super_block *sb, struct ovl_fs *ofs, 1020 struct ovl_fs_context *ctx, struct ovl_layer *layers) 1021 { 1022 int err; 1023 unsigned int i; 1024 size_t nr_merged_lower; 1025 1026 ofs->fs = kcalloc(ctx->nr + 2, sizeof(struct ovl_sb), GFP_KERNEL); 1027 if (ofs->fs == NULL) 1028 return -ENOMEM; 1029 1030 /* 1031 * idx/fsid 0 are reserved for upper fs even with lower only overlay 1032 * and the last fsid is reserved for "null fs" of the data layers. 1033 */ 1034 ofs->numfs++; 1035 1036 /* 1037 * All lower layers that share the same fs as upper layer, use the same 1038 * pseudo_dev as upper layer. Allocate fs[0].pseudo_dev even for lower 1039 * only overlay to simplify ovl_fs_free(). 1040 * is_lower will be set if upper fs is shared with a lower layer. 1041 */ 1042 err = get_anon_bdev(&ofs->fs[0].pseudo_dev); 1043 if (err) { 1044 pr_err("failed to get anonymous bdev for upper fs\n"); 1045 return err; 1046 } 1047 1048 if (ovl_upper_mnt(ofs)) { 1049 ofs->fs[0].sb = ovl_upper_mnt(ofs)->mnt_sb; 1050 ofs->fs[0].is_lower = false; 1051 1052 if (ofs->casefold) { 1053 err = ovl_set_encoding(sb, ofs->fs[0].sb); 1054 if (err) 1055 return err; 1056 } 1057 } 1058 1059 nr_merged_lower = ctx->nr - ctx->nr_data; 1060 for (i = 0; i < ctx->nr; i++) { 1061 struct ovl_fs_context_layer *l = &ctx->lower[i]; 1062 struct vfsmount *mnt; 1063 struct inode *trap; 1064 int fsid; 1065 1066 if (i < nr_merged_lower) 1067 fsid = ovl_get_fsid(ofs, &l->path); 1068 else 1069 fsid = ovl_get_data_fsid(ofs); 1070 if (fsid < 0) 1071 return fsid; 1072 1073 /* 1074 * Check if lower root conflicts with this overlay layers before 1075 * checking if it is in-use as upperdir/workdir of "another" 1076 * mount, because we do not bother to check in ovl_is_inuse() if 1077 * the upperdir/workdir is in fact in-use by our 1078 * upperdir/workdir. 1079 */ 1080 err = ovl_setup_trap(sb, l->path.dentry, &trap, "lowerdir"); 1081 if (err) 1082 return err; 1083 1084 if (ovl_is_inuse(l->path.dentry)) { 1085 err = ovl_report_in_use(ofs, "lowerdir"); 1086 if (err) { 1087 iput(trap); 1088 return err; 1089 } 1090 } 1091 1092 mnt = clone_private_mount(&l->path); 1093 err = PTR_ERR(mnt); 1094 if (IS_ERR(mnt)) { 1095 pr_err("failed to clone lowerpath\n"); 1096 iput(trap); 1097 return err; 1098 } 1099 1100 /* 1101 * Make lower layers R/O. That way fchmod/fchown on lower file 1102 * will fail instead of modifying lower fs. 1103 */ 1104 mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME; 1105 1106 layers[ofs->numlayer].trap = trap; 1107 layers[ofs->numlayer].mnt = mnt; 1108 layers[ofs->numlayer].idx = ofs->numlayer; 1109 layers[ofs->numlayer].fsid = fsid; 1110 layers[ofs->numlayer].fs = &ofs->fs[fsid]; 1111 /* Store for printing lowerdir=... in ovl_show_options() */ 1112 ofs->config.lowerdirs[ofs->numlayer] = l->name; 1113 l->name = NULL; 1114 ofs->numlayer++; 1115 ofs->fs[fsid].is_lower = true; 1116 1117 if (ofs->casefold) { 1118 if (!ovl_upper_mnt(ofs) && !sb_has_encoding(sb)) { 1119 err = ovl_set_encoding(sb, ofs->fs[fsid].sb); 1120 if (err) 1121 return err; 1122 } 1123 1124 if (!sb_same_encoding(sb, mnt->mnt_sb)) { 1125 pr_err("all layers must have the same encoding\n"); 1126 return -EINVAL; 1127 } 1128 } 1129 } 1130 1131 /* 1132 * When all layers on same fs, overlay can use real inode numbers. 1133 * With mount option "xino=<on|auto>", mounter declares that there are 1134 * enough free high bits in underlying fs to hold the unique fsid. 1135 * If overlayfs does encounter underlying inodes using the high xino 1136 * bits reserved for fsid, it emits a warning and uses the original 1137 * inode number or a non persistent inode number allocated from a 1138 * dedicated range. 1139 */ 1140 if (ofs->numfs - !ovl_upper_mnt(ofs) == 1) { 1141 if (ofs->config.xino == OVL_XINO_ON) 1142 pr_info("\"xino=on\" is useless with all layers on same fs, ignore.\n"); 1143 ofs->xino_mode = 0; 1144 } else if (ofs->config.xino == OVL_XINO_OFF) { 1145 ofs->xino_mode = -1; 1146 } else if (ofs->xino_mode < 0) { 1147 /* 1148 * This is a roundup of number of bits needed for encoding 1149 * fsid, where fsid 0 is reserved for upper fs (even with 1150 * lower only overlay) +1 extra bit is reserved for the non 1151 * persistent inode number range that is used for resolving 1152 * xino lower bits overflow. 1153 */ 1154 BUILD_BUG_ON(ilog2(OVL_MAX_STACK) > 30); 1155 ofs->xino_mode = ilog2(ofs->numfs - 1) + 2; 1156 } 1157 1158 if (ofs->xino_mode > 0) { 1159 pr_info("\"xino\" feature enabled using %d upper inode bits.\n", 1160 ofs->xino_mode); 1161 } 1162 1163 return 0; 1164 } 1165 1166 static struct ovl_entry *ovl_get_lowerstack(struct super_block *sb, 1167 struct ovl_fs_context *ctx, 1168 struct ovl_fs *ofs, 1169 struct ovl_layer *layers) 1170 { 1171 int err; 1172 unsigned int i; 1173 size_t nr_merged_lower; 1174 struct ovl_entry *oe; 1175 struct ovl_path *lowerstack; 1176 1177 struct ovl_fs_context_layer *l; 1178 1179 if (!ofs->config.upperdir && ctx->nr == 1) { 1180 pr_err("at least 2 lowerdir are needed while upperdir nonexistent\n"); 1181 return ERR_PTR(-EINVAL); 1182 } 1183 1184 if (ctx->nr == ctx->nr_data) { 1185 pr_err("at least one non-data lowerdir is required\n"); 1186 return ERR_PTR(-EINVAL); 1187 } 1188 1189 err = -EINVAL; 1190 for (i = 0; i < ctx->nr; i++) { 1191 l = &ctx->lower[i]; 1192 1193 err = ovl_lower_dir(l->name, &l->path, ofs, &sb->s_stack_depth); 1194 if (err) 1195 return ERR_PTR(err); 1196 } 1197 1198 err = -EINVAL; 1199 sb->s_stack_depth++; 1200 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) { 1201 pr_err("maximum fs stacking depth exceeded\n"); 1202 return ERR_PTR(err); 1203 } 1204 1205 err = ovl_get_layers(sb, ofs, ctx, layers); 1206 if (err) 1207 return ERR_PTR(err); 1208 1209 err = -ENOMEM; 1210 /* Data-only layers are not merged in root directory */ 1211 nr_merged_lower = ctx->nr - ctx->nr_data; 1212 oe = ovl_alloc_entry(nr_merged_lower); 1213 if (!oe) 1214 return ERR_PTR(err); 1215 1216 lowerstack = ovl_lowerstack(oe); 1217 for (i = 0; i < nr_merged_lower; i++) { 1218 l = &ctx->lower[i]; 1219 lowerstack[i].dentry = dget(l->path.dentry); 1220 lowerstack[i].layer = &ofs->layers[i + 1]; 1221 } 1222 ofs->numdatalayer = ctx->nr_data; 1223 1224 return oe; 1225 } 1226 1227 /* 1228 * Check if this layer root is a descendant of: 1229 * - another layer of this overlayfs instance 1230 * - upper/work dir of any overlayfs instance 1231 */ 1232 static int ovl_check_layer(struct super_block *sb, struct ovl_fs *ofs, 1233 struct dentry *dentry, const char *name, 1234 bool is_lower) 1235 { 1236 struct dentry *next = dentry, *parent; 1237 int err = 0; 1238 1239 if (!dentry) 1240 return 0; 1241 1242 parent = dget_parent(next); 1243 1244 /* Walk back ancestors to root (inclusive) looking for traps */ 1245 while (!err && parent != next) { 1246 if (is_lower && ovl_lookup_trap_inode(sb, parent)) { 1247 err = -ELOOP; 1248 pr_err("overlapping %s path\n", name); 1249 } else if (ovl_is_inuse(parent)) { 1250 err = ovl_report_in_use(ofs, name); 1251 } 1252 next = parent; 1253 parent = dget_parent(next); 1254 dput(next); 1255 } 1256 1257 dput(parent); 1258 1259 return err; 1260 } 1261 1262 /* 1263 * Check if any of the layers or work dirs overlap. 1264 */ 1265 static int ovl_check_overlapping_layers(struct super_block *sb, 1266 struct ovl_fs *ofs) 1267 { 1268 int i, err; 1269 1270 if (ovl_upper_mnt(ofs)) { 1271 err = ovl_check_layer(sb, ofs, ovl_upper_mnt(ofs)->mnt_root, 1272 "upperdir", false); 1273 if (err) 1274 return err; 1275 1276 /* 1277 * Checking workbasedir avoids hitting ovl_is_inuse(parent) of 1278 * this instance and covers overlapping work and index dirs, 1279 * unless work or index dir have been moved since created inside 1280 * workbasedir. In that case, we already have their traps in 1281 * inode cache and we will catch that case on lookup. 1282 */ 1283 err = ovl_check_layer(sb, ofs, ofs->workbasedir, "workdir", 1284 false); 1285 if (err) 1286 return err; 1287 } 1288 1289 for (i = 1; i < ofs->numlayer; i++) { 1290 err = ovl_check_layer(sb, ofs, 1291 ofs->layers[i].mnt->mnt_root, 1292 "lowerdir", true); 1293 if (err) 1294 return err; 1295 } 1296 1297 return 0; 1298 } 1299 1300 static struct dentry *ovl_get_root(struct super_block *sb, 1301 struct dentry *upperdentry, 1302 struct ovl_entry *oe) 1303 { 1304 struct dentry *root; 1305 struct ovl_fs *ofs = OVL_FS(sb); 1306 struct ovl_path *lowerpath = ovl_lowerstack(oe); 1307 unsigned long ino = d_inode(lowerpath->dentry)->i_ino; 1308 int fsid = lowerpath->layer->fsid; 1309 struct ovl_inode_params oip = { 1310 .upperdentry = upperdentry, 1311 .oe = oe, 1312 }; 1313 1314 root = d_make_root(ovl_new_inode(sb, S_IFDIR, 0)); 1315 if (!root) 1316 return NULL; 1317 1318 if (upperdentry) { 1319 /* Root inode uses upper st_ino/i_ino */ 1320 ino = d_inode(upperdentry)->i_ino; 1321 fsid = 0; 1322 ovl_dentry_set_upper_alias(root); 1323 if (ovl_is_impuredir(sb, upperdentry)) 1324 ovl_set_flag(OVL_IMPURE, d_inode(root)); 1325 } 1326 1327 /* Look for xwhiteouts marker except in the lowermost layer */ 1328 for (int i = 0; i < ovl_numlower(oe) - 1; i++, lowerpath++) { 1329 struct path path = { 1330 .mnt = lowerpath->layer->mnt, 1331 .dentry = lowerpath->dentry, 1332 }; 1333 1334 /* overlay.opaque=x means xwhiteouts directory */ 1335 if (ovl_get_opaquedir_val(ofs, &path) == 'x') { 1336 ovl_layer_set_xwhiteouts(ofs, lowerpath->layer); 1337 ovl_dentry_set_xwhiteouts(root); 1338 } 1339 } 1340 1341 /* Root is always merge -> can have whiteouts */ 1342 ovl_set_flag(OVL_WHITEOUTS, d_inode(root)); 1343 ovl_dentry_set_flag(OVL_E_CONNECTED, root); 1344 ovl_set_upperdata(d_inode(root)); 1345 ovl_inode_init(d_inode(root), &oip, ino, fsid); 1346 WARN_ON(!!IS_CASEFOLDED(d_inode(root)) != ofs->casefold); 1347 ovl_dentry_init_flags(root, upperdentry, oe, DCACHE_OP_WEAK_REVALIDATE); 1348 /* root keeps a reference of upperdentry */ 1349 dget(upperdentry); 1350 1351 return root; 1352 } 1353 1354 static void ovl_set_d_op(struct super_block *sb) 1355 { 1356 #if IS_ENABLED(CONFIG_UNICODE) 1357 struct ovl_fs *ofs = sb->s_fs_info; 1358 1359 if (ofs->casefold) { 1360 set_default_d_op(sb, &ovl_dentry_ci_operations); 1361 return; 1362 } 1363 #endif 1364 set_default_d_op(sb, &ovl_dentry_operations); 1365 } 1366 1367 static int ovl_fill_super_creds(struct fs_context *fc, struct super_block *sb) 1368 { 1369 struct ovl_fs *ofs = sb->s_fs_info; 1370 struct cred *creator_cred = (struct cred *)ofs->creator_cred; 1371 struct ovl_fs_context *ctx = fc->fs_private; 1372 struct ovl_layer *layers; 1373 struct ovl_entry *oe = NULL; 1374 int err; 1375 1376 err = ovl_fs_params_verify(ctx, &ofs->config); 1377 if (err) 1378 return err; 1379 1380 err = -EINVAL; 1381 if (ctx->nr == 0) { 1382 if (!(fc->sb_flags & SB_SILENT)) 1383 pr_err("missing 'lowerdir'\n"); 1384 return err; 1385 } 1386 1387 err = -ENOMEM; 1388 layers = kcalloc(ctx->nr + 1, sizeof(struct ovl_layer), GFP_KERNEL); 1389 if (!layers) 1390 return err; 1391 1392 ofs->config.lowerdirs = kcalloc(ctx->nr + 1, sizeof(char *), GFP_KERNEL); 1393 if (!ofs->config.lowerdirs) { 1394 kfree(layers); 1395 return err; 1396 } 1397 ofs->layers = layers; 1398 /* 1399 * Layer 0 is reserved for upper even if there's no upper. 1400 * config.lowerdirs[0] is used for storing the user provided colon 1401 * separated lowerdir string. 1402 */ 1403 ofs->config.lowerdirs[0] = ctx->lowerdir_all; 1404 ctx->lowerdir_all = NULL; 1405 ofs->numlayer = 1; 1406 1407 sb->s_stack_depth = 0; 1408 sb->s_maxbytes = MAX_LFS_FILESIZE; 1409 atomic_long_set(&ofs->last_ino, 1); 1410 /* Assume underlying fs uses 32bit inodes unless proven otherwise */ 1411 if (ofs->config.xino != OVL_XINO_OFF) { 1412 ofs->xino_mode = BITS_PER_LONG - 32; 1413 if (!ofs->xino_mode) { 1414 pr_warn("xino not supported on 32bit kernel, falling back to xino=off.\n"); 1415 ofs->config.xino = OVL_XINO_OFF; 1416 } 1417 } 1418 1419 /* alloc/destroy_inode needed for setting up traps in inode cache */ 1420 sb->s_op = &ovl_super_operations; 1421 1422 if (ofs->config.upperdir) { 1423 struct super_block *upper_sb; 1424 1425 err = -EINVAL; 1426 if (!ofs->config.workdir) { 1427 pr_err("missing 'workdir'\n"); 1428 return err; 1429 } 1430 1431 err = ovl_get_upper(sb, ofs, &layers[0], &ctx->upper); 1432 if (err) 1433 return err; 1434 1435 upper_sb = ovl_upper_mnt(ofs)->mnt_sb; 1436 if (!ovl_should_sync(ofs)) { 1437 ofs->errseq = errseq_sample(&upper_sb->s_wb_err); 1438 if (errseq_check(&upper_sb->s_wb_err, ofs->errseq)) { 1439 err = -EIO; 1440 pr_err("Cannot mount volatile when upperdir has an unseen error. Sync upperdir fs to clear state.\n"); 1441 return err; 1442 } 1443 } 1444 1445 err = ovl_get_workdir(sb, ofs, &ctx->upper, &ctx->work); 1446 if (err) 1447 return err; 1448 1449 if (!ofs->workdir) 1450 sb->s_flags |= SB_RDONLY; 1451 1452 sb->s_stack_depth = upper_sb->s_stack_depth; 1453 sb->s_time_gran = upper_sb->s_time_gran; 1454 } 1455 oe = ovl_get_lowerstack(sb, ctx, ofs, layers); 1456 err = PTR_ERR(oe); 1457 if (IS_ERR(oe)) 1458 return err; 1459 1460 /* If the upper fs is nonexistent, we mark overlayfs r/o too */ 1461 if (!ovl_upper_mnt(ofs)) 1462 sb->s_flags |= SB_RDONLY; 1463 1464 if (!ovl_origin_uuid(ofs) && ofs->numfs > 1) { 1465 pr_warn("The uuid=off requires a single fs for lower and upper, falling back to uuid=null.\n"); 1466 ofs->config.uuid = OVL_UUID_NULL; 1467 } else if (ovl_has_fsid(ofs) && ovl_upper_mnt(ofs)) { 1468 /* Use per instance persistent uuid/fsid */ 1469 ovl_init_uuid_xattr(sb, ofs, &ctx->upper); 1470 } 1471 1472 if (!ovl_force_readonly(ofs) && ofs->config.index) { 1473 err = ovl_get_indexdir(sb, ofs, oe, &ctx->upper); 1474 if (err) 1475 goto out_free_oe; 1476 1477 /* Force r/o mount with no index dir */ 1478 if (!ofs->workdir) 1479 sb->s_flags |= SB_RDONLY; 1480 } 1481 1482 err = ovl_check_overlapping_layers(sb, ofs); 1483 if (err) 1484 goto out_free_oe; 1485 1486 /* Show index=off in /proc/mounts for forced r/o mount */ 1487 if (!ofs->workdir) { 1488 ofs->config.index = false; 1489 if (ovl_upper_mnt(ofs) && ofs->config.nfs_export) { 1490 pr_warn("NFS export requires an index dir, falling back to nfs_export=off.\n"); 1491 ofs->config.nfs_export = false; 1492 } 1493 } 1494 1495 if (ofs->config.metacopy && ofs->config.nfs_export) { 1496 pr_warn("NFS export is not supported with metadata only copy up, falling back to nfs_export=off.\n"); 1497 ofs->config.nfs_export = false; 1498 } 1499 1500 /* 1501 * Support encoding decodable file handles with nfs_export=on 1502 * and encoding non-decodable file handles with nfs_export=off 1503 * if all layers support file handles. 1504 */ 1505 if (ofs->config.nfs_export) 1506 sb->s_export_op = &ovl_export_operations; 1507 else if (!ofs->nofh) 1508 sb->s_export_op = &ovl_export_fid_operations; 1509 1510 /* Never override disk quota limits or use reserved space */ 1511 cap_lower(creator_cred->cap_effective, CAP_SYS_RESOURCE); 1512 1513 sb->s_magic = OVERLAYFS_SUPER_MAGIC; 1514 sb->s_xattr = ovl_xattr_handlers(ofs); 1515 sb->s_fs_info = ofs; 1516 #ifdef CONFIG_FS_POSIX_ACL 1517 sb->s_flags |= SB_POSIXACL; 1518 #endif 1519 sb->s_iflags |= SB_I_SKIP_SYNC; 1520 /* 1521 * Ensure that umask handling is done by the filesystems used 1522 * for the the upper layer instead of overlayfs as that would 1523 * lead to unexpected results. 1524 */ 1525 sb->s_iflags |= SB_I_NOUMASK; 1526 sb->s_iflags |= SB_I_EVM_HMAC_UNSUPPORTED; 1527 1528 err = -ENOMEM; 1529 sb->s_root = ovl_get_root(sb, ctx->upper.dentry, oe); 1530 if (!sb->s_root) 1531 goto out_free_oe; 1532 1533 return 0; 1534 1535 out_free_oe: 1536 ovl_free_entry(oe); 1537 return err; 1538 } 1539 1540 int ovl_fill_super(struct super_block *sb, struct fs_context *fc) 1541 { 1542 struct ovl_fs *ofs = sb->s_fs_info; 1543 int err; 1544 1545 err = -EIO; 1546 if (WARN_ON(fc->user_ns != current_user_ns())) 1547 goto out_err; 1548 1549 ovl_set_d_op(sb); 1550 1551 if (!ofs->creator_cred) { 1552 err = -ENOMEM; 1553 ofs->creator_cred = prepare_creds(); 1554 if (!ofs->creator_cred) 1555 goto out_err; 1556 } 1557 1558 with_ovl_creds(sb) 1559 err = ovl_fill_super_creds(fc, sb); 1560 1561 out_err: 1562 if (err) { 1563 ovl_free_fs(ofs); 1564 sb->s_fs_info = NULL; 1565 } 1566 1567 return err; 1568 } 1569 1570 struct file_system_type ovl_fs_type = { 1571 .owner = THIS_MODULE, 1572 .name = "overlay", 1573 .init_fs_context = ovl_init_fs_context, 1574 .parameters = ovl_parameter_spec, 1575 .fs_flags = FS_USERNS_MOUNT, 1576 .kill_sb = kill_anon_super, 1577 }; 1578 MODULE_ALIAS_FS("overlay"); 1579 1580 static void ovl_inode_init_once(void *foo) 1581 { 1582 struct ovl_inode *oi = foo; 1583 1584 inode_init_once(&oi->vfs_inode); 1585 } 1586 1587 static int __init ovl_init(void) 1588 { 1589 int err; 1590 1591 ovl_inode_cachep = kmem_cache_create("ovl_inode", 1592 sizeof(struct ovl_inode), 0, 1593 (SLAB_RECLAIM_ACCOUNT| 1594 SLAB_ACCOUNT), 1595 ovl_inode_init_once); 1596 if (ovl_inode_cachep == NULL) 1597 return -ENOMEM; 1598 1599 err = register_filesystem(&ovl_fs_type); 1600 if (!err) 1601 return 0; 1602 1603 kmem_cache_destroy(ovl_inode_cachep); 1604 1605 return err; 1606 } 1607 1608 static void __exit ovl_exit(void) 1609 { 1610 unregister_filesystem(&ovl_fs_type); 1611 1612 /* 1613 * Make sure all delayed rcu free inodes are flushed before we 1614 * destroy cache. 1615 */ 1616 rcu_barrier(); 1617 kmem_cache_destroy(ovl_inode_cachep); 1618 } 1619 1620 module_init(ovl_init); 1621 module_exit(ovl_exit); 1622