1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2017-2018 HUAWEI, Inc. 4 * https://www.huawei.com/ 5 * Copyright (C) 2021, Alibaba Cloud 6 */ 7 #include <linux/module.h> 8 #include <linux/buffer_head.h> 9 #include <linux/statfs.h> 10 #include <linux/parser.h> 11 #include <linux/seq_file.h> 12 #include <linux/crc32c.h> 13 #include <linux/fs_context.h> 14 #include <linux/fs_parser.h> 15 #include <linux/dax.h> 16 #include "xattr.h" 17 18 #define CREATE_TRACE_POINTS 19 #include <trace/events/erofs.h> 20 21 static struct kmem_cache *erofs_inode_cachep __read_mostly; 22 23 void _erofs_err(struct super_block *sb, const char *function, 24 const char *fmt, ...) 25 { 26 struct va_format vaf; 27 va_list args; 28 29 va_start(args, fmt); 30 31 vaf.fmt = fmt; 32 vaf.va = &args; 33 34 pr_err("(device %s): %s: %pV", sb->s_id, function, &vaf); 35 va_end(args); 36 } 37 38 void _erofs_info(struct super_block *sb, const char *function, 39 const char *fmt, ...) 40 { 41 struct va_format vaf; 42 va_list args; 43 44 va_start(args, fmt); 45 46 vaf.fmt = fmt; 47 vaf.va = &args; 48 49 pr_info("(device %s): %pV", sb->s_id, &vaf); 50 va_end(args); 51 } 52 53 static int erofs_superblock_csum_verify(struct super_block *sb, void *sbdata) 54 { 55 struct erofs_super_block *dsb; 56 u32 expected_crc, crc; 57 58 dsb = kmemdup(sbdata + EROFS_SUPER_OFFSET, 59 EROFS_BLKSIZ - EROFS_SUPER_OFFSET, GFP_KERNEL); 60 if (!dsb) 61 return -ENOMEM; 62 63 expected_crc = le32_to_cpu(dsb->checksum); 64 dsb->checksum = 0; 65 /* to allow for x86 boot sectors and other oddities. */ 66 crc = crc32c(~0, dsb, EROFS_BLKSIZ - EROFS_SUPER_OFFSET); 67 kfree(dsb); 68 69 if (crc != expected_crc) { 70 erofs_err(sb, "invalid checksum 0x%08x, 0x%08x expected", 71 crc, expected_crc); 72 return -EBADMSG; 73 } 74 return 0; 75 } 76 77 static void erofs_inode_init_once(void *ptr) 78 { 79 struct erofs_inode *vi = ptr; 80 81 inode_init_once(&vi->vfs_inode); 82 } 83 84 static struct inode *erofs_alloc_inode(struct super_block *sb) 85 { 86 struct erofs_inode *vi = 87 kmem_cache_alloc(erofs_inode_cachep, GFP_KERNEL); 88 89 if (!vi) 90 return NULL; 91 92 /* zero out everything except vfs_inode */ 93 memset(vi, 0, offsetof(struct erofs_inode, vfs_inode)); 94 return &vi->vfs_inode; 95 } 96 97 static void erofs_free_inode(struct inode *inode) 98 { 99 struct erofs_inode *vi = EROFS_I(inode); 100 101 /* be careful of RCU symlink path */ 102 if (inode->i_op == &erofs_fast_symlink_iops) 103 kfree(inode->i_link); 104 kfree(vi->xattr_shared_xattrs); 105 106 kmem_cache_free(erofs_inode_cachep, vi); 107 } 108 109 static bool check_layout_compatibility(struct super_block *sb, 110 struct erofs_super_block *dsb) 111 { 112 const unsigned int feature = le32_to_cpu(dsb->feature_incompat); 113 114 EROFS_SB(sb)->feature_incompat = feature; 115 116 /* check if current kernel meets all mandatory requirements */ 117 if (feature & (~EROFS_ALL_FEATURE_INCOMPAT)) { 118 erofs_err(sb, 119 "unidentified incompatible feature %x, please upgrade kernel version", 120 feature & ~EROFS_ALL_FEATURE_INCOMPAT); 121 return false; 122 } 123 return true; 124 } 125 126 #ifdef CONFIG_EROFS_FS_ZIP 127 /* read variable-sized metadata, offset will be aligned by 4-byte */ 128 static void *erofs_read_metadata(struct super_block *sb, struct erofs_buf *buf, 129 erofs_off_t *offset, int *lengthp) 130 { 131 u8 *buffer, *ptr; 132 int len, i, cnt; 133 134 *offset = round_up(*offset, 4); 135 ptr = erofs_read_metabuf(buf, sb, erofs_blknr(*offset), EROFS_KMAP); 136 if (IS_ERR(ptr)) 137 return ptr; 138 139 len = le16_to_cpu(*(__le16 *)&ptr[erofs_blkoff(*offset)]); 140 if (!len) 141 len = U16_MAX + 1; 142 buffer = kmalloc(len, GFP_KERNEL); 143 if (!buffer) 144 return ERR_PTR(-ENOMEM); 145 *offset += sizeof(__le16); 146 *lengthp = len; 147 148 for (i = 0; i < len; i += cnt) { 149 cnt = min(EROFS_BLKSIZ - (int)erofs_blkoff(*offset), len - i); 150 ptr = erofs_read_metabuf(buf, sb, erofs_blknr(*offset), 151 EROFS_KMAP); 152 if (IS_ERR(ptr)) { 153 kfree(buffer); 154 return ptr; 155 } 156 memcpy(buffer + i, ptr + erofs_blkoff(*offset), cnt); 157 *offset += cnt; 158 } 159 return buffer; 160 } 161 162 static int erofs_load_compr_cfgs(struct super_block *sb, 163 struct erofs_super_block *dsb) 164 { 165 struct erofs_sb_info *sbi = EROFS_SB(sb); 166 struct erofs_buf buf = __EROFS_BUF_INITIALIZER; 167 unsigned int algs, alg; 168 erofs_off_t offset; 169 int size, ret = 0; 170 171 sbi->available_compr_algs = le16_to_cpu(dsb->u1.available_compr_algs); 172 if (sbi->available_compr_algs & ~Z_EROFS_ALL_COMPR_ALGS) { 173 erofs_err(sb, "try to load compressed fs with unsupported algorithms %x", 174 sbi->available_compr_algs & ~Z_EROFS_ALL_COMPR_ALGS); 175 return -EINVAL; 176 } 177 178 offset = EROFS_SUPER_OFFSET + sbi->sb_size; 179 alg = 0; 180 for (algs = sbi->available_compr_algs; algs; algs >>= 1, ++alg) { 181 void *data; 182 183 if (!(algs & 1)) 184 continue; 185 186 data = erofs_read_metadata(sb, &buf, &offset, &size); 187 if (IS_ERR(data)) { 188 ret = PTR_ERR(data); 189 break; 190 } 191 192 switch (alg) { 193 case Z_EROFS_COMPRESSION_LZ4: 194 ret = z_erofs_load_lz4_config(sb, dsb, data, size); 195 break; 196 case Z_EROFS_COMPRESSION_LZMA: 197 ret = z_erofs_load_lzma_config(sb, dsb, data, size); 198 break; 199 default: 200 DBG_BUGON(1); 201 ret = -EFAULT; 202 } 203 kfree(data); 204 if (ret) 205 break; 206 } 207 erofs_put_metabuf(&buf); 208 return ret; 209 } 210 #else 211 static int erofs_load_compr_cfgs(struct super_block *sb, 212 struct erofs_super_block *dsb) 213 { 214 if (dsb->u1.available_compr_algs) { 215 erofs_err(sb, "try to load compressed fs when compression is disabled"); 216 return -EINVAL; 217 } 218 return 0; 219 } 220 #endif 221 222 static int erofs_init_devices(struct super_block *sb, 223 struct erofs_super_block *dsb) 224 { 225 struct erofs_sb_info *sbi = EROFS_SB(sb); 226 unsigned int ondisk_extradevs; 227 erofs_off_t pos; 228 struct erofs_buf buf = __EROFS_BUF_INITIALIZER; 229 struct erofs_device_info *dif; 230 struct erofs_deviceslot *dis; 231 void *ptr; 232 int id, err = 0; 233 234 sbi->total_blocks = sbi->primarydevice_blocks; 235 if (!erofs_sb_has_device_table(sbi)) 236 ondisk_extradevs = 0; 237 else 238 ondisk_extradevs = le16_to_cpu(dsb->extra_devices); 239 240 if (ondisk_extradevs != sbi->devs->extra_devices) { 241 erofs_err(sb, "extra devices don't match (ondisk %u, given %u)", 242 ondisk_extradevs, sbi->devs->extra_devices); 243 return -EINVAL; 244 } 245 if (!ondisk_extradevs) 246 return 0; 247 248 sbi->device_id_mask = roundup_pow_of_two(ondisk_extradevs + 1) - 1; 249 pos = le16_to_cpu(dsb->devt_slotoff) * EROFS_DEVT_SLOT_SIZE; 250 down_read(&sbi->devs->rwsem); 251 idr_for_each_entry(&sbi->devs->tree, dif, id) { 252 struct block_device *bdev; 253 254 ptr = erofs_read_metabuf(&buf, sb, erofs_blknr(pos), 255 EROFS_KMAP); 256 if (IS_ERR(ptr)) { 257 err = PTR_ERR(ptr); 258 break; 259 } 260 dis = ptr + erofs_blkoff(pos); 261 262 bdev = blkdev_get_by_path(dif->path, 263 FMODE_READ | FMODE_EXCL, 264 sb->s_type); 265 if (IS_ERR(bdev)) { 266 err = PTR_ERR(bdev); 267 break; 268 } 269 dif->bdev = bdev; 270 dif->dax_dev = fs_dax_get_by_bdev(bdev, &dif->dax_part_off); 271 dif->blocks = le32_to_cpu(dis->blocks); 272 dif->mapped_blkaddr = le32_to_cpu(dis->mapped_blkaddr); 273 sbi->total_blocks += dif->blocks; 274 pos += EROFS_DEVT_SLOT_SIZE; 275 } 276 up_read(&sbi->devs->rwsem); 277 erofs_put_metabuf(&buf); 278 return err; 279 } 280 281 static int erofs_read_superblock(struct super_block *sb) 282 { 283 struct erofs_sb_info *sbi; 284 struct page *page; 285 struct erofs_super_block *dsb; 286 unsigned int blkszbits; 287 void *data; 288 int ret; 289 290 page = read_mapping_page(sb->s_bdev->bd_inode->i_mapping, 0, NULL); 291 if (IS_ERR(page)) { 292 erofs_err(sb, "cannot read erofs superblock"); 293 return PTR_ERR(page); 294 } 295 296 sbi = EROFS_SB(sb); 297 298 data = kmap(page); 299 dsb = (struct erofs_super_block *)(data + EROFS_SUPER_OFFSET); 300 301 ret = -EINVAL; 302 if (le32_to_cpu(dsb->magic) != EROFS_SUPER_MAGIC_V1) { 303 erofs_err(sb, "cannot find valid erofs superblock"); 304 goto out; 305 } 306 307 sbi->feature_compat = le32_to_cpu(dsb->feature_compat); 308 if (erofs_sb_has_sb_chksum(sbi)) { 309 ret = erofs_superblock_csum_verify(sb, data); 310 if (ret) 311 goto out; 312 } 313 314 ret = -EINVAL; 315 blkszbits = dsb->blkszbits; 316 /* 9(512 bytes) + LOG_SECTORS_PER_BLOCK == LOG_BLOCK_SIZE */ 317 if (blkszbits != LOG_BLOCK_SIZE) { 318 erofs_err(sb, "blkszbits %u isn't supported on this platform", 319 blkszbits); 320 goto out; 321 } 322 323 if (!check_layout_compatibility(sb, dsb)) 324 goto out; 325 326 sbi->sb_size = 128 + dsb->sb_extslots * EROFS_SB_EXTSLOT_SIZE; 327 if (sbi->sb_size > EROFS_BLKSIZ) { 328 erofs_err(sb, "invalid sb_extslots %u (more than a fs block)", 329 sbi->sb_size); 330 goto out; 331 } 332 sbi->primarydevice_blocks = le32_to_cpu(dsb->blocks); 333 sbi->meta_blkaddr = le32_to_cpu(dsb->meta_blkaddr); 334 #ifdef CONFIG_EROFS_FS_XATTR 335 sbi->xattr_blkaddr = le32_to_cpu(dsb->xattr_blkaddr); 336 #endif 337 sbi->islotbits = ilog2(sizeof(struct erofs_inode_compact)); 338 sbi->root_nid = le16_to_cpu(dsb->root_nid); 339 sbi->inos = le64_to_cpu(dsb->inos); 340 341 sbi->build_time = le64_to_cpu(dsb->build_time); 342 sbi->build_time_nsec = le32_to_cpu(dsb->build_time_nsec); 343 344 memcpy(&sb->s_uuid, dsb->uuid, sizeof(dsb->uuid)); 345 346 ret = strscpy(sbi->volume_name, dsb->volume_name, 347 sizeof(dsb->volume_name)); 348 if (ret < 0) { /* -E2BIG */ 349 erofs_err(sb, "bad volume name without NIL terminator"); 350 ret = -EFSCORRUPTED; 351 goto out; 352 } 353 354 /* parse on-disk compression configurations */ 355 if (erofs_sb_has_compr_cfgs(sbi)) 356 ret = erofs_load_compr_cfgs(sb, dsb); 357 else 358 ret = z_erofs_load_lz4_config(sb, dsb, NULL, 0); 359 if (ret < 0) 360 goto out; 361 362 /* handle multiple devices */ 363 ret = erofs_init_devices(sb, dsb); 364 365 if (erofs_sb_has_ztailpacking(sbi)) 366 erofs_info(sb, "EXPERIMENTAL compressed inline data feature in use. Use at your own risk!"); 367 out: 368 kunmap(page); 369 put_page(page); 370 return ret; 371 } 372 373 /* set up default EROFS parameters */ 374 static void erofs_default_options(struct erofs_fs_context *ctx) 375 { 376 #ifdef CONFIG_EROFS_FS_ZIP 377 ctx->opt.cache_strategy = EROFS_ZIP_CACHE_READAROUND; 378 ctx->opt.max_sync_decompress_pages = 3; 379 ctx->opt.sync_decompress = EROFS_SYNC_DECOMPRESS_AUTO; 380 #endif 381 #ifdef CONFIG_EROFS_FS_XATTR 382 set_opt(&ctx->opt, XATTR_USER); 383 #endif 384 #ifdef CONFIG_EROFS_FS_POSIX_ACL 385 set_opt(&ctx->opt, POSIX_ACL); 386 #endif 387 } 388 389 enum { 390 Opt_user_xattr, 391 Opt_acl, 392 Opt_cache_strategy, 393 Opt_dax, 394 Opt_dax_enum, 395 Opt_device, 396 Opt_err 397 }; 398 399 static const struct constant_table erofs_param_cache_strategy[] = { 400 {"disabled", EROFS_ZIP_CACHE_DISABLED}, 401 {"readahead", EROFS_ZIP_CACHE_READAHEAD}, 402 {"readaround", EROFS_ZIP_CACHE_READAROUND}, 403 {} 404 }; 405 406 static const struct constant_table erofs_dax_param_enums[] = { 407 {"always", EROFS_MOUNT_DAX_ALWAYS}, 408 {"never", EROFS_MOUNT_DAX_NEVER}, 409 {} 410 }; 411 412 static const struct fs_parameter_spec erofs_fs_parameters[] = { 413 fsparam_flag_no("user_xattr", Opt_user_xattr), 414 fsparam_flag_no("acl", Opt_acl), 415 fsparam_enum("cache_strategy", Opt_cache_strategy, 416 erofs_param_cache_strategy), 417 fsparam_flag("dax", Opt_dax), 418 fsparam_enum("dax", Opt_dax_enum, erofs_dax_param_enums), 419 fsparam_string("device", Opt_device), 420 {} 421 }; 422 423 static bool erofs_fc_set_dax_mode(struct fs_context *fc, unsigned int mode) 424 { 425 #ifdef CONFIG_FS_DAX 426 struct erofs_fs_context *ctx = fc->fs_private; 427 428 switch (mode) { 429 case EROFS_MOUNT_DAX_ALWAYS: 430 warnfc(fc, "DAX enabled. Warning: EXPERIMENTAL, use at your own risk"); 431 set_opt(&ctx->opt, DAX_ALWAYS); 432 clear_opt(&ctx->opt, DAX_NEVER); 433 return true; 434 case EROFS_MOUNT_DAX_NEVER: 435 set_opt(&ctx->opt, DAX_NEVER); 436 clear_opt(&ctx->opt, DAX_ALWAYS); 437 return true; 438 default: 439 DBG_BUGON(1); 440 return false; 441 } 442 #else 443 errorfc(fc, "dax options not supported"); 444 return false; 445 #endif 446 } 447 448 static int erofs_fc_parse_param(struct fs_context *fc, 449 struct fs_parameter *param) 450 { 451 struct erofs_fs_context *ctx = fc->fs_private; 452 struct fs_parse_result result; 453 struct erofs_device_info *dif; 454 int opt, ret; 455 456 opt = fs_parse(fc, erofs_fs_parameters, param, &result); 457 if (opt < 0) 458 return opt; 459 460 switch (opt) { 461 case Opt_user_xattr: 462 #ifdef CONFIG_EROFS_FS_XATTR 463 if (result.boolean) 464 set_opt(&ctx->opt, XATTR_USER); 465 else 466 clear_opt(&ctx->opt, XATTR_USER); 467 #else 468 errorfc(fc, "{,no}user_xattr options not supported"); 469 #endif 470 break; 471 case Opt_acl: 472 #ifdef CONFIG_EROFS_FS_POSIX_ACL 473 if (result.boolean) 474 set_opt(&ctx->opt, POSIX_ACL); 475 else 476 clear_opt(&ctx->opt, POSIX_ACL); 477 #else 478 errorfc(fc, "{,no}acl options not supported"); 479 #endif 480 break; 481 case Opt_cache_strategy: 482 #ifdef CONFIG_EROFS_FS_ZIP 483 ctx->opt.cache_strategy = result.uint_32; 484 #else 485 errorfc(fc, "compression not supported, cache_strategy ignored"); 486 #endif 487 break; 488 case Opt_dax: 489 if (!erofs_fc_set_dax_mode(fc, EROFS_MOUNT_DAX_ALWAYS)) 490 return -EINVAL; 491 break; 492 case Opt_dax_enum: 493 if (!erofs_fc_set_dax_mode(fc, result.uint_32)) 494 return -EINVAL; 495 break; 496 case Opt_device: 497 dif = kzalloc(sizeof(*dif), GFP_KERNEL); 498 if (!dif) 499 return -ENOMEM; 500 dif->path = kstrdup(param->string, GFP_KERNEL); 501 if (!dif->path) { 502 kfree(dif); 503 return -ENOMEM; 504 } 505 down_write(&ctx->devs->rwsem); 506 ret = idr_alloc(&ctx->devs->tree, dif, 0, 0, GFP_KERNEL); 507 up_write(&ctx->devs->rwsem); 508 if (ret < 0) { 509 kfree(dif->path); 510 kfree(dif); 511 return ret; 512 } 513 ++ctx->devs->extra_devices; 514 break; 515 default: 516 return -ENOPARAM; 517 } 518 return 0; 519 } 520 521 #ifdef CONFIG_EROFS_FS_ZIP 522 static const struct address_space_operations managed_cache_aops; 523 524 static int erofs_managed_cache_releasepage(struct page *page, gfp_t gfp_mask) 525 { 526 int ret = 1; /* 0 - busy */ 527 struct address_space *const mapping = page->mapping; 528 529 DBG_BUGON(!PageLocked(page)); 530 DBG_BUGON(mapping->a_ops != &managed_cache_aops); 531 532 if (PagePrivate(page)) 533 ret = erofs_try_to_free_cached_page(page); 534 535 return ret; 536 } 537 538 static void erofs_managed_cache_invalidate_folio(struct folio *folio, 539 size_t offset, size_t length) 540 { 541 const size_t stop = length + offset; 542 543 DBG_BUGON(!folio_test_locked(folio)); 544 545 /* Check for potential overflow in debug mode */ 546 DBG_BUGON(stop > folio_size(folio) || stop < length); 547 548 if (offset == 0 && stop == folio_size(folio)) 549 while (!erofs_managed_cache_releasepage(&folio->page, GFP_NOFS)) 550 cond_resched(); 551 } 552 553 static const struct address_space_operations managed_cache_aops = { 554 .releasepage = erofs_managed_cache_releasepage, 555 .invalidate_folio = erofs_managed_cache_invalidate_folio, 556 }; 557 558 static int erofs_init_managed_cache(struct super_block *sb) 559 { 560 struct erofs_sb_info *const sbi = EROFS_SB(sb); 561 struct inode *const inode = new_inode(sb); 562 563 if (!inode) 564 return -ENOMEM; 565 566 set_nlink(inode, 1); 567 inode->i_size = OFFSET_MAX; 568 569 inode->i_mapping->a_ops = &managed_cache_aops; 570 mapping_set_gfp_mask(inode->i_mapping, 571 GFP_NOFS | __GFP_HIGHMEM | __GFP_MOVABLE); 572 sbi->managed_cache = inode; 573 return 0; 574 } 575 #else 576 static int erofs_init_managed_cache(struct super_block *sb) { return 0; } 577 #endif 578 579 static int erofs_fc_fill_super(struct super_block *sb, struct fs_context *fc) 580 { 581 struct inode *inode; 582 struct erofs_sb_info *sbi; 583 struct erofs_fs_context *ctx = fc->fs_private; 584 int err; 585 586 sb->s_magic = EROFS_SUPER_MAGIC; 587 588 if (!sb_set_blocksize(sb, EROFS_BLKSIZ)) { 589 erofs_err(sb, "failed to set erofs blksize"); 590 return -EINVAL; 591 } 592 593 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL); 594 if (!sbi) 595 return -ENOMEM; 596 597 sb->s_fs_info = sbi; 598 sbi->opt = ctx->opt; 599 sbi->dax_dev = fs_dax_get_by_bdev(sb->s_bdev, &sbi->dax_part_off); 600 sbi->devs = ctx->devs; 601 ctx->devs = NULL; 602 603 err = erofs_read_superblock(sb); 604 if (err) 605 return err; 606 607 if (test_opt(&sbi->opt, DAX_ALWAYS)) { 608 BUILD_BUG_ON(EROFS_BLKSIZ != PAGE_SIZE); 609 610 if (!sbi->dax_dev) { 611 errorfc(fc, "DAX unsupported by block device. Turning off DAX."); 612 clear_opt(&sbi->opt, DAX_ALWAYS); 613 } 614 } 615 sb->s_flags |= SB_RDONLY | SB_NOATIME; 616 sb->s_maxbytes = MAX_LFS_FILESIZE; 617 sb->s_time_gran = 1; 618 619 sb->s_op = &erofs_sops; 620 sb->s_xattr = erofs_xattr_handlers; 621 622 if (test_opt(&sbi->opt, POSIX_ACL)) 623 sb->s_flags |= SB_POSIXACL; 624 else 625 sb->s_flags &= ~SB_POSIXACL; 626 627 #ifdef CONFIG_EROFS_FS_ZIP 628 xa_init(&sbi->managed_pslots); 629 #endif 630 631 /* get the root inode */ 632 inode = erofs_iget(sb, ROOT_NID(sbi), true); 633 if (IS_ERR(inode)) 634 return PTR_ERR(inode); 635 636 if (!S_ISDIR(inode->i_mode)) { 637 erofs_err(sb, "rootino(nid %llu) is not a directory(i_mode %o)", 638 ROOT_NID(sbi), inode->i_mode); 639 iput(inode); 640 return -EINVAL; 641 } 642 643 sb->s_root = d_make_root(inode); 644 if (!sb->s_root) 645 return -ENOMEM; 646 647 erofs_shrinker_register(sb); 648 /* sb->s_umount is already locked, SB_ACTIVE and SB_BORN are not set */ 649 err = erofs_init_managed_cache(sb); 650 if (err) 651 return err; 652 653 err = erofs_register_sysfs(sb); 654 if (err) 655 return err; 656 657 erofs_info(sb, "mounted with root inode @ nid %llu.", ROOT_NID(sbi)); 658 return 0; 659 } 660 661 static int erofs_fc_get_tree(struct fs_context *fc) 662 { 663 return get_tree_bdev(fc, erofs_fc_fill_super); 664 } 665 666 static int erofs_fc_reconfigure(struct fs_context *fc) 667 { 668 struct super_block *sb = fc->root->d_sb; 669 struct erofs_sb_info *sbi = EROFS_SB(sb); 670 struct erofs_fs_context *ctx = fc->fs_private; 671 672 DBG_BUGON(!sb_rdonly(sb)); 673 674 if (test_opt(&ctx->opt, POSIX_ACL)) 675 fc->sb_flags |= SB_POSIXACL; 676 else 677 fc->sb_flags &= ~SB_POSIXACL; 678 679 sbi->opt = ctx->opt; 680 681 fc->sb_flags |= SB_RDONLY; 682 return 0; 683 } 684 685 static int erofs_release_device_info(int id, void *ptr, void *data) 686 { 687 struct erofs_device_info *dif = ptr; 688 689 fs_put_dax(dif->dax_dev); 690 if (dif->bdev) 691 blkdev_put(dif->bdev, FMODE_READ | FMODE_EXCL); 692 kfree(dif->path); 693 kfree(dif); 694 return 0; 695 } 696 697 static void erofs_free_dev_context(struct erofs_dev_context *devs) 698 { 699 if (!devs) 700 return; 701 idr_for_each(&devs->tree, &erofs_release_device_info, NULL); 702 idr_destroy(&devs->tree); 703 kfree(devs); 704 } 705 706 static void erofs_fc_free(struct fs_context *fc) 707 { 708 struct erofs_fs_context *ctx = fc->fs_private; 709 710 erofs_free_dev_context(ctx->devs); 711 kfree(ctx); 712 } 713 714 static const struct fs_context_operations erofs_context_ops = { 715 .parse_param = erofs_fc_parse_param, 716 .get_tree = erofs_fc_get_tree, 717 .reconfigure = erofs_fc_reconfigure, 718 .free = erofs_fc_free, 719 }; 720 721 static int erofs_init_fs_context(struct fs_context *fc) 722 { 723 struct erofs_fs_context *ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); 724 725 if (!ctx) 726 return -ENOMEM; 727 ctx->devs = kzalloc(sizeof(struct erofs_dev_context), GFP_KERNEL); 728 if (!ctx->devs) { 729 kfree(ctx); 730 return -ENOMEM; 731 } 732 fc->fs_private = ctx; 733 734 idr_init(&ctx->devs->tree); 735 init_rwsem(&ctx->devs->rwsem); 736 erofs_default_options(ctx); 737 fc->ops = &erofs_context_ops; 738 return 0; 739 } 740 741 /* 742 * could be triggered after deactivate_locked_super() 743 * is called, thus including umount and failed to initialize. 744 */ 745 static void erofs_kill_sb(struct super_block *sb) 746 { 747 struct erofs_sb_info *sbi; 748 749 WARN_ON(sb->s_magic != EROFS_SUPER_MAGIC); 750 751 kill_block_super(sb); 752 753 sbi = EROFS_SB(sb); 754 if (!sbi) 755 return; 756 757 erofs_free_dev_context(sbi->devs); 758 fs_put_dax(sbi->dax_dev); 759 kfree(sbi); 760 sb->s_fs_info = NULL; 761 } 762 763 /* called when ->s_root is non-NULL */ 764 static void erofs_put_super(struct super_block *sb) 765 { 766 struct erofs_sb_info *const sbi = EROFS_SB(sb); 767 768 DBG_BUGON(!sbi); 769 770 erofs_unregister_sysfs(sb); 771 erofs_shrinker_unregister(sb); 772 #ifdef CONFIG_EROFS_FS_ZIP 773 iput(sbi->managed_cache); 774 sbi->managed_cache = NULL; 775 #endif 776 } 777 778 static struct file_system_type erofs_fs_type = { 779 .owner = THIS_MODULE, 780 .name = "erofs", 781 .init_fs_context = erofs_init_fs_context, 782 .kill_sb = erofs_kill_sb, 783 .fs_flags = FS_REQUIRES_DEV, 784 }; 785 MODULE_ALIAS_FS("erofs"); 786 787 static int __init erofs_module_init(void) 788 { 789 int err; 790 791 erofs_check_ondisk_layout_definitions(); 792 793 erofs_inode_cachep = kmem_cache_create("erofs_inode", 794 sizeof(struct erofs_inode), 0, 795 SLAB_RECLAIM_ACCOUNT, 796 erofs_inode_init_once); 797 if (!erofs_inode_cachep) { 798 err = -ENOMEM; 799 goto icache_err; 800 } 801 802 err = erofs_init_shrinker(); 803 if (err) 804 goto shrinker_err; 805 806 err = z_erofs_lzma_init(); 807 if (err) 808 goto lzma_err; 809 810 erofs_pcpubuf_init(); 811 err = z_erofs_init_zip_subsystem(); 812 if (err) 813 goto zip_err; 814 815 err = erofs_init_sysfs(); 816 if (err) 817 goto sysfs_err; 818 819 err = register_filesystem(&erofs_fs_type); 820 if (err) 821 goto fs_err; 822 823 return 0; 824 825 fs_err: 826 erofs_exit_sysfs(); 827 sysfs_err: 828 z_erofs_exit_zip_subsystem(); 829 zip_err: 830 z_erofs_lzma_exit(); 831 lzma_err: 832 erofs_exit_shrinker(); 833 shrinker_err: 834 kmem_cache_destroy(erofs_inode_cachep); 835 icache_err: 836 return err; 837 } 838 839 static void __exit erofs_module_exit(void) 840 { 841 unregister_filesystem(&erofs_fs_type); 842 843 /* Ensure all RCU free inodes / pclusters are safe to be destroyed. */ 844 rcu_barrier(); 845 846 erofs_exit_sysfs(); 847 z_erofs_exit_zip_subsystem(); 848 z_erofs_lzma_exit(); 849 erofs_exit_shrinker(); 850 kmem_cache_destroy(erofs_inode_cachep); 851 erofs_pcpubuf_exit(); 852 } 853 854 /* get filesystem statistics */ 855 static int erofs_statfs(struct dentry *dentry, struct kstatfs *buf) 856 { 857 struct super_block *sb = dentry->d_sb; 858 struct erofs_sb_info *sbi = EROFS_SB(sb); 859 u64 id = huge_encode_dev(sb->s_bdev->bd_dev); 860 861 buf->f_type = sb->s_magic; 862 buf->f_bsize = EROFS_BLKSIZ; 863 buf->f_blocks = sbi->total_blocks; 864 buf->f_bfree = buf->f_bavail = 0; 865 866 buf->f_files = ULLONG_MAX; 867 buf->f_ffree = ULLONG_MAX - sbi->inos; 868 869 buf->f_namelen = EROFS_NAME_LEN; 870 871 buf->f_fsid = u64_to_fsid(id); 872 return 0; 873 } 874 875 static int erofs_show_options(struct seq_file *seq, struct dentry *root) 876 { 877 struct erofs_sb_info *sbi = EROFS_SB(root->d_sb); 878 struct erofs_mount_opts *opt = &sbi->opt; 879 880 #ifdef CONFIG_EROFS_FS_XATTR 881 if (test_opt(opt, XATTR_USER)) 882 seq_puts(seq, ",user_xattr"); 883 else 884 seq_puts(seq, ",nouser_xattr"); 885 #endif 886 #ifdef CONFIG_EROFS_FS_POSIX_ACL 887 if (test_opt(opt, POSIX_ACL)) 888 seq_puts(seq, ",acl"); 889 else 890 seq_puts(seq, ",noacl"); 891 #endif 892 #ifdef CONFIG_EROFS_FS_ZIP 893 if (opt->cache_strategy == EROFS_ZIP_CACHE_DISABLED) 894 seq_puts(seq, ",cache_strategy=disabled"); 895 else if (opt->cache_strategy == EROFS_ZIP_CACHE_READAHEAD) 896 seq_puts(seq, ",cache_strategy=readahead"); 897 else if (opt->cache_strategy == EROFS_ZIP_CACHE_READAROUND) 898 seq_puts(seq, ",cache_strategy=readaround"); 899 #endif 900 if (test_opt(opt, DAX_ALWAYS)) 901 seq_puts(seq, ",dax=always"); 902 if (test_opt(opt, DAX_NEVER)) 903 seq_puts(seq, ",dax=never"); 904 return 0; 905 } 906 907 const struct super_operations erofs_sops = { 908 .put_super = erofs_put_super, 909 .alloc_inode = erofs_alloc_inode, 910 .free_inode = erofs_free_inode, 911 .statfs = erofs_statfs, 912 .show_options = erofs_show_options, 913 }; 914 915 module_init(erofs_module_init); 916 module_exit(erofs_module_exit); 917 918 MODULE_DESCRIPTION("Enhanced ROM File System"); 919 MODULE_AUTHOR("Gao Xiang, Chao Yu, Miao Xie, CONSUMER BG, HUAWEI Inc."); 920 MODULE_LICENSE("GPL"); 921