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" 33557ea5ddSQu Wenruo 34bba4f298SQu Wenruo /* 35bba4f298SQu Wenruo * Error message should follow the following format: 36bba4f298SQu Wenruo * corrupt <type>: <identifier>, <reason>[, <bad_value>] 37bba4f298SQu Wenruo * 38bba4f298SQu Wenruo * @type: leaf or node 39bba4f298SQu Wenruo * @identifier: the necessary info to locate the leaf/node. 40bba4f298SQu Wenruo * It's recommened to decode key.objecitd/offset if it's 41bba4f298SQu Wenruo * meaningful. 42bba4f298SQu Wenruo * @reason: describe the error 43bba4f298SQu Wenruo * @bad_value: optional, it's recommened to output bad value and its 44bba4f298SQu Wenruo * expected value (range). 45bba4f298SQu Wenruo * 46bba4f298SQu Wenruo * Since comma is used to separate the components, only space is allowed 47bba4f298SQu Wenruo * inside each component. 48bba4f298SQu Wenruo */ 49bba4f298SQu Wenruo 50bba4f298SQu Wenruo /* 51bba4f298SQu Wenruo * Append generic "corrupt leaf/node root=%llu block=%llu slot=%d: " to @fmt. 52bba4f298SQu Wenruo * Allows callers to customize the output. 53bba4f298SQu Wenruo */ 54bba4f298SQu Wenruo __printf(4, 5) 55*e67c718bSDavid Sterba __cold 562f659546SQu 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 682f659546SQu 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", 712f659546SQu 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*e67c718bSDavid Sterba __cold 812f659546SQu Wenruo static void file_extent_err(const struct btrfs_fs_info *fs_info, 828806d718SQu Wenruo const struct extent_buffer *eb, int slot, 838806d718SQu Wenruo const char *fmt, ...) 848806d718SQu Wenruo { 858806d718SQu Wenruo struct btrfs_key key; 868806d718SQu Wenruo struct va_format vaf; 878806d718SQu Wenruo va_list args; 888806d718SQu Wenruo 898806d718SQu Wenruo btrfs_item_key_to_cpu(eb, &key, slot); 908806d718SQu Wenruo va_start(args, fmt); 918806d718SQu Wenruo 928806d718SQu Wenruo vaf.fmt = fmt; 938806d718SQu Wenruo vaf.va = &args; 948806d718SQu Wenruo 952f659546SQu Wenruo btrfs_crit(fs_info, 968806d718SQu Wenruo "corrupt %s: root=%llu block=%llu slot=%d ino=%llu file_offset=%llu, %pV", 972f659546SQu Wenruo btrfs_header_level(eb) == 0 ? "leaf" : "node", 982f659546SQu Wenruo btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot, 992f659546SQu Wenruo key.objectid, key.offset, &vaf); 1008806d718SQu Wenruo va_end(args); 1018806d718SQu Wenruo } 1028806d718SQu Wenruo 1038806d718SQu Wenruo /* 1048806d718SQu Wenruo * Return 0 if the btrfs_file_extent_##name is aligned to @alignment 1058806d718SQu Wenruo * Else return 1 1068806d718SQu Wenruo */ 1072f659546SQu Wenruo #define CHECK_FE_ALIGNED(fs_info, leaf, slot, fi, name, alignment) \ 1088806d718SQu Wenruo ({ \ 1098806d718SQu Wenruo if (!IS_ALIGNED(btrfs_file_extent_##name((leaf), (fi)), (alignment))) \ 1102f659546SQu Wenruo file_extent_err((fs_info), (leaf), (slot), \ 1118806d718SQu Wenruo "invalid %s for file extent, have %llu, should be aligned to %u", \ 1128806d718SQu Wenruo (#name), btrfs_file_extent_##name((leaf), (fi)), \ 1138806d718SQu Wenruo (alignment)); \ 1148806d718SQu Wenruo (!IS_ALIGNED(btrfs_file_extent_##name((leaf), (fi)), (alignment))); \ 1158806d718SQu Wenruo }) 1168806d718SQu Wenruo 1172f659546SQu Wenruo static int check_extent_data_item(struct btrfs_fs_info *fs_info, 118557ea5ddSQu Wenruo struct extent_buffer *leaf, 119557ea5ddSQu Wenruo struct btrfs_key *key, int slot) 120557ea5ddSQu Wenruo { 121557ea5ddSQu Wenruo struct btrfs_file_extent_item *fi; 1222f659546SQu Wenruo u32 sectorsize = fs_info->sectorsize; 123557ea5ddSQu Wenruo u32 item_size = btrfs_item_size_nr(leaf, slot); 124557ea5ddSQu Wenruo 125557ea5ddSQu Wenruo if (!IS_ALIGNED(key->offset, sectorsize)) { 1262f659546SQu Wenruo file_extent_err(fs_info, leaf, slot, 1278806d718SQu Wenruo "unaligned file_offset for file extent, have %llu should be aligned to %u", 1288806d718SQu Wenruo key->offset, sectorsize); 129557ea5ddSQu Wenruo return -EUCLEAN; 130557ea5ddSQu Wenruo } 131557ea5ddSQu Wenruo 132557ea5ddSQu Wenruo fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); 133557ea5ddSQu Wenruo 134557ea5ddSQu Wenruo if (btrfs_file_extent_type(leaf, fi) > BTRFS_FILE_EXTENT_TYPES) { 1352f659546SQu Wenruo file_extent_err(fs_info, leaf, slot, 1368806d718SQu Wenruo "invalid type for file extent, have %u expect range [0, %u]", 1378806d718SQu Wenruo btrfs_file_extent_type(leaf, fi), 1388806d718SQu Wenruo BTRFS_FILE_EXTENT_TYPES); 139557ea5ddSQu Wenruo return -EUCLEAN; 140557ea5ddSQu Wenruo } 141557ea5ddSQu Wenruo 142557ea5ddSQu Wenruo /* 143557ea5ddSQu Wenruo * Support for new compression/encrption must introduce incompat flag, 144557ea5ddSQu Wenruo * and must be caught in open_ctree(). 145557ea5ddSQu Wenruo */ 146557ea5ddSQu Wenruo if (btrfs_file_extent_compression(leaf, fi) > BTRFS_COMPRESS_TYPES) { 1472f659546SQu Wenruo file_extent_err(fs_info, leaf, slot, 1488806d718SQu Wenruo "invalid compression for file extent, have %u expect range [0, %u]", 1498806d718SQu Wenruo btrfs_file_extent_compression(leaf, fi), 1508806d718SQu Wenruo BTRFS_COMPRESS_TYPES); 151557ea5ddSQu Wenruo return -EUCLEAN; 152557ea5ddSQu Wenruo } 153557ea5ddSQu Wenruo if (btrfs_file_extent_encryption(leaf, fi)) { 1542f659546SQu Wenruo file_extent_err(fs_info, leaf, slot, 1558806d718SQu Wenruo "invalid encryption for file extent, have %u expect 0", 1568806d718SQu Wenruo btrfs_file_extent_encryption(leaf, fi)); 157557ea5ddSQu Wenruo return -EUCLEAN; 158557ea5ddSQu Wenruo } 159557ea5ddSQu Wenruo if (btrfs_file_extent_type(leaf, fi) == BTRFS_FILE_EXTENT_INLINE) { 160557ea5ddSQu Wenruo /* Inline extent must have 0 as key offset */ 161557ea5ddSQu Wenruo if (key->offset) { 1622f659546SQu Wenruo file_extent_err(fs_info, leaf, slot, 1638806d718SQu Wenruo "invalid file_offset for inline file extent, have %llu expect 0", 1648806d718SQu Wenruo key->offset); 165557ea5ddSQu Wenruo return -EUCLEAN; 166557ea5ddSQu Wenruo } 167557ea5ddSQu Wenruo 168557ea5ddSQu Wenruo /* Compressed inline extent has no on-disk size, skip it */ 169557ea5ddSQu Wenruo if (btrfs_file_extent_compression(leaf, fi) != 170557ea5ddSQu Wenruo BTRFS_COMPRESS_NONE) 171557ea5ddSQu Wenruo return 0; 172557ea5ddSQu Wenruo 173557ea5ddSQu Wenruo /* Uncompressed inline extent size must match item size */ 174557ea5ddSQu Wenruo if (item_size != BTRFS_FILE_EXTENT_INLINE_DATA_START + 175557ea5ddSQu Wenruo btrfs_file_extent_ram_bytes(leaf, fi)) { 1762f659546SQu Wenruo file_extent_err(fs_info, leaf, slot, 1778806d718SQu Wenruo "invalid ram_bytes for uncompressed inline extent, have %u expect %llu", 1788806d718SQu Wenruo item_size, BTRFS_FILE_EXTENT_INLINE_DATA_START + 1798806d718SQu Wenruo btrfs_file_extent_ram_bytes(leaf, fi)); 180557ea5ddSQu Wenruo return -EUCLEAN; 181557ea5ddSQu Wenruo } 182557ea5ddSQu Wenruo return 0; 183557ea5ddSQu Wenruo } 184557ea5ddSQu Wenruo 185557ea5ddSQu Wenruo /* Regular or preallocated extent has fixed item size */ 186557ea5ddSQu Wenruo if (item_size != sizeof(*fi)) { 1872f659546SQu Wenruo file_extent_err(fs_info, leaf, slot, 188709a95c3SArnd Bergmann "invalid item size for reg/prealloc file extent, have %u expect %zu", 1898806d718SQu Wenruo item_size, sizeof(*fi)); 190557ea5ddSQu Wenruo return -EUCLEAN; 191557ea5ddSQu Wenruo } 1922f659546SQu Wenruo if (CHECK_FE_ALIGNED(fs_info, leaf, slot, fi, ram_bytes, sectorsize) || 1932f659546SQu Wenruo CHECK_FE_ALIGNED(fs_info, leaf, slot, fi, disk_bytenr, sectorsize) || 1942f659546SQu Wenruo CHECK_FE_ALIGNED(fs_info, leaf, slot, fi, disk_num_bytes, sectorsize) || 1952f659546SQu Wenruo CHECK_FE_ALIGNED(fs_info, leaf, slot, fi, offset, sectorsize) || 1962f659546SQu Wenruo CHECK_FE_ALIGNED(fs_info, leaf, slot, fi, num_bytes, sectorsize)) 197557ea5ddSQu Wenruo return -EUCLEAN; 198557ea5ddSQu Wenruo return 0; 199557ea5ddSQu Wenruo } 200557ea5ddSQu Wenruo 2012f659546SQu Wenruo static int check_csum_item(struct btrfs_fs_info *fs_info, 2022f659546SQu Wenruo struct extent_buffer *leaf, struct btrfs_key *key, 2032f659546SQu Wenruo int slot) 204557ea5ddSQu Wenruo { 2052f659546SQu Wenruo u32 sectorsize = fs_info->sectorsize; 2062f659546SQu Wenruo u32 csumsize = btrfs_super_csum_size(fs_info->super_copy); 207557ea5ddSQu Wenruo 208557ea5ddSQu Wenruo if (key->objectid != BTRFS_EXTENT_CSUM_OBJECTID) { 2092f659546SQu Wenruo generic_err(fs_info, leaf, slot, 210d508c5f0SQu Wenruo "invalid key objectid for csum item, have %llu expect %llu", 211d508c5f0SQu Wenruo key->objectid, BTRFS_EXTENT_CSUM_OBJECTID); 212557ea5ddSQu Wenruo return -EUCLEAN; 213557ea5ddSQu Wenruo } 214557ea5ddSQu Wenruo if (!IS_ALIGNED(key->offset, sectorsize)) { 2152f659546SQu Wenruo generic_err(fs_info, leaf, slot, 216d508c5f0SQu Wenruo "unaligned key offset for csum item, have %llu should be aligned to %u", 217d508c5f0SQu Wenruo key->offset, sectorsize); 218557ea5ddSQu Wenruo return -EUCLEAN; 219557ea5ddSQu Wenruo } 220557ea5ddSQu Wenruo if (!IS_ALIGNED(btrfs_item_size_nr(leaf, slot), csumsize)) { 2212f659546SQu Wenruo generic_err(fs_info, leaf, slot, 222d508c5f0SQu Wenruo "unaligned item size for csum item, have %u should be aligned to %u", 223d508c5f0SQu Wenruo btrfs_item_size_nr(leaf, slot), csumsize); 224557ea5ddSQu Wenruo return -EUCLEAN; 225557ea5ddSQu Wenruo } 226557ea5ddSQu Wenruo return 0; 227557ea5ddSQu Wenruo } 228557ea5ddSQu Wenruo 229557ea5ddSQu Wenruo /* 230ad7b0368SQu Wenruo * Customized reported for dir_item, only important new info is key->objectid, 231ad7b0368SQu Wenruo * which represents inode number 232ad7b0368SQu Wenruo */ 233ad7b0368SQu Wenruo __printf(4, 5) 234*e67c718bSDavid Sterba __cold 2352f659546SQu Wenruo static void dir_item_err(const struct btrfs_fs_info *fs_info, 236ad7b0368SQu Wenruo const struct extent_buffer *eb, int slot, 237ad7b0368SQu Wenruo const char *fmt, ...) 238ad7b0368SQu Wenruo { 239ad7b0368SQu Wenruo struct btrfs_key key; 240ad7b0368SQu Wenruo struct va_format vaf; 241ad7b0368SQu Wenruo va_list args; 242ad7b0368SQu Wenruo 243ad7b0368SQu Wenruo btrfs_item_key_to_cpu(eb, &key, slot); 244ad7b0368SQu Wenruo va_start(args, fmt); 245ad7b0368SQu Wenruo 246ad7b0368SQu Wenruo vaf.fmt = fmt; 247ad7b0368SQu Wenruo vaf.va = &args; 248ad7b0368SQu Wenruo 2492f659546SQu Wenruo btrfs_crit(fs_info, 250ad7b0368SQu Wenruo "corrupt %s: root=%llu block=%llu slot=%d ino=%llu, %pV", 2512f659546SQu Wenruo btrfs_header_level(eb) == 0 ? "leaf" : "node", 2522f659546SQu Wenruo btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot, 2532f659546SQu Wenruo key.objectid, &vaf); 254ad7b0368SQu Wenruo va_end(args); 255ad7b0368SQu Wenruo } 256ad7b0368SQu Wenruo 2572f659546SQu Wenruo static int check_dir_item(struct btrfs_fs_info *fs_info, 258ad7b0368SQu Wenruo struct extent_buffer *leaf, 259ad7b0368SQu Wenruo struct btrfs_key *key, int slot) 260ad7b0368SQu Wenruo { 261ad7b0368SQu Wenruo struct btrfs_dir_item *di; 262ad7b0368SQu Wenruo u32 item_size = btrfs_item_size_nr(leaf, slot); 263ad7b0368SQu Wenruo u32 cur = 0; 264ad7b0368SQu Wenruo 265ad7b0368SQu Wenruo di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); 266ad7b0368SQu Wenruo while (cur < item_size) { 267ad7b0368SQu Wenruo u32 name_len; 268ad7b0368SQu Wenruo u32 data_len; 269ad7b0368SQu Wenruo u32 max_name_len; 270ad7b0368SQu Wenruo u32 total_size; 271ad7b0368SQu Wenruo u32 name_hash; 272ad7b0368SQu Wenruo u8 dir_type; 273ad7b0368SQu Wenruo 274ad7b0368SQu Wenruo /* header itself should not cross item boundary */ 275ad7b0368SQu Wenruo if (cur + sizeof(*di) > item_size) { 2762f659546SQu Wenruo dir_item_err(fs_info, leaf, slot, 2777cfad652SArnd Bergmann "dir item header crosses item boundary, have %zu boundary %u", 278ad7b0368SQu Wenruo cur + sizeof(*di), item_size); 279ad7b0368SQu Wenruo return -EUCLEAN; 280ad7b0368SQu Wenruo } 281ad7b0368SQu Wenruo 282ad7b0368SQu Wenruo /* dir type check */ 283ad7b0368SQu Wenruo dir_type = btrfs_dir_type(leaf, di); 284ad7b0368SQu Wenruo if (dir_type >= BTRFS_FT_MAX) { 2852f659546SQu Wenruo dir_item_err(fs_info, leaf, slot, 286ad7b0368SQu Wenruo "invalid dir item type, have %u expect [0, %u)", 287ad7b0368SQu Wenruo dir_type, BTRFS_FT_MAX); 288ad7b0368SQu Wenruo return -EUCLEAN; 289ad7b0368SQu Wenruo } 290ad7b0368SQu Wenruo 291ad7b0368SQu Wenruo if (key->type == BTRFS_XATTR_ITEM_KEY && 292ad7b0368SQu Wenruo dir_type != BTRFS_FT_XATTR) { 2932f659546SQu Wenruo dir_item_err(fs_info, leaf, slot, 294ad7b0368SQu Wenruo "invalid dir item type for XATTR key, have %u expect %u", 295ad7b0368SQu Wenruo dir_type, BTRFS_FT_XATTR); 296ad7b0368SQu Wenruo return -EUCLEAN; 297ad7b0368SQu Wenruo } 298ad7b0368SQu Wenruo if (dir_type == BTRFS_FT_XATTR && 299ad7b0368SQu Wenruo key->type != BTRFS_XATTR_ITEM_KEY) { 3002f659546SQu Wenruo dir_item_err(fs_info, leaf, slot, 301ad7b0368SQu Wenruo "xattr dir type found for non-XATTR key"); 302ad7b0368SQu Wenruo return -EUCLEAN; 303ad7b0368SQu Wenruo } 304ad7b0368SQu Wenruo if (dir_type == BTRFS_FT_XATTR) 305ad7b0368SQu Wenruo max_name_len = XATTR_NAME_MAX; 306ad7b0368SQu Wenruo else 307ad7b0368SQu Wenruo max_name_len = BTRFS_NAME_LEN; 308ad7b0368SQu Wenruo 309ad7b0368SQu Wenruo /* Name/data length check */ 310ad7b0368SQu Wenruo name_len = btrfs_dir_name_len(leaf, di); 311ad7b0368SQu Wenruo data_len = btrfs_dir_data_len(leaf, di); 312ad7b0368SQu Wenruo if (name_len > max_name_len) { 3132f659546SQu Wenruo dir_item_err(fs_info, leaf, slot, 314ad7b0368SQu Wenruo "dir item name len too long, have %u max %u", 315ad7b0368SQu Wenruo name_len, max_name_len); 316ad7b0368SQu Wenruo return -EUCLEAN; 317ad7b0368SQu Wenruo } 3182f659546SQu Wenruo if (name_len + data_len > BTRFS_MAX_XATTR_SIZE(fs_info)) { 3192f659546SQu Wenruo dir_item_err(fs_info, leaf, slot, 320ad7b0368SQu Wenruo "dir item name and data len too long, have %u max %u", 321ad7b0368SQu Wenruo name_len + data_len, 3222f659546SQu Wenruo BTRFS_MAX_XATTR_SIZE(fs_info)); 323ad7b0368SQu Wenruo return -EUCLEAN; 324ad7b0368SQu Wenruo } 325ad7b0368SQu Wenruo 326ad7b0368SQu Wenruo if (data_len && dir_type != BTRFS_FT_XATTR) { 3272f659546SQu Wenruo dir_item_err(fs_info, leaf, slot, 328ad7b0368SQu Wenruo "dir item with invalid data len, have %u expect 0", 329ad7b0368SQu Wenruo data_len); 330ad7b0368SQu Wenruo return -EUCLEAN; 331ad7b0368SQu Wenruo } 332ad7b0368SQu Wenruo 333ad7b0368SQu Wenruo total_size = sizeof(*di) + name_len + data_len; 334ad7b0368SQu Wenruo 335ad7b0368SQu Wenruo /* header and name/data should not cross item boundary */ 336ad7b0368SQu Wenruo if (cur + total_size > item_size) { 3372f659546SQu Wenruo dir_item_err(fs_info, leaf, slot, 338ad7b0368SQu Wenruo "dir item data crosses item boundary, have %u boundary %u", 339ad7b0368SQu Wenruo cur + total_size, item_size); 340ad7b0368SQu Wenruo return -EUCLEAN; 341ad7b0368SQu Wenruo } 342ad7b0368SQu Wenruo 343ad7b0368SQu Wenruo /* 344ad7b0368SQu Wenruo * Special check for XATTR/DIR_ITEM, as key->offset is name 345ad7b0368SQu Wenruo * hash, should match its name 346ad7b0368SQu Wenruo */ 347ad7b0368SQu Wenruo if (key->type == BTRFS_DIR_ITEM_KEY || 348ad7b0368SQu Wenruo key->type == BTRFS_XATTR_ITEM_KEY) { 349e2683fc9SDavid Sterba char namebuf[max(BTRFS_NAME_LEN, XATTR_NAME_MAX)]; 350e2683fc9SDavid Sterba 351ad7b0368SQu Wenruo read_extent_buffer(leaf, namebuf, 352ad7b0368SQu Wenruo (unsigned long)(di + 1), name_len); 353ad7b0368SQu Wenruo name_hash = btrfs_name_hash(namebuf, name_len); 354ad7b0368SQu Wenruo if (key->offset != name_hash) { 3552f659546SQu Wenruo dir_item_err(fs_info, leaf, slot, 356ad7b0368SQu Wenruo "name hash mismatch with key, have 0x%016x expect 0x%016llx", 357ad7b0368SQu Wenruo name_hash, key->offset); 358ad7b0368SQu Wenruo return -EUCLEAN; 359ad7b0368SQu Wenruo } 360ad7b0368SQu Wenruo } 361ad7b0368SQu Wenruo cur += total_size; 362ad7b0368SQu Wenruo di = (struct btrfs_dir_item *)((void *)di + total_size); 363ad7b0368SQu Wenruo } 364ad7b0368SQu Wenruo return 0; 365ad7b0368SQu Wenruo } 366ad7b0368SQu Wenruo 367ad7b0368SQu Wenruo /* 368557ea5ddSQu Wenruo * Common point to switch the item-specific validation. 369557ea5ddSQu Wenruo */ 3702f659546SQu Wenruo static int check_leaf_item(struct btrfs_fs_info *fs_info, 371557ea5ddSQu Wenruo struct extent_buffer *leaf, 372557ea5ddSQu Wenruo struct btrfs_key *key, int slot) 373557ea5ddSQu Wenruo { 374557ea5ddSQu Wenruo int ret = 0; 375557ea5ddSQu Wenruo 376557ea5ddSQu Wenruo switch (key->type) { 377557ea5ddSQu Wenruo case BTRFS_EXTENT_DATA_KEY: 3782f659546SQu Wenruo ret = check_extent_data_item(fs_info, leaf, key, slot); 379557ea5ddSQu Wenruo break; 380557ea5ddSQu Wenruo case BTRFS_EXTENT_CSUM_KEY: 3812f659546SQu Wenruo ret = check_csum_item(fs_info, leaf, key, slot); 382557ea5ddSQu Wenruo break; 383ad7b0368SQu Wenruo case BTRFS_DIR_ITEM_KEY: 384ad7b0368SQu Wenruo case BTRFS_DIR_INDEX_KEY: 385ad7b0368SQu Wenruo case BTRFS_XATTR_ITEM_KEY: 3862f659546SQu Wenruo ret = check_dir_item(fs_info, leaf, key, slot); 387ad7b0368SQu Wenruo break; 388557ea5ddSQu Wenruo } 389557ea5ddSQu Wenruo return ret; 390557ea5ddSQu Wenruo } 391557ea5ddSQu Wenruo 3922f659546SQu Wenruo static int check_leaf(struct btrfs_fs_info *fs_info, struct extent_buffer *leaf, 39369fc6cbbSQu Wenruo bool check_item_data) 394557ea5ddSQu Wenruo { 395557ea5ddSQu Wenruo /* No valid key type is 0, so all key should be larger than this key */ 396557ea5ddSQu Wenruo struct btrfs_key prev_key = {0, 0, 0}; 397557ea5ddSQu Wenruo struct btrfs_key key; 398557ea5ddSQu Wenruo u32 nritems = btrfs_header_nritems(leaf); 399557ea5ddSQu Wenruo int slot; 400557ea5ddSQu Wenruo 401557ea5ddSQu Wenruo /* 402557ea5ddSQu Wenruo * Extent buffers from a relocation tree have a owner field that 403557ea5ddSQu Wenruo * corresponds to the subvolume tree they are based on. So just from an 404557ea5ddSQu Wenruo * extent buffer alone we can not find out what is the id of the 405557ea5ddSQu Wenruo * corresponding subvolume tree, so we can not figure out if the extent 406557ea5ddSQu Wenruo * buffer corresponds to the root of the relocation tree or not. So 407557ea5ddSQu Wenruo * skip this check for relocation trees. 408557ea5ddSQu Wenruo */ 409557ea5ddSQu Wenruo if (nritems == 0 && !btrfs_header_flag(leaf, BTRFS_HEADER_FLAG_RELOC)) { 410557ea5ddSQu Wenruo struct btrfs_root *check_root; 411557ea5ddSQu Wenruo 412557ea5ddSQu Wenruo key.objectid = btrfs_header_owner(leaf); 413557ea5ddSQu Wenruo key.type = BTRFS_ROOT_ITEM_KEY; 414557ea5ddSQu Wenruo key.offset = (u64)-1; 415557ea5ddSQu Wenruo 416557ea5ddSQu Wenruo check_root = btrfs_get_fs_root(fs_info, &key, false); 417557ea5ddSQu Wenruo /* 418557ea5ddSQu Wenruo * The only reason we also check NULL here is that during 419557ea5ddSQu Wenruo * open_ctree() some roots has not yet been set up. 420557ea5ddSQu Wenruo */ 421557ea5ddSQu Wenruo if (!IS_ERR_OR_NULL(check_root)) { 422557ea5ddSQu Wenruo struct extent_buffer *eb; 423557ea5ddSQu Wenruo 424557ea5ddSQu Wenruo eb = btrfs_root_node(check_root); 425557ea5ddSQu Wenruo /* if leaf is the root, then it's fine */ 426557ea5ddSQu Wenruo if (leaf != eb) { 4272f659546SQu Wenruo generic_err(fs_info, leaf, 0, 428478d01b3SQu Wenruo "invalid nritems, have %u should not be 0 for non-root leaf", 429478d01b3SQu Wenruo nritems); 430557ea5ddSQu Wenruo free_extent_buffer(eb); 431557ea5ddSQu Wenruo return -EUCLEAN; 432557ea5ddSQu Wenruo } 433557ea5ddSQu Wenruo free_extent_buffer(eb); 434557ea5ddSQu Wenruo } 435557ea5ddSQu Wenruo return 0; 436557ea5ddSQu Wenruo } 437557ea5ddSQu Wenruo 438557ea5ddSQu Wenruo if (nritems == 0) 439557ea5ddSQu Wenruo return 0; 440557ea5ddSQu Wenruo 441557ea5ddSQu Wenruo /* 442557ea5ddSQu Wenruo * Check the following things to make sure this is a good leaf, and 443557ea5ddSQu Wenruo * leaf users won't need to bother with similar sanity checks: 444557ea5ddSQu Wenruo * 445557ea5ddSQu Wenruo * 1) key ordering 446557ea5ddSQu Wenruo * 2) item offset and size 447557ea5ddSQu Wenruo * No overlap, no hole, all inside the leaf. 448557ea5ddSQu Wenruo * 3) item content 449557ea5ddSQu Wenruo * If possible, do comprehensive sanity check. 450557ea5ddSQu Wenruo * NOTE: All checks must only rely on the item data itself. 451557ea5ddSQu Wenruo */ 452557ea5ddSQu Wenruo for (slot = 0; slot < nritems; slot++) { 453557ea5ddSQu Wenruo u32 item_end_expected; 454557ea5ddSQu Wenruo int ret; 455557ea5ddSQu Wenruo 456557ea5ddSQu Wenruo btrfs_item_key_to_cpu(leaf, &key, slot); 457557ea5ddSQu Wenruo 458557ea5ddSQu Wenruo /* Make sure the keys are in the right order */ 459557ea5ddSQu Wenruo if (btrfs_comp_cpu_keys(&prev_key, &key) >= 0) { 4602f659546SQu Wenruo generic_err(fs_info, leaf, slot, 461478d01b3SQu Wenruo "bad key order, prev (%llu %u %llu) current (%llu %u %llu)", 462478d01b3SQu Wenruo prev_key.objectid, prev_key.type, 463478d01b3SQu Wenruo prev_key.offset, key.objectid, key.type, 464478d01b3SQu Wenruo key.offset); 465557ea5ddSQu Wenruo return -EUCLEAN; 466557ea5ddSQu Wenruo } 467557ea5ddSQu Wenruo 468557ea5ddSQu Wenruo /* 469557ea5ddSQu Wenruo * Make sure the offset and ends are right, remember that the 470557ea5ddSQu Wenruo * item data starts at the end of the leaf and grows towards the 471557ea5ddSQu Wenruo * front. 472557ea5ddSQu Wenruo */ 473557ea5ddSQu Wenruo if (slot == 0) 474557ea5ddSQu Wenruo item_end_expected = BTRFS_LEAF_DATA_SIZE(fs_info); 475557ea5ddSQu Wenruo else 476557ea5ddSQu Wenruo item_end_expected = btrfs_item_offset_nr(leaf, 477557ea5ddSQu Wenruo slot - 1); 478557ea5ddSQu Wenruo if (btrfs_item_end_nr(leaf, slot) != item_end_expected) { 4792f659546SQu Wenruo generic_err(fs_info, leaf, slot, 480478d01b3SQu Wenruo "unexpected item end, have %u expect %u", 481478d01b3SQu Wenruo btrfs_item_end_nr(leaf, slot), 482478d01b3SQu Wenruo item_end_expected); 483557ea5ddSQu Wenruo return -EUCLEAN; 484557ea5ddSQu Wenruo } 485557ea5ddSQu Wenruo 486557ea5ddSQu Wenruo /* 487557ea5ddSQu Wenruo * Check to make sure that we don't point outside of the leaf, 488557ea5ddSQu Wenruo * just in case all the items are consistent to each other, but 489557ea5ddSQu Wenruo * all point outside of the leaf. 490557ea5ddSQu Wenruo */ 491557ea5ddSQu Wenruo if (btrfs_item_end_nr(leaf, slot) > 492557ea5ddSQu Wenruo BTRFS_LEAF_DATA_SIZE(fs_info)) { 4932f659546SQu Wenruo generic_err(fs_info, leaf, slot, 494478d01b3SQu Wenruo "slot end outside of leaf, have %u expect range [0, %u]", 495478d01b3SQu Wenruo btrfs_item_end_nr(leaf, slot), 496478d01b3SQu Wenruo BTRFS_LEAF_DATA_SIZE(fs_info)); 497557ea5ddSQu Wenruo return -EUCLEAN; 498557ea5ddSQu Wenruo } 499557ea5ddSQu Wenruo 500557ea5ddSQu Wenruo /* Also check if the item pointer overlaps with btrfs item. */ 501557ea5ddSQu Wenruo if (btrfs_item_nr_offset(slot) + sizeof(struct btrfs_item) > 502557ea5ddSQu Wenruo btrfs_item_ptr_offset(leaf, slot)) { 5032f659546SQu Wenruo generic_err(fs_info, leaf, slot, 504478d01b3SQu Wenruo "slot overlaps with its data, item end %lu data start %lu", 505478d01b3SQu Wenruo btrfs_item_nr_offset(slot) + 506478d01b3SQu Wenruo sizeof(struct btrfs_item), 507478d01b3SQu Wenruo btrfs_item_ptr_offset(leaf, slot)); 508557ea5ddSQu Wenruo return -EUCLEAN; 509557ea5ddSQu Wenruo } 510557ea5ddSQu Wenruo 51169fc6cbbSQu Wenruo if (check_item_data) { 51269fc6cbbSQu Wenruo /* 51369fc6cbbSQu Wenruo * Check if the item size and content meet other 51469fc6cbbSQu Wenruo * criteria 51569fc6cbbSQu Wenruo */ 5162f659546SQu Wenruo ret = check_leaf_item(fs_info, leaf, &key, slot); 517557ea5ddSQu Wenruo if (ret < 0) 518557ea5ddSQu Wenruo return ret; 51969fc6cbbSQu Wenruo } 520557ea5ddSQu Wenruo 521557ea5ddSQu Wenruo prev_key.objectid = key.objectid; 522557ea5ddSQu Wenruo prev_key.type = key.type; 523557ea5ddSQu Wenruo prev_key.offset = key.offset; 524557ea5ddSQu Wenruo } 525557ea5ddSQu Wenruo 526557ea5ddSQu Wenruo return 0; 527557ea5ddSQu Wenruo } 528557ea5ddSQu Wenruo 5292f659546SQu Wenruo int btrfs_check_leaf_full(struct btrfs_fs_info *fs_info, 53069fc6cbbSQu Wenruo struct extent_buffer *leaf) 53169fc6cbbSQu Wenruo { 5322f659546SQu Wenruo return check_leaf(fs_info, leaf, true); 53369fc6cbbSQu Wenruo } 53469fc6cbbSQu Wenruo 5352f659546SQu Wenruo int btrfs_check_leaf_relaxed(struct btrfs_fs_info *fs_info, 5362f659546SQu Wenruo struct extent_buffer *leaf) 5372f659546SQu Wenruo { 5382f659546SQu Wenruo return check_leaf(fs_info, leaf, false); 5392f659546SQu Wenruo } 5402f659546SQu Wenruo 5412f659546SQu Wenruo int btrfs_check_node(struct btrfs_fs_info *fs_info, struct extent_buffer *node) 542557ea5ddSQu Wenruo { 543557ea5ddSQu Wenruo unsigned long nr = btrfs_header_nritems(node); 544557ea5ddSQu Wenruo struct btrfs_key key, next_key; 545557ea5ddSQu Wenruo int slot; 546557ea5ddSQu Wenruo u64 bytenr; 547557ea5ddSQu Wenruo int ret = 0; 548557ea5ddSQu Wenruo 5492f659546SQu Wenruo if (nr == 0 || nr > BTRFS_NODEPTRS_PER_BLOCK(fs_info)) { 5502f659546SQu Wenruo btrfs_crit(fs_info, 551bba4f298SQu Wenruo "corrupt node: root=%llu block=%llu, nritems too %s, have %lu expect range [1,%u]", 5522f659546SQu Wenruo btrfs_header_owner(node), node->start, 553bba4f298SQu Wenruo nr == 0 ? "small" : "large", nr, 5542f659546SQu Wenruo BTRFS_NODEPTRS_PER_BLOCK(fs_info)); 555bba4f298SQu Wenruo return -EUCLEAN; 556557ea5ddSQu Wenruo } 557557ea5ddSQu Wenruo 558557ea5ddSQu Wenruo for (slot = 0; slot < nr - 1; slot++) { 559557ea5ddSQu Wenruo bytenr = btrfs_node_blockptr(node, slot); 560557ea5ddSQu Wenruo btrfs_node_key_to_cpu(node, &key, slot); 561557ea5ddSQu Wenruo btrfs_node_key_to_cpu(node, &next_key, slot + 1); 562557ea5ddSQu Wenruo 563557ea5ddSQu Wenruo if (!bytenr) { 5642f659546SQu Wenruo generic_err(fs_info, node, slot, 565bba4f298SQu Wenruo "invalid NULL node pointer"); 566bba4f298SQu Wenruo ret = -EUCLEAN; 567bba4f298SQu Wenruo goto out; 568bba4f298SQu Wenruo } 5692f659546SQu Wenruo if (!IS_ALIGNED(bytenr, fs_info->sectorsize)) { 5702f659546SQu Wenruo generic_err(fs_info, node, slot, 571bba4f298SQu Wenruo "unaligned pointer, have %llu should be aligned to %u", 5722f659546SQu Wenruo bytenr, fs_info->sectorsize); 573bba4f298SQu Wenruo ret = -EUCLEAN; 574557ea5ddSQu Wenruo goto out; 575557ea5ddSQu Wenruo } 576557ea5ddSQu Wenruo 577557ea5ddSQu Wenruo if (btrfs_comp_cpu_keys(&key, &next_key) >= 0) { 5782f659546SQu Wenruo generic_err(fs_info, node, slot, 579bba4f298SQu Wenruo "bad key order, current (%llu %u %llu) next (%llu %u %llu)", 580bba4f298SQu Wenruo key.objectid, key.type, key.offset, 581bba4f298SQu Wenruo next_key.objectid, next_key.type, 582bba4f298SQu Wenruo next_key.offset); 583bba4f298SQu Wenruo ret = -EUCLEAN; 584557ea5ddSQu Wenruo goto out; 585557ea5ddSQu Wenruo } 586557ea5ddSQu Wenruo } 587557ea5ddSQu Wenruo out: 588557ea5ddSQu Wenruo return ret; 589557ea5ddSQu Wenruo } 590