]> git.hungrycats.org Git - linux/commitdiff
dev-stats: new one-patch series (write dirty device stats when no transaction is...
authorZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Wed, 21 Oct 2026 05:00:37 +0000 (01:00 -0400)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Wed, 21 Oct 2026 05:00:37 +0000 (01:00 -0400)
Promoted on both lanes 2026-10-21 as a new topic, droppable on its own:
misc-next topic 55ade1cf195c, testing 5eed033e8449, zb64 e2c9218febf9.
Cover letter states the behaviour change and that reviewers may reject it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01B6wxsJn1aY7PwnhgXdz6Ec

README.md
fixups/post-merge.patch
topics/dev-stats/0000-cover-letter.patch [new file with mode: 0644]
topics/dev-stats/0001-btrfs-write-dirty-device-stats-when-no-transaction-i.patch [new file with mode: 0644]

index 34eab74ba4786538dba7c2efe3423dcb0a1ec976..17ba5cb871f01409fc320f5af7091d77e8aaba62 100644 (file)
--- a/README.md
+++ b/README.md
@@ -8,8 +8,8 @@ upstream at all (see the table).
 | | |
 |---|---|
 | base of every series | `0c48d26d46bd` ("btrfs: stop reading free space inodes from the commit root"), the misc-next lane base (`misc-next/testing^1`) |
-| integration branch | `misc-next/testing` `bbc86ab46e84` (octopus of all 17 topics) |
-| lane head | `zygo-misc-next-zb64` `3e1390a9de03`; its own commits on top of `testing` (kernel configs, `covers/`) are not exported |
+| integration branch | `misc-next/testing` `5eed033e8449` (octopus of all 18 topics) |
+| lane head | `zygo-misc-next-zb64` `e2c9218febf9`; its own commits on top of `testing` (kernel configs, `covers/`) are not exported |
 
 The base is not a published commit: the lane rebuilds it from
 btrfs/for-next plus David Sterba's queue.  It lives in the
@@ -76,6 +76,7 @@ the integration recipe below uses.
 | `allocator-hints` | 12 | `43c53b3f02db` | Kai Krakow, KeinNiemand, Zygo | new | Kai's series plus follow-ups; the authors' postings are authoritative |
 | `read-policies` | 9 | `18680c6045be` | Kai Krakow | new | Kai's series; his postings are authoritative |
 | `replace-fixes` | 7 | `43c8a1e60a5a` | Zygo | new | patch 7 has a btrfs-progs counterpart (`topics/replace-abort-uncorrectable`) |
+| `dev-stats` | 1 | `55ade1cf195c` | Zygo | new, 0/1 (2026-10-21) | behaviour change, offered for rejection; independent of every other series |
 
 ## Applying one series
 
@@ -110,7 +111,7 @@ B=0c48d26d46bd
 ORDER="logical-ino-costs balance-resume compress-fixes deadlock-fixes dedupe-fixes
        delalloc-fixes dev-extent-defrag prealloc-size raid56-fixes raid56-scrub-replace
        stripe-alloc low-space dedupe-mtime dm-stripfault allocator-hints read-policies
-       replace-fixes"
+       replace-fixes dev-stats"
 
 for t in $ORDER; do
        git checkout -q -b up/$t $B
@@ -132,7 +133,7 @@ plain `git am`, because the later series conflict.  Merging separate
 branches is the supported way.
 
 Verified on the export: the patches of every directory, applied in order
-to `0c48d26d46bd`, give that branch's tree exactly.  Merging all 17 in
+to `0c48d26d46bd`, give that branch's tree exactly.  Merging all 18 in
 the order above with `-X ours` and applying `fixups/post-merge.patch`
 gives the tree of `misc-next/testing` exactly.  The verification used
 git 2.47.
@@ -202,5 +203,5 @@ The fixup adds 640 lines and removes 16, in 10 files.
 - `balance-resume` patch 3 is offered for rejection, as its cover letter
   says.
 - Re-export with `scripts/export.sh` after any lane change.  The
-  directories reflect the lane at `misc-next/testing` `bbc86ab46e84`.
+  directories reflect the lane at `misc-next/testing` `5eed033e8449`.
   Commit the result with the misc-next SHAs that the script prints.
index 21ad63e47540d7dad38a23e67c8d6a8230568ebe..e3c0748ecbae5c7c147ff1449f62aa414ab4769e 100644 (file)
@@ -630,7 +630,7 @@ index b715823b629ceac3b0702214adbc33066e6e0726..3651d1cdad99d241e2e18ec79d7f5161
   * Information about one device.
   *
 diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
-index 08f61b9c9a14ffe164a87218df53b9b44a671427..fc844b6b7f60810f781f72210d92d72ef04760a6 100644
+index b538d4790ef64047e214132263cd4d37eb910a85..816f5522baf27fa701127da27b5e7a5b53c5119e 100644
 --- a/fs/btrfs/volumes.c
 +++ b/fs/btrfs/volumes.c
 @@ -6924,7 +6924,293 @@ bool btrfs_chunk_raid56_mirrored(struct btrfs_fs_info *fs_info, u64 logical)
diff --git a/topics/dev-stats/0000-cover-letter.patch b/topics/dev-stats/0000-cover-letter.patch
new file mode 100644 (file)
index 0000000..39fc9b0
--- /dev/null
@@ -0,0 +1,46 @@
+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
+
diff --git a/topics/dev-stats/0001-btrfs-write-dirty-device-stats-when-no-transaction-i.patch b/topics/dev-stats/0001-btrfs-write-dirty-device-stats-when-no-transaction-i.patch
new file mode 100644 (file)
index 0000000..6fecbc2
--- /dev/null
@@ -0,0 +1,260 @@
+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
+