Limits: case 2 (delalloc already in the range when it is locked) is not
driven.
+## Candidate fix to patch 2 (not promoted; 2026-10-26)
+
+"btrfs: reflink: relock the inline-copied block before returning"
+(Fixes: patch 2) is placed right after patch 2 on the candidates
+`refs/cand/reflink/mn/deadlock-fixes` bfdd15368ad8 (relock 4a51e8a65d8a)
+and `refs/cand/reflink/618/deadlock-fixes` 87e0137eb07a (relock
+e1682b443773). Patch 2 unlocks the inline-copied destination block once
+its inode update is done; the callers then unlock the whole range again,
+a second unlock of a block another task (writeback, a read) may hold by
+then. The fix locks the block again at the end of btrfs_clone(), after
+releasing the path. It relies on the callers locking a block-aligned
+range (e7f4c05a8307, in the misc-next base since v7.3-rc1); 6.18 gets a
+backport of e7f4c05a8307 (7ff63caa110f, author Qu Wenruo) before it.
+
+The double unlock is silent (extent-io-tree unlocks of an unlocked range
+are not checked), so there is no failing reproducer; the evidence is the
+review plus clean regression runs. A first version without the 6.18
+backport HUNG on 6.18: the dedupe path locked `[dst_loff, dst_loff+len-1]`
+unaligned, so the relocked block's tail `[61940, 65535]` stayed locked
+after the dedupe returned; writeback blocked in
+`find_lock_delalloc_range()` and umount stayed in D state (dhive kernel
+log in `raid56-wh-harness/forensics/reflink-inline-20261026/cand618-v1-relock-hang-dmesg.txt`).
+That is why the reflink-inline tests check that umount returns.
+
+| test | verifies | duration | last result | date | kernel | evidence |
+|---|---|---|---|---|---|---|
+| `reflink-inline-largefolio-test.sh` (umount returns after every arm) | the relock is balanced on both clone and dedupe | 3 s | PASS, umount rc 0 | 2026-10-26 | 6.18 #124 (candidate zb64 tree) | `forensics/reflink-inline-20261026/reflink-regress-cand618/` |
+| `reflink-mixed-inline-test.sh` (SRC_METHOD=falloc) | patch 2's deadlock stays fixed with the relock | 607 s | PASS: 567,037 dedupes, 0 errors, commits 91 -> 4978, KASAN/lockdep clean | 2026-10-26 | 6.18 #124 | same |
+Full regression set and fstests: see `topics/reflink-inline-fixes/TESTING.md`
+(same kernels).
+
## Not covered / open
- Slow unmount seen once on #5 (not a deadlock, recorded for
- Patch 2 has no directed reproducer: neither the mixed-layout source
case nor the pre-existing (asynchronous compression) delalloc case.
+- The -EAGAIN that the clone path returns after three flush-and-retry
+ rounds (patch 2) is not spurious: delalloc that survives
+ btrfs_wait_ordered_range() under both i_rwsem and the exclusive
+ i_mmap_lock is a stuck bit, and proceeding would reopen the deadlock.
+ Whether userspace should see -EAGAIN there is Zygo's call; not changed.
+
### Questions for Zygo
- Patch 2 case 2 (delalloc already in the range when it is locked, from
asynchronous compressed writeback) has no directed reproducer. Is one
wanted, or are the hunts enough?
+- The relock commit fixes patch 2 of this unsent series: fold it into
+ patch 2 before sending, or keep it separate?
--- /dev/null
+From be33f3b2272b357904ead23cff12a8e7007d612b Mon Sep 17 00:00:00 2001
+From: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
+Date: Mon, 26 Oct 2026 17:33:57 -0400
+Subject: [PATCH 0/2] btrfs: reflink: two fixes for copying inline extents into the page cache
+To: linux-btrfs@vger.kernel.org
+
+Two bugs in copy_inline_to_page(), the path that clones or dedupes an
+inline extent into a block that is not at file offset 0.
+
+Patch 1: after copying the inline data to the block's offset in the folio,
+the function zeroes the rest of the block at folio offset datal, as if the
+block were the folio's first. With a block size below the page size, or
+with large folios, that zeroes data in the folio's first block (outside the
+cloned range; the zeroes reach disk with the next write to that block) and
+leaves the tail of the destination block unzeroed. The patch message has
+an xfs_io recipe; with large folios it shows dst bytes 500-4095 zeroed
+after the dedupe.
+
+Patch 2: when decompressing the inline extent fails, the error path
+releases the block's reservation but leaves EXTENT_DELALLOC set on a block
+whose folio is never dirtied. The bit is later cleared with
+EXTENT_DO_ACCOUNTING, which releases the reservation a second time, and
+the decompressor has already overwritten a block that may be uptodate in
+the page cache. The delalloc bits are now cleared and the block's
+uptodate state dropped.
+
+Testing (a KASAN + lockdep 6.18 kernel with large folios, details in TESTING.md
+next to these patches): a clone/dedupe x plain/zlib test that checks the
+destination and the rest of its folio, on disk and in the page cache,
+fails every round on the unfixed kernel (16 of 16) and passes every round
+with patch 1 (32 of 32). Patch 2 has no runtime reproducer (it needs a
+corrupt compressed inline extent); it was reviewed, and the clone/dedupe
+regression tests run clean with both patches.
+
+Patch 1 Fixes: 3115deb381e9, patch 2 Fixes: 05a5a7621ce6.
+
+Zygo Blaxell (2):
+ btrfs: reflink: zero the right part of the folio after copying inline
+ data
+ btrfs: reflink: undo delalloc if inline extent decompression fails
+
+ fs/btrfs/reflink.c | 20 ++++++++++++++++++--
+ 1 file changed, 18 insertions(+), 2 deletions(-)
+
+
+base-commit: 99bc6552614f6b25a4a6f07789300d65d384d6ed
+--
+2.47.3
+
--- /dev/null
+From f36cf85b9ec74df310e9a8f24e53d5142b6cad93 Mon Sep 17 00:00:00 2001
+From: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
+Date: Mon, 26 Oct 2026 15:26:35 -0400
+Subject: [PATCH 1/2] btrfs: reflink: zero the right part of the folio after
+ copying inline data
+To: linux-btrfs@vger.kernel.org
+
+copy_inline_to_page() copies the inline extent's data to the destination
+block at offset_in_folio(folio, file_offset), but then zeroes the rest of
+the block starting at folio offset datal, as if the block were the first
+one in its folio. When it is not, which happens with a block size
+smaller than the page size and with large folios, this:
+
+- zeroes [datal, block_size) of the folio's first block, data outside the
+ cloned range. That block is not marked dirty, so the zeroes stay in the
+ page cache until a later write to the block sends them to disk;
+- leaves the tail of the destination block, past the inline data, with
+ whatever the folio held there instead of zeroes.
+
+To reproduce with large folios (CONFIG_BTRFS_EXPERIMENTAL), 4K pages and
+the default max_inline:
+
+ $ xfs_io -f -c "pwrite -S 0xab 0 500" -c fsync src
+ $ xfs_io -f -c "pwrite -S 0xcd -b 64K 0 64K" -c "pwrite -S 0xab 61440 500" \
+ -c "truncate 61940" -c fsync dst
+ $ xfs_io -c "dedupe src 0 61440 500" dst
+ $ od -A d -t x1 dst | head -4
+
+src is a single inline extent, and dst's first 64K is one folio: the
+truncate happens while that folio is dirty, so it is not split. The
+dedupe copies the inline data into that folio at offset 61440, and the
+read shows bytes 500 to 4095 of dst zeroed.
+
+Zero the tail of the block at the block's offset in the folio, as the
+copy does.
+
+Fixes: 3115deb381e9 ("btrfs: reflink: make copy_inline_to_page() to be subpage compatible")
+Assisted-by: Claude:claude-opus-5-5
+---
+ fs/btrfs/reflink.c | 3 ++-
+ 1 file changed, 2 insertions(+), 1 deletion(-)
+
+diff --git a/fs/btrfs/reflink.c b/fs/btrfs/reflink.c
+index b5d2c094941df..43a960cdfd85a 100644
+--- a/fs/btrfs/reflink.c
++++ b/fs/btrfs/reflink.c
+@@ -138,7 +138,8 @@ static int copy_inline_to_page(struct btrfs_inode *inode,
+ * So what's in the range [500, 4095] corresponds to zeroes.
+ */
+ if (datal < block_size)
+- folio_zero_range(folio, datal, block_size - datal);
++ folio_zero_range(folio, offset_in_folio(folio, file_offset) + datal,
++ block_size - datal);
+
+ btrfs_folio_set_uptodate(fs_info, folio, file_offset, block_size);
+ btrfs_folio_set_dirty(fs_info, folio, file_offset, block_size);
+--
+2.47.3
+
--- /dev/null
+From be33f3b2272b357904ead23cff12a8e7007d612b Mon Sep 17 00:00:00 2001
+From: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
+Date: Mon, 26 Oct 2026 15:26:36 -0400
+Subject: [PATCH 2/2] btrfs: reflink: undo delalloc if inline extent
+ decompression fails
+To: linux-btrfs@vger.kernel.org
+
+copy_inline_to_page() marks the destination block delalloc with
+btrfs_reset_extent_delalloc() before it copies or decompresses the inline
+data into the folio, and dirties the folio only once the data is there.
+If btrfs_decompress() fails, the error path releases the space reserved
+for the block but leaves EXTENT_DELALLOC (and EXTENT_DELALLOC_NEW, if the
+block had no extent) set on a block whose folio is not dirty. Writeback
+never visits it, so the bit stays, and with it an outstanding extent, the
+inode's delalloc_bytes and new_delalloc_bytes and its place on the root's
+delalloc list. When the folio is invalidated or the inode evicted, the
+stale bit is cleared with EXTENT_DO_ACCOUNTING, which releases the block's
+data and metadata reservation a second time.
+
+The decompressors also write the destination range on failure (zeroes, or
+part of the data), so a block that was uptodate in the page cache no
+longer matches what is on disk, and reads keep returning it.
+
+On a decompression failure, clear the delalloc bits, without
+EXTENT_DO_ACCOUNTING or the reservation release bits since the error path
+releases the reservation, and clear the block's uptodate state so the next
+read gets it from disk.
+
+Fixes: 05a5a7621ce6 ("Btrfs: implement full reflink support for inline extents")
+Assisted-by: Claude:claude-opus-5-5
+---
+ fs/btrfs/reflink.c | 17 ++++++++++++++++-
+ 1 file changed, 16 insertions(+), 1 deletion(-)
+
+diff --git a/fs/btrfs/reflink.c b/fs/btrfs/reflink.c
+index 43a960cdfd85a..b95aacecbc927 100644
+--- a/fs/btrfs/reflink.c
++++ b/fs/btrfs/reflink.c
+@@ -120,8 +120,23 @@ static int copy_inline_to_page(struct btrfs_inode *inode,
+ ret = btrfs_decompress(comp_type, data_start, folio,
+ offset_in_folio(folio, file_offset),
+ inline_size, datal);
+- if (ret)
++ if (ret) {
++ /*
++ * The block is delalloc but its folio will not be
++ * dirtied, so writeback would never clear that: do it
++ * here, without the accounting bits, as the space
++ * reserved for the block is released by the error path.
++ * The decompression may have written part of the block,
++ * so it is no longer uptodate either.
++ */
++ btrfs_clear_extent_bit(&inode->io_tree, file_offset,
++ range_end,
++ EXTENT_DELALLOC | EXTENT_DELALLOC_NEW,
++ NULL);
++ btrfs_folio_clear_uptodate(fs_info, folio, file_offset,
++ block_size);
+ goto out_unlock;
++ }
+ flush_dcache_folio(folio);
+ }
+
+--
+2.47.3
+
--- /dev/null
+# Testing: reflink-inline-fixes
+
+Two bugs in copy_inline_to_page(), which clones or dedupes an inline
+extent into a block that is not at file offset 0. Patch 1: the tail of
+the destination block was zeroed at folio offset `datal` instead of at the
+block's offset in the folio plus `datal`, so with large folios (or a block
+size below the page size) the folio's first block lost data and the
+destination block's tail was left with stale bytes. Patch 2: when
+decompression of the inline extent failed, the delalloc bits stayed on a
+block whose folio is never dirtied, and the reservation was released
+twice. To verify patch 1: after the clone/dedupe, everything in the folio
+outside the cloned range must still be what was written there, and the
+destination block's tail must be zero, in the page cache and on disk.
+
+Kernels (dhive, KASAN + PROVE_LOCKING + BTRFS_EXPERIMENTAL, built with
+`raid56-wh-harness/scripts/rig-build-kasan.sh`, bzImage only):
+
+| kernel | built from | uname |
+|---|---|---|
+| 6.18 base (unfixed) | 6.18 zb64 8429ba515340 | `6.18.58+ #122 ... Mon Oct 26 15:17:52 EDT 2026` |
+| 6.18 candidate | b0e95c445846, tree identical to candidate zb64 b04bf5ce754b | `6.18.58+ #124 ... Mon Oct 26 16:17:42 EDT 2026` |
+| misc-next candidate | 2457a7beeff8, tree identical to candidate zb64 f7bf0c144ce5 | not booted: the first build hit truncated objects from a dhive reset; dhive went down before the rebuild |
+
+The candidates also carry the deadlock-fixes relock commit ("btrfs:
+reflink: relock the inline-copied block before returning") and, on 6.18,
+its prerequisite backport of e7f4c05a8307; the reflink tests below cover
+those too. Logs are copied to
+`raid56-wh-harness/forensics/reflink-inline-20261026/`.
+
+## New tests
+
+| test | verifies | patch(es) | duration | last result | date | kernel | harness | evidence |
+|---|---|---|---|---|---|---|---|---|
+| `reflink-inline-largefolio-test.sh` (4 arms x 4 rounds; the candidate run did it twice) | clone and dedupe (plain and zlib inline source) of a 500-byte inline extent into the 16th block of a 64K large folio; checks the whole folio and the destination block through the page cache, then again after rewriting its first byte, fsync and POSIX_FADV_DONTNEED (so the folio's first block goes to disk and is read back); checks that the folio really is large (pagemap/kpageflags compound tail) | 1 | 3 s | base: FAIL, 16 of 16 rounds CORRUPT (plain `[500,4096)` zeroed, zlib `[3000,4096)` zeroed, in the page cache and after writeback); candidate: PASS, 32 of 32 rounds intact | 2026-10-26 | 6.18 #122 / #124 | 37a455f | `reflink-largefolio-base618.log`, `reflink-regress-cand618/` |
+| the patch 1 message recipe (`xfs_io` pwrite/truncate/dedupe/od) | the recipe in the patch message reproduces as written | 1 | 1 s | base: dst bytes 500-4095 read back as zeroes; candidate: clean (all `cd`, inline data at 61440) | 2026-10-26 | 6.18 #122 / #124 | n/a | `reflink-msg-repro-base618.log`, `reflink-msg-repro-cand618.log` |
+
+## Existing tests
+
+| test | verifies | patch(es) | duration | last result | date | kernel | harness | evidence |
+|---|---|---|---|---|---|---|---|---|
+| `reflink-mixed-inline-test.sh` | dedupe of inline-then-regular sources into files under flushoncommit commit pressure (the inline copy path, at volume) | 1, 2 (regression) | 607 s | PASS: 567,037 dedupes, 0 errors, commits 91 -> 4978, KASAN/lockdep clean | 2026-10-26 | 6.18 #124 | 37a455f | `reflink-regress-cand618/` |
+| `dedupe-maxlen-test.sh` | dedupe at the maximum length | regression | 122 s | PASS | 2026-10-26 | 6.18 #124 | 37a455f | `reflink-regress-cand618/` |
+| `dedupe-mtime-test.sh` | dedupe-mtime topic behaviour | regression | 118 s | PASS | 2026-10-26 | 6.18 #124 | 37a455f | `reflink-regress-cand618/` |
+| `delayed-ref-merge-clone-test.sh` (WALKER=1) | clone under delayed-ref merging | regression | 240 s | PASS | 2026-10-26 | 6.18 #124 | 37a455f | `reflink-regress-cand618/` |
+| `fiemap-clone-race-test.sh` | fiemap racing clone | regression | 315 s | INCONCLUSIVE (rc 2: the race window was not hit; 0 errors, no hang) | 2026-10-26 | 6.18 #124 | 37a455f | `reflink-regress-cand618/` |
+| fstests: `-g dedupe` + the btrfs clone-group tests + generic/352 370 562 (`fstests-group-test.sh`) | clone/dedupe regressions | 1, 2 (regression) | > 3 h (btrfs/187 alone 5038 s) | INCOMPLETE: 35 btrfs tests passed (incl. btrfs/187 5038 s, btrfs/301 729 s, 113 117 130 183 189), btrfs/313 not run (needs SCRATCH_DEV_POOL), generic/121 passed; generic/122 reported "inconsistent" only because unmounting TEST_DIR failed with "target is busy" and fsck refused the mounted device (no fsck finding), and generic/136 then aborted on the TEST_DEV mount check (harness state, not a test result); the run died at generic/137 when the VM host took dhive (and bhive) down | 2026-10-26 | 6.18 #124 | 37a455f | dhive `/root/tt/reflink-ft-cand618/` (not yet copied: dhive down) |
+
+## Not covered / open
+
+- misc-next: not tested yet. The candidate kernel's build failed at the
+ link (truncated objects left by a dhive reset; the stale outputs were
+ deleted), and dhive went down at the VM-host level (qemu ports refused,
+ bhive too) before it could be rebuilt. The misc-next topic differs from
+ 6.18 only in context (same two hunks).
+- 6.18 fstests: generic/122, 136 and the rest of the dedupe group after
+ them need a rerun (above).
+- dhive reset twice (14:46 on misc-next #50, idle; 17:11 on 6.18 #124
+ during btrfs/187) with no panic or warning in the persistent kernel log;
+ the 19:08 outage took both VMs on the host down. Treated as host-level
+ (cf. dhive's history of KVM-level wedges), not as a finding against
+ these patches.
+
+- Patch 2 has no runtime reproducer: it needs btrfs_decompress() to fail
+ on an inline extent (a corrupt compressed inline item, or error
+ injection on the decompressor). It was checked by review: the error
+ path clears EXTENT_DELALLOC | EXTENT_DELALLOC_NEW without
+ EXTENT_DO_ACCOUNTING (the reservation is released by the existing error
+ path), and clears the block's uptodate bit so btrfs_do_readpage()
+ re-reads it (it skips uptodate blocks, so the other blocks of the folio
+ are not affected).
+- Subpage (64K pages, 4K blocks) has had bug 1 since 3115deb381e9 (v5.14);
+ not tested here (no 64K-page machine). Large folios reproduce the same
+ arithmetic on x86.