git: bca262f0f351 - stable/13 - Fix for tunefs(8) unable to add a UFS/FFS soft update journal.

From: Kirk McKusick <mckusick_at_FreeBSD.org>
Date: Sun, 20 Nov 2022 17:14:14 UTC
The branch stable/13 has been updated by mckusick:

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

commit bca262f0f351e3881d478b7a9764c91ccb83c260
Author:     Kirk McKusick <mckusick@FreeBSD.org>
AuthorDate: 2022-11-12 23:36:07 +0000
Commit:     Kirk McKusick <mckusick@FreeBSD.org>
CommitDate: 2022-11-20 17:13:49 +0000

    Fix for tunefs(8) unable to add a UFS/FFS soft update journal.
    
    The reported bug is UFS: bad file descriptor: soft update journaling
    can not be enabled on some FreeBSD-provided disk images – failed
    to write updated cg.
    
    The UFS library (libufs(3)) failed to reopen its disk descriptor
    when first attempting to update a cylinder group. The error only
    occurred when trying to add journaling to a filesystem whose first
    cylinder group was too full to hold the journal.
    
    PR:           259090
    MFC after:    1 week
    Sponsored by: The FreeBSD Foundation
    
    (cherry picked from commit 5c7ba6bdf83fda82cce70f384e2ec8218f13002e)
---
 lib/libufs/cgroup.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/lib/libufs/cgroup.c b/lib/libufs/cgroup.c
index 1c3e271c4295..41fe587531bf 100644
--- a/lib/libufs/cgroup.c
+++ b/lib/libufs/cgroup.c
@@ -266,6 +266,10 @@ cgwrite1(struct uufsd *disk, int cg)
 	static char errmsg[BUFSIZ];
 
 	if (cg == disk->d_cg.cg_cgx) {
+		if (ufs_disk_write(disk) == -1) {
+			ERROR(disk, "failed to open disk for writing");
+			return (-1);
+		}
 		if (cgput(disk->d_fd, &disk->d_fs, &disk->d_cg) == 0)
 			return (0);
 		ERROR(disk, NULL);