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" 2777eea05eSBoris Burkov #include "btrfs_inode.h" 28557ea5ddSQu Wenruo 29bba4f298SQu Wenruo /* 30bba4f298SQu Wenruo * Error message should follow the following format: 31bba4f298SQu Wenruo * corrupt <type>: <identifier>, <reason>[, <bad_value>] 32bba4f298SQu Wenruo * 33bba4f298SQu Wenruo * @type: leaf or node 34bba4f298SQu Wenruo * @identifier: the necessary info to locate the leaf/node. 3552042d8eSAndrea Gelmini * It's recommended to decode key.objecitd/offset if it's 36bba4f298SQu Wenruo * meaningful. 37bba4f298SQu Wenruo * @reason: describe the error 3852042d8eSAndrea Gelmini * @bad_value: optional, it's recommended to output bad value and its 39bba4f298SQu Wenruo * expected value (range). 40bba4f298SQu Wenruo * 41bba4f298SQu Wenruo * Since comma is used to separate the components, only space is allowed 42bba4f298SQu Wenruo * inside each component. 43bba4f298SQu Wenruo */ 44bba4f298SQu Wenruo 45bba4f298SQu Wenruo /* 46bba4f298SQu Wenruo * Append generic "corrupt leaf/node root=%llu block=%llu slot=%d: " to @fmt. 47bba4f298SQu Wenruo * Allows callers to customize the output. 48bba4f298SQu Wenruo */ 4986a6be3aSDavid Sterba __printf(3, 4) 50e67c718bSDavid Sterba __cold 5186a6be3aSDavid Sterba static void generic_err(const struct extent_buffer *eb, int slot, 52bba4f298SQu Wenruo const char *fmt, ...) 53bba4f298SQu Wenruo { 5486a6be3aSDavid Sterba const struct btrfs_fs_info *fs_info = eb->fs_info; 55bba4f298SQu Wenruo struct va_format vaf; 56bba4f298SQu Wenruo va_list args; 57bba4f298SQu Wenruo 58bba4f298SQu Wenruo va_start(args, fmt); 59bba4f298SQu Wenruo 60bba4f298SQu Wenruo vaf.fmt = fmt; 61bba4f298SQu Wenruo vaf.va = &args; 62bba4f298SQu Wenruo 632f659546SQu Wenruo btrfs_crit(fs_info, 64bba4f298SQu Wenruo "corrupt %s: root=%llu block=%llu slot=%d, %pV", 65bba4f298SQu Wenruo btrfs_header_level(eb) == 0 ? "leaf" : "node", 662f659546SQu Wenruo btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot, &vaf); 67bba4f298SQu Wenruo va_end(args); 68bba4f298SQu Wenruo } 69bba4f298SQu Wenruo 708806d718SQu Wenruo /* 718806d718SQu Wenruo * Customized reporter for extent data item, since its key objectid and 728806d718SQu Wenruo * offset has its own meaning. 738806d718SQu Wenruo */ 741fd715ffSDavid Sterba __printf(3, 4) 75e67c718bSDavid Sterba __cold 761fd715ffSDavid Sterba static void file_extent_err(const struct extent_buffer *eb, int slot, 778806d718SQu Wenruo const char *fmt, ...) 788806d718SQu Wenruo { 791fd715ffSDavid Sterba const struct btrfs_fs_info *fs_info = eb->fs_info; 808806d718SQu Wenruo struct btrfs_key key; 818806d718SQu Wenruo struct va_format vaf; 828806d718SQu Wenruo va_list args; 838806d718SQu Wenruo 848806d718SQu Wenruo btrfs_item_key_to_cpu(eb, &key, slot); 858806d718SQu Wenruo va_start(args, fmt); 868806d718SQu Wenruo 878806d718SQu Wenruo vaf.fmt = fmt; 888806d718SQu Wenruo vaf.va = &args; 898806d718SQu Wenruo 902f659546SQu Wenruo btrfs_crit(fs_info, 918806d718SQu Wenruo "corrupt %s: root=%llu block=%llu slot=%d ino=%llu file_offset=%llu, %pV", 922f659546SQu Wenruo btrfs_header_level(eb) == 0 ? "leaf" : "node", 932f659546SQu Wenruo btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot, 942f659546SQu Wenruo key.objectid, key.offset, &vaf); 958806d718SQu Wenruo va_end(args); 968806d718SQu Wenruo } 978806d718SQu Wenruo 988806d718SQu Wenruo /* 998806d718SQu Wenruo * Return 0 if the btrfs_file_extent_##name is aligned to @alignment 1008806d718SQu Wenruo * Else return 1 1018806d718SQu Wenruo */ 102033774dcSDavid Sterba #define CHECK_FE_ALIGNED(leaf, slot, fi, name, alignment) \ 1038806d718SQu Wenruo ({ \ 104c7c01a4aSDavid Sterba if (unlikely(!IS_ALIGNED(btrfs_file_extent_##name((leaf), (fi)), \ 105c7c01a4aSDavid Sterba (alignment)))) \ 1061fd715ffSDavid Sterba file_extent_err((leaf), (slot), \ 1078806d718SQu Wenruo "invalid %s for file extent, have %llu, should be aligned to %u", \ 1088806d718SQu Wenruo (#name), btrfs_file_extent_##name((leaf), (fi)), \ 1098806d718SQu Wenruo (alignment)); \ 1108806d718SQu Wenruo (!IS_ALIGNED(btrfs_file_extent_##name((leaf), (fi)), (alignment))); \ 1118806d718SQu Wenruo }) 1128806d718SQu Wenruo 1134e9845efSFilipe Manana static u64 file_extent_end(struct extent_buffer *leaf, 1144e9845efSFilipe Manana struct btrfs_key *key, 1154e9845efSFilipe Manana struct btrfs_file_extent_item *extent) 1164e9845efSFilipe Manana { 1174e9845efSFilipe Manana u64 end; 1184e9845efSFilipe Manana u64 len; 1194e9845efSFilipe Manana 1204e9845efSFilipe Manana if (btrfs_file_extent_type(leaf, extent) == BTRFS_FILE_EXTENT_INLINE) { 1214e9845efSFilipe Manana len = btrfs_file_extent_ram_bytes(leaf, extent); 1224e9845efSFilipe Manana end = ALIGN(key->offset + len, leaf->fs_info->sectorsize); 1234e9845efSFilipe Manana } else { 1244e9845efSFilipe Manana len = btrfs_file_extent_num_bytes(leaf, extent); 1254e9845efSFilipe Manana end = key->offset + len; 1264e9845efSFilipe Manana } 1274e9845efSFilipe Manana return end; 1284e9845efSFilipe Manana } 1294e9845efSFilipe Manana 13080d7fd1eSQu Wenruo /* 13180d7fd1eSQu Wenruo * Customized report for dir_item, the only new important information is 13280d7fd1eSQu Wenruo * key->objectid, which represents inode number 13380d7fd1eSQu Wenruo */ 13480d7fd1eSQu Wenruo __printf(3, 4) 13580d7fd1eSQu Wenruo __cold 13680d7fd1eSQu Wenruo static void dir_item_err(const struct extent_buffer *eb, int slot, 13780d7fd1eSQu Wenruo const char *fmt, ...) 13880d7fd1eSQu Wenruo { 13980d7fd1eSQu Wenruo const struct btrfs_fs_info *fs_info = eb->fs_info; 14080d7fd1eSQu Wenruo struct btrfs_key key; 14180d7fd1eSQu Wenruo struct va_format vaf; 14280d7fd1eSQu Wenruo va_list args; 14380d7fd1eSQu Wenruo 14480d7fd1eSQu Wenruo btrfs_item_key_to_cpu(eb, &key, slot); 14580d7fd1eSQu Wenruo va_start(args, fmt); 14680d7fd1eSQu Wenruo 14780d7fd1eSQu Wenruo vaf.fmt = fmt; 14880d7fd1eSQu Wenruo vaf.va = &args; 14980d7fd1eSQu Wenruo 15080d7fd1eSQu Wenruo btrfs_crit(fs_info, 15180d7fd1eSQu Wenruo "corrupt %s: root=%llu block=%llu slot=%d ino=%llu, %pV", 15280d7fd1eSQu Wenruo btrfs_header_level(eb) == 0 ? "leaf" : "node", 15380d7fd1eSQu Wenruo btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot, 15480d7fd1eSQu Wenruo key.objectid, &vaf); 15580d7fd1eSQu Wenruo va_end(args); 15680d7fd1eSQu Wenruo } 15780d7fd1eSQu Wenruo 15880d7fd1eSQu Wenruo /* 15980d7fd1eSQu Wenruo * This functions checks prev_key->objectid, to ensure current key and prev_key 16080d7fd1eSQu Wenruo * share the same objectid as inode number. 16180d7fd1eSQu Wenruo * 16280d7fd1eSQu Wenruo * This is to detect missing INODE_ITEM in subvolume trees. 16380d7fd1eSQu Wenruo * 16480d7fd1eSQu Wenruo * Return true if everything is OK or we don't need to check. 16580d7fd1eSQu Wenruo * Return false if anything is wrong. 16680d7fd1eSQu Wenruo */ 16780d7fd1eSQu Wenruo static bool check_prev_ino(struct extent_buffer *leaf, 16880d7fd1eSQu Wenruo struct btrfs_key *key, int slot, 16980d7fd1eSQu Wenruo struct btrfs_key *prev_key) 17080d7fd1eSQu Wenruo { 17180d7fd1eSQu Wenruo /* No prev key, skip check */ 17280d7fd1eSQu Wenruo if (slot == 0) 17380d7fd1eSQu Wenruo return true; 17480d7fd1eSQu Wenruo 17580d7fd1eSQu Wenruo /* Only these key->types needs to be checked */ 17680d7fd1eSQu Wenruo ASSERT(key->type == BTRFS_XATTR_ITEM_KEY || 17780d7fd1eSQu Wenruo key->type == BTRFS_INODE_REF_KEY || 17880d7fd1eSQu Wenruo key->type == BTRFS_DIR_INDEX_KEY || 17980d7fd1eSQu Wenruo key->type == BTRFS_DIR_ITEM_KEY || 18080d7fd1eSQu Wenruo key->type == BTRFS_EXTENT_DATA_KEY); 18180d7fd1eSQu Wenruo 18280d7fd1eSQu Wenruo /* 18380d7fd1eSQu Wenruo * Only subvolume trees along with their reloc trees need this check. 18480d7fd1eSQu Wenruo * Things like log tree doesn't follow this ino requirement. 18580d7fd1eSQu Wenruo */ 18680d7fd1eSQu Wenruo if (!is_fstree(btrfs_header_owner(leaf))) 18780d7fd1eSQu Wenruo return true; 18880d7fd1eSQu Wenruo 18980d7fd1eSQu Wenruo if (key->objectid == prev_key->objectid) 19080d7fd1eSQu Wenruo return true; 19180d7fd1eSQu Wenruo 19280d7fd1eSQu Wenruo /* Error found */ 19380d7fd1eSQu Wenruo dir_item_err(leaf, slot, 19480d7fd1eSQu Wenruo "invalid previous key objectid, have %llu expect %llu", 19580d7fd1eSQu Wenruo prev_key->objectid, key->objectid); 19680d7fd1eSQu Wenruo return false; 19780d7fd1eSQu Wenruo } 198ae2a19d8SDavid Sterba static int check_extent_data_item(struct extent_buffer *leaf, 1994e9845efSFilipe Manana struct btrfs_key *key, int slot, 2004e9845efSFilipe Manana struct btrfs_key *prev_key) 201557ea5ddSQu Wenruo { 202ae2a19d8SDavid Sterba struct btrfs_fs_info *fs_info = leaf->fs_info; 203557ea5ddSQu Wenruo struct btrfs_file_extent_item *fi; 2042f659546SQu Wenruo u32 sectorsize = fs_info->sectorsize; 2053212fa14SJosef Bacik u32 item_size = btrfs_item_size(leaf, slot); 2064c094c33SQu Wenruo u64 extent_end; 207557ea5ddSQu Wenruo 208c7c01a4aSDavid Sterba if (unlikely(!IS_ALIGNED(key->offset, sectorsize))) { 2091fd715ffSDavid Sterba file_extent_err(leaf, slot, 2108806d718SQu Wenruo "unaligned file_offset for file extent, have %llu should be aligned to %u", 2118806d718SQu Wenruo key->offset, sectorsize); 212557ea5ddSQu Wenruo return -EUCLEAN; 213557ea5ddSQu Wenruo } 214557ea5ddSQu Wenruo 215c18679ebSQu Wenruo /* 216c18679ebSQu Wenruo * Previous key must have the same key->objectid (ino). 217c18679ebSQu Wenruo * It can be XATTR_ITEM, INODE_ITEM or just another EXTENT_DATA. 218c18679ebSQu Wenruo * But if objectids mismatch, it means we have a missing 219c18679ebSQu Wenruo * INODE_ITEM. 220c18679ebSQu Wenruo */ 221c7c01a4aSDavid Sterba if (unlikely(!check_prev_ino(leaf, key, slot, prev_key))) 222c18679ebSQu Wenruo return -EUCLEAN; 223c18679ebSQu Wenruo 224557ea5ddSQu Wenruo fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); 225557ea5ddSQu Wenruo 226153a6d29SQu Wenruo /* 227153a6d29SQu Wenruo * Make sure the item contains at least inline header, so the file 228153a6d29SQu Wenruo * extent type is not some garbage. 229153a6d29SQu Wenruo */ 230c7c01a4aSDavid Sterba if (unlikely(item_size < BTRFS_FILE_EXTENT_INLINE_DATA_START)) { 231153a6d29SQu Wenruo file_extent_err(leaf, slot, 232994bf9cdSAndreas Färber "invalid item size, have %u expect [%zu, %u)", 233153a6d29SQu Wenruo item_size, BTRFS_FILE_EXTENT_INLINE_DATA_START, 234153a6d29SQu Wenruo SZ_4K); 235153a6d29SQu Wenruo return -EUCLEAN; 236153a6d29SQu Wenruo } 237c7c01a4aSDavid Sterba if (unlikely(btrfs_file_extent_type(leaf, fi) >= 238c7c01a4aSDavid Sterba BTRFS_NR_FILE_EXTENT_TYPES)) { 2391fd715ffSDavid Sterba file_extent_err(leaf, slot, 2408806d718SQu Wenruo "invalid type for file extent, have %u expect range [0, %u]", 2418806d718SQu Wenruo btrfs_file_extent_type(leaf, fi), 242b9b1a53eSChengguang Xu BTRFS_NR_FILE_EXTENT_TYPES - 1); 243557ea5ddSQu Wenruo return -EUCLEAN; 244557ea5ddSQu Wenruo } 245557ea5ddSQu Wenruo 246557ea5ddSQu Wenruo /* 24752042d8eSAndrea Gelmini * Support for new compression/encryption must introduce incompat flag, 248557ea5ddSQu Wenruo * and must be caught in open_ctree(). 249557ea5ddSQu Wenruo */ 250c7c01a4aSDavid Sterba if (unlikely(btrfs_file_extent_compression(leaf, fi) >= 251c7c01a4aSDavid Sterba BTRFS_NR_COMPRESS_TYPES)) { 2521fd715ffSDavid Sterba file_extent_err(leaf, slot, 2538806d718SQu Wenruo "invalid compression for file extent, have %u expect range [0, %u]", 2548806d718SQu Wenruo btrfs_file_extent_compression(leaf, fi), 255ce96b7ffSChengguang Xu BTRFS_NR_COMPRESS_TYPES - 1); 256557ea5ddSQu Wenruo return -EUCLEAN; 257557ea5ddSQu Wenruo } 258c7c01a4aSDavid Sterba if (unlikely(btrfs_file_extent_encryption(leaf, fi))) { 2591fd715ffSDavid Sterba file_extent_err(leaf, slot, 2608806d718SQu Wenruo "invalid encryption for file extent, have %u expect 0", 2618806d718SQu Wenruo btrfs_file_extent_encryption(leaf, fi)); 262557ea5ddSQu Wenruo return -EUCLEAN; 263557ea5ddSQu Wenruo } 264557ea5ddSQu Wenruo if (btrfs_file_extent_type(leaf, fi) == BTRFS_FILE_EXTENT_INLINE) { 265557ea5ddSQu Wenruo /* Inline extent must have 0 as key offset */ 266c7c01a4aSDavid Sterba if (unlikely(key->offset)) { 2671fd715ffSDavid Sterba file_extent_err(leaf, slot, 2688806d718SQu Wenruo "invalid file_offset for inline file extent, have %llu expect 0", 2698806d718SQu Wenruo key->offset); 270557ea5ddSQu Wenruo return -EUCLEAN; 271557ea5ddSQu Wenruo } 272557ea5ddSQu Wenruo 273557ea5ddSQu Wenruo /* Compressed inline extent has no on-disk size, skip it */ 274557ea5ddSQu Wenruo if (btrfs_file_extent_compression(leaf, fi) != 275557ea5ddSQu Wenruo BTRFS_COMPRESS_NONE) 276557ea5ddSQu Wenruo return 0; 277557ea5ddSQu Wenruo 278557ea5ddSQu Wenruo /* Uncompressed inline extent size must match item size */ 279c7c01a4aSDavid Sterba if (unlikely(item_size != BTRFS_FILE_EXTENT_INLINE_DATA_START + 280c7c01a4aSDavid Sterba btrfs_file_extent_ram_bytes(leaf, fi))) { 2811fd715ffSDavid Sterba file_extent_err(leaf, slot, 2828806d718SQu Wenruo "invalid ram_bytes for uncompressed inline extent, have %u expect %llu", 2838806d718SQu Wenruo item_size, BTRFS_FILE_EXTENT_INLINE_DATA_START + 2848806d718SQu Wenruo btrfs_file_extent_ram_bytes(leaf, fi)); 285557ea5ddSQu Wenruo return -EUCLEAN; 286557ea5ddSQu Wenruo } 287557ea5ddSQu Wenruo return 0; 288557ea5ddSQu Wenruo } 289557ea5ddSQu Wenruo 290557ea5ddSQu Wenruo /* Regular or preallocated extent has fixed item size */ 291c7c01a4aSDavid Sterba if (unlikely(item_size != sizeof(*fi))) { 2921fd715ffSDavid Sterba file_extent_err(leaf, slot, 293709a95c3SArnd Bergmann "invalid item size for reg/prealloc file extent, have %u expect %zu", 2948806d718SQu Wenruo item_size, sizeof(*fi)); 295557ea5ddSQu Wenruo return -EUCLEAN; 296557ea5ddSQu Wenruo } 297c7c01a4aSDavid Sterba if (unlikely(CHECK_FE_ALIGNED(leaf, slot, fi, ram_bytes, sectorsize) || 298033774dcSDavid Sterba CHECK_FE_ALIGNED(leaf, slot, fi, disk_bytenr, sectorsize) || 299033774dcSDavid Sterba CHECK_FE_ALIGNED(leaf, slot, fi, disk_num_bytes, sectorsize) || 300033774dcSDavid Sterba CHECK_FE_ALIGNED(leaf, slot, fi, offset, sectorsize) || 301c7c01a4aSDavid Sterba CHECK_FE_ALIGNED(leaf, slot, fi, num_bytes, sectorsize))) 302557ea5ddSQu Wenruo return -EUCLEAN; 3034e9845efSFilipe Manana 3044c094c33SQu Wenruo /* Catch extent end overflow */ 305c7c01a4aSDavid Sterba if (unlikely(check_add_overflow(btrfs_file_extent_num_bytes(leaf, fi), 306c7c01a4aSDavid Sterba key->offset, &extent_end))) { 3074c094c33SQu Wenruo file_extent_err(leaf, slot, 3084c094c33SQu Wenruo "extent end overflow, have file offset %llu extent num bytes %llu", 3094c094c33SQu Wenruo key->offset, 3104c094c33SQu Wenruo btrfs_file_extent_num_bytes(leaf, fi)); 3114c094c33SQu Wenruo return -EUCLEAN; 3124c094c33SQu Wenruo } 3134c094c33SQu Wenruo 3144e9845efSFilipe Manana /* 3154e9845efSFilipe Manana * Check that no two consecutive file extent items, in the same leaf, 3164e9845efSFilipe Manana * present ranges that overlap each other. 3174e9845efSFilipe Manana */ 3184e9845efSFilipe Manana if (slot > 0 && 3194e9845efSFilipe Manana prev_key->objectid == key->objectid && 3204e9845efSFilipe Manana prev_key->type == BTRFS_EXTENT_DATA_KEY) { 3214e9845efSFilipe Manana struct btrfs_file_extent_item *prev_fi; 3224e9845efSFilipe Manana u64 prev_end; 3234e9845efSFilipe Manana 3244e9845efSFilipe Manana prev_fi = btrfs_item_ptr(leaf, slot - 1, 3254e9845efSFilipe Manana struct btrfs_file_extent_item); 3264e9845efSFilipe Manana prev_end = file_extent_end(leaf, prev_key, prev_fi); 327c7c01a4aSDavid Sterba if (unlikely(prev_end > key->offset)) { 3284e9845efSFilipe Manana file_extent_err(leaf, slot - 1, 3294e9845efSFilipe Manana "file extent end range (%llu) goes beyond start offset (%llu) of the next file extent", 3304e9845efSFilipe Manana prev_end, key->offset); 3314e9845efSFilipe Manana return -EUCLEAN; 3324e9845efSFilipe Manana } 3334e9845efSFilipe Manana } 3344e9845efSFilipe Manana 335557ea5ddSQu Wenruo return 0; 336557ea5ddSQu Wenruo } 337557ea5ddSQu Wenruo 33868128ce7SDavid Sterba static int check_csum_item(struct extent_buffer *leaf, struct btrfs_key *key, 339ad1d8c43SFilipe Manana int slot, struct btrfs_key *prev_key) 340557ea5ddSQu Wenruo { 34168128ce7SDavid Sterba struct btrfs_fs_info *fs_info = leaf->fs_info; 3422f659546SQu Wenruo u32 sectorsize = fs_info->sectorsize; 343223486c2SDavid Sterba const u32 csumsize = fs_info->csum_size; 344557ea5ddSQu Wenruo 345c7c01a4aSDavid Sterba if (unlikely(key->objectid != BTRFS_EXTENT_CSUM_OBJECTID)) { 34686a6be3aSDavid Sterba generic_err(leaf, slot, 347d508c5f0SQu Wenruo "invalid key objectid for csum item, have %llu expect %llu", 348d508c5f0SQu Wenruo key->objectid, BTRFS_EXTENT_CSUM_OBJECTID); 349557ea5ddSQu Wenruo return -EUCLEAN; 350557ea5ddSQu Wenruo } 351c7c01a4aSDavid Sterba if (unlikely(!IS_ALIGNED(key->offset, sectorsize))) { 35286a6be3aSDavid Sterba generic_err(leaf, slot, 353d508c5f0SQu Wenruo "unaligned key offset for csum item, have %llu should be aligned to %u", 354d508c5f0SQu Wenruo key->offset, sectorsize); 355557ea5ddSQu Wenruo return -EUCLEAN; 356557ea5ddSQu Wenruo } 3573212fa14SJosef Bacik if (unlikely(!IS_ALIGNED(btrfs_item_size(leaf, slot), csumsize))) { 35886a6be3aSDavid Sterba generic_err(leaf, slot, 359d508c5f0SQu Wenruo "unaligned item size for csum item, have %u should be aligned to %u", 3603212fa14SJosef Bacik btrfs_item_size(leaf, slot), csumsize); 361557ea5ddSQu Wenruo return -EUCLEAN; 362557ea5ddSQu Wenruo } 363ad1d8c43SFilipe Manana if (slot > 0 && prev_key->type == BTRFS_EXTENT_CSUM_KEY) { 364ad1d8c43SFilipe Manana u64 prev_csum_end; 365ad1d8c43SFilipe Manana u32 prev_item_size; 366ad1d8c43SFilipe Manana 3673212fa14SJosef Bacik prev_item_size = btrfs_item_size(leaf, slot - 1); 368ad1d8c43SFilipe Manana prev_csum_end = (prev_item_size / csumsize) * sectorsize; 369ad1d8c43SFilipe Manana prev_csum_end += prev_key->offset; 370c7c01a4aSDavid Sterba if (unlikely(prev_csum_end > key->offset)) { 371ad1d8c43SFilipe Manana generic_err(leaf, slot - 1, 372ad1d8c43SFilipe Manana "csum end range (%llu) goes beyond the start range (%llu) of the next csum item", 373ad1d8c43SFilipe Manana prev_csum_end, key->offset); 374ad1d8c43SFilipe Manana return -EUCLEAN; 375ad1d8c43SFilipe Manana } 376ad1d8c43SFilipe Manana } 377557ea5ddSQu Wenruo return 0; 378557ea5ddSQu Wenruo } 379557ea5ddSQu Wenruo 380c23c77b0SQu Wenruo /* Inode item error output has the same format as dir_item_err() */ 381c23c77b0SQu Wenruo #define inode_item_err(eb, slot, fmt, ...) \ 382c23c77b0SQu Wenruo dir_item_err(eb, slot, fmt, __VA_ARGS__) 383c23c77b0SQu Wenruo 384c23c77b0SQu Wenruo static int check_inode_key(struct extent_buffer *leaf, struct btrfs_key *key, 385c23c77b0SQu Wenruo int slot) 386c23c77b0SQu Wenruo { 387c23c77b0SQu Wenruo struct btrfs_key item_key; 388c23c77b0SQu Wenruo bool is_inode_item; 389c23c77b0SQu Wenruo 390c23c77b0SQu Wenruo btrfs_item_key_to_cpu(leaf, &item_key, slot); 391c23c77b0SQu Wenruo is_inode_item = (item_key.type == BTRFS_INODE_ITEM_KEY); 392c23c77b0SQu Wenruo 393c23c77b0SQu Wenruo /* For XATTR_ITEM, location key should be all 0 */ 394c23c77b0SQu Wenruo if (item_key.type == BTRFS_XATTR_ITEM_KEY) { 395c7c01a4aSDavid Sterba if (unlikely(key->objectid != 0 || key->type != 0 || 396c7c01a4aSDavid Sterba key->offset != 0)) 397c23c77b0SQu Wenruo return -EUCLEAN; 398c23c77b0SQu Wenruo return 0; 399c23c77b0SQu Wenruo } 400c23c77b0SQu Wenruo 401c7c01a4aSDavid Sterba if (unlikely((key->objectid < BTRFS_FIRST_FREE_OBJECTID || 402c23c77b0SQu Wenruo key->objectid > BTRFS_LAST_FREE_OBJECTID) && 403c23c77b0SQu Wenruo key->objectid != BTRFS_ROOT_TREE_DIR_OBJECTID && 404c7c01a4aSDavid Sterba key->objectid != BTRFS_FREE_INO_OBJECTID)) { 405c23c77b0SQu Wenruo if (is_inode_item) { 406c23c77b0SQu Wenruo generic_err(leaf, slot, 407c23c77b0SQu Wenruo "invalid key objectid: has %llu expect %llu or [%llu, %llu] or %llu", 408c23c77b0SQu Wenruo key->objectid, BTRFS_ROOT_TREE_DIR_OBJECTID, 409c23c77b0SQu Wenruo BTRFS_FIRST_FREE_OBJECTID, 410c23c77b0SQu Wenruo BTRFS_LAST_FREE_OBJECTID, 411c23c77b0SQu Wenruo BTRFS_FREE_INO_OBJECTID); 412c23c77b0SQu Wenruo } else { 413c23c77b0SQu Wenruo dir_item_err(leaf, slot, 414c23c77b0SQu Wenruo "invalid location key objectid: has %llu expect %llu or [%llu, %llu] or %llu", 415c23c77b0SQu Wenruo key->objectid, BTRFS_ROOT_TREE_DIR_OBJECTID, 416c23c77b0SQu Wenruo BTRFS_FIRST_FREE_OBJECTID, 417c23c77b0SQu Wenruo BTRFS_LAST_FREE_OBJECTID, 418c23c77b0SQu Wenruo BTRFS_FREE_INO_OBJECTID); 419c23c77b0SQu Wenruo } 420c23c77b0SQu Wenruo return -EUCLEAN; 421c23c77b0SQu Wenruo } 422c7c01a4aSDavid Sterba if (unlikely(key->offset != 0)) { 423c23c77b0SQu Wenruo if (is_inode_item) 424c23c77b0SQu Wenruo inode_item_err(leaf, slot, 425c23c77b0SQu Wenruo "invalid key offset: has %llu expect 0", 426c23c77b0SQu Wenruo key->offset); 427c23c77b0SQu Wenruo else 428c23c77b0SQu Wenruo dir_item_err(leaf, slot, 429c23c77b0SQu Wenruo "invalid location key offset:has %llu expect 0", 430c23c77b0SQu Wenruo key->offset); 431c23c77b0SQu Wenruo return -EUCLEAN; 432c23c77b0SQu Wenruo } 433c23c77b0SQu Wenruo return 0; 434c23c77b0SQu Wenruo } 435c23c77b0SQu Wenruo 43657a0e674SQu Wenruo static int check_root_key(struct extent_buffer *leaf, struct btrfs_key *key, 43757a0e674SQu Wenruo int slot) 43857a0e674SQu Wenruo { 43957a0e674SQu Wenruo struct btrfs_key item_key; 44057a0e674SQu Wenruo bool is_root_item; 44157a0e674SQu Wenruo 44257a0e674SQu Wenruo btrfs_item_key_to_cpu(leaf, &item_key, slot); 44357a0e674SQu Wenruo is_root_item = (item_key.type == BTRFS_ROOT_ITEM_KEY); 44457a0e674SQu Wenruo 44557a0e674SQu Wenruo /* No such tree id */ 446c7c01a4aSDavid Sterba if (unlikely(key->objectid == 0)) { 44757a0e674SQu Wenruo if (is_root_item) 44857a0e674SQu Wenruo generic_err(leaf, slot, "invalid root id 0"); 44957a0e674SQu Wenruo else 45057a0e674SQu Wenruo dir_item_err(leaf, slot, 45157a0e674SQu Wenruo "invalid location key root id 0"); 45257a0e674SQu Wenruo return -EUCLEAN; 45357a0e674SQu Wenruo } 45457a0e674SQu Wenruo 45557a0e674SQu Wenruo /* DIR_ITEM/INDEX/INODE_REF is not allowed to point to non-fs trees */ 456c7c01a4aSDavid Sterba if (unlikely(!is_fstree(key->objectid) && !is_root_item)) { 45757a0e674SQu Wenruo dir_item_err(leaf, slot, 45857a0e674SQu Wenruo "invalid location key objectid, have %llu expect [%llu, %llu]", 45957a0e674SQu Wenruo key->objectid, BTRFS_FIRST_FREE_OBJECTID, 46057a0e674SQu Wenruo BTRFS_LAST_FREE_OBJECTID); 46157a0e674SQu Wenruo return -EUCLEAN; 46257a0e674SQu Wenruo } 46357a0e674SQu Wenruo 46457a0e674SQu Wenruo /* 46557a0e674SQu Wenruo * ROOT_ITEM with non-zero offset means this is a snapshot, created at 46657a0e674SQu Wenruo * @offset transid. 46757a0e674SQu Wenruo * Furthermore, for location key in DIR_ITEM, its offset is always -1. 46857a0e674SQu Wenruo * 46957a0e674SQu Wenruo * So here we only check offset for reloc tree whose key->offset must 47057a0e674SQu Wenruo * be a valid tree. 47157a0e674SQu Wenruo */ 472c7c01a4aSDavid Sterba if (unlikely(key->objectid == BTRFS_TREE_RELOC_OBJECTID && 473c7c01a4aSDavid Sterba key->offset == 0)) { 47457a0e674SQu Wenruo generic_err(leaf, slot, "invalid root id 0 for reloc tree"); 47557a0e674SQu Wenruo return -EUCLEAN; 47657a0e674SQu Wenruo } 47757a0e674SQu Wenruo return 0; 47857a0e674SQu Wenruo } 47957a0e674SQu Wenruo 480ce4252c0SDavid Sterba static int check_dir_item(struct extent_buffer *leaf, 481c18679ebSQu Wenruo struct btrfs_key *key, struct btrfs_key *prev_key, 482c18679ebSQu Wenruo int slot) 483ad7b0368SQu Wenruo { 484ce4252c0SDavid Sterba struct btrfs_fs_info *fs_info = leaf->fs_info; 485ad7b0368SQu Wenruo struct btrfs_dir_item *di; 4863212fa14SJosef Bacik u32 item_size = btrfs_item_size(leaf, slot); 487ad7b0368SQu Wenruo u32 cur = 0; 488ad7b0368SQu Wenruo 489c7c01a4aSDavid Sterba if (unlikely(!check_prev_ino(leaf, key, slot, prev_key))) 490c18679ebSQu Wenruo return -EUCLEAN; 491c7c01a4aSDavid Sterba 492ad7b0368SQu Wenruo di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); 493ad7b0368SQu Wenruo while (cur < item_size) { 494147a097cSQu Wenruo struct btrfs_key location_key; 495ad7b0368SQu Wenruo u32 name_len; 496ad7b0368SQu Wenruo u32 data_len; 497ad7b0368SQu Wenruo u32 max_name_len; 498ad7b0368SQu Wenruo u32 total_size; 499ad7b0368SQu Wenruo u32 name_hash; 500ad7b0368SQu Wenruo u8 dir_type; 501147a097cSQu Wenruo int ret; 502ad7b0368SQu Wenruo 503ad7b0368SQu Wenruo /* header itself should not cross item boundary */ 504c7c01a4aSDavid Sterba if (unlikely(cur + sizeof(*di) > item_size)) { 505d98ced68SDavid Sterba dir_item_err(leaf, slot, 5067cfad652SArnd Bergmann "dir item header crosses item boundary, have %zu boundary %u", 507ad7b0368SQu Wenruo cur + sizeof(*di), item_size); 508ad7b0368SQu Wenruo return -EUCLEAN; 509ad7b0368SQu Wenruo } 510ad7b0368SQu Wenruo 511147a097cSQu Wenruo /* Location key check */ 512147a097cSQu Wenruo btrfs_dir_item_key_to_cpu(leaf, di, &location_key); 513147a097cSQu Wenruo if (location_key.type == BTRFS_ROOT_ITEM_KEY) { 514147a097cSQu Wenruo ret = check_root_key(leaf, &location_key, slot); 515c7c01a4aSDavid Sterba if (unlikely(ret < 0)) 516147a097cSQu Wenruo return ret; 517147a097cSQu Wenruo } else if (location_key.type == BTRFS_INODE_ITEM_KEY || 518147a097cSQu Wenruo location_key.type == 0) { 519147a097cSQu Wenruo ret = check_inode_key(leaf, &location_key, slot); 520c7c01a4aSDavid Sterba if (unlikely(ret < 0)) 521147a097cSQu Wenruo return ret; 522147a097cSQu Wenruo } else { 523147a097cSQu Wenruo dir_item_err(leaf, slot, 524147a097cSQu Wenruo "invalid location key type, have %u, expect %u or %u", 525147a097cSQu Wenruo location_key.type, BTRFS_ROOT_ITEM_KEY, 526147a097cSQu Wenruo BTRFS_INODE_ITEM_KEY); 527147a097cSQu Wenruo return -EUCLEAN; 528147a097cSQu Wenruo } 529147a097cSQu Wenruo 530ad7b0368SQu Wenruo /* dir type check */ 531ad7b0368SQu Wenruo dir_type = btrfs_dir_type(leaf, di); 532c7c01a4aSDavid Sterba if (unlikely(dir_type >= BTRFS_FT_MAX)) { 533d98ced68SDavid Sterba dir_item_err(leaf, slot, 534ad7b0368SQu Wenruo "invalid dir item type, have %u expect [0, %u)", 535ad7b0368SQu Wenruo dir_type, BTRFS_FT_MAX); 536ad7b0368SQu Wenruo return -EUCLEAN; 537ad7b0368SQu Wenruo } 538ad7b0368SQu Wenruo 539c7c01a4aSDavid Sterba if (unlikely(key->type == BTRFS_XATTR_ITEM_KEY && 540c7c01a4aSDavid Sterba dir_type != BTRFS_FT_XATTR)) { 541d98ced68SDavid Sterba dir_item_err(leaf, slot, 542ad7b0368SQu Wenruo "invalid dir item type for XATTR key, have %u expect %u", 543ad7b0368SQu Wenruo dir_type, BTRFS_FT_XATTR); 544ad7b0368SQu Wenruo return -EUCLEAN; 545ad7b0368SQu Wenruo } 546c7c01a4aSDavid Sterba if (unlikely(dir_type == BTRFS_FT_XATTR && 547c7c01a4aSDavid Sterba key->type != BTRFS_XATTR_ITEM_KEY)) { 548d98ced68SDavid Sterba dir_item_err(leaf, slot, 549ad7b0368SQu Wenruo "xattr dir type found for non-XATTR key"); 550ad7b0368SQu Wenruo return -EUCLEAN; 551ad7b0368SQu Wenruo } 552ad7b0368SQu Wenruo if (dir_type == BTRFS_FT_XATTR) 553ad7b0368SQu Wenruo max_name_len = XATTR_NAME_MAX; 554ad7b0368SQu Wenruo else 555ad7b0368SQu Wenruo max_name_len = BTRFS_NAME_LEN; 556ad7b0368SQu Wenruo 557ad7b0368SQu Wenruo /* Name/data length check */ 558ad7b0368SQu Wenruo name_len = btrfs_dir_name_len(leaf, di); 559ad7b0368SQu Wenruo data_len = btrfs_dir_data_len(leaf, di); 560c7c01a4aSDavid Sterba if (unlikely(name_len > max_name_len)) { 561d98ced68SDavid Sterba dir_item_err(leaf, slot, 562ad7b0368SQu Wenruo "dir item name len too long, have %u max %u", 563ad7b0368SQu Wenruo name_len, max_name_len); 564ad7b0368SQu Wenruo return -EUCLEAN; 565ad7b0368SQu Wenruo } 566c7c01a4aSDavid Sterba if (unlikely(name_len + data_len > BTRFS_MAX_XATTR_SIZE(fs_info))) { 567d98ced68SDavid Sterba dir_item_err(leaf, slot, 568ad7b0368SQu Wenruo "dir item name and data len too long, have %u max %u", 569ad7b0368SQu Wenruo name_len + data_len, 5702f659546SQu Wenruo BTRFS_MAX_XATTR_SIZE(fs_info)); 571ad7b0368SQu Wenruo return -EUCLEAN; 572ad7b0368SQu Wenruo } 573ad7b0368SQu Wenruo 574c7c01a4aSDavid Sterba if (unlikely(data_len && dir_type != BTRFS_FT_XATTR)) { 575d98ced68SDavid Sterba dir_item_err(leaf, slot, 576ad7b0368SQu Wenruo "dir item with invalid data len, have %u expect 0", 577ad7b0368SQu Wenruo data_len); 578ad7b0368SQu Wenruo return -EUCLEAN; 579ad7b0368SQu Wenruo } 580ad7b0368SQu Wenruo 581ad7b0368SQu Wenruo total_size = sizeof(*di) + name_len + data_len; 582ad7b0368SQu Wenruo 583ad7b0368SQu Wenruo /* header and name/data should not cross item boundary */ 584c7c01a4aSDavid Sterba if (unlikely(cur + total_size > item_size)) { 585d98ced68SDavid Sterba dir_item_err(leaf, slot, 586ad7b0368SQu Wenruo "dir item data crosses item boundary, have %u boundary %u", 587ad7b0368SQu Wenruo cur + total_size, item_size); 588ad7b0368SQu Wenruo return -EUCLEAN; 589ad7b0368SQu Wenruo } 590ad7b0368SQu Wenruo 591ad7b0368SQu Wenruo /* 592ad7b0368SQu Wenruo * Special check for XATTR/DIR_ITEM, as key->offset is name 593ad7b0368SQu Wenruo * hash, should match its name 594ad7b0368SQu Wenruo */ 595ad7b0368SQu Wenruo if (key->type == BTRFS_DIR_ITEM_KEY || 596ad7b0368SQu Wenruo key->type == BTRFS_XATTR_ITEM_KEY) { 597e2683fc9SDavid Sterba char namebuf[max(BTRFS_NAME_LEN, XATTR_NAME_MAX)]; 598e2683fc9SDavid Sterba 599ad7b0368SQu Wenruo read_extent_buffer(leaf, namebuf, 600ad7b0368SQu Wenruo (unsigned long)(di + 1), name_len); 601ad7b0368SQu Wenruo name_hash = btrfs_name_hash(namebuf, name_len); 602c7c01a4aSDavid Sterba if (unlikely(key->offset != name_hash)) { 603d98ced68SDavid Sterba dir_item_err(leaf, slot, 604ad7b0368SQu Wenruo "name hash mismatch with key, have 0x%016x expect 0x%016llx", 605ad7b0368SQu Wenruo name_hash, key->offset); 606ad7b0368SQu Wenruo return -EUCLEAN; 607ad7b0368SQu Wenruo } 608ad7b0368SQu Wenruo } 609ad7b0368SQu Wenruo cur += total_size; 610ad7b0368SQu Wenruo di = (struct btrfs_dir_item *)((void *)di + total_size); 611ad7b0368SQu Wenruo } 612ad7b0368SQu Wenruo return 0; 613ad7b0368SQu Wenruo } 614ad7b0368SQu Wenruo 6154806bd88SDavid Sterba __printf(3, 4) 616fce466eaSQu Wenruo __cold 6174806bd88SDavid Sterba static void block_group_err(const struct extent_buffer *eb, int slot, 618fce466eaSQu Wenruo const char *fmt, ...) 619fce466eaSQu Wenruo { 6204806bd88SDavid Sterba const struct btrfs_fs_info *fs_info = eb->fs_info; 621fce466eaSQu Wenruo struct btrfs_key key; 622fce466eaSQu Wenruo struct va_format vaf; 623fce466eaSQu Wenruo va_list args; 624fce466eaSQu Wenruo 625fce466eaSQu Wenruo btrfs_item_key_to_cpu(eb, &key, slot); 626fce466eaSQu Wenruo va_start(args, fmt); 627fce466eaSQu Wenruo 628fce466eaSQu Wenruo vaf.fmt = fmt; 629fce466eaSQu Wenruo vaf.va = &args; 630fce466eaSQu Wenruo 631fce466eaSQu Wenruo btrfs_crit(fs_info, 632fce466eaSQu Wenruo "corrupt %s: root=%llu block=%llu slot=%d bg_start=%llu bg_len=%llu, %pV", 633fce466eaSQu Wenruo btrfs_header_level(eb) == 0 ? "leaf" : "node", 634fce466eaSQu Wenruo btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot, 635fce466eaSQu Wenruo key.objectid, key.offset, &vaf); 636fce466eaSQu Wenruo va_end(args); 637fce466eaSQu Wenruo } 638fce466eaSQu Wenruo 639af60ce2bSDavid Sterba static int check_block_group_item(struct extent_buffer *leaf, 640fce466eaSQu Wenruo struct btrfs_key *key, int slot) 641fce466eaSQu Wenruo { 642*f7238e50SJosef Bacik struct btrfs_fs_info *fs_info = leaf->fs_info; 643fce466eaSQu Wenruo struct btrfs_block_group_item bgi; 6443212fa14SJosef Bacik u32 item_size = btrfs_item_size(leaf, slot); 645*f7238e50SJosef Bacik u64 chunk_objectid; 646fce466eaSQu Wenruo u64 flags; 647fce466eaSQu Wenruo u64 type; 648fce466eaSQu Wenruo 649fce466eaSQu Wenruo /* 650fce466eaSQu Wenruo * Here we don't really care about alignment since extent allocator can 65110950929SQu Wenruo * handle it. We care more about the size. 652fce466eaSQu Wenruo */ 653c7c01a4aSDavid Sterba if (unlikely(key->offset == 0)) { 6544806bd88SDavid Sterba block_group_err(leaf, slot, 65510950929SQu Wenruo "invalid block group size 0"); 656fce466eaSQu Wenruo return -EUCLEAN; 657fce466eaSQu Wenruo } 658fce466eaSQu Wenruo 659c7c01a4aSDavid Sterba if (unlikely(item_size != sizeof(bgi))) { 6604806bd88SDavid Sterba block_group_err(leaf, slot, 661fce466eaSQu Wenruo "invalid item size, have %u expect %zu", 662fce466eaSQu Wenruo item_size, sizeof(bgi)); 663fce466eaSQu Wenruo return -EUCLEAN; 664fce466eaSQu Wenruo } 665fce466eaSQu Wenruo 666fce466eaSQu Wenruo read_extent_buffer(leaf, &bgi, btrfs_item_ptr_offset(leaf, slot), 667fce466eaSQu Wenruo sizeof(bgi)); 668*f7238e50SJosef Bacik chunk_objectid = btrfs_stack_block_group_chunk_objectid(&bgi); 669*f7238e50SJosef Bacik if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) { 670*f7238e50SJosef Bacik /* 671*f7238e50SJosef Bacik * We don't init the nr_global_roots until we load the global 672*f7238e50SJosef Bacik * roots, so this could be 0 at mount time. If it's 0 we'll 673*f7238e50SJosef Bacik * just assume we're fine, and later we'll check against our 674*f7238e50SJosef Bacik * actual value. 675*f7238e50SJosef Bacik */ 676*f7238e50SJosef Bacik if (unlikely(fs_info->nr_global_roots && 677*f7238e50SJosef Bacik chunk_objectid >= fs_info->nr_global_roots)) { 678*f7238e50SJosef Bacik block_group_err(leaf, slot, 679*f7238e50SJosef Bacik "invalid block group global root id, have %llu, needs to be <= %llu", 680*f7238e50SJosef Bacik chunk_objectid, 681*f7238e50SJosef Bacik fs_info->nr_global_roots); 682*f7238e50SJosef Bacik return -EUCLEAN; 683*f7238e50SJosef Bacik } 684*f7238e50SJosef Bacik } else if (unlikely(chunk_objectid != BTRFS_FIRST_CHUNK_TREE_OBJECTID)) { 6854806bd88SDavid Sterba block_group_err(leaf, slot, 686fce466eaSQu Wenruo "invalid block group chunk objectid, have %llu expect %llu", 687de0dc456SDavid Sterba btrfs_stack_block_group_chunk_objectid(&bgi), 688fce466eaSQu Wenruo BTRFS_FIRST_CHUNK_TREE_OBJECTID); 689fce466eaSQu Wenruo return -EUCLEAN; 690fce466eaSQu Wenruo } 691fce466eaSQu Wenruo 692c7c01a4aSDavid Sterba if (unlikely(btrfs_stack_block_group_used(&bgi) > key->offset)) { 6934806bd88SDavid Sterba block_group_err(leaf, slot, 694fce466eaSQu Wenruo "invalid block group used, have %llu expect [0, %llu)", 695de0dc456SDavid Sterba btrfs_stack_block_group_used(&bgi), key->offset); 696fce466eaSQu Wenruo return -EUCLEAN; 697fce466eaSQu Wenruo } 698fce466eaSQu Wenruo 699de0dc456SDavid Sterba flags = btrfs_stack_block_group_flags(&bgi); 700c7c01a4aSDavid Sterba if (unlikely(hweight64(flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) > 1)) { 7014806bd88SDavid Sterba block_group_err(leaf, slot, 702fce466eaSQu Wenruo "invalid profile flags, have 0x%llx (%lu bits set) expect no more than 1 bit set", 703fce466eaSQu Wenruo flags & BTRFS_BLOCK_GROUP_PROFILE_MASK, 704fce466eaSQu Wenruo hweight64(flags & BTRFS_BLOCK_GROUP_PROFILE_MASK)); 705fce466eaSQu Wenruo return -EUCLEAN; 706fce466eaSQu Wenruo } 707fce466eaSQu Wenruo 708fce466eaSQu Wenruo type = flags & BTRFS_BLOCK_GROUP_TYPE_MASK; 709c7c01a4aSDavid Sterba if (unlikely(type != BTRFS_BLOCK_GROUP_DATA && 710fce466eaSQu Wenruo type != BTRFS_BLOCK_GROUP_METADATA && 711fce466eaSQu Wenruo type != BTRFS_BLOCK_GROUP_SYSTEM && 712fce466eaSQu Wenruo type != (BTRFS_BLOCK_GROUP_METADATA | 713c7c01a4aSDavid Sterba BTRFS_BLOCK_GROUP_DATA))) { 7144806bd88SDavid Sterba block_group_err(leaf, slot, 715761333f2SShaokun Zhang "invalid type, have 0x%llx (%lu bits set) expect either 0x%llx, 0x%llx, 0x%llx or 0x%llx", 716fce466eaSQu Wenruo type, hweight64(type), 717fce466eaSQu Wenruo BTRFS_BLOCK_GROUP_DATA, BTRFS_BLOCK_GROUP_METADATA, 718fce466eaSQu Wenruo BTRFS_BLOCK_GROUP_SYSTEM, 719fce466eaSQu Wenruo BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA); 720fce466eaSQu Wenruo return -EUCLEAN; 721fce466eaSQu Wenruo } 722fce466eaSQu Wenruo return 0; 723fce466eaSQu Wenruo } 724fce466eaSQu Wenruo 725d001e4a3SDavid Sterba __printf(4, 5) 726f1140243SQu Wenruo __cold 727d001e4a3SDavid Sterba static void chunk_err(const struct extent_buffer *leaf, 728f1140243SQu Wenruo const struct btrfs_chunk *chunk, u64 logical, 729f1140243SQu Wenruo const char *fmt, ...) 730f1140243SQu Wenruo { 731d001e4a3SDavid Sterba const struct btrfs_fs_info *fs_info = leaf->fs_info; 732f1140243SQu Wenruo bool is_sb; 733f1140243SQu Wenruo struct va_format vaf; 734f1140243SQu Wenruo va_list args; 735f1140243SQu Wenruo int i; 736f1140243SQu Wenruo int slot = -1; 737f1140243SQu Wenruo 738f1140243SQu Wenruo /* Only superblock eb is able to have such small offset */ 739f1140243SQu Wenruo is_sb = (leaf->start == BTRFS_SUPER_INFO_OFFSET); 740f1140243SQu Wenruo 741f1140243SQu Wenruo if (!is_sb) { 742f1140243SQu Wenruo /* 743f1140243SQu Wenruo * Get the slot number by iterating through all slots, this 744f1140243SQu Wenruo * would provide better readability. 745f1140243SQu Wenruo */ 746f1140243SQu Wenruo for (i = 0; i < btrfs_header_nritems(leaf); i++) { 747f1140243SQu Wenruo if (btrfs_item_ptr_offset(leaf, i) == 748f1140243SQu Wenruo (unsigned long)chunk) { 749f1140243SQu Wenruo slot = i; 750f1140243SQu Wenruo break; 751f1140243SQu Wenruo } 752f1140243SQu Wenruo } 753f1140243SQu Wenruo } 754f1140243SQu Wenruo va_start(args, fmt); 755f1140243SQu Wenruo vaf.fmt = fmt; 756f1140243SQu Wenruo vaf.va = &args; 757f1140243SQu Wenruo 758f1140243SQu Wenruo if (is_sb) 759f1140243SQu Wenruo btrfs_crit(fs_info, 760f1140243SQu Wenruo "corrupt superblock syschunk array: chunk_start=%llu, %pV", 761f1140243SQu Wenruo logical, &vaf); 762f1140243SQu Wenruo else 763f1140243SQu Wenruo btrfs_crit(fs_info, 764f1140243SQu Wenruo "corrupt leaf: root=%llu block=%llu slot=%d chunk_start=%llu, %pV", 765f1140243SQu Wenruo BTRFS_CHUNK_TREE_OBJECTID, leaf->start, slot, 766f1140243SQu Wenruo logical, &vaf); 767f1140243SQu Wenruo va_end(args); 768f1140243SQu Wenruo } 769f1140243SQu Wenruo 770ad7b0368SQu Wenruo /* 77182fc28fbSQu Wenruo * The common chunk check which could also work on super block sys chunk array. 77282fc28fbSQu Wenruo * 773bf871c3bSQu Wenruo * Return -EUCLEAN if anything is corrupted. 77482fc28fbSQu Wenruo * Return 0 if everything is OK. 77582fc28fbSQu Wenruo */ 776ddaf1d5aSDavid Sterba int btrfs_check_chunk_valid(struct extent_buffer *leaf, 77782fc28fbSQu Wenruo struct btrfs_chunk *chunk, u64 logical) 77882fc28fbSQu Wenruo { 779ddaf1d5aSDavid Sterba struct btrfs_fs_info *fs_info = leaf->fs_info; 78082fc28fbSQu Wenruo u64 length; 781347fb0cfSSu Yue u64 chunk_end; 78282fc28fbSQu Wenruo u64 stripe_len; 78382fc28fbSQu Wenruo u16 num_stripes; 78482fc28fbSQu Wenruo u16 sub_stripes; 78582fc28fbSQu Wenruo u64 type; 78682fc28fbSQu Wenruo u64 features; 78782fc28fbSQu Wenruo bool mixed = false; 78885d07fbeSDaniel Xu int raid_index; 78985d07fbeSDaniel Xu int nparity; 79085d07fbeSDaniel Xu int ncopies; 79182fc28fbSQu Wenruo 79282fc28fbSQu Wenruo length = btrfs_chunk_length(leaf, chunk); 79382fc28fbSQu Wenruo stripe_len = btrfs_chunk_stripe_len(leaf, chunk); 79482fc28fbSQu Wenruo num_stripes = btrfs_chunk_num_stripes(leaf, chunk); 79582fc28fbSQu Wenruo sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk); 79682fc28fbSQu Wenruo type = btrfs_chunk_type(leaf, chunk); 79785d07fbeSDaniel Xu raid_index = btrfs_bg_flags_to_raid_index(type); 79885d07fbeSDaniel Xu ncopies = btrfs_raid_array[raid_index].ncopies; 79985d07fbeSDaniel Xu nparity = btrfs_raid_array[raid_index].nparity; 80082fc28fbSQu Wenruo 801c7c01a4aSDavid Sterba if (unlikely(!num_stripes)) { 802d001e4a3SDavid Sterba chunk_err(leaf, chunk, logical, 803f1140243SQu Wenruo "invalid chunk num_stripes, have %u", num_stripes); 804bf871c3bSQu Wenruo return -EUCLEAN; 80582fc28fbSQu Wenruo } 806c7c01a4aSDavid Sterba if (unlikely(num_stripes < ncopies)) { 80785d07fbeSDaniel Xu chunk_err(leaf, chunk, logical, 80885d07fbeSDaniel Xu "invalid chunk num_stripes < ncopies, have %u < %d", 80985d07fbeSDaniel Xu num_stripes, ncopies); 81085d07fbeSDaniel Xu return -EUCLEAN; 81185d07fbeSDaniel Xu } 812c7c01a4aSDavid Sterba if (unlikely(nparity && num_stripes == nparity)) { 81385d07fbeSDaniel Xu chunk_err(leaf, chunk, logical, 81485d07fbeSDaniel Xu "invalid chunk num_stripes == nparity, have %u == %d", 81585d07fbeSDaniel Xu num_stripes, nparity); 81685d07fbeSDaniel Xu return -EUCLEAN; 81785d07fbeSDaniel Xu } 818c7c01a4aSDavid Sterba if (unlikely(!IS_ALIGNED(logical, fs_info->sectorsize))) { 819d001e4a3SDavid Sterba chunk_err(leaf, chunk, logical, 820f1140243SQu Wenruo "invalid chunk logical, have %llu should aligned to %u", 821f1140243SQu Wenruo logical, fs_info->sectorsize); 822bf871c3bSQu Wenruo return -EUCLEAN; 82382fc28fbSQu Wenruo } 824c7c01a4aSDavid Sterba if (unlikely(btrfs_chunk_sector_size(leaf, chunk) != fs_info->sectorsize)) { 825d001e4a3SDavid Sterba chunk_err(leaf, chunk, logical, 826f1140243SQu Wenruo "invalid chunk sectorsize, have %u expect %u", 827f1140243SQu Wenruo btrfs_chunk_sector_size(leaf, chunk), 828f1140243SQu Wenruo fs_info->sectorsize); 829bf871c3bSQu Wenruo return -EUCLEAN; 83082fc28fbSQu Wenruo } 831c7c01a4aSDavid Sterba if (unlikely(!length || !IS_ALIGNED(length, fs_info->sectorsize))) { 832d001e4a3SDavid Sterba chunk_err(leaf, chunk, logical, 833f1140243SQu Wenruo "invalid chunk length, have %llu", length); 834bf871c3bSQu Wenruo return -EUCLEAN; 83582fc28fbSQu Wenruo } 836347fb0cfSSu Yue if (unlikely(check_add_overflow(logical, length, &chunk_end))) { 837347fb0cfSSu Yue chunk_err(leaf, chunk, logical, 838347fb0cfSSu Yue "invalid chunk logical start and length, have logical start %llu length %llu", 839347fb0cfSSu Yue logical, length); 840347fb0cfSSu Yue return -EUCLEAN; 841347fb0cfSSu Yue } 842c7c01a4aSDavid Sterba if (unlikely(!is_power_of_2(stripe_len) || stripe_len != BTRFS_STRIPE_LEN)) { 843d001e4a3SDavid Sterba chunk_err(leaf, chunk, logical, 844f1140243SQu Wenruo "invalid chunk stripe length: %llu", 84582fc28fbSQu Wenruo stripe_len); 846bf871c3bSQu Wenruo return -EUCLEAN; 84782fc28fbSQu Wenruo } 848c7c01a4aSDavid Sterba if (unlikely(type & ~(BTRFS_BLOCK_GROUP_TYPE_MASK | 849c7c01a4aSDavid Sterba BTRFS_BLOCK_GROUP_PROFILE_MASK))) { 850d001e4a3SDavid Sterba chunk_err(leaf, chunk, logical, 851f1140243SQu Wenruo "unrecognized chunk type: 0x%llx", 85282fc28fbSQu Wenruo ~(BTRFS_BLOCK_GROUP_TYPE_MASK | 85382fc28fbSQu Wenruo BTRFS_BLOCK_GROUP_PROFILE_MASK) & 85482fc28fbSQu Wenruo btrfs_chunk_type(leaf, chunk)); 855bf871c3bSQu Wenruo return -EUCLEAN; 85682fc28fbSQu Wenruo } 85782fc28fbSQu Wenruo 858c7c01a4aSDavid Sterba if (unlikely(!has_single_bit_set(type & BTRFS_BLOCK_GROUP_PROFILE_MASK) && 859c7c01a4aSDavid Sterba (type & BTRFS_BLOCK_GROUP_PROFILE_MASK) != 0)) { 860d001e4a3SDavid Sterba chunk_err(leaf, chunk, logical, 86180e46cf2SQu Wenruo "invalid chunk profile flag: 0x%llx, expect 0 or 1 bit set", 86280e46cf2SQu Wenruo type & BTRFS_BLOCK_GROUP_PROFILE_MASK); 86380e46cf2SQu Wenruo return -EUCLEAN; 86480e46cf2SQu Wenruo } 865c7c01a4aSDavid Sterba if (unlikely((type & BTRFS_BLOCK_GROUP_TYPE_MASK) == 0)) { 866d001e4a3SDavid Sterba chunk_err(leaf, chunk, logical, 867f1140243SQu Wenruo "missing chunk type flag, have 0x%llx one bit must be set in 0x%llx", 868f1140243SQu Wenruo type, BTRFS_BLOCK_GROUP_TYPE_MASK); 869bf871c3bSQu Wenruo return -EUCLEAN; 87082fc28fbSQu Wenruo } 87182fc28fbSQu Wenruo 872c7c01a4aSDavid Sterba if (unlikely((type & BTRFS_BLOCK_GROUP_SYSTEM) && 873c7c01a4aSDavid Sterba (type & (BTRFS_BLOCK_GROUP_METADATA | 874c7c01a4aSDavid Sterba BTRFS_BLOCK_GROUP_DATA)))) { 875d001e4a3SDavid Sterba chunk_err(leaf, chunk, logical, 876f1140243SQu Wenruo "system chunk with data or metadata type: 0x%llx", 877f1140243SQu Wenruo type); 878bf871c3bSQu Wenruo return -EUCLEAN; 87982fc28fbSQu Wenruo } 88082fc28fbSQu Wenruo 88182fc28fbSQu Wenruo features = btrfs_super_incompat_flags(fs_info->super_copy); 88282fc28fbSQu Wenruo if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) 88382fc28fbSQu Wenruo mixed = true; 88482fc28fbSQu Wenruo 88582fc28fbSQu Wenruo if (!mixed) { 886c7c01a4aSDavid Sterba if (unlikely((type & BTRFS_BLOCK_GROUP_METADATA) && 887c7c01a4aSDavid Sterba (type & BTRFS_BLOCK_GROUP_DATA))) { 888d001e4a3SDavid Sterba chunk_err(leaf, chunk, logical, 88982fc28fbSQu Wenruo "mixed chunk type in non-mixed mode: 0x%llx", type); 890bf871c3bSQu Wenruo return -EUCLEAN; 89182fc28fbSQu Wenruo } 89282fc28fbSQu Wenruo } 89382fc28fbSQu Wenruo 8940ac6e06bSDavid Sterba if (unlikely((type & BTRFS_BLOCK_GROUP_RAID10 && 8950ac6e06bSDavid Sterba sub_stripes != btrfs_raid_array[BTRFS_RAID_RAID10].sub_stripes) || 8960ac6e06bSDavid Sterba (type & BTRFS_BLOCK_GROUP_RAID1 && 8970ac6e06bSDavid Sterba num_stripes != btrfs_raid_array[BTRFS_RAID_RAID1].devs_min) || 8986c154ba4SDavid Sterba (type & BTRFS_BLOCK_GROUP_RAID1C3 && 8996c154ba4SDavid Sterba num_stripes != btrfs_raid_array[BTRFS_RAID_RAID1C3].devs_min) || 9006c154ba4SDavid Sterba (type & BTRFS_BLOCK_GROUP_RAID1C4 && 9016c154ba4SDavid Sterba num_stripes != btrfs_raid_array[BTRFS_RAID_RAID1C4].devs_min) || 9020ac6e06bSDavid Sterba (type & BTRFS_BLOCK_GROUP_RAID5 && 9030ac6e06bSDavid Sterba num_stripes < btrfs_raid_array[BTRFS_RAID_RAID5].devs_min) || 9040ac6e06bSDavid Sterba (type & BTRFS_BLOCK_GROUP_RAID6 && 9050ac6e06bSDavid Sterba num_stripes < btrfs_raid_array[BTRFS_RAID_RAID6].devs_min) || 9060ac6e06bSDavid Sterba (type & BTRFS_BLOCK_GROUP_DUP && 9070ac6e06bSDavid Sterba num_stripes != btrfs_raid_array[BTRFS_RAID_DUP].dev_stripes) || 908c7c01a4aSDavid Sterba ((type & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 && 9090ac6e06bSDavid Sterba num_stripes != btrfs_raid_array[BTRFS_RAID_SINGLE].dev_stripes))) { 910d001e4a3SDavid Sterba chunk_err(leaf, chunk, logical, 91182fc28fbSQu Wenruo "invalid num_stripes:sub_stripes %u:%u for profile %llu", 91282fc28fbSQu Wenruo num_stripes, sub_stripes, 91382fc28fbSQu Wenruo type & BTRFS_BLOCK_GROUP_PROFILE_MASK); 914bf871c3bSQu Wenruo return -EUCLEAN; 91582fc28fbSQu Wenruo } 91682fc28fbSQu Wenruo 91782fc28fbSQu Wenruo return 0; 91882fc28fbSQu Wenruo } 91982fc28fbSQu Wenruo 920f6d2a5c2SQu Wenruo /* 921f6d2a5c2SQu Wenruo * Enhanced version of chunk item checker. 922f6d2a5c2SQu Wenruo * 923f6d2a5c2SQu Wenruo * The common btrfs_check_chunk_valid() doesn't check item size since it needs 924f6d2a5c2SQu Wenruo * to work on super block sys_chunk_array which doesn't have full item ptr. 925f6d2a5c2SQu Wenruo */ 926f6d2a5c2SQu Wenruo static int check_leaf_chunk_item(struct extent_buffer *leaf, 927f6d2a5c2SQu Wenruo struct btrfs_chunk *chunk, 928f6d2a5c2SQu Wenruo struct btrfs_key *key, int slot) 929f6d2a5c2SQu Wenruo { 930f6d2a5c2SQu Wenruo int num_stripes; 931f6d2a5c2SQu Wenruo 9323212fa14SJosef Bacik if (unlikely(btrfs_item_size(leaf, slot) < sizeof(struct btrfs_chunk))) { 933f6d2a5c2SQu Wenruo chunk_err(leaf, chunk, key->offset, 934f6d2a5c2SQu Wenruo "invalid chunk item size: have %u expect [%zu, %u)", 9353212fa14SJosef Bacik btrfs_item_size(leaf, slot), 936f6d2a5c2SQu Wenruo sizeof(struct btrfs_chunk), 937f6d2a5c2SQu Wenruo BTRFS_LEAF_DATA_SIZE(leaf->fs_info)); 938f6d2a5c2SQu Wenruo return -EUCLEAN; 939f6d2a5c2SQu Wenruo } 940f6d2a5c2SQu Wenruo 941f6d2a5c2SQu Wenruo num_stripes = btrfs_chunk_num_stripes(leaf, chunk); 942f6d2a5c2SQu Wenruo /* Let btrfs_check_chunk_valid() handle this error type */ 943f6d2a5c2SQu Wenruo if (num_stripes == 0) 944f6d2a5c2SQu Wenruo goto out; 945f6d2a5c2SQu Wenruo 946c7c01a4aSDavid Sterba if (unlikely(btrfs_chunk_item_size(num_stripes) != 9473212fa14SJosef Bacik btrfs_item_size(leaf, slot))) { 948f6d2a5c2SQu Wenruo chunk_err(leaf, chunk, key->offset, 949f6d2a5c2SQu Wenruo "invalid chunk item size: have %u expect %lu", 9503212fa14SJosef Bacik btrfs_item_size(leaf, slot), 951f6d2a5c2SQu Wenruo btrfs_chunk_item_size(num_stripes)); 952f6d2a5c2SQu Wenruo return -EUCLEAN; 953f6d2a5c2SQu Wenruo } 954f6d2a5c2SQu Wenruo out: 955f6d2a5c2SQu Wenruo return btrfs_check_chunk_valid(leaf, chunk, key->offset); 956f6d2a5c2SQu Wenruo } 957f6d2a5c2SQu Wenruo 9585617ed80SDavid Sterba __printf(3, 4) 959ab4ba2e1SQu Wenruo __cold 9605617ed80SDavid Sterba static void dev_item_err(const struct extent_buffer *eb, int slot, 961ab4ba2e1SQu Wenruo const char *fmt, ...) 962ab4ba2e1SQu Wenruo { 963ab4ba2e1SQu Wenruo struct btrfs_key key; 964ab4ba2e1SQu Wenruo struct va_format vaf; 965ab4ba2e1SQu Wenruo va_list args; 966ab4ba2e1SQu Wenruo 967ab4ba2e1SQu Wenruo btrfs_item_key_to_cpu(eb, &key, slot); 968ab4ba2e1SQu Wenruo va_start(args, fmt); 969ab4ba2e1SQu Wenruo 970ab4ba2e1SQu Wenruo vaf.fmt = fmt; 971ab4ba2e1SQu Wenruo vaf.va = &args; 972ab4ba2e1SQu Wenruo 9735617ed80SDavid Sterba btrfs_crit(eb->fs_info, 974ab4ba2e1SQu Wenruo "corrupt %s: root=%llu block=%llu slot=%d devid=%llu %pV", 975ab4ba2e1SQu Wenruo btrfs_header_level(eb) == 0 ? "leaf" : "node", 976ab4ba2e1SQu Wenruo btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot, 977ab4ba2e1SQu Wenruo key.objectid, &vaf); 978ab4ba2e1SQu Wenruo va_end(args); 979ab4ba2e1SQu Wenruo } 980ab4ba2e1SQu Wenruo 981412a2312SDavid Sterba static int check_dev_item(struct extent_buffer *leaf, 982ab4ba2e1SQu Wenruo struct btrfs_key *key, int slot) 983ab4ba2e1SQu Wenruo { 984ab4ba2e1SQu Wenruo struct btrfs_dev_item *ditem; 985ea1d1ca4SSu Yue const u32 item_size = btrfs_item_size(leaf, slot); 986ab4ba2e1SQu Wenruo 987c7c01a4aSDavid Sterba if (unlikely(key->objectid != BTRFS_DEV_ITEMS_OBJECTID)) { 9885617ed80SDavid Sterba dev_item_err(leaf, slot, 989ab4ba2e1SQu Wenruo "invalid objectid: has=%llu expect=%llu", 990ab4ba2e1SQu Wenruo key->objectid, BTRFS_DEV_ITEMS_OBJECTID); 991ab4ba2e1SQu Wenruo return -EUCLEAN; 992ab4ba2e1SQu Wenruo } 993ea1d1ca4SSu Yue 994ea1d1ca4SSu Yue if (unlikely(item_size != sizeof(*ditem))) { 995ea1d1ca4SSu Yue dev_item_err(leaf, slot, "invalid item size: has %u expect %zu", 996ea1d1ca4SSu Yue item_size, sizeof(*ditem)); 997ea1d1ca4SSu Yue return -EUCLEAN; 998ea1d1ca4SSu Yue } 999ea1d1ca4SSu Yue 1000ab4ba2e1SQu Wenruo ditem = btrfs_item_ptr(leaf, slot, struct btrfs_dev_item); 1001c7c01a4aSDavid Sterba if (unlikely(btrfs_device_id(leaf, ditem) != key->offset)) { 10025617ed80SDavid Sterba dev_item_err(leaf, slot, 1003ab4ba2e1SQu Wenruo "devid mismatch: key has=%llu item has=%llu", 1004ab4ba2e1SQu Wenruo key->offset, btrfs_device_id(leaf, ditem)); 1005ab4ba2e1SQu Wenruo return -EUCLEAN; 1006ab4ba2e1SQu Wenruo } 1007ab4ba2e1SQu Wenruo 1008ab4ba2e1SQu Wenruo /* 1009ab4ba2e1SQu Wenruo * For device total_bytes, we don't have reliable way to check it, as 1010ab4ba2e1SQu Wenruo * it can be 0 for device removal. Device size check can only be done 1011ab4ba2e1SQu Wenruo * by dev extents check. 1012ab4ba2e1SQu Wenruo */ 1013c7c01a4aSDavid Sterba if (unlikely(btrfs_device_bytes_used(leaf, ditem) > 1014c7c01a4aSDavid Sterba btrfs_device_total_bytes(leaf, ditem))) { 10155617ed80SDavid Sterba dev_item_err(leaf, slot, 1016ab4ba2e1SQu Wenruo "invalid bytes used: have %llu expect [0, %llu]", 1017ab4ba2e1SQu Wenruo btrfs_device_bytes_used(leaf, ditem), 1018ab4ba2e1SQu Wenruo btrfs_device_total_bytes(leaf, ditem)); 1019ab4ba2e1SQu Wenruo return -EUCLEAN; 1020ab4ba2e1SQu Wenruo } 1021ab4ba2e1SQu Wenruo /* 1022ab4ba2e1SQu Wenruo * Remaining members like io_align/type/gen/dev_group aren't really 1023ab4ba2e1SQu Wenruo * utilized. Skip them to make later usage of them easier. 1024ab4ba2e1SQu Wenruo */ 1025ab4ba2e1SQu Wenruo return 0; 1026ab4ba2e1SQu Wenruo } 1027ab4ba2e1SQu Wenruo 102839e57f49SDavid Sterba static int check_inode_item(struct extent_buffer *leaf, 1029496245caSQu Wenruo struct btrfs_key *key, int slot) 1030496245caSQu Wenruo { 103139e57f49SDavid Sterba struct btrfs_fs_info *fs_info = leaf->fs_info; 1032496245caSQu Wenruo struct btrfs_inode_item *iitem; 1033496245caSQu Wenruo u64 super_gen = btrfs_super_generation(fs_info->super_copy); 1034496245caSQu Wenruo u32 valid_mask = (S_IFMT | S_ISUID | S_ISGID | S_ISVTX | 0777); 10350c982944SSu Yue const u32 item_size = btrfs_item_size(leaf, slot); 1036496245caSQu Wenruo u32 mode; 1037c23c77b0SQu Wenruo int ret; 103877eea05eSBoris Burkov u32 flags; 103977eea05eSBoris Burkov u32 ro_flags; 1040496245caSQu Wenruo 1041c23c77b0SQu Wenruo ret = check_inode_key(leaf, key, slot); 1042c7c01a4aSDavid Sterba if (unlikely(ret < 0)) 1043c23c77b0SQu Wenruo return ret; 1044c23c77b0SQu Wenruo 10450c982944SSu Yue if (unlikely(item_size != sizeof(*iitem))) { 10460c982944SSu Yue generic_err(leaf, slot, "invalid item size: has %u expect %zu", 10470c982944SSu Yue item_size, sizeof(*iitem)); 10480c982944SSu Yue return -EUCLEAN; 10490c982944SSu Yue } 10500c982944SSu Yue 1051496245caSQu Wenruo iitem = btrfs_item_ptr(leaf, slot, struct btrfs_inode_item); 1052496245caSQu Wenruo 1053496245caSQu Wenruo /* Here we use super block generation + 1 to handle log tree */ 1054c7c01a4aSDavid Sterba if (unlikely(btrfs_inode_generation(leaf, iitem) > super_gen + 1)) { 1055c3053ebbSQu Wenruo inode_item_err(leaf, slot, 1056496245caSQu Wenruo "invalid inode generation: has %llu expect (0, %llu]", 1057496245caSQu Wenruo btrfs_inode_generation(leaf, iitem), 1058496245caSQu Wenruo super_gen + 1); 1059496245caSQu Wenruo return -EUCLEAN; 1060496245caSQu Wenruo } 1061496245caSQu Wenruo /* Note for ROOT_TREE_DIR_ITEM, mkfs could set its transid 0 */ 1062c7c01a4aSDavid Sterba if (unlikely(btrfs_inode_transid(leaf, iitem) > super_gen + 1)) { 1063c3053ebbSQu Wenruo inode_item_err(leaf, slot, 1064f96d6960SQu Wenruo "invalid inode transid: has %llu expect [0, %llu]", 1065496245caSQu Wenruo btrfs_inode_transid(leaf, iitem), super_gen + 1); 1066496245caSQu Wenruo return -EUCLEAN; 1067496245caSQu Wenruo } 1068496245caSQu Wenruo 1069496245caSQu Wenruo /* 1070496245caSQu Wenruo * For size and nbytes it's better not to be too strict, as for dir 1071496245caSQu Wenruo * item its size/nbytes can easily get wrong, but doesn't affect 1072496245caSQu Wenruo * anything in the fs. So here we skip the check. 1073496245caSQu Wenruo */ 1074496245caSQu Wenruo mode = btrfs_inode_mode(leaf, iitem); 1075c7c01a4aSDavid Sterba if (unlikely(mode & ~valid_mask)) { 1076c3053ebbSQu Wenruo inode_item_err(leaf, slot, 1077496245caSQu Wenruo "unknown mode bit detected: 0x%x", 1078496245caSQu Wenruo mode & ~valid_mask); 1079496245caSQu Wenruo return -EUCLEAN; 1080496245caSQu Wenruo } 1081496245caSQu Wenruo 1082496245caSQu Wenruo /* 1083c1499166SDavid Sterba * S_IFMT is not bit mapped so we can't completely rely on 1084c1499166SDavid Sterba * is_power_of_2/has_single_bit_set, but it can save us from checking 1085c1499166SDavid Sterba * FIFO/CHR/DIR/REG. Only needs to check BLK, LNK and SOCKS 1086496245caSQu Wenruo */ 1087c1499166SDavid Sterba if (!has_single_bit_set(mode & S_IFMT)) { 1088c7c01a4aSDavid Sterba if (unlikely(!S_ISLNK(mode) && !S_ISBLK(mode) && !S_ISSOCK(mode))) { 1089c3053ebbSQu Wenruo inode_item_err(leaf, slot, 1090496245caSQu Wenruo "invalid mode: has 0%o expect valid S_IF* bit(s)", 1091496245caSQu Wenruo mode & S_IFMT); 1092496245caSQu Wenruo return -EUCLEAN; 1093496245caSQu Wenruo } 1094496245caSQu Wenruo } 1095c7c01a4aSDavid Sterba if (unlikely(S_ISDIR(mode) && btrfs_inode_nlink(leaf, iitem) > 1)) { 1096c3053ebbSQu Wenruo inode_item_err(leaf, slot, 1097496245caSQu Wenruo "invalid nlink: has %u expect no more than 1 for dir", 1098496245caSQu Wenruo btrfs_inode_nlink(leaf, iitem)); 1099496245caSQu Wenruo return -EUCLEAN; 1100496245caSQu Wenruo } 110177eea05eSBoris Burkov btrfs_inode_split_flags(btrfs_inode_flags(leaf, iitem), &flags, &ro_flags); 110277eea05eSBoris Burkov if (unlikely(flags & ~BTRFS_INODE_FLAG_MASK)) { 1103c3053ebbSQu Wenruo inode_item_err(leaf, slot, 110477eea05eSBoris Burkov "unknown incompat flags detected: 0x%x", flags); 110577eea05eSBoris Burkov return -EUCLEAN; 110677eea05eSBoris Burkov } 110777eea05eSBoris Burkov if (unlikely(!sb_rdonly(fs_info->sb) && 110877eea05eSBoris Burkov (ro_flags & ~BTRFS_INODE_RO_FLAG_MASK))) { 110977eea05eSBoris Burkov inode_item_err(leaf, slot, 111077eea05eSBoris Burkov "unknown ro-compat flags detected on writeable mount: 0x%x", 111177eea05eSBoris Burkov ro_flags); 1112496245caSQu Wenruo return -EUCLEAN; 1113496245caSQu Wenruo } 1114496245caSQu Wenruo return 0; 1115496245caSQu Wenruo } 1116496245caSQu Wenruo 1117259ee775SQu Wenruo static int check_root_item(struct extent_buffer *leaf, struct btrfs_key *key, 1118259ee775SQu Wenruo int slot) 1119259ee775SQu Wenruo { 1120259ee775SQu Wenruo struct btrfs_fs_info *fs_info = leaf->fs_info; 11211465af12SQu Wenruo struct btrfs_root_item ri = { 0 }; 1122259ee775SQu Wenruo const u64 valid_root_flags = BTRFS_ROOT_SUBVOL_RDONLY | 1123259ee775SQu Wenruo BTRFS_ROOT_SUBVOL_DEAD; 112457a0e674SQu Wenruo int ret; 1125259ee775SQu Wenruo 112657a0e674SQu Wenruo ret = check_root_key(leaf, key, slot); 1127c7c01a4aSDavid Sterba if (unlikely(ret < 0)) 112857a0e674SQu Wenruo return ret; 1129259ee775SQu Wenruo 11303212fa14SJosef Bacik if (unlikely(btrfs_item_size(leaf, slot) != sizeof(ri) && 11313212fa14SJosef Bacik btrfs_item_size(leaf, slot) != 1132c7c01a4aSDavid Sterba btrfs_legacy_root_item_size())) { 1133259ee775SQu Wenruo generic_err(leaf, slot, 11341465af12SQu Wenruo "invalid root item size, have %u expect %zu or %u", 11353212fa14SJosef Bacik btrfs_item_size(leaf, slot), sizeof(ri), 11361465af12SQu Wenruo btrfs_legacy_root_item_size()); 11371a49a97dSDaniel Xu return -EUCLEAN; 1138259ee775SQu Wenruo } 1139259ee775SQu Wenruo 11401465af12SQu Wenruo /* 11411465af12SQu Wenruo * For legacy root item, the members starting at generation_v2 will be 11421465af12SQu Wenruo * all filled with 0. 11431465af12SQu Wenruo * And since we allow geneartion_v2 as 0, it will still pass the check. 11441465af12SQu Wenruo */ 1145259ee775SQu Wenruo read_extent_buffer(leaf, &ri, btrfs_item_ptr_offset(leaf, slot), 11463212fa14SJosef Bacik btrfs_item_size(leaf, slot)); 1147259ee775SQu Wenruo 1148259ee775SQu Wenruo /* Generation related */ 1149c7c01a4aSDavid Sterba if (unlikely(btrfs_root_generation(&ri) > 1150c7c01a4aSDavid Sterba btrfs_super_generation(fs_info->super_copy) + 1)) { 1151259ee775SQu Wenruo generic_err(leaf, slot, 1152259ee775SQu Wenruo "invalid root generation, have %llu expect (0, %llu]", 1153259ee775SQu Wenruo btrfs_root_generation(&ri), 1154259ee775SQu Wenruo btrfs_super_generation(fs_info->super_copy) + 1); 1155259ee775SQu Wenruo return -EUCLEAN; 1156259ee775SQu Wenruo } 1157c7c01a4aSDavid Sterba if (unlikely(btrfs_root_generation_v2(&ri) > 1158c7c01a4aSDavid Sterba btrfs_super_generation(fs_info->super_copy) + 1)) { 1159259ee775SQu Wenruo generic_err(leaf, slot, 1160259ee775SQu Wenruo "invalid root v2 generation, have %llu expect (0, %llu]", 1161259ee775SQu Wenruo btrfs_root_generation_v2(&ri), 1162259ee775SQu Wenruo btrfs_super_generation(fs_info->super_copy) + 1); 1163259ee775SQu Wenruo return -EUCLEAN; 1164259ee775SQu Wenruo } 1165c7c01a4aSDavid Sterba if (unlikely(btrfs_root_last_snapshot(&ri) > 1166c7c01a4aSDavid Sterba btrfs_super_generation(fs_info->super_copy) + 1)) { 1167259ee775SQu Wenruo generic_err(leaf, slot, 1168259ee775SQu Wenruo "invalid root last_snapshot, have %llu expect (0, %llu]", 1169259ee775SQu Wenruo btrfs_root_last_snapshot(&ri), 1170259ee775SQu Wenruo btrfs_super_generation(fs_info->super_copy) + 1); 1171259ee775SQu Wenruo return -EUCLEAN; 1172259ee775SQu Wenruo } 1173259ee775SQu Wenruo 1174259ee775SQu Wenruo /* Alignment and level check */ 1175c7c01a4aSDavid Sterba if (unlikely(!IS_ALIGNED(btrfs_root_bytenr(&ri), fs_info->sectorsize))) { 1176259ee775SQu Wenruo generic_err(leaf, slot, 1177259ee775SQu Wenruo "invalid root bytenr, have %llu expect to be aligned to %u", 1178259ee775SQu Wenruo btrfs_root_bytenr(&ri), fs_info->sectorsize); 1179259ee775SQu Wenruo return -EUCLEAN; 1180259ee775SQu Wenruo } 1181c7c01a4aSDavid Sterba if (unlikely(btrfs_root_level(&ri) >= BTRFS_MAX_LEVEL)) { 1182259ee775SQu Wenruo generic_err(leaf, slot, 1183259ee775SQu Wenruo "invalid root level, have %u expect [0, %u]", 1184259ee775SQu Wenruo btrfs_root_level(&ri), BTRFS_MAX_LEVEL - 1); 1185259ee775SQu Wenruo return -EUCLEAN; 1186259ee775SQu Wenruo } 1187c7c01a4aSDavid Sterba if (unlikely(btrfs_root_drop_level(&ri) >= BTRFS_MAX_LEVEL)) { 1188259ee775SQu Wenruo generic_err(leaf, slot, 1189259ee775SQu Wenruo "invalid root level, have %u expect [0, %u]", 1190c8422684SDavid Sterba btrfs_root_drop_level(&ri), BTRFS_MAX_LEVEL - 1); 1191259ee775SQu Wenruo return -EUCLEAN; 1192259ee775SQu Wenruo } 1193259ee775SQu Wenruo 1194259ee775SQu Wenruo /* Flags check */ 1195c7c01a4aSDavid Sterba if (unlikely(btrfs_root_flags(&ri) & ~valid_root_flags)) { 1196259ee775SQu Wenruo generic_err(leaf, slot, 1197259ee775SQu Wenruo "invalid root flags, have 0x%llx expect mask 0x%llx", 1198259ee775SQu Wenruo btrfs_root_flags(&ri), valid_root_flags); 1199259ee775SQu Wenruo return -EUCLEAN; 1200259ee775SQu Wenruo } 1201259ee775SQu Wenruo return 0; 1202259ee775SQu Wenruo } 1203259ee775SQu Wenruo 1204f82d1c7cSQu Wenruo __printf(3,4) 1205f82d1c7cSQu Wenruo __cold 1206f82d1c7cSQu Wenruo static void extent_err(const struct extent_buffer *eb, int slot, 1207f82d1c7cSQu Wenruo const char *fmt, ...) 1208f82d1c7cSQu Wenruo { 1209f82d1c7cSQu Wenruo struct btrfs_key key; 1210f82d1c7cSQu Wenruo struct va_format vaf; 1211f82d1c7cSQu Wenruo va_list args; 1212f82d1c7cSQu Wenruo u64 bytenr; 1213f82d1c7cSQu Wenruo u64 len; 1214f82d1c7cSQu Wenruo 1215f82d1c7cSQu Wenruo btrfs_item_key_to_cpu(eb, &key, slot); 1216f82d1c7cSQu Wenruo bytenr = key.objectid; 1217e2406a6fSQu Wenruo if (key.type == BTRFS_METADATA_ITEM_KEY || 1218e2406a6fSQu Wenruo key.type == BTRFS_TREE_BLOCK_REF_KEY || 1219e2406a6fSQu Wenruo key.type == BTRFS_SHARED_BLOCK_REF_KEY) 1220f82d1c7cSQu Wenruo len = eb->fs_info->nodesize; 1221f82d1c7cSQu Wenruo else 1222f82d1c7cSQu Wenruo len = key.offset; 1223f82d1c7cSQu Wenruo va_start(args, fmt); 1224f82d1c7cSQu Wenruo 1225f82d1c7cSQu Wenruo vaf.fmt = fmt; 1226f82d1c7cSQu Wenruo vaf.va = &args; 1227f82d1c7cSQu Wenruo 1228f82d1c7cSQu Wenruo btrfs_crit(eb->fs_info, 1229f82d1c7cSQu Wenruo "corrupt %s: block=%llu slot=%d extent bytenr=%llu len=%llu %pV", 1230f82d1c7cSQu Wenruo btrfs_header_level(eb) == 0 ? "leaf" : "node", 1231f82d1c7cSQu Wenruo eb->start, slot, bytenr, len, &vaf); 1232f82d1c7cSQu Wenruo va_end(args); 1233f82d1c7cSQu Wenruo } 1234f82d1c7cSQu Wenruo 1235f82d1c7cSQu Wenruo static int check_extent_item(struct extent_buffer *leaf, 1236f82d1c7cSQu Wenruo struct btrfs_key *key, int slot) 1237f82d1c7cSQu Wenruo { 1238f82d1c7cSQu Wenruo struct btrfs_fs_info *fs_info = leaf->fs_info; 1239f82d1c7cSQu Wenruo struct btrfs_extent_item *ei; 1240f82d1c7cSQu Wenruo bool is_tree_block = false; 1241f82d1c7cSQu Wenruo unsigned long ptr; /* Current pointer inside inline refs */ 1242f82d1c7cSQu Wenruo unsigned long end; /* Extent item end */ 12433212fa14SJosef Bacik const u32 item_size = btrfs_item_size(leaf, slot); 1244f82d1c7cSQu Wenruo u64 flags; 1245f82d1c7cSQu Wenruo u64 generation; 1246f82d1c7cSQu Wenruo u64 total_refs; /* Total refs in btrfs_extent_item */ 1247f82d1c7cSQu Wenruo u64 inline_refs = 0; /* found total inline refs */ 1248f82d1c7cSQu Wenruo 1249c7c01a4aSDavid Sterba if (unlikely(key->type == BTRFS_METADATA_ITEM_KEY && 1250c7c01a4aSDavid Sterba !btrfs_fs_incompat(fs_info, SKINNY_METADATA))) { 1251f82d1c7cSQu Wenruo generic_err(leaf, slot, 1252f82d1c7cSQu Wenruo "invalid key type, METADATA_ITEM type invalid when SKINNY_METADATA feature disabled"); 1253f82d1c7cSQu Wenruo return -EUCLEAN; 1254f82d1c7cSQu Wenruo } 1255f82d1c7cSQu Wenruo /* key->objectid is the bytenr for both key types */ 1256c7c01a4aSDavid Sterba if (unlikely(!IS_ALIGNED(key->objectid, fs_info->sectorsize))) { 1257f82d1c7cSQu Wenruo generic_err(leaf, slot, 1258f82d1c7cSQu Wenruo "invalid key objectid, have %llu expect to be aligned to %u", 1259f82d1c7cSQu Wenruo key->objectid, fs_info->sectorsize); 1260f82d1c7cSQu Wenruo return -EUCLEAN; 1261f82d1c7cSQu Wenruo } 1262f82d1c7cSQu Wenruo 1263f82d1c7cSQu Wenruo /* key->offset is tree level for METADATA_ITEM_KEY */ 1264c7c01a4aSDavid Sterba if (unlikely(key->type == BTRFS_METADATA_ITEM_KEY && 1265c7c01a4aSDavid Sterba key->offset >= BTRFS_MAX_LEVEL)) { 1266f82d1c7cSQu Wenruo extent_err(leaf, slot, 1267f82d1c7cSQu Wenruo "invalid tree level, have %llu expect [0, %u]", 1268f82d1c7cSQu Wenruo key->offset, BTRFS_MAX_LEVEL - 1); 1269f82d1c7cSQu Wenruo return -EUCLEAN; 1270f82d1c7cSQu Wenruo } 1271f82d1c7cSQu Wenruo 1272f82d1c7cSQu Wenruo /* 1273f82d1c7cSQu Wenruo * EXTENT/METADATA_ITEM consists of: 1274f82d1c7cSQu Wenruo * 1) One btrfs_extent_item 1275f82d1c7cSQu Wenruo * Records the total refs, type and generation of the extent. 1276f82d1c7cSQu Wenruo * 1277f82d1c7cSQu Wenruo * 2) One btrfs_tree_block_info (for EXTENT_ITEM and tree backref only) 1278f82d1c7cSQu Wenruo * Records the first key and level of the tree block. 1279f82d1c7cSQu Wenruo * 1280f82d1c7cSQu Wenruo * 2) Zero or more btrfs_extent_inline_ref(s) 1281f82d1c7cSQu Wenruo * Each inline ref has one btrfs_extent_inline_ref shows: 1282f82d1c7cSQu Wenruo * 2.1) The ref type, one of the 4 1283f82d1c7cSQu Wenruo * TREE_BLOCK_REF Tree block only 1284f82d1c7cSQu Wenruo * SHARED_BLOCK_REF Tree block only 1285f82d1c7cSQu Wenruo * EXTENT_DATA_REF Data only 1286f82d1c7cSQu Wenruo * SHARED_DATA_REF Data only 1287f82d1c7cSQu Wenruo * 2.2) Ref type specific data 1288f82d1c7cSQu Wenruo * Either using btrfs_extent_inline_ref::offset, or specific 1289f82d1c7cSQu Wenruo * data structure. 1290f82d1c7cSQu Wenruo */ 1291c7c01a4aSDavid Sterba if (unlikely(item_size < sizeof(*ei))) { 1292f82d1c7cSQu Wenruo extent_err(leaf, slot, 1293f82d1c7cSQu Wenruo "invalid item size, have %u expect [%zu, %u)", 1294f82d1c7cSQu Wenruo item_size, sizeof(*ei), 1295f82d1c7cSQu Wenruo BTRFS_LEAF_DATA_SIZE(fs_info)); 1296f82d1c7cSQu Wenruo return -EUCLEAN; 1297f82d1c7cSQu Wenruo } 1298f82d1c7cSQu Wenruo end = item_size + btrfs_item_ptr_offset(leaf, slot); 1299f82d1c7cSQu Wenruo 1300f82d1c7cSQu Wenruo /* Checks against extent_item */ 1301f82d1c7cSQu Wenruo ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item); 1302f82d1c7cSQu Wenruo flags = btrfs_extent_flags(leaf, ei); 1303f82d1c7cSQu Wenruo total_refs = btrfs_extent_refs(leaf, ei); 1304f82d1c7cSQu Wenruo generation = btrfs_extent_generation(leaf, ei); 1305c7c01a4aSDavid Sterba if (unlikely(generation > 1306c7c01a4aSDavid Sterba btrfs_super_generation(fs_info->super_copy) + 1)) { 1307f82d1c7cSQu Wenruo extent_err(leaf, slot, 1308f82d1c7cSQu Wenruo "invalid generation, have %llu expect (0, %llu]", 1309f82d1c7cSQu Wenruo generation, 1310f82d1c7cSQu Wenruo btrfs_super_generation(fs_info->super_copy) + 1); 1311f82d1c7cSQu Wenruo return -EUCLEAN; 1312f82d1c7cSQu Wenruo } 1313c7c01a4aSDavid Sterba if (unlikely(!has_single_bit_set(flags & (BTRFS_EXTENT_FLAG_DATA | 1314c7c01a4aSDavid Sterba BTRFS_EXTENT_FLAG_TREE_BLOCK)))) { 1315f82d1c7cSQu Wenruo extent_err(leaf, slot, 1316f82d1c7cSQu Wenruo "invalid extent flag, have 0x%llx expect 1 bit set in 0x%llx", 1317f82d1c7cSQu Wenruo flags, BTRFS_EXTENT_FLAG_DATA | 1318f82d1c7cSQu Wenruo BTRFS_EXTENT_FLAG_TREE_BLOCK); 1319f82d1c7cSQu Wenruo return -EUCLEAN; 1320f82d1c7cSQu Wenruo } 1321f82d1c7cSQu Wenruo is_tree_block = !!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK); 1322f82d1c7cSQu Wenruo if (is_tree_block) { 1323c7c01a4aSDavid Sterba if (unlikely(key->type == BTRFS_EXTENT_ITEM_KEY && 1324c7c01a4aSDavid Sterba key->offset != fs_info->nodesize)) { 1325f82d1c7cSQu Wenruo extent_err(leaf, slot, 1326f82d1c7cSQu Wenruo "invalid extent length, have %llu expect %u", 1327f82d1c7cSQu Wenruo key->offset, fs_info->nodesize); 1328f82d1c7cSQu Wenruo return -EUCLEAN; 1329f82d1c7cSQu Wenruo } 1330f82d1c7cSQu Wenruo } else { 1331c7c01a4aSDavid Sterba if (unlikely(key->type != BTRFS_EXTENT_ITEM_KEY)) { 1332f82d1c7cSQu Wenruo extent_err(leaf, slot, 1333f82d1c7cSQu Wenruo "invalid key type, have %u expect %u for data backref", 1334f82d1c7cSQu Wenruo key->type, BTRFS_EXTENT_ITEM_KEY); 1335f82d1c7cSQu Wenruo return -EUCLEAN; 1336f82d1c7cSQu Wenruo } 1337c7c01a4aSDavid Sterba if (unlikely(!IS_ALIGNED(key->offset, fs_info->sectorsize))) { 1338f82d1c7cSQu Wenruo extent_err(leaf, slot, 1339f82d1c7cSQu Wenruo "invalid extent length, have %llu expect aligned to %u", 1340f82d1c7cSQu Wenruo key->offset, fs_info->sectorsize); 1341f82d1c7cSQu Wenruo return -EUCLEAN; 1342f82d1c7cSQu Wenruo } 13430ebb6bbbSJosef Bacik if (unlikely(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) { 13440ebb6bbbSJosef Bacik extent_err(leaf, slot, 13450ebb6bbbSJosef Bacik "invalid extent flag, data has full backref set"); 13460ebb6bbbSJosef Bacik return -EUCLEAN; 13470ebb6bbbSJosef Bacik } 1348f82d1c7cSQu Wenruo } 1349f82d1c7cSQu Wenruo ptr = (unsigned long)(struct btrfs_extent_item *)(ei + 1); 1350f82d1c7cSQu Wenruo 1351f82d1c7cSQu Wenruo /* Check the special case of btrfs_tree_block_info */ 1352f82d1c7cSQu Wenruo if (is_tree_block && key->type != BTRFS_METADATA_ITEM_KEY) { 1353f82d1c7cSQu Wenruo struct btrfs_tree_block_info *info; 1354f82d1c7cSQu Wenruo 1355f82d1c7cSQu Wenruo info = (struct btrfs_tree_block_info *)ptr; 1356c7c01a4aSDavid Sterba if (unlikely(btrfs_tree_block_level(leaf, info) >= BTRFS_MAX_LEVEL)) { 1357f82d1c7cSQu Wenruo extent_err(leaf, slot, 1358f82d1c7cSQu Wenruo "invalid tree block info level, have %u expect [0, %u]", 1359f82d1c7cSQu Wenruo btrfs_tree_block_level(leaf, info), 1360f82d1c7cSQu Wenruo BTRFS_MAX_LEVEL - 1); 1361f82d1c7cSQu Wenruo return -EUCLEAN; 1362f82d1c7cSQu Wenruo } 1363f82d1c7cSQu Wenruo ptr = (unsigned long)(struct btrfs_tree_block_info *)(info + 1); 1364f82d1c7cSQu Wenruo } 1365f82d1c7cSQu Wenruo 1366f82d1c7cSQu Wenruo /* Check inline refs */ 1367f82d1c7cSQu Wenruo while (ptr < end) { 1368f82d1c7cSQu Wenruo struct btrfs_extent_inline_ref *iref; 1369f82d1c7cSQu Wenruo struct btrfs_extent_data_ref *dref; 1370f82d1c7cSQu Wenruo struct btrfs_shared_data_ref *sref; 1371f82d1c7cSQu Wenruo u64 dref_offset; 1372f82d1c7cSQu Wenruo u64 inline_offset; 1373f82d1c7cSQu Wenruo u8 inline_type; 1374f82d1c7cSQu Wenruo 1375c7c01a4aSDavid Sterba if (unlikely(ptr + sizeof(*iref) > end)) { 1376f82d1c7cSQu Wenruo extent_err(leaf, slot, 1377f82d1c7cSQu Wenruo "inline ref item overflows extent item, ptr %lu iref size %zu end %lu", 1378f82d1c7cSQu Wenruo ptr, sizeof(*iref), end); 1379f82d1c7cSQu Wenruo return -EUCLEAN; 1380f82d1c7cSQu Wenruo } 1381f82d1c7cSQu Wenruo iref = (struct btrfs_extent_inline_ref *)ptr; 1382f82d1c7cSQu Wenruo inline_type = btrfs_extent_inline_ref_type(leaf, iref); 1383f82d1c7cSQu Wenruo inline_offset = btrfs_extent_inline_ref_offset(leaf, iref); 1384c7c01a4aSDavid Sterba if (unlikely(ptr + btrfs_extent_inline_ref_size(inline_type) > end)) { 1385f82d1c7cSQu Wenruo extent_err(leaf, slot, 1386f82d1c7cSQu Wenruo "inline ref item overflows extent item, ptr %lu iref size %u end %lu", 1387f82d1c7cSQu Wenruo ptr, inline_type, end); 1388f82d1c7cSQu Wenruo return -EUCLEAN; 1389f82d1c7cSQu Wenruo } 1390f82d1c7cSQu Wenruo 1391f82d1c7cSQu Wenruo switch (inline_type) { 1392f82d1c7cSQu Wenruo /* inline_offset is subvolid of the owner, no need to check */ 1393f82d1c7cSQu Wenruo case BTRFS_TREE_BLOCK_REF_KEY: 1394f82d1c7cSQu Wenruo inline_refs++; 1395f82d1c7cSQu Wenruo break; 1396f82d1c7cSQu Wenruo /* Contains parent bytenr */ 1397f82d1c7cSQu Wenruo case BTRFS_SHARED_BLOCK_REF_KEY: 1398c7c01a4aSDavid Sterba if (unlikely(!IS_ALIGNED(inline_offset, 1399c7c01a4aSDavid Sterba fs_info->sectorsize))) { 1400f82d1c7cSQu Wenruo extent_err(leaf, slot, 1401f82d1c7cSQu Wenruo "invalid tree parent bytenr, have %llu expect aligned to %u", 1402f82d1c7cSQu Wenruo inline_offset, fs_info->sectorsize); 1403f82d1c7cSQu Wenruo return -EUCLEAN; 1404f82d1c7cSQu Wenruo } 1405f82d1c7cSQu Wenruo inline_refs++; 1406f82d1c7cSQu Wenruo break; 1407f82d1c7cSQu Wenruo /* 1408f82d1c7cSQu Wenruo * Contains owner subvolid, owner key objectid, adjusted offset. 1409f82d1c7cSQu Wenruo * The only obvious corruption can happen in that offset. 1410f82d1c7cSQu Wenruo */ 1411f82d1c7cSQu Wenruo case BTRFS_EXTENT_DATA_REF_KEY: 1412f82d1c7cSQu Wenruo dref = (struct btrfs_extent_data_ref *)(&iref->offset); 1413f82d1c7cSQu Wenruo dref_offset = btrfs_extent_data_ref_offset(leaf, dref); 1414c7c01a4aSDavid Sterba if (unlikely(!IS_ALIGNED(dref_offset, 1415c7c01a4aSDavid Sterba fs_info->sectorsize))) { 1416f82d1c7cSQu Wenruo extent_err(leaf, slot, 1417f82d1c7cSQu Wenruo "invalid data ref offset, have %llu expect aligned to %u", 1418f82d1c7cSQu Wenruo dref_offset, fs_info->sectorsize); 1419f82d1c7cSQu Wenruo return -EUCLEAN; 1420f82d1c7cSQu Wenruo } 1421f82d1c7cSQu Wenruo inline_refs += btrfs_extent_data_ref_count(leaf, dref); 1422f82d1c7cSQu Wenruo break; 1423f82d1c7cSQu Wenruo /* Contains parent bytenr and ref count */ 1424f82d1c7cSQu Wenruo case BTRFS_SHARED_DATA_REF_KEY: 1425f82d1c7cSQu Wenruo sref = (struct btrfs_shared_data_ref *)(iref + 1); 1426c7c01a4aSDavid Sterba if (unlikely(!IS_ALIGNED(inline_offset, 1427c7c01a4aSDavid Sterba fs_info->sectorsize))) { 1428f82d1c7cSQu Wenruo extent_err(leaf, slot, 1429f82d1c7cSQu Wenruo "invalid data parent bytenr, have %llu expect aligned to %u", 1430f82d1c7cSQu Wenruo inline_offset, fs_info->sectorsize); 1431f82d1c7cSQu Wenruo return -EUCLEAN; 1432f82d1c7cSQu Wenruo } 1433f82d1c7cSQu Wenruo inline_refs += btrfs_shared_data_ref_count(leaf, sref); 1434f82d1c7cSQu Wenruo break; 1435f82d1c7cSQu Wenruo default: 1436f82d1c7cSQu Wenruo extent_err(leaf, slot, "unknown inline ref type: %u", 1437f82d1c7cSQu Wenruo inline_type); 1438f82d1c7cSQu Wenruo return -EUCLEAN; 1439f82d1c7cSQu Wenruo } 1440f82d1c7cSQu Wenruo ptr += btrfs_extent_inline_ref_size(inline_type); 1441f82d1c7cSQu Wenruo } 1442f82d1c7cSQu Wenruo /* No padding is allowed */ 1443c7c01a4aSDavid Sterba if (unlikely(ptr != end)) { 1444f82d1c7cSQu Wenruo extent_err(leaf, slot, 1445f82d1c7cSQu Wenruo "invalid extent item size, padding bytes found"); 1446f82d1c7cSQu Wenruo return -EUCLEAN; 1447f82d1c7cSQu Wenruo } 1448f82d1c7cSQu Wenruo 1449f82d1c7cSQu Wenruo /* Finally, check the inline refs against total refs */ 1450c7c01a4aSDavid Sterba if (unlikely(inline_refs > total_refs)) { 1451f82d1c7cSQu Wenruo extent_err(leaf, slot, 1452f82d1c7cSQu Wenruo "invalid extent refs, have %llu expect >= inline %llu", 1453f82d1c7cSQu Wenruo total_refs, inline_refs); 1454f82d1c7cSQu Wenruo return -EUCLEAN; 1455f82d1c7cSQu Wenruo } 1456f82d1c7cSQu Wenruo return 0; 1457f82d1c7cSQu Wenruo } 1458f82d1c7cSQu Wenruo 1459e2406a6fSQu Wenruo static int check_simple_keyed_refs(struct extent_buffer *leaf, 1460e2406a6fSQu Wenruo struct btrfs_key *key, int slot) 1461e2406a6fSQu Wenruo { 1462e2406a6fSQu Wenruo u32 expect_item_size = 0; 1463e2406a6fSQu Wenruo 1464e2406a6fSQu Wenruo if (key->type == BTRFS_SHARED_DATA_REF_KEY) 1465e2406a6fSQu Wenruo expect_item_size = sizeof(struct btrfs_shared_data_ref); 1466e2406a6fSQu Wenruo 14673212fa14SJosef Bacik if (unlikely(btrfs_item_size(leaf, slot) != expect_item_size)) { 1468e2406a6fSQu Wenruo generic_err(leaf, slot, 1469e2406a6fSQu Wenruo "invalid item size, have %u expect %u for key type %u", 14703212fa14SJosef Bacik btrfs_item_size(leaf, slot), 1471e2406a6fSQu Wenruo expect_item_size, key->type); 1472e2406a6fSQu Wenruo return -EUCLEAN; 1473e2406a6fSQu Wenruo } 1474c7c01a4aSDavid Sterba if (unlikely(!IS_ALIGNED(key->objectid, leaf->fs_info->sectorsize))) { 1475e2406a6fSQu Wenruo generic_err(leaf, slot, 1476e2406a6fSQu Wenruo "invalid key objectid for shared block ref, have %llu expect aligned to %u", 1477e2406a6fSQu Wenruo key->objectid, leaf->fs_info->sectorsize); 1478e2406a6fSQu Wenruo return -EUCLEAN; 1479e2406a6fSQu Wenruo } 1480c7c01a4aSDavid Sterba if (unlikely(key->type != BTRFS_TREE_BLOCK_REF_KEY && 1481c7c01a4aSDavid Sterba !IS_ALIGNED(key->offset, leaf->fs_info->sectorsize))) { 1482e2406a6fSQu Wenruo extent_err(leaf, slot, 1483e2406a6fSQu Wenruo "invalid tree parent bytenr, have %llu expect aligned to %u", 1484e2406a6fSQu Wenruo key->offset, leaf->fs_info->sectorsize); 1485e2406a6fSQu Wenruo return -EUCLEAN; 1486e2406a6fSQu Wenruo } 1487e2406a6fSQu Wenruo return 0; 1488e2406a6fSQu Wenruo } 1489e2406a6fSQu Wenruo 14900785a9aaSQu Wenruo static int check_extent_data_ref(struct extent_buffer *leaf, 14910785a9aaSQu Wenruo struct btrfs_key *key, int slot) 14920785a9aaSQu Wenruo { 14930785a9aaSQu Wenruo struct btrfs_extent_data_ref *dref; 14940785a9aaSQu Wenruo unsigned long ptr = btrfs_item_ptr_offset(leaf, slot); 14953212fa14SJosef Bacik const unsigned long end = ptr + btrfs_item_size(leaf, slot); 14960785a9aaSQu Wenruo 14973212fa14SJosef Bacik if (unlikely(btrfs_item_size(leaf, slot) % sizeof(*dref) != 0)) { 14980785a9aaSQu Wenruo generic_err(leaf, slot, 14990785a9aaSQu Wenruo "invalid item size, have %u expect aligned to %zu for key type %u", 15003212fa14SJosef Bacik btrfs_item_size(leaf, slot), 15010785a9aaSQu Wenruo sizeof(*dref), key->type); 15026d06b0adSDavid Sterba return -EUCLEAN; 15030785a9aaSQu Wenruo } 1504c7c01a4aSDavid Sterba if (unlikely(!IS_ALIGNED(key->objectid, leaf->fs_info->sectorsize))) { 15050785a9aaSQu Wenruo generic_err(leaf, slot, 15060785a9aaSQu Wenruo "invalid key objectid for shared block ref, have %llu expect aligned to %u", 15070785a9aaSQu Wenruo key->objectid, leaf->fs_info->sectorsize); 15080785a9aaSQu Wenruo return -EUCLEAN; 15090785a9aaSQu Wenruo } 15100785a9aaSQu Wenruo for (; ptr < end; ptr += sizeof(*dref)) { 15110785a9aaSQu Wenruo u64 offset; 15120785a9aaSQu Wenruo 15131119a72eSJosef Bacik /* 15141119a72eSJosef Bacik * We cannot check the extent_data_ref hash due to possible 15151119a72eSJosef Bacik * overflow from the leaf due to hash collisions. 15161119a72eSJosef Bacik */ 15170785a9aaSQu Wenruo dref = (struct btrfs_extent_data_ref *)ptr; 15180785a9aaSQu Wenruo offset = btrfs_extent_data_ref_offset(leaf, dref); 1519c7c01a4aSDavid Sterba if (unlikely(!IS_ALIGNED(offset, leaf->fs_info->sectorsize))) { 15200785a9aaSQu Wenruo extent_err(leaf, slot, 15210785a9aaSQu Wenruo "invalid extent data backref offset, have %llu expect aligned to %u", 15220785a9aaSQu Wenruo offset, leaf->fs_info->sectorsize); 15236d06b0adSDavid Sterba return -EUCLEAN; 15240785a9aaSQu Wenruo } 15250785a9aaSQu Wenruo } 15260785a9aaSQu Wenruo return 0; 15270785a9aaSQu Wenruo } 15280785a9aaSQu Wenruo 1529c3053ebbSQu Wenruo #define inode_ref_err(eb, slot, fmt, args...) \ 1530c3053ebbSQu Wenruo inode_item_err(eb, slot, fmt, ##args) 153171bf92a9SQu Wenruo static int check_inode_ref(struct extent_buffer *leaf, 153271bf92a9SQu Wenruo struct btrfs_key *key, struct btrfs_key *prev_key, 153371bf92a9SQu Wenruo int slot) 153471bf92a9SQu Wenruo { 153571bf92a9SQu Wenruo struct btrfs_inode_ref *iref; 153671bf92a9SQu Wenruo unsigned long ptr; 153771bf92a9SQu Wenruo unsigned long end; 153871bf92a9SQu Wenruo 1539c7c01a4aSDavid Sterba if (unlikely(!check_prev_ino(leaf, key, slot, prev_key))) 154080d7fd1eSQu Wenruo return -EUCLEAN; 154171bf92a9SQu Wenruo /* namelen can't be 0, so item_size == sizeof() is also invalid */ 15423212fa14SJosef Bacik if (unlikely(btrfs_item_size(leaf, slot) <= sizeof(*iref))) { 1543c3053ebbSQu Wenruo inode_ref_err(leaf, slot, 154471bf92a9SQu Wenruo "invalid item size, have %u expect (%zu, %u)", 15453212fa14SJosef Bacik btrfs_item_size(leaf, slot), 154671bf92a9SQu Wenruo sizeof(*iref), BTRFS_LEAF_DATA_SIZE(leaf->fs_info)); 154771bf92a9SQu Wenruo return -EUCLEAN; 154871bf92a9SQu Wenruo } 154971bf92a9SQu Wenruo 155071bf92a9SQu Wenruo ptr = btrfs_item_ptr_offset(leaf, slot); 15513212fa14SJosef Bacik end = ptr + btrfs_item_size(leaf, slot); 155271bf92a9SQu Wenruo while (ptr < end) { 155371bf92a9SQu Wenruo u16 namelen; 155471bf92a9SQu Wenruo 1555c7c01a4aSDavid Sterba if (unlikely(ptr + sizeof(iref) > end)) { 1556c3053ebbSQu Wenruo inode_ref_err(leaf, slot, 155771bf92a9SQu Wenruo "inode ref overflow, ptr %lu end %lu inode_ref_size %zu", 155871bf92a9SQu Wenruo ptr, end, sizeof(iref)); 155971bf92a9SQu Wenruo return -EUCLEAN; 156071bf92a9SQu Wenruo } 156171bf92a9SQu Wenruo 156271bf92a9SQu Wenruo iref = (struct btrfs_inode_ref *)ptr; 156371bf92a9SQu Wenruo namelen = btrfs_inode_ref_name_len(leaf, iref); 1564c7c01a4aSDavid Sterba if (unlikely(ptr + sizeof(*iref) + namelen > end)) { 1565c3053ebbSQu Wenruo inode_ref_err(leaf, slot, 156671bf92a9SQu Wenruo "inode ref overflow, ptr %lu end %lu namelen %u", 156771bf92a9SQu Wenruo ptr, end, namelen); 156871bf92a9SQu Wenruo return -EUCLEAN; 156971bf92a9SQu Wenruo } 157071bf92a9SQu Wenruo 157171bf92a9SQu Wenruo /* 157271bf92a9SQu Wenruo * NOTE: In theory we should record all found index numbers 157371bf92a9SQu Wenruo * to find any duplicated indexes, but that will be too time 157471bf92a9SQu Wenruo * consuming for inodes with too many hard links. 157571bf92a9SQu Wenruo */ 157671bf92a9SQu Wenruo ptr += sizeof(*iref) + namelen; 157771bf92a9SQu Wenruo } 157871bf92a9SQu Wenruo return 0; 157971bf92a9SQu Wenruo } 158071bf92a9SQu Wenruo 158182fc28fbSQu Wenruo /* 1582557ea5ddSQu Wenruo * Common point to switch the item-specific validation. 1583557ea5ddSQu Wenruo */ 15840076bc89SDavid Sterba static int check_leaf_item(struct extent_buffer *leaf, 15854e9845efSFilipe Manana struct btrfs_key *key, int slot, 15864e9845efSFilipe Manana struct btrfs_key *prev_key) 1587557ea5ddSQu Wenruo { 1588557ea5ddSQu Wenruo int ret = 0; 1589075cb3c7SQu Wenruo struct btrfs_chunk *chunk; 1590557ea5ddSQu Wenruo 1591557ea5ddSQu Wenruo switch (key->type) { 1592557ea5ddSQu Wenruo case BTRFS_EXTENT_DATA_KEY: 15934e9845efSFilipe Manana ret = check_extent_data_item(leaf, key, slot, prev_key); 1594557ea5ddSQu Wenruo break; 1595557ea5ddSQu Wenruo case BTRFS_EXTENT_CSUM_KEY: 1596ad1d8c43SFilipe Manana ret = check_csum_item(leaf, key, slot, prev_key); 1597557ea5ddSQu Wenruo break; 1598ad7b0368SQu Wenruo case BTRFS_DIR_ITEM_KEY: 1599ad7b0368SQu Wenruo case BTRFS_DIR_INDEX_KEY: 1600ad7b0368SQu Wenruo case BTRFS_XATTR_ITEM_KEY: 1601c18679ebSQu Wenruo ret = check_dir_item(leaf, key, prev_key, slot); 1602ad7b0368SQu Wenruo break; 160371bf92a9SQu Wenruo case BTRFS_INODE_REF_KEY: 160471bf92a9SQu Wenruo ret = check_inode_ref(leaf, key, prev_key, slot); 160571bf92a9SQu Wenruo break; 1606fce466eaSQu Wenruo case BTRFS_BLOCK_GROUP_ITEM_KEY: 1607af60ce2bSDavid Sterba ret = check_block_group_item(leaf, key, slot); 1608fce466eaSQu Wenruo break; 1609075cb3c7SQu Wenruo case BTRFS_CHUNK_ITEM_KEY: 1610075cb3c7SQu Wenruo chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); 1611f6d2a5c2SQu Wenruo ret = check_leaf_chunk_item(leaf, chunk, key, slot); 1612075cb3c7SQu Wenruo break; 1613ab4ba2e1SQu Wenruo case BTRFS_DEV_ITEM_KEY: 1614412a2312SDavid Sterba ret = check_dev_item(leaf, key, slot); 1615ab4ba2e1SQu Wenruo break; 1616496245caSQu Wenruo case BTRFS_INODE_ITEM_KEY: 161739e57f49SDavid Sterba ret = check_inode_item(leaf, key, slot); 1618496245caSQu Wenruo break; 1619259ee775SQu Wenruo case BTRFS_ROOT_ITEM_KEY: 1620259ee775SQu Wenruo ret = check_root_item(leaf, key, slot); 1621259ee775SQu Wenruo break; 1622f82d1c7cSQu Wenruo case BTRFS_EXTENT_ITEM_KEY: 1623f82d1c7cSQu Wenruo case BTRFS_METADATA_ITEM_KEY: 1624f82d1c7cSQu Wenruo ret = check_extent_item(leaf, key, slot); 1625f82d1c7cSQu Wenruo break; 1626e2406a6fSQu Wenruo case BTRFS_TREE_BLOCK_REF_KEY: 1627e2406a6fSQu Wenruo case BTRFS_SHARED_DATA_REF_KEY: 1628e2406a6fSQu Wenruo case BTRFS_SHARED_BLOCK_REF_KEY: 1629e2406a6fSQu Wenruo ret = check_simple_keyed_refs(leaf, key, slot); 1630e2406a6fSQu Wenruo break; 16310785a9aaSQu Wenruo case BTRFS_EXTENT_DATA_REF_KEY: 16320785a9aaSQu Wenruo ret = check_extent_data_ref(leaf, key, slot); 16330785a9aaSQu Wenruo break; 1634557ea5ddSQu Wenruo } 1635557ea5ddSQu Wenruo return ret; 1636557ea5ddSQu Wenruo } 1637557ea5ddSQu Wenruo 1638e2ccd361SDavid Sterba static int check_leaf(struct extent_buffer *leaf, bool check_item_data) 1639557ea5ddSQu Wenruo { 1640e2ccd361SDavid Sterba struct btrfs_fs_info *fs_info = leaf->fs_info; 1641557ea5ddSQu Wenruo /* No valid key type is 0, so all key should be larger than this key */ 1642557ea5ddSQu Wenruo struct btrfs_key prev_key = {0, 0, 0}; 1643557ea5ddSQu Wenruo struct btrfs_key key; 1644557ea5ddSQu Wenruo u32 nritems = btrfs_header_nritems(leaf); 1645557ea5ddSQu Wenruo int slot; 1646557ea5ddSQu Wenruo 1647c7c01a4aSDavid Sterba if (unlikely(btrfs_header_level(leaf) != 0)) { 164886a6be3aSDavid Sterba generic_err(leaf, 0, 1649f556faa4SQu Wenruo "invalid level for leaf, have %d expect 0", 1650f556faa4SQu Wenruo btrfs_header_level(leaf)); 1651f556faa4SQu Wenruo return -EUCLEAN; 1652f556faa4SQu Wenruo } 1653f556faa4SQu Wenruo 1654557ea5ddSQu Wenruo /* 1655557ea5ddSQu Wenruo * Extent buffers from a relocation tree have a owner field that 1656557ea5ddSQu Wenruo * corresponds to the subvolume tree they are based on. So just from an 1657557ea5ddSQu Wenruo * extent buffer alone we can not find out what is the id of the 1658557ea5ddSQu Wenruo * corresponding subvolume tree, so we can not figure out if the extent 1659557ea5ddSQu Wenruo * buffer corresponds to the root of the relocation tree or not. So 1660557ea5ddSQu Wenruo * skip this check for relocation trees. 1661557ea5ddSQu Wenruo */ 1662557ea5ddSQu Wenruo if (nritems == 0 && !btrfs_header_flag(leaf, BTRFS_HEADER_FLAG_RELOC)) { 1663ba480dd4SQu Wenruo u64 owner = btrfs_header_owner(leaf); 1664557ea5ddSQu Wenruo 1665ba480dd4SQu Wenruo /* These trees must never be empty */ 1666c7c01a4aSDavid Sterba if (unlikely(owner == BTRFS_ROOT_TREE_OBJECTID || 1667ba480dd4SQu Wenruo owner == BTRFS_CHUNK_TREE_OBJECTID || 1668ba480dd4SQu Wenruo owner == BTRFS_DEV_TREE_OBJECTID || 1669ba480dd4SQu Wenruo owner == BTRFS_FS_TREE_OBJECTID || 1670c7c01a4aSDavid Sterba owner == BTRFS_DATA_RELOC_TREE_OBJECTID)) { 167186a6be3aSDavid Sterba generic_err(leaf, 0, 1672ba480dd4SQu Wenruo "invalid root, root %llu must never be empty", 1673ba480dd4SQu Wenruo owner); 1674ba480dd4SQu Wenruo return -EUCLEAN; 1675ba480dd4SQu Wenruo } 1676c2fa821cSJosef Bacik 167762fdaa52SQu Wenruo /* Unknown tree */ 1678c7c01a4aSDavid Sterba if (unlikely(owner == 0)) { 167962fdaa52SQu Wenruo generic_err(leaf, 0, 168062fdaa52SQu Wenruo "invalid owner, root 0 is not defined"); 168162fdaa52SQu Wenruo return -EUCLEAN; 168262fdaa52SQu Wenruo } 1683c2fa821cSJosef Bacik 1684c2fa821cSJosef Bacik /* EXTENT_TREE_V2 can have empty extent trees. */ 1685c2fa821cSJosef Bacik if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) 1686c2fa821cSJosef Bacik return 0; 1687c2fa821cSJosef Bacik 1688c2fa821cSJosef Bacik if (unlikely(owner == BTRFS_EXTENT_TREE_OBJECTID)) { 1689c2fa821cSJosef Bacik generic_err(leaf, 0, 1690c2fa821cSJosef Bacik "invalid root, root %llu must never be empty", 1691c2fa821cSJosef Bacik owner); 1692c2fa821cSJosef Bacik return -EUCLEAN; 1693c2fa821cSJosef Bacik } 1694c2fa821cSJosef Bacik 1695557ea5ddSQu Wenruo return 0; 1696557ea5ddSQu Wenruo } 1697557ea5ddSQu Wenruo 1698c7c01a4aSDavid Sterba if (unlikely(nritems == 0)) 1699557ea5ddSQu Wenruo return 0; 1700557ea5ddSQu Wenruo 1701557ea5ddSQu Wenruo /* 1702557ea5ddSQu Wenruo * Check the following things to make sure this is a good leaf, and 1703557ea5ddSQu Wenruo * leaf users won't need to bother with similar sanity checks: 1704557ea5ddSQu Wenruo * 1705557ea5ddSQu Wenruo * 1) key ordering 1706557ea5ddSQu Wenruo * 2) item offset and size 1707557ea5ddSQu Wenruo * No overlap, no hole, all inside the leaf. 1708557ea5ddSQu Wenruo * 3) item content 1709557ea5ddSQu Wenruo * If possible, do comprehensive sanity check. 1710557ea5ddSQu Wenruo * NOTE: All checks must only rely on the item data itself. 1711557ea5ddSQu Wenruo */ 1712557ea5ddSQu Wenruo for (slot = 0; slot < nritems; slot++) { 1713557ea5ddSQu Wenruo u32 item_end_expected; 1714a6ab66ebSSu Yue u64 item_data_end; 1715557ea5ddSQu Wenruo int ret; 1716557ea5ddSQu Wenruo 1717557ea5ddSQu Wenruo btrfs_item_key_to_cpu(leaf, &key, slot); 1718557ea5ddSQu Wenruo 1719557ea5ddSQu Wenruo /* Make sure the keys are in the right order */ 1720c7c01a4aSDavid Sterba if (unlikely(btrfs_comp_cpu_keys(&prev_key, &key) >= 0)) { 172186a6be3aSDavid Sterba generic_err(leaf, slot, 1722478d01b3SQu Wenruo "bad key order, prev (%llu %u %llu) current (%llu %u %llu)", 1723478d01b3SQu Wenruo prev_key.objectid, prev_key.type, 1724478d01b3SQu Wenruo prev_key.offset, key.objectid, key.type, 1725478d01b3SQu Wenruo key.offset); 1726557ea5ddSQu Wenruo return -EUCLEAN; 1727557ea5ddSQu Wenruo } 1728557ea5ddSQu Wenruo 1729a6ab66ebSSu Yue item_data_end = (u64)btrfs_item_offset(leaf, slot) + 1730a6ab66ebSSu Yue btrfs_item_size(leaf, slot); 1731557ea5ddSQu Wenruo /* 1732557ea5ddSQu Wenruo * Make sure the offset and ends are right, remember that the 1733557ea5ddSQu Wenruo * item data starts at the end of the leaf and grows towards the 1734557ea5ddSQu Wenruo * front. 1735557ea5ddSQu Wenruo */ 1736557ea5ddSQu Wenruo if (slot == 0) 1737557ea5ddSQu Wenruo item_end_expected = BTRFS_LEAF_DATA_SIZE(fs_info); 1738557ea5ddSQu Wenruo else 17393212fa14SJosef Bacik item_end_expected = btrfs_item_offset(leaf, 1740557ea5ddSQu Wenruo slot - 1); 1741a6ab66ebSSu Yue if (unlikely(item_data_end != item_end_expected)) { 174286a6be3aSDavid Sterba generic_err(leaf, slot, 1743a6ab66ebSSu Yue "unexpected item end, have %llu expect %u", 1744a6ab66ebSSu Yue item_data_end, item_end_expected); 1745557ea5ddSQu Wenruo return -EUCLEAN; 1746557ea5ddSQu Wenruo } 1747557ea5ddSQu Wenruo 1748557ea5ddSQu Wenruo /* 1749557ea5ddSQu Wenruo * Check to make sure that we don't point outside of the leaf, 1750557ea5ddSQu Wenruo * just in case all the items are consistent to each other, but 1751557ea5ddSQu Wenruo * all point outside of the leaf. 1752557ea5ddSQu Wenruo */ 1753a6ab66ebSSu Yue if (unlikely(item_data_end > BTRFS_LEAF_DATA_SIZE(fs_info))) { 175486a6be3aSDavid Sterba generic_err(leaf, slot, 1755a6ab66ebSSu Yue "slot end outside of leaf, have %llu expect range [0, %u]", 1756a6ab66ebSSu Yue item_data_end, BTRFS_LEAF_DATA_SIZE(fs_info)); 1757557ea5ddSQu Wenruo return -EUCLEAN; 1758557ea5ddSQu Wenruo } 1759557ea5ddSQu Wenruo 1760557ea5ddSQu Wenruo /* Also check if the item pointer overlaps with btrfs item. */ 1761c7c01a4aSDavid Sterba if (unlikely(btrfs_item_ptr_offset(leaf, slot) < 1762c7c01a4aSDavid Sterba btrfs_item_nr_offset(slot) + sizeof(struct btrfs_item))) { 176386a6be3aSDavid Sterba generic_err(leaf, slot, 1764478d01b3SQu Wenruo "slot overlaps with its data, item end %lu data start %lu", 1765478d01b3SQu Wenruo btrfs_item_nr_offset(slot) + 1766478d01b3SQu Wenruo sizeof(struct btrfs_item), 1767478d01b3SQu Wenruo btrfs_item_ptr_offset(leaf, slot)); 1768557ea5ddSQu Wenruo return -EUCLEAN; 1769557ea5ddSQu Wenruo } 1770557ea5ddSQu Wenruo 177169fc6cbbSQu Wenruo if (check_item_data) { 177269fc6cbbSQu Wenruo /* 177369fc6cbbSQu Wenruo * Check if the item size and content meet other 177469fc6cbbSQu Wenruo * criteria 177569fc6cbbSQu Wenruo */ 17764e9845efSFilipe Manana ret = check_leaf_item(leaf, &key, slot, &prev_key); 1777c7c01a4aSDavid Sterba if (unlikely(ret < 0)) 1778557ea5ddSQu Wenruo return ret; 177969fc6cbbSQu Wenruo } 1780557ea5ddSQu Wenruo 1781557ea5ddSQu Wenruo prev_key.objectid = key.objectid; 1782557ea5ddSQu Wenruo prev_key.type = key.type; 1783557ea5ddSQu Wenruo prev_key.offset = key.offset; 1784557ea5ddSQu Wenruo } 1785557ea5ddSQu Wenruo 1786557ea5ddSQu Wenruo return 0; 1787557ea5ddSQu Wenruo } 1788557ea5ddSQu Wenruo 17891c4360eeSDavid Sterba int btrfs_check_leaf_full(struct extent_buffer *leaf) 179069fc6cbbSQu Wenruo { 1791e2ccd361SDavid Sterba return check_leaf(leaf, true); 179269fc6cbbSQu Wenruo } 179302529d7aSQu Wenruo ALLOW_ERROR_INJECTION(btrfs_check_leaf_full, ERRNO); 179469fc6cbbSQu Wenruo 1795cfdaad5eSDavid Sterba int btrfs_check_leaf_relaxed(struct extent_buffer *leaf) 17962f659546SQu Wenruo { 1797e2ccd361SDavid Sterba return check_leaf(leaf, false); 17982f659546SQu Wenruo } 17992f659546SQu Wenruo 1800813fd1dcSDavid Sterba int btrfs_check_node(struct extent_buffer *node) 1801557ea5ddSQu Wenruo { 1802813fd1dcSDavid Sterba struct btrfs_fs_info *fs_info = node->fs_info; 1803557ea5ddSQu Wenruo unsigned long nr = btrfs_header_nritems(node); 1804557ea5ddSQu Wenruo struct btrfs_key key, next_key; 1805557ea5ddSQu Wenruo int slot; 1806f556faa4SQu Wenruo int level = btrfs_header_level(node); 1807557ea5ddSQu Wenruo u64 bytenr; 1808557ea5ddSQu Wenruo int ret = 0; 1809557ea5ddSQu Wenruo 1810c7c01a4aSDavid Sterba if (unlikely(level <= 0 || level >= BTRFS_MAX_LEVEL)) { 181186a6be3aSDavid Sterba generic_err(node, 0, 1812f556faa4SQu Wenruo "invalid level for node, have %d expect [1, %d]", 1813f556faa4SQu Wenruo level, BTRFS_MAX_LEVEL - 1); 1814f556faa4SQu Wenruo return -EUCLEAN; 1815f556faa4SQu Wenruo } 1816c7c01a4aSDavid Sterba if (unlikely(nr == 0 || nr > BTRFS_NODEPTRS_PER_BLOCK(fs_info))) { 18172f659546SQu Wenruo btrfs_crit(fs_info, 1818bba4f298SQu Wenruo "corrupt node: root=%llu block=%llu, nritems too %s, have %lu expect range [1,%u]", 18192f659546SQu Wenruo btrfs_header_owner(node), node->start, 1820bba4f298SQu Wenruo nr == 0 ? "small" : "large", nr, 18212f659546SQu Wenruo BTRFS_NODEPTRS_PER_BLOCK(fs_info)); 1822bba4f298SQu Wenruo return -EUCLEAN; 1823557ea5ddSQu Wenruo } 1824557ea5ddSQu Wenruo 1825557ea5ddSQu Wenruo for (slot = 0; slot < nr - 1; slot++) { 1826557ea5ddSQu Wenruo bytenr = btrfs_node_blockptr(node, slot); 1827557ea5ddSQu Wenruo btrfs_node_key_to_cpu(node, &key, slot); 1828557ea5ddSQu Wenruo btrfs_node_key_to_cpu(node, &next_key, slot + 1); 1829557ea5ddSQu Wenruo 1830c7c01a4aSDavid Sterba if (unlikely(!bytenr)) { 183186a6be3aSDavid Sterba generic_err(node, slot, 1832bba4f298SQu Wenruo "invalid NULL node pointer"); 1833bba4f298SQu Wenruo ret = -EUCLEAN; 1834bba4f298SQu Wenruo goto out; 1835bba4f298SQu Wenruo } 1836c7c01a4aSDavid Sterba if (unlikely(!IS_ALIGNED(bytenr, fs_info->sectorsize))) { 183786a6be3aSDavid Sterba generic_err(node, slot, 1838bba4f298SQu Wenruo "unaligned pointer, have %llu should be aligned to %u", 18392f659546SQu Wenruo bytenr, fs_info->sectorsize); 1840bba4f298SQu Wenruo ret = -EUCLEAN; 1841557ea5ddSQu Wenruo goto out; 1842557ea5ddSQu Wenruo } 1843557ea5ddSQu Wenruo 1844c7c01a4aSDavid Sterba if (unlikely(btrfs_comp_cpu_keys(&key, &next_key) >= 0)) { 184586a6be3aSDavid Sterba generic_err(node, slot, 1846bba4f298SQu Wenruo "bad key order, current (%llu %u %llu) next (%llu %u %llu)", 1847bba4f298SQu Wenruo key.objectid, key.type, key.offset, 1848bba4f298SQu Wenruo next_key.objectid, next_key.type, 1849bba4f298SQu Wenruo next_key.offset); 1850bba4f298SQu Wenruo ret = -EUCLEAN; 1851557ea5ddSQu Wenruo goto out; 1852557ea5ddSQu Wenruo } 1853557ea5ddSQu Wenruo } 1854557ea5ddSQu Wenruo out: 1855557ea5ddSQu Wenruo return ret; 1856557ea5ddSQu Wenruo } 185702529d7aSQu Wenruo ALLOW_ERROR_INJECTION(btrfs_check_node, ERRNO); 1858