1557ea5ddSQu Wenruo /* 2557ea5ddSQu Wenruo * Copyright (C) Qu Wenruo 2017. All rights reserved. 3557ea5ddSQu Wenruo * 4557ea5ddSQu Wenruo * This program is free software; you can redistribute it and/or 5557ea5ddSQu Wenruo * modify it under the terms of the GNU General Public 6557ea5ddSQu Wenruo * License v2 as published by the Free Software Foundation. 7557ea5ddSQu Wenruo * 8557ea5ddSQu Wenruo * This program is distributed in the hope that it will be useful, 9557ea5ddSQu Wenruo * but WITHOUT ANY WARRANTY; without even the implied warranty of 10557ea5ddSQu Wenruo * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11557ea5ddSQu Wenruo * General Public License for more details. 12557ea5ddSQu Wenruo * 13557ea5ddSQu Wenruo * You should have received a copy of the GNU General Public 14557ea5ddSQu Wenruo * License along with this program. 15557ea5ddSQu Wenruo */ 16557ea5ddSQu Wenruo 17557ea5ddSQu Wenruo /* 18557ea5ddSQu Wenruo * The module is used to catch unexpected/corrupted tree block data. 19557ea5ddSQu Wenruo * Such behavior can be caused either by a fuzzed image or bugs. 20557ea5ddSQu Wenruo * 21557ea5ddSQu Wenruo * The objective is to do leaf/node validation checks when tree block is read 22557ea5ddSQu Wenruo * from disk, and check *every* possible member, so other code won't 23557ea5ddSQu Wenruo * need to checking them again. 24557ea5ddSQu Wenruo * 25557ea5ddSQu Wenruo * Due to the potential and unwanted damage, every checker needs to be 26557ea5ddSQu Wenruo * carefully reviewed otherwise so it does not prevent mount of valid images. 27557ea5ddSQu Wenruo */ 28557ea5ddSQu Wenruo 29557ea5ddSQu Wenruo #include "ctree.h" 30557ea5ddSQu Wenruo #include "tree-checker.h" 31557ea5ddSQu Wenruo #include "disk-io.h" 32557ea5ddSQu Wenruo #include "compression.h" 33ad7b0368SQu Wenruo #include "hash.h" 34557ea5ddSQu Wenruo 35bba4f298SQu Wenruo /* 36bba4f298SQu Wenruo * Error message should follow the following format: 37bba4f298SQu Wenruo * corrupt <type>: <identifier>, <reason>[, <bad_value>] 38bba4f298SQu Wenruo * 39bba4f298SQu Wenruo * @type: leaf or node 40bba4f298SQu Wenruo * @identifier: the necessary info to locate the leaf/node. 41bba4f298SQu Wenruo * It's recommened to decode key.objecitd/offset if it's 42bba4f298SQu Wenruo * meaningful. 43bba4f298SQu Wenruo * @reason: describe the error 44bba4f298SQu Wenruo * @bad_value: optional, it's recommened to output bad value and its 45bba4f298SQu Wenruo * expected value (range). 46bba4f298SQu Wenruo * 47bba4f298SQu Wenruo * Since comma is used to separate the components, only space is allowed 48bba4f298SQu Wenruo * inside each component. 49bba4f298SQu Wenruo */ 50bba4f298SQu Wenruo 51bba4f298SQu Wenruo /* 52bba4f298SQu Wenruo * Append generic "corrupt leaf/node root=%llu block=%llu slot=%d: " to @fmt. 53bba4f298SQu Wenruo * Allows callers to customize the output. 54bba4f298SQu Wenruo */ 55bba4f298SQu Wenruo __printf(4, 5) 56*2f659546SQu Wenruo static void generic_err(const struct btrfs_fs_info *fs_info, 57bba4f298SQu Wenruo const struct extent_buffer *eb, int slot, 58bba4f298SQu Wenruo const char *fmt, ...) 59bba4f298SQu Wenruo { 60bba4f298SQu Wenruo struct va_format vaf; 61bba4f298SQu Wenruo va_list args; 62bba4f298SQu Wenruo 63bba4f298SQu Wenruo va_start(args, fmt); 64bba4f298SQu Wenruo 65bba4f298SQu Wenruo vaf.fmt = fmt; 66bba4f298SQu Wenruo vaf.va = &args; 67bba4f298SQu Wenruo 68*2f659546SQu Wenruo btrfs_crit(fs_info, 69bba4f298SQu Wenruo "corrupt %s: root=%llu block=%llu slot=%d, %pV", 70bba4f298SQu Wenruo btrfs_header_level(eb) == 0 ? "leaf" : "node", 71*2f659546SQu Wenruo btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot, &vaf); 72bba4f298SQu Wenruo va_end(args); 73bba4f298SQu Wenruo } 74bba4f298SQu Wenruo 758806d718SQu Wenruo /* 768806d718SQu Wenruo * Customized reporter for extent data item, since its key objectid and 778806d718SQu Wenruo * offset has its own meaning. 788806d718SQu Wenruo */ 798806d718SQu Wenruo __printf(4, 5) 80*2f659546SQu Wenruo static void file_extent_err(const struct btrfs_fs_info *fs_info, 818806d718SQu Wenruo const struct extent_buffer *eb, int slot, 828806d718SQu Wenruo const char *fmt, ...) 838806d718SQu Wenruo { 848806d718SQu Wenruo struct btrfs_key key; 858806d718SQu Wenruo struct va_format vaf; 868806d718SQu Wenruo va_list args; 878806d718SQu Wenruo 888806d718SQu Wenruo btrfs_item_key_to_cpu(eb, &key, slot); 898806d718SQu Wenruo va_start(args, fmt); 908806d718SQu Wenruo 918806d718SQu Wenruo vaf.fmt = fmt; 928806d718SQu Wenruo vaf.va = &args; 938806d718SQu Wenruo 94*2f659546SQu Wenruo btrfs_crit(fs_info, 958806d718SQu Wenruo "corrupt %s: root=%llu block=%llu slot=%d ino=%llu file_offset=%llu, %pV", 96*2f659546SQu Wenruo btrfs_header_level(eb) == 0 ? "leaf" : "node", 97*2f659546SQu Wenruo btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot, 98*2f659546SQu Wenruo key.objectid, key.offset, &vaf); 998806d718SQu Wenruo va_end(args); 1008806d718SQu Wenruo } 1018806d718SQu Wenruo 1028806d718SQu Wenruo /* 1038806d718SQu Wenruo * Return 0 if the btrfs_file_extent_##name is aligned to @alignment 1048806d718SQu Wenruo * Else return 1 1058806d718SQu Wenruo */ 106*2f659546SQu Wenruo #define CHECK_FE_ALIGNED(fs_info, leaf, slot, fi, name, alignment) \ 1078806d718SQu Wenruo ({ \ 1088806d718SQu Wenruo if (!IS_ALIGNED(btrfs_file_extent_##name((leaf), (fi)), (alignment))) \ 109*2f659546SQu Wenruo file_extent_err((fs_info), (leaf), (slot), \ 1108806d718SQu Wenruo "invalid %s for file extent, have %llu, should be aligned to %u", \ 1118806d718SQu Wenruo (#name), btrfs_file_extent_##name((leaf), (fi)), \ 1128806d718SQu Wenruo (alignment)); \ 1138806d718SQu Wenruo (!IS_ALIGNED(btrfs_file_extent_##name((leaf), (fi)), (alignment))); \ 1148806d718SQu Wenruo }) 1158806d718SQu Wenruo 116*2f659546SQu Wenruo static int check_extent_data_item(struct btrfs_fs_info *fs_info, 117557ea5ddSQu Wenruo struct extent_buffer *leaf, 118557ea5ddSQu Wenruo struct btrfs_key *key, int slot) 119557ea5ddSQu Wenruo { 120557ea5ddSQu Wenruo struct btrfs_file_extent_item *fi; 121*2f659546SQu Wenruo u32 sectorsize = fs_info->sectorsize; 122557ea5ddSQu Wenruo u32 item_size = btrfs_item_size_nr(leaf, slot); 123557ea5ddSQu Wenruo 124557ea5ddSQu Wenruo if (!IS_ALIGNED(key->offset, sectorsize)) { 125*2f659546SQu Wenruo file_extent_err(fs_info, leaf, slot, 1268806d718SQu Wenruo "unaligned file_offset for file extent, have %llu should be aligned to %u", 1278806d718SQu Wenruo key->offset, sectorsize); 128557ea5ddSQu Wenruo return -EUCLEAN; 129557ea5ddSQu Wenruo } 130557ea5ddSQu Wenruo 131557ea5ddSQu Wenruo fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); 132557ea5ddSQu Wenruo 133557ea5ddSQu Wenruo if (btrfs_file_extent_type(leaf, fi) > BTRFS_FILE_EXTENT_TYPES) { 134*2f659546SQu Wenruo file_extent_err(fs_info, leaf, slot, 1358806d718SQu Wenruo "invalid type for file extent, have %u expect range [0, %u]", 1368806d718SQu Wenruo btrfs_file_extent_type(leaf, fi), 1378806d718SQu Wenruo BTRFS_FILE_EXTENT_TYPES); 138557ea5ddSQu Wenruo return -EUCLEAN; 139557ea5ddSQu Wenruo } 140557ea5ddSQu Wenruo 141557ea5ddSQu Wenruo /* 142557ea5ddSQu Wenruo * Support for new compression/encrption must introduce incompat flag, 143557ea5ddSQu Wenruo * and must be caught in open_ctree(). 144557ea5ddSQu Wenruo */ 145557ea5ddSQu Wenruo if (btrfs_file_extent_compression(leaf, fi) > BTRFS_COMPRESS_TYPES) { 146*2f659546SQu Wenruo file_extent_err(fs_info, leaf, slot, 1478806d718SQu Wenruo "invalid compression for file extent, have %u expect range [0, %u]", 1488806d718SQu Wenruo btrfs_file_extent_compression(leaf, fi), 1498806d718SQu Wenruo BTRFS_COMPRESS_TYPES); 150557ea5ddSQu Wenruo return -EUCLEAN; 151557ea5ddSQu Wenruo } 152557ea5ddSQu Wenruo if (btrfs_file_extent_encryption(leaf, fi)) { 153*2f659546SQu Wenruo file_extent_err(fs_info, leaf, slot, 1548806d718SQu Wenruo "invalid encryption for file extent, have %u expect 0", 1558806d718SQu Wenruo btrfs_file_extent_encryption(leaf, fi)); 156557ea5ddSQu Wenruo return -EUCLEAN; 157557ea5ddSQu Wenruo } 158557ea5ddSQu Wenruo if (btrfs_file_extent_type(leaf, fi) == BTRFS_FILE_EXTENT_INLINE) { 159557ea5ddSQu Wenruo /* Inline extent must have 0 as key offset */ 160557ea5ddSQu Wenruo if (key->offset) { 161*2f659546SQu Wenruo file_extent_err(fs_info, leaf, slot, 1628806d718SQu Wenruo "invalid file_offset for inline file extent, have %llu expect 0", 1638806d718SQu Wenruo key->offset); 164557ea5ddSQu Wenruo return -EUCLEAN; 165557ea5ddSQu Wenruo } 166557ea5ddSQu Wenruo 167557ea5ddSQu Wenruo /* Compressed inline extent has no on-disk size, skip it */ 168557ea5ddSQu Wenruo if (btrfs_file_extent_compression(leaf, fi) != 169557ea5ddSQu Wenruo BTRFS_COMPRESS_NONE) 170557ea5ddSQu Wenruo return 0; 171557ea5ddSQu Wenruo 172557ea5ddSQu Wenruo /* Uncompressed inline extent size must match item size */ 173557ea5ddSQu Wenruo if (item_size != BTRFS_FILE_EXTENT_INLINE_DATA_START + 174557ea5ddSQu Wenruo btrfs_file_extent_ram_bytes(leaf, fi)) { 175*2f659546SQu Wenruo file_extent_err(fs_info, leaf, slot, 1768806d718SQu Wenruo "invalid ram_bytes for uncompressed inline extent, have %u expect %llu", 1778806d718SQu Wenruo item_size, BTRFS_FILE_EXTENT_INLINE_DATA_START + 1788806d718SQu Wenruo btrfs_file_extent_ram_bytes(leaf, fi)); 179557ea5ddSQu Wenruo return -EUCLEAN; 180557ea5ddSQu Wenruo } 181557ea5ddSQu Wenruo return 0; 182557ea5ddSQu Wenruo } 183557ea5ddSQu Wenruo 184557ea5ddSQu Wenruo /* Regular or preallocated extent has fixed item size */ 185557ea5ddSQu Wenruo if (item_size != sizeof(*fi)) { 186*2f659546SQu Wenruo file_extent_err(fs_info, leaf, slot, 187709a95c3SArnd Bergmann "invalid item size for reg/prealloc file extent, have %u expect %zu", 1888806d718SQu Wenruo item_size, sizeof(*fi)); 189557ea5ddSQu Wenruo return -EUCLEAN; 190557ea5ddSQu Wenruo } 191*2f659546SQu Wenruo if (CHECK_FE_ALIGNED(fs_info, leaf, slot, fi, ram_bytes, sectorsize) || 192*2f659546SQu Wenruo CHECK_FE_ALIGNED(fs_info, leaf, slot, fi, disk_bytenr, sectorsize) || 193*2f659546SQu Wenruo CHECK_FE_ALIGNED(fs_info, leaf, slot, fi, disk_num_bytes, sectorsize) || 194*2f659546SQu Wenruo CHECK_FE_ALIGNED(fs_info, leaf, slot, fi, offset, sectorsize) || 195*2f659546SQu Wenruo CHECK_FE_ALIGNED(fs_info, leaf, slot, fi, num_bytes, sectorsize)) 196557ea5ddSQu Wenruo return -EUCLEAN; 197557ea5ddSQu Wenruo return 0; 198557ea5ddSQu Wenruo } 199557ea5ddSQu Wenruo 200*2f659546SQu Wenruo static int check_csum_item(struct btrfs_fs_info *fs_info, 201*2f659546SQu Wenruo struct extent_buffer *leaf, struct btrfs_key *key, 202*2f659546SQu Wenruo int slot) 203557ea5ddSQu Wenruo { 204*2f659546SQu Wenruo u32 sectorsize = fs_info->sectorsize; 205*2f659546SQu Wenruo u32 csumsize = btrfs_super_csum_size(fs_info->super_copy); 206557ea5ddSQu Wenruo 207557ea5ddSQu Wenruo if (key->objectid != BTRFS_EXTENT_CSUM_OBJECTID) { 208*2f659546SQu Wenruo generic_err(fs_info, leaf, slot, 209d508c5f0SQu Wenruo "invalid key objectid for csum item, have %llu expect %llu", 210d508c5f0SQu Wenruo key->objectid, BTRFS_EXTENT_CSUM_OBJECTID); 211557ea5ddSQu Wenruo return -EUCLEAN; 212557ea5ddSQu Wenruo } 213557ea5ddSQu Wenruo if (!IS_ALIGNED(key->offset, sectorsize)) { 214*2f659546SQu Wenruo generic_err(fs_info, leaf, slot, 215d508c5f0SQu Wenruo "unaligned key offset for csum item, have %llu should be aligned to %u", 216d508c5f0SQu Wenruo key->offset, sectorsize); 217557ea5ddSQu Wenruo return -EUCLEAN; 218557ea5ddSQu Wenruo } 219557ea5ddSQu Wenruo if (!IS_ALIGNED(btrfs_item_size_nr(leaf, slot), csumsize)) { 220*2f659546SQu Wenruo generic_err(fs_info, leaf, slot, 221d508c5f0SQu Wenruo "unaligned item size for csum item, have %u should be aligned to %u", 222d508c5f0SQu Wenruo btrfs_item_size_nr(leaf, slot), csumsize); 223557ea5ddSQu Wenruo return -EUCLEAN; 224557ea5ddSQu Wenruo } 225557ea5ddSQu Wenruo return 0; 226557ea5ddSQu Wenruo } 227557ea5ddSQu Wenruo 228557ea5ddSQu Wenruo /* 229ad7b0368SQu Wenruo * Customized reported for dir_item, only important new info is key->objectid, 230ad7b0368SQu Wenruo * which represents inode number 231ad7b0368SQu Wenruo */ 232ad7b0368SQu Wenruo __printf(4, 5) 233*2f659546SQu Wenruo static void dir_item_err(const struct btrfs_fs_info *fs_info, 234ad7b0368SQu Wenruo const struct extent_buffer *eb, int slot, 235ad7b0368SQu Wenruo const char *fmt, ...) 236ad7b0368SQu Wenruo { 237ad7b0368SQu Wenruo struct btrfs_key key; 238ad7b0368SQu Wenruo struct va_format vaf; 239ad7b0368SQu Wenruo va_list args; 240ad7b0368SQu Wenruo 241ad7b0368SQu Wenruo btrfs_item_key_to_cpu(eb, &key, slot); 242ad7b0368SQu Wenruo va_start(args, fmt); 243ad7b0368SQu Wenruo 244ad7b0368SQu Wenruo vaf.fmt = fmt; 245ad7b0368SQu Wenruo vaf.va = &args; 246ad7b0368SQu Wenruo 247*2f659546SQu Wenruo btrfs_crit(fs_info, 248ad7b0368SQu Wenruo "corrupt %s: root=%llu block=%llu slot=%d ino=%llu, %pV", 249*2f659546SQu Wenruo btrfs_header_level(eb) == 0 ? "leaf" : "node", 250*2f659546SQu Wenruo btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot, 251*2f659546SQu Wenruo key.objectid, &vaf); 252ad7b0368SQu Wenruo va_end(args); 253ad7b0368SQu Wenruo } 254ad7b0368SQu Wenruo 255*2f659546SQu Wenruo static int check_dir_item(struct btrfs_fs_info *fs_info, 256ad7b0368SQu Wenruo struct extent_buffer *leaf, 257ad7b0368SQu Wenruo struct btrfs_key *key, int slot) 258ad7b0368SQu Wenruo { 259ad7b0368SQu Wenruo struct btrfs_dir_item *di; 260ad7b0368SQu Wenruo u32 item_size = btrfs_item_size_nr(leaf, slot); 261ad7b0368SQu Wenruo u32 cur = 0; 262ad7b0368SQu Wenruo 263ad7b0368SQu Wenruo di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); 264ad7b0368SQu Wenruo while (cur < item_size) { 265ad7b0368SQu Wenruo u32 name_len; 266ad7b0368SQu Wenruo u32 data_len; 267ad7b0368SQu Wenruo u32 max_name_len; 268ad7b0368SQu Wenruo u32 total_size; 269ad7b0368SQu Wenruo u32 name_hash; 270ad7b0368SQu Wenruo u8 dir_type; 271ad7b0368SQu Wenruo 272ad7b0368SQu Wenruo /* header itself should not cross item boundary */ 273ad7b0368SQu Wenruo if (cur + sizeof(*di) > item_size) { 274*2f659546SQu Wenruo dir_item_err(fs_info, leaf, slot, 2757cfad652SArnd Bergmann "dir item header crosses item boundary, have %zu boundary %u", 276ad7b0368SQu Wenruo cur + sizeof(*di), item_size); 277ad7b0368SQu Wenruo return -EUCLEAN; 278ad7b0368SQu Wenruo } 279ad7b0368SQu Wenruo 280ad7b0368SQu Wenruo /* dir type check */ 281ad7b0368SQu Wenruo dir_type = btrfs_dir_type(leaf, di); 282ad7b0368SQu Wenruo if (dir_type >= BTRFS_FT_MAX) { 283*2f659546SQu Wenruo dir_item_err(fs_info, leaf, slot, 284ad7b0368SQu Wenruo "invalid dir item type, have %u expect [0, %u)", 285ad7b0368SQu Wenruo dir_type, BTRFS_FT_MAX); 286ad7b0368SQu Wenruo return -EUCLEAN; 287ad7b0368SQu Wenruo } 288ad7b0368SQu Wenruo 289ad7b0368SQu Wenruo if (key->type == BTRFS_XATTR_ITEM_KEY && 290ad7b0368SQu Wenruo dir_type != BTRFS_FT_XATTR) { 291*2f659546SQu Wenruo dir_item_err(fs_info, leaf, slot, 292ad7b0368SQu Wenruo "invalid dir item type for XATTR key, have %u expect %u", 293ad7b0368SQu Wenruo dir_type, BTRFS_FT_XATTR); 294ad7b0368SQu Wenruo return -EUCLEAN; 295ad7b0368SQu Wenruo } 296ad7b0368SQu Wenruo if (dir_type == BTRFS_FT_XATTR && 297ad7b0368SQu Wenruo key->type != BTRFS_XATTR_ITEM_KEY) { 298*2f659546SQu Wenruo dir_item_err(fs_info, leaf, slot, 299ad7b0368SQu Wenruo "xattr dir type found for non-XATTR key"); 300ad7b0368SQu Wenruo return -EUCLEAN; 301ad7b0368SQu Wenruo } 302ad7b0368SQu Wenruo if (dir_type == BTRFS_FT_XATTR) 303ad7b0368SQu Wenruo max_name_len = XATTR_NAME_MAX; 304ad7b0368SQu Wenruo else 305ad7b0368SQu Wenruo max_name_len = BTRFS_NAME_LEN; 306ad7b0368SQu Wenruo 307ad7b0368SQu Wenruo /* Name/data length check */ 308ad7b0368SQu Wenruo name_len = btrfs_dir_name_len(leaf, di); 309ad7b0368SQu Wenruo data_len = btrfs_dir_data_len(leaf, di); 310ad7b0368SQu Wenruo if (name_len > max_name_len) { 311*2f659546SQu Wenruo dir_item_err(fs_info, leaf, slot, 312ad7b0368SQu Wenruo "dir item name len too long, have %u max %u", 313ad7b0368SQu Wenruo name_len, max_name_len); 314ad7b0368SQu Wenruo return -EUCLEAN; 315ad7b0368SQu Wenruo } 316*2f659546SQu Wenruo if (name_len + data_len > BTRFS_MAX_XATTR_SIZE(fs_info)) { 317*2f659546SQu Wenruo dir_item_err(fs_info, leaf, slot, 318ad7b0368SQu Wenruo "dir item name and data len too long, have %u max %u", 319ad7b0368SQu Wenruo name_len + data_len, 320*2f659546SQu Wenruo BTRFS_MAX_XATTR_SIZE(fs_info)); 321ad7b0368SQu Wenruo return -EUCLEAN; 322ad7b0368SQu Wenruo } 323ad7b0368SQu Wenruo 324ad7b0368SQu Wenruo if (data_len && dir_type != BTRFS_FT_XATTR) { 325*2f659546SQu Wenruo dir_item_err(fs_info, leaf, slot, 326ad7b0368SQu Wenruo "dir item with invalid data len, have %u expect 0", 327ad7b0368SQu Wenruo data_len); 328ad7b0368SQu Wenruo return -EUCLEAN; 329ad7b0368SQu Wenruo } 330ad7b0368SQu Wenruo 331ad7b0368SQu Wenruo total_size = sizeof(*di) + name_len + data_len; 332ad7b0368SQu Wenruo 333ad7b0368SQu Wenruo /* header and name/data should not cross item boundary */ 334ad7b0368SQu Wenruo if (cur + total_size > item_size) { 335*2f659546SQu Wenruo dir_item_err(fs_info, leaf, slot, 336ad7b0368SQu Wenruo "dir item data crosses item boundary, have %u boundary %u", 337ad7b0368SQu Wenruo cur + total_size, item_size); 338ad7b0368SQu Wenruo return -EUCLEAN; 339ad7b0368SQu Wenruo } 340ad7b0368SQu Wenruo 341ad7b0368SQu Wenruo /* 342ad7b0368SQu Wenruo * Special check for XATTR/DIR_ITEM, as key->offset is name 343ad7b0368SQu Wenruo * hash, should match its name 344ad7b0368SQu Wenruo */ 345ad7b0368SQu Wenruo if (key->type == BTRFS_DIR_ITEM_KEY || 346ad7b0368SQu Wenruo key->type == BTRFS_XATTR_ITEM_KEY) { 347e2683fc9SDavid Sterba char namebuf[max(BTRFS_NAME_LEN, XATTR_NAME_MAX)]; 348e2683fc9SDavid Sterba 349ad7b0368SQu Wenruo read_extent_buffer(leaf, namebuf, 350ad7b0368SQu Wenruo (unsigned long)(di + 1), name_len); 351ad7b0368SQu Wenruo name_hash = btrfs_name_hash(namebuf, name_len); 352ad7b0368SQu Wenruo if (key->offset != name_hash) { 353*2f659546SQu Wenruo dir_item_err(fs_info, leaf, slot, 354ad7b0368SQu Wenruo "name hash mismatch with key, have 0x%016x expect 0x%016llx", 355ad7b0368SQu Wenruo name_hash, key->offset); 356ad7b0368SQu Wenruo return -EUCLEAN; 357ad7b0368SQu Wenruo } 358ad7b0368SQu Wenruo } 359ad7b0368SQu Wenruo cur += total_size; 360ad7b0368SQu Wenruo di = (struct btrfs_dir_item *)((void *)di + total_size); 361ad7b0368SQu Wenruo } 362ad7b0368SQu Wenruo return 0; 363ad7b0368SQu Wenruo } 364ad7b0368SQu Wenruo 365ad7b0368SQu Wenruo /* 366557ea5ddSQu Wenruo * Common point to switch the item-specific validation. 367557ea5ddSQu Wenruo */ 368*2f659546SQu Wenruo static int check_leaf_item(struct btrfs_fs_info *fs_info, 369557ea5ddSQu Wenruo struct extent_buffer *leaf, 370557ea5ddSQu Wenruo struct btrfs_key *key, int slot) 371557ea5ddSQu Wenruo { 372557ea5ddSQu Wenruo int ret = 0; 373557ea5ddSQu Wenruo 374557ea5ddSQu Wenruo switch (key->type) { 375557ea5ddSQu Wenruo case BTRFS_EXTENT_DATA_KEY: 376*2f659546SQu Wenruo ret = check_extent_data_item(fs_info, leaf, key, slot); 377557ea5ddSQu Wenruo break; 378557ea5ddSQu Wenruo case BTRFS_EXTENT_CSUM_KEY: 379*2f659546SQu Wenruo ret = check_csum_item(fs_info, leaf, key, slot); 380557ea5ddSQu Wenruo break; 381ad7b0368SQu Wenruo case BTRFS_DIR_ITEM_KEY: 382ad7b0368SQu Wenruo case BTRFS_DIR_INDEX_KEY: 383ad7b0368SQu Wenruo case BTRFS_XATTR_ITEM_KEY: 384*2f659546SQu Wenruo ret = check_dir_item(fs_info, leaf, key, slot); 385ad7b0368SQu Wenruo break; 386557ea5ddSQu Wenruo } 387557ea5ddSQu Wenruo return ret; 388557ea5ddSQu Wenruo } 389557ea5ddSQu Wenruo 390*2f659546SQu Wenruo static int check_leaf(struct btrfs_fs_info *fs_info, struct extent_buffer *leaf, 39169fc6cbbSQu Wenruo bool check_item_data) 392557ea5ddSQu Wenruo { 393557ea5ddSQu Wenruo /* No valid key type is 0, so all key should be larger than this key */ 394557ea5ddSQu Wenruo struct btrfs_key prev_key = {0, 0, 0}; 395557ea5ddSQu Wenruo struct btrfs_key key; 396557ea5ddSQu Wenruo u32 nritems = btrfs_header_nritems(leaf); 397557ea5ddSQu Wenruo int slot; 398557ea5ddSQu Wenruo 399557ea5ddSQu Wenruo /* 400557ea5ddSQu Wenruo * Extent buffers from a relocation tree have a owner field that 401557ea5ddSQu Wenruo * corresponds to the subvolume tree they are based on. So just from an 402557ea5ddSQu Wenruo * extent buffer alone we can not find out what is the id of the 403557ea5ddSQu Wenruo * corresponding subvolume tree, so we can not figure out if the extent 404557ea5ddSQu Wenruo * buffer corresponds to the root of the relocation tree or not. So 405557ea5ddSQu Wenruo * skip this check for relocation trees. 406557ea5ddSQu Wenruo */ 407557ea5ddSQu Wenruo if (nritems == 0 && !btrfs_header_flag(leaf, BTRFS_HEADER_FLAG_RELOC)) { 408557ea5ddSQu Wenruo struct btrfs_root *check_root; 409557ea5ddSQu Wenruo 410557ea5ddSQu Wenruo key.objectid = btrfs_header_owner(leaf); 411557ea5ddSQu Wenruo key.type = BTRFS_ROOT_ITEM_KEY; 412557ea5ddSQu Wenruo key.offset = (u64)-1; 413557ea5ddSQu Wenruo 414557ea5ddSQu Wenruo check_root = btrfs_get_fs_root(fs_info, &key, false); 415557ea5ddSQu Wenruo /* 416557ea5ddSQu Wenruo * The only reason we also check NULL here is that during 417557ea5ddSQu Wenruo * open_ctree() some roots has not yet been set up. 418557ea5ddSQu Wenruo */ 419557ea5ddSQu Wenruo if (!IS_ERR_OR_NULL(check_root)) { 420557ea5ddSQu Wenruo struct extent_buffer *eb; 421557ea5ddSQu Wenruo 422557ea5ddSQu Wenruo eb = btrfs_root_node(check_root); 423557ea5ddSQu Wenruo /* if leaf is the root, then it's fine */ 424557ea5ddSQu Wenruo if (leaf != eb) { 425*2f659546SQu Wenruo generic_err(fs_info, leaf, 0, 426478d01b3SQu Wenruo "invalid nritems, have %u should not be 0 for non-root leaf", 427478d01b3SQu Wenruo nritems); 428557ea5ddSQu Wenruo free_extent_buffer(eb); 429557ea5ddSQu Wenruo return -EUCLEAN; 430557ea5ddSQu Wenruo } 431557ea5ddSQu Wenruo free_extent_buffer(eb); 432557ea5ddSQu Wenruo } 433557ea5ddSQu Wenruo return 0; 434557ea5ddSQu Wenruo } 435557ea5ddSQu Wenruo 436557ea5ddSQu Wenruo if (nritems == 0) 437557ea5ddSQu Wenruo return 0; 438557ea5ddSQu Wenruo 439557ea5ddSQu Wenruo /* 440557ea5ddSQu Wenruo * Check the following things to make sure this is a good leaf, and 441557ea5ddSQu Wenruo * leaf users won't need to bother with similar sanity checks: 442557ea5ddSQu Wenruo * 443557ea5ddSQu Wenruo * 1) key ordering 444557ea5ddSQu Wenruo * 2) item offset and size 445557ea5ddSQu Wenruo * No overlap, no hole, all inside the leaf. 446557ea5ddSQu Wenruo * 3) item content 447557ea5ddSQu Wenruo * If possible, do comprehensive sanity check. 448557ea5ddSQu Wenruo * NOTE: All checks must only rely on the item data itself. 449557ea5ddSQu Wenruo */ 450557ea5ddSQu Wenruo for (slot = 0; slot < nritems; slot++) { 451557ea5ddSQu Wenruo u32 item_end_expected; 452557ea5ddSQu Wenruo int ret; 453557ea5ddSQu Wenruo 454557ea5ddSQu Wenruo btrfs_item_key_to_cpu(leaf, &key, slot); 455557ea5ddSQu Wenruo 456557ea5ddSQu Wenruo /* Make sure the keys are in the right order */ 457557ea5ddSQu Wenruo if (btrfs_comp_cpu_keys(&prev_key, &key) >= 0) { 458*2f659546SQu Wenruo generic_err(fs_info, leaf, slot, 459478d01b3SQu Wenruo "bad key order, prev (%llu %u %llu) current (%llu %u %llu)", 460478d01b3SQu Wenruo prev_key.objectid, prev_key.type, 461478d01b3SQu Wenruo prev_key.offset, key.objectid, key.type, 462478d01b3SQu Wenruo key.offset); 463557ea5ddSQu Wenruo return -EUCLEAN; 464557ea5ddSQu Wenruo } 465557ea5ddSQu Wenruo 466557ea5ddSQu Wenruo /* 467557ea5ddSQu Wenruo * Make sure the offset and ends are right, remember that the 468557ea5ddSQu Wenruo * item data starts at the end of the leaf and grows towards the 469557ea5ddSQu Wenruo * front. 470557ea5ddSQu Wenruo */ 471557ea5ddSQu Wenruo if (slot == 0) 472557ea5ddSQu Wenruo item_end_expected = BTRFS_LEAF_DATA_SIZE(fs_info); 473557ea5ddSQu Wenruo else 474557ea5ddSQu Wenruo item_end_expected = btrfs_item_offset_nr(leaf, 475557ea5ddSQu Wenruo slot - 1); 476557ea5ddSQu Wenruo if (btrfs_item_end_nr(leaf, slot) != item_end_expected) { 477*2f659546SQu Wenruo generic_err(fs_info, leaf, slot, 478478d01b3SQu Wenruo "unexpected item end, have %u expect %u", 479478d01b3SQu Wenruo btrfs_item_end_nr(leaf, slot), 480478d01b3SQu Wenruo item_end_expected); 481557ea5ddSQu Wenruo return -EUCLEAN; 482557ea5ddSQu Wenruo } 483557ea5ddSQu Wenruo 484557ea5ddSQu Wenruo /* 485557ea5ddSQu Wenruo * Check to make sure that we don't point outside of the leaf, 486557ea5ddSQu Wenruo * just in case all the items are consistent to each other, but 487557ea5ddSQu Wenruo * all point outside of the leaf. 488557ea5ddSQu Wenruo */ 489557ea5ddSQu Wenruo if (btrfs_item_end_nr(leaf, slot) > 490557ea5ddSQu Wenruo BTRFS_LEAF_DATA_SIZE(fs_info)) { 491*2f659546SQu Wenruo generic_err(fs_info, leaf, slot, 492478d01b3SQu Wenruo "slot end outside of leaf, have %u expect range [0, %u]", 493478d01b3SQu Wenruo btrfs_item_end_nr(leaf, slot), 494478d01b3SQu Wenruo BTRFS_LEAF_DATA_SIZE(fs_info)); 495557ea5ddSQu Wenruo return -EUCLEAN; 496557ea5ddSQu Wenruo } 497557ea5ddSQu Wenruo 498557ea5ddSQu Wenruo /* Also check if the item pointer overlaps with btrfs item. */ 499557ea5ddSQu Wenruo if (btrfs_item_nr_offset(slot) + sizeof(struct btrfs_item) > 500557ea5ddSQu Wenruo btrfs_item_ptr_offset(leaf, slot)) { 501*2f659546SQu Wenruo generic_err(fs_info, leaf, slot, 502478d01b3SQu Wenruo "slot overlaps with its data, item end %lu data start %lu", 503478d01b3SQu Wenruo btrfs_item_nr_offset(slot) + 504478d01b3SQu Wenruo sizeof(struct btrfs_item), 505478d01b3SQu Wenruo btrfs_item_ptr_offset(leaf, slot)); 506557ea5ddSQu Wenruo return -EUCLEAN; 507557ea5ddSQu Wenruo } 508557ea5ddSQu Wenruo 50969fc6cbbSQu Wenruo if (check_item_data) { 51069fc6cbbSQu Wenruo /* 51169fc6cbbSQu Wenruo * Check if the item size and content meet other 51269fc6cbbSQu Wenruo * criteria 51369fc6cbbSQu Wenruo */ 514*2f659546SQu Wenruo ret = check_leaf_item(fs_info, leaf, &key, slot); 515557ea5ddSQu Wenruo if (ret < 0) 516557ea5ddSQu Wenruo return ret; 51769fc6cbbSQu Wenruo } 518557ea5ddSQu Wenruo 519557ea5ddSQu Wenruo prev_key.objectid = key.objectid; 520557ea5ddSQu Wenruo prev_key.type = key.type; 521557ea5ddSQu Wenruo prev_key.offset = key.offset; 522557ea5ddSQu Wenruo } 523557ea5ddSQu Wenruo 524557ea5ddSQu Wenruo return 0; 525557ea5ddSQu Wenruo } 526557ea5ddSQu Wenruo 527*2f659546SQu Wenruo int btrfs_check_leaf_full(struct btrfs_fs_info *fs_info, 52869fc6cbbSQu Wenruo struct extent_buffer *leaf) 52969fc6cbbSQu Wenruo { 530*2f659546SQu Wenruo return check_leaf(fs_info, leaf, true); 53169fc6cbbSQu Wenruo } 53269fc6cbbSQu Wenruo 533*2f659546SQu Wenruo int btrfs_check_leaf_relaxed(struct btrfs_fs_info *fs_info, 534*2f659546SQu Wenruo struct extent_buffer *leaf) 535*2f659546SQu Wenruo { 536*2f659546SQu Wenruo return check_leaf(fs_info, leaf, false); 537*2f659546SQu Wenruo } 538*2f659546SQu Wenruo 539*2f659546SQu Wenruo int btrfs_check_node(struct btrfs_fs_info *fs_info, struct extent_buffer *node) 540557ea5ddSQu Wenruo { 541557ea5ddSQu Wenruo unsigned long nr = btrfs_header_nritems(node); 542557ea5ddSQu Wenruo struct btrfs_key key, next_key; 543557ea5ddSQu Wenruo int slot; 544557ea5ddSQu Wenruo u64 bytenr; 545557ea5ddSQu Wenruo int ret = 0; 546557ea5ddSQu Wenruo 547*2f659546SQu Wenruo if (nr == 0 || nr > BTRFS_NODEPTRS_PER_BLOCK(fs_info)) { 548*2f659546SQu Wenruo btrfs_crit(fs_info, 549bba4f298SQu Wenruo "corrupt node: root=%llu block=%llu, nritems too %s, have %lu expect range [1,%u]", 550*2f659546SQu Wenruo btrfs_header_owner(node), node->start, 551bba4f298SQu Wenruo nr == 0 ? "small" : "large", nr, 552*2f659546SQu Wenruo BTRFS_NODEPTRS_PER_BLOCK(fs_info)); 553bba4f298SQu Wenruo return -EUCLEAN; 554557ea5ddSQu Wenruo } 555557ea5ddSQu Wenruo 556557ea5ddSQu Wenruo for (slot = 0; slot < nr - 1; slot++) { 557557ea5ddSQu Wenruo bytenr = btrfs_node_blockptr(node, slot); 558557ea5ddSQu Wenruo btrfs_node_key_to_cpu(node, &key, slot); 559557ea5ddSQu Wenruo btrfs_node_key_to_cpu(node, &next_key, slot + 1); 560557ea5ddSQu Wenruo 561557ea5ddSQu Wenruo if (!bytenr) { 562*2f659546SQu Wenruo generic_err(fs_info, node, slot, 563bba4f298SQu Wenruo "invalid NULL node pointer"); 564bba4f298SQu Wenruo ret = -EUCLEAN; 565bba4f298SQu Wenruo goto out; 566bba4f298SQu Wenruo } 567*2f659546SQu Wenruo if (!IS_ALIGNED(bytenr, fs_info->sectorsize)) { 568*2f659546SQu Wenruo generic_err(fs_info, node, slot, 569bba4f298SQu Wenruo "unaligned pointer, have %llu should be aligned to %u", 570*2f659546SQu Wenruo bytenr, fs_info->sectorsize); 571bba4f298SQu Wenruo ret = -EUCLEAN; 572557ea5ddSQu Wenruo goto out; 573557ea5ddSQu Wenruo } 574557ea5ddSQu Wenruo 575557ea5ddSQu Wenruo if (btrfs_comp_cpu_keys(&key, &next_key) >= 0) { 576*2f659546SQu Wenruo generic_err(fs_info, node, slot, 577bba4f298SQu Wenruo "bad key order, current (%llu %u %llu) next (%llu %u %llu)", 578bba4f298SQu Wenruo key.objectid, key.type, key.offset, 579bba4f298SQu Wenruo next_key.objectid, next_key.type, 580bba4f298SQu Wenruo next_key.offset); 581bba4f298SQu Wenruo ret = -EUCLEAN; 582557ea5ddSQu Wenruo goto out; 583557ea5ddSQu Wenruo } 584557ea5ddSQu Wenruo } 585557ea5ddSQu Wenruo out: 586557ea5ddSQu Wenruo return ret; 587557ea5ddSQu Wenruo } 588