Btrfs: Properly handle overlapping extent in shrink_extent_tree
The patch fixes the overlapping extent issue in shrink_extent_tree. It checks whether there is an overlapping extent by using find_previous_extent. If there is an overlapping extent, it setups key.objectid and cur_byte properly. --- Signed-off-by: Chris Mason <chris.mason@oracle.com>
This commit is contained in:
parent
d548ee5182
commit
73e48b277a
|
@ -2317,36 +2317,6 @@ out:
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int find_overlapping_extent(struct btrfs_root *root,
|
|
||||||
struct btrfs_path *path, u64 new_size)
|
|
||||||
{
|
|
||||||
struct btrfs_key found_key;
|
|
||||||
struct extent_buffer *leaf;
|
|
||||||
int ret;
|
|
||||||
|
|
||||||
while(1) {
|
|
||||||
if (path->slots[0] == 0) {
|
|
||||||
ret = btrfs_prev_leaf(root, path);
|
|
||||||
if (ret == 1) {
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
if (ret < 0)
|
|
||||||
return ret;
|
|
||||||
} else {
|
|
||||||
path->slots[0]--;
|
|
||||||
}
|
|
||||||
leaf = path->nodes[0];
|
|
||||||
btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
|
|
||||||
if (found_key.type == BTRFS_EXTENT_ITEM_KEY) {
|
|
||||||
if (found_key.objectid + found_key.offset > new_size)
|
|
||||||
return 0;
|
|
||||||
else
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
int btrfs_shrink_extent_tree(struct btrfs_root *root, u64 new_size)
|
int btrfs_shrink_extent_tree(struct btrfs_root *root, u64 new_size)
|
||||||
{
|
{
|
||||||
struct btrfs_trans_handle *trans;
|
struct btrfs_trans_handle *trans;
|
||||||
|
@ -2357,11 +2327,10 @@ int btrfs_shrink_extent_tree(struct btrfs_root *root, u64 new_size)
|
||||||
struct btrfs_fs_info *info = root->fs_info;
|
struct btrfs_fs_info *info = root->fs_info;
|
||||||
struct extent_map_tree *block_group_cache;
|
struct extent_map_tree *block_group_cache;
|
||||||
struct btrfs_key key;
|
struct btrfs_key key;
|
||||||
struct btrfs_key found_key = { 0, 0, 0 };
|
struct btrfs_key found_key;
|
||||||
struct extent_buffer *leaf;
|
struct extent_buffer *leaf;
|
||||||
u32 nritems;
|
u32 nritems;
|
||||||
int ret;
|
int ret;
|
||||||
int slot;
|
|
||||||
|
|
||||||
btrfs_set_super_total_bytes(&info->super_copy, new_size);
|
btrfs_set_super_total_bytes(&info->super_copy, new_size);
|
||||||
block_group_cache = &info->block_group_cache;
|
block_group_cache = &info->block_group_cache;
|
||||||
|
@ -2372,34 +2341,35 @@ int btrfs_shrink_extent_tree(struct btrfs_root *root, u64 new_size)
|
||||||
again:
|
again:
|
||||||
total_found = 0;
|
total_found = 0;
|
||||||
key.objectid = new_size;
|
key.objectid = new_size;
|
||||||
cur_byte = key.objectid;
|
|
||||||
key.offset = 0;
|
key.offset = 0;
|
||||||
key.type = 0;
|
key.type = 0;
|
||||||
while(1) {
|
cur_byte = key.objectid;
|
||||||
|
|
||||||
ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
|
ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
goto out;
|
goto out;
|
||||||
next:
|
|
||||||
leaf = path->nodes[0];
|
ret = find_previous_extent(root, path);
|
||||||
if (key.objectid == new_size - 1) {
|
|
||||||
ret = find_overlapping_extent(root, path, new_size);
|
|
||||||
if (ret != 0) {
|
|
||||||
btrfs_release_path(root, path);
|
|
||||||
ret = btrfs_search_slot(NULL, root, &key,
|
|
||||||
path, 0, 0);
|
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
goto out;
|
goto out;
|
||||||
|
if (ret == 0) {
|
||||||
|
leaf = path->nodes[0];
|
||||||
|
btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
|
||||||
|
if (found_key.objectid + found_key.offset > new_size) {
|
||||||
|
cur_byte = found_key.objectid;
|
||||||
|
key.objectid = cur_byte;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
btrfs_release_path(root, path);
|
||||||
|
|
||||||
|
while(1) {
|
||||||
|
ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
|
||||||
|
if (ret < 0)
|
||||||
|
goto out;
|
||||||
|
|
||||||
|
leaf = path->nodes[0];
|
||||||
nritems = btrfs_header_nritems(leaf);
|
nritems = btrfs_header_nritems(leaf);
|
||||||
ret = 0;
|
next:
|
||||||
slot = path->slots[0];
|
|
||||||
if (slot < nritems)
|
|
||||||
btrfs_item_key_to_cpu(leaf, &found_key, slot);
|
|
||||||
if (slot == nritems ||
|
|
||||||
btrfs_key_type(&found_key) != BTRFS_EXTENT_ITEM_KEY) {
|
|
||||||
path->slots[0]++;
|
|
||||||
if (path->slots[0] >= nritems) {
|
if (path->slots[0] >= nritems) {
|
||||||
ret = btrfs_next_leaf(root, path);
|
ret = btrfs_next_leaf(root, path);
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
|
@ -2408,12 +2378,17 @@ next:
|
||||||
ret = 0;
|
ret = 0;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
leaf = path->nodes[0];
|
||||||
|
nritems = btrfs_header_nritems(leaf);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
|
||||||
|
if (btrfs_key_type(&found_key) != BTRFS_EXTENT_ITEM_KEY ||
|
||||||
|
found_key.objectid + found_key.offset <= cur_byte) {
|
||||||
|
path->slots[0]++;
|
||||||
goto next;
|
goto next;
|
||||||
}
|
}
|
||||||
btrfs_item_key_to_cpu(leaf, &found_key, slot);
|
|
||||||
if (found_key.objectid + found_key.offset <= cur_byte)
|
|
||||||
continue;
|
|
||||||
total_found++;
|
total_found++;
|
||||||
cur_byte = found_key.objectid + found_key.offset;
|
cur_byte = found_key.objectid + found_key.offset;
|
||||||
key.objectid = cur_byte;
|
key.objectid = cur_byte;
|
||||||
|
@ -2446,21 +2421,10 @@ next:
|
||||||
ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
|
ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
goto out;
|
goto out;
|
||||||
bg_next:
|
|
||||||
leaf = path->nodes[0];
|
leaf = path->nodes[0];
|
||||||
nritems = btrfs_header_nritems(leaf);
|
nritems = btrfs_header_nritems(leaf);
|
||||||
ret = 0;
|
bg_next:
|
||||||
slot = path->slots[0];
|
|
||||||
if (slot < nritems)
|
|
||||||
btrfs_item_key_to_cpu(leaf, &found_key, slot);
|
|
||||||
if (slot == nritems ||
|
|
||||||
btrfs_key_type(&found_key) != BTRFS_BLOCK_GROUP_ITEM_KEY) {
|
|
||||||
if (slot < nritems) {
|
|
||||||
printk("shrinker found key %Lu %u %Lu\n",
|
|
||||||
found_key.objectid, found_key.type,
|
|
||||||
found_key.offset);
|
|
||||||
path->slots[0]++;
|
|
||||||
}
|
|
||||||
if (path->slots[0] >= nritems) {
|
if (path->slots[0] >= nritems) {
|
||||||
ret = btrfs_next_leaf(root, path);
|
ret = btrfs_next_leaf(root, path);
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
|
@ -2469,10 +2433,18 @@ bg_next:
|
||||||
ret = 0;
|
ret = 0;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
leaf = path->nodes[0];
|
||||||
|
nritems = btrfs_header_nritems(leaf);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
|
||||||
|
if (btrfs_key_type(&found_key) != BTRFS_BLOCK_GROUP_ITEM_KEY) {
|
||||||
|
printk("shrinker found key %Lu %u %Lu\n",
|
||||||
|
found_key.objectid, found_key.type,
|
||||||
|
found_key.offset);
|
||||||
|
path->slots[0]++;
|
||||||
goto bg_next;
|
goto bg_next;
|
||||||
}
|
}
|
||||||
btrfs_item_key_to_cpu(leaf, &found_key, slot);
|
|
||||||
ret = get_state_private(&info->block_group_cache,
|
ret = get_state_private(&info->block_group_cache,
|
||||||
found_key.objectid, &ptr);
|
found_key.objectid, &ptr);
|
||||||
if (!ret)
|
if (!ret)
|
||||||
|
|
Loading…
Reference in a new issue