Commit 2f659546 authored by Qu Wenruo's avatar Qu Wenruo Committed by David Sterba

btrfs: tree-checker: Replace root parameter with fs_info

When inspecting the error message with real corruption, the "root=%llu"
always shows "1" (root tree), instead of the correct owner.

The problem is that we are getting @root from page->mapping->host, which
points the same btree inode, so we will always get the same root.

This makes the root owner output meaningless, and harder to port
tree-checker to btrfs-progs.

So get rid of the false and meaningless @root parameter and replace it
with @fs_info.
To get the owner, we can only rely on btrfs_header_owner() now.
Signed-off-by: default avatarQu Wenruo <wqu@suse.com>
Signed-off-by: default avatarDavid Sterba <dsterba@suse.com>
parent 393da918
...@@ -602,12 +602,12 @@ static int btree_readpage_end_io_hook(struct btrfs_io_bio *io_bio, ...@@ -602,12 +602,12 @@ static int btree_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
* that we don't try and read the other copies of this block, just * that we don't try and read the other copies of this block, just
* return -EIO. * return -EIO.
*/ */
if (found_level == 0 && btrfs_check_leaf_full(root, eb)) { if (found_level == 0 && btrfs_check_leaf_full(fs_info, eb)) {
set_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags); set_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags);
ret = -EIO; ret = -EIO;
} }
if (found_level > 0 && btrfs_check_node(root, eb)) if (found_level > 0 && btrfs_check_node(fs_info, eb))
ret = -EIO; ret = -EIO;
if (!ret) if (!ret)
...@@ -3854,7 +3854,7 @@ void btrfs_mark_buffer_dirty(struct extent_buffer *buf) ...@@ -3854,7 +3854,7 @@ void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
* So here we should only check item pointers, not item data. * So here we should only check item pointers, not item data.
*/ */
if (btrfs_header_level(buf) == 0 && if (btrfs_header_level(buf) == 0 &&
btrfs_check_leaf_relaxed(root, buf)) { btrfs_check_leaf_relaxed(fs_info, buf)) {
btrfs_print_leaf(buf); btrfs_print_leaf(buf);
ASSERT(0); ASSERT(0);
} }
......
...@@ -53,7 +53,7 @@ ...@@ -53,7 +53,7 @@
* Allows callers to customize the output. * Allows callers to customize the output.
*/ */
__printf(4, 5) __printf(4, 5)
static void generic_err(const struct btrfs_root *root, static void generic_err(const struct btrfs_fs_info *fs_info,
const struct extent_buffer *eb, int slot, const struct extent_buffer *eb, int slot,
const char *fmt, ...) const char *fmt, ...)
{ {
...@@ -65,10 +65,10 @@ static void generic_err(const struct btrfs_root *root, ...@@ -65,10 +65,10 @@ static void generic_err(const struct btrfs_root *root,
vaf.fmt = fmt; vaf.fmt = fmt;
vaf.va = &args; vaf.va = &args;
btrfs_crit(root->fs_info, btrfs_crit(fs_info,
"corrupt %s: root=%llu block=%llu slot=%d, %pV", "corrupt %s: root=%llu block=%llu slot=%d, %pV",
btrfs_header_level(eb) == 0 ? "leaf" : "node", btrfs_header_level(eb) == 0 ? "leaf" : "node",
root->objectid, btrfs_header_bytenr(eb), slot, &vaf); btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot, &vaf);
va_end(args); va_end(args);
} }
...@@ -77,7 +77,7 @@ static void generic_err(const struct btrfs_root *root, ...@@ -77,7 +77,7 @@ static void generic_err(const struct btrfs_root *root,
* offset has its own meaning. * offset has its own meaning.
*/ */
__printf(4, 5) __printf(4, 5)
static void file_extent_err(const struct btrfs_root *root, static void file_extent_err(const struct btrfs_fs_info *fs_info,
const struct extent_buffer *eb, int slot, const struct extent_buffer *eb, int slot,
const char *fmt, ...) const char *fmt, ...)
{ {
...@@ -91,10 +91,11 @@ static void file_extent_err(const struct btrfs_root *root, ...@@ -91,10 +91,11 @@ static void file_extent_err(const struct btrfs_root *root,
vaf.fmt = fmt; vaf.fmt = fmt;
vaf.va = &args; vaf.va = &args;
btrfs_crit(root->fs_info, btrfs_crit(fs_info,
"corrupt %s: root=%llu block=%llu slot=%d ino=%llu file_offset=%llu, %pV", "corrupt %s: root=%llu block=%llu slot=%d ino=%llu file_offset=%llu, %pV",
btrfs_header_level(eb) == 0 ? "leaf" : "node", root->objectid, btrfs_header_level(eb) == 0 ? "leaf" : "node",
btrfs_header_bytenr(eb), slot, key.objectid, key.offset, &vaf); btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot,
key.objectid, key.offset, &vaf);
va_end(args); va_end(args);
} }
...@@ -102,26 +103,26 @@ static void file_extent_err(const struct btrfs_root *root, ...@@ -102,26 +103,26 @@ static void file_extent_err(const struct btrfs_root *root,
* Return 0 if the btrfs_file_extent_##name is aligned to @alignment * Return 0 if the btrfs_file_extent_##name is aligned to @alignment
* Else return 1 * Else return 1
*/ */
#define CHECK_FE_ALIGNED(root, leaf, slot, fi, name, alignment) \ #define CHECK_FE_ALIGNED(fs_info, leaf, slot, fi, name, alignment) \
({ \ ({ \
if (!IS_ALIGNED(btrfs_file_extent_##name((leaf), (fi)), (alignment))) \ if (!IS_ALIGNED(btrfs_file_extent_##name((leaf), (fi)), (alignment))) \
file_extent_err((root), (leaf), (slot), \ file_extent_err((fs_info), (leaf), (slot), \
"invalid %s for file extent, have %llu, should be aligned to %u", \ "invalid %s for file extent, have %llu, should be aligned to %u", \
(#name), btrfs_file_extent_##name((leaf), (fi)), \ (#name), btrfs_file_extent_##name((leaf), (fi)), \
(alignment)); \ (alignment)); \
(!IS_ALIGNED(btrfs_file_extent_##name((leaf), (fi)), (alignment))); \ (!IS_ALIGNED(btrfs_file_extent_##name((leaf), (fi)), (alignment))); \
}) })
static int check_extent_data_item(struct btrfs_root *root, static int check_extent_data_item(struct btrfs_fs_info *fs_info,
struct extent_buffer *leaf, struct extent_buffer *leaf,
struct btrfs_key *key, int slot) struct btrfs_key *key, int slot)
{ {
struct btrfs_file_extent_item *fi; struct btrfs_file_extent_item *fi;
u32 sectorsize = root->fs_info->sectorsize; u32 sectorsize = fs_info->sectorsize;
u32 item_size = btrfs_item_size_nr(leaf, slot); u32 item_size = btrfs_item_size_nr(leaf, slot);
if (!IS_ALIGNED(key->offset, sectorsize)) { if (!IS_ALIGNED(key->offset, sectorsize)) {
file_extent_err(root, leaf, slot, file_extent_err(fs_info, leaf, slot,
"unaligned file_offset for file extent, have %llu should be aligned to %u", "unaligned file_offset for file extent, have %llu should be aligned to %u",
key->offset, sectorsize); key->offset, sectorsize);
return -EUCLEAN; return -EUCLEAN;
...@@ -130,7 +131,7 @@ static int check_extent_data_item(struct btrfs_root *root, ...@@ -130,7 +131,7 @@ static int check_extent_data_item(struct btrfs_root *root,
fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
if (btrfs_file_extent_type(leaf, fi) > BTRFS_FILE_EXTENT_TYPES) { if (btrfs_file_extent_type(leaf, fi) > BTRFS_FILE_EXTENT_TYPES) {
file_extent_err(root, leaf, slot, file_extent_err(fs_info, leaf, slot,
"invalid type for file extent, have %u expect range [0, %u]", "invalid type for file extent, have %u expect range [0, %u]",
btrfs_file_extent_type(leaf, fi), btrfs_file_extent_type(leaf, fi),
BTRFS_FILE_EXTENT_TYPES); BTRFS_FILE_EXTENT_TYPES);
...@@ -142,14 +143,14 @@ static int check_extent_data_item(struct btrfs_root *root, ...@@ -142,14 +143,14 @@ static int check_extent_data_item(struct btrfs_root *root,
* and must be caught in open_ctree(). * and must be caught in open_ctree().
*/ */
if (btrfs_file_extent_compression(leaf, fi) > BTRFS_COMPRESS_TYPES) { if (btrfs_file_extent_compression(leaf, fi) > BTRFS_COMPRESS_TYPES) {
file_extent_err(root, leaf, slot, file_extent_err(fs_info, leaf, slot,
"invalid compression for file extent, have %u expect range [0, %u]", "invalid compression for file extent, have %u expect range [0, %u]",
btrfs_file_extent_compression(leaf, fi), btrfs_file_extent_compression(leaf, fi),
BTRFS_COMPRESS_TYPES); BTRFS_COMPRESS_TYPES);
return -EUCLEAN; return -EUCLEAN;
} }
if (btrfs_file_extent_encryption(leaf, fi)) { if (btrfs_file_extent_encryption(leaf, fi)) {
file_extent_err(root, leaf, slot, file_extent_err(fs_info, leaf, slot,
"invalid encryption for file extent, have %u expect 0", "invalid encryption for file extent, have %u expect 0",
btrfs_file_extent_encryption(leaf, fi)); btrfs_file_extent_encryption(leaf, fi));
return -EUCLEAN; return -EUCLEAN;
...@@ -157,7 +158,7 @@ static int check_extent_data_item(struct btrfs_root *root, ...@@ -157,7 +158,7 @@ static int check_extent_data_item(struct btrfs_root *root,
if (btrfs_file_extent_type(leaf, fi) == BTRFS_FILE_EXTENT_INLINE) { if (btrfs_file_extent_type(leaf, fi) == BTRFS_FILE_EXTENT_INLINE) {
/* Inline extent must have 0 as key offset */ /* Inline extent must have 0 as key offset */
if (key->offset) { if (key->offset) {
file_extent_err(root, leaf, slot, file_extent_err(fs_info, leaf, slot,
"invalid file_offset for inline file extent, have %llu expect 0", "invalid file_offset for inline file extent, have %llu expect 0",
key->offset); key->offset);
return -EUCLEAN; return -EUCLEAN;
...@@ -171,7 +172,7 @@ static int check_extent_data_item(struct btrfs_root *root, ...@@ -171,7 +172,7 @@ static int check_extent_data_item(struct btrfs_root *root,
/* Uncompressed inline extent size must match item size */ /* Uncompressed inline extent size must match item size */
if (item_size != BTRFS_FILE_EXTENT_INLINE_DATA_START + if (item_size != BTRFS_FILE_EXTENT_INLINE_DATA_START +
btrfs_file_extent_ram_bytes(leaf, fi)) { btrfs_file_extent_ram_bytes(leaf, fi)) {
file_extent_err(root, leaf, slot, file_extent_err(fs_info, leaf, slot,
"invalid ram_bytes for uncompressed inline extent, have %u expect %llu", "invalid ram_bytes for uncompressed inline extent, have %u expect %llu",
item_size, BTRFS_FILE_EXTENT_INLINE_DATA_START + item_size, BTRFS_FILE_EXTENT_INLINE_DATA_START +
btrfs_file_extent_ram_bytes(leaf, fi)); btrfs_file_extent_ram_bytes(leaf, fi));
...@@ -182,40 +183,41 @@ static int check_extent_data_item(struct btrfs_root *root, ...@@ -182,40 +183,41 @@ static int check_extent_data_item(struct btrfs_root *root,
/* Regular or preallocated extent has fixed item size */ /* Regular or preallocated extent has fixed item size */
if (item_size != sizeof(*fi)) { if (item_size != sizeof(*fi)) {
file_extent_err(root, leaf, slot, file_extent_err(fs_info, leaf, slot,
"invalid item size for reg/prealloc file extent, have %u expect %zu", "invalid item size for reg/prealloc file extent, have %u expect %zu",
item_size, sizeof(*fi)); item_size, sizeof(*fi));
return -EUCLEAN; return -EUCLEAN;
} }
if (CHECK_FE_ALIGNED(root, leaf, slot, fi, ram_bytes, sectorsize) || if (CHECK_FE_ALIGNED(fs_info, leaf, slot, fi, ram_bytes, sectorsize) ||
CHECK_FE_ALIGNED(root, leaf, slot, fi, disk_bytenr, sectorsize) || CHECK_FE_ALIGNED(fs_info, leaf, slot, fi, disk_bytenr, sectorsize) ||
CHECK_FE_ALIGNED(root, leaf, slot, fi, disk_num_bytes, sectorsize) || CHECK_FE_ALIGNED(fs_info, leaf, slot, fi, disk_num_bytes, sectorsize) ||
CHECK_FE_ALIGNED(root, leaf, slot, fi, offset, sectorsize) || CHECK_FE_ALIGNED(fs_info, leaf, slot, fi, offset, sectorsize) ||
CHECK_FE_ALIGNED(root, leaf, slot, fi, num_bytes, sectorsize)) CHECK_FE_ALIGNED(fs_info, leaf, slot, fi, num_bytes, sectorsize))
return -EUCLEAN; return -EUCLEAN;
return 0; return 0;
} }
static int check_csum_item(struct btrfs_root *root, struct extent_buffer *leaf, static int check_csum_item(struct btrfs_fs_info *fs_info,
struct btrfs_key *key, int slot) struct extent_buffer *leaf, struct btrfs_key *key,
int slot)
{ {
u32 sectorsize = root->fs_info->sectorsize; u32 sectorsize = fs_info->sectorsize;
u32 csumsize = btrfs_super_csum_size(root->fs_info->super_copy); u32 csumsize = btrfs_super_csum_size(fs_info->super_copy);
if (key->objectid != BTRFS_EXTENT_CSUM_OBJECTID) { if (key->objectid != BTRFS_EXTENT_CSUM_OBJECTID) {
generic_err(root, leaf, slot, generic_err(fs_info, leaf, slot,
"invalid key objectid for csum item, have %llu expect %llu", "invalid key objectid for csum item, have %llu expect %llu",
key->objectid, BTRFS_EXTENT_CSUM_OBJECTID); key->objectid, BTRFS_EXTENT_CSUM_OBJECTID);
return -EUCLEAN; return -EUCLEAN;
} }
if (!IS_ALIGNED(key->offset, sectorsize)) { if (!IS_ALIGNED(key->offset, sectorsize)) {
generic_err(root, leaf, slot, generic_err(fs_info, leaf, slot,
"unaligned key offset for csum item, have %llu should be aligned to %u", "unaligned key offset for csum item, have %llu should be aligned to %u",
key->offset, sectorsize); key->offset, sectorsize);
return -EUCLEAN; return -EUCLEAN;
} }
if (!IS_ALIGNED(btrfs_item_size_nr(leaf, slot), csumsize)) { if (!IS_ALIGNED(btrfs_item_size_nr(leaf, slot), csumsize)) {
generic_err(root, leaf, slot, generic_err(fs_info, leaf, slot,
"unaligned item size for csum item, have %u should be aligned to %u", "unaligned item size for csum item, have %u should be aligned to %u",
btrfs_item_size_nr(leaf, slot), csumsize); btrfs_item_size_nr(leaf, slot), csumsize);
return -EUCLEAN; return -EUCLEAN;
...@@ -228,7 +230,7 @@ static int check_csum_item(struct btrfs_root *root, struct extent_buffer *leaf, ...@@ -228,7 +230,7 @@ static int check_csum_item(struct btrfs_root *root, struct extent_buffer *leaf,
* which represents inode number * which represents inode number
*/ */
__printf(4, 5) __printf(4, 5)
static void dir_item_err(const struct btrfs_root *root, static void dir_item_err(const struct btrfs_fs_info *fs_info,
const struct extent_buffer *eb, int slot, const struct extent_buffer *eb, int slot,
const char *fmt, ...) const char *fmt, ...)
{ {
...@@ -242,14 +244,15 @@ static void dir_item_err(const struct btrfs_root *root, ...@@ -242,14 +244,15 @@ static void dir_item_err(const struct btrfs_root *root,
vaf.fmt = fmt; vaf.fmt = fmt;
vaf.va = &args; vaf.va = &args;
btrfs_crit(root->fs_info, btrfs_crit(fs_info,
"corrupt %s: root=%llu block=%llu slot=%d ino=%llu, %pV", "corrupt %s: root=%llu block=%llu slot=%d ino=%llu, %pV",
btrfs_header_level(eb) == 0 ? "leaf" : "node", root->objectid, btrfs_header_level(eb) == 0 ? "leaf" : "node",
btrfs_header_bytenr(eb), slot, key.objectid, &vaf); btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot,
key.objectid, &vaf);
va_end(args); va_end(args);
} }
static int check_dir_item(struct btrfs_root *root, static int check_dir_item(struct btrfs_fs_info *fs_info,
struct extent_buffer *leaf, struct extent_buffer *leaf,
struct btrfs_key *key, int slot) struct btrfs_key *key, int slot)
{ {
...@@ -268,7 +271,7 @@ static int check_dir_item(struct btrfs_root *root, ...@@ -268,7 +271,7 @@ static int check_dir_item(struct btrfs_root *root,
/* header itself should not cross item boundary */ /* header itself should not cross item boundary */
if (cur + sizeof(*di) > item_size) { if (cur + sizeof(*di) > item_size) {
dir_item_err(root, leaf, slot, dir_item_err(fs_info, leaf, slot,
"dir item header crosses item boundary, have %zu boundary %u", "dir item header crosses item boundary, have %zu boundary %u",
cur + sizeof(*di), item_size); cur + sizeof(*di), item_size);
return -EUCLEAN; return -EUCLEAN;
...@@ -277,7 +280,7 @@ static int check_dir_item(struct btrfs_root *root, ...@@ -277,7 +280,7 @@ static int check_dir_item(struct btrfs_root *root,
/* dir type check */ /* dir type check */
dir_type = btrfs_dir_type(leaf, di); dir_type = btrfs_dir_type(leaf, di);
if (dir_type >= BTRFS_FT_MAX) { if (dir_type >= BTRFS_FT_MAX) {
dir_item_err(root, leaf, slot, dir_item_err(fs_info, leaf, slot,
"invalid dir item type, have %u expect [0, %u)", "invalid dir item type, have %u expect [0, %u)",
dir_type, BTRFS_FT_MAX); dir_type, BTRFS_FT_MAX);
return -EUCLEAN; return -EUCLEAN;
...@@ -285,14 +288,14 @@ static int check_dir_item(struct btrfs_root *root, ...@@ -285,14 +288,14 @@ static int check_dir_item(struct btrfs_root *root,
if (key->type == BTRFS_XATTR_ITEM_KEY && if (key->type == BTRFS_XATTR_ITEM_KEY &&
dir_type != BTRFS_FT_XATTR) { dir_type != BTRFS_FT_XATTR) {
dir_item_err(root, leaf, slot, dir_item_err(fs_info, leaf, slot,
"invalid dir item type for XATTR key, have %u expect %u", "invalid dir item type for XATTR key, have %u expect %u",
dir_type, BTRFS_FT_XATTR); dir_type, BTRFS_FT_XATTR);
return -EUCLEAN; return -EUCLEAN;
} }
if (dir_type == BTRFS_FT_XATTR && if (dir_type == BTRFS_FT_XATTR &&
key->type != BTRFS_XATTR_ITEM_KEY) { key->type != BTRFS_XATTR_ITEM_KEY) {
dir_item_err(root, leaf, slot, dir_item_err(fs_info, leaf, slot,
"xattr dir type found for non-XATTR key"); "xattr dir type found for non-XATTR key");
return -EUCLEAN; return -EUCLEAN;
} }
...@@ -305,21 +308,21 @@ static int check_dir_item(struct btrfs_root *root, ...@@ -305,21 +308,21 @@ static int check_dir_item(struct btrfs_root *root,
name_len = btrfs_dir_name_len(leaf, di); name_len = btrfs_dir_name_len(leaf, di);
data_len = btrfs_dir_data_len(leaf, di); data_len = btrfs_dir_data_len(leaf, di);
if (name_len > max_name_len) { if (name_len > max_name_len) {
dir_item_err(root, leaf, slot, dir_item_err(fs_info, leaf, slot,
"dir item name len too long, have %u max %u", "dir item name len too long, have %u max %u",
name_len, max_name_len); name_len, max_name_len);
return -EUCLEAN; return -EUCLEAN;
} }
if (name_len + data_len > BTRFS_MAX_XATTR_SIZE(root->fs_info)) { if (name_len + data_len > BTRFS_MAX_XATTR_SIZE(fs_info)) {
dir_item_err(root, leaf, slot, dir_item_err(fs_info, leaf, slot,
"dir item name and data len too long, have %u max %u", "dir item name and data len too long, have %u max %u",
name_len + data_len, name_len + data_len,
BTRFS_MAX_XATTR_SIZE(root->fs_info)); BTRFS_MAX_XATTR_SIZE(fs_info));
return -EUCLEAN; return -EUCLEAN;
} }
if (data_len && dir_type != BTRFS_FT_XATTR) { if (data_len && dir_type != BTRFS_FT_XATTR) {
dir_item_err(root, leaf, slot, dir_item_err(fs_info, leaf, slot,
"dir item with invalid data len, have %u expect 0", "dir item with invalid data len, have %u expect 0",
data_len); data_len);
return -EUCLEAN; return -EUCLEAN;
...@@ -329,7 +332,7 @@ static int check_dir_item(struct btrfs_root *root, ...@@ -329,7 +332,7 @@ static int check_dir_item(struct btrfs_root *root,
/* header and name/data should not cross item boundary */ /* header and name/data should not cross item boundary */
if (cur + total_size > item_size) { if (cur + total_size > item_size) {
dir_item_err(root, leaf, slot, dir_item_err(fs_info, leaf, slot,
"dir item data crosses item boundary, have %u boundary %u", "dir item data crosses item boundary, have %u boundary %u",
cur + total_size, item_size); cur + total_size, item_size);
return -EUCLEAN; return -EUCLEAN;
...@@ -347,7 +350,7 @@ static int check_dir_item(struct btrfs_root *root, ...@@ -347,7 +350,7 @@ static int check_dir_item(struct btrfs_root *root,
(unsigned long)(di + 1), name_len); (unsigned long)(di + 1), name_len);
name_hash = btrfs_name_hash(namebuf, name_len); name_hash = btrfs_name_hash(namebuf, name_len);
if (key->offset != name_hash) { if (key->offset != name_hash) {
dir_item_err(root, leaf, slot, dir_item_err(fs_info, leaf, slot,
"name hash mismatch with key, have 0x%016x expect 0x%016llx", "name hash mismatch with key, have 0x%016x expect 0x%016llx",
name_hash, key->offset); name_hash, key->offset);
return -EUCLEAN; return -EUCLEAN;
...@@ -362,7 +365,7 @@ static int check_dir_item(struct btrfs_root *root, ...@@ -362,7 +365,7 @@ static int check_dir_item(struct btrfs_root *root,
/* /*
* Common point to switch the item-specific validation. * Common point to switch the item-specific validation.
*/ */
static int check_leaf_item(struct btrfs_root *root, static int check_leaf_item(struct btrfs_fs_info *fs_info,
struct extent_buffer *leaf, struct extent_buffer *leaf,
struct btrfs_key *key, int slot) struct btrfs_key *key, int slot)
{ {
...@@ -370,24 +373,23 @@ static int check_leaf_item(struct btrfs_root *root, ...@@ -370,24 +373,23 @@ static int check_leaf_item(struct btrfs_root *root,
switch (key->type) { switch (key->type) {
case BTRFS_EXTENT_DATA_KEY: case BTRFS_EXTENT_DATA_KEY:
ret = check_extent_data_item(root, leaf, key, slot); ret = check_extent_data_item(fs_info, leaf, key, slot);
break; break;
case BTRFS_EXTENT_CSUM_KEY: case BTRFS_EXTENT_CSUM_KEY:
ret = check_csum_item(root, leaf, key, slot); ret = check_csum_item(fs_info, leaf, key, slot);
break; break;
case BTRFS_DIR_ITEM_KEY: case BTRFS_DIR_ITEM_KEY:
case BTRFS_DIR_INDEX_KEY: case BTRFS_DIR_INDEX_KEY:
case BTRFS_XATTR_ITEM_KEY: case BTRFS_XATTR_ITEM_KEY:
ret = check_dir_item(root, leaf, key, slot); ret = check_dir_item(fs_info, leaf, key, slot);
break; break;
} }
return ret; return ret;
} }
static int check_leaf(struct btrfs_root *root, struct extent_buffer *leaf, static int check_leaf(struct btrfs_fs_info *fs_info, struct extent_buffer *leaf,
bool check_item_data) bool check_item_data)
{ {
struct btrfs_fs_info *fs_info = root->fs_info;
/* No valid key type is 0, so all key should be larger than this key */ /* No valid key type is 0, so all key should be larger than this key */
struct btrfs_key prev_key = {0, 0, 0}; struct btrfs_key prev_key = {0, 0, 0};
struct btrfs_key key; struct btrfs_key key;
...@@ -420,7 +422,7 @@ static int check_leaf(struct btrfs_root *root, struct extent_buffer *leaf, ...@@ -420,7 +422,7 @@ static int check_leaf(struct btrfs_root *root, struct extent_buffer *leaf,
eb = btrfs_root_node(check_root); eb = btrfs_root_node(check_root);
/* if leaf is the root, then it's fine */ /* if leaf is the root, then it's fine */
if (leaf != eb) { if (leaf != eb) {
generic_err(check_root, leaf, 0, generic_err(fs_info, leaf, 0,
"invalid nritems, have %u should not be 0 for non-root leaf", "invalid nritems, have %u should not be 0 for non-root leaf",
nritems); nritems);
free_extent_buffer(eb); free_extent_buffer(eb);
...@@ -453,7 +455,7 @@ static int check_leaf(struct btrfs_root *root, struct extent_buffer *leaf, ...@@ -453,7 +455,7 @@ static int check_leaf(struct btrfs_root *root, struct extent_buffer *leaf,
/* Make sure the keys are in the right order */ /* Make sure the keys are in the right order */
if (btrfs_comp_cpu_keys(&prev_key, &key) >= 0) { if (btrfs_comp_cpu_keys(&prev_key, &key) >= 0) {
generic_err(root, leaf, slot, generic_err(fs_info, leaf, slot,
"bad key order, prev (%llu %u %llu) current (%llu %u %llu)", "bad key order, prev (%llu %u %llu) current (%llu %u %llu)",
prev_key.objectid, prev_key.type, prev_key.objectid, prev_key.type,
prev_key.offset, key.objectid, key.type, prev_key.offset, key.objectid, key.type,
...@@ -472,7 +474,7 @@ static int check_leaf(struct btrfs_root *root, struct extent_buffer *leaf, ...@@ -472,7 +474,7 @@ static int check_leaf(struct btrfs_root *root, struct extent_buffer *leaf,
item_end_expected = btrfs_item_offset_nr(leaf, item_end_expected = btrfs_item_offset_nr(leaf,
slot - 1); slot - 1);
if (btrfs_item_end_nr(leaf, slot) != item_end_expected) { if (btrfs_item_end_nr(leaf, slot) != item_end_expected) {
generic_err(root, leaf, slot, generic_err(fs_info, leaf, slot,
"unexpected item end, have %u expect %u", "unexpected item end, have %u expect %u",
btrfs_item_end_nr(leaf, slot), btrfs_item_end_nr(leaf, slot),
item_end_expected); item_end_expected);
...@@ -486,7 +488,7 @@ static int check_leaf(struct btrfs_root *root, struct extent_buffer *leaf, ...@@ -486,7 +488,7 @@ static int check_leaf(struct btrfs_root *root, struct extent_buffer *leaf,
*/ */
if (btrfs_item_end_nr(leaf, slot) > if (btrfs_item_end_nr(leaf, slot) >
BTRFS_LEAF_DATA_SIZE(fs_info)) { BTRFS_LEAF_DATA_SIZE(fs_info)) {
generic_err(root, leaf, slot, generic_err(fs_info, leaf, slot,
"slot end outside of leaf, have %u expect range [0, %u]", "slot end outside of leaf, have %u expect range [0, %u]",
btrfs_item_end_nr(leaf, slot), btrfs_item_end_nr(leaf, slot),
BTRFS_LEAF_DATA_SIZE(fs_info)); BTRFS_LEAF_DATA_SIZE(fs_info));
...@@ -496,7 +498,7 @@ static int check_leaf(struct btrfs_root *root, struct extent_buffer *leaf, ...@@ -496,7 +498,7 @@ static int check_leaf(struct btrfs_root *root, struct extent_buffer *leaf,
/* Also check if the item pointer overlaps with btrfs item. */ /* Also check if the item pointer overlaps with btrfs item. */
if (btrfs_item_nr_offset(slot) + sizeof(struct btrfs_item) > if (btrfs_item_nr_offset(slot) + sizeof(struct btrfs_item) >
btrfs_item_ptr_offset(leaf, slot)) { btrfs_item_ptr_offset(leaf, slot)) {
generic_err(root, leaf, slot, generic_err(fs_info, leaf, slot,
"slot overlaps with its data, item end %lu data start %lu", "slot overlaps with its data, item end %lu data start %lu",
btrfs_item_nr_offset(slot) + btrfs_item_nr_offset(slot) +
sizeof(struct btrfs_item), sizeof(struct btrfs_item),
...@@ -509,7 +511,7 @@ static int check_leaf(struct btrfs_root *root, struct extent_buffer *leaf, ...@@ -509,7 +511,7 @@ static int check_leaf(struct btrfs_root *root, struct extent_buffer *leaf,
* Check if the item size and content meet other * Check if the item size and content meet other
* criteria * criteria
*/ */
ret = check_leaf_item(root, leaf, &key, slot); ret = check_leaf_item(fs_info, leaf, &key, slot);
if (ret < 0) if (ret < 0)
return ret; return ret;
} }
...@@ -522,18 +524,19 @@ static int check_leaf(struct btrfs_root *root, struct extent_buffer *leaf, ...@@ -522,18 +524,19 @@ static int check_leaf(struct btrfs_root *root, struct extent_buffer *leaf,
return 0; return 0;
} }
int btrfs_check_leaf_full(struct btrfs_root *root, struct extent_buffer *leaf) int btrfs_check_leaf_full(struct btrfs_fs_info *fs_info,
struct extent_buffer *leaf)
{ {
return check_leaf(root, leaf, true); return check_leaf(fs_info, leaf, true);
} }
int btrfs_check_leaf_relaxed(struct btrfs_root *root, int btrfs_check_leaf_relaxed(struct btrfs_fs_info *fs_info,
struct extent_buffer *leaf) struct extent_buffer *leaf)
{ {
return check_leaf(root, leaf, false); return check_leaf(fs_info, leaf, false);
} }
int btrfs_check_node(struct btrfs_root *root, struct extent_buffer *node) int btrfs_check_node(struct btrfs_fs_info *fs_info, struct extent_buffer *node)
{ {
unsigned long nr = btrfs_header_nritems(node); unsigned long nr = btrfs_header_nritems(node);
struct btrfs_key key, next_key; struct btrfs_key key, next_key;
...@@ -541,12 +544,12 @@ int btrfs_check_node(struct btrfs_root *root, struct extent_buffer *node) ...@@ -541,12 +544,12 @@ int btrfs_check_node(struct btrfs_root *root, struct extent_buffer *node)
u64 bytenr; u64 bytenr;
int ret = 0; int ret = 0;
if (nr == 0 || nr > BTRFS_NODEPTRS_PER_BLOCK(root->fs_info)) { if (nr == 0 || nr > BTRFS_NODEPTRS_PER_BLOCK(fs_info)) {
btrfs_crit(root->fs_info, btrfs_crit(fs_info,
"corrupt node: root=%llu block=%llu, nritems too %s, have %lu expect range [1,%u]", "corrupt node: root=%llu block=%llu, nritems too %s, have %lu expect range [1,%u]",
root->objectid, node->start, btrfs_header_owner(node), node->start,
nr == 0 ? "small" : "large", nr, nr == 0 ? "small" : "large", nr,
BTRFS_NODEPTRS_PER_BLOCK(root->fs_info)); BTRFS_NODEPTRS_PER_BLOCK(fs_info));
return -EUCLEAN; return -EUCLEAN;
} }
...@@ -556,21 +559,21 @@ int btrfs_check_node(struct btrfs_root *root, struct extent_buffer *node) ...@@ -556,21 +559,21 @@ int btrfs_check_node(struct btrfs_root *root, struct extent_buffer *node)
btrfs_node_key_to_cpu(node, &next_key, slot + 1); btrfs_node_key_to_cpu(node, &next_key, slot + 1);
if (!bytenr) { if (!bytenr) {
generic_err(root, node, slot, generic_err(fs_info, node, slot,
"invalid NULL node pointer"); "invalid NULL node pointer");
ret = -EUCLEAN; ret = -EUCLEAN;
goto out; goto out;
} }
if (!IS_ALIGNED(bytenr, root->fs_info->sectorsize)) { if (!IS_ALIGNED(bytenr, fs_info->sectorsize)) {
generic_err(root, node, slot, generic_err(fs_info, node, slot,
"unaligned pointer, have %llu should be aligned to %u", "unaligned pointer, have %llu should be aligned to %u",
bytenr, root->fs_info->sectorsize); bytenr, fs_info->sectorsize);
ret = -EUCLEAN; ret = -EUCLEAN;
goto out; goto out;
} }
if (btrfs_comp_cpu_keys(&key, &next_key) >= 0) { if (btrfs_comp_cpu_keys(&key, &next_key) >= 0) {
generic_err(root, node, slot, generic_err(fs_info, node, slot,
"bad key order, current (%llu %u %llu) next (%llu %u %llu)", "bad key order, current (%llu %u %llu) next (%llu %u %llu)",
key.objectid, key.type, key.offset, key.objectid, key.type, key.offset,
next_key.objectid, next_key.type, next_key.objectid, next_key.type,
......
...@@ -25,14 +25,15 @@ ...@@ -25,14 +25,15 @@
* Will check not only the item pointers, but also every possible member * Will check not only the item pointers, but also every possible member
* in item data. * in item data.
*/ */
int btrfs_check_leaf_full(struct btrfs_root *root, struct extent_buffer *leaf); int btrfs_check_leaf_full(struct btrfs_fs_info *fs_info,
struct extent_buffer *leaf);
/* /*
* Less strict leaf checker. * Less strict leaf checker.
* Will only check item pointers, not reading item data. * Will only check item pointers, not reading item data.
*/ */
int btrfs_check_leaf_relaxed(struct btrfs_root *root, int btrfs_check_leaf_relaxed(struct btrfs_fs_info *fs_info,
struct extent_buffer *leaf); struct extent_buffer *leaf);
int btrfs_check_node(struct btrfs_root *root, struct extent_buffer *node); int btrfs_check_node(struct btrfs_fs_info *fs_info, struct extent_buffer *node);
#endif #endif
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