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 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 BUG_ON(erofs_inode_cachep == NULL);
44 kmem_cache_destroy(erofs_inode_cachep);
47 static struct inode *alloc_inode(struct super_block *sb)
49 struct erofs_vnode *vi =
50 kmem_cache_alloc(erofs_inode_cachep, GFP_KERNEL);
55 /* zero out everything except vfs_inode */
56 memset(vi, 0, offsetof(struct erofs_vnode, vfs_inode));
57 return &vi->vfs_inode;
60 static void i_callback(struct rcu_head *head)
62 struct inode *inode = container_of(head, struct inode, i_rcu);
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 void destroy_inode(struct inode *inode)
76 call_rcu(&inode->i_rcu, i_callback);
79 static int superblock_read(struct super_block *sb)
81 struct erofs_sb_info *sbi;
82 struct buffer_head *bh;
83 struct erofs_super_block *layout;
90 errln("cannot read erofs superblock");
95 layout = (struct erofs_super_block *)((u8 *)bh->b_data
96 + EROFS_SUPER_OFFSET);
99 if (le32_to_cpu(layout->magic) != EROFS_SUPER_MAGIC_V1) {
100 errln("cannot find valid erofs superblock");
104 blkszbits = layout->blkszbits;
105 /* 9(512 bytes) + LOG_SECTORS_PER_BLOCK == LOG_BLOCK_SIZE */
106 if (unlikely(blkszbits != LOG_BLOCK_SIZE)) {
107 errln("blksize %u isn't supported on this platform",
112 sbi->blocks = le32_to_cpu(layout->blocks);
113 sbi->meta_blkaddr = le32_to_cpu(layout->meta_blkaddr);
114 #ifdef CONFIG_EROFS_FS_XATTR
115 sbi->xattr_blkaddr = le32_to_cpu(layout->xattr_blkaddr);
117 sbi->islotbits = ffs(sizeof(struct erofs_inode_v1)) - 1;
118 #ifdef CONFIG_EROFS_FS_ZIP
119 sbi->clusterbits = 12;
121 if (1 << (sbi->clusterbits - 12) > 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));
162 static void default_options(struct erofs_sb_info *sbi)
164 #ifdef CONFIG_EROFS_FS_XATTR
165 set_opt(sbi, XATTR_USER);
168 #ifdef CONFIG_EROFS_FS_POSIX_ACL
169 set_opt(sbi, POSIX_ACL);
182 static match_table_t erofs_tokens = {
183 {Opt_user_xattr, "user_xattr"},
184 {Opt_nouser_xattr, "nouser_xattr"},
186 {Opt_noacl, "noacl"},
187 {Opt_fault_injection, "fault_injection=%u"},
191 static int parse_options(struct super_block *sb, char *options)
193 substring_t args[MAX_OPT_ARGS];
200 while ((p = strsep(&options, ",")) != NULL) {
206 args[0].to = args[0].from = NULL;
207 token = match_token(p, erofs_tokens, args);
210 #ifdef CONFIG_EROFS_FS_XATTR
212 set_opt(EROFS_SB(sb), XATTR_USER);
214 case Opt_nouser_xattr:
215 clear_opt(EROFS_SB(sb), XATTR_USER);
219 infoln("user_xattr options not supported");
221 case Opt_nouser_xattr:
222 infoln("nouser_xattr options not supported");
225 #ifdef CONFIG_EROFS_FS_POSIX_ACL
227 set_opt(EROFS_SB(sb), POSIX_ACL);
230 clear_opt(EROFS_SB(sb), POSIX_ACL);
234 infoln("acl options not supported");
237 infoln("noacl options not supported");
240 case Opt_fault_injection:
241 if (args->from && match_int(args, &arg))
243 #ifdef CONFIG_EROFS_FAULT_INJECTION
244 erofs_build_fault_attr(EROFS_SB(sb), arg);
245 set_opt(EROFS_SB(sb), FAULT_INJECTION);
247 infoln("FAULT_INJECTION was not selected");
251 errln("Unrecognized mount option \"%s\" "
252 "or missing value", p);
259 #ifdef EROFS_FS_HAS_MANAGED_CACHE
261 static const struct address_space_operations managed_cache_aops;
263 static int managed_cache_releasepage(struct page *page, gfp_t gfp_mask)
265 int ret = 1; /* 0 - busy */
266 struct address_space *const mapping = page->mapping;
268 BUG_ON(!PageLocked(page));
269 BUG_ON(mapping->a_ops != &managed_cache_aops);
271 if (PagePrivate(page))
272 ret = erofs_try_to_free_cached_page(mapping, page);
277 static void managed_cache_invalidatepage(struct page *page,
278 unsigned int offset, unsigned int length)
280 const unsigned int stop = length + offset;
282 BUG_ON(!PageLocked(page));
284 /* Check for overflow */
285 BUG_ON(stop > PAGE_SIZE || stop < length);
287 if (offset == 0 && stop == PAGE_SIZE)
288 while (!managed_cache_releasepage(page, GFP_NOFS))
292 static const struct address_space_operations managed_cache_aops = {
293 .releasepage = managed_cache_releasepage,
294 .invalidatepage = managed_cache_invalidatepage,
297 static struct inode *erofs_init_managed_cache(struct super_block *sb)
299 struct inode *inode = new_inode(sb);
301 if (unlikely(inode == NULL))
302 return ERR_PTR(-ENOMEM);
305 inode->i_size = OFFSET_MAX;
307 inode->i_mapping->a_ops = &managed_cache_aops;
308 mapping_set_gfp_mask(inode->i_mapping,
309 GFP_NOFS | __GFP_HIGHMEM |
310 __GFP_MOVABLE | __GFP_NOFAIL);
316 static int erofs_read_super(struct super_block *sb,
317 const char *dev_name, void *data, int silent)
320 struct erofs_sb_info *sbi;
323 infoln("read_super, device -> %s", dev_name);
324 infoln("options -> %s", (char *)data);
326 if (unlikely(!sb_set_blocksize(sb, EROFS_BLKSIZ))) {
327 errln("failed to set erofs blksize");
331 sbi = kzalloc(sizeof(struct erofs_sb_info), GFP_KERNEL);
332 if (unlikely(sbi == NULL)) {
338 err = superblock_read(sb);
342 sb->s_magic = EROFS_SUPER_MAGIC;
343 sb->s_flags |= SB_RDONLY | SB_NOATIME;
344 sb->s_maxbytes = MAX_LFS_FILESIZE;
347 sb->s_op = &erofs_sops;
349 #ifdef CONFIG_EROFS_FS_XATTR
350 sb->s_xattr = erofs_xattr_handlers;
353 /* set erofs default mount options */
354 default_options(sbi);
356 err = parse_options(sb, data);
361 infoln("root inode @ nid %llu", ROOT_NID(sbi));
363 #ifdef CONFIG_EROFS_FS_ZIP
364 INIT_RADIX_TREE(&sbi->workstn_tree, GFP_ATOMIC);
367 #ifdef EROFS_FS_HAS_MANAGED_CACHE
368 sbi->managed_cache = erofs_init_managed_cache(sb);
369 if (IS_ERR(sbi->managed_cache)) {
370 err = PTR_ERR(sbi->managed_cache);
371 goto err_init_managed_cache;
375 /* get the root inode */
376 inode = erofs_iget(sb, ROOT_NID(sbi), true);
378 err = PTR_ERR(inode);
382 if (!S_ISDIR(inode->i_mode)) {
383 errln("rootino(nid %llu) is not a directory(i_mode %o)",
384 ROOT_NID(sbi), inode->i_mode);
389 sb->s_root = d_make_root(inode);
390 if (sb->s_root == NULL) {
395 /* save the device name to sbi */
396 sbi->dev_name = __getname();
397 if (sbi->dev_name == NULL) {
402 snprintf(sbi->dev_name, PATH_MAX, "%s", dev_name);
403 sbi->dev_name[PATH_MAX - 1] = '\0';
405 erofs_register_super(sb);
408 * We already have a positive dentry, which was instantiated
409 * by d_make_root. Just need to d_rehash it.
411 d_rehash(sb->s_root);
414 infoln("mounted on %s with opts: %s.", dev_name,
418 * please add a label for each exit point and use
419 * the following name convention, thus new features
420 * can be integrated easily without renaming labels.
426 if (sb->s_root == NULL)
429 #ifdef EROFS_FS_HAS_MANAGED_CACHE
430 iput(sbi->managed_cache);
431 err_init_managed_cache:
435 sb->s_fs_info = NULL;
442 * could be triggered after deactivate_locked_super()
443 * is called, thus including umount and failed to initialize.
445 static void erofs_put_super(struct super_block *sb)
447 struct erofs_sb_info *sbi = EROFS_SB(sb);
449 /* for cases which are failed in "read_super" */
453 WARN_ON(sb->s_magic != EROFS_SUPER_MAGIC);
455 infoln("unmounted for %s", sbi->dev_name);
456 __putname(sbi->dev_name);
458 #ifdef EROFS_FS_HAS_MANAGED_CACHE
459 iput(sbi->managed_cache);
462 mutex_lock(&sbi->umount_mutex);
464 #ifdef CONFIG_EROFS_FS_ZIP
465 erofs_workstation_cleanup_all(sb);
468 erofs_unregister_super(sb);
469 mutex_unlock(&sbi->umount_mutex);
472 sb->s_fs_info = NULL;
476 struct erofs_mount_private {
477 const char *dev_name;
481 /* support mount_bdev() with options */
482 static int erofs_fill_super(struct super_block *sb,
483 void *_priv, int silent)
485 struct erofs_mount_private *priv = _priv;
487 return erofs_read_super(sb, priv->dev_name,
488 priv->options, silent);
491 static struct dentry *erofs_mount(
492 struct file_system_type *fs_type, int flags,
493 const char *dev_name, void *data)
495 struct erofs_mount_private priv = {
496 .dev_name = dev_name,
500 return mount_bdev(fs_type, flags, dev_name,
501 &priv, erofs_fill_super);
504 static void erofs_kill_sb(struct super_block *sb)
506 kill_block_super(sb);
509 static struct shrinker erofs_shrinker_info = {
510 .scan_objects = erofs_shrink_scan,
511 .count_objects = erofs_shrink_count,
512 .seeks = DEFAULT_SEEKS,
515 static struct file_system_type erofs_fs_type = {
516 .owner = THIS_MODULE,
518 .mount = erofs_mount,
519 .kill_sb = erofs_kill_sb,
520 .fs_flags = FS_REQUIRES_DEV,
522 MODULE_ALIAS_FS("erofs");
524 #ifdef CONFIG_EROFS_FS_ZIP
525 extern int z_erofs_init_zip_subsystem(void);
526 extern void z_erofs_exit_zip_subsystem(void);
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 = register_shrinker(&erofs_shrinker_info);
544 #ifdef CONFIG_EROFS_FS_ZIP
545 err = z_erofs_init_zip_subsystem();
550 err = register_filesystem(&erofs_fs_type);
554 infoln("successfully to initialize erofs");
558 #ifdef CONFIG_EROFS_FS_ZIP
559 z_erofs_exit_zip_subsystem();
562 unregister_shrinker(&erofs_shrinker_info);
564 erofs_exit_inode_cache();
569 static void __exit erofs_module_exit(void)
571 unregister_filesystem(&erofs_fs_type);
572 #ifdef CONFIG_EROFS_FS_ZIP
573 z_erofs_exit_zip_subsystem();
575 unregister_shrinker(&erofs_shrinker_info);
576 erofs_exit_inode_cache();
577 infoln("successfully finalize erofs");
580 /* get filesystem statistics */
581 static int erofs_statfs(struct dentry *dentry, struct kstatfs *buf)
583 struct super_block *sb = dentry->d_sb;
584 struct erofs_sb_info *sbi = EROFS_SB(sb);
585 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
587 buf->f_type = sb->s_magic;
588 buf->f_bsize = EROFS_BLKSIZ;
589 buf->f_blocks = sbi->blocks;
590 buf->f_bfree = buf->f_bavail = 0;
592 buf->f_files = ULLONG_MAX;
593 buf->f_ffree = ULLONG_MAX - sbi->inos;
595 buf->f_namelen = EROFS_NAME_LEN;
597 buf->f_fsid.val[0] = (u32)id;
598 buf->f_fsid.val[1] = (u32)(id >> 32);
602 static int erofs_show_options(struct seq_file *seq, struct dentry *root)
604 struct erofs_sb_info *sbi __maybe_unused = EROFS_SB(root->d_sb);
606 #ifdef CONFIG_EROFS_FS_XATTR
607 if (test_opt(sbi, XATTR_USER))
608 seq_puts(seq, ",user_xattr");
610 seq_puts(seq, ",nouser_xattr");
612 #ifdef CONFIG_EROFS_FS_POSIX_ACL
613 if (test_opt(sbi, POSIX_ACL))
614 seq_puts(seq, ",acl");
616 seq_puts(seq, ",noacl");
618 #ifdef CONFIG_EROFS_FAULT_INJECTION
619 if (test_opt(sbi, FAULT_INJECTION))
620 seq_printf(seq, ",fault_injection=%u",
621 sbi->fault_info.inject_rate);
626 static int erofs_remount(struct super_block *sb, int *flags, char *data)
628 BUG_ON(!sb_rdonly(sb));
634 const struct super_operations erofs_sops = {
635 .put_super = erofs_put_super,
636 .alloc_inode = alloc_inode,
637 .destroy_inode = destroy_inode,
638 .statfs = erofs_statfs,
639 .show_options = erofs_show_options,
640 .remount_fs = erofs_remount,
643 module_init(erofs_module_init);
644 module_exit(erofs_module_exit);
646 MODULE_DESCRIPTION("Enhanced ROM File System");
647 MODULE_AUTHOR("Gao Xiang, Yu Chao, Miao Xie, CONSUMER BG, HUAWEI Inc.");
648 MODULE_LICENSE("GPL");