Message ID | 20181127083828.23861-4-wqu@suse.com (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
Series | btrfs-progs: image: Fix error when restoring multi-disk image to single disk | expand |
On 27.11.18 г. 10:38 ч., Qu Wenruo wrote: > We have btrfs_alloc_dev_extent() accepting @convert flag to toggle > special handling for convert. > > However that @convert flag only determine whether we call > find_free_dev_extent(), and we may later need to insert dev extents > without searching dev tree. > > So refactor btrfs_alloc_dev_extent() into 2 functions, > btrfs_alloc_dev_extent(), which will try to find free dev extent, and > btrfs_insert_dev_extent(), which will just insert a dev extent. > > For implementation, btrfs_alloc_dev_extent() will call > btrfs_insert_dev_extent() anyway, so no duplicated code. > > This removes the need of @convert parameter, and make > btrfs_insert_dev_extent() public for later usage. > > Signed-off-by: Qu Wenruo <wqu@suse.com> This looks much better: Reviewed-by: Nikolay Borisov <nborisov@suse.com> > --- > volumes.c | 48 ++++++++++++++++++++++++++++++------------------ > volumes.h | 3 +++ > 2 files changed, 33 insertions(+), 18 deletions(-) > > diff --git a/volumes.c b/volumes.c > index 30090ce5f8e8..0dd082cd1718 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 b4ea93f0bec3..44284ee75adb 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); >
diff --git a/volumes.c b/volumes.c index 30090ce5f8e8..0dd082cd1718 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 b4ea93f0bec3..44284ee75adb 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);
We have btrfs_alloc_dev_extent() accepting @convert flag to toggle special handling for convert. However that @convert flag only determine whether we call find_free_dev_extent(), and we may later need to insert dev extents without searching dev tree. So refactor btrfs_alloc_dev_extent() into 2 functions, btrfs_alloc_dev_extent(), which will try to find free dev extent, and btrfs_insert_dev_extent(), which will just insert a dev extent. For implementation, btrfs_alloc_dev_extent() will call btrfs_insert_dev_extent() anyway, so no duplicated code. This removes the need of @convert parameter, and make btrfs_insert_dev_extent() public for later usage. Signed-off-by: Qu Wenruo <wqu@suse.com> --- volumes.c | 48 ++++++++++++++++++++++++++++++------------------ volumes.h | 3 +++ 2 files changed, 33 insertions(+), 18 deletions(-)