]> git.hungrycats.org Git - linux/commitdiff
btrfs: raid56: say whether a metadata read-modify-write is a write hole
authorZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Sat, 19 Sep 2026 17:17:49 +0000 (13:17 -0400)
committerZygo Blaxell <ce3g8jdj@umail.furryterror.org>
Sun, 8 Nov 2026 23:38:15 +0000 (18:38 -0500)
meta_rmw counts sub-stripe metadata writes, which is a proxy for exposure
rather than a measurement of it.  A stripe modified in place is only a write
hole if it holds data some completed transaction is relying on; a stripe
that two writes of the *same* transaction happen to split costs an extra
read but risks nothing, because a tear loses that whole transaction anyway.
The counter cannot tell those apart, so it cannot answer the only question
that matters.

The rbio can.  A read-modify-write has already read every column the write
does not cover, so at the point of the report it is holding the stripe's
on-disk contents.  Walk the uncovered tree block positions and read their
headers: a block whose bytenr and fsid match belongs there, and its
generation says which transaction put it there.  Compare that against the
generation of the blocks this write is carrying, taken from the same rbio,
so a transaction committing concurrently cannot skew the verdict:

  meta_rmw_cur   the same transaction's own blocks -- a cost
  meta_rmw_old   an earlier transaction's -- a write hole
  meta_rmw_free  no tree block there at all

System block groups are classified too, not just metadata ones.  A system
chunk carries no METADATA bit, so a flags test excludes it, but it holds
the chunk tree: the same tree block header, the same generation field, and
a worse consequence if a degraded crash tears it, since without the chunk
tree no logical address can be mapped at all.  meta_rmw already counted
system chunks; only the classification skipped them, so their read-modify-
writes were visible as a rate and never as a verdict.  Whether a raid56
system chunk ever rewrites parity over a committed tree block is therefore
an open question that this answers by measurement rather than by reasoning
about how the chunk tree is laid out.

This trusts nothing the allocator says about itself.  The stripe runs, the
liveness map and the drain accounting are all bookkeeping that could be
wrong in the same way twice; the header in the sector is what a degraded
read would actually have to reconstruct.

On a 3-device raid5 filesystem with raid5 metadata, 6000 small files with
periodic syncs and then a third rewritten, plain stripe_alloc reports 249
sub-stripe metadata writes of which 174 rewrite parity over committed tree
blocks.  Adding stripe_meta leaves 37 sub-stripe writes and none of them.

An older header under the write is not yet a write hole: most belong to
blocks that died with their stripe, which a torn parity write cannot hurt.
So the committed extent tree, the witness a degraded read after a crash
would consult, is asked whether the block is still referenced, with an
extent item of the header's generation (a sector re-allocated in the
current transaction carries a new item while the old header is still on
disk).  A live one is reported as before, with its bytenr and reference
count; a dead one is counted as meta_rmw_ghost without a warning.

The lookup runs in rmw_rbio() under the full-stripe lock, where reading a
tree block of the same stripe would queue a rebuild behind that lock, so
it does not block: commit_root_sem is taken with a trylock and the search
uses path->nowait.  A block not in memory is counted as meta_rmw_unknown.

[zygo: 7.3 port: 7.3 has no per-chunk stripe length (map->stripe_len,
bioc->stripe_len, the two-argument btrfs_stripe_nr_to_offset()):
BTRFS_STRIPE_LEN is used instead; the change itself is unchanged]

Assisted-by: Claude:claude-fable-5
Assisted-by: Claude:claude-opus-5-5
fs/btrfs/fs.h
fs/btrfs/raid56.c
fs/btrfs/sysfs.c

