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