Skip to content

Commit b89203f

Browse files
liubchrismason-xx
authored andcommitted
Btrfs: fix eof while discarding extents
We miscalculate the length of extents we're discarding, and it leads to an eof of device. Reported-by: Daniel Blueman <[email protected]> Signed-off-by: Liu Bo <[email protected]> Signed-off-by: Chris Mason <[email protected]>
1 parent d95603b commit b89203f

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

fs/btrfs/volumes.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3833,6 +3833,7 @@ static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
38333833
int sub_stripes = 0;
38343834
u64 stripes_per_dev = 0;
38353835
u32 remaining_stripes = 0;
3836+
u32 last_stripe = 0;
38363837

38373838
if (map->type &
38383839
(BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID10)) {
@@ -3846,6 +3847,8 @@ static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
38463847
stripe_nr_orig,
38473848
factor,
38483849
&remaining_stripes);
3850+
div_u64_rem(stripe_nr_end - 1, factor, &last_stripe);
3851+
last_stripe *= sub_stripes;
38493852
}
38503853

38513854
for (i = 0; i < num_stripes; i++) {
@@ -3858,16 +3861,29 @@ static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
38583861
BTRFS_BLOCK_GROUP_RAID10)) {
38593862
bbio->stripes[i].length = stripes_per_dev *
38603863
map->stripe_len;
3864+
38613865
if (i / sub_stripes < remaining_stripes)
38623866
bbio->stripes[i].length +=
38633867
map->stripe_len;
3868+
3869+
/*
3870+
* Special for the first stripe and
3871+
* the last stripe:
3872+
*
3873+
* |-------|...|-------|
3874+
* |----------|
3875+
* off end_off
3876+
*/
38643877
if (i < sub_stripes)
38653878
bbio->stripes[i].length -=
38663879
stripe_offset;
3867-
if ((i / sub_stripes + 1) %
3868-
sub_stripes == remaining_stripes)
3880+
3881+
if (stripe_index >= last_stripe &&
3882+
stripe_index <= (last_stripe +
3883+
sub_stripes - 1))
38693884
bbio->stripes[i].length -=
38703885
stripe_end_offset;
3886+
38713887
if (i == sub_stripes - 1)
38723888
stripe_offset = 0;
38733889
} else

0 commit comments

Comments
 (0)