index d2b64fb6b6e255696103b260487e1df38dfaa7f1..c9d0d64229164b54d971a69b0c09b8cc3fd45019 100644 (file)
@@ -915,6 +915,20 @@ struct btrfs_fs_info {
                atomic64_t data_rmw_norun_ro;
                atomic64_t data_rmw_reloc_writer;
                atomic64_t meta_rmw;
+               /*
+                * Breakdown of what a metadata read-modify-write found in the
+                * columns it did not cover: a tree block of the generation
+                * being written (a cost, unless an fsynced log block), an
+                * older one (a write hole), or no tree block at all.  See
+                * audit_uncovered_rmw().
+                */
+               atomic64_t meta_rmw_cur;
+               atomic64_t meta_rmw_old;
+               atomic64_t meta_rmw_ghost;      /* older header, block dead in the
+                                                  committed extent tree */
+               atomic64_t meta_rmw_unknown;    /* older header, extent tree
+                                                  not in memory to ask */
+               atomic64_t meta_rmw_free;
                atomic64_t congestion_short;
                /* Stripes pinned in the rbio cache (rbio_needs_pin()). */
                atomic64_t rbio_pinned;
index 5db459538b73f2991bb6c7a042bc2ee9b3738718..7e573b61f5f6b04c04d67d1483a52064f8e0c01b 100644 (file)
@@ -3634,6 +3634,219 @@ static bool need_read_stripe_sectors(struct btrfs_raid_bio *rbio)
        return false;
 }
 
