From: Torstein Eide <torsteine@gmail.com>
To: linux-btrfs@vger.kernel.org
Cc: Torstein Eide <torsteine@gmail.com>
Subject: [PATCH 2/5] btrfs: add in-memory scrub lifetime and session fields
Date: Sun, 19 Apr 2026 16:26:14 +0200 [thread overview]
Message-ID: <20260419142618.3147763-3-torsteine+linux@gmail.com> (raw)
In-Reply-To: <20260419142618.3147763-1-torsteine+linux@gmail.com>
From: Torstein Eide <torsteine@gmail.com>
Extend struct btrfs_device with two counter arrays and session metadata:
scrub_stat_values[] - lifetime totals, persisted across mounts via
the new BTRFS_SCRUB_STATS_OBJECTID tree item.
Protected by an atomic dirty counter
(scrub_stats_ccnt) and flushed at transaction
commit by btrfs_run_scrub_stats().
scrub_session_values[] - per-run counters reset when a new scrub
starts; never written to disk.
scrub_session_{t_start,t_end,last_physical,status} - timing and
progress metadata for the current or most
recent scrub session on this device.
Add matching kobject pointers (scrub_kobj, scrub_lifetime_kobj,
scrub_session_kobj) to both struct btrfs_device and struct btrfs_fs_info
for the sysfs hierarchy added in a later patch.
Add BTRFS_SCRUB_STATUS_* constants and the inline read/write helpers
btrfs_scrub_stat_{read,add,set}() and btrfs_scrub_session_read().
The add/set helpers include an smp_mb__before_atomic() to order stat
updates before the dirty-counter increment, pairing with the smp_rmb()
in btrfs_run_scrub_stats().
Signed-off-by: Torstein Eide <torsteine@gmail.com>
Assisted-by: Claude:claude-sonnet-4-6
---
fs/btrfs/fs.h | 5 +++
fs/btrfs/volumes.h | 79 ++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 84 insertions(+)
diff --git a/fs/btrfs/fs.h b/fs/btrfs/fs.h
index a4758d94b32e9..c333e30c93e40 100644
--- a/fs/btrfs/fs.h
+++ b/fs/btrfs/fs.h
@@ -714,6 +714,11 @@ struct btrfs_fs_info {
struct kobject *qgroups_kobj;
struct kobject *discard_kobj;
+ /* For /sys/fs/btrfs/<UUID>/scrub/{lifetime,session}/ */
+ struct kobject *scrub_kobj;
+ struct kobject *scrub_lifetime_kobj;
+ struct kobject *scrub_session_kobj;
+
/* Track the number of blocks (sectors) read by the filesystem. */
struct percpu_counter stats_read_blocks;
diff --git a/fs/btrfs/volumes.h b/fs/btrfs/volumes.h
index 0082c166af91f..e77f726928abd 100644
--- a/fs/btrfs/volumes.h
+++ b/fs/btrfs/volumes.h
@@ -200,6 +200,29 @@ struct btrfs_device {
atomic_t dev_stats_ccnt;
atomic_t dev_stat_values[BTRFS_DEV_STAT_VALUES_MAX];
+ /*
+ * Scrub lifetime counters. Persisted via BTRFS_SCRUB_STATS_OBJECTID
+ * tree items in the device tree; loaded at mount by
+ * btrfs_init_scrub_stats(), flushed at commit by btrfs_run_scrub_stats().
+ * Index values defined by BTRFS_SCRUB_STAT_* in btrfs_tree.h.
+ */
+ int scrub_stats_valid;
+ atomic_t scrub_stats_ccnt;
+ atomic64_t scrub_stat_values[BTRFS_SCRUB_STAT_VALUES_MAX];
+
+ /*
+ * Per-session scrub counters. Reset to zero when a new scrub starts on
+ * this device. Never persisted to disk.
+ */
+ atomic64_t scrub_session_values[BTRFS_SCRUB_STAT_VALUES_MAX];
+ /* Resume offset at end of session */
+ u64 scrub_session_last_physical;
+ /* Unix time (seconds) when the session started / ended (0 if idle) */
+ u64 scrub_session_t_start;
+ u64 scrub_session_t_end;
+ /* BTRFS_SCRUB_STATUS_* */
+ atomic_t scrub_session_status;
+
/*
* Device's major-minor number. Must be set even if the device is not
* opened (bdev == NULL), unless the device is missing.
@@ -211,6 +234,10 @@ struct btrfs_device {
struct completion kobj_unregister;
/* For sysfs/FSID/devinfo/devid/ */
struct kobject devid_kobj;
+ /* For sysfs/FSID/devinfo/devid/scrub/ hierarchy */
+ struct kobject *scrub_kobj;
+ struct kobject *scrub_lifetime_kobj;
+ struct kobject *scrub_session_kobj;
/* Bandwidth limit for scrub, in bytes */
u64 scrub_speed_max;
@@ -864,6 +891,58 @@ static inline void btrfs_dev_stat_set(struct btrfs_device *dev,
atomic_inc(&dev->dev_stats_ccnt);
}
+/*
+ * Scrub session status values stored in device->scrub_session_status.
+ */
+#define BTRFS_SCRUB_STATUS_IDLE 0
+#define BTRFS_SCRUB_STATUS_RUNNING 1
+#define BTRFS_SCRUB_STATUS_FINISHED 2
+#define BTRFS_SCRUB_STATUS_CANCELED 3
+
+static inline u64 btrfs_scrub_stat_read(const struct btrfs_device *dev,
+ int index)
+{
+ return (u64)atomic64_read(&dev->scrub_stat_values[index]);
+}
+
+static inline void btrfs_scrub_stat_add(struct btrfs_device *dev,
+ int index, u64 val)
+{
+ atomic64_add((long long)val, &dev->scrub_stat_values[index]);
+ /*
+ * Order the stat update before the dirty-counter increment so that
+ * btrfs_run_scrub_stats() observes a consistent snapshot. Pairs with
+ * smp_rmb() in btrfs_run_scrub_stats().
+ */
+ smp_mb__before_atomic();
+ atomic_inc(&dev->scrub_stats_ccnt);
+}
+
+static inline void btrfs_scrub_stat_set(struct btrfs_device *dev,
+ int index, u64 val)
+{
+ atomic64_set(&dev->scrub_stat_values[index], (long long)val);
+ /*
+ * Order the stat update before the dirty-counter increment so that
+ * btrfs_run_scrub_stats() observes a consistent snapshot. Pairs with
+ * smp_rmb() in btrfs_run_scrub_stats().
+ */
+ smp_mb__before_atomic();
+ atomic_inc(&dev->scrub_stats_ccnt);
+}
+
+static inline u64 btrfs_scrub_session_read(const struct btrfs_device *dev,
+ int index)
+{
+ return (u64)atomic64_read(&dev->scrub_session_values[index]);
+}
+
+int btrfs_init_scrub_stats(struct btrfs_fs_info *fs_info);
+int btrfs_run_scrub_stats(struct btrfs_trans_handle *trans);
+void btrfs_update_scrub_stats(struct btrfs_device *dev,
+ const struct btrfs_scrub_progress *progress,
+ int scrub_ret);
+
static inline const char *btrfs_dev_name(const struct btrfs_device *device)
{
if (!device || test_bit(BTRFS_DEV_STATE_MISSING, &device->dev_state))
--
2.48.1
next prev parent reply other threads:[~2026-04-19 14:26 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-19 14:26 [PATCH 0/5] btrfs: add persistent scrub lifetime and session counters Torstein Eide
2026-04-19 14:26 ` [PATCH 1/5] btrfs: uapi: introduce on-disk scrub stats item Torstein Eide
2026-04-19 14:26 ` Torstein Eide [this message]
2026-04-19 14:26 ` [PATCH 3/5] btrfs: persist scrub lifetime stats to the device tree Torstein Eide
2026-04-19 14:26 ` [PATCH 4/5] btrfs: hook scrub session tracking into btrfs_scrub_dev() Torstein Eide
2026-04-19 14:26 ` [PATCH 5/5] btrfs: expose scrub lifetime and session counters via sysfs Torstein Eide
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260419142618.3147763-3-torsteine+linux@gmail.com \
--to=torsteine@gmail.com \
--cc=linux-btrfs@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox