Skip to contentSkip to conversation

erofs-utils: lib: use BLK_ROUND_UP() for __erofs_battach()

PATCH New Gao Xiang 1 participant 4 messages Oct 23, 2023 – Oct 25, 2023 Library code

lore
Share & tools
lore

Gao Xiang

author
#0 lore raw

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

Also avoid division in BLK_ROUND_UP().

---
 include/erofs/internal.h |  3 ++-
 lib/cache.c              | 15 ++++++++-------
 2 files changed, 10 insertions(+), 8 deletions(-)
include/erofs/internal.h +2 -1
index c1ff582..4d794ae 100644
@@ -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;
 
lib/cache.c +8 -7
index caca49b..3424e59 100644
@@ -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;
Signature · 1 line
2.39.3
author
#1 lore raw

PATCH 2/3 erofs-utils: get rid of .preflush()

It's actually never used.

---
 include/erofs/cache.h | 1 -
 lib/cache.c           | 5 -----
 2 files changed, 6 deletions(-)
include/erofs/cache.h +0 -1
index de5584e..31f54a4 100644
@@ -30,7 +30,6 @@ struct erofs_buffer_block;
 #define DEVT		5
 
 struct erofs_bhops {
-	bool (*preflush)(struct erofs_buffer_head *bh);
 	bool (*flush)(struct erofs_buffer_head *bh);
 };
 
lib/cache.c +0 -5
index 3424e59..e3cf69e 100644
@@ -380,11 +380,6 @@ bool erofs_bflush(struct erofs_buffer_block *bb)
 		if (p == bb)
 			break;
 
