David Sterba | c1d7c51 | 2018-04-03 19:23:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) Qu Wenruo 2017. All rights reserved. |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | /* |
| 7 | * The module is used to catch unexpected/corrupted tree block data. |
| 8 | * Such behavior can be caused either by a fuzzed image or bugs. |
| 9 | * |
| 10 | * The objective is to do leaf/node validation checks when tree block is read |
| 11 | * from disk, and check *every* possible member, so other code won't |
| 12 | * need to checking them again. |
| 13 | * |
| 14 | * Due to the potential and unwanted damage, every checker needs to be |
| 15 | * carefully reviewed otherwise so it does not prevent mount of valid images. |
| 16 | */ |
| 17 | |
| 18 | #include "ctree.h" |
| 19 | #include "tree-checker.h" |
| 20 | #include "disk-io.h" |
| 21 | #include "compression.h" |
Qu Wenruo | fce466e | 2018-07-03 17:10:05 +0800 | [diff] [blame] | 22 | #include "volumes.h" |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 23 | |
Qu Wenruo | bba4f29 | 2017-10-09 01:51:03 +0000 | [diff] [blame] | 24 | /* |
| 25 | * Error message should follow the following format: |
| 26 | * corrupt <type>: <identifier>, <reason>[, <bad_value>] |
| 27 | * |
| 28 | * @type: leaf or node |
| 29 | * @identifier: the necessary info to locate the leaf/node. |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 30 | * It's recommended to decode key.objecitd/offset if it's |
Qu Wenruo | bba4f29 | 2017-10-09 01:51:03 +0000 | [diff] [blame] | 31 | * meaningful. |
| 32 | * @reason: describe the error |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 33 | * @bad_value: optional, it's recommended to output bad value and its |
Qu Wenruo | bba4f29 | 2017-10-09 01:51:03 +0000 | [diff] [blame] | 34 | * expected value (range). |
| 35 | * |
| 36 | * Since comma is used to separate the components, only space is allowed |
| 37 | * inside each component. |
| 38 | */ |
| 39 | |
| 40 | /* |
| 41 | * Append generic "corrupt leaf/node root=%llu block=%llu slot=%d: " to @fmt. |
| 42 | * Allows callers to customize the output. |
| 43 | */ |
| 44 | __printf(4, 5) |
David Sterba | e67c718 | 2018-02-19 17:24:18 +0100 | [diff] [blame] | 45 | __cold |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 46 | static void generic_err(const struct btrfs_fs_info *fs_info, |
Qu Wenruo | bba4f29 | 2017-10-09 01:51:03 +0000 | [diff] [blame] | 47 | const struct extent_buffer *eb, int slot, |
| 48 | const char *fmt, ...) |
| 49 | { |
| 50 | struct va_format vaf; |
| 51 | va_list args; |
| 52 | |
| 53 | va_start(args, fmt); |
| 54 | |
| 55 | vaf.fmt = fmt; |
| 56 | vaf.va = &args; |
| 57 | |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 58 | btrfs_crit(fs_info, |
Qu Wenruo | bba4f29 | 2017-10-09 01:51:03 +0000 | [diff] [blame] | 59 | "corrupt %s: root=%llu block=%llu slot=%d, %pV", |
| 60 | btrfs_header_level(eb) == 0 ? "leaf" : "node", |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 61 | btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot, &vaf); |
Qu Wenruo | bba4f29 | 2017-10-09 01:51:03 +0000 | [diff] [blame] | 62 | va_end(args); |
| 63 | } |
| 64 | |
Qu Wenruo | 8806d71 | 2017-10-09 01:51:06 +0000 | [diff] [blame] | 65 | /* |
| 66 | * Customized reporter for extent data item, since its key objectid and |
| 67 | * offset has its own meaning. |
| 68 | */ |
| 69 | __printf(4, 5) |
David Sterba | e67c718 | 2018-02-19 17:24:18 +0100 | [diff] [blame] | 70 | __cold |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 71 | static void file_extent_err(const struct btrfs_fs_info *fs_info, |
Qu Wenruo | 8806d71 | 2017-10-09 01:51:06 +0000 | [diff] [blame] | 72 | const struct extent_buffer *eb, int slot, |
| 73 | const char *fmt, ...) |
| 74 | { |
| 75 | struct btrfs_key key; |
| 76 | struct va_format vaf; |
| 77 | va_list args; |
| 78 | |
| 79 | btrfs_item_key_to_cpu(eb, &key, slot); |
| 80 | va_start(args, fmt); |
| 81 | |
| 82 | vaf.fmt = fmt; |
| 83 | vaf.va = &args; |
| 84 | |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 85 | btrfs_crit(fs_info, |
Qu Wenruo | 8806d71 | 2017-10-09 01:51:06 +0000 | [diff] [blame] | 86 | "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] | 87 | btrfs_header_level(eb) == 0 ? "leaf" : "node", |
| 88 | btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot, |
| 89 | key.objectid, key.offset, &vaf); |
Qu Wenruo | 8806d71 | 2017-10-09 01:51:06 +0000 | [diff] [blame] | 90 | va_end(args); |
| 91 | } |
| 92 | |
| 93 | /* |
| 94 | * Return 0 if the btrfs_file_extent_##name is aligned to @alignment |
| 95 | * Else return 1 |
| 96 | */ |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 97 | #define CHECK_FE_ALIGNED(fs_info, leaf, slot, fi, name, alignment) \ |
Qu Wenruo | 8806d71 | 2017-10-09 01:51:06 +0000 | [diff] [blame] | 98 | ({ \ |
| 99 | if (!IS_ALIGNED(btrfs_file_extent_##name((leaf), (fi)), (alignment))) \ |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 100 | file_extent_err((fs_info), (leaf), (slot), \ |
Qu Wenruo | 8806d71 | 2017-10-09 01:51:06 +0000 | [diff] [blame] | 101 | "invalid %s for file extent, have %llu, should be aligned to %u", \ |
| 102 | (#name), btrfs_file_extent_##name((leaf), (fi)), \ |
| 103 | (alignment)); \ |
| 104 | (!IS_ALIGNED(btrfs_file_extent_##name((leaf), (fi)), (alignment))); \ |
| 105 | }) |
| 106 | |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 107 | static int check_extent_data_item(struct btrfs_fs_info *fs_info, |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 108 | struct extent_buffer *leaf, |
| 109 | struct btrfs_key *key, int slot) |
| 110 | { |
| 111 | struct btrfs_file_extent_item *fi; |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 112 | u32 sectorsize = fs_info->sectorsize; |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 113 | u32 item_size = btrfs_item_size_nr(leaf, slot); |
| 114 | |
| 115 | if (!IS_ALIGNED(key->offset, sectorsize)) { |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 116 | file_extent_err(fs_info, leaf, slot, |
Qu Wenruo | 8806d71 | 2017-10-09 01:51:06 +0000 | [diff] [blame] | 117 | "unaligned file_offset for file extent, have %llu should be aligned to %u", |
| 118 | key->offset, sectorsize); |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 119 | return -EUCLEAN; |
| 120 | } |
| 121 | |
| 122 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); |
| 123 | |
| 124 | if (btrfs_file_extent_type(leaf, fi) > BTRFS_FILE_EXTENT_TYPES) { |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 125 | file_extent_err(fs_info, leaf, slot, |
Qu Wenruo | 8806d71 | 2017-10-09 01:51:06 +0000 | [diff] [blame] | 126 | "invalid type for file extent, have %u expect range [0, %u]", |
| 127 | btrfs_file_extent_type(leaf, fi), |
| 128 | BTRFS_FILE_EXTENT_TYPES); |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 129 | return -EUCLEAN; |
| 130 | } |
| 131 | |
| 132 | /* |
Andrea Gelmini | 52042d8 | 2018-11-28 12:05:13 +0100 | [diff] [blame] | 133 | * Support for new compression/encryption must introduce incompat flag, |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 134 | * and must be caught in open_ctree(). |
| 135 | */ |
| 136 | if (btrfs_file_extent_compression(leaf, fi) > BTRFS_COMPRESS_TYPES) { |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 137 | file_extent_err(fs_info, leaf, slot, |
Qu Wenruo | 8806d71 | 2017-10-09 01:51:06 +0000 | [diff] [blame] | 138 | "invalid compression for file extent, have %u expect range [0, %u]", |
| 139 | btrfs_file_extent_compression(leaf, fi), |
| 140 | BTRFS_COMPRESS_TYPES); |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 141 | return -EUCLEAN; |
| 142 | } |
| 143 | if (btrfs_file_extent_encryption(leaf, fi)) { |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 144 | file_extent_err(fs_info, leaf, slot, |
Qu Wenruo | 8806d71 | 2017-10-09 01:51:06 +0000 | [diff] [blame] | 145 | "invalid encryption for file extent, have %u expect 0", |
| 146 | btrfs_file_extent_encryption(leaf, fi)); |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 147 | return -EUCLEAN; |
| 148 | } |
| 149 | if (btrfs_file_extent_type(leaf, fi) == BTRFS_FILE_EXTENT_INLINE) { |
| 150 | /* Inline extent must have 0 as key offset */ |
| 151 | if (key->offset) { |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 152 | file_extent_err(fs_info, leaf, slot, |
Qu Wenruo | 8806d71 | 2017-10-09 01:51:06 +0000 | [diff] [blame] | 153 | "invalid file_offset for inline file extent, have %llu expect 0", |
| 154 | key->offset); |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 155 | return -EUCLEAN; |
| 156 | } |
| 157 | |
| 158 | /* Compressed inline extent has no on-disk size, skip it */ |
| 159 | if (btrfs_file_extent_compression(leaf, fi) != |
| 160 | BTRFS_COMPRESS_NONE) |
| 161 | return 0; |
| 162 | |
| 163 | /* Uncompressed inline extent size must match item size */ |
| 164 | if (item_size != BTRFS_FILE_EXTENT_INLINE_DATA_START + |
| 165 | btrfs_file_extent_ram_bytes(leaf, fi)) { |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 166 | file_extent_err(fs_info, leaf, slot, |
Qu Wenruo | 8806d71 | 2017-10-09 01:51:06 +0000 | [diff] [blame] | 167 | "invalid ram_bytes for uncompressed inline extent, have %u expect %llu", |
| 168 | item_size, BTRFS_FILE_EXTENT_INLINE_DATA_START + |
| 169 | btrfs_file_extent_ram_bytes(leaf, fi)); |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 170 | return -EUCLEAN; |
| 171 | } |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | /* Regular or preallocated extent has fixed item size */ |
| 176 | if (item_size != sizeof(*fi)) { |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 177 | file_extent_err(fs_info, leaf, slot, |
Arnd Bergmann | 709a95c | 2017-10-13 11:27:35 +0200 | [diff] [blame] | 178 | "invalid item size for reg/prealloc file extent, have %u expect %zu", |
Qu Wenruo | 8806d71 | 2017-10-09 01:51:06 +0000 | [diff] [blame] | 179 | item_size, sizeof(*fi)); |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 180 | return -EUCLEAN; |
| 181 | } |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 182 | if (CHECK_FE_ALIGNED(fs_info, leaf, slot, fi, ram_bytes, sectorsize) || |
| 183 | CHECK_FE_ALIGNED(fs_info, leaf, slot, fi, disk_bytenr, sectorsize) || |
| 184 | CHECK_FE_ALIGNED(fs_info, leaf, slot, fi, disk_num_bytes, sectorsize) || |
| 185 | CHECK_FE_ALIGNED(fs_info, leaf, slot, fi, offset, sectorsize) || |
| 186 | CHECK_FE_ALIGNED(fs_info, leaf, slot, fi, num_bytes, sectorsize)) |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 187 | return -EUCLEAN; |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 188 | return 0; |
| 189 | } |
| 190 | |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 191 | static int check_csum_item(struct btrfs_fs_info *fs_info, |
| 192 | struct extent_buffer *leaf, struct btrfs_key *key, |
| 193 | int slot) |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 194 | { |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 195 | u32 sectorsize = fs_info->sectorsize; |
| 196 | u32 csumsize = btrfs_super_csum_size(fs_info->super_copy); |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 197 | |
| 198 | if (key->objectid != BTRFS_EXTENT_CSUM_OBJECTID) { |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 199 | generic_err(fs_info, leaf, slot, |
Qu Wenruo | d508c5f | 2017-10-09 01:51:05 +0000 | [diff] [blame] | 200 | "invalid key objectid for csum item, have %llu expect %llu", |
| 201 | key->objectid, BTRFS_EXTENT_CSUM_OBJECTID); |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 202 | return -EUCLEAN; |
| 203 | } |
| 204 | if (!IS_ALIGNED(key->offset, sectorsize)) { |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 205 | generic_err(fs_info, leaf, slot, |
Qu Wenruo | d508c5f | 2017-10-09 01:51:05 +0000 | [diff] [blame] | 206 | "unaligned key offset for csum item, have %llu should be aligned to %u", |
| 207 | key->offset, sectorsize); |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 208 | return -EUCLEAN; |
| 209 | } |
| 210 | if (!IS_ALIGNED(btrfs_item_size_nr(leaf, slot), csumsize)) { |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 211 | generic_err(fs_info, leaf, slot, |
Qu Wenruo | d508c5f | 2017-10-09 01:51:05 +0000 | [diff] [blame] | 212 | "unaligned item size for csum item, have %u should be aligned to %u", |
| 213 | btrfs_item_size_nr(leaf, slot), csumsize); |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 214 | return -EUCLEAN; |
| 215 | } |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | /* |
Qu Wenruo | ad7b0368 | 2017-11-08 08:54:25 +0800 | [diff] [blame] | 220 | * Customized reported for dir_item, only important new info is key->objectid, |
| 221 | * which represents inode number |
| 222 | */ |
| 223 | __printf(4, 5) |
David Sterba | e67c718 | 2018-02-19 17:24:18 +0100 | [diff] [blame] | 224 | __cold |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 225 | static void dir_item_err(const struct btrfs_fs_info *fs_info, |
Qu Wenruo | ad7b0368 | 2017-11-08 08:54:25 +0800 | [diff] [blame] | 226 | const struct extent_buffer *eb, int slot, |
| 227 | const char *fmt, ...) |
| 228 | { |
| 229 | struct btrfs_key key; |
| 230 | struct va_format vaf; |
| 231 | va_list args; |
| 232 | |
| 233 | btrfs_item_key_to_cpu(eb, &key, slot); |
| 234 | va_start(args, fmt); |
| 235 | |
| 236 | vaf.fmt = fmt; |
| 237 | vaf.va = &args; |
| 238 | |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 239 | btrfs_crit(fs_info, |
Qu Wenruo | ad7b0368 | 2017-11-08 08:54:25 +0800 | [diff] [blame] | 240 | "corrupt %s: root=%llu block=%llu slot=%d ino=%llu, %pV", |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 241 | btrfs_header_level(eb) == 0 ? "leaf" : "node", |
| 242 | btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot, |
| 243 | key.objectid, &vaf); |
Qu Wenruo | ad7b0368 | 2017-11-08 08:54:25 +0800 | [diff] [blame] | 244 | va_end(args); |
| 245 | } |
| 246 | |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 247 | static int check_dir_item(struct btrfs_fs_info *fs_info, |
Qu Wenruo | ad7b0368 | 2017-11-08 08:54:25 +0800 | [diff] [blame] | 248 | struct extent_buffer *leaf, |
| 249 | struct btrfs_key *key, int slot) |
| 250 | { |
| 251 | struct btrfs_dir_item *di; |
| 252 | u32 item_size = btrfs_item_size_nr(leaf, slot); |
| 253 | u32 cur = 0; |
| 254 | |
| 255 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
| 256 | while (cur < item_size) { |
Qu Wenruo | ad7b0368 | 2017-11-08 08:54:25 +0800 | [diff] [blame] | 257 | u32 name_len; |
| 258 | u32 data_len; |
| 259 | u32 max_name_len; |
| 260 | u32 total_size; |
| 261 | u32 name_hash; |
| 262 | u8 dir_type; |
| 263 | |
| 264 | /* header itself should not cross item boundary */ |
| 265 | if (cur + sizeof(*di) > item_size) { |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 266 | dir_item_err(fs_info, leaf, slot, |
Arnd Bergmann | 7cfad65 | 2017-12-06 15:18:14 +0100 | [diff] [blame] | 267 | "dir item header crosses item boundary, have %zu boundary %u", |
Qu Wenruo | ad7b0368 | 2017-11-08 08:54:25 +0800 | [diff] [blame] | 268 | cur + sizeof(*di), item_size); |
| 269 | return -EUCLEAN; |
| 270 | } |
| 271 | |
| 272 | /* dir type check */ |
| 273 | dir_type = btrfs_dir_type(leaf, di); |
| 274 | if (dir_type >= BTRFS_FT_MAX) { |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 275 | dir_item_err(fs_info, leaf, slot, |
Qu Wenruo | ad7b0368 | 2017-11-08 08:54:25 +0800 | [diff] [blame] | 276 | "invalid dir item type, have %u expect [0, %u)", |
| 277 | dir_type, BTRFS_FT_MAX); |
| 278 | return -EUCLEAN; |
| 279 | } |
| 280 | |
| 281 | if (key->type == BTRFS_XATTR_ITEM_KEY && |
| 282 | dir_type != BTRFS_FT_XATTR) { |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 283 | dir_item_err(fs_info, leaf, slot, |
Qu Wenruo | ad7b0368 | 2017-11-08 08:54:25 +0800 | [diff] [blame] | 284 | "invalid dir item type for XATTR key, have %u expect %u", |
| 285 | dir_type, BTRFS_FT_XATTR); |
| 286 | return -EUCLEAN; |
| 287 | } |
| 288 | if (dir_type == BTRFS_FT_XATTR && |
| 289 | key->type != BTRFS_XATTR_ITEM_KEY) { |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 290 | dir_item_err(fs_info, leaf, slot, |
Qu Wenruo | ad7b0368 | 2017-11-08 08:54:25 +0800 | [diff] [blame] | 291 | "xattr dir type found for non-XATTR key"); |
| 292 | return -EUCLEAN; |
| 293 | } |
| 294 | if (dir_type == BTRFS_FT_XATTR) |
| 295 | max_name_len = XATTR_NAME_MAX; |
| 296 | else |
| 297 | max_name_len = BTRFS_NAME_LEN; |
| 298 | |
| 299 | /* Name/data length check */ |
| 300 | name_len = btrfs_dir_name_len(leaf, di); |
| 301 | data_len = btrfs_dir_data_len(leaf, di); |
| 302 | if (name_len > max_name_len) { |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 303 | dir_item_err(fs_info, leaf, slot, |
Qu Wenruo | ad7b0368 | 2017-11-08 08:54:25 +0800 | [diff] [blame] | 304 | "dir item name len too long, have %u max %u", |
| 305 | name_len, max_name_len); |
| 306 | return -EUCLEAN; |
| 307 | } |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 308 | if (name_len + data_len > BTRFS_MAX_XATTR_SIZE(fs_info)) { |
| 309 | dir_item_err(fs_info, leaf, slot, |
Qu Wenruo | ad7b0368 | 2017-11-08 08:54:25 +0800 | [diff] [blame] | 310 | "dir item name and data len too long, have %u max %u", |
| 311 | name_len + data_len, |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 312 | BTRFS_MAX_XATTR_SIZE(fs_info)); |
Qu Wenruo | ad7b0368 | 2017-11-08 08:54:25 +0800 | [diff] [blame] | 313 | return -EUCLEAN; |
| 314 | } |
| 315 | |
| 316 | if (data_len && dir_type != BTRFS_FT_XATTR) { |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 317 | dir_item_err(fs_info, leaf, slot, |
Qu Wenruo | ad7b0368 | 2017-11-08 08:54:25 +0800 | [diff] [blame] | 318 | "dir item with invalid data len, have %u expect 0", |
| 319 | data_len); |
| 320 | return -EUCLEAN; |
| 321 | } |
| 322 | |
| 323 | total_size = sizeof(*di) + name_len + data_len; |
| 324 | |
| 325 | /* header and name/data should not cross item boundary */ |
| 326 | if (cur + total_size > item_size) { |
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 data crosses item boundary, have %u boundary %u", |
| 329 | cur + total_size, item_size); |
| 330 | return -EUCLEAN; |
| 331 | } |
| 332 | |
| 333 | /* |
| 334 | * Special check for XATTR/DIR_ITEM, as key->offset is name |
| 335 | * hash, should match its name |
| 336 | */ |
| 337 | if (key->type == BTRFS_DIR_ITEM_KEY || |
| 338 | key->type == BTRFS_XATTR_ITEM_KEY) { |
David Sterba | e2683fc | 2018-01-10 15:13:07 +0100 | [diff] [blame] | 339 | char namebuf[max(BTRFS_NAME_LEN, XATTR_NAME_MAX)]; |
| 340 | |
Qu Wenruo | ad7b0368 | 2017-11-08 08:54:25 +0800 | [diff] [blame] | 341 | read_extent_buffer(leaf, namebuf, |
| 342 | (unsigned long)(di + 1), name_len); |
| 343 | name_hash = btrfs_name_hash(namebuf, name_len); |
| 344 | if (key->offset != name_hash) { |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 345 | dir_item_err(fs_info, leaf, slot, |
Qu Wenruo | ad7b0368 | 2017-11-08 08:54:25 +0800 | [diff] [blame] | 346 | "name hash mismatch with key, have 0x%016x expect 0x%016llx", |
| 347 | name_hash, key->offset); |
| 348 | return -EUCLEAN; |
| 349 | } |
| 350 | } |
| 351 | cur += total_size; |
| 352 | di = (struct btrfs_dir_item *)((void *)di + total_size); |
| 353 | } |
| 354 | return 0; |
| 355 | } |
| 356 | |
Qu Wenruo | fce466e | 2018-07-03 17:10:05 +0800 | [diff] [blame] | 357 | __printf(4, 5) |
| 358 | __cold |
| 359 | static void block_group_err(const struct btrfs_fs_info *fs_info, |
| 360 | const struct extent_buffer *eb, int slot, |
| 361 | const char *fmt, ...) |
| 362 | { |
| 363 | struct btrfs_key key; |
| 364 | struct va_format vaf; |
| 365 | va_list args; |
| 366 | |
| 367 | btrfs_item_key_to_cpu(eb, &key, slot); |
| 368 | va_start(args, fmt); |
| 369 | |
| 370 | vaf.fmt = fmt; |
| 371 | vaf.va = &args; |
| 372 | |
| 373 | btrfs_crit(fs_info, |
| 374 | "corrupt %s: root=%llu block=%llu slot=%d bg_start=%llu bg_len=%llu, %pV", |
| 375 | btrfs_header_level(eb) == 0 ? "leaf" : "node", |
| 376 | btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot, |
| 377 | key.objectid, key.offset, &vaf); |
| 378 | va_end(args); |
| 379 | } |
| 380 | |
| 381 | static int check_block_group_item(struct btrfs_fs_info *fs_info, |
| 382 | struct extent_buffer *leaf, |
| 383 | struct btrfs_key *key, int slot) |
| 384 | { |
| 385 | struct btrfs_block_group_item bgi; |
| 386 | u32 item_size = btrfs_item_size_nr(leaf, slot); |
| 387 | u64 flags; |
| 388 | u64 type; |
| 389 | |
| 390 | /* |
| 391 | * Here we don't really care about alignment since extent allocator can |
Qu Wenruo | 1095092 | 2018-11-23 09:06:36 +0800 | [diff] [blame] | 392 | * handle it. We care more about the size. |
Qu Wenruo | fce466e | 2018-07-03 17:10:05 +0800 | [diff] [blame] | 393 | */ |
Qu Wenruo | 1095092 | 2018-11-23 09:06:36 +0800 | [diff] [blame] | 394 | if (key->offset == 0) { |
Qu Wenruo | fce466e | 2018-07-03 17:10:05 +0800 | [diff] [blame] | 395 | block_group_err(fs_info, leaf, slot, |
Qu Wenruo | 1095092 | 2018-11-23 09:06:36 +0800 | [diff] [blame] | 396 | "invalid block group size 0"); |
Qu Wenruo | fce466e | 2018-07-03 17:10:05 +0800 | [diff] [blame] | 397 | return -EUCLEAN; |
| 398 | } |
| 399 | |
| 400 | if (item_size != sizeof(bgi)) { |
| 401 | block_group_err(fs_info, leaf, slot, |
| 402 | "invalid item size, have %u expect %zu", |
| 403 | item_size, sizeof(bgi)); |
| 404 | return -EUCLEAN; |
| 405 | } |
| 406 | |
| 407 | read_extent_buffer(leaf, &bgi, btrfs_item_ptr_offset(leaf, slot), |
| 408 | sizeof(bgi)); |
| 409 | if (btrfs_block_group_chunk_objectid(&bgi) != |
| 410 | BTRFS_FIRST_CHUNK_TREE_OBJECTID) { |
| 411 | block_group_err(fs_info, leaf, slot, |
| 412 | "invalid block group chunk objectid, have %llu expect %llu", |
| 413 | btrfs_block_group_chunk_objectid(&bgi), |
| 414 | BTRFS_FIRST_CHUNK_TREE_OBJECTID); |
| 415 | return -EUCLEAN; |
| 416 | } |
| 417 | |
| 418 | if (btrfs_block_group_used(&bgi) > key->offset) { |
| 419 | block_group_err(fs_info, leaf, slot, |
| 420 | "invalid block group used, have %llu expect [0, %llu)", |
| 421 | btrfs_block_group_used(&bgi), key->offset); |
| 422 | return -EUCLEAN; |
| 423 | } |
| 424 | |
| 425 | flags = btrfs_block_group_flags(&bgi); |
| 426 | if (hweight64(flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) > 1) { |
| 427 | block_group_err(fs_info, leaf, slot, |
| 428 | "invalid profile flags, have 0x%llx (%lu bits set) expect no more than 1 bit set", |
| 429 | flags & BTRFS_BLOCK_GROUP_PROFILE_MASK, |
| 430 | hweight64(flags & BTRFS_BLOCK_GROUP_PROFILE_MASK)); |
| 431 | return -EUCLEAN; |
| 432 | } |
| 433 | |
| 434 | type = flags & BTRFS_BLOCK_GROUP_TYPE_MASK; |
| 435 | if (type != BTRFS_BLOCK_GROUP_DATA && |
| 436 | type != BTRFS_BLOCK_GROUP_METADATA && |
| 437 | type != BTRFS_BLOCK_GROUP_SYSTEM && |
| 438 | type != (BTRFS_BLOCK_GROUP_METADATA | |
| 439 | BTRFS_BLOCK_GROUP_DATA)) { |
| 440 | block_group_err(fs_info, leaf, slot, |
Shaokun Zhang | 761333f | 2018-11-05 18:49:09 +0800 | [diff] [blame] | 441 | "invalid type, have 0x%llx (%lu bits set) expect either 0x%llx, 0x%llx, 0x%llx or 0x%llx", |
Qu Wenruo | fce466e | 2018-07-03 17:10:05 +0800 | [diff] [blame] | 442 | type, hweight64(type), |
| 443 | BTRFS_BLOCK_GROUP_DATA, BTRFS_BLOCK_GROUP_METADATA, |
| 444 | BTRFS_BLOCK_GROUP_SYSTEM, |
| 445 | BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA); |
| 446 | return -EUCLEAN; |
| 447 | } |
| 448 | return 0; |
| 449 | } |
| 450 | |
Qu Wenruo | f114024 | 2019-03-20 13:36:06 +0800 | [diff] [blame] | 451 | __printf(5, 6) |
| 452 | __cold |
| 453 | static void chunk_err(const struct btrfs_fs_info *fs_info, |
| 454 | const struct extent_buffer *leaf, |
| 455 | const struct btrfs_chunk *chunk, u64 logical, |
| 456 | const char *fmt, ...) |
| 457 | { |
| 458 | bool is_sb; |
| 459 | struct va_format vaf; |
| 460 | va_list args; |
| 461 | int i; |
| 462 | int slot = -1; |
| 463 | |
| 464 | /* Only superblock eb is able to have such small offset */ |
| 465 | is_sb = (leaf->start == BTRFS_SUPER_INFO_OFFSET); |
| 466 | |
| 467 | if (!is_sb) { |
| 468 | /* |
| 469 | * Get the slot number by iterating through all slots, this |
| 470 | * would provide better readability. |
| 471 | */ |
| 472 | for (i = 0; i < btrfs_header_nritems(leaf); i++) { |
| 473 | if (btrfs_item_ptr_offset(leaf, i) == |
| 474 | (unsigned long)chunk) { |
| 475 | slot = i; |
| 476 | break; |
| 477 | } |
| 478 | } |
| 479 | } |
| 480 | va_start(args, fmt); |
| 481 | vaf.fmt = fmt; |
| 482 | vaf.va = &args; |
| 483 | |
| 484 | if (is_sb) |
| 485 | btrfs_crit(fs_info, |
| 486 | "corrupt superblock syschunk array: chunk_start=%llu, %pV", |
| 487 | logical, &vaf); |
| 488 | else |
| 489 | btrfs_crit(fs_info, |
| 490 | "corrupt leaf: root=%llu block=%llu slot=%d chunk_start=%llu, %pV", |
| 491 | BTRFS_CHUNK_TREE_OBJECTID, leaf->start, slot, |
| 492 | logical, &vaf); |
| 493 | va_end(args); |
| 494 | } |
| 495 | |
Qu Wenruo | ad7b0368 | 2017-11-08 08:54:25 +0800 | [diff] [blame] | 496 | /* |
Qu Wenruo | 82fc28f | 2019-03-20 13:16:42 +0800 | [diff] [blame] | 497 | * The common chunk check which could also work on super block sys chunk array. |
| 498 | * |
Qu Wenruo | bf871c3 | 2019-03-20 13:39:14 +0800 | [diff] [blame] | 499 | * Return -EUCLEAN if anything is corrupted. |
Qu Wenruo | 82fc28f | 2019-03-20 13:16:42 +0800 | [diff] [blame] | 500 | * Return 0 if everything is OK. |
| 501 | */ |
| 502 | int btrfs_check_chunk_valid(struct btrfs_fs_info *fs_info, |
| 503 | struct extent_buffer *leaf, |
| 504 | struct btrfs_chunk *chunk, u64 logical) |
| 505 | { |
| 506 | u64 length; |
| 507 | u64 stripe_len; |
| 508 | u16 num_stripes; |
| 509 | u16 sub_stripes; |
| 510 | u64 type; |
| 511 | u64 features; |
| 512 | bool mixed = false; |
| 513 | |
| 514 | length = btrfs_chunk_length(leaf, chunk); |
| 515 | stripe_len = btrfs_chunk_stripe_len(leaf, chunk); |
| 516 | num_stripes = btrfs_chunk_num_stripes(leaf, chunk); |
| 517 | sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk); |
| 518 | type = btrfs_chunk_type(leaf, chunk); |
| 519 | |
| 520 | if (!num_stripes) { |
Qu Wenruo | f114024 | 2019-03-20 13:36:06 +0800 | [diff] [blame] | 521 | chunk_err(fs_info, leaf, chunk, logical, |
| 522 | "invalid chunk num_stripes, have %u", num_stripes); |
Qu Wenruo | bf871c3 | 2019-03-20 13:39:14 +0800 | [diff] [blame] | 523 | return -EUCLEAN; |
Qu Wenruo | 82fc28f | 2019-03-20 13:16:42 +0800 | [diff] [blame] | 524 | } |
| 525 | if (!IS_ALIGNED(logical, fs_info->sectorsize)) { |
Qu Wenruo | f114024 | 2019-03-20 13:36:06 +0800 | [diff] [blame] | 526 | chunk_err(fs_info, leaf, chunk, logical, |
| 527 | "invalid chunk logical, have %llu should aligned to %u", |
| 528 | logical, fs_info->sectorsize); |
Qu Wenruo | bf871c3 | 2019-03-20 13:39:14 +0800 | [diff] [blame] | 529 | return -EUCLEAN; |
Qu Wenruo | 82fc28f | 2019-03-20 13:16:42 +0800 | [diff] [blame] | 530 | } |
| 531 | if (btrfs_chunk_sector_size(leaf, chunk) != fs_info->sectorsize) { |
Qu Wenruo | f114024 | 2019-03-20 13:36:06 +0800 | [diff] [blame] | 532 | chunk_err(fs_info, leaf, chunk, logical, |
| 533 | "invalid chunk sectorsize, have %u expect %u", |
| 534 | btrfs_chunk_sector_size(leaf, chunk), |
| 535 | fs_info->sectorsize); |
Qu Wenruo | bf871c3 | 2019-03-20 13:39:14 +0800 | [diff] [blame] | 536 | return -EUCLEAN; |
Qu Wenruo | 82fc28f | 2019-03-20 13:16:42 +0800 | [diff] [blame] | 537 | } |
| 538 | if (!length || !IS_ALIGNED(length, fs_info->sectorsize)) { |
Qu Wenruo | f114024 | 2019-03-20 13:36:06 +0800 | [diff] [blame] | 539 | chunk_err(fs_info, leaf, chunk, logical, |
| 540 | "invalid chunk length, have %llu", length); |
Qu Wenruo | bf871c3 | 2019-03-20 13:39:14 +0800 | [diff] [blame] | 541 | return -EUCLEAN; |
Qu Wenruo | 82fc28f | 2019-03-20 13:16:42 +0800 | [diff] [blame] | 542 | } |
| 543 | if (!is_power_of_2(stripe_len) || stripe_len != BTRFS_STRIPE_LEN) { |
Qu Wenruo | f114024 | 2019-03-20 13:36:06 +0800 | [diff] [blame] | 544 | chunk_err(fs_info, leaf, chunk, logical, |
| 545 | "invalid chunk stripe length: %llu", |
Qu Wenruo | 82fc28f | 2019-03-20 13:16:42 +0800 | [diff] [blame] | 546 | stripe_len); |
Qu Wenruo | bf871c3 | 2019-03-20 13:39:14 +0800 | [diff] [blame] | 547 | return -EUCLEAN; |
Qu Wenruo | 82fc28f | 2019-03-20 13:16:42 +0800 | [diff] [blame] | 548 | } |
| 549 | if (~(BTRFS_BLOCK_GROUP_TYPE_MASK | BTRFS_BLOCK_GROUP_PROFILE_MASK) & |
| 550 | type) { |
Qu Wenruo | f114024 | 2019-03-20 13:36:06 +0800 | [diff] [blame] | 551 | chunk_err(fs_info, leaf, chunk, logical, |
| 552 | "unrecognized chunk type: 0x%llx", |
Qu Wenruo | 82fc28f | 2019-03-20 13:16:42 +0800 | [diff] [blame] | 553 | ~(BTRFS_BLOCK_GROUP_TYPE_MASK | |
| 554 | BTRFS_BLOCK_GROUP_PROFILE_MASK) & |
| 555 | btrfs_chunk_type(leaf, chunk)); |
Qu Wenruo | bf871c3 | 2019-03-20 13:39:14 +0800 | [diff] [blame] | 556 | return -EUCLEAN; |
Qu Wenruo | 82fc28f | 2019-03-20 13:16:42 +0800 | [diff] [blame] | 557 | } |
| 558 | |
Qu Wenruo | 80e46cf | 2019-03-13 12:17:50 +0800 | [diff] [blame] | 559 | if (!is_power_of_2(type & BTRFS_BLOCK_GROUP_PROFILE_MASK) && |
| 560 | (type & BTRFS_BLOCK_GROUP_PROFILE_MASK) != 0) { |
| 561 | chunk_err(fs_info, leaf, chunk, logical, |
| 562 | "invalid chunk profile flag: 0x%llx, expect 0 or 1 bit set", |
| 563 | type & BTRFS_BLOCK_GROUP_PROFILE_MASK); |
| 564 | return -EUCLEAN; |
| 565 | } |
Qu Wenruo | 82fc28f | 2019-03-20 13:16:42 +0800 | [diff] [blame] | 566 | if ((type & BTRFS_BLOCK_GROUP_TYPE_MASK) == 0) { |
Qu Wenruo | f114024 | 2019-03-20 13:36:06 +0800 | [diff] [blame] | 567 | chunk_err(fs_info, leaf, chunk, logical, |
| 568 | "missing chunk type flag, have 0x%llx one bit must be set in 0x%llx", |
| 569 | type, BTRFS_BLOCK_GROUP_TYPE_MASK); |
Qu Wenruo | bf871c3 | 2019-03-20 13:39:14 +0800 | [diff] [blame] | 570 | return -EUCLEAN; |
Qu Wenruo | 82fc28f | 2019-03-20 13:16:42 +0800 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | if ((type & BTRFS_BLOCK_GROUP_SYSTEM) && |
| 574 | (type & (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA))) { |
Qu Wenruo | f114024 | 2019-03-20 13:36:06 +0800 | [diff] [blame] | 575 | chunk_err(fs_info, leaf, chunk, logical, |
| 576 | "system chunk with data or metadata type: 0x%llx", |
| 577 | type); |
Qu Wenruo | bf871c3 | 2019-03-20 13:39:14 +0800 | [diff] [blame] | 578 | return -EUCLEAN; |
Qu Wenruo | 82fc28f | 2019-03-20 13:16:42 +0800 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | features = btrfs_super_incompat_flags(fs_info->super_copy); |
| 582 | if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) |
| 583 | mixed = true; |
| 584 | |
| 585 | if (!mixed) { |
| 586 | if ((type & BTRFS_BLOCK_GROUP_METADATA) && |
| 587 | (type & BTRFS_BLOCK_GROUP_DATA)) { |
Qu Wenruo | f114024 | 2019-03-20 13:36:06 +0800 | [diff] [blame] | 588 | chunk_err(fs_info, leaf, chunk, logical, |
Qu Wenruo | 82fc28f | 2019-03-20 13:16:42 +0800 | [diff] [blame] | 589 | "mixed chunk type in non-mixed mode: 0x%llx", type); |
Qu Wenruo | bf871c3 | 2019-03-20 13:39:14 +0800 | [diff] [blame] | 590 | return -EUCLEAN; |
Qu Wenruo | 82fc28f | 2019-03-20 13:16:42 +0800 | [diff] [blame] | 591 | } |
| 592 | } |
| 593 | |
| 594 | if ((type & BTRFS_BLOCK_GROUP_RAID10 && sub_stripes != 2) || |
| 595 | (type & BTRFS_BLOCK_GROUP_RAID1 && num_stripes != 2) || |
| 596 | (type & BTRFS_BLOCK_GROUP_RAID5 && num_stripes < 2) || |
| 597 | (type & BTRFS_BLOCK_GROUP_RAID6 && num_stripes < 3) || |
| 598 | (type & BTRFS_BLOCK_GROUP_DUP && num_stripes != 2) || |
| 599 | ((type & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 && num_stripes != 1)) { |
Qu Wenruo | f114024 | 2019-03-20 13:36:06 +0800 | [diff] [blame] | 600 | chunk_err(fs_info, leaf, chunk, logical, |
Qu Wenruo | 82fc28f | 2019-03-20 13:16:42 +0800 | [diff] [blame] | 601 | "invalid num_stripes:sub_stripes %u:%u for profile %llu", |
| 602 | num_stripes, sub_stripes, |
| 603 | type & BTRFS_BLOCK_GROUP_PROFILE_MASK); |
Qu Wenruo | bf871c3 | 2019-03-20 13:39:14 +0800 | [diff] [blame] | 604 | return -EUCLEAN; |
Qu Wenruo | 82fc28f | 2019-03-20 13:16:42 +0800 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | return 0; |
| 608 | } |
| 609 | |
Qu Wenruo | ab4ba2e | 2019-03-08 14:20:03 +0800 | [diff] [blame] | 610 | __printf(4, 5) |
| 611 | __cold |
| 612 | static void dev_item_err(const struct btrfs_fs_info *fs_info, |
| 613 | const struct extent_buffer *eb, int slot, |
| 614 | const char *fmt, ...) |
| 615 | { |
| 616 | struct btrfs_key key; |
| 617 | struct va_format vaf; |
| 618 | va_list args; |
| 619 | |
| 620 | btrfs_item_key_to_cpu(eb, &key, slot); |
| 621 | va_start(args, fmt); |
| 622 | |
| 623 | vaf.fmt = fmt; |
| 624 | vaf.va = &args; |
| 625 | |
| 626 | btrfs_crit(fs_info, |
| 627 | "corrupt %s: root=%llu block=%llu slot=%d devid=%llu %pV", |
| 628 | btrfs_header_level(eb) == 0 ? "leaf" : "node", |
| 629 | btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot, |
| 630 | key.objectid, &vaf); |
| 631 | va_end(args); |
| 632 | } |
| 633 | |
| 634 | static int check_dev_item(struct btrfs_fs_info *fs_info, |
| 635 | struct extent_buffer *leaf, |
| 636 | struct btrfs_key *key, int slot) |
| 637 | { |
| 638 | struct btrfs_dev_item *ditem; |
| 639 | u64 max_devid = max(BTRFS_MAX_DEVS(fs_info), BTRFS_MAX_DEVS_SYS_CHUNK); |
| 640 | |
| 641 | if (key->objectid != BTRFS_DEV_ITEMS_OBJECTID) { |
| 642 | dev_item_err(fs_info, leaf, slot, |
| 643 | "invalid objectid: has=%llu expect=%llu", |
| 644 | key->objectid, BTRFS_DEV_ITEMS_OBJECTID); |
| 645 | return -EUCLEAN; |
| 646 | } |
| 647 | if (key->offset > max_devid) { |
| 648 | dev_item_err(fs_info, leaf, slot, |
| 649 | "invalid devid: has=%llu expect=[0, %llu]", |
| 650 | key->offset, max_devid); |
| 651 | return -EUCLEAN; |
| 652 | } |
| 653 | ditem = btrfs_item_ptr(leaf, slot, struct btrfs_dev_item); |
| 654 | if (btrfs_device_id(leaf, ditem) != key->offset) { |
| 655 | dev_item_err(fs_info, leaf, slot, |
| 656 | "devid mismatch: key has=%llu item has=%llu", |
| 657 | key->offset, btrfs_device_id(leaf, ditem)); |
| 658 | return -EUCLEAN; |
| 659 | } |
| 660 | |
| 661 | /* |
| 662 | * For device total_bytes, we don't have reliable way to check it, as |
| 663 | * it can be 0 for device removal. Device size check can only be done |
| 664 | * by dev extents check. |
| 665 | */ |
| 666 | if (btrfs_device_bytes_used(leaf, ditem) > |
| 667 | btrfs_device_total_bytes(leaf, ditem)) { |
| 668 | dev_item_err(fs_info, leaf, slot, |
| 669 | "invalid bytes used: have %llu expect [0, %llu]", |
| 670 | btrfs_device_bytes_used(leaf, ditem), |
| 671 | btrfs_device_total_bytes(leaf, ditem)); |
| 672 | return -EUCLEAN; |
| 673 | } |
| 674 | /* |
| 675 | * Remaining members like io_align/type/gen/dev_group aren't really |
| 676 | * utilized. Skip them to make later usage of them easier. |
| 677 | */ |
| 678 | return 0; |
| 679 | } |
| 680 | |
Qu Wenruo | 496245c | 2019-03-13 14:31:35 +0800 | [diff] [blame] | 681 | /* Inode item error output has the same format as dir_item_err() */ |
| 682 | #define inode_item_err(fs_info, eb, slot, fmt, ...) \ |
| 683 | dir_item_err(fs_info, eb, slot, fmt, __VA_ARGS__) |
| 684 | |
| 685 | static int check_inode_item(struct btrfs_fs_info *fs_info, |
| 686 | struct extent_buffer *leaf, |
| 687 | struct btrfs_key *key, int slot) |
| 688 | { |
| 689 | struct btrfs_inode_item *iitem; |
| 690 | u64 super_gen = btrfs_super_generation(fs_info->super_copy); |
| 691 | u32 valid_mask = (S_IFMT | S_ISUID | S_ISGID | S_ISVTX | 0777); |
| 692 | u32 mode; |
| 693 | |
| 694 | if ((key->objectid < BTRFS_FIRST_FREE_OBJECTID || |
| 695 | key->objectid > BTRFS_LAST_FREE_OBJECTID) && |
| 696 | key->objectid != BTRFS_ROOT_TREE_DIR_OBJECTID && |
| 697 | key->objectid != BTRFS_FREE_INO_OBJECTID) { |
| 698 | generic_err(fs_info, leaf, slot, |
| 699 | "invalid key objectid: has %llu expect %llu or [%llu, %llu] or %llu", |
| 700 | key->objectid, BTRFS_ROOT_TREE_DIR_OBJECTID, |
| 701 | BTRFS_FIRST_FREE_OBJECTID, |
| 702 | BTRFS_LAST_FREE_OBJECTID, |
| 703 | BTRFS_FREE_INO_OBJECTID); |
| 704 | return -EUCLEAN; |
| 705 | } |
| 706 | if (key->offset != 0) { |
| 707 | inode_item_err(fs_info, leaf, slot, |
| 708 | "invalid key offset: has %llu expect 0", |
| 709 | key->offset); |
| 710 | return -EUCLEAN; |
| 711 | } |
| 712 | iitem = btrfs_item_ptr(leaf, slot, struct btrfs_inode_item); |
| 713 | |
| 714 | /* Here we use super block generation + 1 to handle log tree */ |
| 715 | if (btrfs_inode_generation(leaf, iitem) > super_gen + 1) { |
| 716 | inode_item_err(fs_info, leaf, slot, |
| 717 | "invalid inode generation: has %llu expect (0, %llu]", |
| 718 | btrfs_inode_generation(leaf, iitem), |
| 719 | super_gen + 1); |
| 720 | return -EUCLEAN; |
| 721 | } |
| 722 | /* Note for ROOT_TREE_DIR_ITEM, mkfs could set its transid 0 */ |
| 723 | if (btrfs_inode_transid(leaf, iitem) > super_gen + 1) { |
| 724 | inode_item_err(fs_info, leaf, slot, |
| 725 | "invalid inode generation: has %llu expect [0, %llu]", |
| 726 | btrfs_inode_transid(leaf, iitem), super_gen + 1); |
| 727 | return -EUCLEAN; |
| 728 | } |
| 729 | |
| 730 | /* |
| 731 | * For size and nbytes it's better not to be too strict, as for dir |
| 732 | * item its size/nbytes can easily get wrong, but doesn't affect |
| 733 | * anything in the fs. So here we skip the check. |
| 734 | */ |
| 735 | mode = btrfs_inode_mode(leaf, iitem); |
| 736 | if (mode & ~valid_mask) { |
| 737 | inode_item_err(fs_info, leaf, slot, |
| 738 | "unknown mode bit detected: 0x%x", |
| 739 | mode & ~valid_mask); |
| 740 | return -EUCLEAN; |
| 741 | } |
| 742 | |
| 743 | /* |
| 744 | * S_IFMT is not bit mapped so we can't completely rely on is_power_of_2, |
| 745 | * but is_power_of_2() can save us from checking FIFO/CHR/DIR/REG. |
| 746 | * Only needs to check BLK, LNK and SOCKS |
| 747 | */ |
| 748 | if (!is_power_of_2(mode & S_IFMT)) { |
| 749 | if (!S_ISLNK(mode) && !S_ISBLK(mode) && !S_ISSOCK(mode)) { |
| 750 | inode_item_err(fs_info, leaf, slot, |
| 751 | "invalid mode: has 0%o expect valid S_IF* bit(s)", |
| 752 | mode & S_IFMT); |
| 753 | return -EUCLEAN; |
| 754 | } |
| 755 | } |
| 756 | if (S_ISDIR(mode) && btrfs_inode_nlink(leaf, iitem) > 1) { |
| 757 | inode_item_err(fs_info, leaf, slot, |
| 758 | "invalid nlink: has %u expect no more than 1 for dir", |
| 759 | btrfs_inode_nlink(leaf, iitem)); |
| 760 | return -EUCLEAN; |
| 761 | } |
| 762 | if (btrfs_inode_flags(leaf, iitem) & ~BTRFS_INODE_FLAG_MASK) { |
| 763 | inode_item_err(fs_info, leaf, slot, |
| 764 | "unknown flags detected: 0x%llx", |
| 765 | btrfs_inode_flags(leaf, iitem) & |
| 766 | ~BTRFS_INODE_FLAG_MASK); |
| 767 | return -EUCLEAN; |
| 768 | } |
| 769 | return 0; |
| 770 | } |
| 771 | |
Qu Wenruo | 82fc28f | 2019-03-20 13:16:42 +0800 | [diff] [blame] | 772 | /* |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 773 | * Common point to switch the item-specific validation. |
| 774 | */ |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 775 | static int check_leaf_item(struct btrfs_fs_info *fs_info, |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 776 | struct extent_buffer *leaf, |
| 777 | struct btrfs_key *key, int slot) |
| 778 | { |
| 779 | int ret = 0; |
Qu Wenruo | 075cb3c | 2019-03-20 13:42:33 +0800 | [diff] [blame] | 780 | struct btrfs_chunk *chunk; |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 781 | |
| 782 | switch (key->type) { |
| 783 | case BTRFS_EXTENT_DATA_KEY: |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 784 | ret = check_extent_data_item(fs_info, leaf, key, slot); |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 785 | break; |
| 786 | case BTRFS_EXTENT_CSUM_KEY: |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 787 | ret = check_csum_item(fs_info, leaf, key, slot); |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 788 | break; |
Qu Wenruo | ad7b0368 | 2017-11-08 08:54:25 +0800 | [diff] [blame] | 789 | case BTRFS_DIR_ITEM_KEY: |
| 790 | case BTRFS_DIR_INDEX_KEY: |
| 791 | case BTRFS_XATTR_ITEM_KEY: |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 792 | ret = check_dir_item(fs_info, leaf, key, slot); |
Qu Wenruo | ad7b0368 | 2017-11-08 08:54:25 +0800 | [diff] [blame] | 793 | break; |
Qu Wenruo | fce466e | 2018-07-03 17:10:05 +0800 | [diff] [blame] | 794 | case BTRFS_BLOCK_GROUP_ITEM_KEY: |
| 795 | ret = check_block_group_item(fs_info, leaf, key, slot); |
| 796 | break; |
Qu Wenruo | 075cb3c | 2019-03-20 13:42:33 +0800 | [diff] [blame] | 797 | case BTRFS_CHUNK_ITEM_KEY: |
| 798 | chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk); |
| 799 | ret = btrfs_check_chunk_valid(fs_info, leaf, chunk, |
| 800 | key->offset); |
| 801 | break; |
Qu Wenruo | ab4ba2e | 2019-03-08 14:20:03 +0800 | [diff] [blame] | 802 | case BTRFS_DEV_ITEM_KEY: |
| 803 | ret = check_dev_item(fs_info, leaf, key, slot); |
| 804 | break; |
Qu Wenruo | 496245c | 2019-03-13 14:31:35 +0800 | [diff] [blame] | 805 | case BTRFS_INODE_ITEM_KEY: |
| 806 | ret = check_inode_item(fs_info, leaf, key, slot); |
| 807 | break; |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 808 | } |
| 809 | return ret; |
| 810 | } |
| 811 | |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 812 | 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] | 813 | bool check_item_data) |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 814 | { |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 815 | /* No valid key type is 0, so all key should be larger than this key */ |
| 816 | struct btrfs_key prev_key = {0, 0, 0}; |
| 817 | struct btrfs_key key; |
| 818 | u32 nritems = btrfs_header_nritems(leaf); |
| 819 | int slot; |
| 820 | |
Qu Wenruo | f556faa | 2018-09-28 07:59:34 +0800 | [diff] [blame] | 821 | if (btrfs_header_level(leaf) != 0) { |
| 822 | generic_err(fs_info, leaf, 0, |
| 823 | "invalid level for leaf, have %d expect 0", |
| 824 | btrfs_header_level(leaf)); |
| 825 | return -EUCLEAN; |
| 826 | } |
| 827 | |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 828 | /* |
| 829 | * Extent buffers from a relocation tree have a owner field that |
| 830 | * corresponds to the subvolume tree they are based on. So just from an |
| 831 | * extent buffer alone we can not find out what is the id of the |
| 832 | * corresponding subvolume tree, so we can not figure out if the extent |
| 833 | * buffer corresponds to the root of the relocation tree or not. So |
| 834 | * skip this check for relocation trees. |
| 835 | */ |
| 836 | if (nritems == 0 && !btrfs_header_flag(leaf, BTRFS_HEADER_FLAG_RELOC)) { |
Qu Wenruo | ba480dd | 2018-07-03 17:10:06 +0800 | [diff] [blame] | 837 | u64 owner = btrfs_header_owner(leaf); |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 838 | struct btrfs_root *check_root; |
| 839 | |
Qu Wenruo | ba480dd | 2018-07-03 17:10:06 +0800 | [diff] [blame] | 840 | /* These trees must never be empty */ |
| 841 | if (owner == BTRFS_ROOT_TREE_OBJECTID || |
| 842 | owner == BTRFS_CHUNK_TREE_OBJECTID || |
| 843 | owner == BTRFS_EXTENT_TREE_OBJECTID || |
| 844 | owner == BTRFS_DEV_TREE_OBJECTID || |
| 845 | owner == BTRFS_FS_TREE_OBJECTID || |
| 846 | owner == BTRFS_DATA_RELOC_TREE_OBJECTID) { |
| 847 | generic_err(fs_info, leaf, 0, |
| 848 | "invalid root, root %llu must never be empty", |
| 849 | owner); |
| 850 | return -EUCLEAN; |
| 851 | } |
| 852 | key.objectid = owner; |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 853 | key.type = BTRFS_ROOT_ITEM_KEY; |
| 854 | key.offset = (u64)-1; |
| 855 | |
| 856 | check_root = btrfs_get_fs_root(fs_info, &key, false); |
| 857 | /* |
| 858 | * The only reason we also check NULL here is that during |
| 859 | * open_ctree() some roots has not yet been set up. |
| 860 | */ |
| 861 | if (!IS_ERR_OR_NULL(check_root)) { |
| 862 | struct extent_buffer *eb; |
| 863 | |
| 864 | eb = btrfs_root_node(check_root); |
| 865 | /* if leaf is the root, then it's fine */ |
| 866 | if (leaf != eb) { |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 867 | generic_err(fs_info, leaf, 0, |
Qu Wenruo | 478d01b | 2017-10-09 01:51:04 +0000 | [diff] [blame] | 868 | "invalid nritems, have %u should not be 0 for non-root leaf", |
| 869 | nritems); |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 870 | free_extent_buffer(eb); |
| 871 | return -EUCLEAN; |
| 872 | } |
| 873 | free_extent_buffer(eb); |
| 874 | } |
| 875 | return 0; |
| 876 | } |
| 877 | |
| 878 | if (nritems == 0) |
| 879 | return 0; |
| 880 | |
| 881 | /* |
| 882 | * Check the following things to make sure this is a good leaf, and |
| 883 | * leaf users won't need to bother with similar sanity checks: |
| 884 | * |
| 885 | * 1) key ordering |
| 886 | * 2) item offset and size |
| 887 | * No overlap, no hole, all inside the leaf. |
| 888 | * 3) item content |
| 889 | * If possible, do comprehensive sanity check. |
| 890 | * NOTE: All checks must only rely on the item data itself. |
| 891 | */ |
| 892 | for (slot = 0; slot < nritems; slot++) { |
| 893 | u32 item_end_expected; |
| 894 | int ret; |
| 895 | |
| 896 | btrfs_item_key_to_cpu(leaf, &key, slot); |
| 897 | |
| 898 | /* Make sure the keys are in the right order */ |
| 899 | if (btrfs_comp_cpu_keys(&prev_key, &key) >= 0) { |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 900 | generic_err(fs_info, leaf, slot, |
Qu Wenruo | 478d01b | 2017-10-09 01:51:04 +0000 | [diff] [blame] | 901 | "bad key order, prev (%llu %u %llu) current (%llu %u %llu)", |
| 902 | prev_key.objectid, prev_key.type, |
| 903 | prev_key.offset, key.objectid, key.type, |
| 904 | key.offset); |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 905 | return -EUCLEAN; |
| 906 | } |
| 907 | |
| 908 | /* |
| 909 | * Make sure the offset and ends are right, remember that the |
| 910 | * item data starts at the end of the leaf and grows towards the |
| 911 | * front. |
| 912 | */ |
| 913 | if (slot == 0) |
| 914 | item_end_expected = BTRFS_LEAF_DATA_SIZE(fs_info); |
| 915 | else |
| 916 | item_end_expected = btrfs_item_offset_nr(leaf, |
| 917 | slot - 1); |
| 918 | if (btrfs_item_end_nr(leaf, slot) != item_end_expected) { |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 919 | generic_err(fs_info, leaf, slot, |
Qu Wenruo | 478d01b | 2017-10-09 01:51:04 +0000 | [diff] [blame] | 920 | "unexpected item end, have %u expect %u", |
| 921 | btrfs_item_end_nr(leaf, slot), |
| 922 | item_end_expected); |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 923 | return -EUCLEAN; |
| 924 | } |
| 925 | |
| 926 | /* |
| 927 | * Check to make sure that we don't point outside of the leaf, |
| 928 | * just in case all the items are consistent to each other, but |
| 929 | * all point outside of the leaf. |
| 930 | */ |
| 931 | if (btrfs_item_end_nr(leaf, slot) > |
| 932 | BTRFS_LEAF_DATA_SIZE(fs_info)) { |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 933 | generic_err(fs_info, leaf, slot, |
Qu Wenruo | 478d01b | 2017-10-09 01:51:04 +0000 | [diff] [blame] | 934 | "slot end outside of leaf, have %u expect range [0, %u]", |
| 935 | btrfs_item_end_nr(leaf, slot), |
| 936 | BTRFS_LEAF_DATA_SIZE(fs_info)); |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 937 | return -EUCLEAN; |
| 938 | } |
| 939 | |
| 940 | /* Also check if the item pointer overlaps with btrfs item. */ |
| 941 | if (btrfs_item_nr_offset(slot) + sizeof(struct btrfs_item) > |
| 942 | btrfs_item_ptr_offset(leaf, slot)) { |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 943 | generic_err(fs_info, leaf, slot, |
Qu Wenruo | 478d01b | 2017-10-09 01:51:04 +0000 | [diff] [blame] | 944 | "slot overlaps with its data, item end %lu data start %lu", |
| 945 | btrfs_item_nr_offset(slot) + |
| 946 | sizeof(struct btrfs_item), |
| 947 | btrfs_item_ptr_offset(leaf, slot)); |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 948 | return -EUCLEAN; |
| 949 | } |
| 950 | |
Qu Wenruo | 69fc6cb | 2017-11-08 08:54:24 +0800 | [diff] [blame] | 951 | if (check_item_data) { |
| 952 | /* |
| 953 | * Check if the item size and content meet other |
| 954 | * criteria |
| 955 | */ |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 956 | ret = check_leaf_item(fs_info, leaf, &key, slot); |
Qu Wenruo | 69fc6cb | 2017-11-08 08:54:24 +0800 | [diff] [blame] | 957 | if (ret < 0) |
| 958 | return ret; |
| 959 | } |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 960 | |
| 961 | prev_key.objectid = key.objectid; |
| 962 | prev_key.type = key.type; |
| 963 | prev_key.offset = key.offset; |
| 964 | } |
| 965 | |
| 966 | return 0; |
| 967 | } |
| 968 | |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 969 | int btrfs_check_leaf_full(struct btrfs_fs_info *fs_info, |
| 970 | struct extent_buffer *leaf) |
Qu Wenruo | 69fc6cb | 2017-11-08 08:54:24 +0800 | [diff] [blame] | 971 | { |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 972 | return check_leaf(fs_info, leaf, true); |
Qu Wenruo | 69fc6cb | 2017-11-08 08:54:24 +0800 | [diff] [blame] | 973 | } |
| 974 | |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 975 | int btrfs_check_leaf_relaxed(struct btrfs_fs_info *fs_info, |
Qu Wenruo | 69fc6cb | 2017-11-08 08:54:24 +0800 | [diff] [blame] | 976 | struct extent_buffer *leaf) |
| 977 | { |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 978 | return check_leaf(fs_info, leaf, false); |
Qu Wenruo | 69fc6cb | 2017-11-08 08:54:24 +0800 | [diff] [blame] | 979 | } |
| 980 | |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 981 | 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] | 982 | { |
| 983 | unsigned long nr = btrfs_header_nritems(node); |
| 984 | struct btrfs_key key, next_key; |
| 985 | int slot; |
Qu Wenruo | f556faa | 2018-09-28 07:59:34 +0800 | [diff] [blame] | 986 | int level = btrfs_header_level(node); |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 987 | u64 bytenr; |
| 988 | int ret = 0; |
| 989 | |
Qu Wenruo | f556faa | 2018-09-28 07:59:34 +0800 | [diff] [blame] | 990 | if (level <= 0 || level >= BTRFS_MAX_LEVEL) { |
| 991 | generic_err(fs_info, node, 0, |
| 992 | "invalid level for node, have %d expect [1, %d]", |
| 993 | level, BTRFS_MAX_LEVEL - 1); |
| 994 | return -EUCLEAN; |
| 995 | } |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 996 | if (nr == 0 || nr > BTRFS_NODEPTRS_PER_BLOCK(fs_info)) { |
| 997 | btrfs_crit(fs_info, |
Qu Wenruo | bba4f29 | 2017-10-09 01:51:03 +0000 | [diff] [blame] | 998 | "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] | 999 | btrfs_header_owner(node), node->start, |
Qu Wenruo | bba4f29 | 2017-10-09 01:51:03 +0000 | [diff] [blame] | 1000 | nr == 0 ? "small" : "large", nr, |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 1001 | BTRFS_NODEPTRS_PER_BLOCK(fs_info)); |
Qu Wenruo | bba4f29 | 2017-10-09 01:51:03 +0000 | [diff] [blame] | 1002 | return -EUCLEAN; |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 1003 | } |
| 1004 | |
| 1005 | for (slot = 0; slot < nr - 1; slot++) { |
| 1006 | bytenr = btrfs_node_blockptr(node, slot); |
| 1007 | btrfs_node_key_to_cpu(node, &key, slot); |
| 1008 | btrfs_node_key_to_cpu(node, &next_key, slot + 1); |
| 1009 | |
| 1010 | if (!bytenr) { |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 1011 | generic_err(fs_info, node, slot, |
Qu Wenruo | bba4f29 | 2017-10-09 01:51:03 +0000 | [diff] [blame] | 1012 | "invalid NULL node pointer"); |
| 1013 | ret = -EUCLEAN; |
| 1014 | goto out; |
| 1015 | } |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 1016 | if (!IS_ALIGNED(bytenr, fs_info->sectorsize)) { |
| 1017 | generic_err(fs_info, node, slot, |
Qu Wenruo | bba4f29 | 2017-10-09 01:51:03 +0000 | [diff] [blame] | 1018 | "unaligned pointer, have %llu should be aligned to %u", |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 1019 | bytenr, fs_info->sectorsize); |
Qu Wenruo | bba4f29 | 2017-10-09 01:51:03 +0000 | [diff] [blame] | 1020 | ret = -EUCLEAN; |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 1021 | goto out; |
| 1022 | } |
| 1023 | |
| 1024 | if (btrfs_comp_cpu_keys(&key, &next_key) >= 0) { |
Qu Wenruo | 2f65954 | 2018-01-25 14:56:18 +0800 | [diff] [blame] | 1025 | generic_err(fs_info, node, slot, |
Qu Wenruo | bba4f29 | 2017-10-09 01:51:03 +0000 | [diff] [blame] | 1026 | "bad key order, current (%llu %u %llu) next (%llu %u %llu)", |
| 1027 | key.objectid, key.type, key.offset, |
| 1028 | next_key.objectid, next_key.type, |
| 1029 | next_key.offset); |
| 1030 | ret = -EUCLEAN; |
Qu Wenruo | 557ea5d | 2017-10-09 01:51:02 +0000 | [diff] [blame] | 1031 | goto out; |
| 1032 | } |
| 1033 | } |
| 1034 | out: |
| 1035 | return ret; |
| 1036 | } |