git: 5c0715d55ed5 - main - ext2fs: fix post-commit review issues in multi-block allocator

From: Kirk McKusick <mckusick_at_FreeBSD.org>
Date: Tue, 24 Nov 2026 03:25:50 UTC
The branch main has been updated by mckusick:

URL: https://cgit.FreeBSD.org/src/commit/?id=5c0715d55ed577eeb71078490d2c14df9fd98746

commit 5c0715d55ed577eeb71078490d2c14df9fd98746
Author:     Kirk McKusick <mckusick@FreeBSD.org>
AuthorDate: 2026-11-24 03:23:35 +0000
Commit:     Kirk McKusick <mckusick@FreeBSD.org>
CommitDate: 2026-11-24 03:25:24 +0000

    ext2fs: fix post-commit review issues in multi-block allocator
    
    Fix 8 issues identified in post-commit review of e590490:
    
    POSTCR-1: ext2_clusteralloc() asserted MA_NOTOWNED while holding
    EXT2_LOCK, causing INVARIANTS panic on successful cluster lookup.
    Changed assertion to MA_OWNED.
    
    POSTCR-2: ext2_balloc() IO_SYNC and ext4_ext_split() error paths freed
    blocks after bwrite() failure, but brelse() had redirtied the buffer
    which still named the freed block. Added B_INVALONERR flag before
    bwrite() to invalidate buffers on error (4 locations in ext2_balloc.c,
    3 in ext2_extents.c).
    
    POSTCR-3: ext4_ext_get_blocks() freed new run when ext4_ext_dirty()
    failed, but extent remained in i_data or cached leaf buffer. Simplified
    ext4_ext_dirty() error handling to use B_INVALONERR instead of manual
    getblk/bundirty/brelse.
    
    POSTCR-4: ext2_run_desired_length() truncated 64-bit e2fs_fbcount to
    uint32_t, causing spurious ENOSPC on filesystems with >2^32 free blocks.
    Changed all comparisons to use uint64_t arithmetic.
    
    POSTCR-5: ext4_ext_grow_indepth() double brelse() on bp after failed
    bwrite() had already released it. Set bp = NULL before goto out.
    
    POSTCR-6: ext4_ext_split() and ext4_ext_grow_indepth() returned 0 when
    ext4_ext_alloc_meta() failed, leading to buffer overflow or endless loop.
    Both now explicitly return ENOSPC.
    
    POSTCR-7: ext2_balloc() freed new indirect block after bwrite() failure
    while redirtied buffer stayed on vnode. Added B_INVALONERR to child
    indirect block writes (2 locations).
    
    POSTCR-8: ext2_clusteralloc() modified block bitmap without updating
    metadata_csum checksum. Added ext2_gd_b_bitmap_csum_set() and
    ext2_b_bitmap_validate() before bdwrite().
    
    Submitted by: Pedro Giffuni <pfg@freebsd.org>
    Reviewed by:  Kirk McKusick
    Reported by:  llm-postcr
    Differential Revision: https://reviews.freebsd.org/D61937
    MFC-after:    2 weeks
---
 sys/fs/ext2fs/ext2_alloc.c   | 25 ++++++++++++++++----
 sys/fs/ext2fs/ext2_balloc.c  |  4 ++++
 sys/fs/ext2fs/ext2_extents.c | 54 ++++++++++++++++++++++++++------------------
 3 files changed, 56 insertions(+), 27 deletions(-)

diff --git a/sys/fs/ext2fs/ext2_alloc.c b/sys/fs/ext2fs/ext2_alloc.c
index f6cd624ac542..afdb068d8a7f 100644
--- a/sys/fs/ext2fs/ext2_alloc.c
+++ b/sys/fs/ext2fs/ext2_alloc.c
@@ -190,12 +190,12 @@ ext2_run_desired_length(struct inode *ip, uint32_t logical_length,
 	fs = ip->i_e2fs;
 
 	/* Hard caps: free blocks, cluster summaries, group size, sysctl. */
-	maxlen = (uint32_t)fs->e2fs_fbcount;
-	if (fs->e2fs_contigsumsize > 0 && (uint32_t)fs->e2fs_contigsumsize < maxlen)
+	maxlen = fs->e2fs_fbcount;
+	if (fs->e2fs_contigsumsize > 0 && (uint64_t)fs->e2fs_contigsumsize < maxlen)
 		maxlen = fs->e2fs_contigsumsize;
-	if ((uint32_t)fs->e2fs_bpg < maxlen)
+	if ((uint64_t)fs->e2fs_bpg < maxlen)
 		maxlen = fs->e2fs_bpg;
-	if ((uint32_t)ext2_alloc_max_run < maxlen)
+	if ((uint64_t)ext2_alloc_max_run < maxlen)
 		maxlen = ext2_alloc_max_run;
 	if (maxlen < logical_length)
 		return (0);
@@ -1617,7 +1617,7 @@ ext2_clusteralloc(struct inode *ip, int cg, daddr_t bpref, int len)
 		fs->e2fs_maxcluster[cg] = i;
 		goto fail;
 	}
