git: a0c3799828e5 - stable/13 - fsck_ffs: don't try to write in read-only mode

From: Kirk McKusick <mckusick_at_FreeBSD.org>
Date: Mon, 28 Feb 2022 05:38:43 UTC
The branch stable/13 has been updated by mckusick:

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

commit a0c3799828e52151560e37855f14ee39a3e8aca0
Author:     Chuck Silvers <chs@FreeBSD.org>
AuthorDate: 2021-06-29 21:29:15 +0000
Commit:     Kirk McKusick <mckusick@FreeBSD.org>
CommitDate: 2022-02-28 05:36:06 +0000

    fsck_ffs: don't try to write in read-only mode
    
    (cherry picked from commit ed1a156b038dee0e7a02c2935186ad5d8f4c36c1)
    
    Sponsored by:   Netflix
---
 sbin/fsck_ffs/main.c | 24 +++++++++++++-----------
 1 file changed, 13 insertions(+), 11 deletions(-)

diff --git a/sbin/fsck_ffs/main.c b/sbin/fsck_ffs/main.c
index 45bb279ce374..2724893a5b9a 100644
--- a/sbin/fsck_ffs/main.c
+++ b/sbin/fsck_ffs/main.c
@@ -364,17 +364,19 @@ checkfilesys(char *filesys)
 			sujrecovery = 0;
 			printf("** Skipping journal, falling through to full fsck\n\n");
 		}
-		/*
-		 * Write the superblock so we don't try to recover the
-		 * journal on another pass. If this is the only change
-		 * to the filesystem, we do not want it to be called
-		 * out as modified.
-		 */
-		sblock.fs_mtime = time(NULL);
-		sbdirty();
-		ofsmodified = fsmodified;
-		flush(fswritefd, &sblk);
-		fsmodified = ofsmodified;
+		if (fswritefd != -1) {
+			/*
+			 * Write the superblock so we don't try to recover the
+			 * journal on another pass. If this is the only change
+			 * to the filesystem, we do not want it to be called
+			 * out as modified.
+			 */
+			sblock.fs_mtime = time(NULL);
+			sbdirty();
+			ofsmodified = fsmodified;
+			flush(fswritefd, &sblk);
+			fsmodified = ofsmodified;
+		}
 	}
 	/*
 	 * If the filesystem was run on an old kernel that did not