Commit | Line | Data |
---|---|---|
c1d7c514 | 1 | // SPDX-License-Identifier: GPL-2.0 |
557ea5dd QW |
2 | /* |
3 | * Copyright (C) Qu Wenruo 2017. All rights reserved. | |
557ea5dd QW |
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" | |
fce466ea | 22 | #include "volumes.h" |
557ea5dd | 23 | |
bba4f298 QW |
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. | |
30 | * It's recommened to decode key.objecitd/offset if it's | |
31 | * meaningful. | |
32 | * @reason: describe the error | |
33 | * @bad_value: optional, it's recommened to output bad value and its | |
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) | |
e67c718b | 45 | __cold |
2f659546 | 46 | static void generic_err(const struct btrfs_fs_info *fs_info, |
bba4f298 QW |
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 | ||
2f659546 | 58 | btrfs_crit(fs_info, |
bba4f298 QW |
59 | "corrupt %s: root=%llu block=%llu slot=%d, %pV", |
60 | btrfs_header_level(eb) == 0 ? "leaf" : "node", | |
2f659546 | 61 | btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot, &vaf); |
bba4f298 QW |
62 | va_end(args); |
63 | } | |
64 | ||
8806d718 QW |
65 | /* |
66 | * Customized reporter for extent data item, since its key objectid and | |
67 | * offset has its own meaning. | |
68 | */ | |
69 | __printf(4, 5) | |
e67c718b | 70 | __cold |
2f659546 | 71 | static void file_extent_err(const struct btrfs_fs_info *fs_info, |
8806d718 QW |
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 | ||
2f659546 | 85 | btrfs_crit(fs_info, |
8806d718 | 86 | "corrupt %s: root=%llu block=%llu slot=%d ino=%llu file_offset=%llu, %pV", |
2f659546 QW |
87 | btrfs_header_level(eb) == 0 ? "leaf" : "node", |
88 | btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot, | |
89 | key.objectid, key.offset, &vaf); | |
8806d718 QW |
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 | */ | |
2f659546 | 97 | #define CHECK_FE_ALIGNED(fs_info, leaf, slot, fi, name, alignment) \ |
8806d718 QW |
98 | ({ \ |
99 | if (!IS_ALIGNED(btrfs_file_extent_##name((leaf), (fi)), (alignment))) \ | |
2f659546 | 100 | file_extent_err((fs_info), (leaf), (slot), \ |
8806d718 QW |
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 | ||
2f659546 | 107 | static int check_extent_data_item(struct btrfs_fs_info *fs_info, |
557ea5dd QW |
108 | struct extent_buffer *leaf, |
109 | struct btrfs_key *key, int slot) | |
110 | { | |
111 | struct btrfs_file_extent_item *fi; | |
2f659546 | 112 | u32 sectorsize = fs_info->sectorsize; |
557ea5dd QW |
113 | u32 item_size = btrfs_item_size_nr(leaf, slot); |
114 | ||
115 | if (!IS_ALIGNED(key->offset, sectorsize)) { | |
2f659546 | 116 | file_extent_err(fs_info, leaf, slot, |
8806d718 QW |
117 | "unaligned file_offset for file extent, have %llu should be aligned to %u", |
118 | key->offset, sectorsize); | |
557ea5dd QW |
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) { | |
2f659546 | 125 | file_extent_err(fs_info, leaf, slot, |
8806d718 QW |
126 | "invalid type for file extent, have %u expect range [0, %u]", |
127 | btrfs_file_extent_type(leaf, fi), | |
128 | BTRFS_FILE_EXTENT_TYPES); | |
557ea5dd QW |
129 | return -EUCLEAN; |
130 | } | |
131 | ||
132 | /* | |
133 | * Support for new compression/encrption must introduce incompat flag, | |
134 | * and must be caught in open_ctree(). | |
135 | */ | |
136 | if (btrfs_file_extent_compression(leaf, fi) > BTRFS_COMPRESS_TYPES) { | |
2f659546 | 137 | file_extent_err(fs_info, leaf, slot, |
8806d718 QW |
138 | "invalid compression for file extent, have %u expect range [0, %u]", |
139 | btrfs_file_extent_compression(leaf, fi), | |
140 | BTRFS_COMPRESS_TYPES); | |
557ea5dd QW |
141 | return -EUCLEAN; |
142 | } | |
143 | if (btrfs_file_extent_encryption(leaf, fi)) { | |
2f659546 | 144 | file_extent_err(fs_info, leaf, slot, |
8806d718 QW |
145 | "invalid encryption for file extent, have %u expect 0", |
146 | btrfs_file_extent_encryption(leaf, fi)); | |
557ea5dd QW |
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) { | |
2f659546 | 152 | file_extent_err(fs_info, leaf, slot, |
8806d718 QW |
153 | "invalid file_offset for inline file extent, have %llu expect 0", |
154 | key->offset); | |
557ea5dd QW |
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)) { | |
2f659546 | 166 | file_extent_err(fs_info, leaf, slot, |
8806d718 QW |
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)); | |
557ea5dd QW |
170 | return -EUCLEAN; |
171 | } | |
172 | return 0; | |
173 | } | |
174 | ||
175 | /* Regular or preallocated extent has fixed item size */ | |
176 | if (item_size != sizeof(*fi)) { | |
2f659546 | 177 | file_extent_err(fs_info, leaf, slot, |
709a95c3 | 178 | "invalid item size for reg/prealloc file extent, have %u expect %zu", |
8806d718 | 179 | item_size, sizeof(*fi)); |
557ea5dd QW |
180 | return -EUCLEAN; |
181 | } | |
2f659546 QW |
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)) | |
557ea5dd | 187 | return -EUCLEAN; |
557ea5dd QW |
188 | return 0; |
189 | } | |
190 | ||
2f659546 QW |
191 | static int check_csum_item(struct btrfs_fs_info *fs_info, |
192 | struct extent_buffer *leaf, struct btrfs_key *key, | |
193 | int slot) | |
557ea5dd | 194 | { |
2f659546 QW |
195 | u32 sectorsize = fs_info->sectorsize; |
196 | u32 csumsize = btrfs_super_csum_size(fs_info->super_copy); | |
557ea5dd QW |
197 | |
198 | if (key->objectid != BTRFS_EXTENT_CSUM_OBJECTID) { | |
2f659546 | 199 | generic_err(fs_info, leaf, slot, |
d508c5f0 QW |
200 | "invalid key objectid for csum item, have %llu expect %llu", |
201 | key->objectid, BTRFS_EXTENT_CSUM_OBJECTID); | |
557ea5dd QW |
202 | return -EUCLEAN; |
203 | } | |
204 | if (!IS_ALIGNED(key->offset, sectorsize)) { | |
2f659546 | 205 | generic_err(fs_info, leaf, slot, |
d508c5f0 QW |
206 | "unaligned key offset for csum item, have %llu should be aligned to %u", |
207 | key->offset, sectorsize); | |
557ea5dd QW |
208 | return -EUCLEAN; |
209 | } | |
210 | if (!IS_ALIGNED(btrfs_item_size_nr(leaf, slot), csumsize)) { | |
2f659546 | 211 | generic_err(fs_info, leaf, slot, |
d508c5f0 QW |
212 | "unaligned item size for csum item, have %u should be aligned to %u", |
213 | btrfs_item_size_nr(leaf, slot), csumsize); | |
557ea5dd QW |
214 | return -EUCLEAN; |
215 | } | |
216 | return 0; | |
217 | } | |
218 | ||
ad7b0368 QW |
219 | /* |
220 | * Customized reported for dir_item, only important new info is key->objectid, | |
221 | * which represents inode number | |
222 | */ | |
223 | __printf(4, 5) | |
e67c718b | 224 | __cold |
2f659546 | 225 | static void dir_item_err(const struct btrfs_fs_info *fs_info, |
ad7b0368 QW |
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 | ||
2f659546 | 239 | btrfs_crit(fs_info, |
ad7b0368 | 240 | "corrupt %s: root=%llu block=%llu slot=%d ino=%llu, %pV", |
2f659546 QW |
241 | btrfs_header_level(eb) == 0 ? "leaf" : "node", |
242 | btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot, | |
243 | key.objectid, &vaf); | |
ad7b0368 QW |
244 | va_end(args); |
245 | } | |
246 | ||
2f659546 | 247 | static int check_dir_item(struct btrfs_fs_info *fs_info, |
ad7b0368 QW |
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) { | |
ad7b0368 QW |
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) { | |
2f659546 | 266 | dir_item_err(fs_info, leaf, slot, |
7cfad652 | 267 | "dir item header crosses item boundary, have %zu boundary %u", |
ad7b0368 QW |
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) { | |
2f659546 | 275 | dir_item_err(fs_info, leaf, slot, |
ad7b0368 QW |
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) { | |
2f659546 | 283 | dir_item_err(fs_info, leaf, slot, |
ad7b0368 QW |
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) { | |
2f659546 | 290 | dir_item_err(fs_info, leaf, slot, |
ad7b0368 QW |
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) { | |
2f659546 | 303 | dir_item_err(fs_info, leaf, slot, |
ad7b0368 QW |
304 | "dir item name len too long, have %u max %u", |
305 | name_len, max_name_len); | |
306 | return -EUCLEAN; | |
307 | } | |
2f659546 QW |
308 | if (name_len + data_len > BTRFS_MAX_XATTR_SIZE(fs_info)) { |
309 | dir_item_err(fs_info, leaf, slot, | |
ad7b0368 QW |
310 | "dir item name and data len too long, have %u max %u", |
311 | name_len + data_len, | |
2f659546 | 312 | BTRFS_MAX_XATTR_SIZE(fs_info)); |
ad7b0368 QW |
313 | return -EUCLEAN; |
314 | } | |
315 | ||
316 | if (data_len && dir_type != BTRFS_FT_XATTR) { | |
2f659546 | 317 | dir_item_err(fs_info, leaf, slot, |
ad7b0368 QW |
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) { | |
2f659546 | 327 | dir_item_err(fs_info, leaf, slot, |
ad7b0368 QW |
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) { | |
e2683fc9 DS |
339 | char namebuf[max(BTRFS_NAME_LEN, XATTR_NAME_MAX)]; |
340 | ||
ad7b0368 QW |
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) { | |
2f659546 | 345 | dir_item_err(fs_info, leaf, slot, |
ad7b0368 QW |
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 | ||
fce466ea QW |
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 | |
392 | * handle it. We care more about the size, as if one block group is | |
393 | * larger than maximum size, it's must be some obvious corruption. | |
394 | */ | |
395 | if (key->offset > BTRFS_MAX_DATA_CHUNK_SIZE || key->offset == 0) { | |
396 | block_group_err(fs_info, leaf, slot, | |
397 | "invalid block group size, have %llu expect (0, %llu]", | |
398 | key->offset, BTRFS_MAX_DATA_CHUNK_SIZE); | |
399 | return -EUCLEAN; | |
400 | } | |
401 | ||
402 | if (item_size != sizeof(bgi)) { | |
403 | block_group_err(fs_info, leaf, slot, | |
404 | "invalid item size, have %u expect %zu", | |
405 | item_size, sizeof(bgi)); | |
406 | return -EUCLEAN; | |
407 | } | |
408 | ||
409 | read_extent_buffer(leaf, &bgi, btrfs_item_ptr_offset(leaf, slot), | |
410 | sizeof(bgi)); | |
411 | if (btrfs_block_group_chunk_objectid(&bgi) != | |
412 | BTRFS_FIRST_CHUNK_TREE_OBJECTID) { | |
413 | block_group_err(fs_info, leaf, slot, | |
414 | "invalid block group chunk objectid, have %llu expect %llu", | |
415 | btrfs_block_group_chunk_objectid(&bgi), | |
416 | BTRFS_FIRST_CHUNK_TREE_OBJECTID); | |
417 | return -EUCLEAN; | |
418 | } | |
419 | ||
420 | if (btrfs_block_group_used(&bgi) > key->offset) { | |
421 | block_group_err(fs_info, leaf, slot, | |
422 | "invalid block group used, have %llu expect [0, %llu)", | |
423 | btrfs_block_group_used(&bgi), key->offset); | |
424 | return -EUCLEAN; | |
425 | } | |
426 | ||
427 | flags = btrfs_block_group_flags(&bgi); | |
428 | if (hweight64(flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) > 1) { | |
429 | block_group_err(fs_info, leaf, slot, | |
430 | "invalid profile flags, have 0x%llx (%lu bits set) expect no more than 1 bit set", | |
431 | flags & BTRFS_BLOCK_GROUP_PROFILE_MASK, | |
432 | hweight64(flags & BTRFS_BLOCK_GROUP_PROFILE_MASK)); | |
433 | return -EUCLEAN; | |
434 | } | |
435 | ||
436 | type = flags & BTRFS_BLOCK_GROUP_TYPE_MASK; | |
437 | if (type != BTRFS_BLOCK_GROUP_DATA && | |
438 | type != BTRFS_BLOCK_GROUP_METADATA && | |
439 | type != BTRFS_BLOCK_GROUP_SYSTEM && | |
440 | type != (BTRFS_BLOCK_GROUP_METADATA | | |
441 | BTRFS_BLOCK_GROUP_DATA)) { | |
442 | block_group_err(fs_info, leaf, slot, | |
443 | "invalid type, have 0x%llx (%lu bits set) expect either 0x%llx, 0x%llx, 0x%llu or 0x%llx", | |
444 | type, hweight64(type), | |
445 | BTRFS_BLOCK_GROUP_DATA, BTRFS_BLOCK_GROUP_METADATA, | |
446 | BTRFS_BLOCK_GROUP_SYSTEM, | |
447 | BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA); | |
448 | return -EUCLEAN; | |
449 | } | |
450 | return 0; | |
451 | } | |
452 | ||
557ea5dd QW |
453 | /* |
454 | * Common point to switch the item-specific validation. | |
455 | */ | |
2f659546 | 456 | static int check_leaf_item(struct btrfs_fs_info *fs_info, |
557ea5dd QW |
457 | struct extent_buffer *leaf, |
458 | struct btrfs_key *key, int slot) | |
459 | { | |
460 | int ret = 0; | |
461 | ||
462 | switch (key->type) { | |
463 | case BTRFS_EXTENT_DATA_KEY: | |
2f659546 | 464 | ret = check_extent_data_item(fs_info, leaf, key, slot); |
557ea5dd QW |
465 | break; |
466 | case BTRFS_EXTENT_CSUM_KEY: | |
2f659546 | 467 | ret = check_csum_item(fs_info, leaf, key, slot); |
557ea5dd | 468 | break; |
ad7b0368 QW |
469 | case BTRFS_DIR_ITEM_KEY: |
470 | case BTRFS_DIR_INDEX_KEY: | |
471 | case BTRFS_XATTR_ITEM_KEY: | |
2f659546 | 472 | ret = check_dir_item(fs_info, leaf, key, slot); |
ad7b0368 | 473 | break; |
fce466ea QW |
474 | case BTRFS_BLOCK_GROUP_ITEM_KEY: |
475 | ret = check_block_group_item(fs_info, leaf, key, slot); | |
476 | break; | |
557ea5dd QW |
477 | } |
478 | return ret; | |
479 | } | |
480 | ||
2f659546 | 481 | static int check_leaf(struct btrfs_fs_info *fs_info, struct extent_buffer *leaf, |
69fc6cbb | 482 | bool check_item_data) |
557ea5dd | 483 | { |
557ea5dd QW |
484 | /* No valid key type is 0, so all key should be larger than this key */ |
485 | struct btrfs_key prev_key = {0, 0, 0}; | |
486 | struct btrfs_key key; | |
487 | u32 nritems = btrfs_header_nritems(leaf); | |
488 | int slot; | |
489 | ||
490 | /* | |
491 | * Extent buffers from a relocation tree have a owner field that | |
492 | * corresponds to the subvolume tree they are based on. So just from an | |
493 | * extent buffer alone we can not find out what is the id of the | |
494 | * corresponding subvolume tree, so we can not figure out if the extent | |
495 | * buffer corresponds to the root of the relocation tree or not. So | |
496 | * skip this check for relocation trees. | |
497 | */ | |
498 | if (nritems == 0 && !btrfs_header_flag(leaf, BTRFS_HEADER_FLAG_RELOC)) { | |
ba480dd4 | 499 | u64 owner = btrfs_header_owner(leaf); |
557ea5dd QW |
500 | struct btrfs_root *check_root; |
501 | ||
ba480dd4 QW |
502 | /* These trees must never be empty */ |
503 | if (owner == BTRFS_ROOT_TREE_OBJECTID || | |
504 | owner == BTRFS_CHUNK_TREE_OBJECTID || | |
505 | owner == BTRFS_EXTENT_TREE_OBJECTID || | |
506 | owner == BTRFS_DEV_TREE_OBJECTID || | |
507 | owner == BTRFS_FS_TREE_OBJECTID || | |
508 | owner == BTRFS_DATA_RELOC_TREE_OBJECTID) { | |
509 | generic_err(fs_info, leaf, 0, | |
510 | "invalid root, root %llu must never be empty", | |
511 | owner); | |
512 | return -EUCLEAN; | |
513 | } | |
514 | key.objectid = owner; | |
557ea5dd QW |
515 | key.type = BTRFS_ROOT_ITEM_KEY; |
516 | key.offset = (u64)-1; | |
517 | ||
518 | check_root = btrfs_get_fs_root(fs_info, &key, false); | |
519 | /* | |
520 | * The only reason we also check NULL here is that during | |
521 | * open_ctree() some roots has not yet been set up. | |
522 | */ | |
523 | if (!IS_ERR_OR_NULL(check_root)) { | |
524 | struct extent_buffer *eb; | |
525 | ||
526 | eb = btrfs_root_node(check_root); | |
527 | /* if leaf is the root, then it's fine */ | |
528 | if (leaf != eb) { | |
2f659546 | 529 | generic_err(fs_info, leaf, 0, |
478d01b3 QW |
530 | "invalid nritems, have %u should not be 0 for non-root leaf", |
531 | nritems); | |
557ea5dd QW |
532 | free_extent_buffer(eb); |
533 | return -EUCLEAN; | |
534 | } | |
535 | free_extent_buffer(eb); | |
536 | } | |
537 | return 0; | |
538 | } | |
539 | ||
540 | if (nritems == 0) | |
541 | return 0; | |
542 | ||
543 | /* | |
544 | * Check the following things to make sure this is a good leaf, and | |
545 | * leaf users won't need to bother with similar sanity checks: | |
546 | * | |
547 | * 1) key ordering | |
548 | * 2) item offset and size | |
549 | * No overlap, no hole, all inside the leaf. | |
550 | * 3) item content | |
551 | * If possible, do comprehensive sanity check. | |
552 | * NOTE: All checks must only rely on the item data itself. | |
553 | */ | |
554 | for (slot = 0; slot < nritems; slot++) { | |
555 | u32 item_end_expected; | |
556 | int ret; | |
557 | ||
558 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
559 | ||
560 | /* Make sure the keys are in the right order */ | |
561 | if (btrfs_comp_cpu_keys(&prev_key, &key) >= 0) { | |
2f659546 | 562 | generic_err(fs_info, leaf, slot, |
478d01b3 QW |
563 | "bad key order, prev (%llu %u %llu) current (%llu %u %llu)", |
564 | prev_key.objectid, prev_key.type, | |
565 | prev_key.offset, key.objectid, key.type, | |
566 | key.offset); | |
557ea5dd QW |
567 | return -EUCLEAN; |
568 | } | |
569 | ||
570 | /* | |
571 | * Make sure the offset and ends are right, remember that the | |
572 | * item data starts at the end of the leaf and grows towards the | |
573 | * front. | |
574 | */ | |
575 | if (slot == 0) | |
576 | item_end_expected = BTRFS_LEAF_DATA_SIZE(fs_info); | |
577 | else | |
578 | item_end_expected = btrfs_item_offset_nr(leaf, | |
579 | slot - 1); | |
580 | if (btrfs_item_end_nr(leaf, slot) != item_end_expected) { | |
2f659546 | 581 | generic_err(fs_info, leaf, slot, |
478d01b3 QW |
582 | "unexpected item end, have %u expect %u", |
583 | btrfs_item_end_nr(leaf, slot), | |
584 | item_end_expected); | |
557ea5dd QW |
585 | return -EUCLEAN; |
586 | } | |
587 | ||
588 | /* | |
589 | * Check to make sure that we don't point outside of the leaf, | |
590 | * just in case all the items are consistent to each other, but | |
591 | * all point outside of the leaf. | |
592 | */ | |
593 | if (btrfs_item_end_nr(leaf, slot) > | |
594 | BTRFS_LEAF_DATA_SIZE(fs_info)) { | |
2f659546 | 595 | generic_err(fs_info, leaf, slot, |
478d01b3 QW |
596 | "slot end outside of leaf, have %u expect range [0, %u]", |
597 | btrfs_item_end_nr(leaf, slot), | |
598 | BTRFS_LEAF_DATA_SIZE(fs_info)); | |
557ea5dd QW |
599 | return -EUCLEAN; |
600 | } | |
601 | ||
602 | /* Also check if the item pointer overlaps with btrfs item. */ | |
603 | if (btrfs_item_nr_offset(slot) + sizeof(struct btrfs_item) > | |
604 | btrfs_item_ptr_offset(leaf, slot)) { | |
2f659546 | 605 | generic_err(fs_info, leaf, slot, |
478d01b3 QW |
606 | "slot overlaps with its data, item end %lu data start %lu", |
607 | btrfs_item_nr_offset(slot) + | |
608 | sizeof(struct btrfs_item), | |
609 | btrfs_item_ptr_offset(leaf, slot)); | |
557ea5dd QW |
610 | return -EUCLEAN; |
611 | } | |
612 | ||
69fc6cbb QW |
613 | if (check_item_data) { |
614 | /* | |
615 | * Check if the item size and content meet other | |
616 | * criteria | |
617 | */ | |
2f659546 | 618 | ret = check_leaf_item(fs_info, leaf, &key, slot); |
69fc6cbb QW |
619 | if (ret < 0) |
620 | return ret; | |
621 | } | |
557ea5dd QW |
622 | |
623 | prev_key.objectid = key.objectid; | |
624 | prev_key.type = key.type; | |
625 | prev_key.offset = key.offset; | |
626 | } | |
627 | ||
628 | return 0; | |
629 | } | |
630 | ||
2f659546 QW |
631 | int btrfs_check_leaf_full(struct btrfs_fs_info *fs_info, |
632 | struct extent_buffer *leaf) | |
69fc6cbb | 633 | { |
2f659546 | 634 | return check_leaf(fs_info, leaf, true); |
69fc6cbb QW |
635 | } |
636 | ||
2f659546 | 637 | int btrfs_check_leaf_relaxed(struct btrfs_fs_info *fs_info, |
69fc6cbb QW |
638 | struct extent_buffer *leaf) |
639 | { | |
2f659546 | 640 | return check_leaf(fs_info, leaf, false); |
69fc6cbb QW |
641 | } |
642 | ||
2f659546 | 643 | int btrfs_check_node(struct btrfs_fs_info *fs_info, struct extent_buffer *node) |
557ea5dd QW |
644 | { |
645 | unsigned long nr = btrfs_header_nritems(node); | |
646 | struct btrfs_key key, next_key; | |
647 | int slot; | |
648 | u64 bytenr; | |
649 | int ret = 0; | |
650 | ||
2f659546 QW |
651 | if (nr == 0 || nr > BTRFS_NODEPTRS_PER_BLOCK(fs_info)) { |
652 | btrfs_crit(fs_info, | |
bba4f298 | 653 | "corrupt node: root=%llu block=%llu, nritems too %s, have %lu expect range [1,%u]", |
2f659546 | 654 | btrfs_header_owner(node), node->start, |
bba4f298 | 655 | nr == 0 ? "small" : "large", nr, |
2f659546 | 656 | BTRFS_NODEPTRS_PER_BLOCK(fs_info)); |
bba4f298 | 657 | return -EUCLEAN; |
557ea5dd QW |
658 | } |
659 | ||
660 | for (slot = 0; slot < nr - 1; slot++) { | |
661 | bytenr = btrfs_node_blockptr(node, slot); | |
662 | btrfs_node_key_to_cpu(node, &key, slot); | |
663 | btrfs_node_key_to_cpu(node, &next_key, slot + 1); | |
664 | ||
665 | if (!bytenr) { | |
2f659546 | 666 | generic_err(fs_info, node, slot, |
bba4f298 QW |
667 | "invalid NULL node pointer"); |
668 | ret = -EUCLEAN; | |
669 | goto out; | |
670 | } | |
2f659546 QW |
671 | if (!IS_ALIGNED(bytenr, fs_info->sectorsize)) { |
672 | generic_err(fs_info, node, slot, | |
bba4f298 | 673 | "unaligned pointer, have %llu should be aligned to %u", |
2f659546 | 674 | bytenr, fs_info->sectorsize); |
bba4f298 | 675 | ret = -EUCLEAN; |
557ea5dd QW |
676 | goto out; |
677 | } | |
678 | ||
679 | if (btrfs_comp_cpu_keys(&key, &next_key) >= 0) { | |
2f659546 | 680 | generic_err(fs_info, node, slot, |
bba4f298 QW |
681 | "bad key order, current (%llu %u %llu) next (%llu %u %llu)", |
682 | key.objectid, key.type, key.offset, | |
683 | next_key.objectid, next_key.type, | |
684 | next_key.offset); | |
685 | ret = -EUCLEAN; | |
557ea5dd QW |
686 | goto out; |
687 | } | |
688 | } | |
689 | out: | |
690 | return ret; | |
691 | } |