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