xref: /openbmc/linux/fs/btrfs/tree-checker.c (revision c3053ebb0b8044d8360db0f6717f8fdcbde8c370)
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 
376ce4252c0SDavid Sterba static int check_dir_item(struct extent_buffer *leaf,
377c18679ebSQu Wenruo 			  struct btrfs_key *key, struct btrfs_key *prev_key,
378c18679ebSQu Wenruo 			  int slot)
379ad7b0368SQu Wenruo {
380ce4252c0SDavid Sterba 	struct btrfs_fs_info *fs_info = leaf->fs_info;
381ad7b0368SQu Wenruo 	struct btrfs_dir_item *di;
382ad7b0368SQu Wenruo 	u32 item_size = btrfs_item_size_nr(leaf, slot);
383ad7b0368SQu Wenruo 	u32 cur = 0;
384ad7b0368SQu Wenruo 
38580d7fd1eSQu Wenruo 	if (!check_prev_ino(leaf, key, slot, prev_key))
386c18679ebSQu Wenruo 		return -EUCLEAN;
387ad7b0368SQu Wenruo 	di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
388ad7b0368SQu Wenruo 	while (cur < item_size) {
389ad7b0368SQu Wenruo 		u32 name_len;
390ad7b0368SQu Wenruo 		u32 data_len;
391ad7b0368SQu Wenruo 		u32 max_name_len;
392ad7b0368SQu Wenruo 		u32 total_size;
393ad7b0368SQu Wenruo 		u32 name_hash;
394ad7b0368SQu Wenruo 		u8 dir_type;
395ad7b0368SQu Wenruo 
396ad7b0368SQu Wenruo 		/* header itself should not cross item boundary */
397ad7b0368SQu Wenruo 		if (cur + sizeof(*di) > item_size) {
398d98ced68SDavid Sterba 			dir_item_err(leaf, slot,
3997cfad652SArnd Bergmann 		"dir item header crosses item boundary, have %zu boundary %u",
400ad7b0368SQu Wenruo 				cur + sizeof(*di), item_size);
401ad7b0368SQu Wenruo 			return -EUCLEAN;
402ad7b0368SQu Wenruo 		}
403ad7b0368SQu Wenruo 
404ad7b0368SQu Wenruo 		/* dir type check */
405ad7b0368SQu Wenruo 		dir_type = btrfs_dir_type(leaf, di);
406ad7b0368SQu Wenruo 		if (dir_type >= BTRFS_FT_MAX) {
407d98ced68SDavid Sterba 			dir_item_err(leaf, slot,
408ad7b0368SQu Wenruo 			"invalid dir item type, have %u expect [0, %u)",
409ad7b0368SQu Wenruo 				dir_type, BTRFS_FT_MAX);
410ad7b0368SQu Wenruo 			return -EUCLEAN;
411ad7b0368SQu Wenruo 		}
412ad7b0368SQu Wenruo 
413ad7b0368SQu Wenruo 		if (key->type == BTRFS_XATTR_ITEM_KEY &&
414ad7b0368SQu Wenruo 		    dir_type != BTRFS_FT_XATTR) {
415d98ced68SDavid Sterba 			dir_item_err(leaf, slot,
416ad7b0368SQu Wenruo 		"invalid dir item type for XATTR key, have %u expect %u",
417ad7b0368SQu Wenruo 				dir_type, BTRFS_FT_XATTR);
418ad7b0368SQu Wenruo 			return -EUCLEAN;
419ad7b0368SQu Wenruo 		}
420ad7b0368SQu Wenruo 		if (dir_type == BTRFS_FT_XATTR &&
421ad7b0368SQu Wenruo 		    key->type != BTRFS_XATTR_ITEM_KEY) {
422d98ced68SDavid Sterba 			dir_item_err(leaf, slot,
423ad7b0368SQu Wenruo 			"xattr dir type found for non-XATTR key");
424ad7b0368SQu Wenruo 			return -EUCLEAN;
425ad7b0368SQu Wenruo 		}
426ad7b0368SQu Wenruo 		if (dir_type == BTRFS_FT_XATTR)
427ad7b0368SQu Wenruo 			max_name_len = XATTR_NAME_MAX;
428ad7b0368SQu Wenruo 		else
429ad7b0368SQu Wenruo 			max_name_len = BTRFS_NAME_LEN;
430ad7b0368SQu Wenruo 
431ad7b0368SQu Wenruo 		/* Name/data length check */
432ad7b0368SQu Wenruo 		name_len = btrfs_dir_name_len(leaf, di);
433ad7b0368SQu Wenruo 		data_len = btrfs_dir_data_len(leaf, di);
434ad7b0368SQu Wenruo 		if (name_len > max_name_len) {
435d98ced68SDavid Sterba 			dir_item_err(leaf, slot,
436ad7b0368SQu Wenruo 			"dir item name len too long, have %u max %u",
437ad7b0368SQu Wenruo 				name_len, max_name_len);
438ad7b0368SQu Wenruo 			return -EUCLEAN;
439ad7b0368SQu Wenruo 		}
4402f659546SQu Wenruo 		if (name_len + data_len > BTRFS_MAX_XATTR_SIZE(fs_info)) {
441d98ced68SDavid Sterba 			dir_item_err(leaf, slot,
442ad7b0368SQu Wenruo 			"dir item name and data len too long, have %u max %u",
443ad7b0368SQu Wenruo 				name_len + data_len,
4442f659546SQu Wenruo 				BTRFS_MAX_XATTR_SIZE(fs_info));
445ad7b0368SQu Wenruo 			return -EUCLEAN;
446ad7b0368SQu Wenruo 		}
447ad7b0368SQu Wenruo 
448ad7b0368SQu Wenruo 		if (data_len && dir_type != BTRFS_FT_XATTR) {
449d98ced68SDavid Sterba 			dir_item_err(leaf, slot,
450ad7b0368SQu Wenruo 			"dir item with invalid data len, have %u expect 0",
451ad7b0368SQu Wenruo 				data_len);
452ad7b0368SQu Wenruo 			return -EUCLEAN;
453ad7b0368SQu Wenruo 		}
454ad7b0368SQu Wenruo 
455ad7b0368SQu Wenruo 		total_size = sizeof(*di) + name_len + data_len;
456ad7b0368SQu Wenruo 
457ad7b0368SQu Wenruo 		/* header and name/data should not cross item boundary */
458ad7b0368SQu Wenruo 		if (cur + total_size > item_size) {
459d98ced68SDavid Sterba 			dir_item_err(leaf, slot,
460ad7b0368SQu Wenruo 		"dir item data crosses item boundary, have %u boundary %u",
461ad7b0368SQu Wenruo 				cur + total_size, item_size);
462ad7b0368SQu Wenruo 			return -EUCLEAN;
463ad7b0368SQu Wenruo 		}
464ad7b0368SQu Wenruo 
465ad7b0368SQu Wenruo 		/*
466ad7b0368SQu Wenruo 		 * Special check for XATTR/DIR_ITEM, as key->offset is name
467ad7b0368SQu Wenruo 		 * hash, should match its name
468ad7b0368SQu Wenruo 		 */
469ad7b0368SQu Wenruo 		if (key->type == BTRFS_DIR_ITEM_KEY ||
470ad7b0368SQu Wenruo 		    key->type == BTRFS_XATTR_ITEM_KEY) {
471e2683fc9SDavid Sterba 			char namebuf[max(BTRFS_NAME_LEN, XATTR_NAME_MAX)];
472e2683fc9SDavid Sterba 
473ad7b0368SQu Wenruo 			read_extent_buffer(leaf, namebuf,
474ad7b0368SQu Wenruo 					(unsigned long)(di + 1), name_len);
475ad7b0368SQu Wenruo 			name_hash = btrfs_name_hash(namebuf, name_len);
476ad7b0368SQu Wenruo 			if (key->offset != name_hash) {
477d98ced68SDavid Sterba 				dir_item_err(leaf, slot,
478ad7b0368SQu Wenruo 		"name hash mismatch with key, have 0x%016x expect 0x%016llx",
479ad7b0368SQu Wenruo 					name_hash, key->offset);
480ad7b0368SQu Wenruo 				return -EUCLEAN;
481ad7b0368SQu Wenruo 			}
482ad7b0368SQu Wenruo 		}
483ad7b0368SQu Wenruo 		cur += total_size;
484ad7b0368SQu Wenruo 		di = (struct btrfs_dir_item *)((void *)di + total_size);
485ad7b0368SQu Wenruo 	}
486ad7b0368SQu Wenruo 	return 0;
487ad7b0368SQu Wenruo }
488ad7b0368SQu Wenruo 
4894806bd88SDavid Sterba __printf(3, 4)
490fce466eaSQu Wenruo __cold
4914806bd88SDavid Sterba static void block_group_err(const struct extent_buffer *eb, int slot,
492fce466eaSQu Wenruo 			    const char *fmt, ...)
493fce466eaSQu Wenruo {
4944806bd88SDavid Sterba 	const struct btrfs_fs_info *fs_info = eb->fs_info;
495fce466eaSQu Wenruo 	struct btrfs_key key;
496fce466eaSQu Wenruo 	struct va_format vaf;
497fce466eaSQu Wenruo 	va_list args;
498fce466eaSQu Wenruo 
499fce466eaSQu Wenruo 	btrfs_item_key_to_cpu(eb, &key, slot);
500fce466eaSQu Wenruo 	va_start(args, fmt);
501fce466eaSQu Wenruo 
502fce466eaSQu Wenruo 	vaf.fmt = fmt;
503fce466eaSQu Wenruo 	vaf.va = &args;
504fce466eaSQu Wenruo 
505fce466eaSQu Wenruo 	btrfs_crit(fs_info,
506fce466eaSQu Wenruo 	"corrupt %s: root=%llu block=%llu slot=%d bg_start=%llu bg_len=%llu, %pV",
507fce466eaSQu Wenruo 		btrfs_header_level(eb) == 0 ? "leaf" : "node",
508fce466eaSQu Wenruo 		btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot,
509fce466eaSQu Wenruo 		key.objectid, key.offset, &vaf);
510fce466eaSQu Wenruo 	va_end(args);
511fce466eaSQu Wenruo }
512fce466eaSQu Wenruo 
513af60ce2bSDavid Sterba static int check_block_group_item(struct extent_buffer *leaf,
514fce466eaSQu Wenruo 				  struct btrfs_key *key, int slot)
515fce466eaSQu Wenruo {
516fce466eaSQu Wenruo 	struct btrfs_block_group_item bgi;
517fce466eaSQu Wenruo 	u32 item_size = btrfs_item_size_nr(leaf, slot);
518fce466eaSQu Wenruo 	u64 flags;
519fce466eaSQu Wenruo 	u64 type;
520fce466eaSQu Wenruo 
521fce466eaSQu Wenruo 	/*
522fce466eaSQu Wenruo 	 * Here we don't really care about alignment since extent allocator can
52310950929SQu Wenruo 	 * handle it.  We care more about the size.
524fce466eaSQu Wenruo 	 */
52510950929SQu Wenruo 	if (key->offset == 0) {
5264806bd88SDavid Sterba 		block_group_err(leaf, slot,
52710950929SQu Wenruo 				"invalid block group size 0");
528fce466eaSQu Wenruo 		return -EUCLEAN;
529fce466eaSQu Wenruo 	}
530fce466eaSQu Wenruo 
531fce466eaSQu Wenruo 	if (item_size != sizeof(bgi)) {
5324806bd88SDavid Sterba 		block_group_err(leaf, slot,
533fce466eaSQu Wenruo 			"invalid item size, have %u expect %zu",
534fce466eaSQu Wenruo 				item_size, sizeof(bgi));
535fce466eaSQu Wenruo 		return -EUCLEAN;
536fce466eaSQu Wenruo 	}
537fce466eaSQu Wenruo 
538fce466eaSQu Wenruo 	read_extent_buffer(leaf, &bgi, btrfs_item_ptr_offset(leaf, slot),
539fce466eaSQu Wenruo 			   sizeof(bgi));
540de0dc456SDavid Sterba 	if (btrfs_stack_block_group_chunk_objectid(&bgi) !=
541fce466eaSQu Wenruo 	    BTRFS_FIRST_CHUNK_TREE_OBJECTID) {
5424806bd88SDavid Sterba 		block_group_err(leaf, slot,
543fce466eaSQu Wenruo 		"invalid block group chunk objectid, have %llu expect %llu",
544de0dc456SDavid Sterba 				btrfs_stack_block_group_chunk_objectid(&bgi),
545fce466eaSQu Wenruo 				BTRFS_FIRST_CHUNK_TREE_OBJECTID);
546fce466eaSQu Wenruo 		return -EUCLEAN;
547fce466eaSQu Wenruo 	}
548fce466eaSQu Wenruo 
549de0dc456SDavid Sterba 	if (btrfs_stack_block_group_used(&bgi) > key->offset) {
5504806bd88SDavid Sterba 		block_group_err(leaf, slot,
551fce466eaSQu Wenruo 			"invalid block group used, have %llu expect [0, %llu)",
552de0dc456SDavid Sterba 				btrfs_stack_block_group_used(&bgi), key->offset);
553fce466eaSQu Wenruo 		return -EUCLEAN;
554fce466eaSQu Wenruo 	}
555fce466eaSQu Wenruo 
556de0dc456SDavid Sterba 	flags = btrfs_stack_block_group_flags(&bgi);
557fce466eaSQu Wenruo 	if (hweight64(flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) > 1) {
5584806bd88SDavid Sterba 		block_group_err(leaf, slot,
559fce466eaSQu Wenruo "invalid profile flags, have 0x%llx (%lu bits set) expect no more than 1 bit set",
560fce466eaSQu Wenruo 			flags & BTRFS_BLOCK_GROUP_PROFILE_MASK,
561fce466eaSQu Wenruo 			hweight64(flags & BTRFS_BLOCK_GROUP_PROFILE_MASK));
562fce466eaSQu Wenruo 		return -EUCLEAN;
563fce466eaSQu Wenruo 	}
564fce466eaSQu Wenruo 
565fce466eaSQu Wenruo 	type = flags & BTRFS_BLOCK_GROUP_TYPE_MASK;
566fce466eaSQu Wenruo 	if (type != BTRFS_BLOCK_GROUP_DATA &&
567fce466eaSQu Wenruo 	    type != BTRFS_BLOCK_GROUP_METADATA &&
568fce466eaSQu Wenruo 	    type != BTRFS_BLOCK_GROUP_SYSTEM &&
569fce466eaSQu Wenruo 	    type != (BTRFS_BLOCK_GROUP_METADATA |
570fce466eaSQu Wenruo 			   BTRFS_BLOCK_GROUP_DATA)) {
5714806bd88SDavid Sterba 		block_group_err(leaf, slot,
572761333f2SShaokun Zhang "invalid type, have 0x%llx (%lu bits set) expect either 0x%llx, 0x%llx, 0x%llx or 0x%llx",
573fce466eaSQu Wenruo 			type, hweight64(type),
574fce466eaSQu Wenruo 			BTRFS_BLOCK_GROUP_DATA, BTRFS_BLOCK_GROUP_METADATA,
575fce466eaSQu Wenruo 			BTRFS_BLOCK_GROUP_SYSTEM,
576fce466eaSQu Wenruo 			BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA);
577fce466eaSQu Wenruo 		return -EUCLEAN;
578fce466eaSQu Wenruo 	}
579fce466eaSQu Wenruo 	return 0;
580fce466eaSQu Wenruo }
581fce466eaSQu Wenruo 
582d001e4a3SDavid Sterba __printf(4, 5)
583f1140243SQu Wenruo __cold
584d001e4a3SDavid Sterba static void chunk_err(const struct extent_buffer *leaf,
585f1140243SQu Wenruo 		      const struct btrfs_chunk *chunk, u64 logical,
586f1140243SQu Wenruo 		      const char *fmt, ...)
587f1140243SQu Wenruo {
588d001e4a3SDavid Sterba 	const struct btrfs_fs_info *fs_info = leaf->fs_info;
589f1140243SQu Wenruo 	bool is_sb;
590f1140243SQu Wenruo 	struct va_format vaf;
591f1140243SQu Wenruo 	va_list args;
592f1140243SQu Wenruo 	int i;
593f1140243SQu Wenruo 	int slot = -1;
594f1140243SQu Wenruo 
595f1140243SQu Wenruo 	/* Only superblock eb is able to have such small offset */
596f1140243SQu Wenruo 	is_sb = (leaf->start == BTRFS_SUPER_INFO_OFFSET);
597f1140243SQu Wenruo 
598f1140243SQu Wenruo 	if (!is_sb) {
599f1140243SQu Wenruo 		/*
600f1140243SQu Wenruo 		 * Get the slot number by iterating through all slots, this
601f1140243SQu Wenruo 		 * would provide better readability.
602f1140243SQu Wenruo 		 */
603f1140243SQu Wenruo 		for (i = 0; i < btrfs_header_nritems(leaf); i++) {
604f1140243SQu Wenruo 			if (btrfs_item_ptr_offset(leaf, i) ==
605f1140243SQu Wenruo 					(unsigned long)chunk) {
606f1140243SQu Wenruo 				slot = i;
607f1140243SQu Wenruo 				break;
608f1140243SQu Wenruo 			}
609f1140243SQu Wenruo 		}
610f1140243SQu Wenruo 	}
611f1140243SQu Wenruo 	va_start(args, fmt);
612f1140243SQu Wenruo 	vaf.fmt = fmt;
613f1140243SQu Wenruo 	vaf.va = &args;
614f1140243SQu Wenruo 
615f1140243SQu Wenruo 	if (is_sb)
616f1140243SQu Wenruo 		btrfs_crit(fs_info,
617f1140243SQu Wenruo 		"corrupt superblock syschunk array: chunk_start=%llu, %pV",
618f1140243SQu Wenruo 			   logical, &vaf);
619f1140243SQu Wenruo 	else
620f1140243SQu Wenruo 		btrfs_crit(fs_info,
621f1140243SQu Wenruo 	"corrupt leaf: root=%llu block=%llu slot=%d chunk_start=%llu, %pV",
622f1140243SQu Wenruo 			   BTRFS_CHUNK_TREE_OBJECTID, leaf->start, slot,
623f1140243SQu Wenruo 			   logical, &vaf);
624f1140243SQu Wenruo 	va_end(args);
625f1140243SQu Wenruo }
626f1140243SQu Wenruo 
627ad7b0368SQu Wenruo /*
62882fc28fbSQu Wenruo  * The common chunk check which could also work on super block sys chunk array.
62982fc28fbSQu Wenruo  *
630bf871c3bSQu Wenruo  * Return -EUCLEAN if anything is corrupted.
63182fc28fbSQu Wenruo  * Return 0 if everything is OK.
63282fc28fbSQu Wenruo  */
633ddaf1d5aSDavid Sterba int btrfs_check_chunk_valid(struct extent_buffer *leaf,
63482fc28fbSQu Wenruo 			    struct btrfs_chunk *chunk, u64 logical)
63582fc28fbSQu Wenruo {
636ddaf1d5aSDavid Sterba 	struct btrfs_fs_info *fs_info = leaf->fs_info;
63782fc28fbSQu Wenruo 	u64 length;
63882fc28fbSQu Wenruo 	u64 stripe_len;
63982fc28fbSQu Wenruo 	u16 num_stripes;
64082fc28fbSQu Wenruo 	u16 sub_stripes;
64182fc28fbSQu Wenruo 	u64 type;
64282fc28fbSQu Wenruo 	u64 features;
64382fc28fbSQu Wenruo 	bool mixed = false;
64482fc28fbSQu Wenruo 
64582fc28fbSQu Wenruo 	length = btrfs_chunk_length(leaf, chunk);
64682fc28fbSQu Wenruo 	stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
64782fc28fbSQu Wenruo 	num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
64882fc28fbSQu Wenruo 	sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
64982fc28fbSQu Wenruo 	type = btrfs_chunk_type(leaf, chunk);
65082fc28fbSQu Wenruo 
65182fc28fbSQu Wenruo 	if (!num_stripes) {
652d001e4a3SDavid Sterba 		chunk_err(leaf, chunk, logical,
653f1140243SQu Wenruo 			  "invalid chunk num_stripes, have %u", num_stripes);
654bf871c3bSQu Wenruo 		return -EUCLEAN;
65582fc28fbSQu Wenruo 	}
65682fc28fbSQu Wenruo 	if (!IS_ALIGNED(logical, fs_info->sectorsize)) {
657d001e4a3SDavid Sterba 		chunk_err(leaf, chunk, logical,
658f1140243SQu Wenruo 		"invalid chunk logical, have %llu should aligned to %u",
659f1140243SQu Wenruo 			  logical, fs_info->sectorsize);
660bf871c3bSQu Wenruo 		return -EUCLEAN;
66182fc28fbSQu Wenruo 	}
66282fc28fbSQu Wenruo 	if (btrfs_chunk_sector_size(leaf, chunk) != fs_info->sectorsize) {
663d001e4a3SDavid Sterba 		chunk_err(leaf, chunk, logical,
664f1140243SQu Wenruo 			  "invalid chunk sectorsize, have %u expect %u",
665f1140243SQu Wenruo 			  btrfs_chunk_sector_size(leaf, chunk),
666f1140243SQu Wenruo 			  fs_info->sectorsize);
667bf871c3bSQu Wenruo 		return -EUCLEAN;
66882fc28fbSQu Wenruo 	}
66982fc28fbSQu Wenruo 	if (!length || !IS_ALIGNED(length, fs_info->sectorsize)) {
670d001e4a3SDavid Sterba 		chunk_err(leaf, chunk, logical,
671f1140243SQu Wenruo 			  "invalid chunk length, have %llu", length);
672bf871c3bSQu Wenruo 		return -EUCLEAN;
67382fc28fbSQu Wenruo 	}
67482fc28fbSQu Wenruo 	if (!is_power_of_2(stripe_len) || stripe_len != BTRFS_STRIPE_LEN) {
675d001e4a3SDavid Sterba 		chunk_err(leaf, chunk, logical,
676f1140243SQu Wenruo 			  "invalid chunk stripe length: %llu",
67782fc28fbSQu Wenruo 			  stripe_len);
678bf871c3bSQu Wenruo 		return -EUCLEAN;
67982fc28fbSQu Wenruo 	}
68082fc28fbSQu Wenruo 	if (~(BTRFS_BLOCK_GROUP_TYPE_MASK | BTRFS_BLOCK_GROUP_PROFILE_MASK) &
68182fc28fbSQu Wenruo 	    type) {
682d001e4a3SDavid Sterba 		chunk_err(leaf, chunk, logical,
683f1140243SQu Wenruo 			  "unrecognized chunk type: 0x%llx",
68482fc28fbSQu Wenruo 			  ~(BTRFS_BLOCK_GROUP_TYPE_MASK |
68582fc28fbSQu Wenruo 			    BTRFS_BLOCK_GROUP_PROFILE_MASK) &
68682fc28fbSQu Wenruo 			  btrfs_chunk_type(leaf, chunk));
687bf871c3bSQu Wenruo 		return -EUCLEAN;
68882fc28fbSQu Wenruo 	}
68982fc28fbSQu Wenruo 
690c1499166SDavid Sterba 	if (!has_single_bit_set(type & BTRFS_BLOCK_GROUP_PROFILE_MASK) &&
69180e46cf2SQu Wenruo 	    (type & BTRFS_BLOCK_GROUP_PROFILE_MASK) != 0) {
692d001e4a3SDavid Sterba 		chunk_err(leaf, chunk, logical,
69380e46cf2SQu Wenruo 		"invalid chunk profile flag: 0x%llx, expect 0 or 1 bit set",
69480e46cf2SQu Wenruo 			  type & BTRFS_BLOCK_GROUP_PROFILE_MASK);
69580e46cf2SQu Wenruo 		return -EUCLEAN;
69680e46cf2SQu Wenruo 	}
69782fc28fbSQu Wenruo 	if ((type & BTRFS_BLOCK_GROUP_TYPE_MASK) == 0) {
698d001e4a3SDavid Sterba 		chunk_err(leaf, chunk, logical,
699f1140243SQu Wenruo 	"missing chunk type flag, have 0x%llx one bit must be set in 0x%llx",
700f1140243SQu Wenruo 			  type, BTRFS_BLOCK_GROUP_TYPE_MASK);
701bf871c3bSQu Wenruo 		return -EUCLEAN;
70282fc28fbSQu Wenruo 	}
70382fc28fbSQu Wenruo 
70482fc28fbSQu Wenruo 	if ((type & BTRFS_BLOCK_GROUP_SYSTEM) &&
70582fc28fbSQu Wenruo 	    (type & (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA))) {
706d001e4a3SDavid Sterba 		chunk_err(leaf, chunk, logical,
707f1140243SQu Wenruo 			  "system chunk with data or metadata type: 0x%llx",
708f1140243SQu Wenruo 			  type);
709bf871c3bSQu Wenruo 		return -EUCLEAN;
71082fc28fbSQu Wenruo 	}
71182fc28fbSQu Wenruo 
71282fc28fbSQu Wenruo 	features = btrfs_super_incompat_flags(fs_info->super_copy);
71382fc28fbSQu Wenruo 	if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
71482fc28fbSQu Wenruo 		mixed = true;
71582fc28fbSQu Wenruo 
71682fc28fbSQu Wenruo 	if (!mixed) {
71782fc28fbSQu Wenruo 		if ((type & BTRFS_BLOCK_GROUP_METADATA) &&
71882fc28fbSQu Wenruo 		    (type & BTRFS_BLOCK_GROUP_DATA)) {
719d001e4a3SDavid Sterba 			chunk_err(leaf, chunk, logical,
72082fc28fbSQu Wenruo 			"mixed chunk type in non-mixed mode: 0x%llx", type);
721bf871c3bSQu Wenruo 			return -EUCLEAN;
72282fc28fbSQu Wenruo 		}
72382fc28fbSQu Wenruo 	}
72482fc28fbSQu Wenruo 
72582fc28fbSQu Wenruo 	if ((type & BTRFS_BLOCK_GROUP_RAID10 && sub_stripes != 2) ||
72682fc28fbSQu Wenruo 	    (type & BTRFS_BLOCK_GROUP_RAID1 && num_stripes != 2) ||
72782fc28fbSQu Wenruo 	    (type & BTRFS_BLOCK_GROUP_RAID5 && num_stripes < 2) ||
72882fc28fbSQu Wenruo 	    (type & BTRFS_BLOCK_GROUP_RAID6 && num_stripes < 3) ||
72982fc28fbSQu Wenruo 	    (type & BTRFS_BLOCK_GROUP_DUP && num_stripes != 2) ||
73082fc28fbSQu Wenruo 	    ((type & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 && num_stripes != 1)) {
731d001e4a3SDavid Sterba 		chunk_err(leaf, chunk, logical,
73282fc28fbSQu Wenruo 			"invalid num_stripes:sub_stripes %u:%u for profile %llu",
73382fc28fbSQu Wenruo 			num_stripes, sub_stripes,
73482fc28fbSQu Wenruo 			type & BTRFS_BLOCK_GROUP_PROFILE_MASK);
735bf871c3bSQu Wenruo 		return -EUCLEAN;
73682fc28fbSQu Wenruo 	}
73782fc28fbSQu Wenruo 
73882fc28fbSQu Wenruo 	return 0;
73982fc28fbSQu Wenruo }
74082fc28fbSQu Wenruo 
741f6d2a5c2SQu Wenruo /*
742f6d2a5c2SQu Wenruo  * Enhanced version of chunk item checker.
743f6d2a5c2SQu Wenruo  *
744f6d2a5c2SQu Wenruo  * The common btrfs_check_chunk_valid() doesn't check item size since it needs
745f6d2a5c2SQu Wenruo  * to work on super block sys_chunk_array which doesn't have full item ptr.
746f6d2a5c2SQu Wenruo  */
747f6d2a5c2SQu Wenruo static int check_leaf_chunk_item(struct extent_buffer *leaf,
748f6d2a5c2SQu Wenruo 				 struct btrfs_chunk *chunk,
749f6d2a5c2SQu Wenruo 				 struct btrfs_key *key, int slot)
750f6d2a5c2SQu Wenruo {
751f6d2a5c2SQu Wenruo 	int num_stripes;
752f6d2a5c2SQu Wenruo 
753f6d2a5c2SQu Wenruo 	if (btrfs_item_size_nr(leaf, slot) < sizeof(struct btrfs_chunk)) {
754f6d2a5c2SQu Wenruo 		chunk_err(leaf, chunk, key->offset,
755f6d2a5c2SQu Wenruo 			"invalid chunk item size: have %u expect [%zu, %u)",
756f6d2a5c2SQu Wenruo 			btrfs_item_size_nr(leaf, slot),
757f6d2a5c2SQu Wenruo 			sizeof(struct btrfs_chunk),
758f6d2a5c2SQu Wenruo 			BTRFS_LEAF_DATA_SIZE(leaf->fs_info));
759f6d2a5c2SQu Wenruo 		return -EUCLEAN;
760f6d2a5c2SQu Wenruo 	}
761f6d2a5c2SQu Wenruo 
762f6d2a5c2SQu Wenruo 	num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
763f6d2a5c2SQu Wenruo 	/* Let btrfs_check_chunk_valid() handle this error type */
764f6d2a5c2SQu Wenruo 	if (num_stripes == 0)
765f6d2a5c2SQu Wenruo 		goto out;
766f6d2a5c2SQu Wenruo 
767f6d2a5c2SQu Wenruo 	if (btrfs_chunk_item_size(num_stripes) !=
768f6d2a5c2SQu Wenruo 	    btrfs_item_size_nr(leaf, slot)) {
769f6d2a5c2SQu Wenruo 		chunk_err(leaf, chunk, key->offset,
770f6d2a5c2SQu Wenruo 			"invalid chunk item size: have %u expect %lu",
771f6d2a5c2SQu Wenruo 			btrfs_item_size_nr(leaf, slot),
772f6d2a5c2SQu Wenruo 			btrfs_chunk_item_size(num_stripes));
773f6d2a5c2SQu Wenruo 		return -EUCLEAN;
774f6d2a5c2SQu Wenruo 	}
775f6d2a5c2SQu Wenruo out:
776f6d2a5c2SQu Wenruo 	return btrfs_check_chunk_valid(leaf, chunk, key->offset);
777f6d2a5c2SQu Wenruo }
778f6d2a5c2SQu Wenruo 
7795617ed80SDavid Sterba __printf(3, 4)
780ab4ba2e1SQu Wenruo __cold
7815617ed80SDavid Sterba static void dev_item_err(const struct extent_buffer *eb, int slot,
782ab4ba2e1SQu Wenruo 			 const char *fmt, ...)
783ab4ba2e1SQu Wenruo {
784ab4ba2e1SQu Wenruo 	struct btrfs_key key;
785ab4ba2e1SQu Wenruo 	struct va_format vaf;
786ab4ba2e1SQu Wenruo 	va_list args;
787ab4ba2e1SQu Wenruo 
788ab4ba2e1SQu Wenruo 	btrfs_item_key_to_cpu(eb, &key, slot);
789ab4ba2e1SQu Wenruo 	va_start(args, fmt);
790ab4ba2e1SQu Wenruo 
791ab4ba2e1SQu Wenruo 	vaf.fmt = fmt;
792ab4ba2e1SQu Wenruo 	vaf.va = &args;
793ab4ba2e1SQu Wenruo 
7945617ed80SDavid Sterba 	btrfs_crit(eb->fs_info,
795ab4ba2e1SQu Wenruo 	"corrupt %s: root=%llu block=%llu slot=%d devid=%llu %pV",
796ab4ba2e1SQu Wenruo 		btrfs_header_level(eb) == 0 ? "leaf" : "node",
797ab4ba2e1SQu Wenruo 		btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot,
798ab4ba2e1SQu Wenruo 		key.objectid, &vaf);
799ab4ba2e1SQu Wenruo 	va_end(args);
800ab4ba2e1SQu Wenruo }
801ab4ba2e1SQu Wenruo 
802412a2312SDavid Sterba static int check_dev_item(struct extent_buffer *leaf,
803ab4ba2e1SQu Wenruo 			  struct btrfs_key *key, int slot)
804ab4ba2e1SQu Wenruo {
805ab4ba2e1SQu Wenruo 	struct btrfs_dev_item *ditem;
806ab4ba2e1SQu Wenruo 
807ab4ba2e1SQu Wenruo 	if (key->objectid != BTRFS_DEV_ITEMS_OBJECTID) {
8085617ed80SDavid Sterba 		dev_item_err(leaf, slot,
809ab4ba2e1SQu Wenruo 			     "invalid objectid: has=%llu expect=%llu",
810ab4ba2e1SQu Wenruo 			     key->objectid, BTRFS_DEV_ITEMS_OBJECTID);
811ab4ba2e1SQu Wenruo 		return -EUCLEAN;
812ab4ba2e1SQu Wenruo 	}
813ab4ba2e1SQu Wenruo 	ditem = btrfs_item_ptr(leaf, slot, struct btrfs_dev_item);
814ab4ba2e1SQu Wenruo 	if (btrfs_device_id(leaf, ditem) != key->offset) {
8155617ed80SDavid Sterba 		dev_item_err(leaf, slot,
816ab4ba2e1SQu Wenruo 			     "devid mismatch: key has=%llu item has=%llu",
817ab4ba2e1SQu Wenruo 			     key->offset, btrfs_device_id(leaf, ditem));
818ab4ba2e1SQu Wenruo 		return -EUCLEAN;
819ab4ba2e1SQu Wenruo 	}
820ab4ba2e1SQu Wenruo 
821ab4ba2e1SQu Wenruo 	/*
822ab4ba2e1SQu Wenruo 	 * For device total_bytes, we don't have reliable way to check it, as
823ab4ba2e1SQu Wenruo 	 * it can be 0 for device removal. Device size check can only be done
824ab4ba2e1SQu Wenruo 	 * by dev extents check.
825ab4ba2e1SQu Wenruo 	 */
826ab4ba2e1SQu Wenruo 	if (btrfs_device_bytes_used(leaf, ditem) >
827ab4ba2e1SQu Wenruo 	    btrfs_device_total_bytes(leaf, ditem)) {
8285617ed80SDavid Sterba 		dev_item_err(leaf, slot,
829ab4ba2e1SQu Wenruo 			     "invalid bytes used: have %llu expect [0, %llu]",
830ab4ba2e1SQu Wenruo 			     btrfs_device_bytes_used(leaf, ditem),
831ab4ba2e1SQu Wenruo 			     btrfs_device_total_bytes(leaf, ditem));
832ab4ba2e1SQu Wenruo 		return -EUCLEAN;
833ab4ba2e1SQu Wenruo 	}
834ab4ba2e1SQu Wenruo 	/*
835ab4ba2e1SQu Wenruo 	 * Remaining members like io_align/type/gen/dev_group aren't really
836ab4ba2e1SQu Wenruo 	 * utilized.  Skip them to make later usage of them easier.
837ab4ba2e1SQu Wenruo 	 */
838ab4ba2e1SQu Wenruo 	return 0;
839ab4ba2e1SQu Wenruo }
840ab4ba2e1SQu Wenruo 
841496245caSQu Wenruo /* Inode item error output has the same format as dir_item_err() */
842*c3053ebbSQu Wenruo #define inode_item_err(eb, slot, fmt, ...)			\
843d98ced68SDavid Sterba 	dir_item_err(eb, slot, fmt, __VA_ARGS__)
844496245caSQu Wenruo 
84539e57f49SDavid Sterba static int check_inode_item(struct extent_buffer *leaf,
846496245caSQu Wenruo 			    struct btrfs_key *key, int slot)
847496245caSQu Wenruo {
84839e57f49SDavid Sterba 	struct btrfs_fs_info *fs_info = leaf->fs_info;
849496245caSQu Wenruo 	struct btrfs_inode_item *iitem;
850496245caSQu Wenruo 	u64 super_gen = btrfs_super_generation(fs_info->super_copy);
851496245caSQu Wenruo 	u32 valid_mask = (S_IFMT | S_ISUID | S_ISGID | S_ISVTX | 0777);
852496245caSQu Wenruo 	u32 mode;
853496245caSQu Wenruo 
854496245caSQu Wenruo 	if ((key->objectid < BTRFS_FIRST_FREE_OBJECTID ||
855496245caSQu Wenruo 	     key->objectid > BTRFS_LAST_FREE_OBJECTID) &&
856496245caSQu Wenruo 	    key->objectid != BTRFS_ROOT_TREE_DIR_OBJECTID &&
857496245caSQu Wenruo 	    key->objectid != BTRFS_FREE_INO_OBJECTID) {
85886a6be3aSDavid Sterba 		generic_err(leaf, slot,
859496245caSQu Wenruo 	"invalid key objectid: has %llu expect %llu or [%llu, %llu] or %llu",
860496245caSQu Wenruo 			    key->objectid, BTRFS_ROOT_TREE_DIR_OBJECTID,
861496245caSQu Wenruo 			    BTRFS_FIRST_FREE_OBJECTID,
862496245caSQu Wenruo 			    BTRFS_LAST_FREE_OBJECTID,
863496245caSQu Wenruo 			    BTRFS_FREE_INO_OBJECTID);
864496245caSQu Wenruo 		return -EUCLEAN;
865496245caSQu Wenruo 	}
866496245caSQu Wenruo 	if (key->offset != 0) {
867*c3053ebbSQu Wenruo 		inode_item_err(leaf, slot,
868496245caSQu Wenruo 			"invalid key offset: has %llu expect 0",
869496245caSQu Wenruo 			key->offset);
870496245caSQu Wenruo 		return -EUCLEAN;
871496245caSQu Wenruo 	}
872496245caSQu Wenruo 	iitem = btrfs_item_ptr(leaf, slot, struct btrfs_inode_item);
873496245caSQu Wenruo 
874496245caSQu Wenruo 	/* Here we use super block generation + 1 to handle log tree */
875496245caSQu Wenruo 	if (btrfs_inode_generation(leaf, iitem) > super_gen + 1) {
876*c3053ebbSQu Wenruo 		inode_item_err(leaf, slot,
877496245caSQu Wenruo 			"invalid inode generation: has %llu expect (0, %llu]",
878496245caSQu Wenruo 			       btrfs_inode_generation(leaf, iitem),
879496245caSQu Wenruo 			       super_gen + 1);
880496245caSQu Wenruo 		return -EUCLEAN;
881496245caSQu Wenruo 	}
882496245caSQu Wenruo 	/* Note for ROOT_TREE_DIR_ITEM, mkfs could set its transid 0 */
883496245caSQu Wenruo 	if (btrfs_inode_transid(leaf, iitem) > super_gen + 1) {
884*c3053ebbSQu Wenruo 		inode_item_err(leaf, slot,
885496245caSQu Wenruo 			"invalid inode generation: has %llu expect [0, %llu]",
886496245caSQu Wenruo 			       btrfs_inode_transid(leaf, iitem), super_gen + 1);
887496245caSQu Wenruo 		return -EUCLEAN;
888496245caSQu Wenruo 	}
889496245caSQu Wenruo 
890496245caSQu Wenruo 	/*
891496245caSQu Wenruo 	 * For size and nbytes it's better not to be too strict, as for dir
892496245caSQu Wenruo 	 * item its size/nbytes can easily get wrong, but doesn't affect
893496245caSQu Wenruo 	 * anything in the fs. So here we skip the check.
894496245caSQu Wenruo 	 */
895496245caSQu Wenruo 	mode = btrfs_inode_mode(leaf, iitem);
896496245caSQu Wenruo 	if (mode & ~valid_mask) {
897*c3053ebbSQu Wenruo 		inode_item_err(leaf, slot,
898496245caSQu Wenruo 			       "unknown mode bit detected: 0x%x",
899496245caSQu Wenruo 			       mode & ~valid_mask);
900496245caSQu Wenruo 		return -EUCLEAN;
901496245caSQu Wenruo 	}
902496245caSQu Wenruo 
903496245caSQu Wenruo 	/*
904c1499166SDavid Sterba 	 * S_IFMT is not bit mapped so we can't completely rely on
905c1499166SDavid Sterba 	 * is_power_of_2/has_single_bit_set, but it can save us from checking
906c1499166SDavid Sterba 	 * FIFO/CHR/DIR/REG.  Only needs to check BLK, LNK and SOCKS
907496245caSQu Wenruo 	 */
908c1499166SDavid Sterba 	if (!has_single_bit_set(mode & S_IFMT)) {
909496245caSQu Wenruo 		if (!S_ISLNK(mode) && !S_ISBLK(mode) && !S_ISSOCK(mode)) {
910*c3053ebbSQu Wenruo 			inode_item_err(leaf, slot,
911496245caSQu Wenruo 			"invalid mode: has 0%o expect valid S_IF* bit(s)",
912496245caSQu Wenruo 				       mode & S_IFMT);
913496245caSQu Wenruo 			return -EUCLEAN;
914496245caSQu Wenruo 		}
915496245caSQu Wenruo 	}
916496245caSQu Wenruo 	if (S_ISDIR(mode) && btrfs_inode_nlink(leaf, iitem) > 1) {
917*c3053ebbSQu Wenruo 		inode_item_err(leaf, slot,
918496245caSQu Wenruo 		       "invalid nlink: has %u expect no more than 1 for dir",
919496245caSQu Wenruo 			btrfs_inode_nlink(leaf, iitem));
920496245caSQu Wenruo 		return -EUCLEAN;
921496245caSQu Wenruo 	}
922496245caSQu Wenruo 	if (btrfs_inode_flags(leaf, iitem) & ~BTRFS_INODE_FLAG_MASK) {
923*c3053ebbSQu Wenruo 		inode_item_err(leaf, slot,
924496245caSQu Wenruo 			       "unknown flags detected: 0x%llx",
925496245caSQu Wenruo 			       btrfs_inode_flags(leaf, iitem) &
926496245caSQu Wenruo 			       ~BTRFS_INODE_FLAG_MASK);
927496245caSQu Wenruo 		return -EUCLEAN;
928496245caSQu Wenruo 	}
929496245caSQu Wenruo 	return 0;
930496245caSQu Wenruo }
931496245caSQu Wenruo 
932259ee775SQu Wenruo static int check_root_item(struct extent_buffer *leaf, struct btrfs_key *key,
933259ee775SQu Wenruo 			   int slot)
934259ee775SQu Wenruo {
935259ee775SQu Wenruo 	struct btrfs_fs_info *fs_info = leaf->fs_info;
936259ee775SQu Wenruo 	struct btrfs_root_item ri;
937259ee775SQu Wenruo 	const u64 valid_root_flags = BTRFS_ROOT_SUBVOL_RDONLY |
938259ee775SQu Wenruo 				     BTRFS_ROOT_SUBVOL_DEAD;
939259ee775SQu Wenruo 
940259ee775SQu Wenruo 	/* No such tree id */
941259ee775SQu Wenruo 	if (key->objectid == 0) {
942259ee775SQu Wenruo 		generic_err(leaf, slot, "invalid root id 0");
943259ee775SQu Wenruo 		return -EUCLEAN;
944259ee775SQu Wenruo 	}
945259ee775SQu Wenruo 
946259ee775SQu Wenruo 	/*
947259ee775SQu Wenruo 	 * Some older kernel may create ROOT_ITEM with non-zero offset, so here
948259ee775SQu Wenruo 	 * we only check offset for reloc tree whose key->offset must be a
949259ee775SQu Wenruo 	 * valid tree.
950259ee775SQu Wenruo 	 */
951259ee775SQu Wenruo 	if (key->objectid == BTRFS_TREE_RELOC_OBJECTID && key->offset == 0) {
952259ee775SQu Wenruo 		generic_err(leaf, slot, "invalid root id 0 for reloc tree");
953259ee775SQu Wenruo 		return -EUCLEAN;
954259ee775SQu Wenruo 	}
955259ee775SQu Wenruo 
956259ee775SQu Wenruo 	if (btrfs_item_size_nr(leaf, slot) != sizeof(ri)) {
957259ee775SQu Wenruo 		generic_err(leaf, slot,
958259ee775SQu Wenruo 			    "invalid root item size, have %u expect %zu",
959259ee775SQu Wenruo 			    btrfs_item_size_nr(leaf, slot), sizeof(ri));
960259ee775SQu Wenruo 	}
961259ee775SQu Wenruo 
962259ee775SQu Wenruo 	read_extent_buffer(leaf, &ri, btrfs_item_ptr_offset(leaf, slot),
963259ee775SQu Wenruo 			   sizeof(ri));
964259ee775SQu Wenruo 
965259ee775SQu Wenruo 	/* Generation related */
966259ee775SQu Wenruo 	if (btrfs_root_generation(&ri) >
967259ee775SQu Wenruo 	    btrfs_super_generation(fs_info->super_copy) + 1) {
968259ee775SQu Wenruo 		generic_err(leaf, slot,
969259ee775SQu Wenruo 			"invalid root generation, have %llu expect (0, %llu]",
970259ee775SQu Wenruo 			    btrfs_root_generation(&ri),
971259ee775SQu Wenruo 			    btrfs_super_generation(fs_info->super_copy) + 1);
972259ee775SQu Wenruo 		return -EUCLEAN;
973259ee775SQu Wenruo 	}
974259ee775SQu Wenruo 	if (btrfs_root_generation_v2(&ri) >
975259ee775SQu Wenruo 	    btrfs_super_generation(fs_info->super_copy) + 1) {
976259ee775SQu Wenruo 		generic_err(leaf, slot,
977259ee775SQu Wenruo 		"invalid root v2 generation, have %llu expect (0, %llu]",
978259ee775SQu Wenruo 			    btrfs_root_generation_v2(&ri),
979259ee775SQu Wenruo 			    btrfs_super_generation(fs_info->super_copy) + 1);
980259ee775SQu Wenruo 		return -EUCLEAN;
981259ee775SQu Wenruo 	}
982259ee775SQu Wenruo 	if (btrfs_root_last_snapshot(&ri) >
983259ee775SQu Wenruo 	    btrfs_super_generation(fs_info->super_copy) + 1) {
984259ee775SQu Wenruo 		generic_err(leaf, slot,
985259ee775SQu Wenruo 		"invalid root last_snapshot, have %llu expect (0, %llu]",
986259ee775SQu Wenruo 			    btrfs_root_last_snapshot(&ri),
987259ee775SQu Wenruo 			    btrfs_super_generation(fs_info->super_copy) + 1);
988259ee775SQu Wenruo 		return -EUCLEAN;
989259ee775SQu Wenruo 	}
990259ee775SQu Wenruo 
991259ee775SQu Wenruo 	/* Alignment and level check */
992259ee775SQu Wenruo 	if (!IS_ALIGNED(btrfs_root_bytenr(&ri), fs_info->sectorsize)) {
993259ee775SQu Wenruo 		generic_err(leaf, slot,
994259ee775SQu Wenruo 		"invalid root bytenr, have %llu expect to be aligned to %u",
995259ee775SQu Wenruo 			    btrfs_root_bytenr(&ri), fs_info->sectorsize);
996259ee775SQu Wenruo 		return -EUCLEAN;
997259ee775SQu Wenruo 	}
998259ee775SQu Wenruo 	if (btrfs_root_level(&ri) >= BTRFS_MAX_LEVEL) {
999259ee775SQu Wenruo 		generic_err(leaf, slot,
1000259ee775SQu Wenruo 			    "invalid root level, have %u expect [0, %u]",
1001259ee775SQu Wenruo 			    btrfs_root_level(&ri), BTRFS_MAX_LEVEL - 1);
1002259ee775SQu Wenruo 		return -EUCLEAN;
1003259ee775SQu Wenruo 	}
1004259ee775SQu Wenruo 	if (ri.drop_level >= BTRFS_MAX_LEVEL) {
1005259ee775SQu Wenruo 		generic_err(leaf, slot,
1006259ee775SQu Wenruo 			    "invalid root level, have %u expect [0, %u]",
1007259ee775SQu Wenruo 			    ri.drop_level, BTRFS_MAX_LEVEL - 1);
1008259ee775SQu Wenruo 		return -EUCLEAN;
1009259ee775SQu Wenruo 	}
1010259ee775SQu Wenruo 
1011259ee775SQu Wenruo 	/* Flags check */
1012259ee775SQu Wenruo 	if (btrfs_root_flags(&ri) & ~valid_root_flags) {
1013259ee775SQu Wenruo 		generic_err(leaf, slot,
1014259ee775SQu Wenruo 			    "invalid root flags, have 0x%llx expect mask 0x%llx",
1015259ee775SQu Wenruo 			    btrfs_root_flags(&ri), valid_root_flags);
1016259ee775SQu Wenruo 		return -EUCLEAN;
1017259ee775SQu Wenruo 	}
1018259ee775SQu Wenruo 	return 0;
1019259ee775SQu Wenruo }
1020259ee775SQu Wenruo 
1021f82d1c7cSQu Wenruo __printf(3,4)
1022f82d1c7cSQu Wenruo __cold
1023f82d1c7cSQu Wenruo static void extent_err(const struct extent_buffer *eb, int slot,
1024f82d1c7cSQu Wenruo 		       const char *fmt, ...)
1025f82d1c7cSQu Wenruo {
1026f82d1c7cSQu Wenruo 	struct btrfs_key key;
1027f82d1c7cSQu Wenruo 	struct va_format vaf;
1028f82d1c7cSQu Wenruo 	va_list args;
1029f82d1c7cSQu Wenruo 	u64 bytenr;
1030f82d1c7cSQu Wenruo 	u64 len;
1031f82d1c7cSQu Wenruo 
1032f82d1c7cSQu Wenruo 	btrfs_item_key_to_cpu(eb, &key, slot);
1033f82d1c7cSQu Wenruo 	bytenr = key.objectid;
1034e2406a6fSQu Wenruo 	if (key.type == BTRFS_METADATA_ITEM_KEY ||
1035e2406a6fSQu Wenruo 	    key.type == BTRFS_TREE_BLOCK_REF_KEY ||
1036e2406a6fSQu Wenruo 	    key.type == BTRFS_SHARED_BLOCK_REF_KEY)
1037f82d1c7cSQu Wenruo 		len = eb->fs_info->nodesize;
1038f82d1c7cSQu Wenruo 	else
1039f82d1c7cSQu Wenruo 		len = key.offset;
1040f82d1c7cSQu Wenruo 	va_start(args, fmt);
1041f82d1c7cSQu Wenruo 
1042f82d1c7cSQu Wenruo 	vaf.fmt = fmt;
1043f82d1c7cSQu Wenruo 	vaf.va = &args;
1044f82d1c7cSQu Wenruo 
1045f82d1c7cSQu Wenruo 	btrfs_crit(eb->fs_info,
1046f82d1c7cSQu Wenruo 	"corrupt %s: block=%llu slot=%d extent bytenr=%llu len=%llu %pV",
1047f82d1c7cSQu Wenruo 		btrfs_header_level(eb) == 0 ? "leaf" : "node",
1048f82d1c7cSQu Wenruo 		eb->start, slot, bytenr, len, &vaf);
1049f82d1c7cSQu Wenruo 	va_end(args);
1050f82d1c7cSQu Wenruo }
1051f82d1c7cSQu Wenruo 
1052f82d1c7cSQu Wenruo static int check_extent_item(struct extent_buffer *leaf,
1053f82d1c7cSQu Wenruo 			     struct btrfs_key *key, int slot)
1054f82d1c7cSQu Wenruo {
1055f82d1c7cSQu Wenruo 	struct btrfs_fs_info *fs_info = leaf->fs_info;
1056f82d1c7cSQu Wenruo 	struct btrfs_extent_item *ei;
1057f82d1c7cSQu Wenruo 	bool is_tree_block = false;
1058f82d1c7cSQu Wenruo 	unsigned long ptr;	/* Current pointer inside inline refs */
1059f82d1c7cSQu Wenruo 	unsigned long end;	/* Extent item end */
1060f82d1c7cSQu Wenruo 	const u32 item_size = btrfs_item_size_nr(leaf, slot);
1061f82d1c7cSQu Wenruo 	u64 flags;
1062f82d1c7cSQu Wenruo 	u64 generation;
1063f82d1c7cSQu Wenruo 	u64 total_refs;		/* Total refs in btrfs_extent_item */
1064f82d1c7cSQu Wenruo 	u64 inline_refs = 0;	/* found total inline refs */
1065f82d1c7cSQu Wenruo 
1066f82d1c7cSQu Wenruo 	if (key->type == BTRFS_METADATA_ITEM_KEY &&
1067f82d1c7cSQu Wenruo 	    !btrfs_fs_incompat(fs_info, SKINNY_METADATA)) {
1068f82d1c7cSQu Wenruo 		generic_err(leaf, slot,
1069f82d1c7cSQu Wenruo "invalid key type, METADATA_ITEM type invalid when SKINNY_METADATA feature disabled");
1070f82d1c7cSQu Wenruo 		return -EUCLEAN;
1071f82d1c7cSQu Wenruo 	}
1072f82d1c7cSQu Wenruo 	/* key->objectid is the bytenr for both key types */
1073f82d1c7cSQu Wenruo 	if (!IS_ALIGNED(key->objectid, fs_info->sectorsize)) {
1074f82d1c7cSQu Wenruo 		generic_err(leaf, slot,
1075f82d1c7cSQu Wenruo 		"invalid key objectid, have %llu expect to be aligned to %u",
1076f82d1c7cSQu Wenruo 			   key->objectid, fs_info->sectorsize);
1077f82d1c7cSQu Wenruo 		return -EUCLEAN;
1078f82d1c7cSQu Wenruo 	}
1079f82d1c7cSQu Wenruo 
1080f82d1c7cSQu Wenruo 	/* key->offset is tree level for METADATA_ITEM_KEY */
1081f82d1c7cSQu Wenruo 	if (key->type == BTRFS_METADATA_ITEM_KEY &&
1082f82d1c7cSQu Wenruo 	    key->offset >= BTRFS_MAX_LEVEL) {
1083f82d1c7cSQu Wenruo 		extent_err(leaf, slot,
1084f82d1c7cSQu Wenruo 			   "invalid tree level, have %llu expect [0, %u]",
1085f82d1c7cSQu Wenruo 			   key->offset, BTRFS_MAX_LEVEL - 1);
1086f82d1c7cSQu Wenruo 		return -EUCLEAN;
1087f82d1c7cSQu Wenruo 	}
1088f82d1c7cSQu Wenruo 
1089f82d1c7cSQu Wenruo 	/*
1090f82d1c7cSQu Wenruo 	 * EXTENT/METADATA_ITEM consists of:
1091f82d1c7cSQu Wenruo 	 * 1) One btrfs_extent_item
1092f82d1c7cSQu Wenruo 	 *    Records the total refs, type and generation of the extent.
1093f82d1c7cSQu Wenruo 	 *
1094f82d1c7cSQu Wenruo 	 * 2) One btrfs_tree_block_info (for EXTENT_ITEM and tree backref only)
1095f82d1c7cSQu Wenruo 	 *    Records the first key and level of the tree block.
1096f82d1c7cSQu Wenruo 	 *
1097f82d1c7cSQu Wenruo 	 * 2) Zero or more btrfs_extent_inline_ref(s)
1098f82d1c7cSQu Wenruo 	 *    Each inline ref has one btrfs_extent_inline_ref shows:
1099f82d1c7cSQu Wenruo 	 *    2.1) The ref type, one of the 4
1100f82d1c7cSQu Wenruo 	 *         TREE_BLOCK_REF	Tree block only
1101f82d1c7cSQu Wenruo 	 *         SHARED_BLOCK_REF	Tree block only
1102f82d1c7cSQu Wenruo 	 *         EXTENT_DATA_REF	Data only
1103f82d1c7cSQu Wenruo 	 *         SHARED_DATA_REF	Data only
1104f82d1c7cSQu Wenruo 	 *    2.2) Ref type specific data
1105f82d1c7cSQu Wenruo 	 *         Either using btrfs_extent_inline_ref::offset, or specific
1106f82d1c7cSQu Wenruo 	 *         data structure.
1107f82d1c7cSQu Wenruo 	 */
1108f82d1c7cSQu Wenruo 	if (item_size < sizeof(*ei)) {
1109f82d1c7cSQu Wenruo 		extent_err(leaf, slot,
1110f82d1c7cSQu Wenruo 			   "invalid item size, have %u expect [%zu, %u)",
1111f82d1c7cSQu Wenruo 			   item_size, sizeof(*ei),
1112f82d1c7cSQu Wenruo 			   BTRFS_LEAF_DATA_SIZE(fs_info));
1113f82d1c7cSQu Wenruo 		return -EUCLEAN;
1114f82d1c7cSQu Wenruo 	}
1115f82d1c7cSQu Wenruo 	end = item_size + btrfs_item_ptr_offset(leaf, slot);
1116f82d1c7cSQu Wenruo 
1117f82d1c7cSQu Wenruo 	/* Checks against extent_item */
1118f82d1c7cSQu Wenruo 	ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
1119f82d1c7cSQu Wenruo 	flags = btrfs_extent_flags(leaf, ei);
1120f82d1c7cSQu Wenruo 	total_refs = btrfs_extent_refs(leaf, ei);
1121f82d1c7cSQu Wenruo 	generation = btrfs_extent_generation(leaf, ei);
1122f82d1c7cSQu Wenruo 	if (generation > btrfs_super_generation(fs_info->super_copy) + 1) {
1123f82d1c7cSQu Wenruo 		extent_err(leaf, slot,
1124f82d1c7cSQu Wenruo 			   "invalid generation, have %llu expect (0, %llu]",
1125f82d1c7cSQu Wenruo 			   generation,
1126f82d1c7cSQu Wenruo 			   btrfs_super_generation(fs_info->super_copy) + 1);
1127f82d1c7cSQu Wenruo 		return -EUCLEAN;
1128f82d1c7cSQu Wenruo 	}
1129c1499166SDavid Sterba 	if (!has_single_bit_set(flags & (BTRFS_EXTENT_FLAG_DATA |
1130f82d1c7cSQu Wenruo 					 BTRFS_EXTENT_FLAG_TREE_BLOCK))) {
1131f82d1c7cSQu Wenruo 		extent_err(leaf, slot,
1132f82d1c7cSQu Wenruo 		"invalid extent flag, have 0x%llx expect 1 bit set in 0x%llx",
1133f82d1c7cSQu Wenruo 			flags, BTRFS_EXTENT_FLAG_DATA |
1134f82d1c7cSQu Wenruo 			BTRFS_EXTENT_FLAG_TREE_BLOCK);
1135f82d1c7cSQu Wenruo 		return -EUCLEAN;
1136f82d1c7cSQu Wenruo 	}
1137f82d1c7cSQu Wenruo 	is_tree_block = !!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK);
1138f82d1c7cSQu Wenruo 	if (is_tree_block) {
1139f82d1c7cSQu Wenruo 		if (key->type == BTRFS_EXTENT_ITEM_KEY &&
1140f82d1c7cSQu Wenruo 		    key->offset != fs_info->nodesize) {
1141f82d1c7cSQu Wenruo 			extent_err(leaf, slot,
1142f82d1c7cSQu Wenruo 				   "invalid extent length, have %llu expect %u",
1143f82d1c7cSQu Wenruo 				   key->offset, fs_info->nodesize);
1144f82d1c7cSQu Wenruo 			return -EUCLEAN;
1145f82d1c7cSQu Wenruo 		}
1146f82d1c7cSQu Wenruo 	} else {
1147f82d1c7cSQu Wenruo 		if (key->type != BTRFS_EXTENT_ITEM_KEY) {
1148f82d1c7cSQu Wenruo 			extent_err(leaf, slot,
1149f82d1c7cSQu Wenruo 			"invalid key type, have %u expect %u for data backref",
1150f82d1c7cSQu Wenruo 				   key->type, BTRFS_EXTENT_ITEM_KEY);
1151f82d1c7cSQu Wenruo 			return -EUCLEAN;
1152f82d1c7cSQu Wenruo 		}
1153f82d1c7cSQu Wenruo 		if (!IS_ALIGNED(key->offset, fs_info->sectorsize)) {
1154f82d1c7cSQu Wenruo 			extent_err(leaf, slot,
1155f82d1c7cSQu Wenruo 			"invalid extent length, have %llu expect aligned to %u",
1156f82d1c7cSQu Wenruo 				   key->offset, fs_info->sectorsize);
1157f82d1c7cSQu Wenruo 			return -EUCLEAN;
1158f82d1c7cSQu Wenruo 		}
1159f82d1c7cSQu Wenruo 	}
1160f82d1c7cSQu Wenruo 	ptr = (unsigned long)(struct btrfs_extent_item *)(ei + 1);
1161f82d1c7cSQu Wenruo 
1162f82d1c7cSQu Wenruo 	/* Check the special case of btrfs_tree_block_info */
1163f82d1c7cSQu Wenruo 	if (is_tree_block && key->type != BTRFS_METADATA_ITEM_KEY) {
1164f82d1c7cSQu Wenruo 		struct btrfs_tree_block_info *info;
1165f82d1c7cSQu Wenruo 
1166f82d1c7cSQu Wenruo 		info = (struct btrfs_tree_block_info *)ptr;
1167f82d1c7cSQu Wenruo 		if (btrfs_tree_block_level(leaf, info) >= BTRFS_MAX_LEVEL) {
1168f82d1c7cSQu Wenruo 			extent_err(leaf, slot,
1169f82d1c7cSQu Wenruo 			"invalid tree block info level, have %u expect [0, %u]",
1170f82d1c7cSQu Wenruo 				   btrfs_tree_block_level(leaf, info),
1171f82d1c7cSQu Wenruo 				   BTRFS_MAX_LEVEL - 1);
1172f82d1c7cSQu Wenruo 			return -EUCLEAN;
1173f82d1c7cSQu Wenruo 		}
1174f82d1c7cSQu Wenruo 		ptr = (unsigned long)(struct btrfs_tree_block_info *)(info + 1);
1175f82d1c7cSQu Wenruo 	}
1176f82d1c7cSQu Wenruo 
1177f82d1c7cSQu Wenruo 	/* Check inline refs */
1178f82d1c7cSQu Wenruo 	while (ptr < end) {
1179f82d1c7cSQu Wenruo 		struct btrfs_extent_inline_ref *iref;
1180f82d1c7cSQu Wenruo 		struct btrfs_extent_data_ref *dref;
1181f82d1c7cSQu Wenruo 		struct btrfs_shared_data_ref *sref;
1182f82d1c7cSQu Wenruo 		u64 dref_offset;
1183f82d1c7cSQu Wenruo 		u64 inline_offset;
1184f82d1c7cSQu Wenruo 		u8 inline_type;
1185f82d1c7cSQu Wenruo 
1186f82d1c7cSQu Wenruo 		if (ptr + sizeof(*iref) > end) {
1187f82d1c7cSQu Wenruo 			extent_err(leaf, slot,
1188f82d1c7cSQu Wenruo "inline ref item overflows extent item, ptr %lu iref size %zu end %lu",
1189f82d1c7cSQu Wenruo 				   ptr, sizeof(*iref), end);
1190f82d1c7cSQu Wenruo 			return -EUCLEAN;
1191f82d1c7cSQu Wenruo 		}
1192f82d1c7cSQu Wenruo 		iref = (struct btrfs_extent_inline_ref *)ptr;
1193f82d1c7cSQu Wenruo 		inline_type = btrfs_extent_inline_ref_type(leaf, iref);
1194f82d1c7cSQu Wenruo 		inline_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1195f82d1c7cSQu Wenruo 		if (ptr + btrfs_extent_inline_ref_size(inline_type) > end) {
1196f82d1c7cSQu Wenruo 			extent_err(leaf, slot,
1197f82d1c7cSQu Wenruo "inline ref item overflows extent item, ptr %lu iref size %u end %lu",
1198f82d1c7cSQu Wenruo 				   ptr, inline_type, end);
1199f82d1c7cSQu Wenruo 			return -EUCLEAN;
1200f82d1c7cSQu Wenruo 		}
1201f82d1c7cSQu Wenruo 
1202f82d1c7cSQu Wenruo 		switch (inline_type) {
1203f82d1c7cSQu Wenruo 		/* inline_offset is subvolid of the owner, no need to check */
1204f82d1c7cSQu Wenruo 		case BTRFS_TREE_BLOCK_REF_KEY:
1205f82d1c7cSQu Wenruo 			inline_refs++;
1206f82d1c7cSQu Wenruo 			break;
1207f82d1c7cSQu Wenruo 		/* Contains parent bytenr */
1208f82d1c7cSQu Wenruo 		case BTRFS_SHARED_BLOCK_REF_KEY:
1209f82d1c7cSQu Wenruo 			if (!IS_ALIGNED(inline_offset, fs_info->sectorsize)) {
1210f82d1c7cSQu Wenruo 				extent_err(leaf, slot,
1211f82d1c7cSQu Wenruo 		"invalid tree parent bytenr, have %llu expect aligned to %u",
1212f82d1c7cSQu Wenruo 					   inline_offset, fs_info->sectorsize);
1213f82d1c7cSQu Wenruo 				return -EUCLEAN;
1214f82d1c7cSQu Wenruo 			}
1215f82d1c7cSQu Wenruo 			inline_refs++;
1216f82d1c7cSQu Wenruo 			break;
1217f82d1c7cSQu Wenruo 		/*
1218f82d1c7cSQu Wenruo 		 * Contains owner subvolid, owner key objectid, adjusted offset.
1219f82d1c7cSQu Wenruo 		 * The only obvious corruption can happen in that offset.
1220f82d1c7cSQu Wenruo 		 */
1221f82d1c7cSQu Wenruo 		case BTRFS_EXTENT_DATA_REF_KEY:
1222f82d1c7cSQu Wenruo 			dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1223f82d1c7cSQu Wenruo 			dref_offset = btrfs_extent_data_ref_offset(leaf, dref);
1224f82d1c7cSQu Wenruo 			if (!IS_ALIGNED(dref_offset, fs_info->sectorsize)) {
1225f82d1c7cSQu Wenruo 				extent_err(leaf, slot,
1226f82d1c7cSQu Wenruo 		"invalid data ref offset, have %llu expect aligned to %u",
1227f82d1c7cSQu Wenruo 					   dref_offset, fs_info->sectorsize);
1228f82d1c7cSQu Wenruo 				return -EUCLEAN;
1229f82d1c7cSQu Wenruo 			}
1230f82d1c7cSQu Wenruo 			inline_refs += btrfs_extent_data_ref_count(leaf, dref);
1231f82d1c7cSQu Wenruo 			break;
1232f82d1c7cSQu Wenruo 		/* Contains parent bytenr and ref count */
1233f82d1c7cSQu Wenruo 		case BTRFS_SHARED_DATA_REF_KEY:
1234f82d1c7cSQu Wenruo 			sref = (struct btrfs_shared_data_ref *)(iref + 1);
1235f82d1c7cSQu Wenruo 			if (!IS_ALIGNED(inline_offset, fs_info->sectorsize)) {
1236f82d1c7cSQu Wenruo 				extent_err(leaf, slot,
1237f82d1c7cSQu Wenruo 		"invalid data parent bytenr, have %llu expect aligned to %u",
1238f82d1c7cSQu Wenruo 					   inline_offset, fs_info->sectorsize);
1239f82d1c7cSQu Wenruo 				return -EUCLEAN;
1240f82d1c7cSQu Wenruo 			}
1241f82d1c7cSQu Wenruo 			inline_refs += btrfs_shared_data_ref_count(leaf, sref);
1242f82d1c7cSQu Wenruo 			break;
1243f82d1c7cSQu Wenruo 		default:
1244f82d1c7cSQu Wenruo 			extent_err(leaf, slot, "unknown inline ref type: %u",
1245f82d1c7cSQu Wenruo 				   inline_type);
1246f82d1c7cSQu Wenruo 			return -EUCLEAN;
1247f82d1c7cSQu Wenruo 		}
1248f82d1c7cSQu Wenruo 		ptr += btrfs_extent_inline_ref_size(inline_type);
1249f82d1c7cSQu Wenruo 	}
1250f82d1c7cSQu Wenruo 	/* No padding is allowed */
1251f82d1c7cSQu Wenruo 	if (ptr != end) {
1252f82d1c7cSQu Wenruo 		extent_err(leaf, slot,
1253f82d1c7cSQu Wenruo 			   "invalid extent item size, padding bytes found");
1254f82d1c7cSQu Wenruo 		return -EUCLEAN;
1255f82d1c7cSQu Wenruo 	}
1256f82d1c7cSQu Wenruo 
1257f82d1c7cSQu Wenruo 	/* Finally, check the inline refs against total refs */
1258f82d1c7cSQu Wenruo 	if (inline_refs > total_refs) {
1259f82d1c7cSQu Wenruo 		extent_err(leaf, slot,
1260f82d1c7cSQu Wenruo 			"invalid extent refs, have %llu expect >= inline %llu",
1261f82d1c7cSQu Wenruo 			   total_refs, inline_refs);
1262f82d1c7cSQu Wenruo 		return -EUCLEAN;
1263f82d1c7cSQu Wenruo 	}
1264f82d1c7cSQu Wenruo 	return 0;
1265f82d1c7cSQu Wenruo }
1266f82d1c7cSQu Wenruo 
1267e2406a6fSQu Wenruo static int check_simple_keyed_refs(struct extent_buffer *leaf,
1268e2406a6fSQu Wenruo 				   struct btrfs_key *key, int slot)
1269e2406a6fSQu Wenruo {
1270e2406a6fSQu Wenruo 	u32 expect_item_size = 0;
1271e2406a6fSQu Wenruo 
1272e2406a6fSQu Wenruo 	if (key->type == BTRFS_SHARED_DATA_REF_KEY)
1273e2406a6fSQu Wenruo 		expect_item_size = sizeof(struct btrfs_shared_data_ref);
1274e2406a6fSQu Wenruo 
1275e2406a6fSQu Wenruo 	if (btrfs_item_size_nr(leaf, slot) != expect_item_size) {
1276e2406a6fSQu Wenruo 		generic_err(leaf, slot,
1277e2406a6fSQu Wenruo 		"invalid item size, have %u expect %u for key type %u",
1278e2406a6fSQu Wenruo 			    btrfs_item_size_nr(leaf, slot),
1279e2406a6fSQu Wenruo 			    expect_item_size, key->type);
1280e2406a6fSQu Wenruo 		return -EUCLEAN;
1281e2406a6fSQu Wenruo 	}
1282e2406a6fSQu Wenruo 	if (!IS_ALIGNED(key->objectid, leaf->fs_info->sectorsize)) {
1283e2406a6fSQu Wenruo 		generic_err(leaf, slot,
1284e2406a6fSQu Wenruo "invalid key objectid for shared block ref, have %llu expect aligned to %u",
1285e2406a6fSQu Wenruo 			    key->objectid, leaf->fs_info->sectorsize);
1286e2406a6fSQu Wenruo 		return -EUCLEAN;
1287e2406a6fSQu Wenruo 	}
1288e2406a6fSQu Wenruo 	if (key->type != BTRFS_TREE_BLOCK_REF_KEY &&
1289e2406a6fSQu Wenruo 	    !IS_ALIGNED(key->offset, leaf->fs_info->sectorsize)) {
1290e2406a6fSQu Wenruo 		extent_err(leaf, slot,
1291e2406a6fSQu Wenruo 		"invalid tree parent bytenr, have %llu expect aligned to %u",
1292e2406a6fSQu Wenruo 			   key->offset, leaf->fs_info->sectorsize);
1293e2406a6fSQu Wenruo 		return -EUCLEAN;
1294e2406a6fSQu Wenruo 	}
1295e2406a6fSQu Wenruo 	return 0;
1296e2406a6fSQu Wenruo }
1297e2406a6fSQu Wenruo 
12980785a9aaSQu Wenruo static int check_extent_data_ref(struct extent_buffer *leaf,
12990785a9aaSQu Wenruo 				 struct btrfs_key *key, int slot)
13000785a9aaSQu Wenruo {
13010785a9aaSQu Wenruo 	struct btrfs_extent_data_ref *dref;
13020785a9aaSQu Wenruo 	unsigned long ptr = btrfs_item_ptr_offset(leaf, slot);
13030785a9aaSQu Wenruo 	const unsigned long end = ptr + btrfs_item_size_nr(leaf, slot);
13040785a9aaSQu Wenruo 
13050785a9aaSQu Wenruo 	if (btrfs_item_size_nr(leaf, slot) % sizeof(*dref) != 0) {
13060785a9aaSQu Wenruo 		generic_err(leaf, slot,
13070785a9aaSQu Wenruo 	"invalid item size, have %u expect aligned to %zu for key type %u",
13080785a9aaSQu Wenruo 			    btrfs_item_size_nr(leaf, slot),
13090785a9aaSQu Wenruo 			    sizeof(*dref), key->type);
13100785a9aaSQu Wenruo 	}
13110785a9aaSQu Wenruo 	if (!IS_ALIGNED(key->objectid, leaf->fs_info->sectorsize)) {
13120785a9aaSQu Wenruo 		generic_err(leaf, slot,
13130785a9aaSQu Wenruo "invalid key objectid for shared block ref, have %llu expect aligned to %u",
13140785a9aaSQu Wenruo 			    key->objectid, leaf->fs_info->sectorsize);
13150785a9aaSQu Wenruo 		return -EUCLEAN;
13160785a9aaSQu Wenruo 	}
13170785a9aaSQu Wenruo 	for (; ptr < end; ptr += sizeof(*dref)) {
13180785a9aaSQu Wenruo 		u64 root_objectid;
13190785a9aaSQu Wenruo 		u64 owner;
13200785a9aaSQu Wenruo 		u64 offset;
13210785a9aaSQu Wenruo 		u64 hash;
13220785a9aaSQu Wenruo 
13230785a9aaSQu Wenruo 		dref = (struct btrfs_extent_data_ref *)ptr;
13240785a9aaSQu Wenruo 		root_objectid = btrfs_extent_data_ref_root(leaf, dref);
13250785a9aaSQu Wenruo 		owner = btrfs_extent_data_ref_objectid(leaf, dref);
13260785a9aaSQu Wenruo 		offset = btrfs_extent_data_ref_offset(leaf, dref);
13270785a9aaSQu Wenruo 		hash = hash_extent_data_ref(root_objectid, owner, offset);
13280785a9aaSQu Wenruo 		if (hash != key->offset) {
13290785a9aaSQu Wenruo 			extent_err(leaf, slot,
13300785a9aaSQu Wenruo 	"invalid extent data ref hash, item has 0x%016llx key has 0x%016llx",
13310785a9aaSQu Wenruo 				   hash, key->offset);
13320785a9aaSQu Wenruo 			return -EUCLEAN;
13330785a9aaSQu Wenruo 		}
13340785a9aaSQu Wenruo 		if (!IS_ALIGNED(offset, leaf->fs_info->sectorsize)) {
13350785a9aaSQu Wenruo 			extent_err(leaf, slot,
13360785a9aaSQu Wenruo 	"invalid extent data backref offset, have %llu expect aligned to %u",
13370785a9aaSQu Wenruo 				   offset, leaf->fs_info->sectorsize);
13380785a9aaSQu Wenruo 		}
13390785a9aaSQu Wenruo 	}
13400785a9aaSQu Wenruo 	return 0;
13410785a9aaSQu Wenruo }
13420785a9aaSQu Wenruo 
1343*c3053ebbSQu Wenruo #define inode_ref_err(eb, slot, fmt, args...)			\
1344*c3053ebbSQu Wenruo 	inode_item_err(eb, slot, fmt, ##args)
134571bf92a9SQu Wenruo static int check_inode_ref(struct extent_buffer *leaf,
134671bf92a9SQu Wenruo 			   struct btrfs_key *key, struct btrfs_key *prev_key,
134771bf92a9SQu Wenruo 			   int slot)
134871bf92a9SQu Wenruo {
134971bf92a9SQu Wenruo 	struct btrfs_inode_ref *iref;
135071bf92a9SQu Wenruo 	unsigned long ptr;
135171bf92a9SQu Wenruo 	unsigned long end;
135271bf92a9SQu Wenruo 
135380d7fd1eSQu Wenruo 	if (!check_prev_ino(leaf, key, slot, prev_key))
135480d7fd1eSQu Wenruo 		return -EUCLEAN;
135571bf92a9SQu Wenruo 	/* namelen can't be 0, so item_size == sizeof() is also invalid */
135671bf92a9SQu Wenruo 	if (btrfs_item_size_nr(leaf, slot) <= sizeof(*iref)) {
1357*c3053ebbSQu Wenruo 		inode_ref_err(leaf, slot,
135871bf92a9SQu Wenruo 			"invalid item size, have %u expect (%zu, %u)",
135971bf92a9SQu Wenruo 			btrfs_item_size_nr(leaf, slot),
136071bf92a9SQu Wenruo 			sizeof(*iref), BTRFS_LEAF_DATA_SIZE(leaf->fs_info));
136171bf92a9SQu Wenruo 		return -EUCLEAN;
136271bf92a9SQu Wenruo 	}
136371bf92a9SQu Wenruo 
136471bf92a9SQu Wenruo 	ptr = btrfs_item_ptr_offset(leaf, slot);
136571bf92a9SQu Wenruo 	end = ptr + btrfs_item_size_nr(leaf, slot);
136671bf92a9SQu Wenruo 	while (ptr < end) {
136771bf92a9SQu Wenruo 		u16 namelen;
136871bf92a9SQu Wenruo 
136971bf92a9SQu Wenruo 		if (ptr + sizeof(iref) > end) {
1370*c3053ebbSQu Wenruo 			inode_ref_err(leaf, slot,
137171bf92a9SQu Wenruo 			"inode ref overflow, ptr %lu end %lu inode_ref_size %zu",
137271bf92a9SQu Wenruo 				ptr, end, sizeof(iref));
137371bf92a9SQu Wenruo 			return -EUCLEAN;
137471bf92a9SQu Wenruo 		}
137571bf92a9SQu Wenruo 
137671bf92a9SQu Wenruo 		iref = (struct btrfs_inode_ref *)ptr;
137771bf92a9SQu Wenruo 		namelen = btrfs_inode_ref_name_len(leaf, iref);
137871bf92a9SQu Wenruo 		if (ptr + sizeof(*iref) + namelen > end) {
1379*c3053ebbSQu Wenruo 			inode_ref_err(leaf, slot,
138071bf92a9SQu Wenruo 				"inode ref overflow, ptr %lu end %lu namelen %u",
138171bf92a9SQu Wenruo 				ptr, end, namelen);
138271bf92a9SQu Wenruo 			return -EUCLEAN;
138371bf92a9SQu Wenruo 		}
138471bf92a9SQu Wenruo 
138571bf92a9SQu Wenruo 		/*
138671bf92a9SQu Wenruo 		 * NOTE: In theory we should record all found index numbers
138771bf92a9SQu Wenruo 		 * to find any duplicated indexes, but that will be too time
138871bf92a9SQu Wenruo 		 * consuming for inodes with too many hard links.
138971bf92a9SQu Wenruo 		 */
139071bf92a9SQu Wenruo 		ptr += sizeof(*iref) + namelen;
139171bf92a9SQu Wenruo 	}
139271bf92a9SQu Wenruo 	return 0;
139371bf92a9SQu Wenruo }
139471bf92a9SQu Wenruo 
139582fc28fbSQu Wenruo /*
1396557ea5ddSQu Wenruo  * Common point to switch the item-specific validation.
1397557ea5ddSQu Wenruo  */
13980076bc89SDavid Sterba static int check_leaf_item(struct extent_buffer *leaf,
13994e9845efSFilipe Manana 			   struct btrfs_key *key, int slot,
14004e9845efSFilipe Manana 			   struct btrfs_key *prev_key)
1401557ea5ddSQu Wenruo {
1402557ea5ddSQu Wenruo 	int ret = 0;
1403075cb3c7SQu Wenruo 	struct btrfs_chunk *chunk;
1404557ea5ddSQu Wenruo 
1405557ea5ddSQu Wenruo 	switch (key->type) {
1406557ea5ddSQu Wenruo 	case BTRFS_EXTENT_DATA_KEY:
14074e9845efSFilipe Manana 		ret = check_extent_data_item(leaf, key, slot, prev_key);
1408557ea5ddSQu Wenruo 		break;
1409557ea5ddSQu Wenruo 	case BTRFS_EXTENT_CSUM_KEY:
1410ad1d8c43SFilipe Manana 		ret = check_csum_item(leaf, key, slot, prev_key);
1411557ea5ddSQu Wenruo 		break;
1412ad7b0368SQu Wenruo 	case BTRFS_DIR_ITEM_KEY:
1413ad7b0368SQu Wenruo 	case BTRFS_DIR_INDEX_KEY:
1414ad7b0368SQu Wenruo 	case BTRFS_XATTR_ITEM_KEY:
1415c18679ebSQu Wenruo 		ret = check_dir_item(leaf, key, prev_key, slot);
1416ad7b0368SQu Wenruo 		break;
141771bf92a9SQu Wenruo 	case BTRFS_INODE_REF_KEY:
141871bf92a9SQu Wenruo 		ret = check_inode_ref(leaf, key, prev_key, slot);
141971bf92a9SQu Wenruo 		break;
1420fce466eaSQu Wenruo 	case BTRFS_BLOCK_GROUP_ITEM_KEY:
1421af60ce2bSDavid Sterba 		ret = check_block_group_item(leaf, key, slot);
1422fce466eaSQu Wenruo 		break;
1423075cb3c7SQu Wenruo 	case BTRFS_CHUNK_ITEM_KEY:
1424075cb3c7SQu Wenruo 		chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
1425f6d2a5c2SQu Wenruo 		ret = check_leaf_chunk_item(leaf, chunk, key, slot);
1426075cb3c7SQu Wenruo 		break;
1427ab4ba2e1SQu Wenruo 	case BTRFS_DEV_ITEM_KEY:
1428412a2312SDavid Sterba 		ret = check_dev_item(leaf, key, slot);
1429ab4ba2e1SQu Wenruo 		break;
1430496245caSQu Wenruo 	case BTRFS_INODE_ITEM_KEY:
143139e57f49SDavid Sterba 		ret = check_inode_item(leaf, key, slot);
1432496245caSQu Wenruo 		break;
1433259ee775SQu Wenruo 	case BTRFS_ROOT_ITEM_KEY:
1434259ee775SQu Wenruo 		ret = check_root_item(leaf, key, slot);
1435259ee775SQu Wenruo 		break;
1436f82d1c7cSQu Wenruo 	case BTRFS_EXTENT_ITEM_KEY:
1437f82d1c7cSQu Wenruo 	case BTRFS_METADATA_ITEM_KEY:
1438f82d1c7cSQu Wenruo 		ret = check_extent_item(leaf, key, slot);
1439f82d1c7cSQu Wenruo 		break;
1440e2406a6fSQu Wenruo 	case BTRFS_TREE_BLOCK_REF_KEY:
1441e2406a6fSQu Wenruo 	case BTRFS_SHARED_DATA_REF_KEY:
1442e2406a6fSQu Wenruo 	case BTRFS_SHARED_BLOCK_REF_KEY:
1443e2406a6fSQu Wenruo 		ret = check_simple_keyed_refs(leaf, key, slot);
1444e2406a6fSQu Wenruo 		break;
14450785a9aaSQu Wenruo 	case BTRFS_EXTENT_DATA_REF_KEY:
14460785a9aaSQu Wenruo 		ret = check_extent_data_ref(leaf, key, slot);
14470785a9aaSQu Wenruo 		break;
1448557ea5ddSQu Wenruo 	}
1449557ea5ddSQu Wenruo 	return ret;
1450557ea5ddSQu Wenruo }
1451557ea5ddSQu Wenruo 
1452e2ccd361SDavid Sterba static int check_leaf(struct extent_buffer *leaf, bool check_item_data)
1453557ea5ddSQu Wenruo {
1454e2ccd361SDavid Sterba 	struct btrfs_fs_info *fs_info = leaf->fs_info;
1455557ea5ddSQu Wenruo 	/* No valid key type is 0, so all key should be larger than this key */
1456557ea5ddSQu Wenruo 	struct btrfs_key prev_key = {0, 0, 0};
1457557ea5ddSQu Wenruo 	struct btrfs_key key;
1458557ea5ddSQu Wenruo 	u32 nritems = btrfs_header_nritems(leaf);
1459557ea5ddSQu Wenruo 	int slot;
1460557ea5ddSQu Wenruo 
1461f556faa4SQu Wenruo 	if (btrfs_header_level(leaf) != 0) {
146286a6be3aSDavid Sterba 		generic_err(leaf, 0,
1463f556faa4SQu Wenruo 			"invalid level for leaf, have %d expect 0",
1464f556faa4SQu Wenruo 			btrfs_header_level(leaf));
1465f556faa4SQu Wenruo 		return -EUCLEAN;
1466f556faa4SQu Wenruo 	}
1467f556faa4SQu Wenruo 
1468557ea5ddSQu Wenruo 	/*
1469557ea5ddSQu Wenruo 	 * Extent buffers from a relocation tree have a owner field that
1470557ea5ddSQu Wenruo 	 * corresponds to the subvolume tree they are based on. So just from an
1471557ea5ddSQu Wenruo 	 * extent buffer alone we can not find out what is the id of the
1472557ea5ddSQu Wenruo 	 * corresponding subvolume tree, so we can not figure out if the extent
1473557ea5ddSQu Wenruo 	 * buffer corresponds to the root of the relocation tree or not. So
1474557ea5ddSQu Wenruo 	 * skip this check for relocation trees.
1475557ea5ddSQu Wenruo 	 */
1476557ea5ddSQu Wenruo 	if (nritems == 0 && !btrfs_header_flag(leaf, BTRFS_HEADER_FLAG_RELOC)) {
1477ba480dd4SQu Wenruo 		u64 owner = btrfs_header_owner(leaf);
1478557ea5ddSQu Wenruo 
1479ba480dd4SQu Wenruo 		/* These trees must never be empty */
1480ba480dd4SQu Wenruo 		if (owner == BTRFS_ROOT_TREE_OBJECTID ||
1481ba480dd4SQu Wenruo 		    owner == BTRFS_CHUNK_TREE_OBJECTID ||
1482ba480dd4SQu Wenruo 		    owner == BTRFS_EXTENT_TREE_OBJECTID ||
1483ba480dd4SQu Wenruo 		    owner == BTRFS_DEV_TREE_OBJECTID ||
1484ba480dd4SQu Wenruo 		    owner == BTRFS_FS_TREE_OBJECTID ||
1485ba480dd4SQu Wenruo 		    owner == BTRFS_DATA_RELOC_TREE_OBJECTID) {
148686a6be3aSDavid Sterba 			generic_err(leaf, 0,
1487ba480dd4SQu Wenruo 			"invalid root, root %llu must never be empty",
1488ba480dd4SQu Wenruo 				    owner);
1489ba480dd4SQu Wenruo 			return -EUCLEAN;
1490ba480dd4SQu Wenruo 		}
149162fdaa52SQu Wenruo 		/* Unknown tree */
149262fdaa52SQu Wenruo 		if (owner == 0) {
149362fdaa52SQu Wenruo 			generic_err(leaf, 0,
149462fdaa52SQu Wenruo 				"invalid owner, root 0 is not defined");
149562fdaa52SQu Wenruo 			return -EUCLEAN;
149662fdaa52SQu Wenruo 		}
1497557ea5ddSQu Wenruo 		return 0;
1498557ea5ddSQu Wenruo 	}
1499557ea5ddSQu Wenruo 
1500557ea5ddSQu Wenruo 	if (nritems == 0)
1501557ea5ddSQu Wenruo 		return 0;
1502557ea5ddSQu Wenruo 
1503557ea5ddSQu Wenruo 	/*
1504557ea5ddSQu Wenruo 	 * Check the following things to make sure this is a good leaf, and
1505557ea5ddSQu Wenruo 	 * leaf users won't need to bother with similar sanity checks:
1506557ea5ddSQu Wenruo 	 *
1507557ea5ddSQu Wenruo 	 * 1) key ordering
1508557ea5ddSQu Wenruo 	 * 2) item offset and size
1509557ea5ddSQu Wenruo 	 *    No overlap, no hole, all inside the leaf.
1510557ea5ddSQu Wenruo 	 * 3) item content
1511557ea5ddSQu Wenruo 	 *    If possible, do comprehensive sanity check.
1512557ea5ddSQu Wenruo 	 *    NOTE: All checks must only rely on the item data itself.
1513557ea5ddSQu Wenruo 	 */
1514557ea5ddSQu Wenruo 	for (slot = 0; slot < nritems; slot++) {
1515557ea5ddSQu Wenruo 		u32 item_end_expected;
1516557ea5ddSQu Wenruo 		int ret;
1517557ea5ddSQu Wenruo 
1518557ea5ddSQu Wenruo 		btrfs_item_key_to_cpu(leaf, &key, slot);
1519557ea5ddSQu Wenruo 
1520557ea5ddSQu Wenruo 		/* Make sure the keys are in the right order */
1521557ea5ddSQu Wenruo 		if (btrfs_comp_cpu_keys(&prev_key, &key) >= 0) {
152286a6be3aSDavid Sterba 			generic_err(leaf, slot,
1523478d01b3SQu Wenruo 	"bad key order, prev (%llu %u %llu) current (%llu %u %llu)",
1524478d01b3SQu Wenruo 				prev_key.objectid, prev_key.type,
1525478d01b3SQu Wenruo 				prev_key.offset, key.objectid, key.type,
1526478d01b3SQu Wenruo 				key.offset);
1527557ea5ddSQu Wenruo 			return -EUCLEAN;
1528557ea5ddSQu Wenruo 		}
1529557ea5ddSQu Wenruo 
1530557ea5ddSQu Wenruo 		/*
1531557ea5ddSQu Wenruo 		 * Make sure the offset and ends are right, remember that the
1532557ea5ddSQu Wenruo 		 * item data starts at the end of the leaf and grows towards the
1533557ea5ddSQu Wenruo 		 * front.
1534557ea5ddSQu Wenruo 		 */
1535557ea5ddSQu Wenruo 		if (slot == 0)
1536557ea5ddSQu Wenruo 			item_end_expected = BTRFS_LEAF_DATA_SIZE(fs_info);
1537557ea5ddSQu Wenruo 		else
1538557ea5ddSQu Wenruo 			item_end_expected = btrfs_item_offset_nr(leaf,
1539557ea5ddSQu Wenruo 								 slot - 1);
1540557ea5ddSQu Wenruo 		if (btrfs_item_end_nr(leaf, slot) != item_end_expected) {
154186a6be3aSDavid Sterba 			generic_err(leaf, slot,
1542478d01b3SQu Wenruo 				"unexpected item end, have %u expect %u",
1543478d01b3SQu Wenruo 				btrfs_item_end_nr(leaf, slot),
1544478d01b3SQu Wenruo 				item_end_expected);
1545557ea5ddSQu Wenruo 			return -EUCLEAN;
1546557ea5ddSQu Wenruo 		}
1547557ea5ddSQu Wenruo 
1548557ea5ddSQu Wenruo 		/*
1549557ea5ddSQu Wenruo 		 * Check to make sure that we don't point outside of the leaf,
1550557ea5ddSQu Wenruo 		 * just in case all the items are consistent to each other, but
1551557ea5ddSQu Wenruo 		 * all point outside of the leaf.
1552557ea5ddSQu Wenruo 		 */
1553557ea5ddSQu Wenruo 		if (btrfs_item_end_nr(leaf, slot) >
1554557ea5ddSQu Wenruo 		    BTRFS_LEAF_DATA_SIZE(fs_info)) {
155586a6be3aSDavid Sterba 			generic_err(leaf, slot,
1556478d01b3SQu Wenruo 			"slot end outside of leaf, have %u expect range [0, %u]",
1557478d01b3SQu Wenruo 				btrfs_item_end_nr(leaf, slot),
1558478d01b3SQu Wenruo 				BTRFS_LEAF_DATA_SIZE(fs_info));
1559557ea5ddSQu Wenruo 			return -EUCLEAN;
1560557ea5ddSQu Wenruo 		}
1561557ea5ddSQu Wenruo 
1562557ea5ddSQu Wenruo 		/* Also check if the item pointer overlaps with btrfs item. */
1563557ea5ddSQu Wenruo 		if (btrfs_item_nr_offset(slot) + sizeof(struct btrfs_item) >
1564557ea5ddSQu Wenruo 		    btrfs_item_ptr_offset(leaf, slot)) {
156586a6be3aSDavid Sterba 			generic_err(leaf, slot,
1566478d01b3SQu Wenruo 		"slot overlaps with its data, item end %lu data start %lu",
1567478d01b3SQu Wenruo 				btrfs_item_nr_offset(slot) +
1568478d01b3SQu Wenruo 				sizeof(struct btrfs_item),
1569478d01b3SQu Wenruo 				btrfs_item_ptr_offset(leaf, slot));
1570557ea5ddSQu Wenruo 			return -EUCLEAN;
1571557ea5ddSQu Wenruo 		}
1572557ea5ddSQu Wenruo 
157369fc6cbbSQu Wenruo 		if (check_item_data) {
157469fc6cbbSQu Wenruo 			/*
157569fc6cbbSQu Wenruo 			 * Check if the item size and content meet other
157669fc6cbbSQu Wenruo 			 * criteria
157769fc6cbbSQu Wenruo 			 */
15784e9845efSFilipe Manana 			ret = check_leaf_item(leaf, &key, slot, &prev_key);
1579557ea5ddSQu Wenruo 			if (ret < 0)
1580557ea5ddSQu Wenruo 				return ret;
158169fc6cbbSQu Wenruo 		}
1582557ea5ddSQu Wenruo 
1583557ea5ddSQu Wenruo 		prev_key.objectid = key.objectid;
1584557ea5ddSQu Wenruo 		prev_key.type = key.type;
1585557ea5ddSQu Wenruo 		prev_key.offset = key.offset;
1586557ea5ddSQu Wenruo 	}
1587557ea5ddSQu Wenruo 
1588557ea5ddSQu Wenruo 	return 0;
1589557ea5ddSQu Wenruo }
1590557ea5ddSQu Wenruo 
15911c4360eeSDavid Sterba int btrfs_check_leaf_full(struct extent_buffer *leaf)
159269fc6cbbSQu Wenruo {
1593e2ccd361SDavid Sterba 	return check_leaf(leaf, true);
159469fc6cbbSQu Wenruo }
159502529d7aSQu Wenruo ALLOW_ERROR_INJECTION(btrfs_check_leaf_full, ERRNO);
159669fc6cbbSQu Wenruo 
1597cfdaad5eSDavid Sterba int btrfs_check_leaf_relaxed(struct extent_buffer *leaf)
15982f659546SQu Wenruo {
1599e2ccd361SDavid Sterba 	return check_leaf(leaf, false);
16002f659546SQu Wenruo }
16012f659546SQu Wenruo 
1602813fd1dcSDavid Sterba int btrfs_check_node(struct extent_buffer *node)
1603557ea5ddSQu Wenruo {
1604813fd1dcSDavid Sterba 	struct btrfs_fs_info *fs_info = node->fs_info;
1605557ea5ddSQu Wenruo 	unsigned long nr = btrfs_header_nritems(node);
1606557ea5ddSQu Wenruo 	struct btrfs_key key, next_key;
1607557ea5ddSQu Wenruo 	int slot;
1608f556faa4SQu Wenruo 	int level = btrfs_header_level(node);
1609557ea5ddSQu Wenruo 	u64 bytenr;
1610557ea5ddSQu Wenruo 	int ret = 0;
1611557ea5ddSQu Wenruo 
1612f556faa4SQu Wenruo 	if (level <= 0 || level >= BTRFS_MAX_LEVEL) {
161386a6be3aSDavid Sterba 		generic_err(node, 0,
1614f556faa4SQu Wenruo 			"invalid level for node, have %d expect [1, %d]",
1615f556faa4SQu Wenruo 			level, BTRFS_MAX_LEVEL - 1);
1616f556faa4SQu Wenruo 		return -EUCLEAN;
1617f556faa4SQu Wenruo 	}
16182f659546SQu Wenruo 	if (nr == 0 || nr > BTRFS_NODEPTRS_PER_BLOCK(fs_info)) {
16192f659546SQu Wenruo 		btrfs_crit(fs_info,
1620bba4f298SQu Wenruo "corrupt node: root=%llu block=%llu, nritems too %s, have %lu expect range [1,%u]",
16212f659546SQu Wenruo 			   btrfs_header_owner(node), node->start,
1622bba4f298SQu Wenruo 			   nr == 0 ? "small" : "large", nr,
16232f659546SQu Wenruo 			   BTRFS_NODEPTRS_PER_BLOCK(fs_info));
1624bba4f298SQu Wenruo 		return -EUCLEAN;
1625557ea5ddSQu Wenruo 	}
1626557ea5ddSQu Wenruo 
1627557ea5ddSQu Wenruo 	for (slot = 0; slot < nr - 1; slot++) {
1628557ea5ddSQu Wenruo 		bytenr = btrfs_node_blockptr(node, slot);
1629557ea5ddSQu Wenruo 		btrfs_node_key_to_cpu(node, &key, slot);
1630557ea5ddSQu Wenruo 		btrfs_node_key_to_cpu(node, &next_key, slot + 1);
1631557ea5ddSQu Wenruo 
1632557ea5ddSQu Wenruo 		if (!bytenr) {
163386a6be3aSDavid Sterba 			generic_err(node, slot,
1634bba4f298SQu Wenruo 				"invalid NULL node pointer");
1635bba4f298SQu Wenruo 			ret = -EUCLEAN;
1636bba4f298SQu Wenruo 			goto out;
1637bba4f298SQu Wenruo 		}
16382f659546SQu Wenruo 		if (!IS_ALIGNED(bytenr, fs_info->sectorsize)) {
163986a6be3aSDavid Sterba 			generic_err(node, slot,
1640bba4f298SQu Wenruo 			"unaligned pointer, have %llu should be aligned to %u",
16412f659546SQu Wenruo 				bytenr, fs_info->sectorsize);
1642bba4f298SQu Wenruo 			ret = -EUCLEAN;
1643557ea5ddSQu Wenruo 			goto out;
1644557ea5ddSQu Wenruo 		}
1645557ea5ddSQu Wenruo 
1646557ea5ddSQu Wenruo 		if (btrfs_comp_cpu_keys(&key, &next_key) >= 0) {
164786a6be3aSDavid Sterba 			generic_err(node, slot,
1648bba4f298SQu Wenruo 	"bad key order, current (%llu %u %llu) next (%llu %u %llu)",
1649bba4f298SQu Wenruo 				key.objectid, key.type, key.offset,
1650bba4f298SQu Wenruo 				next_key.objectid, next_key.type,
1651bba4f298SQu Wenruo 				next_key.offset);
1652bba4f298SQu Wenruo 			ret = -EUCLEAN;
1653557ea5ddSQu Wenruo 			goto out;
1654557ea5ddSQu Wenruo 		}
1655557ea5ddSQu Wenruo 	}
1656557ea5ddSQu Wenruo out:
1657557ea5ddSQu Wenruo 	return ret;
1658557ea5ddSQu Wenruo }
165902529d7aSQu Wenruo ALLOW_ERROR_INJECTION(btrfs_check_node, ERRNO);
1660