super.c 21.7 KB
Newer Older
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 8 9 10
 */
#include <linux/module.h>
#include <linux/buffer_head.h>
#include <linux/statfs.h>
#include <linux/parser.h>
11
#include <linux/seq_file.h>
12
#include <linux/crc32c.h>
13 14
#include <linux/fs_context.h>
#include <linux/fs_parser.h>
15
#include <linux/dax.h>
16
#include "xattr.h"
17

Chao Yu's avatar
Chao Yu committed
18 19 20
#define CREATE_TRACE_POINTS
#include <trace/events/erofs.h>

21 22
static struct kmem_cache *erofs_inode_cachep __read_mostly;

23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
void _erofs_err(struct super_block *sb, const char *function,
		const char *fmt, ...)
{
	struct va_format vaf;
	va_list args;

	va_start(args, fmt);

	vaf.fmt = fmt;
	vaf.va = &args;

	pr_err("(device %s): %s: %pV", sb->s_id, function, &vaf);
	va_end(args);
}

void _erofs_info(struct super_block *sb, const char *function,
		 const char *fmt, ...)
{
	struct va_format vaf;
	va_list args;

	va_start(args, fmt);

	vaf.fmt = fmt;
	vaf.va = &args;

	pr_info("(device %s): %pV", sb->s_id, &vaf);
	va_end(args);
}

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
static int erofs_superblock_csum_verify(struct super_block *sb, void *sbdata)
{
	struct erofs_super_block *dsb;
	u32 expected_crc, crc;

	dsb = kmemdup(sbdata + EROFS_SUPER_OFFSET,
		      EROFS_BLKSIZ - EROFS_SUPER_OFFSET, GFP_KERNEL);
	if (!dsb)
		return -ENOMEM;

	expected_crc = le32_to_cpu(dsb->checksum);
	dsb->checksum = 0;
	/* to allow for x86 boot sectors and other oddities. */
	crc = crc32c(~0, dsb, EROFS_BLKSIZ - EROFS_SUPER_OFFSET);
	kfree(dsb);

	if (crc != expected_crc) {
		erofs_err(sb, "invalid checksum 0x%08x, 0x%08x expected",
			  crc, expected_crc);
		return -EBADMSG;
	}
	return 0;
}

77
static void erofs_inode_init_once(void *ptr)
78
{
Gao Xiang's avatar
Gao Xiang committed
79
	struct erofs_inode *vi = ptr;
80 81 82 83

	inode_init_once(&vi->vfs_inode);
}

84
static struct inode *erofs_alloc_inode(struct super_block *sb)
85
{
Gao Xiang's avatar
Gao Xiang committed
86
	struct erofs_inode *vi =
87
		alloc_inode_sb(sb, erofs_inode_cachep, GFP_KERNEL);
88

89
	if (!vi)
90 91 92
		return NULL;

	/* zero out everything except vfs_inode */
Gao Xiang's avatar
Gao Xiang committed
93
	memset(vi, 0, offsetof(struct erofs_inode, vfs_inode));
94 95 96
	return &vi->vfs_inode;
}

97
static void erofs_free_inode(struct inode *inode)
98
{
Gao Xiang's avatar
Gao Xiang committed
99
	struct erofs_inode *vi = EROFS_I(inode);
100

101 102
	/* be careful of RCU symlink path */
	if (inode->i_op == &erofs_fast_symlink_iops)
103 104 105 106 107 108
		kfree(inode->i_link);
	kfree(vi->xattr_shared_xattrs);

	kmem_cache_free(erofs_inode_cachep, vi);
}

109
static bool check_layout_compatibility(struct super_block *sb,
110
				       struct erofs_super_block *dsb)
111
{
112
	const unsigned int feature = le32_to_cpu(dsb->feature_incompat);
113

114
	EROFS_SB(sb)->feature_incompat = feature;
115 116

	/* check if current kernel meets all mandatory requirements */
117
	if (feature & (~EROFS_ALL_FEATURE_INCOMPAT)) {
118 119 120
		erofs_err(sb,
			  "unidentified incompatible feature %x, please upgrade kernel version",
			   feature & ~EROFS_ALL_FEATURE_INCOMPAT);
121 122 123 124 125
		return false;
	}
	return true;
}

126 127
#ifdef CONFIG_EROFS_FS_ZIP
/* read variable-sized metadata, offset will be aligned by 4-byte */
128
static void *erofs_read_metadata(struct super_block *sb, struct erofs_buf *buf,
129 130 131 132 133 134
				 erofs_off_t *offset, int *lengthp)
{
	u8 *buffer, *ptr;
	int len, i, cnt;

	*offset = round_up(*offset, 4);
135 136 137
	ptr = erofs_read_metabuf(buf, sb, erofs_blknr(*offset), EROFS_KMAP);
	if (IS_ERR(ptr))
		return ptr;
138 139 140 141 142

	len = le16_to_cpu(*(__le16 *)&ptr[erofs_blkoff(*offset)]);
	if (!len)
		len = U16_MAX + 1;
	buffer = kmalloc(len, GFP_KERNEL);
143 144
	if (!buffer)
		return ERR_PTR(-ENOMEM);
145 146 147 148 149
	*offset += sizeof(__le16);
	*lengthp = len;

	for (i = 0; i < len; i += cnt) {
		cnt = min(EROFS_BLKSIZ - (int)erofs_blkoff(*offset), len - i);
150 151 152 153 154
		ptr = erofs_read_metabuf(buf, sb, erofs_blknr(*offset),
					 EROFS_KMAP);
		if (IS_ERR(ptr)) {
			kfree(buffer);
			return ptr;
155 156 157 158 159 160 161 162 163 164
		}
		memcpy(buffer + i, ptr + erofs_blkoff(*offset), cnt);
		*offset += cnt;
	}
	return buffer;
}

static int erofs_load_compr_cfgs(struct super_block *sb,
				 struct erofs_super_block *dsb)
{
165 166
	struct erofs_sb_info *sbi = EROFS_SB(sb);
	struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
167 168
	unsigned int algs, alg;
	erofs_off_t offset;
169
	int size, ret = 0;
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185

	sbi->available_compr_algs = le16_to_cpu(dsb->u1.available_compr_algs);
	if (sbi->available_compr_algs & ~Z_EROFS_ALL_COMPR_ALGS) {
		erofs_err(sb, "try to load compressed fs with unsupported algorithms %x",
			  sbi->available_compr_algs & ~Z_EROFS_ALL_COMPR_ALGS);
		return -EINVAL;
	}

	offset = EROFS_SUPER_OFFSET + sbi->sb_size;
	alg = 0;
	for (algs = sbi->available_compr_algs; algs; algs >>= 1, ++alg) {
		void *data;

		if (!(algs & 1))
			continue;

186
		data = erofs_read_metadata(sb, &buf, &offset, &size);
187 188
		if (IS_ERR(data)) {
			ret = PTR_ERR(data);
189
			break;
190 191 192 193 194 195
		}

		switch (alg) {
		case Z_EROFS_COMPRESSION_LZ4:
			ret = z_erofs_load_lz4_config(sb, dsb, data, size);
			break;
Gao Xiang's avatar
Gao Xiang committed
196 197 198
		case Z_EROFS_COMPRESSION_LZMA:
			ret = z_erofs_load_lzma_config(sb, dsb, data, size);
			break;
199 200 201 202 203 204
		default:
			DBG_BUGON(1);
			ret = -EFAULT;
		}
		kfree(data);
		if (ret)
205
			break;
206
	}
207
	erofs_put_metabuf(&buf);
208 209 210 211 212 213 214 215 216 217 218 219 220 221
	return ret;
}
#else
static int erofs_load_compr_cfgs(struct super_block *sb,
				 struct erofs_super_block *dsb)
{
	if (dsb->u1.available_compr_algs) {
		erofs_err(sb, "try to load compressed fs when compression is disabled");
		return -EINVAL;
	}
	return 0;
}
#endif

222 223 224 225 226 227
static int erofs_init_devices(struct super_block *sb,
			      struct erofs_super_block *dsb)
{
	struct erofs_sb_info *sbi = EROFS_SB(sb);
	unsigned int ondisk_extradevs;
	erofs_off_t pos;
228
	struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
	struct erofs_device_info *dif;
	struct erofs_deviceslot *dis;
	void *ptr;
	int id, err = 0;

	sbi->total_blocks = sbi->primarydevice_blocks;
	if (!erofs_sb_has_device_table(sbi))
		ondisk_extradevs = 0;
	else
		ondisk_extradevs = le16_to_cpu(dsb->extra_devices);

	if (ondisk_extradevs != sbi->devs->extra_devices) {
		erofs_err(sb, "extra devices don't match (ondisk %u, given %u)",
			  ondisk_extradevs, sbi->devs->extra_devices);
		return -EINVAL;
	}
	if (!ondisk_extradevs)
		return 0;

	sbi->device_id_mask = roundup_pow_of_two(ondisk_extradevs + 1) - 1;
	pos = le16_to_cpu(dsb->devt_slotoff) * EROFS_DEVT_SLOT_SIZE;
	down_read(&sbi->devs->rwsem);
	idr_for_each_entry(&sbi->devs->tree, dif, id) {
		struct block_device *bdev;

254 255 256 257 258
		ptr = erofs_read_metabuf(&buf, sb, erofs_blknr(pos),
					 EROFS_KMAP);
		if (IS_ERR(ptr)) {
			err = PTR_ERR(ptr);
			break;
259 260 261 262 263 264 265 266
		}
		dis = ptr + erofs_blkoff(pos);

		bdev = blkdev_get_by_path(dif->path,
					  FMODE_READ | FMODE_EXCL,
					  sb->s_type);
		if (IS_ERR(bdev)) {
			err = PTR_ERR(bdev);
267
			break;
268 269
		}
		dif->bdev = bdev;
270
		dif->dax_dev = fs_dax_get_by_bdev(bdev, &dif->dax_part_off);
271 272 273 274 275 276
		dif->blocks = le32_to_cpu(dis->blocks);
		dif->mapped_blkaddr = le32_to_cpu(dis->mapped_blkaddr);
		sbi->total_blocks += dif->blocks;
		pos += EROFS_DEVT_SLOT_SIZE;
	}
	up_read(&sbi->devs->rwsem);
277
	erofs_put_metabuf(&buf);
278 279 280
	return err;
}

281
static int erofs_read_superblock(struct super_block *sb)
282 283
{
	struct erofs_sb_info *sbi;
284
	struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
285
	struct erofs_super_block *dsb;
286
	unsigned int blkszbits;
287
	void *data;
288 289
	int ret;

290 291
	data = erofs_read_metabuf(&buf, sb, 0, EROFS_KMAP);
	if (IS_ERR(data)) {
292
		erofs_err(sb, "cannot read erofs superblock");
293
		return PTR_ERR(data);
294 295 296
	}

	sbi = EROFS_SB(sb);
297
	dsb = (struct erofs_super_block *)(data + EROFS_SUPER_OFFSET);
298 299

	ret = -EINVAL;
300
	if (le32_to_cpu(dsb->magic) != EROFS_SUPER_MAGIC_V1) {
301
		erofs_err(sb, "cannot find valid erofs superblock");
302 303 304
		goto out;
	}

305
	sbi->feature_compat = le32_to_cpu(dsb->feature_compat);
306
	if (erofs_sb_has_sb_chksum(sbi)) {
307 308 309 310 311
		ret = erofs_superblock_csum_verify(sb, data);
		if (ret)
			goto out;
	}

312
	ret = -EINVAL;
313
	blkszbits = dsb->blkszbits;
314
	/* 9(512 bytes) + LOG_SECTORS_PER_BLOCK == LOG_BLOCK_SIZE */
315
	if (blkszbits != LOG_BLOCK_SIZE) {
316 317
		erofs_err(sb, "blkszbits %u isn't supported on this platform",
			  blkszbits);
318 319 320
		goto out;
	}

321
	if (!check_layout_compatibility(sb, dsb))
322 323
		goto out;

324 325 326 327 328 329
	sbi->sb_size = 128 + dsb->sb_extslots * EROFS_SB_EXTSLOT_SIZE;
	if (sbi->sb_size > EROFS_BLKSIZ) {
		erofs_err(sb, "invalid sb_extslots %u (more than a fs block)",
			  sbi->sb_size);
		goto out;
	}
330
	sbi->primarydevice_blocks = le32_to_cpu(dsb->blocks);
331
	sbi->meta_blkaddr = le32_to_cpu(dsb->meta_blkaddr);
332
#ifdef CONFIG_EROFS_FS_XATTR
333
	sbi->xattr_blkaddr = le32_to_cpu(dsb->xattr_blkaddr);
334
#endif
335
	sbi->islotbits = ilog2(sizeof(struct erofs_inode_compact));
336 337
	sbi->root_nid = le16_to_cpu(dsb->root_nid);
	sbi->inos = le64_to_cpu(dsb->inos);
338

339 340
	sbi->build_time = le64_to_cpu(dsb->build_time);
	sbi->build_time_nsec = le32_to_cpu(dsb->build_time_nsec);
341

342
	memcpy(&sb->s_uuid, dsb->uuid, sizeof(dsb->uuid));
343

344 345
	ret = strscpy(sbi->volume_name, dsb->volume_name,
		      sizeof(dsb->volume_name));
346
	if (ret < 0) {	/* -E2BIG */
347
		erofs_err(sb, "bad volume name without NIL terminator");
348 349 350
		ret = -EFSCORRUPTED;
		goto out;
	}
351 352

	/* parse on-disk compression configurations */
353 354 355 356
	if (erofs_sb_has_compr_cfgs(sbi))
		ret = erofs_load_compr_cfgs(sb, dsb);
	else
		ret = z_erofs_load_lz4_config(sb, dsb, NULL, 0);
357 358 359 360 361
	if (ret < 0)
		goto out;

	/* handle multiple devices */
	ret = erofs_init_devices(sb, dsb);
362 363 364

	if (erofs_sb_has_ztailpacking(sbi))
		erofs_info(sb, "EXPERIMENTAL compressed inline data feature in use. Use at your own risk!");
365
out:
366
	erofs_put_metabuf(&buf);
367 368 369
	return ret;
}

