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>
20 #define CREATE_TRACE_POINTS
21 #include <trace/events/erofs.h>
23 static struct kmem_cache *erofs_inode_cachep __read_mostly;
25 static void init_once(void *ptr)
27 struct erofs_vnode *vi = ptr;
29 inode_init_once(&vi->vfs_inode);
32 static int __init erofs_init_inode_cache(void)
34 erofs_inode_cachep = kmem_cache_create("erofs_inode",
35 sizeof(struct erofs_vnode), 0,
36 SLAB_RECLAIM_ACCOUNT, init_once);
38 return erofs_inode_cachep != NULL ? 0 : -ENOMEM;
41 static void erofs_exit_inode_cache(void)
43 kmem_cache_destroy(erofs_inode_cachep);
46 static struct inode *alloc_inode(struct super_block *sb)
48 struct erofs_vnode *vi =
49 kmem_cache_alloc(erofs_inode_cachep, GFP_KERNEL);
54 /* zero out everything except vfs_inode */
55 memset(vi, 0, offsetof(struct erofs_vnode, vfs_inode));
56 return &vi->vfs_inode;
59 static void i_callback(struct rcu_head *head)
61 struct inode *inode = container_of(head, struct inode, i_rcu);
62 struct erofs_vnode *vi = EROFS_V(inode);
64 /* be careful RCU symlink path (see ext4_inode_info->i_data)! */
65 if (is_inode_fast_symlink(inode))
68 kfree(vi->xattr_shared_xattrs);
70 kmem_cache_free(erofs_inode_cachep, vi);
73 static void destroy_inode(struct inode *inode)
75 call_rcu(&inode->i_rcu, i_callback);
78 static int superblock_read(struct super_block *sb)
80 struct erofs_sb_info *sbi;
81 struct buffer_head *bh;
82 struct erofs_super_block *layout;
83 unsigned int blkszbits;
89 errln("cannot read erofs superblock");
94 layout = (struct erofs_super_block *)((u8 *)bh->b_data
95 + EROFS_SUPER_OFFSET);
98 if (le32_to_cpu(layout->magic) != EROFS_SUPER_MAGIC_V1) {
99 errln("cannot find valid erofs superblock");
103 blkszbits = layout->blkszbits;
104 /* 9(512 bytes) + LOG_SECTORS_PER_BLOCK == LOG_BLOCK_SIZE */
105 if (unlikely(blkszbits != LOG_BLOCK_SIZE)) {
106 errln("blksize %u isn't supported on this platform",
111 sbi->blocks = le32_to_cpu(layout->blocks);
112 sbi->meta_blkaddr = le32_to_cpu(layout->meta_blkaddr);
113 #ifdef CONFIG_EROFS_FS_XATTR
114 sbi->xattr_blkaddr = le32_to_cpu(layout->xattr_blkaddr);
116 sbi->islotbits = ffs(sizeof(struct erofs_inode_v1)) - 1;
117 #ifdef CONFIG_EROFS_FS_ZIP
118 /* TODO: clusterbits should be related to inode */
119 sbi->clusterbits = blkszbits;
121 if (1 << (sbi->clusterbits - PAGE_SHIFT) > Z_EROFS_CLUSTER_MAX_PAGES)
122 errln("clusterbits %u is not supported on this kernel",
126 sbi->root_nid = le16_to_cpu(layout->root_nid);
127 sbi->inos = le64_to_cpu(layout->inos);
129 sbi->build_time = le64_to_cpu(layout->build_time);
130 sbi->build_time_nsec = le32_to_cpu(layout->build_time_nsec);
132 memcpy(&sb->s_uuid, layout->uuid, sizeof(layout->uuid));
133 memcpy(sbi->volume_name, layout->volume_name,
134 sizeof(layout->volume_name));
142 #ifdef CONFIG_EROFS_FAULT_INJECTION
143 char *erofs_fault_name[FAULT_MAX] = {
144 [FAULT_KMALLOC] = "kmalloc",
147 static void __erofs_build_fault_attr(struct erofs_sb_info *sbi,
150 struct erofs_fault_info *ffi = &sbi->fault_info;
153 atomic_set(&ffi->inject_ops, 0);
154 ffi->inject_rate = rate;
155 ffi->inject_type = (1 << FAULT_MAX) - 1;
157 memset(ffi, 0, sizeof(struct erofs_fault_info));
160 set_opt(sbi, FAULT_INJECTION);
163 static int erofs_build_fault_attr(struct erofs_sb_info *sbi,
168 if (args->from && match_int(args, &rate))
171 __erofs_build_fault_attr(sbi, rate);
175 static unsigned int erofs_get_fault_rate(struct erofs_sb_info *sbi)
177 return sbi->fault_info.inject_rate;
180 static void __erofs_build_fault_attr(struct erofs_sb_info *sbi,
185 static int erofs_build_fault_attr(struct erofs_sb_info *sbi,
188 infoln("fault_injection options not supported");
192 static unsigned int erofs_get_fault_rate(struct erofs_sb_info *sbi)
198 static void default_options(struct erofs_sb_info *sbi)
200 /* set up some FS parameters */
201 #ifdef CONFIG_EROFS_FS_ZIP
202 sbi->max_sync_decompress_pages = DEFAULT_MAX_SYNC_DECOMPRESS_PAGES;
205 #ifdef CONFIG_EROFS_FS_XATTR
206 set_opt(sbi, XATTR_USER);
209 #ifdef CONFIG_EROFS_FS_POSIX_ACL
210 set_opt(sbi, POSIX_ACL);
223 static match_table_t erofs_tokens = {
224 {Opt_user_xattr, "user_xattr"},
225 {Opt_nouser_xattr, "nouser_xattr"},
227 {Opt_noacl, "noacl"},
228 {Opt_fault_injection, "fault_injection=%u"},
232 static int parse_options(struct super_block *sb, char *options)
234 substring_t args[MAX_OPT_ARGS];
241 while ((p = strsep(&options, ",")) != NULL) {
247 args[0].to = args[0].from = NULL;
248 token = match_token(p, erofs_tokens, args);
251 #ifdef CONFIG_EROFS_FS_XATTR
253 set_opt(EROFS_SB(sb), XATTR_USER);
255 case Opt_nouser_xattr:
256 clear_opt(EROFS_SB(sb), XATTR_USER);
260 infoln("user_xattr options not supported");
262 case Opt_nouser_xattr:
263 infoln("nouser_xattr options not supported");
266 #ifdef CONFIG_EROFS_FS_POSIX_ACL
268 set_opt(EROFS_SB(sb), POSIX_ACL);
271 clear_opt(EROFS_SB(sb), POSIX_ACL);
275 infoln("acl options not supported");
278 infoln("noacl options not supported");
281 case Opt_fault_injection:
282 err = erofs_build_fault_attr(EROFS_SB(sb), args);
288 errln("Unrecognized mount option \"%s\" "
289 "or missing value", p);
296 #ifdef EROFS_FS_HAS_MANAGED_CACHE
298 static const struct address_space_operations managed_cache_aops;
300 static int managed_cache_releasepage(struct page *page, gfp_t gfp_mask)
302 int ret = 1; /* 0 - busy */
303 struct address_space *const mapping = page->mapping;
305 DBG_BUGON(!PageLocked(page));
306 DBG_BUGON(mapping->a_ops != &managed_cache_aops);
308 if (PagePrivate(page))
309 ret = erofs_try_to_free_cached_page(mapping, page);
314 static void managed_cache_invalidatepage(struct page *page,
315 unsigned int offset, unsigned int length)
317 const unsigned int stop = length + offset;
319 DBG_BUGON(!PageLocked(page));
321 /* Check for potential overflow in debug mode */
322 DBG_BUGON(stop > PAGE_SIZE || stop < length);
324 if (offset == 0 && stop == PAGE_SIZE)
325 while (!managed_cache_releasepage(page, GFP_NOFS))
329 static const struct address_space_operations managed_cache_aops = {
330 .releasepage = managed_cache_releasepage,
331 .invalidatepage = managed_cache_invalidatepage,
334 static struct inode *erofs_init_managed_cache(struct super_block *sb)
336 struct inode *inode = new_inode(sb);
338 if (unlikely(inode == NULL))
339 return ERR_PTR(-ENOMEM);
342 inode->i_size = OFFSET_MAX;
344 inode->i_mapping->a_ops = &managed_cache_aops;
345 mapping_set_gfp_mask(inode->i_mapping,
346 GFP_NOFS | __GFP_HIGHMEM |
347 __GFP_MOVABLE | __GFP_NOFAIL);
353 static int erofs_read_super(struct super_block *sb,
354 const char *dev_name, void *data, int silent)
357 struct erofs_sb_info *sbi;
360 infoln("read_super, device -> %s", dev_name);
361 infoln("options -> %s", (char *)data);
363 if (unlikely(!sb_set_blocksize(sb, EROFS_BLKSIZ))) {
364 errln("failed to set erofs blksize");
368 sbi = kzalloc(sizeof(struct erofs_sb_info), GFP_KERNEL);
369 if (unlikely(sbi == NULL)) {
375 err = superblock_read(sb);
379 sb->s_magic = EROFS_SUPER_MAGIC;
380 sb->s_flags |= SB_RDONLY | SB_NOATIME;
381 sb->s_maxbytes = MAX_LFS_FILESIZE;
384 sb->s_op = &erofs_sops;
386 #ifdef CONFIG_EROFS_FS_XATTR
387 sb->s_xattr = erofs_xattr_handlers;
390 /* set erofs default mount options */
391 default_options(sbi);
393 err = parse_options(sb, data);
398 infoln("root inode @ nid %llu", ROOT_NID(sbi));
400 #ifdef CONFIG_EROFS_FS_ZIP
401 INIT_RADIX_TREE(&sbi->workstn_tree, GFP_ATOMIC);
404 #ifdef EROFS_FS_HAS_MANAGED_CACHE
405 sbi->managed_cache = erofs_init_managed_cache(sb);
406 if (IS_ERR(sbi->managed_cache)) {
407 err = PTR_ERR(sbi->managed_cache);
408 goto err_init_managed_cache;
412 /* get the root inode */
413 inode = erofs_iget(sb, ROOT_NID(sbi), true);
415 err = PTR_ERR(inode);
419 if (!S_ISDIR(inode->i_mode)) {
420 errln("rootino(nid %llu) is not a directory(i_mode %o)",
421 ROOT_NID(sbi), inode->i_mode);
426 sb->s_root = d_make_root(inode);
427 if (sb->s_root == NULL) {
432 /* save the device name to sbi */
433 sbi->dev_name = __getname();
434 if (sbi->dev_name == NULL) {
439 snprintf(sbi->dev_name, PATH_MAX, "%s", dev_name);
440 sbi->dev_name[PATH_MAX - 1] = '\0';
442 erofs_register_super(sb);
445 infoln("mounted on %s with opts: %s.", dev_name,
449 * please add a label for each exit point and use
450 * the following name convention, thus new features
451 * can be integrated easily without renaming labels.
457 if (sb->s_root == NULL)
460 #ifdef EROFS_FS_HAS_MANAGED_CACHE
461 iput(sbi->managed_cache);
462 err_init_managed_cache:
466 sb->s_fs_info = NULL;
473 * could be triggered after deactivate_locked_super()
474 * is called, thus including umount and failed to initialize.
476 static void erofs_put_super(struct super_block *sb)
478 struct erofs_sb_info *sbi = EROFS_SB(sb);
480 /* for cases which are failed in "read_super" */
484 WARN_ON(sb->s_magic != EROFS_SUPER_MAGIC);
486 infoln("unmounted for %s", sbi->dev_name);
487 __putname(sbi->dev_name);
489 #ifdef EROFS_FS_HAS_MANAGED_CACHE
490 iput(sbi->managed_cache);
493 mutex_lock(&sbi->umount_mutex);
495 #ifdef CONFIG_EROFS_FS_ZIP
496 erofs_workstation_cleanup_all(sb);
499 erofs_unregister_super(sb);
500 mutex_unlock(&sbi->umount_mutex);
503 sb->s_fs_info = NULL;
507 struct erofs_mount_private {
508 const char *dev_name;
512 /* support mount_bdev() with options */
513 static int erofs_fill_super(struct super_block *sb,
514 void *_priv, int silent)
516 struct erofs_mount_private *priv = _priv;
518 return erofs_read_super(sb, priv->dev_name,
519 priv->options, silent);
522 static struct dentry *erofs_mount(
523 struct file_system_type *fs_type, int flags,
524 const char *dev_name, void *data)
526 struct erofs_mount_private priv = {
527 .dev_name = dev_name,
531 return mount_bdev(fs_type, flags, dev_name,
532 &priv, erofs_fill_super);
535 static void erofs_kill_sb(struct super_block *sb)
537 kill_block_super(sb);
540 static struct shrinker erofs_shrinker_info = {
541 .scan_objects = erofs_shrink_scan,
542 .count_objects = erofs_shrink_count,
543 .seeks = DEFAULT_SEEKS,
546 static struct file_system_type erofs_fs_type = {
547 .owner = THIS_MODULE,
549 .mount = erofs_mount,
550 .kill_sb = erofs_kill_sb,
551 .fs_flags = FS_REQUIRES_DEV,
553 MODULE_ALIAS_FS("erofs");
555 static int __init erofs_module_init(void)
559 erofs_check_ondisk_layout_definitions();
560 infoln("initializing erofs " EROFS_VERSION);
562 err = erofs_init_inode_cache();
566 err = register_shrinker(&erofs_shrinker_info);
570 err = z_erofs_init_zip_subsystem();
574 err = register_filesystem(&erofs_fs_type);
578 infoln("successfully to initialize erofs");
582 z_erofs_exit_zip_subsystem();
584 unregister_shrinker(&erofs_shrinker_info);
586 erofs_exit_inode_cache();
591 static void __exit erofs_module_exit(void)
593 unregister_filesystem(&erofs_fs_type);
594 z_erofs_exit_zip_subsystem();
595 unregister_shrinker(&erofs_shrinker_info);
596 erofs_exit_inode_cache();
597 infoln("successfully finalize erofs");
600 /* get filesystem statistics */
601 static int erofs_statfs(struct dentry *dentry, struct kstatfs *buf)
603 struct super_block *sb = dentry->d_sb;
604 struct erofs_sb_info *sbi = EROFS_SB(sb);
605 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
607 buf->f_type = sb->s_magic;
608 buf->f_bsize = EROFS_BLKSIZ;
609 buf->f_blocks = sbi->blocks;
610 buf->f_bfree = buf->f_bavail = 0;
612 buf->f_files = ULLONG_MAX;
613 buf->f_ffree = ULLONG_MAX - sbi->inos;
615 buf->f_namelen = EROFS_NAME_LEN;
617 buf->f_fsid.val[0] = (u32)id;
618 buf->f_fsid.val[1] = (u32)(id >> 32);
622 static int erofs_show_options(struct seq_file *seq, struct dentry *root)
624 struct erofs_sb_info *sbi __maybe_unused = EROFS_SB(root->d_sb);
626 #ifdef CONFIG_EROFS_FS_XATTR
627 if (test_opt(sbi, XATTR_USER))
628 seq_puts(seq, ",user_xattr");
630 seq_puts(seq, ",nouser_xattr");
632 #ifdef CONFIG_EROFS_FS_POSIX_ACL
633 if (test_opt(sbi, POSIX_ACL))
634 seq_puts(seq, ",acl");
636 seq_puts(seq, ",noacl");
638 if (test_opt(sbi, FAULT_INJECTION))
639 seq_printf(seq, ",fault_injection=%u",
640 erofs_get_fault_rate(sbi));
644 static int erofs_remount(struct super_block *sb, int *flags, char *data)
646 struct erofs_sb_info *sbi = EROFS_SB(sb);
647 unsigned int org_mnt_opt = sbi->mount_opt;
648 unsigned int org_inject_rate = erofs_get_fault_rate(sbi);
651 DBG_BUGON(!sb_rdonly(sb));
652 err = parse_options(sb, data);
659 __erofs_build_fault_attr(sbi, org_inject_rate);
660 sbi->mount_opt = org_mnt_opt;
665 const struct super_operations erofs_sops = {
666 .put_super = erofs_put_super,
667 .alloc_inode = alloc_inode,
668 .destroy_inode = destroy_inode,
669 .statfs = erofs_statfs,
670 .show_options = erofs_show_options,
671 .remount_fs = erofs_remount,
674 module_init(erofs_module_init);
675 module_exit(erofs_module_exit);
677 MODULE_DESCRIPTION("Enhanced ROM File System");
678 MODULE_AUTHOR("Gao Xiang, Yu Chao, Miao Xie, CONSUMER BG, HUAWEI Inc.");
679 MODULE_LICENSE("GPL");