-		/* check if the buffer block can flush */
-		list_for_each_entry(bh, &p->buffers.list, list)
-			if (bh->op->preflush && !bh->op->preflush(bh))
-				return false;
-
 		blkaddr = __erofs_mapbh(p);
 
 		list_for_each_entry_safe(bh, nbh, &p->buffers.list, list) {
Signature · 1 line
2.39.3
author
#2 lore raw

PATCH 3/3 erofs-utils: lib: propagate return code for erofs_bflush()

In reply to Gao Xiang

Instead of just using a boolean.

---
 include/erofs/cache.h |  8 ++++----
 lib/cache.c           | 21 ++++++++++++++-------
 lib/inode.c           | 18 +++++++++---------
 mkfs/main.c           | 14 +++++++-------
 4 files changed, 34 insertions(+), 27 deletions(-)
include/erofs/cache.h +4 -4
index 31f54a4..11fc6ab 100644
@@ -30,7 +30,7 @@ struct erofs_buffer_block;
 #define DEVT		5
 
 struct erofs_bhops {
-	bool (*flush)(struct erofs_buffer_head *bh);
+	int (*flush)(struct erofs_buffer_head *bh);
 };
 
 struct erofs_buffer_head {
@@ -91,11 +91,11 @@ static inline erofs_off_t erofs_btell(struct erofs_buffer_head *bh, bool end)
 		(end ? list_next_entry(bh, list)->off : bh->off);
 }
 
-static inline bool erofs_bh_flush_generic_end(struct erofs_buffer_head *bh)
+static inline int erofs_bh_flush_generic_end(struct erofs_buffer_head *bh)
 {
 	list_del(&bh->list);
 	free(bh);
-	return true;
+	return 0;
 }
 
 struct erofs_buffer_head *erofs_buffer_init(void);
@@ -108,7 +108,7 @@ struct erofs_buffer_head *erofs_battach(struct erofs_buffer_head *bh,
 					int type, unsigned int size);
 
 erofs_blk_t erofs_mapbh(struct erofs_buffer_block *bb);
-bool erofs_bflush(struct erofs_buffer_block *bb);
+int erofs_bflush(struct erofs_buffer_block *bb);
 
 void erofs_bdrop(struct erofs_buffer_head *bh, bool tryrevoke);
 erofs_blk_t erofs_total_metablocks(void);
lib/cache.c +14 -7
index e3cf69e..393b275 100644
@@ -21,7 +21,7 @@ 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 bool erofs_bh_flush_drop_directly(struct erofs_buffer_head *bh)
+static int erofs_bh_flush_drop_directly(struct erofs_buffer_head *bh)
 {
 	return erofs_bh_flush_generic_end(bh);
 }
@@ -30,9 +30,9 @@ const struct erofs_bhops erofs_drop_directly_bhops = {
 	.flush = erofs_bh_flush_drop_directly,
 };
 
-static bool erofs_bh_flush_skip_write(struct erofs_buffer_head *bh)
+static int erofs_bh_flush_skip_write(struct erofs_buffer_head *bh)
 {
-	return false;
+	return -EBUSY;
 }
 
 const struct erofs_bhops erofs_skip_write_bhops = {
@@ -366,7 +366,7 @@ static void erofs_bfree(struct erofs_buffer_block *bb)
 	free(bb);
 }
 
-bool erofs_bflush(struct erofs_buffer_block *bb)
+int erofs_bflush(struct erofs_buffer_block *bb)
 {
 	const unsigned int blksiz = erofs_blksiz(&sbi);
 	struct erofs_buffer_block *p, *n;
@@ -376,6 +376,7 @@ bool erofs_bflush(struct erofs_buffer_block *bb)
 		struct erofs_buffer_head *bh, *nbh;
 		unsigned int padding;
 		bool skip = false;
+		int ret;
 
 		if (p == bb)
 			break;
@@ -383,9 +384,15 @@ bool erofs_bflush(struct erofs_buffer_block *bb)
 		blkaddr = __erofs_mapbh(p);
 
 		list_for_each_entry_safe(bh, nbh, &p->buffers.list, list) {
-			/* flush and remove bh */
-			if (!bh->op->flush(bh))
+			if (bh->op == &erofs_skip_write_bhops) {
 				skip = true;
+				continue;
+			}
+
+			/* flush and remove bh */
+			ret = bh->op->flush(bh);
+			if (ret < 0)
+				return ret;
 		}
 
 		if (skip)
@@ -401,7 +408,7 @@ bool erofs_bflush(struct erofs_buffer_block *bb)
 		erofs_dbg("block %u to %u flushed", p->blkaddr, blkaddr - 1);
 		erofs_bfree(p);
 	}
-	return true;
+	return 0;
 }
 
 void erofs_bdrop(struct erofs_buffer_head *bh, bool tryrevoke)
lib/inode.c +9 -9
index e68b9d1..bcdb4b8 100644
@@ -136,6 +136,7 @@ unsigned int erofs_iput(struct erofs_inode *inode)
 	list_for_each_entry_safe(d, t, &inode->i_subdirs, d_child)
 		free(d);
 
+	free(inode->compressmeta);
 	if (inode->eof_tailraw)
 		free(inode->eof_tailraw);
 	list_del(&inode->i_hash);
@@ -505,7 +506,7 @@ int erofs_write_file(struct erofs_inode *inode, int fd, u64 fpos)
 	return write_uncompressed_file_from_fd(inode, fd);
 }
 
-static bool erofs_bh_flush_write_inode(struct erofs_buffer_head *bh)
+static int erofs_bh_flush_write_inode(struct erofs_buffer_head *bh)
 {
 	struct erofs_inode *const inode = bh->fsprivate;
 	struct erofs_sb_info *sbi = inode->sbi;
@@ -597,19 +598,19 @@ static bool erofs_bh_flush_write_inode(struct erofs_buffer_head *bh)
 
 	ret = dev_write(sbi, &u, off, inode->inode_isize);
 	if (ret)
-		return false;
+		return ret;
 	off += inode->inode_isize;
 
 	if (inode->xattr_isize) {
 		char *xattrs = erofs_export_xattr_ibody(inode);
 
 		if (IS_ERR(xattrs))
-			return false;
+			return PTR_ERR(xattrs);
 
 		ret = dev_write(sbi, xattrs, off, inode->xattr_isize);
 		free(xattrs);
 		if (ret)
-			return false;
+			return ret;
 
 		off += inode->xattr_isize;
 	}
@@ -618,15 +619,14 @@ static bool erofs_bh_flush_write_inode(struct erofs_buffer_head *bh)
 		if (inode->datalayout == EROFS_INODE_CHUNK_BASED) {
 			ret = erofs_blob_write_chunk_indexes(inode, off);
 			if (ret)
-				return false;
+				return ret;
 		} else {
 			/* write compression metadata */
 			off = roundup(off, 8);
 			ret = dev_write(sbi, inode->compressmeta, off,
 					inode->extent_isize);
 			if (ret)
-				return false;
-			free(inode->compressmeta);
+				return ret;
 		}
 	}
 
@@ -737,7 +737,7 @@ noinline:
 	return 0;
 }
 
-static bool erofs_bh_flush_write_inline(struct erofs_buffer_head *bh)
+static int erofs_bh_flush_write_inline(struct erofs_buffer_head *bh)
 {
 	struct erofs_inode *const inode = bh->fsprivate;
 	const erofs_off_t off = erofs_btell(bh, false);
@@ -745,7 +745,7 @@ static bool erofs_bh_flush_write_inline(struct erofs_buffer_head *bh)
 
 	ret = dev_write(inode->sbi, inode->idata, off, inode->idata_size);
 	if (ret)
-		return false;
+		return ret;
 
 	inode->idata_size = 0;
 	free(inode->idata);
mkfs/main.c +7 -7
index 6d2b700..637d1b9 100644
@@ -1170,10 +1170,9 @@ int main(int argc, char **argv)
 	}
 
 	/* flush all buffers except for the superblock */
-	if (!erofs_bflush(NULL)) {
-		err = -EIO;
+	err = erofs_bflush(NULL);
+	if (err)
 		goto exit;
-	}
 
 	err = erofs_mkfs_update_super_block(sb_bh, root_nid, &nblocks,
 					    packed_nid);
@@ -1181,10 +1180,11 @@ int main(int argc, char **argv)
 		goto exit;
 
 	/* flush all remaining buffers */
-	if (!erofs_bflush(NULL))
-		err = -EIO;
-	else
-		err = dev_resize(&sbi, nblocks);
+	err = erofs_bflush(NULL);
+	if (err)
+		goto exit;
+
+	err = dev_resize(&sbi, nblocks);
 
 	if (!err && erofs_sb_has_sb_chksum(&sbi))
 		err = erofs_mkfs_superblock_csum_set();
Signature · 1 line
2.39.3
author
#3 lore raw

PATCH v2 3/3 erofs-utils: lib: propagate return code for erofs_bflush()

Instead of just using a boolean.

---
 include/erofs/cache.h |  8 ++++----
 lib/cache.c           | 21 ++++++++++++++-------
 lib/compress.c        |  1 +
 lib/inode.c           | 18 +++++++++---------
 mkfs/main.c           | 14 +++++++-------
 5 files changed, 35 insertions(+), 27 deletions(-)
include/erofs/cache.h +4 -4
index 31f54a4..11fc6ab 100644
@@ -30,7 +30,7 @@ struct erofs_buffer_block;
 #define DEVT		5
 
 struct erofs_bhops {
-	bool (*flush)(struct erofs_buffer_head *bh);
+	int (*flush)(struct erofs_buffer_head *bh);
 };
 
 struct erofs_buffer_head {
@@ -91,11 +91,11 @@ static inline erofs_off_t erofs_btell(struct erofs_buffer_head *bh, bool end)
 		(end ? list_next_entry(bh, list)->off : bh->off);
 }
 
