1 /* 2 * 3 * Copyright (C) 2011 Novell Inc. 4 * 5 * This program is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 as published by 7 * the Free Software Foundation. 8 */ 9 10 #include <uapi/linux/magic.h> 11 #include <linux/fs.h> 12 #include <linux/namei.h> 13 #include <linux/xattr.h> 14 #include <linux/mount.h> 15 #include <linux/parser.h> 16 #include <linux/module.h> 17 #include <linux/statfs.h> 18 #include <linux/seq_file.h> 19 #include <linux/posix_acl_xattr.h> 20 #include <linux/exportfs.h> 21 #include "overlayfs.h" 22 23 MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>"); 24 MODULE_DESCRIPTION("Overlay filesystem"); 25 MODULE_LICENSE("GPL"); 26 27 28 struct ovl_dir_cache; 29 30 #define OVL_MAX_STACK 500 31 32 static bool ovl_redirect_dir_def = IS_ENABLED(CONFIG_OVERLAY_FS_REDIRECT_DIR); 33 module_param_named(redirect_dir, ovl_redirect_dir_def, bool, 0644); 34 MODULE_PARM_DESC(ovl_redirect_dir_def, 35 "Default to on or off for the redirect_dir feature"); 36 37 static bool ovl_redirect_always_follow = 38 IS_ENABLED(CONFIG_OVERLAY_FS_REDIRECT_ALWAYS_FOLLOW); 39 module_param_named(redirect_always_follow, ovl_redirect_always_follow, 40 bool, 0644); 41 MODULE_PARM_DESC(ovl_redirect_always_follow, 42 "Follow redirects even if redirect_dir feature is turned off"); 43 44 static bool ovl_index_def = IS_ENABLED(CONFIG_OVERLAY_FS_INDEX); 45 module_param_named(index, ovl_index_def, bool, 0644); 46 MODULE_PARM_DESC(ovl_index_def, 47 "Default to on or off for the inodes index feature"); 48 49 static bool ovl_nfs_export_def = IS_ENABLED(CONFIG_OVERLAY_FS_NFS_EXPORT); 50 module_param_named(nfs_export, ovl_nfs_export_def, bool, 0644); 51 MODULE_PARM_DESC(ovl_nfs_export_def, 52 "Default to on or off for the NFS export feature"); 53 54 static bool ovl_xino_auto_def = IS_ENABLED(CONFIG_OVERLAY_FS_XINO_AUTO); 55 module_param_named(xino_auto, ovl_xino_auto_def, bool, 0644); 56 MODULE_PARM_DESC(ovl_xino_auto_def, 57 "Auto enable xino feature"); 58 59 static void ovl_entry_stack_free(struct ovl_entry *oe) 60 { 61 unsigned int i; 62 63 for (i = 0; i < oe->numlower; i++) 64 dput(oe->lowerstack[i].dentry); 65 } 66 67 static bool ovl_metacopy_def = IS_ENABLED(CONFIG_OVERLAY_FS_METACOPY); 68 module_param_named(metacopy, ovl_metacopy_def, bool, 0644); 69 MODULE_PARM_DESC(ovl_metacopy_def, 70 "Default to on or off for the metadata only copy up feature"); 71 72 static void ovl_dentry_release(struct dentry *dentry) 73 { 74 struct ovl_entry *oe = dentry->d_fsdata; 75 76 if (oe) { 77 ovl_entry_stack_free(oe); 78 kfree_rcu(oe, rcu); 79 } 80 } 81 82 static struct dentry *ovl_d_real(struct dentry *dentry, 83 const struct inode *inode) 84 { 85 struct dentry *real; 86 87 /* It's an overlay file */ 88 if (inode && d_inode(dentry) == inode) 89 return dentry; 90 91 if (!d_is_reg(dentry)) { 92 if (!inode || inode == d_inode(dentry)) 93 return dentry; 94 goto bug; 95 } 96 97 real = ovl_dentry_upper(dentry); 98 if (real && (inode == d_inode(real))) 99 return real; 100 101 if (real && !inode && ovl_has_upperdata(d_inode(dentry))) 102 return real; 103 104 real = ovl_dentry_lowerdata(dentry); 105 if (!real) 106 goto bug; 107 108 /* Handle recursion */ 109 real = d_real(real, inode); 110 111 if (!inode || inode == d_inode(real)) 112 return real; 113 bug: 114 WARN(1, "ovl_d_real(%pd4, %s:%lu): real dentry not found\n", dentry, 115 inode ? inode->i_sb->s_id : "NULL", inode ? inode->i_ino : 0); 116 return dentry; 117 } 118 119 static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags) 120 { 121 struct ovl_entry *oe = dentry->d_fsdata; 122 unsigned int i; 123 int ret = 1; 124 125 for (i = 0; i < oe->numlower; i++) { 126 struct dentry *d = oe->lowerstack[i].dentry; 127 128 if (d->d_flags & DCACHE_OP_REVALIDATE) { 129 ret = d->d_op->d_revalidate(d, flags); 130 if (ret < 0) 131 return ret; 132 if (!ret) { 133 if (!(flags & LOOKUP_RCU)) 134 d_invalidate(d); 135 return -ESTALE; 136 } 137 } 138 } 139 return 1; 140 } 141 142 static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags) 143 { 144 struct ovl_entry *oe = dentry->d_fsdata; 145 unsigned int i; 146 int ret = 1; 147 148 for (i = 0; i < oe->numlower; i++) { 149 struct dentry *d = oe->lowerstack[i].dentry; 150 151 if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE) { 152 ret = d->d_op->d_weak_revalidate(d, flags); 153 if (ret <= 0) 154 break; 155 } 156 } 157 return ret; 158 } 159 160 static const struct dentry_operations ovl_dentry_operations = { 161 .d_release = ovl_dentry_release, 162 .d_real = ovl_d_real, 163 }; 164 165 static const struct dentry_operations ovl_reval_dentry_operations = { 166 .d_release = ovl_dentry_release, 167 .d_real = ovl_d_real, 168 .d_revalidate = ovl_dentry_revalidate, 169 .d_weak_revalidate = ovl_dentry_weak_revalidate, 170 }; 171 172 static struct kmem_cache *ovl_inode_cachep; 173 174 static struct inode *ovl_alloc_inode(struct super_block *sb) 175 { 176 struct ovl_inode *oi = kmem_cache_alloc(ovl_inode_cachep, GFP_KERNEL); 177 178 if (!oi) 179 return NULL; 180 181 oi->cache = NULL; 182 oi->redirect = NULL; 183 oi->version = 0; 184 oi->flags = 0; 185 oi->__upperdentry = NULL; 186 oi->lower = NULL; 187 oi->lowerdata = NULL; 188 mutex_init(&oi->lock); 189 190 return &oi->vfs_inode; 191 } 192 193 static void ovl_free_inode(struct inode *inode) 194 { 195 struct ovl_inode *oi = OVL_I(inode); 196 197 kfree(oi->redirect); 198 mutex_destroy(&oi->lock); 199 kmem_cache_free(ovl_inode_cachep, oi); 200 } 201 202 static void ovl_destroy_inode(struct inode *inode) 203 { 204 struct ovl_inode *oi = OVL_I(inode); 205 206 dput(oi->__upperdentry); 207 iput(oi->lower); 208 if (S_ISDIR(inode->i_mode)) 209 ovl_dir_cache_free(inode); 210 else 211 iput(oi->lowerdata); 212 } 213 214 static void ovl_free_fs(struct ovl_fs *ofs) 215 { 216 unsigned i; 217 218 iput(ofs->indexdir_trap); 219 iput(ofs->workdir_trap); 220 iput(ofs->upperdir_trap); 221 dput(ofs->indexdir); 222 dput(ofs->workdir); 223 if (ofs->workdir_locked) 224 ovl_inuse_unlock(ofs->workbasedir); 225 dput(ofs->workbasedir); 226 if (ofs->upperdir_locked) 227 ovl_inuse_unlock(ofs->upper_mnt->mnt_root); 228 mntput(ofs->upper_mnt); 229 for (i = 0; i < ofs->numlower; i++) { 230 iput(ofs->lower_layers[i].trap); 231 mntput(ofs->lower_layers[i].mnt); 232 } 233 for (i = 0; i < ofs->numlowerfs; i++) 234 free_anon_bdev(ofs->lower_fs[i].pseudo_dev); 235 kfree(ofs->lower_layers); 236 kfree(ofs->lower_fs); 237 238 kfree(ofs->config.lowerdir); 239 kfree(ofs->config.upperdir); 240 kfree(ofs->config.workdir); 241 kfree(ofs->config.redirect_mode); 242 if (ofs->creator_cred) 243 put_cred(ofs->creator_cred); 244 kfree(ofs); 245 } 246 247 static void ovl_put_super(struct super_block *sb) 248 { 249 struct ovl_fs *ofs = sb->s_fs_info; 250 251 ovl_free_fs(ofs); 252 } 253 254 /* Sync real dirty inodes in upper filesystem (if it exists) */ 255 static int ovl_sync_fs(struct super_block *sb, int wait) 256 { 257 struct ovl_fs *ofs = sb->s_fs_info; 258 struct super_block *upper_sb; 259 int ret; 260 261 if (!ofs->upper_mnt) 262 return 0; 263 264 /* 265 * If this is a sync(2) call or an emergency sync, all the super blocks 266 * will be iterated, including upper_sb, so no need to do anything. 267 * 268 * If this is a syncfs(2) call, then we do need to call 269 * sync_filesystem() on upper_sb, but enough if we do it when being 270 * called with wait == 1. 271 */ 272 if (!wait) 273 return 0; 274 275 upper_sb = ofs->upper_mnt->mnt_sb; 276 277 down_read(&upper_sb->s_umount); 278 ret = sync_filesystem(upper_sb); 279 up_read(&upper_sb->s_umount); 280 281 return ret; 282 } 283 284 /** 285 * ovl_statfs 286 * @sb: The overlayfs super block 287 * @buf: The struct kstatfs to fill in with stats 288 * 289 * Get the filesystem statistics. As writes always target the upper layer 290 * filesystem pass the statfs to the upper filesystem (if it exists) 291 */ 292 static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf) 293 { 294 struct ovl_fs *ofs = dentry->d_sb->s_fs_info; 295 struct dentry *root_dentry = dentry->d_sb->s_root; 296 struct path path; 297 int err; 298 299 ovl_path_real(root_dentry, &path); 300 301 err = vfs_statfs(&path, buf); 302 if (!err) { 303 buf->f_namelen = ofs->namelen; 304 buf->f_type = OVERLAYFS_SUPER_MAGIC; 305 } 306 307 return err; 308 } 309 310 /* Will this overlay be forced to mount/remount ro? */ 311 static bool ovl_force_readonly(struct ovl_fs *ofs) 312 { 313 return (!ofs->upper_mnt || !ofs->workdir); 314 } 315 316 static const char *ovl_redirect_mode_def(void) 317 { 318 return ovl_redirect_dir_def ? "on" : "off"; 319 } 320 321 enum { 322 OVL_XINO_OFF, 323 OVL_XINO_AUTO, 324 OVL_XINO_ON, 325 }; 326 327 static const char * const ovl_xino_str[] = { 328 "off", 329 "auto", 330 "on", 331 }; 332 333 static inline int ovl_xino_def(void) 334 { 335 return ovl_xino_auto_def ? OVL_XINO_AUTO : OVL_XINO_OFF; 336 } 337 338 /** 339 * ovl_show_options 340 * 341 * Prints the mount options for a given superblock. 342 * Returns zero; does not fail. 343 */ 344 static int ovl_show_options(struct seq_file *m, struct dentry *dentry) 345 { 346 struct super_block *sb = dentry->d_sb; 347 struct ovl_fs *ofs = sb->s_fs_info; 348 349 seq_show_option(m, "lowerdir", ofs->config.lowerdir); 350 if (ofs->config.upperdir) { 351 seq_show_option(m, "upperdir", ofs->config.upperdir); 352 seq_show_option(m, "workdir", ofs->config.workdir); 353 } 354 if (ofs->config.default_permissions) 355 seq_puts(m, ",default_permissions"); 356 if (strcmp(ofs->config.redirect_mode, ovl_redirect_mode_def()) != 0) 357 seq_printf(m, ",redirect_dir=%s", ofs->config.redirect_mode); 358 if (ofs->config.index != ovl_index_def) 359 seq_printf(m, ",index=%s", ofs->config.index ? "on" : "off"); 360 if (ofs->config.nfs_export != ovl_nfs_export_def) 361 seq_printf(m, ",nfs_export=%s", ofs->config.nfs_export ? 362 "on" : "off"); 363 if (ofs->config.xino != ovl_xino_def()) 364 seq_printf(m, ",xino=%s", ovl_xino_str[ofs->config.xino]); 365 if (ofs->config.metacopy != ovl_metacopy_def) 366 seq_printf(m, ",metacopy=%s", 367 ofs->config.metacopy ? "on" : "off"); 368 return 0; 369 } 370 371 static int ovl_remount(struct super_block *sb, int *flags, char *data) 372 { 373 struct ovl_fs *ofs = sb->s_fs_info; 374 375 if (!(*flags & SB_RDONLY) && ovl_force_readonly(ofs)) 376 return -EROFS; 377 378 return 0; 379 } 380 381 static const struct super_operations ovl_super_operations = { 382 .alloc_inode = ovl_alloc_inode, 383 .free_inode = ovl_free_inode, 384 .destroy_inode = ovl_destroy_inode, 385 .drop_inode = generic_delete_inode, 386 .put_super = ovl_put_super, 387 .sync_fs = ovl_sync_fs, 388 .statfs = ovl_statfs, 389 .show_options = ovl_show_options, 390 .remount_fs = ovl_remount, 391 }; 392 393 enum { 394 OPT_LOWERDIR, 395 OPT_UPPERDIR, 396 OPT_WORKDIR, 397 OPT_DEFAULT_PERMISSIONS, 398 OPT_REDIRECT_DIR, 399 OPT_INDEX_ON, 400 OPT_INDEX_OFF, 401 OPT_NFS_EXPORT_ON, 402 OPT_NFS_EXPORT_OFF, 403 OPT_XINO_ON, 404 OPT_XINO_OFF, 405 OPT_XINO_AUTO, 406 OPT_METACOPY_ON, 407 OPT_METACOPY_OFF, 408 OPT_ERR, 409 }; 410 411 static const match_table_t ovl_tokens = { 412 {OPT_LOWERDIR, "lowerdir=%s"}, 413 {OPT_UPPERDIR, "upperdir=%s"}, 414 {OPT_WORKDIR, "workdir=%s"}, 415 {OPT_DEFAULT_PERMISSIONS, "default_permissions"}, 416 {OPT_REDIRECT_DIR, "redirect_dir=%s"}, 417 {OPT_INDEX_ON, "index=on"}, 418 {OPT_INDEX_OFF, "index=off"}, 419 {OPT_NFS_EXPORT_ON, "nfs_export=on"}, 420 {OPT_NFS_EXPORT_OFF, "nfs_export=off"}, 421 {OPT_XINO_ON, "xino=on"}, 422 {OPT_XINO_OFF, "xino=off"}, 423 {OPT_XINO_AUTO, "xino=auto"}, 424 {OPT_METACOPY_ON, "metacopy=on"}, 425 {OPT_METACOPY_OFF, "metacopy=off"}, 426 {OPT_ERR, NULL} 427 }; 428 429 static char *ovl_next_opt(char **s) 430 { 431 char *sbegin = *s; 432 char *p; 433 434 if (sbegin == NULL) 435 return NULL; 436 437 for (p = sbegin; *p; p++) { 438 if (*p == '\\') { 439 p++; 440 if (!*p) 441 break; 442 } else if (*p == ',') { 443 *p = '\0'; 444 *s = p + 1; 445 return sbegin; 446 } 447 } 448 *s = NULL; 449 return sbegin; 450 } 451 452 static int ovl_parse_redirect_mode(struct ovl_config *config, const char *mode) 453 { 454 if (strcmp(mode, "on") == 0) { 455 config->redirect_dir = true; 456 /* 457 * Does not make sense to have redirect creation without 458 * redirect following. 459 */ 460 config->redirect_follow = true; 461 } else if (strcmp(mode, "follow") == 0) { 462 config->redirect_follow = true; 463 } else if (strcmp(mode, "off") == 0) { 464 if (ovl_redirect_always_follow) 465 config->redirect_follow = true; 466 } else if (strcmp(mode, "nofollow") != 0) { 467 pr_err("overlayfs: bad mount option \"redirect_dir=%s\"\n", 468 mode); 469 return -EINVAL; 470 } 471 472 return 0; 473 } 474 475 static int ovl_parse_opt(char *opt, struct ovl_config *config) 476 { 477 char *p; 478 int err; 479 bool metacopy_opt = false, redirect_opt = false; 480 481 config->redirect_mode = kstrdup(ovl_redirect_mode_def(), GFP_KERNEL); 482 if (!config->redirect_mode) 483 return -ENOMEM; 484 485 while ((p = ovl_next_opt(&opt)) != NULL) { 486 int token; 487 substring_t args[MAX_OPT_ARGS]; 488 489 if (!*p) 490 continue; 491 492 token = match_token(p, ovl_tokens, args); 493 switch (token) { 494 case OPT_UPPERDIR: 495 kfree(config->upperdir); 496 config->upperdir = match_strdup(&args[0]); 497 if (!config->upperdir) 498 return -ENOMEM; 499 break; 500 501 case OPT_LOWERDIR: 502 kfree(config->lowerdir); 503 config->lowerdir = match_strdup(&args[0]); 504 if (!config->lowerdir) 505 return -ENOMEM; 506 break; 507 508 case OPT_WORKDIR: 509 kfree(config->workdir); 510 config->workdir = match_strdup(&args[0]); 511 if (!config->workdir) 512 return -ENOMEM; 513 break; 514 515 case OPT_DEFAULT_PERMISSIONS: 516 config->default_permissions = true; 517 break; 518 519 case OPT_REDIRECT_DIR: 520 kfree(config->redirect_mode); 521 config->redirect_mode = match_strdup(&args[0]); 522 if (!config->redirect_mode) 523 return -ENOMEM; 524 redirect_opt = true; 525 break; 526 527 case OPT_INDEX_ON: 528 config->index = true; 529 break; 530 531 case OPT_INDEX_OFF: 532 config->index = false; 533 break; 534 535 case OPT_NFS_EXPORT_ON: 536 config->nfs_export = true; 537 break; 538 539 case OPT_NFS_EXPORT_OFF: 540 config->nfs_export = false; 541 break; 542 543 case OPT_XINO_ON: 544 config->xino = OVL_XINO_ON; 545 break; 546 547 case OPT_XINO_OFF: 548 config->xino = OVL_XINO_OFF; 549 break; 550 551 case OPT_XINO_AUTO: 552 config->xino = OVL_XINO_AUTO; 553 break; 554 555 case OPT_METACOPY_ON: 556 config->metacopy = true; 557 metacopy_opt = true; 558 break; 559 560 case OPT_METACOPY_OFF: 561 config->metacopy = false; 562 break; 563 564 default: 565 pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p); 566 return -EINVAL; 567 } 568 } 569 570 /* Workdir is useless in non-upper mount */ 571 if (!config->upperdir && config->workdir) { 572 pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n", 573 config->workdir); 574 kfree(config->workdir); 575 config->workdir = NULL; 576 } 577 578 err = ovl_parse_redirect_mode(config, config->redirect_mode); 579 if (err) 580 return err; 581 582 /* 583 * This is to make the logic below simpler. It doesn't make any other 584 * difference, since config->redirect_dir is only used for upper. 585 */ 586 if (!config->upperdir && config->redirect_follow) 587 config->redirect_dir = true; 588 589 /* Resolve metacopy -> redirect_dir dependency */ 590 if (config->metacopy && !config->redirect_dir) { 591 if (metacopy_opt && redirect_opt) { 592 pr_err("overlayfs: conflicting options: metacopy=on,redirect_dir=%s\n", 593 config->redirect_mode); 594 return -EINVAL; 595 } 596 if (redirect_opt) { 597 /* 598 * There was an explicit redirect_dir=... that resulted 599 * in this conflict. 600 */ 601 pr_info("overlayfs: disabling metacopy due to redirect_dir=%s\n", 602 config->redirect_mode); 603 config->metacopy = false; 604 } else { 605 /* Automatically enable redirect otherwise. */ 606 config->redirect_follow = config->redirect_dir = true; 607 } 608 } 609 610 return 0; 611 } 612 613 #define OVL_WORKDIR_NAME "work" 614 #define OVL_INDEXDIR_NAME "index" 615 616 static struct dentry *ovl_workdir_create(struct ovl_fs *ofs, 617 const char *name, bool persist) 618 { 619 struct inode *dir = ofs->workbasedir->d_inode; 620 struct vfsmount *mnt = ofs->upper_mnt; 621 struct dentry *work; 622 int err; 623 bool retried = false; 624 bool locked = false; 625 626 inode_lock_nested(dir, I_MUTEX_PARENT); 627 locked = true; 628 629 retry: 630 work = lookup_one_len(name, ofs->workbasedir, strlen(name)); 631 632 if (!IS_ERR(work)) { 633 struct iattr attr = { 634 .ia_valid = ATTR_MODE, 635 .ia_mode = S_IFDIR | 0, 636 }; 637 638 if (work->d_inode) { 639 err = -EEXIST; 640 if (retried) 641 goto out_dput; 642 643 if (persist) 644 goto out_unlock; 645 646 retried = true; 647 ovl_workdir_cleanup(dir, mnt, work, 0); 648 dput(work); 649 goto retry; 650 } 651 652 work = ovl_create_real(dir, work, OVL_CATTR(attr.ia_mode)); 653 err = PTR_ERR(work); 654 if (IS_ERR(work)) 655 goto out_err; 656 657 /* 658 * Try to remove POSIX ACL xattrs from workdir. We are good if: 659 * 660 * a) success (there was a POSIX ACL xattr and was removed) 661 * b) -ENODATA (there was no POSIX ACL xattr) 662 * c) -EOPNOTSUPP (POSIX ACL xattrs are not supported) 663 * 664 * There are various other error values that could effectively 665 * mean that the xattr doesn't exist (e.g. -ERANGE is returned 666 * if the xattr name is too long), but the set of filesystems 667 * allowed as upper are limited to "normal" ones, where checking 668 * for the above two errors is sufficient. 669 */ 670 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_DEFAULT); 671 if (err && err != -ENODATA && err != -EOPNOTSUPP) 672 goto out_dput; 673 674 err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_ACCESS); 675 if (err && err != -ENODATA && err != -EOPNOTSUPP) 676 goto out_dput; 677 678 /* Clear any inherited mode bits */ 679 inode_lock(work->d_inode); 680 err = notify_change(work, &attr, NULL); 681 inode_unlock(work->d_inode); 682 if (err) 683 goto out_dput; 684 } else { 685 err = PTR_ERR(work); 686 goto out_err; 687 } 688 out_unlock: 689 if (locked) 690 inode_unlock(dir); 691 692 return work; 693 694 out_dput: 695 dput(work); 696 out_err: 697 pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n", 698 ofs->config.workdir, name, -err); 699 work = NULL; 700 goto out_unlock; 701 } 702 703 static void ovl_unescape(char *s) 704 { 705 char *d = s; 706 707 for (;; s++, d++) { 708 if (*s == '\\') 709 s++; 710 *d = *s; 711 if (!*s) 712 break; 713 } 714 } 715 716 static int ovl_mount_dir_noesc(const char *name, struct path *path) 717 { 718 int err = -EINVAL; 719 720 if (!*name) { 721 pr_err("overlayfs: empty lowerdir\n"); 722 goto out; 723 } 724 err = kern_path(name, LOOKUP_FOLLOW, path); 725 if (err) { 726 pr_err("overlayfs: failed to resolve '%s': %i\n", name, err); 727 goto out; 728 } 729 err = -EINVAL; 730 if (ovl_dentry_weird(path->dentry)) { 731 pr_err("overlayfs: filesystem on '%s' not supported\n", name); 732 goto out_put; 733 } 734 if (!d_is_dir(path->dentry)) { 735 pr_err("overlayfs: '%s' not a directory\n", name); 736 goto out_put; 737 } 738 return 0; 739 740 out_put: 741 path_put_init(path); 742 out: 743 return err; 744 } 745 746 static int ovl_mount_dir(const char *name, struct path *path) 747 { 748 int err = -ENOMEM; 749 char *tmp = kstrdup(name, GFP_KERNEL); 750 751 if (tmp) { 752 ovl_unescape(tmp); 753 err = ovl_mount_dir_noesc(tmp, path); 754 755 if (!err) 756 if (ovl_dentry_remote(path->dentry)) { 757 pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n", 758 tmp); 759 path_put_init(path); 760 err = -EINVAL; 761 } 762 kfree(tmp); 763 } 764 return err; 765 } 766 767 static int ovl_check_namelen(struct path *path, struct ovl_fs *ofs, 768 const char *name) 769 { 770 struct kstatfs statfs; 771 int err = vfs_statfs(path, &statfs); 772 773 if (err) 774 pr_err("overlayfs: statfs failed on '%s'\n", name); 775 else 776 ofs->namelen = max(ofs->namelen, statfs.f_namelen); 777 778 return err; 779 } 780 781 static int ovl_lower_dir(const char *name, struct path *path, 782 struct ovl_fs *ofs, int *stack_depth, bool *remote) 783 { 784 int fh_type; 785 int err; 786 787 err = ovl_mount_dir_noesc(name, path); 788 if (err) 789 goto out; 790 791 err = ovl_check_namelen(path, ofs, name); 792 if (err) 793 goto out_put; 794 795 *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth); 796 797 if (ovl_dentry_remote(path->dentry)) 798 *remote = true; 799 800 /* 801 * The inodes index feature and NFS export need to encode and decode 802 * file handles, so they require that all layers support them. 803 */ 804 fh_type = ovl_can_decode_fh(path->dentry->d_sb); 805 if ((ofs->config.nfs_export || 806 (ofs->config.index && ofs->config.upperdir)) && !fh_type) { 807 ofs->config.index = false; 808 ofs->config.nfs_export = false; 809 pr_warn("overlayfs: fs on '%s' does not support file handles, falling back to index=off,nfs_export=off.\n", 810 name); 811 } 812 813 /* Check if lower fs has 32bit inode numbers */ 814 if (fh_type != FILEID_INO32_GEN) 815 ofs->xino_bits = 0; 816 817 return 0; 818 819 out_put: 820 path_put_init(path); 821 out: 822 return err; 823 } 824 825 /* Workdir should not be subdir of upperdir and vice versa */ 826 static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir) 827 { 828 bool ok = false; 829 830 if (workdir != upperdir) { 831 ok = (lock_rename(workdir, upperdir) == NULL); 832 unlock_rename(workdir, upperdir); 833 } 834 return ok; 835 } 836 837 static unsigned int ovl_split_lowerdirs(char *str) 838 { 839 unsigned int ctr = 1; 840 char *s, *d; 841 842 for (s = d = str;; s++, d++) { 843 if (*s == '\\') { 844 s++; 845 } else if (*s == ':') { 846 *d = '\0'; 847 ctr++; 848 continue; 849 } 850 *d = *s; 851 if (!*s) 852 break; 853 } 854 return ctr; 855 } 856 857 static int __maybe_unused 858 ovl_posix_acl_xattr_get(const struct xattr_handler *handler, 859 struct dentry *dentry, struct inode *inode, 860 const char *name, void *buffer, size_t size) 861 { 862 return ovl_xattr_get(dentry, inode, handler->name, buffer, size); 863 } 864 865 static int __maybe_unused 866 ovl_posix_acl_xattr_set(const struct xattr_handler *handler, 867 struct dentry *dentry, struct inode *inode, 868 const char *name, const void *value, 869 size_t size, int flags) 870 { 871 struct dentry *workdir = ovl_workdir(dentry); 872 struct inode *realinode = ovl_inode_real(inode); 873 struct posix_acl *acl = NULL; 874 int err; 875 876 /* Check that everything is OK before copy-up */ 877 if (value) { 878 acl = posix_acl_from_xattr(&init_user_ns, value, size); 879 if (IS_ERR(acl)) 880 return PTR_ERR(acl); 881 } 882 err = -EOPNOTSUPP; 883 if (!IS_POSIXACL(d_inode(workdir))) 884 goto out_acl_release; 885 if (!realinode->i_op->set_acl) 886 goto out_acl_release; 887 if (handler->flags == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode)) { 888 err = acl ? -EACCES : 0; 889 goto out_acl_release; 890 } 891 err = -EPERM; 892 if (!inode_owner_or_capable(inode)) 893 goto out_acl_release; 894 895 posix_acl_release(acl); 896 897 /* 898 * Check if sgid bit needs to be cleared (actual setacl operation will 899 * be done with mounter's capabilities and so that won't do it for us). 900 */ 901 if (unlikely(inode->i_mode & S_ISGID) && 902 handler->flags == ACL_TYPE_ACCESS && 903 !in_group_p(inode->i_gid) && 904 !capable_wrt_inode_uidgid(inode, CAP_FSETID)) { 905 struct iattr iattr = { .ia_valid = ATTR_KILL_SGID }; 906 907 err = ovl_setattr(dentry, &iattr); 908 if (err) 909 return err; 910 } 911 912 err = ovl_xattr_set(dentry, inode, handler->name, value, size, flags); 913 if (!err) 914 ovl_copyattr(ovl_inode_real(inode), inode); 915 916 return err; 917 918 out_acl_release: 919 posix_acl_release(acl); 920 return err; 921 } 922 923 static int ovl_own_xattr_get(const struct xattr_handler *handler, 924 struct dentry *dentry, struct inode *inode, 925 const char *name, void *buffer, size_t size) 926 { 927 return -EOPNOTSUPP; 928 } 929 930 static int ovl_own_xattr_set(const struct xattr_handler *handler, 931 struct dentry *dentry, struct inode *inode, 932 const char *name, const void *value, 933 size_t size, int flags) 934 { 935 return -EOPNOTSUPP; 936 } 937 938 static int ovl_other_xattr_get(const struct xattr_handler *handler, 939 struct dentry *dentry, struct inode *inode, 940 const char *name, void *buffer, size_t size) 941 { 942 return ovl_xattr_get(dentry, inode, name, buffer, size); 943 } 944 945 static int ovl_other_xattr_set(const struct xattr_handler *handler, 946 struct dentry *dentry, struct inode *inode, 947 const char *name, const void *value, 948 size_t size, int flags) 949 { 950 return ovl_xattr_set(dentry, inode, name, value, size, flags); 951 } 952 953 static const struct xattr_handler __maybe_unused 954 ovl_posix_acl_access_xattr_handler = { 955 .name = XATTR_NAME_POSIX_ACL_ACCESS, 956 .flags = ACL_TYPE_ACCESS, 957 .get = ovl_posix_acl_xattr_get, 958 .set = ovl_posix_acl_xattr_set, 959 }; 960 961 static const struct xattr_handler __maybe_unused 962 ovl_posix_acl_default_xattr_handler = { 963 .name = XATTR_NAME_POSIX_ACL_DEFAULT, 964 .flags = ACL_TYPE_DEFAULT, 965 .get = ovl_posix_acl_xattr_get, 966 .set = ovl_posix_acl_xattr_set, 967 }; 968 969 static const struct xattr_handler ovl_own_xattr_handler = { 970 .prefix = OVL_XATTR_PREFIX, 971 .get = ovl_own_xattr_get, 972 .set = ovl_own_xattr_set, 973 }; 974 975 static const struct xattr_handler ovl_other_xattr_handler = { 976 .prefix = "", /* catch all */ 977 .get = ovl_other_xattr_get, 978 .set = ovl_other_xattr_set, 979 }; 980 981 static const struct xattr_handler *ovl_xattr_handlers[] = { 982 #ifdef CONFIG_FS_POSIX_ACL 983 &ovl_posix_acl_access_xattr_handler, 984 &ovl_posix_acl_default_xattr_handler, 985 #endif 986 &ovl_own_xattr_handler, 987 &ovl_other_xattr_handler, 988 NULL 989 }; 990 991 static int ovl_setup_trap(struct super_block *sb, struct dentry *dir, 992 struct inode **ptrap, const char *name) 993 { 994 struct inode *trap; 995 int err; 996 997 trap = ovl_get_trap_inode(sb, dir); 998 err = PTR_ERR(trap); 999 if (IS_ERR(trap)) { 1000 if (err == -ELOOP) 1001 pr_err("overlayfs: conflicting %s path\n", name); 1002 return err; 1003 } 1004 1005 *ptrap = trap; 1006 return 0; 1007 } 1008 1009 static int ovl_get_upper(struct super_block *sb, struct ovl_fs *ofs, 1010 struct path *upperpath) 1011 { 1012 struct vfsmount *upper_mnt; 1013 int err; 1014 1015 err = ovl_mount_dir(ofs->config.upperdir, upperpath); 1016 if (err) 1017 goto out; 1018 1019 /* Upper fs should not be r/o */ 1020 if (sb_rdonly(upperpath->mnt->mnt_sb)) { 1021 pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n"); 1022 err = -EINVAL; 1023 goto out; 1024 } 1025 1026 err = ovl_check_namelen(upperpath, ofs, ofs->config.upperdir); 1027 if (err) 1028 goto out; 1029 1030 err = ovl_setup_trap(sb, upperpath->dentry, &ofs->upperdir_trap, 1031 "upperdir"); 1032 if (err) 1033 goto out; 1034 1035 upper_mnt = clone_private_mount(upperpath); 1036 err = PTR_ERR(upper_mnt); 1037 if (IS_ERR(upper_mnt)) { 1038 pr_err("overlayfs: failed to clone upperpath\n"); 1039 goto out; 1040 } 1041 1042 /* Don't inherit atime flags */ 1043 upper_mnt->mnt_flags &= ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME); 1044 ofs->upper_mnt = upper_mnt; 1045 1046 err = -EBUSY; 1047 if (ovl_inuse_trylock(ofs->upper_mnt->mnt_root)) { 1048 ofs->upperdir_locked = true; 1049 } else if (ofs->config.index) { 1050 pr_err("overlayfs: upperdir is in-use by another mount, mount with '-o index=off' to override exclusive upperdir protection.\n"); 1051 goto out; 1052 } else { 1053 pr_warn("overlayfs: upperdir is in-use by another mount, accessing files from both mounts will result in undefined behavior.\n"); 1054 } 1055 1056 err = 0; 1057 out: 1058 return err; 1059 } 1060 1061 static int ovl_make_workdir(struct super_block *sb, struct ovl_fs *ofs, 1062 struct path *workpath) 1063 { 1064 struct vfsmount *mnt = ofs->upper_mnt; 1065 struct dentry *temp; 1066 int fh_type; 1067 int err; 1068 1069 err = mnt_want_write(mnt); 1070 if (err) 1071 return err; 1072 1073 ofs->workdir = ovl_workdir_create(ofs, OVL_WORKDIR_NAME, false); 1074 if (!ofs->workdir) 1075 goto out; 1076 1077 err = ovl_setup_trap(sb, ofs->workdir, &ofs->workdir_trap, "workdir"); 1078 if (err) 1079 goto out; 1080 1081 /* 1082 * Upper should support d_type, else whiteouts are visible. Given 1083 * workdir and upper are on same fs, we can do iterate_dir() on 1084 * workdir. This check requires successful creation of workdir in 1085 * previous step. 1086 */ 1087 err = ovl_check_d_type_supported(workpath); 1088 if (err < 0) 1089 goto out; 1090 1091 /* 1092 * We allowed this configuration and don't want to break users over 1093 * kernel upgrade. So warn instead of erroring out. 1094 */ 1095 if (!err) 1096 pr_warn("overlayfs: upper fs needs to support d_type.\n"); 1097 1098 /* Check if upper/work fs supports O_TMPFILE */ 1099 temp = ovl_do_tmpfile(ofs->workdir, S_IFREG | 0); 1100 ofs->tmpfile = !IS_ERR(temp); 1101 if (ofs->tmpfile) 1102 dput(temp); 1103 else 1104 pr_warn("overlayfs: upper fs does not support tmpfile.\n"); 1105 1106 /* 1107 * Check if upper/work fs supports trusted.overlay.* xattr 1108 */ 1109 err = ovl_do_setxattr(ofs->workdir, OVL_XATTR_OPAQUE, "0", 1, 0); 1110 if (err) { 1111 ofs->noxattr = true; 1112 ofs->config.index = false; 1113 ofs->config.metacopy = false; 1114 pr_warn("overlayfs: upper fs does not support xattr, falling back to index=off and metacopy=off.\n"); 1115 err = 0; 1116 } else { 1117 vfs_removexattr(ofs->workdir, OVL_XATTR_OPAQUE); 1118 } 1119 1120 /* Check if upper/work fs supports file handles */ 1121 fh_type = ovl_can_decode_fh(ofs->workdir->d_sb); 1122 if (ofs->config.index && !fh_type) { 1123 ofs->config.index = false; 1124 pr_warn("overlayfs: upper fs does not support file handles, falling back to index=off.\n"); 1125 } 1126 1127 /* Check if upper fs has 32bit inode numbers */ 1128 if (fh_type != FILEID_INO32_GEN) 1129 ofs->xino_bits = 0; 1130 1131 /* NFS export of r/w mount depends on index */ 1132 if (ofs->config.nfs_export && !ofs->config.index) { 1133 pr_warn("overlayfs: NFS export requires \"index=on\", falling back to nfs_export=off.\n"); 1134 ofs->config.nfs_export = false; 1135 } 1136 out: 1137 mnt_drop_write(mnt); 1138 return err; 1139 } 1140 1141 static int ovl_get_workdir(struct super_block *sb, struct ovl_fs *ofs, 1142 struct path *upperpath) 1143 { 1144 int err; 1145 struct path workpath = { }; 1146 1147 err = ovl_mount_dir(ofs->config.workdir, &workpath); 1148 if (err) 1149 goto out; 1150 1151 err = -EINVAL; 1152 if (upperpath->mnt != workpath.mnt) { 1153 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n"); 1154 goto out; 1155 } 1156 if (!ovl_workdir_ok(workpath.dentry, upperpath->dentry)) { 1157 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n"); 1158 goto out; 1159 } 1160 1161 ofs->workbasedir = dget(workpath.dentry); 1162 1163 err = -EBUSY; 1164 if (ovl_inuse_trylock(ofs->workbasedir)) { 1165 ofs->workdir_locked = true; 1166 } else if (ofs->config.index) { 1167 pr_err("overlayfs: workdir is in-use by another mount, mount with '-o index=off' to override exclusive workdir protection.\n"); 1168 goto out; 1169 } else { 1170 pr_warn("overlayfs: workdir is in-use by another mount, accessing files from both mounts will result in undefined behavior.\n"); 1171 } 1172 1173 err = ovl_make_workdir(sb, ofs, &workpath); 1174 1175 out: 1176 path_put(&workpath); 1177 1178 return err; 1179 } 1180 1181 static int ovl_get_indexdir(struct super_block *sb, struct ovl_fs *ofs, 1182 struct ovl_entry *oe, struct path *upperpath) 1183 { 1184 struct vfsmount *mnt = ofs->upper_mnt; 1185 int err; 1186 1187 err = mnt_want_write(mnt); 1188 if (err) 1189 return err; 1190 1191 /* Verify lower root is upper root origin */ 1192 err = ovl_verify_origin(upperpath->dentry, oe->lowerstack[0].dentry, 1193 true); 1194 if (err) { 1195 pr_err("overlayfs: failed to verify upper root origin\n"); 1196 goto out; 1197 } 1198 1199 ofs->indexdir = ovl_workdir_create(ofs, OVL_INDEXDIR_NAME, true); 1200 if (ofs->indexdir) { 1201 err = ovl_setup_trap(sb, ofs->indexdir, &ofs->indexdir_trap, 1202 "indexdir"); 1203 if (err) 1204 goto out; 1205 1206 /* 1207 * Verify upper root is exclusively associated with index dir. 1208 * Older kernels stored upper fh in "trusted.overlay.origin" 1209 * xattr. If that xattr exists, verify that it is a match to 1210 * upper dir file handle. In any case, verify or set xattr 1211 * "trusted.overlay.upper" to indicate that index may have 1212 * directory entries. 1213 */ 1214 if (ovl_check_origin_xattr(ofs->indexdir)) { 1215 err = ovl_verify_set_fh(ofs->indexdir, OVL_XATTR_ORIGIN, 1216 upperpath->dentry, true, false); 1217 if (err) 1218 pr_err("overlayfs: failed to verify index dir 'origin' xattr\n"); 1219 } 1220 err = ovl_verify_upper(ofs->indexdir, upperpath->dentry, true); 1221 if (err) 1222 pr_err("overlayfs: failed to verify index dir 'upper' xattr\n"); 1223 1224 /* Cleanup bad/stale/orphan index entries */ 1225 if (!err) 1226 err = ovl_indexdir_cleanup(ofs); 1227 } 1228 if (err || !ofs->indexdir) 1229 pr_warn("overlayfs: try deleting index dir or mounting with '-o index=off' to disable inodes index.\n"); 1230 1231 out: 1232 mnt_drop_write(mnt); 1233 return err; 1234 } 1235 1236 static bool ovl_lower_uuid_ok(struct ovl_fs *ofs, const uuid_t *uuid) 1237 { 1238 unsigned int i; 1239 1240 if (!ofs->config.nfs_export && !(ofs->config.index && ofs->upper_mnt)) 1241 return true; 1242 1243 for (i = 0; i < ofs->numlowerfs; i++) { 1244 /* 1245 * We use uuid to associate an overlay lower file handle with a 1246 * lower layer, so we can accept lower fs with null uuid as long 1247 * as all lower layers with null uuid are on the same fs. 1248 */ 1249 if (uuid_equal(&ofs->lower_fs[i].sb->s_uuid, uuid)) 1250 return false; 1251 } 1252 return true; 1253 } 1254 1255 /* Get a unique fsid for the layer */ 1256 static int ovl_get_fsid(struct ovl_fs *ofs, const struct path *path) 1257 { 1258 struct super_block *sb = path->mnt->mnt_sb; 1259 unsigned int i; 1260 dev_t dev; 1261 int err; 1262 1263 /* fsid 0 is reserved for upper fs even with non upper overlay */ 1264 if (ofs->upper_mnt && ofs->upper_mnt->mnt_sb == sb) 1265 return 0; 1266 1267 for (i = 0; i < ofs->numlowerfs; i++) { 1268 if (ofs->lower_fs[i].sb == sb) 1269 return i + 1; 1270 } 1271 1272 if (!ovl_lower_uuid_ok(ofs, &sb->s_uuid)) { 1273 ofs->config.index = false; 1274 ofs->config.nfs_export = false; 1275 pr_warn("overlayfs: %s uuid detected in lower fs '%pd2', falling back to index=off,nfs_export=off.\n", 1276 uuid_is_null(&sb->s_uuid) ? "null" : "conflicting", 1277 path->dentry); 1278 } 1279 1280 err = get_anon_bdev(&dev); 1281 if (err) { 1282 pr_err("overlayfs: failed to get anonymous bdev for lowerpath\n"); 1283 return err; 1284 } 1285 1286 ofs->lower_fs[ofs->numlowerfs].sb = sb; 1287 ofs->lower_fs[ofs->numlowerfs].pseudo_dev = dev; 1288 ofs->numlowerfs++; 1289 1290 return ofs->numlowerfs; 1291 } 1292 1293 static int ovl_get_lower_layers(struct super_block *sb, struct ovl_fs *ofs, 1294 struct path *stack, unsigned int numlower) 1295 { 1296 int err; 1297 unsigned int i; 1298 1299 err = -ENOMEM; 1300 ofs->lower_layers = kcalloc(numlower, sizeof(struct ovl_layer), 1301 GFP_KERNEL); 1302 if (ofs->lower_layers == NULL) 1303 goto out; 1304 1305 ofs->lower_fs = kcalloc(numlower, sizeof(struct ovl_sb), 1306 GFP_KERNEL); 1307 if (ofs->lower_fs == NULL) 1308 goto out; 1309 1310 for (i = 0; i < numlower; i++) { 1311 struct vfsmount *mnt; 1312 struct inode *trap; 1313 int fsid; 1314 1315 err = fsid = ovl_get_fsid(ofs, &stack[i]); 1316 if (err < 0) 1317 goto out; 1318 1319 err = -EBUSY; 1320 if (ovl_is_inuse(stack[i].dentry)) { 1321 pr_err("overlayfs: lowerdir is in-use as upperdir/workdir\n"); 1322 goto out; 1323 } 1324 1325 err = ovl_setup_trap(sb, stack[i].dentry, &trap, "lowerdir"); 1326 if (err) 1327 goto out; 1328 1329 mnt = clone_private_mount(&stack[i]); 1330 err = PTR_ERR(mnt); 1331 if (IS_ERR(mnt)) { 1332 pr_err("overlayfs: failed to clone lowerpath\n"); 1333 iput(trap); 1334 goto out; 1335 } 1336 1337 /* 1338 * Make lower layers R/O. That way fchmod/fchown on lower file 1339 * will fail instead of modifying lower fs. 1340 */ 1341 mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME; 1342 1343 ofs->lower_layers[ofs->numlower].trap = trap; 1344 ofs->lower_layers[ofs->numlower].mnt = mnt; 1345 ofs->lower_layers[ofs->numlower].idx = i + 1; 1346 ofs->lower_layers[ofs->numlower].fsid = fsid; 1347 if (fsid) { 1348 ofs->lower_layers[ofs->numlower].fs = 1349 &ofs->lower_fs[fsid - 1]; 1350 } 1351 ofs->numlower++; 1352 } 1353 1354 /* 1355 * When all layers on same fs, overlay can use real inode numbers. 1356 * With mount option "xino=on", mounter declares that there are enough 1357 * free high bits in underlying fs to hold the unique fsid. 1358 * If overlayfs does encounter underlying inodes using the high xino 1359 * bits reserved for fsid, it emits a warning and uses the original 1360 * inode number. 1361 */ 1362 if (!ofs->numlowerfs || (ofs->numlowerfs == 1 && !ofs->upper_mnt)) { 1363 ofs->xino_bits = 0; 1364 ofs->config.xino = OVL_XINO_OFF; 1365 } else if (ofs->config.xino == OVL_XINO_ON && !ofs->xino_bits) { 1366 /* 1367 * This is a roundup of number of bits needed for numlowerfs+1 1368 * (i.e. ilog2(numlowerfs+1 - 1) + 1). fsid 0 is reserved for 1369 * upper fs even with non upper overlay. 1370 */ 1371 BUILD_BUG_ON(ilog2(OVL_MAX_STACK) > 31); 1372 ofs->xino_bits = ilog2(ofs->numlowerfs) + 1; 1373 } 1374 1375 if (ofs->xino_bits) { 1376 pr_info("overlayfs: \"xino\" feature enabled using %d upper inode bits.\n", 1377 ofs->xino_bits); 1378 } 1379 1380 err = 0; 1381 out: 1382 return err; 1383 } 1384 1385 static struct ovl_entry *ovl_get_lowerstack(struct super_block *sb, 1386 struct ovl_fs *ofs) 1387 { 1388 int err; 1389 char *lowertmp, *lower; 1390 struct path *stack = NULL; 1391 unsigned int stacklen, numlower = 0, i; 1392 bool remote = false; 1393 struct ovl_entry *oe; 1394 1395 err = -ENOMEM; 1396 lowertmp = kstrdup(ofs->config.lowerdir, GFP_KERNEL); 1397 if (!lowertmp) 1398 goto out_err; 1399 1400 err = -EINVAL; 1401 stacklen = ovl_split_lowerdirs(lowertmp); 1402 if (stacklen > OVL_MAX_STACK) { 1403 pr_err("overlayfs: too many lower directories, limit is %d\n", 1404 OVL_MAX_STACK); 1405 goto out_err; 1406 } else if (!ofs->config.upperdir && stacklen == 1) { 1407 pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n"); 1408 goto out_err; 1409 } else if (!ofs->config.upperdir && ofs->config.nfs_export && 1410 ofs->config.redirect_follow) { 1411 pr_warn("overlayfs: NFS export requires \"redirect_dir=nofollow\" on non-upper mount, falling back to nfs_export=off.\n"); 1412 ofs->config.nfs_export = false; 1413 } 1414 1415 err = -ENOMEM; 1416 stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL); 1417 if (!stack) 1418 goto out_err; 1419 1420 err = -EINVAL; 1421 lower = lowertmp; 1422 for (numlower = 0; numlower < stacklen; numlower++) { 1423 err = ovl_lower_dir(lower, &stack[numlower], ofs, 1424 &sb->s_stack_depth, &remote); 1425 if (err) 1426 goto out_err; 1427 1428 lower = strchr(lower, '\0') + 1; 1429 } 1430 1431 err = -EINVAL; 1432 sb->s_stack_depth++; 1433 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) { 1434 pr_err("overlayfs: maximum fs stacking depth exceeded\n"); 1435 goto out_err; 1436 } 1437 1438 err = ovl_get_lower_layers(sb, ofs, stack, numlower); 1439 if (err) 1440 goto out_err; 1441 1442 err = -ENOMEM; 1443 oe = ovl_alloc_entry(numlower); 1444 if (!oe) 1445 goto out_err; 1446 1447 for (i = 0; i < numlower; i++) { 1448 oe->lowerstack[i].dentry = dget(stack[i].dentry); 1449 oe->lowerstack[i].layer = &ofs->lower_layers[i]; 1450 } 1451 1452 if (remote) 1453 sb->s_d_op = &ovl_reval_dentry_operations; 1454 else 1455 sb->s_d_op = &ovl_dentry_operations; 1456 1457 out: 1458 for (i = 0; i < numlower; i++) 1459 path_put(&stack[i]); 1460 kfree(stack); 1461 kfree(lowertmp); 1462 1463 return oe; 1464 1465 out_err: 1466 oe = ERR_PTR(err); 1467 goto out; 1468 } 1469 1470 /* 1471 * Check if this layer root is a descendant of: 1472 * - another layer of this overlayfs instance 1473 * - upper/work dir of any overlayfs instance 1474 * - a disconnected dentry (detached root) 1475 */ 1476 static int ovl_check_layer(struct super_block *sb, struct dentry *dentry, 1477 const char *name) 1478 { 1479 struct dentry *next, *parent; 1480 bool is_root = false; 1481 int err = 0; 1482 1483 if (!dentry || dentry == dentry->d_sb->s_root) 1484 return 0; 1485 1486 next = dget(dentry); 1487 /* Walk back ancestors to fs root (inclusive) looking for traps */ 1488 do { 1489 parent = dget_parent(next); 1490 is_root = (parent == next); 1491 if (ovl_is_inuse(parent)) { 1492 err = -EBUSY; 1493 pr_err("overlayfs: %s path overlapping in-use upperdir/workdir\n", 1494 name); 1495 } else if (ovl_lookup_trap_inode(sb, parent)) { 1496 err = -ELOOP; 1497 pr_err("overlayfs: overlapping %s path\n", name); 1498 } 1499 dput(next); 1500 next = parent; 1501 } while (!err && !is_root); 1502 1503 /* Did we really walk to fs root or found a detached root? */ 1504 if (!err && next != dentry->d_sb->s_root) { 1505 err = -ESTALE; 1506 pr_err("overlayfs: disconnected %s path\n", name); 1507 } 1508 1509 dput(next); 1510 1511 return err; 1512 } 1513 1514 /* 1515 * Check if any of the layers or work dirs overlap. 1516 */ 1517 static int ovl_check_overlapping_layers(struct super_block *sb, 1518 struct ovl_fs *ofs) 1519 { 1520 int i, err; 1521 1522 if (ofs->upper_mnt) { 1523 err = ovl_check_layer(sb, ofs->upper_mnt->mnt_root, "upperdir"); 1524 if (err) 1525 return err; 1526 1527 /* 1528 * Checking workbasedir avoids hitting ovl_is_inuse(parent) of 1529 * this instance and covers overlapping work and index dirs, 1530 * unless work or index dir have been moved since created inside 1531 * workbasedir. In that case, we already have their traps in 1532 * inode cache and we will catch that case on lookup. 1533 */ 1534 err = ovl_check_layer(sb, ofs->workbasedir, "workdir"); 1535 if (err) 1536 return err; 1537 } 1538 1539 for (i = 0; i < ofs->numlower; i++) { 1540 err = ovl_check_layer(sb, ofs->lower_layers[i].mnt->mnt_root, 1541 "lowerdir"); 1542 if (err) 1543 return err; 1544 } 1545 1546 return 0; 1547 } 1548 1549 static int ovl_fill_super(struct super_block *sb, void *data, int silent) 1550 { 1551 struct path upperpath = { }; 1552 struct dentry *root_dentry; 1553 struct ovl_entry *oe; 1554 struct ovl_fs *ofs; 1555 struct cred *cred; 1556 int err; 1557 1558 err = -ENOMEM; 1559 ofs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL); 1560 if (!ofs) 1561 goto out; 1562 1563 ofs->creator_cred = cred = prepare_creds(); 1564 if (!cred) 1565 goto out_err; 1566 1567 ofs->config.index = ovl_index_def; 1568 ofs->config.nfs_export = ovl_nfs_export_def; 1569 ofs->config.xino = ovl_xino_def(); 1570 ofs->config.metacopy = ovl_metacopy_def; 1571 err = ovl_parse_opt((char *) data, &ofs->config); 1572 if (err) 1573 goto out_err; 1574 1575 err = -EINVAL; 1576 if (!ofs->config.lowerdir) { 1577 if (!silent) 1578 pr_err("overlayfs: missing 'lowerdir'\n"); 1579 goto out_err; 1580 } 1581 1582 sb->s_stack_depth = 0; 1583 sb->s_maxbytes = MAX_LFS_FILESIZE; 1584 /* Assume underlaying fs uses 32bit inodes unless proven otherwise */ 1585 if (ofs->config.xino != OVL_XINO_OFF) 1586 ofs->xino_bits = BITS_PER_LONG - 32; 1587 1588 /* alloc/destroy_inode needed for setting up traps in inode cache */ 1589 sb->s_op = &ovl_super_operations; 1590 1591 if (ofs->config.upperdir) { 1592 if (!ofs->config.workdir) { 1593 pr_err("overlayfs: missing 'workdir'\n"); 1594 goto out_err; 1595 } 1596 1597 err = ovl_get_upper(sb, ofs, &upperpath); 1598 if (err) 1599 goto out_err; 1600 1601 err = ovl_get_workdir(sb, ofs, &upperpath); 1602 if (err) 1603 goto out_err; 1604 1605 if (!ofs->workdir) 1606 sb->s_flags |= SB_RDONLY; 1607 1608 sb->s_stack_depth = ofs->upper_mnt->mnt_sb->s_stack_depth; 1609 sb->s_time_gran = ofs->upper_mnt->mnt_sb->s_time_gran; 1610 1611 } 1612 oe = ovl_get_lowerstack(sb, ofs); 1613 err = PTR_ERR(oe); 1614 if (IS_ERR(oe)) 1615 goto out_err; 1616 1617 /* If the upper fs is nonexistent, we mark overlayfs r/o too */ 1618 if (!ofs->upper_mnt) 1619 sb->s_flags |= SB_RDONLY; 1620 1621 if (!(ovl_force_readonly(ofs)) && ofs->config.index) { 1622 err = ovl_get_indexdir(sb, ofs, oe, &upperpath); 1623 if (err) 1624 goto out_free_oe; 1625 1626 /* Force r/o mount with no index dir */ 1627 if (!ofs->indexdir) { 1628 dput(ofs->workdir); 1629 ofs->workdir = NULL; 1630 sb->s_flags |= SB_RDONLY; 1631 } 1632 1633 } 1634 1635 err = ovl_check_overlapping_layers(sb, ofs); 1636 if (err) 1637 goto out_free_oe; 1638 1639 /* Show index=off in /proc/mounts for forced r/o mount */ 1640 if (!ofs->indexdir) { 1641 ofs->config.index = false; 1642 if (ofs->upper_mnt && ofs->config.nfs_export) { 1643 pr_warn("overlayfs: NFS export requires an index dir, falling back to nfs_export=off.\n"); 1644 ofs->config.nfs_export = false; 1645 } 1646 } 1647 1648 if (ofs->config.metacopy && ofs->config.nfs_export) { 1649 pr_warn("overlayfs: NFS export is not supported with metadata only copy up, falling back to nfs_export=off.\n"); 1650 ofs->config.nfs_export = false; 1651 } 1652 1653 if (ofs->config.nfs_export) 1654 sb->s_export_op = &ovl_export_operations; 1655 1656 /* Never override disk quota limits or use reserved space */ 1657 cap_lower(cred->cap_effective, CAP_SYS_RESOURCE); 1658 1659 sb->s_magic = OVERLAYFS_SUPER_MAGIC; 1660 sb->s_xattr = ovl_xattr_handlers; 1661 sb->s_fs_info = ofs; 1662 sb->s_flags |= SB_POSIXACL; 1663 1664 err = -ENOMEM; 1665 root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, 0)); 1666 if (!root_dentry) 1667 goto out_free_oe; 1668 1669 root_dentry->d_fsdata = oe; 1670 1671 mntput(upperpath.mnt); 1672 if (upperpath.dentry) { 1673 ovl_dentry_set_upper_alias(root_dentry); 1674 if (ovl_is_impuredir(upperpath.dentry)) 1675 ovl_set_flag(OVL_IMPURE, d_inode(root_dentry)); 1676 } 1677 1678 /* Root is always merge -> can have whiteouts */ 1679 ovl_set_flag(OVL_WHITEOUTS, d_inode(root_dentry)); 1680 ovl_dentry_set_flag(OVL_E_CONNECTED, root_dentry); 1681 ovl_set_upperdata(d_inode(root_dentry)); 1682 ovl_inode_init(d_inode(root_dentry), upperpath.dentry, 1683 ovl_dentry_lower(root_dentry), NULL); 1684 1685 sb->s_root = root_dentry; 1686 1687 return 0; 1688 1689 out_free_oe: 1690 ovl_entry_stack_free(oe); 1691 kfree(oe); 1692 out_err: 1693 path_put(&upperpath); 1694 ovl_free_fs(ofs); 1695 out: 1696 return err; 1697 } 1698 1699 static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags, 1700 const char *dev_name, void *raw_data) 1701 { 1702 return mount_nodev(fs_type, flags, raw_data, ovl_fill_super); 1703 } 1704 1705 static struct file_system_type ovl_fs_type = { 1706 .owner = THIS_MODULE, 1707 .name = "overlay", 1708 .mount = ovl_mount, 1709 .kill_sb = kill_anon_super, 1710 }; 1711 MODULE_ALIAS_FS("overlay"); 1712 1713 static void ovl_inode_init_once(void *foo) 1714 { 1715 struct ovl_inode *oi = foo; 1716 1717 inode_init_once(&oi->vfs_inode); 1718 } 1719 1720 static int __init ovl_init(void) 1721 { 1722 int err; 1723 1724 ovl_inode_cachep = kmem_cache_create("ovl_inode", 1725 sizeof(struct ovl_inode), 0, 1726 (SLAB_RECLAIM_ACCOUNT| 1727 SLAB_MEM_SPREAD|SLAB_ACCOUNT), 1728 ovl_inode_init_once); 1729 if (ovl_inode_cachep == NULL) 1730 return -ENOMEM; 1731 1732 err = register_filesystem(&ovl_fs_type); 1733 if (err) 1734 kmem_cache_destroy(ovl_inode_cachep); 1735 1736 return err; 1737 } 1738 1739 static void __exit ovl_exit(void) 1740 { 1741 unregister_filesystem(&ovl_fs_type); 1742 1743 /* 1744 * Make sure all delayed rcu free inodes are flushed before we 1745 * destroy cache. 1746 */ 1747 rcu_barrier(); 1748 kmem_cache_destroy(ovl_inode_cachep); 1749 1750 } 1751 1752 module_init(ovl_init); 1753 module_exit(ovl_exit); 1754