--- /dev/null
+From 8763f9e2126d43bc4504612d63f08807aec86a7e Mon Sep 17 00:00:00 2001
+From: Zygo Blaxell <ce3g8jdj@umail.furryterror.org>
+Date: Wed, 21 Oct 2026 19:15:12 -0400
+Subject: [PATCH 0/1] btrfs: resize: allow shrinking a missing device
+To: linux-btrfs@vger.kernel.org
+
+A failed device can't be replaced by one that is even slightly smaller:
+dev-replace refuses a target smaller than the source's recorded size, and
+the resize ioctl refuses a missing device because it isn't writeable. The
+only way out has been "btrfs device remove missing", which rewrites every
+chunk that had a stripe on the missing device. That takes a long time and,
+on a full filesystem, may not be possible at all.
+
+The patch lets resize shrink a missing device, relocating the chunks above
+the new size as for a present device. The smaller replacement then fits.
+A missing device has no block device to measure, so it can't grow: "max",
+"+size" and an absolute size above the current one return -EINVAL. A
+present device that isn't writeable still gets -EPERM.
+
+Qu Wenruo wrote this patch in November 2019 for a RAID10 user who hit the
+problem; it was never merged. This version fixes two bugs in it: the
+"+size" check on a missing device only logged a message and went ahead,
+and an absolute size skipped the check entirely, so growth was refused
+only by btrfs_grow_device() returning -EACCES. It also treats a device
+with no bdev as missing, whether or not BTRFS_DEV_STATE_MISSING is set,
+because a device can go missing at runtime while its bdev pointer is still
+set, and a rescan can clear the MISSING bit while no bdev is open.
+
+Tested with resize-missing-test.sh on raid1 and raid10, misc-next and
+6.18, with the device lost before mount and, on misc-next, at runtime
+(a null_blk member powered off under the mounted filesystem). The grow
+attempts fail with -EINVAL, shrinking relocates everything above the new
+size, and replacing with a target of exactly the shrunk size succeeds;
+scrub, checksums and btrfs check are clean afterwards. Without the patch,
+every resize of the missing device fails with -EPERM and the replace is
+refused.
+
+Qu Wenruo (1):
+ btrfs: resize: allow shrinking a missing device
+
+ fs/btrfs/ioctl.c | 32 ++++++++++++++++++++++++++++----
+ 1 file changed, 28 insertions(+), 4 deletions(-)
+
+
+base-commit: 0c48d26d46bdd6422bb91ba7f1c519a2993116dc
+--
+2.47.3
+
--- /dev/null
+# Testing: resize-missing
+
+The one patch lets the resize ioctl shrink a missing device, so that a
+replacement smaller than the missing device's recorded size can be used
+with `btrfs replace`. A missing device is one with
+`BTRFS_DEV_STATE_MISSING` set or no bdev. It cannot grow: `max`, `+size`
+and an absolute size above the current one return -EINVAL. A present,
+non-writeable device still gets -EPERM.
+
+Without the patch, every resize of a missing device fails with -EPERM
+("resizer unable to apply on readonly device"), and `btrfs replace`
+refuses a target smaller than the source ("target device smaller than
+source device").
+
+This is a rewrite of Qu Wenruo's 2019 patch (874977e114cc0 as ported to
+7.0). The original had two bugs, which a test must rule out:
+- `+size` on a missing device logged a message and carried on, with no
+ error returned;
+- an absolute size skipped the missing-device check, so only
+ `btrfs_grow_device()`'s -EACCES stopped the growth.
+
+To verify, a test must show:
+- every way of growing a missing device (`max`, `+size`, absolute larger)
+ is refused, and the recorded size is unchanged;
+- shrinking a missing device works and relocates every chunk that had a
+ stripe above the new size;
+- a replace with a target of exactly the shrunk size then succeeds, and
+ the data is intact (scrub, checksums, `btrfs check`, non-degraded
+ remount);
+- resizing a present device still works;
+- the same holds for a device that goes missing at runtime (misc-next has
+ `btrfs_remove_bdev()`, which sets MISSING while the bdev is still set).
+
+Added to both lanes 2026-10-21: misc-next topic 8763f9e2126d (testing
+723245adb5c1, zb64 2550544f9c6e), 6.18 topic 42d60c710e72 (testing
+ba7d11a7b7ac, zb64 7626a4a10198). The tested kernels were built from
+`refs/cand/resize-missing/{mn,618}/zb64`: the previous lane heads plus
+this patch, with the same fs/btrfs trees as the new zb64 heads.
+
+## Existing tests
+
+None. No harness test resized a missing device.
+
+## New tests
+
+| test | verifies | patch(es) | duration | last result | date | kernel | harness | evidence |
+|---|---|---|---|---|---|---|---|---|
+| `resize-missing-test.sh` raid1 (3 devs), raid10 (4 devs), device lost before mount | grow refused (`max`, `+1G`, absolute cur+1G: EINVAL, size unchanged); absolute cur is a no-op; `-512M` and a shrink below the highest extent relocate; present device `-1G` / `max` work; replace with a target of exactly the shrunk size, then scrub, checksums, `btrfs check`, non-degraded remount | 1 | ~3-5 min per profile | PASS on both profiles, no WARN/BUG/KASAN/lockdep/hung task | 2026-10-21 | misc-next cand zb64 86426366cf7a (7.3.0-rc7+ #1, ahive) | 9a2358c | forensics/n-rsz-20261021/ahive/candidate-static/ |
+| same | as above | 1 | ~3-5 min per profile | PASS on both profiles, kernel log clean | 2026-10-21 | 6.18 cand zb64 55546d681ccf (6.18.57+ #1, ehive) | 9a2358c | forensics/n-rsz-20261021/ehive/candidate-static/ |
+| same, runtime loss (raid1; a null_blk member powered off under the mounted fs) | the device counts as missing after `btrfs_remove_bdev()`; same resize, replace and verification steps | 1 | ~5 min | PASS, no oops | 2026-10-21 | misc-next cand zb64 86426366cf7a (#1, with `CONFIG_BLK_DEV_NULL_BLK=y`) | 9a2358c | forensics/n-rsz-20261021/ahive/candidate-runtime/ |
+| same, negative control (`RM_EXPECT=base`) | without the patch every resize of the missing device is EPERM and the smaller replace is refused; a full-size replace (control) works | - | ~3-5 min per profile | as expected on both profiles and both lanes | 2026-10-21 | misc-next zb64 568c45b64012 (#5, ahive); 6.18 zb64 39e1125533c7 (6.18.57+ #2, ehive: the candidate minus the patch) | 9a2358c | forensics/n-rsz-20261021/{ahive,ehive}/base-raid{1,10}/ |
+| W=1 `make fs/btrfs/`, then a full bzImage | no new warnings; the kernel links | 1 | - | no new warning in ioctl.c on either lane; the known ones (stripe_whole_bytes unused, frame-size warnings in free-space-cache.c and tree-log.c) are identical with and without the patch; bzImage rc 0 on all three kernels | 2026-10-21 | mn 86426366cf7a, 618 55546d681ccf and 39e1125533c7 | 9a2358c | forensics/n-rsz-20261021/{ahive,ehive}/w1-*.txt |
+
+**resize-missing-test.sh.**
+
+Fixture:
+- 3 GiB loop images, every member including the replace target prefilled
+ with labelled poison. mkfs -K, mount -o nodiscard, `replace -K`.
+- reclaim-regimen stopped for the run.
+- About 1 GiB of files with recorded sha256. The script keeps writing
+ until the victim device has an extent in its top 512 MiB (checked with
+ `dump-tree -t dev` after `blockdev --flushbufs`). It then deletes the
+ earliest files down to 1 GiB and runs `balance -dusage=50`, so the
+ degraded filesystem has room for the replace. Without that release, the
+ replace hit ENOSPC on the base kernels too: a fixture problem, not the
+ patch.
+- The victim is lost with `losetup -d` before a `-o degraded` mount, or, in
+ the runtime arm, by powering off its null_blk device under the mounted
+ filesystem.
+
+Coverage of the top 512 MiB: raid1 reached it on every run (highest victim
+extent 2581-3072 MiB). raid10 reached it only on the misc-next candidate
+(2876 MiB); on the other raid10 runs the highest extent was at 2252 MiB.
+There, the second shrink, to below the highest extent, is the step that
+relocates. Every run ends with no victim extent above the new size.
+
+The replace target size is the victim's committed `total_bytes` after the
+shrink, read from the dev item in the chunk tree. `btrfs fi show` and
+`dev usage` print size 0 for a missing device.
+
+Messages seen on the candidates:
+- `resizer unable to apply 'max' on missing device N`;
+- `resizer unable to grow missing device N`, shared by `+size` and an
+ absolute grow;
+- `resize device <missing disk> (devid N) from A to B`.
+
+## Open
+
+- In the runtime arm, the dead null_blk device kept receiving writes after
+ "has gone missing, continue as degraded" (`errs: wr 1..164`, then
+ `rd 1..5` and "scrub: fixed up error" during the replace). This looks
+ like existing runtime-missing behaviour and does not involve the resize
+ path, but it was not compared against a kernel without the patch.
+- Not covered: a missing device that is the source of a suspended
+ dev-replace (the exclusive operation normally prevents a concurrent
+ resize, but a replace suspended at mount is a separate state), and
+ zoned devices.