2012-02-21 19:37:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 Oracle. All rights reserved.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public
|
|
|
|
* License v2 as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public
|
|
|
|
* License along with this program; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 021110-1307, USA.
|
|
|
|
*/
|
|
|
|
|
2020-08-18 13:56:04 +00:00
|
|
|
#include "kernel-shared/ctree.h"
|
2021-11-10 20:08:01 +00:00
|
|
|
#include "kernel-shared/transaction.h"
|
2021-11-10 20:08:03 +00:00
|
|
|
#include "kernel-shared/disk-io.h"
|
2020-08-18 09:12:28 +00:00
|
|
|
#include "common/extent-cache.h"
|
2019-06-19 23:46:21 +00:00
|
|
|
#include "common/utils.h"
|
2021-04-30 20:04:03 +00:00
|
|
|
#include "common/repair.h"
|
2012-02-21 19:37:21 +00:00
|
|
|
|
2016-07-04 13:22:53 +00:00
|
|
|
int repair = 0;
|
|
|
|
|
2012-02-21 19:37:21 +00:00
|
|
|
int btrfs_add_corrupt_extent_record(struct btrfs_fs_info *info,
|
|
|
|
struct btrfs_key *first_key,
|
|
|
|
u64 start, u64 len, int level)
|
|
|
|
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
struct btrfs_corrupt_block *corrupt;
|
|
|
|
|
|
|
|
if (!info->corrupt_blocks)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
corrupt = malloc(sizeof(*corrupt));
|
|
|
|
if (!corrupt)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
memcpy(&corrupt->key, first_key, sizeof(*first_key));
|
|
|
|
corrupt->cache.start = start;
|
|
|
|
corrupt->cache.size = len;
|
|
|
|
corrupt->level = level;
|
|
|
|
|
2013-07-03 13:25:13 +00:00
|
|
|
ret = insert_cache_extent(info->corrupt_blocks, &corrupt->cache);
|
2012-02-21 19:37:21 +00:00
|
|
|
if (ret)
|
|
|
|
free(corrupt);
|
|
|
|
BUG_ON(ret && ret != -EEXIST);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-11-10 20:08:03 +00:00
|
|
|
static int traverse_tree_blocks(struct extent_io_tree *tree,
|
|
|
|
struct extent_buffer *eb, int tree_root)
|
|
|
|
{
|
|
|
|
struct btrfs_fs_info *fs_info = eb->fs_info;
|
|
|
|
struct extent_buffer *tmp;
|
|
|
|
struct btrfs_root_item *ri;
|
|
|
|
struct btrfs_key key;
|
|
|
|
u64 bytenr;
|
|
|
|
int level = btrfs_header_level(eb);
|
|
|
|
int nritems;
|
|
|
|
int ret;
|
|
|
|
int i;
|
|
|
|
u64 end = eb->start + eb->len;
|
|
|
|
bool pin = tree == &fs_info->pinned_extents;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we have pinned/excluded this block before, don't do it again.
|
|
|
|
* This can not only avoid forever loop with broken filesystem
|
|
|
|
* but also give us some speedups.
|
|
|
|
*/
|
|
|
|
if (test_range_bit(tree, eb->start, end - 1, EXTENT_DIRTY, 0))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (pin)
|
|
|
|
btrfs_pin_extent(fs_info, eb->start, eb->len);
|
|
|
|
else
|
|
|
|
set_extent_dirty(tree, eb->start, end - 1);
|
|
|
|
|
|
|
|
nritems = btrfs_header_nritems(eb);
|
|
|
|
for (i = 0; i < nritems; i++) {
|
|
|
|
if (level == 0) {
|
|
|
|
bool is_extent_root;
|
|
|
|
btrfs_item_key_to_cpu(eb, &key, i);
|
|
|
|
if (key.type != BTRFS_ROOT_ITEM_KEY)
|
|
|
|
continue;
|
|
|
|
is_extent_root =
|
|
|
|
key.objectid == BTRFS_EXTENT_TREE_OBJECTID;
|
|
|
|
/* If pin, skip the extent root */
|
|
|
|
if (pin && is_extent_root)
|
|
|
|
continue;
|
|
|
|
ri = btrfs_item_ptr(eb, i, struct btrfs_root_item);
|
|
|
|
bytenr = btrfs_disk_root_bytenr(eb, ri);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If at any point we start needing the real root we
|
|
|
|
* will have to build a stump root for the root we are
|
|
|
|
* in, but for now this doesn't actually use the root so
|
|
|
|
* just pass in extent_root.
|
|
|
|
*/
|
|
|
|
tmp = read_tree_block(fs_info, bytenr, 0);
|
|
|
|
if (!extent_buffer_uptodate(tmp)) {
|
|
|
|
fprintf(stderr, "Error reading root block\n");
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
ret = traverse_tree_blocks(tree, tmp, 0);
|
|
|
|
free_extent_buffer(tmp);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
} else {
|
|
|
|
u64 end;
|
|
|
|
|
|
|
|
bytenr = btrfs_node_blockptr(eb, i);
|
|
|
|
end = bytenr + fs_info->nodesize - 1;
|
|
|
|
|
|
|
|
/* If we aren't the tree root don't read the block */
|
|
|
|
if (level == 1 && !tree_root) {
|
|
|
|
if (pin)
|
|
|
|
btrfs_pin_extent(fs_info, bytenr,
|
|
|
|
fs_info->nodesize);
|
|
|
|
else
|
|
|
|
set_extent_dirty(tree, bytenr, end);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp = read_tree_block(fs_info, bytenr, 0);
|
|
|
|
if (!extent_buffer_uptodate(tmp)) {
|
|
|
|
fprintf(stderr, "Error reading tree block\n");
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
ret = traverse_tree_blocks(tree, tmp, tree_root);
|
|
|
|
free_extent_buffer(tmp);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int btrfs_mark_used_tree_blocks(struct btrfs_fs_info *fs_info,
|
|
|
|
struct extent_io_tree *tree)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = traverse_tree_blocks(tree, fs_info->chunk_root->node, 0);
|
|
|
|
if (!ret)
|
|
|
|
ret = traverse_tree_blocks(tree, fs_info->tree_root->node, 1);
|
2022-03-07 22:10:56 +00:00
|
|
|
if (!ret && fs_info->block_group_root)
|
|
|
|
ret = traverse_tree_blocks(tree,
|
|
|
|
fs_info->block_group_root->node, 0);
|
2021-11-10 20:08:03 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-11-08 19:26:48 +00:00
|
|
|
static int populate_used_from_extent_root(struct btrfs_root *root,
|
|
|
|
struct extent_io_tree *io_tree)
|
2021-11-10 20:08:01 +00:00
|
|
|
{
|
2021-11-08 19:26:48 +00:00
|
|
|
struct btrfs_fs_info *fs_info = root->fs_info;
|
|
|
|
struct extent_buffer *leaf;
|
2021-11-10 20:08:01 +00:00
|
|
|
struct btrfs_path path;
|
|
|
|
struct btrfs_key key;
|
2021-11-08 19:26:48 +00:00
|
|
|
int slot;
|
|
|
|
int ret;
|
2021-11-10 20:08:01 +00:00
|
|
|
|
|
|
|
btrfs_init_path(&path);
|
|
|
|
key.offset = 0;
|
|
|
|
key.objectid = 0;
|
|
|
|
key.type = BTRFS_EXTENT_ITEM_KEY;
|
2021-11-08 19:26:48 +00:00
|
|
|
ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
|
2021-11-10 20:08:01 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2021-11-08 19:26:48 +00:00
|
|
|
|
2021-11-10 20:08:01 +00:00
|
|
|
while(1) {
|
2021-11-08 19:26:48 +00:00
|
|
|
u64 start, end;
|
|
|
|
|
2021-11-10 20:08:01 +00:00
|
|
|
leaf = path.nodes[0];
|
|
|
|
slot = path.slots[0];
|
|
|
|
if (slot >= btrfs_header_nritems(leaf)) {
|
|
|
|
ret = btrfs_next_leaf(root, &path);
|
|
|
|
if (ret < 0)
|
|
|
|
break;
|
2022-02-22 22:22:38 +00:00
|
|
|
if (ret > 0) {
|
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
}
|
2021-11-10 20:08:01 +00:00
|
|
|
leaf = path.nodes[0];
|
|
|
|
slot = path.slots[0];
|
|
|
|
}
|
|
|
|
btrfs_item_key_to_cpu(leaf, &key, slot);
|
2021-11-08 19:26:48 +00:00
|
|
|
start = end = key.objectid;
|
|
|
|
if (key.type == BTRFS_EXTENT_ITEM_KEY)
|
|
|
|
end = start + key.offset - 1;
|
|
|
|
else if (key.type == BTRFS_METADATA_ITEM_KEY)
|
|
|
|
end = start + fs_info->nodesize - 1;
|
|
|
|
|
2022-02-22 22:22:38 +00:00
|
|
|
if (start != end) {
|
|
|
|
if (!IS_ALIGNED(start, fs_info->sectorsize) ||
|
|
|
|
!IS_ALIGNED(end + 1, fs_info->sectorsize)) {
|
|
|
|
fprintf(stderr, "unaligned value in the extent tree start %llu end %llu\n",
|
|
|
|
start, end + 1);
|
|
|
|
ret = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
2021-11-08 19:26:48 +00:00
|
|
|
set_extent_dirty(io_tree, start, end);
|
2022-02-22 22:22:38 +00:00
|
|
|
}
|
2021-11-08 19:26:48 +00:00
|
|
|
|
2021-11-10 20:08:01 +00:00
|
|
|
path.slots[0]++;
|
|
|
|
}
|
2021-11-08 19:26:48 +00:00
|
|
|
btrfs_release_path(&path);
|
2022-02-22 22:22:38 +00:00
|
|
|
return ret;
|
2021-11-08 19:26:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int btrfs_mark_used_blocks(struct btrfs_fs_info *fs_info,
|
|
|
|
struct extent_io_tree *tree)
|
|
|
|
{
|
|
|
|
struct btrfs_root *root;
|
|
|
|
struct rb_node *n;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
root = btrfs_extent_root(fs_info, 0);
|
|
|
|
while (1) {
|
|
|
|
ret = populate_used_from_extent_root(root, tree);
|
|
|
|
if (ret)
|
|
|
|
break;
|
|
|
|
n = rb_next(&root->rb_node);
|
|
|
|
if (!n)
|
|
|
|
break;
|
|
|
|
root = rb_entry(n, struct btrfs_root, rb_node);
|
|
|
|
if (root->root_key.objectid != BTRFS_EXTENT_TREE_OBJECTID)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Fixup block accounting. The initial block accounting created by
|
|
|
|
* make_block_groups isn't accuracy in this case.
|
|
|
|
*/
|
|
|
|
int btrfs_fix_block_accounting(struct btrfs_trans_handle *trans)
|
|
|
|
{
|
|
|
|
struct extent_io_tree used;
|
|
|
|
struct btrfs_fs_info *fs_info = trans->fs_info;
|
|
|
|
struct btrfs_block_group *cache;
|
|
|
|
u64 start, end;
|
|
|
|
u64 bytes_used = 0;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
ret = btrfs_run_delayed_refs(trans, -1);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
extent_io_tree_init(&used);
|
|
|
|
|
|
|
|
ret = btrfs_mark_used_blocks(fs_info, &used);
|
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
start = 0;
|
|
|
|
while(1) {
|
|
|
|
cache = btrfs_lookup_first_block_group(fs_info, start);
|
|
|
|
if (!cache)
|
|
|
|
break;
|
|
|
|
|
|
|
|
start = cache->start + cache->length;
|
|
|
|
cache->used = 0;
|
|
|
|
cache->space_info->bytes_used = 0;
|
|
|
|
if (list_empty(&cache->dirty_list))
|
|
|
|
list_add_tail(&cache->dirty_list, &trans->dirty_bgs);
|
|
|
|
}
|
|
|
|
|
|
|
|
start = 0;
|
|
|
|
while (1) {
|
|
|
|
ret = find_first_extent_bit(&used, 0, &start, &end,
|
|
|
|
EXTENT_DIRTY);
|
|
|
|
if (ret)
|
|
|
|
break;
|
|
|
|
|
|
|
|
bytes_used += end - start + 1;
|
|
|
|
ret = btrfs_update_block_group(trans, start, end - start + 1,
|
|
|
|
1, 0);
|
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
clear_extent_dirty(&used, start, end);
|
|
|
|
}
|
|
|
|
btrfs_set_super_bytes_used(fs_info->super_copy, bytes_used);
|
2021-11-10 20:08:01 +00:00
|
|
|
ret = 0;
|
|
|
|
out:
|
2021-11-08 19:26:48 +00:00
|
|
|
extent_io_tree_cleanup(&used);
|
2021-11-10 20:08:01 +00:00
|
|
|
return ret;
|
|
|
|
}
|