Skip to content

Commit

Permalink
Btrfs: fix a bug of balance on full multi-disk partitions
Browse files Browse the repository at this point in the history
When balancing, we'll first try to shrink devices for some space,
but if it is working on a full multi-disk partition with raid protection,
we may encounter a bug, that is, while shrinking, total_bytes may be less
than bytes_used, and btrfs may allocate a dev extent that accesses out of
device's bounds.

Then we will not be able to write or read the data which stores at the end
of the device, and get the followings:

device fsid 0939f071-7ea3-46c8-95df-f176d773bfb6 devid 1 transid 10 /dev/sdb5
Btrfs detected SSD devices, enabling SSD mode
btrfs: relocating block group 476315648 flags 9
btrfs: found 4 extents
attempt to access beyond end of device
sdb5: rw=145, want=546176, limit=546147
attempt to access beyond end of device
sdb5: rw=145, want=546304, limit=546147
attempt to access beyond end of device
sdb5: rw=145, want=546432, limit=546147
attempt to access beyond end of device
sdb5: rw=145, want=546560, limit=546147
attempt to access beyond end of device

Signed-off-by: Liu Bo <[email protected]>
Signed-off-by: Chris Mason <[email protected]>
  • Loading branch information
liub authored and chrismason-xx committed Aug 17, 2011
1 parent 34f3e4f commit 38c01b9
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions fs/btrfs/volumes.c
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,7 @@ int find_free_dev_extent(struct btrfs_trans_handle *trans,

max_hole_start = search_start;
max_hole_size = 0;
hole_size = 0;

if (search_start >= search_end) {
ret = -ENOSPC;
Expand Down Expand Up @@ -945,7 +946,14 @@ int find_free_dev_extent(struct btrfs_trans_handle *trans,
cond_resched();
}

hole_size = search_end- search_start;
/*
* At this point, search_start should be the end of
* allocated dev extents, and when shrinking the device,
* search_end may be smaller than search_start.
*/
if (search_end > search_start)
hole_size = search_end - search_start;

if (hole_size > max_hole_size) {
max_hole_start = search_start;
max_hole_size = hole_size;
Expand Down Expand Up @@ -2447,9 +2455,10 @@ static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
total_avail = device->total_bytes - device->bytes_used;
else
total_avail = 0;
/* avail is off by max(alloc_start, 1MB), but that is the same
* for all devices, so it doesn't hurt the sorting later on
*/

/* If there is no space on this device, skip it. */
if (total_avail == 0)
continue;

ret = find_free_dev_extent(trans, device,
max_stripe_size * dev_stripes,
Expand Down

0 comments on commit 38c01b9

Please sign in to comment.