diff --git a/volumes.c b/volumes.c index 30090ce5..0dd082cd 100644 --- a/volumes.c +++ b/volumes.c @@ -530,10 +530,12 @@ static int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes, return find_free_dev_extent_start(device, num_bytes, 0, start, len); } -static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans, - struct btrfs_device *device, - u64 chunk_offset, u64 num_bytes, u64 *start, - int convert) +/* + * Insert one device extent into the fs. + */ +int btrfs_insert_dev_extent(struct btrfs_trans_handle *trans, + struct btrfs_device *device, + u64 chunk_offset, u64 num_bytes, u64 start) { int ret; struct btrfs_path *path; @@ -546,18 +548,8 @@ static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans, if (!path) return -ENOMEM; - /* - * For convert case, just skip search free dev_extent, as caller - * is responsible to make sure it's free. - */ - if (!convert) { - ret = find_free_dev_extent(device, num_bytes, start, NULL); - if (ret) - goto err; - } - key.objectid = device->devid; - key.offset = *start; + key.offset = start; key.type = BTRFS_DEV_EXTENT_KEY; ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*extent)); @@ -583,6 +575,22 @@ err: return ret; } +/* + * Allocate one free dev extent and insert it into the fs. + */ +static int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans, + struct btrfs_device *device, + u64 chunk_offset, u64 num_bytes, u64 *start) +{ + int ret; + + ret = find_free_dev_extent(device, num_bytes, start, NULL); + if (ret) + return ret; + return btrfs_insert_dev_extent(trans, device, chunk_offset, num_bytes, + *start); +} + static int find_next_chunk(struct btrfs_fs_info *fs_info, u64 *offset) { struct btrfs_root *root = fs_info->chunk_root; @@ -1107,7 +1115,7 @@ again: list_move_tail(&device->dev_list, dev_list); ret = btrfs_alloc_dev_extent(trans, device, key.offset, - calc_size, &dev_offset, 0); + calc_size, &dev_offset); if (ret < 0) goto out_chunk_map; @@ -1241,8 +1249,12 @@ int btrfs_alloc_data_chunk(struct btrfs_trans_handle *trans, while (index < num_stripes) { struct btrfs_stripe *stripe; - ret = btrfs_alloc_dev_extent(trans, device, key.offset, - calc_size, &dev_offset, convert); + if (convert) + ret = btrfs_insert_dev_extent(trans, device, key.offset, + calc_size, dev_offset); + else + ret = btrfs_alloc_dev_extent(trans, device, key.offset, + calc_size, &dev_offset); BUG_ON(ret); device->bytes_used += calc_size; diff --git a/volumes.h b/volumes.h index b4ea93f0..44284ee7 100644 --- a/volumes.h +++ b/volumes.h @@ -268,6 +268,9 @@ int btrfs_open_devices(struct btrfs_fs_devices *fs_devices, int flags); int btrfs_close_devices(struct btrfs_fs_devices *fs_devices); void btrfs_close_all_devices(void); +int btrfs_insert_dev_extent(struct btrfs_trans_handle *trans, + struct btrfs_device *device, + u64 chunk_offset, u64 num_bytes, u64 start); int btrfs_add_device(struct btrfs_trans_handle *trans, struct btrfs_fs_info *fs_info, struct btrfs_device *device);