370
/* set up default EROFS parameters */
371
static void erofs_default_options(struct erofs_fs_context *ctx)
372
{
373
#ifdef CONFIG_EROFS_FS_ZIP
374 375
	ctx->opt.cache_strategy = EROFS_ZIP_CACHE_READAROUND;
	ctx->opt.max_sync_decompress_pages = 3;
376
	ctx->opt.sync_decompress = EROFS_SYNC_DECOMPRESS_AUTO;
377
#endif
378
#ifdef CONFIG_EROFS_FS_XATTR
379
	set_opt(&ctx->opt, XATTR_USER);
380 381
#endif
#ifdef CONFIG_EROFS_FS_POSIX_ACL
382
	set_opt(&ctx->opt, POSIX_ACL);
383
#endif
384 385 386
}

enum {
387 388
	Opt_user_xattr,
	Opt_acl,
389
	Opt_cache_strategy,
390 391
	Opt_dax,
	Opt_dax_enum,
392
	Opt_device,
393 394 395
	Opt_err
};

396 397 398 399 400
static const struct constant_table erofs_param_cache_strategy[] = {
	{"disabled",	EROFS_ZIP_CACHE_DISABLED},
	{"readahead",	EROFS_ZIP_CACHE_READAHEAD},
	{"readaround",	EROFS_ZIP_CACHE_READAROUND},
	{}
401 402
};

403 404 405 406 407 408
static const struct constant_table erofs_dax_param_enums[] = {
	{"always",	EROFS_MOUNT_DAX_ALWAYS},
	{"never",	EROFS_MOUNT_DAX_NEVER},
	{}
};

409 410 411 412 413
static const struct fs_parameter_spec erofs_fs_parameters[] = {
	fsparam_flag_no("user_xattr",	Opt_user_xattr),
	fsparam_flag_no("acl",		Opt_acl),
	fsparam_enum("cache_strategy",	Opt_cache_strategy,
		     erofs_param_cache_strategy),
414 415
	fsparam_flag("dax",             Opt_dax),
	fsparam_enum("dax",		Opt_dax_enum, erofs_dax_param_enums),
416
	fsparam_string("device",	Opt_device),
417 418
	{}
};
419

420 421 422 423 424 425 426 427
static bool erofs_fc_set_dax_mode(struct fs_context *fc, unsigned int mode)
{
#ifdef CONFIG_FS_DAX
	struct erofs_fs_context *ctx = fc->fs_private;

	switch (mode) {
	case EROFS_MOUNT_DAX_ALWAYS:
		warnfc(fc, "DAX enabled. Warning: EXPERIMENTAL, use at your own risk");
428 429
		set_opt(&ctx->opt, DAX_ALWAYS);
		clear_opt(&ctx->opt, DAX_NEVER);
430 431
		return true;
	case EROFS_MOUNT_DAX_NEVER:
432 433
		set_opt(&ctx->opt, DAX_NEVER);
		clear_opt(&ctx->opt, DAX_ALWAYS);
434 435 436 437 438 439 440 441 442 443 444
		return true;
	default:
		DBG_BUGON(1);
		return false;
	}
#else
	errorfc(fc, "dax options not supported");
	return false;
#endif
}

