mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/4] ufs: fs_clean and fs_mtime of a 44bsd filesystem that is read-write
@ 2026-11-27  5:22 Showta Ishizaki
  2026-11-27  5:22 ` [PATCH 1/4] ufs: clear fs_clean of a 44bsd filesystem while it " Showta Ishizaki
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Showta Ishizaki @ 2026-11-27  5:22 UTC (permalink / raw)
  To: Alexander Viro, Christian Brauner
  Cc: Jan Kara, Ali Ahmet Memis, linux-fsdevel, linux-kernel, Showta Ishizaki

After Linux crashes with a FreeBSD UFS mounted read-write, fsck_ffs -p
of FreeBSD 15.0 says

  /dev/md0: FILE SYSTEM CLEAN; SKIPPING CHECKS

and fsck_ffs -fn then finds 161 to 224 errors in it.  ufs leaves
fs_clean at 1 while the filesystem is read-write.

Patch 1 keeps fs_clean at 0 while a 44bsd or ufs2 filesystem is
read-write.  Patch 2 reads 2, which NetBSD leaves after a crash, as not
clean.  Patch 3 sets fs_mtime, without which FreeBSD's fsck recovers
from its soft updates journal and marks the filesystem clean.  Patch 4
keeps ufs_error() from storing 0xff, which the BSDs read as clean.

What changes for a user of Linux alone: after a crash the filesystem is
mounted read-only the next time, as one that FreeBSD left unclean is
today.  It can still be made read-write with remount, and then stays not
clean until a fsck on a BSD.

The filesystems were UFS1 and UFS2 with journaled soft updates.  Six
processes created, renamed and removed files, the device was suspended
with dmsetup, and its backing file was copied; eight such copies for
each kernel.  The UFS2 had its check hashes turned off in the image, as
Linux does not keep them; with them on fsck_ffs -p stops at the cylinder
group hashes with and without the patches.

Without the patches fsck_ffs -p skipped all eight as clean, and
fsck_ffs -fn found 161 to 224 errors in each.  With patch 1 alone it
recovered all eight from the journal and marked them clean, and -fn
found 154 to 225 errors.  With all four it says "Journal timestamp does
not match fs mount time" for all eight, goes on to the full check and
exits with 8, asking for a manual fsck; -fn reports 139 to 240 errors.

After an unmount fsck_ffs -fn finds nothing with any of the three.
After a remount to read-only it finds one without the patches, FREE BLK
COUNT(S) WRONG IN SUPERBLK, and none with them.  fs_clean goes
1, 0, 1, 0, 1 over mount read-only, remount read-write, read-only,
read-write, unmount.  A filesystem with 0 that is remounted read-write
has 0 after the unmount, and FreeBSD checks it.  One with 2 is mounted
read-only and keeps 2.  After ufs_error() fs_clean is 0, and 0 after
the unmount; it was 255.

Not tried: the background check that fsck -F leaves a filesystem with
soft updates only to, gjournal, write errors, and a filesystem made by
NetBSD.  That the BSD kernels read 0xff as clean is from their sources.

Patches 1, 7 and 8 of "ufs: correctness fixes for valid on-disk
filesystems" by Ali Ahmet Memis (2026-08-01) touch the same places.
Patch 1 there names the fields in fs_sparecon, fs_mtime among them, as
patch 3 here does for that one.  Patch 7 there refuses to remount
read-write a filesystem that is not clean; patch 1 here lets it through
and leaves it not clean at unmount, as the BSDs do after a forced mount.
Patch 8 there refuses UFS2 with a journal read-write; patch 3 here
leaves it writable and has FreeBSD's fsck ignore the journal.

This is on the fs/ufs of next-20261125.  Patch 2 applies to Linus' tree
(4dce97c6c265) as well; the others are on top of the sb_emergency_ro()
changes in next.

Showta Ishizaki (4):
  ufs: clear fs_clean of a 44bsd filesystem while it is read-write
  ufs: do not take a filesystem NetBSD has mounted for clean
  ufs: let FreeBSD see that its soft updates journal is out of date
  ufs: leave fs_clean of a 44bsd filesystem at 0 after an error

 fs/ufs/super.c  | 102 ++++++++++++++++++++++++++++++++++++++++++++++--
 fs/ufs/ufs.h    |   1 +
 fs/ufs/ufs_fs.h |   4 +-
 3 files changed, 103 insertions(+), 4 deletions(-)

-- 
2.54.0 (Apple Git-157)


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 1/4] ufs: clear fs_clean of a 44bsd filesystem while it is read-write
  2026-11-27  5:22 [PATCH 0/4] ufs: fs_clean and fs_mtime of a 44bsd filesystem that is read-write Showta Ishizaki
@ 2026-11-27  5:22 ` Showta Ishizaki
  2026-11-27  5:22 ` [PATCH 2/4] ufs: do not take a filesystem NetBSD has mounted for clean Showta Ishizaki
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Showta Ishizaki @ 2026-11-27  5:22 UTC (permalink / raw)
  To: Alexander Viro, Christian Brauner
  Cc: Jan Kara, Ali Ahmet Memis, linux-fsdevel, linux-kernel, Showta Ishizaki

FreeBSD has fs_clean at 0 and NetBSD at 2 while a filesystem is mounted
read-write, and they set it to 1 at unmount, so that fsck checks it
after a crash.  ufs leaves it at 1, and after a crash of Linux
fsck_ffs -p on FreeBSD does not look at the filesystem:

  /dev/md0: FILE SYSTEM CLEAN; SKIPPING CHECKS

Set it to 0 when the filesystem becomes read-write, and to 1 when it
stops being so and the rest is on the disk, if it was 1 to begin with.
One that was not clean and was made read-write with remount stays not
clean, as it does on the BSDs.

I copied the disk under a mounted filesystem while files were created,
renamed and removed on it, and ran fsck_ffs of FreeBSD 15.0 on the copy.
The filesystems were UFS1 and UFS2 with journaled soft updates, the UFS2
with its check hashes turned off, as Linux does not keep them and fsck
stops there first.  Without the patch fsck_ffs -p skipped all eight
copies, and fsck_ffs -fn found 161 to 224 errors in each.  With it none
was skipped.  After an unmount or a remount to read-only fsck_ffs -fn
finds nothing.

Signed-off-by: Showta Ishizaki <zakinko@snowrabbit.org>
---
 fs/ufs/super.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++-
 fs/ufs/ufs.h   |  1 +
 2 files changed, 53 insertions(+), 1 deletion(-)

diff --git a/fs/ufs/super.c b/fs/ufs/super.c
index ecf422e41..cbb034a87 100644
--- a/fs/ufs/super.c
+++ b/fs/ufs/super.c
@@ -599,6 +599,51 @@ static void ufs_put_cstotal(struct super_block *sb)
 	UFSD("EXIT\n");
 }
 
+/*
+ * FreeBSD keeps fs_clean at 0 and NetBSD at 2 or 0 while a filesystem is
+ * mounted read-write, and at 1 otherwise, so that their fsck checks it after
+ * a crash.  Use 0 here.
+ */
+static void ufs_44bsd_set_active(struct super_block *sb)
+{
+	struct ufs_sb_info *sbi = UFS_SB(sb);
+	struct ufs_super_block_first *usb1 = ubh_get_usb_first(sbi->s_uspi);
+
+	if ((sbi->s_flags & UFS_ST_MASK) != UFS_ST_44BSD)
+		return;
+	sbi->s_was_clean = usb1->fs_clean == UFS_FSCLEAN;
+	usb1->fs_clean = UFS_FSACTIVE;
+	ubh_mark_buffer_dirty(USPI_UBH(sbi->s_uspi));
+	ubh_sync_block(USPI_UBH(sbi->s_uspi));
+}
+
+/*
+ * Nothing here has repaired a filesystem that was not clean to begin with,
+ * so leave it to fsck.
+ */
+static void ufs_44bsd_set_clean(struct super_block *sb)
+{
+	struct ufs_sb_info *sbi = UFS_SB(sb);
+	struct ufs_super_block_first *usb1 = ubh_get_usb_first(sbi->s_uspi);
+	int err;
+
+	if ((sbi->s_flags & UFS_ST_MASK) != UFS_ST_44BSD || !sbi->s_was_clean)
+		return;
+	/* Everything else has to be on the disk before it is called clean. */
+	err = sync_blockdev(sb->s_bdev);
+	/* The error has been reported; fs_clean stays as that left it. */
+	if (sb_emergency_ro(sb))
+		return;
+	if (err) {
+		pr_err("(device %s): not marking the filesystem clean, error %d\n",
+		       sb->s_id, err);
+		return;
+	}
+	usb1->fs_clean = UFS_FSCLEAN;
+	ubh_mark_buffer_dirty(USPI_UBH(sbi->s_uspi));
+	ubh_sync_block(USPI_UBH(sbi->s_uspi));
+}
+
 /**
  * ufs_put_super_internal() - put on-disk intrenal structures
  * @sb: pointer to super_block structure
@@ -712,8 +757,10 @@ static void ufs_put_super(struct super_block *sb)
 	UFSD("ENTER\n");
 
 	/* NULL if a remount to read-only freed the cylinder groups */
-	if (sbi->s_ucg)
+	if (sbi->s_ucg) {
 		ufs_put_super_internal(sb);
+		ufs_44bsd_set_clean(sb);
+	}
 	cancel_delayed_work_sync(&sbi->sync_work);
 
 	ubh_brelse_uspi (sbi->s_uspi);
@@ -1246,6 +1293,8 @@ static int ufs_fill_super(struct super_block *sb, struct fs_context *fc)
 		ret = -ENOMEM;
 		goto failed_cg;
 	}
+	if (!sb_rdonly(sb))
+		ufs_44bsd_set_active(sb);
 
 	UFSD("EXIT\n");
 	return 0;
@@ -1308,6 +1357,7 @@ static int ufs_reconfigure(struct fs_context *fc)
 				ufs_set_fs_state(sb, usb1, usb3,
 					UFS_FSOK - fs32_to_cpu(sb, usb1->fs_time));
 			ubh_mark_buffer_dirty (USPI_UBH(uspi));
+			ufs_44bsd_set_clean(sb);
 		}
 		sb->s_flags |= SB_RDONLY;
 	} else {
@@ -1333,6 +1383,7 @@ static int ufs_reconfigure(struct fs_context *fc)
 			mutex_unlock(&UFS_SB(sb)->s_lock);
 			return -EPERM;
 		}
+		ufs_44bsd_set_active(sb);
 		sb->s_flags &= ~SB_RDONLY;
 #endif
 	}
diff --git a/fs/ufs/ufs.h b/fs/ufs/ufs.h
index 541566f5b..86e045f20 100644
--- a/fs/ufs/ufs.h
+++ b/fs/ufs/ufs.h
@@ -26,6 +26,7 @@ struct ufs_sb_info {
 	unsigned short s_cg_loaded;
 	unsigned s_flavour;
 	unsigned s_on_err;
+	bool s_was_clean;	/* 44bsd: clean when it became read-write */
 	struct super_block *sb;
 	int work_queued; /* non-zero if the delayed work is queued */
 	struct delayed_work sync_work; /* FS sync delayed work */
-- 
2.54.0 (Apple Git-157)


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 2/4] ufs: do not take a filesystem NetBSD has mounted for clean
  2026-11-27  5:22 [PATCH 0/4] ufs: fs_clean and fs_mtime of a 44bsd filesystem that is read-write Showta Ishizaki
  2026-11-27  5:22 ` [PATCH 1/4] ufs: clear fs_clean of a 44bsd filesystem while it " Showta Ishizaki
