btrfs: write the remaining balance limits back to the balance item
The limit filters are counted down in memory: should_balance_chunk()
decrements limit, or limit_max of a limit_min..limit_max range, for every
chunk that passes the other filters, and __btrfs_balance() restores them
after its counting pass. A balance resumed after a pause continues with
what is left. The balance item is never updated, so a balance resumed
after an unmount or a crash starts again from the limits that were given:
every resume can relocate up to limit more chunks. The minimum shifts
too: the counting pass of the resume sees only the chunks that are left,
which can be fewer than limit_min, and the resume then relocates nothing.
Count a chunk against both ends of the range, and write the remaining
limits to the balance item, once the chunk has been processed. limit and
limit_max are still decremented when the chunk is selected, as before,
and limit_min is now decremented (down to 0) when it has been processed,
after the limit_min check has let it through, so limit_min means the
remaining minimum, on disk and in memory. A resume after an unmount or a
crash then continues from the remaining limits, like a resume after a
pause, and its limit_min check compares the chunks that are left with the
minimum that is left. The values written are those of the real pass:
the counting pass returns to the loop before any chunk is processed, and
its decrements are undone before the real pass starts.
A chunk counts as processed when its relocation has returned for that
chunk: relocated, or left in place by ENOSPC or an active swapfile. This
is what the in-memory limit has always counted, so a resume after an
unmount or a crash now behaves exactly like a resume after a pause. A
pause cannot stop a chunk halfway: the pause request is checked between
chunks, so the chunk being relocated is processed and written first. A
cancel or a fatal signal can stop a relocation, but it also ends the
balance and deletes the item. If the item cannot be written, the balance
stops with the error, like any other error in the loop, and its item is
deleted; if the item survives that (an aborted transaction), it holds
the last committed limits, within the bound below.
The update takes its own handle on the running transaction once the
relocation has returned, and ends it without committing. The
transaction that removed the relocated chunk has ended by then, and is
usually still the running one, so both changes usually commit together,
and the update can never commit before the removal. A crash in between
loses the count of that one chunk, and the resumed balance can relocate
one chunk more than requested, never one fewer. Making the update part
of the removal's transaction would mean passing the balance into
btrfs_relocate_chunk(), which device shrink and removal also use.
Only chunk types with a limit filter write the item, once per processed
chunk; a balance without limits makes no more writes than before. The
remaining limits are visible: the BTRFS_IOC_BALANCE_PROGRESS ioctl
already returned them during the balance and now returns the same values
after a remount, and the "balance: resume" log line shows them.