git: 4313e2ae44ba - main - Avoid lost buffers in fsck_ffs.

From: Kirk McKusick <mckusick_at_FreeBSD.org>
Date: Thu, 07 Oct 2021 22:53:43 UTC
The branch main has been updated by mckusick:

URL: https://cgit.FreeBSD.org/src/commit/?id=4313e2ae44ba4e416a7ddaeaccf8ad311902f1c8

commit 4313e2ae44ba4e416a7ddaeaccf8ad311902f1c8
Author:     Kirk McKusick <mckusick@FreeBSD.org>
AuthorDate: 2021-10-07 22:51:56 +0000
Commit:     Kirk McKusick <mckusick@FreeBSD.org>
CommitDate: 2021-10-07 22:52:58 +0000

    Avoid lost buffers in fsck_ffs.
    
    The ino_blkatoff() and indir_blkatoff() functions failed to release
    the buffers holding second and third level indirect blocks. This
    commit ensures that these buffers are now properly released.
    
    MFC after:    1 week
    Sponsored by: Netflix
---
 sbin/fsck_ffs/inode.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)

diff --git a/sbin/fsck_ffs/inode.c b/sbin/fsck_ffs/inode.c
index ba2d5892238e..dafc99bd92da 100644
--- a/sbin/fsck_ffs/inode.c
+++ b/sbin/fsck_ffs/inode.c
@@ -264,6 +264,8 @@ ino_blkatoff(union dinode *dp, ino_t ino, ufs_lbn_t lbn, int *frags,
 	int i;
 
 	*frags = 0;
+	if (bpp != NULL)
+		*bpp = NULL;
 	/*
 	 * Handle extattr blocks first.
 	 */
@@ -300,6 +302,8 @@ ino_blkatoff(union dinode *dp, ino_t ino, ufs_lbn_t lbn, int *frags,
 			continue;
 		if (lbn > 0 && lbn >= next)
 			continue;
+		if (DIP(dp, di_ib[i]) == 0)
+			return (0);
 		return (indir_blkatoff(DIP(dp, di_ib[i]), ino, -cur - i, lbn,
 		    bpp));
 	}
@@ -321,8 +325,6 @@ indir_blkatoff(ufs2_daddr_t blk, ino_t ino, ufs_lbn_t cur, ufs_lbn_t lbn,
 	ufs_lbn_t base;
 	int i, level;
 
-	if (blk == 0)
-		return (0);
 	level = lbn_level(cur);
 	if (level == -1)
 		pfatal("Invalid indir lbn %jd in ino %ju\n",
@@ -352,12 +354,14 @@ indir_blkatoff(ufs2_daddr_t blk, ino_t ino, ufs_lbn_t cur, ufs_lbn_t lbn,
 		return (0);
 	blk = IBLK(bp, i);
 	bp->b_index = i;
-	if (bpp != NULL)
-		*bpp = bp;
-	else
-		brelse(bp);
-	if (cur == lbn)
+	if (cur == lbn || blk == 0) {
+		if (bpp != NULL)
+			*bpp = bp;
+		else
+			brelse(bp);
 		return (blk);
+	}
+	brelse(bp);
 	if (level == 0)
 		pfatal("Invalid lbn %jd at level 0 for ino %ju\n", lbn,
 		    (uintmax_t)ino);