+/*
+ * Read the header of the tree block at data-stripe sector @i, if there is
+ * one, from whichever copy of the sector this rbio is holding.
+ *
+ * @from_bio picks the source: the caller's pages (what this write is putting
+ * there) or the stripe pages (what an RMW read found on the disk).  Returns
+ * false when the sector holds no tree block that belongs at this address --
+ * free space, or the remains of one from a previous life of the stripe.
+ */
+static bool rmw_peek_eb_generation(struct btrfs_raid_bio *rbio, u32 i,
+                                  bool from_bio, u64 *generation)
+{
+       struct btrfs_fs_info *fs_info = rbio->bioc->fs_info;
+       const int stripe_nr = i / rbio->stripe_nsectors;
+       const int sectornr = i % rbio->stripe_nsectors;
+       const u64 logical = rbio->bioc->full_stripe_logical +
+                           (u64)stripe_nr * BTRFS_STRIPE_LEN +
+                           (u64)sectornr * fs_info->sectorsize;
+       const phys_addr_t *paddrs = from_bio ? rbio->bio_paddrs
+                                            : rbio->stripe_paddrs;
+       struct btrfs_header *hdr;
+       phys_addr_t paddr;
+       u64 found_bytenr;
+       bool bad_fsid;
+       void *kaddr;
+
+       paddr = paddrs[i * rbio->sector_nsteps];
+       if (paddr == INVALID_PADDR)
+               return false;
+       if (!from_bio && !test_bit(i, rbio->stripe_uptodate_bitmap))
+               return false;
+
+       kaddr = kmap_local_paddr(paddr);
+       hdr = kaddr;
+       found_bytenr = btrfs_stack_header_bytenr(hdr);
+       *generation = btrfs_stack_header_generation(hdr);
+       bad_fsid = memcmp(hdr->fsid, fs_info->fs_devices->metadata_uuid,
+                         BTRFS_FSID_SIZE) != 0;
+       kunmap_local(kaddr);
+
+       return found_bytenr == logical && !bad_fsid;
+}
+
+/*
+ * Is @bytenr a tree block the committed extent tree still references?  Read
+ * the commit root without locking, as the caching thread does, so this is
+ * safe from the raid56 worker with no transaction to join.
+ *
+ * Returns 1 if it is (or the lookup failed), 0 if not, and -EAGAIN if the
+ * answer needs a tree block that is not in memory.  The caller holds the
+ * full stripe lock, so it must not read from disk: a read of a block in
+ * this stripe that needs reconstruction would wait for that lock.
+ */
+static int rmw_block_referenced(struct btrfs_fs_info *fs_info, u64 bytenr,
+                               u64 header_gen, u64 *refs, u64 *extent_gen)
+{
+       struct btrfs_root *extent_root = btrfs_extent_root(fs_info, bytenr);
+       struct btrfs_path *path;
+       struct btrfs_key key;
+       int live = 1;
+       int ret;
+
+       *refs = 0;
+       *extent_gen = 0;
+       path = btrfs_alloc_path();
+       if (!path)
+               return 1;
+       path->search_commit_root = 1;
+       path->skip_locking = 1;
+       path->nowait = 1;
+       key.objectid = bytenr;
+       key.type = BTRFS_METADATA_ITEM_KEY;
+       key.offset = (u64)-1;
+       if (!down_read_trylock(&fs_info->commit_root_sem)) {
+               btrfs_free_path(path);
+               return -EAGAIN;
+       }
+       ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
+       if (ret > 0)
+               ret = btrfs_previous_extent_item(extent_root, path, bytenr);
+       if (ret == 0) {
+               btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
+               if (key.objectid == bytenr &&
+                   (key.type == BTRFS_METADATA_ITEM_KEY ||
+                    key.type == BTRFS_EXTENT_ITEM_KEY)) {
+                       struct btrfs_extent_item *ei;
+
+                       ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
+                                           struct btrfs_extent_item);
+                       *refs = btrfs_extent_refs(path->nodes[0], ei);
+                       *extent_gen = btrfs_extent_generation(path->nodes[0], ei);
+                       /*
+                        * An extent item at this bytenr from a LATER
+                        * transaction than the header on disk belongs to a
+                        * new block allocated here whose write has not landed
+                        * yet; the header is the dead previous occupant.
+                        * Only an item of the header's own generation makes
+                        * the block on disk live.
+                        */
+                       live = (*refs != 0 && *extent_gen == header_gen);
+               } else {
+                       live = 0;
+               }
+       } else if (ret > 0) {
+               live = 0;
+       } else if (ret == -EAGAIN) {
+               live = -EAGAIN;
+       }
+       up_read(&fs_info->commit_root_sem);
+       btrfs_free_path(path);
+       return live;
+}
+
+/*
+ * Decide whether an uncovered metadata read-modify-write is a write hole or
+ * only a cost, from the tree blocks it read: their parity is about to be
+ * rewritten while their own sectors stay put.
+ *
+ *  - Same generation as the blocks this write carries (meta_rmw_cur): the
+ *    block belongs to the running transaction, which a crash discards --
+ *    except a log tree block, which is durable once its fsync returns.
+ *    The audit does not tell log blocks apart, so it cannot see a write
+ *    hole over fsynced log blocks; keeping them out of shared stripes is
+ *    stripe_log_run's job.
+ *  - Older, and still referenced by the committed extent tree: a write hole
+ *    over committed metadata (meta_rmw_old).
+ *
+ * Comparing against this rbio's own blocks, not fs_info's committed
+ * generation, keeps a concurrent commit from skewing the verdict.
+ */
+static void audit_uncovered_rmw(struct btrfs_raid_bio *rbio)
+{
+       struct btrfs_fs_info *fs_info = rbio->bioc->fs_info;
+       const u32 sectors_per_tree = fs_info->nodesize >> fs_info->sectorsize_bits;
+       const u32 nsectors = rbio->nr_data * rbio->stripe_nsectors;
+       u64 write_gen = 0;
+
+       /*
+        * Tree blocks only: metadata and system groups.  A mixed group's
+        * sectors need not hold tree blocks.
+        */
+       if (rbio->bioc->map_type & BTRFS_BLOCK_GROUP_DATA)
+               return;
+       if (!(rbio->bioc->map_type & (BTRFS_BLOCK_GROUP_METADATA |
+                                     BTRFS_BLOCK_GROUP_SYSTEM)))
+               return;
+
+       /*
+        * Both loops read bio_paddrs to tell what this write covers, and the
+        * paths that reach here do not all populate it (a cached rbio that
+        * failed to pad never indexes).  Idempotent, so just do it.
+        */
+       index_rbio_pages(rbio);
+
+       /* The generation this write is putting into the stripe. */
+       for (u32 i = 0; i < nsectors; i += sectors_per_tree) {
+               u64 gen;
+
+               if (rmw_peek_eb_generation(rbio, i, true, &gen))
+                       write_gen = max(write_gen, gen);
+       }
+       if (!write_gen)
+               return;
+
+       for (u32 i = 0; i < nsectors; i += sectors_per_tree) {
+               u64 gen;
+
+               /* Covered by this write: it is what we are putting there. */
+               if (rbio->bio_paddrs[i * rbio->sector_nsteps] != INVALID_PADDR)
+                       continue;
+               if (!rmw_peek_eb_generation(rbio, i, false, &gen)) {
+                       atomic64_inc(&fs_info->stripe_park_stats.meta_rmw_free);
+                       continue;
+               }
+               if (gen >= write_gen) {
+                       atomic64_inc(&fs_info->stripe_park_stats.meta_rmw_cur);
+                       continue;
+               }
+               /*
+                * An older header is a write hole only if the committed extent
+                * tree still references the block: a reclaimed stripe keeps the
+                * headers of dead blocks, and tearing those harms nothing a
+                * reader can reach.  The commit root is what a crash would see.
+                */
+               {
+                       const int stripe_nr = i / rbio->stripe_nsectors;
+                       const int sectornr = i % rbio->stripe_nsectors;
+                       const u64 logical = rbio->bioc->full_stripe_logical +
+                               (u64)stripe_nr * BTRFS_STRIPE_LEN +
+                               (u64)sectornr * fs_info->sectorsize;
+                       u64 refs;
+                       u64 egen;
+                       int ret;
+
+                       ret = rmw_block_referenced(fs_info, logical, gen, &refs,
+                                                  &egen);
+                       if (ret == -EAGAIN) {
+                               atomic64_inc(&fs_info->stripe_park_stats.meta_rmw_unknown);
+                               continue;
+                       }
+                       if (!ret) {
+                               atomic64_inc(&fs_info->stripe_park_stats.meta_rmw_ghost);
+                               continue;
+                       }
+                       atomic64_inc(&fs_info->stripe_park_stats.meta_rmw_old);
+                       btrfs_warn_rl(fs_info,
+"read-modify-write of stripe %llu rewrites parity over committed tree block %llu at generation %llu (refs %llu, extent generation %llu) while writing generation %llu: write hole",
+                                     rbio->bioc->full_stripe_logical, logical,
+                                     gen, refs, egen, write_gen);
+               }
+       }
+}
+
 /*
  * Count and audit a metadata or system read-modify-write
  * (audit_uncovered_rmw()), and warn if it lands in a raid56 group that
@@ -3653,6 +3866,7 @@ static void report_uncovered_rmw(struct btrfs_raid_bio *rbio)
                return;
 
        atomic64_inc(&fs_info->stripe_park_stats.meta_rmw);
+       audit_uncovered_rmw(rbio);
        /*
         * Every raid56 metadata and system group is a stripe_meta group while
         * stripe_alloc is on, and stripe_alloc refuses to enable on mixed
index 9629a237ddffda7093862d16c24c68ab7520b63e..a64a99545315ef2e0b738f56d3eb7b7c3a2b5f26 100644 (file)
@@ -1325,6 +1325,11 @@ static ssize_t btrfs_stripe_park_stats_show(struct kobject *kobj,
                "padded_unwritten %lld\n"
                "rmw_reads %lld\n"
                "meta_rmw %lld\n"
+               "meta_rmw_cur %lld\n"
+               "meta_rmw_old %lld\n"
+               "meta_rmw_ghost %lld\n"
+               "meta_rmw_unknown %lld\n"
+               "meta_rmw_free %lld\n"
                "rbio_pin_refused %lld\n"
                "rbio_pinned_now %d\n"
                "rbio_pinned_live %d\n"
@@ -1345,6 +1350,11 @@ static ssize_t btrfs_stripe_park_stats_show(struct kobject *kobj,
                atomic64_read(&fs_info->stripe_park_stats.padded_unwritten),
                atomic64_read(&fs_info->stripe_park_stats.rmw_reads),
                atomic64_read(&fs_info->stripe_park_stats.meta_rmw),
+               atomic64_read(&fs_info->stripe_park_stats.meta_rmw_cur),
+               atomic64_read(&fs_info->stripe_park_stats.meta_rmw_old),
+               atomic64_read(&fs_info->stripe_park_stats.meta_rmw_ghost),
+               atomic64_read(&fs_info->stripe_park_stats.meta_rmw_unknown),
+               atomic64_read(&fs_info->stripe_park_stats.meta_rmw_free),
                atomic64_read(&fs_info->stripe_park_stats.rbio_pin_refused),
                pinned,
                live,