1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2017-2018 HUAWEI, Inc.
4 * https://www.huawei.com/
6 #include <linux/module.h>
7 #include <linux/buffer_head.h>
8 #include <linux/statfs.h>
9 #include <linux/parser.h>
10 #include <linux/seq_file.h>
11 #include <linux/crc32c.h>
12 #include <linux/fs_context.h>
13 #include <linux/fs_parser.h>
14 #include <linux/dax.h>
17 #define CREATE_TRACE_POINTS
18 #include <trace/events/erofs.h>
20 static struct kmem_cache *erofs_inode_cachep __read_mostly;
22 void _erofs_err(struct super_block *sb, const char *function,
33 pr_err("(device %s): %s: %pV", sb->s_id, function, &vaf);
37 void _erofs_info(struct super_block *sb, const char *function,
48 pr_info("(device %s): %pV", sb->s_id, &vaf);
52 static int erofs_superblock_csum_verify(struct super_block *sb, void *sbdata)
54 struct erofs_super_block *dsb;
55 u32 expected_crc, crc;
57 dsb = kmemdup(sbdata + EROFS_SUPER_OFFSET,
58 EROFS_BLKSIZ - EROFS_SUPER_OFFSET, GFP_KERNEL);
62 expected_crc = le32_to_cpu(dsb->checksum);
64 /* to allow for x86 boot sectors and other oddities. */
65 crc = crc32c(~0, dsb, EROFS_BLKSIZ - EROFS_SUPER_OFFSET);
68 if (crc != expected_crc) {
69 erofs_err(sb, "invalid checksum 0x%08x, 0x%08x expected",
76 static void erofs_inode_init_once(void *ptr)
78 struct erofs_inode *vi = ptr;
80 inode_init_once(&vi->vfs_inode);
83 static struct inode *erofs_alloc_inode(struct super_block *sb)
85 struct erofs_inode *vi =
86 kmem_cache_alloc(erofs_inode_cachep, GFP_KERNEL);
91 /* zero out everything except vfs_inode */
92 memset(vi, 0, offsetof(struct erofs_inode, vfs_inode));
93 return &vi->vfs_inode;
96 static void erofs_free_inode(struct inode *inode)
98 struct erofs_inode *vi = EROFS_I(inode);
100 /* be careful of RCU symlink path */
101 if (inode->i_op == &erofs_fast_symlink_iops)
102 kfree(inode->i_link);
103 kfree(vi->xattr_shared_xattrs);
105 kmem_cache_free(erofs_inode_cachep, vi);
108 static bool check_layout_compatibility(struct super_block *sb,
109 struct erofs_super_block *dsb)
111 const unsigned int feature = le32_to_cpu(dsb->feature_incompat);
113 EROFS_SB(sb)->feature_incompat = feature;
115 /* check if current kernel meets all mandatory requirements */
116 if (feature & (~EROFS_ALL_FEATURE_INCOMPAT)) {
118 "unidentified incompatible feature %x, please upgrade kernel version",
119 feature & ~EROFS_ALL_FEATURE_INCOMPAT);
125 #ifdef CONFIG_EROFS_FS_ZIP
126 /* read variable-sized metadata, offset will be aligned by 4-byte */
127 static void *erofs_read_metadata(struct super_block *sb, struct page **pagep,
128 erofs_off_t *offset, int *lengthp)
130 struct page *page = *pagep;
135 *offset = round_up(*offset, 4);
136 blk = erofs_blknr(*offset);
138 if (!page || page->index != blk) {
143 page = erofs_get_meta_page(sb, blk);
149 len = le16_to_cpu(*(__le16 *)&ptr[erofs_blkoff(*offset)]);
152 buffer = kmalloc(len, GFP_KERNEL);
154 buffer = ERR_PTR(-ENOMEM);
157 *offset += sizeof(__le16);
160 for (i = 0; i < len; i += cnt) {
161 cnt = min(EROFS_BLKSIZ - (int)erofs_blkoff(*offset), len - i);
162 blk = erofs_blknr(*offset);
164 if (!page || page->index != blk) {
170 page = erofs_get_meta_page(sb, blk);
177 memcpy(buffer + i, ptr + erofs_blkoff(*offset), cnt);
189 static int erofs_load_compr_cfgs(struct super_block *sb,
190 struct erofs_super_block *dsb)
192 struct erofs_sb_info *sbi;
194 unsigned int algs, alg;
199 sbi->available_compr_algs = le16_to_cpu(dsb->u1.available_compr_algs);
201 if (sbi->available_compr_algs & ~Z_EROFS_ALL_COMPR_ALGS) {
202 erofs_err(sb, "try to load compressed fs with unsupported algorithms %x",
203 sbi->available_compr_algs & ~Z_EROFS_ALL_COMPR_ALGS);
207 offset = EROFS_SUPER_OFFSET + sbi->sb_size;
212 for (algs = sbi->available_compr_algs; algs; algs >>= 1, ++alg) {
218 data = erofs_read_metadata(sb, &page, &offset, &size);
225 case Z_EROFS_COMPRESSION_LZ4:
226 ret = z_erofs_load_lz4_config(sb, dsb, data, size);
244 static int erofs_load_compr_cfgs(struct super_block *sb,
245 struct erofs_super_block *dsb)
247 if (dsb->u1.available_compr_algs) {
248 erofs_err(sb, "try to load compressed fs when compression is disabled");
255 static int erofs_read_superblock(struct super_block *sb)
257 struct erofs_sb_info *sbi;
259 struct erofs_super_block *dsb;
260 unsigned int blkszbits;
264 page = read_mapping_page(sb->s_bdev->bd_inode->i_mapping, 0, NULL);
266 erofs_err(sb, "cannot read erofs superblock");
267 return PTR_ERR(page);
273 dsb = (struct erofs_super_block *)(data + EROFS_SUPER_OFFSET);
276 if (le32_to_cpu(dsb->magic) != EROFS_SUPER_MAGIC_V1) {
277 erofs_err(sb, "cannot find valid erofs superblock");
281 sbi->feature_compat = le32_to_cpu(dsb->feature_compat);
282 if (erofs_sb_has_sb_chksum(sbi)) {
283 ret = erofs_superblock_csum_verify(sb, data);
289 blkszbits = dsb->blkszbits;
290 /* 9(512 bytes) + LOG_SECTORS_PER_BLOCK == LOG_BLOCK_SIZE */
291 if (blkszbits != LOG_BLOCK_SIZE) {
292 erofs_err(sb, "blkszbits %u isn't supported on this platform",
297 if (!check_layout_compatibility(sb, dsb))
300 sbi->sb_size = 128 + dsb->sb_extslots * EROFS_SB_EXTSLOT_SIZE;
301 if (sbi->sb_size > EROFS_BLKSIZ) {
302 erofs_err(sb, "invalid sb_extslots %u (more than a fs block)",
306 sbi->blocks = le32_to_cpu(dsb->blocks);
307 sbi->meta_blkaddr = le32_to_cpu(dsb->meta_blkaddr);
308 #ifdef CONFIG_EROFS_FS_XATTR
309 sbi->xattr_blkaddr = le32_to_cpu(dsb->xattr_blkaddr);
311 sbi->islotbits = ilog2(sizeof(struct erofs_inode_compact));
312 sbi->root_nid = le16_to_cpu(dsb->root_nid);
313 sbi->inos = le64_to_cpu(dsb->inos);
315 sbi->build_time = le64_to_cpu(dsb->build_time);
316 sbi->build_time_nsec = le32_to_cpu(dsb->build_time_nsec);
318 memcpy(&sb->s_uuid, dsb->uuid, sizeof(dsb->uuid));
320 ret = strscpy(sbi->volume_name, dsb->volume_name,
321 sizeof(dsb->volume_name));
322 if (ret < 0) { /* -E2BIG */
323 erofs_err(sb, "bad volume name without NIL terminator");
328 /* parse on-disk compression configurations */
329 if (erofs_sb_has_compr_cfgs(sbi))
330 ret = erofs_load_compr_cfgs(sb, dsb);
332 ret = z_erofs_load_lz4_config(sb, dsb, NULL, 0);
339 /* set up default EROFS parameters */
340 static void erofs_default_options(struct erofs_fs_context *ctx)
342 #ifdef CONFIG_EROFS_FS_ZIP
343 ctx->cache_strategy = EROFS_ZIP_CACHE_READAROUND;
344 ctx->max_sync_decompress_pages = 3;
345 ctx->readahead_sync_decompress = false;
347 #ifdef CONFIG_EROFS_FS_XATTR
348 set_opt(ctx, XATTR_USER);
350 #ifdef CONFIG_EROFS_FS_POSIX_ACL
351 set_opt(ctx, POSIX_ACL);
364 static const struct constant_table erofs_param_cache_strategy[] = {
365 {"disabled", EROFS_ZIP_CACHE_DISABLED},
366 {"readahead", EROFS_ZIP_CACHE_READAHEAD},
367 {"readaround", EROFS_ZIP_CACHE_READAROUND},
371 static const struct constant_table erofs_dax_param_enums[] = {
372 {"always", EROFS_MOUNT_DAX_ALWAYS},
373 {"never", EROFS_MOUNT_DAX_NEVER},
377 static const struct fs_parameter_spec erofs_fs_parameters[] = {
378 fsparam_flag_no("user_xattr", Opt_user_xattr),
379 fsparam_flag_no("acl", Opt_acl),
380 fsparam_enum("cache_strategy", Opt_cache_strategy,
381 erofs_param_cache_strategy),
382 fsparam_flag("dax", Opt_dax),
383 fsparam_enum("dax", Opt_dax_enum, erofs_dax_param_enums),
387 static bool erofs_fc_set_dax_mode(struct fs_context *fc, unsigned int mode)
390 struct erofs_fs_context *ctx = fc->fs_private;
393 case EROFS_MOUNT_DAX_ALWAYS:
394 warnfc(fc, "DAX enabled. Warning: EXPERIMENTAL, use at your own risk");
395 set_opt(ctx, DAX_ALWAYS);
396 clear_opt(ctx, DAX_NEVER);
398 case EROFS_MOUNT_DAX_NEVER:
399 set_opt(ctx, DAX_NEVER);
400 clear_opt(ctx, DAX_ALWAYS);
407 errorfc(fc, "dax options not supported");
412 static int erofs_fc_parse_param(struct fs_context *fc,
413 struct fs_parameter *param)
415 struct erofs_fs_context *ctx __maybe_unused = fc->fs_private;
416 struct fs_parse_result result;
419 opt = fs_parse(fc, erofs_fs_parameters, param, &result);
425 #ifdef CONFIG_EROFS_FS_XATTR
427 set_opt(ctx, XATTR_USER);
429 clear_opt(ctx, XATTR_USER);
431 errorfc(fc, "{,no}user_xattr options not supported");
435 #ifdef CONFIG_EROFS_FS_POSIX_ACL
437 set_opt(ctx, POSIX_ACL);
439 clear_opt(ctx, POSIX_ACL);
441 errorfc(fc, "{,no}acl options not supported");
444 case Opt_cache_strategy:
445 #ifdef CONFIG_EROFS_FS_ZIP
446 ctx->cache_strategy = result.uint_32;
448 errorfc(fc, "compression not supported, cache_strategy ignored");
452 if (!erofs_fc_set_dax_mode(fc, EROFS_MOUNT_DAX_ALWAYS))
456 if (!erofs_fc_set_dax_mode(fc, result.uint_32))
465 #ifdef CONFIG_EROFS_FS_ZIP
466 static const struct address_space_operations managed_cache_aops;
468 static int erofs_managed_cache_releasepage(struct page *page, gfp_t gfp_mask)
470 int ret = 1; /* 0 - busy */
471 struct address_space *const mapping = page->mapping;
473 DBG_BUGON(!PageLocked(page));
474 DBG_BUGON(mapping->a_ops != &managed_cache_aops);
476 if (PagePrivate(page))
477 ret = erofs_try_to_free_cached_page(page);
482 static void erofs_managed_cache_invalidatepage(struct page *page,
486 const unsigned int stop = length + offset;
488 DBG_BUGON(!PageLocked(page));
490 /* Check for potential overflow in debug mode */
491 DBG_BUGON(stop > PAGE_SIZE || stop < length);
493 if (offset == 0 && stop == PAGE_SIZE)
494 while (!erofs_managed_cache_releasepage(page, GFP_NOFS))
498 static const struct address_space_operations managed_cache_aops = {
499 .releasepage = erofs_managed_cache_releasepage,
500 .invalidatepage = erofs_managed_cache_invalidatepage,
503 static int erofs_init_managed_cache(struct super_block *sb)
505 struct erofs_sb_info *const sbi = EROFS_SB(sb);
506 struct inode *const inode = new_inode(sb);
512 inode->i_size = OFFSET_MAX;
514 inode->i_mapping->a_ops = &managed_cache_aops;
515 mapping_set_gfp_mask(inode->i_mapping,
516 GFP_NOFS | __GFP_HIGHMEM | __GFP_MOVABLE);
517 sbi->managed_cache = inode;
521 static int erofs_init_managed_cache(struct super_block *sb) { return 0; }
524 static int erofs_fc_fill_super(struct super_block *sb, struct fs_context *fc)
527 struct erofs_sb_info *sbi;
528 struct erofs_fs_context *ctx = fc->fs_private;
531 sb->s_magic = EROFS_SUPER_MAGIC;
533 if (!sb_set_blocksize(sb, EROFS_BLKSIZ)) {
534 erofs_err(sb, "failed to set erofs blksize");
538 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
543 sbi->dax_dev = fs_dax_get_by_bdev(sb->s_bdev);
544 err = erofs_read_superblock(sb);
548 if (test_opt(ctx, DAX_ALWAYS) &&
549 !dax_supported(sbi->dax_dev, sb->s_bdev, EROFS_BLKSIZ, 0, bdev_nr_sectors(sb->s_bdev))) {
550 errorfc(fc, "DAX unsupported by block device. Turning off DAX.");
551 clear_opt(ctx, DAX_ALWAYS);
553 sb->s_flags |= SB_RDONLY | SB_NOATIME;
554 sb->s_maxbytes = MAX_LFS_FILESIZE;
557 sb->s_op = &erofs_sops;
558 sb->s_xattr = erofs_xattr_handlers;
560 if (test_opt(ctx, POSIX_ACL))
561 sb->s_flags |= SB_POSIXACL;
563 sb->s_flags &= ~SB_POSIXACL;
567 #ifdef CONFIG_EROFS_FS_ZIP
568 xa_init(&sbi->managed_pslots);
571 /* get the root inode */
572 inode = erofs_iget(sb, ROOT_NID(sbi), true);
574 return PTR_ERR(inode);
576 if (!S_ISDIR(inode->i_mode)) {
577 erofs_err(sb, "rootino(nid %llu) is not a directory(i_mode %o)",
578 ROOT_NID(sbi), inode->i_mode);
583 sb->s_root = d_make_root(inode);
587 erofs_shrinker_register(sb);
588 /* sb->s_umount is already locked, SB_ACTIVE and SB_BORN are not set */
589 err = erofs_init_managed_cache(sb);
593 erofs_info(sb, "mounted with root inode @ nid %llu.", ROOT_NID(sbi));
597 static int erofs_fc_get_tree(struct fs_context *fc)
599 return get_tree_bdev(fc, erofs_fc_fill_super);
602 static int erofs_fc_reconfigure(struct fs_context *fc)
604 struct super_block *sb = fc->root->d_sb;
605 struct erofs_sb_info *sbi = EROFS_SB(sb);
606 struct erofs_fs_context *ctx = fc->fs_private;
608 DBG_BUGON(!sb_rdonly(sb));
610 if (test_opt(ctx, POSIX_ACL))
611 fc->sb_flags |= SB_POSIXACL;
613 fc->sb_flags &= ~SB_POSIXACL;
617 fc->sb_flags |= SB_RDONLY;
621 static void erofs_fc_free(struct fs_context *fc)
623 kfree(fc->fs_private);
626 static const struct fs_context_operations erofs_context_ops = {
627 .parse_param = erofs_fc_parse_param,
628 .get_tree = erofs_fc_get_tree,
629 .reconfigure = erofs_fc_reconfigure,
630 .free = erofs_fc_free,
633 static int erofs_init_fs_context(struct fs_context *fc)
635 fc->fs_private = kzalloc(sizeof(struct erofs_fs_context), GFP_KERNEL);
639 /* set default mount options */
640 erofs_default_options(fc->fs_private);
642 fc->ops = &erofs_context_ops;
648 * could be triggered after deactivate_locked_super()
649 * is called, thus including umount and failed to initialize.
651 static void erofs_kill_sb(struct super_block *sb)
653 struct erofs_sb_info *sbi;
655 WARN_ON(sb->s_magic != EROFS_SUPER_MAGIC);
657 kill_block_super(sb);
662 fs_put_dax(sbi->dax_dev);
664 sb->s_fs_info = NULL;
667 /* called when ->s_root is non-NULL */
668 static void erofs_put_super(struct super_block *sb)
670 struct erofs_sb_info *const sbi = EROFS_SB(sb);
674 erofs_shrinker_unregister(sb);
675 #ifdef CONFIG_EROFS_FS_ZIP
676 iput(sbi->managed_cache);
677 sbi->managed_cache = NULL;
681 static struct file_system_type erofs_fs_type = {
682 .owner = THIS_MODULE,
684 .init_fs_context = erofs_init_fs_context,
685 .kill_sb = erofs_kill_sb,
686 .fs_flags = FS_REQUIRES_DEV,
688 MODULE_ALIAS_FS("erofs");
690 static int __init erofs_module_init(void)
694 erofs_check_ondisk_layout_definitions();
696 erofs_inode_cachep = kmem_cache_create("erofs_inode",
697 sizeof(struct erofs_inode), 0,
698 SLAB_RECLAIM_ACCOUNT,
699 erofs_inode_init_once);
700 if (!erofs_inode_cachep) {
705 err = erofs_init_shrinker();
709 erofs_pcpubuf_init();
710 err = z_erofs_init_zip_subsystem();
714 err = register_filesystem(&erofs_fs_type);
721 z_erofs_exit_zip_subsystem();
723 erofs_exit_shrinker();
725 kmem_cache_destroy(erofs_inode_cachep);
730 static void __exit erofs_module_exit(void)
732 unregister_filesystem(&erofs_fs_type);
733 z_erofs_exit_zip_subsystem();
734 erofs_exit_shrinker();
736 /* Ensure all RCU free inodes are safe before cache is destroyed. */
738 kmem_cache_destroy(erofs_inode_cachep);
739 erofs_pcpubuf_exit();
742 /* get filesystem statistics */
743 static int erofs_statfs(struct dentry *dentry, struct kstatfs *buf)
745 struct super_block *sb = dentry->d_sb;
746 struct erofs_sb_info *sbi = EROFS_SB(sb);
747 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
749 buf->f_type = sb->s_magic;
750 buf->f_bsize = EROFS_BLKSIZ;
751 buf->f_blocks = sbi->blocks;
752 buf->f_bfree = buf->f_bavail = 0;
754 buf->f_files = ULLONG_MAX;
755 buf->f_ffree = ULLONG_MAX - sbi->inos;
757 buf->f_namelen = EROFS_NAME_LEN;
759 buf->f_fsid = u64_to_fsid(id);
763 static int erofs_show_options(struct seq_file *seq, struct dentry *root)
765 struct erofs_sb_info *sbi = EROFS_SB(root->d_sb);
766 struct erofs_fs_context *ctx = &sbi->ctx;
768 #ifdef CONFIG_EROFS_FS_XATTR
769 if (test_opt(ctx, XATTR_USER))
770 seq_puts(seq, ",user_xattr");
772 seq_puts(seq, ",nouser_xattr");
774 #ifdef CONFIG_EROFS_FS_POSIX_ACL
775 if (test_opt(ctx, POSIX_ACL))
776 seq_puts(seq, ",acl");
778 seq_puts(seq, ",noacl");
780 #ifdef CONFIG_EROFS_FS_ZIP
781 if (ctx->cache_strategy == EROFS_ZIP_CACHE_DISABLED)
782 seq_puts(seq, ",cache_strategy=disabled");
783 else if (ctx->cache_strategy == EROFS_ZIP_CACHE_READAHEAD)
784 seq_puts(seq, ",cache_strategy=readahead");
785 else if (ctx->cache_strategy == EROFS_ZIP_CACHE_READAROUND)
786 seq_puts(seq, ",cache_strategy=readaround");
788 if (test_opt(ctx, DAX_ALWAYS))
789 seq_puts(seq, ",dax=always");
790 if (test_opt(ctx, DAX_NEVER))
791 seq_puts(seq, ",dax=never");
795 const struct super_operations erofs_sops = {
796 .put_super = erofs_put_super,
797 .alloc_inode = erofs_alloc_inode,
798 .free_inode = erofs_free_inode,
799 .statfs = erofs_statfs,
800 .show_options = erofs_show_options,
803 module_init(erofs_module_init);
804 module_exit(erofs_module_exit);
806 MODULE_DESCRIPTION("Enhanced ROM File System");
807 MODULE_AUTHOR("Gao Xiang, Chao Yu, Miao Xie, CONSUMER BG, HUAWEI Inc.");
808 MODULE_LICENSE("GPL");