btrfs: refactor find_free_dev_extent_start()

Factor out two functions from find_free_dev_extent_start().
dev_extent_search_start() decides the starting position of the search.
dev_extent_hole_check() checks if a hole found is suitable for device
extent allocation.

These functions also have the switch-cases to change the allocation
behavior depending on the policy.

Reviewed-by: Josef Bacik <josef@toxicpanda.com>
Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Signed-off-by: Naohiro Aota <naohiro.aota@wdc.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
Naohiro Aota 2020-02-25 12:56:09 +09:00 committed by David Sterba
parent c4a816c67c
commit 3b4ffa4088

View file

@ -1381,6 +1381,59 @@ static bool contains_pending_extent(struct btrfs_device *device, u64 *start,
return false;
}
static u64 dev_extent_search_start(struct btrfs_device *device, u64 start)
{
switch (device->fs_devices->chunk_alloc_policy) {
case BTRFS_CHUNK_ALLOC_REGULAR:
/*
* We don't want to overwrite the superblock on the drive nor
* any area used by the boot loader (grub for example), so we
* make sure to start at an offset of at least 1MB.
*/
return max_t(u64, start, SZ_1M);
default:
BUG();
}
}
/**
* dev_extent_hole_check - check if specified hole is suitable for allocation
* @device: the device which we have the hole
* @hole_start: starting position of the hole
* @hole_size: the size of the hole
* @num_bytes: the size of the free space that we need
*
* This function may modify @hole_start and @hole_end to reflect the suitable
* position for allocation. Returns 1 if hole position is updated, 0 otherwise.
*/
static bool dev_extent_hole_check(struct btrfs_device *device, u64 *hole_start,
u64 *hole_size, u64 num_bytes)
{
bool changed = false;
u64 hole_end = *hole_start + *hole_size;
/*
* Check before we set max_hole_start, otherwise we could end up
* sending back this offset anyway.
*/
if (contains_pending_extent(device, hole_start, *hole_size)) {
if (hole_end >= *hole_start)
*hole_size = hole_end - *hole_start;
else
*hole_size = 0;
changed = true;
}
switch (device->fs_devices->chunk_alloc_policy) {
case BTRFS_CHUNK_ALLOC_REGULAR:
/* No extra check */
break;
default:
BUG();
}
return changed;
}
/*
* find_free_dev_extent_start - find free space in the specified device
@ -1427,12 +1480,7 @@ static int find_free_dev_extent_start(struct btrfs_device *device,
int slot;
struct extent_buffer *l;
/*
* We don't want to overwrite the superblock on the drive nor any area
* used by the boot loader (grub for example), so we make sure to start
* at an offset of at least 1MB.
*/
search_start = max_t(u64, search_start, SZ_1M);
search_start = dev_extent_search_start(device, search_start);
path = btrfs_alloc_path();
if (!path)
@ -1490,18 +1538,8 @@ static int find_free_dev_extent_start(struct btrfs_device *device,
if (key.offset > search_start) {
hole_size = key.offset - search_start;
/*
* Have to check before we set max_hole_start, otherwise
* we could end up sending back this offset anyway.
*/
if (contains_pending_extent(device, &search_start,
hole_size)) {
if (key.offset >= search_start)
hole_size = key.offset - search_start;
else
hole_size = 0;
}
dev_extent_hole_check(device, &search_start, &hole_size,
num_bytes);
if (hole_size > max_hole_size) {
max_hole_start = search_start;
@ -1540,8 +1578,8 @@ static int find_free_dev_extent_start(struct btrfs_device *device,
*/
if (search_end > search_start) {
hole_size = search_end - search_start;
if (contains_pending_extent(device, &search_start, hole_size)) {
if (dev_extent_hole_check(device, &search_start, &hole_size,
num_bytes)) {
btrfs_release_path(path);
goto again;
}