Skip to contentSkip to conversation

erofs-utils: lib: add per-sbi buffer support

PATCH In review Hongzhen Luo 2 participants 3 messages Jul 3, 2024 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 erofs-utils: lib: add per-sbi buffer support

Get rid of the global sbi and add per-sbi buffer support.

---
 include/erofs/cache.h    |  36 ++++++---
 include/erofs/internal.h |   5 +-
 lib/cache.c              | 159 +++++++++++++++++++++------------------
 lib/super.c              |  21 +++---
 4 files changed, 128 insertions(+), 93 deletions(-)
include/erofs/cache.h +26 -10
index f30fe9f..3e82311 100644
@@ -53,6 +53,15 @@ struct erofs_buffer_block {
 	struct erofs_buffer_head buffers;
 };
 
+struct erofs_balloc {
+	struct erofs_buffer_block blkh;
+	erofs_blk_t tail_blkaddr, erofs_metablkcnt;
+	/* buckets for all mapped buffer blocks to boost up allocation */
+	struct list_head mapped_buckets[META + 1][EROFS_MAX_BLOCK_SIZE];
+	/* last mapped buffer block to accelerate erofs_mapbh() */
+	struct erofs_buffer_block *last_mapped_block;
+};
+
 static inline const int get_alignsize(int type, int *type_ret)
 {
 	if (type == DATA)
@@ -80,14 +89,15 @@ static inline const int get_alignsize(int type, int *type_ret)
 extern const struct erofs_bhops erofs_drop_directly_bhops;
 extern const struct erofs_bhops erofs_skip_write_bhops;
 
-static inline erofs_off_t erofs_btell(struct erofs_buffer_head *bh, bool end)
+static inline erofs_off_t erofs_btell(struct erofs_sb_info *sbi,
+				      struct erofs_buffer_head *bh, bool end)
 {
 	const struct erofs_buffer_block *bb = bh->block;
 
 	if (bb->blkaddr == NULL_ADDR)
 		return NULL_ADDR_UL;
 
-	return erofs_pos(&sbi, bb->blkaddr) +
+	return erofs_pos(sbi, bb->blkaddr) +
 		(end ? list_next_entry(bh, list)->off : bh->off);
 }
 
@@ -98,21 +108,27 @@ static inline int erofs_bh_flush_generic_end(struct erofs_buffer_head *bh)
 	return 0;
 }
 
-void erofs_buffer_init(erofs_blk_t startblk);
-int erofs_bh_balloon(struct erofs_buffer_head *bh, erofs_off_t incr);
+void erofs_buffer_init(struct erofs_sb_info *sbi, erofs_blk_t startblk);
+int erofs_bh_balloon(struct erofs_sb_info *sbi, struct erofs_buffer_head *bh,
+		     erofs_off_t incr);
 
