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