445 446 447
static int erofs_fc_parse_param(struct fs_context *fc,
				struct fs_parameter *param)
{
448
	struct erofs_fs_context *ctx = fc->fs_private;
449
	struct fs_parse_result result;
450 451
	struct erofs_device_info *dif;
	int opt, ret;
452

453 454 455
	opt = fs_parse(fc, erofs_fs_parameters, param, &result);
	if (opt < 0)
		return opt;
456

457 458
	switch (opt) {
	case Opt_user_xattr:
459
#ifdef CONFIG_EROFS_FS_XATTR
460
		if (result.boolean)
461
			set_opt(&ctx->opt, XATTR_USER);
462
		else
463
			clear_opt(&ctx->opt, XATTR_USER);
464
#else
465
		errorfc(fc, "{,no}user_xattr options not supported");
466
#endif
467 468
		break;
	case Opt_acl:
469
#ifdef CONFIG_EROFS_FS_POSIX_ACL
470
		if (result.boolean)
471
			set_opt(&ctx->opt, POSIX_ACL);
472
		else
473
			clear_opt(&ctx->opt, POSIX_ACL);
474
#else
475
		errorfc(fc, "{,no}acl options not supported");
476
#endif
477 478 479
		break;
	case Opt_cache_strategy:
#ifdef CONFIG_EROFS_FS_ZIP
480
		ctx->opt.cache_strategy = result.uint_32;
481 482 483 484
#else
		errorfc(fc, "compression not supported, cache_strategy ignored");
#endif
		break;
485 486 487 488 489 490 491 492
	case Opt_dax:
		if (!erofs_fc_set_dax_mode(fc, EROFS_MOUNT_DAX_ALWAYS))
			return -EINVAL;
		break;
	case Opt_dax_enum:
		if (!erofs_fc_set_dax_mode(fc, result.uint_32))
			return -EINVAL;
		break;
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
	case Opt_device:
		dif = kzalloc(sizeof(*dif), GFP_KERNEL);
		if (!dif)
			return -ENOMEM;
		dif->path = kstrdup(param->string, GFP_KERNEL);
		if (!dif->path) {
			kfree(dif);
			return -ENOMEM;
		}
		down_write(&ctx->devs->rwsem);
		ret = idr_alloc(&ctx->devs->tree, dif, 0, 0, GFP_KERNEL);
		up_write(&ctx->devs->rwsem);
		if (ret < 0) {
			kfree(dif->path);
			kfree(dif);
			return ret;
		}
		++ctx->devs->extra_devices;
		break;
512 513
	default:
		return -ENOPARAM;
514 515 516 517
	}
	return 0;
}

518
#ifdef CONFIG_EROFS_FS_ZIP
519 520
static const struct address_space_operations managed_cache_aops;

521
static bool erofs_managed_cache_release_folio(struct folio *folio, gfp_t gfp)
522
{
523 524
	bool ret = true;
	struct address_space *const mapping = folio->mapping;
525

526
	DBG_BUGON(!folio_test_locked(folio));
527
	DBG_BUGON(mapping->a_ops != &managed_cache_aops);
528

529 530
	if (folio_test_private(folio))
		ret = erofs_try_to_free_cached_page(&folio->page);
531 532 533 534

	return ret;
}

535 536 537 538 539
/*
 * It will be called only on inode eviction. In case that there are still some
 * decompression requests in progress, wait with rescheduling for a bit here.
 * We could introduce an extra locking instead but it seems unnecessary.
 */
540 541
static void erofs_managed_cache_invalidate_folio(struct folio *folio,
					       size_t offset, size_t length)
542
{
543
	const size_t stop = length + offset;
544

545
	DBG_BUGON(!folio_test_locked(folio));
546

547
	/* Check for potential overflow in debug mode */
548
	DBG_BUGON(stop > folio_size(folio) || stop < length);
549

550
	if (offset == 0 && stop == folio_size(folio))
551
		while (!erofs_managed_cache_release_folio(folio, GFP_NOFS))
552 553 554 555
			cond_resched();
}

static const struct address_space_operations managed_cache_aops = {
556
	.release_folio = erofs_managed_cache_release_folio,
557
	.invalidate_folio = erofs_managed_cache_invalidate_folio,
558 559
};

560
static int erofs_init_managed_cache(struct super_block *sb)
561
{
562 563
	struct erofs_sb_info *const sbi = EROFS_SB(sb);
	struct inode *const inode = new_inode(sb);
564

565
	if (!inode)
566
		return -ENOMEM;
567 568 569 570 571

	set_nlink(inode, 1);
	inode->i_size = OFFSET_MAX;

	inode->i_mapping->a_ops = &managed_cache_aops;
572
	mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
573 574
	sbi->managed_cache = inode;
	return 0;
575
}
576 577
#else
static int erofs_init_managed_cache(struct super_block *sb) { return 0; }
578 579
#endif

