[PATCH 1/3] erofs-utils: lib: use BLK_ROUND_UP() for __erofs_battach()

Gao Xiang hsiangkao at linux.alibaba.com
Mon Oct 23 19:12:39 AEDT 2023


Also avoid division in BLK_ROUND_UP().

Signed-off-by: Gao Xiang <hsiangkao at linux.alibaba.com>
---
 include/erofs/internal.h |  3 ++-
 lib/cache.c              | 15 ++++++++-------
 2 files changed, 10 insertions(+), 8 deletions(-)

diff --git a/include/erofs/internal.h b/include/erofs/internal.h
index c1ff582..4d794ae 100644
--- a/include/erofs/internal.h
+++ b/include/erofs/internal.h
@@ -49,7 +49,8 @@ extern struct erofs_sb_info sbi;
 #define erofs_blknr(sbi, addr)  ((addr) >> (sbi)->blkszbits)
 #define erofs_blkoff(sbi, addr) ((addr) & (erofs_blksiz(sbi) - 1))
 #define erofs_pos(sbi, nr)      ((erofs_off_t)(nr) << (sbi)->blkszbits)
-#define BLK_ROUND_UP(sbi, addr)	DIV_ROUND_UP(addr, erofs_blksiz(sbi))
+#define BLK_ROUND_UP(sbi, addr)	\
+	(roundup(addr, erofs_blksiz(sbi)) >> (sbi)->blkszbits)
 
 struct erofs_buffer_head;
 
diff --git a/lib/cache.c b/lib/cache.c
index caca49b..3424e59 100644
--- a/lib/cache.c
+++ b/lib/cache.c
@@ -79,9 +79,10 @@ static int __erofs_battach(struct erofs_buffer_block *bb,
 {
 	const unsigned int blksiz = erofs_blksiz(&sbi);
 	const unsigned int blkmask = blksiz - 1;
-	const erofs_off_t alignedoffset = roundup(bb->buffers.off, alignsize);
-	const int oob = cmpsgn(roundup(((bb->buffers.off - 1) & blkmask) + 1,
-				       alignsize) + incr + extrasize, blksiz);
+	erofs_off_t boff = bb->buffers.off;
+	const erofs_off_t alignedoffset = roundup(boff, alignsize);
+	const int oob = cmpsgn(roundup(((boff - 1) & blkmask) + 1, alignsize) +
+					incr + extrasize, blksiz);
 	bool tailupdate = false;
 	erofs_blk_t blkaddr;
 
@@ -93,7 +94,7 @@ static int __erofs_battach(struct erofs_buffer_block *bb,
 		blkaddr = bb->blkaddr;
 		if (blkaddr != NULL_ADDR) {
 			tailupdate = (tail_blkaddr == blkaddr +
-				      DIV_ROUND_UP(bb->buffers.off, blksiz));
+				      BLK_ROUND_UP(&sbi, boff));
 			if (oob && !tailupdate)
 				return -EINVAL;
 		}
@@ -105,11 +106,11 @@ static int __erofs_battach(struct erofs_buffer_block *bb,
 			bh->block = bb;
 			list_add_tail(&bh->list, &bb->buffers.list);
 		}
-		bb->buffers.off = alignedoffset + incr;
+		boff = alignedoffset + incr;
+		bb->buffers.off = boff;
 		/* need to update the tail_blkaddr */
 		if (tailupdate)
-			tail_blkaddr = blkaddr +
-					DIV_ROUND_UP(bb->buffers.off, blksiz);
+			tail_blkaddr = blkaddr + BLK_ROUND_UP(&sbi, boff);
 		erofs_bupdate_mapped(bb);
 	}
 	return ((alignedoffset + incr - 1) & blkmask) + 1;
-- 
2.39.3



More information about the Linux-erofs mailing list