@ 2026-11-27  5:22 ` Showta Ishizaki
  2026-11-27  5:22 ` [PATCH 3/4] ufs: let FreeBSD see that its soft updates journal is out of date Showta Ishizaki
  2026-11-27  5:22 ` [PATCH 4/4] ufs: leave fs_clean of a 44bsd filesystem at 0 after an error Showta Ishizaki
  3 siblings, 0 replies; 5+ messages in thread
From: Showta Ishizaki @ 2026-11-27  5:22 UTC (permalink / raw)
  To: Alexander Viro, Christian Brauner
  Cc: Jan Kara, Ali Ahmet Memis, linux-fsdevel, linux-kernel, Showta Ishizaki

NetBSD has fs_clean at 2, FS_WASCLEAN, while a filesystem that was clean
is mounted read-write, so it is what is left after a crash.  ufs reads
it as Solaris' "stable" and mounts the filesystem read-write.  Mount it
read-only, as one with 0.

Signed-off-by: Showta Ishizaki <zakinko@snowrabbit.org>
---
 fs/ufs/super.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/fs/ufs/super.c b/fs/ufs/super.c
index cbb034a87..c204af7ce 100644
--- a/fs/ufs/super.c
+++ b/fs/ufs/super.c
@@ -1135,6 +1135,12 @@ static int ufs_fill_super(struct super_block *sb, struct fs_context *fc)
 			UFSD("fs is clean\n");
 			break;
 		case UFS_FSSTABLE:
+			/* NetBSD's FS_WASCLEAN: mounted, or not unmounted */
+			if ((flags & UFS_ST_MASK) == UFS_ST_44BSD) {
+				pr_err("%s(): fs is active\n", __func__);
+				sb->s_flags |= SB_RDONLY;
+				break;
+			}
 			UFSD("fs is stable\n");
 			break;
 		case UFS_FSLOG:
-- 
2.54.0 (Apple Git-157)


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 3/4] ufs: let FreeBSD see that its soft updates journal is out of date
  2026-11-27  5:22 [PATCH 0/4] ufs: fs_clean and fs_mtime of a 44bsd filesystem that is read-write Showta Ishizaki
  2026-11-27  5:22 ` [PATCH 1/4] ufs: clear fs_clean of a 44bsd filesystem while it " Showta Ishizaki
  2026-11-27  5:22 ` [PATCH 2/4] ufs: do not take a filesystem NetBSD has mounted for clean Showta Ishizaki
@ 2026-11-27  5:22 ` Showta Ishizaki
  2026-11-27  5:22 ` [PATCH 4/4] ufs: leave fs_clean of a 44bsd filesystem at 0 after an error Showta Ishizaki
  3 siblings, 0 replies; 5+ messages in thread
From: Showta Ishizaki @ 2026-11-27  5:22 UTC (permalink / raw)
  To: Alexander Viro, Christian Brauner
  Cc: Jan Kara, Ali Ahmet Memis, linux-fsdevel, linux-kernel, Showta Ishizaki

fsck on FreeBSD uses the journal of a filesystem with journaled soft
updates when the time stored with the journal is fs_mtime, and then does
not do the full check.  FreeBSD sets fs_mtime at each read-write mount.
ufs does not, so after a crash of Linux fsck recovers from a journal
that has nothing of what Linux wrote, and marks the filesystem clean.

Set fs_mtime when the filesystem becomes read-write.

With only fs_clean cleared, fsck_ffs -p of FreeBSD 15.0 used the journal
and marked clean all eight of the copies from the first patch, and
fsck_ffs -fn then found 154 to 225 errors in each.  With this it says

  Journal timestamp does not match fs mount time

goes on to the full check, and asks for a manual fsck.  This is about
fsck_ffs -p; I did not look at the background fsck of a filesystem with
soft updates only, or at gjournal.

Signed-off-by: Showta Ishizaki <zakinko@snowrabbit.org>
---
 fs/ufs/super.c  | 26 ++++++++++++++++++++++++++
 fs/ufs/ufs_fs.h |  4 +++-
 2 files changed, 29 insertions(+), 1 deletion(-)

diff --git a/fs/ufs/super.c b/fs/ufs/super.c
index c204af7ce..716867fac 100644
--- a/fs/ufs/super.c
+++ b/fs/ufs/super.c
@@ -599,6 +599,31 @@ static void ufs_put_cstotal(struct super_block *sb)
 	UFSD("EXIT\n");
 }
 
+/*
+ * FreeBSD's fsck takes the soft updates journal as current when the time
+ * kept with it is fs_mtime, and then skips the full check.  FreeBSD sets
+ * fs_mtime at each read-write mount; do the same, as the journal knows
+ * nothing about what is written here.
+ */
+static void ufs_44bsd_set_mtime(struct super_block *sb)
+{
+	struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
+	struct ufs_super_block_first *usb1 = ubh_get_usb_first(uspi);
+	struct ufs_super_block_third *usb3 = ubh_get_usb_third(uspi);
+	time64_t now = ktime_get_real_seconds();
+	__fs64 mtime;
+
+	/* Without these the place belongs to fields of the old superblock. */
+	if (uspi->fs_magic != UFS2_MAGIC &&
+	    !(usb1->fs_flags & UFS_FLAGS_UPDATED))
+		return;
+	memcpy(&mtime, usb3->fs_un2.fs_44.fs_mtime, sizeof(mtime));
+	if (now == fs64_to_cpu(sb, mtime))
+		now++;
+	mtime = cpu_to_fs64(sb, now);
+	memcpy(usb3->fs_un2.fs_44.fs_mtime, &mtime, sizeof(mtime));
+}
+
 /*
  * FreeBSD keeps fs_clean at 0 and NetBSD at 2 or 0 while a filesystem is
  * mounted read-write, and at 1 otherwise, so that their fsck checks it after
@@ -613,6 +638,7 @@ static void ufs_44bsd_set_active(struct super_block *sb)
 		return;
 	sbi->s_was_clean = usb1->fs_clean == UFS_FSCLEAN;
 	usb1->fs_clean = UFS_FSACTIVE;
+	ufs_44bsd_set_mtime(sb);
 	ubh_mark_buffer_dirty(USPI_UBH(sbi->s_uspi));
 	ubh_sync_block(USPI_UBH(sbi->s_uspi));
 }
diff --git a/fs/ufs/ufs_fs.h b/fs/ufs/ufs_fs.h
index b8dc354ae..c5e9c6aab 100644
--- a/fs/ufs/ufs_fs.h
+++ b/fs/ufs/ufs_fs.h
@@ -937,7 +937,9 @@ struct ufs_super_block_third {
 			__fs32	fs_qfmask[2];	/* ~usb_fmask */
 		} fs_sunx86;
 		struct {
-			__fs32	fs_sparecon[50];/* reserved for future constants */
+			__fs32	fs_sparecon[23];/* reserved for future constants */
+			__fs32	fs_mtime[2];	/* last mount or fsck time */
+			__fs32	fs_sparecon2[25];/* reserved for future constants */
 			__fs32	fs_contigsumsize;/* size of cluster summary array */
 			__fs32	fs_maxsymlinklen;/* max length of an internal symlink */
 			__fs32	fs_inodefmt;	/* format of on-disk inodes */
-- 
2.54.0 (Apple Git-157)


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 4/4] ufs: leave fs_clean of a 44bsd filesystem at 0 after an error
  2026-11-27  5:22 [PATCH 0/4] ufs: fs_clean and fs_mtime of a 44bsd filesystem that is read-write Showta Ishizaki
                   ` (2 preceding siblings ...)
  2026-11-27  5:22 ` [PATCH 3/4] ufs: let FreeBSD see that its soft updates journal is out of date Showta Ishizaki
@ 2026-11-27  5:22 ` Showta Ishizaki
  3 siblings, 0 replies; 5+ messages in thread
