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