Skip to content

Commit 1b98450

Browse files
forrest1209masoncl
authored andcommitted
Btrfs: fix find_free_dev_extent() malfunction in case device tree has hole
If device tree has hole, find_free_dev_extent() cannot find available address properly. The problem can be reproduce by following script. mntpath=/btrfs loopdev=/dev/loop0 filepath=/home/forrest/image umount $mntpath losetup -d $loopdev truncate --size 100g $filepath losetup $loopdev $filepath mkfs.btrfs -f $loopdev mount $loopdev $mntpath # make device tree with one big hole for i in `seq 1 1 100`; do fallocate -l 1g $mntpath/$i done sync for i in `seq 1 1 95`; do rm $mntpath/$i done sync # wait cleaner thread remove unused block group sleep 300 fallocate -l 1g $mntpath/aaa # failed to allocate new chunk fallocate -l 1g $mntpath/bbb Above script will make device tree with one big hole, and can only allocate just one chunk in a transaction, so failed to allocate new chunk for $mntpath/bbb item 8 key (1 DEV_EXTENT 2185232384) itemoff 15859 itemsize 48 dev extent chunk_tree 3 chunk objectid 256 chunk offset 106292051968 length 1073741824 item 9 key (1 DEV_EXTENT 104190705664) itemoff 15811 itemsize 48 dev extent chunk_tree 3 chunk objectid 256 chunk offset 103108575232 length 1073741824 Signed-off-by: Forrest Liu <forrestl@synology.com> Reviewed-by: Liu Bo <bo.li.liu@oracle.com> Signed-off-by: Chris Mason <clm@fb.com>
1 parent e4c88f0 commit 1b98450

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

fs/btrfs/volumes.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,6 +1058,7 @@ static int contains_pending_extent(struct btrfs_trans_handle *trans,
10581058
struct extent_map *em;
10591059
struct list_head *search_list = &trans->transaction->pending_chunks;
10601060
int ret = 0;
1061+
u64 physical_start = *start;
10611062

10621063
again:
10631064
list_for_each_entry(em, search_list, list) {
@@ -1068,9 +1069,9 @@ static int contains_pending_extent(struct btrfs_trans_handle *trans,
10681069
for (i = 0; i < map->num_stripes; i++) {
10691070
if (map->stripes[i].dev != device)
10701071
continue;
1071-
if (map->stripes[i].physical >= *start + len ||
1072+
if (map->stripes[i].physical >= physical_start + len ||
10721073
map->stripes[i].physical + em->orig_block_len <=
1073-
*start)
1074+
physical_start)
10741075
continue;
10751076
*start = map->stripes[i].physical +
10761077
em->orig_block_len;
@@ -1193,8 +1194,14 @@ int find_free_dev_extent(struct btrfs_trans_handle *trans,
11931194
*/
11941195
if (contains_pending_extent(trans, device,
11951196
&search_start,
1196-
hole_size))
1197-
hole_size = 0;
1197+
hole_size)) {
1198+
if (key.offset >= search_start) {
1199+
hole_size = key.offset - search_start;
1200+
} else {
1201+
WARN_ON_ONCE(1);
1202+
hole_size = 0;
1203+
}
1204+
}
11981205

11991206
if (hole_size > max_hole_size) {
12001207
max_hole_start = search_start;

0 commit comments

Comments
 (0)