btrfs: replace the balance resume heuristics with a vrange bound
When a balance is resumed after a pause or an unmount,
update_balance_args() adds a usage=90 filter to every chunk type that is
not being converted. That changes the set of block groups the original
request selected. It skips full block groups the interrupted balance had
not reached yet, so the resumed balance can finish without having done
what was asked, and it selects again any block group the interrupted
balance created that is less than 90% full, relocating the same data a
second time. For the chunk types that are being converted it adds the
soft filter instead, so the resume does not convert again the block
groups the interrupted balance converted, but it also skips the ones
that already had the target profile when the balance started, which the
original request selected.
Replace it with a bound fixed when the balance starts. Before the
balance item is written, limit the vrange filter of each chunk type to
the addresses below the next chunk to be allocated: add a 0..end range
when the request had no vrange filter, or lower the end of the one it
had. Every block group that existed when the balance started stays
selected until the balance relocates it.
New chunks are allocated after the last chunk, but relocation can remove
the highest chunks without allocating any (their data fits in free space
lower down), and the next chunk would then reuse their addresses, below
the bound. A resumed balance would select it again, although the
interrupted one, walking downward, never would have. So while a balance
item exists, find_next_chunk() never returns an address below the bound:
an in-memory floor, set when the balance starts and when the balance
item is recovered at mount, and cleared when the item is deleted. Every
chunk allocated in that time, relocation targets included, stays outside
the range, on the first run and after a resume. The gap the floor can
leave in the logical address space is harmless.
A resume now uses the arguments exactly as they were stored: the bound
already keeps out every block group the interrupted balance created,
converted ones included, so neither the usage filter nor the soft filter
is needed, and update_balance_args() goes away.
The bound does not move as the balance progresses. Block groups the
balance has relocated are gone, so on resume the walk only revisits the
chunks it left in place above the point where it stopped (filtered out,
ENOSPC, active swapfile) and checks them against the filters again.
Nothing is skipped.
Because the bound is written with the balance item and never changes,
there is no progress to persist: a resume after an unmount or a crash
needs no extra writes.
A balance item recovered at mount is limited in the same way before the
floor is set from it: a vrange 0..end is added to each chunk type that
has none, and a vrange that ends above the next chunk is lowered to it.
That covers items written by older kernels, which have no vrange or a
vrange the user gave, which can end anywhere (a 1 TiB end would put
every new chunk at 1 TiB and above). A bound written by this kernel
changes only when relocation removed the highest chunks and allocated
none above it; no chunk exists between the next chunk and the old end,
so lowering it drops nothing. When the recovered arguments change,
they are written back to the balance item once, when the balance
resumes, so a resume that is interrupted again resumes with the same
bound and not with a new one that includes the chunks the first resume
allocated. If that write fails, the balance is not resumed: as for the
other errors at that point, it ends with the error logged, and its item
is deleted. Nothing has been relocated yet, so if the item survives (the
delete fails too, or the system crashes first), the next mount bounds it
again to the same chunks.
A balance item written by an older kernel therefore resumes with the
bound and without the usage or soft filter: the resumed balance restarts
from the beginning, relocating again every block group the request
selects, including the ones the interrupted run created or converted,
but it skips none.
The bound is visible: `btrfs balance status -v`, the
BTRFS_IOC_BALANCE_PROGRESS ioctl and the "balance: start" and "balance:
resume" log lines show a vrange the user did not give.