xref: /openbmc/linux/fs/btrfs/tree-checker.c (revision 1465af12)
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 ({									      \
1038806d718SQu Wenruo 	if (!IS_ALIGNED(btrfs_file_extent_##name((leaf), (fi)), (alignment))) \
1041fd715ffSDavid Sterba 		file_extent_err((leaf), (slot),				      \
1058806d718SQu Wenruo 	"invalid %s for file extent, have %llu, should be aligned to %u",     \
1068806d718SQu Wenruo 			(#name), btrfs_file_extent_##name((leaf), (fi)),      \
1078806d718SQu Wenruo 			(alignment));					      \
1088806d718SQu Wenruo 	(!IS_ALIGNED(btrfs_file_extent_##name((leaf), (fi)), (alignment)));   \
1098806d718SQu Wenruo })
1108806d718SQu Wenruo 
1114e9845efSFilipe Manana static u64 file_extent_end(struct extent_buffer *leaf,
1124e9845efSFilipe Manana 			   struct btrfs_key *key,
1134e9845efSFilipe Manana 			   struct btrfs_file_extent_item *extent)
1144e9845efSFilipe Manana {
1154e9845efSFilipe Manana 	u64 end;
1164e9845efSFilipe Manana 	u64 len;
1174e9845efSFilipe Manana 
1184e9845efSFilipe Manana 	if (btrfs_file_extent_type(leaf, extent) == BTRFS_FILE_EXTENT_INLINE) {
1194e9845efSFilipe Manana 		len = btrfs_file_extent_ram_bytes(leaf, extent);
1204e9845efSFilipe Manana 		end = ALIGN(key->offset + len, leaf->fs_info->sectorsize);
1214e9845efSFilipe Manana 	} else {
1224e9845efSFilipe Manana 		len = btrfs_file_extent_num_bytes(leaf, extent);
1234e9845efSFilipe Manana 		end = key->offset + len;
1244e9845efSFilipe Manana 	}
1254e9845efSFilipe Manana 	return end;
1264e9845efSFilipe Manana }
1274e9845efSFilipe Manana 
12880d7fd1eSQu Wenruo /*
12980d7fd1eSQu Wenruo  * Customized report for dir_item, the only new important information is
13080d7fd1eSQu Wenruo  * key->objectid, which represents inode number
13180d7fd1eSQu Wenruo  */
13280d7fd1eSQu Wenruo __printf(3, 4)
13380d7fd1eSQu Wenruo __cold
13480d7fd1eSQu Wenruo static void dir_item_err(const struct extent_buffer *eb, int slot,
13580d7fd1eSQu Wenruo 			 const char *fmt, ...)
13680d7fd1eSQu Wenruo {
13780d7fd1eSQu Wenruo 	const struct btrfs_fs_info *fs_info = eb->fs_info;
13880d7fd1eSQu Wenruo 	struct btrfs_key key;
13980d7fd1eSQu Wenruo 	struct va_format vaf;
14080d7fd1eSQu Wenruo 	va_list args;
14180d7fd1eSQu Wenruo 
14280d7fd1eSQu Wenruo 	btrfs_item_key_to_cpu(eb, &key, slot);
14380d7fd1eSQu Wenruo 	va_start(args, fmt);
14480d7fd1eSQu Wenruo 
14580d7fd1eSQu Wenruo 	vaf.fmt = fmt;
14680d7fd1eSQu Wenruo 	vaf.va = &args;
14780d7fd1eSQu Wenruo 
14880d7fd1eSQu Wenruo 	btrfs_crit(fs_info,
14980d7fd1eSQu Wenruo 		"corrupt %s: root=%llu block=%llu slot=%d ino=%llu, %pV",
15080d7fd1eSQu Wenruo 		btrfs_header_level(eb) == 0 ? "leaf" : "node",
15180d7fd1eSQu Wenruo 		btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot,
15280d7fd1eSQu Wenruo 		key.objectid, &vaf);
15380d7fd1eSQu Wenruo 	va_end(args);
15480d7fd1eSQu Wenruo }
15580d7fd1eSQu Wenruo 
15680d7fd1eSQu Wenruo /*
15780d7fd1eSQu Wenruo  * This functions checks prev_key->objectid, to ensure current key and prev_key
15880d7fd1eSQu Wenruo  * share the same objectid as inode number.
15980d7fd1eSQu Wenruo  *
16080d7fd1eSQu Wenruo  * This is to detect missing INODE_ITEM in subvolume trees.
16180d7fd1eSQu Wenruo  *
16280d7fd1eSQu Wenruo  * Return true if everything is OK or we don't need to check.
16380d7fd1eSQu Wenruo  * Return false if anything is wrong.
16480d7fd1eSQu Wenruo  */
16580d7fd1eSQu Wenruo static bool check_prev_ino(struct extent_buffer *leaf,
16680d7fd1eSQu Wenruo 			   struct btrfs_key *key, int slot,
16780d7fd1eSQu Wenruo 			   struct btrfs_key *prev_key)
16880d7fd1eSQu Wenruo {
16980d7fd1eSQu Wenruo 	/* No prev key, skip check */
17080d7fd1eSQu Wenruo 	if (slot == 0)
17180d7fd1eSQu Wenruo 		return true;
17280d7fd1eSQu Wenruo 
17380d7fd1eSQu Wenruo 	/* Only these key->types needs to be checked */
17480d7fd1eSQu Wenruo 	ASSERT(key->type == BTRFS_XATTR_ITEM_KEY ||
17580d7fd1eSQu Wenruo 	       key->type == BTRFS_INODE_REF_KEY ||
17680d7fd1eSQu Wenruo 	       key->type == BTRFS_DIR_INDEX_KEY ||
17780d7fd1eSQu Wenruo 	       key->type == BTRFS_DIR_ITEM_KEY ||
17880d7fd1eSQu Wenruo 	       key->type == BTRFS_EXTENT_DATA_KEY);
17980d7fd1eSQu Wenruo 
18080d7fd1eSQu Wenruo 	/*
18180d7fd1eSQu Wenruo 	 * Only subvolume trees along with their reloc trees need this check.
18280d7fd1eSQu Wenruo 	 * Things like log tree doesn't follow this ino requirement.
18380d7fd1eSQu Wenruo 	 */
18480d7fd1eSQu Wenruo 	if (!is_fstree(btrfs_header_owner(leaf)))
18580d7fd1eSQu Wenruo 		return true;
18680d7fd1eSQu Wenruo 
18780d7fd1eSQu Wenruo 	if (key->objectid == prev_key->objectid)
18880d7fd1eSQu Wenruo 		return true;
18980d7fd1eSQu Wenruo 
19080d7fd1eSQu Wenruo 	/* Error found */
19180d7fd1eSQu Wenruo 	dir_item_err(leaf, slot,
19280d7fd1eSQu Wenruo 		"invalid previous key objectid, have %llu expect %llu",
19380d7fd1eSQu Wenruo 		prev_key->objectid, key->objectid);
19480d7fd1eSQu Wenruo 	return false;
19580d7fd1eSQu Wenruo }
196ae2a19d8SDavid Sterba static int check_extent_data_item(struct extent_buffer *leaf,
1974e9845efSFilipe Manana 				  struct btrfs_key *key, int slot,
1984e9845efSFilipe Manana 				  struct btrfs_key *prev_key)
199557ea5ddSQu Wenruo {
200ae2a19d8SDavid Sterba 	struct btrfs_fs_info *fs_info = leaf->fs_info;
201557ea5ddSQu Wenruo 	struct btrfs_file_extent_item *fi;
2022f659546SQu Wenruo 	u32 sectorsize = fs_info->sectorsize;
203557ea5ddSQu Wenruo 	u32 item_size = btrfs_item_size_nr(leaf, slot);
2044c094c33SQu Wenruo 	u64 extent_end;
205557ea5ddSQu Wenruo 
206557ea5ddSQu Wenruo 	if (!IS_ALIGNED(key->offset, sectorsize)) {
2071fd715ffSDavid Sterba 		file_extent_err(leaf, slot,
2088806d718SQu Wenruo "unaligned file_offset for file extent, have %llu should be aligned to %u",
2098806d718SQu Wenruo 			key->offset, sectorsize);
210557ea5ddSQu Wenruo 		return -EUCLEAN;
211557ea5ddSQu Wenruo 	}
212557ea5ddSQu Wenruo 
213c18679ebSQu Wenruo 	/*
214c18679ebSQu Wenruo 	 * Previous key must have the same key->objectid (ino).
215c18679ebSQu Wenruo 	 * It can be XATTR_ITEM, INODE_ITEM or just another EXTENT_DATA.
216c18679ebSQu Wenruo 	 * But if objectids mismatch, it means we have a missing
217c18679ebSQu Wenruo 	 * INODE_ITEM.
218c18679ebSQu Wenruo 	 */
21980d7fd1eSQu Wenruo 	if (!check_prev_ino(leaf, key, slot, prev_key))
220c18679ebSQu Wenruo 		return -EUCLEAN;
221c18679ebSQu Wenruo 
222557ea5ddSQu Wenruo 	fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
223557ea5ddSQu Wenruo 
224153a6d29SQu Wenruo 	/*
225153a6d29SQu Wenruo 	 * Make sure the item contains at least inline header, so the file
226153a6d29SQu Wenruo 	 * extent type is not some garbage.
227153a6d29SQu Wenruo 	 */
228153a6d29SQu Wenruo 	if (item_size < BTRFS_FILE_EXTENT_INLINE_DATA_START) {
229153a6d29SQu Wenruo 		file_extent_err(leaf, slot,
230994bf9cdSAndreas Färber 				"invalid item size, have %u expect [%zu, %u)",
231153a6d29SQu Wenruo 				item_size, BTRFS_FILE_EXTENT_INLINE_DATA_START,
232153a6d29SQu Wenruo 				SZ_4K);
233153a6d29SQu Wenruo 		return -EUCLEAN;
234153a6d29SQu Wenruo 	}
235b9b1a53eSChengguang Xu 	if (btrfs_file_extent_type(leaf, fi) >= BTRFS_NR_FILE_EXTENT_TYPES) {
2361fd715ffSDavid Sterba 		file_extent_err(leaf, slot,
2378806d718SQu Wenruo 		"invalid type for file extent, have %u expect range [0, %u]",
2388806d718SQu Wenruo 			btrfs_file_extent_type(leaf, fi),
239b9b1a53eSChengguang Xu 			BTRFS_NR_FILE_EXTENT_TYPES - 1);
240557ea5ddSQu Wenruo 		return -EUCLEAN;
241557ea5ddSQu Wenruo 	}
242557ea5ddSQu Wenruo 
243557ea5ddSQu Wenruo 	/*
24452042d8eSAndrea Gelmini 	 * Support for new compression/encryption must introduce incompat flag,
245557ea5ddSQu Wenruo 	 * and must be caught in open_ctree().
246557ea5ddSQu Wenruo 	 */
247ce96b7ffSChengguang Xu 	if (btrfs_file_extent_compression(leaf, fi) >= BTRFS_NR_COMPRESS_TYPES) {
2481fd715ffSDavid Sterba 		file_extent_err(leaf, slot,
2498806d718SQu Wenruo 	"invalid compression for file extent, have %u expect range [0, %u]",
2508806d718SQu Wenruo 			btrfs_file_extent_compression(leaf, fi),
251ce96b7ffSChengguang Xu 			BTRFS_NR_COMPRESS_TYPES - 1);
252557ea5ddSQu Wenruo 		return -EUCLEAN;
253557ea5ddSQu Wenruo 	}
254557ea5ddSQu Wenruo 	if (btrfs_file_extent_encryption(leaf, fi)) {
2551fd715ffSDavid Sterba 		file_extent_err(leaf, slot,
2568806d718SQu Wenruo 			"invalid encryption for file extent, have %u expect 0",
2578806d718SQu Wenruo 			btrfs_file_extent_encryption(leaf, fi));
258557ea5ddSQu Wenruo 		return -EUCLEAN;
259557ea5ddSQu Wenruo 	}
260557ea5ddSQu Wenruo 	if (btrfs_file_extent_type(leaf, fi) == BTRFS_FILE_EXTENT_INLINE) {
261557ea5ddSQu Wenruo 		/* Inline extent must have 0 as key offset */
262557ea5ddSQu Wenruo 		if (key->offset) {
2631fd715ffSDavid Sterba 			file_extent_err(leaf, slot,
2648806d718SQu Wenruo 		"invalid file_offset for inline file extent, have %llu expect 0",
2658806d718SQu Wenruo 				key->offset);
266557ea5ddSQu Wenruo 			return -EUCLEAN;
267557ea5ddSQu Wenruo 		}
268557ea5ddSQu Wenruo 
269557ea5ddSQu Wenruo 		/* Compressed inline extent has no on-disk size, skip it */
270557ea5ddSQu Wenruo 		if (btrfs_file_extent_compression(leaf, fi) !=
271557ea5ddSQu Wenruo 		    BTRFS_COMPRESS_NONE)
272557ea5ddSQu Wenruo 			return 0;
273557ea5ddSQu Wenruo 
274557ea5ddSQu Wenruo 		/* Uncompressed inline extent size must match item size */
275557ea5ddSQu Wenruo 		if (item_size != BTRFS_FILE_EXTENT_INLINE_DATA_START +
276557ea5ddSQu Wenruo 		    btrfs_file_extent_ram_bytes(leaf, fi)) {
2771fd715ffSDavid Sterba 			file_extent_err(leaf, slot,
2788806d718SQu Wenruo 	"invalid ram_bytes for uncompressed inline extent, have %u expect %llu",
2798806d718SQu Wenruo 				item_size, BTRFS_FILE_EXTENT_INLINE_DATA_START +
2808806d718SQu Wenruo 				btrfs_file_extent_ram_bytes(leaf, fi));
281557ea5ddSQu Wenruo 			return -EUCLEAN;
282557ea5ddSQu Wenruo 		}
283557ea5ddSQu Wenruo 		return 0;
284557ea5ddSQu Wenruo 	}
285557ea5ddSQu Wenruo 
286557ea5ddSQu Wenruo 	/* Regular or preallocated extent has fixed item size */
287557ea5ddSQu Wenruo 	if (item_size != sizeof(*fi)) {
2881fd715ffSDavid Sterba 		file_extent_err(leaf, slot,
289709a95c3SArnd Bergmann 	"invalid item size for reg/prealloc file extent, have %u expect %zu",
2908806d718SQu Wenruo 			item_size, sizeof(*fi));
291557ea5ddSQu Wenruo 		return -EUCLEAN;
292557ea5ddSQu Wenruo 	}
293033774dcSDavid Sterba 	if (CHECK_FE_ALIGNED(leaf, slot, fi, ram_bytes, sectorsize) ||
294033774dcSDavid Sterba 	    CHECK_FE_ALIGNED(leaf, slot, fi, disk_bytenr, sectorsize) ||
295033774dcSDavid Sterba 	    CHECK_FE_ALIGNED(leaf, slot, fi, disk_num_bytes, sectorsize) ||
296033774dcSDavid Sterba 	    CHECK_FE_ALIGNED(leaf, slot, fi, offset, sectorsize) ||
297033774dcSDavid Sterba 	    CHECK_FE_ALIGNED(leaf, slot, fi, num_bytes, sectorsize))
298557ea5ddSQu Wenruo 		return -EUCLEAN;
2994e9845efSFilipe Manana 
3004c094c33SQu Wenruo 	/* Catch extent end overflow */
3014c094c33SQu Wenruo 	if (check_add_overflow(btrfs_file_extent_num_bytes(leaf, fi),
3024c094c33SQu Wenruo 			       key->offset, &extent_end)) {
3034c094c33SQu Wenruo 		file_extent_err(leaf, slot,
3044c094c33SQu Wenruo 	"extent end overflow, have file offset %llu extent num bytes %llu",
3054c094c33SQu Wenruo 				key->offset,
3064c094c33SQu Wenruo 				btrfs_file_extent_num_bytes(leaf, fi));
3074c094c33SQu Wenruo 		return -EUCLEAN;
3084c094c33SQu Wenruo 	}
3094c094c33SQu Wenruo 
3104e9845efSFilipe Manana 	/*
3114e9845efSFilipe Manana 	 * Check that no two consecutive file extent items, in the same leaf,
3124e9845efSFilipe Manana 	 * present ranges that overlap each other.
3134e9845efSFilipe Manana 	 */
3144e9845efSFilipe Manana 	if (slot > 0 &&
3154e9845efSFilipe Manana 	    prev_key->objectid == key->objectid &&
3164e9845efSFilipe Manana 	    prev_key->type == BTRFS_EXTENT_DATA_KEY) {
3174e9845efSFilipe Manana 		struct btrfs_file_extent_item *prev_fi;
3184e9845efSFilipe Manana 		u64 prev_end;
3194e9845efSFilipe Manana 
3204e9845efSFilipe Manana 		prev_fi = btrfs_item_ptr(leaf, slot - 1,
3214e9845efSFilipe Manana 					 struct btrfs_file_extent_item);
3224e9845efSFilipe Manana 		prev_end = file_extent_end(leaf, prev_key, prev_fi);
3234e9845efSFilipe Manana 		if (prev_end > key->offset) {
3244e9845efSFilipe Manana 			file_extent_err(leaf, slot - 1,
3254e9845efSFilipe Manana "file extent end range (%llu) goes beyond start offset (%llu) of the next file extent",
3264e9845efSFilipe Manana 					prev_end, key->offset);
3274e9845efSFilipe Manana 			return -EUCLEAN;
3284e9845efSFilipe Manana 		}
3294e9845efSFilipe Manana 	}
3304e9845efSFilipe Manana 
331557ea5ddSQu Wenruo 	return 0;
332557ea5ddSQu Wenruo }
333557ea5ddSQu Wenruo 
33468128ce7SDavid Sterba static int check_csum_item(struct extent_buffer *leaf, struct btrfs_key *key,
335ad1d8c43SFilipe Manana 			   int slot, struct btrfs_key *prev_key)
336557ea5ddSQu Wenruo {
33768128ce7SDavid Sterba 	struct btrfs_fs_info *fs_info = leaf->fs_info;
3382f659546SQu Wenruo 	u32 sectorsize = fs_info->sectorsize;
3392f659546SQu Wenruo 	u32 csumsize = btrfs_super_csum_size(fs_info->super_copy);
340557ea5ddSQu Wenruo 
341557ea5ddSQu Wenruo 	if (key->objectid != BTRFS_EXTENT_CSUM_OBJECTID) {
34286a6be3aSDavid Sterba 		generic_err(leaf, slot,
343d508c5f0SQu Wenruo 		"invalid key objectid for csum item, have %llu expect %llu",
344d508c5f0SQu Wenruo 			key->objectid, BTRFS_EXTENT_CSUM_OBJECTID);
345557ea5ddSQu Wenruo 		return -EUCLEAN;
346557ea5ddSQu Wenruo 	}
347557ea5ddSQu Wenruo 	if (!IS_ALIGNED(key->offset, sectorsize)) {
34886a6be3aSDavid Sterba 		generic_err(leaf, slot,
349d508c5f0SQu Wenruo 	"unaligned key offset for csum item, have %llu should be aligned to %u",
350d508c5f0SQu Wenruo 			key->offset, sectorsize);
351557ea5ddSQu Wenruo 		return -EUCLEAN;
352557ea5ddSQu Wenruo 	}
353557ea5ddSQu Wenruo 	if (!IS_ALIGNED(btrfs_item_size_nr(leaf, slot), csumsize)) {
35486a6be3aSDavid Sterba 		generic_err(leaf, slot,
355d508c5f0SQu Wenruo 	"unaligned item size for csum item, have %u should be aligned to %u",
356d508c5f0SQu Wenruo 			btrfs_item_size_nr(leaf, slot), csumsize);
357557ea5ddSQu Wenruo 		return -EUCLEAN;
358557ea5ddSQu Wenruo 	}
359ad1d8c43SFilipe Manana 	if (slot > 0 && prev_key->type == BTRFS_EXTENT_CSUM_KEY) {
360ad1d8c43SFilipe Manana 		u64 prev_csum_end;
361ad1d8c43SFilipe Manana 		u32 prev_item_size;
362ad1d8c43SFilipe Manana 
363ad1d8c43SFilipe Manana 		prev_item_size = btrfs_item_size_nr(leaf, slot - 1);
364ad1d8c43SFilipe Manana 		prev_csum_end = (prev_item_size / csumsize) * sectorsize;
365ad1d8c43SFilipe Manana 		prev_csum_end += prev_key->offset;
366ad1d8c43SFilipe Manana 		if (prev_csum_end > key->offset) {
367ad1d8c43SFilipe Manana 			generic_err(leaf, slot - 1,
368ad1d8c43SFilipe Manana "csum end range (%llu) goes beyond the start range (%llu) of the next csum item",
369ad1d8c43SFilipe Manana 				    prev_csum_end, key->offset);
370ad1d8c43SFilipe Manana 			return -EUCLEAN;
371ad1d8c43SFilipe Manana 		}
372ad1d8c43SFilipe Manana 	}
373557ea5ddSQu Wenruo 	return 0;
374557ea5ddSQu Wenruo }
375557ea5ddSQu Wenruo 
376c23c77b0SQu Wenruo /* Inode item error output has the same format as dir_item_err() */
377c23c77b0SQu Wenruo #define inode_item_err(eb, slot, fmt, ...)			\
378c23c77b0SQu Wenruo 	dir_item_err(eb, slot, fmt, __VA_ARGS__)
379c23c77b0SQu Wenruo 
380c23c77b0SQu Wenruo static int check_inode_key(struct extent_buffer *leaf, struct btrfs_key *key,
381c23c77b0SQu Wenruo 			   int slot)
382c23c77b0SQu Wenruo {
383c23c77b0SQu Wenruo 	struct btrfs_key item_key;
384c23c77b0SQu Wenruo 	bool is_inode_item;
385c23c77b0SQu Wenruo 
386c23c77b0SQu Wenruo 	btrfs_item_key_to_cpu(leaf, &item_key, slot);
387c23c77b0SQu Wenruo 	is_inode_item = (item_key.type == BTRFS_INODE_ITEM_KEY);
388c23c77b0SQu Wenruo 
389c23c77b0SQu Wenruo 	/* For XATTR_ITEM, location key should be all 0 */
390c23c77b0SQu Wenruo 	if (item_key.type == BTRFS_XATTR_ITEM_KEY) {
391c23c77b0SQu Wenruo 		if (key->type != 0 || key->objectid != 0 || key->offset != 0)
392c23c77b0SQu Wenruo 			return -EUCLEAN;
393c23c77b0SQu Wenruo 		return 0;
394c23c77b0SQu Wenruo 	}
395c23c77b0SQu Wenruo 
396c23c77b0SQu Wenruo 	if ((key->objectid < BTRFS_FIRST_FREE_OBJECTID ||
397c23c77b0SQu Wenruo 	     key->objectid > BTRFS_LAST_FREE_OBJECTID) &&
398c23c77b0SQu Wenruo 	    key->objectid != BTRFS_ROOT_TREE_DIR_OBJECTID &&
399c23c77b0SQu Wenruo 	    key->objectid != BTRFS_FREE_INO_OBJECTID) {
400c23c77b0SQu Wenruo 		if (is_inode_item) {
401c23c77b0SQu Wenruo 			generic_err(leaf, slot,
402c23c77b0SQu Wenruo 	"invalid key objectid: has %llu expect %llu or [%llu, %llu] or %llu",
403c23c77b0SQu Wenruo 				key->objectid, BTRFS_ROOT_TREE_DIR_OBJECTID,
404c23c77b0SQu Wenruo 				BTRFS_FIRST_FREE_OBJECTID,
405c23c77b0SQu Wenruo 				BTRFS_LAST_FREE_OBJECTID,
406c23c77b0SQu Wenruo 				BTRFS_FREE_INO_OBJECTID);
407c23c77b0SQu Wenruo 		} else {
408c23c77b0SQu Wenruo 			dir_item_err(leaf, slot,
409c23c77b0SQu Wenruo "invalid location key objectid: has %llu expect %llu or [%llu, %llu] or %llu",
410c23c77b0SQu Wenruo 				key->objectid, BTRFS_ROOT_TREE_DIR_OBJECTID,
411c23c77b0SQu Wenruo 				BTRFS_FIRST_FREE_OBJECTID,
412c23c77b0SQu Wenruo 				BTRFS_LAST_FREE_OBJECTID,
413c23c77b0SQu Wenruo 				BTRFS_FREE_INO_OBJECTID);
414c23c77b0SQu Wenruo 		}
415c23c77b0SQu Wenruo 		return -EUCLEAN;
416c23c77b0SQu Wenruo 	}
417c23c77b0SQu Wenruo 	if (key->offset != 0) {
418c23c77b0SQu Wenruo 		if (is_inode_item)
419c23c77b0SQu Wenruo 			inode_item_err(leaf, slot,
420c23c77b0SQu Wenruo 				       "invalid key offset: has %llu expect 0",
421c23c77b0SQu Wenruo 				       key->offset);
422c23c77b0SQu Wenruo 		else
423c23c77b0SQu Wenruo 			dir_item_err(leaf, slot,
424c23c77b0SQu Wenruo 				"invalid location key offset:has %llu expect 0",
425c23c77b0SQu Wenruo 				key->offset);
426c23c77b0SQu Wenruo 		return -EUCLEAN;
427c23c77b0SQu Wenruo 	}
428c23c77b0SQu Wenruo 	return 0;
429c23c77b0SQu Wenruo }
430c23c77b0SQu Wenruo 
43157a0e674SQu Wenruo static int check_root_key(struct extent_buffer *leaf, struct btrfs_key *key,
43257a0e674SQu Wenruo 			  int slot)
43357a0e674SQu Wenruo {
43457a0e674SQu Wenruo 	struct btrfs_key item_key;
43557a0e674SQu Wenruo 	bool is_root_item;
43657a0e674SQu Wenruo 
43757a0e674SQu Wenruo 	btrfs_item_key_to_cpu(leaf, &item_key, slot);
43857a0e674SQu Wenruo 	is_root_item = (item_key.type == BTRFS_ROOT_ITEM_KEY);
43957a0e674SQu Wenruo 
44057a0e674SQu Wenruo 	/* No such tree id */
44157a0e674SQu Wenruo 	if (key->objectid == 0) {
44257a0e674SQu Wenruo 		if (is_root_item)
44357a0e674SQu Wenruo 			generic_err(leaf, slot, "invalid root id 0");
44457a0e674SQu Wenruo 		else
44557a0e674SQu Wenruo 			dir_item_err(leaf, slot,
44657a0e674SQu Wenruo 				     "invalid location key root id 0");
44757a0e674SQu Wenruo 		return -EUCLEAN;
44857a0e674SQu Wenruo 	}
44957a0e674SQu Wenruo 
45057a0e674SQu Wenruo 	/* DIR_ITEM/INDEX/INODE_REF is not allowed to point to non-fs trees */
45157a0e674SQu Wenruo 	if (!is_fstree(key->objectid) && !is_root_item) {
45257a0e674SQu Wenruo 		dir_item_err(leaf, slot,
45357a0e674SQu Wenruo 		"invalid location key objectid, have %llu expect [%llu, %llu]",
45457a0e674SQu Wenruo 				key->objectid, BTRFS_FIRST_FREE_OBJECTID,
45557a0e674SQu Wenruo 				BTRFS_LAST_FREE_OBJECTID);
45657a0e674SQu Wenruo 		return -EUCLEAN;
45757a0e674SQu Wenruo 	}
45857a0e674SQu Wenruo 
45957a0e674SQu Wenruo 	/*
46057a0e674SQu Wenruo 	 * ROOT_ITEM with non-zero offset means this is a snapshot, created at
46157a0e674SQu Wenruo 	 * @offset transid.
46257a0e674SQu Wenruo 	 * Furthermore, for location key in DIR_ITEM, its offset is always -1.
46357a0e674SQu Wenruo 	 *
46457a0e674SQu Wenruo 	 * So here we only check offset for reloc tree whose key->offset must
46557a0e674SQu Wenruo 	 * be a valid tree.
46657a0e674SQu Wenruo 	 */
46757a0e674SQu Wenruo 	if (key->objectid == BTRFS_TREE_RELOC_OBJECTID && key->offset == 0) {
46857a0e674SQu Wenruo 		generic_err(leaf, slot, "invalid root id 0 for reloc tree");
46957a0e674SQu Wenruo 		return -EUCLEAN;
47057a0e674SQu Wenruo 	}
47157a0e674SQu Wenruo 	return 0;
47257a0e674SQu Wenruo }
47357a0e674SQu Wenruo 
474ce4252c0SDavid Sterba static int check_dir_item(struct extent_buffer *leaf,
475c18679ebSQu Wenruo 			  struct btrfs_key *key, struct btrfs_key *prev_key,
476c18679ebSQu Wenruo 			  int slot)
477ad7b0368SQu Wenruo {
478ce4252c0SDavid Sterba 	struct btrfs_fs_info *fs_info = leaf->fs_info;
479ad7b0368SQu Wenruo 	struct btrfs_dir_item *di;
480ad7b0368SQu Wenruo 	u32 item_size = btrfs_item_size_nr(leaf, slot);
481ad7b0368SQu Wenruo 	u32 cur = 0;
482ad7b0368SQu Wenruo 
48380d7fd1eSQu Wenruo 	if (!check_prev_ino(leaf, key, slot, prev_key))
484c18679ebSQu Wenruo 		return -EUCLEAN;
485ad7b0368SQu Wenruo 	di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
486ad7b0368SQu Wenruo 	while (cur < item_size) {
487147a097cSQu Wenruo 		struct btrfs_key location_key;
488ad7b0368SQu Wenruo 		u32 name_len;
489ad7b0368SQu Wenruo 		u32 data_len;
490ad7b0368SQu Wenruo 		u32 max_name_len;
491ad7b0368SQu Wenruo 		u32 total_size;
492ad7b0368SQu Wenruo 		u32 name_hash;
493ad7b0368SQu Wenruo 		u8 dir_type;
494147a097cSQu Wenruo 		int ret;
495ad7b0368SQu Wenruo 
496ad7b0368SQu Wenruo 		/* header itself should not cross item boundary */
497ad7b0368SQu Wenruo 		if (cur + sizeof(*di) > item_size) {
498d98ced68SDavid Sterba 			dir_item_err(leaf, slot,
4997cfad652SArnd Bergmann 		"dir item header crosses item boundary, have %zu boundary %u",
500ad7b0368SQu Wenruo 				cur + sizeof(*di), item_size);
501ad7b0368SQu Wenruo 			return -EUCLEAN;
502ad7b0368SQu Wenruo 		}
503ad7b0368SQu Wenruo 
504147a097cSQu Wenruo 		/* Location key check */
505147a097cSQu Wenruo 		btrfs_dir_item_key_to_cpu(leaf, di, &location_key);
506147a097cSQu Wenruo 		if (location_key.type == BTRFS_ROOT_ITEM_KEY) {
507147a097cSQu Wenruo 			ret = check_root_key(leaf, &location_key, slot);
508147a097cSQu Wenruo 			if (ret < 0)
509147a097cSQu Wenruo 				return ret;
510147a097cSQu Wenruo 		} else if (location_key.type == BTRFS_INODE_ITEM_KEY ||
511147a097cSQu Wenruo 			   location_key.type == 0) {
512147a097cSQu Wenruo 			ret = check_inode_key(leaf, &location_key, slot);
513147a097cSQu Wenruo 			if (ret < 0)
514147a097cSQu Wenruo 				return ret;
515147a097cSQu Wenruo 		} else {
516147a097cSQu Wenruo 			dir_item_err(leaf, slot,
517147a097cSQu Wenruo 			"invalid location key type, have %u, expect %u or %u",
518147a097cSQu Wenruo 				     location_key.type, BTRFS_ROOT_ITEM_KEY,
519147a097cSQu Wenruo 				     BTRFS_INODE_ITEM_KEY);
520147a097cSQu Wenruo 			return -EUCLEAN;
521147a097cSQu Wenruo 		}
522147a097cSQu Wenruo 
523ad7b0368SQu Wenruo 		/* dir type check */
524ad7b0368SQu Wenruo 		dir_type = btrfs_dir_type(leaf, di);
525ad7b0368SQu Wenruo 		if (dir_type >= BTRFS_FT_MAX) {
526d98ced68SDavid Sterba 			dir_item_err(leaf, slot,
527ad7b0368SQu Wenruo 			"invalid dir item type, have %u expect [0, %u)",
528ad7b0368SQu Wenruo 				dir_type, BTRFS_FT_MAX);
529ad7b0368SQu Wenruo 			return -EUCLEAN;
530ad7b0368SQu Wenruo 		}
531ad7b0368SQu Wenruo 
532ad7b0368SQu Wenruo 		if (key->type == BTRFS_XATTR_ITEM_KEY &&
533ad7b0368SQu Wenruo 		    dir_type != BTRFS_FT_XATTR) {
534d98ced68SDavid Sterba 			dir_item_err(leaf, slot,
535ad7b0368SQu Wenruo 		"invalid dir item type for XATTR key, have %u expect %u",
536ad7b0368SQu Wenruo 				dir_type, BTRFS_FT_XATTR);
537ad7b0368SQu Wenruo 			return -EUCLEAN;
538ad7b0368SQu Wenruo 		}
539ad7b0368SQu Wenruo 		if (dir_type == BTRFS_FT_XATTR &&
540ad7b0368SQu Wenruo 		    key->type != BTRFS_XATTR_ITEM_KEY) {
541d98ced68SDavid Sterba 			dir_item_err(leaf, slot,
542ad7b0368SQu Wenruo 			"xattr dir type found for non-XATTR key");
543ad7b0368SQu Wenruo 			return -EUCLEAN;
544ad7b0368SQu Wenruo 		}
545ad7b0368SQu Wenruo 		if (dir_type == BTRFS_FT_XATTR)
546ad7b0368SQu Wenruo 			max_name_len = XATTR_NAME_MAX;
547ad7b0368SQu Wenruo 		else
548ad7b0368SQu Wenruo 			max_name_len = BTRFS_NAME_LEN;
549ad7b0368SQu Wenruo 
550ad7b0368SQu Wenruo 		/* Name/data length check */
551ad7b0368SQu Wenruo 		name_len = btrfs_dir_name_len(leaf, di);
552ad7b0368SQu Wenruo 		data_len = btrfs_dir_data_len(leaf, di);
553ad7b0368SQu Wenruo 		if (name_len > max_name_len) {
554d98ced68SDavid Sterba 			dir_item_err(leaf, slot,
555ad7b0368SQu Wenruo 			"dir item name len too long, have %u max %u",
556ad7b0368SQu Wenruo 				name_len, max_name_len);
557ad7b0368SQu Wenruo 			return -EUCLEAN;
558ad7b0368SQu Wenruo 		}
5592f659546SQu Wenruo 		if (name_len + data_len > BTRFS_MAX_XATTR_SIZE(fs_info)) {
560d98ced68SDavid Sterba 			dir_item_err(leaf, slot,
561ad7b0368SQu Wenruo 			"dir item name and data len too long, have %u max %u",
562ad7b0368SQu Wenruo 				name_len + data_len,
5632f659546SQu Wenruo 				BTRFS_MAX_XATTR_SIZE(fs_info));
564ad7b0368SQu Wenruo 			return -EUCLEAN;
565ad7b0368SQu Wenruo 		}
566ad7b0368SQu Wenruo 
567ad7b0368SQu Wenruo 		if (data_len && dir_type != BTRFS_FT_XATTR) {
568d98ced68SDavid Sterba 			dir_item_err(leaf, slot,
569ad7b0368SQu Wenruo 			"dir item with invalid data len, have %u expect 0",
570ad7b0368SQu Wenruo 				data_len);
571ad7b0368SQu Wenruo 			return -EUCLEAN;
572ad7b0368SQu Wenruo 		}
573ad7b0368SQu Wenruo 
574ad7b0368SQu Wenruo 		total_size = sizeof(*di) + name_len + data_len;
575ad7b0368SQu Wenruo 
576ad7b0368SQu Wenruo 		/* header and name/data should not cross item boundary */
577ad7b0368SQu Wenruo 		if (cur + total_size > item_size) {
578d98ced68SDavid Sterba 			dir_item_err(leaf, slot,
579ad7b0368SQu Wenruo 		"dir item data crosses item boundary, have %u boundary %u",
580ad7b0368SQu Wenruo 				cur + total_size, item_size);
581ad7b0368SQu Wenruo 			return -EUCLEAN;
582ad7b0368SQu Wenruo 		}
583ad7b0368SQu Wenruo 
584ad7b0368SQu Wenruo 		/*
585ad7b0368SQu Wenruo 		 * Special check for XATTR/DIR_ITEM, as key->offset is name
586ad7b0368SQu Wenruo 		 * hash, should match its name
587ad7b0368SQu Wenruo 		 */
588ad7b0368SQu Wenruo 		if (key->type == BTRFS_DIR_ITEM_KEY ||
589ad7b0368SQu Wenruo 		    key->type == BTRFS_XATTR_ITEM_KEY) {
590e2683fc9SDavid Sterba 			char namebuf[max(BTRFS_NAME_LEN, XATTR_NAME_MAX)];
591e2683fc9SDavid Sterba 
592ad7b0368SQu Wenruo 			read_extent_buffer(leaf, namebuf,
593ad7b0368SQu Wenruo 					(unsigned long)(di + 1), name_len);
594ad7b0368SQu Wenruo 			name_hash = btrfs_name_hash(namebuf, name_len);
595ad7b0368SQu Wenruo 			if (key->offset != name_hash) {
596d98ced68SDavid Sterba 				dir_item_err(leaf, slot,
597ad7b0368SQu Wenruo 		"name hash mismatch with key, have 0x%016x expect 0x%016llx",
598ad7b0368SQu Wenruo 					name_hash, key->offset);
599ad7b0368SQu Wenruo 				return -EUCLEAN;
600ad7b0368SQu Wenruo 			}
601ad7b0368SQu Wenruo 		}
602ad7b0368SQu Wenruo 		cur += total_size;
603ad7b0368SQu Wenruo 		di = (struct btrfs_dir_item *)((void *)di + total_size);
604ad7b0368SQu Wenruo 	}
605ad7b0368SQu Wenruo 	return 0;
606ad7b0368SQu Wenruo }
607ad7b0368SQu Wenruo 
6084806bd88SDavid Sterba __printf(3, 4)
609fce466eaSQu Wenruo __cold
6104806bd88SDavid Sterba static void block_group_err(const struct extent_buffer *eb, int slot,
611fce466eaSQu Wenruo 			    const char *fmt, ...)
612fce466eaSQu Wenruo {
6134806bd88SDavid Sterba 	const struct btrfs_fs_info *fs_info = eb->fs_info;
614fce466eaSQu Wenruo 	struct btrfs_key key;
615fce466eaSQu Wenruo 	struct va_format vaf;
616fce466eaSQu Wenruo 	va_list args;
617fce466eaSQu Wenruo 
618fce466eaSQu Wenruo 	btrfs_item_key_to_cpu(eb, &key, slot);
619fce466eaSQu Wenruo 	va_start(args, fmt);
620fce466eaSQu Wenruo 
621fce466eaSQu Wenruo 	vaf.fmt = fmt;
622fce466eaSQu Wenruo 	vaf.va = &args;
623fce466eaSQu Wenruo 
624fce466eaSQu Wenruo 	btrfs_crit(fs_info,
625fce466eaSQu Wenruo 	"corrupt %s: root=%llu block=%llu slot=%d bg_start=%llu bg_len=%llu, %pV",
626fce466eaSQu Wenruo 		btrfs_header_level(eb) == 0 ? "leaf" : "node",
627fce466eaSQu Wenruo 		btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot,
628fce466eaSQu Wenruo 		key.objectid, key.offset, &vaf);
629fce466eaSQu Wenruo 	va_end(args);
630fce466eaSQu Wenruo }
631fce466eaSQu Wenruo 
632af60ce2bSDavid Sterba static int check_block_group_item(struct extent_buffer *leaf,
633fce466eaSQu Wenruo 				  struct btrfs_key *key, int slot)
634fce466eaSQu Wenruo {
635fce466eaSQu Wenruo 	struct btrfs_block_group_item bgi;
636fce466eaSQu Wenruo 	u32 item_size = btrfs_item_size_nr(leaf, slot);
637fce466eaSQu Wenruo 	u64 flags;
638fce466eaSQu Wenruo 	u64 type;
639fce466eaSQu Wenruo 
640fce466eaSQu Wenruo 	/*
641fce466eaSQu Wenruo 	 * Here we don't really care about alignment since extent allocator can
64210950929SQu Wenruo 	 * handle it.  We care more about the size.
643fce466eaSQu Wenruo 	 */
64410950929SQu Wenruo 	if (key->offset == 0) {
6454806bd88SDavid Sterba 		block_group_err(leaf, slot,
64610950929SQu Wenruo 				"invalid block group size 0");
647fce466eaSQu Wenruo 		return -EUCLEAN;
648fce466eaSQu Wenruo 	}
649fce466eaSQu Wenruo 
650fce466eaSQu Wenruo 	if (item_size != sizeof(bgi)) {
6514806bd88SDavid Sterba 		block_group_err(leaf, slot,
652fce466eaSQu Wenruo 			"invalid item size, have %u expect %zu",
653fce466eaSQu Wenruo 				item_size, sizeof(bgi));
654fce466eaSQu Wenruo 		return -EUCLEAN;
655fce466eaSQu Wenruo 	}
656fce466eaSQu Wenruo 
657fce466eaSQu Wenruo 	read_extent_buffer(leaf, &bgi, btrfs_item_ptr_offset(leaf, slot),
658fce466eaSQu Wenruo 			   sizeof(bgi));
659de0dc456SDavid Sterba 	if (btrfs_stack_block_group_chunk_objectid(&bgi) !=
660fce466eaSQu Wenruo 	    BTRFS_FIRST_CHUNK_TREE_OBJECTID) {
6614806bd88SDavid Sterba 		block_group_err(leaf, slot,
662fce466eaSQu Wenruo 		"invalid block group chunk objectid, have %llu expect %llu",
663de0dc456SDavid Sterba 				btrfs_stack_block_group_chunk_objectid(&bgi),
664fce466eaSQu Wenruo 				BTRFS_FIRST_CHUNK_TREE_OBJECTID);
665fce466eaSQu Wenruo 		return -EUCLEAN;
666fce466eaSQu Wenruo 	}
667fce466eaSQu Wenruo 
668de0dc456SDavid Sterba 	if (btrfs_stack_block_group_used(&bgi) > key->offset) {
6694806bd88SDavid Sterba 		block_group_err(leaf, slot,
670fce466eaSQu Wenruo 			"invalid block group used, have %llu expect [0, %llu)",
671de0dc456SDavid Sterba 				btrfs_stack_block_group_used(&bgi), key->offset);
672fce466eaSQu Wenruo 		return -EUCLEAN;
673fce466eaSQu Wenruo 	}
674fce466eaSQu Wenruo 
675de0dc456SDavid Sterba 	flags = btrfs_stack_block_group_flags(&bgi);
676fce466eaSQu Wenruo 	if (hweight64(flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) > 1) {
6774806bd88SDavid Sterba 		block_group_err(leaf, slot,
678fce466eaSQu Wenruo "invalid profile flags, have 0x%llx (%lu bits set) expect no more than 1 bit set",
679fce466eaSQu Wenruo 			flags & BTRFS_BLOCK_GROUP_PROFILE_MASK,
680fce466eaSQu Wenruo 			hweight64(flags & BTRFS_BLOCK_GROUP_PROFILE_MASK));
681fce466eaSQu Wenruo 		return -EUCLEAN;
682fce466eaSQu Wenruo 	}
683fce466eaSQu Wenruo 
684fce466eaSQu Wenruo 	type = flags & BTRFS_BLOCK_GROUP_TYPE_MASK;
685fce466eaSQu Wenruo 	if (type != BTRFS_BLOCK_GROUP_DATA &&
686fce466eaSQu Wenruo 	    type != BTRFS_BLOCK_GROUP_METADATA &&
687fce466eaSQu Wenruo 	    type != BTRFS_BLOCK_GROUP_SYSTEM &&
688fce466eaSQu Wenruo 	    type != (BTRFS_BLOCK_GROUP_METADATA |
689fce466eaSQu Wenruo 			   BTRFS_BLOCK_GROUP_DATA)) {
6904806bd88SDavid Sterba 		block_group_err(leaf, slot,
691761333f2SShaokun Zhang "invalid type, have 0x%llx (%lu bits set) expect either 0x%llx, 0x%llx, 0x%llx or 0x%llx",
692fce466eaSQu Wenruo 			type, hweight64(type),
693fce466eaSQu Wenruo 			BTRFS_BLOCK_GROUP_DATA, BTRFS_BLOCK_GROUP_METADATA,
694fce466eaSQu Wenruo 			BTRFS_BLOCK_GROUP_SYSTEM,
695fce466eaSQu Wenruo 			BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA);
696fce466eaSQu Wenruo 		return -EUCLEAN;
697fce466eaSQu Wenruo 	}
698fce466eaSQu Wenruo 	return 0;
699fce466eaSQu Wenruo }
700fce466eaSQu Wenruo 
701d001e4a3SDavid Sterba __printf(4, 5)
702f1140243SQu Wenruo __cold
703d001e4a3SDavid Sterba static void chunk_err(const struct extent_buffer *leaf,
704f1140243SQu Wenruo 		      const struct btrfs_chunk *chunk, u64 logical,
705f1140243SQu Wenruo 		      const char *fmt, ...)
706f1140243SQu Wenruo {
707d001e4a3SDavid Sterba 	const struct btrfs_fs_info *fs_info = leaf->fs_info;
708f1140243SQu Wenruo 	bool is_sb;
709f1140243SQu Wenruo 	struct va_format vaf;
710f1140243SQu Wenruo 	va_list args;
711f1140243SQu Wenruo 	int i;
712f1140243SQu Wenruo 	int slot = -1;
713f1140243SQu Wenruo 
714f1140243SQu Wenruo 	/* Only superblock eb is able to have such small offset */
715f1140243SQu Wenruo 	is_sb = (leaf->start == BTRFS_SUPER_INFO_OFFSET);
716f1140243SQu Wenruo 
717f1140243SQu Wenruo 	if (!is_sb) {
718f1140243SQu Wenruo 		/*
719f1140243SQu Wenruo 		 * Get the slot number by iterating through all slots, this
720f1140243SQu Wenruo 		 * would provide better readability.
721f1140243SQu Wenruo 		 */
722f1140243SQu Wenruo 		for (i = 0; i < btrfs_header_nritems(leaf); i++) {
723f1140243SQu Wenruo 			if (btrfs_item_ptr_offset(leaf, i) ==
724f1140243SQu Wenruo 					(unsigned long)chunk) {
725f1140243SQu Wenruo 				slot = i;
726f1140243SQu Wenruo 				break;
727f1140243SQu Wenruo 			}
728f1140243SQu Wenruo 		}
729f1140243SQu Wenruo 	}
730f1140243SQu Wenruo 	va_start(args, fmt);
731f1140243SQu Wenruo 	vaf.fmt = fmt;
732f1140243SQu Wenruo 	vaf.va = &args;
733f1140243SQu Wenruo 
734f1140243SQu Wenruo 	if (is_sb)
735f1140243SQu Wenruo 		btrfs_crit(fs_info,
736f1140243SQu Wenruo 		"corrupt superblock syschunk array: chunk_start=%llu, %pV",
737f1140243SQu Wenruo 			   logical, &vaf);
738f1140243SQu Wenruo 	else
739f1140243SQu Wenruo 		btrfs_crit(fs_info,
740f1140243SQu Wenruo 	"corrupt leaf: root=%llu block=%llu slot=%d chunk_start=%llu, %pV",
741f1140243SQu Wenruo 			   BTRFS_CHUNK_TREE_OBJECTID, leaf->start, slot,
742f1140243SQu Wenruo 			   logical, &vaf);
743f1140243SQu Wenruo 	va_end(args);
744f1140243SQu Wenruo }
745f1140243SQu Wenruo 
746ad7b0368SQu Wenruo /*
74782fc28fbSQu Wenruo  * The common chunk check which could also work on super block sys chunk array.
74882fc28fbSQu Wenruo  *
749bf871c3bSQu Wenruo  * Return -EUCLEAN if anything is corrupted.
75082fc28fbSQu Wenruo  * Return 0 if everything is OK.
75182fc28fbSQu Wenruo  */
752ddaf1d5aSDavid Sterba int btrfs_check_chunk_valid(struct extent_buffer *leaf,
75382fc28fbSQu Wenruo 			    struct btrfs_chunk *chunk, u64 logical)
75482fc28fbSQu Wenruo {
755ddaf1d5aSDavid Sterba 	struct btrfs_fs_info *fs_info = leaf->fs_info;
75682fc28fbSQu Wenruo 	u64 length;
75782fc28fbSQu Wenruo 	u64 stripe_len;
75882fc28fbSQu Wenruo 	u16 num_stripes;
75982fc28fbSQu Wenruo 	u16 sub_stripes;
76082fc28fbSQu Wenruo 	u64 type;
76182fc28fbSQu Wenruo 	u64 features;
76282fc28fbSQu Wenruo 	bool mixed = false;
76382fc28fbSQu Wenruo 
76482fc28fbSQu Wenruo 	length = btrfs_chunk_length(leaf, chunk);
76582fc28fbSQu Wenruo 	stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
76682fc28fbSQu Wenruo 	num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
76782fc28fbSQu Wenruo 	sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
76882fc28fbSQu Wenruo 	type = btrfs_chunk_type(leaf, chunk);
76982fc28fbSQu Wenruo 
77082fc28fbSQu Wenruo 	if (!num_stripes) {
771d001e4a3SDavid Sterba 		chunk_err(leaf, chunk, logical,
772f1140243SQu Wenruo 			  "invalid chunk num_stripes, have %u", num_stripes);
773bf871c3bSQu Wenruo 		return -EUCLEAN;
77482fc28fbSQu Wenruo 	}
77582fc28fbSQu Wenruo 	if (!IS_ALIGNED(logical, fs_info->sectorsize)) {
776d001e4a3SDavid Sterba 		chunk_err(leaf, chunk, logical,
777f1140243SQu Wenruo 		"invalid chunk logical, have %llu should aligned to %u",
778f1140243SQu Wenruo 			  logical, fs_info->sectorsize);
779bf871c3bSQu Wenruo 		return -EUCLEAN;
78082fc28fbSQu Wenruo 	}
78182fc28fbSQu Wenruo 	if (btrfs_chunk_sector_size(leaf, chunk) != fs_info->sectorsize) {
782d001e4a3SDavid Sterba 		chunk_err(leaf, chunk, logical,
783f1140243SQu Wenruo 			  "invalid chunk sectorsize, have %u expect %u",
784f1140243SQu Wenruo 			  btrfs_chunk_sector_size(leaf, chunk),
785f1140243SQu Wenruo 			  fs_info->sectorsize);
786bf871c3bSQu Wenruo 		return -EUCLEAN;
78782fc28fbSQu Wenruo 	}
78882fc28fbSQu Wenruo 	if (!length || !IS_ALIGNED(length, fs_info->sectorsize)) {
789d001e4a3SDavid Sterba 		chunk_err(leaf, chunk, logical,
790f1140243SQu Wenruo 			  "invalid chunk length, have %llu", length);
791bf871c3bSQu Wenruo 		return -EUCLEAN;
79282fc28fbSQu Wenruo 	}
79382fc28fbSQu Wenruo 	if (!is_power_of_2(stripe_len) || stripe_len != BTRFS_STRIPE_LEN) {
794d001e4a3SDavid Sterba 		chunk_err(leaf, chunk, logical,
795f1140243SQu Wenruo 			  "invalid chunk stripe length: %llu",
79682fc28fbSQu Wenruo 			  stripe_len);
797bf871c3bSQu Wenruo 		return -EUCLEAN;
79882fc28fbSQu Wenruo 	}
79982fc28fbSQu Wenruo 	if (~(BTRFS_BLOCK_GROUP_TYPE_MASK | BTRFS_BLOCK_GROUP_PROFILE_MASK) &
80082fc28fbSQu Wenruo 	    type) {
801d001e4a3SDavid Sterba 		chunk_err(leaf, chunk, logical,
802f1140243SQu Wenruo 			  "unrecognized chunk type: 0x%llx",
80382fc28fbSQu Wenruo 			  ~(BTRFS_BLOCK_GROUP_TYPE_MASK |
80482fc28fbSQu Wenruo 			    BTRFS_BLOCK_GROUP_PROFILE_MASK) &
80582fc28fbSQu Wenruo 			  btrfs_chunk_type(leaf, chunk));
806bf871c3bSQu Wenruo 		return -EUCLEAN;
80782fc28fbSQu Wenruo 	}
80882fc28fbSQu Wenruo 
809c1499166SDavid Sterba 	if (!has_single_bit_set(type & BTRFS_BLOCK_GROUP_PROFILE_MASK) &&
81080e46cf2SQu Wenruo 	    (type & BTRFS_BLOCK_GROUP_PROFILE_MASK) != 0) {
811d001e4a3SDavid Sterba 		chunk_err(leaf, chunk, logical,
81280e46cf2SQu Wenruo 		"invalid chunk profile flag: 0x%llx, expect 0 or 1 bit set",
81380e46cf2SQu Wenruo 			  type & BTRFS_BLOCK_GROUP_PROFILE_MASK);
81480e46cf2SQu Wenruo 		return -EUCLEAN;
81580e46cf2SQu Wenruo 	}
81682fc28fbSQu Wenruo 	if ((type & BTRFS_BLOCK_GROUP_TYPE_MASK) == 0) {
817d001e4a3SDavid Sterba 		chunk_err(leaf, chunk, logical,
818f1140243SQu Wenruo 	"missing chunk type flag, have 0x%llx one bit must be set in 0x%llx",
819f1140243SQu Wenruo 			  type, BTRFS_BLOCK_GROUP_TYPE_MASK);
820bf871c3bSQu Wenruo 		return -EUCLEAN;
82182fc28fbSQu Wenruo 	}
82282fc28fbSQu Wenruo 
82382fc28fbSQu Wenruo 	if ((type & BTRFS_BLOCK_GROUP_SYSTEM) &&
82482fc28fbSQu Wenruo 	    (type & (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA))) {
825d001e4a3SDavid Sterba 		chunk_err(leaf, chunk, logical,
826f1140243SQu Wenruo 			  "system chunk with data or metadata type: 0x%llx",
827f1140243SQu Wenruo 			  type);
828bf871c3bSQu Wenruo 		return -EUCLEAN;
82982fc28fbSQu Wenruo 	}
83082fc28fbSQu Wenruo 
83182fc28fbSQu Wenruo 	features = btrfs_super_incompat_flags(fs_info->super_copy);
83282fc28fbSQu Wenruo 	if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
83382fc28fbSQu Wenruo 		mixed = true;
83482fc28fbSQu Wenruo 
83582fc28fbSQu Wenruo 	if (!mixed) {
83682fc28fbSQu Wenruo 		if ((type & BTRFS_BLOCK_GROUP_METADATA) &&
83782fc28fbSQu Wenruo 		    (type & BTRFS_BLOCK_GROUP_DATA)) {
838d001e4a3SDavid Sterba 			chunk_err(leaf, chunk, logical,
83982fc28fbSQu Wenruo 			"mixed chunk type in non-mixed mode: 0x%llx", type);
840bf871c3bSQu Wenruo 			return -EUCLEAN;
84182fc28fbSQu Wenruo 		}
84282fc28fbSQu Wenruo 	}
84382fc28fbSQu Wenruo 
84482fc28fbSQu Wenruo 	if ((type & BTRFS_BLOCK_GROUP_RAID10 && sub_stripes != 2) ||
84582fc28fbSQu Wenruo 	    (type & BTRFS_BLOCK_GROUP_RAID1 && num_stripes != 2) ||
84682fc28fbSQu Wenruo 	    (type & BTRFS_BLOCK_GROUP_RAID5 && num_stripes < 2) ||
84782fc28fbSQu Wenruo 	    (type & BTRFS_BLOCK_GROUP_RAID6 && num_stripes < 3) ||
84882fc28fbSQu Wenruo 	    (type & BTRFS_BLOCK_GROUP_DUP && num_stripes != 2) ||
84982fc28fbSQu Wenruo 	    ((type & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 && num_stripes != 1)) {
850d001e4a3SDavid Sterba 		chunk_err(leaf, chunk, logical,
85182fc28fbSQu Wenruo 			"invalid num_stripes:sub_stripes %u:%u for profile %llu",
85282fc28fbSQu Wenruo 			num_stripes, sub_stripes,
85382fc28fbSQu Wenruo 			type & BTRFS_BLOCK_GROUP_PROFILE_MASK);
854bf871c3bSQu Wenruo 		return -EUCLEAN;
85582fc28fbSQu Wenruo 	}
85682fc28fbSQu Wenruo 
85782fc28fbSQu Wenruo 	return 0;
85882fc28fbSQu Wenruo }
85982fc28fbSQu Wenruo 
860f6d2a5c2SQu Wenruo /*
861f6d2a5c2SQu Wenruo  * Enhanced version of chunk item checker.
862f6d2a5c2SQu Wenruo  *
863f6d2a5c2SQu Wenruo  * The common btrfs_check_chunk_valid() doesn't check item size since it needs
864f6d2a5c2SQu Wenruo  * to work on super block sys_chunk_array which doesn't have full item ptr.
865f6d2a5c2SQu Wenruo  */
866f6d2a5c2SQu Wenruo static int check_leaf_chunk_item(struct extent_buffer *leaf,
867f6d2a5c2SQu Wenruo 				 struct btrfs_chunk *chunk,
868f6d2a5c2SQu Wenruo 				 struct btrfs_key *key, int slot)
869f6d2a5c2SQu Wenruo {
870f6d2a5c2SQu Wenruo 	int num_stripes;
871f6d2a5c2SQu Wenruo 
872f6d2a5c2SQu Wenruo 	if (btrfs_item_size_nr(leaf, slot) < sizeof(struct btrfs_chunk)) {
873f6d2a5c2SQu Wenruo 		chunk_err(leaf, chunk, key->offset,
874f6d2a5c2SQu Wenruo 			"invalid chunk item size: have %u expect [%zu, %u)",
875f6d2a5c2SQu Wenruo 			btrfs_item_size_nr(leaf, slot),
876f6d2a5c2SQu Wenruo 			sizeof(struct btrfs_chunk),
877f6d2a5c2SQu Wenruo 			BTRFS_LEAF_DATA_SIZE(leaf->fs_info));
878f6d2a5c2SQu Wenruo 		return -EUCLEAN;
879f6d2a5c2SQu Wenruo 	}
880f6d2a5c2SQu Wenruo 
881f6d2a5c2SQu Wenruo 	num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
882f6d2a5c2SQu Wenruo 	/* Let btrfs_check_chunk_valid() handle this error type */
883f6d2a5c2SQu Wenruo 	if (num_stripes == 0)
884f6d2a5c2SQu Wenruo 		goto out;
885f6d2a5c2SQu Wenruo 
886f6d2a5c2SQu Wenruo 	if (btrfs_chunk_item_size(num_stripes) !=
887f6d2a5c2SQu Wenruo 	    btrfs_item_size_nr(leaf, slot)) {
888f6d2a5c2SQu Wenruo 		chunk_err(leaf, chunk, key->offset,
889f6d2a5c2SQu Wenruo 			"invalid chunk item size: have %u expect %lu",
890f6d2a5c2SQu Wenruo 			btrfs_item_size_nr(leaf, slot),
891f6d2a5c2SQu Wenruo 			btrfs_chunk_item_size(num_stripes));
892f6d2a5c2SQu Wenruo 		return -EUCLEAN;
893f6d2a5c2SQu Wenruo 	}
894f6d2a5c2SQu Wenruo out:
895f6d2a5c2SQu Wenruo 	return btrfs_check_chunk_valid(leaf, chunk, key->offset);
896f6d2a5c2SQu Wenruo }
897f6d2a5c2SQu Wenruo 
8985617ed80SDavid Sterba __printf(3, 4)
899ab4ba2e1SQu Wenruo __cold
9005617ed80SDavid Sterba static void dev_item_err(const struct extent_buffer *eb, int slot,
901ab4ba2e1SQu Wenruo 			 const char *fmt, ...)
902ab4ba2e1SQu Wenruo {
903ab4ba2e1SQu Wenruo 	struct btrfs_key key;
904ab4ba2e1SQu Wenruo 	struct va_format vaf;
905ab4ba2e1SQu Wenruo 	va_list args;
906ab4ba2e1SQu Wenruo 
907ab4ba2e1SQu Wenruo 	btrfs_item_key_to_cpu(eb, &key, slot);
908ab4ba2e1SQu Wenruo 	va_start(args, fmt);
909ab4ba2e1SQu Wenruo 
910ab4ba2e1SQu Wenruo 	vaf.fmt = fmt;
911ab4ba2e1SQu Wenruo 	vaf.va = &args;
912ab4ba2e1SQu Wenruo 
9135617ed80SDavid Sterba 	btrfs_crit(eb->fs_info,
914ab4ba2e1SQu Wenruo 	"corrupt %s: root=%llu block=%llu slot=%d devid=%llu %pV",
915ab4ba2e1SQu Wenruo 		btrfs_header_level(eb) == 0 ? "leaf" : "node",
916ab4ba2e1SQu Wenruo 		btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot,
917ab4ba2e1SQu Wenruo 		key.objectid, &vaf);
918ab4ba2e1SQu Wenruo 	va_end(args);
919ab4ba2e1SQu Wenruo }
920ab4ba2e1SQu Wenruo 
921412a2312SDavid Sterba static int check_dev_item(struct extent_buffer *leaf,
922ab4ba2e1SQu Wenruo 			  struct btrfs_key *key, int slot)
923ab4ba2e1SQu Wenruo {
924ab4ba2e1SQu Wenruo 	struct btrfs_dev_item *ditem;
925ab4ba2e1SQu Wenruo 
926ab4ba2e1SQu Wenruo 	if (key->objectid != BTRFS_DEV_ITEMS_OBJECTID) {
9275617ed80SDavid Sterba 		dev_item_err(leaf, slot,
928ab4ba2e1SQu Wenruo 			     "invalid objectid: has=%llu expect=%llu",
929ab4ba2e1SQu Wenruo 			     key->objectid, BTRFS_DEV_ITEMS_OBJECTID);
930ab4ba2e1SQu Wenruo 		return -EUCLEAN;
931ab4ba2e1SQu Wenruo 	}
932ab4ba2e1SQu Wenruo 	ditem = btrfs_item_ptr(leaf, slot, struct btrfs_dev_item);
933ab4ba2e1SQu Wenruo 	if (btrfs_device_id(leaf, ditem) != key->offset) {
9345617ed80SDavid Sterba 		dev_item_err(leaf, slot,
935ab4ba2e1SQu Wenruo 			     "devid mismatch: key has=%llu item has=%llu",
936ab4ba2e1SQu Wenruo 			     key->offset, btrfs_device_id(leaf, ditem));
937ab4ba2e1SQu Wenruo 		return -EUCLEAN;
938ab4ba2e1SQu Wenruo 	}
939ab4ba2e1SQu Wenruo 
940ab4ba2e1SQu Wenruo 	/*
941ab4ba2e1SQu Wenruo 	 * For device total_bytes, we don't have reliable way to check it, as
942ab4ba2e1SQu Wenruo 	 * it can be 0 for device removal. Device size check can only be done
943ab4ba2e1SQu Wenruo 	 * by dev extents check.
944ab4ba2e1SQu Wenruo 	 */
945ab4ba2e1SQu Wenruo 	if (btrfs_device_bytes_used(leaf, ditem) >
946ab4ba2e1SQu Wenruo 	    btrfs_device_total_bytes(leaf, ditem)) {
9475617ed80SDavid Sterba 		dev_item_err(leaf, slot,
948ab4ba2e1SQu Wenruo 			     "invalid bytes used: have %llu expect [0, %llu]",
949ab4ba2e1SQu Wenruo 			     btrfs_device_bytes_used(leaf, ditem),
950ab4ba2e1SQu Wenruo 			     btrfs_device_total_bytes(leaf, ditem));
951ab4ba2e1SQu Wenruo 		return -EUCLEAN;
952ab4ba2e1SQu Wenruo 	}
953ab4ba2e1SQu Wenruo 	/*
954ab4ba2e1SQu Wenruo 	 * Remaining members like io_align/type/gen/dev_group aren't really
955ab4ba2e1SQu Wenruo 	 * utilized.  Skip them to make later usage of them easier.
956ab4ba2e1SQu Wenruo 	 */
957ab4ba2e1SQu Wenruo 	return 0;
958ab4ba2e1SQu Wenruo }
959ab4ba2e1SQu Wenruo 
96039e57f49SDavid Sterba static int check_inode_item(struct extent_buffer *leaf,
961496245caSQu Wenruo 			    struct btrfs_key *key, int slot)
962496245caSQu Wenruo {
96339e57f49SDavid Sterba 	struct btrfs_fs_info *fs_info = leaf->fs_info;
964496245caSQu Wenruo 	struct btrfs_inode_item *iitem;
965496245caSQu Wenruo 	u64 super_gen = btrfs_super_generation(fs_info->super_copy);
966496245caSQu Wenruo 	u32 valid_mask = (S_IFMT | S_ISUID | S_ISGID | S_ISVTX | 0777);
967496245caSQu Wenruo 	u32 mode;
968c23c77b0SQu Wenruo 	int ret;
969496245caSQu Wenruo 
970c23c77b0SQu Wenruo 	ret = check_inode_key(leaf, key, slot);
971c23c77b0SQu Wenruo 	if (ret < 0)
972c23c77b0SQu Wenruo 		return ret;
973c23c77b0SQu Wenruo 
974496245caSQu Wenruo 	iitem = btrfs_item_ptr(leaf, slot, struct btrfs_inode_item);
975496245caSQu Wenruo 
976496245caSQu Wenruo 	/* Here we use super block generation + 1 to handle log tree */
977496245caSQu Wenruo 	if (btrfs_inode_generation(leaf, iitem) > super_gen + 1) {
978c3053ebbSQu Wenruo 		inode_item_err(leaf, slot,
979496245caSQu Wenruo 			"invalid inode generation: has %llu expect (0, %llu]",
980496245caSQu Wenruo 			       btrfs_inode_generation(leaf, iitem),
981496245caSQu Wenruo 			       super_gen + 1);
982496245caSQu Wenruo 		return -EUCLEAN;
983496245caSQu Wenruo 	}
984496245caSQu Wenruo 	/* Note for ROOT_TREE_DIR_ITEM, mkfs could set its transid 0 */
985496245caSQu Wenruo 	if (btrfs_inode_transid(leaf, iitem) > super_gen + 1) {
986c3053ebbSQu Wenruo 		inode_item_err(leaf, slot,
987f96d6960SQu Wenruo 			"invalid inode transid: has %llu expect [0, %llu]",
988496245caSQu Wenruo 			       btrfs_inode_transid(leaf, iitem), super_gen + 1);
989496245caSQu Wenruo 		return -EUCLEAN;
990496245caSQu Wenruo 	}
991496245caSQu Wenruo 
992496245caSQu Wenruo 	/*
993496245caSQu Wenruo 	 * For size and nbytes it's better not to be too strict, as for dir
994496245caSQu Wenruo 	 * item its size/nbytes can easily get wrong, but doesn't affect
995496245caSQu Wenruo 	 * anything in the fs. So here we skip the check.
996496245caSQu Wenruo 	 */
997496245caSQu Wenruo 	mode = btrfs_inode_mode(leaf, iitem);
998496245caSQu Wenruo 	if (mode & ~valid_mask) {
999c3053ebbSQu Wenruo 		inode_item_err(leaf, slot,
1000496245caSQu Wenruo 			       "unknown mode bit detected: 0x%x",
1001496245caSQu Wenruo 			       mode & ~valid_mask);
1002496245caSQu Wenruo 		return -EUCLEAN;
1003496245caSQu Wenruo 	}
1004496245caSQu Wenruo 
1005496245caSQu Wenruo 	/*
1006c1499166SDavid Sterba 	 * S_IFMT is not bit mapped so we can't completely rely on
1007c1499166SDavid Sterba 	 * is_power_of_2/has_single_bit_set, but it can save us from checking
1008c1499166SDavid Sterba 	 * FIFO/CHR/DIR/REG.  Only needs to check BLK, LNK and SOCKS
1009496245caSQu Wenruo 	 */
1010c1499166SDavid Sterba 	if (!has_single_bit_set(mode & S_IFMT)) {
1011496245caSQu Wenruo 		if (!S_ISLNK(mode) && !S_ISBLK(mode) && !S_ISSOCK(mode)) {
1012c3053ebbSQu Wenruo 			inode_item_err(leaf, slot,
1013496245caSQu Wenruo 			"invalid mode: has 0%o expect valid S_IF* bit(s)",
1014496245caSQu Wenruo 				       mode & S_IFMT);
1015496245caSQu Wenruo 			return -EUCLEAN;
1016496245caSQu Wenruo 		}
1017496245caSQu Wenruo 	}
1018496245caSQu Wenruo 	if (S_ISDIR(mode) && btrfs_inode_nlink(leaf, iitem) > 1) {
1019c3053ebbSQu Wenruo 		inode_item_err(leaf, slot,
1020496245caSQu Wenruo 		       "invalid nlink: has %u expect no more than 1 for dir",
1021496245caSQu Wenruo 			btrfs_inode_nlink(leaf, iitem));
1022496245caSQu Wenruo 		return -EUCLEAN;
1023496245caSQu Wenruo 	}
1024496245caSQu Wenruo 	if (btrfs_inode_flags(leaf, iitem) & ~BTRFS_INODE_FLAG_MASK) {
1025c3053ebbSQu Wenruo 		inode_item_err(leaf, slot,
1026496245caSQu Wenruo 			       "unknown flags detected: 0x%llx",
1027496245caSQu Wenruo 			       btrfs_inode_flags(leaf, iitem) &
1028496245caSQu Wenruo 			       ~BTRFS_INODE_FLAG_MASK);
1029496245caSQu Wenruo 		return -EUCLEAN;
1030496245caSQu Wenruo 	}
1031496245caSQu Wenruo 	return 0;
1032496245caSQu Wenruo }
1033496245caSQu Wenruo 
1034259ee775SQu Wenruo static int check_root_item(struct extent_buffer *leaf, struct btrfs_key *key,
1035259ee775SQu Wenruo 			   int slot)
1036259ee775SQu Wenruo {
1037259ee775SQu Wenruo 	struct btrfs_fs_info *fs_info = leaf->fs_info;
10381465af12SQu Wenruo 	struct btrfs_root_item ri = { 0 };
1039259ee775SQu Wenruo 	const u64 valid_root_flags = BTRFS_ROOT_SUBVOL_RDONLY |
1040259ee775SQu Wenruo 				     BTRFS_ROOT_SUBVOL_DEAD;
104157a0e674SQu Wenruo 	int ret;
1042259ee775SQu Wenruo 
104357a0e674SQu Wenruo 	ret = check_root_key(leaf, key, slot);
104457a0e674SQu Wenruo 	if (ret < 0)
104557a0e674SQu Wenruo 		return ret;
1046259ee775SQu Wenruo 
10471465af12SQu Wenruo 	if (btrfs_item_size_nr(leaf, slot) != sizeof(ri) &&
10481465af12SQu Wenruo 	    btrfs_item_size_nr(leaf, slot) != btrfs_legacy_root_item_size()) {
1049259ee775SQu Wenruo 		generic_err(leaf, slot,
10501465af12SQu Wenruo 			    "invalid root item size, have %u expect %zu or %u",
10511465af12SQu Wenruo 			    btrfs_item_size_nr(leaf, slot), sizeof(ri),
10521465af12SQu Wenruo 			    btrfs_legacy_root_item_size());
1053259ee775SQu Wenruo 	}
1054259ee775SQu Wenruo 
10551465af12SQu Wenruo 	/*
10561465af12SQu Wenruo 	 * For legacy root item, the members starting at generation_v2 will be
10571465af12SQu Wenruo 	 * all filled with 0.
10581465af12SQu Wenruo 	 * And since we allow geneartion_v2 as 0, it will still pass the check.
10591465af12SQu Wenruo 	 */
1060259ee775SQu Wenruo 	read_extent_buffer(leaf, &ri, btrfs_item_ptr_offset(leaf, slot),
10611465af12SQu Wenruo 			   btrfs_item_size_nr(leaf, slot));
1062259ee775SQu Wenruo 
1063259ee775SQu Wenruo 	/* Generation related */
1064259ee775SQu Wenruo 	if (btrfs_root_generation(&ri) >
1065259ee775SQu Wenruo 	    btrfs_super_generation(fs_info->super_copy) + 1) {
1066259ee775SQu Wenruo 		generic_err(leaf, slot,
1067259ee775SQu Wenruo 			"invalid root generation, have %llu expect (0, %llu]",
1068259ee775SQu Wenruo 			    btrfs_root_generation(&ri),
1069259ee775SQu Wenruo 			    btrfs_super_generation(fs_info->super_copy) + 1);
1070259ee775SQu Wenruo 		return -EUCLEAN;
1071259ee775SQu Wenruo 	}
1072259ee775SQu Wenruo 	if (btrfs_root_generation_v2(&ri) >
1073259ee775SQu Wenruo 	    btrfs_super_generation(fs_info->super_copy) + 1) {
1074259ee775SQu Wenruo 		generic_err(leaf, slot,
1075259ee775SQu Wenruo 		"invalid root v2 generation, have %llu expect (0, %llu]",
1076259ee775SQu Wenruo 			    btrfs_root_generation_v2(&ri),
1077259ee775SQu Wenruo 			    btrfs_super_generation(fs_info->super_copy) + 1);
1078259ee775SQu Wenruo 		return -EUCLEAN;
1079259ee775SQu Wenruo 	}
1080259ee775SQu Wenruo 	if (btrfs_root_last_snapshot(&ri) >
1081259ee775SQu Wenruo 	    btrfs_super_generation(fs_info->super_copy) + 1) {
1082259ee775SQu Wenruo 		generic_err(leaf, slot,
1083259ee775SQu Wenruo 		"invalid root last_snapshot, have %llu expect (0, %llu]",
1084259ee775SQu Wenruo 			    btrfs_root_last_snapshot(&ri),
1085259ee775SQu Wenruo 			    btrfs_super_generation(fs_info->super_copy) + 1);
1086259ee775SQu Wenruo 		return -EUCLEAN;
1087259ee775SQu Wenruo 	}
1088259ee775SQu Wenruo 
1089259ee775SQu Wenruo 	/* Alignment and level check */
1090259ee775SQu Wenruo 	if (!IS_ALIGNED(btrfs_root_bytenr(&ri), fs_info->sectorsize)) {
1091259ee775SQu Wenruo 		generic_err(leaf, slot,
1092259ee775SQu Wenruo 		"invalid root bytenr, have %llu expect to be aligned to %u",
1093259ee775SQu Wenruo 			    btrfs_root_bytenr(&ri), fs_info->sectorsize);
1094259ee775SQu Wenruo 		return -EUCLEAN;
1095259ee775SQu Wenruo 	}
1096259ee775SQu Wenruo 	if (btrfs_root_level(&ri) >= BTRFS_MAX_LEVEL) {
1097259ee775SQu Wenruo 		generic_err(leaf, slot,
1098259ee775SQu Wenruo 			    "invalid root level, have %u expect [0, %u]",
1099259ee775SQu Wenruo 			    btrfs_root_level(&ri), BTRFS_MAX_LEVEL - 1);
1100259ee775SQu Wenruo 		return -EUCLEAN;
1101259ee775SQu Wenruo 	}
1102259ee775SQu Wenruo 	if (ri.drop_level >= BTRFS_MAX_LEVEL) {
1103259ee775SQu Wenruo 		generic_err(leaf, slot,
1104259ee775SQu Wenruo 			    "invalid root level, have %u expect [0, %u]",
1105259ee775SQu Wenruo 			    ri.drop_level, BTRFS_MAX_LEVEL - 1);
1106259ee775SQu Wenruo 		return -EUCLEAN;
1107259ee775SQu Wenruo 	}
1108259ee775SQu Wenruo 
1109259ee775SQu Wenruo 	/* Flags check */
1110259ee775SQu Wenruo 	if (btrfs_root_flags(&ri) & ~valid_root_flags) {
1111259ee775SQu Wenruo 		generic_err(leaf, slot,
1112259ee775SQu Wenruo 			    "invalid root flags, have 0x%llx expect mask 0x%llx",
1113259ee775SQu Wenruo 			    btrfs_root_flags(&ri), valid_root_flags);
1114259ee775SQu Wenruo 		return -EUCLEAN;
1115259ee775SQu Wenruo 	}
1116259ee775SQu Wenruo 	return 0;
1117259ee775SQu Wenruo }
1118259ee775SQu Wenruo 
1119f82d1c7cSQu Wenruo __printf(3,4)
1120f82d1c7cSQu Wenruo __cold
1121f82d1c7cSQu Wenruo static void extent_err(const struct extent_buffer *eb, int slot,
1122f82d1c7cSQu Wenruo 		       const char *fmt, ...)
1123f82d1c7cSQu Wenruo {
1124f82d1c7cSQu Wenruo 	struct btrfs_key key;
1125f82d1c7cSQu Wenruo 	struct va_format vaf;
1126f82d1c7cSQu Wenruo 	va_list args;
1127f82d1c7cSQu Wenruo 	u64 bytenr;
1128f82d1c7cSQu Wenruo 	u64 len;
1129f82d1c7cSQu Wenruo 
1130f82d1c7cSQu Wenruo 	btrfs_item_key_to_cpu(eb, &key, slot);
1131f82d1c7cSQu Wenruo 	bytenr = key.objectid;
1132e2406a6fSQu Wenruo 	if (key.type == BTRFS_METADATA_ITEM_KEY ||
1133e2406a6fSQu Wenruo 	    key.type == BTRFS_TREE_BLOCK_REF_KEY ||
1134e2406a6fSQu Wenruo 	    key.type == BTRFS_SHARED_BLOCK_REF_KEY)
1135f82d1c7cSQu Wenruo 		len = eb->fs_info->nodesize;
1136f82d1c7cSQu Wenruo 	else
1137f82d1c7cSQu Wenruo 		len = key.offset;
1138f82d1c7cSQu Wenruo 	va_start(args, fmt);
1139f82d1c7cSQu Wenruo 
1140f82d1c7cSQu Wenruo 	vaf.fmt = fmt;
1141f82d1c7cSQu Wenruo 	vaf.va = &args;
1142f82d1c7cSQu Wenruo 
1143f82d1c7cSQu Wenruo 	btrfs_crit(eb->fs_info,
1144f82d1c7cSQu Wenruo 	"corrupt %s: block=%llu slot=%d extent bytenr=%llu len=%llu %pV",
1145f82d1c7cSQu Wenruo 		btrfs_header_level(eb) == 0 ? "leaf" : "node",
1146f82d1c7cSQu Wenruo 		eb->start, slot, bytenr, len, &vaf);
1147f82d1c7cSQu Wenruo 	va_end(args);
1148f82d1c7cSQu Wenruo }
1149f82d1c7cSQu Wenruo 
1150f82d1c7cSQu Wenruo static int check_extent_item(struct extent_buffer *leaf,
1151f82d1c7cSQu Wenruo 			     struct btrfs_key *key, int slot)
1152f82d1c7cSQu Wenruo {
1153f82d1c7cSQu Wenruo 	struct btrfs_fs_info *fs_info = leaf->fs_info;
1154f82d1c7cSQu Wenruo 	struct btrfs_extent_item *ei;
1155f82d1c7cSQu Wenruo 	bool is_tree_block = false;
1156f82d1c7cSQu Wenruo 	unsigned long ptr;	/* Current pointer inside inline refs */
1157f82d1c7cSQu Wenruo 	unsigned long end;	/* Extent item end */
1158f82d1c7cSQu Wenruo 	const u32 item_size = btrfs_item_size_nr(leaf, slot);
1159f82d1c7cSQu Wenruo 	u64 flags;
1160f82d1c7cSQu Wenruo 	u64 generation;
1161f82d1c7cSQu Wenruo 	u64 total_refs;		/* Total refs in btrfs_extent_item */
1162f82d1c7cSQu Wenruo 	u64 inline_refs = 0;	/* found total inline refs */
1163f82d1c7cSQu Wenruo 
1164f82d1c7cSQu Wenruo 	if (key->type == BTRFS_METADATA_ITEM_KEY &&
1165f82d1c7cSQu Wenruo 	    !btrfs_fs_incompat(fs_info, SKINNY_METADATA)) {
1166f82d1c7cSQu Wenruo 		generic_err(leaf, slot,
1167f82d1c7cSQu Wenruo "invalid key type, METADATA_ITEM type invalid when SKINNY_METADATA feature disabled");
1168f82d1c7cSQu Wenruo 		return -EUCLEAN;
1169f82d1c7cSQu Wenruo 	}
1170f82d1c7cSQu Wenruo 	/* key->objectid is the bytenr for both key types */
1171f82d1c7cSQu Wenruo 	if (!IS_ALIGNED(key->objectid, fs_info->sectorsize)) {
1172f82d1c7cSQu Wenruo 		generic_err(leaf, slot,
1173f82d1c7cSQu Wenruo 		"invalid key objectid, have %llu expect to be aligned to %u",
1174f82d1c7cSQu Wenruo 			   key->objectid, fs_info->sectorsize);
1175f82d1c7cSQu Wenruo 		return -EUCLEAN;
1176f82d1c7cSQu Wenruo 	}
1177f82d1c7cSQu Wenruo 
1178f82d1c7cSQu Wenruo 	/* key->offset is tree level for METADATA_ITEM_KEY */
1179f82d1c7cSQu Wenruo 	if (key->type == BTRFS_METADATA_ITEM_KEY &&
1180f82d1c7cSQu Wenruo 	    key->offset >= BTRFS_MAX_LEVEL) {
1181f82d1c7cSQu Wenruo 		extent_err(leaf, slot,
1182f82d1c7cSQu Wenruo 			   "invalid tree level, have %llu expect [0, %u]",
1183f82d1c7cSQu Wenruo 			   key->offset, BTRFS_MAX_LEVEL - 1);
1184f82d1c7cSQu Wenruo 		return -EUCLEAN;
1185f82d1c7cSQu Wenruo 	}
1186f82d1c7cSQu Wenruo 
1187f82d1c7cSQu Wenruo 	/*
1188f82d1c7cSQu Wenruo 	 * EXTENT/METADATA_ITEM consists of:
1189f82d1c7cSQu Wenruo 	 * 1) One btrfs_extent_item
1190f82d1c7cSQu Wenruo 	 *    Records the total refs, type and generation of the extent.
1191f82d1c7cSQu Wenruo 	 *
1192f82d1c7cSQu Wenruo 	 * 2) One btrfs_tree_block_info (for EXTENT_ITEM and tree backref only)
1193f82d1c7cSQu Wenruo 	 *    Records the first key and level of the tree block.
1194f82d1c7cSQu Wenruo 	 *
1195f82d1c7cSQu Wenruo 	 * 2) Zero or more btrfs_extent_inline_ref(s)
1196f82d1c7cSQu Wenruo 	 *    Each inline ref has one btrfs_extent_inline_ref shows:
1197f82d1c7cSQu Wenruo 	 *    2.1) The ref type, one of the 4
1198f82d1c7cSQu Wenruo 	 *         TREE_BLOCK_REF	Tree block only
1199f82d1c7cSQu Wenruo 	 *         SHARED_BLOCK_REF	Tree block only
1200f82d1c7cSQu Wenruo 	 *         EXTENT_DATA_REF	Data only
1201f82d1c7cSQu Wenruo 	 *         SHARED_DATA_REF	Data only
1202f82d1c7cSQu Wenruo 	 *    2.2) Ref type specific data
1203f82d1c7cSQu Wenruo 	 *         Either using btrfs_extent_inline_ref::offset, or specific
1204f82d1c7cSQu Wenruo 	 *         data structure.
1205f82d1c7cSQu Wenruo 	 */
1206f82d1c7cSQu Wenruo 	if (item_size < sizeof(*ei)) {
1207f82d1c7cSQu Wenruo 		extent_err(leaf, slot,
1208f82d1c7cSQu Wenruo 			   "invalid item size, have %u expect [%zu, %u)",
1209f82d1c7cSQu Wenruo 			   item_size, sizeof(*ei),
1210f82d1c7cSQu Wenruo 			   BTRFS_LEAF_DATA_SIZE(fs_info));
1211f82d1c7cSQu Wenruo 		return -EUCLEAN;
1212f82d1c7cSQu Wenruo 	}
1213f82d1c7cSQu Wenruo 	end = item_size + btrfs_item_ptr_offset(leaf, slot);
1214f82d1c7cSQu Wenruo 
1215f82d1c7cSQu Wenruo 	/* Checks against extent_item */
1216f82d1c7cSQu Wenruo 	ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
1217f82d1c7cSQu Wenruo 	flags = btrfs_extent_flags(leaf, ei);
1218f82d1c7cSQu Wenruo 	total_refs = btrfs_extent_refs(leaf, ei);
1219f82d1c7cSQu Wenruo 	generation = btrfs_extent_generation(leaf, ei);
1220f82d1c7cSQu Wenruo 	if (generation > btrfs_super_generation(fs_info->super_copy) + 1) {
1221f82d1c7cSQu Wenruo 		extent_err(leaf, slot,
1222f82d1c7cSQu Wenruo 			   "invalid generation, have %llu expect (0, %llu]",
1223f82d1c7cSQu Wenruo 			   generation,
1224f82d1c7cSQu Wenruo 			   btrfs_super_generation(fs_info->super_copy) + 1);
1225f82d1c7cSQu Wenruo 		return -EUCLEAN;
1226f82d1c7cSQu Wenruo 	}
1227c1499166SDavid Sterba 	if (!has_single_bit_set(flags & (BTRFS_EXTENT_FLAG_DATA |
1228f82d1c7cSQu Wenruo 					 BTRFS_EXTENT_FLAG_TREE_BLOCK))) {
1229f82d1c7cSQu Wenruo 		extent_err(leaf, slot,
1230f82d1c7cSQu Wenruo 		"invalid extent flag, have 0x%llx expect 1 bit set in 0x%llx",
1231f82d1c7cSQu Wenruo 			flags, BTRFS_EXTENT_FLAG_DATA |
1232f82d1c7cSQu Wenruo 			BTRFS_EXTENT_FLAG_TREE_BLOCK);
1233f82d1c7cSQu Wenruo 		return -EUCLEAN;
1234f82d1c7cSQu Wenruo 	}
1235f82d1c7cSQu Wenruo 	is_tree_block = !!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK);
1236f82d1c7cSQu Wenruo 	if (is_tree_block) {
1237f82d1c7cSQu Wenruo 		if (key->type == BTRFS_EXTENT_ITEM_KEY &&
1238f82d1c7cSQu Wenruo 		    key->offset != fs_info->nodesize) {
1239f82d1c7cSQu Wenruo 			extent_err(leaf, slot,
1240f82d1c7cSQu Wenruo 				   "invalid extent length, have %llu expect %u",
1241f82d1c7cSQu Wenruo 				   key->offset, fs_info->nodesize);
1242f82d1c7cSQu Wenruo 			return -EUCLEAN;
1243f82d1c7cSQu Wenruo 		}
1244f82d1c7cSQu Wenruo 	} else {
1245f82d1c7cSQu Wenruo 		if (key->type != BTRFS_EXTENT_ITEM_KEY) {
1246f82d1c7cSQu Wenruo 			extent_err(leaf, slot,
1247f82d1c7cSQu Wenruo 			"invalid key type, have %u expect %u for data backref",
1248f82d1c7cSQu Wenruo 				   key->type, BTRFS_EXTENT_ITEM_KEY);
1249f82d1c7cSQu Wenruo 			return -EUCLEAN;
1250f82d1c7cSQu Wenruo 		}
1251f82d1c7cSQu Wenruo 		if (!IS_ALIGNED(key->offset, fs_info->sectorsize)) {
1252f82d1c7cSQu Wenruo 			extent_err(leaf, slot,
1253f82d1c7cSQu Wenruo 			"invalid extent length, have %llu expect aligned to %u",
1254f82d1c7cSQu Wenruo 				   key->offset, fs_info->sectorsize);
1255f82d1c7cSQu Wenruo 			return -EUCLEAN;
1256f82d1c7cSQu Wenruo 		}
1257f82d1c7cSQu Wenruo 	}
1258f82d1c7cSQu Wenruo 	ptr = (unsigned long)(struct btrfs_extent_item *)(ei + 1);
1259f82d1c7cSQu Wenruo 
1260f82d1c7cSQu Wenruo 	/* Check the special case of btrfs_tree_block_info */
1261f82d1c7cSQu Wenruo 	if (is_tree_block && key->type != BTRFS_METADATA_ITEM_KEY) {
1262f82d1c7cSQu Wenruo 		struct btrfs_tree_block_info *info;
1263f82d1c7cSQu Wenruo 
1264f82d1c7cSQu Wenruo 		info = (struct btrfs_tree_block_info *)ptr;
1265f82d1c7cSQu Wenruo 		if (btrfs_tree_block_level(leaf, info) >= BTRFS_MAX_LEVEL) {
1266f82d1c7cSQu Wenruo 			extent_err(leaf, slot,
1267f82d1c7cSQu Wenruo 			"invalid tree block info level, have %u expect [0, %u]",
1268f82d1c7cSQu Wenruo 				   btrfs_tree_block_level(leaf, info),
1269f82d1c7cSQu Wenruo 				   BTRFS_MAX_LEVEL - 1);
1270f82d1c7cSQu Wenruo 			return -EUCLEAN;
1271f82d1c7cSQu Wenruo 		}
1272f82d1c7cSQu Wenruo 		ptr = (unsigned long)(struct btrfs_tree_block_info *)(info + 1);
1273f82d1c7cSQu Wenruo 	}
1274f82d1c7cSQu Wenruo 
1275f82d1c7cSQu Wenruo 	/* Check inline refs */
1276f82d1c7cSQu Wenruo 	while (ptr < end) {
1277f82d1c7cSQu Wenruo 		struct btrfs_extent_inline_ref *iref;
1278f82d1c7cSQu Wenruo 		struct btrfs_extent_data_ref *dref;
1279f82d1c7cSQu Wenruo 		struct btrfs_shared_data_ref *sref;
1280f82d1c7cSQu Wenruo 		u64 dref_offset;
1281f82d1c7cSQu Wenruo 		u64 inline_offset;
1282f82d1c7cSQu Wenruo 		u8 inline_type;
1283f82d1c7cSQu Wenruo 
1284f82d1c7cSQu Wenruo 		if (ptr + sizeof(*iref) > end) {
1285f82d1c7cSQu Wenruo 			extent_err(leaf, slot,
1286f82d1c7cSQu Wenruo "inline ref item overflows extent item, ptr %lu iref size %zu end %lu",
1287f82d1c7cSQu Wenruo 				   ptr, sizeof(*iref), end);
1288f82d1c7cSQu Wenruo 			return -EUCLEAN;
1289f82d1c7cSQu Wenruo 		}
1290f82d1c7cSQu Wenruo 		iref = (struct btrfs_extent_inline_ref *)ptr;
1291f82d1c7cSQu Wenruo 		inline_type = btrfs_extent_inline_ref_type(leaf, iref);
1292f82d1c7cSQu Wenruo 		inline_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1293f82d1c7cSQu Wenruo 		if (ptr + btrfs_extent_inline_ref_size(inline_type) > end) {
1294f82d1c7cSQu Wenruo 			extent_err(leaf, slot,
1295f82d1c7cSQu Wenruo "inline ref item overflows extent item, ptr %lu iref size %u end %lu",
1296f82d1c7cSQu Wenruo 				   ptr, inline_type, end);
1297f82d1c7cSQu Wenruo 			return -EUCLEAN;
1298f82d1c7cSQu Wenruo 		}
1299f82d1c7cSQu Wenruo 
1300f82d1c7cSQu Wenruo 		switch (inline_type) {
1301f82d1c7cSQu Wenruo 		/* inline_offset is subvolid of the owner, no need to check */
1302f82d1c7cSQu Wenruo 		case BTRFS_TREE_BLOCK_REF_KEY:
1303f82d1c7cSQu Wenruo 			inline_refs++;
1304f82d1c7cSQu Wenruo 			break;
1305f82d1c7cSQu Wenruo 		/* Contains parent bytenr */
1306f82d1c7cSQu Wenruo 		case BTRFS_SHARED_BLOCK_REF_KEY:
1307f82d1c7cSQu Wenruo 			if (!IS_ALIGNED(inline_offset, fs_info->sectorsize)) {
1308f82d1c7cSQu Wenruo 				extent_err(leaf, slot,
1309f82d1c7cSQu Wenruo 		"invalid tree parent bytenr, have %llu expect aligned to %u",
1310f82d1c7cSQu Wenruo 					   inline_offset, fs_info->sectorsize);
1311f82d1c7cSQu Wenruo 				return -EUCLEAN;
1312f82d1c7cSQu Wenruo 			}
1313f82d1c7cSQu Wenruo 			inline_refs++;
1314f82d1c7cSQu Wenruo 			break;
1315f82d1c7cSQu Wenruo 		/*
1316f82d1c7cSQu Wenruo 		 * Contains owner subvolid, owner key objectid, adjusted offset.
1317f82d1c7cSQu Wenruo 		 * The only obvious corruption can happen in that offset.
1318f82d1c7cSQu Wenruo 		 */
1319f82d1c7cSQu Wenruo 		case BTRFS_EXTENT_DATA_REF_KEY:
1320f82d1c7cSQu Wenruo 			dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1321f82d1c7cSQu Wenruo 			dref_offset = btrfs_extent_data_ref_offset(leaf, dref);
1322f82d1c7cSQu Wenruo 			if (!IS_ALIGNED(dref_offset, fs_info->sectorsize)) {
1323f82d1c7cSQu Wenruo 				extent_err(leaf, slot,
1324f82d1c7cSQu Wenruo 		"invalid data ref offset, have %llu expect aligned to %u",
1325f82d1c7cSQu Wenruo 					   dref_offset, fs_info->sectorsize);
1326f82d1c7cSQu Wenruo 				return -EUCLEAN;
1327f82d1c7cSQu Wenruo 			}
1328f82d1c7cSQu Wenruo 			inline_refs += btrfs_extent_data_ref_count(leaf, dref);
1329f82d1c7cSQu Wenruo 			break;
1330f82d1c7cSQu Wenruo 		/* Contains parent bytenr and ref count */
1331f82d1c7cSQu Wenruo 		case BTRFS_SHARED_DATA_REF_KEY:
1332f82d1c7cSQu Wenruo 			sref = (struct btrfs_shared_data_ref *)(iref + 1);
1333f82d1c7cSQu Wenruo 			if (!IS_ALIGNED(inline_offset, fs_info->sectorsize)) {
1334f82d1c7cSQu Wenruo 				extent_err(leaf, slot,
1335f82d1c7cSQu Wenruo 		"invalid data parent bytenr, have %llu expect aligned to %u",
1336f82d1c7cSQu Wenruo 					   inline_offset, fs_info->sectorsize);
1337f82d1c7cSQu Wenruo 				return -EUCLEAN;
1338f82d1c7cSQu Wenruo 			}
1339f82d1c7cSQu Wenruo 			inline_refs += btrfs_shared_data_ref_count(leaf, sref);
1340f82d1c7cSQu Wenruo 			break;
1341f82d1c7cSQu Wenruo 		default:
1342f82d1c7cSQu Wenruo 			extent_err(leaf, slot, "unknown inline ref type: %u",
1343f82d1c7cSQu Wenruo 				   inline_type);
1344f82d1c7cSQu Wenruo 			return -EUCLEAN;
1345f82d1c7cSQu Wenruo 		}
1346f82d1c7cSQu Wenruo 		ptr += btrfs_extent_inline_ref_size(inline_type);
1347f82d1c7cSQu Wenruo 	}
1348f82d1c7cSQu Wenruo 	/* No padding is allowed */
1349f82d1c7cSQu Wenruo 	if (ptr != end) {
1350f82d1c7cSQu Wenruo 		extent_err(leaf, slot,
1351f82d1c7cSQu Wenruo 			   "invalid extent item size, padding bytes found");
1352f82d1c7cSQu Wenruo 		return -EUCLEAN;
1353f82d1c7cSQu Wenruo 	}
1354f82d1c7cSQu Wenruo 
1355f82d1c7cSQu Wenruo 	/* Finally, check the inline refs against total refs */
1356f82d1c7cSQu Wenruo 	if (inline_refs > total_refs) {
1357f82d1c7cSQu Wenruo 		extent_err(leaf, slot,
1358f82d1c7cSQu Wenruo 			"invalid extent refs, have %llu expect >= inline %llu",
1359f82d1c7cSQu Wenruo 			   total_refs, inline_refs);
1360f82d1c7cSQu Wenruo 		return -EUCLEAN;
1361f82d1c7cSQu Wenruo 	}
1362f82d1c7cSQu Wenruo 	return 0;
1363f82d1c7cSQu Wenruo }
1364f82d1c7cSQu Wenruo 
1365e2406a6fSQu Wenruo static int check_simple_keyed_refs(struct extent_buffer *leaf,
1366e2406a6fSQu Wenruo 				   struct btrfs_key *key, int slot)
1367e2406a6fSQu Wenruo {
1368e2406a6fSQu Wenruo 	u32 expect_item_size = 0;
1369e2406a6fSQu Wenruo 
1370e2406a6fSQu Wenruo 	if (key->type == BTRFS_SHARED_DATA_REF_KEY)
1371e2406a6fSQu Wenruo 		expect_item_size = sizeof(struct btrfs_shared_data_ref);
1372e2406a6fSQu Wenruo 
1373e2406a6fSQu Wenruo 	if (btrfs_item_size_nr(leaf, slot) != expect_item_size) {
1374e2406a6fSQu Wenruo 		generic_err(leaf, slot,
1375e2406a6fSQu Wenruo 		"invalid item size, have %u expect %u for key type %u",
1376e2406a6fSQu Wenruo 			    btrfs_item_size_nr(leaf, slot),
1377e2406a6fSQu Wenruo 			    expect_item_size, key->type);
1378e2406a6fSQu Wenruo 		return -EUCLEAN;
1379e2406a6fSQu Wenruo 	}
1380e2406a6fSQu Wenruo 	if (!IS_ALIGNED(key->objectid, leaf->fs_info->sectorsize)) {
1381e2406a6fSQu Wenruo 		generic_err(leaf, slot,
1382e2406a6fSQu Wenruo "invalid key objectid for shared block ref, have %llu expect aligned to %u",
1383e2406a6fSQu Wenruo 			    key->objectid, leaf->fs_info->sectorsize);
1384e2406a6fSQu Wenruo 		return -EUCLEAN;
1385e2406a6fSQu Wenruo 	}
1386e2406a6fSQu Wenruo 	if (key->type != BTRFS_TREE_BLOCK_REF_KEY &&
1387e2406a6fSQu Wenruo 	    !IS_ALIGNED(key->offset, leaf->fs_info->sectorsize)) {
1388e2406a6fSQu Wenruo 		extent_err(leaf, slot,
1389e2406a6fSQu Wenruo 		"invalid tree parent bytenr, have %llu expect aligned to %u",
1390e2406a6fSQu Wenruo 			   key->offset, leaf->fs_info->sectorsize);
1391e2406a6fSQu Wenruo 		return -EUCLEAN;
1392e2406a6fSQu Wenruo 	}
1393e2406a6fSQu Wenruo 	return 0;
1394e2406a6fSQu Wenruo }
1395e2406a6fSQu Wenruo 
13960785a9aaSQu Wenruo static int check_extent_data_ref(struct extent_buffer *leaf,
13970785a9aaSQu Wenruo 				 struct btrfs_key *key, int slot)
13980785a9aaSQu Wenruo {
13990785a9aaSQu Wenruo 	struct btrfs_extent_data_ref *dref;
14000785a9aaSQu Wenruo 	unsigned long ptr = btrfs_item_ptr_offset(leaf, slot);
14010785a9aaSQu Wenruo 	const unsigned long end = ptr + btrfs_item_size_nr(leaf, slot);
14020785a9aaSQu Wenruo 
14030785a9aaSQu Wenruo 	if (btrfs_item_size_nr(leaf, slot) % sizeof(*dref) != 0) {
14040785a9aaSQu Wenruo 		generic_err(leaf, slot,
14050785a9aaSQu Wenruo 	"invalid item size, have %u expect aligned to %zu for key type %u",
14060785a9aaSQu Wenruo 			    btrfs_item_size_nr(leaf, slot),
14070785a9aaSQu Wenruo 			    sizeof(*dref), key->type);
14080785a9aaSQu Wenruo 	}
14090785a9aaSQu Wenruo 	if (!IS_ALIGNED(key->objectid, leaf->fs_info->sectorsize)) {
14100785a9aaSQu Wenruo 		generic_err(leaf, slot,
14110785a9aaSQu Wenruo "invalid key objectid for shared block ref, have %llu expect aligned to %u",
14120785a9aaSQu Wenruo 			    key->objectid, leaf->fs_info->sectorsize);
14130785a9aaSQu Wenruo 		return -EUCLEAN;
14140785a9aaSQu Wenruo 	}
14150785a9aaSQu Wenruo 	for (; ptr < end; ptr += sizeof(*dref)) {
14160785a9aaSQu Wenruo 		u64 root_objectid;
14170785a9aaSQu Wenruo 		u64 owner;
14180785a9aaSQu Wenruo 		u64 offset;
14190785a9aaSQu Wenruo 		u64 hash;
14200785a9aaSQu Wenruo 
14210785a9aaSQu Wenruo 		dref = (struct btrfs_extent_data_ref *)ptr;
14220785a9aaSQu Wenruo 		root_objectid = btrfs_extent_data_ref_root(leaf, dref);
14230785a9aaSQu Wenruo 		owner = btrfs_extent_data_ref_objectid(leaf, dref);
14240785a9aaSQu Wenruo 		offset = btrfs_extent_data_ref_offset(leaf, dref);
14250785a9aaSQu Wenruo 		hash = hash_extent_data_ref(root_objectid, owner, offset);
14260785a9aaSQu Wenruo 		if (hash != key->offset) {
14270785a9aaSQu Wenruo 			extent_err(leaf, slot,
14280785a9aaSQu Wenruo 	"invalid extent data ref hash, item has 0x%016llx key has 0x%016llx",
14290785a9aaSQu Wenruo 				   hash, key->offset);
14300785a9aaSQu Wenruo 			return -EUCLEAN;
14310785a9aaSQu Wenruo 		}
14320785a9aaSQu Wenruo 		if (!IS_ALIGNED(offset, leaf->fs_info->sectorsize)) {
14330785a9aaSQu Wenruo 			extent_err(leaf, slot,
14340785a9aaSQu Wenruo 	"invalid extent data backref offset, have %llu expect aligned to %u",
14350785a9aaSQu Wenruo 				   offset, leaf->fs_info->sectorsize);
14360785a9aaSQu Wenruo 		}
14370785a9aaSQu Wenruo 	}
14380785a9aaSQu Wenruo 	return 0;
14390785a9aaSQu Wenruo }
14400785a9aaSQu Wenruo 
1441c3053ebbSQu Wenruo #define inode_ref_err(eb, slot, fmt, args...)			\
1442c3053ebbSQu Wenruo 	inode_item_err(eb, slot, fmt, ##args)
144371bf92a9SQu Wenruo static int check_inode_ref(struct extent_buffer *leaf,
144471bf92a9SQu Wenruo 			   struct btrfs_key *key, struct btrfs_key *prev_key,
144571bf92a9SQu Wenruo 			   int slot)
144671bf92a9SQu Wenruo {
144771bf92a9SQu Wenruo 	struct btrfs_inode_ref *iref;
144871bf92a9SQu Wenruo 	unsigned long ptr;
144971bf92a9SQu Wenruo 	unsigned long end;
145071bf92a9SQu Wenruo 
145180d7fd1eSQu Wenruo 	if (!check_prev_ino(leaf, key, slot, prev_key))
145280d7fd1eSQu Wenruo 		return -EUCLEAN;
145371bf92a9SQu Wenruo 	/* namelen can't be 0, so item_size == sizeof() is also invalid */
145471bf92a9SQu Wenruo 	if (btrfs_item_size_nr(leaf, slot) <= sizeof(*iref)) {
1455c3053ebbSQu Wenruo 		inode_ref_err(leaf, slot,
145671bf92a9SQu Wenruo 			"invalid item size, have %u expect (%zu, %u)",
145771bf92a9SQu Wenruo 			btrfs_item_size_nr(leaf, slot),
145871bf92a9SQu Wenruo 			sizeof(*iref), BTRFS_LEAF_DATA_SIZE(leaf->fs_info));
145971bf92a9SQu Wenruo 		return -EUCLEAN;
146071bf92a9SQu Wenruo 	}
146171bf92a9SQu Wenruo 
146271bf92a9SQu Wenruo 	ptr = btrfs_item_ptr_offset(leaf, slot);
146371bf92a9SQu Wenruo 	end = ptr + btrfs_item_size_nr(leaf, slot);
146471bf92a9SQu Wenruo 	while (ptr < end) {
146571bf92a9SQu Wenruo 		u16 namelen;
146671bf92a9SQu Wenruo 
146771bf92a9SQu Wenruo 		if (ptr + sizeof(iref) > end) {
1468c3053ebbSQu Wenruo 			inode_ref_err(leaf, slot,
146971bf92a9SQu Wenruo 			"inode ref overflow, ptr %lu end %lu inode_ref_size %zu",
147071bf92a9SQu Wenruo 				ptr, end, sizeof(iref));
147171bf92a9SQu Wenruo 			return -EUCLEAN;
147271bf92a9SQu Wenruo 		}
147371bf92a9SQu Wenruo 
147471bf92a9SQu Wenruo 		iref = (struct btrfs_inode_ref *)ptr;
147571bf92a9SQu Wenruo 		namelen = btrfs_inode_ref_name_len(leaf, iref);
147671bf92a9SQu Wenruo 		if (ptr + sizeof(*iref) + namelen > end) {
1477c3053ebbSQu Wenruo 			inode_ref_err(leaf, slot,
147871bf92a9SQu Wenruo 				"inode ref overflow, ptr %lu end %lu namelen %u",
147971bf92a9SQu Wenruo 				ptr, end, namelen);
148071bf92a9SQu Wenruo 			return -EUCLEAN;
148171bf92a9SQu Wenruo 		}
148271bf92a9SQu Wenruo 
148371bf92a9SQu Wenruo 		/*
148471bf92a9SQu Wenruo 		 * NOTE: In theory we should record all found index numbers
148571bf92a9SQu Wenruo 		 * to find any duplicated indexes, but that will be too time
148671bf92a9SQu Wenruo 		 * consuming for inodes with too many hard links.
148771bf92a9SQu Wenruo 		 */
148871bf92a9SQu Wenruo 		ptr += sizeof(*iref) + namelen;
148971bf92a9SQu Wenruo 	}
149071bf92a9SQu Wenruo 	return 0;
149171bf92a9SQu Wenruo }
149271bf92a9SQu Wenruo 
149382fc28fbSQu Wenruo /*
1494557ea5ddSQu Wenruo  * Common point to switch the item-specific validation.
1495557ea5ddSQu Wenruo  */
14960076bc89SDavid Sterba static int check_leaf_item(struct extent_buffer *leaf,
14974e9845efSFilipe Manana 			   struct btrfs_key *key, int slot,
14984e9845efSFilipe Manana 			   struct btrfs_key *prev_key)
1499557ea5ddSQu Wenruo {
1500557ea5ddSQu Wenruo 	int ret = 0;
1501075cb3c7SQu Wenruo 	struct btrfs_chunk *chunk;
1502557ea5ddSQu Wenruo 
1503557ea5ddSQu Wenruo 	switch (key->type) {
1504557ea5ddSQu Wenruo 	case BTRFS_EXTENT_DATA_KEY:
15054e9845efSFilipe Manana 		ret = check_extent_data_item(leaf, key, slot, prev_key);
1506557ea5ddSQu Wenruo 		break;
1507557ea5ddSQu Wenruo 	case BTRFS_EXTENT_CSUM_KEY:
1508ad1d8c43SFilipe Manana 		ret = check_csum_item(leaf, key, slot, prev_key);
1509557ea5ddSQu Wenruo 		break;
1510ad7b0368SQu Wenruo 	case BTRFS_DIR_ITEM_KEY:
1511ad7b0368SQu Wenruo 	case BTRFS_DIR_INDEX_KEY:
1512ad7b0368SQu Wenruo 	case BTRFS_XATTR_ITEM_KEY:
1513c18679ebSQu Wenruo 		ret = check_dir_item(leaf, key, prev_key, slot);
1514ad7b0368SQu Wenruo 		break;
151571bf92a9SQu Wenruo 	case BTRFS_INODE_REF_KEY:
151671bf92a9SQu Wenruo 		ret = check_inode_ref(leaf, key, prev_key, slot);
151771bf92a9SQu Wenruo 		break;
1518fce466eaSQu Wenruo 	case BTRFS_BLOCK_GROUP_ITEM_KEY:
1519af60ce2bSDavid Sterba 		ret = check_block_group_item(leaf, key, slot);
1520fce466eaSQu Wenruo 		break;
1521075cb3c7SQu Wenruo 	case BTRFS_CHUNK_ITEM_KEY:
1522075cb3c7SQu Wenruo 		chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
1523f6d2a5c2SQu Wenruo 		ret = check_leaf_chunk_item(leaf, chunk, key, slot);
1524075cb3c7SQu Wenruo 		break;
1525ab4ba2e1SQu Wenruo 	case BTRFS_DEV_ITEM_KEY:
1526412a2312SDavid Sterba 		ret = check_dev_item(leaf, key, slot);
1527ab4ba2e1SQu Wenruo 		break;
1528496245caSQu Wenruo 	case BTRFS_INODE_ITEM_KEY:
152939e57f49SDavid Sterba 		ret = check_inode_item(leaf, key, slot);
1530496245caSQu Wenruo 		break;
1531259ee775SQu Wenruo 	case BTRFS_ROOT_ITEM_KEY:
1532259ee775SQu Wenruo 		ret = check_root_item(leaf, key, slot);
1533259ee775SQu Wenruo 		break;
1534f82d1c7cSQu Wenruo 	case BTRFS_EXTENT_ITEM_KEY:
1535f82d1c7cSQu Wenruo 	case BTRFS_METADATA_ITEM_KEY:
1536f82d1c7cSQu Wenruo 		ret = check_extent_item(leaf, key, slot);
1537f82d1c7cSQu Wenruo 		break;
1538e2406a6fSQu Wenruo 	case BTRFS_TREE_BLOCK_REF_KEY:
1539e2406a6fSQu Wenruo 	case BTRFS_SHARED_DATA_REF_KEY:
1540e2406a6fSQu Wenruo 	case BTRFS_SHARED_BLOCK_REF_KEY:
1541e2406a6fSQu Wenruo 		ret = check_simple_keyed_refs(leaf, key, slot);
1542e2406a6fSQu Wenruo 		break;
15430785a9aaSQu Wenruo 	case BTRFS_EXTENT_DATA_REF_KEY:
15440785a9aaSQu Wenruo 		ret = check_extent_data_ref(leaf, key, slot);
15450785a9aaSQu Wenruo 		break;
1546557ea5ddSQu Wenruo 	}
1547557ea5ddSQu Wenruo 	return ret;
1548557ea5ddSQu Wenruo }
1549557ea5ddSQu Wenruo 
1550e2ccd361SDavid Sterba static int check_leaf(struct extent_buffer *leaf, bool check_item_data)
1551557ea5ddSQu Wenruo {
1552e2ccd361SDavid Sterba 	struct btrfs_fs_info *fs_info = leaf->fs_info;
1553557ea5ddSQu Wenruo 	/* No valid key type is 0, so all key should be larger than this key */
1554557ea5ddSQu Wenruo 	struct btrfs_key prev_key = {0, 0, 0};
1555557ea5ddSQu Wenruo 	struct btrfs_key key;
1556557ea5ddSQu Wenruo 	u32 nritems = btrfs_header_nritems(leaf);
1557557ea5ddSQu Wenruo 	int slot;
1558557ea5ddSQu Wenruo 
1559f556faa4SQu Wenruo 	if (btrfs_header_level(leaf) != 0) {
156086a6be3aSDavid Sterba 		generic_err(leaf, 0,
1561f556faa4SQu Wenruo 			"invalid level for leaf, have %d expect 0",
1562f556faa4SQu Wenruo 			btrfs_header_level(leaf));
1563f556faa4SQu Wenruo 		return -EUCLEAN;
1564f556faa4SQu Wenruo 	}
1565f556faa4SQu Wenruo 
1566557ea5ddSQu Wenruo 	/*
1567557ea5ddSQu Wenruo 	 * Extent buffers from a relocation tree have a owner field that
1568557ea5ddSQu Wenruo 	 * corresponds to the subvolume tree they are based on. So just from an
1569557ea5ddSQu Wenruo 	 * extent buffer alone we can not find out what is the id of the
1570557ea5ddSQu Wenruo 	 * corresponding subvolume tree, so we can not figure out if the extent
1571557ea5ddSQu Wenruo 	 * buffer corresponds to the root of the relocation tree or not. So
1572557ea5ddSQu Wenruo 	 * skip this check for relocation trees.
1573557ea5ddSQu Wenruo 	 */
1574557ea5ddSQu Wenruo 	if (nritems == 0 && !btrfs_header_flag(leaf, BTRFS_HEADER_FLAG_RELOC)) {
1575ba480dd4SQu Wenruo 		u64 owner = btrfs_header_owner(leaf);
1576557ea5ddSQu Wenruo 
1577ba480dd4SQu Wenruo 		/* These trees must never be empty */
1578ba480dd4SQu Wenruo 		if (owner == BTRFS_ROOT_TREE_OBJECTID ||
1579ba480dd4SQu Wenruo 		    owner == BTRFS_CHUNK_TREE_OBJECTID ||
1580ba480dd4SQu Wenruo 		    owner == BTRFS_EXTENT_TREE_OBJECTID ||
1581ba480dd4SQu Wenruo 		    owner == BTRFS_DEV_TREE_OBJECTID ||
1582ba480dd4SQu Wenruo 		    owner == BTRFS_FS_TREE_OBJECTID ||
1583ba480dd4SQu Wenruo 		    owner == BTRFS_DATA_RELOC_TREE_OBJECTID) {
158486a6be3aSDavid Sterba 			generic_err(leaf, 0,
1585ba480dd4SQu Wenruo 			"invalid root, root %llu must never be empty",
1586ba480dd4SQu Wenruo 				    owner);
1587ba480dd4SQu Wenruo 			return -EUCLEAN;
1588ba480dd4SQu Wenruo 		}
158962fdaa52SQu Wenruo 		/* Unknown tree */
159062fdaa52SQu Wenruo 		if (owner == 0) {
159162fdaa52SQu Wenruo 			generic_err(leaf, 0,
159262fdaa52SQu Wenruo 				"invalid owner, root 0 is not defined");
159362fdaa52SQu Wenruo 			return -EUCLEAN;
159462fdaa52SQu Wenruo 		}
1595557ea5ddSQu Wenruo 		return 0;
1596557ea5ddSQu Wenruo 	}
1597557ea5ddSQu Wenruo 
1598557ea5ddSQu Wenruo 	if (nritems == 0)
1599557ea5ddSQu Wenruo 		return 0;
1600557ea5ddSQu Wenruo 
1601557ea5ddSQu Wenruo 	/*
1602557ea5ddSQu Wenruo 	 * Check the following things to make sure this is a good leaf, and
1603557ea5ddSQu Wenruo 	 * leaf users won't need to bother with similar sanity checks:
1604557ea5ddSQu Wenruo 	 *
1605557ea5ddSQu Wenruo 	 * 1) key ordering
1606557ea5ddSQu Wenruo 	 * 2) item offset and size
1607557ea5ddSQu Wenruo 	 *    No overlap, no hole, all inside the leaf.
1608557ea5ddSQu Wenruo 	 * 3) item content
1609557ea5ddSQu Wenruo 	 *    If possible, do comprehensive sanity check.
1610557ea5ddSQu Wenruo 	 *    NOTE: All checks must only rely on the item data itself.
1611557ea5ddSQu Wenruo 	 */
1612557ea5ddSQu Wenruo 	for (slot = 0; slot < nritems; slot++) {
1613557ea5ddSQu Wenruo 		u32 item_end_expected;
1614557ea5ddSQu Wenruo 		int ret;
1615557ea5ddSQu Wenruo 
1616557ea5ddSQu Wenruo 		btrfs_item_key_to_cpu(leaf, &key, slot);
1617557ea5ddSQu Wenruo 
1618557ea5ddSQu Wenruo 		/* Make sure the keys are in the right order */
1619557ea5ddSQu Wenruo 		if (btrfs_comp_cpu_keys(&prev_key, &key) >= 0) {
162086a6be3aSDavid Sterba 			generic_err(leaf, slot,
1621478d01b3SQu Wenruo 	"bad key order, prev (%llu %u %llu) current (%llu %u %llu)",
1622478d01b3SQu Wenruo 				prev_key.objectid, prev_key.type,
1623478d01b3SQu Wenruo 				prev_key.offset, key.objectid, key.type,
1624478d01b3SQu Wenruo 				key.offset);
1625557ea5ddSQu Wenruo 			return -EUCLEAN;
1626557ea5ddSQu Wenruo 		}
1627557ea5ddSQu Wenruo 
1628557ea5ddSQu Wenruo 		/*
1629557ea5ddSQu Wenruo 		 * Make sure the offset and ends are right, remember that the
1630557ea5ddSQu Wenruo 		 * item data starts at the end of the leaf and grows towards the
1631557ea5ddSQu Wenruo 		 * front.
1632557ea5ddSQu Wenruo 		 */
1633557ea5ddSQu Wenruo 		if (slot == 0)
1634557ea5ddSQu Wenruo 			item_end_expected = BTRFS_LEAF_DATA_SIZE(fs_info);
1635557ea5ddSQu Wenruo 		else
1636557ea5ddSQu Wenruo 			item_end_expected = btrfs_item_offset_nr(leaf,
1637557ea5ddSQu Wenruo 								 slot - 1);
1638557ea5ddSQu Wenruo 		if (btrfs_item_end_nr(leaf, slot) != item_end_expected) {
163986a6be3aSDavid Sterba 			generic_err(leaf, slot,
1640478d01b3SQu Wenruo 				"unexpected item end, have %u expect %u",
1641478d01b3SQu Wenruo 				btrfs_item_end_nr(leaf, slot),
1642478d01b3SQu Wenruo 				item_end_expected);
1643557ea5ddSQu Wenruo 			return -EUCLEAN;
1644557ea5ddSQu Wenruo 		}
1645557ea5ddSQu Wenruo 
1646557ea5ddSQu Wenruo 		/*
1647557ea5ddSQu Wenruo 		 * Check to make sure that we don't point outside of the leaf,
1648557ea5ddSQu Wenruo 		 * just in case all the items are consistent to each other, but
1649557ea5ddSQu Wenruo 		 * all point outside of the leaf.
1650557ea5ddSQu Wenruo 		 */
1651557ea5ddSQu Wenruo 		if (btrfs_item_end_nr(leaf, slot) >
1652557ea5ddSQu Wenruo 		    BTRFS_LEAF_DATA_SIZE(fs_info)) {
165386a6be3aSDavid Sterba 			generic_err(leaf, slot,
1654478d01b3SQu Wenruo 			"slot end outside of leaf, have %u expect range [0, %u]",
1655478d01b3SQu Wenruo 				btrfs_item_end_nr(leaf, slot),
1656478d01b3SQu Wenruo 				BTRFS_LEAF_DATA_SIZE(fs_info));
1657557ea5ddSQu Wenruo 			return -EUCLEAN;
1658557ea5ddSQu Wenruo 		}
1659557ea5ddSQu Wenruo 
1660557ea5ddSQu Wenruo 		/* Also check if the item pointer overlaps with btrfs item. */
1661557ea5ddSQu Wenruo 		if (btrfs_item_nr_offset(slot) + sizeof(struct btrfs_item) >
1662557ea5ddSQu Wenruo 		    btrfs_item_ptr_offset(leaf, slot)) {
166386a6be3aSDavid Sterba 			generic_err(leaf, slot,
1664478d01b3SQu Wenruo 		"slot overlaps with its data, item end %lu data start %lu",
1665478d01b3SQu Wenruo 				btrfs_item_nr_offset(slot) +
1666478d01b3SQu Wenruo 				sizeof(struct btrfs_item),
1667478d01b3SQu Wenruo 				btrfs_item_ptr_offset(leaf, slot));
1668557ea5ddSQu Wenruo 			return -EUCLEAN;
1669557ea5ddSQu Wenruo 		}
1670557ea5ddSQu Wenruo 
167169fc6cbbSQu Wenruo 		if (check_item_data) {
167269fc6cbbSQu Wenruo 			/*
167369fc6cbbSQu Wenruo 			 * Check if the item size and content meet other
167469fc6cbbSQu Wenruo 			 * criteria
167569fc6cbbSQu Wenruo 			 */
16764e9845efSFilipe Manana 			ret = check_leaf_item(leaf, &key, slot, &prev_key);
1677557ea5ddSQu Wenruo 			if (ret < 0)
1678557ea5ddSQu Wenruo 				return ret;
167969fc6cbbSQu Wenruo 		}
1680557ea5ddSQu Wenruo 
1681557ea5ddSQu Wenruo 		prev_key.objectid = key.objectid;
1682557ea5ddSQu Wenruo 		prev_key.type = key.type;
1683557ea5ddSQu Wenruo 		prev_key.offset = key.offset;
1684557ea5ddSQu Wenruo 	}
1685557ea5ddSQu Wenruo 
1686557ea5ddSQu Wenruo 	return 0;
1687557ea5ddSQu Wenruo }
1688557ea5ddSQu Wenruo 
16891c4360eeSDavid Sterba int btrfs_check_leaf_full(struct extent_buffer *leaf)
169069fc6cbbSQu Wenruo {
1691e2ccd361SDavid Sterba 	return check_leaf(leaf, true);
169269fc6cbbSQu Wenruo }
169302529d7aSQu Wenruo ALLOW_ERROR_INJECTION(btrfs_check_leaf_full, ERRNO);
169469fc6cbbSQu Wenruo 
1695cfdaad5eSDavid Sterba int btrfs_check_leaf_relaxed(struct extent_buffer *leaf)
16962f659546SQu Wenruo {
1697e2ccd361SDavid Sterba 	return check_leaf(leaf, false);
16982f659546SQu Wenruo }
16992f659546SQu Wenruo 
1700813fd1dcSDavid Sterba int btrfs_check_node(struct extent_buffer *node)
1701557ea5ddSQu Wenruo {
1702813fd1dcSDavid Sterba 	struct btrfs_fs_info *fs_info = node->fs_info;
1703557ea5ddSQu Wenruo 	unsigned long nr = btrfs_header_nritems(node);
1704557ea5ddSQu Wenruo 	struct btrfs_key key, next_key;
1705557ea5ddSQu Wenruo 	int slot;
1706f556faa4SQu Wenruo 	int level = btrfs_header_level(node);
1707557ea5ddSQu Wenruo 	u64 bytenr;
1708557ea5ddSQu Wenruo 	int ret = 0;
1709557ea5ddSQu Wenruo 
1710f556faa4SQu Wenruo 	if (level <= 0 || level >= BTRFS_MAX_LEVEL) {
171186a6be3aSDavid Sterba 		generic_err(node, 0,
1712f556faa4SQu Wenruo 			"invalid level for node, have %d expect [1, %d]",
1713f556faa4SQu Wenruo 			level, BTRFS_MAX_LEVEL - 1);
1714f556faa4SQu Wenruo 		return -EUCLEAN;
1715f556faa4SQu Wenruo 	}
17162f659546SQu Wenruo 	if (nr == 0 || nr > BTRFS_NODEPTRS_PER_BLOCK(fs_info)) {
17172f659546SQu Wenruo 		btrfs_crit(fs_info,
1718bba4f298SQu Wenruo "corrupt node: root=%llu block=%llu, nritems too %s, have %lu expect range [1,%u]",
17192f659546SQu Wenruo 			   btrfs_header_owner(node), node->start,
1720bba4f298SQu Wenruo 			   nr == 0 ? "small" : "large", nr,
17212f659546SQu Wenruo 			   BTRFS_NODEPTRS_PER_BLOCK(fs_info));
1722bba4f298SQu Wenruo 		return -EUCLEAN;
1723557ea5ddSQu Wenruo 	}
1724557ea5ddSQu Wenruo 
1725557ea5ddSQu Wenruo 	for (slot = 0; slot < nr - 1; slot++) {
1726557ea5ddSQu Wenruo 		bytenr = btrfs_node_blockptr(node, slot);
1727557ea5ddSQu Wenruo 		btrfs_node_key_to_cpu(node, &key, slot);
1728557ea5ddSQu Wenruo 		btrfs_node_key_to_cpu(node, &next_key, slot + 1);
1729557ea5ddSQu Wenruo 
1730557ea5ddSQu Wenruo 		if (!bytenr) {
173186a6be3aSDavid Sterba 			generic_err(node, slot,
1732bba4f298SQu Wenruo 				"invalid NULL node pointer");
1733bba4f298SQu Wenruo 			ret = -EUCLEAN;
1734bba4f298SQu Wenruo 			goto out;
1735bba4f298SQu Wenruo 		}
17362f659546SQu Wenruo 		if (!IS_ALIGNED(bytenr, fs_info->sectorsize)) {
173786a6be3aSDavid Sterba 			generic_err(node, slot,
1738bba4f298SQu Wenruo 			"unaligned pointer, have %llu should be aligned to %u",
17392f659546SQu Wenruo 				bytenr, fs_info->sectorsize);
1740bba4f298SQu Wenruo 			ret = -EUCLEAN;
1741557ea5ddSQu Wenruo 			goto out;
1742557ea5ddSQu Wenruo 		}
1743557ea5ddSQu Wenruo 
1744557ea5ddSQu Wenruo 		if (btrfs_comp_cpu_keys(&key, &next_key) >= 0) {
174586a6be3aSDavid Sterba 			generic_err(node, slot,
1746bba4f298SQu Wenruo 	"bad key order, current (%llu %u %llu) next (%llu %u %llu)",
1747bba4f298SQu Wenruo 				key.objectid, key.type, key.offset,
1748bba4f298SQu Wenruo 				next_key.objectid, next_key.type,
1749bba4f298SQu Wenruo 				next_key.offset);
1750bba4f298SQu Wenruo 			ret = -EUCLEAN;
1751557ea5ddSQu Wenruo 			goto out;
1752557ea5ddSQu Wenruo 		}
1753557ea5ddSQu Wenruo 	}
1754557ea5ddSQu Wenruo out:
1755557ea5ddSQu Wenruo 	return ret;
1756557ea5ddSQu Wenruo }
175702529d7aSQu Wenruo ALLOW_ERROR_INJECTION(btrfs_check_node, ERRNO);
1758