Commit 73e48b27 authored by Yan's avatar Yan Committed by Chris Mason

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: default avatarChris Mason <chris.mason@oracle.com>
parent d548ee51
...@@ -2317,36 +2317,6 @@ static int noinline relocate_one_extent(struct btrfs_root *extent_root, ...@@ -2317,36 +2317,6 @@ static int noinline relocate_one_extent(struct btrfs_root *extent_root,
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) ...@@ -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,48 +2341,54 @@ int btrfs_shrink_extent_tree(struct btrfs_root *root, u64 new_size) ...@@ -2372,48 +2341,54 @@ 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);
if (ret < 0)
goto out;
ret = find_previous_extent(root, path);
if (ret < 0)
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); 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]; leaf = path->nodes[0];
if (key.objectid == new_size - 1) { nritems = btrfs_header_nritems(leaf);
ret = find_overlapping_extent(root, path, new_size); next:
if (ret != 0) { if (path->slots[0] >= nritems) {
btrfs_release_path(root, path); ret = btrfs_next_leaf(root, path);
ret = btrfs_search_slot(NULL, root, &key, if (ret < 0)
path, 0, 0); goto out;
if (ret < 0) if (ret == 1) {
goto out; ret = 0;
break;
} }
leaf = path->nodes[0];
nritems = btrfs_header_nritems(leaf);
} }
nritems = btrfs_header_nritems(leaf);
ret = 0; btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
slot = path->slots[0]; if (btrfs_key_type(&found_key) != BTRFS_EXTENT_ITEM_KEY ||
if (slot < nritems) found_key.objectid + found_key.offset <= cur_byte) {
btrfs_item_key_to_cpu(leaf, &found_key, slot);
if (slot == nritems ||
btrfs_key_type(&found_key) != BTRFS_EXTENT_ITEM_KEY) {
path->slots[0]++; path->slots[0]++;
if (path->slots[0] >= nritems) {
ret = btrfs_next_leaf(root, path);
if (ret < 0)
goto out;
if (ret == 1) {
ret = 0;
break;
}
}
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,33 +2421,30 @@ int btrfs_shrink_extent_tree(struct btrfs_root *root, u64 new_size) ...@@ -2446,33 +2421,30 @@ int btrfs_shrink_extent_tree(struct btrfs_root *root, u64 new_size)
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 (path->slots[0] >= nritems) {
if (slot < nritems) ret = btrfs_next_leaf(root, path);
btrfs_item_key_to_cpu(leaf, &found_key, slot); if (ret < 0)
if (slot == nritems || break;
btrfs_key_type(&found_key) != BTRFS_BLOCK_GROUP_ITEM_KEY) { if (ret == 1) {
if (slot < nritems) { ret = 0;
printk("shrinker found key %Lu %u %Lu\n", break;
found_key.objectid, found_key.type,
found_key.offset);
path->slots[0]++;
}
if (path->slots[0] >= nritems) {
ret = btrfs_next_leaf(root, path);
if (ret < 0)
break;
if (ret == 1) {
ret = 0;
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)
......
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