2 * Copyright (C) Qu Wenruo 2017. All rights reserved.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public
14 * License along with this program.
18 * The module is used to catch unexpected/corrupted tree block data.
19 * Such behavior can be caused either by a fuzzed image or bugs.
21 * The objective is to do leaf/node validation checks when tree block is read
22 * from disk, and check *every* possible member, so other code won't
23 * need to checking them again.
25 * Due to the potential and unwanted damage, every checker needs to be
26 * carefully reviewed otherwise so it does not prevent mount of valid images.
30 #include "tree-checker.h"
32 #include "compression.h"
36 * Error message should follow the following format:
37 * corrupt <type>: <identifier>, <reason>[, <bad_value>]
40 * @identifier: the necessary info to locate the leaf/node.
41 * It's recommened to decode key.objecitd/offset if it's
43 * @reason: describe the error
44 * @bad_value: optional, it's recommened to output bad value and its
45 * expected value (range).
47 * Since comma is used to separate the components, only space is allowed
48 * inside each component.
52 * Append generic "corrupt leaf/node root=%llu block=%llu slot=%d: " to @fmt.
53 * Allows callers to customize the output.
56 static void generic_err(const struct btrfs_root
*root
,
57 const struct extent_buffer
*eb
, int slot
,
68 btrfs_crit(root
->fs_info
,
69 "corrupt %s: root=%llu block=%llu slot=%d, %pV",
70 btrfs_header_level(eb
) == 0 ? "leaf" : "node",
71 root
->objectid
, btrfs_header_bytenr(eb
), slot
, &vaf
);
76 * Customized reporter for extent data item, since its key objectid and
77 * offset has its own meaning.
80 static void file_extent_err(const struct btrfs_root
*root
,
81 const struct extent_buffer
*eb
, int slot
,
88 btrfs_item_key_to_cpu(eb
, &key
, slot
);
94 btrfs_crit(root
->fs_info
,
95 "corrupt %s: root=%llu block=%llu slot=%d ino=%llu file_offset=%llu, %pV",
96 btrfs_header_level(eb
) == 0 ? "leaf" : "node", root
->objectid
,
97 btrfs_header_bytenr(eb
), slot
, key
.objectid
, key
.offset
, &vaf
);
102 * Return 0 if the btrfs_file_extent_##name is aligned to @alignment
105 #define CHECK_FE_ALIGNED(root, leaf, slot, fi, name, alignment) \
107 if (!IS_ALIGNED(btrfs_file_extent_##name((leaf), (fi)), (alignment))) \
108 file_extent_err((root), (leaf), (slot), \
109 "invalid %s for file extent, have %llu, should be aligned to %u", \
110 (#name), btrfs_file_extent_##name((leaf), (fi)), \
112 (!IS_ALIGNED(btrfs_file_extent_##name((leaf), (fi)), (alignment))); \
115 static int check_extent_data_item(struct btrfs_root
*root
,
116 struct extent_buffer
*leaf
,
117 struct btrfs_key
*key
, int slot
)
119 struct btrfs_file_extent_item
*fi
;
120 u32 sectorsize
= root
->fs_info
->sectorsize
;
121 u32 item_size
= btrfs_item_size_nr(leaf
, slot
);
123 if (!IS_ALIGNED(key
->offset
, sectorsize
)) {
124 file_extent_err(root
, leaf
, slot
,
125 "unaligned file_offset for file extent, have %llu should be aligned to %u",
126 key
->offset
, sectorsize
);
130 fi
= btrfs_item_ptr(leaf
, slot
, struct btrfs_file_extent_item
);
132 if (btrfs_file_extent_type(leaf
, fi
) > BTRFS_FILE_EXTENT_TYPES
) {
133 file_extent_err(root
, leaf
, slot
,
134 "invalid type for file extent, have %u expect range [0, %u]",
135 btrfs_file_extent_type(leaf
, fi
),
136 BTRFS_FILE_EXTENT_TYPES
);
141 * Support for new compression/encrption must introduce incompat flag,
142 * and must be caught in open_ctree().
144 if (btrfs_file_extent_compression(leaf
, fi
) > BTRFS_COMPRESS_TYPES
) {
145 file_extent_err(root
, leaf
, slot
,
146 "invalid compression for file extent, have %u expect range [0, %u]",
147 btrfs_file_extent_compression(leaf
, fi
),
148 BTRFS_COMPRESS_TYPES
);
151 if (btrfs_file_extent_encryption(leaf
, fi
)) {
152 file_extent_err(root
, leaf
, slot
,
153 "invalid encryption for file extent, have %u expect 0",
154 btrfs_file_extent_encryption(leaf
, fi
));
157 if (btrfs_file_extent_type(leaf
, fi
) == BTRFS_FILE_EXTENT_INLINE
) {
158 /* Inline extent must have 0 as key offset */
160 file_extent_err(root
, leaf
, slot
,
161 "invalid file_offset for inline file extent, have %llu expect 0",
166 /* Compressed inline extent has no on-disk size, skip it */
167 if (btrfs_file_extent_compression(leaf
, fi
) !=
171 /* Uncompressed inline extent size must match item size */
172 if (item_size
!= BTRFS_FILE_EXTENT_INLINE_DATA_START
+
173 btrfs_file_extent_ram_bytes(leaf
, fi
)) {
174 file_extent_err(root
, leaf
, slot
,
175 "invalid ram_bytes for uncompressed inline extent, have %u expect %llu",
176 item_size
, BTRFS_FILE_EXTENT_INLINE_DATA_START
+
177 btrfs_file_extent_ram_bytes(leaf
, fi
));
183 /* Regular or preallocated extent has fixed item size */
184 if (item_size
!= sizeof(*fi
)) {
185 file_extent_err(root
, leaf
, slot
,
186 "invalid item size for reg/prealloc file extent, have %u expect %zu",
187 item_size
, sizeof(*fi
));
190 if (CHECK_FE_ALIGNED(root
, leaf
, slot
, fi
, ram_bytes
, sectorsize
) ||
191 CHECK_FE_ALIGNED(root
, leaf
, slot
, fi
, disk_bytenr
, sectorsize
) ||
192 CHECK_FE_ALIGNED(root
, leaf
, slot
, fi
, disk_num_bytes
, sectorsize
) ||
193 CHECK_FE_ALIGNED(root
, leaf
, slot
, fi
, offset
, sectorsize
) ||
194 CHECK_FE_ALIGNED(root
, leaf
, slot
, fi
, num_bytes
, sectorsize
))
199 static int check_csum_item(struct btrfs_root
*root
, struct extent_buffer
*leaf
,
200 struct btrfs_key
*key
, int slot
)
202 u32 sectorsize
= root
->fs_info
->sectorsize
;
203 u32 csumsize
= btrfs_super_csum_size(root
->fs_info
->super_copy
);
205 if (key
->objectid
!= BTRFS_EXTENT_CSUM_OBJECTID
) {
206 generic_err(root
, leaf
, slot
,
207 "invalid key objectid for csum item, have %llu expect %llu",
208 key
->objectid
, BTRFS_EXTENT_CSUM_OBJECTID
);
211 if (!IS_ALIGNED(key
->offset
, sectorsize
)) {
212 generic_err(root
, leaf
, slot
,
213 "unaligned key offset for csum item, have %llu should be aligned to %u",
214 key
->offset
, sectorsize
);
217 if (!IS_ALIGNED(btrfs_item_size_nr(leaf
, slot
), csumsize
)) {
218 generic_err(root
, leaf
, slot
,
219 "unaligned item size for csum item, have %u should be aligned to %u",
220 btrfs_item_size_nr(leaf
, slot
), csumsize
);
227 * Customized reported for dir_item, only important new info is key->objectid,
228 * which represents inode number
231 static void dir_item_err(const struct btrfs_root
*root
,
232 const struct extent_buffer
*eb
, int slot
,
233 const char *fmt
, ...)
235 struct btrfs_key key
;
236 struct va_format vaf
;
239 btrfs_item_key_to_cpu(eb
, &key
, slot
);
245 btrfs_crit(root
->fs_info
,
246 "corrupt %s: root=%llu block=%llu slot=%d ino=%llu, %pV",
247 btrfs_header_level(eb
) == 0 ? "leaf" : "node", root
->objectid
,
248 btrfs_header_bytenr(eb
), slot
, key
.objectid
, &vaf
);
252 static int check_dir_item(struct btrfs_root
*root
,
253 struct extent_buffer
*leaf
,
254 struct btrfs_key
*key
, int slot
)
256 struct btrfs_dir_item
*di
;
257 u32 item_size
= btrfs_item_size_nr(leaf
, slot
);
260 di
= btrfs_item_ptr(leaf
, slot
, struct btrfs_dir_item
);
261 while (cur
< item_size
) {
269 /* header itself should not cross item boundary */
270 if (cur
+ sizeof(*di
) > item_size
) {
271 dir_item_err(root
, leaf
, slot
,
272 "dir item header crosses item boundary, have %zu boundary %u",
273 cur
+ sizeof(*di
), item_size
);
278 dir_type
= btrfs_dir_type(leaf
, di
);
279 if (dir_type
>= BTRFS_FT_MAX
) {
280 dir_item_err(root
, leaf
, slot
,
281 "invalid dir item type, have %u expect [0, %u)",
282 dir_type
, BTRFS_FT_MAX
);
286 if (key
->type
== BTRFS_XATTR_ITEM_KEY
&&
287 dir_type
!= BTRFS_FT_XATTR
) {
288 dir_item_err(root
, leaf
, slot
,
289 "invalid dir item type for XATTR key, have %u expect %u",
290 dir_type
, BTRFS_FT_XATTR
);
293 if (dir_type
== BTRFS_FT_XATTR
&&
294 key
->type
!= BTRFS_XATTR_ITEM_KEY
) {
295 dir_item_err(root
, leaf
, slot
,
296 "xattr dir type found for non-XATTR key");
299 if (dir_type
== BTRFS_FT_XATTR
)
300 max_name_len
= XATTR_NAME_MAX
;
302 max_name_len
= BTRFS_NAME_LEN
;
304 /* Name/data length check */
305 name_len
= btrfs_dir_name_len(leaf
, di
);
306 data_len
= btrfs_dir_data_len(leaf
, di
);
307 if (name_len
> max_name_len
) {
308 dir_item_err(root
, leaf
, slot
,
309 "dir item name len too long, have %u max %u",
310 name_len
, max_name_len
);
313 if (name_len
+ data_len
> BTRFS_MAX_XATTR_SIZE(root
->fs_info
)) {
314 dir_item_err(root
, leaf
, slot
,
315 "dir item name and data len too long, have %u max %u",
317 BTRFS_MAX_XATTR_SIZE(root
->fs_info
));
321 if (data_len
&& dir_type
!= BTRFS_FT_XATTR
) {
322 dir_item_err(root
, leaf
, slot
,
323 "dir item with invalid data len, have %u expect 0",
328 total_size
= sizeof(*di
) + name_len
+ data_len
;
330 /* header and name/data should not cross item boundary */
331 if (cur
+ total_size
> item_size
) {
332 dir_item_err(root
, leaf
, slot
,
333 "dir item data crosses item boundary, have %u boundary %u",
334 cur
+ total_size
, item_size
);
339 * Special check for XATTR/DIR_ITEM, as key->offset is name
340 * hash, should match its name
342 if (key
->type
== BTRFS_DIR_ITEM_KEY
||
343 key
->type
== BTRFS_XATTR_ITEM_KEY
) {
344 char namebuf
[max(BTRFS_NAME_LEN
, XATTR_NAME_MAX
)];
346 read_extent_buffer(leaf
, namebuf
,
347 (unsigned long)(di
+ 1), name_len
);
348 name_hash
= btrfs_name_hash(namebuf
, name_len
);
349 if (key
->offset
!= name_hash
) {
350 dir_item_err(root
, leaf
, slot
,
351 "name hash mismatch with key, have 0x%016x expect 0x%016llx",
352 name_hash
, key
->offset
);
357 di
= (struct btrfs_dir_item
*)((void *)di
+ total_size
);
363 * Common point to switch the item-specific validation.
365 static int check_leaf_item(struct btrfs_root
*root
,
366 struct extent_buffer
*leaf
,
367 struct btrfs_key
*key
, int slot
)
372 case BTRFS_EXTENT_DATA_KEY
:
373 ret
= check_extent_data_item(root
, leaf
, key
, slot
);
375 case BTRFS_EXTENT_CSUM_KEY
:
376 ret
= check_csum_item(root
, leaf
, key
, slot
);
378 case BTRFS_DIR_ITEM_KEY
:
379 case BTRFS_DIR_INDEX_KEY
:
380 case BTRFS_XATTR_ITEM_KEY
:
381 ret
= check_dir_item(root
, leaf
, key
, slot
);
387 static int check_leaf(struct btrfs_root
*root
, struct extent_buffer
*leaf
,
388 bool check_item_data
)
390 struct btrfs_fs_info
*fs_info
= root
->fs_info
;
391 /* No valid key type is 0, so all key should be larger than this key */
392 struct btrfs_key prev_key
= {0, 0, 0};
393 struct btrfs_key key
;
394 u32 nritems
= btrfs_header_nritems(leaf
);
398 * Extent buffers from a relocation tree have a owner field that
399 * corresponds to the subvolume tree they are based on. So just from an
400 * extent buffer alone we can not find out what is the id of the
401 * corresponding subvolume tree, so we can not figure out if the extent
402 * buffer corresponds to the root of the relocation tree or not. So
403 * skip this check for relocation trees.
405 if (nritems
== 0 && !btrfs_header_flag(leaf
, BTRFS_HEADER_FLAG_RELOC
)) {
406 struct btrfs_root
*check_root
;
408 key
.objectid
= btrfs_header_owner(leaf
);
409 key
.type
= BTRFS_ROOT_ITEM_KEY
;
410 key
.offset
= (u64
)-1;
412 check_root
= btrfs_get_fs_root(fs_info
, &key
, false);
414 * The only reason we also check NULL here is that during
415 * open_ctree() some roots has not yet been set up.
417 if (!IS_ERR_OR_NULL(check_root
)) {
418 struct extent_buffer
*eb
;
420 eb
= btrfs_root_node(check_root
);
421 /* if leaf is the root, then it's fine */
423 generic_err(check_root
, leaf
, 0,
424 "invalid nritems, have %u should not be 0 for non-root leaf",
426 free_extent_buffer(eb
);
429 free_extent_buffer(eb
);
438 * Check the following things to make sure this is a good leaf, and
439 * leaf users won't need to bother with similar sanity checks:
442 * 2) item offset and size
443 * No overlap, no hole, all inside the leaf.
445 * If possible, do comprehensive sanity check.
446 * NOTE: All checks must only rely on the item data itself.
448 for (slot
= 0; slot
< nritems
; slot
++) {
449 u32 item_end_expected
;
452 btrfs_item_key_to_cpu(leaf
, &key
, slot
);
454 /* Make sure the keys are in the right order */
455 if (btrfs_comp_cpu_keys(&prev_key
, &key
) >= 0) {
456 generic_err(root
, leaf
, slot
,
457 "bad key order, prev (%llu %u %llu) current (%llu %u %llu)",
458 prev_key
.objectid
, prev_key
.type
,
459 prev_key
.offset
, key
.objectid
, key
.type
,
465 * Make sure the offset and ends are right, remember that the
466 * item data starts at the end of the leaf and grows towards the
470 item_end_expected
= BTRFS_LEAF_DATA_SIZE(fs_info
);
472 item_end_expected
= btrfs_item_offset_nr(leaf
,
474 if (btrfs_item_end_nr(leaf
, slot
) != item_end_expected
) {
475 generic_err(root
, leaf
, slot
,
476 "unexpected item end, have %u expect %u",
477 btrfs_item_end_nr(leaf
, slot
),
483 * Check to make sure that we don't point outside of the leaf,
484 * just in case all the items are consistent to each other, but
485 * all point outside of the leaf.
487 if (btrfs_item_end_nr(leaf
, slot
) >
488 BTRFS_LEAF_DATA_SIZE(fs_info
)) {
489 generic_err(root
, leaf
, slot
,
490 "slot end outside of leaf, have %u expect range [0, %u]",
491 btrfs_item_end_nr(leaf
, slot
),
492 BTRFS_LEAF_DATA_SIZE(fs_info
));
496 /* Also check if the item pointer overlaps with btrfs item. */
497 if (btrfs_item_nr_offset(slot
) + sizeof(struct btrfs_item
) >
498 btrfs_item_ptr_offset(leaf
, slot
)) {
499 generic_err(root
, leaf
, slot
,
500 "slot overlaps with its data, item end %lu data start %lu",
501 btrfs_item_nr_offset(slot
) +
502 sizeof(struct btrfs_item
),
503 btrfs_item_ptr_offset(leaf
, slot
));
507 if (check_item_data
) {
509 * Check if the item size and content meet other
512 ret
= check_leaf_item(root
, leaf
, &key
, slot
);
517 prev_key
.objectid
= key
.objectid
;
518 prev_key
.type
= key
.type
;
519 prev_key
.offset
= key
.offset
;
525 int btrfs_check_leaf_full(struct btrfs_root
*root
, struct extent_buffer
*leaf
)
527 return check_leaf(root
, leaf
, true);
530 int btrfs_check_leaf_relaxed(struct btrfs_root
*root
,
531 struct extent_buffer
*leaf
)
533 return check_leaf(root
, leaf
, false);
536 int btrfs_check_node(struct btrfs_root
*root
, struct extent_buffer
*node
)
538 unsigned long nr
= btrfs_header_nritems(node
);
539 struct btrfs_key key
, next_key
;
544 if (nr
== 0 || nr
> BTRFS_NODEPTRS_PER_BLOCK(root
->fs_info
)) {
545 btrfs_crit(root
->fs_info
,
546 "corrupt node: root=%llu block=%llu, nritems too %s, have %lu expect range [1,%u]",
547 root
->objectid
, node
->start
,
548 nr
== 0 ? "small" : "large", nr
,
549 BTRFS_NODEPTRS_PER_BLOCK(root
->fs_info
));
553 for (slot
= 0; slot
< nr
- 1; slot
++) {
554 bytenr
= btrfs_node_blockptr(node
, slot
);
555 btrfs_node_key_to_cpu(node
, &key
, slot
);
556 btrfs_node_key_to_cpu(node
, &next_key
, slot
+ 1);
559 generic_err(root
, node
, slot
,
560 "invalid NULL node pointer");
564 if (!IS_ALIGNED(bytenr
, root
->fs_info
->sectorsize
)) {
565 generic_err(root
, node
, slot
,
566 "unaligned pointer, have %llu should be aligned to %u",
567 bytenr
, root
->fs_info
->sectorsize
);
572 if (btrfs_comp_cpu_keys(&key
, &next_key
) >= 0) {
573 generic_err(root
, node
, slot
,
574 "bad key order, current (%llu %u %llu) next (%llu %u %llu)",
575 key
.objectid
, key
.type
, key
.offset
,
576 next_key
.objectid
, next_key
.type
,