580
static int erofs_fc_fill_super(struct super_block *sb, struct fs_context *fc)
581 582 583
{
	struct inode *inode;
	struct erofs_sb_info *sbi;
584
	struct erofs_fs_context *ctx = fc->fs_private;
585
	int err;
586

587 588
	sb->s_magic = EROFS_SUPER_MAGIC;

589
	if (!sb_set_blocksize(sb, EROFS_BLKSIZ)) {
590
		erofs_err(sb, "failed to set erofs blksize");
591
		return -EINVAL;
592 593
	}

594
	sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
595
	if (!sbi)
596
		return -ENOMEM;
597

598
	sb->s_fs_info = sbi;
599
	sbi->opt = ctx->opt;
600
	sbi->dax_dev = fs_dax_get_by_bdev(sb->s_bdev, &sbi->dax_part_off);
601 602 603
	sbi->devs = ctx->devs;
	ctx->devs = NULL;

604
	err = erofs_read_superblock(sb);
605
	if (err)
606
		return err;
607

Christoph Hellwig's avatar
Christoph Hellwig committed
608 609 610 611 612 613 614
	if (test_opt(&sbi->opt, DAX_ALWAYS)) {
		BUILD_BUG_ON(EROFS_BLKSIZ != PAGE_SIZE);

		if (!sbi->dax_dev) {
			errorfc(fc, "DAX unsupported by block device. Turning off DAX.");
			clear_opt(&sbi->opt, DAX_ALWAYS);
		}
615
	}
616
	sb->s_flags |= SB_RDONLY | SB_NOATIME;
617 618 619 620
	sb->s_maxbytes = MAX_LFS_FILESIZE;
	sb->s_time_gran = 1;

	sb->s_op = &erofs_sops;
621
	sb->s_xattr = erofs_xattr_handlers;
622

623
	if (test_opt(&sbi->opt, POSIX_ACL))
624 625 626 627
		sb->s_flags |= SB_POSIXACL;
	else
		sb->s_flags &= ~SB_POSIXACL;

628
#ifdef CONFIG_EROFS_FS_ZIP
Gao Xiang's avatar
Gao Xiang committed
629
	xa_init(&sbi->managed_pslots);
630 631
#endif

632 633
	/* get the root inode */
	inode = erofs_iget(sb, ROOT_NID(sbi), true);
634 635
	if (IS_ERR(inode))
		return PTR_ERR(inode);
636

637
	if (!S_ISDIR(inode->i_mode)) {
638 639
		erofs_err(sb, "rootino(nid %llu) is not a directory(i_mode %o)",
			  ROOT_NID(sbi), inode->i_mode);
640
		iput(inode);
641
		return -EINVAL;
642 643 644
	}

	sb->s_root = d_make_root(inode);
645
	if (!sb->s_root)
646
		return -ENOMEM;
647

648
	erofs_shrinker_register(sb);
649 650
	/* sb->s_umount is already locked, SB_ACTIVE and SB_BORN are not set */
	err = erofs_init_managed_cache(sb);
651
	if (err)
652
		return err;
653

Huang Jianan's avatar
Huang Jianan committed
654 655 656 657
	err = erofs_register_sysfs(sb);
	if (err)
		return err;

658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674
	erofs_info(sb, "mounted with root inode @ nid %llu.", ROOT_NID(sbi));
	return 0;
}

static int erofs_fc_get_tree(struct fs_context *fc)
{
	return get_tree_bdev(fc, erofs_fc_fill_super);
}

static int erofs_fc_reconfigure(struct fs_context *fc)
{
	struct super_block *sb = fc->root->d_sb;
	struct erofs_sb_info *sbi = EROFS_SB(sb);
	struct erofs_fs_context *ctx = fc->fs_private;

	DBG_BUGON(!sb_rdonly(sb));

675
	if (test_opt(&ctx->opt, POSIX_ACL))
676 677 678 679
		fc->sb_flags |= SB_POSIXACL;
	else
		fc->sb_flags &= ~SB_POSIXACL;

680
	sbi->opt = ctx->opt;
681 682

	fc->sb_flags |= SB_RDONLY;
683
	return 0;
684 685
}

686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706
static int erofs_release_device_info(int id, void *ptr, void *data)
{
	struct erofs_device_info *dif = ptr;

	fs_put_dax(dif->dax_dev);
	if (dif->bdev)
		blkdev_put(dif->bdev, FMODE_READ | FMODE_EXCL);
	kfree(dif->path);
	kfree(dif);
	return 0;
}

