1 // SPDX-License-Identifier: GPL-2.0
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 * This file is subject to the terms and conditions of the GNU General Public
10 * License. See the file COPYING in the main directory of the Linux
11 * distribution for more details.
13 #include <linux/module.h>
14 #include <linux/buffer_head.h>
15 #include <linux/statfs.h>
16 #include <linux/parser.h>
17 #include <linux/seq_file.h>
21 #define CREATE_TRACE_POINTS
22 #include <trace/events/erofs.h>
24 static struct kmem_cache *erofs_inode_cachep __read_mostly;
26 static void init_once(void *ptr)
28 struct erofs_vnode *vi = ptr;
30 inode_init_once(&vi->vfs_inode);
33 static int __init erofs_init_inode_cache(void)
35 erofs_inode_cachep = kmem_cache_create("erofs_inode",
36 sizeof(struct erofs_vnode), 0,
40 return erofs_inode_cachep ? 0 : -ENOMEM;
43 static void erofs_exit_inode_cache(void)
45 kmem_cache_destroy(erofs_inode_cachep);
48 static struct inode *alloc_inode(struct super_block *sb)
50 struct erofs_vnode *vi =
51 kmem_cache_alloc(erofs_inode_cachep, GFP_KERNEL);
56 /* zero out everything except vfs_inode */
57 memset(vi, 0, offsetof(struct erofs_vnode, vfs_inode));
58 return &vi->vfs_inode;
61 static void free_inode(struct inode *inode)
63 struct erofs_vnode *vi = EROFS_V(inode);
65 /* be careful RCU symlink path (see ext4_inode_info->i_data)! */
66 if (is_inode_fast_symlink(inode))
69 kfree(vi->xattr_shared_xattrs);
71 kmem_cache_free(erofs_inode_cachep, vi);
74 static bool check_layout_compatibility(struct super_block *sb,
75 struct erofs_super_block *layout)
77 const unsigned int requirements = le32_to_cpu(layout->requirements);
79 EROFS_SB(sb)->requirements = requirements;
81 /* check if current kernel meets all mandatory requirements */
82 if (requirements & (~EROFS_ALL_REQUIREMENTS)) {
83 errln("unidentified requirements %x, please upgrade kernel version",
84 requirements & ~EROFS_ALL_REQUIREMENTS);
90 static int superblock_read(struct super_block *sb)
92 struct erofs_sb_info *sbi;
93 struct buffer_head *bh;
94 struct erofs_super_block *layout;
95 unsigned int blkszbits;
101 errln("cannot read erofs superblock");
106 layout = (struct erofs_super_block *)((u8 *)bh->b_data
107 + EROFS_SUPER_OFFSET);
110 if (le32_to_cpu(layout->magic) != EROFS_SUPER_MAGIC_V1) {
111 errln("cannot find valid erofs superblock");
115 blkszbits = layout->blkszbits;
116 /* 9(512 bytes) + LOG_SECTORS_PER_BLOCK == LOG_BLOCK_SIZE */
117 if (unlikely(blkszbits != LOG_BLOCK_SIZE)) {
118 errln("blksize %u isn't supported on this platform",
123 if (!check_layout_compatibility(sb, layout))
126 sbi->blocks = le32_to_cpu(layout->blocks);
127 sbi->meta_blkaddr = le32_to_cpu(layout->meta_blkaddr);
128 #ifdef CONFIG_EROFS_FS_XATTR
129 sbi->xattr_blkaddr = le32_to_cpu(layout->xattr_blkaddr);
131 sbi->islotbits = ffs(sizeof(struct erofs_inode_v1)) - 1;
132 #ifdef CONFIG_EROFS_FS_ZIP
133 /* TODO: clusterbits should be related to inode */
134 sbi->clusterbits = blkszbits;
136 if (1 << (sbi->clusterbits - PAGE_SHIFT) > Z_EROFS_CLUSTER_MAX_PAGES)
137 errln("clusterbits %u is not supported on this kernel",
141 sbi->root_nid = le16_to_cpu(layout->root_nid);
142 sbi->inos = le64_to_cpu(layout->inos);
144 sbi->build_time = le64_to_cpu(layout->build_time);
145 sbi->build_time_nsec = le32_to_cpu(layout->build_time_nsec);
147 memcpy(&sb->s_uuid, layout->uuid, sizeof(layout->uuid));
148 memcpy(sbi->volume_name, layout->volume_name,
149 sizeof(layout->volume_name));
157 #ifdef CONFIG_EROFS_FAULT_INJECTION
158 const char *erofs_fault_name[FAULT_MAX] = {
159 [FAULT_KMALLOC] = "kmalloc",
160 [FAULT_READ_IO] = "read IO error",
163 static void __erofs_build_fault_attr(struct erofs_sb_info *sbi,
166 struct erofs_fault_info *ffi = &sbi->fault_info;
169 atomic_set(&ffi->inject_ops, 0);
170 ffi->inject_rate = rate;
171 ffi->inject_type = (1 << FAULT_MAX) - 1;
173 memset(ffi, 0, sizeof(struct erofs_fault_info));
176 set_opt(sbi, FAULT_INJECTION);
179 static int erofs_build_fault_attr(struct erofs_sb_info *sbi,
184 if (args->from && match_int(args, &rate))
187 __erofs_build_fault_attr(sbi, rate);
191 static unsigned int erofs_get_fault_rate(struct erofs_sb_info *sbi)
193 return sbi->fault_info.inject_rate;
196 static void __erofs_build_fault_attr(struct erofs_sb_info *sbi,
201 static int erofs_build_fault_attr(struct erofs_sb_info *sbi,
204 infoln("fault_injection options not supported");
208 static unsigned int erofs_get_fault_rate(struct erofs_sb_info *sbi)
214 static void default_options(struct erofs_sb_info *sbi)
216 /* set up some FS parameters */
217 #ifdef CONFIG_EROFS_FS_ZIP
218 sbi->max_sync_decompress_pages = DEFAULT_MAX_SYNC_DECOMPRESS_PAGES;
221 #ifdef CONFIG_EROFS_FS_XATTR
222 set_opt(sbi, XATTR_USER);
225 #ifdef CONFIG_EROFS_FS_POSIX_ACL
226 set_opt(sbi, POSIX_ACL);
239 static match_table_t erofs_tokens = {
240 {Opt_user_xattr, "user_xattr"},
241 {Opt_nouser_xattr, "nouser_xattr"},
243 {Opt_noacl, "noacl"},
244 {Opt_fault_injection, "fault_injection=%u"},
248 static int parse_options(struct super_block *sb, char *options)
250 substring_t args[MAX_OPT_ARGS];
257 while ((p = strsep(&options, ","))) {
263 args[0].to = args[0].from = NULL;
264 token = match_token(p, erofs_tokens, args);
267 #ifdef CONFIG_EROFS_FS_XATTR
269 set_opt(EROFS_SB(sb), XATTR_USER);
271 case Opt_nouser_xattr:
272 clear_opt(EROFS_SB(sb), XATTR_USER);
276 infoln("user_xattr options not supported");
278 case Opt_nouser_xattr:
279 infoln("nouser_xattr options not supported");
282 #ifdef CONFIG_EROFS_FS_POSIX_ACL
284 set_opt(EROFS_SB(sb), POSIX_ACL);
287 clear_opt(EROFS_SB(sb), POSIX_ACL);
291 infoln("acl options not supported");
294 infoln("noacl options not supported");
297 case Opt_fault_injection:
298 err = erofs_build_fault_attr(EROFS_SB(sb), args);
304 errln("Unrecognized mount option \"%s\" "
305 "or missing value", p);
312 #ifdef EROFS_FS_HAS_MANAGED_CACHE
314 static const struct address_space_operations managed_cache_aops;
316 static int managed_cache_releasepage(struct page *page, gfp_t gfp_mask)
318 int ret = 1; /* 0 - busy */
319 struct address_space *const mapping = page->mapping;
321 DBG_BUGON(!PageLocked(page));
322 DBG_BUGON(mapping->a_ops != &managed_cache_aops);
324 if (PagePrivate(page))
325 ret = erofs_try_to_free_cached_page(mapping, page);
330 static void managed_cache_invalidatepage(struct page *page,
334 const unsigned int stop = length + offset;
336 DBG_BUGON(!PageLocked(page));
338 /* Check for potential overflow in debug mode */
339 DBG_BUGON(stop > PAGE_SIZE || stop < length);
341 if (offset == 0 && stop == PAGE_SIZE)
342 while (!managed_cache_releasepage(page, GFP_NOFS))
346 static const struct address_space_operations managed_cache_aops = {
347 .releasepage = managed_cache_releasepage,
348 .invalidatepage = managed_cache_invalidatepage,
351 static struct inode *erofs_init_managed_cache(struct super_block *sb)
353 struct inode *inode = new_inode(sb);
355 if (unlikely(!inode))
356 return ERR_PTR(-ENOMEM);
359 inode->i_size = OFFSET_MAX;
361 inode->i_mapping->a_ops = &managed_cache_aops;
362 mapping_set_gfp_mask(inode->i_mapping,
363 GFP_NOFS | __GFP_HIGHMEM |
364 __GFP_MOVABLE | __GFP_NOFAIL);
370 static int erofs_read_super(struct super_block *sb,
371 const char *dev_name,
372 void *data, int silent)
375 struct erofs_sb_info *sbi;
378 infoln("read_super, device -> %s", dev_name);
379 infoln("options -> %s", (char *)data);
381 if (unlikely(!sb_set_blocksize(sb, EROFS_BLKSIZ))) {
382 errln("failed to set erofs blksize");
386 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
387 if (unlikely(!sbi)) {
393 err = superblock_read(sb);
397 sb->s_magic = EROFS_SUPER_MAGIC;
398 sb->s_flags |= SB_RDONLY | SB_NOATIME;
399 sb->s_maxbytes = MAX_LFS_FILESIZE;
402 sb->s_op = &erofs_sops;
404 #ifdef CONFIG_EROFS_FS_XATTR
405 sb->s_xattr = erofs_xattr_handlers;
408 /* set erofs default mount options */
409 default_options(sbi);
411 err = parse_options(sb, data);
416 infoln("root inode @ nid %llu", ROOT_NID(sbi));
418 if (test_opt(sbi, POSIX_ACL))
419 sb->s_flags |= SB_POSIXACL;
421 sb->s_flags &= ~SB_POSIXACL;
423 #ifdef CONFIG_EROFS_FS_ZIP
424 INIT_RADIX_TREE(&sbi->workstn_tree, GFP_ATOMIC);
427 #ifdef EROFS_FS_HAS_MANAGED_CACHE
428 sbi->managed_cache = erofs_init_managed_cache(sb);
429 if (IS_ERR(sbi->managed_cache)) {
430 err = PTR_ERR(sbi->managed_cache);
431 goto err_init_managed_cache;
435 /* get the root inode */
436 inode = erofs_iget(sb, ROOT_NID(sbi), true);
438 err = PTR_ERR(inode);
442 if (!S_ISDIR(inode->i_mode)) {
443 errln("rootino(nid %llu) is not a directory(i_mode %o)",
444 ROOT_NID(sbi), inode->i_mode);
450 sb->s_root = d_make_root(inode);
456 /* save the device name to sbi */
457 sbi->dev_name = __getname();
458 if (!sbi->dev_name) {
463 snprintf(sbi->dev_name, PATH_MAX, "%s", dev_name);
464 sbi->dev_name[PATH_MAX - 1] = '\0';
466 erofs_register_super(sb);
469 infoln("mounted on %s with opts: %s.", dev_name,
473 * please add a label for each exit point and use
474 * the following name convention, thus new features
475 * can be integrated easily without renaming labels.
481 #ifdef EROFS_FS_HAS_MANAGED_CACHE
482 iput(sbi->managed_cache);
483 err_init_managed_cache:
487 sb->s_fs_info = NULL;
494 * could be triggered after deactivate_locked_super()
495 * is called, thus including umount and failed to initialize.
497 static void erofs_put_super(struct super_block *sb)
499 struct erofs_sb_info *sbi = EROFS_SB(sb);
501 /* for cases which are failed in "read_super" */
505 WARN_ON(sb->s_magic != EROFS_SUPER_MAGIC);
507 infoln("unmounted for %s", sbi->dev_name);
508 __putname(sbi->dev_name);
510 #ifdef EROFS_FS_HAS_MANAGED_CACHE
511 iput(sbi->managed_cache);
514 mutex_lock(&sbi->umount_mutex);
516 #ifdef CONFIG_EROFS_FS_ZIP
517 /* clean up the compression space of this sb */
518 erofs_shrink_workstation(EROFS_SB(sb), ~0UL, true);
521 erofs_unregister_super(sb);
522 mutex_unlock(&sbi->umount_mutex);
525 sb->s_fs_info = NULL;
529 struct erofs_mount_private {
530 const char *dev_name;
534 /* support mount_bdev() with options */
535 static int erofs_fill_super(struct super_block *sb,
536 void *_priv, int silent)
538 struct erofs_mount_private *priv = _priv;
540 return erofs_read_super(sb, priv->dev_name,
541 priv->options, silent);
544 static struct dentry *erofs_mount(
545 struct file_system_type *fs_type, int flags,
546 const char *dev_name, void *data)
548 struct erofs_mount_private priv = {
549 .dev_name = dev_name,
553 return mount_bdev(fs_type, flags, dev_name,
554 &priv, erofs_fill_super);
557 static void erofs_kill_sb(struct super_block *sb)
559 kill_block_super(sb);
562 static struct file_system_type erofs_fs_type = {
563 .owner = THIS_MODULE,
565 .mount = erofs_mount,
566 .kill_sb = erofs_kill_sb,
567 .fs_flags = FS_REQUIRES_DEV,
569 MODULE_ALIAS_FS("erofs");
571 static int __init erofs_module_init(void)
575 erofs_check_ondisk_layout_definitions();
576 infoln("initializing erofs " EROFS_VERSION);
578 err = erofs_init_inode_cache();
582 err = register_shrinker(&erofs_shrinker_info);
586 err = z_erofs_init_zip_subsystem();
590 err = register_filesystem(&erofs_fs_type);
594 infoln("successfully to initialize erofs");
598 z_erofs_exit_zip_subsystem();
600 unregister_shrinker(&erofs_shrinker_info);
602 erofs_exit_inode_cache();
607 static void __exit erofs_module_exit(void)
609 unregister_filesystem(&erofs_fs_type);
610 z_erofs_exit_zip_subsystem();
611 unregister_shrinker(&erofs_shrinker_info);
612 erofs_exit_inode_cache();
613 infoln("successfully finalize erofs");
616 /* get filesystem statistics */
617 static int erofs_statfs(struct dentry *dentry, struct kstatfs *buf)
619 struct super_block *sb = dentry->d_sb;
620 struct erofs_sb_info *sbi = EROFS_SB(sb);
621 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
623 buf->f_type = sb->s_magic;
624 buf->f_bsize = EROFS_BLKSIZ;
625 buf->f_blocks = sbi->blocks;
626 buf->f_bfree = buf->f_bavail = 0;
628 buf->f_files = ULLONG_MAX;
629 buf->f_ffree = ULLONG_MAX - sbi->inos;
631 buf->f_namelen = EROFS_NAME_LEN;
633 buf->f_fsid.val[0] = (u32)id;
634 buf->f_fsid.val[1] = (u32)(id >> 32);
638 static int erofs_show_options(struct seq_file *seq, struct dentry *root)
640 struct erofs_sb_info *sbi __maybe_unused = EROFS_SB(root->d_sb);
642 #ifdef CONFIG_EROFS_FS_XATTR
643 if (test_opt(sbi, XATTR_USER))
644 seq_puts(seq, ",user_xattr");
646 seq_puts(seq, ",nouser_xattr");
648 #ifdef CONFIG_EROFS_FS_POSIX_ACL
649 if (test_opt(sbi, POSIX_ACL))
650 seq_puts(seq, ",acl");
652 seq_puts(seq, ",noacl");
654 if (test_opt(sbi, FAULT_INJECTION))
655 seq_printf(seq, ",fault_injection=%u",
656 erofs_get_fault_rate(sbi));
660 static int erofs_remount(struct super_block *sb, int *flags, char *data)
662 struct erofs_sb_info *sbi = EROFS_SB(sb);
663 unsigned int org_mnt_opt = sbi->mount_opt;
664 unsigned int org_inject_rate = erofs_get_fault_rate(sbi);
667 DBG_BUGON(!sb_rdonly(sb));
668 err = parse_options(sb, data);
672 if (test_opt(sbi, POSIX_ACL))
673 sb->s_flags |= SB_POSIXACL;
675 sb->s_flags &= ~SB_POSIXACL;
680 __erofs_build_fault_attr(sbi, org_inject_rate);
681 sbi->mount_opt = org_mnt_opt;
686 const struct super_operations erofs_sops = {
687 .put_super = erofs_put_super,
688 .alloc_inode = alloc_inode,
689 .free_inode = free_inode,
690 .statfs = erofs_statfs,
691 .show_options = erofs_show_options,
692 .remount_fs = erofs_remount,
695 module_init(erofs_module_init);
696 module_exit(erofs_module_exit);
698 MODULE_DESCRIPTION("Enhanced ROM File System");
699 MODULE_AUTHOR("Gao Xiang, Yu Chao, Miao Xie, CONSUMER BG, HUAWEI Inc.");
700 MODULE_LICENSE("GPL");