Btrfs: fix btrfs_remove_from_free_space corner case

Yan Zheng hit a problem where we tried to remove some free space but failed
because we couldn't find the free space entry. This is because the free space
was held within a bitmap that had a starting offset well before the actual
offset of the free space, and there were free space extents that were in the
same range as that offset, so tree_search_offset returned with NULL because we
couldn't find a free space extent that had that offset. This is fixed by
making sure that if we fail to find the entry, we re-search again with
bitmap_only set to 1 and do an offset_to_bitmap so we can get the appropriate
bitmap. A similar problem happens in btrfs_alloc_from_bitmap for the
clustering code, but that is not as bad since we will just go and redo our
cluster allocation.

Also this adds some debugging checks to make sure that the free space we are
trying to remove from the bitmap is in fact there. This can probably go away
after a while, but since this code is only used by the tree-logging stuff it
would be nice to run with it for a while to make sure there are no problems.

Signed-off-by: Josef Bacik <jbacik@redhat.com>
Signed-off-by: Chris Mason <chris.mason@oracle.com>

authored by Josef Bacik and committed by Chris Mason 6606bb97 f36f3042

+64 -9
+64 -9
fs/btrfs/free-space-cache.c
··· 414 414 u64 *offset, u64 *bytes) 415 415 { 416 416 u64 end; 417 + u64 search_start, search_bytes; 418 + int ret; 417 419 418 420 again: 419 421 end = bitmap_info->offset + 420 422 (u64)(BITS_PER_BITMAP * block_group->sectorsize) - 1; 423 + 424 + /* 425 + * XXX - this can go away after a few releases. 426 + * 427 + * since the only user of btrfs_remove_free_space is the tree logging 428 + * stuff, and the only way to test that is under crash conditions, we 429 + * want to have this debug stuff here just in case somethings not 430 + * working. Search the bitmap for the space we are trying to use to 431 + * make sure its actually there. If its not there then we need to stop 432 + * because something has gone wrong. 433 + */ 434 + search_start = *offset; 435 + search_bytes = *bytes; 436 + ret = search_bitmap(block_group, bitmap_info, &search_start, 437 + &search_bytes); 438 + BUG_ON(ret < 0 || search_start != *offset); 421 439 422 440 if (*offset > bitmap_info->offset && *offset + *bytes > end) { 423 441 bitmap_clear_bits(block_group, bitmap_info, *offset, ··· 448 430 } 449 431 450 432 if (*bytes) { 433 + struct rb_node *next = rb_next(&bitmap_info->offset_index); 451 434 if (!bitmap_info->bytes) { 452 435 unlink_free_space(block_group, bitmap_info); 453 436 kfree(bitmap_info->bitmap); ··· 457 438 recalculate_thresholds(block_group); 458 439 } 459 440 460 - bitmap_info = tree_search_offset(block_group, 461 - offset_to_bitmap(block_group, 462 - *offset), 463 - 1, 0); 464 - if (!bitmap_info) 441 + /* 442 + * no entry after this bitmap, but we still have bytes to 443 + * remove, so something has gone wrong. 444 + */ 445 + if (!next) 465 446 return -EINVAL; 466 447 448 + bitmap_info = rb_entry(next, struct btrfs_free_space, 449 + offset_index); 450 + 451 + /* 452 + * if the next entry isn't a bitmap we need to return to let the 453 + * extent stuff do its work. 454 + */ 467 455 if (!bitmap_info->bitmap) 456 + return -EAGAIN; 457 + 458 + /* 459 + * Ok the next item is a bitmap, but it may not actually hold 460 + * the information for the rest of this free space stuff, so 461 + * look for it, and if we don't find it return so we can try 462 + * everything over again. 463 + */ 464 + search_start = *offset; 465 + search_bytes = *bytes; 466 + ret = search_bitmap(block_group, bitmap_info, &search_start, 467 + &search_bytes); 468 + if (ret < 0 || search_start != *offset) 468 469 return -EAGAIN; 469 470 470 471 goto again; ··· 683 644 again: 684 645 info = tree_search_offset(block_group, offset, 0, 0); 685 646 if (!info) { 686 - WARN_ON(1); 687 - goto out_lock; 647 + /* 648 + * oops didn't find an extent that matched the space we wanted 649 + * to remove, look for a bitmap instead 650 + */ 651 + info = tree_search_offset(block_group, 652 + offset_to_bitmap(block_group, offset), 653 + 1, 0); 654 + if (!info) { 655 + WARN_ON(1); 656 + goto out_lock; 657 + } 688 658 } 689 659 690 660 if (info->bytes < bytes && rb_next(&info->offset_index)) { ··· 1005 957 if (cluster->block_group != block_group) 1006 958 goto out; 1007 959 1008 - entry = tree_search_offset(block_group, search_start, 0, 0); 1009 - 960 + /* 961 + * search_start is the beginning of the bitmap, but at some point it may 962 + * be a good idea to point to the actual start of the free area in the 963 + * bitmap, so do the offset_to_bitmap trick anyway, and set bitmap_only 964 + * to 1 to make sure we get the bitmap entry 965 + */ 966 + entry = tree_search_offset(block_group, 967 + offset_to_bitmap(block_group, search_start), 968 + 1, 0); 1010 969 if (!entry || !entry->bitmap) 1011 970 goto out; 1012 971