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);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-11-10 20:08:01 +00:00
|
|
|
/*
|
|
|
|
* 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)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
int slot;
|
|
|
|
u64 start = 0;
|
|
|
|
u64 bytes_used = 0;
|
|
|
|
struct btrfs_path path;
|
|
|
|
struct btrfs_key key;
|
|
|
|
struct extent_buffer *leaf;
|
|
|
|
struct btrfs_block_group *cache;
|
|
|
|
struct btrfs_fs_info *fs_info = trans->fs_info;
|
2021-11-08 19:26:41 +00:00
|
|
|
struct btrfs_root *root = btrfs_extent_root(fs_info, 0);
|
2021-11-10 20:08:01 +00:00
|
|
|
|
|
|
|
ret = btrfs_run_delayed_refs(trans, -1);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
btrfs_init_path(&path);
|
|
|
|
key.offset = 0;
|
|
|
|
key.objectid = 0;
|
|
|
|
key.type = BTRFS_EXTENT_ITEM_KEY;
|
2021-11-08 19:26:41 +00:00
|
|
|
ret = btrfs_search_slot(trans, root, &key, &path, 0, 0);
|
2021-11-10 20:08:01 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
while(1) {
|
|
|
|
leaf = path.nodes[0];
|
|
|
|
slot = path.slots[0];
|
|
|
|
if (slot >= btrfs_header_nritems(leaf)) {
|
|
|
|
ret = btrfs_next_leaf(root, &path);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
if (ret > 0)
|
|
|
|
break;
|
|
|
|
leaf = path.nodes[0];
|
|
|
|
slot = path.slots[0];
|
|
|
|
}
|
|
|
|
btrfs_item_key_to_cpu(leaf, &key, slot);
|
|
|
|
if (key.type == BTRFS_EXTENT_ITEM_KEY) {
|
|
|
|
bytes_used += key.offset;
|
|
|
|
ret = btrfs_update_block_group(trans,
|
|
|
|
key.objectid, key.offset, 1, 0);
|
|
|
|
BUG_ON(ret);
|
|
|
|
} else if (key.type == BTRFS_METADATA_ITEM_KEY) {
|
|
|
|
bytes_used += fs_info->nodesize;
|
|
|
|
ret = btrfs_update_block_group(trans,
|
|
|
|
key.objectid, fs_info->nodesize, 1, 0);
|
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
path.slots[0]++;
|
|
|
|
}
|
|
|
|
btrfs_set_super_bytes_used(root->fs_info->super_copy, bytes_used);
|
|
|
|
ret = 0;
|
|
|
|
out:
|
|
|
|
btrfs_release_path(&path);
|
|
|
|
return ret;
|
|
|
|
}
|