static void erofs_free_dev_context(struct erofs_dev_context *devs)
{
	if (!devs)
		return;
	idr_for_each(&devs->tree, &erofs_release_device_info, NULL);
	idr_destroy(&devs->tree);
	kfree(devs);
}

707 708
static void erofs_fc_free(struct fs_context *fc)
{
709 710 711 712
	struct erofs_fs_context *ctx = fc->fs_private;

	erofs_free_dev_context(ctx->devs);
	kfree(ctx);
713 714 715 716 717 718 719 720 721 722
}

static const struct fs_context_operations erofs_context_ops = {
	.parse_param	= erofs_fc_parse_param,
	.get_tree       = erofs_fc_get_tree,
	.reconfigure    = erofs_fc_reconfigure,
	.free		= erofs_fc_free,
};

static int erofs_init_fs_context(struct fs_context *fc)
723
{
724
	struct erofs_fs_context *ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
725

726 727 728 729 730 731 732 733
	if (!ctx)
		return -ENOMEM;
	ctx->devs = kzalloc(sizeof(struct erofs_dev_context), GFP_KERNEL);
	if (!ctx->devs) {
		kfree(ctx);
		return -ENOMEM;
	}
	fc->fs_private = ctx;
734

735 736 737
	idr_init(&ctx->devs->tree);
	init_rwsem(&ctx->devs->rwsem);
	erofs_default_options(ctx);
738 739
	fc->ops = &erofs_context_ops;
	return 0;
740 741 742 743 744 745
}

/*
 * could be triggered after deactivate_locked_super()
 * is called, thus including umount and failed to initialize.
 */
746
static void erofs_kill_sb(struct super_block *sb)
747
{
748 749 750
	struct erofs_sb_info *sbi;

	WARN_ON(sb->s_magic != EROFS_SUPER_MAGIC);
751

752 753 754
	kill_block_super(sb);

	sbi = EROFS_SB(sb);
755
	if (!sbi)
756
		return;
757 758

	erofs_free_dev_context(sbi->devs);
759
	fs_put_dax(sbi->dax_dev);
760 761 762
	kfree(sbi);
	sb->s_fs_info = NULL;
}
763

764 765 766 767
/* called when ->s_root is non-NULL */
static void erofs_put_super(struct super_block *sb)
{
	struct erofs_sb_info *const sbi = EROFS_SB(sb);
768

769
	DBG_BUGON(!sbi);
770

Huang Jianan's avatar
Huang Jianan committed
771
	erofs_unregister_sysfs(sb);
772
	erofs_shrinker_unregister(sb);
773
#ifdef CONFIG_EROFS_FS_ZIP
774
	iput(sbi->managed_cache);
775
	sbi->managed_cache = NULL;
776
#endif
777 778 779 780 781
}

static struct file_system_type erofs_fs_type = {
	.owner          = THIS_MODULE,
	.name           = "erofs",
782
	.init_fs_context = erofs_init_fs_context,
783
	.kill_sb        = erofs_kill_sb,
784 785 786 787 788 789 790 791 792 793
	.fs_flags       = FS_REQUIRES_DEV,
};
MODULE_ALIAS_FS("erofs");

static int __init erofs_module_init(void)
{
	int err;

	erofs_check_ondisk_layout_definitions();

794
	erofs_inode_cachep = kmem_cache_create("erofs_inode",
Gao Xiang's avatar
Gao Xiang committed
795
					       sizeof(struct erofs_inode), 0,
796
					       SLAB_RECLAIM_ACCOUNT,
797
					       erofs_inode_init_once);
798 799
	if (!erofs_inode_cachep) {
		err = -ENOMEM;
800
		goto icache_err;
801
	}
802

803
	err = erofs_init_shrinker();
804 805 806
	if (err)
		goto shrinker_err;

Gao Xiang's avatar
Gao Xiang committed
807 808 809 810
	err = z_erofs_lzma_init();
	if (err)
		goto lzma_err;

811
	erofs_pcpubuf_init();
812 813 814 815
	err = z_erofs_init_zip_subsystem();
	if (err)
		goto zip_err;

Huang Jianan's avatar
Huang Jianan committed
816 817 818 819
	err = erofs_init_sysfs();
	if (err)
		goto sysfs_err;

820 821 822 823 824 825 826
	err = register_filesystem(&erofs_fs_type);
	if (err)
		goto fs_err;

	return 0;

fs_err:
Huang Jianan's avatar
Huang Jianan committed
827 828
	erofs_exit_sysfs();
sysfs_err:
829 830
	z_erofs_exit_zip_subsystem();
zip_err:
Gao Xiang's avatar
Gao Xiang committed
831 832
	z_erofs_lzma_exit();
lzma_err:
833
	erofs_exit_shrinker();
834
shrinker_err:
835
	kmem_cache_destroy(erofs_inode_cachep);
836 837 838 839 840 841 842
icache_err:
	return err;
}

