--- /dev/null
+From 55ade1cf195c58e80606d630120f5434f66b3a36 Mon Sep 17 00:00:00 2001
+From: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
+Date: Wed, 21 Oct 2026 00:59:50 -0400
+Subject: [PATCH 0/1] btrfs: persist device stats counted without a transaction
+To: linux-btrfs@vger.kernel.org
+
+Device stats counters are written to the dev-stats item only inside a
+transaction commit. On a read-write mount with an otherwise read-only
+workload (reads, read repair, csum failures found by readers), nothing
+starts a transaction, and unmount only commits a transaction that is
+already running, so every error counted since the last commit is lost
+at unmount: a device returning bad data on a mostly-read filesystem can
+have its corruption_errs reset by the next reboot.
+
+The patch writes dirty device stats from the transaction kthread and
+at unmount / remount-ro when no transaction is running, only on a
+finished read-write mount that is not frozen, aborted or shut down. A
+clean idle filesystem still makes no commits. Read-only mounts, crashes
+and aborted filesystems keep losing the counts.
+
+This is a change in btrfs behaviour: a read-only workload on a
+read-write mount now causes a transaction commit, and metadata writes,
+whenever an error was counted. Reviewers may reasonably reject it; it
+is sent as a separate series so it can be dropped on its own.
+
+Tested with devstats-persist-test.sh on single, raid1 and raid5: without
+the patch the count is lost at unmount, after idle, at remount-ro and
+across freeze/thaw; with it every count reaches disk, a clean idle
+filesystem makes no commit, and a read-only mount still loses the count.
+
+Zygo Blaxell (1):
+ btrfs: write dirty device stats when no transaction is running
+
+ fs/btrfs/disk-io.c | 14 +++++++++++++-
+ fs/btrfs/super.c | 9 +++++++++
+ fs/btrfs/transaction.c | 35 +++++++++++++++++++++++++++++++++++
+ fs/btrfs/transaction.h | 1 +
+ fs/btrfs/volumes.c | 35 +++++++++++++++++++++++------------
+ fs/btrfs/volumes.h | 1 +
+ 6 files changed, 82 insertions(+), 13 deletions(-)
+
+
+base-commit: 0c48d26d46bdd6422bb91ba7f1c519a2993116dc
+--
+2.47.3
+
--- /dev/null
+From 55ade1cf195c58e80606d630120f5434f66b3a36 Mon Sep 17 00:00:00 2001
+From: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
+Date: Tue, 20 Oct 2026 22:15:41 -0400
+Subject: [PATCH 1/1] btrfs: write dirty device stats when no transaction is
+ running
+To: linux-btrfs@vger.kernel.org
+
+Device stats are counted in memory and written to their dev stats items
+only by btrfs_run_dev_stats(), which runs inside a transaction commit.
+Nothing starts a transaction for them: the transaction kthread commits
+only a running transaction, and at unmount close_ctree() calls
+btrfs_commit_super(), whose btrfs_commit_current_transaction() only
+attaches to a running transaction and returns 0 when there is none.
+
+Read errors, checksum failures and read repair (which rewrites the bad
+copy with bios outside any transaction) count stats without starting a
+transaction. So on a read-write mount whose workload only reads, every
+count since the last commit is lost at unmount, and the next mount
+shows the old values. For example, a cold read of a file with one
+corrupted raid1 copy counts corruption_errs and repairs the copy, and
+after unmount and mount the count is gone.
+
+Start and commit a transaction to write the stats when any device has
+dirty stats, the filesystem was mounted read-write and is not read-only,
+shut down or in an error state, and no transaction is running:
+
+- the transaction kthread does it at its commit_interval wakeup, so the
+ counts reach the disk within one commit interval;
+
+- btrfs_commit_super() does it after committing the running transaction,
+ which covers unmount and remount read-only, including counts made by
+ the scrub, replace and balance that close_ctree() and
+ btrfs_remount_ro() stop just before.
+
+btrfs_remount_ro() takes and releases transaction_kthread_mutex after it
+sets SB_RDONLY, so a kthread pass that saw the filesystem writable can
+not start a transaction after the final commit. A frozen filesystem is
+skipped (sb_start_write_trylock()) and its counts are written after
+thaw. Log replay at mount happens before BTRFS_FS_OPEN is set and does
+not start one. The check reuses the lockless RCU pass of
+btrfs_run_dev_stats(), now btrfs_dev_stats_dirty(), so a healthy idle
+filesystem pays one list walk per commit interval and gets no new
+transactions. The one exception is a filesystem whose devices have no
+dev stats item yet: mkfs does not create them, and loading the stats at
+mount marks such a device dirty, so the first read-write mount writes
+the items in one commit even when idle.
+
+This changes btrfs behaviour. An otherwise read-only workload on a
+read-write mount now causes transaction commits and metadata writes
+whenever errors were counted: up to one commit per commit interval while
+errors keep coming, and one more at unmount. Until now such a workload
+wrote no metadata at all, and someone who relies on that, or who prefers
+losing the counts to writing to a filesystem that is returning errors,
+may reasonably reject this patch.
+
+Read-only mounts are unchanged and still lose their counts at unmount,
+as do a crash and a shutdown or aborted filesystem.
+
+Assisted-by: Claude:claude-opus-5-5
+---
+ fs/btrfs/disk-io.c | 14 +++++++++++++-
+ fs/btrfs/super.c | 9 +++++++++
+ fs/btrfs/transaction.c | 35 +++++++++++++++++++++++++++++++++++
+ fs/btrfs/transaction.h | 1 +
+ fs/btrfs/volumes.c | 35 +++++++++++++++++++++++------------
+ fs/btrfs/volumes.h | 1 +
+ 6 files changed, 82 insertions(+), 13 deletions(-)
+
+diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
+index c45cda4290d5a..2bd33cbcb8b0e 100644
+--- a/fs/btrfs/disk-io.c
++++ b/fs/btrfs/disk-io.c
+@@ -1559,6 +1559,12 @@ static int transaction_kthread(void *arg)
+ cur = fs_info->running_transaction;
+ if (!cur) {
+ spin_unlock(&fs_info->trans_lock);
++ /*
++ * Write device stats counted without a transaction.
++ * At unmount, btrfs_commit_super() writes the last ones.
++ */
++ if (!sb_rdonly(fs_info->sb) && !btrfs_fs_closing(fs_info))
++ btrfs_commit_dirty_dev_stats(fs_info);
+ goto sleep;
+ }
+
+@@ -4250,6 +4256,8 @@ void btrfs_drop_and_free_fs_root(struct btrfs_fs_info *fs_info,
+
+ int btrfs_commit_super(struct btrfs_fs_info *fs_info)
+ {
++ int ret;
++
+ mutex_lock(&fs_info->cleaner_mutex);
+ btrfs_run_delayed_iputs(fs_info);
+ mutex_unlock(&fs_info->cleaner_mutex);
+@@ -4259,7 +4267,11 @@ int btrfs_commit_super(struct btrfs_fs_info *fs_info)
+ down_write(&fs_info->cleanup_work_sem);
+ up_write(&fs_info->cleanup_work_sem);
+
+- return btrfs_commit_current_transaction(fs_info->tree_root);
++ ret = btrfs_commit_current_transaction(fs_info->tree_root);
++ if (ret)
++ return ret;
++
++ return btrfs_commit_dirty_dev_stats(fs_info);
+ }
+
+ static void warn_about_uncommitted_trans(struct btrfs_fs_info *fs_info)
+diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c
+index acb704b20c2be..a21a88935cbb3 100644
+--- a/fs/btrfs/super.c
++++ b/fs/btrfs/super.c
+@@ -1364,6 +1364,15 @@ static int btrfs_remount_ro(struct btrfs_fs_info *fs_info)
+ */
+ wait_on_bit(&fs_info->flags, BTRFS_FS_CLEANER_RUNNING, TASK_UNINTERRUPTIBLE);
+
++ /*
++ * Likewise, the transaction kthread starts a transaction to write
++ * dirty device stats if it saw the filesystem writable. Wait for any
++ * such pass to finish, so it can not start one after our final
++ * commit. The next passes see SB_RDONLY and leave the stats alone.
++ */
++ mutex_lock(&fs_info->transaction_kthread_mutex);
++ mutex_unlock(&fs_info->transaction_kthread_mutex);
++
+ /*
+ * We've set the superblock to RO mode, so we might have made the
+ * cleaner task sleep without running all pending delayed iputs. Go
+diff --git a/fs/btrfs/transaction.c b/fs/btrfs/transaction.c
+index 7431a06f168da..e48d014a0ad80 100644
+--- a/fs/btrfs/transaction.c
++++ b/fs/btrfs/transaction.c
+@@ -2095,6 +2095,41 @@ int btrfs_commit_current_transaction(struct btrfs_root *root)
+ return btrfs_commit_transaction(trans);
+ }
+
++/*
++ * Device stats are written to their items only by a transaction commit, but
++ * read errors, read repair and checksum failures count them without starting
++ * a transaction. On an otherwise idle filesystem those counts would be lost
++ * at unmount, so start and commit a transaction to write them.
++ *
++ * This is only done for a filesystem mounted read-write (BTRFS_FS_OPEN, also
++ * set by a remount to read-write): a read-only mount never writes the counts.
++ * Callers check SB_RDONLY when it matters, btrfs_remount_ro() sets it before
++ * calling btrfs_commit_super(). A frozen filesystem is skipped, its counts
++ * are written after it is thawed.
++ */
++int btrfs_commit_dirty_dev_stats(struct btrfs_fs_info *fs_info)
++{
++ struct btrfs_trans_handle *trans;
++ int ret;
++
++ if (!btrfs_dev_stats_dirty(fs_info))
++ return 0;
++ if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags) ||
++ BTRFS_FS_ERROR(fs_info) || btrfs_is_shutdown(fs_info))
++ return 0;
++ if (!sb_start_write_trylock(fs_info->sb))
++ return 0;
++
++ trans = btrfs_start_transaction(fs_info->tree_root, 0);
++ if (IS_ERR(trans))
++ ret = PTR_ERR(trans);
++ else
++ ret = btrfs_commit_transaction(trans);
++ sb_end_write(fs_info->sb);
++
++ return ret;
++}
++
+ static void cleanup_transaction(struct btrfs_trans_handle *trans, int err)
+ {
+ struct btrfs_fs_info *fs_info = trans->fs_info;
+diff --git a/fs/btrfs/transaction.h b/fs/btrfs/transaction.h
+index 684b46e5f2e9a..05f2a3728065f 100644
+--- a/fs/btrfs/transaction.h
++++ b/fs/btrfs/transaction.h
+@@ -299,6 +299,7 @@ int btrfs_clean_one_deleted_snapshot(struct btrfs_fs_info *fs_info);
+ int btrfs_commit_transaction(struct btrfs_trans_handle *trans);
+ void btrfs_commit_transaction_async(struct btrfs_trans_handle *trans);
+ int btrfs_commit_current_transaction(struct btrfs_root *root);
++int btrfs_commit_dirty_dev_stats(struct btrfs_fs_info *fs_info);
+ int btrfs_end_transaction_throttle(struct btrfs_trans_handle *trans);
+ bool btrfs_should_end_transaction(struct btrfs_trans_handle *trans);
+ void btrfs_throttle(struct btrfs_fs_info *fs_info);
+diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
+index d3e6a9b429dd9..e8a511e814833 100644
+--- a/fs/btrfs/volumes.c
++++ b/fs/btrfs/volumes.c
+@@ -8474,6 +8474,28 @@ static int update_dev_stat_item(struct btrfs_trans_handle *trans,
+ return ret;
+ }
+
++/*
++ * Return true if any device has stats changes that are not written to its
++ * dev stats item yet. Lockless, see btrfs_run_dev_stats().
++ */
++bool btrfs_dev_stats_dirty(struct btrfs_fs_info *fs_info)
++{
++ struct btrfs_device *device;
++ bool dirty = false;
++
++ rcu_read_lock();
++ list_for_each_entry_rcu(device, &fs_info->fs_devices->devices, dev_list) {
++ if (device->dev_stats_valid &&
++ atomic_read(&device->dev_stats_ccnt) != 0) {
++ dirty = true;
++ break;
++ }
++ }
++ rcu_read_unlock();
++
++ return dirty;
++}
++
+ /*
+ * called from commit_transaction. Writes all changed device stats to disk.
+ */
+@@ -8484,7 +8506,6 @@ int btrfs_run_dev_stats(struct btrfs_trans_handle *trans)
+ struct btrfs_device *device;
+ int stats_cnt;
+ int ret = 0;
+- bool need_update_dev_stats = false;
+
+ /*
+ * Do an initial pass using RCU to see if we need to update any dev
+@@ -8502,17 +8523,7 @@ int btrfs_run_dev_stats(struct btrfs_trans_handle *trans)
+ * transaction commit, no one can be concurrently adding or removing a
+ * device.
+ */
+- rcu_read_lock();
+- list_for_each_entry_rcu(device, &fs_devices->devices, dev_list) {
+- if (device->dev_stats_valid &&
+- atomic_read(&device->dev_stats_ccnt) != 0) {
+- need_update_dev_stats = true;
+- break;
+- }
+- }
+- rcu_read_unlock();
+-
+- if (!need_update_dev_stats)
++ if (!btrfs_dev_stats_dirty(fs_info))
+ return 0;
+
+ mutex_lock(&fs_devices->device_list_mutex);
+diff --git a/fs/btrfs/volumes.h b/fs/btrfs/volumes.h
+index 6eddda2392215..00f5d54ae9742 100644
+--- a/fs/btrfs/volumes.h
++++ b/fs/btrfs/volumes.h
+@@ -791,6 +791,7 @@ int btrfs_get_dev_stats(struct btrfs_fs_info *fs_info,
+ int btrfs_init_devices_late(struct btrfs_fs_info *fs_info);
+ int btrfs_init_dev_stats(struct btrfs_fs_info *fs_info);
+ int btrfs_init_writeback_bio_size(struct btrfs_fs_info *fs_info);
++bool btrfs_dev_stats_dirty(struct btrfs_fs_info *fs_info);
+ int btrfs_run_dev_stats(struct btrfs_trans_handle *trans);
+ void btrfs_rm_dev_replace_remove_srcdev(struct btrfs_device *srcdev);
+ void btrfs_rm_dev_replace_free_srcdev(struct btrfs_device *srcdev);
+--
+2.47.3
+