1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2017-2018 HUAWEI, Inc.
4 * http://www.huawei.com/
7 #include <linux/module.h>
8 #include <linux/buffer_head.h>
9 #include <linux/statfs.h>
10 #include <linux/parser.h>
11 #include <linux/seq_file.h>
14 #define CREATE_TRACE_POINTS
15 #include <trace/events/erofs.h>
17 static struct kmem_cache *erofs_inode_cachep __read_mostly;
19 void _erofs_err(struct super_block *sb, const char *function,
30 pr_err("(device %s): %s: %pV", sb->s_id, function, &vaf);
34 void _erofs_info(struct super_block *sb, const char *function,
45 pr_info("(device %s): %pV", sb->s_id, &vaf);
49 static void erofs_inode_init_once(void *ptr)
51 struct erofs_inode *vi = ptr;
53 inode_init_once(&vi->vfs_inode);
56 static struct inode *erofs_alloc_inode(struct super_block *sb)
58 struct erofs_inode *vi =
59 kmem_cache_alloc(erofs_inode_cachep, GFP_KERNEL);
64 /* zero out everything except vfs_inode */
65 memset(vi, 0, offsetof(struct erofs_inode, vfs_inode));
66 return &vi->vfs_inode;
69 static void erofs_free_inode(struct inode *inode)
71 struct erofs_inode *vi = EROFS_I(inode);
73 /* be careful of RCU symlink path */
74 if (inode->i_op == &erofs_fast_symlink_iops)
76 kfree(vi->xattr_shared_xattrs);
78 kmem_cache_free(erofs_inode_cachep, vi);
81 static bool check_layout_compatibility(struct super_block *sb,
82 struct erofs_super_block *dsb)
84 const unsigned int feature = le32_to_cpu(dsb->feature_incompat);
86 EROFS_SB(sb)->feature_incompat = feature;
88 /* check if current kernel meets all mandatory requirements */
89 if (feature & (~EROFS_ALL_FEATURE_INCOMPAT)) {
91 "unidentified incompatible feature %x, please upgrade kernel version",
92 feature & ~EROFS_ALL_FEATURE_INCOMPAT);
98 static int erofs_read_superblock(struct super_block *sb)
100 struct erofs_sb_info *sbi;
102 struct erofs_super_block *dsb;
103 unsigned int blkszbits;
107 page = read_mapping_page(sb->s_bdev->bd_inode->i_mapping, 0, NULL);
109 erofs_err(sb, "cannot read erofs superblock");
110 return PTR_ERR(page);
115 data = kmap_atomic(page);
116 dsb = (struct erofs_super_block *)(data + EROFS_SUPER_OFFSET);
119 if (le32_to_cpu(dsb->magic) != EROFS_SUPER_MAGIC_V1) {
120 erofs_err(sb, "cannot find valid erofs superblock");
124 blkszbits = dsb->blkszbits;
125 /* 9(512 bytes) + LOG_SECTORS_PER_BLOCK == LOG_BLOCK_SIZE */
126 if (blkszbits != LOG_BLOCK_SIZE) {
127 erofs_err(sb, "blksize %u isn't supported on this platform",
132 if (!check_layout_compatibility(sb, dsb))
135 sbi->blocks = le32_to_cpu(dsb->blocks);
136 sbi->meta_blkaddr = le32_to_cpu(dsb->meta_blkaddr);
137 #ifdef CONFIG_EROFS_FS_XATTR
138 sbi->xattr_blkaddr = le32_to_cpu(dsb->xattr_blkaddr);
140 sbi->islotbits = ilog2(sizeof(struct erofs_inode_compact));
141 sbi->root_nid = le16_to_cpu(dsb->root_nid);
142 sbi->inos = le64_to_cpu(dsb->inos);
144 sbi->build_time = le64_to_cpu(dsb->build_time);
145 sbi->build_time_nsec = le32_to_cpu(dsb->build_time_nsec);
147 memcpy(&sb->s_uuid, dsb->uuid, sizeof(dsb->uuid));
149 ret = strscpy(sbi->volume_name, dsb->volume_name,
150 sizeof(dsb->volume_name));
151 if (ret < 0) { /* -E2BIG */
152 erofs_err(sb, "bad volume name without NIL terminator");
163 #ifdef CONFIG_EROFS_FS_ZIP
164 static int erofs_build_cache_strategy(struct super_block *sb,
167 struct erofs_sb_info *sbi = EROFS_SB(sb);
168 const char *cs = match_strdup(args);
172 erofs_err(sb, "Not enough memory to store cache strategy");
176 if (!strcmp(cs, "disabled")) {
177 sbi->cache_strategy = EROFS_ZIP_CACHE_DISABLED;
178 } else if (!strcmp(cs, "readahead")) {
179 sbi->cache_strategy = EROFS_ZIP_CACHE_READAHEAD;
180 } else if (!strcmp(cs, "readaround")) {
181 sbi->cache_strategy = EROFS_ZIP_CACHE_READAROUND;
183 erofs_err(sb, "Unrecognized cache strategy \"%s\"", cs);
190 static int erofs_build_cache_strategy(struct super_block *sb,
193 erofs_info(sb, "EROFS compression is disabled, so cache strategy is ignored");
198 /* set up default EROFS parameters */
199 static void erofs_default_options(struct erofs_sb_info *sbi)
201 #ifdef CONFIG_EROFS_FS_ZIP
202 sbi->cache_strategy = EROFS_ZIP_CACHE_READAROUND;
203 sbi->max_sync_decompress_pages = 3;
205 #ifdef CONFIG_EROFS_FS_XATTR
206 set_opt(sbi, XATTR_USER);
208 #ifdef CONFIG_EROFS_FS_POSIX_ACL
209 set_opt(sbi, POSIX_ACL);
222 static match_table_t erofs_tokens = {
223 {Opt_user_xattr, "user_xattr"},
224 {Opt_nouser_xattr, "nouser_xattr"},
226 {Opt_noacl, "noacl"},
227 {Opt_cache_strategy, "cache_strategy=%s"},
231 static int erofs_parse_options(struct super_block *sb, char *options)
233 substring_t args[MAX_OPT_ARGS];
240 while ((p = strsep(&options, ","))) {
246 args[0].to = args[0].from = NULL;
247 token = match_token(p, erofs_tokens, args);
250 #ifdef CONFIG_EROFS_FS_XATTR
252 set_opt(EROFS_SB(sb), XATTR_USER);
254 case Opt_nouser_xattr:
255 clear_opt(EROFS_SB(sb), XATTR_USER);
259 erofs_info(sb, "user_xattr options not supported");
261 case Opt_nouser_xattr:
262 erofs_info(sb, "nouser_xattr options not supported");
265 #ifdef CONFIG_EROFS_FS_POSIX_ACL
267 set_opt(EROFS_SB(sb), POSIX_ACL);
270 clear_opt(EROFS_SB(sb), POSIX_ACL);
274 erofs_info(sb, "acl options not supported");
277 erofs_info(sb, "noacl options not supported");
280 case Opt_cache_strategy:
281 err = erofs_build_cache_strategy(sb, args);
286 erofs_err(sb, "Unrecognized mount option \"%s\" or missing value", p);
293 #ifdef CONFIG_EROFS_FS_ZIP
294 static const struct address_space_operations managed_cache_aops;
296 static int erofs_managed_cache_releasepage(struct page *page, gfp_t gfp_mask)
298 int ret = 1; /* 0 - busy */
299 struct address_space *const mapping = page->mapping;
301 DBG_BUGON(!PageLocked(page));
302 DBG_BUGON(mapping->a_ops != &managed_cache_aops);
304 if (PagePrivate(page))
305 ret = erofs_try_to_free_cached_page(mapping, page);
310 static void erofs_managed_cache_invalidatepage(struct page *page,
314 const unsigned int stop = length + offset;
316 DBG_BUGON(!PageLocked(page));
318 /* Check for potential overflow in debug mode */
319 DBG_BUGON(stop > PAGE_SIZE || stop < length);
321 if (offset == 0 && stop == PAGE_SIZE)
322 while (!erofs_managed_cache_releasepage(page, GFP_NOFS))
326 static const struct address_space_operations managed_cache_aops = {
327 .releasepage = erofs_managed_cache_releasepage,
328 .invalidatepage = erofs_managed_cache_invalidatepage,
331 static int erofs_init_managed_cache(struct super_block *sb)
333 struct erofs_sb_info *const sbi = EROFS_SB(sb);
334 struct inode *const inode = new_inode(sb);
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 | __GFP_MOVABLE);
345 sbi->managed_cache = inode;
349 static int erofs_init_managed_cache(struct super_block *sb) { return 0; }
352 static int erofs_fill_super(struct super_block *sb, void *data, int silent)
355 struct erofs_sb_info *sbi;
358 sb->s_magic = EROFS_SUPER_MAGIC;
360 if (!sb_set_blocksize(sb, EROFS_BLKSIZ)) {
361 erofs_err(sb, "failed to set erofs blksize");
365 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
370 err = erofs_read_superblock(sb);
374 sb->s_flags |= SB_RDONLY | SB_NOATIME;
375 sb->s_maxbytes = MAX_LFS_FILESIZE;
378 sb->s_op = &erofs_sops;
380 #ifdef CONFIG_EROFS_FS_XATTR
381 sb->s_xattr = erofs_xattr_handlers;
383 /* set erofs default mount options */
384 erofs_default_options(sbi);
386 err = erofs_parse_options(sb, data);
390 if (test_opt(sbi, POSIX_ACL))
391 sb->s_flags |= SB_POSIXACL;
393 sb->s_flags &= ~SB_POSIXACL;
395 #ifdef CONFIG_EROFS_FS_ZIP
396 INIT_RADIX_TREE(&sbi->workstn_tree, GFP_ATOMIC);
399 /* get the root inode */
400 inode = erofs_iget(sb, ROOT_NID(sbi), true);
402 return PTR_ERR(inode);
404 if (!S_ISDIR(inode->i_mode)) {
405 erofs_err(sb, "rootino(nid %llu) is not a directory(i_mode %o)",
406 ROOT_NID(sbi), inode->i_mode);
411 sb->s_root = d_make_root(inode);
415 erofs_shrinker_register(sb);
416 /* sb->s_umount is already locked, SB_ACTIVE and SB_BORN are not set */
417 err = erofs_init_managed_cache(sb);
421 erofs_info(sb, "mounted with opts: %s, root inode @ nid %llu.",
422 (char *)data, ROOT_NID(sbi));
426 static struct dentry *erofs_mount(struct file_system_type *fs_type, int flags,
427 const char *dev_name, void *data)
429 return mount_bdev(fs_type, flags, dev_name, data, erofs_fill_super);
433 * could be triggered after deactivate_locked_super()
434 * is called, thus including umount and failed to initialize.
436 static void erofs_kill_sb(struct super_block *sb)
438 struct erofs_sb_info *sbi;
440 WARN_ON(sb->s_magic != EROFS_SUPER_MAGIC);
442 kill_block_super(sb);
448 sb->s_fs_info = NULL;
451 /* called when ->s_root is non-NULL */
452 static void erofs_put_super(struct super_block *sb)
454 struct erofs_sb_info *const sbi = EROFS_SB(sb);
458 erofs_shrinker_unregister(sb);
459 #ifdef CONFIG_EROFS_FS_ZIP
460 iput(sbi->managed_cache);
461 sbi->managed_cache = NULL;
465 static struct file_system_type erofs_fs_type = {
466 .owner = THIS_MODULE,
468 .mount = erofs_mount,
469 .kill_sb = erofs_kill_sb,
470 .fs_flags = FS_REQUIRES_DEV,
472 MODULE_ALIAS_FS("erofs");
474 static int __init erofs_module_init(void)
478 erofs_check_ondisk_layout_definitions();
480 erofs_inode_cachep = kmem_cache_create("erofs_inode",
481 sizeof(struct erofs_inode), 0,
482 SLAB_RECLAIM_ACCOUNT,
483 erofs_inode_init_once);
484 if (!erofs_inode_cachep) {
489 err = erofs_init_shrinker();
493 err = z_erofs_init_zip_subsystem();
497 err = register_filesystem(&erofs_fs_type);
504 z_erofs_exit_zip_subsystem();
506 erofs_exit_shrinker();
508 kmem_cache_destroy(erofs_inode_cachep);
513 static void __exit erofs_module_exit(void)
515 unregister_filesystem(&erofs_fs_type);
516 z_erofs_exit_zip_subsystem();
517 erofs_exit_shrinker();
519 /* Ensure all RCU free inodes are safe before cache is destroyed. */
521 kmem_cache_destroy(erofs_inode_cachep);
524 /* get filesystem statistics */
525 static int erofs_statfs(struct dentry *dentry, struct kstatfs *buf)
527 struct super_block *sb = dentry->d_sb;
528 struct erofs_sb_info *sbi = EROFS_SB(sb);
529 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
531 buf->f_type = sb->s_magic;
532 buf->f_bsize = EROFS_BLKSIZ;
533 buf->f_blocks = sbi->blocks;
534 buf->f_bfree = buf->f_bavail = 0;
536 buf->f_files = ULLONG_MAX;
537 buf->f_ffree = ULLONG_MAX - sbi->inos;
539 buf->f_namelen = EROFS_NAME_LEN;
541 buf->f_fsid.val[0] = (u32)id;
542 buf->f_fsid.val[1] = (u32)(id >> 32);
546 static int erofs_show_options(struct seq_file *seq, struct dentry *root)
548 struct erofs_sb_info *sbi __maybe_unused = EROFS_SB(root->d_sb);
550 #ifdef CONFIG_EROFS_FS_XATTR
551 if (test_opt(sbi, XATTR_USER))
552 seq_puts(seq, ",user_xattr");
554 seq_puts(seq, ",nouser_xattr");
556 #ifdef CONFIG_EROFS_FS_POSIX_ACL
557 if (test_opt(sbi, POSIX_ACL))
558 seq_puts(seq, ",acl");
560 seq_puts(seq, ",noacl");
562 #ifdef CONFIG_EROFS_FS_ZIP
563 if (sbi->cache_strategy == EROFS_ZIP_CACHE_DISABLED) {
564 seq_puts(seq, ",cache_strategy=disabled");
565 } else if (sbi->cache_strategy == EROFS_ZIP_CACHE_READAHEAD) {
566 seq_puts(seq, ",cache_strategy=readahead");
567 } else if (sbi->cache_strategy == EROFS_ZIP_CACHE_READAROUND) {
568 seq_puts(seq, ",cache_strategy=readaround");
570 seq_puts(seq, ",cache_strategy=(unknown)");
577 static int erofs_remount(struct super_block *sb, int *flags, char *data)
579 struct erofs_sb_info *sbi = EROFS_SB(sb);
580 unsigned int org_mnt_opt = sbi->mount_opt;
583 DBG_BUGON(!sb_rdonly(sb));
584 err = erofs_parse_options(sb, data);
588 if (test_opt(sbi, POSIX_ACL))
589 sb->s_flags |= SB_POSIXACL;
591 sb->s_flags &= ~SB_POSIXACL;
596 sbi->mount_opt = org_mnt_opt;
600 const struct super_operations erofs_sops = {
601 .put_super = erofs_put_super,
602 .alloc_inode = erofs_alloc_inode,
603 .free_inode = erofs_free_inode,
604 .statfs = erofs_statfs,
605 .show_options = erofs_show_options,
606 .remount_fs = erofs_remount,
609 module_init(erofs_module_init);
610 module_exit(erofs_module_exit);
612 MODULE_DESCRIPTION("Enhanced ROM File System");
613 MODULE_AUTHOR("Gao Xiang, Chao Yu, Miao Xie, CONSUMER BG, HUAWEI Inc.");
614 MODULE_LICENSE("GPL");