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