-struct erofs_buffer_head *erofs_balloc(int type, erofs_off_t size,
+struct erofs_buffer_head *erofs_balloc(struct erofs_sb_info *sbi,
+				       int type, erofs_off_t size,
 				       unsigned int required_ext,
 				       unsigned int inline_ext);
-struct erofs_buffer_head *erofs_battach(struct erofs_buffer_head *bh,
+struct erofs_buffer_head *erofs_battach(struct erofs_sb_info *sbi,
+					struct erofs_buffer_head *bh,
 					int type, unsigned int size);
 
-erofs_blk_t erofs_mapbh(struct erofs_buffer_block *bb);
-int erofs_bflush(struct erofs_buffer_block *bb);
+erofs_blk_t erofs_mapbh(struct erofs_sb_info *sbi,
+			struct erofs_buffer_block *bb);
+int erofs_bflush(struct erofs_sb_info *sbi, struct erofs_buffer_block *bb);
 
-void erofs_bdrop(struct erofs_buffer_head *bh, bool tryrevoke);
-erofs_blk_t erofs_total_metablocks(void);
+void erofs_bdrop(struct erofs_sb_info *sbi, struct erofs_buffer_head *bh,
+		 bool tryrevoke);
+erofs_blk_t erofs_total_metablocks(struct erofs_sb_info *sbi);
 
+void erofs_bexit(struct erofs_sb_info *sbi);
 #ifdef __cplusplus
 }
 #endif
include/erofs/internal.h +4 -1
index d5c5ce2..60da612 100644
@@ -58,6 +58,7 @@ extern struct erofs_sb_info sbi;
 	(roundup(addr, erofs_blksiz(sbi)) >> (sbi)->blkszbits)
 
 struct erofs_buffer_head;
+struct erofs_balloc;
 
 struct erofs_device_info {
 	u8 tag[64];
@@ -132,6 +133,8 @@ struct erofs_sb_info {
 	struct erofs_mkfs_dfops *mkfs_dfops;
 #endif
 	bool useqpl;
+
+	struct erofs_balloc *balloc;
 };
 
 #define EROFS_SUPER_END (EROFS_SUPER_OFFSET + sizeof(struct erofs_super_block))
@@ -402,7 +405,7 @@ int erofs_read_superblock(struct erofs_sb_info *sbi);
 void erofs_put_super(struct erofs_sb_info *sbi);
 int erofs_writesb(struct erofs_sb_info *sbi, struct erofs_buffer_head *sb_bh,
 		  erofs_blk_t *blocks);
-struct erofs_buffer_head *erofs_reserve_sb(void);
+struct erofs_buffer_head *erofs_reserve_sb(struct erofs_sb_info *sbi);
 int erofs_enable_sb_chksum(struct erofs_sb_info *sbi, u32 *crc);
 
 /* namei.c */
lib/cache.c +87 -72

365 lines not on this page · raw message

lib/super.c +11 -10
index 3fbaf66..2efa193 100644
@@ -149,6 +149,7 @@ void erofs_put_super(struct erofs_sb_info *sbi)
 		sbi->devs = NULL;
 	}
 	erofs_xattr_prefixes_cleanup(sbi);
+	erofs_bexit(sbi);
 }
 
 int erofs_writesb(struct erofs_sb_info *sbi, struct erofs_buffer_head *sb_bh,
@@ -176,7 +177,7 @@ int erofs_writesb(struct erofs_sb_info *sbi, struct erofs_buffer_head *sb_bh,
 	char *buf;
 	int ret;
 
-	*blocks         = erofs_mapbh(NULL);
+	*blocks         = erofs_mapbh(sbi, NULL);
 	sb.blocks       = cpu_to_le32(*blocks);
 	memcpy(sb.uuid, sbi->uuid, sizeof(sb.uuid));
 	memcpy(sb.volume_name, sbi->volume_name, sizeof(sb.volume_name));
@@ -194,42 +195,42 @@ int erofs_writesb(struct erofs_sb_info *sbi, struct erofs_buffer_head *sb_bh,
 	}
 	memcpy(buf + EROFS_SUPER_OFFSET, &sb, sizeof(sb));
 
-	ret = erofs_dev_write(sbi, buf, sb_bh ? erofs_btell(sb_bh, false) : 0,
+	ret = erofs_dev_write(sbi, buf, sb_bh ? erofs_btell(sbi, sb_bh, false) : 0,
 			      EROFS_SUPER_END);
 	free(buf);
 	if (sb_bh)
-		erofs_bdrop(sb_bh, false);
+		erofs_bdrop(sbi, sb_bh, false);
 	return ret;
 }
 
-struct erofs_buffer_head *erofs_reserve_sb(void)
+struct erofs_buffer_head *erofs_reserve_sb(struct erofs_sb_info *sbi)
 {
 	struct erofs_buffer_head *bh;
 	int err;
 
-	erofs_buffer_init(0);
-	bh = erofs_balloc(META, 0, 0, 0);
+	erofs_buffer_init(sbi, 0);
+	bh = erofs_balloc(sbi, META, 0, 0, 0);
 	if (IS_ERR(bh)) {
 		erofs_err("failed to allocate super: %s", PTR_ERR(bh));
 		return bh;
 	}
 	bh->op = &erofs_skip_write_bhops;
-	err = erofs_bh_balloon(bh, EROFS_SUPER_END);
+	err = erofs_bh_balloon(sbi, bh, EROFS_SUPER_END);
 	if (err < 0) {
 		erofs_err("failed to balloon super: %s", erofs_strerror(err));
 		goto err_bdrop;
 	}
 
 	/* make sure that the super block should be the very first blocks */
-	(void)erofs_mapbh(bh->block);
-	if (erofs_btell(bh, false) != 0) {
+	(void)erofs_mapbh(sbi, bh->block);
+	if (erofs_btell(sbi, bh, false) != 0) {
 		erofs_err("failed to pin super block @ 0");
 		err = -EFAULT;
 		goto err_bdrop;
 	}
 	return bh;
 err_bdrop:
-	erofs_bdrop(bh, true);
+	erofs_bdrop(sbi, bh, true);
 	return ERR_PTR(err);
 }
 
Signature · 1 line
2.39.3
#1 lore raw

PATCH 2/2 erofs-utils: lib: update function calls to align with per-sbi buffer

---
 lib/blobchunk.c | 20 ++++++++++----------
 lib/compress.c  | 40 ++++++++++++++++++++--------------------
 lib/inode.c     | 43 ++++++++++++++++++++++---------------------
 lib/xattr.c     | 10 +++++-----
 mkfs/main.c     | 22 ++++++++++++----------
 5 files changed, 69 insertions(+), 66 deletions(-)
lib/blobchunk.c +10 -10
index 9af223d..1169288 100644
@@ -496,8 +496,8 @@ int erofs_mkfs_dump_blobs(struct erofs_sb_info *sbi)
 		unsigned int i, ret;
 		erofs_blk_t nblocks;
 
-		nblocks = erofs_mapbh(NULL);
-		pos_out = erofs_btell(bh_devt, false);
+		nblocks = erofs_mapbh(sbi, NULL);
+		pos_out = erofs_btell(sbi, bh_devt, false);
 		i = 0;
 		do {
 			struct erofs_deviceslot dis = {
@@ -513,17 +513,17 @@ int erofs_mkfs_dump_blobs(struct erofs_sb_info *sbi)
 			nblocks += sbi->devs[i].blocks;
 		} while (++i < sbi->extra_devices);
 		bh_devt->op = &erofs_drop_directly_bhops;
-		erofs_bdrop(bh_devt, false);
+		erofs_bdrop(sbi, bh_devt, false);
 		return 0;
 	}
 
-	bh = erofs_balloc(DATA, datablob_size, 0, 0);
+	bh = erofs_balloc(sbi, DATA, datablob_size, 0, 0);
 	if (IS_ERR(bh))
 		return PTR_ERR(bh);
 
-	erofs_mapbh(bh->block);
+	erofs_mapbh(sbi, bh->block);
 
-	pos_out = erofs_btell(bh, false);
+	pos_out = erofs_btell(sbi, bh, false);
 	remapped_base = erofs_blknr(sbi, pos_out);
 	pos_out += sbi->bdev.offset;
 	if (blobfile) {
@@ -535,7 +535,7 @@ int erofs_mkfs_dump_blobs(struct erofs_sb_info *sbi)
 		ret = erofs_io_ftruncate(&sbi->bdev, pos_out + datablob_size);
 	}
 	bh->op = &erofs_drop_directly_bhops;
-	erofs_bdrop(bh, false);
+	erofs_bdrop(sbi, bh, false);
 	return ret;
 }
 
@@ -626,15 +626,15 @@ int erofs_mkfs_init_devices(struct erofs_sb_info *sbi, unsigned int devices)
 	if (!sbi->devs)
 		return -ENOMEM;
 
-	bh_devt = erofs_balloc(DEVT,
+	bh_devt = erofs_balloc(sbi, DEVT,
 		sizeof(struct erofs_deviceslot) * devices, 0, 0);
 	if (IS_ERR(bh_devt)) {
 		free(sbi->devs);
 		return PTR_ERR(bh_devt);
 	}
-	erofs_mapbh(bh_devt->block);
+	erofs_mapbh(sbi, bh_devt->block);
 	bh_devt->op = &erofs_skip_write_bhops;
-	sbi->devt_slotoff = erofs_btell(bh_devt, false) / EROFS_DEVT_SLOT_SIZE;
+	sbi->devt_slotoff = erofs_btell(sbi, bh_devt, false) / EROFS_DEVT_SLOT_SIZE;
 	sbi->extra_devices = devices;
 	erofs_sb_set_device_table(sbi);
 	return 0;
lib/compress.c +20 -20
index b473587..e74e58c 100644
@@ -1111,7 +1111,7 @@ int erofs_commit_compressed_file(struct z_erofs_compress_ictx *ictx,
 	}
 
 	if (compressed_blocks) {
-		ret = erofs_bh_balloon(bh, erofs_pos(sbi, compressed_blocks));
+		ret = erofs_bh_balloon(sbi, bh, erofs_pos(sbi, compressed_blocks));
 		DBG_BUGON(ret != erofs_blksiz(sbi));
 	} else {
 		if (!cfg.c_fragments && !cfg.c_dedupe)
@@ -1126,7 +1126,7 @@ int erofs_commit_compressed_file(struct z_erofs_compress_ictx *ictx,
 		bh->op = &erofs_skip_write_bhops;
 		inode->bh_data = bh;
 	} else {
-		erofs_bdrop(bh, false);
+		erofs_bdrop(sbi, bh, false);
 	}
 
 	inode->u.i_blocks = compressed_blocks;
@@ -1148,7 +1148,7 @@ err_free_meta:
 	free(compressmeta);
 	inode->compressmeta = NULL;
 err_free_idata:
-	erofs_bdrop(bh, true);	/* revoke buffer */
+	erofs_bdrop(sbi, bh, true);	/* revoke buffer */
 	if (inode->idata) {
 		free(inode->idata);
 		inode->idata = NULL;
@@ -1366,14 +1366,14 @@ int erofs_mt_write_compressed_file(struct z_erofs_compress_ictx *ictx)
 		pthread_cond_wait(&ictx->cond, &ictx->mutex);
 	pthread_mutex_unlock(&ictx->mutex);
 
-	bh = erofs_balloc(DATA, 0, 0, 0);
+	bh = erofs_balloc(ictx->inode->sbi, DATA, 0, 0, 0);
 	if (IS_ERR(bh)) {
 		ret = PTR_ERR(bh);
 		goto out;
 	}
 
 	DBG_BUGON(!head);
-	blkaddr = erofs_mapbh(bh->block);
+	blkaddr = erofs_mapbh(ictx->inode->sbi, bh->block);
 
 	ret = 0;
 	do {
@@ -1531,12 +1531,12 @@ int erofs_write_compressed_file(struct z_erofs_compress_ictx *ictx)
 #endif
 
 	/* allocate main data buffer */
-	bh = erofs_balloc(DATA, 0, 0, 0);
+	bh = erofs_balloc(inode->sbi, DATA, 0, 0, 0);
 	if (IS_ERR(bh)) {
 		ret = PTR_ERR(bh);
 		goto err_free_idata;
 	}
-	blkaddr = erofs_mapbh(bh->block); /* start_blkaddr */
+	blkaddr = erofs_mapbh(inode->sbi, bh->block); /* start_blkaddr */
 
 	ictx->seg_num = 1;
 	sctx = (struct z_erofs_compress_sctx) {
@@ -1560,7 +1560,7 @@ int erofs_write_compressed_file(struct z_erofs_compress_ictx *ictx)
 	goto out;
 
 err_free_idata:
-	erofs_bdrop(bh, true);	/* revoke buffer */
+	erofs_bdrop(inode->sbi, bh, true);	/* revoke buffer */
 	if (inode->idata) {
 		free(inode->idata);
 		inode->idata = NULL;
@@ -1596,13 +1596,13 @@ static int z_erofs_build_compr_cfgs(struct erofs_sb_info *sbi,
 			}
 		};
 
-		bh = erofs_battach(bh, META, sizeof(lz4alg));
+		bh = erofs_battach(sbi, bh, META, sizeof(lz4alg));
 		if (IS_ERR(bh)) {
 			DBG_BUGON(1);
 			return PTR_ERR(bh);
 		}
-		erofs_mapbh(bh->block);
-		ret = erofs_dev_write(sbi, &lz4alg, erofs_btell(bh, false),
+		erofs_mapbh(sbi, bh->block);
+		ret = erofs_dev_write(sbi, &lz4alg, erofs_btell(sbi, bh, false),
 				      sizeof(lz4alg));
 		bh->op = &erofs_drop_directly_bhops;
 	}
@@ -1620,13 +1620,13 @@ static int z_erofs_build_compr_cfgs(struct erofs_sb_info *sbi,
 			}
 		};
 
-		bh = erofs_battach(bh, META, sizeof(lzmaalg));
+		bh = erofs_battach(sbi, bh, META, sizeof(lzmaalg));
 		if (IS_ERR(bh)) {
 			DBG_BUGON(1);
 			return PTR_ERR(bh);
 		}
-		erofs_mapbh(bh->block);
-		ret = erofs_dev_write(sbi, &lzmaalg, erofs_btell(bh, false),
+		erofs_mapbh(sbi, bh->block);
+		ret = erofs_dev_write(sbi, &lzmaalg, erofs_btell(sbi, bh, false),
 				      sizeof(lzmaalg));
 		bh->op = &erofs_drop_directly_bhops;
 	}
@@ -1644,13 +1644,13 @@ static int z_erofs_build_compr_cfgs(struct erofs_sb_info *sbi,
 			}
 		};
 
-		bh = erofs_battach(bh, META, sizeof(zalg));
+		bh = erofs_battach(sbi, bh, META, sizeof(zalg));
 		if (IS_ERR(bh)) {
 			DBG_BUGON(1);
 			return PTR_ERR(bh);
 		}
-		erofs_mapbh(bh->block);
-		ret = erofs_dev_write(sbi, &zalg, erofs_btell(bh, false),
+		erofs_mapbh(sbi, bh->block);
+		ret = erofs_dev_write(sbi, &zalg, erofs_btell(sbi, bh, false),
 				      sizeof(zalg));
 		bh->op = &erofs_drop_directly_bhops;
 	}
@@ -1667,13 +1667,13 @@ static int z_erofs_build_compr_cfgs(struct erofs_sb_info *sbi,
 			}
 		};
 
-		bh = erofs_battach(bh, META, sizeof(zalg));
+		bh = erofs_battach(sbi, bh, META, sizeof(zalg));
 		if (IS_ERR(bh)) {
 			DBG_BUGON(1);
 			return PTR_ERR(bh);
 		}
-		erofs_mapbh(bh->block);
-		ret = erofs_dev_write(sbi, &zalg, erofs_btell(bh, false),
+		erofs_mapbh(sbi, bh->block);
+		ret = erofs_dev_write(sbi, &zalg, erofs_btell(sbi, bh, false),
 				      sizeof(zalg));
 		bh->op = &erofs_drop_directly_bhops;
 	}
lib/inode.c +22 -21

146 lines not on this page · raw message

lib/xattr.c +5 -5
index b0f80e9..3356c3e 100644
@@ -906,7 +906,7 @@ int erofs_build_shared_xattrs_from_path(struct erofs_sb_info *sbi, const char *p
 		return -ENOMEM;
 	}
 
-	bh = erofs_balloc(XATTR, shared_xattrs_size, 0, 0);
+	bh = erofs_balloc(sbi, XATTR, shared_xattrs_size, 0, 0);
 	if (IS_ERR(bh)) {
 		free(sorted_n);
 		free(buf);
@@ -914,8 +914,8 @@ int erofs_build_shared_xattrs_from_path(struct erofs_sb_info *sbi, const char *p
 	}
 	bh->op = &erofs_skip_write_bhops;
 
-	erofs_mapbh(bh->block);
-	off = erofs_btell(bh, false);
+	erofs_mapbh(sbi, bh->block);
+	off = erofs_btell(sbi, bh, false);
 
 	sbi->xattr_blkaddr = off / erofs_blksiz(sbi);
 	off %= erofs_blksiz(sbi);
@@ -930,9 +930,9 @@ int erofs_build_shared_xattrs_from_path(struct erofs_sb_info *sbi, const char *p
 	shared_xattrs_list = sorted_n[0];
 	free(sorted_n);
 	bh->op = &erofs_drop_directly_bhops;
-	ret = erofs_dev_write(sbi, buf, erofs_btell(bh, false), shared_xattrs_size);
+	ret = erofs_dev_write(sbi, buf, erofs_btell(sbi, bh, false), shared_xattrs_size);
 	free(buf);
-	erofs_bdrop(bh, false);
+	erofs_bdrop(sbi, bh, false);
 out:
 	erofs_cleanxattrs(true);
 	return ret;
mkfs/main.c +12 -10
index bfd6d60..77b2a4b 100644
@@ -1105,7 +1105,8 @@ static int erofs_mkfs_rebuild_load_trees(struct erofs_inode *root)
 	return 0;
 }
 
-static void erofs_mkfs_showsummaries(erofs_blk_t nblocks)
+static void erofs_mkfs_showsummaries(struct erofs_sb_info *sbi,
+				     erofs_blk_t nblocks)
 {
 	char uuid_str[37] = {};
 	char *incr = incremental_mode ? "new" : "total";
@@ -1113,16 +1114,16 @@ static void erofs_mkfs_showsummaries(erofs_blk_t nblocks)
 	if (!(cfg.c_dbg_lvl > EROFS_ERR && cfg.c_showprogress))
 		return;
 
-	erofs_uuid_unparse_lower(sbi.uuid, uuid_str);
+	erofs_uuid_unparse_lower(sbi->uuid, uuid_str);
 
 	fprintf(stdout, "------\nFilesystem UUID: %s\n"
 		"Filesystem total blocks: %u (of %u-byte blocks)\n"
 		"Filesystem total inodes: %llu\n"
 		"Filesystem %s metadata blocks: %u\n"
 		"Filesystem %s deduplicated bytes (of source files): %llu\n",
-		uuid_str, nblocks, 1U << sbi.blkszbits, sbi.inos | 0ULL,
-		incr, erofs_total_metablocks(),
-		incr, sbi.saved_by_deduplication | 0ULL);
+		uuid_str, nblocks, 1U << sbi->blkszbits, sbi->inos | 0ULL,
+		incr, erofs_total_metablocks(sbi),
+		incr, sbi->saved_by_deduplication | 0ULL);
 }
 
 int main(int argc, char **argv)
@@ -1245,7 +1246,7 @@ int main(int argc, char **argv)
 	}
 
 	if (!incremental_mode) {
-		sb_bh = erofs_reserve_sb();
+		sb_bh = erofs_reserve_sb(&sbi);
 		if (IS_ERR(sb_bh)) {
 			err = PTR_ERR(sb_bh);
 			goto exit;
@@ -1270,7 +1271,7 @@ int main(int argc, char **argv)
 			u.startblk = DIV_ROUND_UP(u.st.st_size, erofs_blksiz(&sbi));
 		else
 			u.startblk = sbi.primarydevice_blocks;
-		erofs_buffer_init(u.startblk);
+		erofs_buffer_init(&sbi, u.startblk);
 		sb_bh = NULL;
 	}
 
@@ -1392,7 +1393,7 @@ int main(int argc, char **argv)
 	}
 
 	/* flush all buffers except for the superblock */
-	err = erofs_bflush(NULL);
+	err = erofs_bflush(&sbi, NULL);
 	if (err)
 		goto exit;
 
@@ -1405,7 +1406,7 @@ int main(int argc, char **argv)
 		goto exit;
 
 	/* flush all remaining buffers */
-	err = erofs_bflush(NULL);
+	err = erofs_bflush(&sbi, NULL);
 	if (err)
 		goto exit;
 
@@ -1448,6 +1449,7 @@ exit:
 		return 1;
 	}
 	erofs_update_progressinfo("Build completed.\n");
-	erofs_mkfs_showsummaries(nblocks);
+	erofs_mkfs_showsummaries(&sbi, nblocks);
+	erofs_put_super(&sbi);
 	return 0;
 }
Signature · 1 line
2.39.3
#2 lore raw
In reply to Hongzhen Luo
Quoted 9 lines · Hongzhen Luo

Get rid of the global sbi and add per-sbi buffer support.

---
  include/erofs/cache.h    |  36 ++++++---
  include/erofs/internal.h |   5 +-
  lib/cache.c              | 159 +++++++++++++++++++++------------------
  lib/super.c              |  21 +++---
  4 files changed, 128 insertions(+), 93 deletions(-)

Can this patch compile successfully?

include/erofs/cache.h struct erofs_buffer_block { · line 56

Quoted 10 lines
include/erofs/cache.h +1 -0
index f30fe9f..3e82311 100644
@@ -53,6 +53,15 @@ struct erofs_buffer_block {
  	struct erofs_buffer_head buffers;
  };
 
+struct erofs_balloc {

After rethinking, I think struct erofs_buffer_manager might be a better name.

Quoted 12 lines
include/erofs/cache.h +8 -0
+	struct erofs_buffer_block blkh;
+	erofs_blk_t tail_blkaddr, erofs_metablkcnt;
+	/* buckets for all mapped buffer blocks to boost up allocation */
+	struct list_head mapped_buckets[META + 1][EROFS_MAX_BLOCK_SIZE];
+	/* last mapped buffer block to accelerate erofs_mapbh() */
+	struct erofs_buffer_block *last_mapped_block;
+};
+
  static inline const int get_alignsize(int type, int *type_ret)
  {
  	if (type == DATA)

include/erofs/cache.h static inline const int get_alignsize(int type, int *type_ret) · line 93

Quoted 6 lines
include/erofs/cache.h +2 -1
@@ -80,14 +89,15 @@ static inline const int get_alignsize(int type, int *type_ret)
  extern const struct erofs_bhops erofs_drop_directly_bhops;
  extern const struct erofs_bhops erofs_skip_write_bhops;
 
-static inline erofs_off_t erofs_btell(struct erofs_buffer_head *bh, bool end)
+static inline erofs_off_t erofs_btell(struct erofs_sb_info *sbi,
+				      struct erofs_buffer_head *bh, bool end)

My suggestion is that
1) add a patch to rename global &sbi to &g_sbi in the whole codebase;

2) leave all function definition as-is in this path

include/erofs/cache.h static inline const int get_alignsize(int type, int *type_ret) · line 94

Quoted 1 line
include/erofs/cache.h +0 -0
  {

But

	struct erofs_sb_info *sbi = &g_sbi;

3) Update function definitions and all callers in the following patch.

Quoted 450 lines
include/erofs/cache.h +15 -9
  	const struct erofs_buffer_block *bb = bh->block;
 
  	if (bb->blkaddr == NULL_ADDR)
  		return NULL_ADDR_UL;
 
-	return erofs_pos(&sbi, bb->blkaddr) +
+	return erofs_pos(sbi, bb->blkaddr) +
  		(end ? list_next_entry(bh, list)->off : bh->off);
  }
 