-static inline bool erofs_bh_flush_generic_end(struct erofs_buffer_head *bh)
+static inline int erofs_bh_flush_generic_end(struct erofs_buffer_head *bh)
 {
 	list_del(&bh->list);
 	free(bh);
-	return true;
+	return 0;
 }
 
 struct erofs_buffer_head *erofs_buffer_init(void);
@@ -108,7 +108,7 @@ struct erofs_buffer_head *erofs_battach(struct erofs_buffer_head *bh,
 					int type, unsigned int size);
 
 erofs_blk_t erofs_mapbh(struct erofs_buffer_block *bb);
-bool erofs_bflush(struct erofs_buffer_block *bb);
+int erofs_bflush(struct erofs_buffer_block *bb);
 
 void erofs_bdrop(struct erofs_buffer_head *bh, bool tryrevoke);
 erofs_blk_t erofs_total_metablocks(void);
lib/cache.c +14 -7
index e3cf69e..393b275 100644
@@ -21,7 +21,7 @@ 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 bool erofs_bh_flush_drop_directly(struct erofs_buffer_head *bh)
+static int erofs_bh_flush_drop_directly(struct erofs_buffer_head *bh)
 {
 	return erofs_bh_flush_generic_end(bh);
 }
@@ -30,9 +30,9 @@ const struct erofs_bhops erofs_drop_directly_bhops = {
 	.flush = erofs_bh_flush_drop_directly,
 };
 
-static bool erofs_bh_flush_skip_write(struct erofs_buffer_head *bh)
+static int erofs_bh_flush_skip_write(struct erofs_buffer_head *bh)
 {
-	return false;
+	return -EBUSY;
 }
 
 const struct erofs_bhops erofs_skip_write_bhops = {
@@ -366,7 +366,7 @@ static void erofs_bfree(struct erofs_buffer_block *bb)
 	free(bb);
 }
 
-bool erofs_bflush(struct erofs_buffer_block *bb)
+int erofs_bflush(struct erofs_buffer_block *bb)
 {
 	const unsigned int blksiz = erofs_blksiz(&sbi);
 	struct erofs_buffer_block *p, *n;
@@ -376,6 +376,7 @@ bool erofs_bflush(struct erofs_buffer_block *bb)
 		struct erofs_buffer_head *bh, *nbh;
 		unsigned int padding;
 		bool skip = false;
+		int ret;
 
 		if (p == bb)
 			break;
@@ -383,9 +384,15 @@ bool erofs_bflush(struct erofs_buffer_block *bb)
 		blkaddr = __erofs_mapbh(p);
 
 		list_for_each_entry_safe(bh, nbh, &p->buffers.list, list) {
-			/* flush and remove bh */
-			if (!bh->op->flush(bh))
+			if (bh->op == &erofs_skip_write_bhops) {
 				skip = true;
+				continue;
+			}
+
+			/* flush and remove bh */
+			ret = bh->op->flush(bh);
+			if (ret < 0)
+				return ret;
 		}
 
 		if (skip)
@@ -401,7 +408,7 @@ bool erofs_bflush(struct erofs_buffer_block *bb)
 		erofs_dbg("block %u to %u flushed", p->blkaddr, blkaddr - 1);
 		erofs_bfree(p);
 	}
-	return true;
+	return 0;
 }
 
 void erofs_bdrop(struct erofs_buffer_head *bh, bool tryrevoke)
lib/compress.c +1 -0
index f6dc12a..5900233 100644
@@ -1045,6 +1045,7 @@ err_bdrop:
 	erofs_bdrop(bh, true);	/* revoke buffer */
 err_free_meta:
 	free(compressmeta);
+	inode->compressmeta = NULL;
 	return ret;
 }
 
lib/inode.c +9 -9
index 8409ccd..98892af 100644
@@ -117,6 +117,7 @@ unsigned int erofs_iput(struct erofs_inode *inode)
 	list_for_each_entry_safe(d, t, &inode->i_subdirs, d_child)
 		free(d);
 
