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.
18 #include <linux/types.h>
19 #include <linux/stddef.h>
20 #include <linux/error-injection.h>
22 #include "tree-checker.h"
24 #include "compression.h"
28 * Error message should follow the following format:
29 * corrupt <type>: <identifier>, <reason>[, <bad_value>]
32 * @identifier: the necessary info to locate the leaf/node.
33 * It's recommended to decode key.objecitd/offset if it's
35 * @reason: describe the error
36 * @bad_value: optional, it's recommended to output bad value and its
37 * expected value (range).
39 * Since comma is used to separate the components, only space is allowed
40 * inside each component.
44 * Append generic "corrupt leaf/node root=%llu block=%llu slot=%d: " to @fmt.
45 * Allows callers to customize the output.
49 static void generic_err(const struct extent_buffer
*eb
, int slot
,
52 const struct btrfs_fs_info
*fs_info
= eb
->fs_info
;
62 "corrupt %s: root=%llu block=%llu slot=%d, %pV",
63 btrfs_header_level(eb
) == 0 ? "leaf" : "node",
64 btrfs_header_owner(eb
), btrfs_header_bytenr(eb
), slot
, &vaf
);
69 * Customized reporter for extent data item, since its key objectid and
70 * offset has its own meaning.
74 static void file_extent_err(const struct extent_buffer
*eb
, int slot
,
77 const struct btrfs_fs_info
*fs_info
= eb
->fs_info
;
82 btrfs_item_key_to_cpu(eb
, &key
, slot
);
89 "corrupt %s: root=%llu block=%llu slot=%d ino=%llu file_offset=%llu, %pV",
90 btrfs_header_level(eb
) == 0 ? "leaf" : "node",
91 btrfs_header_owner(eb
), btrfs_header_bytenr(eb
), slot
,
92 key
.objectid
, key
.offset
, &vaf
);
97 * Return 0 if the btrfs_file_extent_##name is aligned to @alignment
100 #define CHECK_FE_ALIGNED(leaf, slot, fi, name, alignment) \
102 if (!IS_ALIGNED(btrfs_file_extent_##name((leaf), (fi)), (alignment))) \
103 file_extent_err((leaf), (slot), \
104 "invalid %s for file extent, have %llu, should be aligned to %u", \
105 (#name), btrfs_file_extent_##name((leaf), (fi)), \
107 (!IS_ALIGNED(btrfs_file_extent_##name((leaf), (fi)), (alignment))); \
110 static u64
file_extent_end(struct extent_buffer
*leaf
,
111 struct btrfs_key
*key
,
112 struct btrfs_file_extent_item
*extent
)
117 if (btrfs_file_extent_type(leaf
, extent
) == BTRFS_FILE_EXTENT_INLINE
) {
118 len
= btrfs_file_extent_ram_bytes(leaf
, extent
);
119 end
= ALIGN(key
->offset
+ len
, leaf
->fs_info
->sectorsize
);
121 len
= btrfs_file_extent_num_bytes(leaf
, extent
);
122 end
= key
->offset
+ len
;
127 static int check_extent_data_item(struct extent_buffer
*leaf
,
128 struct btrfs_key
*key
, int slot
,
129 struct btrfs_key
*prev_key
)
131 struct btrfs_fs_info
*fs_info
= leaf
->fs_info
;
132 struct btrfs_file_extent_item
*fi
;
133 u32 sectorsize
= fs_info
->sectorsize
;
134 u32 item_size
= btrfs_item_size_nr(leaf
, slot
);
136 if (!IS_ALIGNED(key
->offset
, sectorsize
)) {
137 file_extent_err(leaf
, slot
,
138 "unaligned file_offset for file extent, have %llu should be aligned to %u",
139 key
->offset
, sectorsize
);
143 fi
= btrfs_item_ptr(leaf
, slot
, struct btrfs_file_extent_item
);
145 if (btrfs_file_extent_type(leaf
, fi
) > BTRFS_FILE_EXTENT_TYPES
) {
146 file_extent_err(leaf
, slot
,
147 "invalid type for file extent, have %u expect range [0, %u]",
148 btrfs_file_extent_type(leaf
, fi
),
149 BTRFS_FILE_EXTENT_TYPES
);
154 * Support for new compression/encryption must introduce incompat flag,
155 * and must be caught in open_ctree().
157 if (btrfs_file_extent_compression(leaf
, fi
) > BTRFS_COMPRESS_TYPES
) {
158 file_extent_err(leaf
, slot
,
159 "invalid compression for file extent, have %u expect range [0, %u]",
160 btrfs_file_extent_compression(leaf
, fi
),
161 BTRFS_COMPRESS_TYPES
);
164 if (btrfs_file_extent_encryption(leaf
, fi
)) {
165 file_extent_err(leaf
, slot
,
166 "invalid encryption for file extent, have %u expect 0",
167 btrfs_file_extent_encryption(leaf
, fi
));
170 if (btrfs_file_extent_type(leaf
, fi
) == BTRFS_FILE_EXTENT_INLINE
) {
171 /* Inline extent must have 0 as key offset */
173 file_extent_err(leaf
, slot
,
174 "invalid file_offset for inline file extent, have %llu expect 0",
179 /* Compressed inline extent has no on-disk size, skip it */
180 if (btrfs_file_extent_compression(leaf
, fi
) !=
184 /* Uncompressed inline extent size must match item size */
185 if (item_size
!= BTRFS_FILE_EXTENT_INLINE_DATA_START
+
186 btrfs_file_extent_ram_bytes(leaf
, fi
)) {
187 file_extent_err(leaf
, slot
,
188 "invalid ram_bytes for uncompressed inline extent, have %u expect %llu",
189 item_size
, BTRFS_FILE_EXTENT_INLINE_DATA_START
+
190 btrfs_file_extent_ram_bytes(leaf
, fi
));
196 /* Regular or preallocated extent has fixed item size */
197 if (item_size
!= sizeof(*fi
)) {
198 file_extent_err(leaf
, slot
,
199 "invalid item size for reg/prealloc file extent, have %u expect %zu",
200 item_size
, sizeof(*fi
));
203 if (CHECK_FE_ALIGNED(leaf
, slot
, fi
, ram_bytes
, sectorsize
) ||
204 CHECK_FE_ALIGNED(leaf
, slot
, fi
, disk_bytenr
, sectorsize
) ||
205 CHECK_FE_ALIGNED(leaf
, slot
, fi
, disk_num_bytes
, sectorsize
) ||
206 CHECK_FE_ALIGNED(leaf
, slot
, fi
, offset
, sectorsize
) ||
207 CHECK_FE_ALIGNED(leaf
, slot
, fi
, num_bytes
, sectorsize
))
211 * Check that no two consecutive file extent items, in the same leaf,
212 * present ranges that overlap each other.
215 prev_key
->objectid
== key
->objectid
&&
216 prev_key
->type
== BTRFS_EXTENT_DATA_KEY
) {
217 struct btrfs_file_extent_item
*prev_fi
;
220 prev_fi
= btrfs_item_ptr(leaf
, slot
- 1,
221 struct btrfs_file_extent_item
);
222 prev_end
= file_extent_end(leaf
, prev_key
, prev_fi
);
223 if (prev_end
> key
->offset
) {
224 file_extent_err(leaf
, slot
- 1,
225 "file extent end range (%llu) goes beyond start offset (%llu) of the next file extent",
226 prev_end
, key
->offset
);
234 static int check_csum_item(struct extent_buffer
*leaf
, struct btrfs_key
*key
,
237 struct btrfs_fs_info
*fs_info
= leaf
->fs_info
;
238 u32 sectorsize
= fs_info
->sectorsize
;
239 u32 csumsize
= btrfs_super_csum_size(fs_info
->super_copy
);
241 if (key
->objectid
!= BTRFS_EXTENT_CSUM_OBJECTID
) {
242 generic_err(leaf
, slot
,
243 "invalid key objectid for csum item, have %llu expect %llu",
244 key
->objectid
, BTRFS_EXTENT_CSUM_OBJECTID
);
247 if (!IS_ALIGNED(key
->offset
, sectorsize
)) {
248 generic_err(leaf
, slot
,
249 "unaligned key offset for csum item, have %llu should be aligned to %u",
250 key
->offset
, sectorsize
);
253 if (!IS_ALIGNED(btrfs_item_size_nr(leaf
, slot
), csumsize
)) {
254 generic_err(leaf
, slot
,
255 "unaligned item size for csum item, have %u should be aligned to %u",
256 btrfs_item_size_nr(leaf
, slot
), csumsize
);
263 * Customized reported for dir_item, only important new info is key->objectid,
264 * which represents inode number
268 static void dir_item_err(const struct extent_buffer
*eb
, int slot
,
269 const char *fmt
, ...)
271 const struct btrfs_fs_info
*fs_info
= eb
->fs_info
;
272 struct btrfs_key key
;
273 struct va_format vaf
;
276 btrfs_item_key_to_cpu(eb
, &key
, slot
);
283 "corrupt %s: root=%llu block=%llu slot=%d ino=%llu, %pV",
284 btrfs_header_level(eb
) == 0 ? "leaf" : "node",
285 btrfs_header_owner(eb
), btrfs_header_bytenr(eb
), slot
,
290 static int check_dir_item(struct extent_buffer
*leaf
,
291 struct btrfs_key
*key
, int slot
)
293 struct btrfs_fs_info
*fs_info
= leaf
->fs_info
;
294 struct btrfs_dir_item
*di
;
295 u32 item_size
= btrfs_item_size_nr(leaf
, slot
);
298 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
299 while (cur
< item_size
) {
307 /* header itself should not cross item boundary */
308 if (cur
+ sizeof(*di
) > item_size
) {
309 dir_item_err(leaf
, slot
,
310 "dir item header crosses item boundary, have %zu boundary %u",
311 cur
+ sizeof(*di
), item_size
);
316 dir_type
= btrfs_dir_type(leaf
, di
);
317 if (dir_type
>= BTRFS_FT_MAX
) {
318 dir_item_err(leaf
, slot
,
319 "invalid dir item type, have %u expect [0, %u)",
320 dir_type
, BTRFS_FT_MAX
);
324 if (key
->type
== BTRFS_XATTR_ITEM_KEY
&&
325 dir_type
!= BTRFS_FT_XATTR
) {
326 dir_item_err(leaf
, slot
,
327 "invalid dir item type for XATTR key, have %u expect %u",
328 dir_type
, BTRFS_FT_XATTR
);
331 if (dir_type
== BTRFS_FT_XATTR
&&
332 key
->type
!= BTRFS_XATTR_ITEM_KEY
) {
333 dir_item_err(leaf
, slot
,
334 "xattr dir type found for non-XATTR key");
337 if (dir_type
== BTRFS_FT_XATTR
)
338 max_name_len
= XATTR_NAME_MAX
;
340 max_name_len
= BTRFS_NAME_LEN
;
342 /* Name/data length check */
343 name_len
= btrfs_dir_name_len(leaf
, di
);
344 data_len
= btrfs_dir_data_len(leaf
, di
);
345 if (name_len
> max_name_len
) {
346 dir_item_err(leaf
, slot
,
347 "dir item name len too long, have %u max %u",
348 name_len
, max_name_len
);
351 if (name_len
+ data_len
> BTRFS_MAX_XATTR_SIZE(fs_info
)) {
352 dir_item_err(leaf
, slot
,
353 "dir item name and data len too long, have %u max %u",
355 BTRFS_MAX_XATTR_SIZE(fs_info
));
359 if (data_len
&& dir_type
!= BTRFS_FT_XATTR
) {
360 dir_item_err(leaf
, slot
,
361 "dir item with invalid data len, have %u expect 0",
366 total_size
= sizeof(*di
) + name_len
+ data_len
;
368 /* header and name/data should not cross item boundary */
369 if (cur
+ total_size
> item_size
) {
370 dir_item_err(leaf
, slot
,
371 "dir item data crosses item boundary, have %u boundary %u",
372 cur
+ total_size
, item_size
);
377 * Special check for XATTR/DIR_ITEM, as key->offset is name
378 * hash, should match its name
380 if (key
->type
== BTRFS_DIR_ITEM_KEY
||
381 key
->type
== BTRFS_XATTR_ITEM_KEY
) {
382 char namebuf
[max(BTRFS_NAME_LEN
, XATTR_NAME_MAX
)];
384 read_extent_buffer(leaf
, namebuf
,
385 (unsigned long)(di
+ 1), name_len
);
386 name_hash
= btrfs_name_hash(namebuf
, name_len
);
387 if (key
->offset
!= name_hash
) {
388 dir_item_err(leaf
, slot
,
389 "name hash mismatch with key, have 0x%016x expect 0x%016llx",
390 name_hash
, key
->offset
);
395 di
= (struct btrfs_dir_item
*)((void *)di
+ total_size
);
402 static void block_group_err(const struct extent_buffer
*eb
, int slot
,
403 const char *fmt
, ...)
405 const struct btrfs_fs_info
*fs_info
= eb
->fs_info
;
406 struct btrfs_key key
;
407 struct va_format vaf
;
410 btrfs_item_key_to_cpu(eb
, &key
, slot
);
417 "corrupt %s: root=%llu block=%llu slot=%d bg_start=%llu bg_len=%llu, %pV",
418 btrfs_header_level(eb
) == 0 ? "leaf" : "node",
419 btrfs_header_owner(eb
), btrfs_header_bytenr(eb
), slot
,
420 key
.objectid
, key
.offset
, &vaf
);
424 static int check_block_group_item(struct extent_buffer
*leaf
,
425 struct btrfs_key
*key
, int slot
)
427 struct btrfs_block_group_item bgi
;
428 u32 item_size
= btrfs_item_size_nr(leaf
, slot
);
433 * Here we don't really care about alignment since extent allocator can
434 * handle it. We care more about the size.
436 if (key
->offset
== 0) {
437 block_group_err(leaf
, slot
,
438 "invalid block group size 0");
442 if (item_size
!= sizeof(bgi
)) {
443 block_group_err(leaf
, slot
,
444 "invalid item size, have %u expect %zu",
445 item_size
, sizeof(bgi
));
449 read_extent_buffer(leaf
, &bgi
, btrfs_item_ptr_offset(leaf
, slot
),
451 if (btrfs_block_group_chunk_objectid(&bgi
) !=
452 BTRFS_FIRST_CHUNK_TREE_OBJECTID
) {
453 block_group_err(leaf
, slot
,
454 "invalid block group chunk objectid, have %llu expect %llu",
455 btrfs_block_group_chunk_objectid(&bgi
),
456 BTRFS_FIRST_CHUNK_TREE_OBJECTID
);
460 if (btrfs_block_group_used(&bgi
) > key
->offset
) {
461 block_group_err(leaf
, slot
,
462 "invalid block group used, have %llu expect [0, %llu)",
463 btrfs_block_group_used(&bgi
), key
->offset
);
467 flags
= btrfs_block_group_flags(&bgi
);
468 if (hweight64(flags
& BTRFS_BLOCK_GROUP_PROFILE_MASK
) > 1) {
469 block_group_err(leaf
, slot
,
470 "invalid profile flags, have 0x%llx (%lu bits set) expect no more than 1 bit set",
471 flags
& BTRFS_BLOCK_GROUP_PROFILE_MASK
,
472 hweight64(flags
& BTRFS_BLOCK_GROUP_PROFILE_MASK
));
476 type
= flags
& BTRFS_BLOCK_GROUP_TYPE_MASK
;
477 if (type
!= BTRFS_BLOCK_GROUP_DATA
&&
478 type
!= BTRFS_BLOCK_GROUP_METADATA
&&
479 type
!= BTRFS_BLOCK_GROUP_SYSTEM
&&
480 type
!= (BTRFS_BLOCK_GROUP_METADATA
|
481 BTRFS_BLOCK_GROUP_DATA
)) {
482 block_group_err(leaf
, slot
,
483 "invalid type, have 0x%llx (%lu bits set) expect either 0x%llx, 0x%llx, 0x%llx or 0x%llx",
484 type
, hweight64(type
),
485 BTRFS_BLOCK_GROUP_DATA
, BTRFS_BLOCK_GROUP_METADATA
,
486 BTRFS_BLOCK_GROUP_SYSTEM
,
487 BTRFS_BLOCK_GROUP_METADATA
| BTRFS_BLOCK_GROUP_DATA
);
495 static void chunk_err(const struct extent_buffer
*leaf
,
496 const struct btrfs_chunk
*chunk
, u64 logical
,
497 const char *fmt
, ...)
499 const struct btrfs_fs_info
*fs_info
= leaf
->fs_info
;
501 struct va_format vaf
;
506 /* Only superblock eb is able to have such small offset */
507 is_sb
= (leaf
->start
== BTRFS_SUPER_INFO_OFFSET
);
511 * Get the slot number by iterating through all slots, this
512 * would provide better readability.
514 for (i
= 0; i
< btrfs_header_nritems(leaf
); i
++) {
515 if (btrfs_item_ptr_offset(leaf
, i
) ==
516 (unsigned long)chunk
) {
528 "corrupt superblock syschunk array: chunk_start=%llu, %pV",
532 "corrupt leaf: root=%llu block=%llu slot=%d chunk_start=%llu, %pV",
533 BTRFS_CHUNK_TREE_OBJECTID
, leaf
->start
, slot
,
539 * The common chunk check which could also work on super block sys chunk array.
541 * Return -EUCLEAN if anything is corrupted.
542 * Return 0 if everything is OK.
544 int btrfs_check_chunk_valid(struct extent_buffer
*leaf
,
545 struct btrfs_chunk
*chunk
, u64 logical
)
547 struct btrfs_fs_info
*fs_info
= leaf
->fs_info
;
556 length
= btrfs_chunk_length(leaf
, chunk
);
557 stripe_len
= btrfs_chunk_stripe_len(leaf
, chunk
);
558 num_stripes
= btrfs_chunk_num_stripes(leaf
, chunk
);
559 sub_stripes
= btrfs_chunk_sub_stripes(leaf
, chunk
);
560 type
= btrfs_chunk_type(leaf
, chunk
);
563 chunk_err(leaf
, chunk
, logical
,
564 "invalid chunk num_stripes, have %u", num_stripes
);
567 if (!IS_ALIGNED(logical
, fs_info
->sectorsize
)) {
568 chunk_err(leaf
, chunk
, logical
,
569 "invalid chunk logical, have %llu should aligned to %u",
570 logical
, fs_info
->sectorsize
);
573 if (btrfs_chunk_sector_size(leaf
, chunk
) != fs_info
->sectorsize
) {
574 chunk_err(leaf
, chunk
, logical
,
575 "invalid chunk sectorsize, have %u expect %u",
576 btrfs_chunk_sector_size(leaf
, chunk
),
577 fs_info
->sectorsize
);
580 if (!length
|| !IS_ALIGNED(length
, fs_info
->sectorsize
)) {
581 chunk_err(leaf
, chunk
, logical
,
582 "invalid chunk length, have %llu", length
);
585 if (!is_power_of_2(stripe_len
) || stripe_len
!= BTRFS_STRIPE_LEN
) {
586 chunk_err(leaf
, chunk
, logical
,
587 "invalid chunk stripe length: %llu",
591 if (~(BTRFS_BLOCK_GROUP_TYPE_MASK
| BTRFS_BLOCK_GROUP_PROFILE_MASK
) &
593 chunk_err(leaf
, chunk
, logical
,
594 "unrecognized chunk type: 0x%llx",
595 ~(BTRFS_BLOCK_GROUP_TYPE_MASK
|
596 BTRFS_BLOCK_GROUP_PROFILE_MASK
) &
597 btrfs_chunk_type(leaf
, chunk
));
601 if (!is_power_of_2(type
& BTRFS_BLOCK_GROUP_PROFILE_MASK
) &&
602 (type
& BTRFS_BLOCK_GROUP_PROFILE_MASK
) != 0) {
603 chunk_err(leaf
, chunk
, logical
,
604 "invalid chunk profile flag: 0x%llx, expect 0 or 1 bit set",
605 type
& BTRFS_BLOCK_GROUP_PROFILE_MASK
);
608 if ((type
& BTRFS_BLOCK_GROUP_TYPE_MASK
) == 0) {
609 chunk_err(leaf
, chunk
, logical
,
610 "missing chunk type flag, have 0x%llx one bit must be set in 0x%llx",
611 type
, BTRFS_BLOCK_GROUP_TYPE_MASK
);
615 if ((type
& BTRFS_BLOCK_GROUP_SYSTEM
) &&
616 (type
& (BTRFS_BLOCK_GROUP_METADATA
| BTRFS_BLOCK_GROUP_DATA
))) {
617 chunk_err(leaf
, chunk
, logical
,
618 "system chunk with data or metadata type: 0x%llx",
623 features
= btrfs_super_incompat_flags(fs_info
->super_copy
);
624 if (features
& BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS
)
628 if ((type
& BTRFS_BLOCK_GROUP_METADATA
) &&
629 (type
& BTRFS_BLOCK_GROUP_DATA
)) {
630 chunk_err(leaf
, chunk
, logical
,
631 "mixed chunk type in non-mixed mode: 0x%llx", type
);
636 if ((type
& BTRFS_BLOCK_GROUP_RAID10
&& sub_stripes
!= 2) ||
637 (type
& BTRFS_BLOCK_GROUP_RAID1
&& num_stripes
!= 2) ||
638 (type
& BTRFS_BLOCK_GROUP_RAID5
&& num_stripes
< 2) ||
639 (type
& BTRFS_BLOCK_GROUP_RAID6
&& num_stripes
< 3) ||
640 (type
& BTRFS_BLOCK_GROUP_DUP
&& num_stripes
!= 2) ||
641 ((type
& BTRFS_BLOCK_GROUP_PROFILE_MASK
) == 0 && num_stripes
!= 1)) {
642 chunk_err(leaf
, chunk
, logical
,
643 "invalid num_stripes:sub_stripes %u:%u for profile %llu",
644 num_stripes
, sub_stripes
,
645 type
& BTRFS_BLOCK_GROUP_PROFILE_MASK
);
654 static void dev_item_err(const struct extent_buffer
*eb
, int slot
,
655 const char *fmt
, ...)
657 struct btrfs_key key
;
658 struct va_format vaf
;
661 btrfs_item_key_to_cpu(eb
, &key
, slot
);
667 btrfs_crit(eb
->fs_info
,
668 "corrupt %s: root=%llu block=%llu slot=%d devid=%llu %pV",
669 btrfs_header_level(eb
) == 0 ? "leaf" : "node",
670 btrfs_header_owner(eb
), btrfs_header_bytenr(eb
), slot
,
675 static int check_dev_item(struct extent_buffer
*leaf
,
676 struct btrfs_key
*key
, int slot
)
678 struct btrfs_fs_info
*fs_info
= leaf
->fs_info
;
679 struct btrfs_dev_item
*ditem
;
680 u64 max_devid
= max(BTRFS_MAX_DEVS(fs_info
), BTRFS_MAX_DEVS_SYS_CHUNK
);
682 if (key
->objectid
!= BTRFS_DEV_ITEMS_OBJECTID
) {
683 dev_item_err(leaf
, slot
,
684 "invalid objectid: has=%llu expect=%llu",
685 key
->objectid
, BTRFS_DEV_ITEMS_OBJECTID
);
688 if (key
->offset
> max_devid
) {
689 dev_item_err(leaf
, slot
,
690 "invalid devid: has=%llu expect=[0, %llu]",
691 key
->offset
, max_devid
);
694 ditem
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dev_item
);
695 if (btrfs_device_id(leaf
, ditem
) != key
->offset
) {
696 dev_item_err(leaf
, slot
,
697 "devid mismatch: key has=%llu item has=%llu",
698 key
->offset
, btrfs_device_id(leaf
, ditem
));
703 * For device total_bytes, we don't have reliable way to check it, as
704 * it can be 0 for device removal. Device size check can only be done
705 * by dev extents check.
707 if (btrfs_device_bytes_used(leaf
, ditem
) >
708 btrfs_device_total_bytes(leaf
, ditem
)) {
709 dev_item_err(leaf
, slot
,
710 "invalid bytes used: have %llu expect [0, %llu]",
711 btrfs_device_bytes_used(leaf
, ditem
),
712 btrfs_device_total_bytes(leaf
, ditem
));
716 * Remaining members like io_align/type/gen/dev_group aren't really
717 * utilized. Skip them to make later usage of them easier.
722 /* Inode item error output has the same format as dir_item_err() */
723 #define inode_item_err(fs_info, eb, slot, fmt, ...) \
724 dir_item_err(eb, slot, fmt, __VA_ARGS__)
726 static int check_inode_item(struct extent_buffer
*leaf
,
727 struct btrfs_key
*key
, int slot
)
729 struct btrfs_fs_info
*fs_info
= leaf
->fs_info
;
730 struct btrfs_inode_item
*iitem
;
731 u64 super_gen
= btrfs_super_generation(fs_info
->super_copy
);
732 u32 valid_mask
= (S_IFMT
| S_ISUID
| S_ISGID
| S_ISVTX
| 0777);
735 if ((key
->objectid
< BTRFS_FIRST_FREE_OBJECTID
||
736 key
->objectid
> BTRFS_LAST_FREE_OBJECTID
) &&
737 key
->objectid
!= BTRFS_ROOT_TREE_DIR_OBJECTID
&&
738 key
->objectid
!= BTRFS_FREE_INO_OBJECTID
) {
739 generic_err(leaf
, slot
,
740 "invalid key objectid: has %llu expect %llu or [%llu, %llu] or %llu",
741 key
->objectid
, BTRFS_ROOT_TREE_DIR_OBJECTID
,
742 BTRFS_FIRST_FREE_OBJECTID
,
743 BTRFS_LAST_FREE_OBJECTID
,
744 BTRFS_FREE_INO_OBJECTID
);
747 if (key
->offset
!= 0) {
748 inode_item_err(fs_info
, leaf
, slot
,
749 "invalid key offset: has %llu expect 0",
753 iitem
= btrfs_item_ptr(leaf
, slot
, struct btrfs_inode_item
);
755 /* Here we use super block generation + 1 to handle log tree */
756 if (btrfs_inode_generation(leaf
, iitem
) > super_gen
+ 1) {
757 inode_item_err(fs_info
, leaf
, slot
,
758 "invalid inode generation: has %llu expect (0, %llu]",
759 btrfs_inode_generation(leaf
, iitem
),
763 /* Note for ROOT_TREE_DIR_ITEM, mkfs could set its transid 0 */
764 if (btrfs_inode_transid(leaf
, iitem
) > super_gen
+ 1) {
765 inode_item_err(fs_info
, leaf
, slot
,
766 "invalid inode generation: has %llu expect [0, %llu]",
767 btrfs_inode_transid(leaf
, iitem
), super_gen
+ 1);
772 * For size and nbytes it's better not to be too strict, as for dir
773 * item its size/nbytes can easily get wrong, but doesn't affect
774 * anything in the fs. So here we skip the check.
776 mode
= btrfs_inode_mode(leaf
, iitem
);
777 if (mode
& ~valid_mask
) {
778 inode_item_err(fs_info
, leaf
, slot
,
779 "unknown mode bit detected: 0x%x",
785 * S_IFMT is not bit mapped so we can't completely rely on is_power_of_2,
786 * but is_power_of_2() can save us from checking FIFO/CHR/DIR/REG.
787 * Only needs to check BLK, LNK and SOCKS
789 if (!is_power_of_2(mode
& S_IFMT
)) {
790 if (!S_ISLNK(mode
) && !S_ISBLK(mode
) && !S_ISSOCK(mode
)) {
791 inode_item_err(fs_info
, leaf
, slot
,
792 "invalid mode: has 0%o expect valid S_IF* bit(s)",
797 if (S_ISDIR(mode
) && btrfs_inode_nlink(leaf
, iitem
) > 1) {
798 inode_item_err(fs_info
, leaf
, slot
,
799 "invalid nlink: has %u expect no more than 1 for dir",
800 btrfs_inode_nlink(leaf
, iitem
));
803 if (btrfs_inode_flags(leaf
, iitem
) & ~BTRFS_INODE_FLAG_MASK
) {
804 inode_item_err(fs_info
, leaf
, slot
,
805 "unknown flags detected: 0x%llx",
806 btrfs_inode_flags(leaf
, iitem
) &
807 ~BTRFS_INODE_FLAG_MASK
);
814 * Common point to switch the item-specific validation.
816 static int check_leaf_item(struct extent_buffer
*leaf
,
817 struct btrfs_key
*key
, int slot
,
818 struct btrfs_key
*prev_key
)
821 struct btrfs_chunk
*chunk
;
824 case BTRFS_EXTENT_DATA_KEY
:
825 ret
= check_extent_data_item(leaf
, key
, slot
, prev_key
);
827 case BTRFS_EXTENT_CSUM_KEY
:
828 ret
= check_csum_item(leaf
, key
, slot
);
830 case BTRFS_DIR_ITEM_KEY
:
831 case BTRFS_DIR_INDEX_KEY
:
832 case BTRFS_XATTR_ITEM_KEY
:
833 ret
= check_dir_item(leaf
, key
, slot
);
835 case BTRFS_BLOCK_GROUP_ITEM_KEY
:
836 ret
= check_block_group_item(leaf
, key
, slot
);
838 case BTRFS_CHUNK_ITEM_KEY
:
839 chunk
= btrfs_item_ptr(leaf
, slot
, struct btrfs_chunk
);
840 ret
= btrfs_check_chunk_valid(leaf
, chunk
, key
->offset
);
842 case BTRFS_DEV_ITEM_KEY
:
843 ret
= check_dev_item(leaf
, key
, slot
);
845 case BTRFS_INODE_ITEM_KEY
:
846 ret
= check_inode_item(leaf
, key
, slot
);
852 static int check_leaf(struct extent_buffer
*leaf
, bool check_item_data
)
854 struct btrfs_fs_info
*fs_info
= leaf
->fs_info
;
855 /* No valid key type is 0, so all key should be larger than this key */
856 struct btrfs_key prev_key
= {0, 0, 0};
857 struct btrfs_key key
;
858 u32 nritems
= btrfs_header_nritems(leaf
);
861 if (btrfs_header_level(leaf
) != 0) {
863 "invalid level for leaf, have %d expect 0",
864 btrfs_header_level(leaf
));
869 * Extent buffers from a relocation tree have a owner field that
870 * corresponds to the subvolume tree they are based on. So just from an
871 * extent buffer alone we can not find out what is the id of the
872 * corresponding subvolume tree, so we can not figure out if the extent
873 * buffer corresponds to the root of the relocation tree or not. So
874 * skip this check for relocation trees.
876 if (nritems
== 0 && !btrfs_header_flag(leaf
, BTRFS_HEADER_FLAG_RELOC
)) {
877 u64 owner
= btrfs_header_owner(leaf
);
879 /* These trees must never be empty */
880 if (owner
== BTRFS_ROOT_TREE_OBJECTID
||
881 owner
== BTRFS_CHUNK_TREE_OBJECTID
||
882 owner
== BTRFS_EXTENT_TREE_OBJECTID
||
883 owner
== BTRFS_DEV_TREE_OBJECTID
||
884 owner
== BTRFS_FS_TREE_OBJECTID
||
885 owner
== BTRFS_DATA_RELOC_TREE_OBJECTID
) {
887 "invalid root, root %llu must never be empty",
898 * Check the following things to make sure this is a good leaf, and
899 * leaf users won't need to bother with similar sanity checks:
902 * 2) item offset and size
903 * No overlap, no hole, all inside the leaf.
905 * If possible, do comprehensive sanity check.
906 * NOTE: All checks must only rely on the item data itself.
908 for (slot
= 0; slot
< nritems
; slot
++) {
909 u32 item_end_expected
;
912 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
914 /* Make sure the keys are in the right order */
915 if (btrfs_comp_cpu_keys(&prev_key
, &key
) >= 0) {
916 generic_err(leaf
, slot
,
917 "bad key order, prev (%llu %u %llu) current (%llu %u %llu)",
918 prev_key
.objectid
, prev_key
.type
,
919 prev_key
.offset
, key
.objectid
, key
.type
,
925 * Make sure the offset and ends are right, remember that the
926 * item data starts at the end of the leaf and grows towards the
930 item_end_expected
= BTRFS_LEAF_DATA_SIZE(fs_info
);
932 item_end_expected
= btrfs_item_offset_nr(leaf
,
934 if (btrfs_item_end_nr(leaf
, slot
) != item_end_expected
) {
935 generic_err(leaf
, slot
,
936 "unexpected item end, have %u expect %u",
937 btrfs_item_end_nr(leaf
, slot
),
943 * Check to make sure that we don't point outside of the leaf,
944 * just in case all the items are consistent to each other, but
945 * all point outside of the leaf.
947 if (btrfs_item_end_nr(leaf
, slot
) >
948 BTRFS_LEAF_DATA_SIZE(fs_info
)) {
949 generic_err(leaf
, slot
,
950 "slot end outside of leaf, have %u expect range [0, %u]",
951 btrfs_item_end_nr(leaf
, slot
),
952 BTRFS_LEAF_DATA_SIZE(fs_info
));
956 /* Also check if the item pointer overlaps with btrfs item. */
957 if (btrfs_item_nr_offset(slot
) + sizeof(struct btrfs_item
) >
958 btrfs_item_ptr_offset(leaf
, slot
)) {
959 generic_err(leaf
, slot
,
960 "slot overlaps with its data, item end %lu data start %lu",
961 btrfs_item_nr_offset(slot
) +
962 sizeof(struct btrfs_item
),
963 btrfs_item_ptr_offset(leaf
, slot
));
967 if (check_item_data
) {
969 * Check if the item size and content meet other
972 ret
= check_leaf_item(leaf
, &key
, slot
, &prev_key
);
977 prev_key
.objectid
= key
.objectid
;
978 prev_key
.type
= key
.type
;
979 prev_key
.offset
= key
.offset
;
985 int btrfs_check_leaf_full(struct extent_buffer
*leaf
)
987 return check_leaf(leaf
, true);
989 ALLOW_ERROR_INJECTION(btrfs_check_leaf_full
, ERRNO
);
991 int btrfs_check_leaf_relaxed(struct extent_buffer
*leaf
)
993 return check_leaf(leaf
, false);
996 int btrfs_check_node(struct extent_buffer
*node
)
998 struct btrfs_fs_info
*fs_info
= node
->fs_info
;
999 unsigned long nr
= btrfs_header_nritems(node
);
1000 struct btrfs_key key
, next_key
;
1002 int level
= btrfs_header_level(node
);
1006 if (level
<= 0 || level
>= BTRFS_MAX_LEVEL
) {
1007 generic_err(node
, 0,
1008 "invalid level for node, have %d expect [1, %d]",
1009 level
, BTRFS_MAX_LEVEL
- 1);
1012 if (nr
== 0 || nr
> BTRFS_NODEPTRS_PER_BLOCK(fs_info
)) {
1014 "corrupt node: root=%llu block=%llu, nritems too %s, have %lu expect range [1,%u]",
1015 btrfs_header_owner(node
), node
->start
,
1016 nr
== 0 ? "small" : "large", nr
,
1017 BTRFS_NODEPTRS_PER_BLOCK(fs_info
));
1021 for (slot
= 0; slot
< nr
- 1; slot
++) {
1022 bytenr
= btrfs_node_blockptr(node
, slot
);
1023 btrfs_node_key_to_cpu(node
, &key
, slot
);
1024 btrfs_node_key_to_cpu(node
, &next_key
, slot
+ 1);
1027 generic_err(node
, slot
,
1028 "invalid NULL node pointer");
1032 if (!IS_ALIGNED(bytenr
, fs_info
->sectorsize
)) {
1033 generic_err(node
, slot
,
1034 "unaligned pointer, have %llu should be aligned to %u",
1035 bytenr
, fs_info
->sectorsize
);
1040 if (btrfs_comp_cpu_keys(&key
, &next_key
) >= 0) {
1041 generic_err(node
, slot
,
1042 "bad key order, current (%llu %u %llu) next (%llu %u %llu)",
1043 key
.objectid
, key
.type
, key
.offset
,
1044 next_key
.objectid
, next_key
.type
,
1053 ALLOW_ERROR_INJECTION(btrfs_check_node
, ERRNO
);