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