+	free(inode->compressmeta);
 	if (inode->eof_tailraw)
 		free(inode->eof_tailraw);
 	list_del(&inode->i_hash);
@@ -486,7 +487,7 @@ int erofs_write_file(struct erofs_inode *inode, int fd, u64 fpos)
 	return write_uncompressed_file_from_fd(inode, fd);
 }
 
-static bool erofs_bh_flush_write_inode(struct erofs_buffer_head *bh)
+static int erofs_bh_flush_write_inode(struct erofs_buffer_head *bh)
 {
 	struct erofs_inode *const inode = bh->fsprivate;
 	struct erofs_sb_info *sbi = inode->sbi;
@@ -578,19 +579,19 @@ static bool erofs_bh_flush_write_inode(struct erofs_buffer_head *bh)
 
 	ret = dev_write(sbi, &u, off, inode->inode_isize);
 	if (ret)
-		return false;
+		return ret;
 	off += inode->inode_isize;
 
 	if (inode->xattr_isize) {
 		char *xattrs = erofs_export_xattr_ibody(inode);
 
 		if (IS_ERR(xattrs))
-			return false;
+			return PTR_ERR(xattrs);
 
 		ret = dev_write(sbi, xattrs, off, inode->xattr_isize);
 		free(xattrs);
 		if (ret)
-			return false;
+			return ret;
 
 		off += inode->xattr_isize;
 	}
@@ -599,15 +600,14 @@ static bool erofs_bh_flush_write_inode(struct erofs_buffer_head *bh)
 		if (inode->datalayout == EROFS_INODE_CHUNK_BASED) {
 			ret = erofs_blob_write_chunk_indexes(inode, off);
 			if (ret)
-				return false;
+				return ret;
 		} else {
 			/* write compression metadata */
 			off = roundup(off, 8);
 			ret = dev_write(sbi, inode->compressmeta, off,
 					inode->extent_isize);
 			if (ret)
-				return false;
-			free(inode->compressmeta);
+				return ret;
 		}
 	}
 
@@ -718,7 +718,7 @@ noinline:
 	return 0;
 }
 
-static bool erofs_bh_flush_write_inline(struct erofs_buffer_head *bh)
+static int erofs_bh_flush_write_inline(struct erofs_buffer_head *bh)
 {
 	struct erofs_inode *const inode = bh->fsprivate;
 	const erofs_off_t off = erofs_btell(bh, false);
@@ -726,7 +726,7 @@ static bool erofs_bh_flush_write_inline(struct erofs_buffer_head *bh)
 
 	ret = dev_write(inode->sbi, inode->idata, off, inode->idata_size);
 	if (ret)
-		return false;
+		return ret;
 
 	inode->idata_size = 0;
 	free(inode->idata);
mkfs/main.c +7 -7
index 6d2b700..637d1b9 100644
@@ -1170,10 +1170,9 @@ int main(int argc, char **argv)
 	}
 
 	/* flush all buffers except for the superblock */
-	if (!erofs_bflush(NULL)) {
-		err = -EIO;
+	err = erofs_bflush(NULL);
+	if (err)
 		goto exit;
-	}
 
 	err = erofs_mkfs_update_super_block(sb_bh, root_nid, &nblocks,
 					    packed_nid);
@@ -1181,10 +1180,11 @@ int main(int argc, char **argv)
 		goto exit;
 
 	/* flush all remaining buffers */
-	if (!erofs_bflush(NULL))
-		err = -EIO;
-	else
-		err = dev_resize(&sbi, nblocks);
+	err = erofs_bflush(NULL);
+	if (err)
+		goto exit;
+
+	err = dev_resize(&sbi, nblocks);
 
 	if (!err && erofs_sb_has_sb_chksum(&sbi))
 		err = erofs_mkfs_superblock_csum_set();
Signature · 1 line
2.39.3