Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) Qu Wenruo 2017. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public |
| 6 | * License v2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public |
| 14 | * License along with this program. |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * The module is used to catch unexpected/corrupted tree block data. |
| 19 | * Such behavior can be caused either by a fuzzed image or bugs. |
| 20 | * |
| 21 | * The objective is to do leaf/node validation checks when tree block is read |
| 22 | * from disk, and check *every* possible member, so other code won't |
| 23 | * need to checking them again. |
| 24 | * |
| 25 | * Due to the potential and unwanted damage, every checker needs to be |
| 26 | * carefully reviewed otherwise so it does not prevent mount of valid images. |
| 27 | */ |
| 28 | |
| 29 | #include "ctree.h" |
| 30 | #include "tree-checker.h" |
| 31 | #include "disk-io.h" |
| 32 | #include "compression.h" |
| 33 | |
Qu Wenruo | bba4f29 | 2017-10-09 01:51:03 +0000 | [diff] [blame] | 34 | /* |
| 35 | * Error message should follow the following format: |
| 36 | * corrupt <type>: <identifier>, <reason>[, <bad_value>] |
| 37 | * |
| 38 | * @type: leaf or node |
| 39 | * @identifier: the necessary info to locate the leaf/node. |
| 40 | * It's recommened to decode key.objecitd/offset if it's |
| 41 | * meaningful. |
| 42 | * @reason: describe the error |
| 43 | * @bad_value: optional, it's recommened to output bad value and its |
| 44 | * expected value (range). |
| 45 | * |
| 46 | * Since comma is used to separate the components, only space is allowed |
| 47 | * inside each component. |
| 48 | */ |
| 49 | |
| 50 | /* |
| 51 | * Append generic "corrupt leaf/node root=%llu block=%llu slot=%d: " to @fmt. |
| 52 | * Allows callers to customize the output. |
| 53 | */ |
| 54 | __printf(4, 5) |
David Sterba | e67c718 | 2018-02-19 17:24:18 +0100 | [diff] [blame^] | 55 | __cold |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 56 | static void generic_err(const struct btrfs_fs_info *fs_info, |
Qu Wenruo | bba4f29 | 2017-10-09 01:51:03 +0000 | [diff] [blame] | 57 | const struct extent_buffer *eb, int slot, |
| 58 | const char *fmt, ...) |
| 59 | { |
| 60 | struct va_format vaf; |
| 61 | va_list args; |
| 62 | |
| 63 | va_start(args, fmt); |
| 64 | |
| 65 | vaf.fmt = fmt; |
| 66 | vaf.va = &args; |
| 67 | |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 68 | btrfs_crit(fs_info, |
Qu Wenruo | bba4f29 | 2017-10-09 01:51:03 +0000 | [diff] [blame] | 69 | "corrupt %s: root=%llu block=%llu slot=%d, %pV", |
| 70 | btrfs_header_level(eb) == 0 ? "leaf" : "node", |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 71 | btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot, &vaf); |
Qu Wenruo | bba4f29 | 2017-10-09 01:51:03 +0000 | [diff] [blame] | 72 | va_end(args); |
| 73 | } |
| 74 | |
Qu Wenruo | 8806d71 | 2017-10-09 01:51:06 +0000 | [diff] [blame] | 75 | /* |
| 76 | * Customized reporter for extent data item, since its key objectid and |
| 77 | * offset has its own meaning. |
| 78 | */ |
| 79 | __printf(4, 5) |
David Sterba | e67c718 | 2018-02-19 17:24:18 +0100 | [diff] [blame^] | 80 | __cold |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 81 | static void file_extent_err(const struct btrfs_fs_info *fs_info, |
Qu Wenruo | 8806d71 | 2017-10-09 01:51:06 +0000 | [diff] [blame] | 82 | const struct extent_buffer *eb, int slot, |
| 83 | const char *fmt, ...) |
| 84 | { |
| 85 | struct btrfs_key key; |
| 86 | struct va_format vaf; |
| 87 | va_list args; |
| 88 | |
| 89 | btrfs_item_key_to_cpu(eb, &key, slot); |
| 90 | va_start(args, fmt); |
| 91 | |
| 92 | vaf.fmt = fmt; |
| 93 | vaf.va = &args; |
| 94 | |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 95 | btrfs_crit(fs_info, |
Qu Wenruo | 8806d71 | 2017-10-09 01:51:06 +0000 | [diff] [blame] | 96 | "corrupt %s: root=%llu block=%llu slot=%d ino=%llu file_offset=%llu, %pV", |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 97 | btrfs_header_level(eb) == 0 ? "leaf" : "node", |
| 98 | btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot, |
| 99 | key.objectid, key.offset, &vaf); |
Qu Wenruo | 8806d71 | 2017-10-09 01:51:06 +0000 | [diff] [blame] | 100 | va_end(args); |
| 101 | } |
| 102 | |
| 103 | /* |
| 104 | * Return 0 if the btrfs_file_extent_##name is aligned to @alignment |
| 105 | * Else return 1 |
| 106 | */ |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 107 | #define CHECK_FE_ALIGNED(fs_info, leaf, slot, fi, name, alignment) \ |
Qu Wenruo | 8806d71 | 2017-10-09 01:51:06 +0000 | [diff] [blame] | 108 | ({ \ |
| 109 | if (!IS_ALIGNED(btrfs_file_extent_##name((leaf), (fi)), (alignment))) \ |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 110 | file_extent_err((fs_info), (leaf), (slot), \ |
Qu Wenruo | 8806d71 | 2017-10-09 01:51:06 +0000 | [diff] [blame] | 111 | "invalid %s for file extent, have %llu, should be aligned to %u", \ |
| 112 | (#name), btrfs_file_extent_##name((leaf), (fi)), \ |
| 113 | (alignment)); \ |
| 114 | (!IS_ALIGNED(btrfs_file_extent_##name((leaf), (fi)), (alignment))); \ |
| 115 | }) |
| 116 | |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 117 | static int check_extent_data_item(struct btrfs_fs_info *fs_info, |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 118 | struct extent_buffer *leaf, |
| 119 | struct btrfs_key *key, int slot) |
| 120 | { |
| 121 | struct btrfs_file_extent_item *fi; |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 122 | u32 sectorsize = fs_info->sectorsize; |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 123 | u32 item_size = btrfs_item_size_nr(leaf, slot); |
| 124 | |
| 125 | if (!IS_ALIGNED(key->offset, sectorsize)) { |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 126 | file_extent_err(fs_info, leaf, slot, |
Qu Wenruo | 8806d71 | 2017-10-09 01:51:06 +0000 | [diff] [blame] | 127 | "unaligned file_offset for file extent, have %llu should be aligned to %u", |
| 128 | key->offset, sectorsize); |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 129 | return -EUCLEAN; |
| 130 | } |
| 131 | |
| 132 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
| 133 | |
| 134 | if (btrfs_file_extent_type(leaf, fi) > BTRFS_FILE_EXTENT_TYPES) { |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 135 | file_extent_err(fs_info, leaf, slot, |
Qu Wenruo | 8806d71 | 2017-10-09 01:51:06 +0000 | [diff] [blame] | 136 | "invalid type for file extent, have %u expect range [0, %u]", |
| 137 | btrfs_file_extent_type(leaf, fi), |
| 138 | BTRFS_FILE_EXTENT_TYPES); |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 139 | return -EUCLEAN; |
| 140 | } |
| 141 | |
| 142 | /* |
| 143 | * Support for new compression/encrption must introduce incompat flag, |
| 144 | * and must be caught in open_ctree(). |
| 145 | */ |
| 146 | if (btrfs_file_extent_compression(leaf, fi) > BTRFS_COMPRESS_TYPES) { |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 147 | file_extent_err(fs_info, leaf, slot, |
Qu Wenruo | 8806d71 | 2017-10-09 01:51:06 +0000 | [diff] [blame] | 148 | "invalid compression for file extent, have %u expect range [0, %u]", |
| 149 | btrfs_file_extent_compression(leaf, fi), |
| 150 | BTRFS_COMPRESS_TYPES); |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 151 | return -EUCLEAN; |
| 152 | } |
| 153 | if (btrfs_file_extent_encryption(leaf, fi)) { |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 154 | file_extent_err(fs_info, leaf, slot, |
Qu Wenruo | 8806d71 | 2017-10-09 01:51:06 +0000 | [diff] [blame] | 155 | "invalid encryption for file extent, have %u expect 0", |
| 156 | btrfs_file_extent_encryption(leaf, fi)); |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 157 | return -EUCLEAN; |
| 158 | } |
| 159 | if (btrfs_file_extent_type(leaf, fi) == BTRFS_FILE_EXTENT_INLINE) { |
| 160 | /* Inline extent must have 0 as key offset */ |
| 161 | if (key->offset) { |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 162 | file_extent_err(fs_info, leaf, slot, |
Qu Wenruo | 8806d71 | 2017-10-09 01:51:06 +0000 | [diff] [blame] | 163 | "invalid file_offset for inline file extent, have %llu expect 0", |
| 164 | key->offset); |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 165 | return -EUCLEAN; |
| 166 | } |
| 167 | |
| 168 | /* Compressed inline extent has no on-disk size, skip it */ |
| 169 | if (btrfs_file_extent_compression(leaf, fi) != |
| 170 | BTRFS_COMPRESS_NONE) |
| 171 | return 0; |
| 172 | |
| 173 | /* Uncompressed inline extent size must match item size */ |
| 174 | if (item_size != BTRFS_FILE_EXTENT_INLINE_DATA_START + |
| 175 | btrfs_file_extent_ram_bytes(leaf, fi)) { |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 176 | file_extent_err(fs_info, leaf, slot, |
Qu Wenruo | 8806d71 | 2017-10-09 01:51:06 +0000 | [diff] [blame] | 177 | "invalid ram_bytes for uncompressed inline extent, have %u expect %llu", |
| 178 | item_size, BTRFS_FILE_EXTENT_INLINE_DATA_START + |
| 179 | btrfs_file_extent_ram_bytes(leaf, fi)); |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 180 | return -EUCLEAN; |
| 181 | } |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | /* Regular or preallocated extent has fixed item size */ |
| 186 | if (item_size != sizeof(*fi)) { |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 187 | file_extent_err(fs_info, leaf, slot, |
Arnd Bergmann | 709a95c | 2017-10-13 11:27:35 +0200 | [diff] [blame] | 188 | "invalid item size for reg/prealloc file extent, have %u expect %zu", |
Qu Wenruo | 8806d71 | 2017-10-09 01:51:06 +0000 | [diff] [blame] | 189 | item_size, sizeof(*fi)); |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 190 | return -EUCLEAN; |
| 191 | } |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 192 | if (CHECK_FE_ALIGNED(fs_info, leaf, slot, fi, ram_bytes, sectorsize) || |
| 193 | CHECK_FE_ALIGNED(fs_info, leaf, slot, fi, disk_bytenr, sectorsize) || |
| 194 | CHECK_FE_ALIGNED(fs_info, leaf, slot, fi, disk_num_bytes, sectorsize) || |
| 195 | CHECK_FE_ALIGNED(fs_info, leaf, slot, fi, offset, sectorsize) || |
| 196 | CHECK_FE_ALIGNED(fs_info, leaf, slot, fi, num_bytes, sectorsize)) |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 197 | return -EUCLEAN; |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 198 | return 0; |
| 199 | } |
| 200 | |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 201 | static int check_csum_item(struct btrfs_fs_info *fs_info, |
| 202 | struct extent_buffer *leaf, struct btrfs_key *key, |
| 203 | int slot) |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 204 | { |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 205 | u32 sectorsize = fs_info->sectorsize; |
| 206 | u32 csumsize = btrfs_super_csum_size(fs_info->super_copy); |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 207 | |
| 208 | if (key->objectid != BTRFS_EXTENT_CSUM_OBJECTID) { |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 209 | generic_err(fs_info, leaf, slot, |
Qu Wenruo | d508c5f | 2017-10-09 01:51:05 +0000 | [diff] [blame] | 210 | "invalid key objectid for csum item, have %llu expect %llu", |
| 211 | key->objectid, BTRFS_EXTENT_CSUM_OBJECTID); |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 212 | return -EUCLEAN; |
| 213 | } |
| 214 | if (!IS_ALIGNED(key->offset, sectorsize)) { |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 215 | generic_err(fs_info, leaf, slot, |
Qu Wenruo | d508c5f | 2017-10-09 01:51:05 +0000 | [diff] [blame] | 216 | "unaligned key offset for csum item, have %llu should be aligned to %u", |
| 217 | key->offset, sectorsize); |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 218 | return -EUCLEAN; |
| 219 | } |
| 220 | if (!IS_ALIGNED(btrfs_item_size_nr(leaf, slot), csumsize)) { |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 221 | generic_err(fs_info, leaf, slot, |
Qu Wenruo | d508c5f | 2017-10-09 01:51:05 +0000 | [diff] [blame] | 222 | "unaligned item size for csum item, have %u should be aligned to %u", |
| 223 | btrfs_item_size_nr(leaf, slot), csumsize); |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 224 | return -EUCLEAN; |
| 225 | } |
| 226 | return 0; |
| 227 | } |
| 228 | |
| 229 | /* |
Qu Wenruo | ad7b0368 | 2017-11-08 08:54:25 +0800 | [diff] [blame] | 230 | * Customized reported for dir_item, only important new info is key->objectid, |
| 231 | * which represents inode number |
| 232 | */ |
| 233 | __printf(4, 5) |
David Sterba | e67c718 | 2018-02-19 17:24:18 +0100 | [diff] [blame^] | 234 | __cold |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 235 | static void dir_item_err(const struct btrfs_fs_info *fs_info, |
Qu Wenruo | ad7b0368 | 2017-11-08 08:54:25 +0800 | [diff] [blame] | 236 | const struct extent_buffer *eb, int slot, |
| 237 | const char *fmt, ...) |
| 238 | { |
| 239 | struct btrfs_key key; |
| 240 | struct va_format vaf; |
| 241 | va_list args; |
| 242 | |
| 243 | btrfs_item_key_to_cpu(eb, &key, slot); |
| 244 | va_start(args, fmt); |
| 245 | |
| 246 | vaf.fmt = fmt; |
| 247 | vaf.va = &args; |
| 248 | |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 249 | btrfs_crit(fs_info, |
Qu Wenruo | ad7b0368 | 2017-11-08 08:54:25 +0800 | [diff] [blame] | 250 | "corrupt %s: root=%llu block=%llu slot=%d ino=%llu, %pV", |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 251 | btrfs_header_level(eb) == 0 ? "leaf" : "node", |
| 252 | btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot, |
| 253 | key.objectid, &vaf); |
Qu Wenruo | ad7b0368 | 2017-11-08 08:54:25 +0800 | [diff] [blame] | 254 | va_end(args); |
| 255 | } |
| 256 | |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 257 | static int check_dir_item(struct btrfs_fs_info *fs_info, |
Qu Wenruo | ad7b0368 | 2017-11-08 08:54:25 +0800 | [diff] [blame] | 258 | struct extent_buffer *leaf, |
| 259 | struct btrfs_key *key, int slot) |
| 260 | { |
| 261 | struct btrfs_dir_item *di; |
| 262 | u32 item_size = btrfs_item_size_nr(leaf, slot); |
| 263 | u32 cur = 0; |
| 264 | |
| 265 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
| 266 | while (cur < item_size) { |
Qu Wenruo | ad7b0368 | 2017-11-08 08:54:25 +0800 | [diff] [blame] | 267 | u32 name_len; |
| 268 | u32 data_len; |
| 269 | u32 max_name_len; |
| 270 | u32 total_size; |
| 271 | u32 name_hash; |
| 272 | u8 dir_type; |
| 273 | |
| 274 | /* header itself should not cross item boundary */ |
| 275 | if (cur + sizeof(*di) > item_size) { |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 276 | dir_item_err(fs_info, leaf, slot, |
Arnd Bergmann | 7cfad65 | 2017-12-06 15:18:14 +0100 | [diff] [blame] | 277 | "dir item header crosses item boundary, have %zu boundary %u", |
Qu Wenruo | ad7b0368 | 2017-11-08 08:54:25 +0800 | [diff] [blame] | 278 | cur + sizeof(*di), item_size); |
| 279 | return -EUCLEAN; |
| 280 | } |
| 281 | |
| 282 | /* dir type check */ |
| 283 | dir_type = btrfs_dir_type(leaf, di); |
| 284 | if (dir_type >= BTRFS_FT_MAX) { |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 285 | dir_item_err(fs_info, leaf, slot, |
Qu Wenruo | ad7b0368 | 2017-11-08 08:54:25 +0800 | [diff] [blame] | 286 | "invalid dir item type, have %u expect [0, %u)", |
| 287 | dir_type, BTRFS_FT_MAX); |
| 288 | return -EUCLEAN; |
| 289 | } |
| 290 | |
| 291 | if (key->type == BTRFS_XATTR_ITEM_KEY && |
| 292 | dir_type != BTRFS_FT_XATTR) { |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 293 | dir_item_err(fs_info, leaf, slot, |
Qu Wenruo | ad7b0368 | 2017-11-08 08:54:25 +0800 | [diff] [blame] | 294 | "invalid dir item type for XATTR key, have %u expect %u", |
| 295 | dir_type, BTRFS_FT_XATTR); |
| 296 | return -EUCLEAN; |
| 297 | } |
| 298 | if (dir_type == BTRFS_FT_XATTR && |
| 299 | key->type != BTRFS_XATTR_ITEM_KEY) { |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 300 | dir_item_err(fs_info, leaf, slot, |
Qu Wenruo | ad7b0368 | 2017-11-08 08:54:25 +0800 | [diff] [blame] | 301 | "xattr dir type found for non-XATTR key"); |
| 302 | return -EUCLEAN; |
| 303 | } |
| 304 | if (dir_type == BTRFS_FT_XATTR) |
| 305 | max_name_len = XATTR_NAME_MAX; |
| 306 | else |
| 307 | max_name_len = BTRFS_NAME_LEN; |
| 308 | |
| 309 | /* Name/data length check */ |
| 310 | name_len = btrfs_dir_name_len(leaf, di); |
| 311 | data_len = btrfs_dir_data_len(leaf, di); |
| 312 | if (name_len > max_name_len) { |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 313 | dir_item_err(fs_info, leaf, slot, |
Qu Wenruo | ad7b0368 | 2017-11-08 08:54:25 +0800 | [diff] [blame] | 314 | "dir item name len too long, have %u max %u", |
| 315 | name_len, max_name_len); |
| 316 | return -EUCLEAN; |
| 317 | } |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 318 | if (name_len + data_len > BTRFS_MAX_XATTR_SIZE(fs_info)) { |
| 319 | dir_item_err(fs_info, leaf, slot, |
Qu Wenruo | ad7b0368 | 2017-11-08 08:54:25 +0800 | [diff] [blame] | 320 | "dir item name and data len too long, have %u max %u", |
| 321 | name_len + data_len, |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 322 | BTRFS_MAX_XATTR_SIZE(fs_info)); |
Qu Wenruo | ad7b0368 | 2017-11-08 08:54:25 +0800 | [diff] [blame] | 323 | return -EUCLEAN; |
| 324 | } |
| 325 | |
| 326 | if (data_len && dir_type != BTRFS_FT_XATTR) { |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 327 | dir_item_err(fs_info, leaf, slot, |
Qu Wenruo | ad7b0368 | 2017-11-08 08:54:25 +0800 | [diff] [blame] | 328 | "dir item with invalid data len, have %u expect 0", |
| 329 | data_len); |
| 330 | return -EUCLEAN; |
| 331 | } |
| 332 | |
| 333 | total_size = sizeof(*di) + name_len + data_len; |
| 334 | |
| 335 | /* header and name/data should not cross item boundary */ |
| 336 | if (cur + total_size > item_size) { |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 337 | dir_item_err(fs_info, leaf, slot, |
Qu Wenruo | ad7b0368 | 2017-11-08 08:54:25 +0800 | [diff] [blame] | 338 | "dir item data crosses item boundary, have %u boundary %u", |
| 339 | cur + total_size, item_size); |
| 340 | return -EUCLEAN; |
| 341 | } |
| 342 | |
| 343 | /* |
| 344 | * Special check for XATTR/DIR_ITEM, as key->offset is name |
| 345 | * hash, should match its name |
| 346 | */ |
| 347 | if (key->type == BTRFS_DIR_ITEM_KEY || |
| 348 | key->type == BTRFS_XATTR_ITEM_KEY) { |
David Sterba | e2683fc | 2018-01-10 15:13:07 +0100 | [diff] [blame] | 349 | char namebuf[max(BTRFS_NAME_LEN, XATTR_NAME_MAX)]; |
| 350 | |
Qu Wenruo | ad7b0368 | 2017-11-08 08:54:25 +0800 | [diff] [blame] | 351 | read_extent_buffer(leaf, namebuf, |
| 352 | (unsigned long)(di + 1), name_len); |
| 353 | name_hash = btrfs_name_hash(namebuf, name_len); |
| 354 | if (key->offset != name_hash) { |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 355 | dir_item_err(fs_info, leaf, slot, |
Qu Wenruo | ad7b0368 | 2017-11-08 08:54:25 +0800 | [diff] [blame] | 356 | "name hash mismatch with key, have 0x%016x expect 0x%016llx", |
| 357 | name_hash, key->offset); |
| 358 | return -EUCLEAN; |
| 359 | } |
| 360 | } |
| 361 | cur += total_size; |
| 362 | di = (struct btrfs_dir_item *)((void *)di + total_size); |
| 363 | } |
| 364 | return 0; |
| 365 | } |
| 366 | |
| 367 | /* |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 368 | * Common point to switch the item-specific validation. |
| 369 | */ |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 370 | static int check_leaf_item(struct btrfs_fs_info *fs_info, |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 371 | struct extent_buffer *leaf, |
| 372 | struct btrfs_key *key, int slot) |
| 373 | { |
| 374 | int ret = 0; |
| 375 | |
| 376 | switch (key->type) { |
| 377 | case BTRFS_EXTENT_DATA_KEY: |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 378 | ret = check_extent_data_item(fs_info, leaf, key, slot); |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 379 | break; |
| 380 | case BTRFS_EXTENT_CSUM_KEY: |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 381 | ret = check_csum_item(fs_info, leaf, key, slot); |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 382 | break; |
Qu Wenruo | ad7b0368 | 2017-11-08 08:54:25 +0800 | [diff] [blame] | 383 | case BTRFS_DIR_ITEM_KEY: |
| 384 | case BTRFS_DIR_INDEX_KEY: |
| 385 | case BTRFS_XATTR_ITEM_KEY: |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 386 | ret = check_dir_item(fs_info, leaf, key, slot); |
Qu Wenruo | ad7b0368 | 2017-11-08 08:54:25 +0800 | [diff] [blame] | 387 | break; |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 388 | } |
| 389 | return ret; |
| 390 | } |
| 391 | |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 392 | static int check_leaf(struct btrfs_fs_info *fs_info, struct extent_buffer *leaf, |
Qu Wenruo | 69fc6cb | 2017-11-08 08:54:24 +0800 | [diff] [blame] | 393 | bool check_item_data) |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 394 | { |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 395 | /* No valid key type is 0, so all key should be larger than this key */ |
| 396 | struct btrfs_key prev_key = {0, 0, 0}; |
| 397 | struct btrfs_key key; |
| 398 | u32 nritems = btrfs_header_nritems(leaf); |
| 399 | int slot; |
| 400 | |
| 401 | /* |
| 402 | * Extent buffers from a relocation tree have a owner field that |
| 403 | * corresponds to the subvolume tree they are based on. So just from an |
| 404 | * extent buffer alone we can not find out what is the id of the |
| 405 | * corresponding subvolume tree, so we can not figure out if the extent |
| 406 | * buffer corresponds to the root of the relocation tree or not. So |
| 407 | * skip this check for relocation trees. |
| 408 | */ |
| 409 | if (nritems == 0 && !btrfs_header_flag(leaf, BTRFS_HEADER_FLAG_RELOC)) { |
| 410 | struct btrfs_root *check_root; |
| 411 | |
| 412 | key.objectid = btrfs_header_owner(leaf); |
| 413 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 414 | key.offset = (u64)-1; |
| 415 | |
| 416 | check_root = btrfs_get_fs_root(fs_info, &key, false); |
| 417 | /* |
| 418 | * The only reason we also check NULL here is that during |
| 419 | * open_ctree() some roots has not yet been set up. |
| 420 | */ |
| 421 | if (!IS_ERR_OR_NULL(check_root)) { |
| 422 | struct extent_buffer *eb; |
| 423 | |
| 424 | eb = btrfs_root_node(check_root); |
| 425 | /* if leaf is the root, then it's fine */ |
| 426 | if (leaf != eb) { |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 427 | generic_err(fs_info, leaf, 0, |
Qu Wenruo | 478d01b | 2017-10-09 01:51:04 +0000 | [diff] [blame] | 428 | "invalid nritems, have %u should not be 0 for non-root leaf", |
| 429 | nritems); |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 430 | free_extent_buffer(eb); |
| 431 | return -EUCLEAN; |
| 432 | } |
| 433 | free_extent_buffer(eb); |
| 434 | } |
| 435 | return 0; |
| 436 | } |
| 437 | |
| 438 | if (nritems == 0) |
| 439 | return 0; |
| 440 | |
| 441 | /* |
| 442 | * Check the following things to make sure this is a good leaf, and |
| 443 | * leaf users won't need to bother with similar sanity checks: |
| 444 | * |
| 445 | * 1) key ordering |
| 446 | * 2) item offset and size |
| 447 | * No overlap, no hole, all inside the leaf. |
| 448 | * 3) item content |
| 449 | * If possible, do comprehensive sanity check. |
| 450 | * NOTE: All checks must only rely on the item data itself. |
| 451 | */ |
| 452 | for (slot = 0; slot < nritems; slot++) { |
| 453 | u32 item_end_expected; |
| 454 | int ret; |
| 455 | |
| 456 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 457 | |
| 458 | /* Make sure the keys are in the right order */ |
| 459 | if (btrfs_comp_cpu_keys(&prev_key, &key) >= 0) { |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 460 | generic_err(fs_info, leaf, slot, |
Qu Wenruo | 478d01b | 2017-10-09 01:51:04 +0000 | [diff] [blame] | 461 | "bad key order, prev (%llu %u %llu) current (%llu %u %llu)", |
| 462 | prev_key.objectid, prev_key.type, |
| 463 | prev_key.offset, key.objectid, key.type, |
| 464 | key.offset); |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 465 | return -EUCLEAN; |
| 466 | } |
| 467 | |
| 468 | /* |
| 469 | * Make sure the offset and ends are right, remember that the |
| 470 | * item data starts at the end of the leaf and grows towards the |
| 471 | * front. |
| 472 | */ |
| 473 | if (slot == 0) |
| 474 | item_end_expected = BTRFS_LEAF_DATA_SIZE(fs_info); |
| 475 | else |
| 476 | item_end_expected = btrfs_item_offset_nr(leaf, |
| 477 | slot - 1); |
| 478 | if (btrfs_item_end_nr(leaf, slot) != item_end_expected) { |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 479 | generic_err(fs_info, leaf, slot, |
Qu Wenruo | 478d01b | 2017-10-09 01:51:04 +0000 | [diff] [blame] | 480 | "unexpected item end, have %u expect %u", |
| 481 | btrfs_item_end_nr(leaf, slot), |
| 482 | item_end_expected); |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 483 | return -EUCLEAN; |
| 484 | } |
| 485 | |
| 486 | /* |
| 487 | * Check to make sure that we don't point outside of the leaf, |
| 488 | * just in case all the items are consistent to each other, but |
| 489 | * all point outside of the leaf. |
| 490 | */ |
| 491 | if (btrfs_item_end_nr(leaf, slot) > |
| 492 | BTRFS_LEAF_DATA_SIZE(fs_info)) { |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 493 | generic_err(fs_info, leaf, slot, |
Qu Wenruo | 478d01b | 2017-10-09 01:51:04 +0000 | [diff] [blame] | 494 | "slot end outside of leaf, have %u expect range [0, %u]", |
| 495 | btrfs_item_end_nr(leaf, slot), |
| 496 | BTRFS_LEAF_DATA_SIZE(fs_info)); |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 497 | return -EUCLEAN; |
| 498 | } |
| 499 | |
| 500 | /* Also check if the item pointer overlaps with btrfs item. */ |
| 501 | if (btrfs_item_nr_offset(slot) + sizeof(struct btrfs_item) > |
| 502 | btrfs_item_ptr_offset(leaf, slot)) { |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 503 | generic_err(fs_info, leaf, slot, |
Qu Wenruo | 478d01b | 2017-10-09 01:51:04 +0000 | [diff] [blame] | 504 | "slot overlaps with its data, item end %lu data start %lu", |
| 505 | btrfs_item_nr_offset(slot) + |
| 506 | sizeof(struct btrfs_item), |
| 507 | btrfs_item_ptr_offset(leaf, slot)); |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 508 | return -EUCLEAN; |
| 509 | } |
| 510 | |
Qu Wenruo | 69fc6cb | 2017-11-08 08:54:24 +0800 | [diff] [blame] | 511 | if (check_item_data) { |
| 512 | /* |
| 513 | * Check if the item size and content meet other |
| 514 | * criteria |
| 515 | */ |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 516 | ret = check_leaf_item(fs_info, leaf, &key, slot); |
Qu Wenruo | 69fc6cb | 2017-11-08 08:54:24 +0800 | [diff] [blame] | 517 | if (ret < 0) |
| 518 | return ret; |
| 519 | } |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 520 | |
| 521 | prev_key.objectid = key.objectid; |
| 522 | prev_key.type = key.type; |
| 523 | prev_key.offset = key.offset; |
| 524 | } |
| 525 | |
| 526 | return 0; |
| 527 | } |
| 528 | |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 529 | int btrfs_check_leaf_full(struct btrfs_fs_info *fs_info, |
| 530 | struct extent_buffer *leaf) |
Qu Wenruo | 69fc6cb | 2017-11-08 08:54:24 +0800 | [diff] [blame] | 531 | { |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 532 | return check_leaf(fs_info, leaf, true); |
Qu Wenruo | 69fc6cb | 2017-11-08 08:54:24 +0800 | [diff] [blame] | 533 | } |
| 534 | |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 535 | int btrfs_check_leaf_relaxed(struct btrfs_fs_info *fs_info, |
Qu Wenruo | 69fc6cb | 2017-11-08 08:54:24 +0800 | [diff] [blame] | 536 | struct extent_buffer *leaf) |
| 537 | { |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 538 | return check_leaf(fs_info, leaf, false); |
Qu Wenruo | 69fc6cb | 2017-11-08 08:54:24 +0800 | [diff] [blame] | 539 | } |
| 540 | |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 541 | int btrfs_check_node(struct btrfs_fs_info *fs_info, struct extent_buffer *node) |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 542 | { |
| 543 | unsigned long nr = btrfs_header_nritems(node); |
| 544 | struct btrfs_key key, next_key; |
| 545 | int slot; |
| 546 | u64 bytenr; |
| 547 | int ret = 0; |
| 548 | |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 549 | if (nr == 0 || nr > BTRFS_NODEPTRS_PER_BLOCK(fs_info)) { |
| 550 | btrfs_crit(fs_info, |
Qu Wenruo | bba4f29 | 2017-10-09 01:51:03 +0000 | [diff] [blame] | 551 | "corrupt node: root=%llu block=%llu, nritems too %s, have %lu expect range [1,%u]", |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 552 | btrfs_header_owner(node), node->start, |
Qu Wenruo | bba4f29 | 2017-10-09 01:51:03 +0000 | [diff] [blame] | 553 | nr == 0 ? "small" : "large", nr, |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 554 | BTRFS_NODEPTRS_PER_BLOCK(fs_info)); |
Qu Wenruo | bba4f29 | 2017-10-09 01:51:03 +0000 | [diff] [blame] | 555 | return -EUCLEAN; |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | for (slot = 0; slot < nr - 1; slot++) { |
| 559 | bytenr = btrfs_node_blockptr(node, slot); |
| 560 | btrfs_node_key_to_cpu(node, &key, slot); |
| 561 | btrfs_node_key_to_cpu(node, &next_key, slot + 1); |
| 562 | |
| 563 | if (!bytenr) { |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 564 | generic_err(fs_info, node, slot, |
Qu Wenruo | bba4f29 | 2017-10-09 01:51:03 +0000 | [diff] [blame] | 565 | "invalid NULL node pointer"); |
| 566 | ret = -EUCLEAN; |
| 567 | goto out; |
| 568 | } |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 569 | if (!IS_ALIGNED(bytenr, fs_info->sectorsize)) { |
| 570 | generic_err(fs_info, node, slot, |
Qu Wenruo | bba4f29 | 2017-10-09 01:51:03 +0000 | [diff] [blame] | 571 | "unaligned pointer, have %llu should be aligned to %u", |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 572 | bytenr, fs_info->sectorsize); |
Qu Wenruo | bba4f29 | 2017-10-09 01:51:03 +0000 | [diff] [blame] | 573 | ret = -EUCLEAN; |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 574 | goto out; |
| 575 | } |
| 576 | |
| 577 | if (btrfs_comp_cpu_keys(&key, &next_key) >= 0) { |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 578 | generic_err(fs_info, node, slot, |
Qu Wenruo | bba4f29 | 2017-10-09 01:51:03 +0000 | [diff] [blame] | 579 | "bad key order, current (%llu %u %llu) next (%llu %u %llu)", |
| 580 | key.objectid, key.type, key.offset, |
| 581 | next_key.objectid, next_key.type, |
| 582 | next_key.offset); |
| 583 | ret = -EUCLEAN; |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 584 | goto out; |
| 585 | } |
| 586 | } |
| 587 | out: |
| 588 | return ret; |
| 589 | } |