xref: /openbmc/linux/fs/erofs/super.c (revision 25b892b5)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2017-2018 HUAWEI, Inc.
4  *             https://www.huawei.com/
5  */
6 #include <linux/module.h>
7 #include <linux/buffer_head.h>
8 #include <linux/statfs.h>
9 #include <linux/parser.h>
10 #include <linux/seq_file.h>
11 #include <linux/crc32c.h>
12 #include <linux/fs_context.h>
13 #include <linux/fs_parser.h>
14 #include <linux/dax.h>
15 #include "xattr.h"
16 
17 #define CREATE_TRACE_POINTS
18 #include <trace/events/erofs.h>
19 
20 static struct kmem_cache *erofs_inode_cachep __read_mostly;
21 
22 void _erofs_err(struct super_block *sb, const char *function,
23 		const char *fmt, ...)
24 {
25 	struct va_format vaf;
26 	va_list args;
27 
28 	va_start(args, fmt);
29 
30 	vaf.fmt = fmt;
31 	vaf.va = &args;
32 
33 	pr_err("(device %s): %s: %pV", sb->s_id, function, &vaf);
34 	va_end(args);
35 }
36 
37 void _erofs_info(struct super_block *sb, const char *function,
38 		 const char *fmt, ...)
39 {
40 	struct va_format vaf;
41 	va_list args;
42 
43 	va_start(args, fmt);
44 
45 	vaf.fmt = fmt;
46 	vaf.va = &args;
47 
48 	pr_info("(device %s): %pV", sb->s_id, &vaf);
49 	va_end(args);
50 }
51 
52 static int erofs_superblock_csum_verify(struct super_block *sb, void *sbdata)
53 {
54 	struct erofs_super_block *dsb;
55 	u32 expected_crc, crc;
56 
57 	dsb = kmemdup(sbdata + EROFS_SUPER_OFFSET,
58 		      EROFS_BLKSIZ - EROFS_SUPER_OFFSET, GFP_KERNEL);
59 	if (!dsb)
60 		return -ENOMEM;
61 
62 	expected_crc = le32_to_cpu(dsb->checksum);
63 	dsb->checksum = 0;
64 	/* to allow for x86 boot sectors and other oddities. */
65 	crc = crc32c(~0, dsb, EROFS_BLKSIZ - EROFS_SUPER_OFFSET);
66 	kfree(dsb);
67 
68 	if (crc != expected_crc) {
69 		erofs_err(sb, "invalid checksum 0x%08x, 0x%08x expected",
70 			  crc, expected_crc);
71 		return -EBADMSG;
72 	}
73 	return 0;
74 }
75 
76 static void erofs_inode_init_once(void *ptr)
77 {
78 	struct erofs_inode *vi = ptr;
79 
80 	inode_init_once(&vi->vfs_inode);
81 }
82 
83 static struct inode *erofs_alloc_inode(struct super_block *sb)
84 {
85 	struct erofs_inode *vi =
86 		kmem_cache_alloc(erofs_inode_cachep, GFP_KERNEL);
87 
88 	if (!vi)
89 		return NULL;
90 
91 	/* zero out everything except vfs_inode */
92 	memset(vi, 0, offsetof(struct erofs_inode, vfs_inode));
93 	return &vi->vfs_inode;
94 }
95 
96 static void erofs_free_inode(struct inode *inode)
97 {
98 	struct erofs_inode *vi = EROFS_I(inode);
99 
100 	/* be careful of RCU symlink path */
101 	if (inode->i_op == &erofs_fast_symlink_iops)
102 		kfree(inode->i_link);
103 	kfree(vi->xattr_shared_xattrs);
104 
105 	kmem_cache_free(erofs_inode_cachep, vi);
106 }
107 
108 static bool check_layout_compatibility(struct super_block *sb,
109 				       struct erofs_super_block *dsb)
110 {
111 	const unsigned int feature = le32_to_cpu(dsb->feature_incompat);
112 
113 	EROFS_SB(sb)->feature_incompat = feature;
114 
115 	/* check if current kernel meets all mandatory requirements */
116 	if (feature & (~EROFS_ALL_FEATURE_INCOMPAT)) {
117 		erofs_err(sb,
118 			  "unidentified incompatible feature %x, please upgrade kernel version",
119 			   feature & ~EROFS_ALL_FEATURE_INCOMPAT);
120 		return false;
121 	}
122 	return true;
123 }
124 
125 #ifdef CONFIG_EROFS_FS_ZIP
126 /* read variable-sized metadata, offset will be aligned by 4-byte */
127 static void *erofs_read_metadata(struct super_block *sb, struct page **pagep,
128 				 erofs_off_t *offset, int *lengthp)
129 {
130 	struct page *page = *pagep;
131 	u8 *buffer, *ptr;
132 	int len, i, cnt;
133 	erofs_blk_t blk;
134 
135 	*offset = round_up(*offset, 4);
136 	blk = erofs_blknr(*offset);
137 
138 	if (!page || page->index != blk) {
139 		if (page) {
140 			unlock_page(page);
141 			put_page(page);
142 		}
143 		page = erofs_get_meta_page(sb, blk);
144 		if (IS_ERR(page))
145 			goto err_nullpage;
146 	}
147 
148 	ptr = kmap(page);
149 	len = le16_to_cpu(*(__le16 *)&ptr[erofs_blkoff(*offset)]);
150 	if (!len)
151 		len = U16_MAX + 1;
152 	buffer = kmalloc(len, GFP_KERNEL);
153 	if (!buffer) {
154 		buffer = ERR_PTR(-ENOMEM);
155 		goto out;
156 	}
157 	*offset += sizeof(__le16);
158 	*lengthp = len;
159 
160 	for (i = 0; i < len; i += cnt) {
161 		cnt = min(EROFS_BLKSIZ - (int)erofs_blkoff(*offset), len - i);
162 		blk = erofs_blknr(*offset);
163 
164 		if (!page || page->index != blk) {
165 			if (page) {
166 				kunmap(page);
167 				unlock_page(page);
168 				put_page(page);
169 			}
170 			page = erofs_get_meta_page(sb, blk);
171 			if (IS_ERR(page)) {
172 				kfree(buffer);
173 				goto err_nullpage;
174 			}
175 			ptr = kmap(page);
176 		}
177 		memcpy(buffer + i, ptr + erofs_blkoff(*offset), cnt);
178 		*offset += cnt;
179 	}
180 out:
181 	kunmap(page);
182 	*pagep = page;
183 	return buffer;
184 err_nullpage:
185 	*pagep = NULL;
186 	return page;
187 }
188 
189 static int erofs_load_compr_cfgs(struct super_block *sb,
190 				 struct erofs_super_block *dsb)
191 {
192 	struct erofs_sb_info *sbi;
193 	struct page *page;
194 	unsigned int algs, alg;
195 	erofs_off_t offset;
196 	int size, ret;
197 
198 	sbi = EROFS_SB(sb);
199 	sbi->available_compr_algs = le16_to_cpu(dsb->u1.available_compr_algs);
200 
201 	if (sbi->available_compr_algs & ~Z_EROFS_ALL_COMPR_ALGS) {
202 		erofs_err(sb, "try to load compressed fs with unsupported algorithms %x",
203 			  sbi->available_compr_algs & ~Z_EROFS_ALL_COMPR_ALGS);
204 		return -EINVAL;
205 	}
206 
207 	offset = EROFS_SUPER_OFFSET + sbi->sb_size;
208 	page = NULL;
209 	alg = 0;
210 	ret = 0;
211 
212 	for (algs = sbi->available_compr_algs; algs; algs >>= 1, ++alg) {
213 		void *data;
214 
215 		if (!(algs & 1))
216 			continue;
217 
218 		data = erofs_read_metadata(sb, &page, &offset, &size);
219 		if (IS_ERR(data)) {
220 			ret = PTR_ERR(data);
221 			goto err;
222 		}
223 
224 		switch (alg) {
225 		case Z_EROFS_COMPRESSION_LZ4:
226 			ret = z_erofs_load_lz4_config(sb, dsb, data, size);
227 			break;
228 		default:
229 			DBG_BUGON(1);
230 			ret = -EFAULT;
231 		}
232 		kfree(data);
233 		if (ret)
234 			goto err;
235 	}
236 err:
237 	if (page) {
238 		unlock_page(page);
239 		put_page(page);
240 	}
241 	return ret;
242 }
243 #else
244 static int erofs_load_compr_cfgs(struct super_block *sb,
245 				 struct erofs_super_block *dsb)
246 {
247 	if (dsb->u1.available_compr_algs) {
248 		erofs_err(sb, "try to load compressed fs when compression is disabled");
249 		return -EINVAL;
250 	}
251 	return 0;
252 }
253 #endif
254 
255 static int erofs_read_superblock(struct super_block *sb)
256 {
257 	struct erofs_sb_info *sbi;
258 	struct page *page;
259 	struct erofs_super_block *dsb;
260 	unsigned int blkszbits;
261 	void *data;
262 	int ret;
263 
264 	page = read_mapping_page(sb->s_bdev->bd_inode->i_mapping, 0, NULL);
265 	if (IS_ERR(page)) {
266 		erofs_err(sb, "cannot read erofs superblock");
267 		return PTR_ERR(page);
268 	}
269 
270 	sbi = EROFS_SB(sb);
271 
272 	data = kmap(page);
273 	dsb = (struct erofs_super_block *)(data + EROFS_SUPER_OFFSET);
274 
275 	ret = -EINVAL;
276 	if (le32_to_cpu(dsb->magic) != EROFS_SUPER_MAGIC_V1) {
277 		erofs_err(sb, "cannot find valid erofs superblock");
278 		goto out;
279 	}
280 
281 	sbi->feature_compat = le32_to_cpu(dsb->feature_compat);
282 	if (erofs_sb_has_sb_chksum(sbi)) {
283 		ret = erofs_superblock_csum_verify(sb, data);
284 		if (ret)
285 			goto out;
286 	}
287 
288 	ret = -EINVAL;
289 	blkszbits = dsb->blkszbits;
290 	/* 9(512 bytes) + LOG_SECTORS_PER_BLOCK == LOG_BLOCK_SIZE */
291 	if (blkszbits != LOG_BLOCK_SIZE) {
292 		erofs_err(sb, "blkszbits %u isn't supported on this platform",
293 			  blkszbits);
294 		goto out;
295 	}
296 
297 	if (!check_layout_compatibility(sb, dsb))
298 		goto out;
299 
300 	sbi->sb_size = 128 + dsb->sb_extslots * EROFS_SB_EXTSLOT_SIZE;
301 	if (sbi->sb_size > EROFS_BLKSIZ) {
302 		erofs_err(sb, "invalid sb_extslots %u (more than a fs block)",
303 			  sbi->sb_size);
304 		goto out;
305 	}
306 	sbi->blocks = le32_to_cpu(dsb->blocks);
307 	sbi->meta_blkaddr = le32_to_cpu(dsb->meta_blkaddr);
308 #ifdef CONFIG_EROFS_FS_XATTR
309 	sbi->xattr_blkaddr = le32_to_cpu(dsb->xattr_blkaddr);
310 #endif
311 	sbi->islotbits = ilog2(sizeof(struct erofs_inode_compact));
312 	sbi->root_nid = le16_to_cpu(dsb->root_nid);
313 	sbi->inos = le64_to_cpu(dsb->inos);
314 
315 	sbi->build_time = le64_to_cpu(dsb->build_time);
316 	sbi->build_time_nsec = le32_to_cpu(dsb->build_time_nsec);
317 
318 	memcpy(&sb->s_uuid, dsb->uuid, sizeof(dsb->uuid));
319 
320 	ret = strscpy(sbi->volume_name, dsb->volume_name,
321 		      sizeof(dsb->volume_name));
322 	if (ret < 0) {	/* -E2BIG */
323 		erofs_err(sb, "bad volume name without NIL terminator");
324 		ret = -EFSCORRUPTED;
325 		goto out;
326 	}
327 
328 	/* parse on-disk compression configurations */
329 	if (erofs_sb_has_compr_cfgs(sbi))
330 		ret = erofs_load_compr_cfgs(sb, dsb);
331 	else
332 		ret = z_erofs_load_lz4_config(sb, dsb, NULL, 0);
333 out:
334 	kunmap(page);
335 	put_page(page);
336 	return ret;
337 }
338 
339 /* set up default EROFS parameters */
340 static void erofs_default_options(struct erofs_fs_context *ctx)
341 {
342 #ifdef CONFIG_EROFS_FS_ZIP
343 	ctx->cache_strategy = EROFS_ZIP_CACHE_READAROUND;
344 	ctx->max_sync_decompress_pages = 3;
345 	ctx->readahead_sync_decompress = false;
346 #endif
347 #ifdef CONFIG_EROFS_FS_XATTR
348 	set_opt(ctx, XATTR_USER);
349 #endif
350 #ifdef CONFIG_EROFS_FS_POSIX_ACL
351 	set_opt(ctx, POSIX_ACL);
352 #endif
353 }
354 
355 enum {
356 	Opt_user_xattr,
357 	Opt_acl,
358 	Opt_cache_strategy,
359 	Opt_dax,
360 	Opt_dax_enum,
361 	Opt_err
362 };
363 
364 static const struct constant_table erofs_param_cache_strategy[] = {
365 	{"disabled",	EROFS_ZIP_CACHE_DISABLED},
366 	{"readahead",	EROFS_ZIP_CACHE_READAHEAD},
367 	{"readaround",	EROFS_ZIP_CACHE_READAROUND},
368 	{}
369 };
370 
371 static const struct constant_table erofs_dax_param_enums[] = {
372 	{"always",	EROFS_MOUNT_DAX_ALWAYS},
373 	{"never",	EROFS_MOUNT_DAX_NEVER},
374 	{}
375 };
376 
377 static const struct fs_parameter_spec erofs_fs_parameters[] = {
378 	fsparam_flag_no("user_xattr",	Opt_user_xattr),
379 	fsparam_flag_no("acl",		Opt_acl),
380 	fsparam_enum("cache_strategy",	Opt_cache_strategy,
381 		     erofs_param_cache_strategy),
382 	fsparam_flag("dax",             Opt_dax),
383 	fsparam_enum("dax",		Opt_dax_enum, erofs_dax_param_enums),
384 	{}
385 };
386 
387 static bool erofs_fc_set_dax_mode(struct fs_context *fc, unsigned int mode)
388 {
389 #ifdef CONFIG_FS_DAX
390 	struct erofs_fs_context *ctx = fc->fs_private;
391 
392 	switch (mode) {
393 	case EROFS_MOUNT_DAX_ALWAYS:
394 		warnfc(fc, "DAX enabled. Warning: EXPERIMENTAL, use at your own risk");
395 		set_opt(ctx, DAX_ALWAYS);
396 		clear_opt(ctx, DAX_NEVER);
397 		return true;
398 	case EROFS_MOUNT_DAX_NEVER:
399 		set_opt(ctx, DAX_NEVER);
400 		clear_opt(ctx, DAX_ALWAYS);
401 		return true;
402 	default:
403 		DBG_BUGON(1);
404 		return false;
405 	}
406 #else
407 	errorfc(fc, "dax options not supported");
408 	return false;
409 #endif
410 }
411 
412 static int erofs_fc_parse_param(struct fs_context *fc,
413 				struct fs_parameter *param)
414 {
415 	struct erofs_fs_context *ctx __maybe_unused = fc->fs_private;
416 	struct fs_parse_result result;
417 	int opt;
418 
419 	opt = fs_parse(fc, erofs_fs_parameters, param, &result);
420 	if (opt < 0)
421 		return opt;
422 
423 	switch (opt) {
424 	case Opt_user_xattr:
425 #ifdef CONFIG_EROFS_FS_XATTR
426 		if (result.boolean)
427 			set_opt(ctx, XATTR_USER);
428 		else
429 			clear_opt(ctx, XATTR_USER);
430 #else
431 		errorfc(fc, "{,no}user_xattr options not supported");
432 #endif
433 		break;
434 	case Opt_acl:
435 #ifdef CONFIG_EROFS_FS_POSIX_ACL
436 		if (result.boolean)
437 			set_opt(ctx, POSIX_ACL);
438 		else
439 			clear_opt(ctx, POSIX_ACL);
440 #else
441 		errorfc(fc, "{,no}acl options not supported");
442 #endif
443 		break;
444 	case Opt_cache_strategy:
445 #ifdef CONFIG_EROFS_FS_ZIP
446 		ctx->cache_strategy = result.uint_32;
447 #else
448 		errorfc(fc, "compression not supported, cache_strategy ignored");
449 #endif
450 		break;
451 	case Opt_dax:
452 		if (!erofs_fc_set_dax_mode(fc, EROFS_MOUNT_DAX_ALWAYS))
453 			return -EINVAL;
454 		break;
455 	case Opt_dax_enum:
456 		if (!erofs_fc_set_dax_mode(fc, result.uint_32))
457 			return -EINVAL;
458 		break;
459 	default:
460 		return -ENOPARAM;
461 	}
462 	return 0;
463 }
464 
465 #ifdef CONFIG_EROFS_FS_ZIP
466 static const struct address_space_operations managed_cache_aops;
467 
468 static int erofs_managed_cache_releasepage(struct page *page, gfp_t gfp_mask)
469 {
470 	int ret = 1;	/* 0 - busy */
471 	struct address_space *const mapping = page->mapping;
472 
473 	DBG_BUGON(!PageLocked(page));
474 	DBG_BUGON(mapping->a_ops != &managed_cache_aops);
475 
476 	if (PagePrivate(page))
477 		ret = erofs_try_to_free_cached_page(page);
478 
479 	return ret;
480 }
481 
482 static void erofs_managed_cache_invalidatepage(struct page *page,
483 					       unsigned int offset,
484 					       unsigned int length)
485 {
486 	const unsigned int stop = length + offset;
487 
488 	DBG_BUGON(!PageLocked(page));
489 
490 	/* Check for potential overflow in debug mode */
491 	DBG_BUGON(stop > PAGE_SIZE || stop < length);
492 
493 	if (offset == 0 && stop == PAGE_SIZE)
494 		while (!erofs_managed_cache_releasepage(page, GFP_NOFS))
495 			cond_resched();
496 }
497 
498 static const struct address_space_operations managed_cache_aops = {
499 	.releasepage = erofs_managed_cache_releasepage,
500 	.invalidatepage = erofs_managed_cache_invalidatepage,
501 };
502 
503 static int erofs_init_managed_cache(struct super_block *sb)
504 {
505 	struct erofs_sb_info *const sbi = EROFS_SB(sb);
506 	struct inode *const inode = new_inode(sb);
507 
508 	if (!inode)
509 		return -ENOMEM;
510 
511 	set_nlink(inode, 1);
512 	inode->i_size = OFFSET_MAX;
513 
514 	inode->i_mapping->a_ops = &managed_cache_aops;
515 	mapping_set_gfp_mask(inode->i_mapping,
516 			     GFP_NOFS | __GFP_HIGHMEM | __GFP_MOVABLE);
517 	sbi->managed_cache = inode;
518 	return 0;
519 }
520 #else
521 static int erofs_init_managed_cache(struct super_block *sb) { return 0; }
522 #endif
523 
524 static int erofs_fc_fill_super(struct super_block *sb, struct fs_context *fc)
525 {
526 	struct inode *inode;
527 	struct erofs_sb_info *sbi;
528 	struct erofs_fs_context *ctx = fc->fs_private;
529 	int err;
530 
531 	sb->s_magic = EROFS_SUPER_MAGIC;
532 
533 	if (!sb_set_blocksize(sb, EROFS_BLKSIZ)) {
534 		erofs_err(sb, "failed to set erofs blksize");
535 		return -EINVAL;
536 	}
537 
538 	sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
539 	if (!sbi)
540 		return -ENOMEM;
541 
542 	sb->s_fs_info = sbi;
543 	sbi->dax_dev = fs_dax_get_by_bdev(sb->s_bdev);
544 	err = erofs_read_superblock(sb);
545 	if (err)
546 		return err;
547 
548 	if (test_opt(ctx, DAX_ALWAYS) &&
549 	    !dax_supported(sbi->dax_dev, sb->s_bdev, EROFS_BLKSIZ, 0, bdev_nr_sectors(sb->s_bdev))) {
550 		errorfc(fc, "DAX unsupported by block device. Turning off DAX.");
551 		clear_opt(ctx, DAX_ALWAYS);
552 	}
553 	sb->s_flags |= SB_RDONLY | SB_NOATIME;
554 	sb->s_maxbytes = MAX_LFS_FILESIZE;
555 	sb->s_time_gran = 1;
556 
557 	sb->s_op = &erofs_sops;
558 	sb->s_xattr = erofs_xattr_handlers;
559 
560 	if (test_opt(ctx, POSIX_ACL))
561 		sb->s_flags |= SB_POSIXACL;
562 	else
563 		sb->s_flags &= ~SB_POSIXACL;
564 
565 	sbi->ctx = *ctx;
566 
567 #ifdef CONFIG_EROFS_FS_ZIP
568 	xa_init(&sbi->managed_pslots);
569 #endif
570 
571 	/* get the root inode */
572 	inode = erofs_iget(sb, ROOT_NID(sbi), true);
573 	if (IS_ERR(inode))
574 		return PTR_ERR(inode);
575 
576 	if (!S_ISDIR(inode->i_mode)) {
577 		erofs_err(sb, "rootino(nid %llu) is not a directory(i_mode %o)",
578 			  ROOT_NID(sbi), inode->i_mode);
579 		iput(inode);
580 		return -EINVAL;
581 	}
582 
583 	sb->s_root = d_make_root(inode);
584 	if (!sb->s_root)
585 		return -ENOMEM;
586 
587 	erofs_shrinker_register(sb);
588 	/* sb->s_umount is already locked, SB_ACTIVE and SB_BORN are not set */
589 	err = erofs_init_managed_cache(sb);
590 	if (err)
591 		return err;
592 
593 	erofs_info(sb, "mounted with root inode @ nid %llu.", ROOT_NID(sbi));
594 	return 0;
595 }
596 
597 static int erofs_fc_get_tree(struct fs_context *fc)
598 {
599 	return get_tree_bdev(fc, erofs_fc_fill_super);
600 }
601 
602 static int erofs_fc_reconfigure(struct fs_context *fc)
603 {
604 	struct super_block *sb = fc->root->d_sb;
605 	struct erofs_sb_info *sbi = EROFS_SB(sb);
606 	struct erofs_fs_context *ctx = fc->fs_private;
607 
608 	DBG_BUGON(!sb_rdonly(sb));
609 
610 	if (test_opt(ctx, POSIX_ACL))
611 		fc->sb_flags |= SB_POSIXACL;
612 	else
613 		fc->sb_flags &= ~SB_POSIXACL;
614 
615 	sbi->ctx = *ctx;
616 
617 	fc->sb_flags |= SB_RDONLY;
618 	return 0;
619 }
620 
621 static void erofs_fc_free(struct fs_context *fc)
622 {
623 	kfree(fc->fs_private);
624 }
625 
626 static const struct fs_context_operations erofs_context_ops = {
627 	.parse_param	= erofs_fc_parse_param,
628 	.get_tree       = erofs_fc_get_tree,
629 	.reconfigure    = erofs_fc_reconfigure,
630 	.free		= erofs_fc_free,
631 };
632 
633 static int erofs_init_fs_context(struct fs_context *fc)
634 {
635 	fc->fs_private = kzalloc(sizeof(struct erofs_fs_context), GFP_KERNEL);
636 	if (!fc->fs_private)
637 		return -ENOMEM;
638 
639 	/* set default mount options */
640 	erofs_default_options(fc->fs_private);
641 
642 	fc->ops = &erofs_context_ops;
643 
644 	return 0;
645 }
646 
647 /*
648  * could be triggered after deactivate_locked_super()
649  * is called, thus including umount and failed to initialize.
650  */
651 static void erofs_kill_sb(struct super_block *sb)
652 {
653 	struct erofs_sb_info *sbi;
654 
655 	WARN_ON(sb->s_magic != EROFS_SUPER_MAGIC);
656 
657 	kill_block_super(sb);
658 
659 	sbi = EROFS_SB(sb);
660 	if (!sbi)
661 		return;
662 	fs_put_dax(sbi->dax_dev);
663 	kfree(sbi);
664 	sb->s_fs_info = NULL;
665 }
666 
667 /* called when ->s_root is non-NULL */
668 static void erofs_put_super(struct super_block *sb)
669 {
670 	struct erofs_sb_info *const sbi = EROFS_SB(sb);
671 
672 	DBG_BUGON(!sbi);
673 
674 	erofs_shrinker_unregister(sb);
675 #ifdef CONFIG_EROFS_FS_ZIP
676 	iput(sbi->managed_cache);
677 	sbi->managed_cache = NULL;
678 #endif
679 }
680 
681 static struct file_system_type erofs_fs_type = {
682 	.owner          = THIS_MODULE,
683 	.name           = "erofs",
684 	.init_fs_context = erofs_init_fs_context,
685 	.kill_sb        = erofs_kill_sb,
686 	.fs_flags       = FS_REQUIRES_DEV,
687 };
688 MODULE_ALIAS_FS("erofs");
689 
690 static int __init erofs_module_init(void)
691 {
692 	int err;
693 
694 	erofs_check_ondisk_layout_definitions();
695 
696 	erofs_inode_cachep = kmem_cache_create("erofs_inode",
697 					       sizeof(struct erofs_inode), 0,
698 					       SLAB_RECLAIM_ACCOUNT,
699 					       erofs_inode_init_once);
700 	if (!erofs_inode_cachep) {
701 		err = -ENOMEM;
702 		goto icache_err;
703 	}
704 
705 	err = erofs_init_shrinker();
706 	if (err)
707 		goto shrinker_err;
708 
709 	erofs_pcpubuf_init();
710 	err = z_erofs_init_zip_subsystem();
711 	if (err)
712 		goto zip_err;
713 
714 	err = register_filesystem(&erofs_fs_type);
715 	if (err)
716 		goto fs_err;
717 
718 	return 0;
719 
720 fs_err:
721 	z_erofs_exit_zip_subsystem();
722 zip_err:
723 	erofs_exit_shrinker();
724 shrinker_err:
725 	kmem_cache_destroy(erofs_inode_cachep);
726 icache_err:
727 	return err;
728 }
729 
730 static void __exit erofs_module_exit(void)
731 {
732 	unregister_filesystem(&erofs_fs_type);
733 	z_erofs_exit_zip_subsystem();
734 	erofs_exit_shrinker();
735 
736 	/* Ensure all RCU free inodes are safe before cache is destroyed. */
737 	rcu_barrier();
738 	kmem_cache_destroy(erofs_inode_cachep);
739 	erofs_pcpubuf_exit();
740 }
741 
742 /* get filesystem statistics */
743 static int erofs_statfs(struct dentry *dentry, struct kstatfs *buf)
744 {
745 	struct super_block *sb = dentry->d_sb;
746 	struct erofs_sb_info *sbi = EROFS_SB(sb);
747 	u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
748 
749 	buf->f_type = sb->s_magic;
750 	buf->f_bsize = EROFS_BLKSIZ;
751 	buf->f_blocks = sbi->blocks;
752 	buf->f_bfree = buf->f_bavail = 0;
753 
754 	buf->f_files = ULLONG_MAX;
755 	buf->f_ffree = ULLONG_MAX - sbi->inos;
756 
757 	buf->f_namelen = EROFS_NAME_LEN;
758 
759 	buf->f_fsid    = u64_to_fsid(id);
760 	return 0;
761 }
762 
763 static int erofs_show_options(struct seq_file *seq, struct dentry *root)
764 {
765 	struct erofs_sb_info *sbi = EROFS_SB(root->d_sb);
766 	struct erofs_fs_context *ctx = &sbi->ctx;
767 
768 #ifdef CONFIG_EROFS_FS_XATTR
769 	if (test_opt(ctx, XATTR_USER))
770 		seq_puts(seq, ",user_xattr");
771 	else
772 		seq_puts(seq, ",nouser_xattr");
773 #endif
774 #ifdef CONFIG_EROFS_FS_POSIX_ACL
775 	if (test_opt(ctx, POSIX_ACL))
776 		seq_puts(seq, ",acl");
777 	else
778 		seq_puts(seq, ",noacl");
779 #endif
780 #ifdef CONFIG_EROFS_FS_ZIP
781 	if (ctx->cache_strategy == EROFS_ZIP_CACHE_DISABLED)
782 		seq_puts(seq, ",cache_strategy=disabled");
783 	else if (ctx->cache_strategy == EROFS_ZIP_CACHE_READAHEAD)
784 		seq_puts(seq, ",cache_strategy=readahead");
785 	else if (ctx->cache_strategy == EROFS_ZIP_CACHE_READAROUND)
786 		seq_puts(seq, ",cache_strategy=readaround");
787 #endif
788 	if (test_opt(ctx, DAX_ALWAYS))
789 		seq_puts(seq, ",dax=always");
790 	if (test_opt(ctx, DAX_NEVER))
791 		seq_puts(seq, ",dax=never");
792 	return 0;
793 }
794 
795 const struct super_operations erofs_sops = {
796 	.put_super = erofs_put_super,
797 	.alloc_inode = erofs_alloc_inode,
798 	.free_inode = erofs_free_inode,
799 	.statfs = erofs_statfs,
800 	.show_options = erofs_show_options,
801 };
802 
803 module_init(erofs_module_init);
804 module_exit(erofs_module_exit);
805 
806 MODULE_DESCRIPTION("Enhanced ROM File System");
807 MODULE_AUTHOR("Gao Xiang, Chao Yu, Miao Xie, CONSUMER BG, HUAWEI Inc.");
808 MODULE_LICENSE("GPL");
809