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, inode, 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 struct dentry *ovl_d_real(struct dentry *dentry, struct inode *inode) 299 { 300 struct dentry *real; 301 302 if (d_is_dir(dentry)) { 303 if (!inode || inode == d_inode(dentry)) 304 return dentry; 305 goto bug; 306 } 307 308 real = ovl_dentry_upper(dentry); 309 if (real && (!inode || inode == d_inode(real))) 310 return real; 311 312 real = ovl_dentry_lower(dentry); 313 if (!real) 314 goto bug; 315 316 if (!inode || inode == d_inode(real)) 317 return real; 318 319 /* Handle recursion */ 320 if (real->d_flags & DCACHE_OP_REAL) 321 return real->d_op->d_real(real, inode); 322 323 bug: 324 WARN(1, "ovl_d_real(%pd4, %s:%lu\n): real dentry not found\n", dentry, 325 inode ? inode->i_sb->s_id : "NULL", inode ? inode->i_ino : 0); 326 return dentry; 327 } 328 329 static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags) 330 { 331 struct ovl_entry *oe = dentry->d_fsdata; 332 unsigned int i; 333 int ret = 1; 334 335 for (i = 0; i < oe->numlower; i++) { 336 struct dentry *d = oe->lowerstack[i].dentry; 337 338 if (d->d_flags & DCACHE_OP_REVALIDATE) { 339 ret = d->d_op->d_revalidate(d, flags); 340 if (ret < 0) 341 return ret; 342 if (!ret) { 343 if (!(flags & LOOKUP_RCU)) 344 d_invalidate(d); 345 return -ESTALE; 346 } 347 } 348 } 349 return 1; 350 } 351 352 static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags) 353 { 354 struct ovl_entry *oe = dentry->d_fsdata; 355 unsigned int i; 356 int ret = 1; 357 358 for (i = 0; i < oe->numlower; i++) { 359 struct dentry *d = oe->lowerstack[i].dentry; 360 361 if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE) { 362 ret = d->d_op->d_weak_revalidate(d, flags); 363 if (ret <= 0) 364 break; 365 } 366 } 367 return ret; 368 } 369 370 static const struct dentry_operations ovl_dentry_operations = { 371 .d_release = ovl_dentry_release, 372 .d_select_inode = ovl_d_select_inode, 373 .d_real = ovl_d_real, 374 }; 375 376 static const struct dentry_operations ovl_reval_dentry_operations = { 377 .d_release = ovl_dentry_release, 378 .d_select_inode = ovl_d_select_inode, 379 .d_real = ovl_d_real, 380 .d_revalidate = ovl_dentry_revalidate, 381 .d_weak_revalidate = ovl_dentry_weak_revalidate, 382 }; 383 384 static struct ovl_entry *ovl_alloc_entry(unsigned int numlower) 385 { 386 size_t size = offsetof(struct ovl_entry, lowerstack[numlower]); 387 struct ovl_entry *oe = kzalloc(size, GFP_KERNEL); 388 389 if (oe) 390 oe->numlower = numlower; 391 392 return oe; 393 } 394 395 static bool ovl_dentry_remote(struct dentry *dentry) 396 { 397 return dentry->d_flags & 398 (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE); 399 } 400 401 static bool ovl_dentry_weird(struct dentry *dentry) 402 { 403 return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT | 404 DCACHE_MANAGE_TRANSIT | 405 DCACHE_OP_HASH | 406 DCACHE_OP_COMPARE); 407 } 408 409 static inline struct dentry *ovl_lookup_real(struct dentry *dir, 410 struct qstr *name) 411 { 412 struct dentry *dentry; 413 414 dentry = lookup_hash(name, dir); 415 416 if (IS_ERR(dentry)) { 417 if (PTR_ERR(dentry) == -ENOENT) 418 dentry = NULL; 419 } else if (!dentry->d_inode) { 420 dput(dentry); 421 dentry = NULL; 422 } else if (ovl_dentry_weird(dentry)) { 423 dput(dentry); 424 /* Don't support traversing automounts and other weirdness */ 425 dentry = ERR_PTR(-EREMOTE); 426 } 427 return dentry; 428 } 429 430 /* 431 * Returns next layer in stack starting from top. 432 * Returns -1 if this is the last layer. 433 */ 434 int ovl_path_next(int idx, struct dentry *dentry, struct path *path) 435 { 436 struct ovl_entry *oe = dentry->d_fsdata; 437 438 BUG_ON(idx < 0); 439 if (idx == 0) { 440 ovl_path_upper(dentry, path); 441 if (path->dentry) 442 return oe->numlower ? 1 : -1; 443 idx++; 444 } 445 BUG_ON(idx > oe->numlower); 446 *path = oe->lowerstack[idx - 1]; 447 448 return (idx < oe->numlower) ? idx + 1 : -1; 449 } 450 451 struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry, 452 unsigned int flags) 453 { 454 struct ovl_entry *oe; 455 struct ovl_entry *poe = dentry->d_parent->d_fsdata; 456 struct path *stack = NULL; 457 struct dentry *upperdir, *upperdentry = NULL; 458 unsigned int ctr = 0; 459 struct inode *inode = NULL; 460 bool upperopaque = false; 461 struct dentry *this, *prev = NULL; 462 unsigned int i; 463 int err; 464 465 upperdir = ovl_upperdentry_dereference(poe); 466 if (upperdir) { 467 this = ovl_lookup_real(upperdir, &dentry->d_name); 468 err = PTR_ERR(this); 469 if (IS_ERR(this)) 470 goto out; 471 472 if (this) { 473 if (unlikely(ovl_dentry_remote(this))) { 474 dput(this); 475 err = -EREMOTE; 476 goto out; 477 } 478 if (ovl_is_whiteout(this)) { 479 dput(this); 480 this = NULL; 481 upperopaque = true; 482 } else if (poe->numlower && ovl_is_opaquedir(this)) { 483 upperopaque = true; 484 } 485 } 486 upperdentry = prev = this; 487 } 488 489 if (!upperopaque && poe->numlower) { 490 err = -ENOMEM; 491 stack = kcalloc(poe->numlower, sizeof(struct path), GFP_KERNEL); 492 if (!stack) 493 goto out_put_upper; 494 } 495 496 for (i = 0; !upperopaque && i < poe->numlower; i++) { 497 bool opaque = false; 498 struct path lowerpath = poe->lowerstack[i]; 499 500 this = ovl_lookup_real(lowerpath.dentry, &dentry->d_name); 501 err = PTR_ERR(this); 502 if (IS_ERR(this)) { 503 /* 504 * If it's positive, then treat ENAMETOOLONG as ENOENT. 505 */ 506 if (err == -ENAMETOOLONG && (upperdentry || ctr)) 507 continue; 508 goto out_put; 509 } 510 if (!this) 511 continue; 512 if (ovl_is_whiteout(this)) { 513 dput(this); 514 break; 515 } 516 /* 517 * Only makes sense to check opaque dir if this is not the 518 * lowermost layer. 519 */ 520 if (i < poe->numlower - 1 && ovl_is_opaquedir(this)) 521 opaque = true; 522 523 if (prev && (!S_ISDIR(prev->d_inode->i_mode) || 524 !S_ISDIR(this->d_inode->i_mode))) { 525 /* 526 * FIXME: check for upper-opaqueness maybe better done 527 * in remove code. 528 */ 529 if (prev == upperdentry) 530 upperopaque = true; 531 dput(this); 532 break; 533 } 534 /* 535 * If this is a non-directory then stop here. 536 */ 537 if (!S_ISDIR(this->d_inode->i_mode)) 538 opaque = true; 539 540 stack[ctr].dentry = this; 541 stack[ctr].mnt = lowerpath.mnt; 542 ctr++; 543 prev = this; 544 if (opaque) 545 break; 546 } 547 548 oe = ovl_alloc_entry(ctr); 549 err = -ENOMEM; 550 if (!oe) 551 goto out_put; 552 553 if (upperdentry || ctr) { 554 struct dentry *realdentry; 555 556 realdentry = upperdentry ? upperdentry : stack[0].dentry; 557 558 err = -ENOMEM; 559 inode = ovl_new_inode(dentry->d_sb, realdentry->d_inode->i_mode, 560 oe); 561 if (!inode) 562 goto out_free_oe; 563 ovl_copyattr(realdentry->d_inode, inode); 564 } 565 566 oe->opaque = upperopaque; 567 oe->__upperdentry = upperdentry; 568 memcpy(oe->lowerstack, stack, sizeof(struct path) * ctr); 569 kfree(stack); 570 dentry->d_fsdata = oe; 571 d_add(dentry, inode); 572 573 return NULL; 574 575 out_free_oe: 576 kfree(oe); 577 out_put: 578 for (i = 0; i < ctr; i++) 579 dput(stack[i].dentry); 580 kfree(stack); 581 out_put_upper: 582 dput(upperdentry); 583 out: 584 return ERR_PTR(err); 585 } 586 587 struct file *ovl_path_open(struct path *path, int flags) 588 { 589 return dentry_open(path, flags, current_cred()); 590 } 591 592 static void ovl_put_super(struct super_block *sb) 593 { 594 struct ovl_fs *ufs = sb->s_fs_info; 595 unsigned i; 596 597 dput(ufs->workdir); 598 mntput(ufs->upper_mnt); 599 for (i = 0; i < ufs->numlower; i++) 600 mntput(ufs->lower_mnt[i]); 601 kfree(ufs->lower_mnt); 602 603 kfree(ufs->config.lowerdir); 604 kfree(ufs->config.upperdir); 605 kfree(ufs->config.workdir); 606 kfree(ufs); 607 } 608 609 /** 610 * ovl_statfs 611 * @sb: The overlayfs super block 612 * @buf: The struct kstatfs to fill in with stats 613 * 614 * Get the filesystem statistics. As writes always target the upper layer 615 * filesystem pass the statfs to the upper filesystem (if it exists) 616 */ 617 static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf) 618 { 619 struct ovl_fs *ofs = dentry->d_sb->s_fs_info; 620 struct dentry *root_dentry = dentry->d_sb->s_root; 621 struct path path; 622 int err; 623 624 ovl_path_real(root_dentry, &path); 625 626 err = vfs_statfs(&path, buf); 627 if (!err) { 628 buf->f_namelen = max(buf->f_namelen, ofs->lower_namelen); 629 buf->f_type = OVERLAYFS_SUPER_MAGIC; 630 } 631 632 return err; 633 } 634 635 /** 636 * ovl_show_options 637 * 638 * Prints the mount options for a given superblock. 639 * Returns zero; does not fail. 640 */ 641 static int ovl_show_options(struct seq_file *m, struct dentry *dentry) 642 { 643 struct super_block *sb = dentry->d_sb; 644 struct ovl_fs *ufs = sb->s_fs_info; 645 646 seq_show_option(m, "lowerdir", ufs->config.lowerdir); 647 if (ufs->config.upperdir) { 648 seq_show_option(m, "upperdir", ufs->config.upperdir); 649 seq_show_option(m, "workdir", ufs->config.workdir); 650 } 651 if (ufs->config.default_permissions) 652 seq_puts(m, ",default_permissions"); 653 return 0; 654 } 655 656 static int ovl_remount(struct super_block *sb, int *flags, char *data) 657 { 658 struct ovl_fs *ufs = sb->s_fs_info; 659 660 if (!(*flags & MS_RDONLY) && (!ufs->upper_mnt || !ufs->workdir)) 661 return -EROFS; 662 663 return 0; 664 } 665 666 static const struct super_operations ovl_super_operations = { 667 .put_super = ovl_put_super, 668 .statfs = ovl_statfs, 669 .show_options = ovl_show_options, 670 .remount_fs = ovl_remount, 671 }; 672 673 enum { 674 OPT_LOWERDIR, 675 OPT_UPPERDIR, 676 OPT_WORKDIR, 677 OPT_DEFAULT_PERMISSIONS, 678 OPT_ERR, 679 }; 680 681 static const match_table_t ovl_tokens = { 682 {OPT_LOWERDIR, "lowerdir=%s"}, 683 {OPT_UPPERDIR, "upperdir=%s"}, 684 {OPT_WORKDIR, "workdir=%s"}, 685 {OPT_DEFAULT_PERMISSIONS, "default_permissions"}, 686 {OPT_ERR, NULL} 687 }; 688 689 static char *ovl_next_opt(char **s) 690 { 691 char *sbegin = *s; 692 char *p; 693 694 if (sbegin == NULL) 695 return NULL; 696 697 for (p = sbegin; *p; p++) { 698 if (*p == '\\') { 699 p++; 700 if (!*p) 701 break; 702 } else if (*p == ',') { 703 *p = '\0'; 704 *s = p + 1; 705 return sbegin; 706 } 707 } 708 *s = NULL; 709 return sbegin; 710 } 711 712 static int ovl_parse_opt(char *opt, struct ovl_config *config) 713 { 714 char *p; 715 716 while ((p = ovl_next_opt(&opt)) != NULL) { 717 int token; 718 substring_t args[MAX_OPT_ARGS]; 719 720 if (!*p) 721 continue; 722 723 token = match_token(p, ovl_tokens, args); 724 switch (token) { 725 case OPT_UPPERDIR: 726 kfree(config->upperdir); 727 config->upperdir = match_strdup(&args[0]); 728 if (!config->upperdir) 729 return -ENOMEM; 730 break; 731 732 case OPT_LOWERDIR: 733 kfree(config->lowerdir); 734 config->lowerdir = match_strdup(&args[0]); 735 if (!config->lowerdir) 736 return -ENOMEM; 737 break; 738 739 case OPT_WORKDIR: 740 kfree(config->workdir); 741 config->workdir = match_strdup(&args[0]); 742 if (!config->workdir) 743 return -ENOMEM; 744 break; 745 746 case OPT_DEFAULT_PERMISSIONS: 747 config->default_permissions = true; 748 break; 749 750 default: 751 pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p); 752 return -EINVAL; 753 } 754 } 755 756 /* Workdir is useless in non-upper mount */ 757 if (!config->upperdir && config->workdir) { 758 pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n", 759 config->workdir); 760 kfree(config->workdir); 761 config->workdir = NULL; 762 } 763 764 return 0; 765 } 766 767 #define OVL_WORKDIR_NAME "work" 768 769 static struct dentry *ovl_workdir_create(struct vfsmount *mnt, 770 struct dentry *dentry) 771 { 772 struct inode *dir = dentry->d_inode; 773 struct dentry *work; 774 int err; 775 bool retried = false; 776 777 err = mnt_want_write(mnt); 778 if (err) 779 return ERR_PTR(err); 780 781 inode_lock_nested(dir, I_MUTEX_PARENT); 782 retry: 783 work = lookup_one_len(OVL_WORKDIR_NAME, dentry, 784 strlen(OVL_WORKDIR_NAME)); 785 786 if (!IS_ERR(work)) { 787 struct kstat stat = { 788 .mode = S_IFDIR | 0, 789 }; 790 791 if (work->d_inode) { 792 err = -EEXIST; 793 if (retried) 794 goto out_dput; 795 796 retried = true; 797 ovl_cleanup(dir, work); 798 dput(work); 799 goto retry; 800 } 801 802 err = ovl_create_real(dir, work, &stat, NULL, NULL, true); 803 if (err) 804 goto out_dput; 805 } 806 out_unlock: 807 inode_unlock(dir); 808 mnt_drop_write(mnt); 809 810 return work; 811 812 out_dput: 813 dput(work); 814 work = ERR_PTR(err); 815 goto out_unlock; 816 } 817 818 static void ovl_unescape(char *s) 819 { 820 char *d = s; 821 822 for (;; s++, d++) { 823 if (*s == '\\') 824 s++; 825 *d = *s; 826 if (!*s) 827 break; 828 } 829 } 830 831 static int ovl_mount_dir_noesc(const char *name, struct path *path) 832 { 833 int err = -EINVAL; 834 835 if (!*name) { 836 pr_err("overlayfs: empty lowerdir\n"); 837 goto out; 838 } 839 err = kern_path(name, LOOKUP_FOLLOW, path); 840 if (err) { 841 pr_err("overlayfs: failed to resolve '%s': %i\n", name, err); 842 goto out; 843 } 844 err = -EINVAL; 845 if (ovl_dentry_weird(path->dentry)) { 846 pr_err("overlayfs: filesystem on '%s' not supported\n", name); 847 goto out_put; 848 } 849 if (!S_ISDIR(path->dentry->d_inode->i_mode)) { 850 pr_err("overlayfs: '%s' not a directory\n", name); 851 goto out_put; 852 } 853 return 0; 854 855 out_put: 856 path_put(path); 857 out: 858 return err; 859 } 860 861 static int ovl_mount_dir(const char *name, struct path *path) 862 { 863 int err = -ENOMEM; 864 char *tmp = kstrdup(name, GFP_KERNEL); 865 866 if (tmp) { 867 ovl_unescape(tmp); 868 err = ovl_mount_dir_noesc(tmp, path); 869 870 if (!err) 871 if (ovl_dentry_remote(path->dentry)) { 872 pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n", 873 tmp); 874 path_put(path); 875 err = -EINVAL; 876 } 877 kfree(tmp); 878 } 879 return err; 880 } 881 882 static int ovl_lower_dir(const char *name, struct path *path, long *namelen, 883 int *stack_depth, bool *remote) 884 { 885 int err; 886 struct kstatfs statfs; 887 888 err = ovl_mount_dir_noesc(name, path); 889 if (err) 890 goto out; 891 892 err = vfs_statfs(path, &statfs); 893 if (err) { 894 pr_err("overlayfs: statfs failed on '%s'\n", name); 895 goto out_put; 896 } 897 *namelen = max(*namelen, statfs.f_namelen); 898 *stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth); 899 900 if (ovl_dentry_remote(path->dentry)) 901 *remote = true; 902 903 return 0; 904 905 out_put: 906 path_put(path); 907 out: 908 return err; 909 } 910 911 /* Workdir should not be subdir of upperdir and vice versa */ 912 static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir) 913 { 914 bool ok = false; 915 916 if (workdir != upperdir) { 917 ok = (lock_rename(workdir, upperdir) == NULL); 918 unlock_rename(workdir, upperdir); 919 } 920 return ok; 921 } 922 923 static unsigned int ovl_split_lowerdirs(char *str) 924 { 925 unsigned int ctr = 1; 926 char *s, *d; 927 928 for (s = d = str;; s++, d++) { 929 if (*s == '\\') { 930 s++; 931 } else if (*s == ':') { 932 *d = '\0'; 933 ctr++; 934 continue; 935 } 936 *d = *s; 937 if (!*s) 938 break; 939 } 940 return ctr; 941 } 942 943 static int ovl_fill_super(struct super_block *sb, void *data, int silent) 944 { 945 struct path upperpath = { NULL, NULL }; 946 struct path workpath = { NULL, NULL }; 947 struct dentry *root_dentry; 948 struct ovl_entry *oe; 949 struct ovl_fs *ufs; 950 struct path *stack = NULL; 951 char *lowertmp; 952 char *lower; 953 unsigned int numlower; 954 unsigned int stacklen = 0; 955 unsigned int i; 956 bool remote = false; 957 int err; 958 959 err = -ENOMEM; 960 ufs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL); 961 if (!ufs) 962 goto out; 963 964 err = ovl_parse_opt((char *) data, &ufs->config); 965 if (err) 966 goto out_free_config; 967 968 err = -EINVAL; 969 if (!ufs->config.lowerdir) { 970 if (!silent) 971 pr_err("overlayfs: missing 'lowerdir'\n"); 972 goto out_free_config; 973 } 974 975 sb->s_stack_depth = 0; 976 sb->s_maxbytes = MAX_LFS_FILESIZE; 977 if (ufs->config.upperdir) { 978 if (!ufs->config.workdir) { 979 pr_err("overlayfs: missing 'workdir'\n"); 980 goto out_free_config; 981 } 982 983 err = ovl_mount_dir(ufs->config.upperdir, &upperpath); 984 if (err) 985 goto out_free_config; 986 987 /* Upper fs should not be r/o */ 988 if (upperpath.mnt->mnt_sb->s_flags & MS_RDONLY) { 989 pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n"); 990 err = -EINVAL; 991 goto out_put_upperpath; 992 } 993 994 err = ovl_mount_dir(ufs->config.workdir, &workpath); 995 if (err) 996 goto out_put_upperpath; 997 998 err = -EINVAL; 999 if (upperpath.mnt != workpath.mnt) { 1000 pr_err("overlayfs: workdir and upperdir must reside under the same mount\n"); 1001 goto out_put_workpath; 1002 } 1003 if (!ovl_workdir_ok(workpath.dentry, upperpath.dentry)) { 1004 pr_err("overlayfs: workdir and upperdir must be separate subtrees\n"); 1005 goto out_put_workpath; 1006 } 1007 sb->s_stack_depth = upperpath.mnt->mnt_sb->s_stack_depth; 1008 } 1009 err = -ENOMEM; 1010 lowertmp = kstrdup(ufs->config.lowerdir, GFP_KERNEL); 1011 if (!lowertmp) 1012 goto out_put_workpath; 1013 1014 err = -EINVAL; 1015 stacklen = ovl_split_lowerdirs(lowertmp); 1016 if (stacklen > OVL_MAX_STACK) { 1017 pr_err("overlayfs: too many lower directries, limit is %d\n", 1018 OVL_MAX_STACK); 1019 goto out_free_lowertmp; 1020 } else if (!ufs->config.upperdir && stacklen == 1) { 1021 pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n"); 1022 goto out_free_lowertmp; 1023 } 1024 1025 stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL); 1026 if (!stack) 1027 goto out_free_lowertmp; 1028 1029 lower = lowertmp; 1030 for (numlower = 0; numlower < stacklen; numlower++) { 1031 err = ovl_lower_dir(lower, &stack[numlower], 1032 &ufs->lower_namelen, &sb->s_stack_depth, 1033 &remote); 1034 if (err) 1035 goto out_put_lowerpath; 1036 1037 lower = strchr(lower, '\0') + 1; 1038 } 1039 1040 err = -EINVAL; 1041 sb->s_stack_depth++; 1042 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) { 1043 pr_err("overlayfs: maximum fs stacking depth exceeded\n"); 1044 goto out_put_lowerpath; 1045 } 1046 1047 if (ufs->config.upperdir) { 1048 ufs->upper_mnt = clone_private_mount(&upperpath); 1049 err = PTR_ERR(ufs->upper_mnt); 1050 if (IS_ERR(ufs->upper_mnt)) { 1051 pr_err("overlayfs: failed to clone upperpath\n"); 1052 goto out_put_lowerpath; 1053 } 1054 1055 ufs->workdir = ovl_workdir_create(ufs->upper_mnt, workpath.dentry); 1056 err = PTR_ERR(ufs->workdir); 1057 if (IS_ERR(ufs->workdir)) { 1058 pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n", 1059 ufs->config.workdir, OVL_WORKDIR_NAME, -err); 1060 sb->s_flags |= MS_RDONLY; 1061 ufs->workdir = NULL; 1062 } 1063 1064 /* 1065 * Upper should support d_type, else whiteouts are visible. 1066 * Given workdir and upper are on same fs, we can do 1067 * iterate_dir() on workdir. 1068 */ 1069 err = ovl_check_d_type_supported(&workpath); 1070 if (err < 0) 1071 goto out_put_workdir; 1072 1073 if (!err) { 1074 pr_err("overlayfs: upper fs needs to support d_type.\n"); 1075 err = -EINVAL; 1076 goto out_put_workdir; 1077 } 1078 } 1079 1080 err = -ENOMEM; 1081 ufs->lower_mnt = kcalloc(numlower, sizeof(struct vfsmount *), GFP_KERNEL); 1082 if (ufs->lower_mnt == NULL) 1083 goto out_put_workdir; 1084 for (i = 0; i < numlower; i++) { 1085 struct vfsmount *mnt = clone_private_mount(&stack[i]); 1086 1087 err = PTR_ERR(mnt); 1088 if (IS_ERR(mnt)) { 1089 pr_err("overlayfs: failed to clone lowerpath\n"); 1090 goto out_put_lower_mnt; 1091 } 1092 /* 1093 * Make lower_mnt R/O. That way fchmod/fchown on lower file 1094 * will fail instead of modifying lower fs. 1095 */ 1096 mnt->mnt_flags |= MNT_READONLY; 1097 1098 ufs->lower_mnt[ufs->numlower] = mnt; 1099 ufs->numlower++; 1100 } 1101 1102 /* If the upper fs is nonexistent, we mark overlayfs r/o too */ 1103 if (!ufs->upper_mnt) 1104 sb->s_flags |= MS_RDONLY; 1105 1106 if (remote) 1107 sb->s_d_op = &ovl_reval_dentry_operations; 1108 else 1109 sb->s_d_op = &ovl_dentry_operations; 1110 1111 err = -ENOMEM; 1112 oe = ovl_alloc_entry(numlower); 1113 if (!oe) 1114 goto out_put_lower_mnt; 1115 1116 root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, oe)); 1117 if (!root_dentry) 1118 goto out_free_oe; 1119 1120 mntput(upperpath.mnt); 1121 for (i = 0; i < numlower; i++) 1122 mntput(stack[i].mnt); 1123 path_put(&workpath); 1124 kfree(lowertmp); 1125 1126 oe->__upperdentry = upperpath.dentry; 1127 for (i = 0; i < numlower; i++) { 1128 oe->lowerstack[i].dentry = stack[i].dentry; 1129 oe->lowerstack[i].mnt = ufs->lower_mnt[i]; 1130 } 1131 kfree(stack); 1132 1133 root_dentry->d_fsdata = oe; 1134 1135 ovl_copyattr(ovl_dentry_real(root_dentry)->d_inode, 1136 root_dentry->d_inode); 1137 1138 sb->s_magic = OVERLAYFS_SUPER_MAGIC; 1139 sb->s_op = &ovl_super_operations; 1140 sb->s_root = root_dentry; 1141 sb->s_fs_info = ufs; 1142 1143 return 0; 1144 1145 out_free_oe: 1146 kfree(oe); 1147 out_put_lower_mnt: 1148 for (i = 0; i < ufs->numlower; i++) 1149 mntput(ufs->lower_mnt[i]); 1150 kfree(ufs->lower_mnt); 1151 out_put_workdir: 1152 dput(ufs->workdir); 1153 mntput(ufs->upper_mnt); 1154 out_put_lowerpath: 1155 for (i = 0; i < numlower; i++) 1156 path_put(&stack[i]); 1157 kfree(stack); 1158 out_free_lowertmp: 1159 kfree(lowertmp); 1160 out_put_workpath: 1161 path_put(&workpath); 1162 out_put_upperpath: 1163 path_put(&upperpath); 1164 out_free_config: 1165 kfree(ufs->config.lowerdir); 1166 kfree(ufs->config.upperdir); 1167 kfree(ufs->config.workdir); 1168 kfree(ufs); 1169 out: 1170 return err; 1171 } 1172 1173 static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags, 1174 const char *dev_name, void *raw_data) 1175 { 1176 return mount_nodev(fs_type, flags, raw_data, ovl_fill_super); 1177 } 1178 1179 static struct file_system_type ovl_fs_type = { 1180 .owner = THIS_MODULE, 1181 .name = "overlay", 1182 .mount = ovl_mount, 1183 .kill_sb = kill_anon_super, 1184 }; 1185 MODULE_ALIAS_FS("overlay"); 1186 1187 static int __init ovl_init(void) 1188 { 1189 return register_filesystem(&ovl_fs_type); 1190 } 1191 1192 static void __exit ovl_exit(void) 1193 { 1194 unregister_filesystem(&ovl_fs_type); 1195 } 1196 1197 module_init(ovl_init); 1198 module_exit(ovl_exit); 1199