btrfs: allocator hints: keep the hint across device replace
Device replace drops the allocation hint, and changing a hint during a
replace can abort the transaction.
The replace target takes over the source's dev item when the replace
finishes, but not the in-memory device->type. The on-disk item keeps the
hint only until the next btrfs_update_device() on the new device (any
chunk allocation on it), which writes type 0 over it. Copy the type
together with the devid, under the same locks.
During a replace, the target is visible as devinfo/0 and has no dev item.
After the finish, the source's devid is BTRFS_DEV_REPLACE_DEVID until its
sysfs directory goes away. A write to 'type' on either device makes
btrfs_update_device() miss the dev item (-ENOENT), and the store aborts
the transaction, forcing the filesystem read-only in the middle of the
replace. Device remove has the same window between deleting the dev
item and removing the sysfs directory.
Return -EBUSY for the replace target and for a device with the replace
devid. An -ENOENT that still gets past these checks (remove, or a
replace that finishes after them) ends the transaction without aborting
it and returns -ENODEV.
The source's hint can still be changed during a replace, and the target
inherits it at the finish, so a replace can move to a device of a
different type, e.g. SSD to HDD. A write that races the finish can be
lost: it misses the dev item and returns -ENODEV, or the finish has
already copied the old type. That is tolerable, and the next patch
closes the window.