static void __exit erofs_module_exit(void)
{
	unregister_filesystem(&erofs_fs_type);
843

Gao Xiang's avatar
Gao Xiang committed
844
	/* Ensure all RCU free inodes / pclusters are safe to be destroyed. */
845
	rcu_barrier();
Gao Xiang's avatar
Gao Xiang committed
846

Huang Jianan's avatar
Huang Jianan committed
847
	erofs_exit_sysfs();
Gao Xiang's avatar
Gao Xiang committed
848 849 850
	z_erofs_exit_zip_subsystem();
	z_erofs_lzma_exit();
	erofs_exit_shrinker();
851
	kmem_cache_destroy(erofs_inode_cachep);
852
	erofs_pcpubuf_exit();
853 854 855 856 857 858 859 860 861 862 863
}

/* get filesystem statistics */
static int erofs_statfs(struct dentry *dentry, struct kstatfs *buf)
{
	struct super_block *sb = dentry->d_sb;
	struct erofs_sb_info *sbi = EROFS_SB(sb);
	u64 id = huge_encode_dev(sb->s_bdev->bd_dev);

	buf->f_type = sb->s_magic;
	buf->f_bsize = EROFS_BLKSIZ;
864
	buf->f_blocks = sbi->total_blocks;
865 866 867 868 869 870 871
	buf->f_bfree = buf->f_bavail = 0;

	buf->f_files = ULLONG_MAX;
	buf->f_ffree = ULLONG_MAX - sbi->inos;

	buf->f_namelen = EROFS_NAME_LEN;

872
	buf->f_fsid    = u64_to_fsid(id);
873 874 875 876 877
	return 0;
}

static int erofs_show_options(struct seq_file *seq, struct dentry *root)
{
878
	struct erofs_sb_info *sbi = EROFS_SB(root->d_sb);
879
	struct erofs_mount_opts *opt = &sbi->opt;
880 881

#ifdef CONFIG_EROFS_FS_XATTR
882
	if (test_opt(opt, XATTR_USER))
883 884 885 886 887
		seq_puts(seq, ",user_xattr");
	else
		seq_puts(seq, ",nouser_xattr");
#endif
#ifdef CONFIG_EROFS_FS_POSIX_ACL
888
	if (test_opt(opt, POSIX_ACL))
889 890 891
		seq_puts(seq, ",acl");
	else
		seq_puts(seq, ",noacl");
892
#endif
893
#ifdef CONFIG_EROFS_FS_ZIP
894
	if (opt->cache_strategy == EROFS_ZIP_CACHE_DISABLED)
895
		seq_puts(seq, ",cache_strategy=disabled");
896
	else if (opt->cache_strategy == EROFS_ZIP_CACHE_READAHEAD)
897
		seq_puts(seq, ",cache_strategy=readahead");
898
	else if (opt->cache_strategy == EROFS_ZIP_CACHE_READAROUND)
899 900
		seq_puts(seq, ",cache_strategy=readaround");
#endif
901
	if (test_opt(opt, DAX_ALWAYS))
902
		seq_puts(seq, ",dax=always");
903
	if (test_opt(opt, DAX_NEVER))
904
		seq_puts(seq, ",dax=never");
905 906 907 908 909
	return 0;
}

const struct super_operations erofs_sops = {
	.put_super = erofs_put_super,
910 911
	.alloc_inode = erofs_alloc_inode,
	.free_inode = erofs_free_inode,
912 913 914 915 916 917 918 919
	.statfs = erofs_statfs,
	.show_options = erofs_show_options,
};

module_init(erofs_module_init);
module_exit(erofs_module_exit);

MODULE_DESCRIPTION("Enhanced ROM File System");
Gao Xiang's avatar
Gao Xiang committed
920
MODULE_AUTHOR("Gao Xiang, Chao Yu, Miao Xie, CONSUMER BG, HUAWEI Inc.");
921
MODULE_LICENSE("GPL");