1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/drivers/staging/erofs/super.c
5 * Copyright (C) 2017-2018 HUAWEI, Inc.
6 * http://www.huawei.com/
7 * Created by Gao Xiang <gaoxiang25@huawei.com>
9 #include <linux/module.h>
10 #include <linux/buffer_head.h>
11 #include <linux/statfs.h>
12 #include <linux/parser.h>
13 #include <linux/seq_file.h>
16 #define CREATE_TRACE_POINTS
17 #include <trace/events/erofs.h>
19 static struct kmem_cache *erofs_inode_cachep __read_mostly;
21 static void init_once(void *ptr)
23 struct erofs_vnode *vi = ptr;
25 inode_init_once(&vi->vfs_inode);
28 static int __init erofs_init_inode_cache(void)
30 erofs_inode_cachep = kmem_cache_create("erofs_inode",
31 sizeof(struct erofs_vnode), 0,
35 return erofs_inode_cachep ? 0 : -ENOMEM;
38 static void erofs_exit_inode_cache(void)
40 kmem_cache_destroy(erofs_inode_cachep);
43 static struct inode *alloc_inode(struct super_block *sb)
45 struct erofs_vnode *vi =
46 kmem_cache_alloc(erofs_inode_cachep, GFP_KERNEL);
51 /* zero out everything except vfs_inode */
52 memset(vi, 0, offsetof(struct erofs_vnode, vfs_inode));
53 return &vi->vfs_inode;
56 static void free_inode(struct inode *inode)
58 struct erofs_vnode *vi = EROFS_V(inode);
60 /* be careful RCU symlink path (see ext4_inode_info->i_data)! */
61 if (is_inode_fast_symlink(inode))
64 kfree(vi->xattr_shared_xattrs);
66 kmem_cache_free(erofs_inode_cachep, vi);
69 static bool check_layout_compatibility(struct super_block *sb,
70 struct erofs_super_block *layout)
72 const unsigned int requirements = le32_to_cpu(layout->requirements);
74 EROFS_SB(sb)->requirements = requirements;
76 /* check if current kernel meets all mandatory requirements */
77 if (requirements & (~EROFS_ALL_REQUIREMENTS)) {
78 errln("unidentified requirements %x, please upgrade kernel version",
79 requirements & ~EROFS_ALL_REQUIREMENTS);
85 static int superblock_read(struct super_block *sb)
87 struct erofs_sb_info *sbi;
88 struct buffer_head *bh;
89 struct erofs_super_block *layout;
90 unsigned int blkszbits;
96 errln("cannot read erofs superblock");
101 layout = (struct erofs_super_block *)((u8 *)bh->b_data
102 + EROFS_SUPER_OFFSET);
105 if (le32_to_cpu(layout->magic) != EROFS_SUPER_MAGIC_V1) {
106 errln("cannot find valid erofs superblock");
110 blkszbits = layout->blkszbits;
111 /* 9(512 bytes) + LOG_SECTORS_PER_BLOCK == LOG_BLOCK_SIZE */
112 if (unlikely(blkszbits != LOG_BLOCK_SIZE)) {
113 errln("blksize %u isn't supported on this platform",
118 if (!check_layout_compatibility(sb, layout))
121 sbi->blocks = le32_to_cpu(layout->blocks);
122 sbi->meta_blkaddr = le32_to_cpu(layout->meta_blkaddr);
123 #ifdef CONFIG_EROFS_FS_XATTR
124 sbi->xattr_blkaddr = le32_to_cpu(layout->xattr_blkaddr);
126 sbi->islotbits = ffs(sizeof(struct erofs_inode_v1)) - 1;
127 sbi->root_nid = le16_to_cpu(layout->root_nid);
128 sbi->inos = le64_to_cpu(layout->inos);
130 sbi->build_time = le64_to_cpu(layout->build_time);
131 sbi->build_time_nsec = le32_to_cpu(layout->build_time_nsec);
133 memcpy(&sb->s_uuid, layout->uuid, sizeof(layout->uuid));
135 ret = strscpy(sbi->volume_name, layout->volume_name,
136 sizeof(layout->volume_name));
137 if (ret < 0) { /* -E2BIG */
138 errln("bad volume name without NIL terminator");
148 #ifdef CONFIG_EROFS_FAULT_INJECTION
149 const char *erofs_fault_name[FAULT_MAX] = {
150 [FAULT_KMALLOC] = "kmalloc",
151 [FAULT_READ_IO] = "read IO error",
154 static void __erofs_build_fault_attr(struct erofs_sb_info *sbi,
157 struct erofs_fault_info *ffi = &sbi->fault_info;
160 atomic_set(&ffi->inject_ops, 0);
161 ffi->inject_rate = rate;
162 ffi->inject_type = (1 << FAULT_MAX) - 1;
164 memset(ffi, 0, sizeof(struct erofs_fault_info));
167 set_opt(sbi, FAULT_INJECTION);
170 static int erofs_build_fault_attr(struct erofs_sb_info *sbi,
175 if (args->from && match_int(args, &rate))
178 __erofs_build_fault_attr(sbi, rate);
182 static unsigned int erofs_get_fault_rate(struct erofs_sb_info *sbi)
184 return sbi->fault_info.inject_rate;
187 static void __erofs_build_fault_attr(struct erofs_sb_info *sbi,
192 static int erofs_build_fault_attr(struct erofs_sb_info *sbi,
195 infoln("fault_injection options not supported");
199 static unsigned int erofs_get_fault_rate(struct erofs_sb_info *sbi)
205 #ifdef CONFIG_EROFS_FS_ZIP
206 static int erofs_build_cache_strategy(struct erofs_sb_info *sbi,
209 const char *cs = match_strdup(args);
213 errln("Not enough memory to store cache strategy");
217 if (!strcmp(cs, "disabled")) {
218 sbi->cache_strategy = EROFS_ZIP_CACHE_DISABLED;
219 } else if (!strcmp(cs, "readahead")) {
220 sbi->cache_strategy = EROFS_ZIP_CACHE_READAHEAD;
221 } else if (!strcmp(cs, "readaround")) {
222 sbi->cache_strategy = EROFS_ZIP_CACHE_READAROUND;
224 errln("Unrecognized cache strategy \"%s\"", cs);
231 static int erofs_build_cache_strategy(struct erofs_sb_info *sbi,
234 infoln("EROFS compression is disabled, so cache strategy is ignored");
239 /* set up default EROFS parameters */
240 static void default_options(struct erofs_sb_info *sbi)
242 #ifdef CONFIG_EROFS_FS_ZIP
243 sbi->cache_strategy = EROFS_ZIP_CACHE_READAROUND;
244 sbi->max_sync_decompress_pages = 3;
246 #ifdef CONFIG_EROFS_FS_XATTR
247 set_opt(sbi, XATTR_USER);
249 #ifdef CONFIG_EROFS_FS_POSIX_ACL
250 set_opt(sbi, POSIX_ACL);
264 static match_table_t erofs_tokens = {
265 {Opt_user_xattr, "user_xattr"},
266 {Opt_nouser_xattr, "nouser_xattr"},
268 {Opt_noacl, "noacl"},
269 {Opt_fault_injection, "fault_injection=%u"},
270 {Opt_cache_strategy, "cache_strategy=%s"},
274 static int parse_options(struct super_block *sb, char *options)
276 substring_t args[MAX_OPT_ARGS];
283 while ((p = strsep(&options, ","))) {
289 args[0].to = args[0].from = NULL;
290 token = match_token(p, erofs_tokens, args);
293 #ifdef CONFIG_EROFS_FS_XATTR
295 set_opt(EROFS_SB(sb), XATTR_USER);
297 case Opt_nouser_xattr:
298 clear_opt(EROFS_SB(sb), XATTR_USER);
302 infoln("user_xattr options not supported");
304 case Opt_nouser_xattr:
305 infoln("nouser_xattr options not supported");
308 #ifdef CONFIG_EROFS_FS_POSIX_ACL
310 set_opt(EROFS_SB(sb), POSIX_ACL);
313 clear_opt(EROFS_SB(sb), POSIX_ACL);
317 infoln("acl options not supported");
320 infoln("noacl options not supported");
323 case Opt_fault_injection:
324 err = erofs_build_fault_attr(EROFS_SB(sb), args);
328 case Opt_cache_strategy:
329 err = erofs_build_cache_strategy(EROFS_SB(sb), args);
334 errln("Unrecognized mount option \"%s\" or missing value", p);
341 #ifdef CONFIG_EROFS_FS_ZIP
342 static const struct address_space_operations managed_cache_aops;
344 static int managed_cache_releasepage(struct page *page, gfp_t gfp_mask)
346 int ret = 1; /* 0 - busy */
347 struct address_space *const mapping = page->mapping;
349 DBG_BUGON(!PageLocked(page));
350 DBG_BUGON(mapping->a_ops != &managed_cache_aops);
352 if (PagePrivate(page))
353 ret = erofs_try_to_free_cached_page(mapping, page);
358 static void managed_cache_invalidatepage(struct page *page,
362 const unsigned int stop = length + offset;
364 DBG_BUGON(!PageLocked(page));
366 /* Check for potential overflow in debug mode */
367 DBG_BUGON(stop > PAGE_SIZE || stop < length);
369 if (offset == 0 && stop == PAGE_SIZE)
370 while (!managed_cache_releasepage(page, GFP_NOFS))
374 static const struct address_space_operations managed_cache_aops = {
375 .releasepage = managed_cache_releasepage,
376 .invalidatepage = managed_cache_invalidatepage,
379 static int erofs_init_managed_cache(struct super_block *sb)
381 struct erofs_sb_info *const sbi = EROFS_SB(sb);
382 struct inode *const inode = new_inode(sb);
384 if (unlikely(!inode))
388 inode->i_size = OFFSET_MAX;
390 inode->i_mapping->a_ops = &managed_cache_aops;
391 mapping_set_gfp_mask(inode->i_mapping,
392 GFP_NOFS | __GFP_HIGHMEM | __GFP_MOVABLE);
393 sbi->managed_cache = inode;
397 static int erofs_init_managed_cache(struct super_block *sb) { return 0; }
400 static int erofs_fill_super(struct super_block *sb, void *data, int silent)
403 struct erofs_sb_info *sbi;
406 infoln("fill_super, device -> %s", sb->s_id);
407 infoln("options -> %s", (char *)data);
409 sb->s_magic = EROFS_SUPER_MAGIC;
411 if (unlikely(!sb_set_blocksize(sb, EROFS_BLKSIZ))) {
412 errln("failed to set erofs blksize");
416 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
421 err = superblock_read(sb);
425 sb->s_flags |= SB_RDONLY | SB_NOATIME;
426 sb->s_maxbytes = MAX_LFS_FILESIZE;
429 sb->s_op = &erofs_sops;
431 #ifdef CONFIG_EROFS_FS_XATTR
432 sb->s_xattr = erofs_xattr_handlers;
434 /* set erofs default mount options */
435 default_options(sbi);
437 err = parse_options(sb, data);
442 infoln("root inode @ nid %llu", ROOT_NID(sbi));
444 if (test_opt(sbi, POSIX_ACL))
445 sb->s_flags |= SB_POSIXACL;
447 sb->s_flags &= ~SB_POSIXACL;
449 #ifdef CONFIG_EROFS_FS_ZIP
450 INIT_RADIX_TREE(&sbi->workstn_tree, GFP_ATOMIC);
453 /* get the root inode */
454 inode = erofs_iget(sb, ROOT_NID(sbi), true);
456 return PTR_ERR(inode);
458 if (unlikely(!S_ISDIR(inode->i_mode))) {
459 errln("rootino(nid %llu) is not a directory(i_mode %o)",
460 ROOT_NID(sbi), inode->i_mode);
465 sb->s_root = d_make_root(inode);
466 if (unlikely(!sb->s_root))
469 erofs_shrinker_register(sb);
470 /* sb->s_umount is already locked, SB_ACTIVE and SB_BORN are not set */
471 err = erofs_init_managed_cache(sb);
476 infoln("mounted on %s with opts: %s.", sb->s_id, (char *)data);
480 static struct dentry *erofs_mount(struct file_system_type *fs_type, int flags,
481 const char *dev_name, void *data)
483 return mount_bdev(fs_type, flags, dev_name, data, erofs_fill_super);
487 * could be triggered after deactivate_locked_super()
488 * is called, thus including umount and failed to initialize.
490 static void erofs_kill_sb(struct super_block *sb)
492 struct erofs_sb_info *sbi;
494 WARN_ON(sb->s_magic != EROFS_SUPER_MAGIC);
495 infoln("unmounting for %s", sb->s_id);
497 kill_block_super(sb);
503 sb->s_fs_info = NULL;
506 /* called when ->s_root is non-NULL */
507 static void erofs_put_super(struct super_block *sb)
509 struct erofs_sb_info *const sbi = EROFS_SB(sb);
513 erofs_shrinker_unregister(sb);
514 #ifdef CONFIG_EROFS_FS_ZIP
515 iput(sbi->managed_cache);
516 sbi->managed_cache = NULL;
520 static struct file_system_type erofs_fs_type = {
521 .owner = THIS_MODULE,
523 .mount = erofs_mount,
524 .kill_sb = erofs_kill_sb,
525 .fs_flags = FS_REQUIRES_DEV,
527 MODULE_ALIAS_FS("erofs");
529 static int __init erofs_module_init(void)
533 erofs_check_ondisk_layout_definitions();
534 infoln("initializing erofs " EROFS_VERSION);
536 err = erofs_init_inode_cache();
540 err = erofs_init_shrinker();
544 err = z_erofs_init_zip_subsystem();
548 err = register_filesystem(&erofs_fs_type);
552 infoln("successfully to initialize erofs");
556 z_erofs_exit_zip_subsystem();
558 erofs_exit_shrinker();
560 erofs_exit_inode_cache();
565 static void __exit erofs_module_exit(void)
567 unregister_filesystem(&erofs_fs_type);
568 z_erofs_exit_zip_subsystem();
569 erofs_exit_shrinker();
570 erofs_exit_inode_cache();
571 infoln("successfully finalize erofs");
574 /* get filesystem statistics */
575 static int erofs_statfs(struct dentry *dentry, struct kstatfs *buf)
577 struct super_block *sb = dentry->d_sb;
578 struct erofs_sb_info *sbi = EROFS_SB(sb);
579 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
581 buf->f_type = sb->s_magic;
582 buf->f_bsize = EROFS_BLKSIZ;
583 buf->f_blocks = sbi->blocks;
584 buf->f_bfree = buf->f_bavail = 0;
586 buf->f_files = ULLONG_MAX;
587 buf->f_ffree = ULLONG_MAX - sbi->inos;
589 buf->f_namelen = EROFS_NAME_LEN;
591 buf->f_fsid.val[0] = (u32)id;
592 buf->f_fsid.val[1] = (u32)(id >> 32);
596 static int erofs_show_options(struct seq_file *seq, struct dentry *root)
598 struct erofs_sb_info *sbi __maybe_unused = EROFS_SB(root->d_sb);
600 #ifdef CONFIG_EROFS_FS_XATTR
601 if (test_opt(sbi, XATTR_USER))
602 seq_puts(seq, ",user_xattr");
604 seq_puts(seq, ",nouser_xattr");
606 #ifdef CONFIG_EROFS_FS_POSIX_ACL
607 if (test_opt(sbi, POSIX_ACL))
608 seq_puts(seq, ",acl");
610 seq_puts(seq, ",noacl");
612 if (test_opt(sbi, FAULT_INJECTION))
613 seq_printf(seq, ",fault_injection=%u",
614 erofs_get_fault_rate(sbi));
615 #ifdef CONFIG_EROFS_FS_ZIP
616 if (sbi->cache_strategy == EROFS_ZIP_CACHE_DISABLED) {
617 seq_puts(seq, ",cache_strategy=disabled");
618 } else if (sbi->cache_strategy == EROFS_ZIP_CACHE_READAHEAD) {
619 seq_puts(seq, ",cache_strategy=readahead");
620 } else if (sbi->cache_strategy == EROFS_ZIP_CACHE_READAROUND) {
621 seq_puts(seq, ",cache_strategy=readaround");
623 seq_puts(seq, ",cache_strategy=(unknown)");
630 static int erofs_remount(struct super_block *sb, int *flags, char *data)
632 struct erofs_sb_info *sbi = EROFS_SB(sb);
633 unsigned int org_mnt_opt = sbi->mount_opt;
634 unsigned int org_inject_rate = erofs_get_fault_rate(sbi);
637 DBG_BUGON(!sb_rdonly(sb));
638 err = parse_options(sb, data);
642 if (test_opt(sbi, POSIX_ACL))
643 sb->s_flags |= SB_POSIXACL;
645 sb->s_flags &= ~SB_POSIXACL;
650 __erofs_build_fault_attr(sbi, org_inject_rate);
651 sbi->mount_opt = org_mnt_opt;
656 const struct super_operations erofs_sops = {
657 .put_super = erofs_put_super,
658 .alloc_inode = alloc_inode,
659 .free_inode = free_inode,
660 .statfs = erofs_statfs,
661 .show_options = erofs_show_options,
662 .remount_fs = erofs_remount,
665 module_init(erofs_module_init);
666 module_exit(erofs_module_exit);
668 MODULE_DESCRIPTION("Enhanced ROM File System");
669 MODULE_AUTHOR("Gao Xiang, Chao Yu, Miao Xie, CONSUMER BG, HUAWEI Inc.");
670 MODULE_LICENSE("GPL");