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