Commit 3b4ffa40 authored by Naohiro Aota's avatar Naohiro Aota Committed by David Sterba
Browse files

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: default avatarJosef Bacik <josef@toxicpanda.com>
Reviewed-by: default avatarJohannes Thumshirn <johannes.thumshirn@wdc.com>
Signed-off-by: default avatarNaohiro Aota <naohiro.aota@wdc.com>
Reviewed-by: default avatarDavid Sterba <dsterba@suse.com>
Signed-off-by: default avatarDavid Sterba <dsterba@suse.com>
parent c4a816c6
Loading
Loading
Loading
Loading
+58 −20
Original line number Diff line number Diff line
@@ -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 @@ again:

		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 @@ next:
	 */
	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;
		}