1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2017-2018 HUAWEI, Inc.
4 * http://www.huawei.com/
5 * Created by Gao Xiang <gaoxiang25@huawei.com>
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>
14 #define CREATE_TRACE_POINTS
15 #include <trace/events/erofs.h>
17 static struct kmem_cache *erofs_inode_cachep __read_mostly;
19 static void init_once(void *ptr)
21 struct erofs_vnode *vi = ptr;
23 inode_init_once(&vi->vfs_inode);
26 static int __init erofs_init_inode_cache(void)
28 erofs_inode_cachep = kmem_cache_create("erofs_inode",
29 sizeof(struct erofs_vnode), 0,
33 return erofs_inode_cachep ? 0 : -ENOMEM;
36 static void erofs_exit_inode_cache(void)
38 kmem_cache_destroy(erofs_inode_cachep);
41 static struct inode *alloc_inode(struct super_block *sb)
43 struct erofs_vnode *vi =
44 kmem_cache_alloc(erofs_inode_cachep, GFP_KERNEL);
49 /* zero out everything except vfs_inode */
50 memset(vi, 0, offsetof(struct erofs_vnode, vfs_inode));
51 return &vi->vfs_inode;
54 static void free_inode(struct inode *inode)
56 struct erofs_vnode *vi = EROFS_V(inode);
58 /* be careful RCU symlink path (see ext4_inode_info->i_data)! */
59 if (is_inode_fast_symlink(inode))
62 kfree(vi->xattr_shared_xattrs);
64 kmem_cache_free(erofs_inode_cachep, vi);
67 static bool check_layout_compatibility(struct super_block *sb,
68 struct erofs_super_block *layout)
70 const unsigned int feature = le32_to_cpu(layout->feature_incompat);
72 EROFS_SB(sb)->feature_incompat = feature;
74 /* check if current kernel meets all mandatory requirements */
75 if (feature & (~EROFS_ALL_FEATURE_INCOMPAT)) {
76 errln("unidentified incompatible feature %x, please upgrade kernel version",
77 feature & ~EROFS_ALL_FEATURE_INCOMPAT);
83 static int superblock_read(struct super_block *sb)
85 struct erofs_sb_info *sbi;
86 struct buffer_head *bh;
87 struct erofs_super_block *layout;
88 unsigned int blkszbits;
94 errln("cannot read erofs superblock");
99 layout = (struct erofs_super_block *)((u8 *)bh->b_data
100 + EROFS_SUPER_OFFSET);
103 if (le32_to_cpu(layout->magic) != EROFS_SUPER_MAGIC_V1) {
104 errln("cannot find valid erofs superblock");
108 blkszbits = layout->blkszbits;
109 /* 9(512 bytes) + LOG_SECTORS_PER_BLOCK == LOG_BLOCK_SIZE */
110 if (blkszbits != LOG_BLOCK_SIZE) {
111 errln("blksize %u isn't supported on this platform",
116 if (!check_layout_compatibility(sb, layout))
119 sbi->blocks = le32_to_cpu(layout->blocks);
120 sbi->meta_blkaddr = le32_to_cpu(layout->meta_blkaddr);
121 #ifdef CONFIG_EROFS_FS_XATTR
122 sbi->xattr_blkaddr = le32_to_cpu(layout->xattr_blkaddr);
124 sbi->islotbits = ffs(sizeof(struct erofs_inode_v1)) - 1;
125 sbi->root_nid = le16_to_cpu(layout->root_nid);
126 sbi->inos = le64_to_cpu(layout->inos);
128 sbi->build_time = le64_to_cpu(layout->build_time);
129 sbi->build_time_nsec = le32_to_cpu(layout->build_time_nsec);
131 memcpy(&sb->s_uuid, layout->uuid, sizeof(layout->uuid));
133 ret = strscpy(sbi->volume_name, layout->volume_name,
134 sizeof(layout->volume_name));
135 if (ret < 0) { /* -E2BIG */
136 errln("bad volume name without NIL terminator");
146 #ifdef CONFIG_EROFS_FAULT_INJECTION
147 const char *erofs_fault_name[FAULT_MAX] = {
148 [FAULT_KMALLOC] = "kmalloc",
149 [FAULT_READ_IO] = "read IO error",
152 static void __erofs_build_fault_attr(struct erofs_sb_info *sbi,
155 struct erofs_fault_info *ffi = &sbi->fault_info;
158 atomic_set(&ffi->inject_ops, 0);
159 ffi->inject_rate = rate;
160 ffi->inject_type = (1 << FAULT_MAX) - 1;
162 memset(ffi, 0, sizeof(struct erofs_fault_info));
165 set_opt(sbi, FAULT_INJECTION);
168 static int erofs_build_fault_attr(struct erofs_sb_info *sbi,
173 if (args->from && match_int(args, &rate))
176 __erofs_build_fault_attr(sbi, rate);
180 static unsigned int erofs_get_fault_rate(struct erofs_sb_info *sbi)
182 return sbi->fault_info.inject_rate;
185 static void __erofs_build_fault_attr(struct erofs_sb_info *sbi,
190 static int erofs_build_fault_attr(struct erofs_sb_info *sbi,
193 infoln("fault_injection options not supported");
197 static unsigned int erofs_get_fault_rate(struct erofs_sb_info *sbi)
203 #ifdef CONFIG_EROFS_FS_ZIP
204 static int erofs_build_cache_strategy(struct erofs_sb_info *sbi,
207 const char *cs = match_strdup(args);
211 errln("Not enough memory to store cache strategy");
215 if (!strcmp(cs, "disabled")) {
216 sbi->cache_strategy = EROFS_ZIP_CACHE_DISABLED;
217 } else if (!strcmp(cs, "readahead")) {
218 sbi->cache_strategy = EROFS_ZIP_CACHE_READAHEAD;
219 } else if (!strcmp(cs, "readaround")) {
220 sbi->cache_strategy = EROFS_ZIP_CACHE_READAROUND;
222 errln("Unrecognized cache strategy \"%s\"", cs);
229 static int erofs_build_cache_strategy(struct erofs_sb_info *sbi,
232 infoln("EROFS compression is disabled, so cache strategy is ignored");
237 /* set up default EROFS parameters */
238 static void default_options(struct erofs_sb_info *sbi)
240 #ifdef CONFIG_EROFS_FS_ZIP
241 sbi->cache_strategy = EROFS_ZIP_CACHE_READAROUND;
242 sbi->max_sync_decompress_pages = 3;
244 #ifdef CONFIG_EROFS_FS_XATTR
245 set_opt(sbi, XATTR_USER);
247 #ifdef CONFIG_EROFS_FS_POSIX_ACL
248 set_opt(sbi, POSIX_ACL);
262 static match_table_t erofs_tokens = {
263 {Opt_user_xattr, "user_xattr"},
264 {Opt_nouser_xattr, "nouser_xattr"},
266 {Opt_noacl, "noacl"},
267 {Opt_fault_injection, "fault_injection=%u"},
268 {Opt_cache_strategy, "cache_strategy=%s"},
272 static int parse_options(struct super_block *sb, char *options)
274 substring_t args[MAX_OPT_ARGS];
281 while ((p = strsep(&options, ","))) {
287 args[0].to = args[0].from = NULL;
288 token = match_token(p, erofs_tokens, args);
291 #ifdef CONFIG_EROFS_FS_XATTR
293 set_opt(EROFS_SB(sb), XATTR_USER);
295 case Opt_nouser_xattr:
296 clear_opt(EROFS_SB(sb), XATTR_USER);
300 infoln("user_xattr options not supported");
302 case Opt_nouser_xattr:
303 infoln("nouser_xattr options not supported");
306 #ifdef CONFIG_EROFS_FS_POSIX_ACL
308 set_opt(EROFS_SB(sb), POSIX_ACL);
311 clear_opt(EROFS_SB(sb), POSIX_ACL);
315 infoln("acl options not supported");
318 infoln("noacl options not supported");
321 case Opt_fault_injection:
322 err = erofs_build_fault_attr(EROFS_SB(sb), args);
326 case Opt_cache_strategy:
327 err = erofs_build_cache_strategy(EROFS_SB(sb), args);
332 errln("Unrecognized mount option \"%s\" or missing value", p);
339 #ifdef CONFIG_EROFS_FS_ZIP
340 static const struct address_space_operations managed_cache_aops;
342 static int managed_cache_releasepage(struct page *page, gfp_t gfp_mask)
344 int ret = 1; /* 0 - busy */
345 struct address_space *const mapping = page->mapping;
347 DBG_BUGON(!PageLocked(page));
348 DBG_BUGON(mapping->a_ops != &managed_cache_aops);
350 if (PagePrivate(page))
351 ret = erofs_try_to_free_cached_page(mapping, page);
356 static void managed_cache_invalidatepage(struct page *page,
360 const unsigned int stop = length + offset;
362 DBG_BUGON(!PageLocked(page));
364 /* Check for potential overflow in debug mode */
365 DBG_BUGON(stop > PAGE_SIZE || stop < length);
367 if (offset == 0 && stop == PAGE_SIZE)
368 while (!managed_cache_releasepage(page, GFP_NOFS))
372 static const struct address_space_operations managed_cache_aops = {
373 .releasepage = managed_cache_releasepage,
374 .invalidatepage = managed_cache_invalidatepage,
377 static int erofs_init_managed_cache(struct super_block *sb)
379 struct erofs_sb_info *const sbi = EROFS_SB(sb);
380 struct inode *const inode = new_inode(sb);
386 inode->i_size = OFFSET_MAX;
388 inode->i_mapping->a_ops = &managed_cache_aops;
389 mapping_set_gfp_mask(inode->i_mapping,
390 GFP_NOFS | __GFP_HIGHMEM | __GFP_MOVABLE);
391 sbi->managed_cache = inode;
395 static int erofs_init_managed_cache(struct super_block *sb) { return 0; }
398 static int erofs_fill_super(struct super_block *sb, void *data, int silent)
401 struct erofs_sb_info *sbi;
404 infoln("fill_super, device -> %s", sb->s_id);
405 infoln("options -> %s", (char *)data);
407 sb->s_magic = EROFS_SUPER_MAGIC;
409 if (!sb_set_blocksize(sb, EROFS_BLKSIZ)) {
410 errln("failed to set erofs blksize");
414 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
419 err = superblock_read(sb);
423 sb->s_flags |= SB_RDONLY | SB_NOATIME;
424 sb->s_maxbytes = MAX_LFS_FILESIZE;
427 sb->s_op = &erofs_sops;
429 #ifdef CONFIG_EROFS_FS_XATTR
430 sb->s_xattr = erofs_xattr_handlers;
432 /* set erofs default mount options */
433 default_options(sbi);
435 err = parse_options(sb, data);
440 infoln("root inode @ nid %llu", ROOT_NID(sbi));
442 if (test_opt(sbi, POSIX_ACL))
443 sb->s_flags |= SB_POSIXACL;
445 sb->s_flags &= ~SB_POSIXACL;
447 #ifdef CONFIG_EROFS_FS_ZIP
448 INIT_RADIX_TREE(&sbi->workstn_tree, GFP_ATOMIC);
451 /* get the root inode */
452 inode = erofs_iget(sb, ROOT_NID(sbi), true);
454 return PTR_ERR(inode);
456 if (!S_ISDIR(inode->i_mode)) {
457 errln("rootino(nid %llu) is not a directory(i_mode %o)",
458 ROOT_NID(sbi), inode->i_mode);
463 sb->s_root = d_make_root(inode);
467 erofs_shrinker_register(sb);
468 /* sb->s_umount is already locked, SB_ACTIVE and SB_BORN are not set */
469 err = erofs_init_managed_cache(sb);
474 infoln("mounted on %s with opts: %s.", sb->s_id, (char *)data);
478 static struct dentry *erofs_mount(struct file_system_type *fs_type, int flags,
479 const char *dev_name, void *data)
481 return mount_bdev(fs_type, flags, dev_name, data, erofs_fill_super);
485 * could be triggered after deactivate_locked_super()
486 * is called, thus including umount and failed to initialize.
488 static void erofs_kill_sb(struct super_block *sb)
490 struct erofs_sb_info *sbi;
492 WARN_ON(sb->s_magic != EROFS_SUPER_MAGIC);
493 infoln("unmounting for %s", sb->s_id);
495 kill_block_super(sb);
501 sb->s_fs_info = NULL;
504 /* called when ->s_root is non-NULL */
505 static void erofs_put_super(struct super_block *sb)
507 struct erofs_sb_info *const sbi = EROFS_SB(sb);
511 erofs_shrinker_unregister(sb);
512 #ifdef CONFIG_EROFS_FS_ZIP
513 iput(sbi->managed_cache);
514 sbi->managed_cache = NULL;
518 static struct file_system_type erofs_fs_type = {
519 .owner = THIS_MODULE,
521 .mount = erofs_mount,
522 .kill_sb = erofs_kill_sb,
523 .fs_flags = FS_REQUIRES_DEV,
525 MODULE_ALIAS_FS("erofs");
527 static int __init erofs_module_init(void)
531 erofs_check_ondisk_layout_definitions();
532 infoln("initializing erofs " EROFS_VERSION);
534 err = erofs_init_inode_cache();
538 err = erofs_init_shrinker();
542 err = z_erofs_init_zip_subsystem();
546 err = register_filesystem(&erofs_fs_type);
550 infoln("successfully to initialize erofs");
554 z_erofs_exit_zip_subsystem();
556 erofs_exit_shrinker();
558 erofs_exit_inode_cache();
563 static void __exit erofs_module_exit(void)
565 unregister_filesystem(&erofs_fs_type);
566 z_erofs_exit_zip_subsystem();
567 erofs_exit_shrinker();
568 erofs_exit_inode_cache();
569 infoln("successfully finalize erofs");
572 /* get filesystem statistics */
573 static int erofs_statfs(struct dentry *dentry, struct kstatfs *buf)
575 struct super_block *sb = dentry->d_sb;
576 struct erofs_sb_info *sbi = EROFS_SB(sb);
577 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
579 buf->f_type = sb->s_magic;
580 buf->f_bsize = EROFS_BLKSIZ;
581 buf->f_blocks = sbi->blocks;
582 buf->f_bfree = buf->f_bavail = 0;
584 buf->f_files = ULLONG_MAX;
585 buf->f_ffree = ULLONG_MAX - sbi->inos;
587 buf->f_namelen = EROFS_NAME_LEN;
589 buf->f_fsid.val[0] = (u32)id;
590 buf->f_fsid.val[1] = (u32)(id >> 32);
594 static int erofs_show_options(struct seq_file *seq, struct dentry *root)
596 struct erofs_sb_info *sbi __maybe_unused = EROFS_SB(root->d_sb);
598 #ifdef CONFIG_EROFS_FS_XATTR
599 if (test_opt(sbi, XATTR_USER))
600 seq_puts(seq, ",user_xattr");
602 seq_puts(seq, ",nouser_xattr");
604 #ifdef CONFIG_EROFS_FS_POSIX_ACL
605 if (test_opt(sbi, POSIX_ACL))
606 seq_puts(seq, ",acl");
608 seq_puts(seq, ",noacl");
610 if (test_opt(sbi, FAULT_INJECTION))
611 seq_printf(seq, ",fault_injection=%u",
612 erofs_get_fault_rate(sbi));
613 #ifdef CONFIG_EROFS_FS_ZIP
614 if (sbi->cache_strategy == EROFS_ZIP_CACHE_DISABLED) {
615 seq_puts(seq, ",cache_strategy=disabled");
616 } else if (sbi->cache_strategy == EROFS_ZIP_CACHE_READAHEAD) {
617 seq_puts(seq, ",cache_strategy=readahead");
618 } else if (sbi->cache_strategy == EROFS_ZIP_CACHE_READAROUND) {
619 seq_puts(seq, ",cache_strategy=readaround");
621 seq_puts(seq, ",cache_strategy=(unknown)");
628 static int erofs_remount(struct super_block *sb, int *flags, char *data)
630 struct erofs_sb_info *sbi = EROFS_SB(sb);
631 unsigned int org_mnt_opt = sbi->mount_opt;
632 unsigned int org_inject_rate = erofs_get_fault_rate(sbi);
635 DBG_BUGON(!sb_rdonly(sb));
636 err = parse_options(sb, data);
640 if (test_opt(sbi, POSIX_ACL))
641 sb->s_flags |= SB_POSIXACL;
643 sb->s_flags &= ~SB_POSIXACL;
648 __erofs_build_fault_attr(sbi, org_inject_rate);
649 sbi->mount_opt = org_mnt_opt;
654 const struct super_operations erofs_sops = {
655 .put_super = erofs_put_super,
656 .alloc_inode = alloc_inode,
657 .free_inode = free_inode,
658 .statfs = erofs_statfs,
659 .show_options = erofs_show_options,
660 .remount_fs = erofs_remount,
663 module_init(erofs_module_init);
664 module_exit(erofs_module_exit);
666 MODULE_DESCRIPTION("Enhanced ROM File System");
667 MODULE_AUTHOR("Gao Xiang, Chao Yu, Miao Xie, CONSUMER BG, HUAWEI Inc.");
668 MODULE_LICENSE("GPL");