[PATCH] erofs-utils: lib: switch !bh cases in erofs_write_tail_end()

Gao Xiang hsiangkao at linux.alibaba.com
Mon Jan 19 13:37:05 AEDT 2026


To use erofs_allocate_inode_bh_data().

Signed-off-by: Gao Xiang <hsiangkao at linux.alibaba.com>
---
 lib/inode.c | 55 ++++++++++++++++++++++++++++-------------------------
 1 file changed, 29 insertions(+), 26 deletions(-)

diff --git a/lib/inode.c b/lib/inode.c
index 3abc7c1..e3ee79a 100644
--- a/lib/inode.c
+++ b/lib/inode.c
@@ -635,6 +635,7 @@ static int erofs_write_unencoded_data(struct erofs_inode *inode,
 				      bool noseek, bool in_metazone)
 {
 	struct erofs_sb_info *sbi = inode->sbi;
+	struct erofs_buffer_head *bh;
 	struct erofs_bufmgr *bmgr;
 	erofs_off_t remaining, pos;
 	unsigned int len;
@@ -663,16 +664,19 @@ static int erofs_write_unencoded_data(struct erofs_inode *inode,
 	if (ret)
 		return ret;
 
-	bmgr = in_metazone ? erofs_metadata_bmgr(sbi, false) : sbi->bmgr;
-	pos = erofs_pos(sbi, erofs_inode_dev_baddr(inode));
-	while (remaining) {
-		len = min_t(u64, round_down(UINT_MAX, 1U << sbi->blkszbits),
-			    remaining);
-		ret = erofs_io_xcopy(bmgr->vf, pos, vf, len, noseek);
-		if (ret)
-			return ret;
-		pos += len;
-		remaining -= len;
+	bh = inode->bh_data;
+	if (bh) {
+		bmgr = (struct erofs_bufmgr *)bh->block->buffers.fsprivate;
+		pos = erofs_btell(bh, false);
+		do {
+			len = min_t(u64, remaining,
+				    round_down(UINT_MAX, 1U << sbi->blkszbits));
+			ret = erofs_io_xcopy(bmgr->vf, pos, vf, len, noseek);
+			if (ret)
+				return ret;
+			pos += len;
+			remaining -= len;
+		} while (remaining);
 	}
 
 	/* read the tail-end data */
@@ -1071,9 +1075,11 @@ static struct erofs_bhops erofs_write_inline_bhops = {
 	.flush = erofs_bh_flush_write_inline,
 };
 
-static int erofs_write_tail_end(struct erofs_inode *inode)
+static int erofs_write_tail_end(struct erofs_importer *im,
+				struct erofs_inode *inode)
 {
 	static const u8 zeroed[EROFS_MAX_BLOCK_SIZE];
+	const struct erofs_importer_params *params = im->params;
 	struct erofs_sb_info *sbi = inode->sbi;
 	struct erofs_buffer_head *bh, *ibh;
 
@@ -1094,20 +1100,17 @@ static int erofs_write_tail_end(struct erofs_inode *inode)
 		struct iovec iov[2];
 		erofs_off_t pos;
 		int ret;
-		bool h0;
+		bool h0, in_metazone;
 
 		if (!bh) {
-			bh = erofs_balloc(sbi->bmgr,
-					  S_ISDIR(inode->i_mode) ? DIRA: DATA,
-					  erofs_blksiz(sbi), 0);
-			if (IS_ERR(bh))
-				return PTR_ERR(bh);
-			bh->op = &erofs_skip_write_bhops;
-
-			/* get blkaddr of bh */
-			ret = erofs_mapbh(NULL, bh->block);
-			inode->u.i_blkaddr = bh->block->blkaddr;
-			inode->bh_data = bh;
+			in_metazone = S_ISDIR(inode->i_mode) &&
+				params->dirdata_in_metazone;
+
+			ret = erofs_allocate_inode_bh_data(inode, 1,
+							   in_metazone);
+			if (ret)
+				return ret;
+			bh = inode->bh_data;
 		} else {
 			if (inode->lazy_tailblock) {
 				/* expend a tail block (should be successful) */
@@ -1545,7 +1548,7 @@ static int erofs_mkfs_handle_nondirectory(const struct erofs_mkfs_btctx *btctx,
 	if (ret)
 		return ret;
 	erofs_prepare_inode_buffer(btctx->im, inode);
-	erofs_write_tail_end(inode);
+	erofs_write_tail_end(btctx->im, inode);
 	return 0;
 }
 
@@ -1620,7 +1623,7 @@ static int erofs_mkfs_jobfn(const struct erofs_mkfs_btctx *ctx,
 		ret = erofs_write_dir_file(ctx->im, inode);
 		if (ret)
 			return ret;
-		erofs_write_tail_end(inode);
+		erofs_write_tail_end(ctx->im, inode);
 		inode->bh->op = &erofs_write_inode_bhops;
 		erofs_iput(inode);
 		return 0;
@@ -2372,7 +2375,7 @@ struct erofs_inode *erofs_mkfs_build_special_from_fd(struct erofs_importer *im,
 		return ERR_PTR(ret);
 out:
 	erofs_prepare_inode_buffer(im, inode);
-	erofs_write_tail_end(inode);
+	erofs_write_tail_end(im, inode);
 	return inode;
 }
 
-- 
2.43.0



More information about the Linux-erofs mailing list