1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2017-2018 HUAWEI, Inc.
4 * https://www.huawei.com/
5 * Copyright (C) 2021, Alibaba Cloud
7 #include <linux/module.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>
15 #include <linux/exportfs.h>
18 #define CREATE_TRACE_POINTS
19 #include <trace/events/erofs.h>
21 static struct kmem_cache *erofs_inode_cachep __read_mostly;
22 struct file_system_type erofs_fs_type;
24 void _erofs_err(struct super_block *sb, const char *function,
35 pr_err("(device %s): %s: %pV", sb->s_id, function, &vaf);
39 void _erofs_info(struct super_block *sb, const char *function,
50 pr_info("(device %s): %pV", sb->s_id, &vaf);
54 static int erofs_superblock_csum_verify(struct super_block *sb, void *sbdata)
56 size_t len = 1 << EROFS_SB(sb)->blkszbits;
57 struct erofs_super_block *dsb;
58 u32 expected_crc, crc;
60 if (len > EROFS_SUPER_OFFSET)
61 len -= EROFS_SUPER_OFFSET;
63 dsb = kmemdup(sbdata + EROFS_SUPER_OFFSET, len, GFP_KERNEL);
67 expected_crc = le32_to_cpu(dsb->checksum);
69 /* to allow for x86 boot sectors and other oddities. */
70 crc = crc32c(~0, dsb, len);
73 if (crc != expected_crc) {
74 erofs_err(sb, "invalid checksum 0x%08x, 0x%08x expected",
81 static void erofs_inode_init_once(void *ptr)
83 struct erofs_inode *vi = ptr;
85 inode_init_once(&vi->vfs_inode);
88 static struct inode *erofs_alloc_inode(struct super_block *sb)
90 struct erofs_inode *vi =
91 alloc_inode_sb(sb, erofs_inode_cachep, GFP_KERNEL);
96 /* zero out everything except vfs_inode */
97 memset(vi, 0, offsetof(struct erofs_inode, vfs_inode));
98 return &vi->vfs_inode;
101 static void erofs_free_inode(struct inode *inode)
103 struct erofs_inode *vi = EROFS_I(inode);
105 /* be careful of RCU symlink path */
106 if (inode->i_op == &erofs_fast_symlink_iops)
107 kfree(inode->i_link);
108 kfree(vi->xattr_shared_xattrs);
110 kmem_cache_free(erofs_inode_cachep, vi);
113 static bool check_layout_compatibility(struct super_block *sb,
114 struct erofs_super_block *dsb)
116 const unsigned int feature = le32_to_cpu(dsb->feature_incompat);
118 EROFS_SB(sb)->feature_incompat = feature;
120 /* check if current kernel meets all mandatory requirements */
121 if (feature & (~EROFS_ALL_FEATURE_INCOMPAT)) {
123 "unidentified incompatible feature %x, please upgrade kernel version",
124 feature & ~EROFS_ALL_FEATURE_INCOMPAT);
130 /* read variable-sized metadata, offset will be aligned by 4-byte */
131 void *erofs_read_metadata(struct super_block *sb, struct erofs_buf *buf,
132 erofs_off_t *offset, int *lengthp)
137 *offset = round_up(*offset, 4);
138 ptr = erofs_bread(buf, erofs_blknr(sb, *offset), EROFS_KMAP);
142 len = le16_to_cpu(*(__le16 *)&ptr[erofs_blkoff(sb, *offset)]);
145 buffer = kmalloc(len, GFP_KERNEL);
147 return ERR_PTR(-ENOMEM);
148 *offset += sizeof(__le16);
151 for (i = 0; i < len; i += cnt) {
152 cnt = min_t(int, sb->s_blocksize - erofs_blkoff(sb, *offset),
154 ptr = erofs_bread(buf, erofs_blknr(sb, *offset), EROFS_KMAP);
159 memcpy(buffer + i, ptr + erofs_blkoff(sb, *offset), cnt);
165 #ifdef CONFIG_EROFS_FS_ZIP
166 static int erofs_load_compr_cfgs(struct super_block *sb,
167 struct erofs_super_block *dsb)
169 struct erofs_sb_info *sbi = EROFS_SB(sb);
170 struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
171 unsigned int algs, alg;
175 sbi->available_compr_algs = le16_to_cpu(dsb->u1.available_compr_algs);
176 if (sbi->available_compr_algs & ~Z_EROFS_ALL_COMPR_ALGS) {
177 erofs_err(sb, "try to load compressed fs with unsupported algorithms %x",
178 sbi->available_compr_algs & ~Z_EROFS_ALL_COMPR_ALGS);
182 erofs_init_metabuf(&buf, sb);
183 offset = EROFS_SUPER_OFFSET + sbi->sb_size;
185 for (algs = sbi->available_compr_algs; algs; algs >>= 1, ++alg) {
191 data = erofs_read_metadata(sb, &buf, &offset, &size);
198 case Z_EROFS_COMPRESSION_LZ4:
199 ret = z_erofs_load_lz4_config(sb, dsb, data, size);
201 case Z_EROFS_COMPRESSION_LZMA:
202 ret = z_erofs_load_lzma_config(sb, dsb, data, size);
212 erofs_put_metabuf(&buf);
216 static int erofs_load_compr_cfgs(struct super_block *sb,
217 struct erofs_super_block *dsb)
219 if (dsb->u1.available_compr_algs) {
220 erofs_err(sb, "try to load compressed fs when compression is disabled");
227 static int erofs_init_device(struct erofs_buf *buf, struct super_block *sb,
228 struct erofs_device_info *dif, erofs_off_t *pos)
230 struct erofs_sb_info *sbi = EROFS_SB(sb);
231 struct erofs_fscache *fscache;
232 struct erofs_deviceslot *dis;
233 struct block_device *bdev;
236 ptr = erofs_read_metabuf(buf, sb, erofs_blknr(sb, *pos), EROFS_KMAP);
239 dis = ptr + erofs_blkoff(sb, *pos);
243 erofs_err(sb, "empty device tag @ pos %llu", *pos);
246 dif->path = kmemdup_nul(dis->tag, sizeof(dis->tag), GFP_KERNEL);
251 if (erofs_is_fscache_mode(sb)) {
252 fscache = erofs_fscache_register_cookie(sb, dif->path, 0);
254 return PTR_ERR(fscache);
255 dif->fscache = fscache;
256 } else if (!sbi->devs->flatdev) {
257 bdev = blkdev_get_by_path(dif->path, BLK_OPEN_READ, sb->s_type,
260 return PTR_ERR(bdev);
262 dif->dax_dev = fs_dax_get_by_bdev(bdev, &dif->dax_part_off,
266 dif->blocks = le32_to_cpu(dis->blocks);
267 dif->mapped_blkaddr = le32_to_cpu(dis->mapped_blkaddr);
268 sbi->total_blocks += dif->blocks;
269 *pos += EROFS_DEVT_SLOT_SIZE;
273 static int erofs_scan_devices(struct super_block *sb,
274 struct erofs_super_block *dsb)
276 struct erofs_sb_info *sbi = EROFS_SB(sb);
277 unsigned int ondisk_extradevs;
279 struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
280 struct erofs_device_info *dif;
283 sbi->total_blocks = sbi->primarydevice_blocks;
284 if (!erofs_sb_has_device_table(sbi))
285 ondisk_extradevs = 0;
287 ondisk_extradevs = le16_to_cpu(dsb->extra_devices);
289 if (sbi->devs->extra_devices &&
290 ondisk_extradevs != sbi->devs->extra_devices) {
291 erofs_err(sb, "extra devices don't match (ondisk %u, given %u)",
292 ondisk_extradevs, sbi->devs->extra_devices);
295 if (!ondisk_extradevs)
298 if (!sbi->devs->extra_devices && !erofs_is_fscache_mode(sb))
299 sbi->devs->flatdev = true;
301 sbi->device_id_mask = roundup_pow_of_two(ondisk_extradevs + 1) - 1;
302 pos = le16_to_cpu(dsb->devt_slotoff) * EROFS_DEVT_SLOT_SIZE;
303 down_read(&sbi->devs->rwsem);
304 if (sbi->devs->extra_devices) {
305 idr_for_each_entry(&sbi->devs->tree, dif, id) {
306 err = erofs_init_device(&buf, sb, dif, &pos);
311 for (id = 0; id < ondisk_extradevs; id++) {
312 dif = kzalloc(sizeof(*dif), GFP_KERNEL);
318 err = idr_alloc(&sbi->devs->tree, dif, 0, 0, GFP_KERNEL);
323 ++sbi->devs->extra_devices;
325 err = erofs_init_device(&buf, sb, dif, &pos);
330 up_read(&sbi->devs->rwsem);
331 erofs_put_metabuf(&buf);
335 static int erofs_read_superblock(struct super_block *sb)
337 struct erofs_sb_info *sbi;
338 struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
339 struct erofs_super_block *dsb;
343 data = erofs_read_metabuf(&buf, sb, 0, EROFS_KMAP);
345 erofs_err(sb, "cannot read erofs superblock");
346 return PTR_ERR(data);
350 dsb = (struct erofs_super_block *)(data + EROFS_SUPER_OFFSET);
353 if (le32_to_cpu(dsb->magic) != EROFS_SUPER_MAGIC_V1) {
354 erofs_err(sb, "cannot find valid erofs superblock");
358 sbi->blkszbits = dsb->blkszbits;
359 if (sbi->blkszbits < 9 || sbi->blkszbits > PAGE_SHIFT) {
360 erofs_err(sb, "blkszbits %u isn't supported", sbi->blkszbits);
363 if (dsb->dirblkbits) {
364 erofs_err(sb, "dirblkbits %u isn't supported", dsb->dirblkbits);
368 sbi->feature_compat = le32_to_cpu(dsb->feature_compat);
369 if (erofs_sb_has_sb_chksum(sbi)) {
370 ret = erofs_superblock_csum_verify(sb, data);
376 if (!check_layout_compatibility(sb, dsb))
379 sbi->sb_size = 128 + dsb->sb_extslots * EROFS_SB_EXTSLOT_SIZE;
380 if (sbi->sb_size > PAGE_SIZE - EROFS_SUPER_OFFSET) {
381 erofs_err(sb, "invalid sb_extslots %u (more than a fs block)",
385 sbi->primarydevice_blocks = le32_to_cpu(dsb->blocks);
386 sbi->meta_blkaddr = le32_to_cpu(dsb->meta_blkaddr);
387 #ifdef CONFIG_EROFS_FS_XATTR
388 sbi->xattr_blkaddr = le32_to_cpu(dsb->xattr_blkaddr);
389 sbi->xattr_prefix_start = le32_to_cpu(dsb->xattr_prefix_start);
390 sbi->xattr_prefix_count = dsb->xattr_prefix_count;
392 sbi->islotbits = ilog2(sizeof(struct erofs_inode_compact));
393 sbi->root_nid = le16_to_cpu(dsb->root_nid);
394 sbi->packed_nid = le64_to_cpu(dsb->packed_nid);
395 sbi->inos = le64_to_cpu(dsb->inos);
397 sbi->build_time = le64_to_cpu(dsb->build_time);
398 sbi->build_time_nsec = le32_to_cpu(dsb->build_time_nsec);
400 memcpy(&sb->s_uuid, dsb->uuid, sizeof(dsb->uuid));
402 ret = strscpy(sbi->volume_name, dsb->volume_name,
403 sizeof(dsb->volume_name));
404 if (ret < 0) { /* -E2BIG */
405 erofs_err(sb, "bad volume name without NIL terminator");
410 /* parse on-disk compression configurations */
411 if (erofs_sb_has_compr_cfgs(sbi))
412 ret = erofs_load_compr_cfgs(sb, dsb);
414 ret = z_erofs_load_lz4_config(sb, dsb, NULL, 0);
418 /* handle multiple devices */
419 ret = erofs_scan_devices(sb, dsb);
421 if (erofs_is_fscache_mode(sb))
422 erofs_info(sb, "EXPERIMENTAL fscache-based on-demand read feature in use. Use at your own risk!");
423 if (erofs_sb_has_fragments(sbi))
424 erofs_info(sb, "EXPERIMENTAL compressed fragments feature in use. Use at your own risk!");
425 if (erofs_sb_has_dedupe(sbi))
426 erofs_info(sb, "EXPERIMENTAL global deduplication feature in use. Use at your own risk!");
428 erofs_put_metabuf(&buf);
432 /* set up default EROFS parameters */
433 static void erofs_default_options(struct erofs_fs_context *ctx)
435 #ifdef CONFIG_EROFS_FS_ZIP
436 ctx->opt.cache_strategy = EROFS_ZIP_CACHE_READAROUND;
437 ctx->opt.max_sync_decompress_pages = 3;
438 ctx->opt.sync_decompress = EROFS_SYNC_DECOMPRESS_AUTO;
440 #ifdef CONFIG_EROFS_FS_XATTR
441 set_opt(&ctx->opt, XATTR_USER);
443 #ifdef CONFIG_EROFS_FS_POSIX_ACL
444 set_opt(&ctx->opt, POSIX_ACL);
460 static const struct constant_table erofs_param_cache_strategy[] = {
461 {"disabled", EROFS_ZIP_CACHE_DISABLED},
462 {"readahead", EROFS_ZIP_CACHE_READAHEAD},
463 {"readaround", EROFS_ZIP_CACHE_READAROUND},
467 static const struct constant_table erofs_dax_param_enums[] = {
468 {"always", EROFS_MOUNT_DAX_ALWAYS},
469 {"never", EROFS_MOUNT_DAX_NEVER},
473 static const struct fs_parameter_spec erofs_fs_parameters[] = {
474 fsparam_flag_no("user_xattr", Opt_user_xattr),
475 fsparam_flag_no("acl", Opt_acl),
476 fsparam_enum("cache_strategy", Opt_cache_strategy,
477 erofs_param_cache_strategy),
478 fsparam_flag("dax", Opt_dax),
479 fsparam_enum("dax", Opt_dax_enum, erofs_dax_param_enums),
480 fsparam_string("device", Opt_device),
481 fsparam_string("fsid", Opt_fsid),
482 fsparam_string("domain_id", Opt_domain_id),
486 static bool erofs_fc_set_dax_mode(struct fs_context *fc, unsigned int mode)
489 struct erofs_fs_context *ctx = fc->fs_private;
492 case EROFS_MOUNT_DAX_ALWAYS:
493 warnfc(fc, "DAX enabled. Warning: EXPERIMENTAL, use at your own risk");
494 set_opt(&ctx->opt, DAX_ALWAYS);
495 clear_opt(&ctx->opt, DAX_NEVER);
497 case EROFS_MOUNT_DAX_NEVER:
498 set_opt(&ctx->opt, DAX_NEVER);
499 clear_opt(&ctx->opt, DAX_ALWAYS);
506 errorfc(fc, "dax options not supported");
511 static int erofs_fc_parse_param(struct fs_context *fc,
512 struct fs_parameter *param)
514 struct erofs_fs_context *ctx = fc->fs_private;
515 struct fs_parse_result result;
516 struct erofs_device_info *dif;
519 opt = fs_parse(fc, erofs_fs_parameters, param, &result);
525 #ifdef CONFIG_EROFS_FS_XATTR
527 set_opt(&ctx->opt, XATTR_USER);
529 clear_opt(&ctx->opt, XATTR_USER);
531 errorfc(fc, "{,no}user_xattr options not supported");
535 #ifdef CONFIG_EROFS_FS_POSIX_ACL
537 set_opt(&ctx->opt, POSIX_ACL);
539 clear_opt(&ctx->opt, POSIX_ACL);
541 errorfc(fc, "{,no}acl options not supported");
544 case Opt_cache_strategy:
545 #ifdef CONFIG_EROFS_FS_ZIP
546 ctx->opt.cache_strategy = result.uint_32;
548 errorfc(fc, "compression not supported, cache_strategy ignored");
552 if (!erofs_fc_set_dax_mode(fc, EROFS_MOUNT_DAX_ALWAYS))
556 if (!erofs_fc_set_dax_mode(fc, result.uint_32))
560 dif = kzalloc(sizeof(*dif), GFP_KERNEL);
563 dif->path = kstrdup(param->string, GFP_KERNEL);
568 down_write(&ctx->devs->rwsem);
569 ret = idr_alloc(&ctx->devs->tree, dif, 0, 0, GFP_KERNEL);
570 up_write(&ctx->devs->rwsem);
576 ++ctx->devs->extra_devices;
578 #ifdef CONFIG_EROFS_FS_ONDEMAND
581 ctx->fsid = kstrdup(param->string, GFP_KERNEL);
586 kfree(ctx->domain_id);
587 ctx->domain_id = kstrdup(param->string, GFP_KERNEL);
594 errorfc(fc, "%s option not supported", erofs_fs_parameters[opt].name);
603 static struct inode *erofs_nfs_get_inode(struct super_block *sb,
604 u64 ino, u32 generation)
606 return erofs_iget(sb, ino);
609 static struct dentry *erofs_fh_to_dentry(struct super_block *sb,
610 struct fid *fid, int fh_len, int fh_type)
612 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
613 erofs_nfs_get_inode);
616 static struct dentry *erofs_fh_to_parent(struct super_block *sb,
617 struct fid *fid, int fh_len, int fh_type)
619 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
620 erofs_nfs_get_inode);
623 static struct dentry *erofs_get_parent(struct dentry *child)
629 err = erofs_namei(d_inode(child), &dotdot_name, &nid, &d_type);
632 return d_obtain_alias(erofs_iget(child->d_sb, nid));
635 static const struct export_operations erofs_export_ops = {
636 .fh_to_dentry = erofs_fh_to_dentry,
637 .fh_to_parent = erofs_fh_to_parent,
638 .get_parent = erofs_get_parent,
641 static int erofs_fc_fill_pseudo_super(struct super_block *sb, struct fs_context *fc)
643 static const struct tree_descr empty_descr = {""};
645 return simple_fill_super(sb, EROFS_SUPER_MAGIC, &empty_descr);
648 static int erofs_fc_fill_super(struct super_block *sb, struct fs_context *fc)
651 struct erofs_sb_info *sbi;
652 struct erofs_fs_context *ctx = fc->fs_private;
655 sb->s_magic = EROFS_SUPER_MAGIC;
656 sb->s_flags |= SB_RDONLY | SB_NOATIME;
657 sb->s_maxbytes = MAX_LFS_FILESIZE;
658 sb->s_op = &erofs_sops;
660 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
666 sbi->devs = ctx->devs;
668 sbi->fsid = ctx->fsid;
670 sbi->domain_id = ctx->domain_id;
671 ctx->domain_id = NULL;
673 sbi->blkszbits = PAGE_SHIFT;
674 if (erofs_is_fscache_mode(sb)) {
675 sb->s_blocksize = PAGE_SIZE;
676 sb->s_blocksize_bits = PAGE_SHIFT;
678 err = erofs_fscache_register_fs(sb);
682 err = super_setup_bdi(sb);
686 if (!sb_set_blocksize(sb, PAGE_SIZE)) {
687 errorfc(fc, "failed to set initial blksize");
691 sbi->dax_dev = fs_dax_get_by_bdev(sb->s_bdev,
696 err = erofs_read_superblock(sb);
700 if (sb->s_blocksize_bits != sbi->blkszbits) {
701 if (erofs_is_fscache_mode(sb)) {
702 errorfc(fc, "unsupported blksize for fscache mode");
705 if (!sb_set_blocksize(sb, 1 << sbi->blkszbits)) {
706 errorfc(fc, "failed to set erofs blksize");
711 if (test_opt(&sbi->opt, DAX_ALWAYS)) {
713 errorfc(fc, "DAX unsupported by block device. Turning off DAX.");
714 clear_opt(&sbi->opt, DAX_ALWAYS);
715 } else if (sbi->blkszbits != PAGE_SHIFT) {
716 errorfc(fc, "unsupported blocksize for DAX");
717 clear_opt(&sbi->opt, DAX_ALWAYS);
722 sb->s_xattr = erofs_xattr_handlers;
723 sb->s_export_op = &erofs_export_ops;
725 if (test_opt(&sbi->opt, POSIX_ACL))
726 sb->s_flags |= SB_POSIXACL;
728 sb->s_flags &= ~SB_POSIXACL;
730 #ifdef CONFIG_EROFS_FS_ZIP
731 xa_init(&sbi->managed_pslots);
734 /* get the root inode */
735 inode = erofs_iget(sb, ROOT_NID(sbi));
737 return PTR_ERR(inode);
739 if (!S_ISDIR(inode->i_mode)) {
740 erofs_err(sb, "rootino(nid %llu) is not a directory(i_mode %o)",
741 ROOT_NID(sbi), inode->i_mode);
746 sb->s_root = d_make_root(inode);
750 erofs_shrinker_register(sb);
751 /* sb->s_umount is already locked, SB_ACTIVE and SB_BORN are not set */
752 if (erofs_sb_has_fragments(sbi) && sbi->packed_nid) {
753 sbi->packed_inode = erofs_iget(sb, sbi->packed_nid);
754 if (IS_ERR(sbi->packed_inode)) {
755 err = PTR_ERR(sbi->packed_inode);
756 sbi->packed_inode = NULL;
760 err = erofs_init_managed_cache(sb);
764 err = erofs_xattr_prefixes_init(sb);
768 err = erofs_register_sysfs(sb);
772 erofs_info(sb, "mounted with root inode @ nid %llu.", ROOT_NID(sbi));
776 static int erofs_fc_anon_get_tree(struct fs_context *fc)
778 return get_tree_nodev(fc, erofs_fc_fill_pseudo_super);
781 static int erofs_fc_get_tree(struct fs_context *fc)
783 struct erofs_fs_context *ctx = fc->fs_private;
785 if (IS_ENABLED(CONFIG_EROFS_FS_ONDEMAND) && ctx->fsid)
786 return get_tree_nodev(fc, erofs_fc_fill_super);
788 return get_tree_bdev(fc, erofs_fc_fill_super);
791 static int erofs_fc_reconfigure(struct fs_context *fc)
793 struct super_block *sb = fc->root->d_sb;
794 struct erofs_sb_info *sbi = EROFS_SB(sb);
795 struct erofs_fs_context *ctx = fc->fs_private;
797 DBG_BUGON(!sb_rdonly(sb));
799 if (ctx->fsid || ctx->domain_id)
800 erofs_info(sb, "ignoring reconfiguration for fsid|domain_id.");
802 if (test_opt(&ctx->opt, POSIX_ACL))
803 fc->sb_flags |= SB_POSIXACL;
805 fc->sb_flags &= ~SB_POSIXACL;
809 fc->sb_flags |= SB_RDONLY;
813 static int erofs_release_device_info(int id, void *ptr, void *data)
815 struct erofs_device_info *dif = ptr;
817 fs_put_dax(dif->dax_dev, NULL);
819 blkdev_put(dif->bdev, &erofs_fs_type);
820 erofs_fscache_unregister_cookie(dif->fscache);
827 static void erofs_free_dev_context(struct erofs_dev_context *devs)
831 idr_for_each(&devs->tree, &erofs_release_device_info, NULL);
832 idr_destroy(&devs->tree);
836 static void erofs_fc_free(struct fs_context *fc)
838 struct erofs_fs_context *ctx = fc->fs_private;
840 erofs_free_dev_context(ctx->devs);
842 kfree(ctx->domain_id);
846 static const struct fs_context_operations erofs_context_ops = {
847 .parse_param = erofs_fc_parse_param,
848 .get_tree = erofs_fc_get_tree,
849 .reconfigure = erofs_fc_reconfigure,
850 .free = erofs_fc_free,
853 static const struct fs_context_operations erofs_anon_context_ops = {
854 .get_tree = erofs_fc_anon_get_tree,
857 static int erofs_init_fs_context(struct fs_context *fc)
859 struct erofs_fs_context *ctx;
861 /* pseudo mount for anon inodes */
862 if (fc->sb_flags & SB_KERNMOUNT) {
863 fc->ops = &erofs_anon_context_ops;
867 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
870 ctx->devs = kzalloc(sizeof(struct erofs_dev_context), GFP_KERNEL);
875 fc->fs_private = ctx;
877 idr_init(&ctx->devs->tree);
878 init_rwsem(&ctx->devs->rwsem);
879 erofs_default_options(ctx);
880 fc->ops = &erofs_context_ops;
885 * could be triggered after deactivate_locked_super()
886 * is called, thus including umount and failed to initialize.
888 static void erofs_kill_sb(struct super_block *sb)
890 struct erofs_sb_info *sbi;
892 WARN_ON(sb->s_magic != EROFS_SUPER_MAGIC);
894 /* pseudo mount for anon inodes */
895 if (sb->s_flags & SB_KERNMOUNT) {
900 if (erofs_is_fscache_mode(sb))
903 kill_block_super(sb);
909 erofs_free_dev_context(sbi->devs);
910 fs_put_dax(sbi->dax_dev, NULL);
911 erofs_fscache_unregister_fs(sb);
913 kfree(sbi->domain_id);
915 sb->s_fs_info = NULL;
918 /* called when ->s_root is non-NULL */
919 static void erofs_put_super(struct super_block *sb)
921 struct erofs_sb_info *const sbi = EROFS_SB(sb);
925 erofs_unregister_sysfs(sb);
926 erofs_shrinker_unregister(sb);
927 erofs_xattr_prefixes_cleanup(sb);
928 #ifdef CONFIG_EROFS_FS_ZIP
929 iput(sbi->managed_cache);
930 sbi->managed_cache = NULL;
932 iput(sbi->packed_inode);
933 sbi->packed_inode = NULL;
934 erofs_free_dev_context(sbi->devs);
936 erofs_fscache_unregister_fs(sb);
939 struct file_system_type erofs_fs_type = {
940 .owner = THIS_MODULE,
942 .init_fs_context = erofs_init_fs_context,
943 .kill_sb = erofs_kill_sb,
944 .fs_flags = FS_REQUIRES_DEV | FS_ALLOW_IDMAP,
946 MODULE_ALIAS_FS("erofs");
948 static int __init erofs_module_init(void)
952 erofs_check_ondisk_layout_definitions();
954 erofs_inode_cachep = kmem_cache_create("erofs_inode",
955 sizeof(struct erofs_inode), 0,
956 SLAB_RECLAIM_ACCOUNT,
957 erofs_inode_init_once);
958 if (!erofs_inode_cachep)
961 err = erofs_init_shrinker();
965 err = z_erofs_lzma_init();
969 erofs_pcpubuf_init();
970 err = z_erofs_init_zip_subsystem();
974 err = erofs_init_sysfs();
978 err = register_filesystem(&erofs_fs_type);
987 z_erofs_exit_zip_subsystem();
991 erofs_exit_shrinker();
993 kmem_cache_destroy(erofs_inode_cachep);
997 static void __exit erofs_module_exit(void)
999 unregister_filesystem(&erofs_fs_type);
1001 /* Ensure all RCU free inodes / pclusters are safe to be destroyed. */
1005 z_erofs_exit_zip_subsystem();
1006 z_erofs_lzma_exit();
1007 erofs_exit_shrinker();
1008 kmem_cache_destroy(erofs_inode_cachep);
1009 erofs_pcpubuf_exit();
1012 /* get filesystem statistics */
1013 static int erofs_statfs(struct dentry *dentry, struct kstatfs *buf)
1015 struct super_block *sb = dentry->d_sb;
1016 struct erofs_sb_info *sbi = EROFS_SB(sb);
1019 if (!erofs_is_fscache_mode(sb))
1020 id = huge_encode_dev(sb->s_bdev->bd_dev);
1022 buf->f_type = sb->s_magic;
1023 buf->f_bsize = sb->s_blocksize;
1024 buf->f_blocks = sbi->total_blocks;
1025 buf->f_bfree = buf->f_bavail = 0;
1027 buf->f_files = ULLONG_MAX;
1028 buf->f_ffree = ULLONG_MAX - sbi->inos;
1030 buf->f_namelen = EROFS_NAME_LEN;
1032 buf->f_fsid = u64_to_fsid(id);
1036 static int erofs_show_options(struct seq_file *seq, struct dentry *root)
1038 struct erofs_sb_info *sbi = EROFS_SB(root->d_sb);
1039 struct erofs_mount_opts *opt = &sbi->opt;
1041 #ifdef CONFIG_EROFS_FS_XATTR
1042 if (test_opt(opt, XATTR_USER))
1043 seq_puts(seq, ",user_xattr");
1045 seq_puts(seq, ",nouser_xattr");
1047 #ifdef CONFIG_EROFS_FS_POSIX_ACL
1048 if (test_opt(opt, POSIX_ACL))
1049 seq_puts(seq, ",acl");
1051 seq_puts(seq, ",noacl");
1053 #ifdef CONFIG_EROFS_FS_ZIP
1054 if (opt->cache_strategy == EROFS_ZIP_CACHE_DISABLED)
1055 seq_puts(seq, ",cache_strategy=disabled");
1056 else if (opt->cache_strategy == EROFS_ZIP_CACHE_READAHEAD)
1057 seq_puts(seq, ",cache_strategy=readahead");
1058 else if (opt->cache_strategy == EROFS_ZIP_CACHE_READAROUND)
1059 seq_puts(seq, ",cache_strategy=readaround");
1061 if (test_opt(opt, DAX_ALWAYS))
1062 seq_puts(seq, ",dax=always");
1063 if (test_opt(opt, DAX_NEVER))
1064 seq_puts(seq, ",dax=never");
1065 #ifdef CONFIG_EROFS_FS_ONDEMAND
1067 seq_printf(seq, ",fsid=%s", sbi->fsid);
1069 seq_printf(seq, ",domain_id=%s", sbi->domain_id);
1074 const struct super_operations erofs_sops = {
1075 .put_super = erofs_put_super,
1076 .alloc_inode = erofs_alloc_inode,
1077 .free_inode = erofs_free_inode,
1078 .statfs = erofs_statfs,
1079 .show_options = erofs_show_options,
1082 module_init(erofs_module_init);
1083 module_exit(erofs_module_exit);
1085 MODULE_DESCRIPTION("Enhanced ROM File System");
1086 MODULE_AUTHOR("Gao Xiang, Chao Yu, Miao Xie, CONSUMER BG, HUAWEI Inc.");
1087 MODULE_LICENSE("GPL");