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 <linux/fs.h> 11 #include <linux/namei.h> 12 #include <linux/pagemap.h> 13 #include <linux/xattr.h> 14 #include <linux/security.h> 15 #include <linux/mount.h> 16 #include <linux/slab.h> 17 #include <linux/parser.h> 18 #include <linux/module.h> 19 #include <linux/pagemap.h> 20 #include <linux/sched.h> 21 #include <linux/statfs.h> 22 #include <linux/seq_file.h> 23 #include "overlayfs.h" 24 25 MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>"); 26 MODULE_DESCRIPTION("Overlay filesystem"); 27 MODULE_LICENSE("GPL"); 28 29 struct ovl_config { 30 char *lowerdir; 31 char *upperdir; 32 char *workdir; 33 bool default_permissions; 34 }; 35 36 /* private information held for overlayfs's superblock */ 37 struct ovl_fs { 38 struct vfsmount *upper_mnt; 39 unsigned numlower; 40 struct vfsmount **lower_mnt; 41 struct dentry *workdir; 42 long lower_namelen; 43 /* pathnames of lower and upper dirs, for show_options */ 44 struct ovl_config config; 45 }; 46 47 struct ovl_dir_cache; 48 49 /* private information held for every overlayfs dentry */ 50 struct ovl_entry { 51 struct dentry *__upperdentry; 52 struct ovl_dir_cache *cache; 53 union { 54 struct { 55 u64 version; 56 bool opaque; 57 }; 58 struct rcu_head rcu; 59 }; 60 unsigned numlower; 61 struct path lowerstack[]; 62 }; 63 64 #define OVL_MAX_STACK 500 65 66 static struct dentry *__ovl_dentry_lower(struct ovl_entry *oe) 67 { 68 return oe->numlower ? oe->lowerstack[0].dentry : NULL; 69 } 70 71 enum ovl_path_type ovl_path_type(struct dentry *dentry) 72 { 73 struct ovl_entry *oe = dentry->d_fsdata; 74 enum ovl_path_type type = 0; 75 76 if (oe->__upperdentry) { 77 type = __OVL_PATH_UPPER; 78 79 /* 80 * Non-dir dentry can hold lower dentry from previous 81 * location. Its purity depends only on opaque flag. 82 */ 83 if (oe->numlower && S_ISDIR(dentry->d_inode->i_mode)) 84 type |= __OVL_PATH_MERGE; 85 else if (!oe->opaque) 86 type |= __OVL_PATH_PURE; 87 } else { 88 if (oe->numlower > 1) 89 type |= __OVL_PATH_MERGE; 90 } 91 return type; 92 } 93 94 static struct dentry *ovl_upperdentry_dereference(struct ovl_entry *oe) 95 { 96 return lockless_dereference(oe->__upperdentry); 97 } 98 99 void ovl_path_upper(struct dentry *dentry, struct path *path) 100 { 101 struct ovl_fs *ofs = dentry->d_sb->s_fs_info; 102 struct ovl_entry *oe = dentry->d_fsdata; 103 104 path->mnt = ofs->upper_mnt; 105 path->dentry = ovl_upperdentry_dereference(oe); 106 } 107 108 enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path) 109 { 110 enum ovl_path_type type = ovl_path_type(dentry); 111 112 if (!OVL_TYPE_UPPER(type)) 113 ovl_path_lower(dentry, path); 114 else 115 ovl_path_upper(dentry, path); 116 117 return type; 118 } 119 120 struct dentry *ovl_dentry_upper(struct dentry *dentry) 121 { 122 struct ovl_entry *oe = dentry->d_fsdata; 123 124 return ovl_upperdentry_dereference(oe); 125 } 126 127 struct dentry *ovl_dentry_lower(struct dentry *dentry) 128 { 129 struct ovl_entry *oe = dentry->d_fsdata; 130 131 return __ovl_dentry_lower(oe); 132 } 133 134 struct dentry *ovl_dentry_real(struct dentry *dentry) 135 { 136 struct ovl_entry *oe = dentry->d_fsdata; 137 struct dentry *realdentry; 138 139 realdentry = ovl_upperdentry_dereference(oe); 140 if (!realdentry) 141 realdentry = __ovl_dentry_lower(oe); 142 143 return realdentry; 144 } 145 146 struct dentry *ovl_entry_real(struct ovl_entry *oe, bool *is_upper) 147 { 148 struct dentry *realdentry; 149 150 realdentry = ovl_upperdentry_dereference(oe); 151 if (realdentry) { 152 *is_upper = true; 153 } else { 154 realdentry = __ovl_dentry_lower(oe); 155 *is_upper = false; 156 } 157 return realdentry; 158 } 159 160 struct vfsmount *ovl_entry_mnt_real(struct ovl_entry *oe, struct inode *inode, 161 bool is_upper) 162 { 163 if (is_upper) { 164 struct ovl_fs *ofs = inode->i_sb->s_fs_info; 165 166 return ofs->upper_mnt; 167 } else { 168 return oe->numlower ? oe->lowerstack[0].mnt : NULL; 169 } 170 } 171 172 struct ovl_dir_cache *ovl_dir_cache(struct dentry *dentry) 173 { 174 struct ovl_entry *oe = dentry->d_fsdata; 175 176 return oe->cache; 177 } 178 179 bool ovl_is_default_permissions(struct inode *inode) 180 { 181 struct ovl_fs *ofs = inode->i_sb->s_fs_info; 182 183 return ofs->config.default_permissions; 184 } 185 186 void ovl_set_dir_cache(struct dentry *dentry, struct ovl_dir_cache *cache) 187 { 188 struct ovl_entry *oe = dentry->d_fsdata; 189 190 oe->cache = cache; 191 } 192 193 void ovl_path_lower(struct dentry *dentry, struct path *path) 194 { 195 struct ovl_entry *oe = dentry->d_fsdata; 196 197 *path = oe->numlower ? oe->lowerstack[0] : (struct path) { NULL, NULL }; 198 } 199 200 int ovl_want_write(struct dentry *dentry) 201 { 202 struct ovl_fs *ofs = dentry->d_sb->s_fs_info; 203 return mnt_want_write(ofs->upper_mnt); 204 } 205 206 void ovl_drop_write(struct dentry *dentry) 207 { 208 struct ovl_fs *ofs = dentry->d_sb->s_fs_info; 209 mnt_drop_write(ofs->upper_mnt); 210 } 211 212 struct dentry *ovl_workdir(struct dentry *dentry) 213 { 214 struct ovl_fs *ofs = dentry->d_sb->s_fs_info; 215 return ofs->workdir; 216 } 217 218 bool ovl_dentry_is_opaque(struct dentry *dentry) 219 { 220 struct ovl_entry *oe = dentry->d_fsdata; 221 return oe->opaque; 222 } 223 224 void ovl_dentry_set_opaque(struct dentry *dentry, bool opaque) 225 { 226 struct ovl_entry *oe = dentry->d_fsdata; 227 oe->opaque = opaque; 228 } 229 230 void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry) 231 { 232 struct ovl_entry *oe = dentry->d_fsdata; 233 234 WARN_ON(!inode_is_locked(upperdentry->d_parent->d_inode)); 235 WARN_ON(oe->__upperdentry); 236 BUG_ON(!upperdentry->d_inode); 237 /* 238 * Make sure upperdentry is consistent before making it visible to 239 * ovl_upperdentry_dereference(). 240 */ 241 smp_wmb(); 242 oe->__upperdentry = upperdentry; 243 } 244 245 void ovl_dentry_version_inc(struct dentry *dentry) 246 { 247 struct ovl_entry *oe = dentry->d_fsdata; 248 249 WARN_ON(!inode_is_locked(dentry->d_inode)); 250 oe->version++; 251 } 252 253 u64 ovl_dentry_version_get(struct dentry *dentry) 254 { 255 struct ovl_entry *oe = dentry->d_fsdata; 256 257 WARN_ON(!inode_is_locked(dentry->d_inode)); 258 return oe->version; 259 } 260 261 bool ovl_is_whiteout(struct dentry *dentry) 262 { 263 struct inode *inode = dentry->d_inode; 264 265 return inode && IS_WHITEOUT(inode); 266 } 267 268 static bool ovl_is_opaquedir(struct dentry *dentry) 269 { 270 int res; 271 char val; 272 struct inode *inode = dentry->d_inode; 273 274 if (!S_ISDIR(inode->i_mode) || !inode->i_op->getxattr) 275 return false; 276 277 res = inode->i_op->getxattr(dentry, OVL_XATTR_OPAQUE, &val, 1); 278 if (res == 1 && val == 'y') 279 return true; 280 281 return false; 282 } 283 284 static void ovl_dentry_release(struct dentry *dentry) 285 { 286 struct ovl_entry *oe = dentry->d_fsdata; 287 288 if (oe) { 289 unsigned int i; 290 291 dput(oe->__upperdentry); 292 for (i = 0; i < oe->numlower; i++) 293 dput(oe->lowerstack[i].dentry); 294 kfree_rcu(oe, rcu); 295 } 296 } 297 298 static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags) 299 { 300 struct ovl_entry *oe = dentry->d_fsdata; 301 unsigned int i; 302 int ret = 1; 303 304 for (i = 0; i < oe->numlower; i++) { 305 struct dentry *d = oe->lowerstack[i].dentry; 306 307 if (d->d_flags & DCACHE_OP_REVALIDATE) { 308 ret = d->d_op->d_revalidate(d, flags); 309 if (ret < 0) 310 return ret; 311 if (!ret) { 312 if (!(flags & LOOKUP_RCU)) 313 d_invalidate(d); 314 return -ESTALE; 315 } 316 } 317 } 318 return 1; 319 } 320 321 static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags) 322 { 323 struct ovl_entry *oe = dentry->d_fsdata; 324 unsigned int i; 325 int ret = 1; 326 327 for (i = 0; i < oe->numlower; i++) { 328 struct dentry *d = oe->lowerstack[i].dentry; 329 330 if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE) { 331 ret = d->d_op->d_weak_revalidate(d, flags); 332 if (ret <= 0) 333 break; 334 } 335 } 336 return ret; 337 } 338 339 static const struct dentry_operations ovl_dentry_operations = { 340 .d_release = ovl_dentry_release, 341 .d_select_inode = ovl_d_select_inode, 342 }; 343 344 static const struct dentry_operations ovl_reval_dentry_operations = { 345 .d_release = ovl_dentry_release, 346 .d_select_inode = ovl_d_select_inode, 347 .d_revalidate = ovl_dentry_revalidate, 348 .d_weak_revalidate = ovl_dentry_weak_revalidate, 349 }; 350 351 static struct ovl_entry *ovl_alloc_entry(unsigned int numlower) 352 { 353 size_t size = offsetof(struct ovl_entry, lowerstack[numlower]); 354 struct ovl_entry *oe = kzalloc(size, GFP_KERNEL); 355 356 if (oe) 357 oe->numlower = numlower; 358 359 return oe; 360 } 361 362 static bool ovl_dentry_remote(struct dentry *dentry) 363 { 364 return dentry->d_flags & 365 (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE); 366 } 367 368 static bool ovl_dentry_weird(struct dentry *dentry) 369 { 370 return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT | 371 DCACHE_MANAGE_TRANSIT | 372 DCACHE_OP_HASH | 373 DCACHE_OP_COMPARE); 374 } 375 376 static inline struct dentry *ovl_lookup_real(struct dentry *dir, 377 struct qstr *name) 378 { 379 struct dentry *dentry; 380 381 inode_lock(dir->d_inode); 382 dentry = lookup_one_len(name->name, dir, name->len); 383 inode_unlock(dir->d_inode); 384 385 if (IS_ERR(dentry)) { 386 if (PTR_ERR(dentry) == -ENOENT) 387 dentry = NULL; 388 } else if (!dentry->d_inode) { 389 dput(dentry); 390 dentry = NULL; 391 } else if (ovl_dentry_weird(dentry)) { 392 dput(dentry); 393 /* Don't support traversing automounts and other weirdness */ 394 dentry = ERR_PTR(-EREMOTE); 395 } 396 return dentry; 397 } 398 399 /* 400 * Returns next layer in stack starting from top. 401 * Returns -1 if this is the last layer. 402 */ 403 int ovl_path_next(int idx, struct dentry *dentry, struct path *path) 404 { 405 struct ovl_entry *oe = dentry->d_fsdata; 406 407 BUG_ON(idx < 0); 408 if (idx == 0) { 409 ovl_path_upper(dentry, path); 410 if (path->dentry) 411 return oe->numlower ? 1 : -1; 412 idx++; 413 } 414 BUG_ON(idx > oe->numlower); 415 *path = oe->lowerstack[idx - 1]; 416 417 return (idx < oe->numlower) ? idx + 1 : -1; 418 } 419 420 struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry, 421 unsigned int flags) 422 { 423 struct ovl_entry *oe; 424 struct ovl_entry *poe = dentry->d_parent->d_fsdata; 425 struct path *stack = NULL; 426 struct dentry *upperdir, *upperdentry = NULL; 427 unsigned int ctr = 0; 428 struct inode *inode = NULL; 429 bool upperopaque = false; 430 struct dentry *this, *prev = NULL; 431 unsigned int i; 432 int err; 433 434 upperdir = ovl_upperdentry_dereference(poe); 435 if (upperdir) { 436 this = ovl_lookup_real(upperdir, &dentry->d_name); 437 err = PTR_ERR(this); 438 if (IS_ERR(this)) 439 goto out; 440 441 if (this) { 442 if (unlikely(ovl_dentry_remote(this))) { 443 dput(this); 444 err = -EREMOTE; 445 goto out; 446 } 447 if (ovl_is_whiteout(this)) { 448 dput(this); 449 this = NULL; 450 upperopaque = true; 451 } else if (poe->numlower && ovl_is_opaquedir(this)) { 452 upperopaque = true; 453 } 454 } 455 upperdentry = prev = this; 456 } 457 458 if (!upperopaque && poe->numlower) { 459 err = -ENOMEM; 460 stack = kcalloc(poe->numlower, sizeof(struct path), GFP_KERNEL); 461 if (!stack) 462 goto out_put_upper; 463 } 464 465 for (i = 0; !upperopaque && i < poe->numlower; i++) { 466 bool opaque = false; 467 struct path lowerpath = poe->lowerstack[i]; 468 469 this = ovl_lookup_real(lowerpath.dentry, &dentry->d_name); 470 err = PTR_ERR(this); 471 if (IS_ERR(this)) { 472 /* 473 * If it's positive, then treat ENAMETOOLONG as ENOENT. 474 */ 475 if (err == -ENAMETOOLONG && (upperdentry || ctr)) 476 continue; 477 goto out_put; 478 } 479 if (!this) 480 continue; 481 if (ovl_is_whiteout(this)) { 482 dput(this); 483 break; 484 } 485 /* 486 * Only makes sense to check opaque dir if this is not the 487 * lowermost layer. 488 */ 489 if (i < poe->numlower - 1 && ovl_is_opaquedir(this)) 490 opaque = true; 491 492 if (prev && (!S_ISDIR(prev->d_inode->i_mode) || 493 !S_ISDIR(this->d_inode->i_mode))) { 494 /* 495 * FIXME: check for upper-opaqueness maybe better done 496 * in remove code. 497 */ 498 if (prev == upperdentry) 499 upperopaque = true; 500 dput(this); 501 break; 502 } 503 /* 504 * If this is a non-directory then stop here. 505 */ 506 if (!S_ISDIR(this->d_inode->i_mode)) 507 opaque = true; 508 509 stack[ctr].dentry = this; 510 stack[ctr].mnt = lowerpath.mnt; 511 ctr++; 512 prev = this; 513 if (opaque) 514 break; 515 } 516 517 oe = ovl_alloc_entry(ctr); 518 err = -ENOMEM; 519 if (!oe) 520 goto out_put; 521 522 if (upperdentry || ctr) { 523 struct dentry *realdentry; 524 525 realdentry = upperdentry ? upperdentry : stack[0].dentry; 526 527 err = -ENOMEM; 528 inode = ovl_new_inode(dentry->d_sb, realdentry->d_inode->i_mode, 529 oe); 530 if (!inode) 531 goto out_free_oe; 532 ovl_copyattr(realdentry->d_inode, inode); 533 } 534 535 oe->opaque = upperopaque; 536 oe->__upperdentry = upperdentry; 537 memcpy(oe->lowerstack, stack, sizeof(struct path) * ctr); 538 kfree(stack); 539 dentry->d_fsdata = oe; 540 d_add(dentry, inode); 541 542 return NULL; 543 544 out_free_oe: 545 kfree(oe); 546 out_put: 547 for (i = 0; i < ctr; i++) 548 dput(stack[i].dentry); 549 kfree(stack); 550 out_put_upper: 551 dput(upperdentry); 552 out: 553 return ERR_PTR(err); 554 } 555 556 struct file *ovl_path_open(struct path *path, int flags) 557 { 558 return dentry_open(path, flags, current_cred()); 559 } 560 561 static void ovl_put_super(struct super_block *sb) 562 { 563 struct ovl_fs *ufs = sb->s_fs_info; 564 unsigned i; 565 566 dput(ufs->workdir); 567 mntput(ufs->upper_mnt); 568 for (i = 0; i < ufs->numlower; i++) 569 mntput(ufs->lower_mnt[i]); 570 kfree(ufs->lower_mnt); 571 572 kfree(ufs->config.lowerdir); 573 kfree(ufs->config.upperdir); 574 kfree(ufs->config.workdir); 575 kfree(ufs); 576 } 577 578 /** 579 * ovl_statfs 580 * @sb: The overlayfs super block 581 * @buf: The struct kstatfs to fill in with stats 582 * 583 * Get the filesystem statistics. As writes always target the upper layer 584 * filesystem pass the statfs to the upper filesystem (if it exists) 585 */ 586 static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf) 587 { 588 struct ovl_fs *ofs = dentry->d_sb->s_fs_info; 589 struct dentry *root_dentry = dentry->d_sb->s_root; 590 struct path path; 591 int err; 592 593 ovl_path_real(root_dentry, &path); 594 595 err = vfs_statfs(&path, buf); 596 if (!err) { 597 buf->f_namelen = max(buf->f_namelen, ofs->lower_namelen); 598 buf->f_type = OVERLAYFS_SUPER_MAGIC; 599 } 600 601 return err; 602 } 603 604 /** 605 * ovl_show_options 606 * 607 * Prints the mount options for a given superblock. 608 * Returns zero; does not fail. 609 */ 610 static int ovl_show_options(struct seq_file *m, struct dentry *dentry) 611 { 612 struct super_block *sb = dentry->d_sb; 613 struct ovl_fs *ufs = sb->s_fs_info; 614 615 seq_show_option(m, "lowerdir", ufs->config.lowerdir); 616 if (ufs->config.upperdir) { 617 seq_show_option(m, "upperdir", ufs->config.upperdir); 618 seq_show_option(m, "workdir", ufs->config.workdir); 619 } 620 if (ufs->config.default_permissions) 621 seq_puts(m, ",default_permissions"); 622 return 0; 623 } 624 625 static int ovl_remount(struct super_block *sb, int *flags, char *data) 626 { 627 struct ovl_fs *ufs = sb->s_fs_info; 628 629 if (!(*flags & MS_RDONLY) && (!ufs->upper_mnt || !ufs->workdir)) 630 return -EROFS; 631 632 return 0; 633 } 634 635 static const struct super_operations ovl_super_operations = { 636 .put_super = ovl_put_super, 637 .statfs = ovl_statfs, 638 .show_options = ovl_show_options, 639 .remount_fs = ovl_remount, 640 }; 641 642 enum { 643 OPT_LOWERDIR, 644 OPT_UPPERDIR, 645 OPT_WORKDIR, 646 OPT_DEFAULT_PERMISSIONS, 647 OPT_ERR, 648 }; 649 650 static const match_table_t ovl_tokens = { 651 {OPT_LOWERDIR, "lowerdir=%s"}, 652 {OPT_UPPERDIR, "upperdir=%s"}, 653 {OPT_WORKDIR, "workdir=%s"}, 654 {OPT_DEFAULT_PERMISSIONS, "default_permissions"}, 655 {OPT_ERR, NULL} 656 }; 657 658 static char *ovl_next_opt(char **s) 659 { 660 char *sbegin = *s; 661 char *p; 662 663 if (sbegin == NULL) 664 return NULL; 665 666 for (p = sbegin; *p; p++) { 667 if (*p == '\\') { 668 p++; 669 if (!*p) 670 break; 671 } else if (*p == ',') { 672 *p = '\0'; 673 *s = p + 1; 674 return sbegin; 675 } 676 } 677 *s = NULL; 678 return sbegin; 679 } 680 681 static int ovl_parse_opt(char *opt, struct ovl_config *config) 682 { 683 char *p; 684 685 while ((p = ovl_next_opt(&opt)) != NULL) { 686 int token; 687 substring_t args[MAX_OPT_ARGS]; 688 689 if (!*p) 690 continue; 691 692 token = match_token(p, ovl_tokens, args); 693 switch (token) { 694 case OPT_UPPERDIR: 695 kfree(config->upperdir); 696 config->upperdir = match_strdup(&args[0]); 697 if (!config->upperdir) 698 return -ENOMEM; 699 break; 700 701 case OPT_LOWERDIR: 702 kfree(config->lowerdir); 703 config->lowerdir = match_strdup(&args[0]); 704 if (!config->lowerdir) 705 return -ENOMEM; 706 break; 707 708 case OPT_WORKDIR: 709 kfree(config->workdir); 710 config->workdir = match_strdup(&args[0]); 711 if (!config->workdir) 712 return -ENOMEM; 713 break; 714 715 case OPT_DEFAULT_PERMISSIONS: 716 config->default_permissions = true; 717 break; 718 719 default: 720 pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p); 721 return -EINVAL; 722 } 723 } 724 725 /* Workdir is useless in non-upper mount */ 726 if (!config->upperdir && config->workdir) { 727 pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n", 728 config->workdir); 729 kfree(config->workdir); 730 config->workdir = NULL; 731 } 732 733 return 0; 734 } 735 736 #define OVL_WORKDIR_NAME "work" 737 738 static struct dentry *ovl_workdir_create(struct vfsmount *mnt, 739 struct dentry *dentry) 740 { 741 struct inode *dir = dentry->d_inode; 742 struct dentry *work; 743 int err; 744 bool retried = false; 745 746 err = mnt_want_write(mnt); 747 if (err) 748 return ERR_PTR(err); 749 750 inode_lock_nested(dir, I_MUTEX_PARENT); 751 retry: 752 work = lookup_one_len(OVL_WORKDIR_NAME, dentry, 753 strlen(OVL_WORKDIR_NAME)); 754 755 if (!IS_ERR(work)) { 756 struct kstat stat = { 757 .mode = S_IFDIR | 0, 758 }; 759 760 if (work->d_inode) { 761 err = -EEXIST; 762 if (retried) 763 goto out_dput; 764 765 retried = true; 766 ovl_cleanup(dir, work); 767 dput(work); 768 goto retry; 769 } 770 771 err = ovl_create_real(dir, work, &stat, NULL, NULL, true); 772 if (err) 773 goto out_dput; 774 } 775 out_unlock: 776 inode_unlock(dir); 777 mnt_drop_write(mnt); 778 779 return work; 780 781 out_dput: 782 dput(work); 783 work = ERR_PTR(err); 784 goto out_unlock; 785 } 786 787 static void ovl_unescape(char *s) 788 { 789 char *d = s; 790 791 for (;; s++, d++) { 792 if (*s == '\\') 793 s++; 794 *d = *s; 795 if (!*s) 796 break; 797 } 798 } 799 800 static int ovl_mount_dir_noesc(const char *name, struct path *path) 801 { 802 int err = -EINVAL; 803 804 if (!*name) { 805 pr_err("overlayfs: empty lowerdir\n"); 806 goto out; 807 } 808 err = kern_path(name, LOOKUP_FOLLOW, path); 809 if (err) { 810 pr_err("overlayfs: failed to resolve '%s': %i\n", name, err); 811 goto out; 812 } 813 err = -EINVAL; 814 if (ovl_dentry_weird(path->dentry)) { 815 pr_err("overlayfs: filesystem on '%s' not supported\n", name); 816 goto out_put; 817 } 818 if (!S_ISDIR(path->dentry->d_inode->i_mode)) { 819 pr_err("overlayfs: '%s' not a directory\n", name); 820 goto out_put; 821 } 822 return 0; 823 824 out_put: 825 path_put(path); 826 out: 827 return err; 828 } 829 830 static int ovl_mount_dir(const char *name, struct path *path) 831 { 832 int err = -ENOMEM; 833 char *tmp = kstrdup(name, GFP_KERNEL); 834 835 if (tmp) { 836 ovl_unescape(tmp); 837 err = ovl_mount_dir_noesc(tmp, path); 838 839 if (!err) 840 if (ovl_dentry_remote(path->dentry)) { 841 pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n", 842 tmp); 843 path_put(path); 844 err = -EINVAL; 845 } 846 kfree(tmp); 847 } 848 return err; 849 } 850 851 static int ovl_lower_dir(const char *name, struct path *path, long *namelen, 852 int *stack_depth, bool *remote) 853 { 854 int err; 855 struct kstatfs statfs; 856 857 err = ovl_mount_dir_noesc(name, path); 858 if (err) 859 goto out; 860 861 err = vfs_statfs(path, &statfs); 862 if (err) { 863 pr_err("overlayfs: statfs failed on '%s'\n", name); 864 goto out_put; 865 } 866 *namelen = max(*namelen, statfs.f_namelen); 867 *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth); 868 869 if (ovl_dentry_remote(path->dentry)) 870 *remote = true; 871 872 return 0; 873 874 out_put: 875 path_put(path); 876 out: 877 return err; 878 } 879 880 /* Workdir should not be subdir of upperdir and vice versa */ 881 static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir) 882 { 883 bool ok = false; 884 885 if (workdir != upperdir) { 886 ok = (lock_rename(workdir, upperdir) == NULL); 887 unlock_rename(workdir, upperdir); 888 } 889 return ok; 890 } 891 892 static unsigned int ovl_split_lowerdirs(char *str) 893 { 894 unsigned int ctr = 1; 895 char *s, *d; 896 897 for (s = d = str;; s++, d++) { 898 if (*s == '\\') { 899 s++; 900 } else if (*s == ':') { 901 *d = '\0'; 902 ctr++; 903 continue; 904 } 905 *d = *s; 906 if (!*s) 907 break; 908 } 909 return ctr; 910 } 911 912 static int ovl_fill_super(struct super_block *sb, void *data, int silent) 913 { 914 struct path upperpath = { NULL, NULL }; 915 struct path workpath = { NULL, NULL }; 916 struct dentry *root_dentry; 917 struct ovl_entry *oe; 918 struct ovl_fs *ufs; 919 struct path *stack = NULL; 920 char *lowertmp; 921 char *lower; 922 unsigned int numlower; 923 unsigned int stacklen = 0; 924 unsigned int i; 925 bool remote = false; 926 int err; 927 928 err = -ENOMEM; 929 ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL); 930 if (!ufs) 931 goto out; 932 933 err = ovl_parse_opt((char *) data, &ufs->config); 934 if (err) 935 goto out_free_config; 936 937 err = -EINVAL; 938 if (!ufs->config.lowerdir) { 939 if (!silent) 940 pr_err("overlayfs: missing 'lowerdir'\n"); 941 goto out_free_config; 942 } 943 944 sb->s_stack_depth = 0; 945 sb->s_maxbytes = MAX_LFS_FILESIZE; 946 if (ufs->config.upperdir) { 947 if (!ufs->config.workdir) { 948 pr_err("overlayfs: missing 'workdir'\n"); 949 goto out_free_config; 950 } 951 952 err = ovl_mount_dir(ufs->config.upperdir, &upperpath); 953 if (err) 954 goto out_free_config; 955 956 /* Upper fs should not be r/o */ 957 if (upperpath.mnt->mnt_sb->s_flags & MS_RDONLY) { 958 pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n"); 959 err = -EINVAL; 960 goto out_put_upperpath; 961 } 962 963 err = ovl_mount_dir(ufs->config.workdir, &workpath); 964 if (err) 965 goto out_put_upperpath; 966 967 err = -EINVAL; 968 if (upperpath.mnt != workpath.mnt) { 969 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n"); 970 goto out_put_workpath; 971 } 972 if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) { 973 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n"); 974 goto out_put_workpath; 975 } 976 sb->s_stack_depth = upperpath.mnt->mnt_sb->s_stack_depth; 977 } 978 err = -ENOMEM; 979 lowertmp = kstrdup(ufs->config.lowerdir, GFP_KERNEL); 980 if (!lowertmp) 981 goto out_put_workpath; 982 983 err = -EINVAL; 984 stacklen = ovl_split_lowerdirs(lowertmp); 985 if (stacklen > OVL_MAX_STACK) { 986 pr_err("overlayfs: too many lower directries, limit is %d\n", 987 OVL_MAX_STACK); 988 goto out_free_lowertmp; 989 } else if (!ufs->config.upperdir && stacklen == 1) { 990 pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n"); 991 goto out_free_lowertmp; 992 } 993 994 stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL); 995 if (!stack) 996 goto out_free_lowertmp; 997 998 lower = lowertmp; 999 for (numlower = 0; numlower < stacklen; numlower++) { 1000 err = ovl_lower_dir(lower, &stack[numlower], 1001 &ufs->lower_namelen, &sb->s_stack_depth, 1002 &remote); 1003 if (err) 1004 goto out_put_lowerpath; 1005 1006 lower = strchr(lower, '\0') + 1; 1007 } 1008 1009 err = -EINVAL; 1010 sb->s_stack_depth++; 1011 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) { 1012 pr_err("overlayfs: maximum fs stacking depth exceeded\n"); 1013 goto out_put_lowerpath; 1014 } 1015 1016 if (ufs->config.upperdir) { 1017 ufs->upper_mnt = clone_private_mount(&upperpath); 1018 err = PTR_ERR(ufs->upper_mnt); 1019 if (IS_ERR(ufs->upper_mnt)) { 1020 pr_err("overlayfs: failed to clone upperpath\n"); 1021 goto out_put_lowerpath; 1022 } 1023 1024 ufs->workdir = ovl_workdir_create(ufs->upper_mnt, workpath.dentry); 1025 err = PTR_ERR(ufs->workdir); 1026 if (IS_ERR(ufs->workdir)) { 1027 pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n", 1028 ufs->config.workdir, OVL_WORKDIR_NAME, -err); 1029 sb->s_flags |= MS_RDONLY; 1030 ufs->workdir = NULL; 1031 } 1032 1033 /* 1034 * Upper should support d_type, else whiteouts are visible. 1035 * Given workdir and upper are on same fs, we can do 1036 * iterate_dir() on workdir. 1037 */ 1038 err = ovl_check_d_type_supported(&workpath); 1039 if (err < 0) 1040 goto out_put_workdir; 1041 1042 if (!err) { 1043 pr_err("overlayfs: upper fs needs to support d_type.\n"); 1044 err = -EINVAL; 1045 goto out_put_workdir; 1046 } 1047 } 1048 1049 err = -ENOMEM; 1050 ufs->lower_mnt = kcalloc(numlower, sizeof(struct vfsmount *), GFP_KERNEL); 1051 if (ufs->lower_mnt == NULL) 1052 goto out_put_workdir; 1053 for (i = 0; i < numlower; i++) { 1054 struct vfsmount *mnt = clone_private_mount(&stack[i]); 1055 1056 err = PTR_ERR(mnt); 1057 if (IS_ERR(mnt)) { 1058 pr_err("overlayfs: failed to clone lowerpath\n"); 1059 goto out_put_lower_mnt; 1060 } 1061 /* 1062 * Make lower_mnt R/O. That way fchmod/fchown on lower file 1063 * will fail instead of modifying lower fs. 1064 */ 1065 mnt->mnt_flags |= MNT_READONLY; 1066 1067 ufs->lower_mnt[ufs->numlower] = mnt; 1068 ufs->numlower++; 1069 } 1070 1071 /* If the upper fs is nonexistent, we mark overlayfs r/o too */ 1072 if (!ufs->upper_mnt) 1073 sb->s_flags |= MS_RDONLY; 1074 1075 if (remote) 1076 sb->s_d_op = &ovl_reval_dentry_operations; 1077 else 1078 sb->s_d_op = &ovl_dentry_operations; 1079 1080 err = -ENOMEM; 1081 oe = ovl_alloc_entry(numlower); 1082 if (!oe) 1083 goto out_put_lower_mnt; 1084 1085 root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, oe)); 1086 if (!root_dentry) 1087 goto out_free_oe; 1088 1089 mntput(upperpath.mnt); 1090 for (i = 0; i < numlower; i++) 1091 mntput(stack[i].mnt); 1092 path_put(&workpath); 1093 kfree(lowertmp); 1094 1095 oe->__upperdentry = upperpath.dentry; 1096 for (i = 0; i < numlower; i++) { 1097 oe->lowerstack[i].dentry = stack[i].dentry; 1098 oe->lowerstack[i].mnt = ufs->lower_mnt[i]; 1099 } 1100 kfree(stack); 1101 1102 root_dentry->d_fsdata = oe; 1103 1104 ovl_copyattr(ovl_dentry_real(root_dentry)->d_inode, 1105 root_dentry->d_inode); 1106 1107 sb->s_magic = OVERLAYFS_SUPER_MAGIC; 1108 sb->s_op = &ovl_super_operations; 1109 sb->s_root = root_dentry; 1110 sb->s_fs_info = ufs; 1111 1112 return 0; 1113 1114 out_free_oe: 1115 kfree(oe); 1116 out_put_lower_mnt: 1117 for (i = 0; i < ufs->numlower; i++) 1118 mntput(ufs->lower_mnt[i]); 1119 kfree(ufs->lower_mnt); 1120 out_put_workdir: 1121 dput(ufs->workdir); 1122 mntput(ufs->upper_mnt); 1123 out_put_lowerpath: 1124 for (i = 0; i < numlower; i++) 1125 path_put(&stack[i]); 1126 kfree(stack); 1127 out_free_lowertmp: 1128 kfree(lowertmp); 1129 out_put_workpath: 1130 path_put(&workpath); 1131 out_put_upperpath: 1132 path_put(&upperpath); 1133 out_free_config: 1134 kfree(ufs->config.lowerdir); 1135 kfree(ufs->config.upperdir); 1136 kfree(ufs->config.workdir); 1137 kfree(ufs); 1138 out: 1139 return err; 1140 } 1141 1142 static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags, 1143 const char *dev_name, void *raw_data) 1144 { 1145 return mount_nodev(fs_type, flags, raw_data, ovl_fill_super); 1146 } 1147 1148 static struct file_system_type ovl_fs_type = { 1149 .owner = THIS_MODULE, 1150 .name = "overlay", 1151 .mount = ovl_mount, 1152 .kill_sb = kill_anon_super, 1153 }; 1154 MODULE_ALIAS_FS("overlay"); 1155 1156 static int __init ovl_init(void) 1157 { 1158 return register_filesystem(&ovl_fs_type); 1159 } 1160 1161 static void __exit ovl_exit(void) 1162 { 1163 unregister_filesystem(&ovl_fs_type); 1164 } 1165 1166 module_init(ovl_init); 1167 module_exit(ovl_exit); 1168