From: Showta Ishizaki @ 2026-11-27  5:22 UTC (permalink / raw)
  To: Alexander Viro, Christian Brauner
  Cc: Jan Kara, Ali Ahmet Memis, linux-fsdevel, linux-kernel, Showta Ishizaki

ufs_error() and ufs_panic() set fs_clean to 0xff.  That is a Solaris
value.  The FreeBSD kernel tests fs_clean for 0, and the NetBSD kernel
and fsck test its lowest bit, so that they take it for clean.

Leave it at 0.  With an image whose fragments are marked free, removing
the files gives the error, and fs_clean is 0 then and after the unmount;
it was 255.

Signed-off-by: Showta Ishizaki <zakinko@snowrabbit.org>
---
 fs/ufs/super.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

diff --git a/fs/ufs/super.c b/fs/ufs/super.c
index 716867fac..71e0d5808 100644
--- a/fs/ufs/super.c
+++ b/fs/ufs/super.c
@@ -269,6 +269,19 @@ static void ufs_print_cylinder_stuff(struct super_block *sb,
 
 static const struct super_operations ufs_super_ops;
 
+/*
+ * FreeBSD and NetBSD have no such value.  Their kernels, and NetBSD's fsck,
+ * test a bit or compare with 0, and would take UFS_FSBAD for clean.
+ */
+static void ufs_set_bad(struct super_block *sb,
+			struct ufs_super_block_first *usb1)
+{
+	if ((UFS_SB(sb)->s_flags & UFS_ST_MASK) == UFS_ST_44BSD)
+		usb1->fs_clean = UFS_FSACTIVE;
+	else
+		usb1->fs_clean = UFS_FSBAD;
+}
+
 void ufs_error (struct super_block * sb, const char * function,
 	const char * fmt, ...)
 {
@@ -285,7 +298,7 @@ void ufs_error (struct super_block * sb, const char * function,
 	 * marked bad goes out with the block device.
 	 */
 	if (!sb_rdonly(sb) && sb_set_emergency_ro(sb)) {
-		usb1->fs_clean = UFS_FSBAD;
+		ufs_set_bad(sb, usb1);
 		ubh_mark_buffer_dirty(USPI_UBH(uspi));
 	}
 	va_start(args, fmt);
@@ -318,7 +331,7 @@ void ufs_panic (struct super_block * sb, const char * function,
 	
 	/* See ufs_error(). */
 	if (!sb_rdonly(sb) && sb_set_emergency_ro(sb)) {
-		usb1->fs_clean = UFS_FSBAD;
+		ufs_set_bad(sb, usb1);
 		ubh_mark_buffer_dirty(USPI_UBH(uspi));
 	}
 	va_start(args, fmt);
-- 
2.54.0 (Apple Git-157)


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-11-27  5:22 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-27  5:22 [PATCH 0/4] ufs: fs_clean and fs_mtime of a 44bsd filesystem that is read-write Showta Ishizaki
2026-11-27  5:22 ` [PATCH 1/4] ufs: clear fs_clean of a 44bsd filesystem while it " Showta Ishizaki
2026-11-27  5:22 ` [PATCH 2/4] ufs: do not take a filesystem NetBSD has mounted for clean Showta Ishizaki
2026-11-27  5:22 ` [PATCH 3/4] ufs: let FreeBSD see that its soft updates journal is out of date Showta Ishizaki
2026-11-27  5:22 ` [PATCH 4/4] ufs: leave fs_clean of a 44bsd filesystem at 0 after an error Showta Ishizaki

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®