* [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