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