1c1d7c514SDavid Sterba // SPDX-License-Identifier: GPL-2.0 2557ea5ddSQu Wenruo /* 3557ea5ddSQu Wenruo * Copyright (C) Qu Wenruo 2017. All rights reserved. 4557ea5ddSQu Wenruo */ 5557ea5ddSQu Wenruo 6557ea5ddSQu Wenruo /* 7557ea5ddSQu Wenruo * The module is used to catch unexpected/corrupted tree block data. 8557ea5ddSQu Wenruo * Such behavior can be caused either by a fuzzed image or bugs. 9557ea5ddSQu Wenruo * 10557ea5ddSQu Wenruo * The objective is to do leaf/node validation checks when tree block is read 11557ea5ddSQu Wenruo * from disk, and check *every* possible member, so other code won't 12557ea5ddSQu Wenruo * need to checking them again. 13557ea5ddSQu Wenruo * 14557ea5ddSQu Wenruo * Due to the potential and unwanted damage, every checker needs to be 15557ea5ddSQu Wenruo * carefully reviewed otherwise so it does not prevent mount of valid images. 16557ea5ddSQu Wenruo */ 17557ea5ddSQu Wenruo 1802529d7aSQu Wenruo #include <linux/types.h> 1902529d7aSQu Wenruo #include <linux/stddef.h> 2002529d7aSQu Wenruo #include <linux/error-injection.h> 21557ea5ddSQu Wenruo #include "ctree.h" 22557ea5ddSQu Wenruo #include "tree-checker.h" 23557ea5ddSQu Wenruo #include "disk-io.h" 24557ea5ddSQu Wenruo #include "compression.h" 25fce466eaSQu Wenruo #include "volumes.h" 26c1499166SDavid Sterba #include "misc.h" 27557ea5ddSQu Wenruo 28bba4f298SQu Wenruo /* 29bba4f298SQu Wenruo * Error message should follow the following format: 30bba4f298SQu Wenruo * corrupt <type>: <identifier>, <reason>[, <bad_value>] 31bba4f298SQu Wenruo * 32bba4f298SQu Wenruo * @type: leaf or node 33bba4f298SQu Wenruo * @identifier: the necessary info to locate the leaf/node. 3452042d8eSAndrea Gelmini * It's recommended to decode key.objecitd/offset if it's 35bba4f298SQu Wenruo * meaningful. 36bba4f298SQu Wenruo * @reason: describe the error 3752042d8eSAndrea Gelmini * @bad_value: optional, it's recommended to output bad value and its 38bba4f298SQu Wenruo * expected value (range). 39bba4f298SQu Wenruo * 40bba4f298SQu Wenruo * Since comma is used to separate the components, only space is allowed 41bba4f298SQu Wenruo * inside each component. 42bba4f298SQu Wenruo */ 43bba4f298SQu Wenruo 44bba4f298SQu Wenruo /* 45bba4f298SQu Wenruo * Append generic "corrupt leaf/node root=%llu block=%llu slot=%d: " to @fmt. 46bba4f298SQu Wenruo * Allows callers to customize the output. 47bba4f298SQu Wenruo */ 4886a6be3aSDavid Sterba __printf(3, 4) 49e67c718bSDavid Sterba __cold 5086a6be3aSDavid Sterba static void generic_err(const struct extent_buffer *eb, int slot, 51bba4f298SQu Wenruo const char *fmt, ...) 52bba4f298SQu Wenruo { 5386a6be3aSDavid Sterba const struct btrfs_fs_info *fs_info = eb->fs_info; 54bba4f298SQu Wenruo struct va_format vaf; 55bba4f298SQu Wenruo va_list args; 56bba4f298SQu Wenruo 57bba4f298SQu Wenruo va_start(args, fmt); 58bba4f298SQu Wenruo 59bba4f298SQu Wenruo vaf.fmt = fmt; 60bba4f298SQu Wenruo vaf.va = &args; 61bba4f298SQu Wenruo 622f659546SQu Wenruo btrfs_crit(fs_info, 63bba4f298SQu Wenruo "corrupt %s: root=%llu block=%llu slot=%d, %pV", 64bba4f298SQu Wenruo btrfs_header_level(eb) == 0 ? "leaf" : "node", 652f659546SQu Wenruo btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot, &vaf); 66bba4f298SQu Wenruo va_end(args); 67bba4f298SQu Wenruo } 68bba4f298SQu Wenruo 698806d718SQu Wenruo /* 708806d718SQu Wenruo * Customized reporter for extent data item, since its key objectid and 718806d718SQu Wenruo * offset has its own meaning. 728806d718SQu Wenruo */ 731fd715ffSDavid Sterba __printf(3, 4) 74e67c718bSDavid Sterba __cold 751fd715ffSDavid Sterba static void file_extent_err(const struct extent_buffer *eb, int slot, 768806d718SQu Wenruo const char *fmt, ...) 778806d718SQu Wenruo { 781fd715ffSDavid Sterba const struct btrfs_fs_info *fs_info = eb->fs_info; 798806d718SQu Wenruo struct btrfs_key key; 808806d718SQu Wenruo struct va_format vaf; 818806d718SQu Wenruo va_list args; 828806d718SQu Wenruo 838806d718SQu Wenruo btrfs_item_key_to_cpu(eb, &key, slot); 848806d718SQu Wenruo va_start(args, fmt); 858806d718SQu Wenruo 868806d718SQu Wenruo vaf.fmt = fmt; 878806d718SQu Wenruo vaf.va = &args; 888806d718SQu Wenruo 892f659546SQu Wenruo btrfs_crit(fs_info, 908806d718SQu Wenruo "corrupt %s: root=%llu block=%llu slot=%d ino=%llu file_offset=%llu, %pV", 912f659546SQu Wenruo btrfs_header_level(eb) == 0 ? "leaf" : "node", 922f659546SQu Wenruo btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot, 932f659546SQu Wenruo key.objectid, key.offset, &vaf); 948806d718SQu Wenruo va_end(args); 958806d718SQu Wenruo } 968806d718SQu Wenruo 978806d718SQu Wenruo /* 988806d718SQu Wenruo * Return 0 if the btrfs_file_extent_##name is aligned to @alignment 998806d718SQu Wenruo * Else return 1 1008806d718SQu Wenruo */ 101033774dcSDavid Sterba #define CHECK_FE_ALIGNED(leaf, slot, fi, name, alignment) \ 1028806d718SQu Wenruo ({ \ 1038806d718SQu Wenruo if (!IS_ALIGNED(btrfs_file_extent_##name((leaf), (fi)), (alignment))) \ 1041fd715ffSDavid Sterba file_extent_err((leaf), (slot), \ 1058806d718SQu Wenruo "invalid %s for file extent, have %llu, should be aligned to %u", \ 1068806d718SQu Wenruo (#name), btrfs_file_extent_##name((leaf), (fi)), \ 1078806d718SQu Wenruo (alignment)); \ 1088806d718SQu Wenruo (!IS_ALIGNED(btrfs_file_extent_##name((leaf), (fi)), (alignment))); \ 1098806d718SQu Wenruo }) 1108806d718SQu Wenruo 1114e9845efSFilipe Manana static u64 file_extent_end(struct extent_buffer *leaf, 1124e9845efSFilipe Manana struct btrfs_key *key, 1134e9845efSFilipe Manana struct btrfs_file_extent_item *extent) 1144e9845efSFilipe Manana { 1154e9845efSFilipe Manana u64 end; 1164e9845efSFilipe Manana u64 len; 1174e9845efSFilipe Manana 1184e9845efSFilipe Manana if (btrfs_file_extent_type(leaf, extent) == BTRFS_FILE_EXTENT_INLINE) { 1194e9845efSFilipe Manana len = btrfs_file_extent_ram_bytes(leaf, extent); 1204e9845efSFilipe Manana end = ALIGN(key->offset + len, leaf->fs_info->sectorsize); 1214e9845efSFilipe Manana } else { 1224e9845efSFilipe Manana len = btrfs_file_extent_num_bytes(leaf, extent); 1234e9845efSFilipe Manana end = key->offset + len; 1244e9845efSFilipe Manana } 1254e9845efSFilipe Manana return end; 1264e9845efSFilipe Manana } 1274e9845efSFilipe Manana 12880d7fd1eSQu Wenruo /* 12980d7fd1eSQu Wenruo * Customized report for dir_item, the only new important information is 13080d7fd1eSQu Wenruo * key->objectid, which represents inode number 13180d7fd1eSQu Wenruo */ 13280d7fd1eSQu Wenruo __printf(3, 4) 13380d7fd1eSQu Wenruo __cold 13480d7fd1eSQu Wenruo static void dir_item_err(const struct extent_buffer *eb, int slot, 13580d7fd1eSQu Wenruo const char *fmt, ...) 13680d7fd1eSQu Wenruo { 13780d7fd1eSQu Wenruo const struct btrfs_fs_info *fs_info = eb->fs_info; 13880d7fd1eSQu Wenruo struct btrfs_key key; 13980d7fd1eSQu Wenruo struct va_format vaf; 14080d7fd1eSQu Wenruo va_list args; 14180d7fd1eSQu Wenruo 14280d7fd1eSQu Wenruo btrfs_item_key_to_cpu(eb, &key, slot); 14380d7fd1eSQu Wenruo va_start(args, fmt); 14480d7fd1eSQu Wenruo 14580d7fd1eSQu Wenruo vaf.fmt = fmt; 14680d7fd1eSQu Wenruo vaf.va = &args; 14780d7fd1eSQu Wenruo 14880d7fd1eSQu Wenruo btrfs_crit(fs_info, 14980d7fd1eSQu Wenruo "corrupt %s: root=%llu block=%llu slot=%d ino=%llu, %pV", 15080d7fd1eSQu Wenruo btrfs_header_level(eb) == 0 ? "leaf" : "node", 15180d7fd1eSQu Wenruo btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot, 15280d7fd1eSQu Wenruo key.objectid, &vaf); 15380d7fd1eSQu Wenruo va_end(args); 15480d7fd1eSQu Wenruo } 15580d7fd1eSQu Wenruo 15680d7fd1eSQu Wenruo /* 15780d7fd1eSQu Wenruo * This functions checks prev_key->objectid, to ensure current key and prev_key 15880d7fd1eSQu Wenruo * share the same objectid as inode number. 15980d7fd1eSQu Wenruo * 16080d7fd1eSQu Wenruo * This is to detect missing INODE_ITEM in subvolume trees. 16180d7fd1eSQu Wenruo * 16280d7fd1eSQu Wenruo * Return true if everything is OK or we don't need to check. 16380d7fd1eSQu Wenruo * Return false if anything is wrong. 16480d7fd1eSQu Wenruo */ 16580d7fd1eSQu Wenruo static bool check_prev_ino(struct extent_buffer *leaf, 16680d7fd1eSQu Wenruo struct btrfs_key *key, int slot, 16780d7fd1eSQu Wenruo struct btrfs_key *prev_key) 16880d7fd1eSQu Wenruo { 16980d7fd1eSQu Wenruo /* No prev key, skip check */ 17080d7fd1eSQu Wenruo if (slot == 0) 17180d7fd1eSQu Wenruo return true; 17280d7fd1eSQu Wenruo 17380d7fd1eSQu Wenruo /* Only these key->types needs to be checked */ 17480d7fd1eSQu Wenruo ASSERT(key->type == BTRFS_XATTR_ITEM_KEY || 17580d7fd1eSQu Wenruo key->type == BTRFS_INODE_REF_KEY || 17680d7fd1eSQu Wenruo key->type == BTRFS_DIR_INDEX_KEY || 17780d7fd1eSQu Wenruo key->type == BTRFS_DIR_ITEM_KEY || 17880d7fd1eSQu Wenruo key->type == BTRFS_EXTENT_DATA_KEY); 17980d7fd1eSQu Wenruo 18080d7fd1eSQu Wenruo /* 18180d7fd1eSQu Wenruo * Only subvolume trees along with their reloc trees need this check. 18280d7fd1eSQu Wenruo * Things like log tree doesn't follow this ino requirement. 18380d7fd1eSQu Wenruo */ 18480d7fd1eSQu Wenruo if (!is_fstree(btrfs_header_owner(leaf))) 18580d7fd1eSQu Wenruo return true; 18680d7fd1eSQu Wenruo 18780d7fd1eSQu Wenruo if (key->objectid == prev_key->objectid) 18880d7fd1eSQu Wenruo return true; 18980d7fd1eSQu Wenruo 19080d7fd1eSQu Wenruo /* Error found */ 19180d7fd1eSQu Wenruo dir_item_err(leaf, slot, 19280d7fd1eSQu Wenruo "invalid previous key objectid, have %llu expect %llu", 19380d7fd1eSQu Wenruo prev_key->objectid, key->objectid); 19480d7fd1eSQu Wenruo return false; 19580d7fd1eSQu Wenruo } 196ae2a19d8SDavid Sterba static int check_extent_data_item(struct extent_buffer *leaf, 1974e9845efSFilipe Manana struct btrfs_key *key, int slot, 1984e9845efSFilipe Manana struct btrfs_key *prev_key) 199557ea5ddSQu Wenruo { 200ae2a19d8SDavid Sterba struct btrfs_fs_info *fs_info = leaf->fs_info; 201557ea5ddSQu Wenruo struct btrfs_file_extent_item *fi; 2022f659546SQu Wenruo u32 sectorsize = fs_info->sectorsize; 203557ea5ddSQu Wenruo u32 item_size = btrfs_item_size_nr(leaf, slot); 2044c094c33SQu Wenruo u64 extent_end; 205557ea5ddSQu Wenruo 206557ea5ddSQu Wenruo if (!IS_ALIGNED(key->offset, sectorsize)) { 2071fd715ffSDavid Sterba file_extent_err(leaf, slot, 2088806d718SQu Wenruo "unaligned file_offset for file extent, have %llu should be aligned to %u", 2098806d718SQu Wenruo key->offset, sectorsize); 210557ea5ddSQu Wenruo return -EUCLEAN; 211557ea5ddSQu Wenruo } 212557ea5ddSQu Wenruo 213c18679ebSQu Wenruo /* 214c18679ebSQu Wenruo * Previous key must have the same key->objectid (ino). 215c18679ebSQu Wenruo * It can be XATTR_ITEM, INODE_ITEM or just another EXTENT_DATA. 216c18679ebSQu Wenruo * But if objectids mismatch, it means we have a missing 217c18679ebSQu Wenruo * INODE_ITEM. 218c18679ebSQu Wenruo */ 21980d7fd1eSQu Wenruo if (!check_prev_ino(leaf, key, slot, prev_key)) 220c18679ebSQu Wenruo return -EUCLEAN; 221c18679ebSQu Wenruo 222557ea5ddSQu Wenruo fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); 223557ea5ddSQu Wenruo 224b9b1a53eSChengguang Xu if (btrfs_file_extent_type(leaf, fi) >= BTRFS_NR_FILE_EXTENT_TYPES) { 2251fd715ffSDavid Sterba file_extent_err(leaf, slot, 2268806d718SQu Wenruo "invalid type for file extent, have %u expect range [0, %u]", 2278806d718SQu Wenruo btrfs_file_extent_type(leaf, fi), 228b9b1a53eSChengguang Xu BTRFS_NR_FILE_EXTENT_TYPES - 1); 229557ea5ddSQu Wenruo return -EUCLEAN; 230557ea5ddSQu Wenruo } 231557ea5ddSQu Wenruo 232557ea5ddSQu Wenruo /* 23352042d8eSAndrea Gelmini * Support for new compression/encryption must introduce incompat flag, 234557ea5ddSQu Wenruo * and must be caught in open_ctree(). 235557ea5ddSQu Wenruo */ 236*ce96b7ffSChengguang Xu if (btrfs_file_extent_compression(leaf, fi) >= BTRFS_NR_COMPRESS_TYPES) { 2371fd715ffSDavid Sterba file_extent_err(leaf, slot, 2388806d718SQu Wenruo "invalid compression for file extent, have %u expect range [0, %u]", 2398806d718SQu Wenruo btrfs_file_extent_compression(leaf, fi), 240*ce96b7ffSChengguang Xu BTRFS_NR_COMPRESS_TYPES - 1); 241557ea5ddSQu Wenruo return -EUCLEAN; 242557ea5ddSQu Wenruo } 243557ea5ddSQu Wenruo if (btrfs_file_extent_encryption(leaf, fi)) { 2441fd715ffSDavid Sterba file_extent_err(leaf, slot, 2458806d718SQu Wenruo "invalid encryption for file extent, have %u expect 0", 2468806d718SQu Wenruo btrfs_file_extent_encryption(leaf, fi)); 247557ea5ddSQu Wenruo return -EUCLEAN; 248557ea5ddSQu Wenruo } 249557ea5ddSQu Wenruo if (btrfs_file_extent_type(leaf, fi) == BTRFS_FILE_EXTENT_INLINE) { 250557ea5ddSQu Wenruo /* Inline extent must have 0 as key offset */ 251557ea5ddSQu Wenruo if (key->offset) { 2521fd715ffSDavid Sterba file_extent_err(leaf, slot, 2538806d718SQu Wenruo "invalid file_offset for inline file extent, have %llu expect 0", 2548806d718SQu Wenruo key->offset); 255557ea5ddSQu Wenruo return -EUCLEAN; 256557ea5ddSQu Wenruo } 257557ea5ddSQu Wenruo 258557ea5ddSQu Wenruo /* Compressed inline extent has no on-disk size, skip it */ 259557ea5ddSQu Wenruo if (btrfs_file_extent_compression(leaf, fi) != 260557ea5ddSQu Wenruo BTRFS_COMPRESS_NONE) 261557ea5ddSQu Wenruo return 0; 262557ea5ddSQu Wenruo 263557ea5ddSQu Wenruo /* Uncompressed inline extent size must match item size */ 264557ea5ddSQu Wenruo if (item_size != BTRFS_FILE_EXTENT_INLINE_DATA_START + 265557ea5ddSQu Wenruo btrfs_file_extent_ram_bytes(leaf, fi)) { 2661fd715ffSDavid Sterba file_extent_err(leaf, slot, 2678806d718SQu Wenruo "invalid ram_bytes for uncompressed inline extent, have %u expect %llu", 2688806d718SQu Wenruo item_size, BTRFS_FILE_EXTENT_INLINE_DATA_START + 2698806d718SQu Wenruo btrfs_file_extent_ram_bytes(leaf, fi)); 270557ea5ddSQu Wenruo return -EUCLEAN; 271557ea5ddSQu Wenruo } 272557ea5ddSQu Wenruo return 0; 273557ea5ddSQu Wenruo } 274557ea5ddSQu Wenruo 275557ea5ddSQu Wenruo /* Regular or preallocated extent has fixed item size */ 276557ea5ddSQu Wenruo if (item_size != sizeof(*fi)) { 2771fd715ffSDavid Sterba file_extent_err(leaf, slot, 278709a95c3SArnd Bergmann "invalid item size for reg/prealloc file extent, have %u expect %zu", 2798806d718SQu Wenruo item_size, sizeof(*fi)); 280557ea5ddSQu Wenruo return -EUCLEAN; 281557ea5ddSQu Wenruo } 282033774dcSDavid Sterba if (CHECK_FE_ALIGNED(leaf, slot, fi, ram_bytes, sectorsize) || 283033774dcSDavid Sterba CHECK_FE_ALIGNED(leaf, slot, fi, disk_bytenr, sectorsize) || 284033774dcSDavid Sterba CHECK_FE_ALIGNED(leaf, slot, fi, disk_num_bytes, sectorsize) || 285033774dcSDavid Sterba CHECK_FE_ALIGNED(leaf, slot, fi, offset, sectorsize) || 286033774dcSDavid Sterba CHECK_FE_ALIGNED(leaf, slot, fi, num_bytes, sectorsize)) 287557ea5ddSQu Wenruo return -EUCLEAN; 2884e9845efSFilipe Manana 2894c094c33SQu Wenruo /* Catch extent end overflow */ 2904c094c33SQu Wenruo if (check_add_overflow(btrfs_file_extent_num_bytes(leaf, fi), 2914c094c33SQu Wenruo key->offset, &extent_end)) { 2924c094c33SQu Wenruo file_extent_err(leaf, slot, 2934c094c33SQu Wenruo "extent end overflow, have file offset %llu extent num bytes %llu", 2944c094c33SQu Wenruo key->offset, 2954c094c33SQu Wenruo btrfs_file_extent_num_bytes(leaf, fi)); 2964c094c33SQu Wenruo return -EUCLEAN; 2974c094c33SQu Wenruo } 2984c094c33SQu Wenruo 2994e9845efSFilipe Manana /* 3004e9845efSFilipe Manana * Check that no two consecutive file extent items, in the same leaf, 3014e9845efSFilipe Manana * present ranges that overlap each other. 3024e9845efSFilipe Manana */ 3034e9845efSFilipe Manana if (slot > 0 && 3044e9845efSFilipe Manana prev_key->objectid == key->objectid && 3054e9845efSFilipe Manana prev_key->type == BTRFS_EXTENT_DATA_KEY) { 3064e9845efSFilipe Manana struct btrfs_file_extent_item *prev_fi; 3074e9845efSFilipe Manana u64 prev_end; 3084e9845efSFilipe Manana 3094e9845efSFilipe Manana prev_fi = btrfs_item_ptr(leaf, slot - 1, 3104e9845efSFilipe Manana struct btrfs_file_extent_item); 3114e9845efSFilipe Manana prev_end = file_extent_end(leaf, prev_key, prev_fi); 3124e9845efSFilipe Manana if (prev_end > key->offset) { 3134e9845efSFilipe Manana file_extent_err(leaf, slot - 1, 3144e9845efSFilipe Manana "file extent end range (%llu) goes beyond start offset (%llu) of the next file extent", 3154e9845efSFilipe Manana prev_end, key->offset); 3164e9845efSFilipe Manana return -EUCLEAN; 3174e9845efSFilipe Manana } 3184e9845efSFilipe Manana } 3194e9845efSFilipe Manana 320557ea5ddSQu Wenruo return 0; 321557ea5ddSQu Wenruo } 322557ea5ddSQu Wenruo 32368128ce7SDavid Sterba static int check_csum_item(struct extent_buffer *leaf, struct btrfs_key *key, 3242f659546SQu Wenruo int slot) 325557ea5ddSQu Wenruo { 32668128ce7SDavid Sterba struct btrfs_fs_info *fs_info = leaf->fs_info; 3272f659546SQu Wenruo u32 sectorsize = fs_info->sectorsize; 3282f659546SQu Wenruo u32 csumsize = btrfs_super_csum_size(fs_info->super_copy); 329557ea5ddSQu Wenruo 330557ea5ddSQu Wenruo if (key->objectid != BTRFS_EXTENT_CSUM_OBJECTID) { 33186a6be3aSDavid Sterba generic_err(leaf, slot, 332d508c5f0SQu Wenruo "invalid key objectid for csum item, have %llu expect %llu", 333d508c5f0SQu Wenruo key->objectid, BTRFS_EXTENT_CSUM_OBJECTID); 334557ea5ddSQu Wenruo return -EUCLEAN; 335557ea5ddSQu Wenruo } 336557ea5ddSQu Wenruo if (!IS_ALIGNED(key->offset, sectorsize)) { 33786a6be3aSDavid Sterba generic_err(leaf, slot, 338d508c5f0SQu Wenruo "unaligned key offset for csum item, have %llu should be aligned to %u", 339d508c5f0SQu Wenruo key->offset, sectorsize); 340557ea5ddSQu Wenruo return -EUCLEAN; 341557ea5ddSQu Wenruo } 342557ea5ddSQu Wenruo if (!IS_ALIGNED(btrfs_item_size_nr(leaf, slot), csumsize)) { 34386a6be3aSDavid Sterba generic_err(leaf, slot, 344d508c5f0SQu Wenruo "unaligned item size for csum item, have %u should be aligned to %u", 345d508c5f0SQu Wenruo btrfs_item_size_nr(leaf, slot), csumsize); 346557ea5ddSQu Wenruo return -EUCLEAN; 347557ea5ddSQu Wenruo } 348557ea5ddSQu Wenruo return 0; 349557ea5ddSQu Wenruo } 350557ea5ddSQu Wenruo 351ce4252c0SDavid Sterba static int check_dir_item(struct extent_buffer *leaf, 352c18679ebSQu Wenruo struct btrfs_key *key, struct btrfs_key *prev_key, 353c18679ebSQu Wenruo int slot) 354ad7b0368SQu Wenruo { 355ce4252c0SDavid Sterba struct btrfs_fs_info *fs_info = leaf->fs_info; 356ad7b0368SQu Wenruo struct btrfs_dir_item *di; 357ad7b0368SQu Wenruo u32 item_size = btrfs_item_size_nr(leaf, slot); 358ad7b0368SQu Wenruo u32 cur = 0; 359ad7b0368SQu Wenruo 36080d7fd1eSQu Wenruo if (!check_prev_ino(leaf, key, slot, prev_key)) 361c18679ebSQu Wenruo return -EUCLEAN; 362ad7b0368SQu Wenruo di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); 363ad7b0368SQu Wenruo while (cur < item_size) { 364ad7b0368SQu Wenruo u32 name_len; 365ad7b0368SQu Wenruo u32 data_len; 366ad7b0368SQu Wenruo u32 max_name_len; 367ad7b0368SQu Wenruo u32 total_size; 368ad7b0368SQu Wenruo u32 name_hash; 369ad7b0368SQu Wenruo u8 dir_type; 370ad7b0368SQu Wenruo 371ad7b0368SQu Wenruo /* header itself should not cross item boundary */ 372ad7b0368SQu Wenruo if (cur + sizeof(*di) > item_size) { 373d98ced68SDavid Sterba dir_item_err(leaf, slot, 3747cfad652SArnd Bergmann "dir item header crosses item boundary, have %zu boundary %u", 375ad7b0368SQu Wenruo cur + sizeof(*di), item_size); 376ad7b0368SQu Wenruo return -EUCLEAN; 377ad7b0368SQu Wenruo } 378ad7b0368SQu Wenruo 379ad7b0368SQu Wenruo /* dir type check */ 380ad7b0368SQu Wenruo dir_type = btrfs_dir_type(leaf, di); 381ad7b0368SQu Wenruo if (dir_type >= BTRFS_FT_MAX) { 382d98ced68SDavid Sterba dir_item_err(leaf, slot, 383ad7b0368SQu Wenruo "invalid dir item type, have %u expect [0, %u)", 384ad7b0368SQu Wenruo dir_type, BTRFS_FT_MAX); 385ad7b0368SQu Wenruo return -EUCLEAN; 386ad7b0368SQu Wenruo } 387ad7b0368SQu Wenruo 388ad7b0368SQu Wenruo if (key->type == BTRFS_XATTR_ITEM_KEY && 389ad7b0368SQu Wenruo dir_type != BTRFS_FT_XATTR) { 390d98ced68SDavid Sterba dir_item_err(leaf, slot, 391ad7b0368SQu Wenruo "invalid dir item type for XATTR key, have %u expect %u", 392ad7b0368SQu Wenruo dir_type, BTRFS_FT_XATTR); 393ad7b0368SQu Wenruo return -EUCLEAN; 394ad7b0368SQu Wenruo } 395ad7b0368SQu Wenruo if (dir_type == BTRFS_FT_XATTR && 396ad7b0368SQu Wenruo key->type != BTRFS_XATTR_ITEM_KEY) { 397d98ced68SDavid Sterba dir_item_err(leaf, slot, 398ad7b0368SQu Wenruo "xattr dir type found for non-XATTR key"); 399ad7b0368SQu Wenruo return -EUCLEAN; 400ad7b0368SQu Wenruo } 401ad7b0368SQu Wenruo if (dir_type == BTRFS_FT_XATTR) 402ad7b0368SQu Wenruo max_name_len = XATTR_NAME_MAX; 403ad7b0368SQu Wenruo else 404ad7b0368SQu Wenruo max_name_len = BTRFS_NAME_LEN; 405ad7b0368SQu Wenruo 406ad7b0368SQu Wenruo /* Name/data length check */ 407ad7b0368SQu Wenruo name_len = btrfs_dir_name_len(leaf, di); 408ad7b0368SQu Wenruo data_len = btrfs_dir_data_len(leaf, di); 409ad7b0368SQu Wenruo if (name_len > max_name_len) { 410d98ced68SDavid Sterba dir_item_err(leaf, slot, 411ad7b0368SQu Wenruo "dir item name len too long, have %u max %u", 412ad7b0368SQu Wenruo name_len, max_name_len); 413ad7b0368SQu Wenruo return -EUCLEAN; 414ad7b0368SQu Wenruo } 4152f659546SQu Wenruo if (name_len + data_len > BTRFS_MAX_XATTR_SIZE(fs_info)) { 416d98ced68SDavid Sterba dir_item_err(leaf, slot, 417ad7b0368SQu Wenruo "dir item name and data len too long, have %u max %u", 418ad7b0368SQu Wenruo name_len + data_len, 4192f659546SQu Wenruo BTRFS_MAX_XATTR_SIZE(fs_info)); 420ad7b0368SQu Wenruo return -EUCLEAN; 421ad7b0368SQu Wenruo } 422ad7b0368SQu Wenruo 423ad7b0368SQu Wenruo if (data_len && dir_type != BTRFS_FT_XATTR) { 424d98ced68SDavid Sterba dir_item_err(leaf, slot, 425ad7b0368SQu Wenruo "dir item with invalid data len, have %u expect 0", 426ad7b0368SQu Wenruo data_len); 427ad7b0368SQu Wenruo return -EUCLEAN; 428ad7b0368SQu Wenruo } 429ad7b0368SQu Wenruo 430ad7b0368SQu Wenruo total_size = sizeof(*di) + name_len + data_len; 431ad7b0368SQu Wenruo 432ad7b0368SQu Wenruo /* header and name/data should not cross item boundary */ 433ad7b0368SQu Wenruo if (cur + total_size > item_size) { 434d98ced68SDavid Sterba dir_item_err(leaf, slot, 435ad7b0368SQu Wenruo "dir item data crosses item boundary, have %u boundary %u", 436ad7b0368SQu Wenruo cur + total_size, item_size); 437ad7b0368SQu Wenruo return -EUCLEAN; 438ad7b0368SQu Wenruo } 439ad7b0368SQu Wenruo 440ad7b0368SQu Wenruo /* 441ad7b0368SQu Wenruo * Special check for XATTR/DIR_ITEM, as key->offset is name 442ad7b0368SQu Wenruo * hash, should match its name 443ad7b0368SQu Wenruo */ 444ad7b0368SQu Wenruo if (key->type == BTRFS_DIR_ITEM_KEY || 445ad7b0368SQu Wenruo key->type == BTRFS_XATTR_ITEM_KEY) { 446e2683fc9SDavid Sterba char namebuf[max(BTRFS_NAME_LEN, XATTR_NAME_MAX)]; 447e2683fc9SDavid Sterba 448ad7b0368SQu Wenruo read_extent_buffer(leaf, namebuf, 449ad7b0368SQu Wenruo (unsigned long)(di + 1), name_len); 450ad7b0368SQu Wenruo name_hash = btrfs_name_hash(namebuf, name_len); 451ad7b0368SQu Wenruo if (key->offset != name_hash) { 452d98ced68SDavid Sterba dir_item_err(leaf, slot, 453ad7b0368SQu Wenruo "name hash mismatch with key, have 0x%016x expect 0x%016llx", 454ad7b0368SQu Wenruo name_hash, key->offset); 455ad7b0368SQu Wenruo return -EUCLEAN; 456ad7b0368SQu Wenruo } 457ad7b0368SQu Wenruo } 458ad7b0368SQu Wenruo cur += total_size; 459ad7b0368SQu Wenruo di = (struct btrfs_dir_item *)((void *)di + total_size); 460ad7b0368SQu Wenruo } 461ad7b0368SQu Wenruo return 0; 462ad7b0368SQu Wenruo } 463ad7b0368SQu Wenruo 4644806bd88SDavid Sterba __printf(3, 4) 465fce466eaSQu Wenruo __cold 4664806bd88SDavid Sterba static void block_group_err(const struct extent_buffer *eb, int slot, 467fce466eaSQu Wenruo const char *fmt, ...) 468fce466eaSQu Wenruo { 4694806bd88SDavid Sterba const struct btrfs_fs_info *fs_info = eb->fs_info; 470fce466eaSQu Wenruo struct btrfs_key key; 471fce466eaSQu Wenruo struct va_format vaf; 472fce466eaSQu Wenruo va_list args; 473fce466eaSQu Wenruo 474fce466eaSQu Wenruo btrfs_item_key_to_cpu(eb, &key, slot); 475fce466eaSQu Wenruo va_start(args, fmt); 476fce466eaSQu Wenruo 477fce466eaSQu Wenruo vaf.fmt = fmt; 478fce466eaSQu Wenruo vaf.va = &args; 479fce466eaSQu Wenruo 480fce466eaSQu Wenruo btrfs_crit(fs_info, 481fce466eaSQu Wenruo "corrupt %s: root=%llu block=%llu slot=%d bg_start=%llu bg_len=%llu, %pV", 482fce466eaSQu Wenruo btrfs_header_level(eb) == 0 ? "leaf" : "node", 483fce466eaSQu Wenruo btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot, 484fce466eaSQu Wenruo key.objectid, key.offset, &vaf); 485fce466eaSQu Wenruo va_end(args); 486fce466eaSQu Wenruo } 487fce466eaSQu Wenruo 488af60ce2bSDavid Sterba static int check_block_group_item(struct extent_buffer *leaf, 489fce466eaSQu Wenruo struct btrfs_key *key, int slot) 490fce466eaSQu Wenruo { 491fce466eaSQu Wenruo struct btrfs_block_group_item bgi; 492fce466eaSQu Wenruo u32 item_size = btrfs_item_size_nr(leaf, slot); 493fce466eaSQu Wenruo u64 flags; 494fce466eaSQu Wenruo u64 type; 495fce466eaSQu Wenruo 496fce466eaSQu Wenruo /* 497fce466eaSQu Wenruo * Here we don't really care about alignment since extent allocator can 49810950929SQu Wenruo * handle it. We care more about the size. 499fce466eaSQu Wenruo */ 50010950929SQu Wenruo if (key->offset == 0) { 5014806bd88SDavid Sterba block_group_err(leaf, slot, 50210950929SQu Wenruo "invalid block group size 0"); 503fce466eaSQu Wenruo return -EUCLEAN; 504fce466eaSQu Wenruo } 505fce466eaSQu Wenruo 506fce466eaSQu Wenruo if (item_size != sizeof(bgi)) { 5074806bd88SDavid Sterba block_group_err(leaf, slot, 508fce466eaSQu Wenruo "invalid item size, have %u expect %zu", 509fce466eaSQu Wenruo item_size, sizeof(bgi)); 510fce466eaSQu Wenruo return -EUCLEAN; 511fce466eaSQu Wenruo } 512fce466eaSQu Wenruo 513fce466eaSQu Wenruo read_extent_buffer(leaf, &bgi, btrfs_item_ptr_offset(leaf, slot), 514fce466eaSQu Wenruo sizeof(bgi)); 515fce466eaSQu Wenruo if (btrfs_block_group_chunk_objectid(&bgi) != 516fce466eaSQu Wenruo BTRFS_FIRST_CHUNK_TREE_OBJECTID) { 5174806bd88SDavid Sterba block_group_err(leaf, slot, 518fce466eaSQu Wenruo "invalid block group chunk objectid, have %llu expect %llu", 519fce466eaSQu Wenruo btrfs_block_group_chunk_objectid(&bgi), 520fce466eaSQu Wenruo BTRFS_FIRST_CHUNK_TREE_OBJECTID); 521fce466eaSQu Wenruo return -EUCLEAN; 522fce466eaSQu Wenruo } 523fce466eaSQu Wenruo 524fce466eaSQu Wenruo if (btrfs_block_group_used(&bgi) > key->offset) { 5254806bd88SDavid Sterba block_group_err(leaf, slot, 526fce466eaSQu Wenruo "invalid block group used, have %llu expect [0, %llu)", 527fce466eaSQu Wenruo btrfs_block_group_used(&bgi), key->offset); 528fce466eaSQu Wenruo return -EUCLEAN; 529fce466eaSQu Wenruo } 530fce466eaSQu Wenruo 531fce466eaSQu Wenruo flags = btrfs_block_group_flags(&bgi); 532fce466eaSQu Wenruo if (hweight64(flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) > 1) { 5334806bd88SDavid Sterba block_group_err(leaf, slot, 534fce466eaSQu Wenruo "invalid profile flags, have 0x%llx (%lu bits set) expect no more than 1 bit set", 535fce466eaSQu Wenruo flags & BTRFS_BLOCK_GROUP_PROFILE_MASK, 536fce466eaSQu Wenruo hweight64(flags & BTRFS_BLOCK_GROUP_PROFILE_MASK)); 537fce466eaSQu Wenruo return -EUCLEAN; 538fce466eaSQu Wenruo } 539fce466eaSQu Wenruo 540fce466eaSQu Wenruo type = flags & BTRFS_BLOCK_GROUP_TYPE_MASK; 541fce466eaSQu Wenruo if (type != BTRFS_BLOCK_GROUP_DATA && 542fce466eaSQu Wenruo type != BTRFS_BLOCK_GROUP_METADATA && 543fce466eaSQu Wenruo type != BTRFS_BLOCK_GROUP_SYSTEM && 544fce466eaSQu Wenruo type != (BTRFS_BLOCK_GROUP_METADATA | 545fce466eaSQu Wenruo BTRFS_BLOCK_GROUP_DATA)) { 5464806bd88SDavid Sterba block_group_err(leaf, slot, 547761333f2SShaokun Zhang "invalid type, have 0x%llx (%lu bits set) expect either 0x%llx, 0x%llx, 0x%llx or 0x%llx", 548fce466eaSQu Wenruo type, hweight64(type), 549fce466eaSQu Wenruo BTRFS_BLOCK_GROUP_DATA, BTRFS_BLOCK_GROUP_METADATA, 550fce466eaSQu Wenruo BTRFS_BLOCK_GROUP_SYSTEM, 551fce466eaSQu Wenruo BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA); 552fce466eaSQu Wenruo return -EUCLEAN; 553fce466eaSQu Wenruo } 554fce466eaSQu Wenruo return 0; 555fce466eaSQu Wenruo } 556fce466eaSQu Wenruo 557d001e4a3SDavid Sterba __printf(4, 5) 558f1140243SQu Wenruo __cold 559d001e4a3SDavid Sterba static void chunk_err(const struct extent_buffer *leaf, 560f1140243SQu Wenruo const struct btrfs_chunk *chunk, u64 logical, 561f1140243SQu Wenruo const char *fmt, ...) 562f1140243SQu Wenruo { 563d001e4a3SDavid Sterba const struct btrfs_fs_info *fs_info = leaf->fs_info; 564f1140243SQu Wenruo bool is_sb; 565f1140243SQu Wenruo struct va_format vaf; 566f1140243SQu Wenruo va_list args; 567f1140243SQu Wenruo int i; 568f1140243SQu Wenruo int slot = -1; 569f1140243SQu Wenruo 570f1140243SQu Wenruo /* Only superblock eb is able to have such small offset */ 571f1140243SQu Wenruo is_sb = (leaf->start == BTRFS_SUPER_INFO_OFFSET); 572f1140243SQu Wenruo 573f1140243SQu Wenruo if (!is_sb) { 574f1140243SQu Wenruo /* 575f1140243SQu Wenruo * Get the slot number by iterating through all slots, this 576f1140243SQu Wenruo * would provide better readability. 577f1140243SQu Wenruo */ 578f1140243SQu Wenruo for (i = 0; i < btrfs_header_nritems(leaf); i++) { 579f1140243SQu Wenruo if (btrfs_item_ptr_offset(leaf, i) == 580f1140243SQu Wenruo (unsigned long)chunk) { 581f1140243SQu Wenruo slot = i; 582f1140243SQu Wenruo break; 583f1140243SQu Wenruo } 584f1140243SQu Wenruo } 585f1140243SQu Wenruo } 586f1140243SQu Wenruo va_start(args, fmt); 587f1140243SQu Wenruo vaf.fmt = fmt; 588f1140243SQu Wenruo vaf.va = &args; 589f1140243SQu Wenruo 590f1140243SQu Wenruo if (is_sb) 591f1140243SQu Wenruo btrfs_crit(fs_info, 592f1140243SQu Wenruo "corrupt superblock syschunk array: chunk_start=%llu, %pV", 593f1140243SQu Wenruo logical, &vaf); 594f1140243SQu Wenruo else 595f1140243SQu Wenruo btrfs_crit(fs_info, 596f1140243SQu Wenruo "corrupt leaf: root=%llu block=%llu slot=%d chunk_start=%llu, %pV", 597f1140243SQu Wenruo BTRFS_CHUNK_TREE_OBJECTID, leaf->start, slot, 598f1140243SQu Wenruo logical, &vaf); 599f1140243SQu Wenruo va_end(args); 600f1140243SQu Wenruo } 601f1140243SQu Wenruo 602ad7b0368SQu Wenruo /* 60382fc28fbSQu Wenruo * The common chunk check which could also work on super block sys chunk array. 60482fc28fbSQu Wenruo * 605bf871c3bSQu Wenruo * Return -EUCLEAN if anything is corrupted. 60682fc28fbSQu Wenruo * Return 0 if everything is OK. 60782fc28fbSQu Wenruo */ 608ddaf1d5aSDavid Sterba int btrfs_check_chunk_valid(struct extent_buffer *leaf, 60982fc28fbSQu Wenruo struct btrfs_chunk *chunk, u64 logical) 61082fc28fbSQu Wenruo { 611ddaf1d5aSDavid Sterba struct btrfs_fs_info *fs_info = leaf->fs_info; 61282fc28fbSQu Wenruo u64 length; 61382fc28fbSQu Wenruo u64 stripe_len; 61482fc28fbSQu Wenruo u16 num_stripes; 61582fc28fbSQu Wenruo u16 sub_stripes; 61682fc28fbSQu Wenruo u64 type; 61782fc28fbSQu Wenruo u64 features; 61882fc28fbSQu Wenruo bool mixed = false; 61982fc28fbSQu Wenruo 62082fc28fbSQu Wenruo length = btrfs_chunk_length(leaf, chunk); 62182fc28fbSQu Wenruo stripe_len = btrfs_chunk_stripe_len(leaf, chunk); 62282fc28fbSQu Wenruo num_stripes = btrfs_chunk_num_stripes(leaf, chunk); 62382fc28fbSQu Wenruo sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk); 62482fc28fbSQu Wenruo type = btrfs_chunk_type(leaf, chunk); 62582fc28fbSQu Wenruo 62682fc28fbSQu Wenruo if (!num_stripes) { 627d001e4a3SDavid Sterba chunk_err(leaf, chunk, logical, 628f1140243SQu Wenruo "invalid chunk num_stripes, have %u", num_stripes); 629bf871c3bSQu Wenruo return -EUCLEAN; 63082fc28fbSQu Wenruo } 63182fc28fbSQu Wenruo if (!IS_ALIGNED(logical, fs_info->sectorsize)) { 632d001e4a3SDavid Sterba chunk_err(leaf, chunk, logical, 633f1140243SQu Wenruo "invalid chunk logical, have %llu should aligned to %u", 634f1140243SQu Wenruo logical, fs_info->sectorsize); 635bf871c3bSQu Wenruo return -EUCLEAN; 63682fc28fbSQu Wenruo } 63782fc28fbSQu Wenruo if (btrfs_chunk_sector_size(leaf, chunk) != fs_info->sectorsize) { 638d001e4a3SDavid Sterba chunk_err(leaf, chunk, logical, 639f1140243SQu Wenruo "invalid chunk sectorsize, have %u expect %u", 640f1140243SQu Wenruo btrfs_chunk_sector_size(leaf, chunk), 641f1140243SQu Wenruo fs_info->sectorsize); 642bf871c3bSQu Wenruo return -EUCLEAN; 64382fc28fbSQu Wenruo } 64482fc28fbSQu Wenruo if (!length || !IS_ALIGNED(length, fs_info->sectorsize)) { 645d001e4a3SDavid Sterba chunk_err(leaf, chunk, logical, 646f1140243SQu Wenruo "invalid chunk length, have %llu", length); 647bf871c3bSQu Wenruo return -EUCLEAN; 64882fc28fbSQu Wenruo } 64982fc28fbSQu Wenruo if (!is_power_of_2(stripe_len) || stripe_len != BTRFS_STRIPE_LEN) { 650d001e4a3SDavid Sterba chunk_err(leaf, chunk, logical, 651f1140243SQu Wenruo "invalid chunk stripe length: %llu", 65282fc28fbSQu Wenruo stripe_len); 653bf871c3bSQu Wenruo return -EUCLEAN; 65482fc28fbSQu Wenruo } 65582fc28fbSQu Wenruo if (~(BTRFS_BLOCK_GROUP_TYPE_MASK | BTRFS_BLOCK_GROUP_PROFILE_MASK) & 65682fc28fbSQu Wenruo type) { 657d001e4a3SDavid Sterba chunk_err(leaf, chunk, logical, 658f1140243SQu Wenruo "unrecognized chunk type: 0x%llx", 65982fc28fbSQu Wenruo ~(BTRFS_BLOCK_GROUP_TYPE_MASK | 66082fc28fbSQu Wenruo BTRFS_BLOCK_GROUP_PROFILE_MASK) & 66182fc28fbSQu Wenruo btrfs_chunk_type(leaf, chunk)); 662bf871c3bSQu Wenruo return -EUCLEAN; 66382fc28fbSQu Wenruo } 66482fc28fbSQu Wenruo 665c1499166SDavid Sterba if (!has_single_bit_set(type & BTRFS_BLOCK_GROUP_PROFILE_MASK) && 66680e46cf2SQu Wenruo (type & BTRFS_BLOCK_GROUP_PROFILE_MASK) != 0) { 667d001e4a3SDavid Sterba chunk_err(leaf, chunk, logical, 66880e46cf2SQu Wenruo "invalid chunk profile flag: 0x%llx, expect 0 or 1 bit set", 66980e46cf2SQu Wenruo type & BTRFS_BLOCK_GROUP_PROFILE_MASK); 67080e46cf2SQu Wenruo return -EUCLEAN; 67180e46cf2SQu Wenruo } 67282fc28fbSQu Wenruo if ((type & BTRFS_BLOCK_GROUP_TYPE_MASK) == 0) { 673d001e4a3SDavid Sterba chunk_err(leaf, chunk, logical, 674f1140243SQu Wenruo "missing chunk type flag, have 0x%llx one bit must be set in 0x%llx", 675f1140243SQu Wenruo type, BTRFS_BLOCK_GROUP_TYPE_MASK); 676bf871c3bSQu Wenruo return -EUCLEAN; 67782fc28fbSQu Wenruo } 67882fc28fbSQu Wenruo 67982fc28fbSQu Wenruo if ((type & BTRFS_BLOCK_GROUP_SYSTEM) && 68082fc28fbSQu Wenruo (type & (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA))) { 681d001e4a3SDavid Sterba chunk_err(leaf, chunk, logical, 682f1140243SQu Wenruo "system chunk with data or metadata type: 0x%llx", 683f1140243SQu Wenruo type); 684bf871c3bSQu Wenruo return -EUCLEAN; 68582fc28fbSQu Wenruo } 68682fc28fbSQu Wenruo 68782fc28fbSQu Wenruo features = btrfs_super_incompat_flags(fs_info->super_copy); 68882fc28fbSQu Wenruo if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) 68982fc28fbSQu Wenruo mixed = true; 69082fc28fbSQu Wenruo 69182fc28fbSQu Wenruo if (!mixed) { 69282fc28fbSQu Wenruo if ((type & BTRFS_BLOCK_GROUP_METADATA) && 69382fc28fbSQu Wenruo (type & BTRFS_BLOCK_GROUP_DATA)) { 694d001e4a3SDavid Sterba chunk_err(leaf, chunk, logical, 69582fc28fbSQu Wenruo "mixed chunk type in non-mixed mode: 0x%llx", type); 696bf871c3bSQu Wenruo return -EUCLEAN; 69782fc28fbSQu Wenruo } 69882fc28fbSQu Wenruo } 69982fc28fbSQu Wenruo 70082fc28fbSQu Wenruo if ((type & BTRFS_BLOCK_GROUP_RAID10 && sub_stripes != 2) || 70182fc28fbSQu Wenruo (type & BTRFS_BLOCK_GROUP_RAID1 && num_stripes != 2) || 70282fc28fbSQu Wenruo (type & BTRFS_BLOCK_GROUP_RAID5 && num_stripes < 2) || 70382fc28fbSQu Wenruo (type & BTRFS_BLOCK_GROUP_RAID6 && num_stripes < 3) || 70482fc28fbSQu Wenruo (type & BTRFS_BLOCK_GROUP_DUP && num_stripes != 2) || 70582fc28fbSQu Wenruo ((type & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 && num_stripes != 1)) { 706d001e4a3SDavid Sterba chunk_err(leaf, chunk, logical, 70782fc28fbSQu Wenruo "invalid num_stripes:sub_stripes %u:%u for profile %llu", 70882fc28fbSQu Wenruo num_stripes, sub_stripes, 70982fc28fbSQu Wenruo type & BTRFS_BLOCK_GROUP_PROFILE_MASK); 710bf871c3bSQu Wenruo return -EUCLEAN; 71182fc28fbSQu Wenruo } 71282fc28fbSQu Wenruo 71382fc28fbSQu Wenruo return 0; 71482fc28fbSQu Wenruo } 71582fc28fbSQu Wenruo 7165617ed80SDavid Sterba __printf(3, 4) 717ab4ba2e1SQu Wenruo __cold 7185617ed80SDavid Sterba static void dev_item_err(const struct extent_buffer *eb, int slot, 719ab4ba2e1SQu Wenruo const char *fmt, ...) 720ab4ba2e1SQu Wenruo { 721ab4ba2e1SQu Wenruo struct btrfs_key key; 722ab4ba2e1SQu Wenruo struct va_format vaf; 723ab4ba2e1SQu Wenruo va_list args; 724ab4ba2e1SQu Wenruo 725ab4ba2e1SQu Wenruo btrfs_item_key_to_cpu(eb, &key, slot); 726ab4ba2e1SQu Wenruo va_start(args, fmt); 727ab4ba2e1SQu Wenruo 728ab4ba2e1SQu Wenruo vaf.fmt = fmt; 729ab4ba2e1SQu Wenruo vaf.va = &args; 730ab4ba2e1SQu Wenruo 7315617ed80SDavid Sterba btrfs_crit(eb->fs_info, 732ab4ba2e1SQu Wenruo "corrupt %s: root=%llu block=%llu slot=%d devid=%llu %pV", 733ab4ba2e1SQu Wenruo btrfs_header_level(eb) == 0 ? "leaf" : "node", 734ab4ba2e1SQu Wenruo btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot, 735ab4ba2e1SQu Wenruo key.objectid, &vaf); 736ab4ba2e1SQu Wenruo va_end(args); 737ab4ba2e1SQu Wenruo } 738ab4ba2e1SQu Wenruo 739412a2312SDavid Sterba static int check_dev_item(struct extent_buffer *leaf, 740ab4ba2e1SQu Wenruo struct btrfs_key *key, int slot) 741ab4ba2e1SQu Wenruo { 742ab4ba2e1SQu Wenruo struct btrfs_dev_item *ditem; 743ab4ba2e1SQu Wenruo 744ab4ba2e1SQu Wenruo if (key->objectid != BTRFS_DEV_ITEMS_OBJECTID) { 7455617ed80SDavid Sterba dev_item_err(leaf, slot, 746ab4ba2e1SQu Wenruo "invalid objectid: has=%llu expect=%llu", 747ab4ba2e1SQu Wenruo key->objectid, BTRFS_DEV_ITEMS_OBJECTID); 748ab4ba2e1SQu Wenruo return -EUCLEAN; 749ab4ba2e1SQu Wenruo } 750ab4ba2e1SQu Wenruo ditem = btrfs_item_ptr(leaf, slot, struct btrfs_dev_item); 751ab4ba2e1SQu Wenruo if (btrfs_device_id(leaf, ditem) != key->offset) { 7525617ed80SDavid Sterba dev_item_err(leaf, slot, 753ab4ba2e1SQu Wenruo "devid mismatch: key has=%llu item has=%llu", 754ab4ba2e1SQu Wenruo key->offset, btrfs_device_id(leaf, ditem)); 755ab4ba2e1SQu Wenruo return -EUCLEAN; 756ab4ba2e1SQu Wenruo } 757ab4ba2e1SQu Wenruo 758ab4ba2e1SQu Wenruo /* 759ab4ba2e1SQu Wenruo * For device total_bytes, we don't have reliable way to check it, as 760ab4ba2e1SQu Wenruo * it can be 0 for device removal. Device size check can only be done 761ab4ba2e1SQu Wenruo * by dev extents check. 762ab4ba2e1SQu Wenruo */ 763ab4ba2e1SQu Wenruo if (btrfs_device_bytes_used(leaf, ditem) > 764ab4ba2e1SQu Wenruo btrfs_device_total_bytes(leaf, ditem)) { 7655617ed80SDavid Sterba dev_item_err(leaf, slot, 766ab4ba2e1SQu Wenruo "invalid bytes used: have %llu expect [0, %llu]", 767ab4ba2e1SQu Wenruo btrfs_device_bytes_used(leaf, ditem), 768ab4ba2e1SQu Wenruo btrfs_device_total_bytes(leaf, ditem)); 769ab4ba2e1SQu Wenruo return -EUCLEAN; 770ab4ba2e1SQu Wenruo } 771ab4ba2e1SQu Wenruo /* 772ab4ba2e1SQu Wenruo * Remaining members like io_align/type/gen/dev_group aren't really 773ab4ba2e1SQu Wenruo * utilized. Skip them to make later usage of them easier. 774ab4ba2e1SQu Wenruo */ 775ab4ba2e1SQu Wenruo return 0; 776ab4ba2e1SQu Wenruo } 777ab4ba2e1SQu Wenruo 778496245caSQu Wenruo /* Inode item error output has the same format as dir_item_err() */ 779496245caSQu Wenruo #define inode_item_err(fs_info, eb, slot, fmt, ...) \ 780d98ced68SDavid Sterba dir_item_err(eb, slot, fmt, __VA_ARGS__) 781496245caSQu Wenruo 78239e57f49SDavid Sterba static int check_inode_item(struct extent_buffer *leaf, 783496245caSQu Wenruo struct btrfs_key *key, int slot) 784496245caSQu Wenruo { 78539e57f49SDavid Sterba struct btrfs_fs_info *fs_info = leaf->fs_info; 786496245caSQu Wenruo struct btrfs_inode_item *iitem; 787496245caSQu Wenruo u64 super_gen = btrfs_super_generation(fs_info->super_copy); 788496245caSQu Wenruo u32 valid_mask = (S_IFMT | S_ISUID | S_ISGID | S_ISVTX | 0777); 789496245caSQu Wenruo u32 mode; 790496245caSQu Wenruo 791496245caSQu Wenruo if ((key->objectid < BTRFS_FIRST_FREE_OBJECTID || 792496245caSQu Wenruo key->objectid > BTRFS_LAST_FREE_OBJECTID) && 793496245caSQu Wenruo key->objectid != BTRFS_ROOT_TREE_DIR_OBJECTID && 794496245caSQu Wenruo key->objectid != BTRFS_FREE_INO_OBJECTID) { 79586a6be3aSDavid Sterba generic_err(leaf, slot, 796496245caSQu Wenruo "invalid key objectid: has %llu expect %llu or [%llu, %llu] or %llu", 797496245caSQu Wenruo key->objectid, BTRFS_ROOT_TREE_DIR_OBJECTID, 798496245caSQu Wenruo BTRFS_FIRST_FREE_OBJECTID, 799496245caSQu Wenruo BTRFS_LAST_FREE_OBJECTID, 800496245caSQu Wenruo BTRFS_FREE_INO_OBJECTID); 801496245caSQu Wenruo return -EUCLEAN; 802496245caSQu Wenruo } 803496245caSQu Wenruo if (key->offset != 0) { 804496245caSQu Wenruo inode_item_err(fs_info, leaf, slot, 805496245caSQu Wenruo "invalid key offset: has %llu expect 0", 806496245caSQu Wenruo key->offset); 807496245caSQu Wenruo return -EUCLEAN; 808496245caSQu Wenruo } 809496245caSQu Wenruo iitem = btrfs_item_ptr(leaf, slot, struct btrfs_inode_item); 810496245caSQu Wenruo 811496245caSQu Wenruo /* Here we use super block generation + 1 to handle log tree */ 812496245caSQu Wenruo if (btrfs_inode_generation(leaf, iitem) > super_gen + 1) { 813496245caSQu Wenruo inode_item_err(fs_info, leaf, slot, 814496245caSQu Wenruo "invalid inode generation: has %llu expect (0, %llu]", 815496245caSQu Wenruo btrfs_inode_generation(leaf, iitem), 816496245caSQu Wenruo super_gen + 1); 817496245caSQu Wenruo return -EUCLEAN; 818496245caSQu Wenruo } 819496245caSQu Wenruo /* Note for ROOT_TREE_DIR_ITEM, mkfs could set its transid 0 */ 820496245caSQu Wenruo if (btrfs_inode_transid(leaf, iitem) > super_gen + 1) { 821496245caSQu Wenruo inode_item_err(fs_info, leaf, slot, 822496245caSQu Wenruo "invalid inode generation: has %llu expect [0, %llu]", 823496245caSQu Wenruo btrfs_inode_transid(leaf, iitem), super_gen + 1); 824496245caSQu Wenruo return -EUCLEAN; 825496245caSQu Wenruo } 826496245caSQu Wenruo 827496245caSQu Wenruo /* 828496245caSQu Wenruo * For size and nbytes it's better not to be too strict, as for dir 829496245caSQu Wenruo * item its size/nbytes can easily get wrong, but doesn't affect 830496245caSQu Wenruo * anything in the fs. So here we skip the check. 831496245caSQu Wenruo */ 832496245caSQu Wenruo mode = btrfs_inode_mode(leaf, iitem); 833496245caSQu Wenruo if (mode & ~valid_mask) { 834496245caSQu Wenruo inode_item_err(fs_info, leaf, slot, 835496245caSQu Wenruo "unknown mode bit detected: 0x%x", 836496245caSQu Wenruo mode & ~valid_mask); 837496245caSQu Wenruo return -EUCLEAN; 838496245caSQu Wenruo } 839496245caSQu Wenruo 840496245caSQu Wenruo /* 841c1499166SDavid Sterba * S_IFMT is not bit mapped so we can't completely rely on 842c1499166SDavid Sterba * is_power_of_2/has_single_bit_set, but it can save us from checking 843c1499166SDavid Sterba * FIFO/CHR/DIR/REG. Only needs to check BLK, LNK and SOCKS 844496245caSQu Wenruo */ 845c1499166SDavid Sterba if (!has_single_bit_set(mode & S_IFMT)) { 846496245caSQu Wenruo if (!S_ISLNK(mode) && !S_ISBLK(mode) && !S_ISSOCK(mode)) { 847496245caSQu Wenruo inode_item_err(fs_info, leaf, slot, 848496245caSQu Wenruo "invalid mode: has 0%o expect valid S_IF* bit(s)", 849496245caSQu Wenruo mode & S_IFMT); 850496245caSQu Wenruo return -EUCLEAN; 851496245caSQu Wenruo } 852496245caSQu Wenruo } 853496245caSQu Wenruo if (S_ISDIR(mode) && btrfs_inode_nlink(leaf, iitem) > 1) { 854496245caSQu Wenruo inode_item_err(fs_info, leaf, slot, 855496245caSQu Wenruo "invalid nlink: has %u expect no more than 1 for dir", 856496245caSQu Wenruo btrfs_inode_nlink(leaf, iitem)); 857496245caSQu Wenruo return -EUCLEAN; 858496245caSQu Wenruo } 859496245caSQu Wenruo if (btrfs_inode_flags(leaf, iitem) & ~BTRFS_INODE_FLAG_MASK) { 860496245caSQu Wenruo inode_item_err(fs_info, leaf, slot, 861496245caSQu Wenruo "unknown flags detected: 0x%llx", 862496245caSQu Wenruo btrfs_inode_flags(leaf, iitem) & 863496245caSQu Wenruo ~BTRFS_INODE_FLAG_MASK); 864496245caSQu Wenruo return -EUCLEAN; 865496245caSQu Wenruo } 866496245caSQu Wenruo return 0; 867496245caSQu Wenruo } 868496245caSQu Wenruo 869259ee775SQu Wenruo static int check_root_item(struct extent_buffer *leaf, struct btrfs_key *key, 870259ee775SQu Wenruo int slot) 871259ee775SQu Wenruo { 872259ee775SQu Wenruo struct btrfs_fs_info *fs_info = leaf->fs_info; 873259ee775SQu Wenruo struct btrfs_root_item ri; 874259ee775SQu Wenruo const u64 valid_root_flags = BTRFS_ROOT_SUBVOL_RDONLY | 875259ee775SQu Wenruo BTRFS_ROOT_SUBVOL_DEAD; 876259ee775SQu Wenruo 877259ee775SQu Wenruo /* No such tree id */ 878259ee775SQu Wenruo if (key->objectid == 0) { 879259ee775SQu Wenruo generic_err(leaf, slot, "invalid root id 0"); 880259ee775SQu Wenruo return -EUCLEAN; 881259ee775SQu Wenruo } 882259ee775SQu Wenruo 883259ee775SQu Wenruo /* 884259ee775SQu Wenruo * Some older kernel may create ROOT_ITEM with non-zero offset, so here 885259ee775SQu Wenruo * we only check offset for reloc tree whose key->offset must be a 886259ee775SQu Wenruo * valid tree. 887259ee775SQu Wenruo */ 888259ee775SQu Wenruo if (key->objectid == BTRFS_TREE_RELOC_OBJECTID && key->offset == 0) { 889259ee775SQu Wenruo generic_err(leaf, slot, "invalid root id 0 for reloc tree"); 890259ee775SQu Wenruo return -EUCLEAN; 891259ee775SQu Wenruo } 892259ee775SQu Wenruo 893259ee775SQu Wenruo if (btrfs_item_size_nr(leaf, slot) != sizeof(ri)) { 894259ee775SQu Wenruo generic_err(leaf, slot, 895259ee775SQu Wenruo "invalid root item size, have %u expect %zu", 896259ee775SQu Wenruo btrfs_item_size_nr(leaf, slot), sizeof(ri)); 897259ee775SQu Wenruo } 898259ee775SQu Wenruo 899259ee775SQu Wenruo read_extent_buffer(leaf, &ri, btrfs_item_ptr_offset(leaf, slot), 900259ee775SQu Wenruo sizeof(ri)); 901259ee775SQu Wenruo 902259ee775SQu Wenruo /* Generation related */ 903259ee775SQu Wenruo if (btrfs_root_generation(&ri) > 904259ee775SQu Wenruo btrfs_super_generation(fs_info->super_copy) + 1) { 905259ee775SQu Wenruo generic_err(leaf, slot, 906259ee775SQu Wenruo "invalid root generation, have %llu expect (0, %llu]", 907259ee775SQu Wenruo btrfs_root_generation(&ri), 908259ee775SQu Wenruo btrfs_super_generation(fs_info->super_copy) + 1); 909259ee775SQu Wenruo return -EUCLEAN; 910259ee775SQu Wenruo } 911259ee775SQu Wenruo if (btrfs_root_generation_v2(&ri) > 912259ee775SQu Wenruo btrfs_super_generation(fs_info->super_copy) + 1) { 913259ee775SQu Wenruo generic_err(leaf, slot, 914259ee775SQu Wenruo "invalid root v2 generation, have %llu expect (0, %llu]", 915259ee775SQu Wenruo btrfs_root_generation_v2(&ri), 916259ee775SQu Wenruo btrfs_super_generation(fs_info->super_copy) + 1); 917259ee775SQu Wenruo return -EUCLEAN; 918259ee775SQu Wenruo } 919259ee775SQu Wenruo if (btrfs_root_last_snapshot(&ri) > 920259ee775SQu Wenruo btrfs_super_generation(fs_info->super_copy) + 1) { 921259ee775SQu Wenruo generic_err(leaf, slot, 922259ee775SQu Wenruo "invalid root last_snapshot, have %llu expect (0, %llu]", 923259ee775SQu Wenruo btrfs_root_last_snapshot(&ri), 924259ee775SQu Wenruo btrfs_super_generation(fs_info->super_copy) + 1); 925259ee775SQu Wenruo return -EUCLEAN; 926259ee775SQu Wenruo } 927259ee775SQu Wenruo 928259ee775SQu Wenruo /* Alignment and level check */ 929259ee775SQu Wenruo if (!IS_ALIGNED(btrfs_root_bytenr(&ri), fs_info->sectorsize)) { 930259ee775SQu Wenruo generic_err(leaf, slot, 931259ee775SQu Wenruo "invalid root bytenr, have %llu expect to be aligned to %u", 932259ee775SQu Wenruo btrfs_root_bytenr(&ri), fs_info->sectorsize); 933259ee775SQu Wenruo return -EUCLEAN; 934259ee775SQu Wenruo } 935259ee775SQu Wenruo if (btrfs_root_level(&ri) >= BTRFS_MAX_LEVEL) { 936259ee775SQu Wenruo generic_err(leaf, slot, 937259ee775SQu Wenruo "invalid root level, have %u expect [0, %u]", 938259ee775SQu Wenruo btrfs_root_level(&ri), BTRFS_MAX_LEVEL - 1); 939259ee775SQu Wenruo return -EUCLEAN; 940259ee775SQu Wenruo } 941259ee775SQu Wenruo if (ri.drop_level >= BTRFS_MAX_LEVEL) { 942259ee775SQu Wenruo generic_err(leaf, slot, 943259ee775SQu Wenruo "invalid root level, have %u expect [0, %u]", 944259ee775SQu Wenruo ri.drop_level, BTRFS_MAX_LEVEL - 1); 945259ee775SQu Wenruo return -EUCLEAN; 946259ee775SQu Wenruo } 947259ee775SQu Wenruo 948259ee775SQu Wenruo /* Flags check */ 949259ee775SQu Wenruo if (btrfs_root_flags(&ri) & ~valid_root_flags) { 950259ee775SQu Wenruo generic_err(leaf, slot, 951259ee775SQu Wenruo "invalid root flags, have 0x%llx expect mask 0x%llx", 952259ee775SQu Wenruo btrfs_root_flags(&ri), valid_root_flags); 953259ee775SQu Wenruo return -EUCLEAN; 954259ee775SQu Wenruo } 955259ee775SQu Wenruo return 0; 956259ee775SQu Wenruo } 957259ee775SQu Wenruo 958f82d1c7cSQu Wenruo __printf(3,4) 959f82d1c7cSQu Wenruo __cold 960f82d1c7cSQu Wenruo static void extent_err(const struct extent_buffer *eb, int slot, 961f82d1c7cSQu Wenruo const char *fmt, ...) 962f82d1c7cSQu Wenruo { 963f82d1c7cSQu Wenruo struct btrfs_key key; 964f82d1c7cSQu Wenruo struct va_format vaf; 965f82d1c7cSQu Wenruo va_list args; 966f82d1c7cSQu Wenruo u64 bytenr; 967f82d1c7cSQu Wenruo u64 len; 968f82d1c7cSQu Wenruo 969f82d1c7cSQu Wenruo btrfs_item_key_to_cpu(eb, &key, slot); 970f82d1c7cSQu Wenruo bytenr = key.objectid; 971e2406a6fSQu Wenruo if (key.type == BTRFS_METADATA_ITEM_KEY || 972e2406a6fSQu Wenruo key.type == BTRFS_TREE_BLOCK_REF_KEY || 973e2406a6fSQu Wenruo key.type == BTRFS_SHARED_BLOCK_REF_KEY) 974f82d1c7cSQu Wenruo len = eb->fs_info->nodesize; 975f82d1c7cSQu Wenruo else 976f82d1c7cSQu Wenruo len = key.offset; 977f82d1c7cSQu Wenruo va_start(args, fmt); 978f82d1c7cSQu Wenruo 979f82d1c7cSQu Wenruo vaf.fmt = fmt; 980f82d1c7cSQu Wenruo vaf.va = &args; 981f82d1c7cSQu Wenruo 982f82d1c7cSQu Wenruo btrfs_crit(eb->fs_info, 983f82d1c7cSQu Wenruo "corrupt %s: block=%llu slot=%d extent bytenr=%llu len=%llu %pV", 984f82d1c7cSQu Wenruo btrfs_header_level(eb) == 0 ? "leaf" : "node", 985f82d1c7cSQu Wenruo eb->start, slot, bytenr, len, &vaf); 986f82d1c7cSQu Wenruo va_end(args); 987f82d1c7cSQu Wenruo } 988f82d1c7cSQu Wenruo 989f82d1c7cSQu Wenruo static int check_extent_item(struct extent_buffer *leaf, 990f82d1c7cSQu Wenruo struct btrfs_key *key, int slot) 991f82d1c7cSQu Wenruo { 992f82d1c7cSQu Wenruo struct btrfs_fs_info *fs_info = leaf->fs_info; 993f82d1c7cSQu Wenruo struct btrfs_extent_item *ei; 994f82d1c7cSQu Wenruo bool is_tree_block = false; 995f82d1c7cSQu Wenruo unsigned long ptr; /* Current pointer inside inline refs */ 996f82d1c7cSQu Wenruo unsigned long end; /* Extent item end */ 997f82d1c7cSQu Wenruo const u32 item_size = btrfs_item_size_nr(leaf, slot); 998f82d1c7cSQu Wenruo u64 flags; 999f82d1c7cSQu Wenruo u64 generation; 1000f82d1c7cSQu Wenruo u64 total_refs; /* Total refs in btrfs_extent_item */ 1001f82d1c7cSQu Wenruo u64 inline_refs = 0; /* found total inline refs */ 1002f82d1c7cSQu Wenruo 1003f82d1c7cSQu Wenruo if (key->type == BTRFS_METADATA_ITEM_KEY && 1004f82d1c7cSQu Wenruo !btrfs_fs_incompat(fs_info, SKINNY_METADATA)) { 1005f82d1c7cSQu Wenruo generic_err(leaf, slot, 1006f82d1c7cSQu Wenruo "invalid key type, METADATA_ITEM type invalid when SKINNY_METADATA feature disabled"); 1007f82d1c7cSQu Wenruo return -EUCLEAN; 1008f82d1c7cSQu Wenruo } 1009f82d1c7cSQu Wenruo /* key->objectid is the bytenr for both key types */ 1010f82d1c7cSQu Wenruo if (!IS_ALIGNED(key->objectid, fs_info->sectorsize)) { 1011f82d1c7cSQu Wenruo generic_err(leaf, slot, 1012f82d1c7cSQu Wenruo "invalid key objectid, have %llu expect to be aligned to %u", 1013f82d1c7cSQu Wenruo key->objectid, fs_info->sectorsize); 1014f82d1c7cSQu Wenruo return -EUCLEAN; 1015f82d1c7cSQu Wenruo } 1016f82d1c7cSQu Wenruo 1017f82d1c7cSQu Wenruo /* key->offset is tree level for METADATA_ITEM_KEY */ 1018f82d1c7cSQu Wenruo if (key->type == BTRFS_METADATA_ITEM_KEY && 1019f82d1c7cSQu Wenruo key->offset >= BTRFS_MAX_LEVEL) { 1020f82d1c7cSQu Wenruo extent_err(leaf, slot, 1021f82d1c7cSQu Wenruo "invalid tree level, have %llu expect [0, %u]", 1022f82d1c7cSQu Wenruo key->offset, BTRFS_MAX_LEVEL - 1); 1023f82d1c7cSQu Wenruo return -EUCLEAN; 1024f82d1c7cSQu Wenruo } 1025f82d1c7cSQu Wenruo 1026f82d1c7cSQu Wenruo /* 1027f82d1c7cSQu Wenruo * EXTENT/METADATA_ITEM consists of: 1028f82d1c7cSQu Wenruo * 1) One btrfs_extent_item 1029f82d1c7cSQu Wenruo * Records the total refs, type and generation of the extent. 1030f82d1c7cSQu Wenruo * 1031f82d1c7cSQu Wenruo * 2) One btrfs_tree_block_info (for EXTENT_ITEM and tree backref only) 1032f82d1c7cSQu Wenruo * Records the first key and level of the tree block. 1033f82d1c7cSQu Wenruo * 1034f82d1c7cSQu Wenruo * 2) Zero or more btrfs_extent_inline_ref(s) 1035f82d1c7cSQu Wenruo * Each inline ref has one btrfs_extent_inline_ref shows: 1036f82d1c7cSQu Wenruo * 2.1) The ref type, one of the 4 1037f82d1c7cSQu Wenruo * TREE_BLOCK_REF Tree block only 1038f82d1c7cSQu Wenruo * SHARED_BLOCK_REF Tree block only 1039f82d1c7cSQu Wenruo * EXTENT_DATA_REF Data only 1040f82d1c7cSQu Wenruo * SHARED_DATA_REF Data only 1041f82d1c7cSQu Wenruo * 2.2) Ref type specific data 1042f82d1c7cSQu Wenruo * Either using btrfs_extent_inline_ref::offset, or specific 1043f82d1c7cSQu Wenruo * data structure. 1044f82d1c7cSQu Wenruo */ 1045f82d1c7cSQu Wenruo if (item_size < sizeof(*ei)) { 1046f82d1c7cSQu Wenruo extent_err(leaf, slot, 1047f82d1c7cSQu Wenruo "invalid item size, have %u expect [%zu, %u)", 1048f82d1c7cSQu Wenruo item_size, sizeof(*ei), 1049f82d1c7cSQu Wenruo BTRFS_LEAF_DATA_SIZE(fs_info)); 1050f82d1c7cSQu Wenruo return -EUCLEAN; 1051f82d1c7cSQu Wenruo } 1052f82d1c7cSQu Wenruo end = item_size + btrfs_item_ptr_offset(leaf, slot); 1053f82d1c7cSQu Wenruo 1054f82d1c7cSQu Wenruo /* Checks against extent_item */ 1055f82d1c7cSQu Wenruo ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item); 1056f82d1c7cSQu Wenruo flags = btrfs_extent_flags(leaf, ei); 1057f82d1c7cSQu Wenruo total_refs = btrfs_extent_refs(leaf, ei); 1058f82d1c7cSQu Wenruo generation = btrfs_extent_generation(leaf, ei); 1059f82d1c7cSQu Wenruo if (generation > btrfs_super_generation(fs_info->super_copy) + 1) { 1060f82d1c7cSQu Wenruo extent_err(leaf, slot, 1061f82d1c7cSQu Wenruo "invalid generation, have %llu expect (0, %llu]", 1062f82d1c7cSQu Wenruo generation, 1063f82d1c7cSQu Wenruo btrfs_super_generation(fs_info->super_copy) + 1); 1064f82d1c7cSQu Wenruo return -EUCLEAN; 1065f82d1c7cSQu Wenruo } 1066c1499166SDavid Sterba if (!has_single_bit_set(flags & (BTRFS_EXTENT_FLAG_DATA | 1067f82d1c7cSQu Wenruo BTRFS_EXTENT_FLAG_TREE_BLOCK))) { 1068f82d1c7cSQu Wenruo extent_err(leaf, slot, 1069f82d1c7cSQu Wenruo "invalid extent flag, have 0x%llx expect 1 bit set in 0x%llx", 1070f82d1c7cSQu Wenruo flags, BTRFS_EXTENT_FLAG_DATA | 1071f82d1c7cSQu Wenruo BTRFS_EXTENT_FLAG_TREE_BLOCK); 1072f82d1c7cSQu Wenruo return -EUCLEAN; 1073f82d1c7cSQu Wenruo } 1074f82d1c7cSQu Wenruo is_tree_block = !!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK); 1075f82d1c7cSQu Wenruo if (is_tree_block) { 1076f82d1c7cSQu Wenruo if (key->type == BTRFS_EXTENT_ITEM_KEY && 1077f82d1c7cSQu Wenruo key->offset != fs_info->nodesize) { 1078f82d1c7cSQu Wenruo extent_err(leaf, slot, 1079f82d1c7cSQu Wenruo "invalid extent length, have %llu expect %u", 1080f82d1c7cSQu Wenruo key->offset, fs_info->nodesize); 1081f82d1c7cSQu Wenruo return -EUCLEAN; 1082f82d1c7cSQu Wenruo } 1083f82d1c7cSQu Wenruo } else { 1084f82d1c7cSQu Wenruo if (key->type != BTRFS_EXTENT_ITEM_KEY) { 1085f82d1c7cSQu Wenruo extent_err(leaf, slot, 1086f82d1c7cSQu Wenruo "invalid key type, have %u expect %u for data backref", 1087f82d1c7cSQu Wenruo key->type, BTRFS_EXTENT_ITEM_KEY); 1088f82d1c7cSQu Wenruo return -EUCLEAN; 1089f82d1c7cSQu Wenruo } 1090f82d1c7cSQu Wenruo if (!IS_ALIGNED(key->offset, fs_info->sectorsize)) { 1091f82d1c7cSQu Wenruo extent_err(leaf, slot, 1092f82d1c7cSQu Wenruo "invalid extent length, have %llu expect aligned to %u", 1093f82d1c7cSQu Wenruo key->offset, fs_info->sectorsize); 1094f82d1c7cSQu Wenruo return -EUCLEAN; 1095f82d1c7cSQu Wenruo } 1096f82d1c7cSQu Wenruo } 1097f82d1c7cSQu Wenruo ptr = (unsigned long)(struct btrfs_extent_item *)(ei + 1); 1098f82d1c7cSQu Wenruo 1099f82d1c7cSQu Wenruo /* Check the special case of btrfs_tree_block_info */ 1100f82d1c7cSQu Wenruo if (is_tree_block && key->type != BTRFS_METADATA_ITEM_KEY) { 1101f82d1c7cSQu Wenruo struct btrfs_tree_block_info *info; 1102f82d1c7cSQu Wenruo 1103f82d1c7cSQu Wenruo info = (struct btrfs_tree_block_info *)ptr; 1104f82d1c7cSQu Wenruo if (btrfs_tree_block_level(leaf, info) >= BTRFS_MAX_LEVEL) { 1105f82d1c7cSQu Wenruo extent_err(leaf, slot, 1106f82d1c7cSQu Wenruo "invalid tree block info level, have %u expect [0, %u]", 1107f82d1c7cSQu Wenruo btrfs_tree_block_level(leaf, info), 1108f82d1c7cSQu Wenruo BTRFS_MAX_LEVEL - 1); 1109f82d1c7cSQu Wenruo return -EUCLEAN; 1110f82d1c7cSQu Wenruo } 1111f82d1c7cSQu Wenruo ptr = (unsigned long)(struct btrfs_tree_block_info *)(info + 1); 1112f82d1c7cSQu Wenruo } 1113f82d1c7cSQu Wenruo 1114f82d1c7cSQu Wenruo /* Check inline refs */ 1115f82d1c7cSQu Wenruo while (ptr < end) { 1116f82d1c7cSQu Wenruo struct btrfs_extent_inline_ref *iref; 1117f82d1c7cSQu Wenruo struct btrfs_extent_data_ref *dref; 1118f82d1c7cSQu Wenruo struct btrfs_shared_data_ref *sref; 1119f82d1c7cSQu Wenruo u64 dref_offset; 1120f82d1c7cSQu Wenruo u64 inline_offset; 1121f82d1c7cSQu Wenruo u8 inline_type; 1122f82d1c7cSQu Wenruo 1123f82d1c7cSQu Wenruo if (ptr + sizeof(*iref) > end) { 1124f82d1c7cSQu Wenruo extent_err(leaf, slot, 1125f82d1c7cSQu Wenruo "inline ref item overflows extent item, ptr %lu iref size %zu end %lu", 1126f82d1c7cSQu Wenruo ptr, sizeof(*iref), end); 1127f82d1c7cSQu Wenruo return -EUCLEAN; 1128f82d1c7cSQu Wenruo } 1129f82d1c7cSQu Wenruo iref = (struct btrfs_extent_inline_ref *)ptr; 1130f82d1c7cSQu Wenruo inline_type = btrfs_extent_inline_ref_type(leaf, iref); 1131f82d1c7cSQu Wenruo inline_offset = btrfs_extent_inline_ref_offset(leaf, iref); 1132f82d1c7cSQu Wenruo if (ptr + btrfs_extent_inline_ref_size(inline_type) > end) { 1133f82d1c7cSQu Wenruo extent_err(leaf, slot, 1134f82d1c7cSQu Wenruo "inline ref item overflows extent item, ptr %lu iref size %u end %lu", 1135f82d1c7cSQu Wenruo ptr, inline_type, end); 1136f82d1c7cSQu Wenruo return -EUCLEAN; 1137f82d1c7cSQu Wenruo } 1138f82d1c7cSQu Wenruo 1139f82d1c7cSQu Wenruo switch (inline_type) { 1140f82d1c7cSQu Wenruo /* inline_offset is subvolid of the owner, no need to check */ 1141f82d1c7cSQu Wenruo case BTRFS_TREE_BLOCK_REF_KEY: 1142f82d1c7cSQu Wenruo inline_refs++; 1143f82d1c7cSQu Wenruo break; 1144f82d1c7cSQu Wenruo /* Contains parent bytenr */ 1145f82d1c7cSQu Wenruo case BTRFS_SHARED_BLOCK_REF_KEY: 1146f82d1c7cSQu Wenruo if (!IS_ALIGNED(inline_offset, fs_info->sectorsize)) { 1147f82d1c7cSQu Wenruo extent_err(leaf, slot, 1148f82d1c7cSQu Wenruo "invalid tree parent bytenr, have %llu expect aligned to %u", 1149f82d1c7cSQu Wenruo inline_offset, fs_info->sectorsize); 1150f82d1c7cSQu Wenruo return -EUCLEAN; 1151f82d1c7cSQu Wenruo } 1152f82d1c7cSQu Wenruo inline_refs++; 1153f82d1c7cSQu Wenruo break; 1154f82d1c7cSQu Wenruo /* 1155f82d1c7cSQu Wenruo * Contains owner subvolid, owner key objectid, adjusted offset. 1156f82d1c7cSQu Wenruo * The only obvious corruption can happen in that offset. 1157f82d1c7cSQu Wenruo */ 1158f82d1c7cSQu Wenruo case BTRFS_EXTENT_DATA_REF_KEY: 1159f82d1c7cSQu Wenruo dref = (struct btrfs_extent_data_ref *)(&iref->offset); 1160f82d1c7cSQu Wenruo dref_offset = btrfs_extent_data_ref_offset(leaf, dref); 1161f82d1c7cSQu Wenruo if (!IS_ALIGNED(dref_offset, fs_info->sectorsize)) { 1162f82d1c7cSQu Wenruo extent_err(leaf, slot, 1163f82d1c7cSQu Wenruo "invalid data ref offset, have %llu expect aligned to %u", 1164f82d1c7cSQu Wenruo dref_offset, fs_info->sectorsize); 1165f82d1c7cSQu Wenruo return -EUCLEAN; 1166f82d1c7cSQu Wenruo } 1167f82d1c7cSQu Wenruo inline_refs += btrfs_extent_data_ref_count(leaf, dref); 1168f82d1c7cSQu Wenruo break; 1169f82d1c7cSQu Wenruo /* Contains parent bytenr and ref count */ 1170f82d1c7cSQu Wenruo case BTRFS_SHARED_DATA_REF_KEY: 1171f82d1c7cSQu Wenruo sref = (struct btrfs_shared_data_ref *)(iref + 1); 1172f82d1c7cSQu Wenruo if (!IS_ALIGNED(inline_offset, fs_info->sectorsize)) { 1173f82d1c7cSQu Wenruo extent_err(leaf, slot, 1174f82d1c7cSQu Wenruo "invalid data parent bytenr, have %llu expect aligned to %u", 1175f82d1c7cSQu Wenruo inline_offset, fs_info->sectorsize); 1176f82d1c7cSQu Wenruo return -EUCLEAN; 1177f82d1c7cSQu Wenruo } 1178f82d1c7cSQu Wenruo inline_refs += btrfs_shared_data_ref_count(leaf, sref); 1179f82d1c7cSQu Wenruo break; 1180f82d1c7cSQu Wenruo default: 1181f82d1c7cSQu Wenruo extent_err(leaf, slot, "unknown inline ref type: %u", 1182f82d1c7cSQu Wenruo inline_type); 1183f82d1c7cSQu Wenruo return -EUCLEAN; 1184f82d1c7cSQu Wenruo } 1185f82d1c7cSQu Wenruo ptr += btrfs_extent_inline_ref_size(inline_type); 1186f82d1c7cSQu Wenruo } 1187f82d1c7cSQu Wenruo /* No padding is allowed */ 1188f82d1c7cSQu Wenruo if (ptr != end) { 1189f82d1c7cSQu Wenruo extent_err(leaf, slot, 1190f82d1c7cSQu Wenruo "invalid extent item size, padding bytes found"); 1191f82d1c7cSQu Wenruo return -EUCLEAN; 1192f82d1c7cSQu Wenruo } 1193f82d1c7cSQu Wenruo 1194f82d1c7cSQu Wenruo /* Finally, check the inline refs against total refs */ 1195f82d1c7cSQu Wenruo if (inline_refs > total_refs) { 1196f82d1c7cSQu Wenruo extent_err(leaf, slot, 1197f82d1c7cSQu Wenruo "invalid extent refs, have %llu expect >= inline %llu", 1198f82d1c7cSQu Wenruo total_refs, inline_refs); 1199f82d1c7cSQu Wenruo return -EUCLEAN; 1200f82d1c7cSQu Wenruo } 1201f82d1c7cSQu Wenruo return 0; 1202f82d1c7cSQu Wenruo } 1203f82d1c7cSQu Wenruo 1204e2406a6fSQu Wenruo static int check_simple_keyed_refs(struct extent_buffer *leaf, 1205e2406a6fSQu Wenruo struct btrfs_key *key, int slot) 1206e2406a6fSQu Wenruo { 1207e2406a6fSQu Wenruo u32 expect_item_size = 0; 1208e2406a6fSQu Wenruo 1209e2406a6fSQu Wenruo if (key->type == BTRFS_SHARED_DATA_REF_KEY) 1210e2406a6fSQu Wenruo expect_item_size = sizeof(struct btrfs_shared_data_ref); 1211e2406a6fSQu Wenruo 1212e2406a6fSQu Wenruo if (btrfs_item_size_nr(leaf, slot) != expect_item_size) { 1213e2406a6fSQu Wenruo generic_err(leaf, slot, 1214e2406a6fSQu Wenruo "invalid item size, have %u expect %u for key type %u", 1215e2406a6fSQu Wenruo btrfs_item_size_nr(leaf, slot), 1216e2406a6fSQu Wenruo expect_item_size, key->type); 1217e2406a6fSQu Wenruo return -EUCLEAN; 1218e2406a6fSQu Wenruo } 1219e2406a6fSQu Wenruo if (!IS_ALIGNED(key->objectid, leaf->fs_info->sectorsize)) { 1220e2406a6fSQu Wenruo generic_err(leaf, slot, 1221e2406a6fSQu Wenruo "invalid key objectid for shared block ref, have %llu expect aligned to %u", 1222e2406a6fSQu Wenruo key->objectid, leaf->fs_info->sectorsize); 1223e2406a6fSQu Wenruo return -EUCLEAN; 1224e2406a6fSQu Wenruo } 1225e2406a6fSQu Wenruo if (key->type != BTRFS_TREE_BLOCK_REF_KEY && 1226e2406a6fSQu Wenruo !IS_ALIGNED(key->offset, leaf->fs_info->sectorsize)) { 1227e2406a6fSQu Wenruo extent_err(leaf, slot, 1228e2406a6fSQu Wenruo "invalid tree parent bytenr, have %llu expect aligned to %u", 1229e2406a6fSQu Wenruo key->offset, leaf->fs_info->sectorsize); 1230e2406a6fSQu Wenruo return -EUCLEAN; 1231e2406a6fSQu Wenruo } 1232e2406a6fSQu Wenruo return 0; 1233e2406a6fSQu Wenruo } 1234e2406a6fSQu Wenruo 12350785a9aaSQu Wenruo static int check_extent_data_ref(struct extent_buffer *leaf, 12360785a9aaSQu Wenruo struct btrfs_key *key, int slot) 12370785a9aaSQu Wenruo { 12380785a9aaSQu Wenruo struct btrfs_extent_data_ref *dref; 12390785a9aaSQu Wenruo unsigned long ptr = btrfs_item_ptr_offset(leaf, slot); 12400785a9aaSQu Wenruo const unsigned long end = ptr + btrfs_item_size_nr(leaf, slot); 12410785a9aaSQu Wenruo 12420785a9aaSQu Wenruo if (btrfs_item_size_nr(leaf, slot) % sizeof(*dref) != 0) { 12430785a9aaSQu Wenruo generic_err(leaf, slot, 12440785a9aaSQu Wenruo "invalid item size, have %u expect aligned to %zu for key type %u", 12450785a9aaSQu Wenruo btrfs_item_size_nr(leaf, slot), 12460785a9aaSQu Wenruo sizeof(*dref), key->type); 12470785a9aaSQu Wenruo } 12480785a9aaSQu Wenruo if (!IS_ALIGNED(key->objectid, leaf->fs_info->sectorsize)) { 12490785a9aaSQu Wenruo generic_err(leaf, slot, 12500785a9aaSQu Wenruo "invalid key objectid for shared block ref, have %llu expect aligned to %u", 12510785a9aaSQu Wenruo key->objectid, leaf->fs_info->sectorsize); 12520785a9aaSQu Wenruo return -EUCLEAN; 12530785a9aaSQu Wenruo } 12540785a9aaSQu Wenruo for (; ptr < end; ptr += sizeof(*dref)) { 12550785a9aaSQu Wenruo u64 root_objectid; 12560785a9aaSQu Wenruo u64 owner; 12570785a9aaSQu Wenruo u64 offset; 12580785a9aaSQu Wenruo u64 hash; 12590785a9aaSQu Wenruo 12600785a9aaSQu Wenruo dref = (struct btrfs_extent_data_ref *)ptr; 12610785a9aaSQu Wenruo root_objectid = btrfs_extent_data_ref_root(leaf, dref); 12620785a9aaSQu Wenruo owner = btrfs_extent_data_ref_objectid(leaf, dref); 12630785a9aaSQu Wenruo offset = btrfs_extent_data_ref_offset(leaf, dref); 12640785a9aaSQu Wenruo hash = hash_extent_data_ref(root_objectid, owner, offset); 12650785a9aaSQu Wenruo if (hash != key->offset) { 12660785a9aaSQu Wenruo extent_err(leaf, slot, 12670785a9aaSQu Wenruo "invalid extent data ref hash, item has 0x%016llx key has 0x%016llx", 12680785a9aaSQu Wenruo hash, key->offset); 12690785a9aaSQu Wenruo return -EUCLEAN; 12700785a9aaSQu Wenruo } 12710785a9aaSQu Wenruo if (!IS_ALIGNED(offset, leaf->fs_info->sectorsize)) { 12720785a9aaSQu Wenruo extent_err(leaf, slot, 12730785a9aaSQu Wenruo "invalid extent data backref offset, have %llu expect aligned to %u", 12740785a9aaSQu Wenruo offset, leaf->fs_info->sectorsize); 12750785a9aaSQu Wenruo } 12760785a9aaSQu Wenruo } 12770785a9aaSQu Wenruo return 0; 12780785a9aaSQu Wenruo } 12790785a9aaSQu Wenruo 128071bf92a9SQu Wenruo #define inode_ref_err(fs_info, eb, slot, fmt, args...) \ 128171bf92a9SQu Wenruo inode_item_err(fs_info, eb, slot, fmt, ##args) 128271bf92a9SQu Wenruo static int check_inode_ref(struct extent_buffer *leaf, 128371bf92a9SQu Wenruo struct btrfs_key *key, struct btrfs_key *prev_key, 128471bf92a9SQu Wenruo int slot) 128571bf92a9SQu Wenruo { 128671bf92a9SQu Wenruo struct btrfs_inode_ref *iref; 128771bf92a9SQu Wenruo unsigned long ptr; 128871bf92a9SQu Wenruo unsigned long end; 128971bf92a9SQu Wenruo 129080d7fd1eSQu Wenruo if (!check_prev_ino(leaf, key, slot, prev_key)) 129180d7fd1eSQu Wenruo return -EUCLEAN; 129271bf92a9SQu Wenruo /* namelen can't be 0, so item_size == sizeof() is also invalid */ 129371bf92a9SQu Wenruo if (btrfs_item_size_nr(leaf, slot) <= sizeof(*iref)) { 129471bf92a9SQu Wenruo inode_ref_err(fs_info, leaf, slot, 129571bf92a9SQu Wenruo "invalid item size, have %u expect (%zu, %u)", 129671bf92a9SQu Wenruo btrfs_item_size_nr(leaf, slot), 129771bf92a9SQu Wenruo sizeof(*iref), BTRFS_LEAF_DATA_SIZE(leaf->fs_info)); 129871bf92a9SQu Wenruo return -EUCLEAN; 129971bf92a9SQu Wenruo } 130071bf92a9SQu Wenruo 130171bf92a9SQu Wenruo ptr = btrfs_item_ptr_offset(leaf, slot); 130271bf92a9SQu Wenruo end = ptr + btrfs_item_size_nr(leaf, slot); 130371bf92a9SQu Wenruo while (ptr < end) { 130471bf92a9SQu Wenruo u16 namelen; 130571bf92a9SQu Wenruo 130671bf92a9SQu Wenruo if (ptr + sizeof(iref) > end) { 130771bf92a9SQu Wenruo inode_ref_err(fs_info, leaf, slot, 130871bf92a9SQu Wenruo "inode ref overflow, ptr %lu end %lu inode_ref_size %zu", 130971bf92a9SQu Wenruo ptr, end, sizeof(iref)); 131071bf92a9SQu Wenruo return -EUCLEAN; 131171bf92a9SQu Wenruo } 131271bf92a9SQu Wenruo 131371bf92a9SQu Wenruo iref = (struct btrfs_inode_ref *)ptr; 131471bf92a9SQu Wenruo namelen = btrfs_inode_ref_name_len(leaf, iref); 131571bf92a9SQu Wenruo if (ptr + sizeof(*iref) + namelen > end) { 131671bf92a9SQu Wenruo inode_ref_err(fs_info, leaf, slot, 131771bf92a9SQu Wenruo "inode ref overflow, ptr %lu end %lu namelen %u", 131871bf92a9SQu Wenruo ptr, end, namelen); 131971bf92a9SQu Wenruo return -EUCLEAN; 132071bf92a9SQu Wenruo } 132171bf92a9SQu Wenruo 132271bf92a9SQu Wenruo /* 132371bf92a9SQu Wenruo * NOTE: In theory we should record all found index numbers 132471bf92a9SQu Wenruo * to find any duplicated indexes, but that will be too time 132571bf92a9SQu Wenruo * consuming for inodes with too many hard links. 132671bf92a9SQu Wenruo */ 132771bf92a9SQu Wenruo ptr += sizeof(*iref) + namelen; 132871bf92a9SQu Wenruo } 132971bf92a9SQu Wenruo return 0; 133071bf92a9SQu Wenruo } 133171bf92a9SQu Wenruo 133282fc28fbSQu Wenruo /* 1333557ea5ddSQu Wenruo * Common point to switch the item-specific validation. 1334557ea5ddSQu Wenruo */ 13350076bc89SDavid Sterba static int check_leaf_item(struct extent_buffer *leaf, 13364e9845efSFilipe Manana struct btrfs_key *key, int slot, 13374e9845efSFilipe Manana struct btrfs_key *prev_key) 1338557ea5ddSQu Wenruo { 1339557ea5ddSQu Wenruo int ret = 0; 1340075cb3c7SQu Wenruo struct btrfs_chunk *chunk; 1341557ea5ddSQu Wenruo 1342557ea5ddSQu Wenruo switch (key->type) { 1343557ea5ddSQu Wenruo case BTRFS_EXTENT_DATA_KEY: 13444e9845efSFilipe Manana ret = check_extent_data_item(leaf, key, slot, prev_key); 1345557ea5ddSQu Wenruo break; 1346557ea5ddSQu Wenruo case BTRFS_EXTENT_CSUM_KEY: 134768128ce7SDavid Sterba ret = check_csum_item(leaf, key, slot); 1348557ea5ddSQu Wenruo break; 1349ad7b0368SQu Wenruo case BTRFS_DIR_ITEM_KEY: 1350ad7b0368SQu Wenruo case BTRFS_DIR_INDEX_KEY: 1351ad7b0368SQu Wenruo case BTRFS_XATTR_ITEM_KEY: 1352c18679ebSQu Wenruo ret = check_dir_item(leaf, key, prev_key, slot); 1353ad7b0368SQu Wenruo break; 135471bf92a9SQu Wenruo case BTRFS_INODE_REF_KEY: 135571bf92a9SQu Wenruo ret = check_inode_ref(leaf, key, prev_key, slot); 135671bf92a9SQu Wenruo break; 1357fce466eaSQu Wenruo case BTRFS_BLOCK_GROUP_ITEM_KEY: 1358af60ce2bSDavid Sterba ret = check_block_group_item(leaf, key, slot); 1359fce466eaSQu Wenruo break; 1360075cb3c7SQu Wenruo case BTRFS_CHUNK_ITEM_KEY: 1361075cb3c7SQu Wenruo chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); 1362ddaf1d5aSDavid Sterba ret = btrfs_check_chunk_valid(leaf, chunk, key->offset); 1363075cb3c7SQu Wenruo break; 1364ab4ba2e1SQu Wenruo case BTRFS_DEV_ITEM_KEY: 1365412a2312SDavid Sterba ret = check_dev_item(leaf, key, slot); 1366ab4ba2e1SQu Wenruo break; 1367496245caSQu Wenruo case BTRFS_INODE_ITEM_KEY: 136839e57f49SDavid Sterba ret = check_inode_item(leaf, key, slot); 1369496245caSQu Wenruo break; 1370259ee775SQu Wenruo case BTRFS_ROOT_ITEM_KEY: 1371259ee775SQu Wenruo ret = check_root_item(leaf, key, slot); 1372259ee775SQu Wenruo break; 1373f82d1c7cSQu Wenruo case BTRFS_EXTENT_ITEM_KEY: 1374f82d1c7cSQu Wenruo case BTRFS_METADATA_ITEM_KEY: 1375f82d1c7cSQu Wenruo ret = check_extent_item(leaf, key, slot); 1376f82d1c7cSQu Wenruo break; 1377e2406a6fSQu Wenruo case BTRFS_TREE_BLOCK_REF_KEY: 1378e2406a6fSQu Wenruo case BTRFS_SHARED_DATA_REF_KEY: 1379e2406a6fSQu Wenruo case BTRFS_SHARED_BLOCK_REF_KEY: 1380e2406a6fSQu Wenruo ret = check_simple_keyed_refs(leaf, key, slot); 1381e2406a6fSQu Wenruo break; 13820785a9aaSQu Wenruo case BTRFS_EXTENT_DATA_REF_KEY: 13830785a9aaSQu Wenruo ret = check_extent_data_ref(leaf, key, slot); 13840785a9aaSQu Wenruo break; 1385557ea5ddSQu Wenruo } 1386557ea5ddSQu Wenruo return ret; 1387557ea5ddSQu Wenruo } 1388557ea5ddSQu Wenruo 1389e2ccd361SDavid Sterba static int check_leaf(struct extent_buffer *leaf, bool check_item_data) 1390557ea5ddSQu Wenruo { 1391e2ccd361SDavid Sterba struct btrfs_fs_info *fs_info = leaf->fs_info; 1392557ea5ddSQu Wenruo /* No valid key type is 0, so all key should be larger than this key */ 1393557ea5ddSQu Wenruo struct btrfs_key prev_key = {0, 0, 0}; 1394557ea5ddSQu Wenruo struct btrfs_key key; 1395557ea5ddSQu Wenruo u32 nritems = btrfs_header_nritems(leaf); 1396557ea5ddSQu Wenruo int slot; 1397557ea5ddSQu Wenruo 1398f556faa4SQu Wenruo if (btrfs_header_level(leaf) != 0) { 139986a6be3aSDavid Sterba generic_err(leaf, 0, 1400f556faa4SQu Wenruo "invalid level for leaf, have %d expect 0", 1401f556faa4SQu Wenruo btrfs_header_level(leaf)); 1402f556faa4SQu Wenruo return -EUCLEAN; 1403f556faa4SQu Wenruo } 1404f556faa4SQu Wenruo 1405557ea5ddSQu Wenruo /* 1406557ea5ddSQu Wenruo * Extent buffers from a relocation tree have a owner field that 1407557ea5ddSQu Wenruo * corresponds to the subvolume tree they are based on. So just from an 1408557ea5ddSQu Wenruo * extent buffer alone we can not find out what is the id of the 1409557ea5ddSQu Wenruo * corresponding subvolume tree, so we can not figure out if the extent 1410557ea5ddSQu Wenruo * buffer corresponds to the root of the relocation tree or not. So 1411557ea5ddSQu Wenruo * skip this check for relocation trees. 1412557ea5ddSQu Wenruo */ 1413557ea5ddSQu Wenruo if (nritems == 0 && !btrfs_header_flag(leaf, BTRFS_HEADER_FLAG_RELOC)) { 1414ba480dd4SQu Wenruo u64 owner = btrfs_header_owner(leaf); 1415557ea5ddSQu Wenruo 1416ba480dd4SQu Wenruo /* These trees must never be empty */ 1417ba480dd4SQu Wenruo if (owner == BTRFS_ROOT_TREE_OBJECTID || 1418ba480dd4SQu Wenruo owner == BTRFS_CHUNK_TREE_OBJECTID || 1419ba480dd4SQu Wenruo owner == BTRFS_EXTENT_TREE_OBJECTID || 1420ba480dd4SQu Wenruo owner == BTRFS_DEV_TREE_OBJECTID || 1421ba480dd4SQu Wenruo owner == BTRFS_FS_TREE_OBJECTID || 1422ba480dd4SQu Wenruo owner == BTRFS_DATA_RELOC_TREE_OBJECTID) { 142386a6be3aSDavid Sterba generic_err(leaf, 0, 1424ba480dd4SQu Wenruo "invalid root, root %llu must never be empty", 1425ba480dd4SQu Wenruo owner); 1426ba480dd4SQu Wenruo return -EUCLEAN; 1427ba480dd4SQu Wenruo } 142862fdaa52SQu Wenruo /* Unknown tree */ 142962fdaa52SQu Wenruo if (owner == 0) { 143062fdaa52SQu Wenruo generic_err(leaf, 0, 143162fdaa52SQu Wenruo "invalid owner, root 0 is not defined"); 143262fdaa52SQu Wenruo return -EUCLEAN; 143362fdaa52SQu Wenruo } 1434557ea5ddSQu Wenruo return 0; 1435557ea5ddSQu Wenruo } 1436557ea5ddSQu Wenruo 1437557ea5ddSQu Wenruo if (nritems == 0) 1438557ea5ddSQu Wenruo return 0; 1439557ea5ddSQu Wenruo 1440557ea5ddSQu Wenruo /* 1441557ea5ddSQu Wenruo * Check the following things to make sure this is a good leaf, and 1442557ea5ddSQu Wenruo * leaf users won't need to bother with similar sanity checks: 1443557ea5ddSQu Wenruo * 1444557ea5ddSQu Wenruo * 1) key ordering 1445557ea5ddSQu Wenruo * 2) item offset and size 1446557ea5ddSQu Wenruo * No overlap, no hole, all inside the leaf. 1447557ea5ddSQu Wenruo * 3) item content 1448557ea5ddSQu Wenruo * If possible, do comprehensive sanity check. 1449557ea5ddSQu Wenruo * NOTE: All checks must only rely on the item data itself. 1450557ea5ddSQu Wenruo */ 1451557ea5ddSQu Wenruo for (slot = 0; slot < nritems; slot++) { 1452557ea5ddSQu Wenruo u32 item_end_expected; 1453557ea5ddSQu Wenruo int ret; 1454557ea5ddSQu Wenruo 1455557ea5ddSQu Wenruo btrfs_item_key_to_cpu(leaf, &key, slot); 1456557ea5ddSQu Wenruo 1457557ea5ddSQu Wenruo /* Make sure the keys are in the right order */ 1458557ea5ddSQu Wenruo if (btrfs_comp_cpu_keys(&prev_key, &key) >= 0) { 145986a6be3aSDavid Sterba generic_err(leaf, slot, 1460478d01b3SQu Wenruo "bad key order, prev (%llu %u %llu) current (%llu %u %llu)", 1461478d01b3SQu Wenruo prev_key.objectid, prev_key.type, 1462478d01b3SQu Wenruo prev_key.offset, key.objectid, key.type, 1463478d01b3SQu Wenruo key.offset); 1464557ea5ddSQu Wenruo return -EUCLEAN; 1465557ea5ddSQu Wenruo } 1466557ea5ddSQu Wenruo 1467557ea5ddSQu Wenruo /* 1468557ea5ddSQu Wenruo * Make sure the offset and ends are right, remember that the 1469557ea5ddSQu Wenruo * item data starts at the end of the leaf and grows towards the 1470557ea5ddSQu Wenruo * front. 1471557ea5ddSQu Wenruo */ 1472557ea5ddSQu Wenruo if (slot == 0) 1473557ea5ddSQu Wenruo item_end_expected = BTRFS_LEAF_DATA_SIZE(fs_info); 1474557ea5ddSQu Wenruo else 1475557ea5ddSQu Wenruo item_end_expected = btrfs_item_offset_nr(leaf, 1476557ea5ddSQu Wenruo slot - 1); 1477557ea5ddSQu Wenruo if (btrfs_item_end_nr(leaf, slot) != item_end_expected) { 147886a6be3aSDavid Sterba generic_err(leaf, slot, 1479478d01b3SQu Wenruo "unexpected item end, have %u expect %u", 1480478d01b3SQu Wenruo btrfs_item_end_nr(leaf, slot), 1481478d01b3SQu Wenruo item_end_expected); 1482557ea5ddSQu Wenruo return -EUCLEAN; 1483557ea5ddSQu Wenruo } 1484557ea5ddSQu Wenruo 1485557ea5ddSQu Wenruo /* 1486557ea5ddSQu Wenruo * Check to make sure that we don't point outside of the leaf, 1487557ea5ddSQu Wenruo * just in case all the items are consistent to each other, but 1488557ea5ddSQu Wenruo * all point outside of the leaf. 1489557ea5ddSQu Wenruo */ 1490557ea5ddSQu Wenruo if (btrfs_item_end_nr(leaf, slot) > 1491557ea5ddSQu Wenruo BTRFS_LEAF_DATA_SIZE(fs_info)) { 149286a6be3aSDavid Sterba generic_err(leaf, slot, 1493478d01b3SQu Wenruo "slot end outside of leaf, have %u expect range [0, %u]", 1494478d01b3SQu Wenruo btrfs_item_end_nr(leaf, slot), 1495478d01b3SQu Wenruo BTRFS_LEAF_DATA_SIZE(fs_info)); 1496557ea5ddSQu Wenruo return -EUCLEAN; 1497557ea5ddSQu Wenruo } 1498557ea5ddSQu Wenruo 1499557ea5ddSQu Wenruo /* Also check if the item pointer overlaps with btrfs item. */ 1500557ea5ddSQu Wenruo if (btrfs_item_nr_offset(slot) + sizeof(struct btrfs_item) > 1501557ea5ddSQu Wenruo btrfs_item_ptr_offset(leaf, slot)) { 150286a6be3aSDavid Sterba generic_err(leaf, slot, 1503478d01b3SQu Wenruo "slot overlaps with its data, item end %lu data start %lu", 1504478d01b3SQu Wenruo btrfs_item_nr_offset(slot) + 1505478d01b3SQu Wenruo sizeof(struct btrfs_item), 1506478d01b3SQu Wenruo btrfs_item_ptr_offset(leaf, slot)); 1507557ea5ddSQu Wenruo return -EUCLEAN; 1508557ea5ddSQu Wenruo } 1509557ea5ddSQu Wenruo 151069fc6cbbSQu Wenruo if (check_item_data) { 151169fc6cbbSQu Wenruo /* 151269fc6cbbSQu Wenruo * Check if the item size and content meet other 151369fc6cbbSQu Wenruo * criteria 151469fc6cbbSQu Wenruo */ 15154e9845efSFilipe Manana ret = check_leaf_item(leaf, &key, slot, &prev_key); 1516557ea5ddSQu Wenruo if (ret < 0) 1517557ea5ddSQu Wenruo return ret; 151869fc6cbbSQu Wenruo } 1519557ea5ddSQu Wenruo 1520557ea5ddSQu Wenruo prev_key.objectid = key.objectid; 1521557ea5ddSQu Wenruo prev_key.type = key.type; 1522557ea5ddSQu Wenruo prev_key.offset = key.offset; 1523557ea5ddSQu Wenruo } 1524557ea5ddSQu Wenruo 1525557ea5ddSQu Wenruo return 0; 1526557ea5ddSQu Wenruo } 1527557ea5ddSQu Wenruo 15281c4360eeSDavid Sterba int btrfs_check_leaf_full(struct extent_buffer *leaf) 152969fc6cbbSQu Wenruo { 1530e2ccd361SDavid Sterba return check_leaf(leaf, true); 153169fc6cbbSQu Wenruo } 153202529d7aSQu Wenruo ALLOW_ERROR_INJECTION(btrfs_check_leaf_full, ERRNO); 153369fc6cbbSQu Wenruo 1534cfdaad5eSDavid Sterba int btrfs_check_leaf_relaxed(struct extent_buffer *leaf) 15352f659546SQu Wenruo { 1536e2ccd361SDavid Sterba return check_leaf(leaf, false); 15372f659546SQu Wenruo } 15382f659546SQu Wenruo 1539813fd1dcSDavid Sterba int btrfs_check_node(struct extent_buffer *node) 1540557ea5ddSQu Wenruo { 1541813fd1dcSDavid Sterba struct btrfs_fs_info *fs_info = node->fs_info; 1542557ea5ddSQu Wenruo unsigned long nr = btrfs_header_nritems(node); 1543557ea5ddSQu Wenruo struct btrfs_key key, next_key; 1544557ea5ddSQu Wenruo int slot; 1545f556faa4SQu Wenruo int level = btrfs_header_level(node); 1546557ea5ddSQu Wenruo u64 bytenr; 1547557ea5ddSQu Wenruo int ret = 0; 1548557ea5ddSQu Wenruo 1549f556faa4SQu Wenruo if (level <= 0 || level >= BTRFS_MAX_LEVEL) { 155086a6be3aSDavid Sterba generic_err(node, 0, 1551f556faa4SQu Wenruo "invalid level for node, have %d expect [1, %d]", 1552f556faa4SQu Wenruo level, BTRFS_MAX_LEVEL - 1); 1553f556faa4SQu Wenruo return -EUCLEAN; 1554f556faa4SQu Wenruo } 15552f659546SQu Wenruo if (nr == 0 || nr > BTRFS_NODEPTRS_PER_BLOCK(fs_info)) { 15562f659546SQu Wenruo btrfs_crit(fs_info, 1557bba4f298SQu Wenruo "corrupt node: root=%llu block=%llu, nritems too %s, have %lu expect range [1,%u]", 15582f659546SQu Wenruo btrfs_header_owner(node), node->start, 1559bba4f298SQu Wenruo nr == 0 ? "small" : "large", nr, 15602f659546SQu Wenruo BTRFS_NODEPTRS_PER_BLOCK(fs_info)); 1561bba4f298SQu Wenruo return -EUCLEAN; 1562557ea5ddSQu Wenruo } 1563557ea5ddSQu Wenruo 1564557ea5ddSQu Wenruo for (slot = 0; slot < nr - 1; slot++) { 1565557ea5ddSQu Wenruo bytenr = btrfs_node_blockptr(node, slot); 1566557ea5ddSQu Wenruo btrfs_node_key_to_cpu(node, &key, slot); 1567557ea5ddSQu Wenruo btrfs_node_key_to_cpu(node, &next_key, slot + 1); 1568557ea5ddSQu Wenruo 1569557ea5ddSQu Wenruo if (!bytenr) { 157086a6be3aSDavid Sterba generic_err(node, slot, 1571bba4f298SQu Wenruo "invalid NULL node pointer"); 1572bba4f298SQu Wenruo ret = -EUCLEAN; 1573bba4f298SQu Wenruo goto out; 1574bba4f298SQu Wenruo } 15752f659546SQu Wenruo if (!IS_ALIGNED(bytenr, fs_info->sectorsize)) { 157686a6be3aSDavid Sterba generic_err(node, slot, 1577bba4f298SQu Wenruo "unaligned pointer, have %llu should be aligned to %u", 15782f659546SQu Wenruo bytenr, fs_info->sectorsize); 1579bba4f298SQu Wenruo ret = -EUCLEAN; 1580557ea5ddSQu Wenruo goto out; 1581557ea5ddSQu Wenruo } 1582557ea5ddSQu Wenruo 1583557ea5ddSQu Wenruo if (btrfs_comp_cpu_keys(&key, &next_key) >= 0) { 158486a6be3aSDavid Sterba generic_err(node, slot, 1585bba4f298SQu Wenruo "bad key order, current (%llu %u %llu) next (%llu %u %llu)", 1586bba4f298SQu Wenruo key.objectid, key.type, key.offset, 1587bba4f298SQu Wenruo next_key.objectid, next_key.type, 1588bba4f298SQu Wenruo next_key.offset); 1589bba4f298SQu Wenruo ret = -EUCLEAN; 1590557ea5ddSQu Wenruo goto out; 1591557ea5ddSQu Wenruo } 1592557ea5ddSQu Wenruo } 1593557ea5ddSQu Wenruo out: 1594557ea5ddSQu Wenruo return ret; 1595557ea5ddSQu Wenruo } 159602529d7aSQu Wenruo ALLOW_ERROR_INJECTION(btrfs_check_node, ERRNO); 1597