@@ -98,21 +108,27 @@ static inline int erofs_bh_flush_generic_end(struct erofs_buffer_head *bh)
  	return 0;
  }
 
-void erofs_buffer_init(erofs_blk_t startblk);
-int erofs_bh_balloon(struct erofs_buffer_head *bh, erofs_off_t incr);
+void erofs_buffer_init(struct erofs_sb_info *sbi, erofs_blk_t startblk);
+int erofs_bh_balloon(struct erofs_sb_info *sbi, struct erofs_buffer_head *bh,
+		     erofs_off_t incr);
 
-struct erofs_buffer_head *erofs_balloc(int type, erofs_off_t size,
+struct erofs_buffer_head *erofs_balloc(struct erofs_sb_info *sbi,
+				       int type, erofs_off_t size,
  				       unsigned int required_ext,
  				       unsigned int inline_ext);
-struct erofs_buffer_head *erofs_battach(struct erofs_buffer_head *bh,
+struct erofs_buffer_head *erofs_battach(struct erofs_sb_info *sbi,
+					struct erofs_buffer_head *bh,
  					int type, unsigned int size);
 
-erofs_blk_t erofs_mapbh(struct erofs_buffer_block *bb);
-int erofs_bflush(struct erofs_buffer_block *bb);
+erofs_blk_t erofs_mapbh(struct erofs_sb_info *sbi,
+			struct erofs_buffer_block *bb);
+int erofs_bflush(struct erofs_sb_info *sbi, struct erofs_buffer_block *bb);
 
-void erofs_bdrop(struct erofs_buffer_head *bh, bool tryrevoke);
-erofs_blk_t erofs_total_metablocks(void);
+void erofs_bdrop(struct erofs_sb_info *sbi, struct erofs_buffer_head *bh,
+		 bool tryrevoke);
+erofs_blk_t erofs_total_metablocks(struct erofs_sb_info *sbi);
 
+void erofs_bexit(struct erofs_sb_info *sbi);
  #ifdef __cplusplus
  }
  #endif
include/erofs/internal.h +4 -1
index d5c5ce2..60da612 100644
@@ -58,6 +58,7 @@ extern struct erofs_sb_info sbi;
  	(roundup(addr, erofs_blksiz(sbi)) >> (sbi)->blkszbits)
 
  struct erofs_buffer_head;
+struct erofs_balloc;
 
  struct erofs_device_info {
  	u8 tag[64];
@@ -132,6 +133,8 @@ struct erofs_sb_info {
  	struct erofs_mkfs_dfops *mkfs_dfops;
  #endif
  	bool useqpl;
+
+	struct erofs_balloc *balloc;
  };
 
  #define EROFS_SUPER_END (EROFS_SUPER_OFFSET + sizeof(struct erofs_super_block))
@@ -402,7 +405,7 @@ int erofs_read_superblock(struct erofs_sb_info *sbi);
  void erofs_put_super(struct erofs_sb_info *sbi);
  int erofs_writesb(struct erofs_sb_info *sbi, struct erofs_buffer_head *sb_bh,
  		  erofs_blk_t *blocks);
-struct erofs_buffer_head *erofs_reserve_sb(void);
+struct erofs_buffer_head *erofs_reserve_sb(struct erofs_sb_info *sbi);
  int erofs_enable_sb_chksum(struct erofs_sb_info *sbi, u32 *crc);
 
  /* namei.c */
lib/cache.c +77 -70
index 328ca4a..63d7c0c 100644
@@ -9,17 +9,6 @@
  #include <erofs/cache.h>
  #include "erofs/print.h"
 
-static struct erofs_buffer_block blkh = {
-	.list = LIST_HEAD_INIT(blkh.list),
-	.blkaddr = NULL_ADDR,
-};
-static erofs_blk_t tail_blkaddr, erofs_metablkcnt;
-
-/* buckets for all mapped buffer blocks to boost up allocation */
-static struct list_head mapped_buckets[META + 1][EROFS_MAX_BLOCK_SIZE];
-/* last mapped buffer block to accelerate erofs_mapbh() */
-static struct erofs_buffer_block *last_mapped_block = &blkh;
-
  static int erofs_bh_flush_drop_directly(struct erofs_buffer_head *bh)
  {
  	return erofs_bh_flush_generic_end(bh);
@@ -38,38 +27,48 @@ const struct erofs_bhops erofs_skip_write_bhops = {
  	.flush = erofs_bh_flush_skip_write,
  };
 
-void erofs_buffer_init(erofs_blk_t startblk)
+void erofs_buffer_init(struct erofs_sb_info *sbi, erofs_blk_t startblk)
  {
  	int i, j;
 
-	for (i = 0; i < ARRAY_SIZE(mapped_buckets); i++)
-		for (j = 0; j < ARRAY_SIZE(mapped_buckets[0]); j++)
-			init_list_head(&mapped_buckets[i][j]);
-	tail_blkaddr = startblk;
+	sbi->balloc = malloc(sizeof(struct erofs_balloc));
+	if (!sbi->balloc) {
+		erofs_err("fail to prepare balloc");
+		return;
+	}
+	init_list_head(&sbi->balloc->blkh.list);
+	sbi->balloc->blkh.blkaddr = NULL_ADDR;
+	sbi->balloc->last_mapped_block = &sbi->balloc->blkh;
+
+	for (i = 0; i < ARRAY_SIZE(sbi->balloc->mapped_buckets); i++)
+		for (j = 0; j < ARRAY_SIZE(sbi->balloc->mapped_buckets[0]); j++)
+			init_list_head(&sbi->balloc->mapped_buckets[i][j]);
+	sbi->balloc->tail_blkaddr = startblk;
  }
 
-static void erofs_bupdate_mapped(struct erofs_buffer_block *bb)
+static void erofs_bupdate_mapped(struct erofs_sb_info *sbi, struct erofs_buffer_block *bb)
  {
  	struct list_head *bkt;
 
  	if (bb->blkaddr == NULL_ADDR)
  		return;
 
-	bkt = mapped_buckets[bb->type] +
-		(bb->buffers.off & (erofs_blksiz(&sbi) - 1));
+	bkt = sbi->balloc->mapped_buckets[bb->type] +
+		(bb->buffers.off & (erofs_blksiz(sbi) - 1));
  	list_del(&bb->mapped_list);
  	list_add_tail(&bb->mapped_list, bkt);
  }
 
  /* return occupied bytes in specific buffer block if succeed */
-static int __erofs_battach(struct erofs_buffer_block *bb,
+static int __erofs_battach(struct erofs_sb_info *sbi,
+			   struct erofs_buffer_block *bb,
  			   struct erofs_buffer_head *bh,
  			   erofs_off_t incr,
  			   unsigned int alignsize,
  			   unsigned int extrasize,
  			   bool dryrun)
  {
-	const unsigned int blksiz = erofs_blksiz(&sbi);
+	const unsigned int blksiz = erofs_blksiz(sbi);
  	const unsigned int blkmask = blksiz - 1;
  	erofs_off_t boff = bb->buffers.off;
  	const erofs_off_t alignedoffset = roundup(boff, alignsize);
@@ -85,8 +84,8 @@ static int __erofs_battach(struct erofs_buffer_block *bb,
 
  		blkaddr = bb->blkaddr;
  		if (blkaddr != NULL_ADDR) {
-			tailupdate = (tail_blkaddr == blkaddr +
-				      BLK_ROUND_UP(&sbi, boff));
+			tailupdate = (sbi->balloc->tail_blkaddr == blkaddr +
+				      BLK_ROUND_UP(sbi, boff));
  			if (oob && !tailupdate)
  				return -EINVAL;
  		}
@@ -102,13 +101,14 @@ static int __erofs_battach(struct erofs_buffer_block *bb,
  		bb->buffers.off = boff;
  		/* need to update the tail_blkaddr */
  		if (tailupdate)
-			tail_blkaddr = blkaddr + BLK_ROUND_UP(&sbi, boff);
-		erofs_bupdate_mapped(bb);
+			sbi->balloc->tail_blkaddr = blkaddr + BLK_ROUND_UP(sbi, boff);
+		erofs_bupdate_mapped(sbi, bb);
  	}
  	return ((alignedoffset + incr - 1) & blkmask) + 1;
  }
 
-int erofs_bh_balloon(struct erofs_buffer_head *bh, erofs_off_t incr)
+int erofs_bh_balloon(struct erofs_sb_info *sbi, struct erofs_buffer_head *bh,
+		     erofs_off_t incr)
  {
  	struct erofs_buffer_block *const bb = bh->block;
 
@@ -116,16 +116,17 @@ int erofs_bh_balloon(struct erofs_buffer_head *bh, erofs_off_t incr)
  	if (bh->list.next != &bb->buffers.list)
  		return -EINVAL;
 
-	return __erofs_battach(bb, NULL, incr, 1, 0, false);
+	return __erofs_battach(sbi, bb, NULL, incr, 1, 0, false);
  }
 
-static int erofs_bfind_for_attach(int type, erofs_off_t size,
+static int erofs_bfind_for_attach(struct erofs_sb_info *sbi,
+				  int type, erofs_off_t size,
  				  unsigned int required_ext,
  				  unsigned int inline_ext,
  				  unsigned int alignsize,
  				  struct erofs_buffer_block **bbp)
  {
-	const unsigned int blksiz = erofs_blksiz(&sbi);
+	const unsigned int blksiz = erofs_blksiz(sbi);
  	struct erofs_buffer_block *cur, *bb;
  	unsigned int used0, used_before, usedmax, used;
  	int ret;
@@ -150,7 +151,8 @@ static int erofs_bfind_for_attach(int type, erofs_off_t size,
  	used_before = rounddown(blksiz -
  				(size + required_ext + inline_ext), alignsize);
  	for (; used_before; --used_before) {
-		struct list_head *bt = mapped_buckets[type] + used_before;
+		struct list_head *bt = sbi->balloc->mapped_buckets[type] +
+								used_before;
 
  		if (list_empty(bt))
  			continue;
@@ -159,7 +161,7 @@ static int erofs_bfind_for_attach(int type, erofs_off_t size,
 
  		/* last mapped block can be expended, don't handle it here */
  		if (list_next_entry(cur, list)->blkaddr == NULL_ADDR) {
-			DBG_BUGON(cur != last_mapped_block);
+			DBG_BUGON(cur != sbi->balloc->last_mapped_block);
  			continue;
  		}
 
@@ -167,7 +169,7 @@ static int erofs_bfind_for_attach(int type, erofs_off_t size,
  		DBG_BUGON(cur->blkaddr == NULL_ADDR);
  		DBG_BUGON(used_before != (cur->buffers.off & (blksiz - 1)));
 
-		ret = __erofs_battach(cur, NULL, size, alignsize,
+		ret = __erofs_battach(sbi, cur, NULL, size, alignsize,
  				      required_ext + inline_ext, true);
  		if (ret < 0) {
  			DBG_BUGON(1);
@@ -185,10 +187,10 @@ static int erofs_bfind_for_attach(int type, erofs_off_t size,
 
  skip_mapped:
  	/* try to start from the last mapped one, which can be expended */
-	cur = last_mapped_block;
-	if (cur == &blkh)
+	cur = sbi->balloc->last_mapped_block;
+	if (cur == &sbi->balloc->blkh)
  		cur = list_next_entry(cur, list);
-	for (; cur != &blkh; cur = list_next_entry(cur, list)) {
+	for (; cur != &sbi->balloc->blkh; cur = list_next_entry(cur, list)) {
  		used_before = cur->buffers.off & (blksiz - 1);
 
  		/* skip if buffer block is just full */
@@ -199,7 +201,7 @@ skip_mapped:
  		if (cur->type != type)
  			continue;
 
-		ret = __erofs_battach(cur, NULL, size, alignsize,
+		ret = __erofs_battach(sbi, cur, NULL, size, alignsize,
  				      required_ext + inline_ext, true);
  		if (ret < 0)
  			continue;
@@ -226,7 +228,8 @@ skip_mapped:
  	return 0;
  }
 
-struct erofs_buffer_head *erofs_balloc(int type, erofs_off_t size,
+struct erofs_buffer_head *erofs_balloc(struct erofs_sb_info *sbi, int type,
+				       erofs_off_t size,
  				       unsigned int required_ext,
  				       unsigned int inline_ext)
  {
@@ -243,7 +246,7 @@ struct erofs_buffer_head *erofs_balloc(int type, erofs_off_t size,
  	alignsize = ret;
 
  	/* try to find if we could reuse an allocated buffer block */
-	ret = erofs_bfind_for_attach(type, size, required_ext, inline_ext,
+	ret = erofs_bfind_for_attach(sbi, type, size, required_ext, inline_ext,
  				     alignsize, &bb);
  	if (ret)
  		return ERR_PTR(ret);
@@ -263,9 +266,9 @@ struct erofs_buffer_head *erofs_balloc(int type, erofs_off_t size,
  		bb->buffers.off = 0;
  		init_list_head(&bb->buffers.list);
  		if (type == DATA)
-			list_add(&bb->list, &last_mapped_block->list);
+			list_add(&bb->list, &sbi->balloc->last_mapped_block->list);
  		else
-			list_add_tail(&bb->list, &blkh.list);
+			list_add_tail(&bb->list, &sbi->balloc->blkh.list);
  		init_list_head(&bb->mapped_list);
 
  		bh = malloc(sizeof(struct erofs_buffer_head));
@@ -275,7 +278,7 @@ struct erofs_buffer_head *erofs_balloc(int type, erofs_off_t size,
  		}
  	}
 
-	ret = __erofs_battach(bb, bh, size, alignsize,
+	ret = __erofs_battach(sbi, bb, bh, size, alignsize,
  			      required_ext + inline_ext, false);
  	if (ret < 0) {
  		free(bh);
@@ -284,7 +287,8 @@ struct erofs_buffer_head *erofs_balloc(int type, erofs_off_t size,
  	return bh;
  }
 
-struct erofs_buffer_head *erofs_battach(struct erofs_buffer_head *bh,
+struct erofs_buffer_head *erofs_battach(struct erofs_sb_info *sbi,
+					struct erofs_buffer_head *bh,
  					int type, unsigned int size)
  {
  	struct erofs_buffer_block *const bb = bh->block;
@@ -304,7 +308,7 @@ struct erofs_buffer_head *erofs_battach(struct erofs_buffer_head *bh,
  	if (!nbh)
  		return ERR_PTR(-ENOMEM);
 
-	ret = __erofs_battach(bb, nbh, size, alignsize, 0, false);
+	ret = __erofs_battach(sbi, bb, nbh, size, alignsize, 0, false);
  	if (ret < 0) {
  		free(nbh);
  		return ERR_PTR(ret);
@@ -312,59 +316,62 @@ struct erofs_buffer_head *erofs_battach(struct erofs_buffer_head *bh,
  	return nbh;
  }
 
-static erofs_blk_t __erofs_mapbh(struct erofs_buffer_block *bb)
+static erofs_blk_t __erofs_mapbh(struct erofs_sb_info *sbi,
+				 struct erofs_buffer_block *bb)
  {
  	erofs_blk_t blkaddr;
 
  	if (bb->blkaddr == NULL_ADDR) {
-		bb->blkaddr = tail_blkaddr;
-		last_mapped_block = bb;
-		erofs_bupdate_mapped(bb);
+		bb->blkaddr = sbi->balloc->tail_blkaddr;
+		sbi->balloc->last_mapped_block = bb;
+		erofs_bupdate_mapped(sbi, bb);
  	}
 
-	blkaddr = bb->blkaddr + BLK_ROUND_UP(&sbi, bb->buffers.off);
-	if (blkaddr > tail_blkaddr)
-		tail_blkaddr = blkaddr;
+	blkaddr = bb->blkaddr + BLK_ROUND_UP(sbi, bb->buffers.off);
+	if (blkaddr > sbi->balloc->tail_blkaddr)
+		sbi->balloc->tail_blkaddr = blkaddr;
 
  	return blkaddr;
  }
 
-erofs_blk_t erofs_mapbh(struct erofs_buffer_block *bb)
+erofs_blk_t erofs_mapbh(struct erofs_sb_info *sbi,
+			struct erofs_buffer_block *bb)
  {
-	struct erofs_buffer_block *t = last_mapped_block;
+	struct erofs_buffer_block *t = sbi->balloc->last_mapped_block;
 
  	if (bb && bb->blkaddr != NULL_ADDR)
  		return bb->blkaddr;
  	do {
  		t = list_next_entry(t, list);
-		if (t == &blkh)
+		if (t == &sbi->balloc->blkh)
  			break;
 
  		DBG_BUGON(t->blkaddr != NULL_ADDR);
-		(void)__erofs_mapbh(t);
+		(void)__erofs_mapbh(sbi, t);
  	} while (t != bb);
-	return tail_blkaddr;
+	return sbi->balloc->tail_blkaddr;
  }
 
-static void erofs_bfree(struct erofs_buffer_block *bb)
+static void erofs_bfree(struct erofs_sb_info *sbi,
+			struct erofs_buffer_block *bb)
  {
  	DBG_BUGON(!list_empty(&bb->buffers.list));
 
-	if (bb == last_mapped_block)
-		last_mapped_block = list_prev_entry(bb, list);
+	if (bb == sbi->balloc->last_mapped_block)
+		sbi->balloc->last_mapped_block = list_prev_entry(bb, list);
 
  	list_del(&bb->mapped_list);
  	list_del(&bb->list);
  	free(bb);
  }
 
-int erofs_bflush(struct erofs_buffer_block *bb)
+int erofs_bflush(struct erofs_sb_info *sbi, struct erofs_buffer_block *bb)
  {
-	const unsigned int blksiz = erofs_blksiz(&sbi);
+	const unsigned int blksiz = erofs_blksiz(sbi);
  	struct erofs_buffer_block *p, *n;
  	erofs_blk_t blkaddr;
 
-	list_for_each_entry_safe(p, n, &blkh.list, list) {
+	list_for_each_entry_safe(p, n, &sbi->balloc->blkh.list, list) {
  		struct erofs_buffer_head *bh, *nbh;
  		unsigned int padding;
  		bool skip = false;
@@ -373,7 +380,7 @@ int erofs_bflush(struct erofs_buffer_block *bb)
  		if (p == bb)
  			break;
 
-		blkaddr = __erofs_mapbh(p);
+		blkaddr = __erofs_mapbh(sbi, p);
 
  		list_for_each_entry_safe(bh, nbh, &p->buffers.list, list) {
  			if (bh->op == &erofs_skip_write_bhops) {
@@ -392,18 +399,18 @@ int erofs_bflush(struct erofs_buffer_block *bb)
 
  		padding = blksiz - (p->buffers.off & (blksiz - 1));
  		if (padding != blksiz)
-			erofs_dev_fillzero(&sbi, erofs_pos(&sbi, blkaddr) - padding,
+			erofs_dev_fillzero(sbi, erofs_pos(sbi, blkaddr) - padding,
  					   padding, true);
 
  		if (p->type != DATA)
-			erofs_metablkcnt += BLK_ROUND_UP(&sbi, p->buffers.off);
+			sbi->balloc->erofs_metablkcnt += BLK_ROUND_UP(sbi, p->buffers.off);
  		erofs_dbg("block %u to %u flushed", p->blkaddr, blkaddr - 1);
-		erofs_bfree(p);
+		erofs_bfree(sbi, p);
  	}
  	return 0;
  }
 
-void erofs_bdrop(struct erofs_buffer_head *bh, bool tryrevoke)
+void erofs_bdrop(struct erofs_sb_info *sbi, struct erofs_buffer_head *bh, bool tryrevoke)
  {
  	struct erofs_buffer_block *const bb = bh->block;
  	const erofs_blk_t blkaddr = bh->block->blkaddr;
@@ -411,7 +418,7 @@ void erofs_bdrop(struct erofs_buffer_head *bh, bool tryrevoke)
 
  	/* tail_blkaddr could be rolled back after revoking all bhs */
  	if (tryrevoke && blkaddr != NULL_ADDR &&
-	    tail_blkaddr == blkaddr + BLK_ROUND_UP(&sbi, bb->buffers.off))
+	    sbi->balloc->tail_blkaddr == blkaddr + BLK_ROUND_UP(sbi, bb->buffers.off))
  		rollback = true;
 
  	bh->op = &erofs_drop_directly_bhops;
@@ -421,13 +428,21 @@ void erofs_bdrop(struct erofs_buffer_head *bh, bool tryrevoke)
  		return;
 
  	if (!rollback && bb->type != DATA)
-		erofs_metablkcnt += BLK_ROUND_UP(&sbi, bb->buffers.off);
-	erofs_bfree(bb);
+		sbi->balloc->erofs_metablkcnt += BLK_ROUND_UP(sbi, bb->buffers.off);
+	erofs_bfree(sbi, bb);
  	if (rollback)
-		tail_blkaddr = blkaddr;
+		sbi->balloc->tail_blkaddr = blkaddr;

lib/cache.c void erofs_bdrop(struct erofs_buffer_head *bh, bool tryrevoke) · line 442

Quoted 10 lines
lib/cache.c +5 -2
  }
 
-erofs_blk_t erofs_total_metablocks(void)
+erofs_blk_t erofs_total_metablocks(struct erofs_sb_info *sbi)
  {
-	return erofs_metablkcnt;
+	return sbi->balloc->erofs_metablkcnt;
+}
+
+void erofs_bexit(struct erofs_sb_info *sbi)

erofs_buffer_exit(struct erofs_sb_info *sbi)

lib/super.c void erofs_put_super(struct erofs_sb_info *sbi)

Quoted 11 lines
lib/cache.c +5 -0
+{
+	if (sbi->balloc) {
+		free(sbi->balloc);
+		sbi->balloc = NULL;
+	}
  }
lib/super.c +0 -0
index 3fbaf66..2efa193 100644
@@ -149,6 +149,7 @@ void erofs_put_super(struct erofs_sb_info *sbi)

Does mkfs call erofs_put_super()? I think it needs to be fixed first.

Thanks,
Gao Xiang