Btrfs: batched discard support for btrfs
v4 cover Btrfs: batched discard support for btrfs
Dear list,
This is V4 of batched discard support, now we will get full mapping of
the free space on each device for RAID0/1/10/DUP instead of just a single stripe length, and tested with xfsstests 251, Thanks.
Changelog V4:
*make btrfs_map_block() return full mapping.
Changelog V3:
*fix style problems.
*rebase to 2.6.38-rc7.
Changelog V2:
*Check if we have devices support trim before trying to trim the fs, also adjust
minlen according to the discard_granularity.
*Update reserved extent calculations in btrfs_trim_block_group().
*Call cond_resched() without checking need_resched()
*Use bitmap_clear_bits() and unlink_free_space() instead of btrfs_remove_free_space(),
so we won't search the same extent for twice.
*Try harder in btrfs_discard_extent(), now we won't report errors
if it's not a EOPNOTSUPP.
*make sure the block group is cached before trimming it,or we'll see an empty caching
tree if the block group is not cached.
*Minor return value fix in btrfs_discard_block_group().
PATCH v4 1/4 Btrfs: make update_reserved_bytes() public
Make the function public as we should update the reserved extents calculations after taking out an extent for trimming.
--- fs/btrfs/ctree.h | 2 ++ fs/btrfs/extent-tree.c | 16 +++++++--------- 2 files changed, 9 insertions(+), 9 deletions(-) create mode 100644 fs/btrfs/Module.symvers
fs/btrfs/Module.symvers new file +0 -0
fs/btrfs/ctree.h +2 -0
@@ -2157,6 +2157,8 @@ int btrfs_free_extent(struct btrfs_trans_handle *trans, u64 root_objectid, u64 owner, u64 offset);int btrfs_free_reserved_extent (struct btrfs_root *root,u64 start,u64 len); +int btrfs_update_reserved_bytes (struct btrfs_block_group_cache *cache, +u64 num_bytes,int reserve,int sinfo);int btrfs_prepare_extent_commit (struct btrfs_trans_handle *trans,struct btrfs_root *root); int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
fs/btrfs/extent-tree.c +7 -9
@@ -36,8 +36,6 @@static int update_block_group (struct btrfs_trans_handle *trans,struct btrfs_root *root,u64 bytenr,u64 num_bytes,int alloc); -static int update_reserved_bytes (struct btrfs_block_group_cache *cache, -u64 num_bytes,int reserve,int sinfo); static int __btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root *root, u64 bytenr, u64 num_bytes, u64 parent,
@@ -4223,8 +4221,8 @@ int btrfs_pin_extent(struct btrfs_root *root,* update size of reserved extents. this function may return -EAGAIN * if 'reserve' is true or 'sinfo' is false. */ -static int update_reserved_bytes (struct btrfs_block_group_cache *cache, -u64 num_bytes,int reserve,int sinfo) +int btrfs_update_reserved_bytes (struct btrfs_block_group_cache *cache, +u64 num_bytes,int reserve,int sinfo) {int ret =0 ;if (sinfo) {
@@ -4704,10 +4702,10 @@ void btrfs_free_tree_block(struct btrfs_trans_handle *trans,WARN_ON (test_bit (EXTENT_BUFFER_DIRTY , &buf->bflags));btrfs_add_free_space (cache, buf->start, buf->len); - ret =update_reserved_bytes (cache, buf->len,0 ,0 ); + ret =btrfs_update_reserved_bytes (cache, buf->len,0 ,0 );if (ret == -EAGAIN ) {/* block group became read-only */ -update_reserved_bytes (cache, buf->len,0 ,1 ); +btrfs_update_reserved_bytes (cache, buf->len,0 ,1 ); goto out; }
@@ -5191,7 +5189,7 @@ checks: search_start - offset);BUG_ON (offset > search_start); - ret =update_reserved_bytes (block_group, num_bytes,1 , + ret =btrfs_update_reserved_bytes (block_group, num_bytes,1 , (data &BTRFS_BLOCK_GROUP_DATA ));if (ret == -EAGAIN ) {btrfs_add_free_space (block_group, offset, num_bytes);
@@ -5415,7 +5413,7 @@ int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len) ret =btrfs_discard_extent (root, start, len);btrfs_add_free_space (cache, start, len); -update_reserved_bytes (cache, len,0 ,1 ); +btrfs_update_reserved_bytes (cache, len,0 ,1 );btrfs_put_block_group (cache);return ret;
@@ -5614,7 +5612,7 @@ int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,put_caching_control (caching_ctl); } - ret =update_reserved_bytes (block_group, ins->offset,1 ,1 ); + ret =btrfs_update_reserved_bytes (block_group, ins->offset,1 ,1 );BUG_ON (ret);btrfs_put_block_group (block_group); ret = alloc_reserved_file_extent(trans, root, 0, root_objectid,
Signature · 1 line
1.7.4.1
PATCH v4 2/4 Btrfs: make btrfs_map_block() return entire free extent for each device of RAID0/1/10/DUP
In reply to Li Dongyangbtrfs_map_block() will only return a single stripe length, but we want the full extent be mapped to each disk when we are trimming the extent,
so we add length to btrfs_bio_stripe and fill it if we are mapping for REQ_DISCARD.
--- fs/btrfs/volumes.c | 150 ++++++++++++++++++++++++++++++++++++++++++++-------- fs/btrfs/volumes.h | 1 + 2 files changed, 129 insertions(+), 22 deletions(-)
fs/btrfs/volumes.c +128 -22
@@ -2962,7 +2962,10 @@ static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,struct extent_map_tree *em_tree = &map_tree->map_tree;u64 offset;u64 stripe_offset; +u64 stripe_end_offset;u64 stripe_nr; +u64 stripe_nr_orig; +u64 stripe_nr_end;int stripes_allocated =8 ;int stripes_required =1 ;int stripe_index;
@@ -2971,7 +2974,7 @@ static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,int max_errors =0 ;struct btrfs_multi_bio *multi =NULL ; -if (multi_ret && !(rw &REQ_WRITE )) +if (multi_ret && !(rw & (REQ_WRITE |REQ_DISCARD ))) stripes_allocated =1 ; again:if (multi_ret) {
@@ -3017,7 +3020,15 @@ again: max_errors =1 ; } } -if (multi_ret && (rw &REQ_WRITE ) && +if (rw &REQ_DISCARD ) { +if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | +BTRFS_BLOCK_GROUP_RAID1 | +BTRFS_BLOCK_GROUP_DUP | +BTRFS_BLOCK_GROUP_RAID10 )) { + stripes_required = map->num_stripes; + } + } +if (multi_ret && (rw & (REQ_WRITE |REQ_DISCARD )) && stripes_allocated < stripes_required) { stripes_allocated = map->num_stripes;free_extent_map (em);
@@ -3037,12 +3048,15 @@ again:/* stripe_offset is the offset of this block in its stripe*/ stripe_offset = offset - stripe_offset; -if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |BTRFS_BLOCK_GROUP_RAID1 | -BTRFS_BLOCK_GROUP_RAID10 | -BTRFS_BLOCK_GROUP_DUP )) { +if (rw &REQ_DISCARD ) + *length =min_t (u64, em->len - offset, *length); +else if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | +BTRFS_BLOCK_GROUP_RAID1 | +BTRFS_BLOCK_GROUP_RAID10 | +BTRFS_BLOCK_GROUP_DUP )) {/* we limit the length of each bio to what fits in a stripe */ *length =min_t (u64, em->len - offset, - map->stripe_len - stripe_offset); + map->stripe_len - stripe_offset); }else { *length = em->len - offset; }
@@ -3052,8 +3066,19 @@ again: num_stripes =1 ; stripe_index =0 ; -if (map->type &BTRFS_BLOCK_GROUP_RAID1 ) { -if (unplug_page || (rw &REQ_WRITE )) + stripe_nr_orig = stripe_nr; + stripe_nr_end = (offset + *length + map->stripe_len -1 ) & + (~(map->stripe_len -1 )); +do_div (stripe_nr_end, map->stripe_len); + stripe_end_offset = stripe_nr_end * map->stripe_len - + (offset + *length); +if (map->type &BTRFS_BLOCK_GROUP_RAID0 ) { +if (rw &REQ_DISCARD ) + num_stripes =min_t (u64, map->num_stripes, + stripe_nr_end - stripe_nr_orig); + stripe_index =do_div (stripe_nr, map->num_stripes); + }else if (map->type &BTRFS_BLOCK_GROUP_RAID1 ) { +if (unplug_page || (rw & (REQ_WRITE |REQ_DISCARD ))) num_stripes = map->num_stripes;else if (mirror_num) stripe_index = mirror_num -1 ;
@@ -3064,7 +3089,7 @@ again: } }else if (map->type &BTRFS_BLOCK_GROUP_DUP ) { -if (rw &REQ_WRITE ) +if (rw & (REQ_WRITE |REQ_DISCARD )) num_stripes = map->num_stripes;else if (mirror_num) stripe_index = mirror_num -1 ;
@@ -3077,6 +3102,10 @@ again:if (unplug_page || (rw &REQ_WRITE )) num_stripes = map->sub_stripes; +else if (rw &REQ_DISCARD ) + num_stripes =min_t (u64, map->sub_stripes * + (stripe_nr_end - stripe_nr_orig), + map->num_stripes);else if (mirror_num) stripe_index += mirror_num -1 ;else {
@@ -3094,24 +3123,101 @@ again: }BUG_ON (stripe_index >= map->num_stripes); -for (i =0 ; i < num_stripes; i++) { -if (unplug_page) { -struct btrfs_device *device; -struct backing_dev_info *bdi; - - device = map->stripes[stripe_index].dev; -if (device->bdev) { - bdi =blk_get_backing_dev_info (device->bdev); -if (bdi->unplug_io_fn) - bdi->unplug_io_fn (bdi, unplug_page); - } - }else { +if (rw &REQ_DISCARD ) { +for (i =0 ; i < num_stripes; i++) { multi->stripes[i].physical = map->stripes[stripe_index].physical + stripe_offset + stripe_nr * map->stripe_len; multi->stripes[i].dev = map->stripes[stripe_index].dev; + +if (map->type &BTRFS_BLOCK_GROUP_RAID0 ) { +u64 stripes; +int last_stripe = (stripe_nr_end -1 ) % + map->num_stripes; +int j; + +for (j =0 ; j < map->num_stripes; j++) { +if ((stripe_nr_end -1 - j) % + map->num_stripes == stripe_index) +break ; + } + stripes = stripe_nr_end -1 - j; +do_div (stripes, map->num_stripes); + multi->stripes[i].length = map->stripe_len * + (stripes - stripe_nr +1 ); + +if (i ==0 ) { + multi->stripes[i].length -= + stripe_offset; + stripe_offset =0 ; + } +if (stripe_index == last_stripe) + multi->stripes[i].length -= + stripe_end_offset; + }else if (map->type &BTRFS_BLOCK_GROUP_RAID10 ) { +u64 stripes; +int j; +int factor = map->num_stripes / + map->sub_stripes; +int last_stripe = (stripe_nr_end -1 ) % factor; + last_stripe *= map->sub_stripes; + +for (j =0 ; j < factor; j++) { +if ((stripe_nr_end -1 - j) % factor == + stripe_index / map->sub_stripes) +break ; + } + stripes = stripe_nr_end -1 - j; +do_div (stripes, factor); + multi->stripes[i].length = map->stripe_len * + (stripes - stripe_nr +1 ); + +if (i < map->sub_stripes) { + multi->stripes[i].length -= + stripe_offset; +if (i == map->sub_stripes -1 ) + stripe_offset =0 ; + } +if (stripe_index >= last_stripe && + stripe_index <= (last_stripe + + map->sub_stripes -1 )) { + multi->stripes[i].length -= + stripe_end_offset; + } + }else + multi->stripes[i].length = *length; + + stripe_index++; +if (stripe_index == map->num_stripes) { +/* This could only happen for RAID0/10 */ + stripe_index =0 ; + stripe_nr++; + } + } + }else { +for (i =0 ; i < num_stripes; i++) { +if (unplug_page) { +struct btrfs_device *device; +struct backing_dev_info *bdi; + + device = map->stripes[stripe_index].dev; +if (device->bdev) { + bdi =blk_get_backing_dev_info (device-> + bdev); +if (bdi->unplug_io_fn) + bdi->unplug_io_fn (bdi, + unplug_page); + } + }else { + multi->stripes[i].physical = + map->stripes[stripe_index].physical + + stripe_offset + + stripe_nr * map->stripe_len; + multi->stripes[i].dev = + map->stripes[stripe_index].dev; + } + stripe_index++; } - stripe_index++; }if (multi_ret) { *multi_ret = multi;
fs/btrfs/volumes.h +1 -0
@@ -126,6 +126,7 @@ struct btrfs_fs_devices {struct btrfs_bio_stripe {struct btrfs_device *dev;u64 physical; +u64 length;/* only used for discard mappings */ };struct btrfs_multi_bio {
Signature · 1 line
1.7.4.1
PATCH v4 3/4 Btrfs: adjust btrfs_discard_extent() return errors and trimmed bytes
In reply to Li DongyangCallers of btrfs_discard_extent() should check if we are mounted with -o discard, as we want to make fitrim to work even the fs is not mounted with -o discard. Also we should use REQ_DISCARD to map the free extent to get a full mapping, last we only return errors if
1. the error is not a EOPNOTSUPP
2. no device supports discard
--- fs/btrfs/ctree.h | 2 +- fs/btrfs/disk-io.c | 5 ++++- fs/btrfs/extent-tree.c | 45 ++++++++++++++++++++++++++------------------- 3 files changed, 31 insertions(+), 21 deletions(-)
fs/btrfs/ctree.h +1 -1
@@ -2229,7 +2229,7 @@ u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo);int btrfs_error_unpin_extent_range (struct btrfs_root *root,u64 start,u64 end);int btrfs_error_discard_extent (struct btrfs_root *root,u64 bytenr, -u64 num_bytes); +u64 num_bytes,u64 *actual_bytes);int btrfs_force_chunk_alloc (struct btrfs_trans_handle *trans,struct btrfs_root *root,u64 type);
fs/btrfs/disk-io.c +4 -1
@@ -2947,7 +2947,10 @@ static int btrfs_destroy_pinned_extent(struct btrfs_root *root,break ;/* opt_discard */ - ret =btrfs_error_discard_extent (root, start, end +1 - start); +if (btrfs_test_opt (root,DISCARD )) + ret =btrfs_error_discard_extent (root, start, + end +1 - start, +NULL );clear_extent_dirty (unpin, start, end,GFP_NOFS );btrfs_error_unpin_extent_range (root, start, end);
fs/btrfs/extent-tree.c +26 -19
87 lines not on this page · raw message
Signature · 1 line
1.7.4.1
PATCH v4 4/4 Btrfs: add btrfs_trim_fs() to handle FITRIM
In reply to Li DongyangWe take an free extent out from allocator, trim it, then put it back, but before we trim the block group, we should make sure the block group is cached, so plus a little change to make cache_block_group() run without a transaction.
--- fs/btrfs/ctree.h | 1 + fs/btrfs/extent-tree.c | 50 +++++++++++++++++++++++- fs/btrfs/free-space-cache.c | 92 +++++++++++++++++++++++++++++++++++++++++++ fs/btrfs/free-space-cache.h | 2 + fs/btrfs/ioctl.c | 46 +++++++++++++++++++++ 5 files changed, 190 insertions(+), 1 deletions(-)
fs/btrfs/ctree.h +1 -0
@@ -2232,6 +2232,7 @@ int btrfs_error_discard_extent(struct btrfs_root *root, u64 bytenr, u64 num_bytes, u64 *actual_bytes);int btrfs_force_chunk_alloc (struct btrfs_trans_handle *trans,struct btrfs_root *root,u64 type); +int btrfs_trim_fs (struct btrfs_root *root,struct fstrim_range *range);/* ctree.c */ int btrfs_bin_search(struct extent_buffer *eb, struct btrfs_key *key,
fs/btrfs/extent-tree.c +49 -1
59 lines not on this page · raw message
fs/btrfs/free-space-cache.c +92 -0
95 lines not on this page · raw message
fs/btrfs/free-space-cache.h +2 -0
@@ -68,4 +68,6 @@ u64 btrfs_alloc_from_cluster(struct btrfs_block_group_cache *block_group,int btrfs_return_cluster_to_free_space (struct btrfs_block_group_cache *block_group,struct btrfs_free_cluster *cluster); +int btrfs_trim_block_group (struct btrfs_block_group_cache *block_group, +u64 *trimmed,u64 start,u64 end,u64 minlen);#endif
fs/btrfs/ioctl.c +46 -0
64 lines not on this page · raw message
Signature · 1 line
1.7.4.1
Excerpts from Li Dongyang's message of 2011-03-24 06:24:24 -0400:
Quoted 4 lines
Dear list,
This is V4 of batched discard support, now we will get full mapping of the free space on each device for RAID0/1/10/DUP instead of just a single stripe length, and tested with xfsstests 251, Thanks.
I've pushed this out into the for-linus branch, along with a full merge to 2.6.39 current git.
Please take a look and make sure I've merged it correctly.
Thanks!
-chris
Quoted 17 lines
Changelog V4:
*makebtrfs_map_block()return full mapping.
Changelog V3:
*fix style problems.
*rebase to 2.6.38-rc7.
Changelog V2:
*Check if we have devices support trim before trying to trim the fs, also adjust
minlen according to thediscard_granularity.
*Update reserved extent calculations inbtrfs_trim_block_group().
*Callcond_resched()without checkingneed_resched()
*Usebitmap_clear_bits()andunlink_free_space()instead ofbtrfs_remove_free_space(),
so we won't search the same extent for twice.
*Try harder inbtrfs_discard_extent(), now we won't report errors
if it's not a EOPNOTSUPP.
*make sure the block group is cached before trimming it,or we'll see an empty caching
tree if the block group is not cached.
*Minor return value fix inbtrfs_discard_block_group().
Excerpts from Chris Mason's message of 2011-03-27 14:10:46 -0400:
Quoted 10 lines
Excerpts from Li Dongyang's message of 2011-03-24 06:24:24 -0400:
Quoted 4 lines
Dear list,
This is V4 of batched discard support, now we will get full mapping of the free space on each device for RAID0/1/10/DUP instead of just a single stripe length, and tested with xfsstests 251, Thanks.I've pushed this out into the for-linus branch, along with a full merge to 2.6.39 current git.
Please take a look and make sure I've merged it correctly.
Hmmm, this was doing mod operations on 64 bit numbers, so it didn't compile at all on 32 bit machines. I've fixed it up and pushed the result out to for-linus. Please check the math ;)
-chris
Quoted 22 lines
Thanks!
-chris
Quoted 17 lines
Changelog V4:
*makebtrfs_map_block()return full mapping.
Changelog V3:
*fix style problems.
*rebase to 2.6.38-rc7.
Changelog V2:
*Check if we have devices support trim before trying to trim the fs, also adjust
minlen according to thediscard_granularity.
*Update reserved extent calculations inbtrfs_trim_block_group().
*Callcond_resched()without checkingneed_resched()
*Usebitmap_clear_bits()andunlink_free_space()instead ofbtrfs_remove_free_space(),
so we won't search the same extent for twice.
*Try harder inbtrfs_discard_extent(), now we won't report errors
if it's not a EOPNOTSUPP.
*make sure the block group is cached before trimming it,or we'll see an empty caching
tree if the block group is not cached.
*Minor return value fix inbtrfs_discard_block_group().
Excerpts from Chris Mason's message of 2011-03-27 21:30:20 -0400:
Quoted 7 lines
Excerpts from Chris Mason's message of 2011-03-27 14:10:46 -0400:
Quoted 6 lines
Excerpts from Li Dongyang's message of 2011-03-24 06:24:24 -0400:
Quoted 4 lines
Dear list,
This is V4 of batched discard support, now we will get full mapping of the free space on each device for RAID0/1/10/DUP instead of just a single stripe length, and tested with xfsstests 251, Thanks.
Quoted 8 lines
Quoted 4 lines
I've pushed this out into the for-linus branch, along with a full merge to 2.6.39 current git.
Please take a look and make sure I've merged it correctly.
Hmmm, this was doing mod operations on 64 bit numbers, so it didn't compile at all on 32 bit machines. I've fixed it up and pushed the result out to for-linus. Please check the math ;)
BTW, I just rebased this so the incremental fix was before merging into Linus' tree.
-chris
Quoted 25 lines
-chris
Quoted 22 lines
Thanks!
-chris
Quoted 17 lines
Changelog V4:
*makebtrfs_map_block()return full mapping.
Changelog V3:
*fix style problems.
*rebase to 2.6.38-rc7.
Changelog V2:
*Check if we have devices support trim before trying to trim the fs, also adjust
minlen according to thediscard_granularity.
*Update reserved extent calculations inbtrfs_trim_block_group().
*Callcond_resched()without checkingneed_resched()
*Usebitmap_clear_bits()andunlink_free_space()instead ofbtrfs_remove_free_space(),
so we won't search the same extent for twice.
*Try harder inbtrfs_discard_extent(), now we won't report errors
if it's not a EOPNOTSUPP.
*make sure the block group is cached before trimming it,or we'll see an empty caching
tree if the block group is not cached.
*Minor return value fix inbtrfs_discard_block_group().
Quoted 12 lines · Chris Mason
Excerpts from Chris Mason's message of 2011-03-27 21:30:20 -0400:
Quoted 11 lines
Excerpts from Chris Mason's message of 2011-03-27 14:10:46 -0400:
Quoted 10 lines
Excerpts from Li Dongyang's message of 2011-03-24 06:24:24 -0400:
Quoted 4 lines
Dear list,
This is V4 of batched discard support, now we will get full mapping of the free space on each device for RAID0/1/10/DUP instead of just a single stripe length, and tested with xfsstests 251, Thanks.I've pushed this out into the for-linus branch, along with a full merge to 2.6.39 current git.
Please take a look and make sure I've merged it correctly.
Looks good to me.
Quoted 5 lines
Quoted 4 lines
Hmmm, this was doing mod operations on 64 bit numbers, so it didn't compile at all on 32 bit machines. I've fixed it up and pushed the result out to for-linus. Please check the math ;)
sorry for being so stupid, thanks for fixing ;-)
Br,
Li Dongyang
Quoted 31 lines
BTW, I just rebased this so the incremental fix was before merging into Linus' tree.
-chris
Quoted 25 lines
-chris
Quoted 22 lines
Thanks!
-chris
Quoted 17 lines
Changelog V4:
*makebtrfs_map_block()return full mapping.
Changelog V3:
*fix style problems.
*rebase to 2.6.38-rc7.
Changelog V2:
*Check if we have devices support trim before trying to trim the fs, also adjust
minlen according to thediscard_granularity.
*Update reserved extent calculations inbtrfs_trim_block_group().
*Callcond_resched()without checkingneed_resched()
*Usebitmap_clear_bits()andunlink_free_space()instead ofbtrfs_remove_free_space(),
so we won't search the same extent for twice.
*Try harder inbtrfs_discard_extent(), now we won't report errors
if it's not a EOPNOTSUPP.
*make sure the block group is cached before trimming it,or we'll see an empty caching
tree if the block group is not cached.
*Minor return value fix inbtrfs_discard_block_group().