xref: /openbmc/linux/fs/btrfs/tree-checker.c (revision 0ac6e06b6c137e18d95070fdd3c6cbd319005ffb)
1c1d7c514SDavid Sterba // SPDX-License-Identifier: GPL-2.0
2557ea5ddSQu Wenruo /*
3557ea5ddSQu Wenruo  * Copyright (C) Qu Wenruo 2017.  All rights reserved.
4557ea5ddSQu Wenruo  */
5557ea5ddSQu Wenruo 
6557ea5ddSQu Wenruo /*
7557ea5ddSQu Wenruo  * The module is used to catch unexpected/corrupted tree block data.
8557ea5ddSQu Wenruo  * Such behavior can be caused either by a fuzzed image or bugs.
9557ea5ddSQu Wenruo  *
10557ea5ddSQu Wenruo  * The objective is to do leaf/node validation checks when tree block is read
11557ea5ddSQu Wenruo  * from disk, and check *every* possible member, so other code won't
12557ea5ddSQu Wenruo  * need to checking them again.
13557ea5ddSQu Wenruo  *
14557ea5ddSQu Wenruo  * Due to the potential and unwanted damage, every checker needs to be
15557ea5ddSQu Wenruo  * carefully reviewed otherwise so it does not prevent mount of valid images.
16557ea5ddSQu Wenruo  */
17557ea5ddSQu Wenruo 
1802529d7aSQu Wenruo #include <linux/types.h>
1902529d7aSQu Wenruo #include <linux/stddef.h>
2002529d7aSQu Wenruo #include <linux/error-injection.h>
21557ea5ddSQu Wenruo #include "ctree.h"
22557ea5ddSQu Wenruo #include "tree-checker.h"
23557ea5ddSQu Wenruo #include "disk-io.h"
24557ea5ddSQu Wenruo #include "compression.h"
25fce466eaSQu Wenruo #include "volumes.h"
26c1499166SDavid Sterba #include "misc.h"
27557ea5ddSQu Wenruo 
28bba4f298SQu Wenruo /*
29bba4f298SQu Wenruo  * Error message should follow the following format:
30bba4f298SQu Wenruo  * corrupt <type>: <identifier>, <reason>[, <bad_value>]
31bba4f298SQu Wenruo  *
32bba4f298SQu Wenruo  * @type:	leaf or node
33bba4f298SQu Wenruo  * @identifier:	the necessary info to locate the leaf/node.
3452042d8eSAndrea Gelmini  * 		It's recommended to decode key.objecitd/offset if it's
35bba4f298SQu Wenruo  * 		meaningful.
36bba4f298SQu Wenruo  * @reason:	describe the error
3752042d8eSAndrea Gelmini  * @bad_value:	optional, it's recommended to output bad value and its
38bba4f298SQu Wenruo  *		expected value (range).
39bba4f298SQu Wenruo  *
40bba4f298SQu Wenruo  * Since comma is used to separate the components, only space is allowed
41bba4f298SQu Wenruo  * inside each component.
42bba4f298SQu Wenruo  */
43bba4f298SQu Wenruo 
44bba4f298SQu Wenruo /*
45bba4f298SQu Wenruo  * Append generic "corrupt leaf/node root=%llu block=%llu slot=%d: " to @fmt.
46bba4f298SQu Wenruo  * Allows callers to customize the output.
47bba4f298SQu Wenruo  */
4886a6be3aSDavid Sterba __printf(3, 4)
49e67c718bSDavid Sterba __cold
5086a6be3aSDavid Sterba static void generic_err(const struct extent_buffer *eb, int slot,
51bba4f298SQu Wenruo 			const char *fmt, ...)
52bba4f298SQu Wenruo {
5386a6be3aSDavid Sterba 	const struct btrfs_fs_info *fs_info = eb->fs_info;
54bba4f298SQu Wenruo 	struct va_format vaf;
55bba4f298SQu Wenruo 	va_list args;
56bba4f298SQu Wenruo 
57bba4f298SQu Wenruo 	va_start(args, fmt);
58bba4f298SQu Wenruo 
59bba4f298SQu Wenruo 	vaf.fmt = fmt;
60bba4f298SQu Wenruo 	vaf.va = &args;
61bba4f298SQu Wenruo 
622f659546SQu Wenruo 	btrfs_crit(fs_info,
63bba4f298SQu Wenruo 		"corrupt %s: root=%llu block=%llu slot=%d, %pV",
64bba4f298SQu Wenruo 		btrfs_header_level(eb) == 0 ? "leaf" : "node",
652f659546SQu Wenruo 		btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot, &vaf);
66bba4f298SQu Wenruo 	va_end(args);
67bba4f298SQu Wenruo }
68bba4f298SQu Wenruo 
698806d718SQu Wenruo /*
708806d718SQu Wenruo  * Customized reporter for extent data item, since its key objectid and
718806d718SQu Wenruo  * offset has its own meaning.
728806d718SQu Wenruo  */
731fd715ffSDavid Sterba __printf(3, 4)
74e67c718bSDavid Sterba __cold
751fd715ffSDavid Sterba static void file_extent_err(const struct extent_buffer *eb, int slot,
768806d718SQu Wenruo 			    const char *fmt, ...)
778806d718SQu Wenruo {
781fd715ffSDavid Sterba 	const struct btrfs_fs_info *fs_info = eb->fs_info;
798806d718SQu Wenruo 	struct btrfs_key key;
808806d718SQu Wenruo 	struct va_format vaf;
818806d718SQu Wenruo 	va_list args;
828806d718SQu Wenruo 
838806d718SQu Wenruo 	btrfs_item_key_to_cpu(eb, &key, slot);
848806d718SQu Wenruo 	va_start(args, fmt);
858806d718SQu Wenruo 
868806d718SQu Wenruo 	vaf.fmt = fmt;
878806d718SQu Wenruo 	vaf.va = &args;
888806d718SQu Wenruo 
892f659546SQu Wenruo 	btrfs_crit(fs_info,
908806d718SQu Wenruo 	"corrupt %s: root=%llu block=%llu slot=%d ino=%llu file_offset=%llu, %pV",
912f659546SQu Wenruo 		btrfs_header_level(eb) == 0 ? "leaf" : "node",
922f659546SQu Wenruo 		btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot,
932f659546SQu Wenruo 		key.objectid, key.offset, &vaf);
948806d718SQu Wenruo 	va_end(args);
958806d718SQu Wenruo }
968806d718SQu Wenruo 
978806d718SQu Wenruo /*
988806d718SQu Wenruo  * Return 0 if the btrfs_file_extent_##name is aligned to @alignment
998806d718SQu Wenruo  * Else return 1
1008806d718SQu Wenruo  */
101033774dcSDavid Sterba #define CHECK_FE_ALIGNED(leaf, slot, fi, name, alignment)		      \
1028806d718SQu Wenruo ({									      \
103c7c01a4aSDavid Sterba 	if (unlikely(!IS_ALIGNED(btrfs_file_extent_##name((leaf), (fi)),      \
104c7c01a4aSDavid Sterba 				 (alignment))))				      \
1051fd715ffSDavid Sterba 		file_extent_err((leaf), (slot),				      \
1068806d718SQu Wenruo 	"invalid %s for file extent, have %llu, should be aligned to %u",     \
1078806d718SQu Wenruo 			(#name), btrfs_file_extent_##name((leaf), (fi)),      \
1088806d718SQu Wenruo 			(alignment));					      \
1098806d718SQu Wenruo 	(!IS_ALIGNED(btrfs_file_extent_##name((leaf), (fi)), (alignment)));   \
1108806d718SQu Wenruo })
1118806d718SQu Wenruo 
1124e9845efSFilipe Manana static u64 file_extent_end(struct extent_buffer *leaf,
1134e9845efSFilipe Manana 			   struct btrfs_key *key,
1144e9845efSFilipe Manana 			   struct btrfs_file_extent_item *extent)
1154e9845efSFilipe Manana {
1164e9845efSFilipe Manana 	u64 end;
1174e9845efSFilipe Manana 	u64 len;
1184e9845efSFilipe Manana 
1194e9845efSFilipe Manana 	if (btrfs_file_extent_type(leaf, extent) == BTRFS_FILE_EXTENT_INLINE) {
1204e9845efSFilipe Manana 		len = btrfs_file_extent_ram_bytes(leaf, extent);
1214e9845efSFilipe Manana 		end = ALIGN(key->offset + len, leaf->fs_info->sectorsize);
1224e9845efSFilipe Manana 	} else {
1234e9845efSFilipe Manana 		len = btrfs_file_extent_num_bytes(leaf, extent);
1244e9845efSFilipe Manana 		end = key->offset + len;
1254e9845efSFilipe Manana 	}
1264e9845efSFilipe Manana 	return end;
1274e9845efSFilipe Manana }
1284e9845efSFilipe Manana 
12980d7fd1eSQu Wenruo /*
13080d7fd1eSQu Wenruo  * Customized report for dir_item, the only new important information is
13180d7fd1eSQu Wenruo  * key->objectid, which represents inode number
13280d7fd1eSQu Wenruo  */
13380d7fd1eSQu Wenruo __printf(3, 4)
13480d7fd1eSQu Wenruo __cold
13580d7fd1eSQu Wenruo static void dir_item_err(const struct extent_buffer *eb, int slot,
13680d7fd1eSQu Wenruo 			 const char *fmt, ...)
13780d7fd1eSQu Wenruo {
13880d7fd1eSQu Wenruo 	const struct btrfs_fs_info *fs_info = eb->fs_info;
13980d7fd1eSQu Wenruo 	struct btrfs_key key;
14080d7fd1eSQu Wenruo 	struct va_format vaf;
14180d7fd1eSQu Wenruo 	va_list args;
14280d7fd1eSQu Wenruo 
14380d7fd1eSQu Wenruo 	btrfs_item_key_to_cpu(eb, &key, slot);
14480d7fd1eSQu Wenruo 	va_start(args, fmt);
14580d7fd1eSQu Wenruo 
14680d7fd1eSQu Wenruo 	vaf.fmt = fmt;
14780d7fd1eSQu Wenruo 	vaf.va = &args;
14880d7fd1eSQu Wenruo 
14980d7fd1eSQu Wenruo 	btrfs_crit(fs_info,
15080d7fd1eSQu Wenruo 		"corrupt %s: root=%llu block=%llu slot=%d ino=%llu, %pV",
15180d7fd1eSQu Wenruo 		btrfs_header_level(eb) == 0 ? "leaf" : "node",
15280d7fd1eSQu Wenruo 		btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot,
15380d7fd1eSQu Wenruo 		key.objectid, &vaf);
15480d7fd1eSQu Wenruo 	va_end(args);
15580d7fd1eSQu Wenruo }
15680d7fd1eSQu Wenruo 
15780d7fd1eSQu Wenruo /*
15880d7fd1eSQu Wenruo  * This functions checks prev_key->objectid, to ensure current key and prev_key
15980d7fd1eSQu Wenruo  * share the same objectid as inode number.
16080d7fd1eSQu Wenruo  *
16180d7fd1eSQu Wenruo  * This is to detect missing INODE_ITEM in subvolume trees.
16280d7fd1eSQu Wenruo  *
16380d7fd1eSQu Wenruo  * Return true if everything is OK or we don't need to check.
16480d7fd1eSQu Wenruo  * Return false if anything is wrong.
16580d7fd1eSQu Wenruo  */
16680d7fd1eSQu Wenruo static bool check_prev_ino(struct extent_buffer *leaf,
16780d7fd1eSQu Wenruo 			   struct btrfs_key *key, int slot,
16880d7fd1eSQu Wenruo 			   struct btrfs_key *prev_key)
16980d7fd1eSQu Wenruo {
17080d7fd1eSQu Wenruo 	/* No prev key, skip check */
17180d7fd1eSQu Wenruo 	if (slot == 0)
17280d7fd1eSQu Wenruo 		return true;
17380d7fd1eSQu Wenruo 
17480d7fd1eSQu Wenruo 	/* Only these key->types needs to be checked */
17580d7fd1eSQu Wenruo 	ASSERT(key->type == BTRFS_XATTR_ITEM_KEY ||
17680d7fd1eSQu Wenruo 	       key->type == BTRFS_INODE_REF_KEY ||
17780d7fd1eSQu Wenruo 	       key->type == BTRFS_DIR_INDEX_KEY ||
17880d7fd1eSQu Wenruo 	       key->type == BTRFS_DIR_ITEM_KEY ||
17980d7fd1eSQu Wenruo 	       key->type == BTRFS_EXTENT_DATA_KEY);
18080d7fd1eSQu Wenruo 
18180d7fd1eSQu Wenruo 	/*
18280d7fd1eSQu Wenruo 	 * Only subvolume trees along with their reloc trees need this check.
18380d7fd1eSQu Wenruo 	 * Things like log tree doesn't follow this ino requirement.
18480d7fd1eSQu Wenruo 	 */
18580d7fd1eSQu Wenruo 	if (!is_fstree(btrfs_header_owner(leaf)))
18680d7fd1eSQu Wenruo 		return true;
18780d7fd1eSQu Wenruo 
18880d7fd1eSQu Wenruo 	if (key->objectid == prev_key->objectid)
18980d7fd1eSQu Wenruo 		return true;
19080d7fd1eSQu Wenruo 
19180d7fd1eSQu Wenruo 	/* Error found */
19280d7fd1eSQu Wenruo 	dir_item_err(leaf, slot,
19380d7fd1eSQu Wenruo 		"invalid previous key objectid, have %llu expect %llu",
19480d7fd1eSQu Wenruo 		prev_key->objectid, key->objectid);
19580d7fd1eSQu Wenruo 	return false;
19680d7fd1eSQu Wenruo }
197ae2a19d8SDavid Sterba static int check_extent_data_item(struct extent_buffer *leaf,
1984e9845efSFilipe Manana 				  struct btrfs_key *key, int slot,
1994e9845efSFilipe Manana 				  struct btrfs_key *prev_key)
200557ea5ddSQu Wenruo {
201ae2a19d8SDavid Sterba 	struct btrfs_fs_info *fs_info = leaf->fs_info;
202557ea5ddSQu Wenruo 	struct btrfs_file_extent_item *fi;
2032f659546SQu Wenruo 	u32 sectorsize = fs_info->sectorsize;
204557ea5ddSQu Wenruo 	u32 item_size = btrfs_item_size_nr(leaf, slot);
2054c094c33SQu Wenruo 	u64 extent_end;
206557ea5ddSQu Wenruo 
207c7c01a4aSDavid Sterba 	if (unlikely(!IS_ALIGNED(key->offset, sectorsize))) {
2081fd715ffSDavid Sterba 		file_extent_err(leaf, slot,
2098806d718SQu Wenruo "unaligned file_offset for file extent, have %llu should be aligned to %u",
2108806d718SQu Wenruo 			key->offset, sectorsize);
211557ea5ddSQu Wenruo 		return -EUCLEAN;
212557ea5ddSQu Wenruo 	}
213557ea5ddSQu Wenruo 
214c18679ebSQu Wenruo 	/*
215c18679ebSQu Wenruo 	 * Previous key must have the same key->objectid (ino).
216c18679ebSQu Wenruo 	 * It can be XATTR_ITEM, INODE_ITEM or just another EXTENT_DATA.
217c18679ebSQu Wenruo 	 * But if objectids mismatch, it means we have a missing
218c18679ebSQu Wenruo 	 * INODE_ITEM.
219c18679ebSQu Wenruo 	 */
220c7c01a4aSDavid Sterba 	if (unlikely(!check_prev_ino(leaf, key, slot, prev_key)))
221c18679ebSQu Wenruo 		return -EUCLEAN;
222c18679ebSQu Wenruo 
223557ea5ddSQu Wenruo 	fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
224557ea5ddSQu Wenruo 
225153a6d29SQu Wenruo 	/*
226153a6d29SQu Wenruo 	 * Make sure the item contains at least inline header, so the file
227153a6d29SQu Wenruo 	 * extent type is not some garbage.
228153a6d29SQu Wenruo 	 */
229c7c01a4aSDavid Sterba 	if (unlikely(item_size < BTRFS_FILE_EXTENT_INLINE_DATA_START)) {
230153a6d29SQu Wenruo 		file_extent_err(leaf, slot,
231994bf9cdSAndreas Färber 				"invalid item size, have %u expect [%zu, %u)",
232153a6d29SQu Wenruo 				item_size, BTRFS_FILE_EXTENT_INLINE_DATA_START,
233153a6d29SQu Wenruo 				SZ_4K);
234153a6d29SQu Wenruo 		return -EUCLEAN;
235153a6d29SQu Wenruo 	}
236c7c01a4aSDavid Sterba 	if (unlikely(btrfs_file_extent_type(leaf, fi) >=
237c7c01a4aSDavid Sterba 		     BTRFS_NR_FILE_EXTENT_TYPES)) {
2381fd715ffSDavid Sterba 		file_extent_err(leaf, slot,
2398806d718SQu Wenruo 		"invalid type for file extent, have %u expect range [0, %u]",
2408806d718SQu Wenruo 			btrfs_file_extent_type(leaf, fi),
241b9b1a53eSChengguang Xu 			BTRFS_NR_FILE_EXTENT_TYPES - 1);
242557ea5ddSQu Wenruo 		return -EUCLEAN;
243557ea5ddSQu Wenruo 	}
244557ea5ddSQu Wenruo 
245557ea5ddSQu Wenruo 	/*
24652042d8eSAndrea Gelmini 	 * Support for new compression/encryption must introduce incompat flag,
247557ea5ddSQu Wenruo 	 * and must be caught in open_ctree().
248557ea5ddSQu Wenruo 	 */
249c7c01a4aSDavid Sterba 	if (unlikely(btrfs_file_extent_compression(leaf, fi) >=
250c7c01a4aSDavid Sterba 		     BTRFS_NR_COMPRESS_TYPES)) {
2511fd715ffSDavid Sterba 		file_extent_err(leaf, slot,
2528806d718SQu Wenruo 	"invalid compression for file extent, have %u expect range [0, %u]",
2538806d718SQu Wenruo 			btrfs_file_extent_compression(leaf, fi),
254ce96b7ffSChengguang Xu 			BTRFS_NR_COMPRESS_TYPES - 1);
255557ea5ddSQu Wenruo 		return -EUCLEAN;
256557ea5ddSQu Wenruo 	}
257c7c01a4aSDavid Sterba 	if (unlikely(btrfs_file_extent_encryption(leaf, fi))) {
2581fd715ffSDavid Sterba 		file_extent_err(leaf, slot,
2598806d718SQu Wenruo 			"invalid encryption for file extent, have %u expect 0",
2608806d718SQu Wenruo 			btrfs_file_extent_encryption(leaf, fi));
261557ea5ddSQu Wenruo 		return -EUCLEAN;
262557ea5ddSQu Wenruo 	}
263557ea5ddSQu Wenruo 	if (btrfs_file_extent_type(leaf, fi) == BTRFS_FILE_EXTENT_INLINE) {
264557ea5ddSQu Wenruo 		/* Inline extent must have 0 as key offset */
265c7c01a4aSDavid Sterba 		if (unlikely(key->offset)) {
2661fd715ffSDavid Sterba 			file_extent_err(leaf, slot,
2678806d718SQu Wenruo 		"invalid file_offset for inline file extent, have %llu expect 0",
2688806d718SQu Wenruo 				key->offset);
269557ea5ddSQu Wenruo 			return -EUCLEAN;
270557ea5ddSQu Wenruo 		}
271557ea5ddSQu Wenruo 
272557ea5ddSQu Wenruo 		/* Compressed inline extent has no on-disk size, skip it */
273557ea5ddSQu Wenruo 		if (btrfs_file_extent_compression(leaf, fi) !=
274557ea5ddSQu Wenruo 		    BTRFS_COMPRESS_NONE)
275557ea5ddSQu Wenruo 			return 0;
276557ea5ddSQu Wenruo 
277557ea5ddSQu Wenruo 		/* Uncompressed inline extent size must match item size */
278c7c01a4aSDavid Sterba 		if (unlikely(item_size != BTRFS_FILE_EXTENT_INLINE_DATA_START +
279c7c01a4aSDavid Sterba 					  btrfs_file_extent_ram_bytes(leaf, fi))) {
2801fd715ffSDavid Sterba 			file_extent_err(leaf, slot,
2818806d718SQu Wenruo 	"invalid ram_bytes for uncompressed inline extent, have %u expect %llu",
2828806d718SQu Wenruo 				item_size, BTRFS_FILE_EXTENT_INLINE_DATA_START +
2838806d718SQu Wenruo 				btrfs_file_extent_ram_bytes(leaf, fi));
284557ea5ddSQu Wenruo 			return -EUCLEAN;
285557ea5ddSQu Wenruo 		}
286557ea5ddSQu Wenruo 		return 0;
287557ea5ddSQu Wenruo 	}
288557ea5ddSQu Wenruo 
289557ea5ddSQu Wenruo 	/* Regular or preallocated extent has fixed item size */
290c7c01a4aSDavid Sterba 	if (unlikely(item_size != sizeof(*fi))) {
2911fd715ffSDavid Sterba 		file_extent_err(leaf, slot,
292709a95c3SArnd Bergmann 	"invalid item size for reg/prealloc file extent, have %u expect %zu",
2938806d718SQu Wenruo 			item_size, sizeof(*fi));
294557ea5ddSQu Wenruo 		return -EUCLEAN;
295557ea5ddSQu Wenruo 	}
296c7c01a4aSDavid Sterba 	if (unlikely(CHECK_FE_ALIGNED(leaf, slot, fi, ram_bytes, sectorsize) ||
297033774dcSDavid Sterba 		     CHECK_FE_ALIGNED(leaf, slot, fi, disk_bytenr, sectorsize) ||
298033774dcSDavid Sterba 		     CHECK_FE_ALIGNED(leaf, slot, fi, disk_num_bytes, sectorsize) ||
299033774dcSDavid Sterba 		     CHECK_FE_ALIGNED(leaf, slot, fi, offset, sectorsize) ||
300c7c01a4aSDavid Sterba 		     CHECK_FE_ALIGNED(leaf, slot, fi, num_bytes, sectorsize)))
301557ea5ddSQu Wenruo 		return -EUCLEAN;
3024e9845efSFilipe Manana 
3034c094c33SQu Wenruo 	/* Catch extent end overflow */
304c7c01a4aSDavid Sterba 	if (unlikely(check_add_overflow(btrfs_file_extent_num_bytes(leaf, fi),
305c7c01a4aSDavid Sterba 					key->offset, &extent_end))) {
3064c094c33SQu Wenruo 		file_extent_err(leaf, slot,
3074c094c33SQu Wenruo 	"extent end overflow, have file offset %llu extent num bytes %llu",
3084c094c33SQu Wenruo 				key->offset,
3094c094c33SQu Wenruo 				btrfs_file_extent_num_bytes(leaf, fi));
3104c094c33SQu Wenruo 		return -EUCLEAN;
3114c094c33SQu Wenruo 	}
3124c094c33SQu Wenruo 
3134e9845efSFilipe Manana 	/*
3144e9845efSFilipe Manana 	 * Check that no two consecutive file extent items, in the same leaf,
3154e9845efSFilipe Manana 	 * present ranges that overlap each other.
3164e9845efSFilipe Manana 	 */
3174e9845efSFilipe Manana 	if (slot > 0 &&
3184e9845efSFilipe Manana 	    prev_key->objectid == key->objectid &&
3194e9845efSFilipe Manana 	    prev_key->type == BTRFS_EXTENT_DATA_KEY) {
3204e9845efSFilipe Manana 		struct btrfs_file_extent_item *prev_fi;
3214e9845efSFilipe Manana 		u64 prev_end;
3224e9845efSFilipe Manana 
3234e9845efSFilipe Manana 		prev_fi = btrfs_item_ptr(leaf, slot - 1,
3244e9845efSFilipe Manana 					 struct btrfs_file_extent_item);
3254e9845efSFilipe Manana 		prev_end = file_extent_end(leaf, prev_key, prev_fi);
326c7c01a4aSDavid Sterba 		if (unlikely(prev_end > key->offset)) {
3274e9845efSFilipe Manana 			file_extent_err(leaf, slot - 1,
3284e9845efSFilipe Manana "file extent end range (%llu) goes beyond start offset (%llu) of the next file extent",
3294e9845efSFilipe Manana 					prev_end, key->offset);
3304e9845efSFilipe Manana 			return -EUCLEAN;
3314e9845efSFilipe Manana 		}
3324e9845efSFilipe Manana 	}
3334e9845efSFilipe Manana 
334557ea5ddSQu Wenruo 	return 0;
335557ea5ddSQu Wenruo }
336557ea5ddSQu Wenruo 
33768128ce7SDavid Sterba static int check_csum_item(struct extent_buffer *leaf, struct btrfs_key *key,
338ad1d8c43SFilipe Manana 			   int slot, struct btrfs_key *prev_key)
339557ea5ddSQu Wenruo {
34068128ce7SDavid Sterba 	struct btrfs_fs_info *fs_info = leaf->fs_info;
3412f659546SQu Wenruo 	u32 sectorsize = fs_info->sectorsize;
342223486c2SDavid Sterba 	const u32 csumsize = fs_info->csum_size;
343557ea5ddSQu Wenruo 
344c7c01a4aSDavid Sterba 	if (unlikely(key->objectid != BTRFS_EXTENT_CSUM_OBJECTID)) {
34586a6be3aSDavid Sterba 		generic_err(leaf, slot,
346d508c5f0SQu Wenruo 		"invalid key objectid for csum item, have %llu expect %llu",
347d508c5f0SQu Wenruo 			key->objectid, BTRFS_EXTENT_CSUM_OBJECTID);
348557ea5ddSQu Wenruo 		return -EUCLEAN;
349557ea5ddSQu Wenruo 	}
350c7c01a4aSDavid Sterba 	if (unlikely(!IS_ALIGNED(key->offset, sectorsize))) {
35186a6be3aSDavid Sterba 		generic_err(leaf, slot,
352d508c5f0SQu Wenruo 	"unaligned key offset for csum item, have %llu should be aligned to %u",
353d508c5f0SQu Wenruo 			key->offset, sectorsize);
354557ea5ddSQu Wenruo 		return -EUCLEAN;
355557ea5ddSQu Wenruo 	}
356c7c01a4aSDavid Sterba 	if (unlikely(!IS_ALIGNED(btrfs_item_size_nr(leaf, slot), csumsize))) {
35786a6be3aSDavid Sterba 		generic_err(leaf, slot,
358d508c5f0SQu Wenruo 	"unaligned item size for csum item, have %u should be aligned to %u",
359d508c5f0SQu Wenruo 			btrfs_item_size_nr(leaf, slot), csumsize);
360557ea5ddSQu Wenruo 		return -EUCLEAN;
361557ea5ddSQu Wenruo 	}
362ad1d8c43SFilipe Manana 	if (slot > 0 && prev_key->type == BTRFS_EXTENT_CSUM_KEY) {
363ad1d8c43SFilipe Manana 		u64 prev_csum_end;
364ad1d8c43SFilipe Manana 		u32 prev_item_size;
365ad1d8c43SFilipe Manana 
366ad1d8c43SFilipe Manana 		prev_item_size = btrfs_item_size_nr(leaf, slot - 1);
367ad1d8c43SFilipe Manana 		prev_csum_end = (prev_item_size / csumsize) * sectorsize;
368ad1d8c43SFilipe Manana 		prev_csum_end += prev_key->offset;
369c7c01a4aSDavid Sterba 		if (unlikely(prev_csum_end > key->offset)) {
370ad1d8c43SFilipe Manana 			generic_err(leaf, slot - 1,
371ad1d8c43SFilipe Manana "csum end range (%llu) goes beyond the start range (%llu) of the next csum item",
372ad1d8c43SFilipe Manana 				    prev_csum_end, key->offset);
373ad1d8c43SFilipe Manana 			return -EUCLEAN;
374ad1d8c43SFilipe Manana 		}
375ad1d8c43SFilipe Manana 	}
376557ea5ddSQu Wenruo 	return 0;
377557ea5ddSQu Wenruo }
378557ea5ddSQu Wenruo 
379c23c77b0SQu Wenruo /* Inode item error output has the same format as dir_item_err() */
380c23c77b0SQu Wenruo #define inode_item_err(eb, slot, fmt, ...)			\
381c23c77b0SQu Wenruo 	dir_item_err(eb, slot, fmt, __VA_ARGS__)
382c23c77b0SQu Wenruo 
383c23c77b0SQu Wenruo static int check_inode_key(struct extent_buffer *leaf, struct btrfs_key *key,
384c23c77b0SQu Wenruo 			   int slot)
385c23c77b0SQu Wenruo {
386c23c77b0SQu Wenruo 	struct btrfs_key item_key;
387c23c77b0SQu Wenruo 	bool is_inode_item;
388c23c77b0SQu Wenruo 
389c23c77b0SQu Wenruo 	btrfs_item_key_to_cpu(leaf, &item_key, slot);
390c23c77b0SQu Wenruo 	is_inode_item = (item_key.type == BTRFS_INODE_ITEM_KEY);
391c23c77b0SQu Wenruo 
392c23c77b0SQu Wenruo 	/* For XATTR_ITEM, location key should be all 0 */
393c23c77b0SQu Wenruo 	if (item_key.type == BTRFS_XATTR_ITEM_KEY) {
394c7c01a4aSDavid Sterba 		if (unlikely(key->objectid != 0 || key->type != 0 ||
395c7c01a4aSDavid Sterba 			     key->offset != 0))
396c23c77b0SQu Wenruo 			return -EUCLEAN;
397c23c77b0SQu Wenruo 		return 0;
398c23c77b0SQu Wenruo 	}
399c23c77b0SQu Wenruo 
400c7c01a4aSDavid Sterba 	if (unlikely((key->objectid < BTRFS_FIRST_FREE_OBJECTID ||
401c23c77b0SQu Wenruo 		      key->objectid > BTRFS_LAST_FREE_OBJECTID) &&
402c23c77b0SQu Wenruo 		     key->objectid != BTRFS_ROOT_TREE_DIR_OBJECTID &&
403c7c01a4aSDavid Sterba 		     key->objectid != BTRFS_FREE_INO_OBJECTID)) {
404c23c77b0SQu Wenruo 		if (is_inode_item) {
405c23c77b0SQu Wenruo 			generic_err(leaf, slot,
406c23c77b0SQu Wenruo 	"invalid key objectid: has %llu expect %llu or [%llu, %llu] or %llu",
407c23c77b0SQu Wenruo 				key->objectid, BTRFS_ROOT_TREE_DIR_OBJECTID,
408c23c77b0SQu Wenruo 				BTRFS_FIRST_FREE_OBJECTID,
409c23c77b0SQu Wenruo 				BTRFS_LAST_FREE_OBJECTID,
410c23c77b0SQu Wenruo 				BTRFS_FREE_INO_OBJECTID);
411c23c77b0SQu Wenruo 		} else {
412c23c77b0SQu Wenruo 			dir_item_err(leaf, slot,
413c23c77b0SQu Wenruo "invalid location key objectid: has %llu expect %llu or [%llu, %llu] or %llu",
414c23c77b0SQu Wenruo 				key->objectid, BTRFS_ROOT_TREE_DIR_OBJECTID,
415c23c77b0SQu Wenruo 				BTRFS_FIRST_FREE_OBJECTID,
416c23c77b0SQu Wenruo 				BTRFS_LAST_FREE_OBJECTID,
417c23c77b0SQu Wenruo 				BTRFS_FREE_INO_OBJECTID);
418c23c77b0SQu Wenruo 		}
419c23c77b0SQu Wenruo 		return -EUCLEAN;
420c23c77b0SQu Wenruo 	}
421c7c01a4aSDavid Sterba 	if (unlikely(key->offset != 0)) {
422c23c77b0SQu Wenruo 		if (is_inode_item)
423c23c77b0SQu Wenruo 			inode_item_err(leaf, slot,
424c23c77b0SQu Wenruo 				       "invalid key offset: has %llu expect 0",
425c23c77b0SQu Wenruo 				       key->offset);
426c23c77b0SQu Wenruo 		else
427c23c77b0SQu Wenruo 			dir_item_err(leaf, slot,
428c23c77b0SQu Wenruo 				"invalid location key offset:has %llu expect 0",
429c23c77b0SQu Wenruo 				key->offset);
430c23c77b0SQu Wenruo 		return -EUCLEAN;
431c23c77b0SQu Wenruo 	}
432c23c77b0SQu Wenruo 	return 0;
433c23c77b0SQu Wenruo }
434c23c77b0SQu Wenruo 
43557a0e674SQu Wenruo static int check_root_key(struct extent_buffer *leaf, struct btrfs_key *key,
43657a0e674SQu Wenruo 			  int slot)
43757a0e674SQu Wenruo {
43857a0e674SQu Wenruo 	struct btrfs_key item_key;
43957a0e674SQu Wenruo 	bool is_root_item;
44057a0e674SQu Wenruo 
44157a0e674SQu Wenruo 	btrfs_item_key_to_cpu(leaf, &item_key, slot);
44257a0e674SQu Wenruo 	is_root_item = (item_key.type == BTRFS_ROOT_ITEM_KEY);
44357a0e674SQu Wenruo 
44457a0e674SQu Wenruo 	/* No such tree id */
445c7c01a4aSDavid Sterba 	if (unlikely(key->objectid == 0)) {
44657a0e674SQu Wenruo 		if (is_root_item)
44757a0e674SQu Wenruo 			generic_err(leaf, slot, "invalid root id 0");
44857a0e674SQu Wenruo 		else
44957a0e674SQu Wenruo 			dir_item_err(leaf, slot,
45057a0e674SQu Wenruo 				     "invalid location key root id 0");
45157a0e674SQu Wenruo 		return -EUCLEAN;
45257a0e674SQu Wenruo 	}
45357a0e674SQu Wenruo 
45457a0e674SQu Wenruo 	/* DIR_ITEM/INDEX/INODE_REF is not allowed to point to non-fs trees */
455c7c01a4aSDavid Sterba 	if (unlikely(!is_fstree(key->objectid) && !is_root_item)) {
45657a0e674SQu Wenruo 		dir_item_err(leaf, slot,
45757a0e674SQu Wenruo 		"invalid location key objectid, have %llu expect [%llu, %llu]",
45857a0e674SQu Wenruo 				key->objectid, BTRFS_FIRST_FREE_OBJECTID,
45957a0e674SQu Wenruo 				BTRFS_LAST_FREE_OBJECTID);
46057a0e674SQu Wenruo 		return -EUCLEAN;
46157a0e674SQu Wenruo 	}
46257a0e674SQu Wenruo 
46357a0e674SQu Wenruo 	/*
46457a0e674SQu Wenruo 	 * ROOT_ITEM with non-zero offset means this is a snapshot, created at
46557a0e674SQu Wenruo 	 * @offset transid.
46657a0e674SQu Wenruo 	 * Furthermore, for location key in DIR_ITEM, its offset is always -1.
46757a0e674SQu Wenruo 	 *
46857a0e674SQu Wenruo 	 * So here we only check offset for reloc tree whose key->offset must
46957a0e674SQu Wenruo 	 * be a valid tree.
47057a0e674SQu Wenruo 	 */
471c7c01a4aSDavid Sterba 	if (unlikely(key->objectid == BTRFS_TREE_RELOC_OBJECTID &&
472c7c01a4aSDavid Sterba 		     key->offset == 0)) {
47357a0e674SQu Wenruo 		generic_err(leaf, slot, "invalid root id 0 for reloc tree");
47457a0e674SQu Wenruo 		return -EUCLEAN;
47557a0e674SQu Wenruo 	}
47657a0e674SQu Wenruo 	return 0;
47757a0e674SQu Wenruo }
47857a0e674SQu Wenruo 
479ce4252c0SDavid Sterba static int check_dir_item(struct extent_buffer *leaf,
480c18679ebSQu Wenruo 			  struct btrfs_key *key, struct btrfs_key *prev_key,
481c18679ebSQu Wenruo 			  int slot)
482ad7b0368SQu Wenruo {
483ce4252c0SDavid Sterba 	struct btrfs_fs_info *fs_info = leaf->fs_info;
484ad7b0368SQu Wenruo 	struct btrfs_dir_item *di;
485ad7b0368SQu Wenruo 	u32 item_size = btrfs_item_size_nr(leaf, slot);
486ad7b0368SQu Wenruo 	u32 cur = 0;
487ad7b0368SQu Wenruo 
488c7c01a4aSDavid Sterba 	if (unlikely(!check_prev_ino(leaf, key, slot, prev_key)))
489c18679ebSQu Wenruo 		return -EUCLEAN;
490c7c01a4aSDavid Sterba 
491ad7b0368SQu Wenruo 	di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
492ad7b0368SQu Wenruo 	while (cur < item_size) {
493147a097cSQu Wenruo 		struct btrfs_key location_key;
494ad7b0368SQu Wenruo 		u32 name_len;
495ad7b0368SQu Wenruo 		u32 data_len;
496ad7b0368SQu Wenruo 		u32 max_name_len;
497ad7b0368SQu Wenruo 		u32 total_size;
498ad7b0368SQu Wenruo 		u32 name_hash;
499ad7b0368SQu Wenruo 		u8 dir_type;
500147a097cSQu Wenruo 		int ret;
501ad7b0368SQu Wenruo 
502ad7b0368SQu Wenruo 		/* header itself should not cross item boundary */
503c7c01a4aSDavid Sterba 		if (unlikely(cur + sizeof(*di) > item_size)) {
504d98ced68SDavid Sterba 			dir_item_err(leaf, slot,
5057cfad652SArnd Bergmann 		"dir item header crosses item boundary, have %zu boundary %u",
506ad7b0368SQu Wenruo 				cur + sizeof(*di), item_size);
507ad7b0368SQu Wenruo 			return -EUCLEAN;
508ad7b0368SQu Wenruo 		}
509ad7b0368SQu Wenruo 
510147a097cSQu Wenruo 		/* Location key check */
511147a097cSQu Wenruo 		btrfs_dir_item_key_to_cpu(leaf, di, &location_key);
512147a097cSQu Wenruo 		if (location_key.type == BTRFS_ROOT_ITEM_KEY) {
513147a097cSQu Wenruo 			ret = check_root_key(leaf, &location_key, slot);
514c7c01a4aSDavid Sterba 			if (unlikely(ret < 0))
515147a097cSQu Wenruo 				return ret;
516147a097cSQu Wenruo 		} else if (location_key.type == BTRFS_INODE_ITEM_KEY ||
517147a097cSQu Wenruo 			   location_key.type == 0) {
518147a097cSQu Wenruo 			ret = check_inode_key(leaf, &location_key, slot);
519c7c01a4aSDavid Sterba 			if (unlikely(ret < 0))
520147a097cSQu Wenruo 				return ret;
521147a097cSQu Wenruo 		} else {
522147a097cSQu Wenruo 			dir_item_err(leaf, slot,
523147a097cSQu Wenruo 			"invalid location key type, have %u, expect %u or %u",
524147a097cSQu Wenruo 				     location_key.type, BTRFS_ROOT_ITEM_KEY,
525147a097cSQu Wenruo 				     BTRFS_INODE_ITEM_KEY);
526147a097cSQu Wenruo 			return -EUCLEAN;
527147a097cSQu Wenruo 		}
528147a097cSQu Wenruo 
529ad7b0368SQu Wenruo 		/* dir type check */
530ad7b0368SQu Wenruo 		dir_type = btrfs_dir_type(leaf, di);
531c7c01a4aSDavid Sterba 		if (unlikely(dir_type >= BTRFS_FT_MAX)) {
532d98ced68SDavid Sterba 			dir_item_err(leaf, slot,
533ad7b0368SQu Wenruo 			"invalid dir item type, have %u expect [0, %u)",
534ad7b0368SQu Wenruo 				dir_type, BTRFS_FT_MAX);
535ad7b0368SQu Wenruo 			return -EUCLEAN;
536ad7b0368SQu Wenruo 		}
537ad7b0368SQu Wenruo 
538c7c01a4aSDavid Sterba 		if (unlikely(key->type == BTRFS_XATTR_ITEM_KEY &&
539c7c01a4aSDavid Sterba 			     dir_type != BTRFS_FT_XATTR)) {
540d98ced68SDavid Sterba 			dir_item_err(leaf, slot,
541ad7b0368SQu Wenruo 		"invalid dir item type for XATTR key, have %u expect %u",
542ad7b0368SQu Wenruo 				dir_type, BTRFS_FT_XATTR);
543ad7b0368SQu Wenruo 			return -EUCLEAN;
544ad7b0368SQu Wenruo 		}
545c7c01a4aSDavid Sterba 		if (unlikely(dir_type == BTRFS_FT_XATTR &&
546c7c01a4aSDavid Sterba 			     key->type != BTRFS_XATTR_ITEM_KEY)) {
547d98ced68SDavid Sterba 			dir_item_err(leaf, slot,
548ad7b0368SQu Wenruo 			"xattr dir type found for non-XATTR key");
549ad7b0368SQu Wenruo 			return -EUCLEAN;
550ad7b0368SQu Wenruo 		}
551ad7b0368SQu Wenruo 		if (dir_type == BTRFS_FT_XATTR)
552ad7b0368SQu Wenruo 			max_name_len = XATTR_NAME_MAX;
553ad7b0368SQu Wenruo 		else
554ad7b0368SQu Wenruo 			max_name_len = BTRFS_NAME_LEN;
555ad7b0368SQu Wenruo 
556ad7b0368SQu Wenruo 		/* Name/data length check */
557ad7b0368SQu Wenruo 		name_len = btrfs_dir_name_len(leaf, di);
558ad7b0368SQu Wenruo 		data_len = btrfs_dir_data_len(leaf, di);
559c7c01a4aSDavid Sterba 		if (unlikely(name_len > max_name_len)) {
560d98ced68SDavid Sterba 			dir_item_err(leaf, slot,
561ad7b0368SQu Wenruo 			"dir item name len too long, have %u max %u",
562ad7b0368SQu Wenruo 				name_len, max_name_len);
563ad7b0368SQu Wenruo 			return -EUCLEAN;
564ad7b0368SQu Wenruo 		}
565c7c01a4aSDavid Sterba 		if (unlikely(name_len + data_len > BTRFS_MAX_XATTR_SIZE(fs_info))) {
566d98ced68SDavid Sterba 			dir_item_err(leaf, slot,
567ad7b0368SQu Wenruo 			"dir item name and data len too long, have %u max %u",
568ad7b0368SQu Wenruo 				name_len + data_len,
5692f659546SQu Wenruo 				BTRFS_MAX_XATTR_SIZE(fs_info));
570ad7b0368SQu Wenruo 			return -EUCLEAN;
571ad7b0368SQu Wenruo 		}
572ad7b0368SQu Wenruo 
573c7c01a4aSDavid Sterba 		if (unlikely(data_len && dir_type != BTRFS_FT_XATTR)) {
574d98ced68SDavid Sterba 			dir_item_err(leaf, slot,
575ad7b0368SQu Wenruo 			"dir item with invalid data len, have %u expect 0",
576ad7b0368SQu Wenruo 				data_len);
577ad7b0368SQu Wenruo 			return -EUCLEAN;
578ad7b0368SQu Wenruo 		}
579ad7b0368SQu Wenruo 
580ad7b0368SQu Wenruo 		total_size = sizeof(*di) + name_len + data_len;
581ad7b0368SQu Wenruo 
582ad7b0368SQu Wenruo 		/* header and name/data should not cross item boundary */
583c7c01a4aSDavid Sterba 		if (unlikely(cur + total_size > item_size)) {
584d98ced68SDavid Sterba 			dir_item_err(leaf, slot,
585ad7b0368SQu Wenruo 		"dir item data crosses item boundary, have %u boundary %u",
586ad7b0368SQu Wenruo 				cur + total_size, item_size);
587ad7b0368SQu Wenruo 			return -EUCLEAN;
588ad7b0368SQu Wenruo 		}
589ad7b0368SQu Wenruo 
590ad7b0368SQu Wenruo 		/*
591ad7b0368SQu Wenruo 		 * Special check for XATTR/DIR_ITEM, as key->offset is name
592ad7b0368SQu Wenruo 		 * hash, should match its name
593ad7b0368SQu Wenruo 		 */
594ad7b0368SQu Wenruo 		if (key->type == BTRFS_DIR_ITEM_KEY ||
595ad7b0368SQu Wenruo 		    key->type == BTRFS_XATTR_ITEM_KEY) {
596e2683fc9SDavid Sterba 			char namebuf[max(BTRFS_NAME_LEN, XATTR_NAME_MAX)];
597e2683fc9SDavid Sterba 
598ad7b0368SQu Wenruo 			read_extent_buffer(leaf, namebuf,
599ad7b0368SQu Wenruo 					(unsigned long)(di + 1), name_len);
600ad7b0368SQu Wenruo 			name_hash = btrfs_name_hash(namebuf, name_len);
601c7c01a4aSDavid Sterba 			if (unlikely(key->offset != name_hash)) {
602d98ced68SDavid Sterba 				dir_item_err(leaf, slot,
603ad7b0368SQu Wenruo 		"name hash mismatch with key, have 0x%016x expect 0x%016llx",
604ad7b0368SQu Wenruo 					name_hash, key->offset);
605ad7b0368SQu Wenruo 				return -EUCLEAN;
606ad7b0368SQu Wenruo 			}
607ad7b0368SQu Wenruo 		}
608ad7b0368SQu Wenruo 		cur += total_size;
609ad7b0368SQu Wenruo 		di = (struct btrfs_dir_item *)((void *)di + total_size);
610ad7b0368SQu Wenruo 	}
611ad7b0368SQu Wenruo 	return 0;
612ad7b0368SQu Wenruo }
613ad7b0368SQu Wenruo 
6144806bd88SDavid Sterba __printf(3, 4)
615fce466eaSQu Wenruo __cold
6164806bd88SDavid Sterba static void block_group_err(const struct extent_buffer *eb, int slot,
617fce466eaSQu Wenruo 			    const char *fmt, ...)
618fce466eaSQu Wenruo {
6194806bd88SDavid Sterba 	const struct btrfs_fs_info *fs_info = eb->fs_info;
620fce466eaSQu Wenruo 	struct btrfs_key key;
621fce466eaSQu Wenruo 	struct va_format vaf;
622fce466eaSQu Wenruo 	va_list args;
623fce466eaSQu Wenruo 
624fce466eaSQu Wenruo 	btrfs_item_key_to_cpu(eb, &key, slot);
625fce466eaSQu Wenruo 	va_start(args, fmt);
626fce466eaSQu Wenruo 
627fce466eaSQu Wenruo 	vaf.fmt = fmt;
628fce466eaSQu Wenruo 	vaf.va = &args;
629fce466eaSQu Wenruo 
630fce466eaSQu Wenruo 	btrfs_crit(fs_info,
631fce466eaSQu Wenruo 	"corrupt %s: root=%llu block=%llu slot=%d bg_start=%llu bg_len=%llu, %pV",
632fce466eaSQu Wenruo 		btrfs_header_level(eb) == 0 ? "leaf" : "node",
633fce466eaSQu Wenruo 		btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot,
634fce466eaSQu Wenruo 		key.objectid, key.offset, &vaf);
635fce466eaSQu Wenruo 	va_end(args);
636fce466eaSQu Wenruo }
637fce466eaSQu Wenruo 
638af60ce2bSDavid Sterba static int check_block_group_item(struct extent_buffer *leaf,
639fce466eaSQu Wenruo 				  struct btrfs_key *key, int slot)
640fce466eaSQu Wenruo {
641fce466eaSQu Wenruo 	struct btrfs_block_group_item bgi;
642fce466eaSQu Wenruo 	u32 item_size = btrfs_item_size_nr(leaf, slot);
643fce466eaSQu Wenruo 	u64 flags;
644fce466eaSQu Wenruo 	u64 type;
645fce466eaSQu Wenruo 
646fce466eaSQu Wenruo 	/*
647fce466eaSQu Wenruo 	 * Here we don't really care about alignment since extent allocator can
64810950929SQu Wenruo 	 * handle it.  We care more about the size.
649fce466eaSQu Wenruo 	 */
650c7c01a4aSDavid Sterba 	if (unlikely(key->offset == 0)) {
6514806bd88SDavid Sterba 		block_group_err(leaf, slot,
65210950929SQu Wenruo 				"invalid block group size 0");
653fce466eaSQu Wenruo 		return -EUCLEAN;
654fce466eaSQu Wenruo 	}
655fce466eaSQu Wenruo 
656c7c01a4aSDavid Sterba 	if (unlikely(item_size != sizeof(bgi))) {
6574806bd88SDavid Sterba 		block_group_err(leaf, slot,
658fce466eaSQu Wenruo 			"invalid item size, have %u expect %zu",
659fce466eaSQu Wenruo 				item_size, sizeof(bgi));
660fce466eaSQu Wenruo 		return -EUCLEAN;
661fce466eaSQu Wenruo 	}
662fce466eaSQu Wenruo 
663fce466eaSQu Wenruo 	read_extent_buffer(leaf, &bgi, btrfs_item_ptr_offset(leaf, slot),
664fce466eaSQu Wenruo 			   sizeof(bgi));
665c7c01a4aSDavid Sterba 	if (unlikely(btrfs_stack_block_group_chunk_objectid(&bgi) !=
666c7c01a4aSDavid Sterba 		     BTRFS_FIRST_CHUNK_TREE_OBJECTID)) {
6674806bd88SDavid Sterba 		block_group_err(leaf, slot,
668fce466eaSQu Wenruo 		"invalid block group chunk objectid, have %llu expect %llu",
669de0dc456SDavid Sterba 				btrfs_stack_block_group_chunk_objectid(&bgi),
670fce466eaSQu Wenruo 				BTRFS_FIRST_CHUNK_TREE_OBJECTID);
671fce466eaSQu Wenruo 		return -EUCLEAN;
672fce466eaSQu Wenruo 	}
673fce466eaSQu Wenruo 
674c7c01a4aSDavid Sterba 	if (unlikely(btrfs_stack_block_group_used(&bgi) > key->offset)) {
6754806bd88SDavid Sterba 		block_group_err(leaf, slot,
676fce466eaSQu Wenruo 			"invalid block group used, have %llu expect [0, %llu)",
677de0dc456SDavid Sterba 				btrfs_stack_block_group_used(&bgi), key->offset);
678fce466eaSQu Wenruo 		return -EUCLEAN;
679fce466eaSQu Wenruo 	}
680fce466eaSQu Wenruo 
681de0dc456SDavid Sterba 	flags = btrfs_stack_block_group_flags(&bgi);
682c7c01a4aSDavid Sterba 	if (unlikely(hweight64(flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) > 1)) {
6834806bd88SDavid Sterba 		block_group_err(leaf, slot,
684fce466eaSQu Wenruo "invalid profile flags, have 0x%llx (%lu bits set) expect no more than 1 bit set",
685fce466eaSQu Wenruo 			flags & BTRFS_BLOCK_GROUP_PROFILE_MASK,
686fce466eaSQu Wenruo 			hweight64(flags & BTRFS_BLOCK_GROUP_PROFILE_MASK));
687fce466eaSQu Wenruo 		return -EUCLEAN;
688fce466eaSQu Wenruo 	}
689fce466eaSQu Wenruo 
690fce466eaSQu Wenruo 	type = flags & BTRFS_BLOCK_GROUP_TYPE_MASK;
691c7c01a4aSDavid Sterba 	if (unlikely(type != BTRFS_BLOCK_GROUP_DATA &&
692fce466eaSQu Wenruo 		     type != BTRFS_BLOCK_GROUP_METADATA &&
693fce466eaSQu Wenruo 		     type != BTRFS_BLOCK_GROUP_SYSTEM &&
694fce466eaSQu Wenruo 		     type != (BTRFS_BLOCK_GROUP_METADATA |
695c7c01a4aSDavid Sterba 			      BTRFS_BLOCK_GROUP_DATA))) {
6964806bd88SDavid Sterba 		block_group_err(leaf, slot,
697761333f2SShaokun Zhang "invalid type, have 0x%llx (%lu bits set) expect either 0x%llx, 0x%llx, 0x%llx or 0x%llx",
698fce466eaSQu Wenruo 			type, hweight64(type),
699fce466eaSQu Wenruo 			BTRFS_BLOCK_GROUP_DATA, BTRFS_BLOCK_GROUP_METADATA,
700fce466eaSQu Wenruo 			BTRFS_BLOCK_GROUP_SYSTEM,
701fce466eaSQu Wenruo 			BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA);
702fce466eaSQu Wenruo 		return -EUCLEAN;
703fce466eaSQu Wenruo 	}
704fce466eaSQu Wenruo 	return 0;
705fce466eaSQu Wenruo }
706fce466eaSQu Wenruo 
707d001e4a3SDavid Sterba __printf(4, 5)
708f1140243SQu Wenruo __cold
709d001e4a3SDavid Sterba static void chunk_err(const struct extent_buffer *leaf,
710f1140243SQu Wenruo 		      const struct btrfs_chunk *chunk, u64 logical,
711f1140243SQu Wenruo 		      const char *fmt, ...)
712f1140243SQu Wenruo {
713d001e4a3SDavid Sterba 	const struct btrfs_fs_info *fs_info = leaf->fs_info;
714f1140243SQu Wenruo 	bool is_sb;
715f1140243SQu Wenruo 	struct va_format vaf;
716f1140243SQu Wenruo 	va_list args;
717f1140243SQu Wenruo 	int i;
718f1140243SQu Wenruo 	int slot = -1;
719f1140243SQu Wenruo 
720f1140243SQu Wenruo 	/* Only superblock eb is able to have such small offset */
721f1140243SQu Wenruo 	is_sb = (leaf->start == BTRFS_SUPER_INFO_OFFSET);
722f1140243SQu Wenruo 
723f1140243SQu Wenruo 	if (!is_sb) {
724f1140243SQu Wenruo 		/*
725f1140243SQu Wenruo 		 * Get the slot number by iterating through all slots, this
726f1140243SQu Wenruo 		 * would provide better readability.
727f1140243SQu Wenruo 		 */
728f1140243SQu Wenruo 		for (i = 0; i < btrfs_header_nritems(leaf); i++) {
729f1140243SQu Wenruo 			if (btrfs_item_ptr_offset(leaf, i) ==
730f1140243SQu Wenruo 					(unsigned long)chunk) {
731f1140243SQu Wenruo 				slot = i;
732f1140243SQu Wenruo 				break;
733f1140243SQu Wenruo 			}
734f1140243SQu Wenruo 		}
735f1140243SQu Wenruo 	}
736f1140243SQu Wenruo 	va_start(args, fmt);
737f1140243SQu Wenruo 	vaf.fmt = fmt;
738f1140243SQu Wenruo 	vaf.va = &args;
739f1140243SQu Wenruo 
740f1140243SQu Wenruo 	if (is_sb)
741f1140243SQu Wenruo 		btrfs_crit(fs_info,
742f1140243SQu Wenruo 		"corrupt superblock syschunk array: chunk_start=%llu, %pV",
743f1140243SQu Wenruo 			   logical, &vaf);
744f1140243SQu Wenruo 	else
745f1140243SQu Wenruo 		btrfs_crit(fs_info,
746f1140243SQu Wenruo 	"corrupt leaf: root=%llu block=%llu slot=%d chunk_start=%llu, %pV",
747f1140243SQu Wenruo 			   BTRFS_CHUNK_TREE_OBJECTID, leaf->start, slot,
748f1140243SQu Wenruo 			   logical, &vaf);
749f1140243SQu Wenruo 	va_end(args);
750f1140243SQu Wenruo }
751f1140243SQu Wenruo 
752ad7b0368SQu Wenruo /*
75382fc28fbSQu Wenruo  * The common chunk check which could also work on super block sys chunk array.
75482fc28fbSQu Wenruo  *
755bf871c3bSQu Wenruo  * Return -EUCLEAN if anything is corrupted.
75682fc28fbSQu Wenruo  * Return 0 if everything is OK.
75782fc28fbSQu Wenruo  */
758ddaf1d5aSDavid Sterba int btrfs_check_chunk_valid(struct extent_buffer *leaf,
75982fc28fbSQu Wenruo 			    struct btrfs_chunk *chunk, u64 logical)
76082fc28fbSQu Wenruo {
761ddaf1d5aSDavid Sterba 	struct btrfs_fs_info *fs_info = leaf->fs_info;
76282fc28fbSQu Wenruo 	u64 length;
763347fb0cfSSu Yue 	u64 chunk_end;
76482fc28fbSQu Wenruo 	u64 stripe_len;
76582fc28fbSQu Wenruo 	u16 num_stripes;
76682fc28fbSQu Wenruo 	u16 sub_stripes;
76782fc28fbSQu Wenruo 	u64 type;
76882fc28fbSQu Wenruo 	u64 features;
76982fc28fbSQu Wenruo 	bool mixed = false;
77085d07fbeSDaniel Xu 	int raid_index;
77185d07fbeSDaniel Xu 	int nparity;
77285d07fbeSDaniel Xu 	int ncopies;
77382fc28fbSQu Wenruo 
77482fc28fbSQu Wenruo 	length = btrfs_chunk_length(leaf, chunk);
77582fc28fbSQu Wenruo 	stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
77682fc28fbSQu Wenruo 	num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
77782fc28fbSQu Wenruo 	sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
77882fc28fbSQu Wenruo 	type = btrfs_chunk_type(leaf, chunk);
77985d07fbeSDaniel Xu 	raid_index = btrfs_bg_flags_to_raid_index(type);
78085d07fbeSDaniel Xu 	ncopies = btrfs_raid_array[raid_index].ncopies;
78185d07fbeSDaniel Xu 	nparity = btrfs_raid_array[raid_index].nparity;
78282fc28fbSQu Wenruo 
783c7c01a4aSDavid Sterba 	if (unlikely(!num_stripes)) {
784d001e4a3SDavid Sterba 		chunk_err(leaf, chunk, logical,
785f1140243SQu Wenruo 			  "invalid chunk num_stripes, have %u", num_stripes);
786bf871c3bSQu Wenruo 		return -EUCLEAN;
78782fc28fbSQu Wenruo 	}
788c7c01a4aSDavid Sterba 	if (unlikely(num_stripes < ncopies)) {
78985d07fbeSDaniel Xu 		chunk_err(leaf, chunk, logical,
79085d07fbeSDaniel Xu 			  "invalid chunk num_stripes < ncopies, have %u < %d",
79185d07fbeSDaniel Xu 			  num_stripes, ncopies);
79285d07fbeSDaniel Xu 		return -EUCLEAN;
79385d07fbeSDaniel Xu 	}
794c7c01a4aSDavid Sterba 	if (unlikely(nparity && num_stripes == nparity)) {
79585d07fbeSDaniel Xu 		chunk_err(leaf, chunk, logical,
79685d07fbeSDaniel Xu 			  "invalid chunk num_stripes == nparity, have %u == %d",
79785d07fbeSDaniel Xu 			  num_stripes, nparity);
79885d07fbeSDaniel Xu 		return -EUCLEAN;
79985d07fbeSDaniel Xu 	}
800c7c01a4aSDavid Sterba 	if (unlikely(!IS_ALIGNED(logical, fs_info->sectorsize))) {
801d001e4a3SDavid Sterba 		chunk_err(leaf, chunk, logical,
802f1140243SQu Wenruo 		"invalid chunk logical, have %llu should aligned to %u",
803f1140243SQu Wenruo 			  logical, fs_info->sectorsize);
804bf871c3bSQu Wenruo 		return -EUCLEAN;
80582fc28fbSQu Wenruo 	}
806c7c01a4aSDavid Sterba 	if (unlikely(btrfs_chunk_sector_size(leaf, chunk) != fs_info->sectorsize)) {
807d001e4a3SDavid Sterba 		chunk_err(leaf, chunk, logical,
808f1140243SQu Wenruo 			  "invalid chunk sectorsize, have %u expect %u",
809f1140243SQu Wenruo 			  btrfs_chunk_sector_size(leaf, chunk),
810f1140243SQu Wenruo 			  fs_info->sectorsize);
811bf871c3bSQu Wenruo 		return -EUCLEAN;
81282fc28fbSQu Wenruo 	}
813c7c01a4aSDavid Sterba 	if (unlikely(!length || !IS_ALIGNED(length, fs_info->sectorsize))) {
814d001e4a3SDavid Sterba 		chunk_err(leaf, chunk, logical,
815f1140243SQu Wenruo 			  "invalid chunk length, have %llu", length);
816bf871c3bSQu Wenruo 		return -EUCLEAN;
81782fc28fbSQu Wenruo 	}
818347fb0cfSSu Yue 	if (unlikely(check_add_overflow(logical, length, &chunk_end))) {
819347fb0cfSSu Yue 		chunk_err(leaf, chunk, logical,
820347fb0cfSSu Yue "invalid chunk logical start and length, have logical start %llu length %llu",
821347fb0cfSSu Yue 			  logical, length);
822347fb0cfSSu Yue 		return -EUCLEAN;
823347fb0cfSSu Yue 	}
824c7c01a4aSDavid Sterba 	if (unlikely(!is_power_of_2(stripe_len) || stripe_len != BTRFS_STRIPE_LEN)) {
825d001e4a3SDavid Sterba 		chunk_err(leaf, chunk, logical,
826f1140243SQu Wenruo 			  "invalid chunk stripe length: %llu",
82782fc28fbSQu Wenruo 			  stripe_len);
828bf871c3bSQu Wenruo 		return -EUCLEAN;
82982fc28fbSQu Wenruo 	}
830c7c01a4aSDavid Sterba 	if (unlikely(type & ~(BTRFS_BLOCK_GROUP_TYPE_MASK |
831c7c01a4aSDavid Sterba 			      BTRFS_BLOCK_GROUP_PROFILE_MASK))) {
832d001e4a3SDavid Sterba 		chunk_err(leaf, chunk, logical,
833f1140243SQu Wenruo 			  "unrecognized chunk type: 0x%llx",
83482fc28fbSQu Wenruo 			  ~(BTRFS_BLOCK_GROUP_TYPE_MASK |
83582fc28fbSQu Wenruo 			    BTRFS_BLOCK_GROUP_PROFILE_MASK) &
83682fc28fbSQu Wenruo 			  btrfs_chunk_type(leaf, chunk));
837bf871c3bSQu Wenruo 		return -EUCLEAN;
83882fc28fbSQu Wenruo 	}
83982fc28fbSQu Wenruo 
840c7c01a4aSDavid Sterba 	if (unlikely(!has_single_bit_set(type & BTRFS_BLOCK_GROUP_PROFILE_MASK) &&
841c7c01a4aSDavid Sterba 		     (type & BTRFS_BLOCK_GROUP_PROFILE_MASK) != 0)) {
842d001e4a3SDavid Sterba 		chunk_err(leaf, chunk, logical,
84380e46cf2SQu Wenruo 		"invalid chunk profile flag: 0x%llx, expect 0 or 1 bit set",
84480e46cf2SQu Wenruo 			  type & BTRFS_BLOCK_GROUP_PROFILE_MASK);
84580e46cf2SQu Wenruo 		return -EUCLEAN;
84680e46cf2SQu Wenruo 	}
847c7c01a4aSDavid Sterba 	if (unlikely((type & BTRFS_BLOCK_GROUP_TYPE_MASK) == 0)) {
848d001e4a3SDavid Sterba 		chunk_err(leaf, chunk, logical,
849f1140243SQu Wenruo 	"missing chunk type flag, have 0x%llx one bit must be set in 0x%llx",
850f1140243SQu Wenruo 			  type, BTRFS_BLOCK_GROUP_TYPE_MASK);
851bf871c3bSQu Wenruo 		return -EUCLEAN;
85282fc28fbSQu Wenruo 	}
85382fc28fbSQu Wenruo 
854c7c01a4aSDavid Sterba 	if (unlikely((type & BTRFS_BLOCK_GROUP_SYSTEM) &&
855c7c01a4aSDavid Sterba 		     (type & (BTRFS_BLOCK_GROUP_METADATA |
856c7c01a4aSDavid Sterba 			      BTRFS_BLOCK_GROUP_DATA)))) {
857d001e4a3SDavid Sterba 		chunk_err(leaf, chunk, logical,
858f1140243SQu Wenruo 			  "system chunk with data or metadata type: 0x%llx",
859f1140243SQu Wenruo 			  type);
860bf871c3bSQu Wenruo 		return -EUCLEAN;
86182fc28fbSQu Wenruo 	}
86282fc28fbSQu Wenruo 
86382fc28fbSQu Wenruo 	features = btrfs_super_incompat_flags(fs_info->super_copy);
86482fc28fbSQu Wenruo 	if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
86582fc28fbSQu Wenruo 		mixed = true;
86682fc28fbSQu Wenruo 
86782fc28fbSQu Wenruo 	if (!mixed) {
868c7c01a4aSDavid Sterba 		if (unlikely((type & BTRFS_BLOCK_GROUP_METADATA) &&
869c7c01a4aSDavid Sterba 			     (type & BTRFS_BLOCK_GROUP_DATA))) {
870d001e4a3SDavid Sterba 			chunk_err(leaf, chunk, logical,
87182fc28fbSQu Wenruo 			"mixed chunk type in non-mixed mode: 0x%llx", type);
872bf871c3bSQu Wenruo 			return -EUCLEAN;
87382fc28fbSQu Wenruo 		}
87482fc28fbSQu Wenruo 	}
87582fc28fbSQu Wenruo 
876*0ac6e06bSDavid Sterba 	if (unlikely((type & BTRFS_BLOCK_GROUP_RAID10 &&
877*0ac6e06bSDavid Sterba 		      sub_stripes != btrfs_raid_array[BTRFS_RAID_RAID10].sub_stripes) ||
878*0ac6e06bSDavid Sterba 		     (type & BTRFS_BLOCK_GROUP_RAID1 &&
879*0ac6e06bSDavid Sterba 		      num_stripes != btrfs_raid_array[BTRFS_RAID_RAID1].devs_min) ||
880*0ac6e06bSDavid Sterba 		     (type & BTRFS_BLOCK_GROUP_RAID5 &&
881*0ac6e06bSDavid Sterba 		      num_stripes < btrfs_raid_array[BTRFS_RAID_RAID5].devs_min) ||
882*0ac6e06bSDavid Sterba 		     (type & BTRFS_BLOCK_GROUP_RAID6 &&
883*0ac6e06bSDavid Sterba 		      num_stripes < btrfs_raid_array[BTRFS_RAID_RAID6].devs_min) ||
884*0ac6e06bSDavid Sterba 		     (type & BTRFS_BLOCK_GROUP_DUP &&
885*0ac6e06bSDavid Sterba 		      num_stripes != btrfs_raid_array[BTRFS_RAID_DUP].dev_stripes) ||
886c7c01a4aSDavid Sterba 		     ((type & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 &&
887*0ac6e06bSDavid Sterba 		      num_stripes != btrfs_raid_array[BTRFS_RAID_SINGLE].dev_stripes))) {
888d001e4a3SDavid Sterba 		chunk_err(leaf, chunk, logical,
88982fc28fbSQu Wenruo 			"invalid num_stripes:sub_stripes %u:%u for profile %llu",
89082fc28fbSQu Wenruo 			num_stripes, sub_stripes,
89182fc28fbSQu Wenruo 			type & BTRFS_BLOCK_GROUP_PROFILE_MASK);
892bf871c3bSQu Wenruo 		return -EUCLEAN;
89382fc28fbSQu Wenruo 	}
89482fc28fbSQu Wenruo 
89582fc28fbSQu Wenruo 	return 0;
89682fc28fbSQu Wenruo }
89782fc28fbSQu Wenruo 
898f6d2a5c2SQu Wenruo /*
899f6d2a5c2SQu Wenruo  * Enhanced version of chunk item checker.
900f6d2a5c2SQu Wenruo  *
901f6d2a5c2SQu Wenruo  * The common btrfs_check_chunk_valid() doesn't check item size since it needs
902f6d2a5c2SQu Wenruo  * to work on super block sys_chunk_array which doesn't have full item ptr.
903f6d2a5c2SQu Wenruo  */
904f6d2a5c2SQu Wenruo static int check_leaf_chunk_item(struct extent_buffer *leaf,
905f6d2a5c2SQu Wenruo 				 struct btrfs_chunk *chunk,
906f6d2a5c2SQu Wenruo 				 struct btrfs_key *key, int slot)
907f6d2a5c2SQu Wenruo {
908f6d2a5c2SQu Wenruo 	int num_stripes;
909f6d2a5c2SQu Wenruo 
910c7c01a4aSDavid Sterba 	if (unlikely(btrfs_item_size_nr(leaf, slot) < sizeof(struct btrfs_chunk))) {
911f6d2a5c2SQu Wenruo 		chunk_err(leaf, chunk, key->offset,
912f6d2a5c2SQu Wenruo 			"invalid chunk item size: have %u expect [%zu, %u)",
913f6d2a5c2SQu Wenruo 			btrfs_item_size_nr(leaf, slot),
914f6d2a5c2SQu Wenruo 			sizeof(struct btrfs_chunk),
915f6d2a5c2SQu Wenruo 			BTRFS_LEAF_DATA_SIZE(leaf->fs_info));
916f6d2a5c2SQu Wenruo 		return -EUCLEAN;
917f6d2a5c2SQu Wenruo 	}
918f6d2a5c2SQu Wenruo 
919f6d2a5c2SQu Wenruo 	num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
920f6d2a5c2SQu Wenruo 	/* Let btrfs_check_chunk_valid() handle this error type */
921f6d2a5c2SQu Wenruo 	if (num_stripes == 0)
922f6d2a5c2SQu Wenruo 		goto out;
923f6d2a5c2SQu Wenruo 
924c7c01a4aSDavid Sterba 	if (unlikely(btrfs_chunk_item_size(num_stripes) !=
925c7c01a4aSDavid Sterba 		     btrfs_item_size_nr(leaf, slot))) {
926f6d2a5c2SQu Wenruo 		chunk_err(leaf, chunk, key->offset,
927f6d2a5c2SQu Wenruo 			"invalid chunk item size: have %u expect %lu",
928f6d2a5c2SQu Wenruo 			btrfs_item_size_nr(leaf, slot),
929f6d2a5c2SQu Wenruo 			btrfs_chunk_item_size(num_stripes));
930f6d2a5c2SQu Wenruo 		return -EUCLEAN;
931f6d2a5c2SQu Wenruo 	}
932f6d2a5c2SQu Wenruo out:
933f6d2a5c2SQu Wenruo 	return btrfs_check_chunk_valid(leaf, chunk, key->offset);
934f6d2a5c2SQu Wenruo }
935f6d2a5c2SQu Wenruo 
9365617ed80SDavid Sterba __printf(3, 4)
937ab4ba2e1SQu Wenruo __cold
9385617ed80SDavid Sterba static void dev_item_err(const struct extent_buffer *eb, int slot,
939ab4ba2e1SQu Wenruo 			 const char *fmt, ...)
940ab4ba2e1SQu Wenruo {
941ab4ba2e1SQu Wenruo 	struct btrfs_key key;
942ab4ba2e1SQu Wenruo 	struct va_format vaf;
943ab4ba2e1SQu Wenruo 	va_list args;
944ab4ba2e1SQu Wenruo 
945ab4ba2e1SQu Wenruo 	btrfs_item_key_to_cpu(eb, &key, slot);
946ab4ba2e1SQu Wenruo 	va_start(args, fmt);
947ab4ba2e1SQu Wenruo 
948ab4ba2e1SQu Wenruo 	vaf.fmt = fmt;
949ab4ba2e1SQu Wenruo 	vaf.va = &args;
950ab4ba2e1SQu Wenruo 
9515617ed80SDavid Sterba 	btrfs_crit(eb->fs_info,
952ab4ba2e1SQu Wenruo 	"corrupt %s: root=%llu block=%llu slot=%d devid=%llu %pV",
953ab4ba2e1SQu Wenruo 		btrfs_header_level(eb) == 0 ? "leaf" : "node",
954ab4ba2e1SQu Wenruo 		btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot,
955ab4ba2e1SQu Wenruo 		key.objectid, &vaf);
956ab4ba2e1SQu Wenruo 	va_end(args);
957ab4ba2e1SQu Wenruo }
958ab4ba2e1SQu Wenruo 
959412a2312SDavid Sterba static int check_dev_item(struct extent_buffer *leaf,
960ab4ba2e1SQu Wenruo 			  struct btrfs_key *key, int slot)
961ab4ba2e1SQu Wenruo {
962ab4ba2e1SQu Wenruo 	struct btrfs_dev_item *ditem;
963ab4ba2e1SQu Wenruo 
964c7c01a4aSDavid Sterba 	if (unlikely(key->objectid != BTRFS_DEV_ITEMS_OBJECTID)) {
9655617ed80SDavid Sterba 		dev_item_err(leaf, slot,
966ab4ba2e1SQu Wenruo 			     "invalid objectid: has=%llu expect=%llu",
967ab4ba2e1SQu Wenruo 			     key->objectid, BTRFS_DEV_ITEMS_OBJECTID);
968ab4ba2e1SQu Wenruo 		return -EUCLEAN;
969ab4ba2e1SQu Wenruo 	}
970ab4ba2e1SQu Wenruo 	ditem = btrfs_item_ptr(leaf, slot, struct btrfs_dev_item);
971c7c01a4aSDavid Sterba 	if (unlikely(btrfs_device_id(leaf, ditem) != key->offset)) {
9725617ed80SDavid Sterba 		dev_item_err(leaf, slot,
973ab4ba2e1SQu Wenruo 			     "devid mismatch: key has=%llu item has=%llu",
974ab4ba2e1SQu Wenruo 			     key->offset, btrfs_device_id(leaf, ditem));
975ab4ba2e1SQu Wenruo 		return -EUCLEAN;
976ab4ba2e1SQu Wenruo 	}
977ab4ba2e1SQu Wenruo 
978ab4ba2e1SQu Wenruo 	/*
979ab4ba2e1SQu Wenruo 	 * For device total_bytes, we don't have reliable way to check it, as
980ab4ba2e1SQu Wenruo 	 * it can be 0 for device removal. Device size check can only be done
981ab4ba2e1SQu Wenruo 	 * by dev extents check.
982ab4ba2e1SQu Wenruo 	 */
983c7c01a4aSDavid Sterba 	if (unlikely(btrfs_device_bytes_used(leaf, ditem) >
984c7c01a4aSDavid Sterba 		     btrfs_device_total_bytes(leaf, ditem))) {
9855617ed80SDavid Sterba 		dev_item_err(leaf, slot,
986ab4ba2e1SQu Wenruo 			     "invalid bytes used: have %llu expect [0, %llu]",
987ab4ba2e1SQu Wenruo 			     btrfs_device_bytes_used(leaf, ditem),
988ab4ba2e1SQu Wenruo 			     btrfs_device_total_bytes(leaf, ditem));
989ab4ba2e1SQu Wenruo 		return -EUCLEAN;
990ab4ba2e1SQu Wenruo 	}
991ab4ba2e1SQu Wenruo 	/*
992ab4ba2e1SQu Wenruo 	 * Remaining members like io_align/type/gen/dev_group aren't really
993ab4ba2e1SQu Wenruo 	 * utilized.  Skip them to make later usage of them easier.
994ab4ba2e1SQu Wenruo 	 */
995ab4ba2e1SQu Wenruo 	return 0;
996ab4ba2e1SQu Wenruo }
997ab4ba2e1SQu Wenruo 
99839e57f49SDavid Sterba static int check_inode_item(struct extent_buffer *leaf,
999496245caSQu Wenruo 			    struct btrfs_key *key, int slot)
1000496245caSQu Wenruo {
100139e57f49SDavid Sterba 	struct btrfs_fs_info *fs_info = leaf->fs_info;
1002496245caSQu Wenruo 	struct btrfs_inode_item *iitem;
1003496245caSQu Wenruo 	u64 super_gen = btrfs_super_generation(fs_info->super_copy);
1004496245caSQu Wenruo 	u32 valid_mask = (S_IFMT | S_ISUID | S_ISGID | S_ISVTX | 0777);
1005496245caSQu Wenruo 	u32 mode;
1006c23c77b0SQu Wenruo 	int ret;
1007496245caSQu Wenruo 
1008c23c77b0SQu Wenruo 	ret = check_inode_key(leaf, key, slot);
1009c7c01a4aSDavid Sterba 	if (unlikely(ret < 0))
1010c23c77b0SQu Wenruo 		return ret;
1011c23c77b0SQu Wenruo 
1012496245caSQu Wenruo 	iitem = btrfs_item_ptr(leaf, slot, struct btrfs_inode_item);
1013496245caSQu Wenruo 
1014496245caSQu Wenruo 	/* Here we use super block generation + 1 to handle log tree */
1015c7c01a4aSDavid Sterba 	if (unlikely(btrfs_inode_generation(leaf, iitem) > super_gen + 1)) {
1016c3053ebbSQu Wenruo 		inode_item_err(leaf, slot,
1017496245caSQu Wenruo 			"invalid inode generation: has %llu expect (0, %llu]",
1018496245caSQu Wenruo 			       btrfs_inode_generation(leaf, iitem),
1019496245caSQu Wenruo 			       super_gen + 1);
1020496245caSQu Wenruo 		return -EUCLEAN;
1021496245caSQu Wenruo 	}
1022496245caSQu Wenruo 	/* Note for ROOT_TREE_DIR_ITEM, mkfs could set its transid 0 */
1023c7c01a4aSDavid Sterba 	if (unlikely(btrfs_inode_transid(leaf, iitem) > super_gen + 1)) {
1024c3053ebbSQu Wenruo 		inode_item_err(leaf, slot,
1025f96d6960SQu Wenruo 			"invalid inode transid: has %llu expect [0, %llu]",
1026496245caSQu Wenruo 			       btrfs_inode_transid(leaf, iitem), super_gen + 1);
1027496245caSQu Wenruo 		return -EUCLEAN;
1028496245caSQu Wenruo 	}
1029496245caSQu Wenruo 
1030496245caSQu Wenruo 	/*
1031496245caSQu Wenruo 	 * For size and nbytes it's better not to be too strict, as for dir
1032496245caSQu Wenruo 	 * item its size/nbytes can easily get wrong, but doesn't affect
1033496245caSQu Wenruo 	 * anything in the fs. So here we skip the check.
1034496245caSQu Wenruo 	 */
1035496245caSQu Wenruo 	mode = btrfs_inode_mode(leaf, iitem);
1036c7c01a4aSDavid Sterba 	if (unlikely(mode & ~valid_mask)) {
1037c3053ebbSQu Wenruo 		inode_item_err(leaf, slot,
1038496245caSQu Wenruo 			       "unknown mode bit detected: 0x%x",
1039496245caSQu Wenruo 			       mode & ~valid_mask);
1040496245caSQu Wenruo 		return -EUCLEAN;
1041496245caSQu Wenruo 	}
1042496245caSQu Wenruo 
1043496245caSQu Wenruo 	/*
1044c1499166SDavid Sterba 	 * S_IFMT is not bit mapped so we can't completely rely on
1045c1499166SDavid Sterba 	 * is_power_of_2/has_single_bit_set, but it can save us from checking
1046c1499166SDavid Sterba 	 * FIFO/CHR/DIR/REG.  Only needs to check BLK, LNK and SOCKS
1047496245caSQu Wenruo 	 */
1048c1499166SDavid Sterba 	if (!has_single_bit_set(mode & S_IFMT)) {
1049c7c01a4aSDavid Sterba 		if (unlikely(!S_ISLNK(mode) && !S_ISBLK(mode) && !S_ISSOCK(mode))) {
1050c3053ebbSQu Wenruo 			inode_item_err(leaf, slot,
1051496245caSQu Wenruo 			"invalid mode: has 0%o expect valid S_IF* bit(s)",
1052496245caSQu Wenruo 				       mode & S_IFMT);
1053496245caSQu Wenruo 			return -EUCLEAN;
1054496245caSQu Wenruo 		}
1055496245caSQu Wenruo 	}
1056c7c01a4aSDavid Sterba 	if (unlikely(S_ISDIR(mode) && btrfs_inode_nlink(leaf, iitem) > 1)) {
1057c3053ebbSQu Wenruo 		inode_item_err(leaf, slot,
1058496245caSQu Wenruo 		       "invalid nlink: has %u expect no more than 1 for dir",
1059496245caSQu Wenruo 			btrfs_inode_nlink(leaf, iitem));
1060496245caSQu Wenruo 		return -EUCLEAN;
1061496245caSQu Wenruo 	}
1062c7c01a4aSDavid Sterba 	if (unlikely(btrfs_inode_flags(leaf, iitem) & ~BTRFS_INODE_FLAG_MASK)) {
1063c3053ebbSQu Wenruo 		inode_item_err(leaf, slot,
1064496245caSQu Wenruo 			       "unknown flags detected: 0x%llx",
1065496245caSQu Wenruo 			       btrfs_inode_flags(leaf, iitem) &
1066496245caSQu Wenruo 			       ~BTRFS_INODE_FLAG_MASK);
1067496245caSQu Wenruo 		return -EUCLEAN;
1068496245caSQu Wenruo 	}
1069496245caSQu Wenruo 	return 0;
1070496245caSQu Wenruo }
1071496245caSQu Wenruo 
1072259ee775SQu Wenruo static int check_root_item(struct extent_buffer *leaf, struct btrfs_key *key,
1073259ee775SQu Wenruo 			   int slot)
1074259ee775SQu Wenruo {
1075259ee775SQu Wenruo 	struct btrfs_fs_info *fs_info = leaf->fs_info;
10761465af12SQu Wenruo 	struct btrfs_root_item ri = { 0 };
1077259ee775SQu Wenruo 	const u64 valid_root_flags = BTRFS_ROOT_SUBVOL_RDONLY |
1078259ee775SQu Wenruo 				     BTRFS_ROOT_SUBVOL_DEAD;
107957a0e674SQu Wenruo 	int ret;
1080259ee775SQu Wenruo 
108157a0e674SQu Wenruo 	ret = check_root_key(leaf, key, slot);
1082c7c01a4aSDavid Sterba 	if (unlikely(ret < 0))
108357a0e674SQu Wenruo 		return ret;
1084259ee775SQu Wenruo 
1085c7c01a4aSDavid Sterba 	if (unlikely(btrfs_item_size_nr(leaf, slot) != sizeof(ri) &&
1086c7c01a4aSDavid Sterba 		     btrfs_item_size_nr(leaf, slot) !=
1087c7c01a4aSDavid Sterba 		     btrfs_legacy_root_item_size())) {
1088259ee775SQu Wenruo 		generic_err(leaf, slot,
10891465af12SQu Wenruo 			    "invalid root item size, have %u expect %zu or %u",
10901465af12SQu Wenruo 			    btrfs_item_size_nr(leaf, slot), sizeof(ri),
10911465af12SQu Wenruo 			    btrfs_legacy_root_item_size());
10921a49a97dSDaniel Xu 		return -EUCLEAN;
1093259ee775SQu Wenruo 	}
1094259ee775SQu Wenruo 
10951465af12SQu Wenruo 	/*
10961465af12SQu Wenruo 	 * For legacy root item, the members starting at generation_v2 will be
10971465af12SQu Wenruo 	 * all filled with 0.
10981465af12SQu Wenruo 	 * And since we allow geneartion_v2 as 0, it will still pass the check.
10991465af12SQu Wenruo 	 */
1100259ee775SQu Wenruo 	read_extent_buffer(leaf, &ri, btrfs_item_ptr_offset(leaf, slot),
11011465af12SQu Wenruo 			   btrfs_item_size_nr(leaf, slot));
1102259ee775SQu Wenruo 
1103259ee775SQu Wenruo 	/* Generation related */
1104c7c01a4aSDavid Sterba 	if (unlikely(btrfs_root_generation(&ri) >
1105c7c01a4aSDavid Sterba 		     btrfs_super_generation(fs_info->super_copy) + 1)) {
1106259ee775SQu Wenruo 		generic_err(leaf, slot,
1107259ee775SQu Wenruo 			"invalid root generation, have %llu expect (0, %llu]",
1108259ee775SQu Wenruo 			    btrfs_root_generation(&ri),
1109259ee775SQu Wenruo 			    btrfs_super_generation(fs_info->super_copy) + 1);
1110259ee775SQu Wenruo 		return -EUCLEAN;
1111259ee775SQu Wenruo 	}
1112c7c01a4aSDavid Sterba 	if (unlikely(btrfs_root_generation_v2(&ri) >
1113c7c01a4aSDavid Sterba 		     btrfs_super_generation(fs_info->super_copy) + 1)) {
1114259ee775SQu Wenruo 		generic_err(leaf, slot,
1115259ee775SQu Wenruo 		"invalid root v2 generation, have %llu expect (0, %llu]",
1116259ee775SQu Wenruo 			    btrfs_root_generation_v2(&ri),
1117259ee775SQu Wenruo 			    btrfs_super_generation(fs_info->super_copy) + 1);
1118259ee775SQu Wenruo 		return -EUCLEAN;
1119259ee775SQu Wenruo 	}
1120c7c01a4aSDavid Sterba 	if (unlikely(btrfs_root_last_snapshot(&ri) >
1121c7c01a4aSDavid Sterba 		     btrfs_super_generation(fs_info->super_copy) + 1)) {
1122259ee775SQu Wenruo 		generic_err(leaf, slot,
1123259ee775SQu Wenruo 		"invalid root last_snapshot, have %llu expect (0, %llu]",
1124259ee775SQu Wenruo 			    btrfs_root_last_snapshot(&ri),
1125259ee775SQu Wenruo 			    btrfs_super_generation(fs_info->super_copy) + 1);
1126259ee775SQu Wenruo 		return -EUCLEAN;
1127259ee775SQu Wenruo 	}
1128259ee775SQu Wenruo 
1129259ee775SQu Wenruo 	/* Alignment and level check */
1130c7c01a4aSDavid Sterba 	if (unlikely(!IS_ALIGNED(btrfs_root_bytenr(&ri), fs_info->sectorsize))) {
1131259ee775SQu Wenruo 		generic_err(leaf, slot,
1132259ee775SQu Wenruo 		"invalid root bytenr, have %llu expect to be aligned to %u",
1133259ee775SQu Wenruo 			    btrfs_root_bytenr(&ri), fs_info->sectorsize);
1134259ee775SQu Wenruo 		return -EUCLEAN;
1135259ee775SQu Wenruo 	}
1136c7c01a4aSDavid Sterba 	if (unlikely(btrfs_root_level(&ri) >= BTRFS_MAX_LEVEL)) {
1137259ee775SQu Wenruo 		generic_err(leaf, slot,
1138259ee775SQu Wenruo 			    "invalid root level, have %u expect [0, %u]",
1139259ee775SQu Wenruo 			    btrfs_root_level(&ri), BTRFS_MAX_LEVEL - 1);
1140259ee775SQu Wenruo 		return -EUCLEAN;
1141259ee775SQu Wenruo 	}
1142c7c01a4aSDavid Sterba 	if (unlikely(btrfs_root_drop_level(&ri) >= BTRFS_MAX_LEVEL)) {
1143259ee775SQu Wenruo 		generic_err(leaf, slot,
1144259ee775SQu Wenruo 			    "invalid root level, have %u expect [0, %u]",
1145c8422684SDavid Sterba 			    btrfs_root_drop_level(&ri), BTRFS_MAX_LEVEL - 1);
1146259ee775SQu Wenruo 		return -EUCLEAN;
1147259ee775SQu Wenruo 	}
1148259ee775SQu Wenruo 
1149259ee775SQu Wenruo 	/* Flags check */
1150c7c01a4aSDavid Sterba 	if (unlikely(btrfs_root_flags(&ri) & ~valid_root_flags)) {
1151259ee775SQu Wenruo 		generic_err(leaf, slot,
1152259ee775SQu Wenruo 			    "invalid root flags, have 0x%llx expect mask 0x%llx",
1153259ee775SQu Wenruo 			    btrfs_root_flags(&ri), valid_root_flags);
1154259ee775SQu Wenruo 		return -EUCLEAN;
1155259ee775SQu Wenruo 	}
1156259ee775SQu Wenruo 	return 0;
1157259ee775SQu Wenruo }
1158259ee775SQu Wenruo 
1159f82d1c7cSQu Wenruo __printf(3,4)
1160f82d1c7cSQu Wenruo __cold
1161f82d1c7cSQu Wenruo static void extent_err(const struct extent_buffer *eb, int slot,
1162f82d1c7cSQu Wenruo 		       const char *fmt, ...)
1163f82d1c7cSQu Wenruo {
1164f82d1c7cSQu Wenruo 	struct btrfs_key key;
1165f82d1c7cSQu Wenruo 	struct va_format vaf;
1166f82d1c7cSQu Wenruo 	va_list args;
1167f82d1c7cSQu Wenruo 	u64 bytenr;
1168f82d1c7cSQu Wenruo 	u64 len;
1169f82d1c7cSQu Wenruo 
1170f82d1c7cSQu Wenruo 	btrfs_item_key_to_cpu(eb, &key, slot);
1171f82d1c7cSQu Wenruo 	bytenr = key.objectid;
1172e2406a6fSQu Wenruo 	if (key.type == BTRFS_METADATA_ITEM_KEY ||
1173e2406a6fSQu Wenruo 	    key.type == BTRFS_TREE_BLOCK_REF_KEY ||
1174e2406a6fSQu Wenruo 	    key.type == BTRFS_SHARED_BLOCK_REF_KEY)
1175f82d1c7cSQu Wenruo 		len = eb->fs_info->nodesize;
1176f82d1c7cSQu Wenruo 	else
1177f82d1c7cSQu Wenruo 		len = key.offset;
1178f82d1c7cSQu Wenruo 	va_start(args, fmt);
1179f82d1c7cSQu Wenruo 
1180f82d1c7cSQu Wenruo 	vaf.fmt = fmt;
1181f82d1c7cSQu Wenruo 	vaf.va = &args;
1182f82d1c7cSQu Wenruo 
1183f82d1c7cSQu Wenruo 	btrfs_crit(eb->fs_info,
1184f82d1c7cSQu Wenruo 	"corrupt %s: block=%llu slot=%d extent bytenr=%llu len=%llu %pV",
1185f82d1c7cSQu Wenruo 		btrfs_header_level(eb) == 0 ? "leaf" : "node",
1186f82d1c7cSQu Wenruo 		eb->start, slot, bytenr, len, &vaf);
1187f82d1c7cSQu Wenruo 	va_end(args);
1188f82d1c7cSQu Wenruo }
1189f82d1c7cSQu Wenruo 
1190f82d1c7cSQu Wenruo static int check_extent_item(struct extent_buffer *leaf,
1191f82d1c7cSQu Wenruo 			     struct btrfs_key *key, int slot)
1192f82d1c7cSQu Wenruo {
1193f82d1c7cSQu Wenruo 	struct btrfs_fs_info *fs_info = leaf->fs_info;
1194f82d1c7cSQu Wenruo 	struct btrfs_extent_item *ei;
1195f82d1c7cSQu Wenruo 	bool is_tree_block = false;
1196f82d1c7cSQu Wenruo 	unsigned long ptr;	/* Current pointer inside inline refs */
1197f82d1c7cSQu Wenruo 	unsigned long end;	/* Extent item end */
1198f82d1c7cSQu Wenruo 	const u32 item_size = btrfs_item_size_nr(leaf, slot);
1199f82d1c7cSQu Wenruo 	u64 flags;
1200f82d1c7cSQu Wenruo 	u64 generation;
1201f82d1c7cSQu Wenruo 	u64 total_refs;		/* Total refs in btrfs_extent_item */
1202f82d1c7cSQu Wenruo 	u64 inline_refs = 0;	/* found total inline refs */
1203f82d1c7cSQu Wenruo 
1204c7c01a4aSDavid Sterba 	if (unlikely(key->type == BTRFS_METADATA_ITEM_KEY &&
1205c7c01a4aSDavid Sterba 		     !btrfs_fs_incompat(fs_info, SKINNY_METADATA))) {
1206f82d1c7cSQu Wenruo 		generic_err(leaf, slot,
1207f82d1c7cSQu Wenruo "invalid key type, METADATA_ITEM type invalid when SKINNY_METADATA feature disabled");
1208f82d1c7cSQu Wenruo 		return -EUCLEAN;
1209f82d1c7cSQu Wenruo 	}
1210f82d1c7cSQu Wenruo 	/* key->objectid is the bytenr for both key types */
1211c7c01a4aSDavid Sterba 	if (unlikely(!IS_ALIGNED(key->objectid, fs_info->sectorsize))) {
1212f82d1c7cSQu Wenruo 		generic_err(leaf, slot,
1213f82d1c7cSQu Wenruo 		"invalid key objectid, have %llu expect to be aligned to %u",
1214f82d1c7cSQu Wenruo 			   key->objectid, fs_info->sectorsize);
1215f82d1c7cSQu Wenruo 		return -EUCLEAN;
1216f82d1c7cSQu Wenruo 	}
1217f82d1c7cSQu Wenruo 
1218f82d1c7cSQu Wenruo 	/* key->offset is tree level for METADATA_ITEM_KEY */
1219c7c01a4aSDavid Sterba 	if (unlikely(key->type == BTRFS_METADATA_ITEM_KEY &&
1220c7c01a4aSDavid Sterba 		     key->offset >= BTRFS_MAX_LEVEL)) {
1221f82d1c7cSQu Wenruo 		extent_err(leaf, slot,
1222f82d1c7cSQu Wenruo 			   "invalid tree level, have %llu expect [0, %u]",
1223f82d1c7cSQu Wenruo 			   key->offset, BTRFS_MAX_LEVEL - 1);
1224f82d1c7cSQu Wenruo 		return -EUCLEAN;
1225f82d1c7cSQu Wenruo 	}
1226f82d1c7cSQu Wenruo 
1227f82d1c7cSQu Wenruo 	/*
1228f82d1c7cSQu Wenruo 	 * EXTENT/METADATA_ITEM consists of:
1229f82d1c7cSQu Wenruo 	 * 1) One btrfs_extent_item
1230f82d1c7cSQu Wenruo 	 *    Records the total refs, type and generation of the extent.
1231f82d1c7cSQu Wenruo 	 *
1232f82d1c7cSQu Wenruo 	 * 2) One btrfs_tree_block_info (for EXTENT_ITEM and tree backref only)
1233f82d1c7cSQu Wenruo 	 *    Records the first key and level of the tree block.
1234f82d1c7cSQu Wenruo 	 *
1235f82d1c7cSQu Wenruo 	 * 2) Zero or more btrfs_extent_inline_ref(s)
1236f82d1c7cSQu Wenruo 	 *    Each inline ref has one btrfs_extent_inline_ref shows:
1237f82d1c7cSQu Wenruo 	 *    2.1) The ref type, one of the 4
1238f82d1c7cSQu Wenruo 	 *         TREE_BLOCK_REF	Tree block only
1239f82d1c7cSQu Wenruo 	 *         SHARED_BLOCK_REF	Tree block only
1240f82d1c7cSQu Wenruo 	 *         EXTENT_DATA_REF	Data only
1241f82d1c7cSQu Wenruo 	 *         SHARED_DATA_REF	Data only
1242f82d1c7cSQu Wenruo 	 *    2.2) Ref type specific data
1243f82d1c7cSQu Wenruo 	 *         Either using btrfs_extent_inline_ref::offset, or specific
1244f82d1c7cSQu Wenruo 	 *         data structure.
1245f82d1c7cSQu Wenruo 	 */
1246c7c01a4aSDavid Sterba 	if (unlikely(item_size < sizeof(*ei))) {
1247f82d1c7cSQu Wenruo 		extent_err(leaf, slot,
1248f82d1c7cSQu Wenruo 			   "invalid item size, have %u expect [%zu, %u)",
1249f82d1c7cSQu Wenruo 			   item_size, sizeof(*ei),
1250f82d1c7cSQu Wenruo 			   BTRFS_LEAF_DATA_SIZE(fs_info));
1251f82d1c7cSQu Wenruo 		return -EUCLEAN;
1252f82d1c7cSQu Wenruo 	}
1253f82d1c7cSQu Wenruo 	end = item_size + btrfs_item_ptr_offset(leaf, slot);
1254f82d1c7cSQu Wenruo 
1255f82d1c7cSQu Wenruo 	/* Checks against extent_item */
1256f82d1c7cSQu Wenruo 	ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
1257f82d1c7cSQu Wenruo 	flags = btrfs_extent_flags(leaf, ei);
1258f82d1c7cSQu Wenruo 	total_refs = btrfs_extent_refs(leaf, ei);
1259f82d1c7cSQu Wenruo 	generation = btrfs_extent_generation(leaf, ei);
1260c7c01a4aSDavid Sterba 	if (unlikely(generation >
1261c7c01a4aSDavid Sterba 		     btrfs_super_generation(fs_info->super_copy) + 1)) {
1262f82d1c7cSQu Wenruo 		extent_err(leaf, slot,
1263f82d1c7cSQu Wenruo 			   "invalid generation, have %llu expect (0, %llu]",
1264f82d1c7cSQu Wenruo 			   generation,
1265f82d1c7cSQu Wenruo 			   btrfs_super_generation(fs_info->super_copy) + 1);
1266f82d1c7cSQu Wenruo 		return -EUCLEAN;
1267f82d1c7cSQu Wenruo 	}
1268c7c01a4aSDavid Sterba 	if (unlikely(!has_single_bit_set(flags & (BTRFS_EXTENT_FLAG_DATA |
1269c7c01a4aSDavid Sterba 						  BTRFS_EXTENT_FLAG_TREE_BLOCK)))) {
1270f82d1c7cSQu Wenruo 		extent_err(leaf, slot,
1271f82d1c7cSQu Wenruo 		"invalid extent flag, have 0x%llx expect 1 bit set in 0x%llx",
1272f82d1c7cSQu Wenruo 			flags, BTRFS_EXTENT_FLAG_DATA |
1273f82d1c7cSQu Wenruo 			BTRFS_EXTENT_FLAG_TREE_BLOCK);
1274f82d1c7cSQu Wenruo 		return -EUCLEAN;
1275f82d1c7cSQu Wenruo 	}
1276f82d1c7cSQu Wenruo 	is_tree_block = !!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK);
1277f82d1c7cSQu Wenruo 	if (is_tree_block) {
1278c7c01a4aSDavid Sterba 		if (unlikely(key->type == BTRFS_EXTENT_ITEM_KEY &&
1279c7c01a4aSDavid Sterba 			     key->offset != fs_info->nodesize)) {
1280f82d1c7cSQu Wenruo 			extent_err(leaf, slot,
1281f82d1c7cSQu Wenruo 				   "invalid extent length, have %llu expect %u",
1282f82d1c7cSQu Wenruo 				   key->offset, fs_info->nodesize);
1283f82d1c7cSQu Wenruo 			return -EUCLEAN;
1284f82d1c7cSQu Wenruo 		}
1285f82d1c7cSQu Wenruo 	} else {
1286c7c01a4aSDavid Sterba 		if (unlikely(key->type != BTRFS_EXTENT_ITEM_KEY)) {
1287f82d1c7cSQu Wenruo 			extent_err(leaf, slot,
1288f82d1c7cSQu Wenruo 			"invalid key type, have %u expect %u for data backref",
1289f82d1c7cSQu Wenruo 				   key->type, BTRFS_EXTENT_ITEM_KEY);
1290f82d1c7cSQu Wenruo 			return -EUCLEAN;
1291f82d1c7cSQu Wenruo 		}
1292c7c01a4aSDavid Sterba 		if (unlikely(!IS_ALIGNED(key->offset, fs_info->sectorsize))) {
1293f82d1c7cSQu Wenruo 			extent_err(leaf, slot,
1294f82d1c7cSQu Wenruo 			"invalid extent length, have %llu expect aligned to %u",
1295f82d1c7cSQu Wenruo 				   key->offset, fs_info->sectorsize);
1296f82d1c7cSQu Wenruo 			return -EUCLEAN;
1297f82d1c7cSQu Wenruo 		}
12980ebb6bbbSJosef Bacik 		if (unlikely(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
12990ebb6bbbSJosef Bacik 			extent_err(leaf, slot,
13000ebb6bbbSJosef Bacik 			"invalid extent flag, data has full backref set");
13010ebb6bbbSJosef Bacik 			return -EUCLEAN;
13020ebb6bbbSJosef Bacik 		}
1303f82d1c7cSQu Wenruo 	}
1304f82d1c7cSQu Wenruo 	ptr = (unsigned long)(struct btrfs_extent_item *)(ei + 1);
1305f82d1c7cSQu Wenruo 
1306f82d1c7cSQu Wenruo 	/* Check the special case of btrfs_tree_block_info */
1307f82d1c7cSQu Wenruo 	if (is_tree_block && key->type != BTRFS_METADATA_ITEM_KEY) {
1308f82d1c7cSQu Wenruo 		struct btrfs_tree_block_info *info;
1309f82d1c7cSQu Wenruo 
1310f82d1c7cSQu Wenruo 		info = (struct btrfs_tree_block_info *)ptr;
1311c7c01a4aSDavid Sterba 		if (unlikely(btrfs_tree_block_level(leaf, info) >= BTRFS_MAX_LEVEL)) {
1312f82d1c7cSQu Wenruo 			extent_err(leaf, slot,
1313f82d1c7cSQu Wenruo 			"invalid tree block info level, have %u expect [0, %u]",
1314f82d1c7cSQu Wenruo 				   btrfs_tree_block_level(leaf, info),
1315f82d1c7cSQu Wenruo 				   BTRFS_MAX_LEVEL - 1);
1316f82d1c7cSQu Wenruo 			return -EUCLEAN;
1317f82d1c7cSQu Wenruo 		}
1318f82d1c7cSQu Wenruo 		ptr = (unsigned long)(struct btrfs_tree_block_info *)(info + 1);
1319f82d1c7cSQu Wenruo 	}
1320f82d1c7cSQu Wenruo 
1321f82d1c7cSQu Wenruo 	/* Check inline refs */
1322f82d1c7cSQu Wenruo 	while (ptr < end) {
1323f82d1c7cSQu Wenruo 		struct btrfs_extent_inline_ref *iref;
1324f82d1c7cSQu Wenruo 		struct btrfs_extent_data_ref *dref;
1325f82d1c7cSQu Wenruo 		struct btrfs_shared_data_ref *sref;
1326f82d1c7cSQu Wenruo 		u64 dref_offset;
1327f82d1c7cSQu Wenruo 		u64 inline_offset;
1328f82d1c7cSQu Wenruo 		u8 inline_type;
1329f82d1c7cSQu Wenruo 
1330c7c01a4aSDavid Sterba 		if (unlikely(ptr + sizeof(*iref) > end)) {
1331f82d1c7cSQu Wenruo 			extent_err(leaf, slot,
1332f82d1c7cSQu Wenruo "inline ref item overflows extent item, ptr %lu iref size %zu end %lu",
1333f82d1c7cSQu Wenruo 				   ptr, sizeof(*iref), end);
1334f82d1c7cSQu Wenruo 			return -EUCLEAN;
1335f82d1c7cSQu Wenruo 		}
1336f82d1c7cSQu Wenruo 		iref = (struct btrfs_extent_inline_ref *)ptr;
1337f82d1c7cSQu Wenruo 		inline_type = btrfs_extent_inline_ref_type(leaf, iref);
1338f82d1c7cSQu Wenruo 		inline_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1339c7c01a4aSDavid Sterba 		if (unlikely(ptr + btrfs_extent_inline_ref_size(inline_type) > end)) {
1340f82d1c7cSQu Wenruo 			extent_err(leaf, slot,
1341f82d1c7cSQu Wenruo "inline ref item overflows extent item, ptr %lu iref size %u end %lu",
1342f82d1c7cSQu Wenruo 				   ptr, inline_type, end);
1343f82d1c7cSQu Wenruo 			return -EUCLEAN;
1344f82d1c7cSQu Wenruo 		}
1345f82d1c7cSQu Wenruo 
1346f82d1c7cSQu Wenruo 		switch (inline_type) {
1347f82d1c7cSQu Wenruo 		/* inline_offset is subvolid of the owner, no need to check */
1348f82d1c7cSQu Wenruo 		case BTRFS_TREE_BLOCK_REF_KEY:
1349f82d1c7cSQu Wenruo 			inline_refs++;
1350f82d1c7cSQu Wenruo 			break;
1351f82d1c7cSQu Wenruo 		/* Contains parent bytenr */
1352f82d1c7cSQu Wenruo 		case BTRFS_SHARED_BLOCK_REF_KEY:
1353c7c01a4aSDavid Sterba 			if (unlikely(!IS_ALIGNED(inline_offset,
1354c7c01a4aSDavid Sterba 						 fs_info->sectorsize))) {
1355f82d1c7cSQu Wenruo 				extent_err(leaf, slot,
1356f82d1c7cSQu Wenruo 		"invalid tree parent bytenr, have %llu expect aligned to %u",
1357f82d1c7cSQu Wenruo 					   inline_offset, fs_info->sectorsize);
1358f82d1c7cSQu Wenruo 				return -EUCLEAN;
1359f82d1c7cSQu Wenruo 			}
1360f82d1c7cSQu Wenruo 			inline_refs++;
1361f82d1c7cSQu Wenruo 			break;
1362f82d1c7cSQu Wenruo 		/*
1363f82d1c7cSQu Wenruo 		 * Contains owner subvolid, owner key objectid, adjusted offset.
1364f82d1c7cSQu Wenruo 		 * The only obvious corruption can happen in that offset.
1365f82d1c7cSQu Wenruo 		 */
1366f82d1c7cSQu Wenruo 		case BTRFS_EXTENT_DATA_REF_KEY:
1367f82d1c7cSQu Wenruo 			dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1368f82d1c7cSQu Wenruo 			dref_offset = btrfs_extent_data_ref_offset(leaf, dref);
1369c7c01a4aSDavid Sterba 			if (unlikely(!IS_ALIGNED(dref_offset,
1370c7c01a4aSDavid Sterba 						 fs_info->sectorsize))) {
1371f82d1c7cSQu Wenruo 				extent_err(leaf, slot,
1372f82d1c7cSQu Wenruo 		"invalid data ref offset, have %llu expect aligned to %u",
1373f82d1c7cSQu Wenruo 					   dref_offset, fs_info->sectorsize);
1374f82d1c7cSQu Wenruo 				return -EUCLEAN;
1375f82d1c7cSQu Wenruo 			}
1376f82d1c7cSQu Wenruo 			inline_refs += btrfs_extent_data_ref_count(leaf, dref);
1377f82d1c7cSQu Wenruo 			break;
1378f82d1c7cSQu Wenruo 		/* Contains parent bytenr and ref count */
1379f82d1c7cSQu Wenruo 		case BTRFS_SHARED_DATA_REF_KEY:
1380f82d1c7cSQu Wenruo 			sref = (struct btrfs_shared_data_ref *)(iref + 1);
1381c7c01a4aSDavid Sterba 			if (unlikely(!IS_ALIGNED(inline_offset,
1382c7c01a4aSDavid Sterba 						 fs_info->sectorsize))) {
1383f82d1c7cSQu Wenruo 				extent_err(leaf, slot,
1384f82d1c7cSQu Wenruo 		"invalid data parent bytenr, have %llu expect aligned to %u",
1385f82d1c7cSQu Wenruo 					   inline_offset, fs_info->sectorsize);
1386f82d1c7cSQu Wenruo 				return -EUCLEAN;
1387f82d1c7cSQu Wenruo 			}
1388f82d1c7cSQu Wenruo 			inline_refs += btrfs_shared_data_ref_count(leaf, sref);
1389f82d1c7cSQu Wenruo 			break;
1390f82d1c7cSQu Wenruo 		default:
1391f82d1c7cSQu Wenruo 			extent_err(leaf, slot, "unknown inline ref type: %u",
1392f82d1c7cSQu Wenruo 				   inline_type);
1393f82d1c7cSQu Wenruo 			return -EUCLEAN;
1394f82d1c7cSQu Wenruo 		}
1395f82d1c7cSQu Wenruo 		ptr += btrfs_extent_inline_ref_size(inline_type);
1396f82d1c7cSQu Wenruo 	}
1397f82d1c7cSQu Wenruo 	/* No padding is allowed */
1398c7c01a4aSDavid Sterba 	if (unlikely(ptr != end)) {
1399f82d1c7cSQu Wenruo 		extent_err(leaf, slot,
1400f82d1c7cSQu Wenruo 			   "invalid extent item size, padding bytes found");
1401f82d1c7cSQu Wenruo 		return -EUCLEAN;
1402f82d1c7cSQu Wenruo 	}
1403f82d1c7cSQu Wenruo 
1404f82d1c7cSQu Wenruo 	/* Finally, check the inline refs against total refs */
1405c7c01a4aSDavid Sterba 	if (unlikely(inline_refs > total_refs)) {
1406f82d1c7cSQu Wenruo 		extent_err(leaf, slot,
1407f82d1c7cSQu Wenruo 			"invalid extent refs, have %llu expect >= inline %llu",
1408f82d1c7cSQu Wenruo 			   total_refs, inline_refs);
1409f82d1c7cSQu Wenruo 		return -EUCLEAN;
1410f82d1c7cSQu Wenruo 	}
1411f82d1c7cSQu Wenruo 	return 0;
1412f82d1c7cSQu Wenruo }
1413f82d1c7cSQu Wenruo 
1414e2406a6fSQu Wenruo static int check_simple_keyed_refs(struct extent_buffer *leaf,
1415e2406a6fSQu Wenruo 				   struct btrfs_key *key, int slot)
1416e2406a6fSQu Wenruo {
1417e2406a6fSQu Wenruo 	u32 expect_item_size = 0;
1418e2406a6fSQu Wenruo 
1419e2406a6fSQu Wenruo 	if (key->type == BTRFS_SHARED_DATA_REF_KEY)
1420e2406a6fSQu Wenruo 		expect_item_size = sizeof(struct btrfs_shared_data_ref);
1421e2406a6fSQu Wenruo 
1422c7c01a4aSDavid Sterba 	if (unlikely(btrfs_item_size_nr(leaf, slot) != expect_item_size)) {
1423e2406a6fSQu Wenruo 		generic_err(leaf, slot,
1424e2406a6fSQu Wenruo 		"invalid item size, have %u expect %u for key type %u",
1425e2406a6fSQu Wenruo 			    btrfs_item_size_nr(leaf, slot),
1426e2406a6fSQu Wenruo 			    expect_item_size, key->type);
1427e2406a6fSQu Wenruo 		return -EUCLEAN;
1428e2406a6fSQu Wenruo 	}
1429c7c01a4aSDavid Sterba 	if (unlikely(!IS_ALIGNED(key->objectid, leaf->fs_info->sectorsize))) {
1430e2406a6fSQu Wenruo 		generic_err(leaf, slot,
1431e2406a6fSQu Wenruo "invalid key objectid for shared block ref, have %llu expect aligned to %u",
1432e2406a6fSQu Wenruo 			    key->objectid, leaf->fs_info->sectorsize);
1433e2406a6fSQu Wenruo 		return -EUCLEAN;
1434e2406a6fSQu Wenruo 	}
1435c7c01a4aSDavid Sterba 	if (unlikely(key->type != BTRFS_TREE_BLOCK_REF_KEY &&
1436c7c01a4aSDavid Sterba 		     !IS_ALIGNED(key->offset, leaf->fs_info->sectorsize))) {
1437e2406a6fSQu Wenruo 		extent_err(leaf, slot,
1438e2406a6fSQu Wenruo 		"invalid tree parent bytenr, have %llu expect aligned to %u",
1439e2406a6fSQu Wenruo 			   key->offset, leaf->fs_info->sectorsize);
1440e2406a6fSQu Wenruo 		return -EUCLEAN;
1441e2406a6fSQu Wenruo 	}
1442e2406a6fSQu Wenruo 	return 0;
1443e2406a6fSQu Wenruo }
1444e2406a6fSQu Wenruo 
14450785a9aaSQu Wenruo static int check_extent_data_ref(struct extent_buffer *leaf,
14460785a9aaSQu Wenruo 				 struct btrfs_key *key, int slot)
14470785a9aaSQu Wenruo {
14480785a9aaSQu Wenruo 	struct btrfs_extent_data_ref *dref;
14490785a9aaSQu Wenruo 	unsigned long ptr = btrfs_item_ptr_offset(leaf, slot);
14500785a9aaSQu Wenruo 	const unsigned long end = ptr + btrfs_item_size_nr(leaf, slot);
14510785a9aaSQu Wenruo 
1452c7c01a4aSDavid Sterba 	if (unlikely(btrfs_item_size_nr(leaf, slot) % sizeof(*dref) != 0)) {
14530785a9aaSQu Wenruo 		generic_err(leaf, slot,
14540785a9aaSQu Wenruo 	"invalid item size, have %u expect aligned to %zu for key type %u",
14550785a9aaSQu Wenruo 			    btrfs_item_size_nr(leaf, slot),
14560785a9aaSQu Wenruo 			    sizeof(*dref), key->type);
14576d06b0adSDavid Sterba 		return -EUCLEAN;
14580785a9aaSQu Wenruo 	}
1459c7c01a4aSDavid Sterba 	if (unlikely(!IS_ALIGNED(key->objectid, leaf->fs_info->sectorsize))) {
14600785a9aaSQu Wenruo 		generic_err(leaf, slot,
14610785a9aaSQu Wenruo "invalid key objectid for shared block ref, have %llu expect aligned to %u",
14620785a9aaSQu Wenruo 			    key->objectid, leaf->fs_info->sectorsize);
14630785a9aaSQu Wenruo 		return -EUCLEAN;
14640785a9aaSQu Wenruo 	}
14650785a9aaSQu Wenruo 	for (; ptr < end; ptr += sizeof(*dref)) {
14660785a9aaSQu Wenruo 		u64 offset;
14670785a9aaSQu Wenruo 
14681119a72eSJosef Bacik 		/*
14691119a72eSJosef Bacik 		 * We cannot check the extent_data_ref hash due to possible
14701119a72eSJosef Bacik 		 * overflow from the leaf due to hash collisions.
14711119a72eSJosef Bacik 		 */
14720785a9aaSQu Wenruo 		dref = (struct btrfs_extent_data_ref *)ptr;
14730785a9aaSQu Wenruo 		offset = btrfs_extent_data_ref_offset(leaf, dref);
1474c7c01a4aSDavid Sterba 		if (unlikely(!IS_ALIGNED(offset, leaf->fs_info->sectorsize))) {
14750785a9aaSQu Wenruo 			extent_err(leaf, slot,
14760785a9aaSQu Wenruo 	"invalid extent data backref offset, have %llu expect aligned to %u",
14770785a9aaSQu Wenruo 				   offset, leaf->fs_info->sectorsize);
14786d06b0adSDavid Sterba 			return -EUCLEAN;
14790785a9aaSQu Wenruo 		}
14800785a9aaSQu Wenruo 	}
14810785a9aaSQu Wenruo 	return 0;
14820785a9aaSQu Wenruo }
14830785a9aaSQu Wenruo 
1484c3053ebbSQu Wenruo #define inode_ref_err(eb, slot, fmt, args...)			\
1485c3053ebbSQu Wenruo 	inode_item_err(eb, slot, fmt, ##args)
148671bf92a9SQu Wenruo static int check_inode_ref(struct extent_buffer *leaf,
148771bf92a9SQu Wenruo 			   struct btrfs_key *key, struct btrfs_key *prev_key,
148871bf92a9SQu Wenruo 			   int slot)
148971bf92a9SQu Wenruo {
149071bf92a9SQu Wenruo 	struct btrfs_inode_ref *iref;
149171bf92a9SQu Wenruo 	unsigned long ptr;
149271bf92a9SQu Wenruo 	unsigned long end;
149371bf92a9SQu Wenruo 
1494c7c01a4aSDavid Sterba 	if (unlikely(!check_prev_ino(leaf, key, slot, prev_key)))
149580d7fd1eSQu Wenruo 		return -EUCLEAN;
149671bf92a9SQu Wenruo 	/* namelen can't be 0, so item_size == sizeof() is also invalid */
1497c7c01a4aSDavid Sterba 	if (unlikely(btrfs_item_size_nr(leaf, slot) <= sizeof(*iref))) {
1498c3053ebbSQu Wenruo 		inode_ref_err(leaf, slot,
149971bf92a9SQu Wenruo 			"invalid item size, have %u expect (%zu, %u)",
150071bf92a9SQu Wenruo 			btrfs_item_size_nr(leaf, slot),
150171bf92a9SQu Wenruo 			sizeof(*iref), BTRFS_LEAF_DATA_SIZE(leaf->fs_info));
150271bf92a9SQu Wenruo 		return -EUCLEAN;
150371bf92a9SQu Wenruo 	}
150471bf92a9SQu Wenruo 
150571bf92a9SQu Wenruo 	ptr = btrfs_item_ptr_offset(leaf, slot);
150671bf92a9SQu Wenruo 	end = ptr + btrfs_item_size_nr(leaf, slot);
150771bf92a9SQu Wenruo 	while (ptr < end) {
150871bf92a9SQu Wenruo 		u16 namelen;
150971bf92a9SQu Wenruo 
1510c7c01a4aSDavid Sterba 		if (unlikely(ptr + sizeof(iref) > end)) {
1511c3053ebbSQu Wenruo 			inode_ref_err(leaf, slot,
151271bf92a9SQu Wenruo 			"inode ref overflow, ptr %lu end %lu inode_ref_size %zu",
151371bf92a9SQu Wenruo 				ptr, end, sizeof(iref));
151471bf92a9SQu Wenruo 			return -EUCLEAN;
151571bf92a9SQu Wenruo 		}
151671bf92a9SQu Wenruo 
151771bf92a9SQu Wenruo 		iref = (struct btrfs_inode_ref *)ptr;
151871bf92a9SQu Wenruo 		namelen = btrfs_inode_ref_name_len(leaf, iref);
1519c7c01a4aSDavid Sterba 		if (unlikely(ptr + sizeof(*iref) + namelen > end)) {
1520c3053ebbSQu Wenruo 			inode_ref_err(leaf, slot,
152171bf92a9SQu Wenruo 				"inode ref overflow, ptr %lu end %lu namelen %u",
152271bf92a9SQu Wenruo 				ptr, end, namelen);
152371bf92a9SQu Wenruo 			return -EUCLEAN;
152471bf92a9SQu Wenruo 		}
152571bf92a9SQu Wenruo 
152671bf92a9SQu Wenruo 		/*
152771bf92a9SQu Wenruo 		 * NOTE: In theory we should record all found index numbers
152871bf92a9SQu Wenruo 		 * to find any duplicated indexes, but that will be too time
152971bf92a9SQu Wenruo 		 * consuming for inodes with too many hard links.
153071bf92a9SQu Wenruo 		 */
153171bf92a9SQu Wenruo 		ptr += sizeof(*iref) + namelen;
153271bf92a9SQu Wenruo 	}
153371bf92a9SQu Wenruo 	return 0;
153471bf92a9SQu Wenruo }
153571bf92a9SQu Wenruo 
153682fc28fbSQu Wenruo /*
1537557ea5ddSQu Wenruo  * Common point to switch the item-specific validation.
1538557ea5ddSQu Wenruo  */
15390076bc89SDavid Sterba static int check_leaf_item(struct extent_buffer *leaf,
15404e9845efSFilipe Manana 			   struct btrfs_key *key, int slot,
15414e9845efSFilipe Manana 			   struct btrfs_key *prev_key)
1542557ea5ddSQu Wenruo {
1543557ea5ddSQu Wenruo 	int ret = 0;
1544075cb3c7SQu Wenruo 	struct btrfs_chunk *chunk;
1545557ea5ddSQu Wenruo 
1546557ea5ddSQu Wenruo 	switch (key->type) {
1547557ea5ddSQu Wenruo 	case BTRFS_EXTENT_DATA_KEY:
15484e9845efSFilipe Manana 		ret = check_extent_data_item(leaf, key, slot, prev_key);
1549557ea5ddSQu Wenruo 		break;
1550557ea5ddSQu Wenruo 	case BTRFS_EXTENT_CSUM_KEY:
1551ad1d8c43SFilipe Manana 		ret = check_csum_item(leaf, key, slot, prev_key);
1552557ea5ddSQu Wenruo 		break;
1553ad7b0368SQu Wenruo 	case BTRFS_DIR_ITEM_KEY:
1554ad7b0368SQu Wenruo 	case BTRFS_DIR_INDEX_KEY:
1555ad7b0368SQu Wenruo 	case BTRFS_XATTR_ITEM_KEY:
1556c18679ebSQu Wenruo 		ret = check_dir_item(leaf, key, prev_key, slot);
1557ad7b0368SQu Wenruo 		break;
155871bf92a9SQu Wenruo 	case BTRFS_INODE_REF_KEY:
155971bf92a9SQu Wenruo 		ret = check_inode_ref(leaf, key, prev_key, slot);
156071bf92a9SQu Wenruo 		break;
1561fce466eaSQu Wenruo 	case BTRFS_BLOCK_GROUP_ITEM_KEY:
1562af60ce2bSDavid Sterba 		ret = check_block_group_item(leaf, key, slot);
1563fce466eaSQu Wenruo 		break;
1564075cb3c7SQu Wenruo 	case BTRFS_CHUNK_ITEM_KEY:
1565075cb3c7SQu Wenruo 		chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
1566f6d2a5c2SQu Wenruo 		ret = check_leaf_chunk_item(leaf, chunk, key, slot);
1567075cb3c7SQu Wenruo 		break;
1568ab4ba2e1SQu Wenruo 	case BTRFS_DEV_ITEM_KEY:
1569412a2312SDavid Sterba 		ret = check_dev_item(leaf, key, slot);
1570ab4ba2e1SQu Wenruo 		break;
1571496245caSQu Wenruo 	case BTRFS_INODE_ITEM_KEY:
157239e57f49SDavid Sterba 		ret = check_inode_item(leaf, key, slot);
1573496245caSQu Wenruo 		break;
1574259ee775SQu Wenruo 	case BTRFS_ROOT_ITEM_KEY:
1575259ee775SQu Wenruo 		ret = check_root_item(leaf, key, slot);
1576259ee775SQu Wenruo 		break;
1577f82d1c7cSQu Wenruo 	case BTRFS_EXTENT_ITEM_KEY:
1578f82d1c7cSQu Wenruo 	case BTRFS_METADATA_ITEM_KEY:
1579f82d1c7cSQu Wenruo 		ret = check_extent_item(leaf, key, slot);
1580f82d1c7cSQu Wenruo 		break;
1581e2406a6fSQu Wenruo 	case BTRFS_TREE_BLOCK_REF_KEY:
1582e2406a6fSQu Wenruo 	case BTRFS_SHARED_DATA_REF_KEY:
1583e2406a6fSQu Wenruo 	case BTRFS_SHARED_BLOCK_REF_KEY:
1584e2406a6fSQu Wenruo 		ret = check_simple_keyed_refs(leaf, key, slot);
1585e2406a6fSQu Wenruo 		break;
15860785a9aaSQu Wenruo 	case BTRFS_EXTENT_DATA_REF_KEY:
15870785a9aaSQu Wenruo 		ret = check_extent_data_ref(leaf, key, slot);
15880785a9aaSQu Wenruo 		break;
1589557ea5ddSQu Wenruo 	}
1590557ea5ddSQu Wenruo 	return ret;
1591557ea5ddSQu Wenruo }
1592557ea5ddSQu Wenruo 
1593e2ccd361SDavid Sterba static int check_leaf(struct extent_buffer *leaf, bool check_item_data)
1594557ea5ddSQu Wenruo {
1595e2ccd361SDavid Sterba 	struct btrfs_fs_info *fs_info = leaf->fs_info;
1596557ea5ddSQu Wenruo 	/* No valid key type is 0, so all key should be larger than this key */
1597557ea5ddSQu Wenruo 	struct btrfs_key prev_key = {0, 0, 0};
1598557ea5ddSQu Wenruo 	struct btrfs_key key;
1599557ea5ddSQu Wenruo 	u32 nritems = btrfs_header_nritems(leaf);
1600557ea5ddSQu Wenruo 	int slot;
1601557ea5ddSQu Wenruo 
1602c7c01a4aSDavid Sterba 	if (unlikely(btrfs_header_level(leaf) != 0)) {
160386a6be3aSDavid Sterba 		generic_err(leaf, 0,
1604f556faa4SQu Wenruo 			"invalid level for leaf, have %d expect 0",
1605f556faa4SQu Wenruo 			btrfs_header_level(leaf));
1606f556faa4SQu Wenruo 		return -EUCLEAN;
1607f556faa4SQu Wenruo 	}
1608f556faa4SQu Wenruo 
1609557ea5ddSQu Wenruo 	/*
1610557ea5ddSQu Wenruo 	 * Extent buffers from a relocation tree have a owner field that
1611557ea5ddSQu Wenruo 	 * corresponds to the subvolume tree they are based on. So just from an
1612557ea5ddSQu Wenruo 	 * extent buffer alone we can not find out what is the id of the
1613557ea5ddSQu Wenruo 	 * corresponding subvolume tree, so we can not figure out if the extent
1614557ea5ddSQu Wenruo 	 * buffer corresponds to the root of the relocation tree or not. So
1615557ea5ddSQu Wenruo 	 * skip this check for relocation trees.
1616557ea5ddSQu Wenruo 	 */
1617557ea5ddSQu Wenruo 	if (nritems == 0 && !btrfs_header_flag(leaf, BTRFS_HEADER_FLAG_RELOC)) {
1618ba480dd4SQu Wenruo 		u64 owner = btrfs_header_owner(leaf);
1619557ea5ddSQu Wenruo 
1620ba480dd4SQu Wenruo 		/* These trees must never be empty */
1621c7c01a4aSDavid Sterba 		if (unlikely(owner == BTRFS_ROOT_TREE_OBJECTID ||
1622ba480dd4SQu Wenruo 			     owner == BTRFS_CHUNK_TREE_OBJECTID ||
1623ba480dd4SQu Wenruo 			     owner == BTRFS_EXTENT_TREE_OBJECTID ||
1624ba480dd4SQu Wenruo 			     owner == BTRFS_DEV_TREE_OBJECTID ||
1625ba480dd4SQu Wenruo 			     owner == BTRFS_FS_TREE_OBJECTID ||
1626c7c01a4aSDavid Sterba 			     owner == BTRFS_DATA_RELOC_TREE_OBJECTID)) {
162786a6be3aSDavid Sterba 			generic_err(leaf, 0,
1628ba480dd4SQu Wenruo 			"invalid root, root %llu must never be empty",
1629ba480dd4SQu Wenruo 				    owner);
1630ba480dd4SQu Wenruo 			return -EUCLEAN;
1631ba480dd4SQu Wenruo 		}
163262fdaa52SQu Wenruo 		/* Unknown tree */
1633c7c01a4aSDavid Sterba 		if (unlikely(owner == 0)) {
163462fdaa52SQu Wenruo 			generic_err(leaf, 0,
163562fdaa52SQu Wenruo 				"invalid owner, root 0 is not defined");
163662fdaa52SQu Wenruo 			return -EUCLEAN;
163762fdaa52SQu Wenruo 		}
1638557ea5ddSQu Wenruo 		return 0;
1639557ea5ddSQu Wenruo 	}
1640557ea5ddSQu Wenruo 
1641c7c01a4aSDavid Sterba 	if (unlikely(nritems == 0))
1642557ea5ddSQu Wenruo 		return 0;
1643557ea5ddSQu Wenruo 
1644557ea5ddSQu Wenruo 	/*
1645557ea5ddSQu Wenruo 	 * Check the following things to make sure this is a good leaf, and
1646557ea5ddSQu Wenruo 	 * leaf users won't need to bother with similar sanity checks:
1647557ea5ddSQu Wenruo 	 *
1648557ea5ddSQu Wenruo 	 * 1) key ordering
1649557ea5ddSQu Wenruo 	 * 2) item offset and size
1650557ea5ddSQu Wenruo 	 *    No overlap, no hole, all inside the leaf.
1651557ea5ddSQu Wenruo 	 * 3) item content
1652557ea5ddSQu Wenruo 	 *    If possible, do comprehensive sanity check.
1653557ea5ddSQu Wenruo 	 *    NOTE: All checks must only rely on the item data itself.
1654557ea5ddSQu Wenruo 	 */
1655557ea5ddSQu Wenruo 	for (slot = 0; slot < nritems; slot++) {
1656557ea5ddSQu Wenruo 		u32 item_end_expected;
1657557ea5ddSQu Wenruo 		int ret;
1658557ea5ddSQu Wenruo 
1659557ea5ddSQu Wenruo 		btrfs_item_key_to_cpu(leaf, &key, slot);
1660557ea5ddSQu Wenruo 
1661557ea5ddSQu Wenruo 		/* Make sure the keys are in the right order */
1662c7c01a4aSDavid Sterba 		if (unlikely(btrfs_comp_cpu_keys(&prev_key, &key) >= 0)) {
166386a6be3aSDavid Sterba 			generic_err(leaf, slot,
1664478d01b3SQu Wenruo 	"bad key order, prev (%llu %u %llu) current (%llu %u %llu)",
1665478d01b3SQu Wenruo 				prev_key.objectid, prev_key.type,
1666478d01b3SQu Wenruo 				prev_key.offset, key.objectid, key.type,
1667478d01b3SQu Wenruo 				key.offset);
1668557ea5ddSQu Wenruo 			return -EUCLEAN;
1669557ea5ddSQu Wenruo 		}
1670557ea5ddSQu Wenruo 
1671557ea5ddSQu Wenruo 		/*
1672557ea5ddSQu Wenruo 		 * Make sure the offset and ends are right, remember that the
1673557ea5ddSQu Wenruo 		 * item data starts at the end of the leaf and grows towards the
1674557ea5ddSQu Wenruo 		 * front.
1675557ea5ddSQu Wenruo 		 */
1676557ea5ddSQu Wenruo 		if (slot == 0)
1677557ea5ddSQu Wenruo 			item_end_expected = BTRFS_LEAF_DATA_SIZE(fs_info);
1678557ea5ddSQu Wenruo 		else
1679557ea5ddSQu Wenruo 			item_end_expected = btrfs_item_offset_nr(leaf,
1680557ea5ddSQu Wenruo 								 slot - 1);
1681c7c01a4aSDavid Sterba 		if (unlikely(btrfs_item_end_nr(leaf, slot) != item_end_expected)) {
168286a6be3aSDavid Sterba 			generic_err(leaf, slot,
1683478d01b3SQu Wenruo 				"unexpected item end, have %u expect %u",
1684478d01b3SQu Wenruo 				btrfs_item_end_nr(leaf, slot),
1685478d01b3SQu Wenruo 				item_end_expected);
1686557ea5ddSQu Wenruo 			return -EUCLEAN;
1687557ea5ddSQu Wenruo 		}
1688557ea5ddSQu Wenruo 
1689557ea5ddSQu Wenruo 		/*
1690557ea5ddSQu Wenruo 		 * Check to make sure that we don't point outside of the leaf,
1691557ea5ddSQu Wenruo 		 * just in case all the items are consistent to each other, but
1692557ea5ddSQu Wenruo 		 * all point outside of the leaf.
1693557ea5ddSQu Wenruo 		 */
1694c7c01a4aSDavid Sterba 		if (unlikely(btrfs_item_end_nr(leaf, slot) >
1695c7c01a4aSDavid Sterba 			     BTRFS_LEAF_DATA_SIZE(fs_info))) {
169686a6be3aSDavid Sterba 			generic_err(leaf, slot,
1697478d01b3SQu Wenruo 			"slot end outside of leaf, have %u expect range [0, %u]",
1698478d01b3SQu Wenruo 				btrfs_item_end_nr(leaf, slot),
1699478d01b3SQu Wenruo 				BTRFS_LEAF_DATA_SIZE(fs_info));
1700557ea5ddSQu Wenruo 			return -EUCLEAN;
1701557ea5ddSQu Wenruo 		}
1702557ea5ddSQu Wenruo 
1703557ea5ddSQu Wenruo 		/* Also check if the item pointer overlaps with btrfs item. */
1704c7c01a4aSDavid Sterba 		if (unlikely(btrfs_item_ptr_offset(leaf, slot) <
1705c7c01a4aSDavid Sterba 			     btrfs_item_nr_offset(slot) + sizeof(struct btrfs_item))) {
170686a6be3aSDavid Sterba 			generic_err(leaf, slot,
1707478d01b3SQu Wenruo 		"slot overlaps with its data, item end %lu data start %lu",
1708478d01b3SQu Wenruo 				btrfs_item_nr_offset(slot) +
1709478d01b3SQu Wenruo 				sizeof(struct btrfs_item),
1710478d01b3SQu Wenruo 				btrfs_item_ptr_offset(leaf, slot));
1711557ea5ddSQu Wenruo 			return -EUCLEAN;
1712557ea5ddSQu Wenruo 		}
1713557ea5ddSQu Wenruo 
171469fc6cbbSQu Wenruo 		if (check_item_data) {
171569fc6cbbSQu Wenruo 			/*
171669fc6cbbSQu Wenruo 			 * Check if the item size and content meet other
171769fc6cbbSQu Wenruo 			 * criteria
171869fc6cbbSQu Wenruo 			 */
17194e9845efSFilipe Manana 			ret = check_leaf_item(leaf, &key, slot, &prev_key);
1720c7c01a4aSDavid Sterba 			if (unlikely(ret < 0))
1721557ea5ddSQu Wenruo 				return ret;
172269fc6cbbSQu Wenruo 		}
1723557ea5ddSQu Wenruo 
1724557ea5ddSQu Wenruo 		prev_key.objectid = key.objectid;
1725557ea5ddSQu Wenruo 		prev_key.type = key.type;
1726557ea5ddSQu Wenruo 		prev_key.offset = key.offset;
1727557ea5ddSQu Wenruo 	}
1728557ea5ddSQu Wenruo 
1729557ea5ddSQu Wenruo 	return 0;
1730557ea5ddSQu Wenruo }
1731557ea5ddSQu Wenruo 
17321c4360eeSDavid Sterba int btrfs_check_leaf_full(struct extent_buffer *leaf)
173369fc6cbbSQu Wenruo {
1734e2ccd361SDavid Sterba 	return check_leaf(leaf, true);
173569fc6cbbSQu Wenruo }
173602529d7aSQu Wenruo ALLOW_ERROR_INJECTION(btrfs_check_leaf_full, ERRNO);
173769fc6cbbSQu Wenruo 
1738cfdaad5eSDavid Sterba int btrfs_check_leaf_relaxed(struct extent_buffer *leaf)
17392f659546SQu Wenruo {
1740e2ccd361SDavid Sterba 	return check_leaf(leaf, false);
17412f659546SQu Wenruo }
17422f659546SQu Wenruo 
1743813fd1dcSDavid Sterba int btrfs_check_node(struct extent_buffer *node)
1744557ea5ddSQu Wenruo {
1745813fd1dcSDavid Sterba 	struct btrfs_fs_info *fs_info = node->fs_info;
1746557ea5ddSQu Wenruo 	unsigned long nr = btrfs_header_nritems(node);
1747557ea5ddSQu Wenruo 	struct btrfs_key key, next_key;
1748557ea5ddSQu Wenruo 	int slot;
1749f556faa4SQu Wenruo 	int level = btrfs_header_level(node);
1750557ea5ddSQu Wenruo 	u64 bytenr;
1751557ea5ddSQu Wenruo 	int ret = 0;
1752557ea5ddSQu Wenruo 
1753c7c01a4aSDavid Sterba 	if (unlikely(level <= 0 || level >= BTRFS_MAX_LEVEL)) {
175486a6be3aSDavid Sterba 		generic_err(node, 0,
1755f556faa4SQu Wenruo 			"invalid level for node, have %d expect [1, %d]",
1756f556faa4SQu Wenruo 			level, BTRFS_MAX_LEVEL - 1);
1757f556faa4SQu Wenruo 		return -EUCLEAN;
1758f556faa4SQu Wenruo 	}
1759c7c01a4aSDavid Sterba 	if (unlikely(nr == 0 || nr > BTRFS_NODEPTRS_PER_BLOCK(fs_info))) {
17602f659546SQu Wenruo 		btrfs_crit(fs_info,
1761bba4f298SQu Wenruo "corrupt node: root=%llu block=%llu, nritems too %s, have %lu expect range [1,%u]",
17622f659546SQu Wenruo 			   btrfs_header_owner(node), node->start,
1763bba4f298SQu Wenruo 			   nr == 0 ? "small" : "large", nr,
17642f659546SQu Wenruo 			   BTRFS_NODEPTRS_PER_BLOCK(fs_info));
1765bba4f298SQu Wenruo 		return -EUCLEAN;
1766557ea5ddSQu Wenruo 	}
1767557ea5ddSQu Wenruo 
1768557ea5ddSQu Wenruo 	for (slot = 0; slot < nr - 1; slot++) {
1769557ea5ddSQu Wenruo 		bytenr = btrfs_node_blockptr(node, slot);
1770557ea5ddSQu Wenruo 		btrfs_node_key_to_cpu(node, &key, slot);
1771557ea5ddSQu Wenruo 		btrfs_node_key_to_cpu(node, &next_key, slot + 1);
1772557ea5ddSQu Wenruo 
1773c7c01a4aSDavid Sterba 		if (unlikely(!bytenr)) {
177486a6be3aSDavid Sterba 			generic_err(node, slot,
1775bba4f298SQu Wenruo 				"invalid NULL node pointer");
1776bba4f298SQu Wenruo 			ret = -EUCLEAN;
1777bba4f298SQu Wenruo 			goto out;
1778bba4f298SQu Wenruo 		}
1779c7c01a4aSDavid Sterba 		if (unlikely(!IS_ALIGNED(bytenr, fs_info->sectorsize))) {
178086a6be3aSDavid Sterba 			generic_err(node, slot,
1781bba4f298SQu Wenruo 			"unaligned pointer, have %llu should be aligned to %u",
17822f659546SQu Wenruo 				bytenr, fs_info->sectorsize);
1783bba4f298SQu Wenruo 			ret = -EUCLEAN;
1784557ea5ddSQu Wenruo 			goto out;
1785557ea5ddSQu Wenruo 		}
1786557ea5ddSQu Wenruo 
1787c7c01a4aSDavid Sterba 		if (unlikely(btrfs_comp_cpu_keys(&key, &next_key) >= 0)) {
178886a6be3aSDavid Sterba 			generic_err(node, slot,
1789bba4f298SQu Wenruo 	"bad key order, current (%llu %u %llu) next (%llu %u %llu)",
1790bba4f298SQu Wenruo 				key.objectid, key.type, key.offset,
1791bba4f298SQu Wenruo 				next_key.objectid, next_key.type,
1792bba4f298SQu Wenruo 				next_key.offset);
1793bba4f298SQu Wenruo 			ret = -EUCLEAN;
1794557ea5ddSQu Wenruo 			goto out;
1795557ea5ddSQu Wenruo 		}
1796557ea5ddSQu Wenruo 	}
1797557ea5ddSQu Wenruo out:
1798557ea5ddSQu Wenruo 	return ret;
1799557ea5ddSQu Wenruo }
180002529d7aSQu Wenruo ALLOW_ERROR_INJECTION(btrfs_check_node, ERRNO);
1801