git: d74a8305b0ca - stable/13 - makefs: call brelse from bread

From: Ed Maste <emaste_at_FreeBSD.org>
Date: Thu, 27 Apr 2023 16:52:52 UTC
The branch stable/13 has been updated by emaste:

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

commit d74a8305b0ca1b12b16b92146e8e40440d4e408e
Author:     Ed Maste <emaste@FreeBSD.org>
AuthorDate: 2023-03-13 21:00:20 +0000
Commit:     Ed Maste <emaste@FreeBSD.org>
CommitDate: 2023-04-27 16:45:37 +0000

    makefs: call brelse from bread
    
    This matches NetBSD and rationalizes makefs with the kernel API.
    
    This reverts commit 370e009188ba90c3290b1479aa06ec98b66e140a.
    
    Reviewed by:    mckusick
    Sponsored by:   The FreeBSD Foundation
    Obtained from:  NetBSD 0a62dad69f62, 0c4125e1a19f, cb6a5a3575fd
    Differential Revision:  https://reviews.freebsd.org/D39070
    
    (cherry picked from commit e5551216d8e563043248bc7f3cb1c75b2dbd9791)
---
 usr.sbin/makefs/ffs.c                 |  1 -
 usr.sbin/makefs/ffs/buf.c             | 25 +++++++++++++++----------
 usr.sbin/makefs/msdos/msdosfs_vnops.c |  1 -
 3 files changed, 15 insertions(+), 12 deletions(-)

diff --git a/usr.sbin/makefs/ffs.c b/usr.sbin/makefs/ffs.c
index 448aa08b9d21..1eafa2bd00ef 100644
--- a/usr.sbin/makefs/ffs.c
+++ b/usr.sbin/makefs/ffs.c
@@ -1002,7 +1002,6 @@ ffs_write_file(union dinode *din, uint32_t ino, void *buf, fsinfo_t *fsopts)
 		errno = bwrite(bp);
 		if (errno != 0)
 			goto bad_ffs_write_file;
-		brelse(bp);
 		if (!isfile)
 			p += chunk;
 	}
diff --git a/usr.sbin/makefs/ffs/buf.c b/usr.sbin/makefs/ffs/buf.c
index 11b7c4b0f1f8..6caa3fd4fbc6 100644
--- a/usr.sbin/makefs/ffs/buf.c
+++ b/usr.sbin/makefs/ffs/buf.c
@@ -127,26 +127,31 @@ bwrite(struct buf *bp)
 {
 	off_t	offset;
 	ssize_t	rv;
+	size_t	bytes;
+	int	e;
 	fsinfo_t *fs = bp->b_fs;
 
 	assert (bp != NULL);
 	offset = (off_t)bp->b_blkno * fs->sectorsize + fs->offset;
+	bytes = (size_t)bp->b_bcount;
 	if (debug & DEBUG_BUF_BWRITE)
-		printf("%s: blkno %lld offset %lld bcount %ld\n", __func__,
-		    (long long)bp->b_blkno, (long long) offset,
-		    bp->b_bcount);
-	if (lseek(bp->b_fs->fd, offset, SEEK_SET) == -1)
+		printf("%s: blkno %lld offset %lld bcount %zu\n", __func__,
+		    (long long)bp->b_blkno, (long long) offset, bytes);
+	if (lseek(bp->b_fs->fd, offset, SEEK_SET) == -1) {
+		brelse(bp);
 		return (errno);
-	rv = write(bp->b_fs->fd, bp->b_data, bp->b_bcount);
+	}
+	rv = write(bp->b_fs->fd, bp->b_data, bytes);
+	e = errno;
 	if (debug & DEBUG_BUF_BWRITE)
 		printf("%s: write %ld (offset %lld) returned %lld\n", __func__,
 		    bp->b_bcount, (long long)offset, (long long)rv);
-	if (rv == bp->b_bcount)
+	brelse(bp);
+	if (rv == (ssize_t)bytes)
 		return (0);
-	else if (rv == -1)		/* write error */
-		return (errno);
-	else				/* short write ? */
-		return (EAGAIN);
+	if (rv == -1)		/* write error */
+		return (e);
+	return (EAGAIN);
 }
 
 void
diff --git a/usr.sbin/makefs/msdos/msdosfs_vnops.c b/usr.sbin/makefs/msdos/msdosfs_vnops.c
index 198efae31a0e..ff470576ee79 100644
--- a/usr.sbin/makefs/msdos/msdosfs_vnops.c
+++ b/usr.sbin/makefs/msdos/msdosfs_vnops.c
@@ -501,7 +501,6 @@ msdosfs_wfile(const char *path, struct denode *dep, fsnode *node)
 		cpsize = MIN((nsize - offs), blsize - on);
 		memcpy(bp->b_data + on, dat + offs, cpsize);
 		bwrite(bp);
-		brelse(bp);
 		offs += cpsize;
 	}