1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) Qu Wenruo 2017. All rights reserved.
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.
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.
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.
19 #include "tree-checker.h"
21 #include "compression.h"
25 * Error message should follow the following format:
26 * corrupt <type>: <identifier>, <reason>[, <bad_value>]
29 * @identifier: the necessary info to locate the leaf/node.
30 * It's recommened to decode key.objecitd/offset if it's
32 * @reason: describe the error
33 * @bad_value: optional, it's recommened to output bad value and its
34 * expected value (range).
36 * Since comma is used to separate the components, only space is allowed
37 * inside each component.
41 * Append generic "corrupt leaf/node root=%llu block=%llu slot=%d: " to @fmt.
42 * Allows callers to customize the output.
46 static void generic_err(const struct btrfs_fs_info *fs_info,
47 const struct extent_buffer *eb, int slot,
59 "corrupt %s: root=%llu block=%llu slot=%d, %pV",
60 btrfs_header_level(eb) == 0 ? "leaf" : "node",
61 btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot, &vaf);
66 * Customized reporter for extent data item, since its key objectid and
67 * offset has its own meaning.
71 static void file_extent_err(const struct btrfs_fs_info *fs_info,
72 const struct extent_buffer *eb, int slot,
79 btrfs_item_key_to_cpu(eb, &key, slot);
86 "corrupt %s: root=%llu block=%llu slot=%d ino=%llu file_offset=%llu, %pV",
87 btrfs_header_level(eb) == 0 ? "leaf" : "node",
88 btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot,
89 key.objectid, key.offset, &vaf);
94 * Return 0 if the btrfs_file_extent_##name is aligned to @alignment
97 #define CHECK_FE_ALIGNED(fs_info, leaf, slot, fi, name, alignment) \
99 if (!IS_ALIGNED(btrfs_file_extent_##name((leaf), (fi)), (alignment))) \
100 file_extent_err((fs_info), (leaf), (slot), \
101 "invalid %s for file extent, have %llu, should be aligned to %u", \
102 (#name), btrfs_file_extent_##name((leaf), (fi)), \
104 (!IS_ALIGNED(btrfs_file_extent_##name((leaf), (fi)), (alignment))); \
107 static int check_extent_data_item(struct btrfs_fs_info *fs_info,
108 struct extent_buffer *leaf,
109 struct btrfs_key *key, int slot)
111 struct btrfs_file_extent_item *fi;
112 u32 sectorsize = fs_info->sectorsize;
113 u32 item_size = btrfs_item_size_nr(leaf, slot);
115 if (!IS_ALIGNED(key->offset, sectorsize)) {
116 file_extent_err(fs_info, leaf, slot,
117 "unaligned file_offset for file extent, have %llu should be aligned to %u",
118 key->offset, sectorsize);
122 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
124 if (btrfs_file_extent_type(leaf, fi) > BTRFS_FILE_EXTENT_TYPES) {
125 file_extent_err(fs_info, leaf, slot,
126 "invalid type for file extent, have %u expect range [0, %u]",
127 btrfs_file_extent_type(leaf, fi),
128 BTRFS_FILE_EXTENT_TYPES);
133 * Support for new compression/encrption must introduce incompat flag,
134 * and must be caught in open_ctree().
136 if (btrfs_file_extent_compression(leaf, fi) > BTRFS_COMPRESS_TYPES) {
137 file_extent_err(fs_info, leaf, slot,
138 "invalid compression for file extent, have %u expect range [0, %u]",
139 btrfs_file_extent_compression(leaf, fi),
140 BTRFS_COMPRESS_TYPES);
143 if (btrfs_file_extent_encryption(leaf, fi)) {
144 file_extent_err(fs_info, leaf, slot,
145 "invalid encryption for file extent, have %u expect 0",
146 btrfs_file_extent_encryption(leaf, fi));
149 if (btrfs_file_extent_type(leaf, fi) == BTRFS_FILE_EXTENT_INLINE) {
150 /* Inline extent must have 0 as key offset */
152 file_extent_err(fs_info, leaf, slot,
153 "invalid file_offset for inline file extent, have %llu expect 0",
158 /* Compressed inline extent has no on-disk size, skip it */
159 if (btrfs_file_extent_compression(leaf, fi) !=
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)) {
166 file_extent_err(fs_info, leaf, slot,
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));
175 /* Regular or preallocated extent has fixed item size */
176 if (item_size != sizeof(*fi)) {
177 file_extent_err(fs_info, leaf, slot,
178 "invalid item size for reg/prealloc file extent, have %u expect %zu",
179 item_size, sizeof(*fi));
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))
191 static int check_csum_item(struct btrfs_fs_info *fs_info,
192 struct extent_buffer *leaf, struct btrfs_key *key,
195 u32 sectorsize = fs_info->sectorsize;
196 u32 csumsize = btrfs_super_csum_size(fs_info->super_copy);
198 if (key->objectid != BTRFS_EXTENT_CSUM_OBJECTID) {
199 generic_err(fs_info, leaf, slot,
200 "invalid key objectid for csum item, have %llu expect %llu",
201 key->objectid, BTRFS_EXTENT_CSUM_OBJECTID);
204 if (!IS_ALIGNED(key->offset, sectorsize)) {
205 generic_err(fs_info, leaf, slot,
206 "unaligned key offset for csum item, have %llu should be aligned to %u",
207 key->offset, sectorsize);
210 if (!IS_ALIGNED(btrfs_item_size_nr(leaf, slot), csumsize)) {
211 generic_err(fs_info, leaf, slot,
212 "unaligned item size for csum item, have %u should be aligned to %u",
213 btrfs_item_size_nr(leaf, slot), csumsize);
220 * Customized reported for dir_item, only important new info is key->objectid,
221 * which represents inode number
225 static void dir_item_err(const struct btrfs_fs_info *fs_info,
226 const struct extent_buffer *eb, int slot,
227 const char *fmt, ...)
229 struct btrfs_key key;
230 struct va_format vaf;
233 btrfs_item_key_to_cpu(eb, &key, slot);
240 "corrupt %s: root=%llu block=%llu slot=%d ino=%llu, %pV",
241 btrfs_header_level(eb) == 0 ? "leaf" : "node",
242 btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot,
247 static int check_dir_item(struct btrfs_fs_info *fs_info,
248 struct extent_buffer *leaf,
249 struct btrfs_key *key, int slot)
251 struct btrfs_dir_item *di;
252 u32 item_size = btrfs_item_size_nr(leaf, slot);
255 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
256 while (cur < item_size) {
264 /* header itself should not cross item boundary */
265 if (cur + sizeof(*di) > item_size) {
266 dir_item_err(fs_info, leaf, slot,
267 "dir item header crosses item boundary, have %zu boundary %u",
268 cur + sizeof(*di), item_size);
273 dir_type = btrfs_dir_type(leaf, di);
274 if (dir_type >= BTRFS_FT_MAX) {
275 dir_item_err(fs_info, leaf, slot,
276 "invalid dir item type, have %u expect [0, %u)",
277 dir_type, BTRFS_FT_MAX);
281 if (key->type == BTRFS_XATTR_ITEM_KEY &&
282 dir_type != BTRFS_FT_XATTR) {
283 dir_item_err(fs_info, leaf, slot,
284 "invalid dir item type for XATTR key, have %u expect %u",
285 dir_type, BTRFS_FT_XATTR);
288 if (dir_type == BTRFS_FT_XATTR &&
289 key->type != BTRFS_XATTR_ITEM_KEY) {
290 dir_item_err(fs_info, leaf, slot,
291 "xattr dir type found for non-XATTR key");
294 if (dir_type == BTRFS_FT_XATTR)
295 max_name_len = XATTR_NAME_MAX;
297 max_name_len = BTRFS_NAME_LEN;
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) {
303 dir_item_err(fs_info, leaf, slot,
304 "dir item name len too long, have %u max %u",
305 name_len, max_name_len);
308 if (name_len + data_len > BTRFS_MAX_XATTR_SIZE(fs_info)) {
309 dir_item_err(fs_info, leaf, slot,
310 "dir item name and data len too long, have %u max %u",
312 BTRFS_MAX_XATTR_SIZE(fs_info));
316 if (data_len && dir_type != BTRFS_FT_XATTR) {
317 dir_item_err(fs_info, leaf, slot,
318 "dir item with invalid data len, have %u expect 0",
323 total_size = sizeof(*di) + name_len + data_len;
325 /* header and name/data should not cross item boundary */
326 if (cur + total_size > item_size) {
327 dir_item_err(fs_info, leaf, slot,
328 "dir item data crosses item boundary, have %u boundary %u",
329 cur + total_size, item_size);
334 * Special check for XATTR/DIR_ITEM, as key->offset is name
335 * hash, should match its name
337 if (key->type == BTRFS_DIR_ITEM_KEY ||
338 key->type == BTRFS_XATTR_ITEM_KEY) {
339 char namebuf[max(BTRFS_NAME_LEN, XATTR_NAME_MAX)];
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) {
345 dir_item_err(fs_info, leaf, slot,
346 "name hash mismatch with key, have 0x%016x expect 0x%016llx",
347 name_hash, key->offset);
352 di = (struct btrfs_dir_item *)((void *)di + total_size);
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, ...)
363 struct btrfs_key key;
364 struct va_format vaf;
367 btrfs_item_key_to_cpu(eb, &key, slot);
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);
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)
385 struct btrfs_block_group_item bgi;
386 u32 item_size = btrfs_item_size_nr(leaf, slot);
391 * Here we don't really care about alignment since extent allocator can
392 * handle it. We care more about the size.
394 if (key->offset == 0) {
395 block_group_err(fs_info, leaf, slot,
396 "invalid block group size 0");
400 if (item_size != sizeof(bgi)) {
401 block_group_err(fs_info, leaf, slot,
402 "invalid item size, have %u expect %zu",
403 item_size, sizeof(bgi));
407 read_extent_buffer(leaf, &bgi, btrfs_item_ptr_offset(leaf, slot),
409 if (btrfs_block_group_chunk_objectid(&bgi) !=
410 BTRFS_FIRST_CHUNK_TREE_OBJECTID) {
411 block_group_err(fs_info, leaf, slot,
412 "invalid block group chunk objectid, have %llu expect %llu",
413 btrfs_block_group_chunk_objectid(&bgi),
414 BTRFS_FIRST_CHUNK_TREE_OBJECTID);
418 if (btrfs_block_group_used(&bgi) > key->offset) {
419 block_group_err(fs_info, leaf, slot,
420 "invalid block group used, have %llu expect [0, %llu)",
421 btrfs_block_group_used(&bgi), key->offset);
425 flags = btrfs_block_group_flags(&bgi);
426 if (hweight64(flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) > 1) {
427 block_group_err(fs_info, leaf, slot,
428 "invalid profile flags, have 0x%llx (%lu bits set) expect no more than 1 bit set",
429 flags & BTRFS_BLOCK_GROUP_PROFILE_MASK,
430 hweight64(flags & BTRFS_BLOCK_GROUP_PROFILE_MASK));
434 type = flags & BTRFS_BLOCK_GROUP_TYPE_MASK;
435 if (type != BTRFS_BLOCK_GROUP_DATA &&
436 type != BTRFS_BLOCK_GROUP_METADATA &&
437 type != BTRFS_BLOCK_GROUP_SYSTEM &&
438 type != (BTRFS_BLOCK_GROUP_METADATA |
439 BTRFS_BLOCK_GROUP_DATA)) {
440 block_group_err(fs_info, leaf, slot,
441 "invalid type, have 0x%llx (%lu bits set) expect either 0x%llx, 0x%llx, 0x%llx or 0x%llx",
442 type, hweight64(type),
443 BTRFS_BLOCK_GROUP_DATA, BTRFS_BLOCK_GROUP_METADATA,
444 BTRFS_BLOCK_GROUP_SYSTEM,
445 BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA);
453 static void chunk_err(const struct btrfs_fs_info *fs_info,
454 const struct extent_buffer *leaf,
455 const struct btrfs_chunk *chunk, u64 logical,
456 const char *fmt, ...)
459 struct va_format vaf;
464 /* Only superblock eb is able to have such small offset */
465 is_sb = (leaf->start == BTRFS_SUPER_INFO_OFFSET);
469 * Get the slot number by iterating through all slots, this
470 * would provide better readability.
472 for (i = 0; i < btrfs_header_nritems(leaf); i++) {
473 if (btrfs_item_ptr_offset(leaf, i) ==
474 (unsigned long)chunk) {
486 "corrupt superblock syschunk array: chunk_start=%llu, %pV",
490 "corrupt leaf: root=%llu block=%llu slot=%d chunk_start=%llu, %pV",
491 BTRFS_CHUNK_TREE_OBJECTID, leaf->start, slot,
497 * The common chunk check which could also work on super block sys chunk array.
499 * Return -EUCLEAN if anything is corrupted.
500 * Return 0 if everything is OK.
502 int btrfs_check_chunk_valid(struct btrfs_fs_info *fs_info,
503 struct extent_buffer *leaf,
504 struct btrfs_chunk *chunk, u64 logical)
514 length = btrfs_chunk_length(leaf, chunk);
515 stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
516 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
517 sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
518 type = btrfs_chunk_type(leaf, chunk);
521 chunk_err(fs_info, leaf, chunk, logical,
522 "invalid chunk num_stripes, have %u", num_stripes);
525 if (!IS_ALIGNED(logical, fs_info->sectorsize)) {
526 chunk_err(fs_info, leaf, chunk, logical,
527 "invalid chunk logical, have %llu should aligned to %u",
528 logical, fs_info->sectorsize);
531 if (btrfs_chunk_sector_size(leaf, chunk) != fs_info->sectorsize) {
532 chunk_err(fs_info, leaf, chunk, logical,
533 "invalid chunk sectorsize, have %u expect %u",
534 btrfs_chunk_sector_size(leaf, chunk),
535 fs_info->sectorsize);
538 if (!length || !IS_ALIGNED(length, fs_info->sectorsize)) {
539 chunk_err(fs_info, leaf, chunk, logical,
540 "invalid chunk length, have %llu", length);
543 if (!is_power_of_2(stripe_len) || stripe_len != BTRFS_STRIPE_LEN) {
544 chunk_err(fs_info, leaf, chunk, logical,
545 "invalid chunk stripe length: %llu",
549 if (~(BTRFS_BLOCK_GROUP_TYPE_MASK | BTRFS_BLOCK_GROUP_PROFILE_MASK) &
551 chunk_err(fs_info, leaf, chunk, logical,
552 "unrecognized chunk type: 0x%llx",
553 ~(BTRFS_BLOCK_GROUP_TYPE_MASK |
554 BTRFS_BLOCK_GROUP_PROFILE_MASK) &
555 btrfs_chunk_type(leaf, chunk));
559 if (!is_power_of_2(type & BTRFS_BLOCK_GROUP_PROFILE_MASK) &&
560 (type & BTRFS_BLOCK_GROUP_PROFILE_MASK) != 0) {
561 chunk_err(fs_info, leaf, chunk, logical,
562 "invalid chunk profile flag: 0x%llx, expect 0 or 1 bit set",
563 type & BTRFS_BLOCK_GROUP_PROFILE_MASK);
566 if ((type & BTRFS_BLOCK_GROUP_TYPE_MASK) == 0) {
567 chunk_err(fs_info, leaf, chunk, logical,
568 "missing chunk type flag, have 0x%llx one bit must be set in 0x%llx",
569 type, BTRFS_BLOCK_GROUP_TYPE_MASK);
573 if ((type & BTRFS_BLOCK_GROUP_SYSTEM) &&
574 (type & (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA))) {
575 chunk_err(fs_info, leaf, chunk, logical,
576 "system chunk with data or metadata type: 0x%llx",
581 features = btrfs_super_incompat_flags(fs_info->super_copy);
582 if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
586 if ((type & BTRFS_BLOCK_GROUP_METADATA) &&
587 (type & BTRFS_BLOCK_GROUP_DATA)) {
588 chunk_err(fs_info, leaf, chunk, logical,
589 "mixed chunk type in non-mixed mode: 0x%llx", type);
594 if ((type & BTRFS_BLOCK_GROUP_RAID10 && sub_stripes != 2) ||
595 (type & BTRFS_BLOCK_GROUP_RAID1 && num_stripes != 2) ||
596 (type & BTRFS_BLOCK_GROUP_RAID5 && num_stripes < 2) ||
597 (type & BTRFS_BLOCK_GROUP_RAID6 && num_stripes < 3) ||
598 (type & BTRFS_BLOCK_GROUP_DUP && num_stripes != 2) ||
599 ((type & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 && num_stripes != 1)) {
600 chunk_err(fs_info, leaf, chunk, logical,
601 "invalid num_stripes:sub_stripes %u:%u for profile %llu",
602 num_stripes, sub_stripes,
603 type & BTRFS_BLOCK_GROUP_PROFILE_MASK);
612 static void dev_item_err(const struct btrfs_fs_info *fs_info,
613 const struct extent_buffer *eb, int slot,
614 const char *fmt, ...)
616 struct btrfs_key key;
617 struct va_format vaf;
620 btrfs_item_key_to_cpu(eb, &key, slot);
627 "corrupt %s: root=%llu block=%llu slot=%d devid=%llu %pV",
628 btrfs_header_level(eb) == 0 ? "leaf" : "node",
629 btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot,
634 static int check_dev_item(struct btrfs_fs_info *fs_info,
635 struct extent_buffer *leaf,
636 struct btrfs_key *key, int slot)
638 struct btrfs_dev_item *ditem;
640 if (key->objectid != BTRFS_DEV_ITEMS_OBJECTID) {
641 dev_item_err(fs_info, leaf, slot,
642 "invalid objectid: has=%llu expect=%llu",
643 key->objectid, BTRFS_DEV_ITEMS_OBJECTID);
646 ditem = btrfs_item_ptr(leaf, slot, struct btrfs_dev_item);
647 if (btrfs_device_id(leaf, ditem) != key->offset) {
648 dev_item_err(fs_info, leaf, slot,
649 "devid mismatch: key has=%llu item has=%llu",
650 key->offset, btrfs_device_id(leaf, ditem));
655 * For device total_bytes, we don't have reliable way to check it, as
656 * it can be 0 for device removal. Device size check can only be done
657 * by dev extents check.
659 if (btrfs_device_bytes_used(leaf, ditem) >
660 btrfs_device_total_bytes(leaf, ditem)) {
661 dev_item_err(fs_info, leaf, slot,
662 "invalid bytes used: have %llu expect [0, %llu]",
663 btrfs_device_bytes_used(leaf, ditem),
664 btrfs_device_total_bytes(leaf, ditem));
668 * Remaining members like io_align/type/gen/dev_group aren't really
669 * utilized. Skip them to make later usage of them easier.
674 /* Inode item error output has the same format as dir_item_err() */
675 #define inode_item_err(fs_info, eb, slot, fmt, ...) \
676 dir_item_err(fs_info, eb, slot, fmt, __VA_ARGS__)
678 static int check_inode_item(struct btrfs_fs_info *fs_info,
679 struct extent_buffer *leaf,
680 struct btrfs_key *key, int slot)
682 struct btrfs_inode_item *iitem;
683 u64 super_gen = btrfs_super_generation(fs_info->super_copy);
684 u32 valid_mask = (S_IFMT | S_ISUID | S_ISGID | S_ISVTX | 0777);
687 if ((key->objectid < BTRFS_FIRST_FREE_OBJECTID ||
688 key->objectid > BTRFS_LAST_FREE_OBJECTID) &&
689 key->objectid != BTRFS_ROOT_TREE_DIR_OBJECTID &&
690 key->objectid != BTRFS_FREE_INO_OBJECTID) {
691 generic_err(fs_info, leaf, slot,
692 "invalid key objectid: has %llu expect %llu or [%llu, %llu] or %llu",
693 key->objectid, BTRFS_ROOT_TREE_DIR_OBJECTID,
694 BTRFS_FIRST_FREE_OBJECTID,
695 BTRFS_LAST_FREE_OBJECTID,
696 BTRFS_FREE_INO_OBJECTID);
699 if (key->offset != 0) {
700 inode_item_err(fs_info, leaf, slot,
701 "invalid key offset: has %llu expect 0",
705 iitem = btrfs_item_ptr(leaf, slot, struct btrfs_inode_item);
707 /* Here we use super block generation + 1 to handle log tree */
708 if (btrfs_inode_generation(leaf, iitem) > super_gen + 1) {
709 inode_item_err(fs_info, leaf, slot,
710 "invalid inode generation: has %llu expect (0, %llu]",
711 btrfs_inode_generation(leaf, iitem),
715 /* Note for ROOT_TREE_DIR_ITEM, mkfs could set its transid 0 */
716 if (btrfs_inode_transid(leaf, iitem) > super_gen + 1) {
717 inode_item_err(fs_info, leaf, slot,
718 "invalid inode transid: has %llu expect [0, %llu]",
719 btrfs_inode_transid(leaf, iitem), super_gen + 1);
724 * For size and nbytes it's better not to be too strict, as for dir
725 * item its size/nbytes can easily get wrong, but doesn't affect
726 * anything in the fs. So here we skip the check.
728 mode = btrfs_inode_mode(leaf, iitem);
729 if (mode & ~valid_mask) {
730 inode_item_err(fs_info, leaf, slot,
731 "unknown mode bit detected: 0x%x",
737 * S_IFMT is not bit mapped so we can't completely rely on is_power_of_2,
738 * but is_power_of_2() can save us from checking FIFO/CHR/DIR/REG.
739 * Only needs to check BLK, LNK and SOCKS
741 if (!is_power_of_2(mode & S_IFMT)) {
742 if (!S_ISLNK(mode) && !S_ISBLK(mode) && !S_ISSOCK(mode)) {
743 inode_item_err(fs_info, leaf, slot,
744 "invalid mode: has 0%o expect valid S_IF* bit(s)",
749 if (S_ISDIR(mode) && btrfs_inode_nlink(leaf, iitem) > 1) {
750 inode_item_err(fs_info, leaf, slot,
751 "invalid nlink: has %u expect no more than 1 for dir",
752 btrfs_inode_nlink(leaf, iitem));
755 if (btrfs_inode_flags(leaf, iitem) & ~BTRFS_INODE_FLAG_MASK) {
756 inode_item_err(fs_info, leaf, slot,
757 "unknown flags detected: 0x%llx",
758 btrfs_inode_flags(leaf, iitem) &
759 ~BTRFS_INODE_FLAG_MASK);
766 * Common point to switch the item-specific validation.
768 static int check_leaf_item(struct btrfs_fs_info *fs_info,
769 struct extent_buffer *leaf,
770 struct btrfs_key *key, int slot)
773 struct btrfs_chunk *chunk;
776 case BTRFS_EXTENT_DATA_KEY:
777 ret = check_extent_data_item(fs_info, leaf, key, slot);
779 case BTRFS_EXTENT_CSUM_KEY:
780 ret = check_csum_item(fs_info, leaf, key, slot);
782 case BTRFS_DIR_ITEM_KEY:
783 case BTRFS_DIR_INDEX_KEY:
784 case BTRFS_XATTR_ITEM_KEY:
785 ret = check_dir_item(fs_info, leaf, key, slot);
787 case BTRFS_BLOCK_GROUP_ITEM_KEY:
788 ret = check_block_group_item(fs_info, leaf, key, slot);
790 case BTRFS_CHUNK_ITEM_KEY:
791 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
792 ret = btrfs_check_chunk_valid(fs_info, leaf, chunk,
795 case BTRFS_DEV_ITEM_KEY:
796 ret = check_dev_item(fs_info, leaf, key, slot);
798 case BTRFS_INODE_ITEM_KEY:
799 ret = check_inode_item(fs_info, leaf, key, slot);
805 static int check_leaf(struct btrfs_fs_info *fs_info, struct extent_buffer *leaf,
806 bool check_item_data)
808 /* No valid key type is 0, so all key should be larger than this key */
809 struct btrfs_key prev_key = {0, 0, 0};
810 struct btrfs_key key;
811 u32 nritems = btrfs_header_nritems(leaf);
814 if (btrfs_header_level(leaf) != 0) {
815 generic_err(fs_info, leaf, 0,
816 "invalid level for leaf, have %d expect 0",
817 btrfs_header_level(leaf));
822 * Extent buffers from a relocation tree have a owner field that
823 * corresponds to the subvolume tree they are based on. So just from an
824 * extent buffer alone we can not find out what is the id of the
825 * corresponding subvolume tree, so we can not figure out if the extent
826 * buffer corresponds to the root of the relocation tree or not. So
827 * skip this check for relocation trees.
829 if (nritems == 0 && !btrfs_header_flag(leaf, BTRFS_HEADER_FLAG_RELOC)) {
830 u64 owner = btrfs_header_owner(leaf);
831 struct btrfs_root *check_root;
833 /* These trees must never be empty */
834 if (owner == BTRFS_ROOT_TREE_OBJECTID ||
835 owner == BTRFS_CHUNK_TREE_OBJECTID ||
836 owner == BTRFS_EXTENT_TREE_OBJECTID ||
837 owner == BTRFS_DEV_TREE_OBJECTID ||
838 owner == BTRFS_FS_TREE_OBJECTID ||
839 owner == BTRFS_DATA_RELOC_TREE_OBJECTID) {
840 generic_err(fs_info, leaf, 0,
841 "invalid root, root %llu must never be empty",
847 generic_err(fs_info, leaf, 0,
848 "invalid owner, root 0 is not defined");
851 key.objectid = owner;
852 key.type = BTRFS_ROOT_ITEM_KEY;
853 key.offset = (u64)-1;
855 check_root = btrfs_get_fs_root(fs_info, &key, false);
857 * The only reason we also check NULL here is that during
858 * open_ctree() some roots has not yet been set up.
860 if (!IS_ERR_OR_NULL(check_root)) {
861 struct extent_buffer *eb;
863 eb = btrfs_root_node(check_root);
864 /* if leaf is the root, then it's fine */
866 generic_err(fs_info, leaf, 0,
867 "invalid nritems, have %u should not be 0 for non-root leaf",
869 free_extent_buffer(eb);
872 free_extent_buffer(eb);
881 * Check the following things to make sure this is a good leaf, and
882 * leaf users won't need to bother with similar sanity checks:
885 * 2) item offset and size
886 * No overlap, no hole, all inside the leaf.
888 * If possible, do comprehensive sanity check.
889 * NOTE: All checks must only rely on the item data itself.
891 for (slot = 0; slot < nritems; slot++) {
892 u32 item_end_expected;
895 btrfs_item_key_to_cpu(leaf, &key, slot);
897 /* Make sure the keys are in the right order */
898 if (btrfs_comp_cpu_keys(&prev_key, &key) >= 0) {
899 generic_err(fs_info, leaf, slot,
900 "bad key order, prev (%llu %u %llu) current (%llu %u %llu)",
901 prev_key.objectid, prev_key.type,
902 prev_key.offset, key.objectid, key.type,
908 * Make sure the offset and ends are right, remember that the
909 * item data starts at the end of the leaf and grows towards the
913 item_end_expected = BTRFS_LEAF_DATA_SIZE(fs_info);
915 item_end_expected = btrfs_item_offset_nr(leaf,
917 if (btrfs_item_end_nr(leaf, slot) != item_end_expected) {
918 generic_err(fs_info, leaf, slot,
919 "unexpected item end, have %u expect %u",
920 btrfs_item_end_nr(leaf, slot),
926 * Check to make sure that we don't point outside of the leaf,
927 * just in case all the items are consistent to each other, but
928 * all point outside of the leaf.
930 if (btrfs_item_end_nr(leaf, slot) >
931 BTRFS_LEAF_DATA_SIZE(fs_info)) {
932 generic_err(fs_info, leaf, slot,
933 "slot end outside of leaf, have %u expect range [0, %u]",
934 btrfs_item_end_nr(leaf, slot),
935 BTRFS_LEAF_DATA_SIZE(fs_info));
939 /* Also check if the item pointer overlaps with btrfs item. */
940 if (btrfs_item_nr_offset(slot) + sizeof(struct btrfs_item) >
941 btrfs_item_ptr_offset(leaf, slot)) {
942 generic_err(fs_info, leaf, slot,
943 "slot overlaps with its data, item end %lu data start %lu",
944 btrfs_item_nr_offset(slot) +
945 sizeof(struct btrfs_item),
946 btrfs_item_ptr_offset(leaf, slot));
950 if (check_item_data) {
952 * Check if the item size and content meet other
955 ret = check_leaf_item(fs_info, leaf, &key, slot);
960 prev_key.objectid = key.objectid;
961 prev_key.type = key.type;
962 prev_key.offset = key.offset;
968 int btrfs_check_leaf_full(struct btrfs_fs_info *fs_info,
969 struct extent_buffer *leaf)
971 return check_leaf(fs_info, leaf, true);
974 int btrfs_check_leaf_relaxed(struct btrfs_fs_info *fs_info,
975 struct extent_buffer *leaf)
977 return check_leaf(fs_info, leaf, false);
980 int btrfs_check_node(struct btrfs_fs_info *fs_info, struct extent_buffer *node)
982 unsigned long nr = btrfs_header_nritems(node);
983 struct btrfs_key key, next_key;
985 int level = btrfs_header_level(node);
989 if (level <= 0 || level >= BTRFS_MAX_LEVEL) {
990 generic_err(fs_info, node, 0,
991 "invalid level for node, have %d expect [1, %d]",
992 level, BTRFS_MAX_LEVEL - 1);
995 if (nr == 0 || nr > BTRFS_NODEPTRS_PER_BLOCK(fs_info)) {
997 "corrupt node: root=%llu block=%llu, nritems too %s, have %lu expect range [1,%u]",
998 btrfs_header_owner(node), node->start,
999 nr == 0 ? "small" : "large", nr,
1000 BTRFS_NODEPTRS_PER_BLOCK(fs_info));
1004 for (slot = 0; slot < nr - 1; slot++) {
1005 bytenr = btrfs_node_blockptr(node, slot);
1006 btrfs_node_key_to_cpu(node, &key, slot);
1007 btrfs_node_key_to_cpu(node, &next_key, slot + 1);
1010 generic_err(fs_info, node, slot,
1011 "invalid NULL node pointer");
1015 if (!IS_ALIGNED(bytenr, fs_info->sectorsize)) {
1016 generic_err(fs_info, node, slot,
1017 "unaligned pointer, have %llu should be aligned to %u",
1018 bytenr, fs_info->sectorsize);
1023 if (btrfs_comp_cpu_keys(&key, &next_key) >= 0) {
1024 generic_err(fs_info, node, slot,
1025 "bad key order, current (%llu %u %llu) next (%llu %u %llu)",
1026 key.objectid, key.type, key.offset,
1027 next_key.objectid, next_key.type,