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 int superblock_read(struct super_block *sb)
76 struct erofs_sb_info *sbi;
77 struct buffer_head *bh;
78 struct erofs_super_block *layout;
79 unsigned int blkszbits;
85 errln("cannot read erofs superblock");
90 layout = (struct erofs_super_block *)((u8 *)bh->b_data
91 + EROFS_SUPER_OFFSET);
94 if (le32_to_cpu(layout->magic) != EROFS_SUPER_MAGIC_V1) {
95 errln("cannot find valid erofs superblock");
99 blkszbits = layout->blkszbits;
100 /* 9(512 bytes) + LOG_SECTORS_PER_BLOCK == LOG_BLOCK_SIZE */
101 if (unlikely(blkszbits != LOG_BLOCK_SIZE)) {
102 errln("blksize %u isn't supported on this platform",
107 sbi->blocks = le32_to_cpu(layout->blocks);
108 sbi->meta_blkaddr = le32_to_cpu(layout->meta_blkaddr);
109 #ifdef CONFIG_EROFS_FS_XATTR
110 sbi->xattr_blkaddr = le32_to_cpu(layout->xattr_blkaddr);
112 sbi->islotbits = ffs(sizeof(struct erofs_inode_v1)) - 1;
113 #ifdef CONFIG_EROFS_FS_ZIP
114 /* TODO: clusterbits should be related to inode */
115 sbi->clusterbits = blkszbits;
117 if (1 << (sbi->clusterbits - PAGE_SHIFT) > Z_EROFS_CLUSTER_MAX_PAGES)
118 errln("clusterbits %u is not supported on this kernel",
122 sbi->root_nid = le16_to_cpu(layout->root_nid);
123 sbi->inos = le64_to_cpu(layout->inos);
125 sbi->build_time = le64_to_cpu(layout->build_time);
126 sbi->build_time_nsec = le32_to_cpu(layout->build_time_nsec);
128 memcpy(&sb->s_uuid, layout->uuid, sizeof(layout->uuid));
129 memcpy(sbi->volume_name, layout->volume_name,
130 sizeof(layout->volume_name));
138 #ifdef CONFIG_EROFS_FAULT_INJECTION
139 const char *erofs_fault_name[FAULT_MAX] = {
140 [FAULT_KMALLOC] = "kmalloc",
141 [FAULT_READ_IO] = "read IO error",
144 static void __erofs_build_fault_attr(struct erofs_sb_info *sbi,
147 struct erofs_fault_info *ffi = &sbi->fault_info;
150 atomic_set(&ffi->inject_ops, 0);
151 ffi->inject_rate = rate;
152 ffi->inject_type = (1 << FAULT_MAX) - 1;
154 memset(ffi, 0, sizeof(struct erofs_fault_info));
157 set_opt(sbi, FAULT_INJECTION);
160 static int erofs_build_fault_attr(struct erofs_sb_info *sbi,
165 if (args->from && match_int(args, &rate))
168 __erofs_build_fault_attr(sbi, rate);
172 static unsigned int erofs_get_fault_rate(struct erofs_sb_info *sbi)
174 return sbi->fault_info.inject_rate;
177 static void __erofs_build_fault_attr(struct erofs_sb_info *sbi,
182 static int erofs_build_fault_attr(struct erofs_sb_info *sbi,
185 infoln("fault_injection options not supported");
189 static unsigned int erofs_get_fault_rate(struct erofs_sb_info *sbi)
195 static void default_options(struct erofs_sb_info *sbi)
197 /* set up some FS parameters */
198 #ifdef CONFIG_EROFS_FS_ZIP
199 sbi->max_sync_decompress_pages = DEFAULT_MAX_SYNC_DECOMPRESS_PAGES;
202 #ifdef CONFIG_EROFS_FS_XATTR
203 set_opt(sbi, XATTR_USER);
206 #ifdef CONFIG_EROFS_FS_POSIX_ACL
207 set_opt(sbi, POSIX_ACL);
220 static match_table_t erofs_tokens = {
221 {Opt_user_xattr, "user_xattr"},
222 {Opt_nouser_xattr, "nouser_xattr"},
224 {Opt_noacl, "noacl"},
225 {Opt_fault_injection, "fault_injection=%u"},
229 static int parse_options(struct super_block *sb, char *options)
231 substring_t args[MAX_OPT_ARGS];
238 while ((p = strsep(&options, ","))) {
244 args[0].to = args[0].from = NULL;
245 token = match_token(p, erofs_tokens, args);
248 #ifdef CONFIG_EROFS_FS_XATTR
250 set_opt(EROFS_SB(sb), XATTR_USER);
252 case Opt_nouser_xattr:
253 clear_opt(EROFS_SB(sb), XATTR_USER);
257 infoln("user_xattr options not supported");
259 case Opt_nouser_xattr:
260 infoln("nouser_xattr options not supported");
263 #ifdef CONFIG_EROFS_FS_POSIX_ACL
265 set_opt(EROFS_SB(sb), POSIX_ACL);
268 clear_opt(EROFS_SB(sb), POSIX_ACL);
272 infoln("acl options not supported");
275 infoln("noacl options not supported");
278 case Opt_fault_injection:
279 err = erofs_build_fault_attr(EROFS_SB(sb), args);
285 errln("Unrecognized mount option \"%s\" "
286 "or missing value", p);
293 #ifdef EROFS_FS_HAS_MANAGED_CACHE
295 static const struct address_space_operations managed_cache_aops;
297 static int managed_cache_releasepage(struct page *page, gfp_t gfp_mask)
299 int ret = 1; /* 0 - busy */
300 struct address_space *const mapping = page->mapping;
302 DBG_BUGON(!PageLocked(page));
303 DBG_BUGON(mapping->a_ops != &managed_cache_aops);
305 if (PagePrivate(page))
306 ret = erofs_try_to_free_cached_page(mapping, page);
311 static void managed_cache_invalidatepage(struct page *page,
315 const unsigned int stop = length + offset;
317 DBG_BUGON(!PageLocked(page));
319 /* Check for potential overflow in debug mode */
320 DBG_BUGON(stop > PAGE_SIZE || stop < length);
322 if (offset == 0 && stop == PAGE_SIZE)
323 while (!managed_cache_releasepage(page, GFP_NOFS))
327 static const struct address_space_operations managed_cache_aops = {
328 .releasepage = managed_cache_releasepage,
329 .invalidatepage = managed_cache_invalidatepage,
332 static struct inode *erofs_init_managed_cache(struct super_block *sb)
334 struct inode *inode = new_inode(sb);
336 if (unlikely(!inode))
337 return ERR_PTR(-ENOMEM);
340 inode->i_size = OFFSET_MAX;
342 inode->i_mapping->a_ops = &managed_cache_aops;
343 mapping_set_gfp_mask(inode->i_mapping,
344 GFP_NOFS | __GFP_HIGHMEM |
345 __GFP_MOVABLE | __GFP_NOFAIL);
351 static int erofs_read_super(struct super_block *sb,
352 const char *dev_name,
353 void *data, int silent)
356 struct erofs_sb_info *sbi;
359 infoln("read_super, device -> %s", dev_name);
360 infoln("options -> %s", (char *)data);
362 if (unlikely(!sb_set_blocksize(sb, EROFS_BLKSIZ))) {
363 errln("failed to set erofs blksize");
367 sbi = kzalloc(sizeof(struct erofs_sb_info), GFP_KERNEL);
368 if (unlikely(!sbi)) {
374 err = superblock_read(sb);
378 sb->s_magic = EROFS_SUPER_MAGIC;
379 sb->s_flags |= SB_RDONLY | SB_NOATIME;
380 sb->s_maxbytes = MAX_LFS_FILESIZE;
383 sb->s_op = &erofs_sops;
385 #ifdef CONFIG_EROFS_FS_XATTR
386 sb->s_xattr = erofs_xattr_handlers;
389 /* set erofs default mount options */
390 default_options(sbi);
392 err = parse_options(sb, data);
397 infoln("root inode @ nid %llu", ROOT_NID(sbi));
399 if (test_opt(sbi, POSIX_ACL))
400 sb->s_flags |= SB_POSIXACL;
402 sb->s_flags &= ~SB_POSIXACL;
404 #ifdef CONFIG_EROFS_FS_ZIP
405 INIT_RADIX_TREE(&sbi->workstn_tree, GFP_ATOMIC);
408 #ifdef EROFS_FS_HAS_MANAGED_CACHE
409 sbi->managed_cache = erofs_init_managed_cache(sb);
410 if (IS_ERR(sbi->managed_cache)) {
411 err = PTR_ERR(sbi->managed_cache);
412 goto err_init_managed_cache;
416 /* get the root inode */
417 inode = erofs_iget(sb, ROOT_NID(sbi), true);
419 err = PTR_ERR(inode);
423 if (!S_ISDIR(inode->i_mode)) {
424 errln("rootino(nid %llu) is not a directory(i_mode %o)",
425 ROOT_NID(sbi), inode->i_mode);
431 sb->s_root = d_make_root(inode);
437 /* save the device name to sbi */
438 sbi->dev_name = __getname();
439 if (!sbi->dev_name) {
444 snprintf(sbi->dev_name, PATH_MAX, "%s", dev_name);
445 sbi->dev_name[PATH_MAX - 1] = '\0';
447 erofs_register_super(sb);
450 infoln("mounted on %s with opts: %s.", dev_name,
454 * please add a label for each exit point and use
455 * the following name convention, thus new features
456 * can be integrated easily without renaming labels.
461 #ifdef EROFS_FS_HAS_MANAGED_CACHE
462 iput(sbi->managed_cache);
463 err_init_managed_cache:
467 sb->s_fs_info = NULL;
474 * could be triggered after deactivate_locked_super()
475 * is called, thus including umount and failed to initialize.
477 static void erofs_put_super(struct super_block *sb)
479 struct erofs_sb_info *sbi = EROFS_SB(sb);
481 /* for cases which are failed in "read_super" */
485 WARN_ON(sb->s_magic != EROFS_SUPER_MAGIC);
487 infoln("unmounted for %s", sbi->dev_name);
488 __putname(sbi->dev_name);
490 #ifdef EROFS_FS_HAS_MANAGED_CACHE
491 iput(sbi->managed_cache);
494 mutex_lock(&sbi->umount_mutex);
496 #ifdef CONFIG_EROFS_FS_ZIP
497 /* clean up the compression space of this sb */
498 erofs_shrink_workstation(EROFS_SB(sb), ~0UL, true);
501 erofs_unregister_super(sb);
502 mutex_unlock(&sbi->umount_mutex);
505 sb->s_fs_info = NULL;
509 struct erofs_mount_private {
510 const char *dev_name;
514 /* support mount_bdev() with options */
515 static int erofs_fill_super(struct super_block *sb,
516 void *_priv, int silent)
518 struct erofs_mount_private *priv = _priv;
520 return erofs_read_super(sb, priv->dev_name,
521 priv->options, silent);
524 static struct dentry *erofs_mount(
525 struct file_system_type *fs_type, int flags,
526 const char *dev_name, void *data)
528 struct erofs_mount_private priv = {
529 .dev_name = dev_name,
533 return mount_bdev(fs_type, flags, dev_name,
534 &priv, erofs_fill_super);
537 static void erofs_kill_sb(struct super_block *sb)
539 kill_block_super(sb);
542 static struct file_system_type erofs_fs_type = {
543 .owner = THIS_MODULE,
545 .mount = erofs_mount,
546 .kill_sb = erofs_kill_sb,
547 .fs_flags = FS_REQUIRES_DEV,
549 MODULE_ALIAS_FS("erofs");
551 static int __init erofs_module_init(void)
555 erofs_check_ondisk_layout_definitions();
556 infoln("initializing erofs " EROFS_VERSION);
558 err = erofs_init_inode_cache();
562 err = register_shrinker(&erofs_shrinker_info);
566 err = z_erofs_init_zip_subsystem();
570 err = register_filesystem(&erofs_fs_type);
574 infoln("successfully to initialize erofs");
578 z_erofs_exit_zip_subsystem();
580 unregister_shrinker(&erofs_shrinker_info);
582 erofs_exit_inode_cache();
587 static void __exit erofs_module_exit(void)
589 unregister_filesystem(&erofs_fs_type);
590 z_erofs_exit_zip_subsystem();
591 unregister_shrinker(&erofs_shrinker_info);
592 erofs_exit_inode_cache();
593 infoln("successfully finalize erofs");
596 /* get filesystem statistics */
597 static int erofs_statfs(struct dentry *dentry, struct kstatfs *buf)
599 struct super_block *sb = dentry->d_sb;
600 struct erofs_sb_info *sbi = EROFS_SB(sb);
601 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
603 buf->f_type = sb->s_magic;
604 buf->f_bsize = EROFS_BLKSIZ;
605 buf->f_blocks = sbi->blocks;
606 buf->f_bfree = buf->f_bavail = 0;
608 buf->f_files = ULLONG_MAX;
609 buf->f_ffree = ULLONG_MAX - sbi->inos;
611 buf->f_namelen = EROFS_NAME_LEN;
613 buf->f_fsid.val[0] = (u32)id;
614 buf->f_fsid.val[1] = (u32)(id >> 32);
618 static int erofs_show_options(struct seq_file *seq, struct dentry *root)
620 struct erofs_sb_info *sbi __maybe_unused = EROFS_SB(root->d_sb);
622 #ifdef CONFIG_EROFS_FS_XATTR
623 if (test_opt(sbi, XATTR_USER))
624 seq_puts(seq, ",user_xattr");
626 seq_puts(seq, ",nouser_xattr");
628 #ifdef CONFIG_EROFS_FS_POSIX_ACL
629 if (test_opt(sbi, POSIX_ACL))
630 seq_puts(seq, ",acl");
632 seq_puts(seq, ",noacl");
634 if (test_opt(sbi, FAULT_INJECTION))
635 seq_printf(seq, ",fault_injection=%u",
636 erofs_get_fault_rate(sbi));
640 static int erofs_remount(struct super_block *sb, int *flags, char *data)
642 struct erofs_sb_info *sbi = EROFS_SB(sb);
643 unsigned int org_mnt_opt = sbi->mount_opt;
644 unsigned int org_inject_rate = erofs_get_fault_rate(sbi);
647 DBG_BUGON(!sb_rdonly(sb));
648 err = parse_options(sb, data);
652 if (test_opt(sbi, POSIX_ACL))
653 sb->s_flags |= SB_POSIXACL;
655 sb->s_flags &= ~SB_POSIXACL;
660 __erofs_build_fault_attr(sbi, org_inject_rate);
661 sbi->mount_opt = org_mnt_opt;
666 const struct super_operations erofs_sops = {
667 .put_super = erofs_put_super,
668 .alloc_inode = alloc_inode,
669 .free_inode = free_inode,
670 .statfs = erofs_statfs,
671 .show_options = erofs_show_options,
672 .remount_fs = erofs_remount,
675 module_init(erofs_module_init);
676 module_exit(erofs_module_exit);
678 MODULE_DESCRIPTION("Enhanced ROM File System");
679 MODULE_AUTHOR("Gao Xiang, Yu Chao, Miao Xie, CONSUMER BG, HUAWEI Inc.");
680 MODULE_LICENSE("GPL");