Commit db804f23 authored by Li Zefan's avatar Li Zefan

Btrfs: add pinned extents to on-disk free space cache correctly

I got this while running xfstests:

[24256.836098] block group 317849600 has an wrong amount of free space
[24256.836100] btrfs: failed to load free space cache for block group 317849600

We should clamp the extent returned by find_first_extent_bit(),
so the start of the extent won't smaller than the start of the
block group.
Signed-off-by: default avatarLi Zefan <lizf@cn.fujitsu.com>
parent d25223a0
...@@ -838,7 +838,7 @@ int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode, ...@@ -838,7 +838,7 @@ int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode,
struct io_ctl io_ctl; struct io_ctl io_ctl;
struct list_head bitmap_list; struct list_head bitmap_list;
struct btrfs_key key; struct btrfs_key key;
u64 start, end, len; u64 start, extent_start, extent_end, len;
int entries = 0; int entries = 0;
int bitmaps = 0; int bitmaps = 0;
int ret; int ret;
...@@ -857,25 +857,12 @@ int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode, ...@@ -857,25 +857,12 @@ int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode,
struct btrfs_free_cluster, struct btrfs_free_cluster,
block_group_list); block_group_list);
/*
* We shouldn't have switched the pinned extents yet so this is the
* right one
*/
unpin = root->fs_info->pinned_extents;
/* Lock all pages first so we can lock the extent safely. */ /* Lock all pages first so we can lock the extent safely. */
io_ctl_prepare_pages(&io_ctl, inode, 0); io_ctl_prepare_pages(&io_ctl, inode, 0);
lock_extent_bits(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1, lock_extent_bits(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1,
0, &cached_state, GFP_NOFS); 0, &cached_state, GFP_NOFS);
/*
* When searching for pinned extents, we need to start at our start
* offset.
*/
if (block_group)
start = block_group->key.objectid;
node = rb_first(&ctl->free_space_offset); node = rb_first(&ctl->free_space_offset);
if (!node && cluster) { if (!node && cluster) {
node = rb_first(&cluster->root); node = rb_first(&cluster->root);
...@@ -918,9 +905,20 @@ int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode, ...@@ -918,9 +905,20 @@ int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode,
* We want to add any pinned extents to our free space cache * We want to add any pinned extents to our free space cache
* so we don't leak the space * so we don't leak the space
*/ */
/*
* We shouldn't have switched the pinned extents yet so this is the
* right one
*/
unpin = root->fs_info->pinned_extents;
if (block_group)
start = block_group->key.objectid;
while (block_group && (start < block_group->key.objectid + while (block_group && (start < block_group->key.objectid +
block_group->key.offset)) { block_group->key.offset)) {
ret = find_first_extent_bit(unpin, start, &start, &end, ret = find_first_extent_bit(unpin, start,
&extent_start, &extent_end,
EXTENT_DIRTY); EXTENT_DIRTY);
if (ret) { if (ret) {
ret = 0; ret = 0;
...@@ -928,20 +926,21 @@ int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode, ...@@ -928,20 +926,21 @@ int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode,
} }
/* This pinned extent is out of our range */ /* This pinned extent is out of our range */
if (start >= block_group->key.objectid + if (extent_start >= block_group->key.objectid +
block_group->key.offset) block_group->key.offset)
break; break;
len = block_group->key.objectid + extent_start = max(extent_start, start);
block_group->key.offset - start; extent_end = min(block_group->key.objectid +
len = min(len, end + 1 - start); block_group->key.offset, extent_end + 1);
len = extent_end - extent_start;
entries++; entries++;
ret = io_ctl_add_entry(&io_ctl, start, len, NULL); ret = io_ctl_add_entry(&io_ctl, extent_start, len, NULL);
if (ret) if (ret)
goto out_nospc; goto out_nospc;
start = end + 1; start = extent_end;
} }
/* Write out the bitmaps */ /* Write out the bitmaps */
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment