on every mid-chunk umount, were not run; zoned replace resume, which
skips every chunk regardless, is untouched.
+## Replace with discards (2026-10-31)
+
+Test: `replace-discard-test.sh` + `replace-discard.py` (rd; TEST-CATALOG
+5t). **The bug it finds is upstream and is NOT fixed by this topic**:
+Josef Bacik, "[PATCH 5/5] btrfs: don't trim the target of a device
+replace" (linux-btrfs 2026-10-30, R-b Qu, Fixes 499f377f49f0).
+`btrfs_trim_free_extents()` discards every range of a writeable device
+that is not CHUNK_ALLOCATED in `device->alloc_state`, and a replace
+target's alloc_state stays empty until `btrfs_set_target_alloc_state()` at
+the finish, so a FITRIM while the replace runs discards everything copied
+so far (and the duplicated writes); the replace still reports "finished,
+0 write errs, 0 uncorr. read errs". Arms per lane: lane zb64 (base) and
+lane zb64 + Josef 5/5 (`refs/cand/josef-trim/{618,mn}`, candidate only,
+not promoted).
+
+The rows above all run poison prefill + mkfs -K + nodiscard + replace -K,
+and that rule still governs them (zeros from a discard would hide an
+unwritten target sector). These rows run WITH discards, so:
+
+- **Invalid in discard mode** (not used as pass criteria): the poison
+ strip checks (`wh_replace_check` moved / unwritten / MISPLACED,
+ replace-status unc-raid5 `targetcheck`), and any check that a target
+ sector "still holds poison"; a discarded sector on loop-on-ext4 reads
+ zero, which is what an unwritten sector would also look like. Images
+ are sparse; mkfs -K and replace -K are kept except in the mkfsnoK / noK
+ rows.
+- **Detectors used**: sha256 manifest (4 GiB of 64 MiB files, 20000 small
+ files, plus the files a writer acked during the replace; O_DIRECT for
+ 4K-multiple sizes) read on a fresh READ-ONLY mount, after a scrub -BdRr
+ that reports devid 1's (the target's) csum / verify / read / super
+ errors; raid1 also a degraded,ro mount and `btrfs check` with ONLY the
+ target present (every read from the target); single: the target is the
+ only device; `btrfs check --readonly`, counting only error lines new
+ relative to a check before the replace (6.18 keeps free space tree keys
+ of the system mkfs 6.14's temporary chunks, "Space key ... has no
+ corresponding block group"; misc-next does not); kernel log (csum
+ failed / bad tree block / transid / corrupt counts = loss); a final rw
+ mount. The post checks must be read-only: on the first 6.18 baseline
+ run a rw manifest read of fstrim-raid5 hit 117610 csum errors on the
+ target, raid5 read repair wrote the blocks back, and the scrub after it
+ found 0 (`forensics/replace-discard-20261031/618-base-v1-aborted`).
+- **Plumbing, else BLIND (77)**: discard_max_bytes > 0 on every loop and
+ on the dm-delay node; a blkdiscard of 32 MiB through the dm-delay node
+ (and the source node) frees the backing file's blocks and reads zero;
+ fstrim rc 0 with bytes > 0; per-device discard sectors
+ (/sys/block/*/stat field 14) in the mode's window: members during the
+ replace (fstrim / dsync / dasync), the target before the copy (noK),
+ members at mkfs (mkfsnoK); fstrim / resume: the one fstrim ran while
+ the replace was running and past the planned progress (the end of
+ devid 1's first >= 128 MiB data dev extent and first metadata dev
+ extent). One fstrim only: CHUNK_TRIMMED makes a second one a no-op on
+ the target.
+- **Outcome**: PASS / FAIL(loss:...) (acked data, or the target's copy,
+ lost; raid1/raid5 "masked" = the files read back from the other copies
+ but the target copy is gone) / FAIL(enospc:...) / BLIND.
+
+Cases (fresh fs each, devid 1 replaced by a sparse target behind dm-delay
+20 ms, 250 ms for dasync-raid1; 16 GiB devices; a writer adds / fsyncs /
+deletes 4 MiB files, 1 GiB, during the replace): fstrim (one fstrim
+mid-copy, nodiscard mount), dsync (mount -o discard=sync), dasync
+(discard=async), noK (replace without -K: progs discards the whole,
+unpoisoned target before the start), mkfsnoK (mkfs without -K), each on
+raid1 and raid5 (metadata raid1), fstrim also on single (data single,
+metadata dup: nothing masks it); resume-single: crash image (dm suspend
+--nolockfs) of a running replace, mount (the kthread resumes the replace;
+the crash cursor was 0, the item not yet committed), one fstrim during
+the resumed copy. dsync / dasync / noK / mkfsnoK are controls: an
+extent-free discard maps through `btrfs_map_discard()`, which never
+reaches the target (non-zoned) and refuses RAID5/6 chunks altogether ("we
+don't discard raid56 yet"), and progs' discards come before any copy.
+
+Kernels: 6.18 base zb64 bba843a8d056 (6.18.59+ #4, bzImage md5 150c366a),
+candidate 60b237cb9407 (#5, c8ba50e2), on ahive; misc-next base zb64
+9291348d2cbe (7.3.0-rc7+ #28, d09372a9), candidate 9a785826b28d (#29,
+fbec86e7), on ehive; all KASAN + lockdep (debug_locks 1 after every
+run). progs: `/usr/local/btrfs-progs-abortflag` (v7.1, md5 e470837e),
+mkfs: system mkfs.btrfs 6.14. Harness 5bca9c14 (base cases 1-6) /
+eaec6d55 (the rest, and every candidate case). Evidence:
+`raid56-wh-harness/forensics/replace-discard-20261031/` (README.md).
+
+| test | verifies | patch(es) | duration | last result | date | kernel | harness | evidence |
+|---|---|---|---|---|---|---|---|---|
+| `replace-discard-test.sh` fstrim-single (data single, metadata dup, 1 device) | one fstrim while the replace copies; the source is gone afterwards, nothing masks | upstream: Josef 5/5 (not this series) | ~1 min | **base FAIL (loss) both lanes**: replace "finished, 0 errs", then the fs does not mount: "bad tree block start, mirror 1 want 30523392 have 0" (dup copy 2 the same), btrfs check 6 new errors; fstrim discarded the whole target (+33552384 sectors). **cand PASS both lanes**: 20237/20237 files, scrub -r 0 errors on devid 1, check clean, fstrim discarded 0 sectors on the target | 2026-10-31 19:23-20:47 (base), 20:56-21:43 (cand) | base: 6.18 zb64 bba843a8d056 #4, mn zb64 9291348d2cbe #28; cand: refs/cand/josef-trim/618 60b237cb9407 #5, /mn 9a785826b28d #29 | 5bca9c14 (base), eaec6d55 (cand) | forensics/replace-discard-20261031/{618,mn}-{base,cand}/fstrim-single |
+| `replace-discard-test.sh` fstrim-raid1 | as above, raid1; files read from the mirror, so the target is checked alone (degraded,ro, check) and by scrub -r | upstream: Josef 5/5 | ~2 min | **base FAIL (loss of the target copy, masked)**: all files read back on the full fs, but target-only mount fails, scrub -r on devid 1: 6.18 337920 csum + 7452 verify, mn 282624 csum + 7576 verify. **cand PASS**: target-only read 20237/20237, scrub 0, check clean | same | same | same | .../fstrim-raid1 |
+| `replace-discard-test.sh` fstrim-raid5 (metadata raid1) | as above, raid5 | upstream: Josef 5/5 | ~6 min | **base FAIL (loss of the target copy, silent to the user)**: every file reads back (rebuilt from parity), replace "finished, 0 errs", scrub -r 169296 csum errors on devid 1 (both lanes). **cand PASS**: scrub 0 | same | same | same | .../fstrim-raid5 |
+| `replace-discard-test.sh` resume-single | crash image of a running replace, the kthread resumes it at mount, one fstrim during the resumed copy | upstream: Josef 5/5 (and 9 for the mount) | ~2 min | **base FAIL (loss)** both lanes, as fstrim-single; **cand PASS** both (resumed target also skipped: 0 discard sectors on it). Crash cursor 0 (item not yet committed) | same | same | eaec6d55 | .../{618,mn}-{base2,cand}/resume-single |
+| `replace-discard-test.sh` dsync-raid1, dsync-raid5 (control) | mount -o discard=sync with a writer adding / deleting during the replace | regression (1-10) | ~2 / ~6 min | PASS on all four kernels; member discards during the replace: raid1 ~383000 sectors, raid5 ~26000 (raid1 metadata frees only: raid56 data is never discarded) | same | same | 5bca9c14 / eaec6d55 | .../dsync-* |
+| `replace-discard-test.sh` dasync-raid1 (control), dasync-raid5 | mount -o discard=async; raid1 at 250 ms dm-delay so the replace outlasts the 2 min async delay | regression (1-10) | ~4 / ~6 min | raid1 PASS on all four (~330000-377000 discarded sectors during the replace). raid5 **BLIND** on all four: no discard reaches a member (btrfs_map_discard() refuses RAID5/6, "we don't discard raid56 yet"; control without a replace: same) | same | same | eaec6d55 | .../dasync-*, r5async-control.txt |
+| `replace-discard-test.sh` noK-raid1, noK-raid5 (control) | replace without -K: progs discards the whole, unpoisoned target before the copy | regression (1-10) | ~2 / ~6 min | PASS on all four (target +33554432 discard sectors before the copy, marker region zero) | same | same | eaec6d55 | .../noK-* |
+| `replace-discard-test.sh` mkfsnoK-raid1, mkfsnoK-raid5 (control) | mkfs without -K (members discarded at mkfs), then replace | regression (1-10) | ~2 / ~6 min | PASS on all four (mkfs discarded 67108864 / 100663296 sectors) | same | same | eaec6d55 | .../mkfsnoK-* |
+| `replace-status-test.sh` RS_MOUNT_OPTS=noatime,discard=async (clean, cancel, unc-raid1, unc-raid5, werr) | the status / cancel / write-error paths with async discard on | 2, 3, 5, 7 | ~15 min | PASS, same as nodiscard: unc 3/3 (raid1, raid5), werr canceled "failed -5" at 15.9% with 16384 write errs, source write_io_errs 0, cancel at 3.4%; unc-raid5 target strip check invalid(discard), not run. Discards issued not measured | 2026-10-31 21:45-21:59 (6.18), 22:04-22:28 (mn) | cand kernels #5 / #29 (the patch only touches FITRIM) | eaec6d55 | .../{618,mn}-dasync-reruns/rs-* |
+| `replace-cancel-persist-test.sh` RCP_MOUNT_OPTS=noatime,discard=async (meta, cancel, ok) | idle unmount after a failed / canceled / good replace with async discard on | 1, 7 | ~11 min | PASS on both lanes, as nodiscard: meta canceled at 0.3% (4 uncorr), cancel canceled at 3.4%, ok FINISHED; item after the idle umount CANCELED / CANCELED / FINISHED, every remount ok | same | same | eaec6d55 | .../{618,mn}-dasync-reruns/rcp-* |
+
+All rows: no WARNING / BUG / KASAN / lockdep / hung task, taint 0,
+debug_locks 1. "same" = the kernels / date of the first row.
+
+Not covered: zoned (the zone reset path in `do_discard_extent()` does
+send to the target); a resume whose crash cursor is past 0 (the item was
+not committed 3 s into the copy); fstrim during a replace on raid6 /
+raid10 / dup data (same code path, not run); scrub during a replace with
+discards.
+
## Not covered / open
- Patch 1's commit-failure branch (the commit after the cleanup fails; only
- Patch 6 after a remount (reports 0 by design).
- `alloc-hints-replace-test.sh` (part of the retrofit set) was not rerun:
it belongs to allocator-hints.
+- FITRIM during a replace discards the target (Josef Bacik's 5/5, see
+ "Replace with discards"): upstream, not fixed by this series; the
+ candidate refs/cand/josef-trim/{618,mn} fixes it on both lanes.
+- replace-abort-test.sh was not rerun with discards (~100 min, ~100 GiB).
### Questions for Zygo