xref: /openbmc/linux/fs/erofs/super.c (revision c665b394)
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_devices(struct super_block *sb,
224 			      struct erofs_super_block *dsb)
225 {
226 	struct erofs_sb_info *sbi = EROFS_SB(sb);
227 	unsigned int ondisk_extradevs;
228 	erofs_off_t pos;
229 	struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
230 	struct erofs_device_info *dif;
231 	struct erofs_deviceslot *dis;
232 	void *ptr;
233 	int id, err = 0;
234 
235 	sbi->total_blocks = sbi->primarydevice_blocks;
236 	if (!erofs_sb_has_device_table(sbi))
237 		ondisk_extradevs = 0;
238 	else
239 		ondisk_extradevs = le16_to_cpu(dsb->extra_devices);
240 
241 	if (ondisk_extradevs != sbi->devs->extra_devices) {
242 		erofs_err(sb, "extra devices don't match (ondisk %u, given %u)",
243 			  ondisk_extradevs, sbi->devs->extra_devices);
244 		return -EINVAL;
245 	}
246 	if (!ondisk_extradevs)
247 		return 0;
248 
249 	sbi->device_id_mask = roundup_pow_of_two(ondisk_extradevs + 1) - 1;
250 	pos = le16_to_cpu(dsb->devt_slotoff) * EROFS_DEVT_SLOT_SIZE;
251 	down_read(&sbi->devs->rwsem);
252 	idr_for_each_entry(&sbi->devs->tree, dif, id) {
253 		struct block_device *bdev;
254 
255 		ptr = erofs_read_metabuf(&buf, sb, erofs_blknr(pos),
256 					 EROFS_KMAP);
257 		if (IS_ERR(ptr)) {
258 			err = PTR_ERR(ptr);
259 			break;
260 		}
261 		dis = ptr + erofs_blkoff(pos);
262 
263 		if (erofs_is_fscache_mode(sb)) {
264 			err = erofs_fscache_register_cookie(sb, &dif->fscache,
265 							    dif->path, false);
266 			if (err)
267 				break;
268 		} else {
269 			bdev = blkdev_get_by_path(dif->path,
270 						  FMODE_READ | FMODE_EXCL,
271 						  sb->s_type);
272 			if (IS_ERR(bdev)) {
273 				err = PTR_ERR(bdev);
274 				break;
275 			}
276 			dif->bdev = bdev;
277 			dif->dax_dev = fs_dax_get_by_bdev(bdev,
278 							  &dif->dax_part_off);
279 		}
280 
281 		dif->blocks = le32_to_cpu(dis->blocks);
282 		dif->mapped_blkaddr = le32_to_cpu(dis->mapped_blkaddr);
283 		sbi->total_blocks += dif->blocks;
284 		pos += EROFS_DEVT_SLOT_SIZE;
285 	}
286 	up_read(&sbi->devs->rwsem);
287 	erofs_put_metabuf(&buf);
288 	return err;
289 }
290 
291 static int erofs_read_superblock(struct super_block *sb)
292 {
293 	struct erofs_sb_info *sbi;
294 	struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
295 	struct erofs_super_block *dsb;
296 	unsigned int blkszbits;
297 	void *data;
298 	int ret;
299 
300 	data = erofs_read_metabuf(&buf, sb, 0, EROFS_KMAP);
301 	if (IS_ERR(data)) {
302 		erofs_err(sb, "cannot read erofs superblock");
303 		return PTR_ERR(data);
304 	}
305 
306 	sbi = EROFS_SB(sb);
307 	dsb = (struct erofs_super_block *)(data + EROFS_SUPER_OFFSET);
308 
309 	ret = -EINVAL;
310 	if (le32_to_cpu(dsb->magic) != EROFS_SUPER_MAGIC_V1) {
311 		erofs_err(sb, "cannot find valid erofs superblock");
312 		goto out;
313 	}
314 
315 	sbi->feature_compat = le32_to_cpu(dsb->feature_compat);
316 	if (erofs_sb_has_sb_chksum(sbi)) {
317 		ret = erofs_superblock_csum_verify(sb, data);
318 		if (ret)
319 			goto out;
320 	}
321 
322 	ret = -EINVAL;
323 	blkszbits = dsb->blkszbits;
324 	/* 9(512 bytes) + LOG_SECTORS_PER_BLOCK == LOG_BLOCK_SIZE */
325 	if (blkszbits != LOG_BLOCK_SIZE) {
326 		erofs_err(sb, "blkszbits %u isn't supported on this platform",
327 			  blkszbits);
328 		goto out;
329 	}
330 
331 	if (!check_layout_compatibility(sb, dsb))
332 		goto out;
333 
334 	sbi->sb_size = 128 + dsb->sb_extslots * EROFS_SB_EXTSLOT_SIZE;
335 	if (sbi->sb_size > EROFS_BLKSIZ) {
336 		erofs_err(sb, "invalid sb_extslots %u (more than a fs block)",
337 			  sbi->sb_size);
338 		goto out;
339 	}
340 	sbi->primarydevice_blocks = le32_to_cpu(dsb->blocks);
341 	sbi->meta_blkaddr = le32_to_cpu(dsb->meta_blkaddr);
342 #ifdef CONFIG_EROFS_FS_XATTR
343 	sbi->xattr_blkaddr = le32_to_cpu(dsb->xattr_blkaddr);
344 #endif
345 	sbi->islotbits = ilog2(sizeof(struct erofs_inode_compact));
346 	sbi->root_nid = le16_to_cpu(dsb->root_nid);
347 	sbi->inos = le64_to_cpu(dsb->inos);
348 
349 	sbi->build_time = le64_to_cpu(dsb->build_time);
350 	sbi->build_time_nsec = le32_to_cpu(dsb->build_time_nsec);
351 
352 	memcpy(&sb->s_uuid, dsb->uuid, sizeof(dsb->uuid));
353 
354 	ret = strscpy(sbi->volume_name, dsb->volume_name,
355 		      sizeof(dsb->volume_name));
356 	if (ret < 0) {	/* -E2BIG */
357 		erofs_err(sb, "bad volume name without NIL terminator");
358 		ret = -EFSCORRUPTED;
359 		goto out;
360 	}
361 
362 	/* parse on-disk compression configurations */
363 	if (erofs_sb_has_compr_cfgs(sbi))
364 		ret = erofs_load_compr_cfgs(sb, dsb);
365 	else
366 		ret = z_erofs_load_lz4_config(sb, dsb, NULL, 0);
367 	if (ret < 0)
368 		goto out;
369 
370 	/* handle multiple devices */
371 	ret = erofs_init_devices(sb, dsb);
372 
373 	if (erofs_sb_has_ztailpacking(sbi))
374 		erofs_info(sb, "EXPERIMENTAL compressed inline data feature in use. Use at your own risk!");
375 out:
376 	erofs_put_metabuf(&buf);
377 	return ret;
378 }
379 
380 /* set up default EROFS parameters */
381 static void erofs_default_options(struct erofs_fs_context *ctx)
382 {
383 #ifdef CONFIG_EROFS_FS_ZIP
384 	ctx->opt.cache_strategy = EROFS_ZIP_CACHE_READAROUND;
385 	ctx->opt.max_sync_decompress_pages = 3;
386 	ctx->opt.sync_decompress = EROFS_SYNC_DECOMPRESS_AUTO;
387 #endif
388 #ifdef CONFIG_EROFS_FS_XATTR
389 	set_opt(&ctx->opt, XATTR_USER);
390 #endif
391 #ifdef CONFIG_EROFS_FS_POSIX_ACL
392 	set_opt(&ctx->opt, POSIX_ACL);
393 #endif
394 }
395 
396 enum {
397 	Opt_user_xattr,
398 	Opt_acl,
399 	Opt_cache_strategy,
400 	Opt_dax,
401 	Opt_dax_enum,
402 	Opt_device,
403 	Opt_err
404 };
405 
406 static const struct constant_table erofs_param_cache_strategy[] = {
407 	{"disabled",	EROFS_ZIP_CACHE_DISABLED},
408 	{"readahead",	EROFS_ZIP_CACHE_READAHEAD},
409 	{"readaround",	EROFS_ZIP_CACHE_READAROUND},
410 	{}
411 };
412 
413 static const struct constant_table erofs_dax_param_enums[] = {
414 	{"always",	EROFS_MOUNT_DAX_ALWAYS},
415 	{"never",	EROFS_MOUNT_DAX_NEVER},
416 	{}
417 };
418 
419 static const struct fs_parameter_spec erofs_fs_parameters[] = {
420 	fsparam_flag_no("user_xattr",	Opt_user_xattr),
421 	fsparam_flag_no("acl",		Opt_acl),
422 	fsparam_enum("cache_strategy",	Opt_cache_strategy,
423 		     erofs_param_cache_strategy),
424 	fsparam_flag("dax",             Opt_dax),
425 	fsparam_enum("dax",		Opt_dax_enum, erofs_dax_param_enums),
426 	fsparam_string("device",	Opt_device),
427 	{}
428 };
429 
430 static bool erofs_fc_set_dax_mode(struct fs_context *fc, unsigned int mode)
431 {
432 #ifdef CONFIG_FS_DAX
433 	struct erofs_fs_context *ctx = fc->fs_private;
434 
435 	switch (mode) {
436 	case EROFS_MOUNT_DAX_ALWAYS:
437 		warnfc(fc, "DAX enabled. Warning: EXPERIMENTAL, use at your own risk");
438 		set_opt(&ctx->opt, DAX_ALWAYS);
439 		clear_opt(&ctx->opt, DAX_NEVER);
440 		return true;
441 	case EROFS_MOUNT_DAX_NEVER:
442 		set_opt(&ctx->opt, DAX_NEVER);
443 		clear_opt(&ctx->opt, DAX_ALWAYS);
444 		return true;
445 	default:
446 		DBG_BUGON(1);
447 		return false;
448 	}
449 #else
450 	errorfc(fc, "dax options not supported");
451 	return false;
452 #endif
453 }
454 
455 static int erofs_fc_parse_param(struct fs_context *fc,
456 				struct fs_parameter *param)
457 {
458 	struct erofs_fs_context *ctx = fc->fs_private;
459 	struct fs_parse_result result;
460 	struct erofs_device_info *dif;
461 	int opt, ret;
462 
463 	opt = fs_parse(fc, erofs_fs_parameters, param, &result);
464 	if (opt < 0)
465 		return opt;
466 
467 	switch (opt) {
468 	case Opt_user_xattr:
469 #ifdef CONFIG_EROFS_FS_XATTR
470 		if (result.boolean)
471 			set_opt(&ctx->opt, XATTR_USER);
472 		else
473 			clear_opt(&ctx->opt, XATTR_USER);
474 #else
475 		errorfc(fc, "{,no}user_xattr options not supported");
476 #endif
477 		break;
478 	case Opt_acl:
479 #ifdef CONFIG_EROFS_FS_POSIX_ACL
480 		if (result.boolean)
481 			set_opt(&ctx->opt, POSIX_ACL);
482 		else
483 			clear_opt(&ctx->opt, POSIX_ACL);
484 #else
485 		errorfc(fc, "{,no}acl options not supported");
486 #endif
487 		break;
488 	case Opt_cache_strategy:
489 #ifdef CONFIG_EROFS_FS_ZIP
490 		ctx->opt.cache_strategy = result.uint_32;
491 #else
492 		errorfc(fc, "compression not supported, cache_strategy ignored");
493 #endif
494 		break;
495 	case Opt_dax:
496 		if (!erofs_fc_set_dax_mode(fc, EROFS_MOUNT_DAX_ALWAYS))
497 			return -EINVAL;
498 		break;
499 	case Opt_dax_enum:
500 		if (!erofs_fc_set_dax_mode(fc, result.uint_32))
501 			return -EINVAL;
502 		break;
503 	case Opt_device:
504 		dif = kzalloc(sizeof(*dif), GFP_KERNEL);
505 		if (!dif)
506 			return -ENOMEM;
507 		dif->path = kstrdup(param->string, GFP_KERNEL);
508 		if (!dif->path) {
509 			kfree(dif);
510 			return -ENOMEM;
511 		}
512 		down_write(&ctx->devs->rwsem);
513 		ret = idr_alloc(&ctx->devs->tree, dif, 0, 0, GFP_KERNEL);
514 		up_write(&ctx->devs->rwsem);
515 		if (ret < 0) {
516 			kfree(dif->path);
517 			kfree(dif);
518 			return ret;
519 		}
520 		++ctx->devs->extra_devices;
521 		break;
522 	default:
523 		return -ENOPARAM;
524 	}
525 	return 0;
526 }
527 
528 #ifdef CONFIG_EROFS_FS_ZIP
529 static const struct address_space_operations managed_cache_aops;
530 
531 static int erofs_managed_cache_releasepage(struct page *page, gfp_t gfp_mask)
532 {
533 	int ret = 1;	/* 0 - busy */
534 	struct address_space *const mapping = page->mapping;
535 
536 	DBG_BUGON(!PageLocked(page));
537 	DBG_BUGON(mapping->a_ops != &managed_cache_aops);
538 
539 	if (PagePrivate(page))
540 		ret = erofs_try_to_free_cached_page(page);
541 
542 	return ret;
543 }
544 
545 /*
546  * It will be called only on inode eviction. In case that there are still some
547  * decompression requests in progress, wait with rescheduling for a bit here.
548  * We could introduce an extra locking instead but it seems unnecessary.
549  */
550 static void erofs_managed_cache_invalidate_folio(struct folio *folio,
551 					       size_t offset, size_t length)
552 {
553 	const size_t stop = length + offset;
554 
555 	DBG_BUGON(!folio_test_locked(folio));
556 
557 	/* Check for potential overflow in debug mode */
558 	DBG_BUGON(stop > folio_size(folio) || stop < length);
559 
560 	if (offset == 0 && stop == folio_size(folio))
561 		while (!erofs_managed_cache_releasepage(&folio->page, GFP_NOFS))
562 			cond_resched();
563 }
564 
565 static const struct address_space_operations managed_cache_aops = {
566 	.releasepage = erofs_managed_cache_releasepage,
567 	.invalidate_folio = erofs_managed_cache_invalidate_folio,
568 };
569 
570 static int erofs_init_managed_cache(struct super_block *sb)
571 {
572 	struct erofs_sb_info *const sbi = EROFS_SB(sb);
573 	struct inode *const inode = new_inode(sb);
574 
575 	if (!inode)
576 		return -ENOMEM;
577 
578 	set_nlink(inode, 1);
579 	inode->i_size = OFFSET_MAX;
580 
581 	inode->i_mapping->a_ops = &managed_cache_aops;
582 	mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
583 	sbi->managed_cache = inode;
584 	return 0;
585 }
586 #else
587 static int erofs_init_managed_cache(struct super_block *sb) { return 0; }
588 #endif
589 
590 static struct inode *erofs_nfs_get_inode(struct super_block *sb,
591 					 u64 ino, u32 generation)
592 {
593 	return erofs_iget(sb, ino, false);
594 }
595 
596 static struct dentry *erofs_fh_to_dentry(struct super_block *sb,
597 		struct fid *fid, int fh_len, int fh_type)
598 {
599 	return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
600 				    erofs_nfs_get_inode);
601 }
602 
603 static struct dentry *erofs_fh_to_parent(struct super_block *sb,
604 		struct fid *fid, int fh_len, int fh_type)
605 {
606 	return generic_fh_to_parent(sb, fid, fh_len, fh_type,
607 				    erofs_nfs_get_inode);
608 }
609 
610 static struct dentry *erofs_get_parent(struct dentry *child)
611 {
612 	erofs_nid_t nid;
613 	unsigned int d_type;
614 	int err;
615 
616 	err = erofs_namei(d_inode(child), &dotdot_name, &nid, &d_type);
617 	if (err)
618 		return ERR_PTR(err);
619 	return d_obtain_alias(erofs_iget(child->d_sb, nid, d_type == FT_DIR));
620 }
621 
622 static const struct export_operations erofs_export_ops = {
623 	.fh_to_dentry = erofs_fh_to_dentry,
624 	.fh_to_parent = erofs_fh_to_parent,
625 	.get_parent = erofs_get_parent,
626 };
627 
628 static int erofs_fc_fill_super(struct super_block *sb, struct fs_context *fc)
629 {
630 	struct inode *inode;
631 	struct erofs_sb_info *sbi;
632 	struct erofs_fs_context *ctx = fc->fs_private;
633 	int err;
634 
635 	sb->s_magic = EROFS_SUPER_MAGIC;
636 	sb->s_flags |= SB_RDONLY | SB_NOATIME;
637 	sb->s_maxbytes = MAX_LFS_FILESIZE;
638 	sb->s_op = &erofs_sops;
639 
640 	sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
641 	if (!sbi)
642 		return -ENOMEM;
643 
644 	sb->s_fs_info = sbi;
645 	sbi->opt = ctx->opt;
646 	sbi->devs = ctx->devs;
647 	ctx->devs = NULL;
648 
649 	if (erofs_is_fscache_mode(sb)) {
650 		sb->s_blocksize = EROFS_BLKSIZ;
651 		sb->s_blocksize_bits = LOG_BLOCK_SIZE;
652 
653 		err = erofs_fscache_register_fs(sb);
654 		if (err)
655 			return err;
656 
657 		err = erofs_fscache_register_cookie(sb, &sbi->s_fscache,
658 						    sbi->opt.fsid, true);
659 		if (err)
660 			return err;
661 
662 		err = super_setup_bdi(sb);
663 		if (err)
664 			return err;
665 	} else {
666 		if (!sb_set_blocksize(sb, EROFS_BLKSIZ)) {
667 			erofs_err(sb, "failed to set erofs blksize");
668 			return -EINVAL;
669 		}
670 
671 		sbi->dax_dev = fs_dax_get_by_bdev(sb->s_bdev,
672 						  &sbi->dax_part_off);
673 	}
674 
675 	err = erofs_read_superblock(sb);
676 	if (err)
677 		return err;
678 
679 	if (test_opt(&sbi->opt, DAX_ALWAYS)) {
680 		BUILD_BUG_ON(EROFS_BLKSIZ != PAGE_SIZE);
681 
682 		if (!sbi->dax_dev) {
683 			errorfc(fc, "DAX unsupported by block device. Turning off DAX.");
684 			clear_opt(&sbi->opt, DAX_ALWAYS);
685 		}
686 	}
687 
688 	sb->s_time_gran = 1;
689 	sb->s_xattr = erofs_xattr_handlers;
690 	sb->s_export_op = &erofs_export_ops;
691 
692 	if (test_opt(&sbi->opt, POSIX_ACL))
693 		sb->s_flags |= SB_POSIXACL;
694 	else
695 		sb->s_flags &= ~SB_POSIXACL;
696 
697 #ifdef CONFIG_EROFS_FS_ZIP
698 	xa_init(&sbi->managed_pslots);
699 #endif
700 
701 	/* get the root inode */
702 	inode = erofs_iget(sb, ROOT_NID(sbi), true);
703 	if (IS_ERR(inode))
704 		return PTR_ERR(inode);
705 
706 	if (!S_ISDIR(inode->i_mode)) {
707 		erofs_err(sb, "rootino(nid %llu) is not a directory(i_mode %o)",
708 			  ROOT_NID(sbi), inode->i_mode);
709 		iput(inode);
710 		return -EINVAL;
711 	}
712 
713 	sb->s_root = d_make_root(inode);
714 	if (!sb->s_root)
715 		return -ENOMEM;
716 
717 	erofs_shrinker_register(sb);
718 	/* sb->s_umount is already locked, SB_ACTIVE and SB_BORN are not set */
719 	err = erofs_init_managed_cache(sb);
720 	if (err)
721 		return err;
722 
723 	err = erofs_register_sysfs(sb);
724 	if (err)
725 		return err;
726 
727 	erofs_info(sb, "mounted with root inode @ nid %llu.", ROOT_NID(sbi));
728 	return 0;
729 }
730 
731 static int erofs_fc_get_tree(struct fs_context *fc)
732 {
733 	return get_tree_bdev(fc, erofs_fc_fill_super);
734 }
735 
736 static int erofs_fc_reconfigure(struct fs_context *fc)
737 {
738 	struct super_block *sb = fc->root->d_sb;
739 	struct erofs_sb_info *sbi = EROFS_SB(sb);
740 	struct erofs_fs_context *ctx = fc->fs_private;
741 
742 	DBG_BUGON(!sb_rdonly(sb));
743 
744 	if (test_opt(&ctx->opt, POSIX_ACL))
745 		fc->sb_flags |= SB_POSIXACL;
746 	else
747 		fc->sb_flags &= ~SB_POSIXACL;
748 
749 	sbi->opt = ctx->opt;
750 
751 	fc->sb_flags |= SB_RDONLY;
752 	return 0;
753 }
754 
755 static int erofs_release_device_info(int id, void *ptr, void *data)
756 {
757 	struct erofs_device_info *dif = ptr;
758 
759 	fs_put_dax(dif->dax_dev);
760 	if (dif->bdev)
761 		blkdev_put(dif->bdev, FMODE_READ | FMODE_EXCL);
762 	erofs_fscache_unregister_cookie(&dif->fscache);
763 	kfree(dif->path);
764 	kfree(dif);
765 	return 0;
766 }
767 
768 static void erofs_free_dev_context(struct erofs_dev_context *devs)
769 {
770 	if (!devs)
771 		return;
772 	idr_for_each(&devs->tree, &erofs_release_device_info, NULL);
773 	idr_destroy(&devs->tree);
774 	kfree(devs);
775 }
776 
777 static void erofs_fc_free(struct fs_context *fc)
778 {
779 	struct erofs_fs_context *ctx = fc->fs_private;
780 
781 	erofs_free_dev_context(ctx->devs);
782 	kfree(ctx);
783 }
784 
785 static const struct fs_context_operations erofs_context_ops = {
786 	.parse_param	= erofs_fc_parse_param,
787 	.get_tree       = erofs_fc_get_tree,
788 	.reconfigure    = erofs_fc_reconfigure,
789 	.free		= erofs_fc_free,
790 };
791 
792 static int erofs_init_fs_context(struct fs_context *fc)
793 {
794 	struct erofs_fs_context *ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
795 
796 	if (!ctx)
797 		return -ENOMEM;
798 	ctx->devs = kzalloc(sizeof(struct erofs_dev_context), GFP_KERNEL);
799 	if (!ctx->devs) {
800 		kfree(ctx);
801 		return -ENOMEM;
802 	}
803 	fc->fs_private = ctx;
804 
805 	idr_init(&ctx->devs->tree);
806 	init_rwsem(&ctx->devs->rwsem);
807 	erofs_default_options(ctx);
808 	fc->ops = &erofs_context_ops;
809 	return 0;
810 }
811 
812 /*
813  * could be triggered after deactivate_locked_super()
814  * is called, thus including umount and failed to initialize.
815  */
816 static void erofs_kill_sb(struct super_block *sb)
817 {
818 	struct erofs_sb_info *sbi;
819 
820 	WARN_ON(sb->s_magic != EROFS_SUPER_MAGIC);
821 
822 	kill_block_super(sb);
823 
824 	sbi = EROFS_SB(sb);
825 	if (!sbi)
826 		return;
827 
828 	erofs_free_dev_context(sbi->devs);
829 	fs_put_dax(sbi->dax_dev);
830 	erofs_fscache_unregister_cookie(&sbi->s_fscache);
831 	erofs_fscache_unregister_fs(sb);
832 	kfree(sbi);
833 	sb->s_fs_info = NULL;
834 }
835 
836 /* called when ->s_root is non-NULL */
837 static void erofs_put_super(struct super_block *sb)
838 {
839 	struct erofs_sb_info *const sbi = EROFS_SB(sb);
840 
841 	DBG_BUGON(!sbi);
842 
843 	erofs_unregister_sysfs(sb);
844 	erofs_shrinker_unregister(sb);
845 #ifdef CONFIG_EROFS_FS_ZIP
846 	iput(sbi->managed_cache);
847 	sbi->managed_cache = NULL;
848 #endif
849 	erofs_fscache_unregister_cookie(&sbi->s_fscache);
850 }
851 
852 static struct file_system_type erofs_fs_type = {
853 	.owner          = THIS_MODULE,
854 	.name           = "erofs",
855 	.init_fs_context = erofs_init_fs_context,
856 	.kill_sb        = erofs_kill_sb,
857 	.fs_flags       = FS_REQUIRES_DEV | FS_ALLOW_IDMAP,
858 };
859 MODULE_ALIAS_FS("erofs");
860 
861 static int __init erofs_module_init(void)
862 {
863 	int err;
864 
865 	erofs_check_ondisk_layout_definitions();
866 
867 	erofs_inode_cachep = kmem_cache_create("erofs_inode",
868 					       sizeof(struct erofs_inode), 0,
869 					       SLAB_RECLAIM_ACCOUNT,
870 					       erofs_inode_init_once);
871 	if (!erofs_inode_cachep) {
872 		err = -ENOMEM;
873 		goto icache_err;
874 	}
875 
876 	err = erofs_init_shrinker();
877 	if (err)
878 		goto shrinker_err;
879 
880 	err = z_erofs_lzma_init();
881 	if (err)
882 		goto lzma_err;
883 
884 	erofs_pcpubuf_init();
885 	err = z_erofs_init_zip_subsystem();
886 	if (err)
887 		goto zip_err;
888 
889 	err = erofs_init_sysfs();
890 	if (err)
891 		goto sysfs_err;
892 
893 	err = register_filesystem(&erofs_fs_type);
894 	if (err)
895 		goto fs_err;
896 
897 	return 0;
898 
899 fs_err:
900 	erofs_exit_sysfs();
901 sysfs_err:
902 	z_erofs_exit_zip_subsystem();
903 zip_err:
904 	z_erofs_lzma_exit();
905 lzma_err:
906 	erofs_exit_shrinker();
907 shrinker_err:
908 	kmem_cache_destroy(erofs_inode_cachep);
909 icache_err:
910 	return err;
911 }
912 
913 static void __exit erofs_module_exit(void)
914 {
915 	unregister_filesystem(&erofs_fs_type);
916 
917 	/* Ensure all RCU free inodes / pclusters are safe to be destroyed. */
918 	rcu_barrier();
919 
920 	erofs_exit_sysfs();
921 	z_erofs_exit_zip_subsystem();
922 	z_erofs_lzma_exit();
923 	erofs_exit_shrinker();
924 	kmem_cache_destroy(erofs_inode_cachep);
925 	erofs_pcpubuf_exit();
926 }
927 
928 /* get filesystem statistics */
929 static int erofs_statfs(struct dentry *dentry, struct kstatfs *buf)
930 {
931 	struct super_block *sb = dentry->d_sb;
932 	struct erofs_sb_info *sbi = EROFS_SB(sb);
933 	u64 id = 0;
934 
935 	if (!erofs_is_fscache_mode(sb))
936 		id = huge_encode_dev(sb->s_bdev->bd_dev);
937 
938 	buf->f_type = sb->s_magic;
939 	buf->f_bsize = EROFS_BLKSIZ;
940 	buf->f_blocks = sbi->total_blocks;
941 	buf->f_bfree = buf->f_bavail = 0;
942 
943 	buf->f_files = ULLONG_MAX;
944 	buf->f_ffree = ULLONG_MAX - sbi->inos;
945 
946 	buf->f_namelen = EROFS_NAME_LEN;
947 
948 	buf->f_fsid    = u64_to_fsid(id);
949 	return 0;
950 }
951 
952 static int erofs_show_options(struct seq_file *seq, struct dentry *root)
953 {
954 	struct erofs_sb_info *sbi = EROFS_SB(root->d_sb);
955 	struct erofs_mount_opts *opt = &sbi->opt;
956 
957 #ifdef CONFIG_EROFS_FS_XATTR
958 	if (test_opt(opt, XATTR_USER))
959 		seq_puts(seq, ",user_xattr");
960 	else
961 		seq_puts(seq, ",nouser_xattr");
962 #endif
963 #ifdef CONFIG_EROFS_FS_POSIX_ACL
964 	if (test_opt(opt, POSIX_ACL))
965 		seq_puts(seq, ",acl");
966 	else
967 		seq_puts(seq, ",noacl");
968 #endif
969 #ifdef CONFIG_EROFS_FS_ZIP
970 	if (opt->cache_strategy == EROFS_ZIP_CACHE_DISABLED)
971 		seq_puts(seq, ",cache_strategy=disabled");
972 	else if (opt->cache_strategy == EROFS_ZIP_CACHE_READAHEAD)
973 		seq_puts(seq, ",cache_strategy=readahead");
974 	else if (opt->cache_strategy == EROFS_ZIP_CACHE_READAROUND)
975 		seq_puts(seq, ",cache_strategy=readaround");
976 #endif
977 	if (test_opt(opt, DAX_ALWAYS))
978 		seq_puts(seq, ",dax=always");
979 	if (test_opt(opt, DAX_NEVER))
980 		seq_puts(seq, ",dax=never");
981 	return 0;
982 }
983 
984 const struct super_operations erofs_sops = {
985 	.put_super = erofs_put_super,
986 	.alloc_inode = erofs_alloc_inode,
987 	.free_inode = erofs_free_inode,
988 	.statfs = erofs_statfs,
989 	.show_options = erofs_show_options,
990 };
991 
992 module_init(erofs_module_init);
993 module_exit(erofs_module_exit);
994 
995 MODULE_DESCRIPTION("Enhanced ROM File System");
996 MODULE_AUTHOR("Gao Xiang, Chao Yu, Miao Xie, CONSUMER BG, HUAWEI Inc.");
997 MODULE_LICENSE("GPL");
998