-	mtx_assert(EXT2_MTX(ump), MA_NOTOWNED);
+	mtx_assert(EXT2_MTX(ump), MA_OWNED);
 	EXT2_UNLOCK(ump);
 
 	/* Search the bitmap to find a big enough cluster like in FFS. */
@@ -1656,6 +1656,19 @@ ext2_clusteralloc(struct inode *ip, int cg, daddr_t bpref, int len)
 		panic("ext2_clusteralloc: allocated out of group");
 
 	EXT2_LOCK(ump);
+	if (EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_GDT_CSUM) ||
+	    EXT2_HAS_RO_COMPAT_FEATURE(fs, EXT2F_ROCOMPAT_METADATA_CKSUM)) {
+		error = ext2_gd_b_bitmap_csum_verify(fs, cg, bp);
+		if (error) {
+			EXT2_UNLOCK(ump);
+			goto fail_lock;
+		}
+	}
+	error = ext2_b_bitmap_validate(fs, bp, cg);
+	if (error) {
+		EXT2_UNLOCK(ump);
+		goto fail_lock;
+	}
 	for (i = 0; i < len; i += fs->e2fs_fpb) {
 		setbit(bbp, bno + i);
 		ext2_clusteracct(fs, bbp, cg, bno + i, -1);
@@ -1666,6 +1679,8 @@ ext2_clusteralloc(struct inode *ip, int cg, daddr_t bpref, int len)
 	fs->e2fs_fmod = 1;
 	EXT2_UNLOCK(ump);
 
+	ext2_gd_b_bitmap_csum_set(fs, cg, bp);
+	ext2_b_bitmap_validate(fs, bp, cg);
 	bdwrite(bp);
 	return (cg * fs->e2fs_bpg + le32toh(fs->e2fs->e2fs_first_dblock)
 	    + bno);
diff --git a/sys/fs/ext2fs/ext2_balloc.c b/sys/fs/ext2fs/ext2_balloc.c
index bfbd4a417f61..224b70173c1b 100644
--- a/sys/fs/ext2fs/ext2_balloc.c
+++ b/sys/fs/ext2fs/ext2_balloc.c
@@ -215,6 +215,7 @@ ext2_balloc(struct inode *ip, e2fs_lbn_t lbn, int size, struct ucred *cred,
 		 * Write synchronously so that indirect blocks
 		 * never point at garbage.
 		 */
+		bp->b_flags |= B_INVALONERR;
 		if ((error = bwrite(bp)) != 0) {
 			/* bwrite() released bp; pref is still unreferenced. */
 			ext2_rollback_unpublished(ip, pref, 1);
@@ -264,6 +265,7 @@ ext2_balloc(struct inode *ip, e2fs_lbn_t lbn, int size, struct ucred *cred,
 		 * Write synchronously so that indirect blocks
 		 * never point at garbage.
 		 */
+		nbp->b_flags |= B_INVALONERR;
 		if ((error = bwrite(nbp)) != 0) {
 			/* bwrite() released nbp; pref is still unreferenced. */
 			ext2_rollback_unpublished(ip, pref, 1);
@@ -278,6 +280,7 @@ ext2_balloc(struct inode *ip, e2fs_lbn_t lbn, int size, struct ucred *cred,
 		 * at the top of the next iteration.
 		 */
 		if (flags & IO_SYNC) {
+			bp->b_flags |= B_INVALONERR;
 			if ((error = bwrite(bp)) != 0) {
 				/*
 				 * The child indirect block is on disk but
@@ -340,6 +343,7 @@ ext2_balloc(struct inode *ip, e2fs_lbn_t lbn, int size, struct ucred *cred,
 		 * delayed write.
 		 */
 		if (flags & IO_SYNC) {
+			bp->b_flags |= B_INVALONERR;
 			if ((error = bwrite(bp)) != 0) {
 				/*
 				 * The parent indirect block did not reach
diff --git a/sys/fs/ext2fs/ext2_extents.c b/sys/fs/ext2fs/ext2_extents.c
index b7c203f4ed50..3d02fe06bd52 100644
--- a/sys/fs/ext2fs/ext2_extents.c
+++ b/sys/fs/ext2fs/ext2_extents.c
@@ -830,20 +830,9 @@ ext4_ext_dirty(struct inode *ip, struct ext4_extent_path *path)
 			return (EIO);
 		ext4_ext_fill_path_buf(path, bp);
 		ext2_extent_blk_csum_set(ip, bp->b_data);
+		bp->b_flags |= B_INVALONERR;
 		error = bwrite(bp);
 		if (error) {
-			/*
-			 * bwrite() left the modified extent tree dirty in
-			 * the cache.  Discard it, so a later read sees the
-			 * on-disk tree instead of one naming blocks the
-			 * caller's rollback is about to free.
-			 */
-			bp = getblk(ip->i_devvp, fsbtodb(fs, blk),
-			    fs->e2fs_bsize, 0, 0, 0);
-			if (bp) {
-				bundirty(bp);
-				brelse(bp);
-			}
 			return (error);
 		}
 	} else {
@@ -972,8 +961,10 @@ ext4_ext_split(struct inode *ip, struct ext4_extent_path *path,
 	    M_EXT2EXTENTS, M_WAITOK | M_ZERO);
 	for (a = 0; a < depth - at; a++) {
 		newblk = ext4_ext_alloc_meta(ip);
-		if (newblk == 0)
+		if (newblk == 0) {
+			error = ENOSPC;
 			goto cleanup;
+		}
 		ablks[a] = newblk;
 	}
 
@@ -1013,6 +1004,7 @@ ext4_ext_split(struct inode *ip, struct ext4_extent_path *path,
 	}
 
 	ext2_extent_blk_csum_set(ip, bp->b_data);
+	bp->b_flags |= B_INVALONERR;
 	error = bwrite(bp);
 	bp = NULL;
 	if (error)
@@ -1063,14 +1055,16 @@ ext4_ext_split(struct inode *ip, struct ext4_extent_path *path,
 			neh->eh_ecount = htole16(le16toh(neh->eh_ecount) + m);
 		}
 
-		ext2_extent_blk_csum_set(ip, bp->b_data);
-		error = bwrite(bp);
-		bp = NULL;
-		if (error)
-			goto cleanup;
+ext2_extent_blk_csum_set(ip, bp->b_data);
+	bp->b_flags |= B_INVALONERR;
+	error = bwrite(bp);
+	bp = NULL;
+	if (error) {
+		goto cleanup;
+	}
 
-		/* Fix old index. */
-		if (m) {
+	/* Fix old index. */
+	if (m) {
 			path[i].ep_header->eh_ecount =
 			    htole16(le16toh(path[i].ep_header->eh_ecount) - m);
 			error = ext4_ext_dirty(ip, path + i);
@@ -1117,7 +1111,7 @@ ext4_ext_grow_indepth(struct inode *ip, struct ext4_extent_path *path,
 
 	newblk = ext4_ext_alloc_meta(ip);
 	if (newblk == 0)
-		return (error);
+		return (ENOSPC);
 
 	bp = getblk(ip->i_devvp, fsbtodb(fs, newblk), fs->e2fs_bsize, 0, 0, 0);
 	if (!bp) {
@@ -1138,9 +1132,11 @@ ext4_ext_grow_indepth(struct inode *ip, struct ext4_extent_path *path,
 		neh->eh_max = htole16(ext4_ext_space_block(ip));
 
 	ext2_extent_blk_csum_set(ip, bp->b_data);
+	bp->b_flags |= B_INVALONERR;
 	error = bwrite(bp);
 	if (error) {
 		ext2_rollback_unpublished(ip, newblk, 1);
+		bp = NULL;
 		goto out;
 	}
 
@@ -1274,6 +1270,8 @@ ext4_ext_insert_extent(struct inode *ip, struct ext4_extent_path *path,
 	struct ext4_extent *ex, *nex, *nearex;
 	struct ext4_extent_path *npath;
 	int depth, len, error, next;
+	char saved_i_data[sizeof(ip->i_data)];
+	bool saved_i_data_valid = false;
 
 	depth = ext4_ext_inode_depth(ip);
 	ex = path[depth].ep_ext;
@@ -1284,6 +1282,10 @@ ext4_ext_insert_extent(struct inode *ip, struct ext4_extent_path *path,
 
 	/* Insert block into found extent. */
 	if (ex && ext4_can_extents_be_merged(ex, newext)) {
+		if (depth == 0) {
+			memcpy(saved_i_data, ip->i_data, sizeof(ip->i_data));
+			saved_i_data_valid = true;
+		}
 		ex->e_len = htole16(le16toh(ex->e_len) + le16toh(newext->e_len));
 		eh = path[depth].ep_header;
 		nearex = ex;
@@ -1332,6 +1334,10 @@ repeat:
 	eh = path[depth].ep_header;
 
 has_space:
+	if (depth == 0 && !saved_i_data_valid) {
+		memcpy(saved_i_data, ip->i_data, sizeof(ip->i_data));
+		saved_i_data_valid = true;
+	}
 	nearex = path[depth].ep_ext;
 	if (!nearex) {
 		/* Create new extent in the leaf. */
@@ -1387,8 +1393,12 @@ merge:
 		goto cleanup;
 
 	error = ext4_ext_dirty(ip, path + depth);
-	if (error)
+	if (error) {
+		if (depth == 0 && saved_i_data_valid) {
+			memcpy(ip->i_data, saved_i_data, sizeof(ip->i_data));
+		}
 		goto cleanup;
+	}
 
 	/*
 	 * ext4_ext_dirty() wrote the tree out, so the extent now names