Merge tag 'linux-kselftest-next-6.6-rc2' of git://git.kernel.org/pub/scm/linux/kernel...
[linux-block.git] / fs / btrfs / tree-checker.c
CommitLineData
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
02529d7a
QW
18#include <linux/types.h>
19#include <linux/stddef.h>
20#include <linux/error-injection.h>
9b569ea0 21#include "messages.h"
557ea5dd
QW
22#include "ctree.h"
23#include "tree-checker.h"
24#include "disk-io.h"
25#include "compression.h"
fce466ea 26#include "volumes.h"
c1499166 27#include "misc.h"
c7f13d42 28#include "fs.h"
07e81dc9 29#include "accessors.h"
6bfd0ffa 30#include "file-item.h"
f541833c 31#include "inode-item.h"
557ea5dd 32
bba4f298
QW
33/*
34 * Error message should follow the following format:
35 * corrupt <type>: <identifier>, <reason>[, <bad_value>]
36 *
37 * @type: leaf or node
38 * @identifier: the necessary info to locate the leaf/node.
52042d8e 39 * It's recommended to decode key.objecitd/offset if it's
bba4f298
QW
40 * meaningful.
41 * @reason: describe the error
52042d8e 42 * @bad_value: optional, it's recommended to output bad value and its
bba4f298
QW
43 * expected value (range).
44 *
45 * Since comma is used to separate the components, only space is allowed
46 * inside each component.
47 */
48
49/*
50 * Append generic "corrupt leaf/node root=%llu block=%llu slot=%d: " to @fmt.
51 * Allows callers to customize the output.
52 */
86a6be3a 53__printf(3, 4)
e67c718b 54__cold
86a6be3a 55static void generic_err(const struct extent_buffer *eb, int slot,
bba4f298
QW
56 const char *fmt, ...)
57{
86a6be3a 58 const struct btrfs_fs_info *fs_info = eb->fs_info;
bba4f298
QW
59 struct va_format vaf;
60 va_list args;
61
62 va_start(args, fmt);
63
64 vaf.fmt = fmt;
65 vaf.va = &args;
66
2f659546 67 btrfs_crit(fs_info,
bba4f298
QW
68 "corrupt %s: root=%llu block=%llu slot=%d, %pV",
69 btrfs_header_level(eb) == 0 ? "leaf" : "node",
2f659546 70 btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot, &vaf);
bba4f298
QW
71 va_end(args);
72}
73
8806d718
QW
74/*
75 * Customized reporter for extent data item, since its key objectid and
76 * offset has its own meaning.
77 */
1fd715ff 78__printf(3, 4)
e67c718b 79__cold
1fd715ff 80static void file_extent_err(const struct extent_buffer *eb, int slot,
8806d718
QW
81 const char *fmt, ...)
82{
1fd715ff 83 const struct btrfs_fs_info *fs_info = eb->fs_info;
8806d718
QW
84 struct btrfs_key key;
85 struct va_format vaf;
86 va_list args;
87
88 btrfs_item_key_to_cpu(eb, &key, slot);
89 va_start(args, fmt);
90
91 vaf.fmt = fmt;
92 vaf.va = &args;
93
2f659546 94 btrfs_crit(fs_info,
8806d718 95 "corrupt %s: root=%llu block=%llu slot=%d ino=%llu file_offset=%llu, %pV",
2f659546
QW
96 btrfs_header_level(eb) == 0 ? "leaf" : "node",
97 btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot,
98 key.objectid, key.offset, &vaf);
8806d718
QW
99 va_end(args);
100}
101
102/*
103 * Return 0 if the btrfs_file_extent_##name is aligned to @alignment
104 * Else return 1
105 */
033774dc 106#define CHECK_FE_ALIGNED(leaf, slot, fi, name, alignment) \
8806d718 107({ \
c7c01a4a
DS
108 if (unlikely(!IS_ALIGNED(btrfs_file_extent_##name((leaf), (fi)), \
109 (alignment)))) \
1fd715ff 110 file_extent_err((leaf), (slot), \
8806d718
QW
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
4e9845ef
FM
117static u64 file_extent_end(struct extent_buffer *leaf,
118 struct btrfs_key *key,
119 struct btrfs_file_extent_item *extent)
120{
121 u64 end;
122 u64 len;
123
124 if (btrfs_file_extent_type(leaf, extent) == BTRFS_FILE_EXTENT_INLINE) {
125 len = btrfs_file_extent_ram_bytes(leaf, extent);
126 end = ALIGN(key->offset + len, leaf->fs_info->sectorsize);
127 } else {
128 len = btrfs_file_extent_num_bytes(leaf, extent);
129 end = key->offset + len;
130 }
131 return end;
132}
133
80d7fd1e
QW
134/*
135 * Customized report for dir_item, the only new important information is
136 * key->objectid, which represents inode number
137 */
138__printf(3, 4)
139__cold
140static void dir_item_err(const struct extent_buffer *eb, int slot,
141 const char *fmt, ...)
142{
143 const struct btrfs_fs_info *fs_info = eb->fs_info;
144 struct btrfs_key key;
145 struct va_format vaf;
146 va_list args;
147
148 btrfs_item_key_to_cpu(eb, &key, slot);
149 va_start(args, fmt);
150
151 vaf.fmt = fmt;
152 vaf.va = &args;
153
154 btrfs_crit(fs_info,
155 "corrupt %s: root=%llu block=%llu slot=%d ino=%llu, %pV",
156 btrfs_header_level(eb) == 0 ? "leaf" : "node",
157 btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot,
158 key.objectid, &vaf);
159 va_end(args);
160}
161
162/*
163 * This functions checks prev_key->objectid, to ensure current key and prev_key
164 * share the same objectid as inode number.
165 *
166 * This is to detect missing INODE_ITEM in subvolume trees.
167 *
168 * Return true if everything is OK or we don't need to check.
169 * Return false if anything is wrong.
170 */
171static bool check_prev_ino(struct extent_buffer *leaf,
172 struct btrfs_key *key, int slot,
173 struct btrfs_key *prev_key)
174{
175 /* No prev key, skip check */
176 if (slot == 0)
177 return true;
178
179 /* Only these key->types needs to be checked */
180 ASSERT(key->type == BTRFS_XATTR_ITEM_KEY ||
181 key->type == BTRFS_INODE_REF_KEY ||
182 key->type == BTRFS_DIR_INDEX_KEY ||
183 key->type == BTRFS_DIR_ITEM_KEY ||
184 key->type == BTRFS_EXTENT_DATA_KEY);
185
186 /*
187 * Only subvolume trees along with their reloc trees need this check.
188 * Things like log tree doesn't follow this ino requirement.
189 */
190 if (!is_fstree(btrfs_header_owner(leaf)))
191 return true;
192
193 if (key->objectid == prev_key->objectid)
194 return true;
195
196 /* Error found */
197 dir_item_err(leaf, slot,
198 "invalid previous key objectid, have %llu expect %llu",
199 prev_key->objectid, key->objectid);
200 return false;
201}
ae2a19d8 202static int check_extent_data_item(struct extent_buffer *leaf,
4e9845ef
FM
203 struct btrfs_key *key, int slot,
204 struct btrfs_key *prev_key)
557ea5dd 205{
ae2a19d8 206 struct btrfs_fs_info *fs_info = leaf->fs_info;
557ea5dd 207 struct btrfs_file_extent_item *fi;
2f659546 208 u32 sectorsize = fs_info->sectorsize;
3212fa14 209 u32 item_size = btrfs_item_size(leaf, slot);
4c094c33 210 u64 extent_end;
557ea5dd 211
c7c01a4a 212 if (unlikely(!IS_ALIGNED(key->offset, sectorsize))) {
1fd715ff 213 file_extent_err(leaf, slot,
8806d718
QW
214"unaligned file_offset for file extent, have %llu should be aligned to %u",
215 key->offset, sectorsize);
557ea5dd
QW
216 return -EUCLEAN;
217 }
218
c18679eb
QW
219 /*
220 * Previous key must have the same key->objectid (ino).
221 * It can be XATTR_ITEM, INODE_ITEM or just another EXTENT_DATA.
222 * But if objectids mismatch, it means we have a missing
223 * INODE_ITEM.
224 */
c7c01a4a 225 if (unlikely(!check_prev_ino(leaf, key, slot, prev_key)))
c18679eb 226 return -EUCLEAN;
c18679eb 227
557ea5dd
QW
228 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
229
153a6d29
QW
230 /*
231 * Make sure the item contains at least inline header, so the file
232 * extent type is not some garbage.
233 */
c7c01a4a 234 if (unlikely(item_size < BTRFS_FILE_EXTENT_INLINE_DATA_START)) {
153a6d29 235 file_extent_err(leaf, slot,
994bf9cd 236 "invalid item size, have %u expect [%zu, %u)",
153a6d29
QW
237 item_size, BTRFS_FILE_EXTENT_INLINE_DATA_START,
238 SZ_4K);
239 return -EUCLEAN;
240 }
c7c01a4a
DS
241 if (unlikely(btrfs_file_extent_type(leaf, fi) >=
242 BTRFS_NR_FILE_EXTENT_TYPES)) {
1fd715ff 243 file_extent_err(leaf, slot,
8806d718
QW
244 "invalid type for file extent, have %u expect range [0, %u]",
245 btrfs_file_extent_type(leaf, fi),
b9b1a53e 246 BTRFS_NR_FILE_EXTENT_TYPES - 1);
557ea5dd
QW
247 return -EUCLEAN;
248 }
249
250 /*
52042d8e 251 * Support for new compression/encryption must introduce incompat flag,
557ea5dd
QW
252 * and must be caught in open_ctree().
253 */
c7c01a4a
DS
254 if (unlikely(btrfs_file_extent_compression(leaf, fi) >=
255 BTRFS_NR_COMPRESS_TYPES)) {
1fd715ff 256 file_extent_err(leaf, slot,
8806d718
QW
257 "invalid compression for file extent, have %u expect range [0, %u]",
258 btrfs_file_extent_compression(leaf, fi),
ce96b7ff 259 BTRFS_NR_COMPRESS_TYPES - 1);
557ea5dd
QW
260 return -EUCLEAN;
261 }
c7c01a4a 262 if (unlikely(btrfs_file_extent_encryption(leaf, fi))) {
1fd715ff 263 file_extent_err(leaf, slot,
8806d718
QW
264 "invalid encryption for file extent, have %u expect 0",
265 btrfs_file_extent_encryption(leaf, fi));
557ea5dd
QW
266 return -EUCLEAN;
267 }
268 if (btrfs_file_extent_type(leaf, fi) == BTRFS_FILE_EXTENT_INLINE) {
269 /* Inline extent must have 0 as key offset */
c7c01a4a 270 if (unlikely(key->offset)) {
1fd715ff 271 file_extent_err(leaf, slot,
8806d718
QW
272 "invalid file_offset for inline file extent, have %llu expect 0",
273 key->offset);
557ea5dd
QW
274 return -EUCLEAN;
275 }
276
277 /* Compressed inline extent has no on-disk size, skip it */
278 if (btrfs_file_extent_compression(leaf, fi) !=
279 BTRFS_COMPRESS_NONE)
280 return 0;
281
282 /* Uncompressed inline extent size must match item size */
c7c01a4a
DS
283 if (unlikely(item_size != BTRFS_FILE_EXTENT_INLINE_DATA_START +
284 btrfs_file_extent_ram_bytes(leaf, fi))) {
1fd715ff 285 file_extent_err(leaf, slot,
8806d718
QW
286 "invalid ram_bytes for uncompressed inline extent, have %u expect %llu",
287 item_size, BTRFS_FILE_EXTENT_INLINE_DATA_START +
288 btrfs_file_extent_ram_bytes(leaf, fi));
557ea5dd
QW
289 return -EUCLEAN;
290 }
291 return 0;
292 }
293
294 /* Regular or preallocated extent has fixed item size */
c7c01a4a 295 if (unlikely(item_size != sizeof(*fi))) {
1fd715ff 296 file_extent_err(leaf, slot,
709a95c3 297 "invalid item size for reg/prealloc file extent, have %u expect %zu",
8806d718 298 item_size, sizeof(*fi));
557ea5dd
QW
299 return -EUCLEAN;
300 }
c7c01a4a
DS
301 if (unlikely(CHECK_FE_ALIGNED(leaf, slot, fi, ram_bytes, sectorsize) ||
302 CHECK_FE_ALIGNED(leaf, slot, fi, disk_bytenr, sectorsize) ||
303 CHECK_FE_ALIGNED(leaf, slot, fi, disk_num_bytes, sectorsize) ||
304 CHECK_FE_ALIGNED(leaf, slot, fi, offset, sectorsize) ||
305 CHECK_FE_ALIGNED(leaf, slot, fi, num_bytes, sectorsize)))
557ea5dd 306 return -EUCLEAN;
4e9845ef 307
4c094c33 308 /* Catch extent end overflow */
c7c01a4a
DS
309 if (unlikely(check_add_overflow(btrfs_file_extent_num_bytes(leaf, fi),
310 key->offset, &extent_end))) {
4c094c33
QW
311 file_extent_err(leaf, slot,
312 "extent end overflow, have file offset %llu extent num bytes %llu",
313 key->offset,
314 btrfs_file_extent_num_bytes(leaf, fi));
315 return -EUCLEAN;
316 }
317
4e9845ef
FM
318 /*
319 * Check that no two consecutive file extent items, in the same leaf,
320 * present ranges that overlap each other.
321 */
322 if (slot > 0 &&
323 prev_key->objectid == key->objectid &&
324 prev_key->type == BTRFS_EXTENT_DATA_KEY) {
325 struct btrfs_file_extent_item *prev_fi;
326 u64 prev_end;
327
328 prev_fi = btrfs_item_ptr(leaf, slot - 1,
329 struct btrfs_file_extent_item);
330 prev_end = file_extent_end(leaf, prev_key, prev_fi);
c7c01a4a 331 if (unlikely(prev_end > key->offset)) {
4e9845ef
FM
332 file_extent_err(leaf, slot - 1,
333"file extent end range (%llu) goes beyond start offset (%llu) of the next file extent",
334 prev_end, key->offset);
335 return -EUCLEAN;
336 }
337 }
338
557ea5dd
QW
339 return 0;
340}
341
68128ce7 342static int check_csum_item(struct extent_buffer *leaf, struct btrfs_key *key,
ad1d8c43 343 int slot, struct btrfs_key *prev_key)
557ea5dd 344{
68128ce7 345 struct btrfs_fs_info *fs_info = leaf->fs_info;
2f659546 346 u32 sectorsize = fs_info->sectorsize;
223486c2 347 const u32 csumsize = fs_info->csum_size;
557ea5dd 348
c7c01a4a 349 if (unlikely(key->objectid != BTRFS_EXTENT_CSUM_OBJECTID)) {
86a6be3a 350 generic_err(leaf, slot,
d508c5f0
QW
351 "invalid key objectid for csum item, have %llu expect %llu",
352 key->objectid, BTRFS_EXTENT_CSUM_OBJECTID);
557ea5dd
QW
353 return -EUCLEAN;
354 }
c7c01a4a 355 if (unlikely(!IS_ALIGNED(key->offset, sectorsize))) {
86a6be3a 356 generic_err(leaf, slot,
d508c5f0
QW
357 "unaligned key offset for csum item, have %llu should be aligned to %u",
358 key->offset, sectorsize);
557ea5dd
QW
359 return -EUCLEAN;
360 }
3212fa14 361 if (unlikely(!IS_ALIGNED(btrfs_item_size(leaf, slot), csumsize))) {
86a6be3a 362 generic_err(leaf, slot,
d508c5f0 363 "unaligned item size for csum item, have %u should be aligned to %u",
3212fa14 364 btrfs_item_size(leaf, slot), csumsize);
557ea5dd
QW
365 return -EUCLEAN;
366 }
ad1d8c43
FM
367 if (slot > 0 && prev_key->type == BTRFS_EXTENT_CSUM_KEY) {
368 u64 prev_csum_end;
369 u32 prev_item_size;
370
3212fa14 371 prev_item_size = btrfs_item_size(leaf, slot - 1);
ad1d8c43
FM
372 prev_csum_end = (prev_item_size / csumsize) * sectorsize;
373 prev_csum_end += prev_key->offset;
c7c01a4a 374 if (unlikely(prev_csum_end > key->offset)) {
ad1d8c43
FM
375 generic_err(leaf, slot - 1,
376"csum end range (%llu) goes beyond the start range (%llu) of the next csum item",
377 prev_csum_end, key->offset);
378 return -EUCLEAN;
379 }
380 }
557ea5dd
QW
381 return 0;
382}
383
c23c77b0
QW
384/* Inode item error output has the same format as dir_item_err() */
385#define inode_item_err(eb, slot, fmt, ...) \
386 dir_item_err(eb, slot, fmt, __VA_ARGS__)
387
388static int check_inode_key(struct extent_buffer *leaf, struct btrfs_key *key,
389 int slot)
390{
391 struct btrfs_key item_key;
392 bool is_inode_item;
393
394 btrfs_item_key_to_cpu(leaf, &item_key, slot);
395 is_inode_item = (item_key.type == BTRFS_INODE_ITEM_KEY);
396
397 /* For XATTR_ITEM, location key should be all 0 */
398 if (item_key.type == BTRFS_XATTR_ITEM_KEY) {
c7c01a4a
DS
399 if (unlikely(key->objectid != 0 || key->type != 0 ||
400 key->offset != 0))
c23c77b0
QW
401 return -EUCLEAN;
402 return 0;
403 }
404
c7c01a4a
DS
405 if (unlikely((key->objectid < BTRFS_FIRST_FREE_OBJECTID ||
406 key->objectid > BTRFS_LAST_FREE_OBJECTID) &&
407 key->objectid != BTRFS_ROOT_TREE_DIR_OBJECTID &&
408 key->objectid != BTRFS_FREE_INO_OBJECTID)) {
c23c77b0
QW
409 if (is_inode_item) {
410 generic_err(leaf, slot,
411 "invalid key objectid: has %llu expect %llu or [%llu, %llu] or %llu",
412 key->objectid, BTRFS_ROOT_TREE_DIR_OBJECTID,
413 BTRFS_FIRST_FREE_OBJECTID,
414 BTRFS_LAST_FREE_OBJECTID,
415 BTRFS_FREE_INO_OBJECTID);
416 } else {
417 dir_item_err(leaf, slot,
418"invalid location key objectid: has %llu expect %llu or [%llu, %llu] or %llu",
419 key->objectid, BTRFS_ROOT_TREE_DIR_OBJECTID,
420 BTRFS_FIRST_FREE_OBJECTID,
421 BTRFS_LAST_FREE_OBJECTID,
422 BTRFS_FREE_INO_OBJECTID);
423 }
424 return -EUCLEAN;
425 }
c7c01a4a 426 if (unlikely(key->offset != 0)) {
c23c77b0
QW
427 if (is_inode_item)
428 inode_item_err(leaf, slot,
429 "invalid key offset: has %llu expect 0",
430 key->offset);
431 else
432 dir_item_err(leaf, slot,
433 "invalid location key offset:has %llu expect 0",
434 key->offset);
435 return -EUCLEAN;
436 }
437 return 0;
438}
439
57a0e674
QW
440static int check_root_key(struct extent_buffer *leaf, struct btrfs_key *key,
441 int slot)
442{
443 struct btrfs_key item_key;
444 bool is_root_item;
445
446 btrfs_item_key_to_cpu(leaf, &item_key, slot);
447 is_root_item = (item_key.type == BTRFS_ROOT_ITEM_KEY);
448
6ebcd021
QW
449 /*
450 * Bad rootid for reloc trees.
451 *
452 * Reloc trees are only for subvolume trees, other trees only need
453 * to be COWed to be relocated.
454 */
455 if (unlikely(is_root_item && key->objectid == BTRFS_TREE_RELOC_OBJECTID &&
456 !is_fstree(key->offset))) {
457 generic_err(leaf, slot,
458 "invalid reloc tree for root %lld, root id is not a subvolume tree",
459 key->offset);
460 return -EUCLEAN;
461 }
462
57a0e674 463 /* No such tree id */
c7c01a4a 464 if (unlikely(key->objectid == 0)) {
57a0e674
QW
465 if (is_root_item)
466 generic_err(leaf, slot, "invalid root id 0");
467 else
468 dir_item_err(leaf, slot,
469 "invalid location key root id 0");
470 return -EUCLEAN;
471 }
472
473 /* DIR_ITEM/INDEX/INODE_REF is not allowed to point to non-fs trees */
c7c01a4a 474 if (unlikely(!is_fstree(key->objectid) && !is_root_item)) {
57a0e674
QW
475 dir_item_err(leaf, slot,
476 "invalid location key objectid, have %llu expect [%llu, %llu]",
477 key->objectid, BTRFS_FIRST_FREE_OBJECTID,
478 BTRFS_LAST_FREE_OBJECTID);
479 return -EUCLEAN;
480 }
481
482 /*
483 * ROOT_ITEM with non-zero offset means this is a snapshot, created at
484 * @offset transid.
485 * Furthermore, for location key in DIR_ITEM, its offset is always -1.
486 *
487 * So here we only check offset for reloc tree whose key->offset must
488 * be a valid tree.
489 */
c7c01a4a
DS
490 if (unlikely(key->objectid == BTRFS_TREE_RELOC_OBJECTID &&
491 key->offset == 0)) {
57a0e674
QW
492 generic_err(leaf, slot, "invalid root id 0 for reloc tree");
493 return -EUCLEAN;
494 }
495 return 0;
496}
497
ce4252c0 498static int check_dir_item(struct extent_buffer *leaf,
c18679eb
QW
499 struct btrfs_key *key, struct btrfs_key *prev_key,
500 int slot)
ad7b0368 501{
ce4252c0 502 struct btrfs_fs_info *fs_info = leaf->fs_info;
ad7b0368 503 struct btrfs_dir_item *di;
3212fa14 504 u32 item_size = btrfs_item_size(leaf, slot);
ad7b0368
QW
505 u32 cur = 0;
506
c7c01a4a 507 if (unlikely(!check_prev_ino(leaf, key, slot, prev_key)))
c18679eb 508 return -EUCLEAN;
c7c01a4a 509
ad7b0368
QW
510 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
511 while (cur < item_size) {
147a097c 512 struct btrfs_key location_key;
ad7b0368
QW
513 u32 name_len;
514 u32 data_len;
515 u32 max_name_len;
516 u32 total_size;
517 u32 name_hash;
518 u8 dir_type;
147a097c 519 int ret;
ad7b0368
QW
520
521 /* header itself should not cross item boundary */
c7c01a4a 522 if (unlikely(cur + sizeof(*di) > item_size)) {
d98ced68 523 dir_item_err(leaf, slot,
7cfad652 524 "dir item header crosses item boundary, have %zu boundary %u",
ad7b0368
QW
525 cur + sizeof(*di), item_size);
526 return -EUCLEAN;
527 }
528
147a097c
QW
529 /* Location key check */
530 btrfs_dir_item_key_to_cpu(leaf, di, &location_key);
531 if (location_key.type == BTRFS_ROOT_ITEM_KEY) {
532 ret = check_root_key(leaf, &location_key, slot);
c7c01a4a 533 if (unlikely(ret < 0))
147a097c
QW
534 return ret;
535 } else if (location_key.type == BTRFS_INODE_ITEM_KEY ||
536 location_key.type == 0) {
537 ret = check_inode_key(leaf, &location_key, slot);
c7c01a4a 538 if (unlikely(ret < 0))
147a097c
QW
539 return ret;
540 } else {
541 dir_item_err(leaf, slot,
542 "invalid location key type, have %u, expect %u or %u",
543 location_key.type, BTRFS_ROOT_ITEM_KEY,
544 BTRFS_INODE_ITEM_KEY);
545 return -EUCLEAN;
546 }
547
ad7b0368 548 /* dir type check */
94a48aef 549 dir_type = btrfs_dir_ftype(leaf, di);
c7c01a4a 550 if (unlikely(dir_type >= BTRFS_FT_MAX)) {
d98ced68 551 dir_item_err(leaf, slot,
ad7b0368
QW
552 "invalid dir item type, have %u expect [0, %u)",
553 dir_type, BTRFS_FT_MAX);
554 return -EUCLEAN;
555 }
556
c7c01a4a
DS
557 if (unlikely(key->type == BTRFS_XATTR_ITEM_KEY &&
558 dir_type != BTRFS_FT_XATTR)) {
d98ced68 559 dir_item_err(leaf, slot,
ad7b0368
QW
560 "invalid dir item type for XATTR key, have %u expect %u",
561 dir_type, BTRFS_FT_XATTR);
562 return -EUCLEAN;
563 }
c7c01a4a
DS
564 if (unlikely(dir_type == BTRFS_FT_XATTR &&
565 key->type != BTRFS_XATTR_ITEM_KEY)) {
d98ced68 566 dir_item_err(leaf, slot,
ad7b0368
QW
567 "xattr dir type found for non-XATTR key");
568 return -EUCLEAN;
569 }
570 if (dir_type == BTRFS_FT_XATTR)
571 max_name_len = XATTR_NAME_MAX;
572 else
573 max_name_len = BTRFS_NAME_LEN;
574
575 /* Name/data length check */
576 name_len = btrfs_dir_name_len(leaf, di);
577 data_len = btrfs_dir_data_len(leaf, di);
c7c01a4a 578 if (unlikely(name_len > max_name_len)) {
d98ced68 579 dir_item_err(leaf, slot,
ad7b0368
QW
580 "dir item name len too long, have %u max %u",
581 name_len, max_name_len);
582 return -EUCLEAN;
583 }
c7c01a4a 584 if (unlikely(name_len + data_len > BTRFS_MAX_XATTR_SIZE(fs_info))) {
d98ced68 585 dir_item_err(leaf, slot,
ad7b0368
QW
586 "dir item name and data len too long, have %u max %u",
587 name_len + data_len,
2f659546 588 BTRFS_MAX_XATTR_SIZE(fs_info));
ad7b0368
QW
589 return -EUCLEAN;
590 }
591
c7c01a4a 592 if (unlikely(data_len && dir_type != BTRFS_FT_XATTR)) {
d98ced68 593 dir_item_err(leaf, slot,
ad7b0368
QW
594 "dir item with invalid data len, have %u expect 0",
595 data_len);
596 return -EUCLEAN;
597 }
598
599 total_size = sizeof(*di) + name_len + data_len;
600
601 /* header and name/data should not cross item boundary */
c7c01a4a 602 if (unlikely(cur + total_size > item_size)) {
d98ced68 603 dir_item_err(leaf, slot,
ad7b0368
QW
604 "dir item data crosses item boundary, have %u boundary %u",
605 cur + total_size, item_size);
606 return -EUCLEAN;
607 }
608
609 /*
610 * Special check for XATTR/DIR_ITEM, as key->offset is name
611 * hash, should match its name
612 */
613 if (key->type == BTRFS_DIR_ITEM_KEY ||
614 key->type == BTRFS_XATTR_ITEM_KEY) {
e2683fc9
DS
615 char namebuf[max(BTRFS_NAME_LEN, XATTR_NAME_MAX)];
616
ad7b0368
QW
617 read_extent_buffer(leaf, namebuf,
618 (unsigned long)(di + 1), name_len);
619 name_hash = btrfs_name_hash(namebuf, name_len);
c7c01a4a 620 if (unlikely(key->offset != name_hash)) {
d98ced68 621 dir_item_err(leaf, slot,
ad7b0368
QW
622 "name hash mismatch with key, have 0x%016x expect 0x%016llx",
623 name_hash, key->offset);
624 return -EUCLEAN;
625 }
626 }
627 cur += total_size;
628 di = (struct btrfs_dir_item *)((void *)di + total_size);
629 }
630 return 0;
631}
632
4806bd88 633__printf(3, 4)
fce466ea 634__cold
4806bd88 635static void block_group_err(const struct extent_buffer *eb, int slot,
fce466ea
QW
636 const char *fmt, ...)
637{
4806bd88 638 const struct btrfs_fs_info *fs_info = eb->fs_info;
fce466ea
QW
639 struct btrfs_key key;
640 struct va_format vaf;
641 va_list args;
642
643 btrfs_item_key_to_cpu(eb, &key, slot);
644 va_start(args, fmt);
645
646 vaf.fmt = fmt;
647 vaf.va = &args;
648
649 btrfs_crit(fs_info,
650 "corrupt %s: root=%llu block=%llu slot=%d bg_start=%llu bg_len=%llu, %pV",
651 btrfs_header_level(eb) == 0 ? "leaf" : "node",
652 btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot,
653 key.objectid, key.offset, &vaf);
654 va_end(args);
655}
656
af60ce2b 657static int check_block_group_item(struct extent_buffer *leaf,
fce466ea
QW
658 struct btrfs_key *key, int slot)
659{
f7238e50 660 struct btrfs_fs_info *fs_info = leaf->fs_info;
fce466ea 661 struct btrfs_block_group_item bgi;
3212fa14 662 u32 item_size = btrfs_item_size(leaf, slot);
f7238e50 663 u64 chunk_objectid;
fce466ea
QW
664 u64 flags;
665 u64 type;
666
667 /*
668 * Here we don't really care about alignment since extent allocator can
10950929 669 * handle it. We care more about the size.
fce466ea 670 */
c7c01a4a 671 if (unlikely(key->offset == 0)) {
4806bd88 672 block_group_err(leaf, slot,
10950929 673 "invalid block group size 0");
fce466ea
QW
674 return -EUCLEAN;
675 }
676
c7c01a4a 677 if (unlikely(item_size != sizeof(bgi))) {
4806bd88 678 block_group_err(leaf, slot,
fce466ea
QW
679 "invalid item size, have %u expect %zu",
680 item_size, sizeof(bgi));
681 return -EUCLEAN;
682 }
683
684 read_extent_buffer(leaf, &bgi, btrfs_item_ptr_offset(leaf, slot),
685 sizeof(bgi));
f7238e50
JB
686 chunk_objectid = btrfs_stack_block_group_chunk_objectid(&bgi);
687 if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) {
688 /*
689 * We don't init the nr_global_roots until we load the global
690 * roots, so this could be 0 at mount time. If it's 0 we'll
691 * just assume we're fine, and later we'll check against our
692 * actual value.
693 */
694 if (unlikely(fs_info->nr_global_roots &&
695 chunk_objectid >= fs_info->nr_global_roots)) {
696 block_group_err(leaf, slot,
697 "invalid block group global root id, have %llu, needs to be <= %llu",
698 chunk_objectid,
699 fs_info->nr_global_roots);
700 return -EUCLEAN;
701 }
702 } else if (unlikely(chunk_objectid != BTRFS_FIRST_CHUNK_TREE_OBJECTID)) {
4806bd88 703 block_group_err(leaf, slot,
fce466ea 704 "invalid block group chunk objectid, have %llu expect %llu",
de0dc456 705 btrfs_stack_block_group_chunk_objectid(&bgi),
fce466ea
QW
706 BTRFS_FIRST_CHUNK_TREE_OBJECTID);
707 return -EUCLEAN;
708 }
709
c7c01a4a 710 if (unlikely(btrfs_stack_block_group_used(&bgi) > key->offset)) {
4806bd88 711 block_group_err(leaf, slot,
fce466ea 712 "invalid block group used, have %llu expect [0, %llu)",
de0dc456 713 btrfs_stack_block_group_used(&bgi), key->offset);
fce466ea
QW
714 return -EUCLEAN;
715 }
716
de0dc456 717 flags = btrfs_stack_block_group_flags(&bgi);
c7c01a4a 718 if (unlikely(hweight64(flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) > 1)) {
4806bd88 719 block_group_err(leaf, slot,
fce466ea
QW
720"invalid profile flags, have 0x%llx (%lu bits set) expect no more than 1 bit set",
721 flags & BTRFS_BLOCK_GROUP_PROFILE_MASK,
722 hweight64(flags & BTRFS_BLOCK_GROUP_PROFILE_MASK));
723 return -EUCLEAN;
724 }
725
726 type = flags & BTRFS_BLOCK_GROUP_TYPE_MASK;
c7c01a4a
DS
727 if (unlikely(type != BTRFS_BLOCK_GROUP_DATA &&
728 type != BTRFS_BLOCK_GROUP_METADATA &&
729 type != BTRFS_BLOCK_GROUP_SYSTEM &&
730 type != (BTRFS_BLOCK_GROUP_METADATA |
731 BTRFS_BLOCK_GROUP_DATA))) {
4806bd88 732 block_group_err(leaf, slot,
761333f2 733"invalid type, have 0x%llx (%lu bits set) expect either 0x%llx, 0x%llx, 0x%llx or 0x%llx",
fce466ea
QW
734 type, hweight64(type),
735 BTRFS_BLOCK_GROUP_DATA, BTRFS_BLOCK_GROUP_METADATA,
736 BTRFS_BLOCK_GROUP_SYSTEM,
737 BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA);
738 return -EUCLEAN;
739 }
740 return 0;
82fc28fb
QW
741}
742
d001e4a3 743__printf(4, 5)
f1140243 744__cold
d001e4a3 745static void chunk_err(const struct extent_buffer *leaf,
f1140243
QW
746 const struct btrfs_chunk *chunk, u64 logical,
747 const char *fmt, ...)
748{
d001e4a3 749 const struct btrfs_fs_info *fs_info = leaf->fs_info;
f1140243
QW
750 bool is_sb;
751 struct va_format vaf;
752 va_list args;
753 int i;
754 int slot = -1;
755
756 /* Only superblock eb is able to have such small offset */
757 is_sb = (leaf->start == BTRFS_SUPER_INFO_OFFSET);
758
759 if (!is_sb) {
760 /*
761 * Get the slot number by iterating through all slots, this
762 * would provide better readability.
763 */
764 for (i = 0; i < btrfs_header_nritems(leaf); i++) {
765 if (btrfs_item_ptr_offset(leaf, i) ==
766 (unsigned long)chunk) {
767 slot = i;
768 break;
769 }
770 }
771 }
772 va_start(args, fmt);
773 vaf.fmt = fmt;
774 vaf.va = &args;
775
776 if (is_sb)
777 btrfs_crit(fs_info,
778 "corrupt superblock syschunk array: chunk_start=%llu, %pV",
779 logical, &vaf);
780 else
781 btrfs_crit(fs_info,
782 "corrupt leaf: root=%llu block=%llu slot=%d chunk_start=%llu, %pV",
783 BTRFS_CHUNK_TREE_OBJECTID, leaf->start, slot,
784 logical, &vaf);
785 va_end(args);
786}
787
82fc28fb
QW
788/*
789 * The common chunk check which could also work on super block sys chunk array.
790 *
bf871c3b 791 * Return -EUCLEAN if anything is corrupted.
82fc28fb
QW
792 * Return 0 if everything is OK.
793 */
ddaf1d5a 794int btrfs_check_chunk_valid(struct extent_buffer *leaf,
82fc28fb
QW
795 struct btrfs_chunk *chunk, u64 logical)
796{
ddaf1d5a 797 struct btrfs_fs_info *fs_info = leaf->fs_info;
82fc28fb 798 u64 length;
347fb0cf 799 u64 chunk_end;
82fc28fb
QW
800 u64 stripe_len;
801 u16 num_stripes;
802 u16 sub_stripes;
803 u64 type;
804 u64 features;
805 bool mixed = false;
85d07fbe
DX
806 int raid_index;
807 int nparity;
808 int ncopies;
82fc28fb
QW
809
810 length = btrfs_chunk_length(leaf, chunk);
811 stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
812 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
813 sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
814 type = btrfs_chunk_type(leaf, chunk);
85d07fbe
DX
815 raid_index = btrfs_bg_flags_to_raid_index(type);
816 ncopies = btrfs_raid_array[raid_index].ncopies;
817 nparity = btrfs_raid_array[raid_index].nparity;
82fc28fb 818
c7c01a4a 819 if (unlikely(!num_stripes)) {
d001e4a3 820 chunk_err(leaf, chunk, logical,
f1140243 821 "invalid chunk num_stripes, have %u", num_stripes);
bf871c3b 822 return -EUCLEAN;
82fc28fb 823 }
c7c01a4a 824 if (unlikely(num_stripes < ncopies)) {
85d07fbe
DX
825 chunk_err(leaf, chunk, logical,
826 "invalid chunk num_stripes < ncopies, have %u < %d",
827 num_stripes, ncopies);
828 return -EUCLEAN;
829 }
c7c01a4a 830 if (unlikely(nparity && num_stripes == nparity)) {
85d07fbe
DX
831 chunk_err(leaf, chunk, logical,
832 "invalid chunk num_stripes == nparity, have %u == %d",
833 num_stripes, nparity);
834 return -EUCLEAN;
835 }
c7c01a4a 836 if (unlikely(!IS_ALIGNED(logical, fs_info->sectorsize))) {
d001e4a3 837 chunk_err(leaf, chunk, logical,
f1140243
QW
838 "invalid chunk logical, have %llu should aligned to %u",
839 logical, fs_info->sectorsize);
bf871c3b 840 return -EUCLEAN;
82fc28fb 841 }
c7c01a4a 842 if (unlikely(btrfs_chunk_sector_size(leaf, chunk) != fs_info->sectorsize)) {
d001e4a3 843 chunk_err(leaf, chunk, logical,
f1140243
QW
844 "invalid chunk sectorsize, have %u expect %u",
845 btrfs_chunk_sector_size(leaf, chunk),
846 fs_info->sectorsize);
bf871c3b 847 return -EUCLEAN;
82fc28fb 848 }
c7c01a4a 849 if (unlikely(!length || !IS_ALIGNED(length, fs_info->sectorsize))) {
d001e4a3 850 chunk_err(leaf, chunk, logical,
f1140243 851 "invalid chunk length, have %llu", length);
bf871c3b 852 return -EUCLEAN;
82fc28fb 853 }
347fb0cf
SY
854 if (unlikely(check_add_overflow(logical, length, &chunk_end))) {
855 chunk_err(leaf, chunk, logical,
856"invalid chunk logical start and length, have logical start %llu length %llu",
857 logical, length);
858 return -EUCLEAN;
859 }
c7c01a4a 860 if (unlikely(!is_power_of_2(stripe_len) || stripe_len != BTRFS_STRIPE_LEN)) {
d001e4a3 861 chunk_err(leaf, chunk, logical,
f1140243 862 "invalid chunk stripe length: %llu",
82fc28fb 863 stripe_len);
bf871c3b 864 return -EUCLEAN;
82fc28fb 865 }
6ded22c1
QW
866 /*
867 * We artificially limit the chunk size, so that the number of stripes
868 * inside a chunk can be fit into a U32. The current limit (256G) is
869 * way too large for real world usage anyway, and it's also much larger
870 * than our existing limit (10G).
871 *
872 * Thus it should be a good way to catch obvious bitflips.
873 */
cb091225 874 if (unlikely(length >= btrfs_stripe_nr_to_offset(U32_MAX))) {
6ded22c1
QW
875 chunk_err(leaf, chunk, logical,
876 "chunk length too large: have %llu limit %llu",
cb091225 877 length, btrfs_stripe_nr_to_offset(U32_MAX));
6ded22c1
QW
878 return -EUCLEAN;
879 }
c7c01a4a
DS
880 if (unlikely(type & ~(BTRFS_BLOCK_GROUP_TYPE_MASK |
881 BTRFS_BLOCK_GROUP_PROFILE_MASK))) {
d001e4a3 882 chunk_err(leaf, chunk, logical,
f1140243 883 "unrecognized chunk type: 0x%llx",
82fc28fb
QW
884 ~(BTRFS_BLOCK_GROUP_TYPE_MASK |
885 BTRFS_BLOCK_GROUP_PROFILE_MASK) &
886 btrfs_chunk_type(leaf, chunk));
bf871c3b 887 return -EUCLEAN;
82fc28fb
QW
888 }
889
c7c01a4a
DS
890 if (unlikely(!has_single_bit_set(type & BTRFS_BLOCK_GROUP_PROFILE_MASK) &&
891 (type & BTRFS_BLOCK_GROUP_PROFILE_MASK) != 0)) {
d001e4a3 892 chunk_err(leaf, chunk, logical,
80e46cf2
QW
893 "invalid chunk profile flag: 0x%llx, expect 0 or 1 bit set",
894 type & BTRFS_BLOCK_GROUP_PROFILE_MASK);
895 return -EUCLEAN;
896 }
c7c01a4a 897 if (unlikely((type & BTRFS_BLOCK_GROUP_TYPE_MASK) == 0)) {
d001e4a3 898 chunk_err(leaf, chunk, logical,
f1140243
QW
899 "missing chunk type flag, have 0x%llx one bit must be set in 0x%llx",
900 type, BTRFS_BLOCK_GROUP_TYPE_MASK);
bf871c3b 901 return -EUCLEAN;
82fc28fb
QW
902 }
903
c7c01a4a
DS
904 if (unlikely((type & BTRFS_BLOCK_GROUP_SYSTEM) &&
905 (type & (BTRFS_BLOCK_GROUP_METADATA |
906 BTRFS_BLOCK_GROUP_DATA)))) {
d001e4a3 907 chunk_err(leaf, chunk, logical,
f1140243
QW
908 "system chunk with data or metadata type: 0x%llx",
909 type);
bf871c3b 910 return -EUCLEAN;
82fc28fb
QW
911 }
912
913 features = btrfs_super_incompat_flags(fs_info->super_copy);
914 if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
915 mixed = true;
916
917 if (!mixed) {
c7c01a4a
DS
918 if (unlikely((type & BTRFS_BLOCK_GROUP_METADATA) &&
919 (type & BTRFS_BLOCK_GROUP_DATA))) {
d001e4a3 920 chunk_err(leaf, chunk, logical,
82fc28fb 921 "mixed chunk type in non-mixed mode: 0x%llx", type);
bf871c3b 922 return -EUCLEAN;
82fc28fb
QW
923 }
924 }
925
0ac6e06b
DS
926 if (unlikely((type & BTRFS_BLOCK_GROUP_RAID10 &&
927 sub_stripes != btrfs_raid_array[BTRFS_RAID_RAID10].sub_stripes) ||
928 (type & BTRFS_BLOCK_GROUP_RAID1 &&
929 num_stripes != btrfs_raid_array[BTRFS_RAID_RAID1].devs_min) ||
6c154ba4
DS
930 (type & BTRFS_BLOCK_GROUP_RAID1C3 &&
931 num_stripes != btrfs_raid_array[BTRFS_RAID_RAID1C3].devs_min) ||
932 (type & BTRFS_BLOCK_GROUP_RAID1C4 &&
933 num_stripes != btrfs_raid_array[BTRFS_RAID_RAID1C4].devs_min) ||
0ac6e06b
DS
934 (type & BTRFS_BLOCK_GROUP_RAID5 &&
935 num_stripes < btrfs_raid_array[BTRFS_RAID_RAID5].devs_min) ||
936 (type & BTRFS_BLOCK_GROUP_RAID6 &&
937 num_stripes < btrfs_raid_array[BTRFS_RAID_RAID6].devs_min) ||
938 (type & BTRFS_BLOCK_GROUP_DUP &&
939 num_stripes != btrfs_raid_array[BTRFS_RAID_DUP].dev_stripes) ||
c7c01a4a 940 ((type & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 &&
0ac6e06b 941 num_stripes != btrfs_raid_array[BTRFS_RAID_SINGLE].dev_stripes))) {
d001e4a3 942 chunk_err(leaf, chunk, logical,
82fc28fb
QW
943 "invalid num_stripes:sub_stripes %u:%u for profile %llu",
944 num_stripes, sub_stripes,
945 type & BTRFS_BLOCK_GROUP_PROFILE_MASK);
bf871c3b 946 return -EUCLEAN;
82fc28fb
QW
947 }
948
949 return 0;
fce466ea
QW
950}
951
f6d2a5c2
QW
952/*
953 * Enhanced version of chunk item checker.
954 *
955 * The common btrfs_check_chunk_valid() doesn't check item size since it needs
956 * to work on super block sys_chunk_array which doesn't have full item ptr.
957 */
958static int check_leaf_chunk_item(struct extent_buffer *leaf,
959 struct btrfs_chunk *chunk,
960 struct btrfs_key *key, int slot)
961{
962 int num_stripes;
963
3212fa14 964 if (unlikely(btrfs_item_size(leaf, slot) < sizeof(struct btrfs_chunk))) {
f6d2a5c2
QW
965 chunk_err(leaf, chunk, key->offset,
966 "invalid chunk item size: have %u expect [%zu, %u)",
3212fa14 967 btrfs_item_size(leaf, slot),
f6d2a5c2
QW
968 sizeof(struct btrfs_chunk),
969 BTRFS_LEAF_DATA_SIZE(leaf->fs_info));
970 return -EUCLEAN;
971 }
972
973 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
974 /* Let btrfs_check_chunk_valid() handle this error type */
975 if (num_stripes == 0)
976 goto out;
977
c7c01a4a 978 if (unlikely(btrfs_chunk_item_size(num_stripes) !=
3212fa14 979 btrfs_item_size(leaf, slot))) {
f6d2a5c2
QW
980 chunk_err(leaf, chunk, key->offset,
981 "invalid chunk item size: have %u expect %lu",
3212fa14 982 btrfs_item_size(leaf, slot),
f6d2a5c2
QW
983 btrfs_chunk_item_size(num_stripes));
984 return -EUCLEAN;
985 }
986out:
987 return btrfs_check_chunk_valid(leaf, chunk, key->offset);
988}
989
5617ed80 990__printf(3, 4)
ab4ba2e1 991__cold
5617ed80 992static void dev_item_err(const struct extent_buffer *eb, int slot,
ab4ba2e1
QW
993 const char *fmt, ...)
994{
995 struct btrfs_key key;
996 struct va_format vaf;
997 va_list args;
998
999 btrfs_item_key_to_cpu(eb, &key, slot);
1000 va_start(args, fmt);
1001
1002 vaf.fmt = fmt;
1003 vaf.va = &args;
1004
5617ed80 1005 btrfs_crit(eb->fs_info,
ab4ba2e1
QW
1006 "corrupt %s: root=%llu block=%llu slot=%d devid=%llu %pV",
1007 btrfs_header_level(eb) == 0 ? "leaf" : "node",
1008 btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot,
1009 key.objectid, &vaf);
1010 va_end(args);
1011}
1012
412a2312 1013static int check_dev_item(struct extent_buffer *leaf,
ab4ba2e1
QW
1014 struct btrfs_key *key, int slot)
1015{
1016 struct btrfs_dev_item *ditem;
ea1d1ca4 1017 const u32 item_size = btrfs_item_size(leaf, slot);
ab4ba2e1 1018
c7c01a4a 1019 if (unlikely(key->objectid != BTRFS_DEV_ITEMS_OBJECTID)) {
5617ed80 1020 dev_item_err(leaf, slot,
ab4ba2e1
QW
1021 "invalid objectid: has=%llu expect=%llu",
1022 key->objectid, BTRFS_DEV_ITEMS_OBJECTID);
1023 return -EUCLEAN;
1024 }
ea1d1ca4
SY
1025
1026 if (unlikely(item_size != sizeof(*ditem))) {
1027 dev_item_err(leaf, slot, "invalid item size: has %u expect %zu",
1028 item_size, sizeof(*ditem));
1029 return -EUCLEAN;
1030 }
1031
ab4ba2e1 1032 ditem = btrfs_item_ptr(leaf, slot, struct btrfs_dev_item);
c7c01a4a 1033 if (unlikely(btrfs_device_id(leaf, ditem) != key->offset)) {
5617ed80 1034 dev_item_err(leaf, slot,
ab4ba2e1
QW
1035 "devid mismatch: key has=%llu item has=%llu",
1036 key->offset, btrfs_device_id(leaf, ditem));
1037 return -EUCLEAN;
1038 }
1039
1040 /*
1041 * For device total_bytes, we don't have reliable way to check it, as
1042 * it can be 0 for device removal. Device size check can only be done
1043 * by dev extents check.
1044 */
c7c01a4a
DS
1045 if (unlikely(btrfs_device_bytes_used(leaf, ditem) >
1046 btrfs_device_total_bytes(leaf, ditem))) {
5617ed80 1047 dev_item_err(leaf, slot,
ab4ba2e1
QW
1048 "invalid bytes used: have %llu expect [0, %llu]",
1049 btrfs_device_bytes_used(leaf, ditem),
1050 btrfs_device_total_bytes(leaf, ditem));
1051 return -EUCLEAN;
1052 }
1053 /*
1054 * Remaining members like io_align/type/gen/dev_group aren't really
1055 * utilized. Skip them to make later usage of them easier.
1056 */
1057 return 0;
1058}
1059
39e57f49 1060static int check_inode_item(struct extent_buffer *leaf,
496245ca
QW
1061 struct btrfs_key *key, int slot)
1062{
39e57f49 1063 struct btrfs_fs_info *fs_info = leaf->fs_info;
496245ca
QW
1064 struct btrfs_inode_item *iitem;
1065 u64 super_gen = btrfs_super_generation(fs_info->super_copy);
1066 u32 valid_mask = (S_IFMT | S_ISUID | S_ISGID | S_ISVTX | 0777);
0c982944 1067 const u32 item_size = btrfs_item_size(leaf, slot);
496245ca 1068 u32 mode;
c23c77b0 1069 int ret;
77eea05e
BB
1070 u32 flags;
1071 u32 ro_flags;
c23c77b0
QW
1072
1073 ret = check_inode_key(leaf, key, slot);
c7c01a4a 1074 if (unlikely(ret < 0))
c23c77b0 1075 return ret;
496245ca 1076
0c982944
SY
1077 if (unlikely(item_size != sizeof(*iitem))) {
1078 generic_err(leaf, slot, "invalid item size: has %u expect %zu",
1079 item_size, sizeof(*iitem));
1080 return -EUCLEAN;
1081 }
1082
496245ca
QW
1083 iitem = btrfs_item_ptr(leaf, slot, struct btrfs_inode_item);
1084
1085 /* Here we use super block generation + 1 to handle log tree */
c7c01a4a 1086 if (unlikely(btrfs_inode_generation(leaf, iitem) > super_gen + 1)) {
c3053ebb 1087 inode_item_err(leaf, slot,
496245ca
QW
1088 "invalid inode generation: has %llu expect (0, %llu]",
1089 btrfs_inode_generation(leaf, iitem),
1090 super_gen + 1);
1091 return -EUCLEAN;
1092 }
1093 /* Note for ROOT_TREE_DIR_ITEM, mkfs could set its transid 0 */
c7c01a4a 1094 if (unlikely(btrfs_inode_transid(leaf, iitem) > super_gen + 1)) {
c3053ebb 1095 inode_item_err(leaf, slot,
f96d6960 1096 "invalid inode transid: has %llu expect [0, %llu]",
496245ca
QW
1097 btrfs_inode_transid(leaf, iitem), super_gen + 1);
1098 return -EUCLEAN;
1099 }
1100
1101 /*
1102 * For size and nbytes it's better not to be too strict, as for dir
1103 * item its size/nbytes can easily get wrong, but doesn't affect
1104 * anything in the fs. So here we skip the check.
1105 */
1106 mode = btrfs_inode_mode(leaf, iitem);
c7c01a4a 1107 if (unlikely(mode & ~valid_mask)) {
c3053ebb 1108 inode_item_err(leaf, slot,
496245ca
QW
1109 "unknown mode bit detected: 0x%x",
1110 mode & ~valid_mask);
1111 return -EUCLEAN;
1112 }
1113
1114 /*
c1499166
DS
1115 * S_IFMT is not bit mapped so we can't completely rely on
1116 * is_power_of_2/has_single_bit_set, but it can save us from checking
1117 * FIFO/CHR/DIR/REG. Only needs to check BLK, LNK and SOCKS
496245ca 1118 */
c1499166 1119 if (!has_single_bit_set(mode & S_IFMT)) {
c7c01a4a 1120 if (unlikely(!S_ISLNK(mode) && !S_ISBLK(mode) && !S_ISSOCK(mode))) {
c3053ebb 1121 inode_item_err(leaf, slot,
496245ca
QW
1122 "invalid mode: has 0%o expect valid S_IF* bit(s)",
1123 mode & S_IFMT);
1124 return -EUCLEAN;
1125 }
1126 }
c7c01a4a 1127 if (unlikely(S_ISDIR(mode) && btrfs_inode_nlink(leaf, iitem) > 1)) {
c3053ebb 1128 inode_item_err(leaf, slot,
496245ca
QW
1129 "invalid nlink: has %u expect no more than 1 for dir",
1130 btrfs_inode_nlink(leaf, iitem));
1131 return -EUCLEAN;
1132 }
77eea05e
BB
1133 btrfs_inode_split_flags(btrfs_inode_flags(leaf, iitem), &flags, &ro_flags);
1134 if (unlikely(flags & ~BTRFS_INODE_FLAG_MASK)) {
c3053ebb 1135 inode_item_err(leaf, slot,
77eea05e
BB
1136 "unknown incompat flags detected: 0x%x", flags);
1137 return -EUCLEAN;
1138 }
1139 if (unlikely(!sb_rdonly(fs_info->sb) &&
1140 (ro_flags & ~BTRFS_INODE_RO_FLAG_MASK))) {
1141 inode_item_err(leaf, slot,
1142 "unknown ro-compat flags detected on writeable mount: 0x%x",
1143 ro_flags);
496245ca
QW
1144 return -EUCLEAN;
1145 }
1146 return 0;
1147}
1148
259ee775
QW
1149static int check_root_item(struct extent_buffer *leaf, struct btrfs_key *key,
1150 int slot)
1151{
1152 struct btrfs_fs_info *fs_info = leaf->fs_info;
1465af12 1153 struct btrfs_root_item ri = { 0 };
259ee775
QW
1154 const u64 valid_root_flags = BTRFS_ROOT_SUBVOL_RDONLY |
1155 BTRFS_ROOT_SUBVOL_DEAD;
57a0e674 1156 int ret;
259ee775 1157
57a0e674 1158 ret = check_root_key(leaf, key, slot);
c7c01a4a 1159 if (unlikely(ret < 0))
57a0e674 1160 return ret;
259ee775 1161
3212fa14
JB
1162 if (unlikely(btrfs_item_size(leaf, slot) != sizeof(ri) &&
1163 btrfs_item_size(leaf, slot) !=
c7c01a4a 1164 btrfs_legacy_root_item_size())) {
259ee775 1165 generic_err(leaf, slot,
1465af12 1166 "invalid root item size, have %u expect %zu or %u",
3212fa14 1167 btrfs_item_size(leaf, slot), sizeof(ri),
1465af12 1168 btrfs_legacy_root_item_size());
1a49a97d 1169 return -EUCLEAN;
259ee775
QW
1170 }
1171
1465af12
QW
1172 /*
1173 * For legacy root item, the members starting at generation_v2 will be
1174 * all filled with 0.
1175 * And since we allow geneartion_v2 as 0, it will still pass the check.
1176 */
259ee775 1177 read_extent_buffer(leaf, &ri, btrfs_item_ptr_offset(leaf, slot),
3212fa14 1178 btrfs_item_size(leaf, slot));
259ee775
QW
1179
1180 /* Generation related */
c7c01a4a
DS
1181 if (unlikely(btrfs_root_generation(&ri) >
1182 btrfs_super_generation(fs_info->super_copy) + 1)) {
259ee775
QW
1183 generic_err(leaf, slot,
1184 "invalid root generation, have %llu expect (0, %llu]",
1185 btrfs_root_generation(&ri),
1186 btrfs_super_generation(fs_info->super_copy) + 1);
1187 return -EUCLEAN;
1188 }
c7c01a4a
DS
1189 if (unlikely(btrfs_root_generation_v2(&ri) >
1190 btrfs_super_generation(fs_info->super_copy) + 1)) {
259ee775
QW
1191 generic_err(leaf, slot,
1192 "invalid root v2 generation, have %llu expect (0, %llu]",
1193 btrfs_root_generation_v2(&ri),
1194 btrfs_super_generation(fs_info->super_copy) + 1);
1195 return -EUCLEAN;
1196 }
c7c01a4a
DS
1197 if (unlikely(btrfs_root_last_snapshot(&ri) >
1198 btrfs_super_generation(fs_info->super_copy) + 1)) {
259ee775
QW
1199 generic_err(leaf, slot,
1200 "invalid root last_snapshot, have %llu expect (0, %llu]",
1201 btrfs_root_last_snapshot(&ri),
1202 btrfs_super_generation(fs_info->super_copy) + 1);
1203 return -EUCLEAN;
1204 }
1205
1206 /* Alignment and level check */
c7c01a4a 1207 if (unlikely(!IS_ALIGNED(btrfs_root_bytenr(&ri), fs_info->sectorsize))) {
259ee775
QW
1208 generic_err(leaf, slot,
1209 "invalid root bytenr, have %llu expect to be aligned to %u",
1210 btrfs_root_bytenr(&ri), fs_info->sectorsize);
1211 return -EUCLEAN;
1212 }
c7c01a4a 1213 if (unlikely(btrfs_root_level(&ri) >= BTRFS_MAX_LEVEL)) {
259ee775
QW
1214 generic_err(leaf, slot,
1215 "invalid root level, have %u expect [0, %u]",
1216 btrfs_root_level(&ri), BTRFS_MAX_LEVEL - 1);
1217 return -EUCLEAN;
1218 }
c7c01a4a 1219 if (unlikely(btrfs_root_drop_level(&ri) >= BTRFS_MAX_LEVEL)) {
259ee775
QW
1220 generic_err(leaf, slot,
1221 "invalid root level, have %u expect [0, %u]",
c8422684 1222 btrfs_root_drop_level(&ri), BTRFS_MAX_LEVEL - 1);
259ee775
QW
1223 return -EUCLEAN;
1224 }
1225
1226 /* Flags check */
c7c01a4a 1227 if (unlikely(btrfs_root_flags(&ri) & ~valid_root_flags)) {
259ee775
QW
1228 generic_err(leaf, slot,
1229 "invalid root flags, have 0x%llx expect mask 0x%llx",
1230 btrfs_root_flags(&ri), valid_root_flags);
1231 return -EUCLEAN;
1232 }
1233 return 0;
1234}
1235
f82d1c7c
QW
1236__printf(3,4)
1237__cold
1238static void extent_err(const struct extent_buffer *eb, int slot,
1239 const char *fmt, ...)
1240{
1241 struct btrfs_key key;
1242 struct va_format vaf;
1243 va_list args;
1244 u64 bytenr;
1245 u64 len;
1246
1247 btrfs_item_key_to_cpu(eb, &key, slot);
1248 bytenr = key.objectid;
e2406a6f
QW
1249 if (key.type == BTRFS_METADATA_ITEM_KEY ||
1250 key.type == BTRFS_TREE_BLOCK_REF_KEY ||
1251 key.type == BTRFS_SHARED_BLOCK_REF_KEY)
f82d1c7c
QW
1252 len = eb->fs_info->nodesize;
1253 else
1254 len = key.offset;
1255 va_start(args, fmt);
1256
1257 vaf.fmt = fmt;
1258 vaf.va = &args;
1259
1260 btrfs_crit(eb->fs_info,
1261 "corrupt %s: block=%llu slot=%d extent bytenr=%llu len=%llu %pV",
1262 btrfs_header_level(eb) == 0 ? "leaf" : "node",
1263 eb->start, slot, bytenr, len, &vaf);
1264 va_end(args);
1265}
1266
1267static int check_extent_item(struct extent_buffer *leaf,
899b7f69
JB
1268 struct btrfs_key *key, int slot,
1269 struct btrfs_key *prev_key)
f82d1c7c
QW
1270{
1271 struct btrfs_fs_info *fs_info = leaf->fs_info;
1272 struct btrfs_extent_item *ei;
1273 bool is_tree_block = false;
1274 unsigned long ptr; /* Current pointer inside inline refs */
1275 unsigned long end; /* Extent item end */
3212fa14 1276 const u32 item_size = btrfs_item_size(leaf, slot);
f82d1c7c
QW
1277 u64 flags;
1278 u64 generation;
1279 u64 total_refs; /* Total refs in btrfs_extent_item */
1280 u64 inline_refs = 0; /* found total inline refs */
1281
c7c01a4a
DS
1282 if (unlikely(key->type == BTRFS_METADATA_ITEM_KEY &&
1283 !btrfs_fs_incompat(fs_info, SKINNY_METADATA))) {
f82d1c7c
QW
1284 generic_err(leaf, slot,
1285"invalid key type, METADATA_ITEM type invalid when SKINNY_METADATA feature disabled");
1286 return -EUCLEAN;
1287 }
1288 /* key->objectid is the bytenr for both key types */
c7c01a4a 1289 if (unlikely(!IS_ALIGNED(key->objectid, fs_info->sectorsize))) {
f82d1c7c
QW
1290 generic_err(leaf, slot,
1291 "invalid key objectid, have %llu expect to be aligned to %u",
1292 key->objectid, fs_info->sectorsize);
1293 return -EUCLEAN;
1294 }
1295
1296 /* key->offset is tree level for METADATA_ITEM_KEY */
c7c01a4a
DS
1297 if (unlikely(key->type == BTRFS_METADATA_ITEM_KEY &&
1298 key->offset >= BTRFS_MAX_LEVEL)) {
f82d1c7c
QW
1299 extent_err(leaf, slot,
1300 "invalid tree level, have %llu expect [0, %u]",
1301 key->offset, BTRFS_MAX_LEVEL - 1);
1302 return -EUCLEAN;
1303 }
1304
1305 /*
1306 * EXTENT/METADATA_ITEM consists of:
1307 * 1) One btrfs_extent_item
1308 * Records the total refs, type and generation of the extent.
1309 *
1310 * 2) One btrfs_tree_block_info (for EXTENT_ITEM and tree backref only)
1311 * Records the first key and level of the tree block.
1312 *
1313 * 2) Zero or more btrfs_extent_inline_ref(s)
1314 * Each inline ref has one btrfs_extent_inline_ref shows:
1315 * 2.1) The ref type, one of the 4
1316 * TREE_BLOCK_REF Tree block only
1317 * SHARED_BLOCK_REF Tree block only
1318 * EXTENT_DATA_REF Data only
1319 * SHARED_DATA_REF Data only
1320 * 2.2) Ref type specific data
1321 * Either using btrfs_extent_inline_ref::offset, or specific
1322 * data structure.
1323 */
c7c01a4a 1324 if (unlikely(item_size < sizeof(*ei))) {
f82d1c7c
QW
1325 extent_err(leaf, slot,
1326 "invalid item size, have %u expect [%zu, %u)",
1327 item_size, sizeof(*ei),
1328 BTRFS_LEAF_DATA_SIZE(fs_info));
1329 return -EUCLEAN;
1330 }
1331 end = item_size + btrfs_item_ptr_offset(leaf, slot);
1332
1333 /* Checks against extent_item */
1334 ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
1335 flags = btrfs_extent_flags(leaf, ei);
1336 total_refs = btrfs_extent_refs(leaf, ei);
1337 generation = btrfs_extent_generation(leaf, ei);
c7c01a4a
DS
1338 if (unlikely(generation >
1339 btrfs_super_generation(fs_info->super_copy) + 1)) {
f82d1c7c
QW
1340 extent_err(leaf, slot,
1341 "invalid generation, have %llu expect (0, %llu]",
1342 generation,
1343 btrfs_super_generation(fs_info->super_copy) + 1);
1344 return -EUCLEAN;
1345 }
c7c01a4a
DS
1346 if (unlikely(!has_single_bit_set(flags & (BTRFS_EXTENT_FLAG_DATA |
1347 BTRFS_EXTENT_FLAG_TREE_BLOCK)))) {
f82d1c7c
QW
1348 extent_err(leaf, slot,
1349 "invalid extent flag, have 0x%llx expect 1 bit set in 0x%llx",
1350 flags, BTRFS_EXTENT_FLAG_DATA |
1351 BTRFS_EXTENT_FLAG_TREE_BLOCK);
1352 return -EUCLEAN;
1353 }
1354 is_tree_block = !!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK);
1355 if (is_tree_block) {
c7c01a4a
DS
1356 if (unlikely(key->type == BTRFS_EXTENT_ITEM_KEY &&
1357 key->offset != fs_info->nodesize)) {
f82d1c7c
QW
1358 extent_err(leaf, slot,
1359 "invalid extent length, have %llu expect %u",
1360 key->offset, fs_info->nodesize);
1361 return -EUCLEAN;
1362 }
1363 } else {
c7c01a4a 1364 if (unlikely(key->type != BTRFS_EXTENT_ITEM_KEY)) {
f82d1c7c
QW
1365 extent_err(leaf, slot,
1366 "invalid key type, have %u expect %u for data backref",
1367 key->type, BTRFS_EXTENT_ITEM_KEY);
1368 return -EUCLEAN;
1369 }
c7c01a4a 1370 if (unlikely(!IS_ALIGNED(key->offset, fs_info->sectorsize))) {
f82d1c7c
QW
1371 extent_err(leaf, slot,
1372 "invalid extent length, have %llu expect aligned to %u",
1373 key->offset, fs_info->sectorsize);
1374 return -EUCLEAN;
1375 }
0ebb6bbb
JB
1376 if (unlikely(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
1377 extent_err(leaf, slot,
1378 "invalid extent flag, data has full backref set");
1379 return -EUCLEAN;
1380 }
f82d1c7c
QW
1381 }
1382 ptr = (unsigned long)(struct btrfs_extent_item *)(ei + 1);
1383
1384 /* Check the special case of btrfs_tree_block_info */
1385 if (is_tree_block && key->type != BTRFS_METADATA_ITEM_KEY) {
1386 struct btrfs_tree_block_info *info;
1387
1388 info = (struct btrfs_tree_block_info *)ptr;
c7c01a4a 1389 if (unlikely(btrfs_tree_block_level(leaf, info) >= BTRFS_MAX_LEVEL)) {
f82d1c7c
QW
1390 extent_err(leaf, slot,
1391 "invalid tree block info level, have %u expect [0, %u]",
1392 btrfs_tree_block_level(leaf, info),
1393 BTRFS_MAX_LEVEL - 1);
1394 return -EUCLEAN;
1395 }
1396 ptr = (unsigned long)(struct btrfs_tree_block_info *)(info + 1);
1397 }
1398
1399 /* Check inline refs */
1400 while (ptr < end) {
1401 struct btrfs_extent_inline_ref *iref;
1402 struct btrfs_extent_data_ref *dref;
1403 struct btrfs_shared_data_ref *sref;
1404 u64 dref_offset;
1405 u64 inline_offset;
1406 u8 inline_type;
1407
c7c01a4a 1408 if (unlikely(ptr + sizeof(*iref) > end)) {
f82d1c7c
QW
1409 extent_err(leaf, slot,
1410"inline ref item overflows extent item, ptr %lu iref size %zu end %lu",
1411 ptr, sizeof(*iref), end);
1412 return -EUCLEAN;
1413 }
1414 iref = (struct btrfs_extent_inline_ref *)ptr;
1415 inline_type = btrfs_extent_inline_ref_type(leaf, iref);
1416 inline_offset = btrfs_extent_inline_ref_offset(leaf, iref);
c7c01a4a 1417 if (unlikely(ptr + btrfs_extent_inline_ref_size(inline_type) > end)) {
f82d1c7c
QW
1418 extent_err(leaf, slot,
1419"inline ref item overflows extent item, ptr %lu iref size %u end %lu",
1420 ptr, inline_type, end);
1421 return -EUCLEAN;
1422 }
1423
1424 switch (inline_type) {
1425 /* inline_offset is subvolid of the owner, no need to check */
1426 case BTRFS_TREE_BLOCK_REF_KEY:
1427 inline_refs++;
1428 break;
1429 /* Contains parent bytenr */
1430 case BTRFS_SHARED_BLOCK_REF_KEY:
c7c01a4a
DS
1431 if (unlikely(!IS_ALIGNED(inline_offset,
1432 fs_info->sectorsize))) {
f82d1c7c
QW
1433 extent_err(leaf, slot,
1434 "invalid tree parent bytenr, have %llu expect aligned to %u",
1435 inline_offset, fs_info->sectorsize);
1436 return -EUCLEAN;
1437 }
1438 inline_refs++;
1439 break;
1440 /*
1441 * Contains owner subvolid, owner key objectid, adjusted offset.
1442 * The only obvious corruption can happen in that offset.
1443 */
1444 case BTRFS_EXTENT_DATA_REF_KEY:
1445 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1446 dref_offset = btrfs_extent_data_ref_offset(leaf, dref);
c7c01a4a
DS
1447 if (unlikely(!IS_ALIGNED(dref_offset,
1448 fs_info->sectorsize))) {
f82d1c7c
QW
1449 extent_err(leaf, slot,
1450 "invalid data ref offset, have %llu expect aligned to %u",
1451 dref_offset, fs_info->sectorsize);
1452 return -EUCLEAN;
1453 }
1454 inline_refs += btrfs_extent_data_ref_count(leaf, dref);
1455 break;
1456 /* Contains parent bytenr and ref count */
1457 case BTRFS_SHARED_DATA_REF_KEY:
1458 sref = (struct btrfs_shared_data_ref *)(iref + 1);
c7c01a4a
DS
1459 if (unlikely(!IS_ALIGNED(inline_offset,
1460 fs_info->sectorsize))) {
f82d1c7c
QW
1461 extent_err(leaf, slot,
1462 "invalid data parent bytenr, have %llu expect aligned to %u",
1463 inline_offset, fs_info->sectorsize);
1464 return -EUCLEAN;
1465 }
1466 inline_refs += btrfs_shared_data_ref_count(leaf, sref);
1467 break;
1468 default:
1469 extent_err(leaf, slot, "unknown inline ref type: %u",
1470 inline_type);
1471 return -EUCLEAN;
1472 }
1473 ptr += btrfs_extent_inline_ref_size(inline_type);
1474 }
1475 /* No padding is allowed */
c7c01a4a 1476 if (unlikely(ptr != end)) {
f82d1c7c
QW
1477 extent_err(leaf, slot,
1478 "invalid extent item size, padding bytes found");
1479 return -EUCLEAN;
1480 }
1481
1482 /* Finally, check the inline refs against total refs */
c7c01a4a 1483 if (unlikely(inline_refs > total_refs)) {
f82d1c7c
QW
1484 extent_err(leaf, slot,
1485 "invalid extent refs, have %llu expect >= inline %llu",
1486 total_refs, inline_refs);
1487 return -EUCLEAN;
1488 }
899b7f69
JB
1489
1490 if ((prev_key->type == BTRFS_EXTENT_ITEM_KEY) ||
1491 (prev_key->type == BTRFS_METADATA_ITEM_KEY)) {
1492 u64 prev_end = prev_key->objectid;
1493
1494 if (prev_key->type == BTRFS_METADATA_ITEM_KEY)
1495 prev_end += fs_info->nodesize;
1496 else
1497 prev_end += prev_key->offset;
1498
1499 if (unlikely(prev_end > key->objectid)) {
1500 extent_err(leaf, slot,
1501 "previous extent [%llu %u %llu] overlaps current extent [%llu %u %llu]",
1502 prev_key->objectid, prev_key->type,
1503 prev_key->offset, key->objectid, key->type,
1504 key->offset);
1505 return -EUCLEAN;
1506 }
1507 }
1508
f82d1c7c
QW
1509 return 0;
1510}
1511
e2406a6f
QW
1512static int check_simple_keyed_refs(struct extent_buffer *leaf,
1513 struct btrfs_key *key, int slot)
1514{
1515 u32 expect_item_size = 0;
1516
1517 if (key->type == BTRFS_SHARED_DATA_REF_KEY)
1518 expect_item_size = sizeof(struct btrfs_shared_data_ref);
1519
3212fa14 1520 if (unlikely(btrfs_item_size(leaf, slot) != expect_item_size)) {
e2406a6f
QW
1521 generic_err(leaf, slot,
1522 "invalid item size, have %u expect %u for key type %u",
3212fa14 1523 btrfs_item_size(leaf, slot),
e2406a6f
QW
1524 expect_item_size, key->type);
1525 return -EUCLEAN;
1526 }
c7c01a4a 1527 if (unlikely(!IS_ALIGNED(key->objectid, leaf->fs_info->sectorsize))) {
e2406a6f
QW
1528 generic_err(leaf, slot,
1529"invalid key objectid for shared block ref, have %llu expect aligned to %u",
1530 key->objectid, leaf->fs_info->sectorsize);
1531 return -EUCLEAN;
1532 }
c7c01a4a
DS
1533 if (unlikely(key->type != BTRFS_TREE_BLOCK_REF_KEY &&
1534 !IS_ALIGNED(key->offset, leaf->fs_info->sectorsize))) {
e2406a6f
QW
1535 extent_err(leaf, slot,
1536 "invalid tree parent bytenr, have %llu expect aligned to %u",
1537 key->offset, leaf->fs_info->sectorsize);
1538 return -EUCLEAN;
1539 }
1540 return 0;
1541}
1542
0785a9aa
QW
1543static int check_extent_data_ref(struct extent_buffer *leaf,
1544 struct btrfs_key *key, int slot)
1545{
1546 struct btrfs_extent_data_ref *dref;
1547 unsigned long ptr = btrfs_item_ptr_offset(leaf, slot);
3212fa14 1548 const unsigned long end = ptr + btrfs_item_size(leaf, slot);
0785a9aa 1549
3212fa14 1550 if (unlikely(btrfs_item_size(leaf, slot) % sizeof(*dref) != 0)) {
0785a9aa
QW
1551 generic_err(leaf, slot,
1552 "invalid item size, have %u expect aligned to %zu for key type %u",
3212fa14 1553 btrfs_item_size(leaf, slot),
0785a9aa 1554 sizeof(*dref), key->type);
6d06b0ad 1555 return -EUCLEAN;
0785a9aa 1556 }
c7c01a4a 1557 if (unlikely(!IS_ALIGNED(key->objectid, leaf->fs_info->sectorsize))) {
0785a9aa
QW
1558 generic_err(leaf, slot,
1559"invalid key objectid for shared block ref, have %llu expect aligned to %u",
1560 key->objectid, leaf->fs_info->sectorsize);
1561 return -EUCLEAN;
1562 }
1563 for (; ptr < end; ptr += sizeof(*dref)) {
0785a9aa 1564 u64 offset;
0785a9aa 1565
1119a72e
JB
1566 /*
1567 * We cannot check the extent_data_ref hash due to possible
1568 * overflow from the leaf due to hash collisions.
1569 */
0785a9aa 1570 dref = (struct btrfs_extent_data_ref *)ptr;
0785a9aa 1571 offset = btrfs_extent_data_ref_offset(leaf, dref);
c7c01a4a 1572 if (unlikely(!IS_ALIGNED(offset, leaf->fs_info->sectorsize))) {
0785a9aa
QW
1573 extent_err(leaf, slot,
1574 "invalid extent data backref offset, have %llu expect aligned to %u",
1575 offset, leaf->fs_info->sectorsize);
6d06b0ad 1576 return -EUCLEAN;
0785a9aa
QW
1577 }
1578 }
1579 return 0;
1580}
1581
c3053ebb
QW
1582#define inode_ref_err(eb, slot, fmt, args...) \
1583 inode_item_err(eb, slot, fmt, ##args)
71bf92a9
QW
1584static int check_inode_ref(struct extent_buffer *leaf,
1585 struct btrfs_key *key, struct btrfs_key *prev_key,
1586 int slot)
1587{
1588 struct btrfs_inode_ref *iref;
1589 unsigned long ptr;
1590 unsigned long end;
1591
c7c01a4a 1592 if (unlikely(!check_prev_ino(leaf, key, slot, prev_key)))
80d7fd1e 1593 return -EUCLEAN;
71bf92a9 1594 /* namelen can't be 0, so item_size == sizeof() is also invalid */
3212fa14 1595 if (unlikely(btrfs_item_size(leaf, slot) <= sizeof(*iref))) {
c3053ebb 1596 inode_ref_err(leaf, slot,
71bf92a9 1597 "invalid item size, have %u expect (%zu, %u)",
3212fa14 1598 btrfs_item_size(leaf, slot),
71bf92a9
QW
1599 sizeof(*iref), BTRFS_LEAF_DATA_SIZE(leaf->fs_info));
1600 return -EUCLEAN;
1601 }
1602
1603 ptr = btrfs_item_ptr_offset(leaf, slot);
3212fa14 1604 end = ptr + btrfs_item_size(leaf, slot);
71bf92a9
QW
1605 while (ptr < end) {
1606 u16 namelen;
1607
c7c01a4a 1608 if (unlikely(ptr + sizeof(iref) > end)) {
c3053ebb 1609 inode_ref_err(leaf, slot,
71bf92a9
QW
1610 "inode ref overflow, ptr %lu end %lu inode_ref_size %zu",
1611 ptr, end, sizeof(iref));
1612 return -EUCLEAN;
1613 }
1614
1615 iref = (struct btrfs_inode_ref *)ptr;
1616 namelen = btrfs_inode_ref_name_len(leaf, iref);
c7c01a4a 1617 if (unlikely(ptr + sizeof(*iref) + namelen > end)) {
c3053ebb 1618 inode_ref_err(leaf, slot,
71bf92a9
QW
1619 "inode ref overflow, ptr %lu end %lu namelen %u",
1620 ptr, end, namelen);
1621 return -EUCLEAN;
1622 }
1623
1624 /*
1625 * NOTE: In theory we should record all found index numbers
1626 * to find any duplicated indexes, but that will be too time
1627 * consuming for inodes with too many hard links.
1628 */
1629 ptr += sizeof(*iref) + namelen;
1630 }
1631 return 0;
1632}
1633
557ea5dd
QW
1634/*
1635 * Common point to switch the item-specific validation.
1636 */
c8d54215
JB
1637static enum btrfs_tree_block_status check_leaf_item(struct extent_buffer *leaf,
1638 struct btrfs_key *key,
1639 int slot,
1640 struct btrfs_key *prev_key)
557ea5dd
QW
1641{
1642 int ret = 0;
075cb3c7 1643 struct btrfs_chunk *chunk;
557ea5dd
QW
1644
1645 switch (key->type) {
1646 case BTRFS_EXTENT_DATA_KEY:
4e9845ef 1647 ret = check_extent_data_item(leaf, key, slot, prev_key);
557ea5dd
QW
1648 break;
1649 case BTRFS_EXTENT_CSUM_KEY:
ad1d8c43 1650 ret = check_csum_item(leaf, key, slot, prev_key);
557ea5dd 1651 break;
ad7b0368
QW
1652 case BTRFS_DIR_ITEM_KEY:
1653 case BTRFS_DIR_INDEX_KEY:
1654 case BTRFS_XATTR_ITEM_KEY:
c18679eb 1655 ret = check_dir_item(leaf, key, prev_key, slot);
ad7b0368 1656 break;
71bf92a9
QW
1657 case BTRFS_INODE_REF_KEY:
1658 ret = check_inode_ref(leaf, key, prev_key, slot);
1659 break;
fce466ea 1660 case BTRFS_BLOCK_GROUP_ITEM_KEY:
af60ce2b 1661 ret = check_block_group_item(leaf, key, slot);
fce466ea 1662 break;
075cb3c7
QW
1663 case BTRFS_CHUNK_ITEM_KEY:
1664 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
f6d2a5c2 1665 ret = check_leaf_chunk_item(leaf, chunk, key, slot);
075cb3c7 1666 break;
ab4ba2e1 1667 case BTRFS_DEV_ITEM_KEY:
412a2312 1668 ret = check_dev_item(leaf, key, slot);
ab4ba2e1 1669 break;
496245ca 1670 case BTRFS_INODE_ITEM_KEY:
39e57f49 1671 ret = check_inode_item(leaf, key, slot);
496245ca 1672 break;
259ee775
QW
1673 case BTRFS_ROOT_ITEM_KEY:
1674 ret = check_root_item(leaf, key, slot);
1675 break;
f82d1c7c
QW
1676 case BTRFS_EXTENT_ITEM_KEY:
1677 case BTRFS_METADATA_ITEM_KEY:
899b7f69 1678 ret = check_extent_item(leaf, key, slot, prev_key);
f82d1c7c 1679 break;
e2406a6f
QW
1680 case BTRFS_TREE_BLOCK_REF_KEY:
1681 case BTRFS_SHARED_DATA_REF_KEY:
1682 case BTRFS_SHARED_BLOCK_REF_KEY:
1683 ret = check_simple_keyed_refs(leaf, key, slot);
1684 break;
0785a9aa
QW
1685 case BTRFS_EXTENT_DATA_REF_KEY:
1686 ret = check_extent_data_ref(leaf, key, slot);
1687 break;
557ea5dd 1688 }
c8d54215
JB
1689
1690 if (ret)
1691 return BTRFS_TREE_BLOCK_INVALID_ITEM;
1692 return BTRFS_TREE_BLOCK_CLEAN;
557ea5dd
QW
1693}
1694
924452c8 1695enum btrfs_tree_block_status __btrfs_check_leaf(struct extent_buffer *leaf)
557ea5dd 1696{
e2ccd361 1697 struct btrfs_fs_info *fs_info = leaf->fs_info;
557ea5dd
QW
1698 /* No valid key type is 0, so all key should be larger than this key */
1699 struct btrfs_key prev_key = {0, 0, 0};
1700 struct btrfs_key key;
1701 u32 nritems = btrfs_header_nritems(leaf);
1702 int slot;
1703
c7c01a4a 1704 if (unlikely(btrfs_header_level(leaf) != 0)) {
86a6be3a 1705 generic_err(leaf, 0,
f556faa4
QW
1706 "invalid level for leaf, have %d expect 0",
1707 btrfs_header_level(leaf));
924452c8 1708 return BTRFS_TREE_BLOCK_INVALID_LEVEL;
f556faa4
QW
1709 }
1710
557ea5dd
QW
1711 /*
1712 * Extent buffers from a relocation tree have a owner field that
1713 * corresponds to the subvolume tree they are based on. So just from an
1714 * extent buffer alone we can not find out what is the id of the
1715 * corresponding subvolume tree, so we can not figure out if the extent
1716 * buffer corresponds to the root of the relocation tree or not. So
1717 * skip this check for relocation trees.
1718 */
1719 if (nritems == 0 && !btrfs_header_flag(leaf, BTRFS_HEADER_FLAG_RELOC)) {
ba480dd4 1720 u64 owner = btrfs_header_owner(leaf);
557ea5dd 1721
ba480dd4 1722 /* These trees must never be empty */
c7c01a4a
DS
1723 if (unlikely(owner == BTRFS_ROOT_TREE_OBJECTID ||
1724 owner == BTRFS_CHUNK_TREE_OBJECTID ||
c7c01a4a
DS
1725 owner == BTRFS_DEV_TREE_OBJECTID ||
1726 owner == BTRFS_FS_TREE_OBJECTID ||
1727 owner == BTRFS_DATA_RELOC_TREE_OBJECTID)) {
86a6be3a 1728 generic_err(leaf, 0,
ba480dd4
QW
1729 "invalid root, root %llu must never be empty",
1730 owner);
924452c8 1731 return BTRFS_TREE_BLOCK_INVALID_NRITEMS;
ba480dd4 1732 }
c2fa821c 1733
62fdaa52 1734 /* Unknown tree */
c7c01a4a 1735 if (unlikely(owner == 0)) {
62fdaa52
QW
1736 generic_err(leaf, 0,
1737 "invalid owner, root 0 is not defined");
924452c8 1738 return BTRFS_TREE_BLOCK_INVALID_OWNER;
62fdaa52 1739 }
c2fa821c
JB
1740
1741 /* EXTENT_TREE_V2 can have empty extent trees. */
1742 if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2))
924452c8 1743 return BTRFS_TREE_BLOCK_CLEAN;
c2fa821c
JB
1744
1745 if (unlikely(owner == BTRFS_EXTENT_TREE_OBJECTID)) {
1746 generic_err(leaf, 0,
1747 "invalid root, root %llu must never be empty",
1748 owner);
924452c8 1749 return BTRFS_TREE_BLOCK_INVALID_NRITEMS;
c2fa821c
JB
1750 }
1751
924452c8 1752 return BTRFS_TREE_BLOCK_CLEAN;
557ea5dd
QW
1753 }
1754
c7c01a4a 1755 if (unlikely(nritems == 0))
924452c8 1756 return BTRFS_TREE_BLOCK_CLEAN;
557ea5dd
QW
1757
1758 /*
1759 * Check the following things to make sure this is a good leaf, and
1760 * leaf users won't need to bother with similar sanity checks:
1761 *
1762 * 1) key ordering
1763 * 2) item offset and size
1764 * No overlap, no hole, all inside the leaf.
1765 * 3) item content
1766 * If possible, do comprehensive sanity check.
1767 * NOTE: All checks must only rely on the item data itself.
1768 */
1769 for (slot = 0; slot < nritems; slot++) {
1770 u32 item_end_expected;
a6ab66eb 1771 u64 item_data_end;
557ea5dd
QW
1772
1773 btrfs_item_key_to_cpu(leaf, &key, slot);
1774
1775 /* Make sure the keys are in the right order */
c7c01a4a 1776 if (unlikely(btrfs_comp_cpu_keys(&prev_key, &key) >= 0)) {
86a6be3a 1777 generic_err(leaf, slot,
478d01b3
QW
1778 "bad key order, prev (%llu %u %llu) current (%llu %u %llu)",
1779 prev_key.objectid, prev_key.type,
1780 prev_key.offset, key.objectid, key.type,
1781 key.offset);
924452c8 1782 return BTRFS_TREE_BLOCK_BAD_KEY_ORDER;
557ea5dd
QW
1783 }
1784
a6ab66eb
SY
1785 item_data_end = (u64)btrfs_item_offset(leaf, slot) +
1786 btrfs_item_size(leaf, slot);
557ea5dd
QW
1787 /*
1788 * Make sure the offset and ends are right, remember that the
1789 * item data starts at the end of the leaf and grows towards the
1790 * front.
1791 */
1792 if (slot == 0)
1793 item_end_expected = BTRFS_LEAF_DATA_SIZE(fs_info);
1794 else
3212fa14 1795 item_end_expected = btrfs_item_offset(leaf,
557ea5dd 1796 slot - 1);
a6ab66eb 1797 if (unlikely(item_data_end != item_end_expected)) {
86a6be3a 1798 generic_err(leaf, slot,
a6ab66eb
SY
1799 "unexpected item end, have %llu expect %u",
1800 item_data_end, item_end_expected);
924452c8 1801 return BTRFS_TREE_BLOCK_INVALID_OFFSETS;
557ea5dd
QW
1802 }
1803
1804 /*
1805 * Check to make sure that we don't point outside of the leaf,
1806 * just in case all the items are consistent to each other, but
1807 * all point outside of the leaf.
1808 */
a6ab66eb 1809 if (unlikely(item_data_end > BTRFS_LEAF_DATA_SIZE(fs_info))) {
86a6be3a 1810 generic_err(leaf, slot,
a6ab66eb
SY
1811 "slot end outside of leaf, have %llu expect range [0, %u]",
1812 item_data_end, BTRFS_LEAF_DATA_SIZE(fs_info));
924452c8 1813 return BTRFS_TREE_BLOCK_INVALID_OFFSETS;
557ea5dd
QW
1814 }
1815
1816 /* Also check if the item pointer overlaps with btrfs item. */
c7c01a4a 1817 if (unlikely(btrfs_item_ptr_offset(leaf, slot) <
42c9419a 1818 btrfs_item_nr_offset(leaf, slot) + sizeof(struct btrfs_item))) {
86a6be3a 1819 generic_err(leaf, slot,
478d01b3 1820 "slot overlaps with its data, item end %lu data start %lu",
42c9419a 1821 btrfs_item_nr_offset(leaf, slot) +
478d01b3
QW
1822 sizeof(struct btrfs_item),
1823 btrfs_item_ptr_offset(leaf, slot));
924452c8 1824 return BTRFS_TREE_BLOCK_INVALID_OFFSETS;
557ea5dd
QW
1825 }
1826
85d8a826
JB
1827 /*
1828 * We only want to do this if WRITTEN is set, otherwise the leaf
1829 * may be in some intermediate state and won't appear valid.
1830 */
1831 if (btrfs_header_flag(leaf, BTRFS_HEADER_FLAG_WRITTEN)) {
c8d54215
JB
1832 enum btrfs_tree_block_status ret;
1833
69fc6cbb
QW
1834 /*
1835 * Check if the item size and content meet other
1836 * criteria
1837 */
4e9845ef 1838 ret = check_leaf_item(leaf, &key, slot, &prev_key);
c8d54215 1839 if (unlikely(ret != BTRFS_TREE_BLOCK_CLEAN))
69fc6cbb
QW
1840 return ret;
1841 }
557ea5dd
QW
1842
1843 prev_key.objectid = key.objectid;
1844 prev_key.type = key.type;
1845 prev_key.offset = key.offset;
1846 }
1847
924452c8 1848 return BTRFS_TREE_BLOCK_CLEAN;
557ea5dd
QW
1849}
1850
924452c8 1851int btrfs_check_leaf(struct extent_buffer *leaf)
69fc6cbb 1852{
924452c8 1853 enum btrfs_tree_block_status ret;
69fc6cbb 1854
924452c8
JB
1855 ret = __btrfs_check_leaf(leaf);
1856 if (unlikely(ret != BTRFS_TREE_BLOCK_CLEAN))
1857 return -EUCLEAN;
557ea5dd 1858 return 0;
69fc6cbb 1859}
85d8a826 1860ALLOW_ERROR_INJECTION(btrfs_check_leaf, ERRNO);
69fc6cbb 1861
c26fa931 1862enum btrfs_tree_block_status __btrfs_check_node(struct extent_buffer *node)
557ea5dd 1863{
813fd1dc 1864 struct btrfs_fs_info *fs_info = node->fs_info;
557ea5dd
QW
1865 unsigned long nr = btrfs_header_nritems(node);
1866 struct btrfs_key key, next_key;
1867 int slot;
f556faa4 1868 int level = btrfs_header_level(node);
557ea5dd 1869 u64 bytenr;
557ea5dd 1870
c7c01a4a 1871 if (unlikely(level <= 0 || level >= BTRFS_MAX_LEVEL)) {
86a6be3a 1872 generic_err(node, 0,
f556faa4
QW
1873 "invalid level for node, have %d expect [1, %d]",
1874 level, BTRFS_MAX_LEVEL - 1);
c26fa931 1875 return BTRFS_TREE_BLOCK_INVALID_LEVEL;
f556faa4 1876 }
c7c01a4a 1877 if (unlikely(nr == 0 || nr > BTRFS_NODEPTRS_PER_BLOCK(fs_info))) {
2f659546 1878 btrfs_crit(fs_info,
bba4f298 1879"corrupt node: root=%llu block=%llu, nritems too %s, have %lu expect range [1,%u]",
2f659546 1880 btrfs_header_owner(node), node->start,
bba4f298 1881 nr == 0 ? "small" : "large", nr,
2f659546 1882 BTRFS_NODEPTRS_PER_BLOCK(fs_info));
c26fa931 1883 return BTRFS_TREE_BLOCK_INVALID_NRITEMS;
557ea5dd
QW
1884 }
1885
1886 for (slot = 0; slot < nr - 1; slot++) {
1887 bytenr = btrfs_node_blockptr(node, slot);
1888 btrfs_node_key_to_cpu(node, &key, slot);
1889 btrfs_node_key_to_cpu(node, &next_key, slot + 1);
1890
c7c01a4a 1891 if (unlikely(!bytenr)) {
86a6be3a 1892 generic_err(node, slot,
bba4f298 1893 "invalid NULL node pointer");
c26fa931 1894 return BTRFS_TREE_BLOCK_INVALID_BLOCKPTR;
bba4f298 1895 }
c7c01a4a 1896 if (unlikely(!IS_ALIGNED(bytenr, fs_info->sectorsize))) {
86a6be3a 1897 generic_err(node, slot,
bba4f298 1898 "unaligned pointer, have %llu should be aligned to %u",
2f659546 1899 bytenr, fs_info->sectorsize);
c26fa931 1900 return BTRFS_TREE_BLOCK_INVALID_BLOCKPTR;
557ea5dd
QW
1901 }
1902
c7c01a4a 1903 if (unlikely(btrfs_comp_cpu_keys(&key, &next_key) >= 0)) {
86a6be3a 1904 generic_err(node, slot,
bba4f298
QW
1905 "bad key order, current (%llu %u %llu) next (%llu %u %llu)",
1906 key.objectid, key.type, key.offset,
1907 next_key.objectid, next_key.type,
1908 next_key.offset);
c26fa931 1909 return BTRFS_TREE_BLOCK_BAD_KEY_ORDER;
557ea5dd
QW
1910 }
1911 }
c26fa931
JB
1912 return BTRFS_TREE_BLOCK_CLEAN;
1913}
1914
1915int btrfs_check_node(struct extent_buffer *node)
1916{
1917 enum btrfs_tree_block_status ret;
1918
1919 ret = __btrfs_check_node(node);
1920 if (unlikely(ret != BTRFS_TREE_BLOCK_CLEAN))
1921 return -EUCLEAN;
1922 return 0;
557ea5dd 1923}
02529d7a 1924ALLOW_ERROR_INJECTION(btrfs_check_node, ERRNO);
88c602ab
QW
1925
1926int btrfs_check_eb_owner(const struct extent_buffer *eb, u64 root_owner)
1927{
1928 const bool is_subvol = is_fstree(root_owner);
1929 const u64 eb_owner = btrfs_header_owner(eb);
1930
1931 /*
1932 * Skip dummy fs, as selftests don't create unique ebs for each dummy
1933 * root.
1934 */
1935 if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &eb->fs_info->fs_state))
1936 return 0;
1937 /*
1938 * There are several call sites (backref walking, qgroup, and data
1939 * reloc) passing 0 as @root_owner, as they are not holding the
1940 * tree root. In that case, we can not do a reliable ownership check,
1941 * so just exit.
1942 */
1943 if (root_owner == 0)
1944 return 0;
1945 /*
1946 * These trees use key.offset as their owner, our callers don't have
1947 * the extra capacity to pass key.offset here. So we just skip them.
1948 */
1949 if (root_owner == BTRFS_TREE_LOG_OBJECTID ||
1950 root_owner == BTRFS_TREE_RELOC_OBJECTID)
1951 return 0;
1952
1953 if (!is_subvol) {
1954 /* For non-subvolume trees, the eb owner should match root owner */
1955 if (unlikely(root_owner != eb_owner)) {
1956 btrfs_crit(eb->fs_info,
1957"corrupted %s, root=%llu block=%llu owner mismatch, have %llu expect %llu",
1958 btrfs_header_level(eb) == 0 ? "leaf" : "node",
1959 root_owner, btrfs_header_bytenr(eb), eb_owner,
1960 root_owner);
1961 return -EUCLEAN;
1962 }
1963 return 0;
1964 }
1965
1966 /*
1967 * For subvolume trees, owners can mismatch, but they should all belong
1968 * to subvolume trees.
1969 */
1970 if (unlikely(is_subvol != is_fstree(eb_owner))) {
1971 btrfs_crit(eb->fs_info,
1972"corrupted %s, root=%llu block=%llu owner mismatch, have %llu expect [%llu, %llu]",
1973 btrfs_header_level(eb) == 0 ? "leaf" : "node",
1974 root_owner, btrfs_header_bytenr(eb), eb_owner,
1975 BTRFS_FIRST_FREE_OBJECTID, BTRFS_LAST_FREE_OBJECTID);
1976 return -EUCLEAN;
1977 }
1978 return 0;
1979}
2cac5af1
JB
1980
1981int btrfs_verify_level_key(struct extent_buffer *eb, int level,
1982 struct btrfs_key *first_key, u64 parent_transid)
1983{
1984 struct btrfs_fs_info *fs_info = eb->fs_info;
1985 int found_level;
1986 struct btrfs_key found_key;
1987 int ret;
1988
1989 found_level = btrfs_header_level(eb);
1990 if (found_level != level) {
1991 WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG),
1992 KERN_ERR "BTRFS: tree level check failed\n");
1993 btrfs_err(fs_info,
1994"tree level mismatch detected, bytenr=%llu level expected=%u has=%u",
1995 eb->start, level, found_level);
1996 return -EIO;
1997 }
1998
1999 if (!first_key)
2000 return 0;
2001
2002 /*
2003 * For live tree block (new tree blocks in current transaction),
2004 * we need proper lock context to avoid race, which is impossible here.
2005 * So we only checks tree blocks which is read from disk, whose
2006 * generation <= fs_info->last_trans_committed.
2007 */
2008 if (btrfs_header_generation(eb) > fs_info->last_trans_committed)
2009 return 0;
2010
2011 /* We have @first_key, so this @eb must have at least one item */
2012 if (btrfs_header_nritems(eb) == 0) {
2013 btrfs_err(fs_info,
2014 "invalid tree nritems, bytenr=%llu nritems=0 expect >0",
2015 eb->start);
2016 WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG));
2017 return -EUCLEAN;
2018 }
2019
2020 if (found_level)
2021 btrfs_node_key_to_cpu(eb, &found_key, 0);
2022 else
2023 btrfs_item_key_to_cpu(eb, &found_key, 0);
2024 ret = btrfs_comp_cpu_keys(first_key, &found_key);
2025
2026 if (ret) {
2027 WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG),
2028 KERN_ERR "BTRFS: tree first key check failed\n");
2029 btrfs_err(fs_info,
2030"tree first key mismatch detected, bytenr=%llu parent_transid=%llu key expected=(%llu,%u,%llu) has=(%llu,%u,%llu)",
2031 eb->start, parent_transid, first_key->objectid,
2032 first_key->type, first_key->offset,
2033 found_key.objectid, found_key.type,
2034 found_key.offset);
2035 }
2036 return ret;
2037}