xref: /openbmc/linux/fs/erofs/super.c (revision 12c2987e)
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/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 <linux/exportfs.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 	size_t len = 1 << EROFS_SB(sb)->blkszbits;
56 	struct erofs_super_block *dsb;
57 	u32 expected_crc, crc;
58 
59 	if (len > EROFS_SUPER_OFFSET)
60 		len -= EROFS_SUPER_OFFSET;
61 
62 	dsb = kmemdup(sbdata + EROFS_SUPER_OFFSET, len, GFP_KERNEL);
63 	if (!dsb)
64 		return -ENOMEM;
65 
66 	expected_crc = le32_to_cpu(dsb->checksum);
67 	dsb->checksum = 0;
68 	/* to allow for x86 boot sectors and other oddities. */
69 	crc = crc32c(~0, dsb, len);
70 	kfree(dsb);
71 
72 	if (crc != expected_crc) {
73 		erofs_err(sb, "invalid checksum 0x%08x, 0x%08x expected",
74 			  crc, expected_crc);
75 		return -EBADMSG;
76 	}
77 	return 0;
78 }
79 
80 static void erofs_inode_init_once(void *ptr)
81 {
82 	struct erofs_inode *vi = ptr;
83 
84 	inode_init_once(&vi->vfs_inode);
85 }
86 
87 static struct inode *erofs_alloc_inode(struct super_block *sb)
88 {
89 	struct erofs_inode *vi =
90 		alloc_inode_sb(sb, erofs_inode_cachep, GFP_KERNEL);
91 
92 	if (!vi)
93 		return NULL;
94 
95 	/* zero out everything except vfs_inode */
96 	memset(vi, 0, offsetof(struct erofs_inode, vfs_inode));
97 	return &vi->vfs_inode;
98 }
99 
100 static void erofs_free_inode(struct inode *inode)
101 {
102 	struct erofs_inode *vi = EROFS_I(inode);
103 
104 	/* be careful of RCU symlink path */
105 	if (inode->i_op == &erofs_fast_symlink_iops)
106 		kfree(inode->i_link);
107 	kfree(vi->xattr_shared_xattrs);
108 
109 	kmem_cache_free(erofs_inode_cachep, vi);
110 }
111 
112 static bool check_layout_compatibility(struct super_block *sb,
113 				       struct erofs_super_block *dsb)
114 {
115 	const unsigned int feature = le32_to_cpu(dsb->feature_incompat);
116 
117 	EROFS_SB(sb)->feature_incompat = feature;
118 
119 	/* check if current kernel meets all mandatory requirements */
120 	if (feature & (~EROFS_ALL_FEATURE_INCOMPAT)) {
121 		erofs_err(sb,
122 			  "unidentified incompatible feature %x, please upgrade kernel version",
123 			   feature & ~EROFS_ALL_FEATURE_INCOMPAT);
124 		return false;
125 	}
126 	return true;
127 }
128 
129 #ifdef CONFIG_EROFS_FS_ZIP
130 /* read variable-sized metadata, offset will be aligned by 4-byte */
131 static void *erofs_read_metadata(struct super_block *sb, struct erofs_buf *buf,
132 				 erofs_off_t *offset, int *lengthp)
133 {
134 	u8 *buffer, *ptr;
135 	int len, i, cnt;
136 
137 	*offset = round_up(*offset, 4);
138 	ptr = erofs_read_metabuf(buf, sb, erofs_blknr(sb, *offset), EROFS_KMAP);
139 	if (IS_ERR(ptr))
140 		return ptr;
141 
142 	len = le16_to_cpu(*(__le16 *)&ptr[erofs_blkoff(sb, *offset)]);
143 	if (!len)
144 		len = U16_MAX + 1;
145 	buffer = kmalloc(len, GFP_KERNEL);
146 	if (!buffer)
147 		return ERR_PTR(-ENOMEM);
148 	*offset += sizeof(__le16);
149 	*lengthp = len;
150 
151 	for (i = 0; i < len; i += cnt) {
152 		cnt = min_t(int, sb->s_blocksize - erofs_blkoff(sb, *offset),
153 			    len - i);
154 		ptr = erofs_read_metabuf(buf, sb, erofs_blknr(sb, *offset),
155 					 EROFS_KMAP);
156 		if (IS_ERR(ptr)) {
157 			kfree(buffer);
158 			return ptr;
159 		}
160 		memcpy(buffer + i, ptr + erofs_blkoff(sb, *offset), cnt);
161 		*offset += cnt;
162 	}
163 	return buffer;
164 }
165 
166 static int erofs_load_compr_cfgs(struct super_block *sb,
167 				 struct erofs_super_block *dsb)
168 {
169 	struct erofs_sb_info *sbi = EROFS_SB(sb);
170 	struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
171 	unsigned int algs, alg;
172 	erofs_off_t offset;
173 	int size, ret = 0;
174 
175 	sbi->available_compr_algs = le16_to_cpu(dsb->u1.available_compr_algs);
176 	if (sbi->available_compr_algs & ~Z_EROFS_ALL_COMPR_ALGS) {
177 		erofs_err(sb, "try to load compressed fs with unsupported algorithms %x",
178 			  sbi->available_compr_algs & ~Z_EROFS_ALL_COMPR_ALGS);
179 		return -EINVAL;
180 	}
181 
182 	offset = EROFS_SUPER_OFFSET + sbi->sb_size;
183 	alg = 0;
184 	for (algs = sbi->available_compr_algs; algs; algs >>= 1, ++alg) {
185 		void *data;
186 
187 		if (!(algs & 1))
188 			continue;
189 
190 		data = erofs_read_metadata(sb, &buf, &offset, &size);
191 		if (IS_ERR(data)) {
192 			ret = PTR_ERR(data);
193 			break;
194 		}
195 
196 		switch (alg) {
197 		case Z_EROFS_COMPRESSION_LZ4:
198 			ret = z_erofs_load_lz4_config(sb, dsb, data, size);
199 			break;
200 		case Z_EROFS_COMPRESSION_LZMA:
201 			ret = z_erofs_load_lzma_config(sb, dsb, data, size);
202 			break;
203 		default:
204 			DBG_BUGON(1);
205 			ret = -EFAULT;
206 		}
207 		kfree(data);
208 		if (ret)
209 			break;
210 	}
211 	erofs_put_metabuf(&buf);
212 	return ret;
213 }
214 #else
215 static int erofs_load_compr_cfgs(struct super_block *sb,
216 				 struct erofs_super_block *dsb)
217 {
218 	if (dsb->u1.available_compr_algs) {
219 		erofs_err(sb, "try to load compressed fs when compression is disabled");
220 		return -EINVAL;
221 	}
222 	return 0;
223 }
224 #endif
225 
226 static int erofs_init_device(struct erofs_buf *buf, struct super_block *sb,
227 			     struct erofs_device_info *dif, erofs_off_t *pos)
228 {
229 	struct erofs_sb_info *sbi = EROFS_SB(sb);
230 	struct erofs_fscache *fscache;
231 	struct erofs_deviceslot *dis;
232 	struct block_device *bdev;
233 	void *ptr;
234 
235 	ptr = erofs_read_metabuf(buf, sb, erofs_blknr(sb, *pos), EROFS_KMAP);
236 	if (IS_ERR(ptr))
237 		return PTR_ERR(ptr);
238 	dis = ptr + erofs_blkoff(sb, *pos);
239 
240 	if (!dif->path) {
241 		if (!dis->tag[0]) {
242 			erofs_err(sb, "empty device tag @ pos %llu", *pos);
243 			return -EINVAL;
244 		}
245 		dif->path = kmemdup_nul(dis->tag, sizeof(dis->tag), GFP_KERNEL);
246 		if (!dif->path)
247 			return -ENOMEM;
248 	}
249 
250 	if (erofs_is_fscache_mode(sb)) {
251 		fscache = erofs_fscache_register_cookie(sb, dif->path, 0);
252 		if (IS_ERR(fscache))
253 			return PTR_ERR(fscache);
254 		dif->fscache = fscache;
255 	} else if (!sbi->devs->flatdev) {
256 		bdev = blkdev_get_by_path(dif->path, FMODE_READ | FMODE_EXCL,
257 					  sb->s_type);
258 		if (IS_ERR(bdev))
259 			return PTR_ERR(bdev);
260 		dif->bdev = bdev;
261 		dif->dax_dev = fs_dax_get_by_bdev(bdev, &dif->dax_part_off,
262 						  NULL, NULL);
263 	}
264 
265 	dif->blocks = le32_to_cpu(dis->blocks);
266 	dif->mapped_blkaddr = le32_to_cpu(dis->mapped_blkaddr);
267 	sbi->total_blocks += dif->blocks;
268 	*pos += EROFS_DEVT_SLOT_SIZE;
269 	return 0;
270 }
271 
272 static int erofs_scan_devices(struct super_block *sb,
273 			      struct erofs_super_block *dsb)
274 {
275 	struct erofs_sb_info *sbi = EROFS_SB(sb);
276 	unsigned int ondisk_extradevs;
277 	erofs_off_t pos;
278 	struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
279 	struct erofs_device_info *dif;
280 	int id, err = 0;
281 
282 	sbi->total_blocks = sbi->primarydevice_blocks;
283 	if (!erofs_sb_has_device_table(sbi))
284 		ondisk_extradevs = 0;
285 	else
286 		ondisk_extradevs = le16_to_cpu(dsb->extra_devices);
287 
288 	if (sbi->devs->extra_devices &&
289 	    ondisk_extradevs != sbi->devs->extra_devices) {
290 		erofs_err(sb, "extra devices don't match (ondisk %u, given %u)",
291 			  ondisk_extradevs, sbi->devs->extra_devices);
292 		return -EINVAL;
293 	}
294 	if (!ondisk_extradevs)
295 		return 0;
296 
297 	if (!sbi->devs->extra_devices && !erofs_is_fscache_mode(sb))
298 		sbi->devs->flatdev = true;
299 
300 	sbi->device_id_mask = roundup_pow_of_two(ondisk_extradevs + 1) - 1;
301 	pos = le16_to_cpu(dsb->devt_slotoff) * EROFS_DEVT_SLOT_SIZE;
302 	down_read(&sbi->devs->rwsem);
303 	if (sbi->devs->extra_devices) {
304 		idr_for_each_entry(&sbi->devs->tree, dif, id) {
305 			err = erofs_init_device(&buf, sb, dif, &pos);
306 			if (err)
307 				break;
308 		}
309 	} else {
310 		for (id = 0; id < ondisk_extradevs; id++) {
311 			dif = kzalloc(sizeof(*dif), GFP_KERNEL);
312 			if (!dif) {
313 				err = -ENOMEM;
314 				break;
315 			}
316 
317 			err = idr_alloc(&sbi->devs->tree, dif, 0, 0, GFP_KERNEL);
318 			if (err < 0) {
319 				kfree(dif);
320 				break;
321 			}
322 			++sbi->devs->extra_devices;
323 
324 			err = erofs_init_device(&buf, sb, dif, &pos);
325 			if (err)
326 				break;
327 		}
328 	}
329 	up_read(&sbi->devs->rwsem);
330 	erofs_put_metabuf(&buf);
331 	return err;
332 }
333 
334 static int erofs_read_superblock(struct super_block *sb)
335 {
336 	struct erofs_sb_info *sbi;
337 	struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
338 	struct erofs_super_block *dsb;
339 	void *data;
340 	int ret;
341 
342 	data = erofs_read_metabuf(&buf, sb, 0, EROFS_KMAP);
343 	if (IS_ERR(data)) {
344 		erofs_err(sb, "cannot read erofs superblock");
345 		return PTR_ERR(data);
346 	}
347 
348 	sbi = EROFS_SB(sb);
349 	dsb = (struct erofs_super_block *)(data + EROFS_SUPER_OFFSET);
350 
351 	ret = -EINVAL;
352 	if (le32_to_cpu(dsb->magic) != EROFS_SUPER_MAGIC_V1) {
353 		erofs_err(sb, "cannot find valid erofs superblock");
354 		goto out;
355 	}
356 
357 	sbi->blkszbits  = dsb->blkszbits;
358 	if (sbi->blkszbits < 9 || sbi->blkszbits > PAGE_SHIFT) {
359 		erofs_err(sb, "blkszbits %u isn't supported", sbi->blkszbits);
360 		goto out;
361 	}
362 	if (dsb->dirblkbits) {
363 		erofs_err(sb, "dirblkbits %u isn't supported", dsb->dirblkbits);
364 		goto out;
365 	}
366 
367 	sbi->feature_compat = le32_to_cpu(dsb->feature_compat);
368 	if (erofs_sb_has_sb_chksum(sbi)) {
369 		ret = erofs_superblock_csum_verify(sb, data);
370 		if (ret)
371 			goto out;
372 	}
373 
374 	ret = -EINVAL;
375 	if (!check_layout_compatibility(sb, dsb))
376 		goto out;
377 
378 	sbi->sb_size = 128 + dsb->sb_extslots * EROFS_SB_EXTSLOT_SIZE;
379 	if (sbi->sb_size > PAGE_SIZE - EROFS_SUPER_OFFSET) {
380 		erofs_err(sb, "invalid sb_extslots %u (more than a fs block)",
381 			  sbi->sb_size);
382 		goto out;
383 	}
384 	sbi->primarydevice_blocks = le32_to_cpu(dsb->blocks);
385 	sbi->meta_blkaddr = le32_to_cpu(dsb->meta_blkaddr);
386 #ifdef CONFIG_EROFS_FS_XATTR
387 	sbi->xattr_blkaddr = le32_to_cpu(dsb->xattr_blkaddr);
388 #endif
389 	sbi->islotbits = ilog2(sizeof(struct erofs_inode_compact));
390 	sbi->root_nid = le16_to_cpu(dsb->root_nid);
391 #ifdef CONFIG_EROFS_FS_ZIP
392 	sbi->packed_inode = NULL;
393 	if (erofs_sb_has_fragments(sbi) && dsb->packed_nid) {
394 		sbi->packed_inode =
395 			erofs_iget(sb, le64_to_cpu(dsb->packed_nid));
396 		if (IS_ERR(sbi->packed_inode)) {
397 			ret = PTR_ERR(sbi->packed_inode);
398 			goto out;
399 		}
400 	}
401 #endif
402 	sbi->inos = le64_to_cpu(dsb->inos);
403 
404 	sbi->build_time = le64_to_cpu(dsb->build_time);
405 	sbi->build_time_nsec = le32_to_cpu(dsb->build_time_nsec);
406 
407 	memcpy(&sb->s_uuid, dsb->uuid, sizeof(dsb->uuid));
408 
409 	ret = strscpy(sbi->volume_name, dsb->volume_name,
410 		      sizeof(dsb->volume_name));
411 	if (ret < 0) {	/* -E2BIG */
412 		erofs_err(sb, "bad volume name without NIL terminator");
413 		ret = -EFSCORRUPTED;
414 		goto out;
415 	}
416 
417 	/* parse on-disk compression configurations */
418 	if (erofs_sb_has_compr_cfgs(sbi))
419 		ret = erofs_load_compr_cfgs(sb, dsb);
420 	else
421 		ret = z_erofs_load_lz4_config(sb, dsb, NULL, 0);
422 	if (ret < 0)
423 		goto out;
424 
425 	/* handle multiple devices */
426 	ret = erofs_scan_devices(sb, dsb);
427 
428 	if (erofs_is_fscache_mode(sb))
429 		erofs_info(sb, "EXPERIMENTAL fscache-based on-demand read feature in use. Use at your own risk!");
430 	if (erofs_sb_has_fragments(sbi))
431 		erofs_info(sb, "EXPERIMENTAL compressed fragments feature in use. Use at your own risk!");
432 	if (erofs_sb_has_dedupe(sbi))
433 		erofs_info(sb, "EXPERIMENTAL global deduplication feature in use. Use at your own risk!");
434 out:
435 	erofs_put_metabuf(&buf);
436 	return ret;
437 }
438 
439 /* set up default EROFS parameters */
440 static void erofs_default_options(struct erofs_fs_context *ctx)
441 {
442 #ifdef CONFIG_EROFS_FS_ZIP
443 	ctx->opt.cache_strategy = EROFS_ZIP_CACHE_READAROUND;
444 	ctx->opt.max_sync_decompress_pages = 3;
445 	ctx->opt.sync_decompress = EROFS_SYNC_DECOMPRESS_AUTO;
446 #endif
447 #ifdef CONFIG_EROFS_FS_XATTR
448 	set_opt(&ctx->opt, XATTR_USER);
449 #endif
450 #ifdef CONFIG_EROFS_FS_POSIX_ACL
451 	set_opt(&ctx->opt, POSIX_ACL);
452 #endif
453 }
454 
455 enum {
456 	Opt_user_xattr,
457 	Opt_acl,
458 	Opt_cache_strategy,
459 	Opt_dax,
460 	Opt_dax_enum,
461 	Opt_device,
462 	Opt_fsid,
463 	Opt_domain_id,
464 	Opt_err
465 };
466 
467 static const struct constant_table erofs_param_cache_strategy[] = {
468 	{"disabled",	EROFS_ZIP_CACHE_DISABLED},
469 	{"readahead",	EROFS_ZIP_CACHE_READAHEAD},
470 	{"readaround",	EROFS_ZIP_CACHE_READAROUND},
471 	{}
472 };
473 
474 static const struct constant_table erofs_dax_param_enums[] = {
475 	{"always",	EROFS_MOUNT_DAX_ALWAYS},
476 	{"never",	EROFS_MOUNT_DAX_NEVER},
477 	{}
478 };
479 
480 static const struct fs_parameter_spec erofs_fs_parameters[] = {
481 	fsparam_flag_no("user_xattr",	Opt_user_xattr),
482 	fsparam_flag_no("acl",		Opt_acl),
483 	fsparam_enum("cache_strategy",	Opt_cache_strategy,
484 		     erofs_param_cache_strategy),
485 	fsparam_flag("dax",             Opt_dax),
486 	fsparam_enum("dax",		Opt_dax_enum, erofs_dax_param_enums),
487 	fsparam_string("device",	Opt_device),
488 	fsparam_string("fsid",		Opt_fsid),
489 	fsparam_string("domain_id",	Opt_domain_id),
490 	{}
491 };
492 
493 static bool erofs_fc_set_dax_mode(struct fs_context *fc, unsigned int mode)
494 {
495 #ifdef CONFIG_FS_DAX
496 	struct erofs_fs_context *ctx = fc->fs_private;
497 
498 	switch (mode) {
499 	case EROFS_MOUNT_DAX_ALWAYS:
500 		warnfc(fc, "DAX enabled. Warning: EXPERIMENTAL, use at your own risk");
501 		set_opt(&ctx->opt, DAX_ALWAYS);
502 		clear_opt(&ctx->opt, DAX_NEVER);
503 		return true;
504 	case EROFS_MOUNT_DAX_NEVER:
505 		set_opt(&ctx->opt, DAX_NEVER);
506 		clear_opt(&ctx->opt, DAX_ALWAYS);
507 		return true;
508 	default:
509 		DBG_BUGON(1);
510 		return false;
511 	}
512 #else
513 	errorfc(fc, "dax options not supported");
514 	return false;
515 #endif
516 }
517 
518 static int erofs_fc_parse_param(struct fs_context *fc,
519 				struct fs_parameter *param)
520 {
521 	struct erofs_fs_context *ctx = fc->fs_private;
522 	struct fs_parse_result result;
523 	struct erofs_device_info *dif;
524 	int opt, ret;
525 
526 	opt = fs_parse(fc, erofs_fs_parameters, param, &result);
527 	if (opt < 0)
528 		return opt;
529 
530 	switch (opt) {
531 	case Opt_user_xattr:
532 #ifdef CONFIG_EROFS_FS_XATTR
533 		if (result.boolean)
534 			set_opt(&ctx->opt, XATTR_USER);
535 		else
536 			clear_opt(&ctx->opt, XATTR_USER);
537 #else
538 		errorfc(fc, "{,no}user_xattr options not supported");
539 #endif
540 		break;
541 	case Opt_acl:
542 #ifdef CONFIG_EROFS_FS_POSIX_ACL
543 		if (result.boolean)
544 			set_opt(&ctx->opt, POSIX_ACL);
545 		else
546 			clear_opt(&ctx->opt, POSIX_ACL);
547 #else
548 		errorfc(fc, "{,no}acl options not supported");
549 #endif
550 		break;
551 	case Opt_cache_strategy:
552 #ifdef CONFIG_EROFS_FS_ZIP
553 		ctx->opt.cache_strategy = result.uint_32;
554 #else
555 		errorfc(fc, "compression not supported, cache_strategy ignored");
556 #endif
557 		break;
558 	case Opt_dax:
559 		if (!erofs_fc_set_dax_mode(fc, EROFS_MOUNT_DAX_ALWAYS))
560 			return -EINVAL;
561 		break;
562 	case Opt_dax_enum:
563 		if (!erofs_fc_set_dax_mode(fc, result.uint_32))
564 			return -EINVAL;
565 		break;
566 	case Opt_device:
567 		dif = kzalloc(sizeof(*dif), GFP_KERNEL);
568 		if (!dif)
569 			return -ENOMEM;
570 		dif->path = kstrdup(param->string, GFP_KERNEL);
571 		if (!dif->path) {
572 			kfree(dif);
573 			return -ENOMEM;
574 		}
575 		down_write(&ctx->devs->rwsem);
576 		ret = idr_alloc(&ctx->devs->tree, dif, 0, 0, GFP_KERNEL);
577 		up_write(&ctx->devs->rwsem);
578 		if (ret < 0) {
579 			kfree(dif->path);
580 			kfree(dif);
581 			return ret;
582 		}
583 		++ctx->devs->extra_devices;
584 		break;
585 #ifdef CONFIG_EROFS_FS_ONDEMAND
586 	case Opt_fsid:
587 		kfree(ctx->fsid);
588 		ctx->fsid = kstrdup(param->string, GFP_KERNEL);
589 		if (!ctx->fsid)
590 			return -ENOMEM;
591 		break;
592 	case Opt_domain_id:
593 		kfree(ctx->domain_id);
594 		ctx->domain_id = kstrdup(param->string, GFP_KERNEL);
595 		if (!ctx->domain_id)
596 			return -ENOMEM;
597 		break;
598 #else
599 	case Opt_fsid:
600 	case Opt_domain_id:
601 		errorfc(fc, "%s option not supported", erofs_fs_parameters[opt].name);
602 		break;
603 #endif
604 	default:
605 		return -ENOPARAM;
606 	}
607 	return 0;
608 }
609 
610 #ifdef CONFIG_EROFS_FS_ZIP
611 static const struct address_space_operations managed_cache_aops;
612 
613 static bool erofs_managed_cache_release_folio(struct folio *folio, gfp_t gfp)
614 {
615 	bool ret = true;
616 	struct address_space *const mapping = folio->mapping;
617 
618 	DBG_BUGON(!folio_test_locked(folio));
619 	DBG_BUGON(mapping->a_ops != &managed_cache_aops);
620 
621 	if (folio_test_private(folio))
622 		ret = erofs_try_to_free_cached_page(&folio->page);
623 
624 	return ret;
625 }
626 
627 /*
628  * It will be called only on inode eviction. In case that there are still some
629  * decompression requests in progress, wait with rescheduling for a bit here.
630  * We could introduce an extra locking instead but it seems unnecessary.
631  */
632 static void erofs_managed_cache_invalidate_folio(struct folio *folio,
633 					       size_t offset, size_t length)
634 {
635 	const size_t stop = length + offset;
636 
637 	DBG_BUGON(!folio_test_locked(folio));
638 
639 	/* Check for potential overflow in debug mode */
640 	DBG_BUGON(stop > folio_size(folio) || stop < length);
641 
642 	if (offset == 0 && stop == folio_size(folio))
643 		while (!erofs_managed_cache_release_folio(folio, GFP_NOFS))
644 			cond_resched();
645 }
646 
647 static const struct address_space_operations managed_cache_aops = {
648 	.release_folio = erofs_managed_cache_release_folio,
649 	.invalidate_folio = erofs_managed_cache_invalidate_folio,
650 };
651 
652 static int erofs_init_managed_cache(struct super_block *sb)
653 {
654 	struct erofs_sb_info *const sbi = EROFS_SB(sb);
655 	struct inode *const inode = new_inode(sb);
656 
657 	if (!inode)
658 		return -ENOMEM;
659 
660 	set_nlink(inode, 1);
661 	inode->i_size = OFFSET_MAX;
662 
663 	inode->i_mapping->a_ops = &managed_cache_aops;
664 	mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
665 	sbi->managed_cache = inode;
666 	return 0;
667 }
668 #else
669 static int erofs_init_managed_cache(struct super_block *sb) { return 0; }
670 #endif
671 
672 static struct inode *erofs_nfs_get_inode(struct super_block *sb,
673 					 u64 ino, u32 generation)
674 {
675 	return erofs_iget(sb, ino);
676 }
677 
678 static struct dentry *erofs_fh_to_dentry(struct super_block *sb,
679 		struct fid *fid, int fh_len, int fh_type)
680 {
681 	return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
682 				    erofs_nfs_get_inode);
683 }
684 
685 static struct dentry *erofs_fh_to_parent(struct super_block *sb,
686 		struct fid *fid, int fh_len, int fh_type)
687 {
688 	return generic_fh_to_parent(sb, fid, fh_len, fh_type,
689 				    erofs_nfs_get_inode);
690 }
691 
692 static struct dentry *erofs_get_parent(struct dentry *child)
693 {
694 	erofs_nid_t nid;
695 	unsigned int d_type;
696 	int err;
697 
698 	err = erofs_namei(d_inode(child), &dotdot_name, &nid, &d_type);
699 	if (err)
700 		return ERR_PTR(err);
701 	return d_obtain_alias(erofs_iget(child->d_sb, nid));
702 }
703 
704 static const struct export_operations erofs_export_ops = {
705 	.fh_to_dentry = erofs_fh_to_dentry,
706 	.fh_to_parent = erofs_fh_to_parent,
707 	.get_parent = erofs_get_parent,
708 };
709 
710 static int erofs_fc_fill_pseudo_super(struct super_block *sb, struct fs_context *fc)
711 {
712 	static const struct tree_descr empty_descr = {""};
713 
714 	return simple_fill_super(sb, EROFS_SUPER_MAGIC, &empty_descr);
715 }
716 
717 static int erofs_fc_fill_super(struct super_block *sb, struct fs_context *fc)
718 {
719 	struct inode *inode;
720 	struct erofs_sb_info *sbi;
721 	struct erofs_fs_context *ctx = fc->fs_private;
722 	int err;
723 
724 	sb->s_magic = EROFS_SUPER_MAGIC;
725 	sb->s_flags |= SB_RDONLY | SB_NOATIME;
726 	sb->s_maxbytes = MAX_LFS_FILESIZE;
727 	sb->s_op = &erofs_sops;
728 
729 	sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
730 	if (!sbi)
731 		return -ENOMEM;
732 
733 	sb->s_fs_info = sbi;
734 	sbi->opt = ctx->opt;
735 	sbi->devs = ctx->devs;
736 	ctx->devs = NULL;
737 	sbi->fsid = ctx->fsid;
738 	ctx->fsid = NULL;
739 	sbi->domain_id = ctx->domain_id;
740 	ctx->domain_id = NULL;
741 
742 	sbi->blkszbits = PAGE_SHIFT;
743 	if (erofs_is_fscache_mode(sb)) {
744 		sb->s_blocksize = PAGE_SIZE;
745 		sb->s_blocksize_bits = PAGE_SHIFT;
746 
747 		err = erofs_fscache_register_fs(sb);
748 		if (err)
749 			return err;
750 
751 		err = super_setup_bdi(sb);
752 		if (err)
753 			return err;
754 	} else {
755 		if (!sb_set_blocksize(sb, PAGE_SIZE)) {
756 			errorfc(fc, "failed to set initial blksize");
757 			return -EINVAL;
758 		}
759 
760 		sbi->dax_dev = fs_dax_get_by_bdev(sb->s_bdev,
761 						  &sbi->dax_part_off,
762 						  NULL, NULL);
763 	}
764 
765 	err = erofs_read_superblock(sb);
766 	if (err)
767 		return err;
768 
769 	if (sb->s_blocksize_bits != sbi->blkszbits) {
770 		if (erofs_is_fscache_mode(sb)) {
771 			errorfc(fc, "unsupported blksize for fscache mode");
772 			return -EINVAL;
773 		}
774 		if (!sb_set_blocksize(sb, 1 << sbi->blkszbits)) {
775 			errorfc(fc, "failed to set erofs blksize");
776 			return -EINVAL;
777 		}
778 	}
779 
780 	if (test_opt(&sbi->opt, DAX_ALWAYS)) {
781 		if (!sbi->dax_dev) {
782 			errorfc(fc, "DAX unsupported by block device. Turning off DAX.");
783 			clear_opt(&sbi->opt, DAX_ALWAYS);
784 		} else if (sbi->blkszbits != PAGE_SHIFT) {
785 			errorfc(fc, "unsupported blocksize for DAX");
786 			clear_opt(&sbi->opt, DAX_ALWAYS);
787 		}
788 	}
789 
790 	sb->s_time_gran = 1;
791 	sb->s_xattr = erofs_xattr_handlers;
792 	sb->s_export_op = &erofs_export_ops;
793 
794 	if (test_opt(&sbi->opt, POSIX_ACL))
795 		sb->s_flags |= SB_POSIXACL;
796 	else
797 		sb->s_flags &= ~SB_POSIXACL;
798 
799 #ifdef CONFIG_EROFS_FS_ZIP
800 	xa_init(&sbi->managed_pslots);
801 #endif
802 
803 	/* get the root inode */
804 	inode = erofs_iget(sb, ROOT_NID(sbi));
805 	if (IS_ERR(inode))
806 		return PTR_ERR(inode);
807 
808 	if (!S_ISDIR(inode->i_mode)) {
809 		erofs_err(sb, "rootino(nid %llu) is not a directory(i_mode %o)",
810 			  ROOT_NID(sbi), inode->i_mode);
811 		iput(inode);
812 		return -EINVAL;
813 	}
814 
815 	sb->s_root = d_make_root(inode);
816 	if (!sb->s_root)
817 		return -ENOMEM;
818 
819 	erofs_shrinker_register(sb);
820 	/* sb->s_umount is already locked, SB_ACTIVE and SB_BORN are not set */
821 	err = erofs_init_managed_cache(sb);
822 	if (err)
823 		return err;
824 
825 	err = erofs_register_sysfs(sb);
826 	if (err)
827 		return err;
828 
829 	erofs_info(sb, "mounted with root inode @ nid %llu.", ROOT_NID(sbi));
830 	return 0;
831 }
832 
833 static int erofs_fc_anon_get_tree(struct fs_context *fc)
834 {
835 	return get_tree_nodev(fc, erofs_fc_fill_pseudo_super);
836 }
837 
838 static int erofs_fc_get_tree(struct fs_context *fc)
839 {
840 	struct erofs_fs_context *ctx = fc->fs_private;
841 
842 	if (IS_ENABLED(CONFIG_EROFS_FS_ONDEMAND) && ctx->fsid)
843 		return get_tree_nodev(fc, erofs_fc_fill_super);
844 
845 	return get_tree_bdev(fc, erofs_fc_fill_super);
846 }
847 
848 static int erofs_fc_reconfigure(struct fs_context *fc)
849 {
850 	struct super_block *sb = fc->root->d_sb;
851 	struct erofs_sb_info *sbi = EROFS_SB(sb);
852 	struct erofs_fs_context *ctx = fc->fs_private;
853 
854 	DBG_BUGON(!sb_rdonly(sb));
855 
856 	if (ctx->fsid || ctx->domain_id)
857 		erofs_info(sb, "ignoring reconfiguration for fsid|domain_id.");
858 
859 	if (test_opt(&ctx->opt, POSIX_ACL))
860 		fc->sb_flags |= SB_POSIXACL;
861 	else
862 		fc->sb_flags &= ~SB_POSIXACL;
863 
864 	sbi->opt = ctx->opt;
865 
866 	fc->sb_flags |= SB_RDONLY;
867 	return 0;
868 }
869 
870 static int erofs_release_device_info(int id, void *ptr, void *data)
871 {
872 	struct erofs_device_info *dif = ptr;
873 
874 	fs_put_dax(dif->dax_dev, NULL);
875 	if (dif->bdev)
876 		blkdev_put(dif->bdev, FMODE_READ | FMODE_EXCL);
877 	erofs_fscache_unregister_cookie(dif->fscache);
878 	dif->fscache = NULL;
879 	kfree(dif->path);
880 	kfree(dif);
881 	return 0;
882 }
883 
884 static void erofs_free_dev_context(struct erofs_dev_context *devs)
885 {
886 	if (!devs)
887 		return;
888 	idr_for_each(&devs->tree, &erofs_release_device_info, NULL);
889 	idr_destroy(&devs->tree);
890 	kfree(devs);
891 }
892 
893 static void erofs_fc_free(struct fs_context *fc)
894 {
895 	struct erofs_fs_context *ctx = fc->fs_private;
896 
897 	erofs_free_dev_context(ctx->devs);
898 	kfree(ctx->fsid);
899 	kfree(ctx->domain_id);
900 	kfree(ctx);
901 }
902 
903 static const struct fs_context_operations erofs_context_ops = {
904 	.parse_param	= erofs_fc_parse_param,
905 	.get_tree       = erofs_fc_get_tree,
906 	.reconfigure    = erofs_fc_reconfigure,
907 	.free		= erofs_fc_free,
908 };
909 
910 static const struct fs_context_operations erofs_anon_context_ops = {
911 	.get_tree       = erofs_fc_anon_get_tree,
912 };
913 
914 static int erofs_init_fs_context(struct fs_context *fc)
915 {
916 	struct erofs_fs_context *ctx;
917 
918 	/* pseudo mount for anon inodes */
919 	if (fc->sb_flags & SB_KERNMOUNT) {
920 		fc->ops = &erofs_anon_context_ops;
921 		return 0;
922 	}
923 
924 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
925 	if (!ctx)
926 		return -ENOMEM;
927 	ctx->devs = kzalloc(sizeof(struct erofs_dev_context), GFP_KERNEL);
928 	if (!ctx->devs) {
929 		kfree(ctx);
930 		return -ENOMEM;
931 	}
932 	fc->fs_private = ctx;
933 
934 	idr_init(&ctx->devs->tree);
935 	init_rwsem(&ctx->devs->rwsem);
936 	erofs_default_options(ctx);
937 	fc->ops = &erofs_context_ops;
938 	return 0;
939 }
940 
941 /*
942  * could be triggered after deactivate_locked_super()
943  * is called, thus including umount and failed to initialize.
944  */
945 static void erofs_kill_sb(struct super_block *sb)
946 {
947 	struct erofs_sb_info *sbi;
948 
949 	WARN_ON(sb->s_magic != EROFS_SUPER_MAGIC);
950 
951 	/* pseudo mount for anon inodes */
952 	if (sb->s_flags & SB_KERNMOUNT) {
953 		kill_anon_super(sb);
954 		return;
955 	}
956 
957 	if (erofs_is_fscache_mode(sb))
958 		kill_anon_super(sb);
959 	else
960 		kill_block_super(sb);
961 
962 	sbi = EROFS_SB(sb);
963 	if (!sbi)
964 		return;
965 
966 	erofs_free_dev_context(sbi->devs);
967 	fs_put_dax(sbi->dax_dev, NULL);
968 	erofs_fscache_unregister_fs(sb);
969 	kfree(sbi->fsid);
970 	kfree(sbi->domain_id);
971 	kfree(sbi);
972 	sb->s_fs_info = NULL;
973 }
974 
975 /* called when ->s_root is non-NULL */
976 static void erofs_put_super(struct super_block *sb)
977 {
978 	struct erofs_sb_info *const sbi = EROFS_SB(sb);
979 
980 	DBG_BUGON(!sbi);
981 
982 	erofs_unregister_sysfs(sb);
983 	erofs_shrinker_unregister(sb);
984 #ifdef CONFIG_EROFS_FS_ZIP
985 	iput(sbi->managed_cache);
986 	sbi->managed_cache = NULL;
987 	iput(sbi->packed_inode);
988 	sbi->packed_inode = NULL;
989 #endif
990 	erofs_free_dev_context(sbi->devs);
991 	sbi->devs = NULL;
992 	erofs_fscache_unregister_fs(sb);
993 }
994 
995 struct file_system_type erofs_fs_type = {
996 	.owner          = THIS_MODULE,
997 	.name           = "erofs",
998 	.init_fs_context = erofs_init_fs_context,
999 	.kill_sb        = erofs_kill_sb,
1000 	.fs_flags       = FS_REQUIRES_DEV | FS_ALLOW_IDMAP,
1001 };
1002 MODULE_ALIAS_FS("erofs");
1003 
1004 static int __init erofs_module_init(void)
1005 {
1006 	int err;
1007 
1008 	erofs_check_ondisk_layout_definitions();
1009 
1010 	erofs_inode_cachep = kmem_cache_create("erofs_inode",
1011 					       sizeof(struct erofs_inode), 0,
1012 					       SLAB_RECLAIM_ACCOUNT,
1013 					       erofs_inode_init_once);
1014 	if (!erofs_inode_cachep) {
1015 		err = -ENOMEM;
1016 		goto icache_err;
1017 	}
1018 
1019 	err = erofs_init_shrinker();
1020 	if (err)
1021 		goto shrinker_err;
1022 
1023 	err = z_erofs_lzma_init();
1024 	if (err)
1025 		goto lzma_err;
1026 
1027 	erofs_pcpubuf_init();
1028 	err = z_erofs_init_zip_subsystem();
1029 	if (err)
1030 		goto zip_err;
1031 
1032 	err = erofs_init_sysfs();
1033 	if (err)
1034 		goto sysfs_err;
1035 
1036 	err = register_filesystem(&erofs_fs_type);
1037 	if (err)
1038 		goto fs_err;
1039 
1040 	return 0;
1041 
1042 fs_err:
1043 	erofs_exit_sysfs();
1044 sysfs_err:
1045 	z_erofs_exit_zip_subsystem();
1046 zip_err:
1047 	z_erofs_lzma_exit();
1048 lzma_err:
1049 	erofs_exit_shrinker();
1050 shrinker_err:
1051 	kmem_cache_destroy(erofs_inode_cachep);
1052 icache_err:
1053 	return err;
1054 }
1055 
1056 static void __exit erofs_module_exit(void)
1057 {
1058 	unregister_filesystem(&erofs_fs_type);
1059 
1060 	/* Ensure all RCU free inodes / pclusters are safe to be destroyed. */
1061 	rcu_barrier();
1062 
1063 	erofs_exit_sysfs();
1064 	z_erofs_exit_zip_subsystem();
1065 	z_erofs_lzma_exit();
1066 	erofs_exit_shrinker();
1067 	kmem_cache_destroy(erofs_inode_cachep);
1068 	erofs_pcpubuf_exit();
1069 }
1070 
1071 /* get filesystem statistics */
1072 static int erofs_statfs(struct dentry *dentry, struct kstatfs *buf)
1073 {
1074 	struct super_block *sb = dentry->d_sb;
1075 	struct erofs_sb_info *sbi = EROFS_SB(sb);
1076 	u64 id = 0;
1077 
1078 	if (!erofs_is_fscache_mode(sb))
1079 		id = huge_encode_dev(sb->s_bdev->bd_dev);
1080 
1081 	buf->f_type = sb->s_magic;
1082 	buf->f_bsize = sb->s_blocksize;
1083 	buf->f_blocks = sbi->total_blocks;
1084 	buf->f_bfree = buf->f_bavail = 0;
1085 
1086 	buf->f_files = ULLONG_MAX;
1087 	buf->f_ffree = ULLONG_MAX - sbi->inos;
1088 
1089 	buf->f_namelen = EROFS_NAME_LEN;
1090 
1091 	buf->f_fsid    = u64_to_fsid(id);
1092 	return 0;
1093 }
1094 
1095 static int erofs_show_options(struct seq_file *seq, struct dentry *root)
1096 {
1097 	struct erofs_sb_info *sbi = EROFS_SB(root->d_sb);
1098 	struct erofs_mount_opts *opt = &sbi->opt;
1099 
1100 #ifdef CONFIG_EROFS_FS_XATTR
1101 	if (test_opt(opt, XATTR_USER))
1102 		seq_puts(seq, ",user_xattr");
1103 	else
1104 		seq_puts(seq, ",nouser_xattr");
1105 #endif
1106 #ifdef CONFIG_EROFS_FS_POSIX_ACL
1107 	if (test_opt(opt, POSIX_ACL))
1108 		seq_puts(seq, ",acl");
1109 	else
1110 		seq_puts(seq, ",noacl");
1111 #endif
1112 #ifdef CONFIG_EROFS_FS_ZIP
1113 	if (opt->cache_strategy == EROFS_ZIP_CACHE_DISABLED)
1114 		seq_puts(seq, ",cache_strategy=disabled");
1115 	else if (opt->cache_strategy == EROFS_ZIP_CACHE_READAHEAD)
1116 		seq_puts(seq, ",cache_strategy=readahead");
1117 	else if (opt->cache_strategy == EROFS_ZIP_CACHE_READAROUND)
1118 		seq_puts(seq, ",cache_strategy=readaround");
1119 #endif
1120 	if (test_opt(opt, DAX_ALWAYS))
1121 		seq_puts(seq, ",dax=always");
1122 	if (test_opt(opt, DAX_NEVER))
1123 		seq_puts(seq, ",dax=never");
1124 #ifdef CONFIG_EROFS_FS_ONDEMAND
1125 	if (sbi->fsid)
1126 		seq_printf(seq, ",fsid=%s", sbi->fsid);
1127 	if (sbi->domain_id)
1128 		seq_printf(seq, ",domain_id=%s", sbi->domain_id);
1129 #endif
1130 	return 0;
1131 }
1132 
1133 const struct super_operations erofs_sops = {
1134 	.put_super = erofs_put_super,
1135 	.alloc_inode = erofs_alloc_inode,
1136 	.free_inode = erofs_free_inode,
1137 	.statfs = erofs_statfs,
1138 	.show_options = erofs_show_options,
1139 };
1140 
1141 module_init(erofs_module_init);
1142 module_exit(erofs_module_exit);
1143 
1144 MODULE_DESCRIPTION("Enhanced ROM File System");
1145 MODULE_AUTHOR("Gao Xiang, Chao Yu, Miao Xie, CONSUMER BG, HUAWEI Inc.");
1146 MODULE_LICENSE("GPL");
1147