xref: /linux/fs/erofs/super.c (revision 5928397f5739fb94559350575826d94fa8c35929)
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 
_erofs_printk(struct super_block * sb,const char * fmt,...)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 
erofs_superblock_csum_verify(struct super_block * sb,void * sbdata)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 
erofs_inode_init_once(void * ptr)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 
erofs_alloc_inode(struct super_block * sb)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 
erofs_free_inode(struct inode * inode)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 */
erofs_read_metadata(struct super_block * sb,struct erofs_buf * buf,erofs_off_t * offset,int * lengthp)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
z_erofs_parse_cfgs(struct super_block * sb,struct erofs_super_block * dsb)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 
erofs_init_device(struct erofs_buf * buf,struct super_block * sb,struct erofs_device_info * dif,erofs_off_t * pos)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, false);
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 			if (file == ERR_PTR(-ENOTBLK))
170 				return -EINVAL;
171 			return PTR_ERR(file);
172 		}
173 
174 		if (!erofs_is_fileio_mode(sbi)) {
175 			dif->dax_dev = fs_dax_get_by_bdev(file_bdev(file),
176 					&dif->dax_part_off, NULL, NULL);
177 			if (!dif->dax_dev && test_opt(&sbi->opt, DAX_ALWAYS)) {
178 				erofs_info(sb, "DAX unsupported by %s. Turning off DAX.",
179 					   dif->path);
180 				clear_opt(&sbi->opt, DAX_ALWAYS);
181 			}
182 		} else if (!S_ISREG(file_inode(file)->i_mode)) {
183 			fput(file);
184 			return -EINVAL;
185 		}
186 		dif->file = file;
187 	}
188 
189 	dif->blocks = le32_to_cpu(dis->blocks_lo);
190 	dif->uniaddr = le32_to_cpu(dis->uniaddr_lo);
191 	sbi->total_blocks += dif->blocks;
192 	*pos += EROFS_DEVT_SLOT_SIZE;
193 	return 0;
194 }
195 
erofs_scan_devices(struct super_block * sb,struct erofs_super_block * dsb)196 static int erofs_scan_devices(struct super_block *sb,
197 			      struct erofs_super_block *dsb)
198 {
199 	struct erofs_sb_info *sbi = EROFS_SB(sb);
200 	unsigned int ondisk_extradevs;
201 	erofs_off_t pos;
202 	struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
203 	struct erofs_device_info *dif;
204 	int id, err = 0;
205 
206 	sbi->total_blocks = sbi->dif0.blocks;
207 	if (!erofs_sb_has_device_table(sbi))
208 		ondisk_extradevs = 0;
209 	else
210 		ondisk_extradevs = le16_to_cpu(dsb->extra_devices);
211 
212 	if (sbi->devs->extra_devices &&
213 	    ondisk_extradevs != sbi->devs->extra_devices) {
214 		erofs_err(sb, "extra devices don't match (ondisk %u, given %u)",
215 			  ondisk_extradevs, sbi->devs->extra_devices);
216 		return -EINVAL;
217 	}
218 	if (!ondisk_extradevs) {
219 		if (test_opt(&sbi->opt, DAX_ALWAYS) && !sbi->dif0.dax_dev) {
220 			erofs_info(sb, "DAX unsupported by block device. Turning off DAX.");
221 			clear_opt(&sbi->opt, DAX_ALWAYS);
222 		}
223 		return 0;
224 	}
225 
226 	if (!sbi->devs->extra_devices && !erofs_is_fscache_mode(sb))
227 		sbi->devs->flatdev = true;
228 
229 	sbi->device_id_mask = roundup_pow_of_two(ondisk_extradevs + 1) - 1;
230 	pos = le16_to_cpu(dsb->devt_slotoff) * EROFS_DEVT_SLOT_SIZE;
231 	down_read(&sbi->devs->rwsem);
232 	if (sbi->devs->extra_devices) {
233 		idr_for_each_entry(&sbi->devs->tree, dif, id) {
234 			err = erofs_init_device(&buf, sb, dif, &pos);
235 			if (err)
236 				break;
237 		}
238 	} else {
239 		for (id = 0; id < ondisk_extradevs; id++) {
240 			dif = kzalloc(sizeof(*dif), GFP_KERNEL);
241 			if (!dif) {
242 				err = -ENOMEM;
243 				break;
244 			}
245 
246 			err = idr_alloc(&sbi->devs->tree, dif, 0, 0, GFP_KERNEL);
247 			if (err < 0) {
248 				kfree(dif);
249 				break;
250 			}
251 			++sbi->devs->extra_devices;
252 
253 			err = erofs_init_device(&buf, sb, dif, &pos);
254 			if (err)
255 				break;
256 		}
257 	}
258 	up_read(&sbi->devs->rwsem);
259 	erofs_put_metabuf(&buf);
260 	return err;
261 }
262 
erofs_read_superblock(struct super_block * sb)263 static int erofs_read_superblock(struct super_block *sb)
264 {
265 	struct erofs_sb_info *sbi = EROFS_SB(sb);
266 	struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
267 	struct erofs_super_block *dsb;
268 	void *data;
269 	int ret;
270 
271 	data = erofs_read_metabuf(&buf, sb, 0, false);
272 	if (IS_ERR(data)) {
273 		erofs_err(sb, "cannot read erofs superblock");
274 		return PTR_ERR(data);
275 	}
276 
277 	dsb = (struct erofs_super_block *)(data + EROFS_SUPER_OFFSET);
278 	ret = -EINVAL;
279 	if (le32_to_cpu(dsb->magic) != EROFS_SUPER_MAGIC_V1) {
280 		erofs_err(sb, "cannot find valid erofs superblock");
281 		goto out;
282 	}
283 
284 	sbi->blkszbits = dsb->blkszbits;
285 	if (sbi->blkszbits < 9 || sbi->blkszbits > PAGE_SHIFT) {
286 		erofs_err(sb, "blkszbits %u isn't supported", sbi->blkszbits);
287 		goto out;
288 	}
289 	if (dsb->dirblkbits) {
290 		erofs_err(sb, "dirblkbits %u isn't supported", dsb->dirblkbits);
291 		goto out;
292 	}
293 
294 	sbi->feature_compat = le32_to_cpu(dsb->feature_compat);
295 	if (erofs_sb_has_sb_chksum(sbi)) {
296 		ret = erofs_superblock_csum_verify(sb, data);
297 		if (ret)
298 			goto out;
299 	}
300 
301 	ret = -EINVAL;
302 	sbi->feature_incompat = le32_to_cpu(dsb->feature_incompat);
303 	if (sbi->feature_incompat & ~EROFS_ALL_FEATURE_INCOMPAT) {
304 		erofs_err(sb, "unidentified incompatible feature %x, please upgrade kernel",
305 			  sbi->feature_incompat & ~EROFS_ALL_FEATURE_INCOMPAT);
306 		goto out;
307 	}
308 
309 	sbi->sb_size = 128 + dsb->sb_extslots * EROFS_SB_EXTSLOT_SIZE;
310 	if (sbi->sb_size > PAGE_SIZE - EROFS_SUPER_OFFSET) {
311 		erofs_err(sb, "invalid sb_extslots %u (more than a fs block)",
312 			  sbi->sb_size);
313 		goto out;
314 	}
315 	sbi->dif0.blocks = le32_to_cpu(dsb->blocks_lo);
316 	sbi->meta_blkaddr = le32_to_cpu(dsb->meta_blkaddr);
317 #ifdef CONFIG_EROFS_FS_XATTR
318 	sbi->xattr_blkaddr = le32_to_cpu(dsb->xattr_blkaddr);
319 	sbi->xattr_prefix_start = le32_to_cpu(dsb->xattr_prefix_start);
320 	sbi->xattr_prefix_count = dsb->xattr_prefix_count;
321 	sbi->xattr_filter_reserved = dsb->xattr_filter_reserved;
322 #endif
323 	sbi->islotbits = ilog2(sizeof(struct erofs_inode_compact));
324 	if (erofs_sb_has_48bit(sbi) && dsb->rootnid_8b) {
325 		sbi->root_nid = le64_to_cpu(dsb->rootnid_8b);
326 		sbi->dif0.blocks = sbi->dif0.blocks |
327 				((u64)le16_to_cpu(dsb->rb.blocks_hi) << 32);
328 	} else {
329 		sbi->root_nid = le16_to_cpu(dsb->rb.rootnid_2b);
330 	}
331 	sbi->packed_nid = le64_to_cpu(dsb->packed_nid);
332 	if (erofs_sb_has_metabox(sbi)) {
333 		if (sbi->sb_size <= offsetof(struct erofs_super_block,
334 					     metabox_nid))
335 			return -EFSCORRUPTED;
336 		sbi->metabox_nid = le64_to_cpu(dsb->metabox_nid);
337 		if (sbi->metabox_nid & BIT_ULL(EROFS_DIRENT_NID_METABOX_BIT))
338 			return -EFSCORRUPTED;	/* self-loop detection */
339 	}
340 	sbi->inos = le64_to_cpu(dsb->inos);
341 
342 	sbi->epoch = (s64)le64_to_cpu(dsb->epoch);
343 	sbi->fixed_nsec = le32_to_cpu(dsb->fixed_nsec);
344 	super_set_uuid(sb, (void *)dsb->uuid, sizeof(dsb->uuid));
345 
346 	if (dsb->volume_name[0]) {
347 		sbi->volume_name = kstrndup(dsb->volume_name,
348 					    sizeof(dsb->volume_name), GFP_KERNEL);
349 		if (!sbi->volume_name)
350 			return -ENOMEM;
351 	}
352 
353 	/* parse on-disk compression configurations */
354 	ret = z_erofs_parse_cfgs(sb, dsb);
355 	if (ret < 0)
356 		goto out;
357 
358 	ret = erofs_scan_devices(sb, dsb);
359 
360 	if (erofs_sb_has_48bit(sbi))
361 		erofs_info(sb, "EXPERIMENTAL 48-bit layout support in use. Use at your own risk!");
362 	if (erofs_sb_has_metabox(sbi))
363 		erofs_info(sb, "EXPERIMENTAL metadata compression support in use. Use at your own risk!");
364 	if (erofs_is_fscache_mode(sb))
365 		erofs_info(sb, "[deprecated] fscache-based on-demand read feature in use. Use at your own risk!");
366 out:
367 	erofs_put_metabuf(&buf);
368 	return ret;
369 }
370 
erofs_default_options(struct erofs_sb_info * sbi)371 static void erofs_default_options(struct erofs_sb_info *sbi)
372 {
373 #ifdef CONFIG_EROFS_FS_ZIP
374 	sbi->opt.cache_strategy = EROFS_ZIP_CACHE_READAROUND;
375 	sbi->opt.max_sync_decompress_pages = 3;
376 	sbi->opt.sync_decompress = EROFS_SYNC_DECOMPRESS_AUTO;
377 #endif
378 #ifdef CONFIG_EROFS_FS_XATTR
379 	set_opt(&sbi->opt, XATTR_USER);
380 #endif
381 #ifdef CONFIG_EROFS_FS_POSIX_ACL
382 	set_opt(&sbi->opt, POSIX_ACL);
383 #endif
384 }
385 
386 enum {
387 	Opt_user_xattr, Opt_acl, Opt_cache_strategy, Opt_dax, Opt_dax_enum,
388 	Opt_device, Opt_fsid, Opt_domain_id, Opt_directio, Opt_fsoffset,
389 };
390 
391 static const struct constant_table erofs_param_cache_strategy[] = {
392 	{"disabled",	EROFS_ZIP_CACHE_DISABLED},
393 	{"readahead",	EROFS_ZIP_CACHE_READAHEAD},
394 	{"readaround",	EROFS_ZIP_CACHE_READAROUND},
395 	{}
396 };
397 
398 static const struct constant_table erofs_dax_param_enums[] = {
399 	{"always",	EROFS_MOUNT_DAX_ALWAYS},
400 	{"never",	EROFS_MOUNT_DAX_NEVER},
401 	{}
402 };
403 
404 static const struct fs_parameter_spec erofs_fs_parameters[] = {
405 	fsparam_flag_no("user_xattr",	Opt_user_xattr),
406 	fsparam_flag_no("acl",		Opt_acl),
407 	fsparam_enum("cache_strategy",	Opt_cache_strategy,
408 		     erofs_param_cache_strategy),
409 	fsparam_flag("dax",             Opt_dax),
410 	fsparam_enum("dax",		Opt_dax_enum, erofs_dax_param_enums),
411 	fsparam_string("device",	Opt_device),
412 	fsparam_string("fsid",		Opt_fsid),
413 	fsparam_string("domain_id",	Opt_domain_id),
414 	fsparam_flag_no("directio",	Opt_directio),
415 	fsparam_u64("fsoffset",		Opt_fsoffset),
416 	{}
417 };
418 
erofs_fc_set_dax_mode(struct fs_context * fc,unsigned int mode)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 
erofs_fc_parse_param(struct fs_context * fc,struct fs_parameter * param)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 	case Opt_directio:
530 #ifdef CONFIG_EROFS_FS_BACKED_BY_FILE
531 		if (result.boolean)
532 			set_opt(&sbi->opt, DIRECT_IO);
533 		else
534 			clear_opt(&sbi->opt, DIRECT_IO);
535 #else
536 		errorfc(fc, "%s option not supported", erofs_fs_parameters[opt].name);
537 #endif
538 		break;
539 	case Opt_fsoffset:
540 		sbi->dif0.fsoff = result.uint_64;
541 		break;
542 	}
543 	return 0;
544 }
545 
erofs_encode_fh(struct inode * inode,u32 * fh,int * max_len,struct inode * parent)546 static int erofs_encode_fh(struct inode *inode, u32 *fh, int *max_len,
547 			   struct inode *parent)
548 {
549 	erofs_nid_t nid = EROFS_I(inode)->nid;
550 	int len = parent ? 6 : 3;
551 
552 	if (*max_len < len) {
553 		*max_len = len;
554 		return FILEID_INVALID;
555 	}
556 
557 	fh[0] = (u32)(nid >> 32);
558 	fh[1] = (u32)(nid & 0xffffffff);
559 	fh[2] = inode->i_generation;
560 
561 	if (parent) {
562 		nid = EROFS_I(parent)->nid;
563 
564 		fh[3] = (u32)(nid >> 32);
565 		fh[4] = (u32)(nid & 0xffffffff);
566 		fh[5] = parent->i_generation;
567 	}
568 
569 	*max_len = len;
570 	return parent ? FILEID_INO64_GEN_PARENT : FILEID_INO64_GEN;
571 }
572 
erofs_fh_to_dentry(struct super_block * sb,struct fid * fid,int fh_len,int fh_type)573 static struct dentry *erofs_fh_to_dentry(struct super_block *sb,
574 		struct fid *fid, int fh_len, int fh_type)
575 {
576 	if ((fh_type != FILEID_INO64_GEN &&
577 	     fh_type != FILEID_INO64_GEN_PARENT) || fh_len < 3)
578 		return NULL;
579 
580 	return d_obtain_alias(erofs_iget(sb,
581 		((u64)fid->raw[0] << 32) | fid->raw[1]));
582 }
583 
erofs_fh_to_parent(struct super_block * sb,struct fid * fid,int fh_len,int fh_type)584 static struct dentry *erofs_fh_to_parent(struct super_block *sb,
585 		struct fid *fid, int fh_len, int fh_type)
586 {
587 	if (fh_type != FILEID_INO64_GEN_PARENT || fh_len < 6)
588 		return NULL;
589 
590 	return d_obtain_alias(erofs_iget(sb,
591 		((u64)fid->raw[3] << 32) | fid->raw[4]));
592 }
593 
erofs_get_parent(struct dentry * child)594 static struct dentry *erofs_get_parent(struct dentry *child)
595 {
596 	erofs_nid_t nid;
597 	unsigned int d_type;
598 	int err;
599 
600 	err = erofs_namei(d_inode(child), &dotdot_name, &nid, &d_type);
601 	if (err)
602 		return ERR_PTR(err);
603 	return d_obtain_alias(erofs_iget(child->d_sb, nid));
604 }
605 
606 static const struct export_operations erofs_export_ops = {
607 	.encode_fh = erofs_encode_fh,
608 	.fh_to_dentry = erofs_fh_to_dentry,
609 	.fh_to_parent = erofs_fh_to_parent,
610 	.get_parent = erofs_get_parent,
611 };
612 
erofs_set_sysfs_name(struct super_block * sb)613 static void erofs_set_sysfs_name(struct super_block *sb)
614 {
615 	struct erofs_sb_info *sbi = EROFS_SB(sb);
616 
617 	if (sbi->domain_id)
618 		super_set_sysfs_name_generic(sb, "%s,%s", sbi->domain_id,
619 					     sbi->fsid);
620 	else if (sbi->fsid)
621 		super_set_sysfs_name_generic(sb, "%s", sbi->fsid);
622 	else if (erofs_is_fileio_mode(sbi))
623 		super_set_sysfs_name_generic(sb, "%s",
624 					     bdi_dev_name(sb->s_bdi));
625 	else
626 		super_set_sysfs_name_id(sb);
627 }
628 
erofs_fc_fill_super(struct super_block * sb,struct fs_context * fc)629 static int erofs_fc_fill_super(struct super_block *sb, struct fs_context *fc)
630 {
631 	struct inode *inode;
632 	struct erofs_sb_info *sbi = EROFS_SB(sb);
633 	int err;
634 
635 	sb->s_magic = EROFS_SUPER_MAGIC;
636 	sb->s_flags |= SB_RDONLY | SB_NOATIME;
637 	sb->s_maxbytes = MAX_LFS_FILESIZE;
638 	sb->s_op = &erofs_sops;
639 
640 	sbi->blkszbits = PAGE_SHIFT;
641 	if (!sb->s_bdev) {
642 		sb->s_blocksize = PAGE_SIZE;
643 		sb->s_blocksize_bits = PAGE_SHIFT;
644 
645 		if (erofs_is_fscache_mode(sb)) {
646 			err = erofs_fscache_register_fs(sb);
647 			if (err)
648 				return err;
649 		}
650 		err = super_setup_bdi(sb);
651 		if (err)
652 			return err;
653 	} else {
654 		if (!sb_set_blocksize(sb, PAGE_SIZE)) {
655 			errorfc(fc, "failed to set initial blksize");
656 			return -EINVAL;
657 		}
658 
659 		sbi->dif0.dax_dev = fs_dax_get_by_bdev(sb->s_bdev,
660 				&sbi->dif0.dax_part_off, NULL, NULL);
661 	}
662 
663 	err = erofs_read_superblock(sb);
664 	if (err)
665 		return err;
666 
667 	if (sb->s_blocksize_bits != sbi->blkszbits) {
668 		if (erofs_is_fscache_mode(sb)) {
669 			errorfc(fc, "unsupported blksize for fscache mode");
670 			return -EINVAL;
671 		}
672 
673 		if (erofs_is_fileio_mode(sbi)) {
674 			sb->s_blocksize = 1 << sbi->blkszbits;
675 			sb->s_blocksize_bits = sbi->blkszbits;
676 		} else if (!sb_set_blocksize(sb, 1 << sbi->blkszbits)) {
677 			errorfc(fc, "failed to set erofs blksize");
678 			return -EINVAL;
679 		}
680 	}
681 
682 	if (sbi->dif0.fsoff) {
683 		if (sbi->dif0.fsoff & (sb->s_blocksize - 1))
684 			return invalfc(fc, "fsoffset %llu is not aligned to block size %lu",
685 				       sbi->dif0.fsoff, sb->s_blocksize);
686 		if (erofs_is_fscache_mode(sb))
687 			return invalfc(fc, "cannot use fsoffset in fscache mode");
688 	}
689 
690 	if (test_opt(&sbi->opt, DAX_ALWAYS) && sbi->blkszbits != PAGE_SHIFT) {
691 		erofs_info(sb, "unsupported blocksize for DAX");
692 		clear_opt(&sbi->opt, DAX_ALWAYS);
693 	}
694 
695 	sb->s_time_gran = 1;
696 	sb->s_xattr = erofs_xattr_handlers;
697 	sb->s_export_op = &erofs_export_ops;
698 
699 	if (test_opt(&sbi->opt, POSIX_ACL))
700 		sb->s_flags |= SB_POSIXACL;
701 	else
702 		sb->s_flags &= ~SB_POSIXACL;
703 
704 	err = z_erofs_init_super(sb);
705 	if (err)
706 		return err;
707 
708 	if (erofs_sb_has_fragments(sbi) && sbi->packed_nid) {
709 		inode = erofs_iget(sb, sbi->packed_nid);
710 		if (IS_ERR(inode))
711 			return PTR_ERR(inode);
712 		sbi->packed_inode = inode;
713 	}
714 	if (erofs_sb_has_metabox(sbi)) {
715 		inode = erofs_iget(sb, sbi->metabox_nid);
716 		if (IS_ERR(inode))
717 			return PTR_ERR(inode);
718 		sbi->metabox_inode = inode;
719 	}
720 
721 	inode = erofs_iget(sb, sbi->root_nid);
722 	if (IS_ERR(inode))
723 		return PTR_ERR(inode);
724 
725 	if (!S_ISDIR(inode->i_mode)) {
726 		erofs_err(sb, "rootino(nid %llu) is not a directory(i_mode %o)",
727 			  sbi->root_nid, inode->i_mode);
728 		iput(inode);
729 		return -EINVAL;
730 	}
731 	sb->s_root = d_make_root(inode);
732 	if (!sb->s_root)
733 		return -ENOMEM;
734 
735 	erofs_shrinker_register(sb);
736 	err = erofs_xattr_prefixes_init(sb);
737 	if (err)
738 		return err;
739 
740 	erofs_set_sysfs_name(sb);
741 	err = erofs_register_sysfs(sb);
742 	if (err)
743 		return err;
744 
745 	sbi->dir_ra_bytes = EROFS_DIR_RA_BYTES;
746 	erofs_info(sb, "mounted with root inode @ nid %llu.", sbi->root_nid);
747 	return 0;
748 }
749 
erofs_fc_get_tree(struct fs_context * fc)750 static int erofs_fc_get_tree(struct fs_context *fc)
751 {
752 	struct erofs_sb_info *sbi = fc->s_fs_info;
753 	int ret;
754 
755 	if (IS_ENABLED(CONFIG_EROFS_FS_ONDEMAND) && sbi->fsid)
756 		return get_tree_nodev(fc, erofs_fc_fill_super);
757 
758 	ret = get_tree_bdev_flags(fc, erofs_fc_fill_super,
759 		IS_ENABLED(CONFIG_EROFS_FS_BACKED_BY_FILE) ?
760 			GET_TREE_BDEV_QUIET_LOOKUP : 0);
761 #ifdef CONFIG_EROFS_FS_BACKED_BY_FILE
762 	if (ret == -ENOTBLK) {
763 		struct file *file;
764 
765 		if (!fc->source)
766 			return invalf(fc, "No source specified");
767 		file = filp_open(fc->source, O_RDONLY | O_LARGEFILE, 0);
768 		if (IS_ERR(file))
769 			return PTR_ERR(file);
770 		sbi->dif0.file = file;
771 
772 		if (S_ISREG(file_inode(sbi->dif0.file)->i_mode) &&
773 		    sbi->dif0.file->f_mapping->a_ops->read_folio)
774 			return get_tree_nodev(fc, erofs_fc_fill_super);
775 	}
776 #endif
777 	return ret;
778 }
779 
erofs_fc_reconfigure(struct fs_context * fc)780 static int erofs_fc_reconfigure(struct fs_context *fc)
781 {
782 	struct super_block *sb = fc->root->d_sb;
783 	struct erofs_sb_info *sbi = EROFS_SB(sb);
784 	struct erofs_sb_info *new_sbi = fc->s_fs_info;
785 
786 	DBG_BUGON(!sb_rdonly(sb));
787 
788 	if (new_sbi->fsid || new_sbi->domain_id)
789 		erofs_info(sb, "ignoring reconfiguration for fsid|domain_id.");
790 
791 	if (test_opt(&new_sbi->opt, POSIX_ACL))
792 		fc->sb_flags |= SB_POSIXACL;
793 	else
794 		fc->sb_flags &= ~SB_POSIXACL;
795 
796 	sbi->opt = new_sbi->opt;
797 
798 	fc->sb_flags |= SB_RDONLY;
799 	return 0;
800 }
801 
erofs_release_device_info(int id,void * ptr,void * data)802 static int erofs_release_device_info(int id, void *ptr, void *data)
803 {
804 	struct erofs_device_info *dif = ptr;
805 
806 	fs_put_dax(dif->dax_dev, NULL);
807 	if (dif->file)
808 		fput(dif->file);
809 	erofs_fscache_unregister_cookie(dif->fscache);
810 	dif->fscache = NULL;
811 	kfree(dif->path);
812 	kfree(dif);
813 	return 0;
814 }
815 
erofs_free_dev_context(struct erofs_dev_context * devs)816 static void erofs_free_dev_context(struct erofs_dev_context *devs)
817 {
818 	if (!devs)
819 		return;
820 	idr_for_each(&devs->tree, &erofs_release_device_info, NULL);
821 	idr_destroy(&devs->tree);
822 	kfree(devs);
823 }
824 
erofs_sb_free(struct erofs_sb_info * sbi)825 static void erofs_sb_free(struct erofs_sb_info *sbi)
826 {
827 	erofs_free_dev_context(sbi->devs);
828 	kfree(sbi->fsid);
829 	kfree(sbi->domain_id);
830 	if (sbi->dif0.file)
831 		fput(sbi->dif0.file);
832 	kfree(sbi->volume_name);
833 	kfree(sbi);
834 }
835 
erofs_fc_free(struct fs_context * fc)836 static void erofs_fc_free(struct fs_context *fc)
837 {
838 	struct erofs_sb_info *sbi = fc->s_fs_info;
839 
840 	if (sbi) /* free here if an error occurs before transferring to sb */
841 		erofs_sb_free(sbi);
842 }
843 
844 static const struct fs_context_operations erofs_context_ops = {
845 	.parse_param	= erofs_fc_parse_param,
846 	.get_tree       = erofs_fc_get_tree,
847 	.reconfigure    = erofs_fc_reconfigure,
848 	.free		= erofs_fc_free,
849 };
850 
erofs_init_fs_context(struct fs_context * fc)851 static int erofs_init_fs_context(struct fs_context *fc)
852 {
853 	struct erofs_sb_info *sbi;
854 
855 	sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
856 	if (!sbi)
857 		return -ENOMEM;
858 
859 	sbi->devs = kzalloc(sizeof(struct erofs_dev_context), GFP_KERNEL);
860 	if (!sbi->devs) {
861 		kfree(sbi);
862 		return -ENOMEM;
863 	}
864 	fc->s_fs_info = sbi;
865 
866 	idr_init(&sbi->devs->tree);
867 	init_rwsem(&sbi->devs->rwsem);
868 	erofs_default_options(sbi);
869 	fc->ops = &erofs_context_ops;
870 	return 0;
871 }
872 
erofs_drop_internal_inodes(struct erofs_sb_info * sbi)873 static void erofs_drop_internal_inodes(struct erofs_sb_info *sbi)
874 {
875 	iput(sbi->packed_inode);
876 	sbi->packed_inode = NULL;
877 	iput(sbi->metabox_inode);
878 	sbi->metabox_inode = NULL;
879 #ifdef CONFIG_EROFS_FS_ZIP
880 	iput(sbi->managed_cache);
881 	sbi->managed_cache = NULL;
882 #endif
883 }
884 
erofs_kill_sb(struct super_block * sb)885 static void erofs_kill_sb(struct super_block *sb)
886 {
887 	struct erofs_sb_info *sbi = EROFS_SB(sb);
888 
889 	if ((IS_ENABLED(CONFIG_EROFS_FS_ONDEMAND) && sbi->fsid) ||
890 	    sbi->dif0.file)
891 		kill_anon_super(sb);
892 	else
893 		kill_block_super(sb);
894 	erofs_drop_internal_inodes(sbi);
895 	fs_put_dax(sbi->dif0.dax_dev, NULL);
896 	erofs_fscache_unregister_fs(sb);
897 	erofs_sb_free(sbi);
898 	sb->s_fs_info = NULL;
899 }
900 
erofs_put_super(struct super_block * sb)901 static void erofs_put_super(struct super_block *sb)
902 {
903 	struct erofs_sb_info *const sbi = EROFS_SB(sb);
904 
905 	erofs_unregister_sysfs(sb);
906 	erofs_shrinker_unregister(sb);
907 	erofs_xattr_prefixes_cleanup(sb);
908 	erofs_drop_internal_inodes(sbi);
909 	erofs_free_dev_context(sbi->devs);
910 	sbi->devs = NULL;
911 	erofs_fscache_unregister_fs(sb);
912 }
913 
914 static struct file_system_type erofs_fs_type = {
915 	.owner          = THIS_MODULE,
916 	.name           = "erofs",
917 	.init_fs_context = erofs_init_fs_context,
918 	.kill_sb        = erofs_kill_sb,
919 	.fs_flags       = FS_REQUIRES_DEV | FS_ALLOW_IDMAP,
920 };
921 MODULE_ALIAS_FS("erofs");
922 
erofs_module_init(void)923 static int __init erofs_module_init(void)
924 {
925 	int err;
926 
927 	erofs_check_ondisk_layout_definitions();
928 
929 	erofs_inode_cachep = kmem_cache_create("erofs_inode",
930 			sizeof(struct erofs_inode), 0,
931 			SLAB_RECLAIM_ACCOUNT | SLAB_ACCOUNT,
932 			erofs_inode_init_once);
933 	if (!erofs_inode_cachep)
934 		return -ENOMEM;
935 
936 	err = erofs_init_shrinker();
937 	if (err)
938 		goto shrinker_err;
939 
940 	err = z_erofs_init_subsystem();
941 	if (err)
942 		goto zip_err;
943 
944 	err = erofs_init_sysfs();
945 	if (err)
946 		goto sysfs_err;
947 
948 	err = register_filesystem(&erofs_fs_type);
949 	if (err)
950 		goto fs_err;
951 
952 	return 0;
953 
954 fs_err:
955 	erofs_exit_sysfs();
956 sysfs_err:
957 	z_erofs_exit_subsystem();
958 zip_err:
959 	erofs_exit_shrinker();
960 shrinker_err:
961 	kmem_cache_destroy(erofs_inode_cachep);
962 	return err;
963 }
964 
erofs_module_exit(void)965 static void __exit erofs_module_exit(void)
966 {
967 	unregister_filesystem(&erofs_fs_type);
968 
969 	/* Ensure all RCU free inodes / pclusters are safe to be destroyed. */
970 	rcu_barrier();
971 
972 	erofs_exit_sysfs();
973 	z_erofs_exit_subsystem();
974 	erofs_exit_shrinker();
975 	kmem_cache_destroy(erofs_inode_cachep);
976 }
977 
erofs_statfs(struct dentry * dentry,struct kstatfs * buf)978 static int erofs_statfs(struct dentry *dentry, struct kstatfs *buf)
979 {
980 	struct super_block *sb = dentry->d_sb;
981 	struct erofs_sb_info *sbi = EROFS_SB(sb);
982 
983 	buf->f_type = sb->s_magic;
984 	buf->f_bsize = sb->s_blocksize;
985 	buf->f_blocks = sbi->total_blocks;
986 	buf->f_bfree = buf->f_bavail = 0;
987 	buf->f_files = ULLONG_MAX;
988 	buf->f_ffree = ULLONG_MAX - sbi->inos;
989 	buf->f_namelen = EROFS_NAME_LEN;
990 
991 	if (uuid_is_null(&sb->s_uuid))
992 		buf->f_fsid = u64_to_fsid(!sb->s_bdev ? 0 :
993 				huge_encode_dev(sb->s_bdev->bd_dev));
994 	else
995 		buf->f_fsid = uuid_to_fsid(sb->s_uuid.b);
996 	return 0;
997 }
998 
erofs_show_options(struct seq_file * seq,struct dentry * root)999 static int erofs_show_options(struct seq_file *seq, struct dentry *root)
1000 {
1001 	struct erofs_sb_info *sbi = EROFS_SB(root->d_sb);
1002 	struct erofs_mount_opts *opt = &sbi->opt;
1003 
1004 	if (IS_ENABLED(CONFIG_EROFS_FS_XATTR))
1005 		seq_puts(seq, test_opt(opt, XATTR_USER) ?
1006 				",user_xattr" : ",nouser_xattr");
1007 	if (IS_ENABLED(CONFIG_EROFS_FS_POSIX_ACL))
1008 		seq_puts(seq, test_opt(opt, POSIX_ACL) ? ",acl" : ",noacl");
1009 	if (IS_ENABLED(CONFIG_EROFS_FS_ZIP))
1010 		seq_printf(seq, ",cache_strategy=%s",
1011 			  erofs_param_cache_strategy[opt->cache_strategy].name);
1012 	if (test_opt(opt, DAX_ALWAYS))
1013 		seq_puts(seq, ",dax=always");
1014 	if (test_opt(opt, DAX_NEVER))
1015 		seq_puts(seq, ",dax=never");
1016 	if (erofs_is_fileio_mode(sbi) && test_opt(opt, DIRECT_IO))
1017 		seq_puts(seq, ",directio");
1018 #ifdef CONFIG_EROFS_FS_ONDEMAND
1019 	if (sbi->fsid)
1020 		seq_printf(seq, ",fsid=%s", sbi->fsid);
1021 	if (sbi->domain_id)
1022 		seq_printf(seq, ",domain_id=%s", sbi->domain_id);
1023 #endif
1024 	if (sbi->dif0.fsoff)
1025 		seq_printf(seq, ",fsoffset=%llu", sbi->dif0.fsoff);
1026 	return 0;
1027 }
1028 
erofs_evict_inode(struct inode * inode)1029 static void erofs_evict_inode(struct inode *inode)
1030 {
1031 #ifdef CONFIG_FS_DAX
1032 	if (IS_DAX(inode))
1033 		dax_break_layout_final(inode);
1034 #endif
1035 
1036 	truncate_inode_pages_final(&inode->i_data);
1037 	clear_inode(inode);
1038 }
1039 
1040 const struct super_operations erofs_sops = {
1041 	.put_super = erofs_put_super,
1042 	.alloc_inode = erofs_alloc_inode,
1043 	.free_inode = erofs_free_inode,
1044 	.evict_inode = erofs_evict_inode,
1045 	.statfs = erofs_statfs,
1046 	.show_options = erofs_show_options,
1047 };
1048 
1049 module_init(erofs_module_init);
1050 module_exit(erofs_module_exit);
1051 
1052 MODULE_DESCRIPTION("Enhanced ROM File System");
1053 MODULE_AUTHOR("Gao Xiang, Chao Yu, Miao Xie, CONSUMER BG, HUAWEI Inc.");
1054 MODULE_LICENSE("GPL");
1055