xref: /openbmc/linux/fs/btrfs/tree-checker.c (revision c7f13d428ea1bfe883f2741a9b5a5352d595eb09)
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"
2777eea05eSBoris Burkov #include "btrfs_inode.h"
28*c7f13d42SJosef Bacik #include "fs.h"
29557ea5ddSQu Wenruo 
30bba4f298SQu Wenruo /*
31bba4f298SQu Wenruo  * Error message should follow the following format:
32bba4f298SQu Wenruo  * corrupt <type>: <identifier>, <reason>[, <bad_value>]
33bba4f298SQu Wenruo  *
34bba4f298SQu Wenruo  * @type:	leaf or node
35bba4f298SQu Wenruo  * @identifier:	the necessary info to locate the leaf/node.
3652042d8eSAndrea Gelmini  * 		It's recommended to decode key.objecitd/offset if it's
37bba4f298SQu Wenruo  * 		meaningful.
38bba4f298SQu Wenruo  * @reason:	describe the error
3952042d8eSAndrea Gelmini  * @bad_value:	optional, it's recommended to output bad value and its
40bba4f298SQu Wenruo  *		expected value (range).
41bba4f298SQu Wenruo  *
42bba4f298SQu Wenruo  * Since comma is used to separate the components, only space is allowed
43bba4f298SQu Wenruo  * inside each component.
44bba4f298SQu Wenruo  */
45bba4f298SQu Wenruo 
46bba4f298SQu Wenruo /*
47bba4f298SQu Wenruo  * Append generic "corrupt leaf/node root=%llu block=%llu slot=%d: " to @fmt.
48bba4f298SQu Wenruo  * Allows callers to customize the output.
49bba4f298SQu Wenruo  */
5086a6be3aSDavid Sterba __printf(3, 4)
51e67c718bSDavid Sterba __cold
5286a6be3aSDavid Sterba static void generic_err(const struct extent_buffer *eb, int slot,
53bba4f298SQu Wenruo 			const char *fmt, ...)
54bba4f298SQu Wenruo {
5586a6be3aSDavid Sterba 	const struct btrfs_fs_info *fs_info = eb->fs_info;
56bba4f298SQu Wenruo 	struct va_format vaf;
57bba4f298SQu Wenruo 	va_list args;
58bba4f298SQu Wenruo 
59bba4f298SQu Wenruo 	va_start(args, fmt);
60bba4f298SQu Wenruo 
61bba4f298SQu Wenruo 	vaf.fmt = fmt;
62bba4f298SQu Wenruo 	vaf.va = &args;
63bba4f298SQu Wenruo 
642f659546SQu Wenruo 	btrfs_crit(fs_info,
65bba4f298SQu Wenruo 		"corrupt %s: root=%llu block=%llu slot=%d, %pV",
66bba4f298SQu Wenruo 		btrfs_header_level(eb) == 0 ? "leaf" : "node",
672f659546SQu Wenruo 		btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot, &vaf);
68bba4f298SQu Wenruo 	va_end(args);
69bba4f298SQu Wenruo }
70bba4f298SQu Wenruo 
718806d718SQu Wenruo /*
728806d718SQu Wenruo  * Customized reporter for extent data item, since its key objectid and
738806d718SQu Wenruo  * offset has its own meaning.
748806d718SQu Wenruo  */
751fd715ffSDavid Sterba __printf(3, 4)
76e67c718bSDavid Sterba __cold
771fd715ffSDavid Sterba static void file_extent_err(const struct extent_buffer *eb, int slot,
788806d718SQu Wenruo 			    const char *fmt, ...)
798806d718SQu Wenruo {
801fd715ffSDavid Sterba 	const struct btrfs_fs_info *fs_info = eb->fs_info;
818806d718SQu Wenruo 	struct btrfs_key key;
828806d718SQu Wenruo 	struct va_format vaf;
838806d718SQu Wenruo 	va_list args;
848806d718SQu Wenruo 
858806d718SQu Wenruo 	btrfs_item_key_to_cpu(eb, &key, slot);
868806d718SQu Wenruo 	va_start(args, fmt);
878806d718SQu Wenruo 
888806d718SQu Wenruo 	vaf.fmt = fmt;
898806d718SQu Wenruo 	vaf.va = &args;
908806d718SQu Wenruo 
912f659546SQu Wenruo 	btrfs_crit(fs_info,
928806d718SQu Wenruo 	"corrupt %s: root=%llu block=%llu slot=%d ino=%llu file_offset=%llu, %pV",
932f659546SQu Wenruo 		btrfs_header_level(eb) == 0 ? "leaf" : "node",
942f659546SQu Wenruo 		btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot,
952f659546SQu Wenruo 		key.objectid, key.offset, &vaf);
968806d718SQu Wenruo 	va_end(args);
978806d718SQu Wenruo }
988806d718SQu Wenruo 
998806d718SQu Wenruo /*
1008806d718SQu Wenruo  * Return 0 if the btrfs_file_extent_##name is aligned to @alignment
1018806d718SQu Wenruo  * Else return 1
1028806d718SQu Wenruo  */
103033774dcSDavid Sterba #define CHECK_FE_ALIGNED(leaf, slot, fi, name, alignment)		      \
1048806d718SQu Wenruo ({									      \
105c7c01a4aSDavid Sterba 	if (unlikely(!IS_ALIGNED(btrfs_file_extent_##name((leaf), (fi)),      \
106c7c01a4aSDavid Sterba 				 (alignment))))				      \
1071fd715ffSDavid Sterba 		file_extent_err((leaf), (slot),				      \
1088806d718SQu Wenruo 	"invalid %s for file extent, have %llu, should be aligned to %u",     \
1098806d718SQu Wenruo 			(#name), btrfs_file_extent_##name((leaf), (fi)),      \
1108806d718SQu Wenruo 			(alignment));					      \
1118806d718SQu Wenruo 	(!IS_ALIGNED(btrfs_file_extent_##name((leaf), (fi)), (alignment)));   \
1128806d718SQu Wenruo })
1138806d718SQu Wenruo 
1144e9845efSFilipe Manana static u64 file_extent_end(struct extent_buffer *leaf,
1154e9845efSFilipe Manana 			   struct btrfs_key *key,
1164e9845efSFilipe Manana 			   struct btrfs_file_extent_item *extent)
1174e9845efSFilipe Manana {
1184e9845efSFilipe Manana 	u64 end;
1194e9845efSFilipe Manana 	u64 len;
1204e9845efSFilipe Manana 
1214e9845efSFilipe Manana 	if (btrfs_file_extent_type(leaf, extent) == BTRFS_FILE_EXTENT_INLINE) {
1224e9845efSFilipe Manana 		len = btrfs_file_extent_ram_bytes(leaf, extent);
1234e9845efSFilipe Manana 		end = ALIGN(key->offset + len, leaf->fs_info->sectorsize);
1244e9845efSFilipe Manana 	} else {
1254e9845efSFilipe Manana 		len = btrfs_file_extent_num_bytes(leaf, extent);
1264e9845efSFilipe Manana 		end = key->offset + len;
1274e9845efSFilipe Manana 	}
1284e9845efSFilipe Manana 	return end;
1294e9845efSFilipe Manana }
1304e9845efSFilipe Manana 
13180d7fd1eSQu Wenruo /*
13280d7fd1eSQu Wenruo  * Customized report for dir_item, the only new important information is
13380d7fd1eSQu Wenruo  * key->objectid, which represents inode number
13480d7fd1eSQu Wenruo  */
13580d7fd1eSQu Wenruo __printf(3, 4)
13680d7fd1eSQu Wenruo __cold
13780d7fd1eSQu Wenruo static void dir_item_err(const struct extent_buffer *eb, int slot,
13880d7fd1eSQu Wenruo 			 const char *fmt, ...)
13980d7fd1eSQu Wenruo {
14080d7fd1eSQu Wenruo 	const struct btrfs_fs_info *fs_info = eb->fs_info;
14180d7fd1eSQu Wenruo 	struct btrfs_key key;
14280d7fd1eSQu Wenruo 	struct va_format vaf;
14380d7fd1eSQu Wenruo 	va_list args;
14480d7fd1eSQu Wenruo 
14580d7fd1eSQu Wenruo 	btrfs_item_key_to_cpu(eb, &key, slot);
14680d7fd1eSQu Wenruo 	va_start(args, fmt);
14780d7fd1eSQu Wenruo 
14880d7fd1eSQu Wenruo 	vaf.fmt = fmt;
14980d7fd1eSQu Wenruo 	vaf.va = &args;
15080d7fd1eSQu Wenruo 
15180d7fd1eSQu Wenruo 	btrfs_crit(fs_info,
15280d7fd1eSQu Wenruo 		"corrupt %s: root=%llu block=%llu slot=%d ino=%llu, %pV",
15380d7fd1eSQu Wenruo 		btrfs_header_level(eb) == 0 ? "leaf" : "node",
15480d7fd1eSQu Wenruo 		btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot,
15580d7fd1eSQu Wenruo 		key.objectid, &vaf);
15680d7fd1eSQu Wenruo 	va_end(args);
15780d7fd1eSQu Wenruo }
15880d7fd1eSQu Wenruo 
15980d7fd1eSQu Wenruo /*
16080d7fd1eSQu Wenruo  * This functions checks prev_key->objectid, to ensure current key and prev_key
16180d7fd1eSQu Wenruo  * share the same objectid as inode number.
16280d7fd1eSQu Wenruo  *
16380d7fd1eSQu Wenruo  * This is to detect missing INODE_ITEM in subvolume trees.
16480d7fd1eSQu Wenruo  *
16580d7fd1eSQu Wenruo  * Return true if everything is OK or we don't need to check.
16680d7fd1eSQu Wenruo  * Return false if anything is wrong.
16780d7fd1eSQu Wenruo  */
16880d7fd1eSQu Wenruo static bool check_prev_ino(struct extent_buffer *leaf,
16980d7fd1eSQu Wenruo 			   struct btrfs_key *key, int slot,
17080d7fd1eSQu Wenruo 			   struct btrfs_key *prev_key)
17180d7fd1eSQu Wenruo {
17280d7fd1eSQu Wenruo 	/* No prev key, skip check */
17380d7fd1eSQu Wenruo 	if (slot == 0)
17480d7fd1eSQu Wenruo 		return true;
17580d7fd1eSQu Wenruo 
17680d7fd1eSQu Wenruo 	/* Only these key->types needs to be checked */
17780d7fd1eSQu Wenruo 	ASSERT(key->type == BTRFS_XATTR_ITEM_KEY ||
17880d7fd1eSQu Wenruo 	       key->type == BTRFS_INODE_REF_KEY ||
17980d7fd1eSQu Wenruo 	       key->type == BTRFS_DIR_INDEX_KEY ||
18080d7fd1eSQu Wenruo 	       key->type == BTRFS_DIR_ITEM_KEY ||
18180d7fd1eSQu Wenruo 	       key->type == BTRFS_EXTENT_DATA_KEY);
18280d7fd1eSQu Wenruo 
18380d7fd1eSQu Wenruo 	/*
18480d7fd1eSQu Wenruo 	 * Only subvolume trees along with their reloc trees need this check.
18580d7fd1eSQu Wenruo 	 * Things like log tree doesn't follow this ino requirement.
18680d7fd1eSQu Wenruo 	 */
18780d7fd1eSQu Wenruo 	if (!is_fstree(btrfs_header_owner(leaf)))
18880d7fd1eSQu Wenruo 		return true;
18980d7fd1eSQu Wenruo 
19080d7fd1eSQu Wenruo 	if (key->objectid == prev_key->objectid)
19180d7fd1eSQu Wenruo 		return true;
19280d7fd1eSQu Wenruo 
19380d7fd1eSQu Wenruo 	/* Error found */
19480d7fd1eSQu Wenruo 	dir_item_err(leaf, slot,
19580d7fd1eSQu Wenruo 		"invalid previous key objectid, have %llu expect %llu",
19680d7fd1eSQu Wenruo 		prev_key->objectid, key->objectid);
19780d7fd1eSQu Wenruo 	return false;
19880d7fd1eSQu Wenruo }
199ae2a19d8SDavid Sterba static int check_extent_data_item(struct extent_buffer *leaf,
2004e9845efSFilipe Manana 				  struct btrfs_key *key, int slot,
2014e9845efSFilipe Manana 				  struct btrfs_key *prev_key)
202557ea5ddSQu Wenruo {
203ae2a19d8SDavid Sterba 	struct btrfs_fs_info *fs_info = leaf->fs_info;
204557ea5ddSQu Wenruo 	struct btrfs_file_extent_item *fi;
2052f659546SQu Wenruo 	u32 sectorsize = fs_info->sectorsize;
2063212fa14SJosef Bacik 	u32 item_size = btrfs_item_size(leaf, slot);
2074c094c33SQu Wenruo 	u64 extent_end;
208557ea5ddSQu Wenruo 
209c7c01a4aSDavid Sterba 	if (unlikely(!IS_ALIGNED(key->offset, sectorsize))) {
2101fd715ffSDavid Sterba 		file_extent_err(leaf, slot,
2118806d718SQu Wenruo "unaligned file_offset for file extent, have %llu should be aligned to %u",
2128806d718SQu Wenruo 			key->offset, sectorsize);
213557ea5ddSQu Wenruo 		return -EUCLEAN;
214557ea5ddSQu Wenruo 	}
215557ea5ddSQu Wenruo 
216c18679ebSQu Wenruo 	/*
217c18679ebSQu Wenruo 	 * Previous key must have the same key->objectid (ino).
218c18679ebSQu Wenruo 	 * It can be XATTR_ITEM, INODE_ITEM or just another EXTENT_DATA.
219c18679ebSQu Wenruo 	 * But if objectids mismatch, it means we have a missing
220c18679ebSQu Wenruo 	 * INODE_ITEM.
221c18679ebSQu Wenruo 	 */
222c7c01a4aSDavid Sterba 	if (unlikely(!check_prev_ino(leaf, key, slot, prev_key)))
223c18679ebSQu Wenruo 		return -EUCLEAN;
224c18679ebSQu Wenruo 
225557ea5ddSQu Wenruo 	fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
226557ea5ddSQu Wenruo 
227153a6d29SQu Wenruo 	/*
228153a6d29SQu Wenruo 	 * Make sure the item contains at least inline header, so the file
229153a6d29SQu Wenruo 	 * extent type is not some garbage.
230153a6d29SQu Wenruo 	 */
231c7c01a4aSDavid Sterba 	if (unlikely(item_size < BTRFS_FILE_EXTENT_INLINE_DATA_START)) {
232153a6d29SQu Wenruo 		file_extent_err(leaf, slot,
233994bf9cdSAndreas Färber 				"invalid item size, have %u expect [%zu, %u)",
234153a6d29SQu Wenruo 				item_size, BTRFS_FILE_EXTENT_INLINE_DATA_START,
235153a6d29SQu Wenruo 				SZ_4K);
236153a6d29SQu Wenruo 		return -EUCLEAN;
237153a6d29SQu Wenruo 	}
238c7c01a4aSDavid Sterba 	if (unlikely(btrfs_file_extent_type(leaf, fi) >=
239c7c01a4aSDavid Sterba 		     BTRFS_NR_FILE_EXTENT_TYPES)) {
2401fd715ffSDavid Sterba 		file_extent_err(leaf, slot,
2418806d718SQu Wenruo 		"invalid type for file extent, have %u expect range [0, %u]",
2428806d718SQu Wenruo 			btrfs_file_extent_type(leaf, fi),
243b9b1a53eSChengguang Xu 			BTRFS_NR_FILE_EXTENT_TYPES - 1);
244557ea5ddSQu Wenruo 		return -EUCLEAN;
245557ea5ddSQu Wenruo 	}
246557ea5ddSQu Wenruo 
247557ea5ddSQu Wenruo 	/*
24852042d8eSAndrea Gelmini 	 * Support for new compression/encryption must introduce incompat flag,
249557ea5ddSQu Wenruo 	 * and must be caught in open_ctree().
250557ea5ddSQu Wenruo 	 */
251c7c01a4aSDavid Sterba 	if (unlikely(btrfs_file_extent_compression(leaf, fi) >=
252c7c01a4aSDavid Sterba 		     BTRFS_NR_COMPRESS_TYPES)) {
2531fd715ffSDavid Sterba 		file_extent_err(leaf, slot,
2548806d718SQu Wenruo 	"invalid compression for file extent, have %u expect range [0, %u]",
2558806d718SQu Wenruo 			btrfs_file_extent_compression(leaf, fi),
256ce96b7ffSChengguang Xu 			BTRFS_NR_COMPRESS_TYPES - 1);
257557ea5ddSQu Wenruo 		return -EUCLEAN;
258557ea5ddSQu Wenruo 	}
259c7c01a4aSDavid Sterba 	if (unlikely(btrfs_file_extent_encryption(leaf, fi))) {
2601fd715ffSDavid Sterba 		file_extent_err(leaf, slot,
2618806d718SQu Wenruo 			"invalid encryption for file extent, have %u expect 0",
2628806d718SQu Wenruo 			btrfs_file_extent_encryption(leaf, fi));
263557ea5ddSQu Wenruo 		return -EUCLEAN;
264557ea5ddSQu Wenruo 	}
265557ea5ddSQu Wenruo 	if (btrfs_file_extent_type(leaf, fi) == BTRFS_FILE_EXTENT_INLINE) {
266557ea5ddSQu Wenruo 		/* Inline extent must have 0 as key offset */
267c7c01a4aSDavid Sterba 		if (unlikely(key->offset)) {
2681fd715ffSDavid Sterba 			file_extent_err(leaf, slot,
2698806d718SQu Wenruo 		"invalid file_offset for inline file extent, have %llu expect 0",
2708806d718SQu Wenruo 				key->offset);
271557ea5ddSQu Wenruo 			return -EUCLEAN;
272557ea5ddSQu Wenruo 		}
273557ea5ddSQu Wenruo 
274557ea5ddSQu Wenruo 		/* Compressed inline extent has no on-disk size, skip it */
275557ea5ddSQu Wenruo 		if (btrfs_file_extent_compression(leaf, fi) !=
276557ea5ddSQu Wenruo 		    BTRFS_COMPRESS_NONE)
277557ea5ddSQu Wenruo 			return 0;
278557ea5ddSQu Wenruo 
279557ea5ddSQu Wenruo 		/* Uncompressed inline extent size must match item size */
280c7c01a4aSDavid Sterba 		if (unlikely(item_size != BTRFS_FILE_EXTENT_INLINE_DATA_START +
281c7c01a4aSDavid Sterba 					  btrfs_file_extent_ram_bytes(leaf, fi))) {
2821fd715ffSDavid Sterba 			file_extent_err(leaf, slot,
2838806d718SQu Wenruo 	"invalid ram_bytes for uncompressed inline extent, have %u expect %llu",
2848806d718SQu Wenruo 				item_size, BTRFS_FILE_EXTENT_INLINE_DATA_START +
2858806d718SQu Wenruo 				btrfs_file_extent_ram_bytes(leaf, fi));
286557ea5ddSQu Wenruo 			return -EUCLEAN;
287557ea5ddSQu Wenruo 		}
288557ea5ddSQu Wenruo 		return 0;
289557ea5ddSQu Wenruo 	}
290557ea5ddSQu Wenruo 
291557ea5ddSQu Wenruo 	/* Regular or preallocated extent has fixed item size */
292c7c01a4aSDavid Sterba 	if (unlikely(item_size != sizeof(*fi))) {
2931fd715ffSDavid Sterba 		file_extent_err(leaf, slot,
294709a95c3SArnd Bergmann 	"invalid item size for reg/prealloc file extent, have %u expect %zu",
2958806d718SQu Wenruo 			item_size, sizeof(*fi));
296557ea5ddSQu Wenruo 		return -EUCLEAN;
297557ea5ddSQu Wenruo 	}
298c7c01a4aSDavid Sterba 	if (unlikely(CHECK_FE_ALIGNED(leaf, slot, fi, ram_bytes, sectorsize) ||
299033774dcSDavid Sterba 		     CHECK_FE_ALIGNED(leaf, slot, fi, disk_bytenr, sectorsize) ||
300033774dcSDavid Sterba 		     CHECK_FE_ALIGNED(leaf, slot, fi, disk_num_bytes, sectorsize) ||
301033774dcSDavid Sterba 		     CHECK_FE_ALIGNED(leaf, slot, fi, offset, sectorsize) ||
302c7c01a4aSDavid Sterba 		     CHECK_FE_ALIGNED(leaf, slot, fi, num_bytes, sectorsize)))
303557ea5ddSQu Wenruo 		return -EUCLEAN;
3044e9845efSFilipe Manana 
3054c094c33SQu Wenruo 	/* Catch extent end overflow */
306c7c01a4aSDavid Sterba 	if (unlikely(check_add_overflow(btrfs_file_extent_num_bytes(leaf, fi),
307c7c01a4aSDavid Sterba 					key->offset, &extent_end))) {
3084c094c33SQu Wenruo 		file_extent_err(leaf, slot,
3094c094c33SQu Wenruo 	"extent end overflow, have file offset %llu extent num bytes %llu",
3104c094c33SQu Wenruo 				key->offset,
3114c094c33SQu Wenruo 				btrfs_file_extent_num_bytes(leaf, fi));
3124c094c33SQu Wenruo 		return -EUCLEAN;
3134c094c33SQu Wenruo 	}
3144c094c33SQu Wenruo 
3154e9845efSFilipe Manana 	/*
3164e9845efSFilipe Manana 	 * Check that no two consecutive file extent items, in the same leaf,
3174e9845efSFilipe Manana 	 * present ranges that overlap each other.
3184e9845efSFilipe Manana 	 */
3194e9845efSFilipe Manana 	if (slot > 0 &&
3204e9845efSFilipe Manana 	    prev_key->objectid == key->objectid &&
3214e9845efSFilipe Manana 	    prev_key->type == BTRFS_EXTENT_DATA_KEY) {
3224e9845efSFilipe Manana 		struct btrfs_file_extent_item *prev_fi;
3234e9845efSFilipe Manana 		u64 prev_end;
3244e9845efSFilipe Manana 
3254e9845efSFilipe Manana 		prev_fi = btrfs_item_ptr(leaf, slot - 1,
3264e9845efSFilipe Manana 					 struct btrfs_file_extent_item);
3274e9845efSFilipe Manana 		prev_end = file_extent_end(leaf, prev_key, prev_fi);
328c7c01a4aSDavid Sterba 		if (unlikely(prev_end > key->offset)) {
3294e9845efSFilipe Manana 			file_extent_err(leaf, slot - 1,
3304e9845efSFilipe Manana "file extent end range (%llu) goes beyond start offset (%llu) of the next file extent",
3314e9845efSFilipe Manana 					prev_end, key->offset);
3324e9845efSFilipe Manana 			return -EUCLEAN;
3334e9845efSFilipe Manana 		}
3344e9845efSFilipe Manana 	}
3354e9845efSFilipe Manana 
336557ea5ddSQu Wenruo 	return 0;
337557ea5ddSQu Wenruo }
338557ea5ddSQu Wenruo 
33968128ce7SDavid Sterba static int check_csum_item(struct extent_buffer *leaf, struct btrfs_key *key,
340ad1d8c43SFilipe Manana 			   int slot, struct btrfs_key *prev_key)
341557ea5ddSQu Wenruo {
34268128ce7SDavid Sterba 	struct btrfs_fs_info *fs_info = leaf->fs_info;
3432f659546SQu Wenruo 	u32 sectorsize = fs_info->sectorsize;
344223486c2SDavid Sterba 	const u32 csumsize = fs_info->csum_size;
345557ea5ddSQu Wenruo 
346c7c01a4aSDavid Sterba 	if (unlikely(key->objectid != BTRFS_EXTENT_CSUM_OBJECTID)) {
34786a6be3aSDavid Sterba 		generic_err(leaf, slot,
348d508c5f0SQu Wenruo 		"invalid key objectid for csum item, have %llu expect %llu",
349d508c5f0SQu Wenruo 			key->objectid, BTRFS_EXTENT_CSUM_OBJECTID);
350557ea5ddSQu Wenruo 		return -EUCLEAN;
351557ea5ddSQu Wenruo 	}
352c7c01a4aSDavid Sterba 	if (unlikely(!IS_ALIGNED(key->offset, sectorsize))) {
35386a6be3aSDavid Sterba 		generic_err(leaf, slot,
354d508c5f0SQu Wenruo 	"unaligned key offset for csum item, have %llu should be aligned to %u",
355d508c5f0SQu Wenruo 			key->offset, sectorsize);
356557ea5ddSQu Wenruo 		return -EUCLEAN;
357557ea5ddSQu Wenruo 	}
3583212fa14SJosef Bacik 	if (unlikely(!IS_ALIGNED(btrfs_item_size(leaf, slot), csumsize))) {
35986a6be3aSDavid Sterba 		generic_err(leaf, slot,
360d508c5f0SQu Wenruo 	"unaligned item size for csum item, have %u should be aligned to %u",
3613212fa14SJosef Bacik 			btrfs_item_size(leaf, slot), csumsize);
362557ea5ddSQu Wenruo 		return -EUCLEAN;
363557ea5ddSQu Wenruo 	}
364ad1d8c43SFilipe Manana 	if (slot > 0 && prev_key->type == BTRFS_EXTENT_CSUM_KEY) {
365ad1d8c43SFilipe Manana 		u64 prev_csum_end;
366ad1d8c43SFilipe Manana 		u32 prev_item_size;
367ad1d8c43SFilipe Manana 
3683212fa14SJosef Bacik 		prev_item_size = btrfs_item_size(leaf, slot - 1);
369ad1d8c43SFilipe Manana 		prev_csum_end = (prev_item_size / csumsize) * sectorsize;
370ad1d8c43SFilipe Manana 		prev_csum_end += prev_key->offset;
371c7c01a4aSDavid Sterba 		if (unlikely(prev_csum_end > key->offset)) {
372ad1d8c43SFilipe Manana 			generic_err(leaf, slot - 1,
373ad1d8c43SFilipe Manana "csum end range (%llu) goes beyond the start range (%llu) of the next csum item",
374ad1d8c43SFilipe Manana 				    prev_csum_end, key->offset);
375ad1d8c43SFilipe Manana 			return -EUCLEAN;
376ad1d8c43SFilipe Manana 		}
377ad1d8c43SFilipe Manana 	}
378557ea5ddSQu Wenruo 	return 0;
379557ea5ddSQu Wenruo }
380557ea5ddSQu Wenruo 
381c23c77b0SQu Wenruo /* Inode item error output has the same format as dir_item_err() */
382c23c77b0SQu Wenruo #define inode_item_err(eb, slot, fmt, ...)			\
383c23c77b0SQu Wenruo 	dir_item_err(eb, slot, fmt, __VA_ARGS__)
384c23c77b0SQu Wenruo 
385c23c77b0SQu Wenruo static int check_inode_key(struct extent_buffer *leaf, struct btrfs_key *key,
386c23c77b0SQu Wenruo 			   int slot)
387c23c77b0SQu Wenruo {
388c23c77b0SQu Wenruo 	struct btrfs_key item_key;
389c23c77b0SQu Wenruo 	bool is_inode_item;
390c23c77b0SQu Wenruo 
391c23c77b0SQu Wenruo 	btrfs_item_key_to_cpu(leaf, &item_key, slot);
392c23c77b0SQu Wenruo 	is_inode_item = (item_key.type == BTRFS_INODE_ITEM_KEY);
393c23c77b0SQu Wenruo 
394c23c77b0SQu Wenruo 	/* For XATTR_ITEM, location key should be all 0 */
395c23c77b0SQu Wenruo 	if (item_key.type == BTRFS_XATTR_ITEM_KEY) {
396c7c01a4aSDavid Sterba 		if (unlikely(key->objectid != 0 || key->type != 0 ||
397c7c01a4aSDavid Sterba 			     key->offset != 0))
398c23c77b0SQu Wenruo 			return -EUCLEAN;
399c23c77b0SQu Wenruo 		return 0;
400c23c77b0SQu Wenruo 	}
401c23c77b0SQu Wenruo 
402c7c01a4aSDavid Sterba 	if (unlikely((key->objectid < BTRFS_FIRST_FREE_OBJECTID ||
403c23c77b0SQu Wenruo 		      key->objectid > BTRFS_LAST_FREE_OBJECTID) &&
404c23c77b0SQu Wenruo 		     key->objectid != BTRFS_ROOT_TREE_DIR_OBJECTID &&
405c7c01a4aSDavid Sterba 		     key->objectid != BTRFS_FREE_INO_OBJECTID)) {
406c23c77b0SQu Wenruo 		if (is_inode_item) {
407c23c77b0SQu Wenruo 			generic_err(leaf, slot,
408c23c77b0SQu Wenruo 	"invalid key objectid: has %llu expect %llu or [%llu, %llu] or %llu",
409c23c77b0SQu Wenruo 				key->objectid, BTRFS_ROOT_TREE_DIR_OBJECTID,
410c23c77b0SQu Wenruo 				BTRFS_FIRST_FREE_OBJECTID,
411c23c77b0SQu Wenruo 				BTRFS_LAST_FREE_OBJECTID,
412c23c77b0SQu Wenruo 				BTRFS_FREE_INO_OBJECTID);
413c23c77b0SQu Wenruo 		} else {
414c23c77b0SQu Wenruo 			dir_item_err(leaf, slot,
415c23c77b0SQu Wenruo "invalid location key objectid: has %llu expect %llu or [%llu, %llu] or %llu",
416c23c77b0SQu Wenruo 				key->objectid, BTRFS_ROOT_TREE_DIR_OBJECTID,
417c23c77b0SQu Wenruo 				BTRFS_FIRST_FREE_OBJECTID,
418c23c77b0SQu Wenruo 				BTRFS_LAST_FREE_OBJECTID,
419c23c77b0SQu Wenruo 				BTRFS_FREE_INO_OBJECTID);
420c23c77b0SQu Wenruo 		}
421c23c77b0SQu Wenruo 		return -EUCLEAN;
422c23c77b0SQu Wenruo 	}
423c7c01a4aSDavid Sterba 	if (unlikely(key->offset != 0)) {
424c23c77b0SQu Wenruo 		if (is_inode_item)
425c23c77b0SQu Wenruo 			inode_item_err(leaf, slot,
426c23c77b0SQu Wenruo 				       "invalid key offset: has %llu expect 0",
427c23c77b0SQu Wenruo 				       key->offset);
428c23c77b0SQu Wenruo 		else
429c23c77b0SQu Wenruo 			dir_item_err(leaf, slot,
430c23c77b0SQu Wenruo 				"invalid location key offset:has %llu expect 0",
431c23c77b0SQu Wenruo 				key->offset);
432c23c77b0SQu Wenruo 		return -EUCLEAN;
433c23c77b0SQu Wenruo 	}
434c23c77b0SQu Wenruo 	return 0;
435c23c77b0SQu Wenruo }
436c23c77b0SQu Wenruo 
43757a0e674SQu Wenruo static int check_root_key(struct extent_buffer *leaf, struct btrfs_key *key,
43857a0e674SQu Wenruo 			  int slot)
43957a0e674SQu Wenruo {
44057a0e674SQu Wenruo 	struct btrfs_key item_key;
44157a0e674SQu Wenruo 	bool is_root_item;
44257a0e674SQu Wenruo 
44357a0e674SQu Wenruo 	btrfs_item_key_to_cpu(leaf, &item_key, slot);
44457a0e674SQu Wenruo 	is_root_item = (item_key.type == BTRFS_ROOT_ITEM_KEY);
44557a0e674SQu Wenruo 
44657a0e674SQu Wenruo 	/* No such tree id */
447c7c01a4aSDavid Sterba 	if (unlikely(key->objectid == 0)) {
44857a0e674SQu Wenruo 		if (is_root_item)
44957a0e674SQu Wenruo 			generic_err(leaf, slot, "invalid root id 0");
45057a0e674SQu Wenruo 		else
45157a0e674SQu Wenruo 			dir_item_err(leaf, slot,
45257a0e674SQu Wenruo 				     "invalid location key root id 0");
45357a0e674SQu Wenruo 		return -EUCLEAN;
45457a0e674SQu Wenruo 	}
45557a0e674SQu Wenruo 
45657a0e674SQu Wenruo 	/* DIR_ITEM/INDEX/INODE_REF is not allowed to point to non-fs trees */
457c7c01a4aSDavid Sterba 	if (unlikely(!is_fstree(key->objectid) && !is_root_item)) {
45857a0e674SQu Wenruo 		dir_item_err(leaf, slot,
45957a0e674SQu Wenruo 		"invalid location key objectid, have %llu expect [%llu, %llu]",
46057a0e674SQu Wenruo 				key->objectid, BTRFS_FIRST_FREE_OBJECTID,
46157a0e674SQu Wenruo 				BTRFS_LAST_FREE_OBJECTID);
46257a0e674SQu Wenruo 		return -EUCLEAN;
46357a0e674SQu Wenruo 	}
46457a0e674SQu Wenruo 
46557a0e674SQu Wenruo 	/*
46657a0e674SQu Wenruo 	 * ROOT_ITEM with non-zero offset means this is a snapshot, created at
46757a0e674SQu Wenruo 	 * @offset transid.
46857a0e674SQu Wenruo 	 * Furthermore, for location key in DIR_ITEM, its offset is always -1.
46957a0e674SQu Wenruo 	 *
47057a0e674SQu Wenruo 	 * So here we only check offset for reloc tree whose key->offset must
47157a0e674SQu Wenruo 	 * be a valid tree.
47257a0e674SQu Wenruo 	 */
473c7c01a4aSDavid Sterba 	if (unlikely(key->objectid == BTRFS_TREE_RELOC_OBJECTID &&
474c7c01a4aSDavid Sterba 		     key->offset == 0)) {
47557a0e674SQu Wenruo 		generic_err(leaf, slot, "invalid root id 0 for reloc tree");
47657a0e674SQu Wenruo 		return -EUCLEAN;
47757a0e674SQu Wenruo 	}
47857a0e674SQu Wenruo 	return 0;
47957a0e674SQu Wenruo }
48057a0e674SQu Wenruo 
481ce4252c0SDavid Sterba static int check_dir_item(struct extent_buffer *leaf,
482c18679ebSQu Wenruo 			  struct btrfs_key *key, struct btrfs_key *prev_key,
483c18679ebSQu Wenruo 			  int slot)
484ad7b0368SQu Wenruo {
485ce4252c0SDavid Sterba 	struct btrfs_fs_info *fs_info = leaf->fs_info;
486ad7b0368SQu Wenruo 	struct btrfs_dir_item *di;
4873212fa14SJosef Bacik 	u32 item_size = btrfs_item_size(leaf, slot);
488ad7b0368SQu Wenruo 	u32 cur = 0;
489ad7b0368SQu Wenruo 
490c7c01a4aSDavid Sterba 	if (unlikely(!check_prev_ino(leaf, key, slot, prev_key)))
491c18679ebSQu Wenruo 		return -EUCLEAN;
492c7c01a4aSDavid Sterba 
493ad7b0368SQu Wenruo 	di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
494ad7b0368SQu Wenruo 	while (cur < item_size) {
495147a097cSQu Wenruo 		struct btrfs_key location_key;
496ad7b0368SQu Wenruo 		u32 name_len;
497ad7b0368SQu Wenruo 		u32 data_len;
498ad7b0368SQu Wenruo 		u32 max_name_len;
499ad7b0368SQu Wenruo 		u32 total_size;
500ad7b0368SQu Wenruo 		u32 name_hash;
501ad7b0368SQu Wenruo 		u8 dir_type;
502147a097cSQu Wenruo 		int ret;
503ad7b0368SQu Wenruo 
504ad7b0368SQu Wenruo 		/* header itself should not cross item boundary */
505c7c01a4aSDavid Sterba 		if (unlikely(cur + sizeof(*di) > item_size)) {
506d98ced68SDavid Sterba 			dir_item_err(leaf, slot,
5077cfad652SArnd Bergmann 		"dir item header crosses item boundary, have %zu boundary %u",
508ad7b0368SQu Wenruo 				cur + sizeof(*di), item_size);
509ad7b0368SQu Wenruo 			return -EUCLEAN;
510ad7b0368SQu Wenruo 		}
511ad7b0368SQu Wenruo 
512147a097cSQu Wenruo 		/* Location key check */
513147a097cSQu Wenruo 		btrfs_dir_item_key_to_cpu(leaf, di, &location_key);
514147a097cSQu Wenruo 		if (location_key.type == BTRFS_ROOT_ITEM_KEY) {
515147a097cSQu Wenruo 			ret = check_root_key(leaf, &location_key, slot);
516c7c01a4aSDavid Sterba 			if (unlikely(ret < 0))
517147a097cSQu Wenruo 				return ret;
518147a097cSQu Wenruo 		} else if (location_key.type == BTRFS_INODE_ITEM_KEY ||
519147a097cSQu Wenruo 			   location_key.type == 0) {
520147a097cSQu Wenruo 			ret = check_inode_key(leaf, &location_key, slot);
521c7c01a4aSDavid Sterba 			if (unlikely(ret < 0))
522147a097cSQu Wenruo 				return ret;
523147a097cSQu Wenruo 		} else {
524147a097cSQu Wenruo 			dir_item_err(leaf, slot,
525147a097cSQu Wenruo 			"invalid location key type, have %u, expect %u or %u",
526147a097cSQu Wenruo 				     location_key.type, BTRFS_ROOT_ITEM_KEY,
527147a097cSQu Wenruo 				     BTRFS_INODE_ITEM_KEY);
528147a097cSQu Wenruo 			return -EUCLEAN;
529147a097cSQu Wenruo 		}
530147a097cSQu Wenruo 
531ad7b0368SQu Wenruo 		/* dir type check */
532ad7b0368SQu Wenruo 		dir_type = btrfs_dir_type(leaf, di);
533c7c01a4aSDavid Sterba 		if (unlikely(dir_type >= BTRFS_FT_MAX)) {
534d98ced68SDavid Sterba 			dir_item_err(leaf, slot,
535ad7b0368SQu Wenruo 			"invalid dir item type, have %u expect [0, %u)",
536ad7b0368SQu Wenruo 				dir_type, BTRFS_FT_MAX);
537ad7b0368SQu Wenruo 			return -EUCLEAN;
538ad7b0368SQu Wenruo 		}
539ad7b0368SQu Wenruo 
540c7c01a4aSDavid Sterba 		if (unlikely(key->type == BTRFS_XATTR_ITEM_KEY &&
541c7c01a4aSDavid Sterba 			     dir_type != BTRFS_FT_XATTR)) {
542d98ced68SDavid Sterba 			dir_item_err(leaf, slot,
543ad7b0368SQu Wenruo 		"invalid dir item type for XATTR key, have %u expect %u",
544ad7b0368SQu Wenruo 				dir_type, BTRFS_FT_XATTR);
545ad7b0368SQu Wenruo 			return -EUCLEAN;
546ad7b0368SQu Wenruo 		}
547c7c01a4aSDavid Sterba 		if (unlikely(dir_type == BTRFS_FT_XATTR &&
548c7c01a4aSDavid Sterba 			     key->type != BTRFS_XATTR_ITEM_KEY)) {
549d98ced68SDavid Sterba 			dir_item_err(leaf, slot,
550ad7b0368SQu Wenruo 			"xattr dir type found for non-XATTR key");
551ad7b0368SQu Wenruo 			return -EUCLEAN;
552ad7b0368SQu Wenruo 		}
553ad7b0368SQu Wenruo 		if (dir_type == BTRFS_FT_XATTR)
554ad7b0368SQu Wenruo 			max_name_len = XATTR_NAME_MAX;
555ad7b0368SQu Wenruo 		else
556ad7b0368SQu Wenruo 			max_name_len = BTRFS_NAME_LEN;
557ad7b0368SQu Wenruo 
558ad7b0368SQu Wenruo 		/* Name/data length check */
559ad7b0368SQu Wenruo 		name_len = btrfs_dir_name_len(leaf, di);
560ad7b0368SQu Wenruo 		data_len = btrfs_dir_data_len(leaf, di);
561c7c01a4aSDavid Sterba 		if (unlikely(name_len > max_name_len)) {
562d98ced68SDavid Sterba 			dir_item_err(leaf, slot,
563ad7b0368SQu Wenruo 			"dir item name len too long, have %u max %u",
564ad7b0368SQu Wenruo 				name_len, max_name_len);
565ad7b0368SQu Wenruo 			return -EUCLEAN;
566ad7b0368SQu Wenruo 		}
567c7c01a4aSDavid Sterba 		if (unlikely(name_len + data_len > BTRFS_MAX_XATTR_SIZE(fs_info))) {
568d98ced68SDavid Sterba 			dir_item_err(leaf, slot,
569ad7b0368SQu Wenruo 			"dir item name and data len too long, have %u max %u",
570ad7b0368SQu Wenruo 				name_len + data_len,
5712f659546SQu Wenruo 				BTRFS_MAX_XATTR_SIZE(fs_info));
572ad7b0368SQu Wenruo 			return -EUCLEAN;
573ad7b0368SQu Wenruo 		}
574ad7b0368SQu Wenruo 
575c7c01a4aSDavid Sterba 		if (unlikely(data_len && dir_type != BTRFS_FT_XATTR)) {
576d98ced68SDavid Sterba 			dir_item_err(leaf, slot,
577ad7b0368SQu Wenruo 			"dir item with invalid data len, have %u expect 0",
578ad7b0368SQu Wenruo 				data_len);
579ad7b0368SQu Wenruo 			return -EUCLEAN;
580ad7b0368SQu Wenruo 		}
581ad7b0368SQu Wenruo 
582ad7b0368SQu Wenruo 		total_size = sizeof(*di) + name_len + data_len;
583ad7b0368SQu Wenruo 
584ad7b0368SQu Wenruo 		/* header and name/data should not cross item boundary */
585c7c01a4aSDavid Sterba 		if (unlikely(cur + total_size > item_size)) {
586d98ced68SDavid Sterba 			dir_item_err(leaf, slot,
587ad7b0368SQu Wenruo 		"dir item data crosses item boundary, have %u boundary %u",
588ad7b0368SQu Wenruo 				cur + total_size, item_size);
589ad7b0368SQu Wenruo 			return -EUCLEAN;
590ad7b0368SQu Wenruo 		}
591ad7b0368SQu Wenruo 
592ad7b0368SQu Wenruo 		/*
593ad7b0368SQu Wenruo 		 * Special check for XATTR/DIR_ITEM, as key->offset is name
594ad7b0368SQu Wenruo 		 * hash, should match its name
595ad7b0368SQu Wenruo 		 */
596ad7b0368SQu Wenruo 		if (key->type == BTRFS_DIR_ITEM_KEY ||
597ad7b0368SQu Wenruo 		    key->type == BTRFS_XATTR_ITEM_KEY) {
598e2683fc9SDavid Sterba 			char namebuf[max(BTRFS_NAME_LEN, XATTR_NAME_MAX)];
599e2683fc9SDavid Sterba 
600ad7b0368SQu Wenruo 			read_extent_buffer(leaf, namebuf,
601ad7b0368SQu Wenruo 					(unsigned long)(di + 1), name_len);
602ad7b0368SQu Wenruo 			name_hash = btrfs_name_hash(namebuf, name_len);
603c7c01a4aSDavid Sterba 			if (unlikely(key->offset != name_hash)) {
604d98ced68SDavid Sterba 				dir_item_err(leaf, slot,
605ad7b0368SQu Wenruo 		"name hash mismatch with key, have 0x%016x expect 0x%016llx",
606ad7b0368SQu Wenruo 					name_hash, key->offset);
607ad7b0368SQu Wenruo 				return -EUCLEAN;
608ad7b0368SQu Wenruo 			}
609ad7b0368SQu Wenruo 		}
610ad7b0368SQu Wenruo 		cur += total_size;
611ad7b0368SQu Wenruo 		di = (struct btrfs_dir_item *)((void *)di + total_size);
612ad7b0368SQu Wenruo 	}
613ad7b0368SQu Wenruo 	return 0;
614ad7b0368SQu Wenruo }
615ad7b0368SQu Wenruo 
6164806bd88SDavid Sterba __printf(3, 4)
617fce466eaSQu Wenruo __cold
6184806bd88SDavid Sterba static void block_group_err(const struct extent_buffer *eb, int slot,
619fce466eaSQu Wenruo 			    const char *fmt, ...)
620fce466eaSQu Wenruo {
6214806bd88SDavid Sterba 	const struct btrfs_fs_info *fs_info = eb->fs_info;
622fce466eaSQu Wenruo 	struct btrfs_key key;
623fce466eaSQu Wenruo 	struct va_format vaf;
624fce466eaSQu Wenruo 	va_list args;
625fce466eaSQu Wenruo 
626fce466eaSQu Wenruo 	btrfs_item_key_to_cpu(eb, &key, slot);
627fce466eaSQu Wenruo 	va_start(args, fmt);
628fce466eaSQu Wenruo 
629fce466eaSQu Wenruo 	vaf.fmt = fmt;
630fce466eaSQu Wenruo 	vaf.va = &args;
631fce466eaSQu Wenruo 
632fce466eaSQu Wenruo 	btrfs_crit(fs_info,
633fce466eaSQu Wenruo 	"corrupt %s: root=%llu block=%llu slot=%d bg_start=%llu bg_len=%llu, %pV",
634fce466eaSQu Wenruo 		btrfs_header_level(eb) == 0 ? "leaf" : "node",
635fce466eaSQu Wenruo 		btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot,
636fce466eaSQu Wenruo 		key.objectid, key.offset, &vaf);
637fce466eaSQu Wenruo 	va_end(args);
638fce466eaSQu Wenruo }
639fce466eaSQu Wenruo 
640af60ce2bSDavid Sterba static int check_block_group_item(struct extent_buffer *leaf,
641fce466eaSQu Wenruo 				  struct btrfs_key *key, int slot)
642fce466eaSQu Wenruo {
643f7238e50SJosef Bacik 	struct btrfs_fs_info *fs_info = leaf->fs_info;
644fce466eaSQu Wenruo 	struct btrfs_block_group_item bgi;
6453212fa14SJosef Bacik 	u32 item_size = btrfs_item_size(leaf, slot);
646f7238e50SJosef Bacik 	u64 chunk_objectid;
647fce466eaSQu Wenruo 	u64 flags;
648fce466eaSQu Wenruo 	u64 type;
649fce466eaSQu Wenruo 
650fce466eaSQu Wenruo 	/*
651fce466eaSQu Wenruo 	 * Here we don't really care about alignment since extent allocator can
65210950929SQu Wenruo 	 * handle it.  We care more about the size.
653fce466eaSQu Wenruo 	 */
654c7c01a4aSDavid Sterba 	if (unlikely(key->offset == 0)) {
6554806bd88SDavid Sterba 		block_group_err(leaf, slot,
65610950929SQu Wenruo 				"invalid block group size 0");
657fce466eaSQu Wenruo 		return -EUCLEAN;
658fce466eaSQu Wenruo 	}
659fce466eaSQu Wenruo 
660c7c01a4aSDavid Sterba 	if (unlikely(item_size != sizeof(bgi))) {
6614806bd88SDavid Sterba 		block_group_err(leaf, slot,
662fce466eaSQu Wenruo 			"invalid item size, have %u expect %zu",
663fce466eaSQu Wenruo 				item_size, sizeof(bgi));
664fce466eaSQu Wenruo 		return -EUCLEAN;
665fce466eaSQu Wenruo 	}
666fce466eaSQu Wenruo 
667fce466eaSQu Wenruo 	read_extent_buffer(leaf, &bgi, btrfs_item_ptr_offset(leaf, slot),
668fce466eaSQu Wenruo 			   sizeof(bgi));
669f7238e50SJosef Bacik 	chunk_objectid = btrfs_stack_block_group_chunk_objectid(&bgi);
670f7238e50SJosef Bacik 	if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) {
671f7238e50SJosef Bacik 		/*
672f7238e50SJosef Bacik 		 * We don't init the nr_global_roots until we load the global
673f7238e50SJosef Bacik 		 * roots, so this could be 0 at mount time.  If it's 0 we'll
674f7238e50SJosef Bacik 		 * just assume we're fine, and later we'll check against our
675f7238e50SJosef Bacik 		 * actual value.
676f7238e50SJosef Bacik 		 */
677f7238e50SJosef Bacik 		if (unlikely(fs_info->nr_global_roots &&
678f7238e50SJosef Bacik 			     chunk_objectid >= fs_info->nr_global_roots)) {
679f7238e50SJosef Bacik 			block_group_err(leaf, slot,
680f7238e50SJosef Bacik 	"invalid block group global root id, have %llu, needs to be <= %llu",
681f7238e50SJosef Bacik 					chunk_objectid,
682f7238e50SJosef Bacik 					fs_info->nr_global_roots);
683f7238e50SJosef Bacik 			return -EUCLEAN;
684f7238e50SJosef Bacik 		}
685f7238e50SJosef Bacik 	} else if (unlikely(chunk_objectid != BTRFS_FIRST_CHUNK_TREE_OBJECTID)) {
6864806bd88SDavid Sterba 		block_group_err(leaf, slot,
687fce466eaSQu Wenruo 		"invalid block group chunk objectid, have %llu expect %llu",
688de0dc456SDavid Sterba 				btrfs_stack_block_group_chunk_objectid(&bgi),
689fce466eaSQu Wenruo 				BTRFS_FIRST_CHUNK_TREE_OBJECTID);
690fce466eaSQu Wenruo 		return -EUCLEAN;
691fce466eaSQu Wenruo 	}
692fce466eaSQu Wenruo 
693c7c01a4aSDavid Sterba 	if (unlikely(btrfs_stack_block_group_used(&bgi) > key->offset)) {
6944806bd88SDavid Sterba 		block_group_err(leaf, slot,
695fce466eaSQu Wenruo 			"invalid block group used, have %llu expect [0, %llu)",
696de0dc456SDavid Sterba 				btrfs_stack_block_group_used(&bgi), key->offset);
697fce466eaSQu Wenruo 		return -EUCLEAN;
698fce466eaSQu Wenruo 	}
699fce466eaSQu Wenruo 
700de0dc456SDavid Sterba 	flags = btrfs_stack_block_group_flags(&bgi);
701c7c01a4aSDavid Sterba 	if (unlikely(hweight64(flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) > 1)) {
7024806bd88SDavid Sterba 		block_group_err(leaf, slot,
703fce466eaSQu Wenruo "invalid profile flags, have 0x%llx (%lu bits set) expect no more than 1 bit set",
704fce466eaSQu Wenruo 			flags & BTRFS_BLOCK_GROUP_PROFILE_MASK,
705fce466eaSQu Wenruo 			hweight64(flags & BTRFS_BLOCK_GROUP_PROFILE_MASK));
706fce466eaSQu Wenruo 		return -EUCLEAN;
707fce466eaSQu Wenruo 	}
708fce466eaSQu Wenruo 
709fce466eaSQu Wenruo 	type = flags & BTRFS_BLOCK_GROUP_TYPE_MASK;
710c7c01a4aSDavid Sterba 	if (unlikely(type != BTRFS_BLOCK_GROUP_DATA &&
711fce466eaSQu Wenruo 		     type != BTRFS_BLOCK_GROUP_METADATA &&
712fce466eaSQu Wenruo 		     type != BTRFS_BLOCK_GROUP_SYSTEM &&
713fce466eaSQu Wenruo 		     type != (BTRFS_BLOCK_GROUP_METADATA |
714c7c01a4aSDavid Sterba 			      BTRFS_BLOCK_GROUP_DATA))) {
7154806bd88SDavid Sterba 		block_group_err(leaf, slot,
716761333f2SShaokun Zhang "invalid type, have 0x%llx (%lu bits set) expect either 0x%llx, 0x%llx, 0x%llx or 0x%llx",
717fce466eaSQu Wenruo 			type, hweight64(type),
718fce466eaSQu Wenruo 			BTRFS_BLOCK_GROUP_DATA, BTRFS_BLOCK_GROUP_METADATA,
719fce466eaSQu Wenruo 			BTRFS_BLOCK_GROUP_SYSTEM,
720fce466eaSQu Wenruo 			BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA);
721fce466eaSQu Wenruo 		return -EUCLEAN;
722fce466eaSQu Wenruo 	}
723fce466eaSQu Wenruo 	return 0;
724fce466eaSQu Wenruo }
725fce466eaSQu Wenruo 
726d001e4a3SDavid Sterba __printf(4, 5)
727f1140243SQu Wenruo __cold
728d001e4a3SDavid Sterba static void chunk_err(const struct extent_buffer *leaf,
729f1140243SQu Wenruo 		      const struct btrfs_chunk *chunk, u64 logical,
730f1140243SQu Wenruo 		      const char *fmt, ...)
731f1140243SQu Wenruo {
732d001e4a3SDavid Sterba 	const struct btrfs_fs_info *fs_info = leaf->fs_info;
733f1140243SQu Wenruo 	bool is_sb;
734f1140243SQu Wenruo 	struct va_format vaf;
735f1140243SQu Wenruo 	va_list args;
736f1140243SQu Wenruo 	int i;
737f1140243SQu Wenruo 	int slot = -1;
738f1140243SQu Wenruo 
739f1140243SQu Wenruo 	/* Only superblock eb is able to have such small offset */
740f1140243SQu Wenruo 	is_sb = (leaf->start == BTRFS_SUPER_INFO_OFFSET);
741f1140243SQu Wenruo 
742f1140243SQu Wenruo 	if (!is_sb) {
743f1140243SQu Wenruo 		/*
744f1140243SQu Wenruo 		 * Get the slot number by iterating through all slots, this
745f1140243SQu Wenruo 		 * would provide better readability.
746f1140243SQu Wenruo 		 */
747f1140243SQu Wenruo 		for (i = 0; i < btrfs_header_nritems(leaf); i++) {
748f1140243SQu Wenruo 			if (btrfs_item_ptr_offset(leaf, i) ==
749f1140243SQu Wenruo 					(unsigned long)chunk) {
750f1140243SQu Wenruo 				slot = i;
751f1140243SQu Wenruo 				break;
752f1140243SQu Wenruo 			}
753f1140243SQu Wenruo 		}
754f1140243SQu Wenruo 	}
755f1140243SQu Wenruo 	va_start(args, fmt);
756f1140243SQu Wenruo 	vaf.fmt = fmt;
757f1140243SQu Wenruo 	vaf.va = &args;
758f1140243SQu Wenruo 
759f1140243SQu Wenruo 	if (is_sb)
760f1140243SQu Wenruo 		btrfs_crit(fs_info,
761f1140243SQu Wenruo 		"corrupt superblock syschunk array: chunk_start=%llu, %pV",
762f1140243SQu Wenruo 			   logical, &vaf);
763f1140243SQu Wenruo 	else
764f1140243SQu Wenruo 		btrfs_crit(fs_info,
765f1140243SQu Wenruo 	"corrupt leaf: root=%llu block=%llu slot=%d chunk_start=%llu, %pV",
766f1140243SQu Wenruo 			   BTRFS_CHUNK_TREE_OBJECTID, leaf->start, slot,
767f1140243SQu Wenruo 			   logical, &vaf);
768f1140243SQu Wenruo 	va_end(args);
769f1140243SQu Wenruo }
770f1140243SQu Wenruo 
771ad7b0368SQu Wenruo /*
77282fc28fbSQu Wenruo  * The common chunk check which could also work on super block sys chunk array.
77382fc28fbSQu Wenruo  *
774bf871c3bSQu Wenruo  * Return -EUCLEAN if anything is corrupted.
77582fc28fbSQu Wenruo  * Return 0 if everything is OK.
77682fc28fbSQu Wenruo  */
777ddaf1d5aSDavid Sterba int btrfs_check_chunk_valid(struct extent_buffer *leaf,
77882fc28fbSQu Wenruo 			    struct btrfs_chunk *chunk, u64 logical)
77982fc28fbSQu Wenruo {
780ddaf1d5aSDavid Sterba 	struct btrfs_fs_info *fs_info = leaf->fs_info;
78182fc28fbSQu Wenruo 	u64 length;
782347fb0cfSSu Yue 	u64 chunk_end;
78382fc28fbSQu Wenruo 	u64 stripe_len;
78482fc28fbSQu Wenruo 	u16 num_stripes;
78582fc28fbSQu Wenruo 	u16 sub_stripes;
78682fc28fbSQu Wenruo 	u64 type;
78782fc28fbSQu Wenruo 	u64 features;
78882fc28fbSQu Wenruo 	bool mixed = false;
78985d07fbeSDaniel Xu 	int raid_index;
79085d07fbeSDaniel Xu 	int nparity;
79185d07fbeSDaniel Xu 	int ncopies;
79282fc28fbSQu Wenruo 
79382fc28fbSQu Wenruo 	length = btrfs_chunk_length(leaf, chunk);
79482fc28fbSQu Wenruo 	stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
79582fc28fbSQu Wenruo 	num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
79682fc28fbSQu Wenruo 	sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
79782fc28fbSQu Wenruo 	type = btrfs_chunk_type(leaf, chunk);
79885d07fbeSDaniel Xu 	raid_index = btrfs_bg_flags_to_raid_index(type);
79985d07fbeSDaniel Xu 	ncopies = btrfs_raid_array[raid_index].ncopies;
80085d07fbeSDaniel Xu 	nparity = btrfs_raid_array[raid_index].nparity;
80182fc28fbSQu Wenruo 
802c7c01a4aSDavid Sterba 	if (unlikely(!num_stripes)) {
803d001e4a3SDavid Sterba 		chunk_err(leaf, chunk, logical,
804f1140243SQu Wenruo 			  "invalid chunk num_stripes, have %u", num_stripes);
805bf871c3bSQu Wenruo 		return -EUCLEAN;
80682fc28fbSQu Wenruo 	}
807c7c01a4aSDavid Sterba 	if (unlikely(num_stripes < ncopies)) {
80885d07fbeSDaniel Xu 		chunk_err(leaf, chunk, logical,
80985d07fbeSDaniel Xu 			  "invalid chunk num_stripes < ncopies, have %u < %d",
81085d07fbeSDaniel Xu 			  num_stripes, ncopies);
81185d07fbeSDaniel Xu 		return -EUCLEAN;
81285d07fbeSDaniel Xu 	}
813c7c01a4aSDavid Sterba 	if (unlikely(nparity && num_stripes == nparity)) {
81485d07fbeSDaniel Xu 		chunk_err(leaf, chunk, logical,
81585d07fbeSDaniel Xu 			  "invalid chunk num_stripes == nparity, have %u == %d",
81685d07fbeSDaniel Xu 			  num_stripes, nparity);
81785d07fbeSDaniel Xu 		return -EUCLEAN;
81885d07fbeSDaniel Xu 	}
819c7c01a4aSDavid Sterba 	if (unlikely(!IS_ALIGNED(logical, fs_info->sectorsize))) {
820d001e4a3SDavid Sterba 		chunk_err(leaf, chunk, logical,
821f1140243SQu Wenruo 		"invalid chunk logical, have %llu should aligned to %u",
822f1140243SQu Wenruo 			  logical, fs_info->sectorsize);
823bf871c3bSQu Wenruo 		return -EUCLEAN;
82482fc28fbSQu Wenruo 	}
825c7c01a4aSDavid Sterba 	if (unlikely(btrfs_chunk_sector_size(leaf, chunk) != fs_info->sectorsize)) {
826d001e4a3SDavid Sterba 		chunk_err(leaf, chunk, logical,
827f1140243SQu Wenruo 			  "invalid chunk sectorsize, have %u expect %u",
828f1140243SQu Wenruo 			  btrfs_chunk_sector_size(leaf, chunk),
829f1140243SQu Wenruo 			  fs_info->sectorsize);
830bf871c3bSQu Wenruo 		return -EUCLEAN;
83182fc28fbSQu Wenruo 	}
832c7c01a4aSDavid Sterba 	if (unlikely(!length || !IS_ALIGNED(length, fs_info->sectorsize))) {
833d001e4a3SDavid Sterba 		chunk_err(leaf, chunk, logical,
834f1140243SQu Wenruo 			  "invalid chunk length, have %llu", length);
835bf871c3bSQu Wenruo 		return -EUCLEAN;
83682fc28fbSQu Wenruo 	}
837347fb0cfSSu Yue 	if (unlikely(check_add_overflow(logical, length, &chunk_end))) {
838347fb0cfSSu Yue 		chunk_err(leaf, chunk, logical,
839347fb0cfSSu Yue "invalid chunk logical start and length, have logical start %llu length %llu",
840347fb0cfSSu Yue 			  logical, length);
841347fb0cfSSu Yue 		return -EUCLEAN;
842347fb0cfSSu Yue 	}
843c7c01a4aSDavid Sterba 	if (unlikely(!is_power_of_2(stripe_len) || stripe_len != BTRFS_STRIPE_LEN)) {
844d001e4a3SDavid Sterba 		chunk_err(leaf, chunk, logical,
845f1140243SQu Wenruo 			  "invalid chunk stripe length: %llu",
84682fc28fbSQu Wenruo 			  stripe_len);
847bf871c3bSQu Wenruo 		return -EUCLEAN;
84882fc28fbSQu Wenruo 	}
849c7c01a4aSDavid Sterba 	if (unlikely(type & ~(BTRFS_BLOCK_GROUP_TYPE_MASK |
850c7c01a4aSDavid Sterba 			      BTRFS_BLOCK_GROUP_PROFILE_MASK))) {
851d001e4a3SDavid Sterba 		chunk_err(leaf, chunk, logical,
852f1140243SQu Wenruo 			  "unrecognized chunk type: 0x%llx",
85382fc28fbSQu Wenruo 			  ~(BTRFS_BLOCK_GROUP_TYPE_MASK |
85482fc28fbSQu Wenruo 			    BTRFS_BLOCK_GROUP_PROFILE_MASK) &
85582fc28fbSQu Wenruo 			  btrfs_chunk_type(leaf, chunk));
856bf871c3bSQu Wenruo 		return -EUCLEAN;
85782fc28fbSQu Wenruo 	}
85882fc28fbSQu Wenruo 
859c7c01a4aSDavid Sterba 	if (unlikely(!has_single_bit_set(type & BTRFS_BLOCK_GROUP_PROFILE_MASK) &&
860c7c01a4aSDavid Sterba 		     (type & BTRFS_BLOCK_GROUP_PROFILE_MASK) != 0)) {
861d001e4a3SDavid Sterba 		chunk_err(leaf, chunk, logical,
86280e46cf2SQu Wenruo 		"invalid chunk profile flag: 0x%llx, expect 0 or 1 bit set",
86380e46cf2SQu Wenruo 			  type & BTRFS_BLOCK_GROUP_PROFILE_MASK);
86480e46cf2SQu Wenruo 		return -EUCLEAN;
86580e46cf2SQu Wenruo 	}
866c7c01a4aSDavid Sterba 	if (unlikely((type & BTRFS_BLOCK_GROUP_TYPE_MASK) == 0)) {
867d001e4a3SDavid Sterba 		chunk_err(leaf, chunk, logical,
868f1140243SQu Wenruo 	"missing chunk type flag, have 0x%llx one bit must be set in 0x%llx",
869f1140243SQu Wenruo 			  type, BTRFS_BLOCK_GROUP_TYPE_MASK);
870bf871c3bSQu Wenruo 		return -EUCLEAN;
87182fc28fbSQu Wenruo 	}
87282fc28fbSQu Wenruo 
873c7c01a4aSDavid Sterba 	if (unlikely((type & BTRFS_BLOCK_GROUP_SYSTEM) &&
874c7c01a4aSDavid Sterba 		     (type & (BTRFS_BLOCK_GROUP_METADATA |
875c7c01a4aSDavid Sterba 			      BTRFS_BLOCK_GROUP_DATA)))) {
876d001e4a3SDavid Sterba 		chunk_err(leaf, chunk, logical,
877f1140243SQu Wenruo 			  "system chunk with data or metadata type: 0x%llx",
878f1140243SQu Wenruo 			  type);
879bf871c3bSQu Wenruo 		return -EUCLEAN;
88082fc28fbSQu Wenruo 	}
88182fc28fbSQu Wenruo 
88282fc28fbSQu Wenruo 	features = btrfs_super_incompat_flags(fs_info->super_copy);
88382fc28fbSQu Wenruo 	if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
88482fc28fbSQu Wenruo 		mixed = true;
88582fc28fbSQu Wenruo 
88682fc28fbSQu Wenruo 	if (!mixed) {
887c7c01a4aSDavid Sterba 		if (unlikely((type & BTRFS_BLOCK_GROUP_METADATA) &&
888c7c01a4aSDavid Sterba 			     (type & BTRFS_BLOCK_GROUP_DATA))) {
889d001e4a3SDavid Sterba 			chunk_err(leaf, chunk, logical,
89082fc28fbSQu Wenruo 			"mixed chunk type in non-mixed mode: 0x%llx", type);
891bf871c3bSQu Wenruo 			return -EUCLEAN;
89282fc28fbSQu Wenruo 		}
89382fc28fbSQu Wenruo 	}
89482fc28fbSQu Wenruo 
8950ac6e06bSDavid Sterba 	if (unlikely((type & BTRFS_BLOCK_GROUP_RAID10 &&
8960ac6e06bSDavid Sterba 		      sub_stripes != btrfs_raid_array[BTRFS_RAID_RAID10].sub_stripes) ||
8970ac6e06bSDavid Sterba 		     (type & BTRFS_BLOCK_GROUP_RAID1 &&
8980ac6e06bSDavid Sterba 		      num_stripes != btrfs_raid_array[BTRFS_RAID_RAID1].devs_min) ||
8996c154ba4SDavid Sterba 		     (type & BTRFS_BLOCK_GROUP_RAID1C3 &&
9006c154ba4SDavid Sterba 		      num_stripes != btrfs_raid_array[BTRFS_RAID_RAID1C3].devs_min) ||
9016c154ba4SDavid Sterba 		     (type & BTRFS_BLOCK_GROUP_RAID1C4 &&
9026c154ba4SDavid Sterba 		      num_stripes != btrfs_raid_array[BTRFS_RAID_RAID1C4].devs_min) ||
9030ac6e06bSDavid Sterba 		     (type & BTRFS_BLOCK_GROUP_RAID5 &&
9040ac6e06bSDavid Sterba 		      num_stripes < btrfs_raid_array[BTRFS_RAID_RAID5].devs_min) ||
9050ac6e06bSDavid Sterba 		     (type & BTRFS_BLOCK_GROUP_RAID6 &&
9060ac6e06bSDavid Sterba 		      num_stripes < btrfs_raid_array[BTRFS_RAID_RAID6].devs_min) ||
9070ac6e06bSDavid Sterba 		     (type & BTRFS_BLOCK_GROUP_DUP &&
9080ac6e06bSDavid Sterba 		      num_stripes != btrfs_raid_array[BTRFS_RAID_DUP].dev_stripes) ||
909c7c01a4aSDavid Sterba 		     ((type & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 &&
9100ac6e06bSDavid Sterba 		      num_stripes != btrfs_raid_array[BTRFS_RAID_SINGLE].dev_stripes))) {
911d001e4a3SDavid Sterba 		chunk_err(leaf, chunk, logical,
91282fc28fbSQu Wenruo 			"invalid num_stripes:sub_stripes %u:%u for profile %llu",
91382fc28fbSQu Wenruo 			num_stripes, sub_stripes,
91482fc28fbSQu Wenruo 			type & BTRFS_BLOCK_GROUP_PROFILE_MASK);
915bf871c3bSQu Wenruo 		return -EUCLEAN;
91682fc28fbSQu Wenruo 	}
91782fc28fbSQu Wenruo 
91882fc28fbSQu Wenruo 	return 0;
91982fc28fbSQu Wenruo }
92082fc28fbSQu Wenruo 
921f6d2a5c2SQu Wenruo /*
922f6d2a5c2SQu Wenruo  * Enhanced version of chunk item checker.
923f6d2a5c2SQu Wenruo  *
924f6d2a5c2SQu Wenruo  * The common btrfs_check_chunk_valid() doesn't check item size since it needs
925f6d2a5c2SQu Wenruo  * to work on super block sys_chunk_array which doesn't have full item ptr.
926f6d2a5c2SQu Wenruo  */
927f6d2a5c2SQu Wenruo static int check_leaf_chunk_item(struct extent_buffer *leaf,
928f6d2a5c2SQu Wenruo 				 struct btrfs_chunk *chunk,
929f6d2a5c2SQu Wenruo 				 struct btrfs_key *key, int slot)
930f6d2a5c2SQu Wenruo {
931f6d2a5c2SQu Wenruo 	int num_stripes;
932f6d2a5c2SQu Wenruo 
9333212fa14SJosef Bacik 	if (unlikely(btrfs_item_size(leaf, slot) < sizeof(struct btrfs_chunk))) {
934f6d2a5c2SQu Wenruo 		chunk_err(leaf, chunk, key->offset,
935f6d2a5c2SQu Wenruo 			"invalid chunk item size: have %u expect [%zu, %u)",
9363212fa14SJosef Bacik 			btrfs_item_size(leaf, slot),
937f6d2a5c2SQu Wenruo 			sizeof(struct btrfs_chunk),
938f6d2a5c2SQu Wenruo 			BTRFS_LEAF_DATA_SIZE(leaf->fs_info));
939f6d2a5c2SQu Wenruo 		return -EUCLEAN;
940f6d2a5c2SQu Wenruo 	}
941f6d2a5c2SQu Wenruo 
942f6d2a5c2SQu Wenruo 	num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
943f6d2a5c2SQu Wenruo 	/* Let btrfs_check_chunk_valid() handle this error type */
944f6d2a5c2SQu Wenruo 	if (num_stripes == 0)
945f6d2a5c2SQu Wenruo 		goto out;
946f6d2a5c2SQu Wenruo 
947c7c01a4aSDavid Sterba 	if (unlikely(btrfs_chunk_item_size(num_stripes) !=
9483212fa14SJosef Bacik 		     btrfs_item_size(leaf, slot))) {
949f6d2a5c2SQu Wenruo 		chunk_err(leaf, chunk, key->offset,
950f6d2a5c2SQu Wenruo 			"invalid chunk item size: have %u expect %lu",
9513212fa14SJosef Bacik 			btrfs_item_size(leaf, slot),
952f6d2a5c2SQu Wenruo 			btrfs_chunk_item_size(num_stripes));
953f6d2a5c2SQu Wenruo 		return -EUCLEAN;
954f6d2a5c2SQu Wenruo 	}
955f6d2a5c2SQu Wenruo out:
956f6d2a5c2SQu Wenruo 	return btrfs_check_chunk_valid(leaf, chunk, key->offset);
957f6d2a5c2SQu Wenruo }
958f6d2a5c2SQu Wenruo 
9595617ed80SDavid Sterba __printf(3, 4)
960ab4ba2e1SQu Wenruo __cold
9615617ed80SDavid Sterba static void dev_item_err(const struct extent_buffer *eb, int slot,
962ab4ba2e1SQu Wenruo 			 const char *fmt, ...)
963ab4ba2e1SQu Wenruo {
964ab4ba2e1SQu Wenruo 	struct btrfs_key key;
965ab4ba2e1SQu Wenruo 	struct va_format vaf;
966ab4ba2e1SQu Wenruo 	va_list args;
967ab4ba2e1SQu Wenruo 
968ab4ba2e1SQu Wenruo 	btrfs_item_key_to_cpu(eb, &key, slot);
969ab4ba2e1SQu Wenruo 	va_start(args, fmt);
970ab4ba2e1SQu Wenruo 
971ab4ba2e1SQu Wenruo 	vaf.fmt = fmt;
972ab4ba2e1SQu Wenruo 	vaf.va = &args;
973ab4ba2e1SQu Wenruo 
9745617ed80SDavid Sterba 	btrfs_crit(eb->fs_info,
975ab4ba2e1SQu Wenruo 	"corrupt %s: root=%llu block=%llu slot=%d devid=%llu %pV",
976ab4ba2e1SQu Wenruo 		btrfs_header_level(eb) == 0 ? "leaf" : "node",
977ab4ba2e1SQu Wenruo 		btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot,
978ab4ba2e1SQu Wenruo 		key.objectid, &vaf);
979ab4ba2e1SQu Wenruo 	va_end(args);
980ab4ba2e1SQu Wenruo }
981ab4ba2e1SQu Wenruo 
982412a2312SDavid Sterba static int check_dev_item(struct extent_buffer *leaf,
983ab4ba2e1SQu Wenruo 			  struct btrfs_key *key, int slot)
984ab4ba2e1SQu Wenruo {
985ab4ba2e1SQu Wenruo 	struct btrfs_dev_item *ditem;
986ea1d1ca4SSu Yue 	const u32 item_size = btrfs_item_size(leaf, slot);
987ab4ba2e1SQu Wenruo 
988c7c01a4aSDavid Sterba 	if (unlikely(key->objectid != BTRFS_DEV_ITEMS_OBJECTID)) {
9895617ed80SDavid Sterba 		dev_item_err(leaf, slot,
990ab4ba2e1SQu Wenruo 			     "invalid objectid: has=%llu expect=%llu",
991ab4ba2e1SQu Wenruo 			     key->objectid, BTRFS_DEV_ITEMS_OBJECTID);
992ab4ba2e1SQu Wenruo 		return -EUCLEAN;
993ab4ba2e1SQu Wenruo 	}
994ea1d1ca4SSu Yue 
995ea1d1ca4SSu Yue 	if (unlikely(item_size != sizeof(*ditem))) {
996ea1d1ca4SSu Yue 		dev_item_err(leaf, slot, "invalid item size: has %u expect %zu",
997ea1d1ca4SSu Yue 			     item_size, sizeof(*ditem));
998ea1d1ca4SSu Yue 		return -EUCLEAN;
999ea1d1ca4SSu Yue 	}
1000ea1d1ca4SSu Yue 
1001ab4ba2e1SQu Wenruo 	ditem = btrfs_item_ptr(leaf, slot, struct btrfs_dev_item);
1002c7c01a4aSDavid Sterba 	if (unlikely(btrfs_device_id(leaf, ditem) != key->offset)) {
10035617ed80SDavid Sterba 		dev_item_err(leaf, slot,
1004ab4ba2e1SQu Wenruo 			     "devid mismatch: key has=%llu item has=%llu",
1005ab4ba2e1SQu Wenruo 			     key->offset, btrfs_device_id(leaf, ditem));
1006ab4ba2e1SQu Wenruo 		return -EUCLEAN;
1007ab4ba2e1SQu Wenruo 	}
1008ab4ba2e1SQu Wenruo 
1009ab4ba2e1SQu Wenruo 	/*
1010ab4ba2e1SQu Wenruo 	 * For device total_bytes, we don't have reliable way to check it, as
1011ab4ba2e1SQu Wenruo 	 * it can be 0 for device removal. Device size check can only be done
1012ab4ba2e1SQu Wenruo 	 * by dev extents check.
1013ab4ba2e1SQu Wenruo 	 */
1014c7c01a4aSDavid Sterba 	if (unlikely(btrfs_device_bytes_used(leaf, ditem) >
1015c7c01a4aSDavid Sterba 		     btrfs_device_total_bytes(leaf, ditem))) {
10165617ed80SDavid Sterba 		dev_item_err(leaf, slot,
1017ab4ba2e1SQu Wenruo 			     "invalid bytes used: have %llu expect [0, %llu]",
1018ab4ba2e1SQu Wenruo 			     btrfs_device_bytes_used(leaf, ditem),
1019ab4ba2e1SQu Wenruo 			     btrfs_device_total_bytes(leaf, ditem));
1020ab4ba2e1SQu Wenruo 		return -EUCLEAN;
1021ab4ba2e1SQu Wenruo 	}
1022ab4ba2e1SQu Wenruo 	/*
1023ab4ba2e1SQu Wenruo 	 * Remaining members like io_align/type/gen/dev_group aren't really
1024ab4ba2e1SQu Wenruo 	 * utilized.  Skip them to make later usage of them easier.
1025ab4ba2e1SQu Wenruo 	 */
1026ab4ba2e1SQu Wenruo 	return 0;
1027ab4ba2e1SQu Wenruo }
1028ab4ba2e1SQu Wenruo 
102939e57f49SDavid Sterba static int check_inode_item(struct extent_buffer *leaf,
1030496245caSQu Wenruo 			    struct btrfs_key *key, int slot)
1031496245caSQu Wenruo {
103239e57f49SDavid Sterba 	struct btrfs_fs_info *fs_info = leaf->fs_info;
1033496245caSQu Wenruo 	struct btrfs_inode_item *iitem;
1034496245caSQu Wenruo 	u64 super_gen = btrfs_super_generation(fs_info->super_copy);
1035496245caSQu Wenruo 	u32 valid_mask = (S_IFMT | S_ISUID | S_ISGID | S_ISVTX | 0777);
10360c982944SSu Yue 	const u32 item_size = btrfs_item_size(leaf, slot);
1037496245caSQu Wenruo 	u32 mode;
1038c23c77b0SQu Wenruo 	int ret;
103977eea05eSBoris Burkov 	u32 flags;
104077eea05eSBoris Burkov 	u32 ro_flags;
1041496245caSQu Wenruo 
1042c23c77b0SQu Wenruo 	ret = check_inode_key(leaf, key, slot);
1043c7c01a4aSDavid Sterba 	if (unlikely(ret < 0))
1044c23c77b0SQu Wenruo 		return ret;
1045c23c77b0SQu Wenruo 
10460c982944SSu Yue 	if (unlikely(item_size != sizeof(*iitem))) {
10470c982944SSu Yue 		generic_err(leaf, slot, "invalid item size: has %u expect %zu",
10480c982944SSu Yue 			    item_size, sizeof(*iitem));
10490c982944SSu Yue 		return -EUCLEAN;
10500c982944SSu Yue 	}
10510c982944SSu Yue 
1052496245caSQu Wenruo 	iitem = btrfs_item_ptr(leaf, slot, struct btrfs_inode_item);
1053496245caSQu Wenruo 
1054496245caSQu Wenruo 	/* Here we use super block generation + 1 to handle log tree */
1055c7c01a4aSDavid Sterba 	if (unlikely(btrfs_inode_generation(leaf, iitem) > super_gen + 1)) {
1056c3053ebbSQu Wenruo 		inode_item_err(leaf, slot,
1057496245caSQu Wenruo 			"invalid inode generation: has %llu expect (0, %llu]",
1058496245caSQu Wenruo 			       btrfs_inode_generation(leaf, iitem),
1059496245caSQu Wenruo 			       super_gen + 1);
1060496245caSQu Wenruo 		return -EUCLEAN;
1061496245caSQu Wenruo 	}
1062496245caSQu Wenruo 	/* Note for ROOT_TREE_DIR_ITEM, mkfs could set its transid 0 */
1063c7c01a4aSDavid Sterba 	if (unlikely(btrfs_inode_transid(leaf, iitem) > super_gen + 1)) {
1064c3053ebbSQu Wenruo 		inode_item_err(leaf, slot,
1065f96d6960SQu Wenruo 			"invalid inode transid: has %llu expect [0, %llu]",
1066496245caSQu Wenruo 			       btrfs_inode_transid(leaf, iitem), super_gen + 1);
1067496245caSQu Wenruo 		return -EUCLEAN;
1068496245caSQu Wenruo 	}
1069496245caSQu Wenruo 
1070496245caSQu Wenruo 	/*
1071496245caSQu Wenruo 	 * For size and nbytes it's better not to be too strict, as for dir
1072496245caSQu Wenruo 	 * item its size/nbytes can easily get wrong, but doesn't affect
1073496245caSQu Wenruo 	 * anything in the fs. So here we skip the check.
1074496245caSQu Wenruo 	 */
1075496245caSQu Wenruo 	mode = btrfs_inode_mode(leaf, iitem);
1076c7c01a4aSDavid Sterba 	if (unlikely(mode & ~valid_mask)) {
1077c3053ebbSQu Wenruo 		inode_item_err(leaf, slot,
1078496245caSQu Wenruo 			       "unknown mode bit detected: 0x%x",
1079496245caSQu Wenruo 			       mode & ~valid_mask);
1080496245caSQu Wenruo 		return -EUCLEAN;
1081496245caSQu Wenruo 	}
1082496245caSQu Wenruo 
1083496245caSQu Wenruo 	/*
1084c1499166SDavid Sterba 	 * S_IFMT is not bit mapped so we can't completely rely on
1085c1499166SDavid Sterba 	 * is_power_of_2/has_single_bit_set, but it can save us from checking
1086c1499166SDavid Sterba 	 * FIFO/CHR/DIR/REG.  Only needs to check BLK, LNK and SOCKS
1087496245caSQu Wenruo 	 */
1088c1499166SDavid Sterba 	if (!has_single_bit_set(mode & S_IFMT)) {
1089c7c01a4aSDavid Sterba 		if (unlikely(!S_ISLNK(mode) && !S_ISBLK(mode) && !S_ISSOCK(mode))) {
1090c3053ebbSQu Wenruo 			inode_item_err(leaf, slot,
1091496245caSQu Wenruo 			"invalid mode: has 0%o expect valid S_IF* bit(s)",
1092496245caSQu Wenruo 				       mode & S_IFMT);
1093496245caSQu Wenruo 			return -EUCLEAN;
1094496245caSQu Wenruo 		}
1095496245caSQu Wenruo 	}
1096c7c01a4aSDavid Sterba 	if (unlikely(S_ISDIR(mode) && btrfs_inode_nlink(leaf, iitem) > 1)) {
1097c3053ebbSQu Wenruo 		inode_item_err(leaf, slot,
1098496245caSQu Wenruo 		       "invalid nlink: has %u expect no more than 1 for dir",
1099496245caSQu Wenruo 			btrfs_inode_nlink(leaf, iitem));
1100496245caSQu Wenruo 		return -EUCLEAN;
1101496245caSQu Wenruo 	}
110277eea05eSBoris Burkov 	btrfs_inode_split_flags(btrfs_inode_flags(leaf, iitem), &flags, &ro_flags);
110377eea05eSBoris Burkov 	if (unlikely(flags & ~BTRFS_INODE_FLAG_MASK)) {
1104c3053ebbSQu Wenruo 		inode_item_err(leaf, slot,
110577eea05eSBoris Burkov 			       "unknown incompat flags detected: 0x%x", flags);
110677eea05eSBoris Burkov 		return -EUCLEAN;
110777eea05eSBoris Burkov 	}
110877eea05eSBoris Burkov 	if (unlikely(!sb_rdonly(fs_info->sb) &&
110977eea05eSBoris Burkov 		     (ro_flags & ~BTRFS_INODE_RO_FLAG_MASK))) {
111077eea05eSBoris Burkov 		inode_item_err(leaf, slot,
111177eea05eSBoris Burkov 			"unknown ro-compat flags detected on writeable mount: 0x%x",
111277eea05eSBoris Burkov 			ro_flags);
1113496245caSQu Wenruo 		return -EUCLEAN;
1114496245caSQu Wenruo 	}
1115496245caSQu Wenruo 	return 0;
1116496245caSQu Wenruo }
1117496245caSQu Wenruo 
1118259ee775SQu Wenruo static int check_root_item(struct extent_buffer *leaf, struct btrfs_key *key,
1119259ee775SQu Wenruo 			   int slot)
1120259ee775SQu Wenruo {
1121259ee775SQu Wenruo 	struct btrfs_fs_info *fs_info = leaf->fs_info;
11221465af12SQu Wenruo 	struct btrfs_root_item ri = { 0 };
1123259ee775SQu Wenruo 	const u64 valid_root_flags = BTRFS_ROOT_SUBVOL_RDONLY |
1124259ee775SQu Wenruo 				     BTRFS_ROOT_SUBVOL_DEAD;
112557a0e674SQu Wenruo 	int ret;
1126259ee775SQu Wenruo 
112757a0e674SQu Wenruo 	ret = check_root_key(leaf, key, slot);
1128c7c01a4aSDavid Sterba 	if (unlikely(ret < 0))
112957a0e674SQu Wenruo 		return ret;
1130259ee775SQu Wenruo 
11313212fa14SJosef Bacik 	if (unlikely(btrfs_item_size(leaf, slot) != sizeof(ri) &&
11323212fa14SJosef Bacik 		     btrfs_item_size(leaf, slot) !=
1133c7c01a4aSDavid Sterba 		     btrfs_legacy_root_item_size())) {
1134259ee775SQu Wenruo 		generic_err(leaf, slot,
11351465af12SQu Wenruo 			    "invalid root item size, have %u expect %zu or %u",
11363212fa14SJosef Bacik 			    btrfs_item_size(leaf, slot), sizeof(ri),
11371465af12SQu Wenruo 			    btrfs_legacy_root_item_size());
11381a49a97dSDaniel Xu 		return -EUCLEAN;
1139259ee775SQu Wenruo 	}
1140259ee775SQu Wenruo 
11411465af12SQu Wenruo 	/*
11421465af12SQu Wenruo 	 * For legacy root item, the members starting at generation_v2 will be
11431465af12SQu Wenruo 	 * all filled with 0.
11441465af12SQu Wenruo 	 * And since we allow geneartion_v2 as 0, it will still pass the check.
11451465af12SQu Wenruo 	 */
1146259ee775SQu Wenruo 	read_extent_buffer(leaf, &ri, btrfs_item_ptr_offset(leaf, slot),
11473212fa14SJosef Bacik 			   btrfs_item_size(leaf, slot));
1148259ee775SQu Wenruo 
1149259ee775SQu Wenruo 	/* Generation related */
1150c7c01a4aSDavid Sterba 	if (unlikely(btrfs_root_generation(&ri) >
1151c7c01a4aSDavid Sterba 		     btrfs_super_generation(fs_info->super_copy) + 1)) {
1152259ee775SQu Wenruo 		generic_err(leaf, slot,
1153259ee775SQu Wenruo 			"invalid root generation, have %llu expect (0, %llu]",
1154259ee775SQu Wenruo 			    btrfs_root_generation(&ri),
1155259ee775SQu Wenruo 			    btrfs_super_generation(fs_info->super_copy) + 1);
1156259ee775SQu Wenruo 		return -EUCLEAN;
1157259ee775SQu Wenruo 	}
1158c7c01a4aSDavid Sterba 	if (unlikely(btrfs_root_generation_v2(&ri) >
1159c7c01a4aSDavid Sterba 		     btrfs_super_generation(fs_info->super_copy) + 1)) {
1160259ee775SQu Wenruo 		generic_err(leaf, slot,
1161259ee775SQu Wenruo 		"invalid root v2 generation, have %llu expect (0, %llu]",
1162259ee775SQu Wenruo 			    btrfs_root_generation_v2(&ri),
1163259ee775SQu Wenruo 			    btrfs_super_generation(fs_info->super_copy) + 1);
1164259ee775SQu Wenruo 		return -EUCLEAN;
1165259ee775SQu Wenruo 	}
1166c7c01a4aSDavid Sterba 	if (unlikely(btrfs_root_last_snapshot(&ri) >
1167c7c01a4aSDavid Sterba 		     btrfs_super_generation(fs_info->super_copy) + 1)) {
1168259ee775SQu Wenruo 		generic_err(leaf, slot,
1169259ee775SQu Wenruo 		"invalid root last_snapshot, have %llu expect (0, %llu]",
1170259ee775SQu Wenruo 			    btrfs_root_last_snapshot(&ri),
1171259ee775SQu Wenruo 			    btrfs_super_generation(fs_info->super_copy) + 1);
1172259ee775SQu Wenruo 		return -EUCLEAN;
1173259ee775SQu Wenruo 	}
1174259ee775SQu Wenruo 
1175259ee775SQu Wenruo 	/* Alignment and level check */
1176c7c01a4aSDavid Sterba 	if (unlikely(!IS_ALIGNED(btrfs_root_bytenr(&ri), fs_info->sectorsize))) {
1177259ee775SQu Wenruo 		generic_err(leaf, slot,
1178259ee775SQu Wenruo 		"invalid root bytenr, have %llu expect to be aligned to %u",
1179259ee775SQu Wenruo 			    btrfs_root_bytenr(&ri), fs_info->sectorsize);
1180259ee775SQu Wenruo 		return -EUCLEAN;
1181259ee775SQu Wenruo 	}
1182c7c01a4aSDavid Sterba 	if (unlikely(btrfs_root_level(&ri) >= BTRFS_MAX_LEVEL)) {
1183259ee775SQu Wenruo 		generic_err(leaf, slot,
1184259ee775SQu Wenruo 			    "invalid root level, have %u expect [0, %u]",
1185259ee775SQu Wenruo 			    btrfs_root_level(&ri), BTRFS_MAX_LEVEL - 1);
1186259ee775SQu Wenruo 		return -EUCLEAN;
1187259ee775SQu Wenruo 	}
1188c7c01a4aSDavid Sterba 	if (unlikely(btrfs_root_drop_level(&ri) >= BTRFS_MAX_LEVEL)) {
1189259ee775SQu Wenruo 		generic_err(leaf, slot,
1190259ee775SQu Wenruo 			    "invalid root level, have %u expect [0, %u]",
1191c8422684SDavid Sterba 			    btrfs_root_drop_level(&ri), BTRFS_MAX_LEVEL - 1);
1192259ee775SQu Wenruo 		return -EUCLEAN;
1193259ee775SQu Wenruo 	}
1194259ee775SQu Wenruo 
1195259ee775SQu Wenruo 	/* Flags check */
1196c7c01a4aSDavid Sterba 	if (unlikely(btrfs_root_flags(&ri) & ~valid_root_flags)) {
1197259ee775SQu Wenruo 		generic_err(leaf, slot,
1198259ee775SQu Wenruo 			    "invalid root flags, have 0x%llx expect mask 0x%llx",
1199259ee775SQu Wenruo 			    btrfs_root_flags(&ri), valid_root_flags);
1200259ee775SQu Wenruo 		return -EUCLEAN;
1201259ee775SQu Wenruo 	}
1202259ee775SQu Wenruo 	return 0;
1203259ee775SQu Wenruo }
1204259ee775SQu Wenruo 
1205f82d1c7cSQu Wenruo __printf(3,4)
1206f82d1c7cSQu Wenruo __cold
1207f82d1c7cSQu Wenruo static void extent_err(const struct extent_buffer *eb, int slot,
1208f82d1c7cSQu Wenruo 		       const char *fmt, ...)
1209f82d1c7cSQu Wenruo {
1210f82d1c7cSQu Wenruo 	struct btrfs_key key;
1211f82d1c7cSQu Wenruo 	struct va_format vaf;
1212f82d1c7cSQu Wenruo 	va_list args;
1213f82d1c7cSQu Wenruo 	u64 bytenr;
1214f82d1c7cSQu Wenruo 	u64 len;
1215f82d1c7cSQu Wenruo 
1216f82d1c7cSQu Wenruo 	btrfs_item_key_to_cpu(eb, &key, slot);
1217f82d1c7cSQu Wenruo 	bytenr = key.objectid;
1218e2406a6fSQu Wenruo 	if (key.type == BTRFS_METADATA_ITEM_KEY ||
1219e2406a6fSQu Wenruo 	    key.type == BTRFS_TREE_BLOCK_REF_KEY ||
1220e2406a6fSQu Wenruo 	    key.type == BTRFS_SHARED_BLOCK_REF_KEY)
1221f82d1c7cSQu Wenruo 		len = eb->fs_info->nodesize;
1222f82d1c7cSQu Wenruo 	else
1223f82d1c7cSQu Wenruo 		len = key.offset;
1224f82d1c7cSQu Wenruo 	va_start(args, fmt);
1225f82d1c7cSQu Wenruo 
1226f82d1c7cSQu Wenruo 	vaf.fmt = fmt;
1227f82d1c7cSQu Wenruo 	vaf.va = &args;
1228f82d1c7cSQu Wenruo 
1229f82d1c7cSQu Wenruo 	btrfs_crit(eb->fs_info,
1230f82d1c7cSQu Wenruo 	"corrupt %s: block=%llu slot=%d extent bytenr=%llu len=%llu %pV",
1231f82d1c7cSQu Wenruo 		btrfs_header_level(eb) == 0 ? "leaf" : "node",
1232f82d1c7cSQu Wenruo 		eb->start, slot, bytenr, len, &vaf);
1233f82d1c7cSQu Wenruo 	va_end(args);
1234f82d1c7cSQu Wenruo }
1235f82d1c7cSQu Wenruo 
1236f82d1c7cSQu Wenruo static int check_extent_item(struct extent_buffer *leaf,
1237899b7f69SJosef Bacik 			     struct btrfs_key *key, int slot,
1238899b7f69SJosef Bacik 			     struct btrfs_key *prev_key)
1239f82d1c7cSQu Wenruo {
1240f82d1c7cSQu Wenruo 	struct btrfs_fs_info *fs_info = leaf->fs_info;
1241f82d1c7cSQu Wenruo 	struct btrfs_extent_item *ei;
1242f82d1c7cSQu Wenruo 	bool is_tree_block = false;
1243f82d1c7cSQu Wenruo 	unsigned long ptr;	/* Current pointer inside inline refs */
1244f82d1c7cSQu Wenruo 	unsigned long end;	/* Extent item end */
12453212fa14SJosef Bacik 	const u32 item_size = btrfs_item_size(leaf, slot);
1246f82d1c7cSQu Wenruo 	u64 flags;
1247f82d1c7cSQu Wenruo 	u64 generation;
1248f82d1c7cSQu Wenruo 	u64 total_refs;		/* Total refs in btrfs_extent_item */
1249f82d1c7cSQu Wenruo 	u64 inline_refs = 0;	/* found total inline refs */
1250f82d1c7cSQu Wenruo 
1251c7c01a4aSDavid Sterba 	if (unlikely(key->type == BTRFS_METADATA_ITEM_KEY &&
1252c7c01a4aSDavid Sterba 		     !btrfs_fs_incompat(fs_info, SKINNY_METADATA))) {
1253f82d1c7cSQu Wenruo 		generic_err(leaf, slot,
1254f82d1c7cSQu Wenruo "invalid key type, METADATA_ITEM type invalid when SKINNY_METADATA feature disabled");
1255f82d1c7cSQu Wenruo 		return -EUCLEAN;
1256f82d1c7cSQu Wenruo 	}
1257f82d1c7cSQu Wenruo 	/* key->objectid is the bytenr for both key types */
1258c7c01a4aSDavid Sterba 	if (unlikely(!IS_ALIGNED(key->objectid, fs_info->sectorsize))) {
1259f82d1c7cSQu Wenruo 		generic_err(leaf, slot,
1260f82d1c7cSQu Wenruo 		"invalid key objectid, have %llu expect to be aligned to %u",
1261f82d1c7cSQu Wenruo 			   key->objectid, fs_info->sectorsize);
1262f82d1c7cSQu Wenruo 		return -EUCLEAN;
1263f82d1c7cSQu Wenruo 	}
1264f82d1c7cSQu Wenruo 
1265f82d1c7cSQu Wenruo 	/* key->offset is tree level for METADATA_ITEM_KEY */
1266c7c01a4aSDavid Sterba 	if (unlikely(key->type == BTRFS_METADATA_ITEM_KEY &&
1267c7c01a4aSDavid Sterba 		     key->offset >= BTRFS_MAX_LEVEL)) {
1268f82d1c7cSQu Wenruo 		extent_err(leaf, slot,
1269f82d1c7cSQu Wenruo 			   "invalid tree level, have %llu expect [0, %u]",
1270f82d1c7cSQu Wenruo 			   key->offset, BTRFS_MAX_LEVEL - 1);
1271f82d1c7cSQu Wenruo 		return -EUCLEAN;
1272f82d1c7cSQu Wenruo 	}
1273f82d1c7cSQu Wenruo 
1274f82d1c7cSQu Wenruo 	/*
1275f82d1c7cSQu Wenruo 	 * EXTENT/METADATA_ITEM consists of:
1276f82d1c7cSQu Wenruo 	 * 1) One btrfs_extent_item
1277f82d1c7cSQu Wenruo 	 *    Records the total refs, type and generation of the extent.
1278f82d1c7cSQu Wenruo 	 *
1279f82d1c7cSQu Wenruo 	 * 2) One btrfs_tree_block_info (for EXTENT_ITEM and tree backref only)
1280f82d1c7cSQu Wenruo 	 *    Records the first key and level of the tree block.
1281f82d1c7cSQu Wenruo 	 *
1282f82d1c7cSQu Wenruo 	 * 2) Zero or more btrfs_extent_inline_ref(s)
1283f82d1c7cSQu Wenruo 	 *    Each inline ref has one btrfs_extent_inline_ref shows:
1284f82d1c7cSQu Wenruo 	 *    2.1) The ref type, one of the 4
1285f82d1c7cSQu Wenruo 	 *         TREE_BLOCK_REF	Tree block only
1286f82d1c7cSQu Wenruo 	 *         SHARED_BLOCK_REF	Tree block only
1287f82d1c7cSQu Wenruo 	 *         EXTENT_DATA_REF	Data only
1288f82d1c7cSQu Wenruo 	 *         SHARED_DATA_REF	Data only
1289f82d1c7cSQu Wenruo 	 *    2.2) Ref type specific data
1290f82d1c7cSQu Wenruo 	 *         Either using btrfs_extent_inline_ref::offset, or specific
1291f82d1c7cSQu Wenruo 	 *         data structure.
1292f82d1c7cSQu Wenruo 	 */
1293c7c01a4aSDavid Sterba 	if (unlikely(item_size < sizeof(*ei))) {
1294f82d1c7cSQu Wenruo 		extent_err(leaf, slot,
1295f82d1c7cSQu Wenruo 			   "invalid item size, have %u expect [%zu, %u)",
1296f82d1c7cSQu Wenruo 			   item_size, sizeof(*ei),
1297f82d1c7cSQu Wenruo 			   BTRFS_LEAF_DATA_SIZE(fs_info));
1298f82d1c7cSQu Wenruo 		return -EUCLEAN;
1299f82d1c7cSQu Wenruo 	}
1300f82d1c7cSQu Wenruo 	end = item_size + btrfs_item_ptr_offset(leaf, slot);
1301f82d1c7cSQu Wenruo 
1302f82d1c7cSQu Wenruo 	/* Checks against extent_item */
1303f82d1c7cSQu Wenruo 	ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
1304f82d1c7cSQu Wenruo 	flags = btrfs_extent_flags(leaf, ei);
1305f82d1c7cSQu Wenruo 	total_refs = btrfs_extent_refs(leaf, ei);
1306f82d1c7cSQu Wenruo 	generation = btrfs_extent_generation(leaf, ei);
1307c7c01a4aSDavid Sterba 	if (unlikely(generation >
1308c7c01a4aSDavid Sterba 		     btrfs_super_generation(fs_info->super_copy) + 1)) {
1309f82d1c7cSQu Wenruo 		extent_err(leaf, slot,
1310f82d1c7cSQu Wenruo 			   "invalid generation, have %llu expect (0, %llu]",
1311f82d1c7cSQu Wenruo 			   generation,
1312f82d1c7cSQu Wenruo 			   btrfs_super_generation(fs_info->super_copy) + 1);
1313f82d1c7cSQu Wenruo 		return -EUCLEAN;
1314f82d1c7cSQu Wenruo 	}
1315c7c01a4aSDavid Sterba 	if (unlikely(!has_single_bit_set(flags & (BTRFS_EXTENT_FLAG_DATA |
1316c7c01a4aSDavid Sterba 						  BTRFS_EXTENT_FLAG_TREE_BLOCK)))) {
1317f82d1c7cSQu Wenruo 		extent_err(leaf, slot,
1318f82d1c7cSQu Wenruo 		"invalid extent flag, have 0x%llx expect 1 bit set in 0x%llx",
1319f82d1c7cSQu Wenruo 			flags, BTRFS_EXTENT_FLAG_DATA |
1320f82d1c7cSQu Wenruo 			BTRFS_EXTENT_FLAG_TREE_BLOCK);
1321f82d1c7cSQu Wenruo 		return -EUCLEAN;
1322f82d1c7cSQu Wenruo 	}
1323f82d1c7cSQu Wenruo 	is_tree_block = !!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK);
1324f82d1c7cSQu Wenruo 	if (is_tree_block) {
1325c7c01a4aSDavid Sterba 		if (unlikely(key->type == BTRFS_EXTENT_ITEM_KEY &&
1326c7c01a4aSDavid Sterba 			     key->offset != fs_info->nodesize)) {
1327f82d1c7cSQu Wenruo 			extent_err(leaf, slot,
1328f82d1c7cSQu Wenruo 				   "invalid extent length, have %llu expect %u",
1329f82d1c7cSQu Wenruo 				   key->offset, fs_info->nodesize);
1330f82d1c7cSQu Wenruo 			return -EUCLEAN;
1331f82d1c7cSQu Wenruo 		}
1332f82d1c7cSQu Wenruo 	} else {
1333c7c01a4aSDavid Sterba 		if (unlikely(key->type != BTRFS_EXTENT_ITEM_KEY)) {
1334f82d1c7cSQu Wenruo 			extent_err(leaf, slot,
1335f82d1c7cSQu Wenruo 			"invalid key type, have %u expect %u for data backref",
1336f82d1c7cSQu Wenruo 				   key->type, BTRFS_EXTENT_ITEM_KEY);
1337f82d1c7cSQu Wenruo 			return -EUCLEAN;
1338f82d1c7cSQu Wenruo 		}
1339c7c01a4aSDavid Sterba 		if (unlikely(!IS_ALIGNED(key->offset, fs_info->sectorsize))) {
1340f82d1c7cSQu Wenruo 			extent_err(leaf, slot,
1341f82d1c7cSQu Wenruo 			"invalid extent length, have %llu expect aligned to %u",
1342f82d1c7cSQu Wenruo 				   key->offset, fs_info->sectorsize);
1343f82d1c7cSQu Wenruo 			return -EUCLEAN;
1344f82d1c7cSQu Wenruo 		}
13450ebb6bbbSJosef Bacik 		if (unlikely(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
13460ebb6bbbSJosef Bacik 			extent_err(leaf, slot,
13470ebb6bbbSJosef Bacik 			"invalid extent flag, data has full backref set");
13480ebb6bbbSJosef Bacik 			return -EUCLEAN;
13490ebb6bbbSJosef Bacik 		}
1350f82d1c7cSQu Wenruo 	}
1351f82d1c7cSQu Wenruo 	ptr = (unsigned long)(struct btrfs_extent_item *)(ei + 1);
1352f82d1c7cSQu Wenruo 
1353f82d1c7cSQu Wenruo 	/* Check the special case of btrfs_tree_block_info */
1354f82d1c7cSQu Wenruo 	if (is_tree_block && key->type != BTRFS_METADATA_ITEM_KEY) {
1355f82d1c7cSQu Wenruo 		struct btrfs_tree_block_info *info;
1356f82d1c7cSQu Wenruo 
1357f82d1c7cSQu Wenruo 		info = (struct btrfs_tree_block_info *)ptr;
1358c7c01a4aSDavid Sterba 		if (unlikely(btrfs_tree_block_level(leaf, info) >= BTRFS_MAX_LEVEL)) {
1359f82d1c7cSQu Wenruo 			extent_err(leaf, slot,
1360f82d1c7cSQu Wenruo 			"invalid tree block info level, have %u expect [0, %u]",
1361f82d1c7cSQu Wenruo 				   btrfs_tree_block_level(leaf, info),
1362f82d1c7cSQu Wenruo 				   BTRFS_MAX_LEVEL - 1);
1363f82d1c7cSQu Wenruo 			return -EUCLEAN;
1364f82d1c7cSQu Wenruo 		}
1365f82d1c7cSQu Wenruo 		ptr = (unsigned long)(struct btrfs_tree_block_info *)(info + 1);
1366f82d1c7cSQu Wenruo 	}
1367f82d1c7cSQu Wenruo 
1368f82d1c7cSQu Wenruo 	/* Check inline refs */
1369f82d1c7cSQu Wenruo 	while (ptr < end) {
1370f82d1c7cSQu Wenruo 		struct btrfs_extent_inline_ref *iref;
1371f82d1c7cSQu Wenruo 		struct btrfs_extent_data_ref *dref;
1372f82d1c7cSQu Wenruo 		struct btrfs_shared_data_ref *sref;
1373f82d1c7cSQu Wenruo 		u64 dref_offset;
1374f82d1c7cSQu Wenruo 		u64 inline_offset;
1375f82d1c7cSQu Wenruo 		u8 inline_type;
1376f82d1c7cSQu Wenruo 
1377c7c01a4aSDavid Sterba 		if (unlikely(ptr + sizeof(*iref) > end)) {
1378f82d1c7cSQu Wenruo 			extent_err(leaf, slot,
1379f82d1c7cSQu Wenruo "inline ref item overflows extent item, ptr %lu iref size %zu end %lu",
1380f82d1c7cSQu Wenruo 				   ptr, sizeof(*iref), end);
1381f82d1c7cSQu Wenruo 			return -EUCLEAN;
1382f82d1c7cSQu Wenruo 		}
1383f82d1c7cSQu Wenruo 		iref = (struct btrfs_extent_inline_ref *)ptr;
1384f82d1c7cSQu Wenruo 		inline_type = btrfs_extent_inline_ref_type(leaf, iref);
1385f82d1c7cSQu Wenruo 		inline_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1386c7c01a4aSDavid Sterba 		if (unlikely(ptr + btrfs_extent_inline_ref_size(inline_type) > end)) {
1387f82d1c7cSQu Wenruo 			extent_err(leaf, slot,
1388f82d1c7cSQu Wenruo "inline ref item overflows extent item, ptr %lu iref size %u end %lu",
1389f82d1c7cSQu Wenruo 				   ptr, inline_type, end);
1390f82d1c7cSQu Wenruo 			return -EUCLEAN;
1391f82d1c7cSQu Wenruo 		}
1392f82d1c7cSQu Wenruo 
1393f82d1c7cSQu Wenruo 		switch (inline_type) {
1394f82d1c7cSQu Wenruo 		/* inline_offset is subvolid of the owner, no need to check */
1395f82d1c7cSQu Wenruo 		case BTRFS_TREE_BLOCK_REF_KEY:
1396f82d1c7cSQu Wenruo 			inline_refs++;
1397f82d1c7cSQu Wenruo 			break;
1398f82d1c7cSQu Wenruo 		/* Contains parent bytenr */
1399f82d1c7cSQu Wenruo 		case BTRFS_SHARED_BLOCK_REF_KEY:
1400c7c01a4aSDavid Sterba 			if (unlikely(!IS_ALIGNED(inline_offset,
1401c7c01a4aSDavid Sterba 						 fs_info->sectorsize))) {
1402f82d1c7cSQu Wenruo 				extent_err(leaf, slot,
1403f82d1c7cSQu Wenruo 		"invalid tree parent bytenr, have %llu expect aligned to %u",
1404f82d1c7cSQu Wenruo 					   inline_offset, fs_info->sectorsize);
1405f82d1c7cSQu Wenruo 				return -EUCLEAN;
1406f82d1c7cSQu Wenruo 			}
1407f82d1c7cSQu Wenruo 			inline_refs++;
1408f82d1c7cSQu Wenruo 			break;
1409f82d1c7cSQu Wenruo 		/*
1410f82d1c7cSQu Wenruo 		 * Contains owner subvolid, owner key objectid, adjusted offset.
1411f82d1c7cSQu Wenruo 		 * The only obvious corruption can happen in that offset.
1412f82d1c7cSQu Wenruo 		 */
1413f82d1c7cSQu Wenruo 		case BTRFS_EXTENT_DATA_REF_KEY:
1414f82d1c7cSQu Wenruo 			dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1415f82d1c7cSQu Wenruo 			dref_offset = btrfs_extent_data_ref_offset(leaf, dref);
1416c7c01a4aSDavid Sterba 			if (unlikely(!IS_ALIGNED(dref_offset,
1417c7c01a4aSDavid Sterba 						 fs_info->sectorsize))) {
1418f82d1c7cSQu Wenruo 				extent_err(leaf, slot,
1419f82d1c7cSQu Wenruo 		"invalid data ref offset, have %llu expect aligned to %u",
1420f82d1c7cSQu Wenruo 					   dref_offset, fs_info->sectorsize);
1421f82d1c7cSQu Wenruo 				return -EUCLEAN;
1422f82d1c7cSQu Wenruo 			}
1423f82d1c7cSQu Wenruo 			inline_refs += btrfs_extent_data_ref_count(leaf, dref);
1424f82d1c7cSQu Wenruo 			break;
1425f82d1c7cSQu Wenruo 		/* Contains parent bytenr and ref count */
1426f82d1c7cSQu Wenruo 		case BTRFS_SHARED_DATA_REF_KEY:
1427f82d1c7cSQu Wenruo 			sref = (struct btrfs_shared_data_ref *)(iref + 1);
1428c7c01a4aSDavid Sterba 			if (unlikely(!IS_ALIGNED(inline_offset,
1429c7c01a4aSDavid Sterba 						 fs_info->sectorsize))) {
1430f82d1c7cSQu Wenruo 				extent_err(leaf, slot,
1431f82d1c7cSQu Wenruo 		"invalid data parent bytenr, have %llu expect aligned to %u",
1432f82d1c7cSQu Wenruo 					   inline_offset, fs_info->sectorsize);
1433f82d1c7cSQu Wenruo 				return -EUCLEAN;
1434f82d1c7cSQu Wenruo 			}
1435f82d1c7cSQu Wenruo 			inline_refs += btrfs_shared_data_ref_count(leaf, sref);
1436f82d1c7cSQu Wenruo 			break;
1437f82d1c7cSQu Wenruo 		default:
1438f82d1c7cSQu Wenruo 			extent_err(leaf, slot, "unknown inline ref type: %u",
1439f82d1c7cSQu Wenruo 				   inline_type);
1440f82d1c7cSQu Wenruo 			return -EUCLEAN;
1441f82d1c7cSQu Wenruo 		}
1442f82d1c7cSQu Wenruo 		ptr += btrfs_extent_inline_ref_size(inline_type);
1443f82d1c7cSQu Wenruo 	}
1444f82d1c7cSQu Wenruo 	/* No padding is allowed */
1445c7c01a4aSDavid Sterba 	if (unlikely(ptr != end)) {
1446f82d1c7cSQu Wenruo 		extent_err(leaf, slot,
1447f82d1c7cSQu Wenruo 			   "invalid extent item size, padding bytes found");
1448f82d1c7cSQu Wenruo 		return -EUCLEAN;
1449f82d1c7cSQu Wenruo 	}
1450f82d1c7cSQu Wenruo 
1451f82d1c7cSQu Wenruo 	/* Finally, check the inline refs against total refs */
1452c7c01a4aSDavid Sterba 	if (unlikely(inline_refs > total_refs)) {
1453f82d1c7cSQu Wenruo 		extent_err(leaf, slot,
1454f82d1c7cSQu Wenruo 			"invalid extent refs, have %llu expect >= inline %llu",
1455f82d1c7cSQu Wenruo 			   total_refs, inline_refs);
1456f82d1c7cSQu Wenruo 		return -EUCLEAN;
1457f82d1c7cSQu Wenruo 	}
1458899b7f69SJosef Bacik 
1459899b7f69SJosef Bacik 	if ((prev_key->type == BTRFS_EXTENT_ITEM_KEY) ||
1460899b7f69SJosef Bacik 	    (prev_key->type == BTRFS_METADATA_ITEM_KEY)) {
1461899b7f69SJosef Bacik 		u64 prev_end = prev_key->objectid;
1462899b7f69SJosef Bacik 
1463899b7f69SJosef Bacik 		if (prev_key->type == BTRFS_METADATA_ITEM_KEY)
1464899b7f69SJosef Bacik 			prev_end += fs_info->nodesize;
1465899b7f69SJosef Bacik 		else
1466899b7f69SJosef Bacik 			prev_end += prev_key->offset;
1467899b7f69SJosef Bacik 
1468899b7f69SJosef Bacik 		if (unlikely(prev_end > key->objectid)) {
1469899b7f69SJosef Bacik 			extent_err(leaf, slot,
1470899b7f69SJosef Bacik 	"previous extent [%llu %u %llu] overlaps current extent [%llu %u %llu]",
1471899b7f69SJosef Bacik 				   prev_key->objectid, prev_key->type,
1472899b7f69SJosef Bacik 				   prev_key->offset, key->objectid, key->type,
1473899b7f69SJosef Bacik 				   key->offset);
1474899b7f69SJosef Bacik 			return -EUCLEAN;
1475899b7f69SJosef Bacik 		}
1476899b7f69SJosef Bacik 	}
1477899b7f69SJosef Bacik 
1478f82d1c7cSQu Wenruo 	return 0;
1479f82d1c7cSQu Wenruo }
1480f82d1c7cSQu Wenruo 
1481e2406a6fSQu Wenruo static int check_simple_keyed_refs(struct extent_buffer *leaf,
1482e2406a6fSQu Wenruo 				   struct btrfs_key *key, int slot)
1483e2406a6fSQu Wenruo {
1484e2406a6fSQu Wenruo 	u32 expect_item_size = 0;
1485e2406a6fSQu Wenruo 
1486e2406a6fSQu Wenruo 	if (key->type == BTRFS_SHARED_DATA_REF_KEY)
1487e2406a6fSQu Wenruo 		expect_item_size = sizeof(struct btrfs_shared_data_ref);
1488e2406a6fSQu Wenruo 
14893212fa14SJosef Bacik 	if (unlikely(btrfs_item_size(leaf, slot) != expect_item_size)) {
1490e2406a6fSQu Wenruo 		generic_err(leaf, slot,
1491e2406a6fSQu Wenruo 		"invalid item size, have %u expect %u for key type %u",
14923212fa14SJosef Bacik 			    btrfs_item_size(leaf, slot),
1493e2406a6fSQu Wenruo 			    expect_item_size, key->type);
1494e2406a6fSQu Wenruo 		return -EUCLEAN;
1495e2406a6fSQu Wenruo 	}
1496c7c01a4aSDavid Sterba 	if (unlikely(!IS_ALIGNED(key->objectid, leaf->fs_info->sectorsize))) {
1497e2406a6fSQu Wenruo 		generic_err(leaf, slot,
1498e2406a6fSQu Wenruo "invalid key objectid for shared block ref, have %llu expect aligned to %u",
1499e2406a6fSQu Wenruo 			    key->objectid, leaf->fs_info->sectorsize);
1500e2406a6fSQu Wenruo 		return -EUCLEAN;
1501e2406a6fSQu Wenruo 	}
1502c7c01a4aSDavid Sterba 	if (unlikely(key->type != BTRFS_TREE_BLOCK_REF_KEY &&
1503c7c01a4aSDavid Sterba 		     !IS_ALIGNED(key->offset, leaf->fs_info->sectorsize))) {
1504e2406a6fSQu Wenruo 		extent_err(leaf, slot,
1505e2406a6fSQu Wenruo 		"invalid tree parent bytenr, have %llu expect aligned to %u",
1506e2406a6fSQu Wenruo 			   key->offset, leaf->fs_info->sectorsize);
1507e2406a6fSQu Wenruo 		return -EUCLEAN;
1508e2406a6fSQu Wenruo 	}
1509e2406a6fSQu Wenruo 	return 0;
1510e2406a6fSQu Wenruo }
1511e2406a6fSQu Wenruo 
15120785a9aaSQu Wenruo static int check_extent_data_ref(struct extent_buffer *leaf,
15130785a9aaSQu Wenruo 				 struct btrfs_key *key, int slot)
15140785a9aaSQu Wenruo {
15150785a9aaSQu Wenruo 	struct btrfs_extent_data_ref *dref;
15160785a9aaSQu Wenruo 	unsigned long ptr = btrfs_item_ptr_offset(leaf, slot);
15173212fa14SJosef Bacik 	const unsigned long end = ptr + btrfs_item_size(leaf, slot);
15180785a9aaSQu Wenruo 
15193212fa14SJosef Bacik 	if (unlikely(btrfs_item_size(leaf, slot) % sizeof(*dref) != 0)) {
15200785a9aaSQu Wenruo 		generic_err(leaf, slot,
15210785a9aaSQu Wenruo 	"invalid item size, have %u expect aligned to %zu for key type %u",
15223212fa14SJosef Bacik 			    btrfs_item_size(leaf, slot),
15230785a9aaSQu Wenruo 			    sizeof(*dref), key->type);
15246d06b0adSDavid Sterba 		return -EUCLEAN;
15250785a9aaSQu Wenruo 	}
1526c7c01a4aSDavid Sterba 	if (unlikely(!IS_ALIGNED(key->objectid, leaf->fs_info->sectorsize))) {
15270785a9aaSQu Wenruo 		generic_err(leaf, slot,
15280785a9aaSQu Wenruo "invalid key objectid for shared block ref, have %llu expect aligned to %u",
15290785a9aaSQu Wenruo 			    key->objectid, leaf->fs_info->sectorsize);
15300785a9aaSQu Wenruo 		return -EUCLEAN;
15310785a9aaSQu Wenruo 	}
15320785a9aaSQu Wenruo 	for (; ptr < end; ptr += sizeof(*dref)) {
15330785a9aaSQu Wenruo 		u64 offset;
15340785a9aaSQu Wenruo 
15351119a72eSJosef Bacik 		/*
15361119a72eSJosef Bacik 		 * We cannot check the extent_data_ref hash due to possible
15371119a72eSJosef Bacik 		 * overflow from the leaf due to hash collisions.
15381119a72eSJosef Bacik 		 */
15390785a9aaSQu Wenruo 		dref = (struct btrfs_extent_data_ref *)ptr;
15400785a9aaSQu Wenruo 		offset = btrfs_extent_data_ref_offset(leaf, dref);
1541c7c01a4aSDavid Sterba 		if (unlikely(!IS_ALIGNED(offset, leaf->fs_info->sectorsize))) {
15420785a9aaSQu Wenruo 			extent_err(leaf, slot,
15430785a9aaSQu Wenruo 	"invalid extent data backref offset, have %llu expect aligned to %u",
15440785a9aaSQu Wenruo 				   offset, leaf->fs_info->sectorsize);
15456d06b0adSDavid Sterba 			return -EUCLEAN;
15460785a9aaSQu Wenruo 		}
15470785a9aaSQu Wenruo 	}
15480785a9aaSQu Wenruo 	return 0;
15490785a9aaSQu Wenruo }
15500785a9aaSQu Wenruo 
1551c3053ebbSQu Wenruo #define inode_ref_err(eb, slot, fmt, args...)			\
1552c3053ebbSQu Wenruo 	inode_item_err(eb, slot, fmt, ##args)
155371bf92a9SQu Wenruo static int check_inode_ref(struct extent_buffer *leaf,
155471bf92a9SQu Wenruo 			   struct btrfs_key *key, struct btrfs_key *prev_key,
155571bf92a9SQu Wenruo 			   int slot)
155671bf92a9SQu Wenruo {
155771bf92a9SQu Wenruo 	struct btrfs_inode_ref *iref;
155871bf92a9SQu Wenruo 	unsigned long ptr;
155971bf92a9SQu Wenruo 	unsigned long end;
156071bf92a9SQu Wenruo 
1561c7c01a4aSDavid Sterba 	if (unlikely(!check_prev_ino(leaf, key, slot, prev_key)))
156280d7fd1eSQu Wenruo 		return -EUCLEAN;
156371bf92a9SQu Wenruo 	/* namelen can't be 0, so item_size == sizeof() is also invalid */
15643212fa14SJosef Bacik 	if (unlikely(btrfs_item_size(leaf, slot) <= sizeof(*iref))) {
1565c3053ebbSQu Wenruo 		inode_ref_err(leaf, slot,
156671bf92a9SQu Wenruo 			"invalid item size, have %u expect (%zu, %u)",
15673212fa14SJosef Bacik 			btrfs_item_size(leaf, slot),
156871bf92a9SQu Wenruo 			sizeof(*iref), BTRFS_LEAF_DATA_SIZE(leaf->fs_info));
156971bf92a9SQu Wenruo 		return -EUCLEAN;
157071bf92a9SQu Wenruo 	}
157171bf92a9SQu Wenruo 
157271bf92a9SQu Wenruo 	ptr = btrfs_item_ptr_offset(leaf, slot);
15733212fa14SJosef Bacik 	end = ptr + btrfs_item_size(leaf, slot);
157471bf92a9SQu Wenruo 	while (ptr < end) {
157571bf92a9SQu Wenruo 		u16 namelen;
157671bf92a9SQu Wenruo 
1577c7c01a4aSDavid Sterba 		if (unlikely(ptr + sizeof(iref) > end)) {
1578c3053ebbSQu Wenruo 			inode_ref_err(leaf, slot,
157971bf92a9SQu Wenruo 			"inode ref overflow, ptr %lu end %lu inode_ref_size %zu",
158071bf92a9SQu Wenruo 				ptr, end, sizeof(iref));
158171bf92a9SQu Wenruo 			return -EUCLEAN;
158271bf92a9SQu Wenruo 		}
158371bf92a9SQu Wenruo 
158471bf92a9SQu Wenruo 		iref = (struct btrfs_inode_ref *)ptr;
158571bf92a9SQu Wenruo 		namelen = btrfs_inode_ref_name_len(leaf, iref);
1586c7c01a4aSDavid Sterba 		if (unlikely(ptr + sizeof(*iref) + namelen > end)) {
1587c3053ebbSQu Wenruo 			inode_ref_err(leaf, slot,
158871bf92a9SQu Wenruo 				"inode ref overflow, ptr %lu end %lu namelen %u",
158971bf92a9SQu Wenruo 				ptr, end, namelen);
159071bf92a9SQu Wenruo 			return -EUCLEAN;
159171bf92a9SQu Wenruo 		}
159271bf92a9SQu Wenruo 
159371bf92a9SQu Wenruo 		/*
159471bf92a9SQu Wenruo 		 * NOTE: In theory we should record all found index numbers
159571bf92a9SQu Wenruo 		 * to find any duplicated indexes, but that will be too time
159671bf92a9SQu Wenruo 		 * consuming for inodes with too many hard links.
159771bf92a9SQu Wenruo 		 */
159871bf92a9SQu Wenruo 		ptr += sizeof(*iref) + namelen;
159971bf92a9SQu Wenruo 	}
160071bf92a9SQu Wenruo 	return 0;
160171bf92a9SQu Wenruo }
160271bf92a9SQu Wenruo 
160382fc28fbSQu Wenruo /*
1604557ea5ddSQu Wenruo  * Common point to switch the item-specific validation.
1605557ea5ddSQu Wenruo  */
16060076bc89SDavid Sterba static int check_leaf_item(struct extent_buffer *leaf,
16074e9845efSFilipe Manana 			   struct btrfs_key *key, int slot,
16084e9845efSFilipe Manana 			   struct btrfs_key *prev_key)
1609557ea5ddSQu Wenruo {
1610557ea5ddSQu Wenruo 	int ret = 0;
1611075cb3c7SQu Wenruo 	struct btrfs_chunk *chunk;
1612557ea5ddSQu Wenruo 
1613557ea5ddSQu Wenruo 	switch (key->type) {
1614557ea5ddSQu Wenruo 	case BTRFS_EXTENT_DATA_KEY:
16154e9845efSFilipe Manana 		ret = check_extent_data_item(leaf, key, slot, prev_key);
1616557ea5ddSQu Wenruo 		break;
1617557ea5ddSQu Wenruo 	case BTRFS_EXTENT_CSUM_KEY:
1618ad1d8c43SFilipe Manana 		ret = check_csum_item(leaf, key, slot, prev_key);
1619557ea5ddSQu Wenruo 		break;
1620ad7b0368SQu Wenruo 	case BTRFS_DIR_ITEM_KEY:
1621ad7b0368SQu Wenruo 	case BTRFS_DIR_INDEX_KEY:
1622ad7b0368SQu Wenruo 	case BTRFS_XATTR_ITEM_KEY:
1623c18679ebSQu Wenruo 		ret = check_dir_item(leaf, key, prev_key, slot);
1624ad7b0368SQu Wenruo 		break;
162571bf92a9SQu Wenruo 	case BTRFS_INODE_REF_KEY:
162671bf92a9SQu Wenruo 		ret = check_inode_ref(leaf, key, prev_key, slot);
162771bf92a9SQu Wenruo 		break;
1628fce466eaSQu Wenruo 	case BTRFS_BLOCK_GROUP_ITEM_KEY:
1629af60ce2bSDavid Sterba 		ret = check_block_group_item(leaf, key, slot);
1630fce466eaSQu Wenruo 		break;
1631075cb3c7SQu Wenruo 	case BTRFS_CHUNK_ITEM_KEY:
1632075cb3c7SQu Wenruo 		chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
1633f6d2a5c2SQu Wenruo 		ret = check_leaf_chunk_item(leaf, chunk, key, slot);
1634075cb3c7SQu Wenruo 		break;
1635ab4ba2e1SQu Wenruo 	case BTRFS_DEV_ITEM_KEY:
1636412a2312SDavid Sterba 		ret = check_dev_item(leaf, key, slot);
1637ab4ba2e1SQu Wenruo 		break;
1638496245caSQu Wenruo 	case BTRFS_INODE_ITEM_KEY:
163939e57f49SDavid Sterba 		ret = check_inode_item(leaf, key, slot);
1640496245caSQu Wenruo 		break;
1641259ee775SQu Wenruo 	case BTRFS_ROOT_ITEM_KEY:
1642259ee775SQu Wenruo 		ret = check_root_item(leaf, key, slot);
1643259ee775SQu Wenruo 		break;
1644f82d1c7cSQu Wenruo 	case BTRFS_EXTENT_ITEM_KEY:
1645f82d1c7cSQu Wenruo 	case BTRFS_METADATA_ITEM_KEY:
1646899b7f69SJosef Bacik 		ret = check_extent_item(leaf, key, slot, prev_key);
1647f82d1c7cSQu Wenruo 		break;
1648e2406a6fSQu Wenruo 	case BTRFS_TREE_BLOCK_REF_KEY:
1649e2406a6fSQu Wenruo 	case BTRFS_SHARED_DATA_REF_KEY:
1650e2406a6fSQu Wenruo 	case BTRFS_SHARED_BLOCK_REF_KEY:
1651e2406a6fSQu Wenruo 		ret = check_simple_keyed_refs(leaf, key, slot);
1652e2406a6fSQu Wenruo 		break;
16530785a9aaSQu Wenruo 	case BTRFS_EXTENT_DATA_REF_KEY:
16540785a9aaSQu Wenruo 		ret = check_extent_data_ref(leaf, key, slot);
16550785a9aaSQu Wenruo 		break;
1656557ea5ddSQu Wenruo 	}
1657557ea5ddSQu Wenruo 	return ret;
1658557ea5ddSQu Wenruo }
1659557ea5ddSQu Wenruo 
1660e2ccd361SDavid Sterba static int check_leaf(struct extent_buffer *leaf, bool check_item_data)
1661557ea5ddSQu Wenruo {
1662e2ccd361SDavid Sterba 	struct btrfs_fs_info *fs_info = leaf->fs_info;
1663557ea5ddSQu Wenruo 	/* No valid key type is 0, so all key should be larger than this key */
1664557ea5ddSQu Wenruo 	struct btrfs_key prev_key = {0, 0, 0};
1665557ea5ddSQu Wenruo 	struct btrfs_key key;
1666557ea5ddSQu Wenruo 	u32 nritems = btrfs_header_nritems(leaf);
1667557ea5ddSQu Wenruo 	int slot;
1668557ea5ddSQu Wenruo 
1669c7c01a4aSDavid Sterba 	if (unlikely(btrfs_header_level(leaf) != 0)) {
167086a6be3aSDavid Sterba 		generic_err(leaf, 0,
1671f556faa4SQu Wenruo 			"invalid level for leaf, have %d expect 0",
1672f556faa4SQu Wenruo 			btrfs_header_level(leaf));
1673f556faa4SQu Wenruo 		return -EUCLEAN;
1674f556faa4SQu Wenruo 	}
1675f556faa4SQu Wenruo 
1676557ea5ddSQu Wenruo 	/*
1677557ea5ddSQu Wenruo 	 * Extent buffers from a relocation tree have a owner field that
1678557ea5ddSQu Wenruo 	 * corresponds to the subvolume tree they are based on. So just from an
1679557ea5ddSQu Wenruo 	 * extent buffer alone we can not find out what is the id of the
1680557ea5ddSQu Wenruo 	 * corresponding subvolume tree, so we can not figure out if the extent
1681557ea5ddSQu Wenruo 	 * buffer corresponds to the root of the relocation tree or not. So
1682557ea5ddSQu Wenruo 	 * skip this check for relocation trees.
1683557ea5ddSQu Wenruo 	 */
1684557ea5ddSQu Wenruo 	if (nritems == 0 && !btrfs_header_flag(leaf, BTRFS_HEADER_FLAG_RELOC)) {
1685ba480dd4SQu Wenruo 		u64 owner = btrfs_header_owner(leaf);
1686557ea5ddSQu Wenruo 
1687ba480dd4SQu Wenruo 		/* These trees must never be empty */
1688c7c01a4aSDavid Sterba 		if (unlikely(owner == BTRFS_ROOT_TREE_OBJECTID ||
1689ba480dd4SQu Wenruo 			     owner == BTRFS_CHUNK_TREE_OBJECTID ||
1690ba480dd4SQu Wenruo 			     owner == BTRFS_DEV_TREE_OBJECTID ||
1691ba480dd4SQu Wenruo 			     owner == BTRFS_FS_TREE_OBJECTID ||
1692c7c01a4aSDavid Sterba 			     owner == BTRFS_DATA_RELOC_TREE_OBJECTID)) {
169386a6be3aSDavid Sterba 			generic_err(leaf, 0,
1694ba480dd4SQu Wenruo 			"invalid root, root %llu must never be empty",
1695ba480dd4SQu Wenruo 				    owner);
1696ba480dd4SQu Wenruo 			return -EUCLEAN;
1697ba480dd4SQu Wenruo 		}
1698c2fa821cSJosef Bacik 
169962fdaa52SQu Wenruo 		/* Unknown tree */
1700c7c01a4aSDavid Sterba 		if (unlikely(owner == 0)) {
170162fdaa52SQu Wenruo 			generic_err(leaf, 0,
170262fdaa52SQu Wenruo 				"invalid owner, root 0 is not defined");
170362fdaa52SQu Wenruo 			return -EUCLEAN;
170462fdaa52SQu Wenruo 		}
1705c2fa821cSJosef Bacik 
1706c2fa821cSJosef Bacik 		/* EXTENT_TREE_V2 can have empty extent trees. */
1707c2fa821cSJosef Bacik 		if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2))
1708c2fa821cSJosef Bacik 			return 0;
1709c2fa821cSJosef Bacik 
1710c2fa821cSJosef Bacik 		if (unlikely(owner == BTRFS_EXTENT_TREE_OBJECTID)) {
1711c2fa821cSJosef Bacik 			generic_err(leaf, 0,
1712c2fa821cSJosef Bacik 			"invalid root, root %llu must never be empty",
1713c2fa821cSJosef Bacik 				    owner);
1714c2fa821cSJosef Bacik 			return -EUCLEAN;
1715c2fa821cSJosef Bacik 		}
1716c2fa821cSJosef Bacik 
1717557ea5ddSQu Wenruo 		return 0;
1718557ea5ddSQu Wenruo 	}
1719557ea5ddSQu Wenruo 
1720c7c01a4aSDavid Sterba 	if (unlikely(nritems == 0))
1721557ea5ddSQu Wenruo 		return 0;
1722557ea5ddSQu Wenruo 
1723557ea5ddSQu Wenruo 	/*
1724557ea5ddSQu Wenruo 	 * Check the following things to make sure this is a good leaf, and
1725557ea5ddSQu Wenruo 	 * leaf users won't need to bother with similar sanity checks:
1726557ea5ddSQu Wenruo 	 *
1727557ea5ddSQu Wenruo 	 * 1) key ordering
1728557ea5ddSQu Wenruo 	 * 2) item offset and size
1729557ea5ddSQu Wenruo 	 *    No overlap, no hole, all inside the leaf.
1730557ea5ddSQu Wenruo 	 * 3) item content
1731557ea5ddSQu Wenruo 	 *    If possible, do comprehensive sanity check.
1732557ea5ddSQu Wenruo 	 *    NOTE: All checks must only rely on the item data itself.
1733557ea5ddSQu Wenruo 	 */
1734557ea5ddSQu Wenruo 	for (slot = 0; slot < nritems; slot++) {
1735557ea5ddSQu Wenruo 		u32 item_end_expected;
1736a6ab66ebSSu Yue 		u64 item_data_end;
1737557ea5ddSQu Wenruo 		int ret;
1738557ea5ddSQu Wenruo 
1739557ea5ddSQu Wenruo 		btrfs_item_key_to_cpu(leaf, &key, slot);
1740557ea5ddSQu Wenruo 
1741557ea5ddSQu Wenruo 		/* Make sure the keys are in the right order */
1742c7c01a4aSDavid Sterba 		if (unlikely(btrfs_comp_cpu_keys(&prev_key, &key) >= 0)) {
174386a6be3aSDavid Sterba 			generic_err(leaf, slot,
1744478d01b3SQu Wenruo 	"bad key order, prev (%llu %u %llu) current (%llu %u %llu)",
1745478d01b3SQu Wenruo 				prev_key.objectid, prev_key.type,
1746478d01b3SQu Wenruo 				prev_key.offset, key.objectid, key.type,
1747478d01b3SQu Wenruo 				key.offset);
1748557ea5ddSQu Wenruo 			return -EUCLEAN;
1749557ea5ddSQu Wenruo 		}
1750557ea5ddSQu Wenruo 
1751a6ab66ebSSu Yue 		item_data_end = (u64)btrfs_item_offset(leaf, slot) +
1752a6ab66ebSSu Yue 				btrfs_item_size(leaf, slot);
1753557ea5ddSQu Wenruo 		/*
1754557ea5ddSQu Wenruo 		 * Make sure the offset and ends are right, remember that the
1755557ea5ddSQu Wenruo 		 * item data starts at the end of the leaf and grows towards the
1756557ea5ddSQu Wenruo 		 * front.
1757557ea5ddSQu Wenruo 		 */
1758557ea5ddSQu Wenruo 		if (slot == 0)
1759557ea5ddSQu Wenruo 			item_end_expected = BTRFS_LEAF_DATA_SIZE(fs_info);
1760557ea5ddSQu Wenruo 		else
17613212fa14SJosef Bacik 			item_end_expected = btrfs_item_offset(leaf,
1762557ea5ddSQu Wenruo 								 slot - 1);
1763a6ab66ebSSu Yue 		if (unlikely(item_data_end != item_end_expected)) {
176486a6be3aSDavid Sterba 			generic_err(leaf, slot,
1765a6ab66ebSSu Yue 				"unexpected item end, have %llu expect %u",
1766a6ab66ebSSu Yue 				item_data_end, item_end_expected);
1767557ea5ddSQu Wenruo 			return -EUCLEAN;
1768557ea5ddSQu Wenruo 		}
1769557ea5ddSQu Wenruo 
1770557ea5ddSQu Wenruo 		/*
1771557ea5ddSQu Wenruo 		 * Check to make sure that we don't point outside of the leaf,
1772557ea5ddSQu Wenruo 		 * just in case all the items are consistent to each other, but
1773557ea5ddSQu Wenruo 		 * all point outside of the leaf.
1774557ea5ddSQu Wenruo 		 */
1775a6ab66ebSSu Yue 		if (unlikely(item_data_end > BTRFS_LEAF_DATA_SIZE(fs_info))) {
177686a6be3aSDavid Sterba 			generic_err(leaf, slot,
1777a6ab66ebSSu Yue 			"slot end outside of leaf, have %llu expect range [0, %u]",
1778a6ab66ebSSu Yue 				item_data_end, BTRFS_LEAF_DATA_SIZE(fs_info));
1779557ea5ddSQu Wenruo 			return -EUCLEAN;
1780557ea5ddSQu Wenruo 		}
1781557ea5ddSQu Wenruo 
1782557ea5ddSQu Wenruo 		/* Also check if the item pointer overlaps with btrfs item. */
1783c7c01a4aSDavid Sterba 		if (unlikely(btrfs_item_ptr_offset(leaf, slot) <
1784c7c01a4aSDavid Sterba 			     btrfs_item_nr_offset(slot) + sizeof(struct btrfs_item))) {
178586a6be3aSDavid Sterba 			generic_err(leaf, slot,
1786478d01b3SQu Wenruo 		"slot overlaps with its data, item end %lu data start %lu",
1787478d01b3SQu Wenruo 				btrfs_item_nr_offset(slot) +
1788478d01b3SQu Wenruo 				sizeof(struct btrfs_item),
1789478d01b3SQu Wenruo 				btrfs_item_ptr_offset(leaf, slot));
1790557ea5ddSQu Wenruo 			return -EUCLEAN;
1791557ea5ddSQu Wenruo 		}
1792557ea5ddSQu Wenruo 
179369fc6cbbSQu Wenruo 		if (check_item_data) {
179469fc6cbbSQu Wenruo 			/*
179569fc6cbbSQu Wenruo 			 * Check if the item size and content meet other
179669fc6cbbSQu Wenruo 			 * criteria
179769fc6cbbSQu Wenruo 			 */
17984e9845efSFilipe Manana 			ret = check_leaf_item(leaf, &key, slot, &prev_key);
1799c7c01a4aSDavid Sterba 			if (unlikely(ret < 0))
1800557ea5ddSQu Wenruo 				return ret;
180169fc6cbbSQu Wenruo 		}
1802557ea5ddSQu Wenruo 
1803557ea5ddSQu Wenruo 		prev_key.objectid = key.objectid;
1804557ea5ddSQu Wenruo 		prev_key.type = key.type;
1805557ea5ddSQu Wenruo 		prev_key.offset = key.offset;
1806557ea5ddSQu Wenruo 	}
1807557ea5ddSQu Wenruo 
1808557ea5ddSQu Wenruo 	return 0;
1809557ea5ddSQu Wenruo }
1810557ea5ddSQu Wenruo 
18111c4360eeSDavid Sterba int btrfs_check_leaf_full(struct extent_buffer *leaf)
181269fc6cbbSQu Wenruo {
1813e2ccd361SDavid Sterba 	return check_leaf(leaf, true);
181469fc6cbbSQu Wenruo }
181502529d7aSQu Wenruo ALLOW_ERROR_INJECTION(btrfs_check_leaf_full, ERRNO);
181669fc6cbbSQu Wenruo 
1817cfdaad5eSDavid Sterba int btrfs_check_leaf_relaxed(struct extent_buffer *leaf)
18182f659546SQu Wenruo {
1819e2ccd361SDavid Sterba 	return check_leaf(leaf, false);
18202f659546SQu Wenruo }
18212f659546SQu Wenruo 
1822813fd1dcSDavid Sterba int btrfs_check_node(struct extent_buffer *node)
1823557ea5ddSQu Wenruo {
1824813fd1dcSDavid Sterba 	struct btrfs_fs_info *fs_info = node->fs_info;
1825557ea5ddSQu Wenruo 	unsigned long nr = btrfs_header_nritems(node);
1826557ea5ddSQu Wenruo 	struct btrfs_key key, next_key;
1827557ea5ddSQu Wenruo 	int slot;
1828f556faa4SQu Wenruo 	int level = btrfs_header_level(node);
1829557ea5ddSQu Wenruo 	u64 bytenr;
1830557ea5ddSQu Wenruo 	int ret = 0;
1831557ea5ddSQu Wenruo 
1832c7c01a4aSDavid Sterba 	if (unlikely(level <= 0 || level >= BTRFS_MAX_LEVEL)) {
183386a6be3aSDavid Sterba 		generic_err(node, 0,
1834f556faa4SQu Wenruo 			"invalid level for node, have %d expect [1, %d]",
1835f556faa4SQu Wenruo 			level, BTRFS_MAX_LEVEL - 1);
1836f556faa4SQu Wenruo 		return -EUCLEAN;
1837f556faa4SQu Wenruo 	}
1838c7c01a4aSDavid Sterba 	if (unlikely(nr == 0 || nr > BTRFS_NODEPTRS_PER_BLOCK(fs_info))) {
18392f659546SQu Wenruo 		btrfs_crit(fs_info,
1840bba4f298SQu Wenruo "corrupt node: root=%llu block=%llu, nritems too %s, have %lu expect range [1,%u]",
18412f659546SQu Wenruo 			   btrfs_header_owner(node), node->start,
1842bba4f298SQu Wenruo 			   nr == 0 ? "small" : "large", nr,
18432f659546SQu Wenruo 			   BTRFS_NODEPTRS_PER_BLOCK(fs_info));
1844bba4f298SQu Wenruo 		return -EUCLEAN;
1845557ea5ddSQu Wenruo 	}
1846557ea5ddSQu Wenruo 
1847557ea5ddSQu Wenruo 	for (slot = 0; slot < nr - 1; slot++) {
1848557ea5ddSQu Wenruo 		bytenr = btrfs_node_blockptr(node, slot);
1849557ea5ddSQu Wenruo 		btrfs_node_key_to_cpu(node, &key, slot);
1850557ea5ddSQu Wenruo 		btrfs_node_key_to_cpu(node, &next_key, slot + 1);
1851557ea5ddSQu Wenruo 
1852c7c01a4aSDavid Sterba 		if (unlikely(!bytenr)) {
185386a6be3aSDavid Sterba 			generic_err(node, slot,
1854bba4f298SQu Wenruo 				"invalid NULL node pointer");
1855bba4f298SQu Wenruo 			ret = -EUCLEAN;
1856bba4f298SQu Wenruo 			goto out;
1857bba4f298SQu Wenruo 		}
1858c7c01a4aSDavid Sterba 		if (unlikely(!IS_ALIGNED(bytenr, fs_info->sectorsize))) {
185986a6be3aSDavid Sterba 			generic_err(node, slot,
1860bba4f298SQu Wenruo 			"unaligned pointer, have %llu should be aligned to %u",
18612f659546SQu Wenruo 				bytenr, fs_info->sectorsize);
1862bba4f298SQu Wenruo 			ret = -EUCLEAN;
1863557ea5ddSQu Wenruo 			goto out;
1864557ea5ddSQu Wenruo 		}
1865557ea5ddSQu Wenruo 
1866c7c01a4aSDavid Sterba 		if (unlikely(btrfs_comp_cpu_keys(&key, &next_key) >= 0)) {
186786a6be3aSDavid Sterba 			generic_err(node, slot,
1868bba4f298SQu Wenruo 	"bad key order, current (%llu %u %llu) next (%llu %u %llu)",
1869bba4f298SQu Wenruo 				key.objectid, key.type, key.offset,
1870bba4f298SQu Wenruo 				next_key.objectid, next_key.type,
1871bba4f298SQu Wenruo 				next_key.offset);
1872bba4f298SQu Wenruo 			ret = -EUCLEAN;
1873557ea5ddSQu Wenruo 			goto out;
1874557ea5ddSQu Wenruo 		}
1875557ea5ddSQu Wenruo 	}
1876557ea5ddSQu Wenruo out:
1877557ea5ddSQu Wenruo 	return ret;
1878557ea5ddSQu Wenruo }
187902529d7aSQu Wenruo ALLOW_ERROR_INJECTION(btrfs_check_node, ERRNO);
188088c602abSQu Wenruo 
188188c602abSQu Wenruo int btrfs_check_eb_owner(const struct extent_buffer *eb, u64 root_owner)
188288c602abSQu Wenruo {
188388c602abSQu Wenruo 	const bool is_subvol = is_fstree(root_owner);
188488c602abSQu Wenruo 	const u64 eb_owner = btrfs_header_owner(eb);
188588c602abSQu Wenruo 
188688c602abSQu Wenruo 	/*
188788c602abSQu Wenruo 	 * Skip dummy fs, as selftests don't create unique ebs for each dummy
188888c602abSQu Wenruo 	 * root.
188988c602abSQu Wenruo 	 */
189088c602abSQu Wenruo 	if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &eb->fs_info->fs_state))
189188c602abSQu Wenruo 		return 0;
189288c602abSQu Wenruo 	/*
189388c602abSQu Wenruo 	 * There are several call sites (backref walking, qgroup, and data
189488c602abSQu Wenruo 	 * reloc) passing 0 as @root_owner, as they are not holding the
189588c602abSQu Wenruo 	 * tree root.  In that case, we can not do a reliable ownership check,
189688c602abSQu Wenruo 	 * so just exit.
189788c602abSQu Wenruo 	 */
189888c602abSQu Wenruo 	if (root_owner == 0)
189988c602abSQu Wenruo 		return 0;
190088c602abSQu Wenruo 	/*
190188c602abSQu Wenruo 	 * These trees use key.offset as their owner, our callers don't have
190288c602abSQu Wenruo 	 * the extra capacity to pass key.offset here.  So we just skip them.
190388c602abSQu Wenruo 	 */
190488c602abSQu Wenruo 	if (root_owner == BTRFS_TREE_LOG_OBJECTID ||
190588c602abSQu Wenruo 	    root_owner == BTRFS_TREE_RELOC_OBJECTID)
190688c602abSQu Wenruo 		return 0;
190788c602abSQu Wenruo 
190888c602abSQu Wenruo 	if (!is_subvol) {
190988c602abSQu Wenruo 		/* For non-subvolume trees, the eb owner should match root owner */
191088c602abSQu Wenruo 		if (unlikely(root_owner != eb_owner)) {
191188c602abSQu Wenruo 			btrfs_crit(eb->fs_info,
191288c602abSQu Wenruo "corrupted %s, root=%llu block=%llu owner mismatch, have %llu expect %llu",
191388c602abSQu Wenruo 				btrfs_header_level(eb) == 0 ? "leaf" : "node",
191488c602abSQu Wenruo 				root_owner, btrfs_header_bytenr(eb), eb_owner,
191588c602abSQu Wenruo 				root_owner);
191688c602abSQu Wenruo 			return -EUCLEAN;
191788c602abSQu Wenruo 		}
191888c602abSQu Wenruo 		return 0;
191988c602abSQu Wenruo 	}
192088c602abSQu Wenruo 
192188c602abSQu Wenruo 	/*
192288c602abSQu Wenruo 	 * For subvolume trees, owners can mismatch, but they should all belong
192388c602abSQu Wenruo 	 * to subvolume trees.
192488c602abSQu Wenruo 	 */
192588c602abSQu Wenruo 	if (unlikely(is_subvol != is_fstree(eb_owner))) {
192688c602abSQu Wenruo 		btrfs_crit(eb->fs_info,
192788c602abSQu Wenruo "corrupted %s, root=%llu block=%llu owner mismatch, have %llu expect [%llu, %llu]",
192888c602abSQu Wenruo 			btrfs_header_level(eb) == 0 ? "leaf" : "node",
192988c602abSQu Wenruo 			root_owner, btrfs_header_bytenr(eb), eb_owner,
193088c602abSQu Wenruo 			BTRFS_FIRST_FREE_OBJECTID, BTRFS_LAST_FREE_OBJECTID);
193188c602abSQu Wenruo 		return -EUCLEAN;
193288c602abSQu Wenruo 	}
193388c602abSQu Wenruo 	return 0;
193488c602abSQu Wenruo }
1935