btrfs: do not wait for device replace in the bio completion workers
Device replace can hang forever at the end, after "replace status"
already reports it finished, when data that needs read repair is read
while it finishes:
and every other task submitting bios to the filesystem blocks behind
them.
btrfs_dev_replace_finishing() sets BTRFS_FS_STATE_DEV_REPLACING and
waits for the bio counter to drop to zero. Every bio that
btrfs_submit_chunk() counted keeps its reference until its completion
runs: a RAID56 bio until btrfs_raid56_end_io() runs in the btrfs-rmw
worker that completes its rbio, a mirrored write until
orig_write_end_io_work() runs in btrfs-endio. Both workqueues run at
most thread_pool workers at a time.
Read repair runs in those same workers: btrfs_check_read_bio() is called
by simple_end_io_work() in btrfs-endio and by btrfs_raid56_end_io() in
btrfs-rmw. It takes the bio counter when it submits a repair read to
the next mirror (repair_one_sector() and btrfs_end_repair_bio(), through
btrfs_submit_chunk()), and when it writes the good copy back
(btrfs_repair_bbio_failure()). While replace finishing has the flag
set, both wait for replace to finish. Once every worker of the pool
waits there, the counted bios queued behind them never complete, and
replace never finishes.
This is easy to hit when a missing device is replaced while its data is
read: each such read fails, is repaired by a rebuild read through
btrfs-rmw, and writes the rebuilt block back to the replace target. On
a 4-device RAID5 with one device missing, 8 O_DIRECT readers and 2
writers, it hangs in about half the replaces. At one hang the counter
was 15: 2 rebuild rbios queued on btrfs-rmw behind the 8 workers waiting
in the write-back, and 13 rebuild rbios waiting for the stripe locks
those 2 hold, one bio each. On RAID6, a repair read that rebuilds a
sector from P and fails its csum goes on to rebuild from Q, and that
submission waits the same way: with the P strips corrupted, the same
test hung in the first replace, with all 8 btrfs-rmw workers in
btrfs_end_repair_bio() -> btrfs_submit_bbio() and a counter of 68 = 23
rebuild rbios queued on btrfs-rmw + 45 waiting for their stripe locks.
Run the parts of read repair that take the bio counter in a new
workqueue, btrfs-repair, that no bio completion waits for. A worker
that waits there for replace to finish holds nothing replace waits for:
the repair bbio is not submitted yet, and the read being repaired is
held back by fbio->repair_count, which is not counted.
As before, the read being repaired completes once the write-back is
done; while replace finishes, that is once replace has finished.
Fixes: 7609afac67754 ("btrfs: handle checksum validation and repair at the storage layer") Assisted-by: Claude:claude-opus-5-5