xref: /openbmc/linux/fs/btrfs/tree-checker.c (revision 94a48aef49f235cc1efc74dc18e7708ca3b8d698)
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>
219b569ea0SJosef Bacik #include "messages.h"
22557ea5ddSQu Wenruo #include "ctree.h"
23557ea5ddSQu Wenruo #include "tree-checker.h"
24557ea5ddSQu Wenruo #include "disk-io.h"
25557ea5ddSQu Wenruo #include "compression.h"
26fce466eaSQu Wenruo #include "volumes.h"
27c1499166SDavid Sterba #include "misc.h"
2877eea05eSBoris Burkov #include "btrfs_inode.h"
29c7f13d42SJosef Bacik #include "fs.h"
3007e81dc9SJosef Bacik #include "accessors.h"
31557ea5ddSQu Wenruo 
32bba4f298SQu Wenruo /*
33bba4f298SQu Wenruo  * Error message should follow the following format:
34bba4f298SQu Wenruo  * corrupt <type>: <identifier>, <reason>[, <bad_value>]
35bba4f298SQu Wenruo  *
36bba4f298SQu Wenruo  * @type:	leaf or node
37bba4f298SQu Wenruo  * @identifier:	the necessary info to locate the leaf/node.
3852042d8eSAndrea Gelmini  * 		It's recommended to decode key.objecitd/offset if it's
39bba4f298SQu Wenruo  * 		meaningful.
40bba4f298SQu Wenruo  * @reason:	describe the error
4152042d8eSAndrea Gelmini  * @bad_value:	optional, it's recommended to output bad value and its
42bba4f298SQu Wenruo  *		expected value (range).
43bba4f298SQu Wenruo  *
44bba4f298SQu Wenruo  * Since comma is used to separate the components, only space is allowed
45bba4f298SQu Wenruo  * inside each component.
46bba4f298SQu Wenruo  */
47bba4f298SQu Wenruo 
48bba4f298SQu Wenruo /*
49bba4f298SQu Wenruo  * Append generic "corrupt leaf/node root=%llu block=%llu slot=%d: " to @fmt.
50bba4f298SQu Wenruo  * Allows callers to customize the output.
51bba4f298SQu Wenruo  */
5286a6be3aSDavid Sterba __printf(3, 4)
53e67c718bSDavid Sterba __cold
5486a6be3aSDavid Sterba static void generic_err(const struct extent_buffer *eb, int slot,
55bba4f298SQu Wenruo 			const char *fmt, ...)
56bba4f298SQu Wenruo {
5786a6be3aSDavid Sterba 	const struct btrfs_fs_info *fs_info = eb->fs_info;
58bba4f298SQu Wenruo 	struct va_format vaf;
59bba4f298SQu Wenruo 	va_list args;
60bba4f298SQu Wenruo 
61bba4f298SQu Wenruo 	va_start(args, fmt);
62bba4f298SQu Wenruo 
63bba4f298SQu Wenruo 	vaf.fmt = fmt;
64bba4f298SQu Wenruo 	vaf.va = &args;
65bba4f298SQu Wenruo 
662f659546SQu Wenruo 	btrfs_crit(fs_info,
67bba4f298SQu Wenruo 		"corrupt %s: root=%llu block=%llu slot=%d, %pV",
68bba4f298SQu Wenruo 		btrfs_header_level(eb) == 0 ? "leaf" : "node",
692f659546SQu Wenruo 		btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot, &vaf);
70bba4f298SQu Wenruo 	va_end(args);
71bba4f298SQu Wenruo }
72bba4f298SQu Wenruo 
738806d718SQu Wenruo /*
748806d718SQu Wenruo  * Customized reporter for extent data item, since its key objectid and
758806d718SQu Wenruo  * offset has its own meaning.
768806d718SQu Wenruo  */
771fd715ffSDavid Sterba __printf(3, 4)
78e67c718bSDavid Sterba __cold
791fd715ffSDavid Sterba static void file_extent_err(const struct extent_buffer *eb, int slot,
808806d718SQu Wenruo 			    const char *fmt, ...)
818806d718SQu Wenruo {
821fd715ffSDavid Sterba 	const struct btrfs_fs_info *fs_info = eb->fs_info;
838806d718SQu Wenruo 	struct btrfs_key key;
848806d718SQu Wenruo 	struct va_format vaf;
858806d718SQu Wenruo 	va_list args;
868806d718SQu Wenruo 
878806d718SQu Wenruo 	btrfs_item_key_to_cpu(eb, &key, slot);
888806d718SQu Wenruo 	va_start(args, fmt);
898806d718SQu Wenruo 
908806d718SQu Wenruo 	vaf.fmt = fmt;
918806d718SQu Wenruo 	vaf.va = &args;
928806d718SQu Wenruo 
932f659546SQu Wenruo 	btrfs_crit(fs_info,
948806d718SQu Wenruo 	"corrupt %s: root=%llu block=%llu slot=%d ino=%llu file_offset=%llu, %pV",
952f659546SQu Wenruo 		btrfs_header_level(eb) == 0 ? "leaf" : "node",
962f659546SQu Wenruo 		btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot,
972f659546SQu Wenruo 		key.objectid, key.offset, &vaf);
988806d718SQu Wenruo 	va_end(args);
998806d718SQu Wenruo }
1008806d718SQu Wenruo 
1018806d718SQu Wenruo /*
1028806d718SQu Wenruo  * Return 0 if the btrfs_file_extent_##name is aligned to @alignment
1038806d718SQu Wenruo  * Else return 1
1048806d718SQu Wenruo  */
105033774dcSDavid Sterba #define CHECK_FE_ALIGNED(leaf, slot, fi, name, alignment)		      \
1068806d718SQu Wenruo ({									      \
107c7c01a4aSDavid Sterba 	if (unlikely(!IS_ALIGNED(btrfs_file_extent_##name((leaf), (fi)),      \
108c7c01a4aSDavid Sterba 				 (alignment))))				      \
1091fd715ffSDavid Sterba 		file_extent_err((leaf), (slot),				      \
1108806d718SQu Wenruo 	"invalid %s for file extent, have %llu, should be aligned to %u",     \
1118806d718SQu Wenruo 			(#name), btrfs_file_extent_##name((leaf), (fi)),      \
1128806d718SQu Wenruo 			(alignment));					      \
1138806d718SQu Wenruo 	(!IS_ALIGNED(btrfs_file_extent_##name((leaf), (fi)), (alignment)));   \
1148806d718SQu Wenruo })
1158806d718SQu Wenruo 
1164e9845efSFilipe Manana static u64 file_extent_end(struct extent_buffer *leaf,
1174e9845efSFilipe Manana 			   struct btrfs_key *key,
1184e9845efSFilipe Manana 			   struct btrfs_file_extent_item *extent)
1194e9845efSFilipe Manana {
1204e9845efSFilipe Manana 	u64 end;
1214e9845efSFilipe Manana 	u64 len;
1224e9845efSFilipe Manana 
1234e9845efSFilipe Manana 	if (btrfs_file_extent_type(leaf, extent) == BTRFS_FILE_EXTENT_INLINE) {
1244e9845efSFilipe Manana 		len = btrfs_file_extent_ram_bytes(leaf, extent);
1254e9845efSFilipe Manana 		end = ALIGN(key->offset + len, leaf->fs_info->sectorsize);
1264e9845efSFilipe Manana 	} else {
1274e9845efSFilipe Manana 		len = btrfs_file_extent_num_bytes(leaf, extent);
1284e9845efSFilipe Manana 		end = key->offset + len;
1294e9845efSFilipe Manana 	}
1304e9845efSFilipe Manana 	return end;
1314e9845efSFilipe Manana }
1324e9845efSFilipe Manana 
13380d7fd1eSQu Wenruo /*
13480d7fd1eSQu Wenruo  * Customized report for dir_item, the only new important information is
13580d7fd1eSQu Wenruo  * key->objectid, which represents inode number
13680d7fd1eSQu Wenruo  */
13780d7fd1eSQu Wenruo __printf(3, 4)
13880d7fd1eSQu Wenruo __cold
13980d7fd1eSQu Wenruo static void dir_item_err(const struct extent_buffer *eb, int slot,
14080d7fd1eSQu Wenruo 			 const char *fmt, ...)
14180d7fd1eSQu Wenruo {
14280d7fd1eSQu Wenruo 	const struct btrfs_fs_info *fs_info = eb->fs_info;
14380d7fd1eSQu Wenruo 	struct btrfs_key key;
14480d7fd1eSQu Wenruo 	struct va_format vaf;
14580d7fd1eSQu Wenruo 	va_list args;
14680d7fd1eSQu Wenruo 
14780d7fd1eSQu Wenruo 	btrfs_item_key_to_cpu(eb, &key, slot);
14880d7fd1eSQu Wenruo 	va_start(args, fmt);
14980d7fd1eSQu Wenruo 
15080d7fd1eSQu Wenruo 	vaf.fmt = fmt;
15180d7fd1eSQu Wenruo 	vaf.va = &args;
15280d7fd1eSQu Wenruo 
15380d7fd1eSQu Wenruo 	btrfs_crit(fs_info,
15480d7fd1eSQu Wenruo 		"corrupt %s: root=%llu block=%llu slot=%d ino=%llu, %pV",
15580d7fd1eSQu Wenruo 		btrfs_header_level(eb) == 0 ? "leaf" : "node",
15680d7fd1eSQu Wenruo 		btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot,
15780d7fd1eSQu Wenruo 		key.objectid, &vaf);
15880d7fd1eSQu Wenruo 	va_end(args);
15980d7fd1eSQu Wenruo }
16080d7fd1eSQu Wenruo 
16180d7fd1eSQu Wenruo /*
16280d7fd1eSQu Wenruo  * This functions checks prev_key->objectid, to ensure current key and prev_key
16380d7fd1eSQu Wenruo  * share the same objectid as inode number.
16480d7fd1eSQu Wenruo  *
16580d7fd1eSQu Wenruo  * This is to detect missing INODE_ITEM in subvolume trees.
16680d7fd1eSQu Wenruo  *
16780d7fd1eSQu Wenruo  * Return true if everything is OK or we don't need to check.
16880d7fd1eSQu Wenruo  * Return false if anything is wrong.
16980d7fd1eSQu Wenruo  */
17080d7fd1eSQu Wenruo static bool check_prev_ino(struct extent_buffer *leaf,
17180d7fd1eSQu Wenruo 			   struct btrfs_key *key, int slot,
17280d7fd1eSQu Wenruo 			   struct btrfs_key *prev_key)
17380d7fd1eSQu Wenruo {
17480d7fd1eSQu Wenruo 	/* No prev key, skip check */
17580d7fd1eSQu Wenruo 	if (slot == 0)
17680d7fd1eSQu Wenruo 		return true;
17780d7fd1eSQu Wenruo 
17880d7fd1eSQu Wenruo 	/* Only these key->types needs to be checked */
17980d7fd1eSQu Wenruo 	ASSERT(key->type == BTRFS_XATTR_ITEM_KEY ||
18080d7fd1eSQu Wenruo 	       key->type == BTRFS_INODE_REF_KEY ||
18180d7fd1eSQu Wenruo 	       key->type == BTRFS_DIR_INDEX_KEY ||
18280d7fd1eSQu Wenruo 	       key->type == BTRFS_DIR_ITEM_KEY ||
18380d7fd1eSQu Wenruo 	       key->type == BTRFS_EXTENT_DATA_KEY);
18480d7fd1eSQu Wenruo 
18580d7fd1eSQu Wenruo 	/*
18680d7fd1eSQu Wenruo 	 * Only subvolume trees along with their reloc trees need this check.
18780d7fd1eSQu Wenruo 	 * Things like log tree doesn't follow this ino requirement.
18880d7fd1eSQu Wenruo 	 */
18980d7fd1eSQu Wenruo 	if (!is_fstree(btrfs_header_owner(leaf)))
19080d7fd1eSQu Wenruo 		return true;
19180d7fd1eSQu Wenruo 
19280d7fd1eSQu Wenruo 	if (key->objectid == prev_key->objectid)
19380d7fd1eSQu Wenruo 		return true;
19480d7fd1eSQu Wenruo 
19580d7fd1eSQu Wenruo 	/* Error found */
19680d7fd1eSQu Wenruo 	dir_item_err(leaf, slot,
19780d7fd1eSQu Wenruo 		"invalid previous key objectid, have %llu expect %llu",
19880d7fd1eSQu Wenruo 		prev_key->objectid, key->objectid);
19980d7fd1eSQu Wenruo 	return false;
20080d7fd1eSQu Wenruo }
201ae2a19d8SDavid Sterba static int check_extent_data_item(struct extent_buffer *leaf,
2024e9845efSFilipe Manana 				  struct btrfs_key *key, int slot,
2034e9845efSFilipe Manana 				  struct btrfs_key *prev_key)
204557ea5ddSQu Wenruo {
205ae2a19d8SDavid Sterba 	struct btrfs_fs_info *fs_info = leaf->fs_info;
206557ea5ddSQu Wenruo 	struct btrfs_file_extent_item *fi;
2072f659546SQu Wenruo 	u32 sectorsize = fs_info->sectorsize;
2083212fa14SJosef Bacik 	u32 item_size = btrfs_item_size(leaf, slot);
2094c094c33SQu Wenruo 	u64 extent_end;
210557ea5ddSQu Wenruo 
211c7c01a4aSDavid Sterba 	if (unlikely(!IS_ALIGNED(key->offset, sectorsize))) {
2121fd715ffSDavid Sterba 		file_extent_err(leaf, slot,
2138806d718SQu Wenruo "unaligned file_offset for file extent, have %llu should be aligned to %u",
2148806d718SQu Wenruo 			key->offset, sectorsize);
215557ea5ddSQu Wenruo 		return -EUCLEAN;
216557ea5ddSQu Wenruo 	}
217557ea5ddSQu Wenruo 
218c18679ebSQu Wenruo 	/*
219c18679ebSQu Wenruo 	 * Previous key must have the same key->objectid (ino).
220c18679ebSQu Wenruo 	 * It can be XATTR_ITEM, INODE_ITEM or just another EXTENT_DATA.
221c18679ebSQu Wenruo 	 * But if objectids mismatch, it means we have a missing
222c18679ebSQu Wenruo 	 * INODE_ITEM.
223c18679ebSQu Wenruo 	 */
224c7c01a4aSDavid Sterba 	if (unlikely(!check_prev_ino(leaf, key, slot, prev_key)))
225c18679ebSQu Wenruo 		return -EUCLEAN;
226c18679ebSQu Wenruo 
227557ea5ddSQu Wenruo 	fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
228557ea5ddSQu Wenruo 
229153a6d29SQu Wenruo 	/*
230153a6d29SQu Wenruo 	 * Make sure the item contains at least inline header, so the file
231153a6d29SQu Wenruo 	 * extent type is not some garbage.
232153a6d29SQu Wenruo 	 */
233c7c01a4aSDavid Sterba 	if (unlikely(item_size < BTRFS_FILE_EXTENT_INLINE_DATA_START)) {
234153a6d29SQu Wenruo 		file_extent_err(leaf, slot,
235994bf9cdSAndreas Färber 				"invalid item size, have %u expect [%zu, %u)",
236153a6d29SQu Wenruo 				item_size, BTRFS_FILE_EXTENT_INLINE_DATA_START,
237153a6d29SQu Wenruo 				SZ_4K);
238153a6d29SQu Wenruo 		return -EUCLEAN;
239153a6d29SQu Wenruo 	}
240c7c01a4aSDavid Sterba 	if (unlikely(btrfs_file_extent_type(leaf, fi) >=
241c7c01a4aSDavid Sterba 		     BTRFS_NR_FILE_EXTENT_TYPES)) {
2421fd715ffSDavid Sterba 		file_extent_err(leaf, slot,
2438806d718SQu Wenruo 		"invalid type for file extent, have %u expect range [0, %u]",
2448806d718SQu Wenruo 			btrfs_file_extent_type(leaf, fi),
245b9b1a53eSChengguang Xu 			BTRFS_NR_FILE_EXTENT_TYPES - 1);
246557ea5ddSQu Wenruo 		return -EUCLEAN;
247557ea5ddSQu Wenruo 	}
248557ea5ddSQu Wenruo 
249557ea5ddSQu Wenruo 	/*
25052042d8eSAndrea Gelmini 	 * Support for new compression/encryption must introduce incompat flag,
251557ea5ddSQu Wenruo 	 * and must be caught in open_ctree().
252557ea5ddSQu Wenruo 	 */
253c7c01a4aSDavid Sterba 	if (unlikely(btrfs_file_extent_compression(leaf, fi) >=
254c7c01a4aSDavid Sterba 		     BTRFS_NR_COMPRESS_TYPES)) {
2551fd715ffSDavid Sterba 		file_extent_err(leaf, slot,
2568806d718SQu Wenruo 	"invalid compression for file extent, have %u expect range [0, %u]",
2578806d718SQu Wenruo 			btrfs_file_extent_compression(leaf, fi),
258ce96b7ffSChengguang Xu 			BTRFS_NR_COMPRESS_TYPES - 1);
259557ea5ddSQu Wenruo 		return -EUCLEAN;
260557ea5ddSQu Wenruo 	}
261c7c01a4aSDavid Sterba 	if (unlikely(btrfs_file_extent_encryption(leaf, fi))) {
2621fd715ffSDavid Sterba 		file_extent_err(leaf, slot,
2638806d718SQu Wenruo 			"invalid encryption for file extent, have %u expect 0",
2648806d718SQu Wenruo 			btrfs_file_extent_encryption(leaf, fi));
265557ea5ddSQu Wenruo 		return -EUCLEAN;
266557ea5ddSQu Wenruo 	}
267557ea5ddSQu Wenruo 	if (btrfs_file_extent_type(leaf, fi) == BTRFS_FILE_EXTENT_INLINE) {
268557ea5ddSQu Wenruo 		/* Inline extent must have 0 as key offset */
269c7c01a4aSDavid Sterba 		if (unlikely(key->offset)) {
2701fd715ffSDavid Sterba 			file_extent_err(leaf, slot,
2718806d718SQu Wenruo 		"invalid file_offset for inline file extent, have %llu expect 0",
2728806d718SQu Wenruo 				key->offset);
273557ea5ddSQu Wenruo 			return -EUCLEAN;
274557ea5ddSQu Wenruo 		}
275557ea5ddSQu Wenruo 
276557ea5ddSQu Wenruo 		/* Compressed inline extent has no on-disk size, skip it */
277557ea5ddSQu Wenruo 		if (btrfs_file_extent_compression(leaf, fi) !=
278557ea5ddSQu Wenruo 		    BTRFS_COMPRESS_NONE)
279557ea5ddSQu Wenruo 			return 0;
280557ea5ddSQu Wenruo 
281557ea5ddSQu Wenruo 		/* Uncompressed inline extent size must match item size */
282c7c01a4aSDavid Sterba 		if (unlikely(item_size != BTRFS_FILE_EXTENT_INLINE_DATA_START +
283c7c01a4aSDavid Sterba 					  btrfs_file_extent_ram_bytes(leaf, fi))) {
2841fd715ffSDavid Sterba 			file_extent_err(leaf, slot,
2858806d718SQu Wenruo 	"invalid ram_bytes for uncompressed inline extent, have %u expect %llu",
2868806d718SQu Wenruo 				item_size, BTRFS_FILE_EXTENT_INLINE_DATA_START +
2878806d718SQu Wenruo 				btrfs_file_extent_ram_bytes(leaf, fi));
288557ea5ddSQu Wenruo 			return -EUCLEAN;
289557ea5ddSQu Wenruo 		}
290557ea5ddSQu Wenruo 		return 0;
291557ea5ddSQu Wenruo 	}
292557ea5ddSQu Wenruo 
293557ea5ddSQu Wenruo 	/* Regular or preallocated extent has fixed item size */
294c7c01a4aSDavid Sterba 	if (unlikely(item_size != sizeof(*fi))) {
2951fd715ffSDavid Sterba 		file_extent_err(leaf, slot,
296709a95c3SArnd Bergmann 	"invalid item size for reg/prealloc file extent, have %u expect %zu",
2978806d718SQu Wenruo 			item_size, sizeof(*fi));
298557ea5ddSQu Wenruo 		return -EUCLEAN;
299557ea5ddSQu Wenruo 	}
300c7c01a4aSDavid Sterba 	if (unlikely(CHECK_FE_ALIGNED(leaf, slot, fi, ram_bytes, sectorsize) ||
301033774dcSDavid Sterba 		     CHECK_FE_ALIGNED(leaf, slot, fi, disk_bytenr, sectorsize) ||
302033774dcSDavid Sterba 		     CHECK_FE_ALIGNED(leaf, slot, fi, disk_num_bytes, sectorsize) ||
303033774dcSDavid Sterba 		     CHECK_FE_ALIGNED(leaf, slot, fi, offset, sectorsize) ||
304c7c01a4aSDavid Sterba 		     CHECK_FE_ALIGNED(leaf, slot, fi, num_bytes, sectorsize)))
305557ea5ddSQu Wenruo 		return -EUCLEAN;
3064e9845efSFilipe Manana 
3074c094c33SQu Wenruo 	/* Catch extent end overflow */
308c7c01a4aSDavid Sterba 	if (unlikely(check_add_overflow(btrfs_file_extent_num_bytes(leaf, fi),
309c7c01a4aSDavid Sterba 					key->offset, &extent_end))) {
3104c094c33SQu Wenruo 		file_extent_err(leaf, slot,
3114c094c33SQu Wenruo 	"extent end overflow, have file offset %llu extent num bytes %llu",
3124c094c33SQu Wenruo 				key->offset,
3134c094c33SQu Wenruo 				btrfs_file_extent_num_bytes(leaf, fi));
3144c094c33SQu Wenruo 		return -EUCLEAN;
3154c094c33SQu Wenruo 	}
3164c094c33SQu Wenruo 
3174e9845efSFilipe Manana 	/*
3184e9845efSFilipe Manana 	 * Check that no two consecutive file extent items, in the same leaf,
3194e9845efSFilipe Manana 	 * present ranges that overlap each other.
3204e9845efSFilipe Manana 	 */
3214e9845efSFilipe Manana 	if (slot > 0 &&
3224e9845efSFilipe Manana 	    prev_key->objectid == key->objectid &&
3234e9845efSFilipe Manana 	    prev_key->type == BTRFS_EXTENT_DATA_KEY) {
3244e9845efSFilipe Manana 		struct btrfs_file_extent_item *prev_fi;
3254e9845efSFilipe Manana 		u64 prev_end;
3264e9845efSFilipe Manana 
3274e9845efSFilipe Manana 		prev_fi = btrfs_item_ptr(leaf, slot - 1,
3284e9845efSFilipe Manana 					 struct btrfs_file_extent_item);
3294e9845efSFilipe Manana 		prev_end = file_extent_end(leaf, prev_key, prev_fi);
330c7c01a4aSDavid Sterba 		if (unlikely(prev_end > key->offset)) {
3314e9845efSFilipe Manana 			file_extent_err(leaf, slot - 1,
3324e9845efSFilipe Manana "file extent end range (%llu) goes beyond start offset (%llu) of the next file extent",
3334e9845efSFilipe Manana 					prev_end, key->offset);
3344e9845efSFilipe Manana 			return -EUCLEAN;
3354e9845efSFilipe Manana 		}
3364e9845efSFilipe Manana 	}
3374e9845efSFilipe Manana 
338557ea5ddSQu Wenruo 	return 0;
339557ea5ddSQu Wenruo }
340557ea5ddSQu Wenruo 
34168128ce7SDavid Sterba static int check_csum_item(struct extent_buffer *leaf, struct btrfs_key *key,
342ad1d8c43SFilipe Manana 			   int slot, struct btrfs_key *prev_key)
343557ea5ddSQu Wenruo {
34468128ce7SDavid Sterba 	struct btrfs_fs_info *fs_info = leaf->fs_info;
3452f659546SQu Wenruo 	u32 sectorsize = fs_info->sectorsize;
346223486c2SDavid Sterba 	const u32 csumsize = fs_info->csum_size;
347557ea5ddSQu Wenruo 
348c7c01a4aSDavid Sterba 	if (unlikely(key->objectid != BTRFS_EXTENT_CSUM_OBJECTID)) {
34986a6be3aSDavid Sterba 		generic_err(leaf, slot,
350d508c5f0SQu Wenruo 		"invalid key objectid for csum item, have %llu expect %llu",
351d508c5f0SQu Wenruo 			key->objectid, BTRFS_EXTENT_CSUM_OBJECTID);
352557ea5ddSQu Wenruo 		return -EUCLEAN;
353557ea5ddSQu Wenruo 	}
354c7c01a4aSDavid Sterba 	if (unlikely(!IS_ALIGNED(key->offset, sectorsize))) {
35586a6be3aSDavid Sterba 		generic_err(leaf, slot,
356d508c5f0SQu Wenruo 	"unaligned key offset for csum item, have %llu should be aligned to %u",
357d508c5f0SQu Wenruo 			key->offset, sectorsize);
358557ea5ddSQu Wenruo 		return -EUCLEAN;
359557ea5ddSQu Wenruo 	}
3603212fa14SJosef Bacik 	if (unlikely(!IS_ALIGNED(btrfs_item_size(leaf, slot), csumsize))) {
36186a6be3aSDavid Sterba 		generic_err(leaf, slot,
362d508c5f0SQu Wenruo 	"unaligned item size for csum item, have %u should be aligned to %u",
3633212fa14SJosef Bacik 			btrfs_item_size(leaf, slot), csumsize);
364557ea5ddSQu Wenruo 		return -EUCLEAN;
365557ea5ddSQu Wenruo 	}
366ad1d8c43SFilipe Manana 	if (slot > 0 && prev_key->type == BTRFS_EXTENT_CSUM_KEY) {
367ad1d8c43SFilipe Manana 		u64 prev_csum_end;
368ad1d8c43SFilipe Manana 		u32 prev_item_size;
369ad1d8c43SFilipe Manana 
3703212fa14SJosef Bacik 		prev_item_size = btrfs_item_size(leaf, slot - 1);
371ad1d8c43SFilipe Manana 		prev_csum_end = (prev_item_size / csumsize) * sectorsize;
372ad1d8c43SFilipe Manana 		prev_csum_end += prev_key->offset;
373c7c01a4aSDavid Sterba 		if (unlikely(prev_csum_end > key->offset)) {
374ad1d8c43SFilipe Manana 			generic_err(leaf, slot - 1,
375ad1d8c43SFilipe Manana "csum end range (%llu) goes beyond the start range (%llu) of the next csum item",
376ad1d8c43SFilipe Manana 				    prev_csum_end, key->offset);
377ad1d8c43SFilipe Manana 			return -EUCLEAN;
378ad1d8c43SFilipe Manana 		}
379ad1d8c43SFilipe Manana 	}
380557ea5ddSQu Wenruo 	return 0;
381557ea5ddSQu Wenruo }
382557ea5ddSQu Wenruo 
383c23c77b0SQu Wenruo /* Inode item error output has the same format as dir_item_err() */
384c23c77b0SQu Wenruo #define inode_item_err(eb, slot, fmt, ...)			\
385c23c77b0SQu Wenruo 	dir_item_err(eb, slot, fmt, __VA_ARGS__)
386c23c77b0SQu Wenruo 
387c23c77b0SQu Wenruo static int check_inode_key(struct extent_buffer *leaf, struct btrfs_key *key,
388c23c77b0SQu Wenruo 			   int slot)
389c23c77b0SQu Wenruo {
390c23c77b0SQu Wenruo 	struct btrfs_key item_key;
391c23c77b0SQu Wenruo 	bool is_inode_item;
392c23c77b0SQu Wenruo 
393c23c77b0SQu Wenruo 	btrfs_item_key_to_cpu(leaf, &item_key, slot);
394c23c77b0SQu Wenruo 	is_inode_item = (item_key.type == BTRFS_INODE_ITEM_KEY);
395c23c77b0SQu Wenruo 
396c23c77b0SQu Wenruo 	/* For XATTR_ITEM, location key should be all 0 */
397c23c77b0SQu Wenruo 	if (item_key.type == BTRFS_XATTR_ITEM_KEY) {
398c7c01a4aSDavid Sterba 		if (unlikely(key->objectid != 0 || key->type != 0 ||
399c7c01a4aSDavid Sterba 			     key->offset != 0))
400c23c77b0SQu Wenruo 			return -EUCLEAN;
401c23c77b0SQu Wenruo 		return 0;
402c23c77b0SQu Wenruo 	}
403c23c77b0SQu Wenruo 
404c7c01a4aSDavid Sterba 	if (unlikely((key->objectid < BTRFS_FIRST_FREE_OBJECTID ||
405c23c77b0SQu Wenruo 		      key->objectid > BTRFS_LAST_FREE_OBJECTID) &&
406c23c77b0SQu Wenruo 		     key->objectid != BTRFS_ROOT_TREE_DIR_OBJECTID &&
407c7c01a4aSDavid Sterba 		     key->objectid != BTRFS_FREE_INO_OBJECTID)) {
408c23c77b0SQu Wenruo 		if (is_inode_item) {
409c23c77b0SQu Wenruo 			generic_err(leaf, slot,
410c23c77b0SQu Wenruo 	"invalid key objectid: has %llu expect %llu or [%llu, %llu] or %llu",
411c23c77b0SQu Wenruo 				key->objectid, BTRFS_ROOT_TREE_DIR_OBJECTID,
412c23c77b0SQu Wenruo 				BTRFS_FIRST_FREE_OBJECTID,
413c23c77b0SQu Wenruo 				BTRFS_LAST_FREE_OBJECTID,
414c23c77b0SQu Wenruo 				BTRFS_FREE_INO_OBJECTID);
415c23c77b0SQu Wenruo 		} else {
416c23c77b0SQu Wenruo 			dir_item_err(leaf, slot,
417c23c77b0SQu Wenruo "invalid location key objectid: has %llu expect %llu or [%llu, %llu] or %llu",
418c23c77b0SQu Wenruo 				key->objectid, BTRFS_ROOT_TREE_DIR_OBJECTID,
419c23c77b0SQu Wenruo 				BTRFS_FIRST_FREE_OBJECTID,
420c23c77b0SQu Wenruo 				BTRFS_LAST_FREE_OBJECTID,
421c23c77b0SQu Wenruo 				BTRFS_FREE_INO_OBJECTID);
422c23c77b0SQu Wenruo 		}
423c23c77b0SQu Wenruo 		return -EUCLEAN;
424c23c77b0SQu Wenruo 	}
425c7c01a4aSDavid Sterba 	if (unlikely(key->offset != 0)) {
426c23c77b0SQu Wenruo 		if (is_inode_item)
427c23c77b0SQu Wenruo 			inode_item_err(leaf, slot,
428c23c77b0SQu Wenruo 				       "invalid key offset: has %llu expect 0",
429c23c77b0SQu Wenruo 				       key->offset);
430c23c77b0SQu Wenruo 		else
431c23c77b0SQu Wenruo 			dir_item_err(leaf, slot,
432c23c77b0SQu Wenruo 				"invalid location key offset:has %llu expect 0",
433c23c77b0SQu Wenruo 				key->offset);
434c23c77b0SQu Wenruo 		return -EUCLEAN;
435c23c77b0SQu Wenruo 	}
436c23c77b0SQu Wenruo 	return 0;
437c23c77b0SQu Wenruo }
438c23c77b0SQu Wenruo 
43957a0e674SQu Wenruo static int check_root_key(struct extent_buffer *leaf, struct btrfs_key *key,
44057a0e674SQu Wenruo 			  int slot)
44157a0e674SQu Wenruo {
44257a0e674SQu Wenruo 	struct btrfs_key item_key;
44357a0e674SQu Wenruo 	bool is_root_item;
44457a0e674SQu Wenruo 
44557a0e674SQu Wenruo 	btrfs_item_key_to_cpu(leaf, &item_key, slot);
44657a0e674SQu Wenruo 	is_root_item = (item_key.type == BTRFS_ROOT_ITEM_KEY);
44757a0e674SQu Wenruo 
44857a0e674SQu Wenruo 	/* No such tree id */
449c7c01a4aSDavid Sterba 	if (unlikely(key->objectid == 0)) {
45057a0e674SQu Wenruo 		if (is_root_item)
45157a0e674SQu Wenruo 			generic_err(leaf, slot, "invalid root id 0");
45257a0e674SQu Wenruo 		else
45357a0e674SQu Wenruo 			dir_item_err(leaf, slot,
45457a0e674SQu Wenruo 				     "invalid location key root id 0");
45557a0e674SQu Wenruo 		return -EUCLEAN;
45657a0e674SQu Wenruo 	}
45757a0e674SQu Wenruo 
45857a0e674SQu Wenruo 	/* DIR_ITEM/INDEX/INODE_REF is not allowed to point to non-fs trees */
459c7c01a4aSDavid Sterba 	if (unlikely(!is_fstree(key->objectid) && !is_root_item)) {
46057a0e674SQu Wenruo 		dir_item_err(leaf, slot,
46157a0e674SQu Wenruo 		"invalid location key objectid, have %llu expect [%llu, %llu]",
46257a0e674SQu Wenruo 				key->objectid, BTRFS_FIRST_FREE_OBJECTID,
46357a0e674SQu Wenruo 				BTRFS_LAST_FREE_OBJECTID);
46457a0e674SQu Wenruo 		return -EUCLEAN;
46557a0e674SQu Wenruo 	}
46657a0e674SQu Wenruo 
46757a0e674SQu Wenruo 	/*
46857a0e674SQu Wenruo 	 * ROOT_ITEM with non-zero offset means this is a snapshot, created at
46957a0e674SQu Wenruo 	 * @offset transid.
47057a0e674SQu Wenruo 	 * Furthermore, for location key in DIR_ITEM, its offset is always -1.
47157a0e674SQu Wenruo 	 *
47257a0e674SQu Wenruo 	 * So here we only check offset for reloc tree whose key->offset must
47357a0e674SQu Wenruo 	 * be a valid tree.
47457a0e674SQu Wenruo 	 */
475c7c01a4aSDavid Sterba 	if (unlikely(key->objectid == BTRFS_TREE_RELOC_OBJECTID &&
476c7c01a4aSDavid Sterba 		     key->offset == 0)) {
47757a0e674SQu Wenruo 		generic_err(leaf, slot, "invalid root id 0 for reloc tree");
47857a0e674SQu Wenruo 		return -EUCLEAN;
47957a0e674SQu Wenruo 	}
48057a0e674SQu Wenruo 	return 0;
48157a0e674SQu Wenruo }
48257a0e674SQu Wenruo 
483ce4252c0SDavid Sterba static int check_dir_item(struct extent_buffer *leaf,
484c18679ebSQu Wenruo 			  struct btrfs_key *key, struct btrfs_key *prev_key,
485c18679ebSQu Wenruo 			  int slot)
486ad7b0368SQu Wenruo {
487ce4252c0SDavid Sterba 	struct btrfs_fs_info *fs_info = leaf->fs_info;
488ad7b0368SQu Wenruo 	struct btrfs_dir_item *di;
4893212fa14SJosef Bacik 	u32 item_size = btrfs_item_size(leaf, slot);
490ad7b0368SQu Wenruo 	u32 cur = 0;
491ad7b0368SQu Wenruo 
492c7c01a4aSDavid Sterba 	if (unlikely(!check_prev_ino(leaf, key, slot, prev_key)))
493c18679ebSQu Wenruo 		return -EUCLEAN;
494c7c01a4aSDavid Sterba 
495ad7b0368SQu Wenruo 	di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
496ad7b0368SQu Wenruo 	while (cur < item_size) {
497147a097cSQu Wenruo 		struct btrfs_key location_key;
498ad7b0368SQu Wenruo 		u32 name_len;
499ad7b0368SQu Wenruo 		u32 data_len;
500ad7b0368SQu Wenruo 		u32 max_name_len;
501ad7b0368SQu Wenruo 		u32 total_size;
502ad7b0368SQu Wenruo 		u32 name_hash;
503ad7b0368SQu Wenruo 		u8 dir_type;
504147a097cSQu Wenruo 		int ret;
505ad7b0368SQu Wenruo 
506ad7b0368SQu Wenruo 		/* header itself should not cross item boundary */
507c7c01a4aSDavid Sterba 		if (unlikely(cur + sizeof(*di) > item_size)) {
508d98ced68SDavid Sterba 			dir_item_err(leaf, slot,
5097cfad652SArnd Bergmann 		"dir item header crosses item boundary, have %zu boundary %u",
510ad7b0368SQu Wenruo 				cur + sizeof(*di), item_size);
511ad7b0368SQu Wenruo 			return -EUCLEAN;
512ad7b0368SQu Wenruo 		}
513ad7b0368SQu Wenruo 
514147a097cSQu Wenruo 		/* Location key check */
515147a097cSQu Wenruo 		btrfs_dir_item_key_to_cpu(leaf, di, &location_key);
516147a097cSQu Wenruo 		if (location_key.type == BTRFS_ROOT_ITEM_KEY) {
517147a097cSQu Wenruo 			ret = check_root_key(leaf, &location_key, slot);
518c7c01a4aSDavid Sterba 			if (unlikely(ret < 0))
519147a097cSQu Wenruo 				return ret;
520147a097cSQu Wenruo 		} else if (location_key.type == BTRFS_INODE_ITEM_KEY ||
521147a097cSQu Wenruo 			   location_key.type == 0) {
522147a097cSQu Wenruo 			ret = check_inode_key(leaf, &location_key, slot);
523c7c01a4aSDavid Sterba 			if (unlikely(ret < 0))
524147a097cSQu Wenruo 				return ret;
525147a097cSQu Wenruo 		} else {
526147a097cSQu Wenruo 			dir_item_err(leaf, slot,
527147a097cSQu Wenruo 			"invalid location key type, have %u, expect %u or %u",
528147a097cSQu Wenruo 				     location_key.type, BTRFS_ROOT_ITEM_KEY,
529147a097cSQu Wenruo 				     BTRFS_INODE_ITEM_KEY);
530147a097cSQu Wenruo 			return -EUCLEAN;
531147a097cSQu Wenruo 		}
532147a097cSQu Wenruo 
533ad7b0368SQu Wenruo 		/* dir type check */
534*94a48aefSOmar Sandoval 		dir_type = btrfs_dir_ftype(leaf, di);
535c7c01a4aSDavid Sterba 		if (unlikely(dir_type >= BTRFS_FT_MAX)) {
536d98ced68SDavid Sterba 			dir_item_err(leaf, slot,
537ad7b0368SQu Wenruo 			"invalid dir item type, have %u expect [0, %u)",
538ad7b0368SQu Wenruo 				dir_type, BTRFS_FT_MAX);
539ad7b0368SQu Wenruo 			return -EUCLEAN;
540ad7b0368SQu Wenruo 		}
541ad7b0368SQu Wenruo 
542c7c01a4aSDavid Sterba 		if (unlikely(key->type == BTRFS_XATTR_ITEM_KEY &&
543c7c01a4aSDavid Sterba 			     dir_type != BTRFS_FT_XATTR)) {
544d98ced68SDavid Sterba 			dir_item_err(leaf, slot,
545ad7b0368SQu Wenruo 		"invalid dir item type for XATTR key, have %u expect %u",
546ad7b0368SQu Wenruo 				dir_type, BTRFS_FT_XATTR);
547ad7b0368SQu Wenruo 			return -EUCLEAN;
548ad7b0368SQu Wenruo 		}
549c7c01a4aSDavid Sterba 		if (unlikely(dir_type == BTRFS_FT_XATTR &&
550c7c01a4aSDavid Sterba 			     key->type != BTRFS_XATTR_ITEM_KEY)) {
551d98ced68SDavid Sterba 			dir_item_err(leaf, slot,
552ad7b0368SQu Wenruo 			"xattr dir type found for non-XATTR key");
553ad7b0368SQu Wenruo 			return -EUCLEAN;
554ad7b0368SQu Wenruo 		}
555ad7b0368SQu Wenruo 		if (dir_type == BTRFS_FT_XATTR)
556ad7b0368SQu Wenruo 			max_name_len = XATTR_NAME_MAX;
557ad7b0368SQu Wenruo 		else
558ad7b0368SQu Wenruo 			max_name_len = BTRFS_NAME_LEN;
559ad7b0368SQu Wenruo 
560ad7b0368SQu Wenruo 		/* Name/data length check */
561ad7b0368SQu Wenruo 		name_len = btrfs_dir_name_len(leaf, di);
562ad7b0368SQu Wenruo 		data_len = btrfs_dir_data_len(leaf, di);
563c7c01a4aSDavid Sterba 		if (unlikely(name_len > max_name_len)) {
564d98ced68SDavid Sterba 			dir_item_err(leaf, slot,
565ad7b0368SQu Wenruo 			"dir item name len too long, have %u max %u",
566ad7b0368SQu Wenruo 				name_len, max_name_len);
567ad7b0368SQu Wenruo 			return -EUCLEAN;
568ad7b0368SQu Wenruo 		}
569c7c01a4aSDavid Sterba 		if (unlikely(name_len + data_len > BTRFS_MAX_XATTR_SIZE(fs_info))) {
570d98ced68SDavid Sterba 			dir_item_err(leaf, slot,
571ad7b0368SQu Wenruo 			"dir item name and data len too long, have %u max %u",
572ad7b0368SQu Wenruo 				name_len + data_len,
5732f659546SQu Wenruo 				BTRFS_MAX_XATTR_SIZE(fs_info));
574ad7b0368SQu Wenruo 			return -EUCLEAN;
575ad7b0368SQu Wenruo 		}
576ad7b0368SQu Wenruo 
577c7c01a4aSDavid Sterba 		if (unlikely(data_len && dir_type != BTRFS_FT_XATTR)) {
578d98ced68SDavid Sterba 			dir_item_err(leaf, slot,
579ad7b0368SQu Wenruo 			"dir item with invalid data len, have %u expect 0",
580ad7b0368SQu Wenruo 				data_len);
581ad7b0368SQu Wenruo 			return -EUCLEAN;
582ad7b0368SQu Wenruo 		}
583ad7b0368SQu Wenruo 
584ad7b0368SQu Wenruo 		total_size = sizeof(*di) + name_len + data_len;
585ad7b0368SQu Wenruo 
586ad7b0368SQu Wenruo 		/* header and name/data should not cross item boundary */
587c7c01a4aSDavid Sterba 		if (unlikely(cur + total_size > item_size)) {
588d98ced68SDavid Sterba 			dir_item_err(leaf, slot,
589ad7b0368SQu Wenruo 		"dir item data crosses item boundary, have %u boundary %u",
590ad7b0368SQu Wenruo 				cur + total_size, item_size);
591ad7b0368SQu Wenruo 			return -EUCLEAN;
592ad7b0368SQu Wenruo 		}
593ad7b0368SQu Wenruo 
594ad7b0368SQu Wenruo 		/*
595ad7b0368SQu Wenruo 		 * Special check for XATTR/DIR_ITEM, as key->offset is name
596ad7b0368SQu Wenruo 		 * hash, should match its name
597ad7b0368SQu Wenruo 		 */
598ad7b0368SQu Wenruo 		if (key->type == BTRFS_DIR_ITEM_KEY ||
599ad7b0368SQu Wenruo 		    key->type == BTRFS_XATTR_ITEM_KEY) {
600e2683fc9SDavid Sterba 			char namebuf[max(BTRFS_NAME_LEN, XATTR_NAME_MAX)];
601e2683fc9SDavid Sterba 
602ad7b0368SQu Wenruo 			read_extent_buffer(leaf, namebuf,
603ad7b0368SQu Wenruo 					(unsigned long)(di + 1), name_len);
604ad7b0368SQu Wenruo 			name_hash = btrfs_name_hash(namebuf, name_len);
605c7c01a4aSDavid Sterba 			if (unlikely(key->offset != name_hash)) {
606d98ced68SDavid Sterba 				dir_item_err(leaf, slot,
607ad7b0368SQu Wenruo 		"name hash mismatch with key, have 0x%016x expect 0x%016llx",
608ad7b0368SQu Wenruo 					name_hash, key->offset);
609ad7b0368SQu Wenruo 				return -EUCLEAN;
610ad7b0368SQu Wenruo 			}
611ad7b0368SQu Wenruo 		}
612ad7b0368SQu Wenruo 		cur += total_size;
613ad7b0368SQu Wenruo 		di = (struct btrfs_dir_item *)((void *)di + total_size);
614ad7b0368SQu Wenruo 	}
615ad7b0368SQu Wenruo 	return 0;
616ad7b0368SQu Wenruo }
617ad7b0368SQu Wenruo 
6184806bd88SDavid Sterba __printf(3, 4)
619fce466eaSQu Wenruo __cold
6204806bd88SDavid Sterba static void block_group_err(const struct extent_buffer *eb, int slot,
621fce466eaSQu Wenruo 			    const char *fmt, ...)
622fce466eaSQu Wenruo {
6234806bd88SDavid Sterba 	const struct btrfs_fs_info *fs_info = eb->fs_info;
624fce466eaSQu Wenruo 	struct btrfs_key key;
625fce466eaSQu Wenruo 	struct va_format vaf;
626fce466eaSQu Wenruo 	va_list args;
627fce466eaSQu Wenruo 
628fce466eaSQu Wenruo 	btrfs_item_key_to_cpu(eb, &key, slot);
629fce466eaSQu Wenruo 	va_start(args, fmt);
630fce466eaSQu Wenruo 
631fce466eaSQu Wenruo 	vaf.fmt = fmt;
632fce466eaSQu Wenruo 	vaf.va = &args;
633fce466eaSQu Wenruo 
634fce466eaSQu Wenruo 	btrfs_crit(fs_info,
635fce466eaSQu Wenruo 	"corrupt %s: root=%llu block=%llu slot=%d bg_start=%llu bg_len=%llu, %pV",
636fce466eaSQu Wenruo 		btrfs_header_level(eb) == 0 ? "leaf" : "node",
637fce466eaSQu Wenruo 		btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot,
638fce466eaSQu Wenruo 		key.objectid, key.offset, &vaf);
639fce466eaSQu Wenruo 	va_end(args);
640fce466eaSQu Wenruo }
641fce466eaSQu Wenruo 
642af60ce2bSDavid Sterba static int check_block_group_item(struct extent_buffer *leaf,
643fce466eaSQu Wenruo 				  struct btrfs_key *key, int slot)
644fce466eaSQu Wenruo {
645f7238e50SJosef Bacik 	struct btrfs_fs_info *fs_info = leaf->fs_info;
646fce466eaSQu Wenruo 	struct btrfs_block_group_item bgi;
6473212fa14SJosef Bacik 	u32 item_size = btrfs_item_size(leaf, slot);
648f7238e50SJosef Bacik 	u64 chunk_objectid;
649fce466eaSQu Wenruo 	u64 flags;
650fce466eaSQu Wenruo 	u64 type;
651fce466eaSQu Wenruo 
652fce466eaSQu Wenruo 	/*
653fce466eaSQu Wenruo 	 * Here we don't really care about alignment since extent allocator can
65410950929SQu Wenruo 	 * handle it.  We care more about the size.
655fce466eaSQu Wenruo 	 */
656c7c01a4aSDavid Sterba 	if (unlikely(key->offset == 0)) {
6574806bd88SDavid Sterba 		block_group_err(leaf, slot,
65810950929SQu Wenruo 				"invalid block group size 0");
659fce466eaSQu Wenruo 		return -EUCLEAN;
660fce466eaSQu Wenruo 	}
661fce466eaSQu Wenruo 
662c7c01a4aSDavid Sterba 	if (unlikely(item_size != sizeof(bgi))) {
6634806bd88SDavid Sterba 		block_group_err(leaf, slot,
664fce466eaSQu Wenruo 			"invalid item size, have %u expect %zu",
665fce466eaSQu Wenruo 				item_size, sizeof(bgi));
666fce466eaSQu Wenruo 		return -EUCLEAN;
667fce466eaSQu Wenruo 	}
668fce466eaSQu Wenruo 
669fce466eaSQu Wenruo 	read_extent_buffer(leaf, &bgi, btrfs_item_ptr_offset(leaf, slot),
670fce466eaSQu Wenruo 			   sizeof(bgi));
671f7238e50SJosef Bacik 	chunk_objectid = btrfs_stack_block_group_chunk_objectid(&bgi);
672f7238e50SJosef Bacik 	if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) {
673f7238e50SJosef Bacik 		/*
674f7238e50SJosef Bacik 		 * We don't init the nr_global_roots until we load the global
675f7238e50SJosef Bacik 		 * roots, so this could be 0 at mount time.  If it's 0 we'll
676f7238e50SJosef Bacik 		 * just assume we're fine, and later we'll check against our
677f7238e50SJosef Bacik 		 * actual value.
678f7238e50SJosef Bacik 		 */
679f7238e50SJosef Bacik 		if (unlikely(fs_info->nr_global_roots &&
680f7238e50SJosef Bacik 			     chunk_objectid >= fs_info->nr_global_roots)) {
681f7238e50SJosef Bacik 			block_group_err(leaf, slot,
682f7238e50SJosef Bacik 	"invalid block group global root id, have %llu, needs to be <= %llu",
683f7238e50SJosef Bacik 					chunk_objectid,
684f7238e50SJosef Bacik 					fs_info->nr_global_roots);
685f7238e50SJosef Bacik 			return -EUCLEAN;
686f7238e50SJosef Bacik 		}
687f7238e50SJosef Bacik 	} else if (unlikely(chunk_objectid != BTRFS_FIRST_CHUNK_TREE_OBJECTID)) {
6884806bd88SDavid Sterba 		block_group_err(leaf, slot,
689fce466eaSQu Wenruo 		"invalid block group chunk objectid, have %llu expect %llu",
690de0dc456SDavid Sterba 				btrfs_stack_block_group_chunk_objectid(&bgi),
691fce466eaSQu Wenruo 				BTRFS_FIRST_CHUNK_TREE_OBJECTID);
692fce466eaSQu Wenruo 		return -EUCLEAN;
693fce466eaSQu Wenruo 	}
694fce466eaSQu Wenruo 
695c7c01a4aSDavid Sterba 	if (unlikely(btrfs_stack_block_group_used(&bgi) > key->offset)) {
6964806bd88SDavid Sterba 		block_group_err(leaf, slot,
697fce466eaSQu Wenruo 			"invalid block group used, have %llu expect [0, %llu)",
698de0dc456SDavid Sterba 				btrfs_stack_block_group_used(&bgi), key->offset);
699fce466eaSQu Wenruo 		return -EUCLEAN;
700fce466eaSQu Wenruo 	}
701fce466eaSQu Wenruo 
702de0dc456SDavid Sterba 	flags = btrfs_stack_block_group_flags(&bgi);
703c7c01a4aSDavid Sterba 	if (unlikely(hweight64(flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) > 1)) {
7044806bd88SDavid Sterba 		block_group_err(leaf, slot,
705fce466eaSQu Wenruo "invalid profile flags, have 0x%llx (%lu bits set) expect no more than 1 bit set",
706fce466eaSQu Wenruo 			flags & BTRFS_BLOCK_GROUP_PROFILE_MASK,
707fce466eaSQu Wenruo 			hweight64(flags & BTRFS_BLOCK_GROUP_PROFILE_MASK));
708fce466eaSQu Wenruo 		return -EUCLEAN;
709fce466eaSQu Wenruo 	}
710fce466eaSQu Wenruo 
711fce466eaSQu Wenruo 	type = flags & BTRFS_BLOCK_GROUP_TYPE_MASK;
712c7c01a4aSDavid Sterba 	if (unlikely(type != BTRFS_BLOCK_GROUP_DATA &&
713fce466eaSQu Wenruo 		     type != BTRFS_BLOCK_GROUP_METADATA &&
714fce466eaSQu Wenruo 		     type != BTRFS_BLOCK_GROUP_SYSTEM &&
715fce466eaSQu Wenruo 		     type != (BTRFS_BLOCK_GROUP_METADATA |
716c7c01a4aSDavid Sterba 			      BTRFS_BLOCK_GROUP_DATA))) {
7174806bd88SDavid Sterba 		block_group_err(leaf, slot,
718761333f2SShaokun Zhang "invalid type, have 0x%llx (%lu bits set) expect either 0x%llx, 0x%llx, 0x%llx or 0x%llx",
719fce466eaSQu Wenruo 			type, hweight64(type),
720fce466eaSQu Wenruo 			BTRFS_BLOCK_GROUP_DATA, BTRFS_BLOCK_GROUP_METADATA,
721fce466eaSQu Wenruo 			BTRFS_BLOCK_GROUP_SYSTEM,
722fce466eaSQu Wenruo 			BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA);
723fce466eaSQu Wenruo 		return -EUCLEAN;
724fce466eaSQu Wenruo 	}
725fce466eaSQu Wenruo 	return 0;
726fce466eaSQu Wenruo }
727fce466eaSQu Wenruo 
728d001e4a3SDavid Sterba __printf(4, 5)
729f1140243SQu Wenruo __cold
730d001e4a3SDavid Sterba static void chunk_err(const struct extent_buffer *leaf,
731f1140243SQu Wenruo 		      const struct btrfs_chunk *chunk, u64 logical,
732f1140243SQu Wenruo 		      const char *fmt, ...)
733f1140243SQu Wenruo {
734d001e4a3SDavid Sterba 	const struct btrfs_fs_info *fs_info = leaf->fs_info;
735f1140243SQu Wenruo 	bool is_sb;
736f1140243SQu Wenruo 	struct va_format vaf;
737f1140243SQu Wenruo 	va_list args;
738f1140243SQu Wenruo 	int i;
739f1140243SQu Wenruo 	int slot = -1;
740f1140243SQu Wenruo 
741f1140243SQu Wenruo 	/* Only superblock eb is able to have such small offset */
742f1140243SQu Wenruo 	is_sb = (leaf->start == BTRFS_SUPER_INFO_OFFSET);
743f1140243SQu Wenruo 
744f1140243SQu Wenruo 	if (!is_sb) {
745f1140243SQu Wenruo 		/*
746f1140243SQu Wenruo 		 * Get the slot number by iterating through all slots, this
747f1140243SQu Wenruo 		 * would provide better readability.
748f1140243SQu Wenruo 		 */
749f1140243SQu Wenruo 		for (i = 0; i < btrfs_header_nritems(leaf); i++) {
750f1140243SQu Wenruo 			if (btrfs_item_ptr_offset(leaf, i) ==
751f1140243SQu Wenruo 					(unsigned long)chunk) {
752f1140243SQu Wenruo 				slot = i;
753f1140243SQu Wenruo 				break;
754f1140243SQu Wenruo 			}
755f1140243SQu Wenruo 		}
756f1140243SQu Wenruo 	}
757f1140243SQu Wenruo 	va_start(args, fmt);
758f1140243SQu Wenruo 	vaf.fmt = fmt;
759f1140243SQu Wenruo 	vaf.va = &args;
760f1140243SQu Wenruo 
761f1140243SQu Wenruo 	if (is_sb)
762f1140243SQu Wenruo 		btrfs_crit(fs_info,
763f1140243SQu Wenruo 		"corrupt superblock syschunk array: chunk_start=%llu, %pV",
764f1140243SQu Wenruo 			   logical, &vaf);
765f1140243SQu Wenruo 	else
766f1140243SQu Wenruo 		btrfs_crit(fs_info,
767f1140243SQu Wenruo 	"corrupt leaf: root=%llu block=%llu slot=%d chunk_start=%llu, %pV",
768f1140243SQu Wenruo 			   BTRFS_CHUNK_TREE_OBJECTID, leaf->start, slot,
769f1140243SQu Wenruo 			   logical, &vaf);
770f1140243SQu Wenruo 	va_end(args);
771f1140243SQu Wenruo }
772f1140243SQu Wenruo 
773ad7b0368SQu Wenruo /*
77482fc28fbSQu Wenruo  * The common chunk check which could also work on super block sys chunk array.
77582fc28fbSQu Wenruo  *
776bf871c3bSQu Wenruo  * Return -EUCLEAN if anything is corrupted.
77782fc28fbSQu Wenruo  * Return 0 if everything is OK.
77882fc28fbSQu Wenruo  */
779ddaf1d5aSDavid Sterba int btrfs_check_chunk_valid(struct extent_buffer *leaf,
78082fc28fbSQu Wenruo 			    struct btrfs_chunk *chunk, u64 logical)
78182fc28fbSQu Wenruo {
782ddaf1d5aSDavid Sterba 	struct btrfs_fs_info *fs_info = leaf->fs_info;
78382fc28fbSQu Wenruo 	u64 length;
784347fb0cfSSu Yue 	u64 chunk_end;
78582fc28fbSQu Wenruo 	u64 stripe_len;
78682fc28fbSQu Wenruo 	u16 num_stripes;
78782fc28fbSQu Wenruo 	u16 sub_stripes;
78882fc28fbSQu Wenruo 	u64 type;
78982fc28fbSQu Wenruo 	u64 features;
79082fc28fbSQu Wenruo 	bool mixed = false;
79185d07fbeSDaniel Xu 	int raid_index;
79285d07fbeSDaniel Xu 	int nparity;
79385d07fbeSDaniel Xu 	int ncopies;
79482fc28fbSQu Wenruo 
79582fc28fbSQu Wenruo 	length = btrfs_chunk_length(leaf, chunk);
79682fc28fbSQu Wenruo 	stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
79782fc28fbSQu Wenruo 	num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
79882fc28fbSQu Wenruo 	sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
79982fc28fbSQu Wenruo 	type = btrfs_chunk_type(leaf, chunk);
80085d07fbeSDaniel Xu 	raid_index = btrfs_bg_flags_to_raid_index(type);
80185d07fbeSDaniel Xu 	ncopies = btrfs_raid_array[raid_index].ncopies;
80285d07fbeSDaniel Xu 	nparity = btrfs_raid_array[raid_index].nparity;
80382fc28fbSQu Wenruo 
804c7c01a4aSDavid Sterba 	if (unlikely(!num_stripes)) {
805d001e4a3SDavid Sterba 		chunk_err(leaf, chunk, logical,
806f1140243SQu Wenruo 			  "invalid chunk num_stripes, have %u", num_stripes);
807bf871c3bSQu Wenruo 		return -EUCLEAN;
80882fc28fbSQu Wenruo 	}
809c7c01a4aSDavid Sterba 	if (unlikely(num_stripes < ncopies)) {
81085d07fbeSDaniel Xu 		chunk_err(leaf, chunk, logical,
81185d07fbeSDaniel Xu 			  "invalid chunk num_stripes < ncopies, have %u < %d",
81285d07fbeSDaniel Xu 			  num_stripes, ncopies);
81385d07fbeSDaniel Xu 		return -EUCLEAN;
81485d07fbeSDaniel Xu 	}
815c7c01a4aSDavid Sterba 	if (unlikely(nparity && num_stripes == nparity)) {
81685d07fbeSDaniel Xu 		chunk_err(leaf, chunk, logical,
81785d07fbeSDaniel Xu 			  "invalid chunk num_stripes == nparity, have %u == %d",
81885d07fbeSDaniel Xu 			  num_stripes, nparity);
81985d07fbeSDaniel Xu 		return -EUCLEAN;
82085d07fbeSDaniel Xu 	}
821c7c01a4aSDavid Sterba 	if (unlikely(!IS_ALIGNED(logical, fs_info->sectorsize))) {
822d001e4a3SDavid Sterba 		chunk_err(leaf, chunk, logical,
823f1140243SQu Wenruo 		"invalid chunk logical, have %llu should aligned to %u",
824f1140243SQu Wenruo 			  logical, fs_info->sectorsize);
825bf871c3bSQu Wenruo 		return -EUCLEAN;
82682fc28fbSQu Wenruo 	}
827c7c01a4aSDavid Sterba 	if (unlikely(btrfs_chunk_sector_size(leaf, chunk) != fs_info->sectorsize)) {
828d001e4a3SDavid Sterba 		chunk_err(leaf, chunk, logical,
829f1140243SQu Wenruo 			  "invalid chunk sectorsize, have %u expect %u",
830f1140243SQu Wenruo 			  btrfs_chunk_sector_size(leaf, chunk),
831f1140243SQu Wenruo 			  fs_info->sectorsize);
832bf871c3bSQu Wenruo 		return -EUCLEAN;
83382fc28fbSQu Wenruo 	}
834c7c01a4aSDavid Sterba 	if (unlikely(!length || !IS_ALIGNED(length, fs_info->sectorsize))) {
835d001e4a3SDavid Sterba 		chunk_err(leaf, chunk, logical,
836f1140243SQu Wenruo 			  "invalid chunk length, have %llu", length);
837bf871c3bSQu Wenruo 		return -EUCLEAN;
83882fc28fbSQu Wenruo 	}
839347fb0cfSSu Yue 	if (unlikely(check_add_overflow(logical, length, &chunk_end))) {
840347fb0cfSSu Yue 		chunk_err(leaf, chunk, logical,
841347fb0cfSSu Yue "invalid chunk logical start and length, have logical start %llu length %llu",
842347fb0cfSSu Yue 			  logical, length);
843347fb0cfSSu Yue 		return -EUCLEAN;
844347fb0cfSSu Yue 	}
845c7c01a4aSDavid Sterba 	if (unlikely(!is_power_of_2(stripe_len) || stripe_len != BTRFS_STRIPE_LEN)) {
846d001e4a3SDavid Sterba 		chunk_err(leaf, chunk, logical,
847f1140243SQu Wenruo 			  "invalid chunk stripe length: %llu",
84882fc28fbSQu Wenruo 			  stripe_len);
849bf871c3bSQu Wenruo 		return -EUCLEAN;
85082fc28fbSQu Wenruo 	}
851c7c01a4aSDavid Sterba 	if (unlikely(type & ~(BTRFS_BLOCK_GROUP_TYPE_MASK |
852c7c01a4aSDavid Sterba 			      BTRFS_BLOCK_GROUP_PROFILE_MASK))) {
853d001e4a3SDavid Sterba 		chunk_err(leaf, chunk, logical,
854f1140243SQu Wenruo 			  "unrecognized chunk type: 0x%llx",
85582fc28fbSQu Wenruo 			  ~(BTRFS_BLOCK_GROUP_TYPE_MASK |
85682fc28fbSQu Wenruo 			    BTRFS_BLOCK_GROUP_PROFILE_MASK) &
85782fc28fbSQu Wenruo 			  btrfs_chunk_type(leaf, chunk));
858bf871c3bSQu Wenruo 		return -EUCLEAN;
85982fc28fbSQu Wenruo 	}
86082fc28fbSQu Wenruo 
861c7c01a4aSDavid Sterba 	if (unlikely(!has_single_bit_set(type & BTRFS_BLOCK_GROUP_PROFILE_MASK) &&
862c7c01a4aSDavid Sterba 		     (type & BTRFS_BLOCK_GROUP_PROFILE_MASK) != 0)) {
863d001e4a3SDavid Sterba 		chunk_err(leaf, chunk, logical,
86480e46cf2SQu Wenruo 		"invalid chunk profile flag: 0x%llx, expect 0 or 1 bit set",
86580e46cf2SQu Wenruo 			  type & BTRFS_BLOCK_GROUP_PROFILE_MASK);
86680e46cf2SQu Wenruo 		return -EUCLEAN;
86780e46cf2SQu Wenruo 	}
868c7c01a4aSDavid Sterba 	if (unlikely((type & BTRFS_BLOCK_GROUP_TYPE_MASK) == 0)) {
869d001e4a3SDavid Sterba 		chunk_err(leaf, chunk, logical,
870f1140243SQu Wenruo 	"missing chunk type flag, have 0x%llx one bit must be set in 0x%llx",
871f1140243SQu Wenruo 			  type, BTRFS_BLOCK_GROUP_TYPE_MASK);
872bf871c3bSQu Wenruo 		return -EUCLEAN;
87382fc28fbSQu Wenruo 	}
87482fc28fbSQu Wenruo 
875c7c01a4aSDavid Sterba 	if (unlikely((type & BTRFS_BLOCK_GROUP_SYSTEM) &&
876c7c01a4aSDavid Sterba 		     (type & (BTRFS_BLOCK_GROUP_METADATA |
877c7c01a4aSDavid Sterba 			      BTRFS_BLOCK_GROUP_DATA)))) {
878d001e4a3SDavid Sterba 		chunk_err(leaf, chunk, logical,
879f1140243SQu Wenruo 			  "system chunk with data or metadata type: 0x%llx",
880f1140243SQu Wenruo 			  type);
881bf871c3bSQu Wenruo 		return -EUCLEAN;
88282fc28fbSQu Wenruo 	}
88382fc28fbSQu Wenruo 
88482fc28fbSQu Wenruo 	features = btrfs_super_incompat_flags(fs_info->super_copy);
88582fc28fbSQu Wenruo 	if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
88682fc28fbSQu Wenruo 		mixed = true;
88782fc28fbSQu Wenruo 
88882fc28fbSQu Wenruo 	if (!mixed) {
889c7c01a4aSDavid Sterba 		if (unlikely((type & BTRFS_BLOCK_GROUP_METADATA) &&
890c7c01a4aSDavid Sterba 			     (type & BTRFS_BLOCK_GROUP_DATA))) {
891d001e4a3SDavid Sterba 			chunk_err(leaf, chunk, logical,
89282fc28fbSQu Wenruo 			"mixed chunk type in non-mixed mode: 0x%llx", type);
893bf871c3bSQu Wenruo 			return -EUCLEAN;
89482fc28fbSQu Wenruo 		}
89582fc28fbSQu Wenruo 	}
89682fc28fbSQu Wenruo 
8970ac6e06bSDavid Sterba 	if (unlikely((type & BTRFS_BLOCK_GROUP_RAID10 &&
8980ac6e06bSDavid Sterba 		      sub_stripes != btrfs_raid_array[BTRFS_RAID_RAID10].sub_stripes) ||
8990ac6e06bSDavid Sterba 		     (type & BTRFS_BLOCK_GROUP_RAID1 &&
9000ac6e06bSDavid Sterba 		      num_stripes != btrfs_raid_array[BTRFS_RAID_RAID1].devs_min) ||
9016c154ba4SDavid Sterba 		     (type & BTRFS_BLOCK_GROUP_RAID1C3 &&
9026c154ba4SDavid Sterba 		      num_stripes != btrfs_raid_array[BTRFS_RAID_RAID1C3].devs_min) ||
9036c154ba4SDavid Sterba 		     (type & BTRFS_BLOCK_GROUP_RAID1C4 &&
9046c154ba4SDavid Sterba 		      num_stripes != btrfs_raid_array[BTRFS_RAID_RAID1C4].devs_min) ||
9050ac6e06bSDavid Sterba 		     (type & BTRFS_BLOCK_GROUP_RAID5 &&
9060ac6e06bSDavid Sterba 		      num_stripes < btrfs_raid_array[BTRFS_RAID_RAID5].devs_min) ||
9070ac6e06bSDavid Sterba 		     (type & BTRFS_BLOCK_GROUP_RAID6 &&
9080ac6e06bSDavid Sterba 		      num_stripes < btrfs_raid_array[BTRFS_RAID_RAID6].devs_min) ||
9090ac6e06bSDavid Sterba 		     (type & BTRFS_BLOCK_GROUP_DUP &&
9100ac6e06bSDavid Sterba 		      num_stripes != btrfs_raid_array[BTRFS_RAID_DUP].dev_stripes) ||
911c7c01a4aSDavid Sterba 		     ((type & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 &&
9120ac6e06bSDavid Sterba 		      num_stripes != btrfs_raid_array[BTRFS_RAID_SINGLE].dev_stripes))) {
913d001e4a3SDavid Sterba 		chunk_err(leaf, chunk, logical,
91482fc28fbSQu Wenruo 			"invalid num_stripes:sub_stripes %u:%u for profile %llu",
91582fc28fbSQu Wenruo 			num_stripes, sub_stripes,
91682fc28fbSQu Wenruo 			type & BTRFS_BLOCK_GROUP_PROFILE_MASK);
917bf871c3bSQu Wenruo 		return -EUCLEAN;
91882fc28fbSQu Wenruo 	}
91982fc28fbSQu Wenruo 
92082fc28fbSQu Wenruo 	return 0;
92182fc28fbSQu Wenruo }
92282fc28fbSQu Wenruo 
923f6d2a5c2SQu Wenruo /*
924f6d2a5c2SQu Wenruo  * Enhanced version of chunk item checker.
925f6d2a5c2SQu Wenruo  *
926f6d2a5c2SQu Wenruo  * The common btrfs_check_chunk_valid() doesn't check item size since it needs
927f6d2a5c2SQu Wenruo  * to work on super block sys_chunk_array which doesn't have full item ptr.
928f6d2a5c2SQu Wenruo  */
929f6d2a5c2SQu Wenruo static int check_leaf_chunk_item(struct extent_buffer *leaf,
930f6d2a5c2SQu Wenruo 				 struct btrfs_chunk *chunk,
931f6d2a5c2SQu Wenruo 				 struct btrfs_key *key, int slot)
932f6d2a5c2SQu Wenruo {
933f6d2a5c2SQu Wenruo 	int num_stripes;
934f6d2a5c2SQu Wenruo 
9353212fa14SJosef Bacik 	if (unlikely(btrfs_item_size(leaf, slot) < sizeof(struct btrfs_chunk))) {
936f6d2a5c2SQu Wenruo 		chunk_err(leaf, chunk, key->offset,
937f6d2a5c2SQu Wenruo 			"invalid chunk item size: have %u expect [%zu, %u)",
9383212fa14SJosef Bacik 			btrfs_item_size(leaf, slot),
939f6d2a5c2SQu Wenruo 			sizeof(struct btrfs_chunk),
940f6d2a5c2SQu Wenruo 			BTRFS_LEAF_DATA_SIZE(leaf->fs_info));
941f6d2a5c2SQu Wenruo 		return -EUCLEAN;
942f6d2a5c2SQu Wenruo 	}
943f6d2a5c2SQu Wenruo 
944f6d2a5c2SQu Wenruo 	num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
945f6d2a5c2SQu Wenruo 	/* Let btrfs_check_chunk_valid() handle this error type */
946f6d2a5c2SQu Wenruo 	if (num_stripes == 0)
947f6d2a5c2SQu Wenruo 		goto out;
948f6d2a5c2SQu Wenruo 
949c7c01a4aSDavid Sterba 	if (unlikely(btrfs_chunk_item_size(num_stripes) !=
9503212fa14SJosef Bacik 		     btrfs_item_size(leaf, slot))) {
951f6d2a5c2SQu Wenruo 		chunk_err(leaf, chunk, key->offset,
952f6d2a5c2SQu Wenruo 			"invalid chunk item size: have %u expect %lu",
9533212fa14SJosef Bacik 			btrfs_item_size(leaf, slot),
954f6d2a5c2SQu Wenruo 			btrfs_chunk_item_size(num_stripes));
955f6d2a5c2SQu Wenruo 		return -EUCLEAN;
956f6d2a5c2SQu Wenruo 	}
957f6d2a5c2SQu Wenruo out:
958f6d2a5c2SQu Wenruo 	return btrfs_check_chunk_valid(leaf, chunk, key->offset);
959f6d2a5c2SQu Wenruo }
960f6d2a5c2SQu Wenruo 
9615617ed80SDavid Sterba __printf(3, 4)
962ab4ba2e1SQu Wenruo __cold
9635617ed80SDavid Sterba static void dev_item_err(const struct extent_buffer *eb, int slot,
964ab4ba2e1SQu Wenruo 			 const char *fmt, ...)
965ab4ba2e1SQu Wenruo {
966ab4ba2e1SQu Wenruo 	struct btrfs_key key;
967ab4ba2e1SQu Wenruo 	struct va_format vaf;
968ab4ba2e1SQu Wenruo 	va_list args;
969ab4ba2e1SQu Wenruo 
970ab4ba2e1SQu Wenruo 	btrfs_item_key_to_cpu(eb, &key, slot);
971ab4ba2e1SQu Wenruo 	va_start(args, fmt);
972ab4ba2e1SQu Wenruo 
973ab4ba2e1SQu Wenruo 	vaf.fmt = fmt;
974ab4ba2e1SQu Wenruo 	vaf.va = &args;
975ab4ba2e1SQu Wenruo 
9765617ed80SDavid Sterba 	btrfs_crit(eb->fs_info,
977ab4ba2e1SQu Wenruo 	"corrupt %s: root=%llu block=%llu slot=%d devid=%llu %pV",
978ab4ba2e1SQu Wenruo 		btrfs_header_level(eb) == 0 ? "leaf" : "node",
979ab4ba2e1SQu Wenruo 		btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot,
980ab4ba2e1SQu Wenruo 		key.objectid, &vaf);
981ab4ba2e1SQu Wenruo 	va_end(args);
982ab4ba2e1SQu Wenruo }
983ab4ba2e1SQu Wenruo 
984412a2312SDavid Sterba static int check_dev_item(struct extent_buffer *leaf,
985ab4ba2e1SQu Wenruo 			  struct btrfs_key *key, int slot)
986ab4ba2e1SQu Wenruo {
987ab4ba2e1SQu Wenruo 	struct btrfs_dev_item *ditem;
988ea1d1ca4SSu Yue 	const u32 item_size = btrfs_item_size(leaf, slot);
989ab4ba2e1SQu Wenruo 
990c7c01a4aSDavid Sterba 	if (unlikely(key->objectid != BTRFS_DEV_ITEMS_OBJECTID)) {
9915617ed80SDavid Sterba 		dev_item_err(leaf, slot,
992ab4ba2e1SQu Wenruo 			     "invalid objectid: has=%llu expect=%llu",
993ab4ba2e1SQu Wenruo 			     key->objectid, BTRFS_DEV_ITEMS_OBJECTID);
994ab4ba2e1SQu Wenruo 		return -EUCLEAN;
995ab4ba2e1SQu Wenruo 	}
996ea1d1ca4SSu Yue 
997ea1d1ca4SSu Yue 	if (unlikely(item_size != sizeof(*ditem))) {
998ea1d1ca4SSu Yue 		dev_item_err(leaf, slot, "invalid item size: has %u expect %zu",
999ea1d1ca4SSu Yue 			     item_size, sizeof(*ditem));
1000ea1d1ca4SSu Yue 		return -EUCLEAN;
1001ea1d1ca4SSu Yue 	}
1002ea1d1ca4SSu Yue 
1003ab4ba2e1SQu Wenruo 	ditem = btrfs_item_ptr(leaf, slot, struct btrfs_dev_item);
1004c7c01a4aSDavid Sterba 	if (unlikely(btrfs_device_id(leaf, ditem) != key->offset)) {
10055617ed80SDavid Sterba 		dev_item_err(leaf, slot,
1006ab4ba2e1SQu Wenruo 			     "devid mismatch: key has=%llu item has=%llu",
1007ab4ba2e1SQu Wenruo 			     key->offset, btrfs_device_id(leaf, ditem));
1008ab4ba2e1SQu Wenruo 		return -EUCLEAN;
1009ab4ba2e1SQu Wenruo 	}
1010ab4ba2e1SQu Wenruo 
1011ab4ba2e1SQu Wenruo 	/*
1012ab4ba2e1SQu Wenruo 	 * For device total_bytes, we don't have reliable way to check it, as
1013ab4ba2e1SQu Wenruo 	 * it can be 0 for device removal. Device size check can only be done
1014ab4ba2e1SQu Wenruo 	 * by dev extents check.
1015ab4ba2e1SQu Wenruo 	 */
1016c7c01a4aSDavid Sterba 	if (unlikely(btrfs_device_bytes_used(leaf, ditem) >
1017c7c01a4aSDavid Sterba 		     btrfs_device_total_bytes(leaf, ditem))) {
10185617ed80SDavid Sterba 		dev_item_err(leaf, slot,
1019ab4ba2e1SQu Wenruo 			     "invalid bytes used: have %llu expect [0, %llu]",
1020ab4ba2e1SQu Wenruo 			     btrfs_device_bytes_used(leaf, ditem),
1021ab4ba2e1SQu Wenruo 			     btrfs_device_total_bytes(leaf, ditem));
1022ab4ba2e1SQu Wenruo 		return -EUCLEAN;
1023ab4ba2e1SQu Wenruo 	}
1024ab4ba2e1SQu Wenruo 	/*
1025ab4ba2e1SQu Wenruo 	 * Remaining members like io_align/type/gen/dev_group aren't really
1026ab4ba2e1SQu Wenruo 	 * utilized.  Skip them to make later usage of them easier.
1027ab4ba2e1SQu Wenruo 	 */
1028ab4ba2e1SQu Wenruo 	return 0;
1029ab4ba2e1SQu Wenruo }
1030ab4ba2e1SQu Wenruo 
103139e57f49SDavid Sterba static int check_inode_item(struct extent_buffer *leaf,
1032496245caSQu Wenruo 			    struct btrfs_key *key, int slot)
1033496245caSQu Wenruo {
103439e57f49SDavid Sterba 	struct btrfs_fs_info *fs_info = leaf->fs_info;
1035496245caSQu Wenruo 	struct btrfs_inode_item *iitem;
1036496245caSQu Wenruo 	u64 super_gen = btrfs_super_generation(fs_info->super_copy);
1037496245caSQu Wenruo 	u32 valid_mask = (S_IFMT | S_ISUID | S_ISGID | S_ISVTX | 0777);
10380c982944SSu Yue 	const u32 item_size = btrfs_item_size(leaf, slot);
1039496245caSQu Wenruo 	u32 mode;
1040c23c77b0SQu Wenruo 	int ret;
104177eea05eSBoris Burkov 	u32 flags;
104277eea05eSBoris Burkov 	u32 ro_flags;
1043496245caSQu Wenruo 
1044c23c77b0SQu Wenruo 	ret = check_inode_key(leaf, key, slot);
1045c7c01a4aSDavid Sterba 	if (unlikely(ret < 0))
1046c23c77b0SQu Wenruo 		return ret;
1047c23c77b0SQu Wenruo 
10480c982944SSu Yue 	if (unlikely(item_size != sizeof(*iitem))) {
10490c982944SSu Yue 		generic_err(leaf, slot, "invalid item size: has %u expect %zu",
10500c982944SSu Yue 			    item_size, sizeof(*iitem));
10510c982944SSu Yue 		return -EUCLEAN;
10520c982944SSu Yue 	}
10530c982944SSu Yue 
1054496245caSQu Wenruo 	iitem = btrfs_item_ptr(leaf, slot, struct btrfs_inode_item);
1055496245caSQu Wenruo 
1056496245caSQu Wenruo 	/* Here we use super block generation + 1 to handle log tree */
1057c7c01a4aSDavid Sterba 	if (unlikely(btrfs_inode_generation(leaf, iitem) > super_gen + 1)) {
1058c3053ebbSQu Wenruo 		inode_item_err(leaf, slot,
1059496245caSQu Wenruo 			"invalid inode generation: has %llu expect (0, %llu]",
1060496245caSQu Wenruo 			       btrfs_inode_generation(leaf, iitem),
1061496245caSQu Wenruo 			       super_gen + 1);
1062496245caSQu Wenruo 		return -EUCLEAN;
1063496245caSQu Wenruo 	}
1064496245caSQu Wenruo 	/* Note for ROOT_TREE_DIR_ITEM, mkfs could set its transid 0 */
1065c7c01a4aSDavid Sterba 	if (unlikely(btrfs_inode_transid(leaf, iitem) > super_gen + 1)) {
1066c3053ebbSQu Wenruo 		inode_item_err(leaf, slot,
1067f96d6960SQu Wenruo 			"invalid inode transid: has %llu expect [0, %llu]",
1068496245caSQu Wenruo 			       btrfs_inode_transid(leaf, iitem), super_gen + 1);
1069496245caSQu Wenruo 		return -EUCLEAN;
1070496245caSQu Wenruo 	}
1071496245caSQu Wenruo 
1072496245caSQu Wenruo 	/*
1073496245caSQu Wenruo 	 * For size and nbytes it's better not to be too strict, as for dir
1074496245caSQu Wenruo 	 * item its size/nbytes can easily get wrong, but doesn't affect
1075496245caSQu Wenruo 	 * anything in the fs. So here we skip the check.
1076496245caSQu Wenruo 	 */
1077496245caSQu Wenruo 	mode = btrfs_inode_mode(leaf, iitem);
1078c7c01a4aSDavid Sterba 	if (unlikely(mode & ~valid_mask)) {
1079c3053ebbSQu Wenruo 		inode_item_err(leaf, slot,
1080496245caSQu Wenruo 			       "unknown mode bit detected: 0x%x",
1081496245caSQu Wenruo 			       mode & ~valid_mask);
1082496245caSQu Wenruo 		return -EUCLEAN;
1083496245caSQu Wenruo 	}
1084496245caSQu Wenruo 
1085496245caSQu Wenruo 	/*
1086c1499166SDavid Sterba 	 * S_IFMT is not bit mapped so we can't completely rely on
1087c1499166SDavid Sterba 	 * is_power_of_2/has_single_bit_set, but it can save us from checking
1088c1499166SDavid Sterba 	 * FIFO/CHR/DIR/REG.  Only needs to check BLK, LNK and SOCKS
1089496245caSQu Wenruo 	 */
1090c1499166SDavid Sterba 	if (!has_single_bit_set(mode & S_IFMT)) {
1091c7c01a4aSDavid Sterba 		if (unlikely(!S_ISLNK(mode) && !S_ISBLK(mode) && !S_ISSOCK(mode))) {
1092c3053ebbSQu Wenruo 			inode_item_err(leaf, slot,
1093496245caSQu Wenruo 			"invalid mode: has 0%o expect valid S_IF* bit(s)",
1094496245caSQu Wenruo 				       mode & S_IFMT);
1095496245caSQu Wenruo 			return -EUCLEAN;
1096496245caSQu Wenruo 		}
1097496245caSQu Wenruo 	}
1098c7c01a4aSDavid Sterba 	if (unlikely(S_ISDIR(mode) && btrfs_inode_nlink(leaf, iitem) > 1)) {
1099c3053ebbSQu Wenruo 		inode_item_err(leaf, slot,
1100496245caSQu Wenruo 		       "invalid nlink: has %u expect no more than 1 for dir",
1101496245caSQu Wenruo 			btrfs_inode_nlink(leaf, iitem));
1102496245caSQu Wenruo 		return -EUCLEAN;
1103496245caSQu Wenruo 	}
110477eea05eSBoris Burkov 	btrfs_inode_split_flags(btrfs_inode_flags(leaf, iitem), &flags, &ro_flags);
110577eea05eSBoris Burkov 	if (unlikely(flags & ~BTRFS_INODE_FLAG_MASK)) {
1106c3053ebbSQu Wenruo 		inode_item_err(leaf, slot,
110777eea05eSBoris Burkov 			       "unknown incompat flags detected: 0x%x", flags);
110877eea05eSBoris Burkov 		return -EUCLEAN;
110977eea05eSBoris Burkov 	}
111077eea05eSBoris Burkov 	if (unlikely(!sb_rdonly(fs_info->sb) &&
111177eea05eSBoris Burkov 		     (ro_flags & ~BTRFS_INODE_RO_FLAG_MASK))) {
111277eea05eSBoris Burkov 		inode_item_err(leaf, slot,
111377eea05eSBoris Burkov 			"unknown ro-compat flags detected on writeable mount: 0x%x",
111477eea05eSBoris Burkov 			ro_flags);
1115496245caSQu Wenruo 		return -EUCLEAN;
1116496245caSQu Wenruo 	}
1117496245caSQu Wenruo 	return 0;
1118496245caSQu Wenruo }
1119496245caSQu Wenruo 
1120259ee775SQu Wenruo static int check_root_item(struct extent_buffer *leaf, struct btrfs_key *key,
1121259ee775SQu Wenruo 			   int slot)
1122259ee775SQu Wenruo {
1123259ee775SQu Wenruo 	struct btrfs_fs_info *fs_info = leaf->fs_info;
11241465af12SQu Wenruo 	struct btrfs_root_item ri = { 0 };
1125259ee775SQu Wenruo 	const u64 valid_root_flags = BTRFS_ROOT_SUBVOL_RDONLY |
1126259ee775SQu Wenruo 				     BTRFS_ROOT_SUBVOL_DEAD;
112757a0e674SQu Wenruo 	int ret;
1128259ee775SQu Wenruo 
112957a0e674SQu Wenruo 	ret = check_root_key(leaf, key, slot);
1130c7c01a4aSDavid Sterba 	if (unlikely(ret < 0))
113157a0e674SQu Wenruo 		return ret;
1132259ee775SQu Wenruo 
11333212fa14SJosef Bacik 	if (unlikely(btrfs_item_size(leaf, slot) != sizeof(ri) &&
11343212fa14SJosef Bacik 		     btrfs_item_size(leaf, slot) !=
1135c7c01a4aSDavid Sterba 		     btrfs_legacy_root_item_size())) {
1136259ee775SQu Wenruo 		generic_err(leaf, slot,
11371465af12SQu Wenruo 			    "invalid root item size, have %u expect %zu or %u",
11383212fa14SJosef Bacik 			    btrfs_item_size(leaf, slot), sizeof(ri),
11391465af12SQu Wenruo 			    btrfs_legacy_root_item_size());
11401a49a97dSDaniel Xu 		return -EUCLEAN;
1141259ee775SQu Wenruo 	}
1142259ee775SQu Wenruo 
11431465af12SQu Wenruo 	/*
11441465af12SQu Wenruo 	 * For legacy root item, the members starting at generation_v2 will be
11451465af12SQu Wenruo 	 * all filled with 0.
11461465af12SQu Wenruo 	 * And since we allow geneartion_v2 as 0, it will still pass the check.
11471465af12SQu Wenruo 	 */
1148259ee775SQu Wenruo 	read_extent_buffer(leaf, &ri, btrfs_item_ptr_offset(leaf, slot),
11493212fa14SJosef Bacik 			   btrfs_item_size(leaf, slot));
1150259ee775SQu Wenruo 
1151259ee775SQu Wenruo 	/* Generation related */
1152c7c01a4aSDavid Sterba 	if (unlikely(btrfs_root_generation(&ri) >
1153c7c01a4aSDavid Sterba 		     btrfs_super_generation(fs_info->super_copy) + 1)) {
1154259ee775SQu Wenruo 		generic_err(leaf, slot,
1155259ee775SQu Wenruo 			"invalid root generation, have %llu expect (0, %llu]",
1156259ee775SQu Wenruo 			    btrfs_root_generation(&ri),
1157259ee775SQu Wenruo 			    btrfs_super_generation(fs_info->super_copy) + 1);
1158259ee775SQu Wenruo 		return -EUCLEAN;
1159259ee775SQu Wenruo 	}
1160c7c01a4aSDavid Sterba 	if (unlikely(btrfs_root_generation_v2(&ri) >
1161c7c01a4aSDavid Sterba 		     btrfs_super_generation(fs_info->super_copy) + 1)) {
1162259ee775SQu Wenruo 		generic_err(leaf, slot,
1163259ee775SQu Wenruo 		"invalid root v2 generation, have %llu expect (0, %llu]",
1164259ee775SQu Wenruo 			    btrfs_root_generation_v2(&ri),
1165259ee775SQu Wenruo 			    btrfs_super_generation(fs_info->super_copy) + 1);
1166259ee775SQu Wenruo 		return -EUCLEAN;
1167259ee775SQu Wenruo 	}
1168c7c01a4aSDavid Sterba 	if (unlikely(btrfs_root_last_snapshot(&ri) >
1169c7c01a4aSDavid Sterba 		     btrfs_super_generation(fs_info->super_copy) + 1)) {
1170259ee775SQu Wenruo 		generic_err(leaf, slot,
1171259ee775SQu Wenruo 		"invalid root last_snapshot, have %llu expect (0, %llu]",
1172259ee775SQu Wenruo 			    btrfs_root_last_snapshot(&ri),
1173259ee775SQu Wenruo 			    btrfs_super_generation(fs_info->super_copy) + 1);
1174259ee775SQu Wenruo 		return -EUCLEAN;
1175259ee775SQu Wenruo 	}
1176259ee775SQu Wenruo 
1177259ee775SQu Wenruo 	/* Alignment and level check */
1178c7c01a4aSDavid Sterba 	if (unlikely(!IS_ALIGNED(btrfs_root_bytenr(&ri), fs_info->sectorsize))) {
1179259ee775SQu Wenruo 		generic_err(leaf, slot,
1180259ee775SQu Wenruo 		"invalid root bytenr, have %llu expect to be aligned to %u",
1181259ee775SQu Wenruo 			    btrfs_root_bytenr(&ri), fs_info->sectorsize);
1182259ee775SQu Wenruo 		return -EUCLEAN;
1183259ee775SQu Wenruo 	}
1184c7c01a4aSDavid Sterba 	if (unlikely(btrfs_root_level(&ri) >= BTRFS_MAX_LEVEL)) {
1185259ee775SQu Wenruo 		generic_err(leaf, slot,
1186259ee775SQu Wenruo 			    "invalid root level, have %u expect [0, %u]",
1187259ee775SQu Wenruo 			    btrfs_root_level(&ri), BTRFS_MAX_LEVEL - 1);
1188259ee775SQu Wenruo 		return -EUCLEAN;
1189259ee775SQu Wenruo 	}
1190c7c01a4aSDavid Sterba 	if (unlikely(btrfs_root_drop_level(&ri) >= BTRFS_MAX_LEVEL)) {
1191259ee775SQu Wenruo 		generic_err(leaf, slot,
1192259ee775SQu Wenruo 			    "invalid root level, have %u expect [0, %u]",
1193c8422684SDavid Sterba 			    btrfs_root_drop_level(&ri), BTRFS_MAX_LEVEL - 1);
1194259ee775SQu Wenruo 		return -EUCLEAN;
1195259ee775SQu Wenruo 	}
1196259ee775SQu Wenruo 
1197259ee775SQu Wenruo 	/* Flags check */
1198c7c01a4aSDavid Sterba 	if (unlikely(btrfs_root_flags(&ri) & ~valid_root_flags)) {
1199259ee775SQu Wenruo 		generic_err(leaf, slot,
1200259ee775SQu Wenruo 			    "invalid root flags, have 0x%llx expect mask 0x%llx",
1201259ee775SQu Wenruo 			    btrfs_root_flags(&ri), valid_root_flags);
1202259ee775SQu Wenruo 		return -EUCLEAN;
1203259ee775SQu Wenruo 	}
1204259ee775SQu Wenruo 	return 0;
1205259ee775SQu Wenruo }
1206259ee775SQu Wenruo 
1207f82d1c7cSQu Wenruo __printf(3,4)
1208f82d1c7cSQu Wenruo __cold
1209f82d1c7cSQu Wenruo static void extent_err(const struct extent_buffer *eb, int slot,
1210f82d1c7cSQu Wenruo 		       const char *fmt, ...)
1211f82d1c7cSQu Wenruo {
1212f82d1c7cSQu Wenruo 	struct btrfs_key key;
1213f82d1c7cSQu Wenruo 	struct va_format vaf;
1214f82d1c7cSQu Wenruo 	va_list args;
1215f82d1c7cSQu Wenruo 	u64 bytenr;
1216f82d1c7cSQu Wenruo 	u64 len;
1217f82d1c7cSQu Wenruo 
1218f82d1c7cSQu Wenruo 	btrfs_item_key_to_cpu(eb, &key, slot);
1219f82d1c7cSQu Wenruo 	bytenr = key.objectid;
1220e2406a6fSQu Wenruo 	if (key.type == BTRFS_METADATA_ITEM_KEY ||
1221e2406a6fSQu Wenruo 	    key.type == BTRFS_TREE_BLOCK_REF_KEY ||
1222e2406a6fSQu Wenruo 	    key.type == BTRFS_SHARED_BLOCK_REF_KEY)
1223f82d1c7cSQu Wenruo 		len = eb->fs_info->nodesize;
1224f82d1c7cSQu Wenruo 	else
1225f82d1c7cSQu Wenruo 		len = key.offset;
1226f82d1c7cSQu Wenruo 	va_start(args, fmt);
1227f82d1c7cSQu Wenruo 
1228f82d1c7cSQu Wenruo 	vaf.fmt = fmt;
1229f82d1c7cSQu Wenruo 	vaf.va = &args;
1230f82d1c7cSQu Wenruo 
1231f82d1c7cSQu Wenruo 	btrfs_crit(eb->fs_info,
1232f82d1c7cSQu Wenruo 	"corrupt %s: block=%llu slot=%d extent bytenr=%llu len=%llu %pV",
1233f82d1c7cSQu Wenruo 		btrfs_header_level(eb) == 0 ? "leaf" : "node",
1234f82d1c7cSQu Wenruo 		eb->start, slot, bytenr, len, &vaf);
1235f82d1c7cSQu Wenruo 	va_end(args);
1236f82d1c7cSQu Wenruo }
1237f82d1c7cSQu Wenruo 
1238f82d1c7cSQu Wenruo static int check_extent_item(struct extent_buffer *leaf,
1239899b7f69SJosef Bacik 			     struct btrfs_key *key, int slot,
1240899b7f69SJosef Bacik 			     struct btrfs_key *prev_key)
1241f82d1c7cSQu Wenruo {
1242f82d1c7cSQu Wenruo 	struct btrfs_fs_info *fs_info = leaf->fs_info;
1243f82d1c7cSQu Wenruo 	struct btrfs_extent_item *ei;
1244f82d1c7cSQu Wenruo 	bool is_tree_block = false;
1245f82d1c7cSQu Wenruo 	unsigned long ptr;	/* Current pointer inside inline refs */
1246f82d1c7cSQu Wenruo 	unsigned long end;	/* Extent item end */
12473212fa14SJosef Bacik 	const u32 item_size = btrfs_item_size(leaf, slot);
1248f82d1c7cSQu Wenruo 	u64 flags;
1249f82d1c7cSQu Wenruo 	u64 generation;
1250f82d1c7cSQu Wenruo 	u64 total_refs;		/* Total refs in btrfs_extent_item */
1251f82d1c7cSQu Wenruo 	u64 inline_refs = 0;	/* found total inline refs */
1252f82d1c7cSQu Wenruo 
1253c7c01a4aSDavid Sterba 	if (unlikely(key->type == BTRFS_METADATA_ITEM_KEY &&
1254c7c01a4aSDavid Sterba 		     !btrfs_fs_incompat(fs_info, SKINNY_METADATA))) {
1255f82d1c7cSQu Wenruo 		generic_err(leaf, slot,
1256f82d1c7cSQu Wenruo "invalid key type, METADATA_ITEM type invalid when SKINNY_METADATA feature disabled");
1257f82d1c7cSQu Wenruo 		return -EUCLEAN;
1258f82d1c7cSQu Wenruo 	}
1259f82d1c7cSQu Wenruo 	/* key->objectid is the bytenr for both key types */
1260c7c01a4aSDavid Sterba 	if (unlikely(!IS_ALIGNED(key->objectid, fs_info->sectorsize))) {
1261f82d1c7cSQu Wenruo 		generic_err(leaf, slot,
1262f82d1c7cSQu Wenruo 		"invalid key objectid, have %llu expect to be aligned to %u",
1263f82d1c7cSQu Wenruo 			   key->objectid, fs_info->sectorsize);
1264f82d1c7cSQu Wenruo 		return -EUCLEAN;
1265f82d1c7cSQu Wenruo 	}
1266f82d1c7cSQu Wenruo 
1267f82d1c7cSQu Wenruo 	/* key->offset is tree level for METADATA_ITEM_KEY */
1268c7c01a4aSDavid Sterba 	if (unlikely(key->type == BTRFS_METADATA_ITEM_KEY &&
1269c7c01a4aSDavid Sterba 		     key->offset >= BTRFS_MAX_LEVEL)) {
1270f82d1c7cSQu Wenruo 		extent_err(leaf, slot,
1271f82d1c7cSQu Wenruo 			   "invalid tree level, have %llu expect [0, %u]",
1272f82d1c7cSQu Wenruo 			   key->offset, BTRFS_MAX_LEVEL - 1);
1273f82d1c7cSQu Wenruo 		return -EUCLEAN;
1274f82d1c7cSQu Wenruo 	}
1275f82d1c7cSQu Wenruo 
1276f82d1c7cSQu Wenruo 	/*
1277f82d1c7cSQu Wenruo 	 * EXTENT/METADATA_ITEM consists of:
1278f82d1c7cSQu Wenruo 	 * 1) One btrfs_extent_item
1279f82d1c7cSQu Wenruo 	 *    Records the total refs, type and generation of the extent.
1280f82d1c7cSQu Wenruo 	 *
1281f82d1c7cSQu Wenruo 	 * 2) One btrfs_tree_block_info (for EXTENT_ITEM and tree backref only)
1282f82d1c7cSQu Wenruo 	 *    Records the first key and level of the tree block.
1283f82d1c7cSQu Wenruo 	 *
1284f82d1c7cSQu Wenruo 	 * 2) Zero or more btrfs_extent_inline_ref(s)
1285f82d1c7cSQu Wenruo 	 *    Each inline ref has one btrfs_extent_inline_ref shows:
1286f82d1c7cSQu Wenruo 	 *    2.1) The ref type, one of the 4
1287f82d1c7cSQu Wenruo 	 *         TREE_BLOCK_REF	Tree block only
1288f82d1c7cSQu Wenruo 	 *         SHARED_BLOCK_REF	Tree block only
1289f82d1c7cSQu Wenruo 	 *         EXTENT_DATA_REF	Data only
1290f82d1c7cSQu Wenruo 	 *         SHARED_DATA_REF	Data only
1291f82d1c7cSQu Wenruo 	 *    2.2) Ref type specific data
1292f82d1c7cSQu Wenruo 	 *         Either using btrfs_extent_inline_ref::offset, or specific
1293f82d1c7cSQu Wenruo 	 *         data structure.
1294f82d1c7cSQu Wenruo 	 */
1295c7c01a4aSDavid Sterba 	if (unlikely(item_size < sizeof(*ei))) {
1296f82d1c7cSQu Wenruo 		extent_err(leaf, slot,
1297f82d1c7cSQu Wenruo 			   "invalid item size, have %u expect [%zu, %u)",
1298f82d1c7cSQu Wenruo 			   item_size, sizeof(*ei),
1299f82d1c7cSQu Wenruo 			   BTRFS_LEAF_DATA_SIZE(fs_info));
1300f82d1c7cSQu Wenruo 		return -EUCLEAN;
1301f82d1c7cSQu Wenruo 	}
1302f82d1c7cSQu Wenruo 	end = item_size + btrfs_item_ptr_offset(leaf, slot);
1303f82d1c7cSQu Wenruo 
1304f82d1c7cSQu Wenruo 	/* Checks against extent_item */
1305f82d1c7cSQu Wenruo 	ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
1306f82d1c7cSQu Wenruo 	flags = btrfs_extent_flags(leaf, ei);
1307f82d1c7cSQu Wenruo 	total_refs = btrfs_extent_refs(leaf, ei);
1308f82d1c7cSQu Wenruo 	generation = btrfs_extent_generation(leaf, ei);
1309c7c01a4aSDavid Sterba 	if (unlikely(generation >
1310c7c01a4aSDavid Sterba 		     btrfs_super_generation(fs_info->super_copy) + 1)) {
1311f82d1c7cSQu Wenruo 		extent_err(leaf, slot,
1312f82d1c7cSQu Wenruo 			   "invalid generation, have %llu expect (0, %llu]",
1313f82d1c7cSQu Wenruo 			   generation,
1314f82d1c7cSQu Wenruo 			   btrfs_super_generation(fs_info->super_copy) + 1);
1315f82d1c7cSQu Wenruo 		return -EUCLEAN;
1316f82d1c7cSQu Wenruo 	}
1317c7c01a4aSDavid Sterba 	if (unlikely(!has_single_bit_set(flags & (BTRFS_EXTENT_FLAG_DATA |
1318c7c01a4aSDavid Sterba 						  BTRFS_EXTENT_FLAG_TREE_BLOCK)))) {
1319f82d1c7cSQu Wenruo 		extent_err(leaf, slot,
1320f82d1c7cSQu Wenruo 		"invalid extent flag, have 0x%llx expect 1 bit set in 0x%llx",
1321f82d1c7cSQu Wenruo 			flags, BTRFS_EXTENT_FLAG_DATA |
1322f82d1c7cSQu Wenruo 			BTRFS_EXTENT_FLAG_TREE_BLOCK);
1323f82d1c7cSQu Wenruo 		return -EUCLEAN;
1324f82d1c7cSQu Wenruo 	}
1325f82d1c7cSQu Wenruo 	is_tree_block = !!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK);
1326f82d1c7cSQu Wenruo 	if (is_tree_block) {
1327c7c01a4aSDavid Sterba 		if (unlikely(key->type == BTRFS_EXTENT_ITEM_KEY &&
1328c7c01a4aSDavid Sterba 			     key->offset != fs_info->nodesize)) {
1329f82d1c7cSQu Wenruo 			extent_err(leaf, slot,
1330f82d1c7cSQu Wenruo 				   "invalid extent length, have %llu expect %u",
1331f82d1c7cSQu Wenruo 				   key->offset, fs_info->nodesize);
1332f82d1c7cSQu Wenruo 			return -EUCLEAN;
1333f82d1c7cSQu Wenruo 		}
1334f82d1c7cSQu Wenruo 	} else {
1335c7c01a4aSDavid Sterba 		if (unlikely(key->type != BTRFS_EXTENT_ITEM_KEY)) {
1336f82d1c7cSQu Wenruo 			extent_err(leaf, slot,
1337f82d1c7cSQu Wenruo 			"invalid key type, have %u expect %u for data backref",
1338f82d1c7cSQu Wenruo 				   key->type, BTRFS_EXTENT_ITEM_KEY);
1339f82d1c7cSQu Wenruo 			return -EUCLEAN;
1340f82d1c7cSQu Wenruo 		}
1341c7c01a4aSDavid Sterba 		if (unlikely(!IS_ALIGNED(key->offset, fs_info->sectorsize))) {
1342f82d1c7cSQu Wenruo 			extent_err(leaf, slot,
1343f82d1c7cSQu Wenruo 			"invalid extent length, have %llu expect aligned to %u",
1344f82d1c7cSQu Wenruo 				   key->offset, fs_info->sectorsize);
1345f82d1c7cSQu Wenruo 			return -EUCLEAN;
1346f82d1c7cSQu Wenruo 		}
13470ebb6bbbSJosef Bacik 		if (unlikely(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
13480ebb6bbbSJosef Bacik 			extent_err(leaf, slot,
13490ebb6bbbSJosef Bacik 			"invalid extent flag, data has full backref set");
13500ebb6bbbSJosef Bacik 			return -EUCLEAN;
13510ebb6bbbSJosef Bacik 		}
1352f82d1c7cSQu Wenruo 	}
1353f82d1c7cSQu Wenruo 	ptr = (unsigned long)(struct btrfs_extent_item *)(ei + 1);
1354f82d1c7cSQu Wenruo 
1355f82d1c7cSQu Wenruo 	/* Check the special case of btrfs_tree_block_info */
1356f82d1c7cSQu Wenruo 	if (is_tree_block && key->type != BTRFS_METADATA_ITEM_KEY) {
1357f82d1c7cSQu Wenruo 		struct btrfs_tree_block_info *info;
1358f82d1c7cSQu Wenruo 
1359f82d1c7cSQu Wenruo 		info = (struct btrfs_tree_block_info *)ptr;
1360c7c01a4aSDavid Sterba 		if (unlikely(btrfs_tree_block_level(leaf, info) >= BTRFS_MAX_LEVEL)) {
1361f82d1c7cSQu Wenruo 			extent_err(leaf, slot,
1362f82d1c7cSQu Wenruo 			"invalid tree block info level, have %u expect [0, %u]",
1363f82d1c7cSQu Wenruo 				   btrfs_tree_block_level(leaf, info),
1364f82d1c7cSQu Wenruo 				   BTRFS_MAX_LEVEL - 1);
1365f82d1c7cSQu Wenruo 			return -EUCLEAN;
1366f82d1c7cSQu Wenruo 		}
1367f82d1c7cSQu Wenruo 		ptr = (unsigned long)(struct btrfs_tree_block_info *)(info + 1);
1368f82d1c7cSQu Wenruo 	}
1369f82d1c7cSQu Wenruo 
1370f82d1c7cSQu Wenruo 	/* Check inline refs */
1371f82d1c7cSQu Wenruo 	while (ptr < end) {
1372f82d1c7cSQu Wenruo 		struct btrfs_extent_inline_ref *iref;
1373f82d1c7cSQu Wenruo 		struct btrfs_extent_data_ref *dref;
1374f82d1c7cSQu Wenruo 		struct btrfs_shared_data_ref *sref;
1375f82d1c7cSQu Wenruo 		u64 dref_offset;
1376f82d1c7cSQu Wenruo 		u64 inline_offset;
1377f82d1c7cSQu Wenruo 		u8 inline_type;
1378f82d1c7cSQu Wenruo 
1379c7c01a4aSDavid Sterba 		if (unlikely(ptr + sizeof(*iref) > end)) {
1380f82d1c7cSQu Wenruo 			extent_err(leaf, slot,
1381f82d1c7cSQu Wenruo "inline ref item overflows extent item, ptr %lu iref size %zu end %lu",
1382f82d1c7cSQu Wenruo 				   ptr, sizeof(*iref), end);
1383f82d1c7cSQu Wenruo 			return -EUCLEAN;
1384f82d1c7cSQu Wenruo 		}
1385f82d1c7cSQu Wenruo 		iref = (struct btrfs_extent_inline_ref *)ptr;
1386f82d1c7cSQu Wenruo 		inline_type = btrfs_extent_inline_ref_type(leaf, iref);
1387f82d1c7cSQu Wenruo 		inline_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1388c7c01a4aSDavid Sterba 		if (unlikely(ptr + btrfs_extent_inline_ref_size(inline_type) > end)) {
1389f82d1c7cSQu Wenruo 			extent_err(leaf, slot,
1390f82d1c7cSQu Wenruo "inline ref item overflows extent item, ptr %lu iref size %u end %lu",
1391f82d1c7cSQu Wenruo 				   ptr, inline_type, end);
1392f82d1c7cSQu Wenruo 			return -EUCLEAN;
1393f82d1c7cSQu Wenruo 		}
1394f82d1c7cSQu Wenruo 
1395f82d1c7cSQu Wenruo 		switch (inline_type) {
1396f82d1c7cSQu Wenruo 		/* inline_offset is subvolid of the owner, no need to check */
1397f82d1c7cSQu Wenruo 		case BTRFS_TREE_BLOCK_REF_KEY:
1398f82d1c7cSQu Wenruo 			inline_refs++;
1399f82d1c7cSQu Wenruo 			break;
1400f82d1c7cSQu Wenruo 		/* Contains parent bytenr */
1401f82d1c7cSQu Wenruo 		case BTRFS_SHARED_BLOCK_REF_KEY:
1402c7c01a4aSDavid Sterba 			if (unlikely(!IS_ALIGNED(inline_offset,
1403c7c01a4aSDavid Sterba 						 fs_info->sectorsize))) {
1404f82d1c7cSQu Wenruo 				extent_err(leaf, slot,
1405f82d1c7cSQu Wenruo 		"invalid tree parent bytenr, have %llu expect aligned to %u",
1406f82d1c7cSQu Wenruo 					   inline_offset, fs_info->sectorsize);
1407f82d1c7cSQu Wenruo 				return -EUCLEAN;
1408f82d1c7cSQu Wenruo 			}
1409f82d1c7cSQu Wenruo 			inline_refs++;
1410f82d1c7cSQu Wenruo 			break;
1411f82d1c7cSQu Wenruo 		/*
1412f82d1c7cSQu Wenruo 		 * Contains owner subvolid, owner key objectid, adjusted offset.
1413f82d1c7cSQu Wenruo 		 * The only obvious corruption can happen in that offset.
1414f82d1c7cSQu Wenruo 		 */
1415f82d1c7cSQu Wenruo 		case BTRFS_EXTENT_DATA_REF_KEY:
1416f82d1c7cSQu Wenruo 			dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1417f82d1c7cSQu Wenruo 			dref_offset = btrfs_extent_data_ref_offset(leaf, dref);
1418c7c01a4aSDavid Sterba 			if (unlikely(!IS_ALIGNED(dref_offset,
1419c7c01a4aSDavid Sterba 						 fs_info->sectorsize))) {
1420f82d1c7cSQu Wenruo 				extent_err(leaf, slot,
1421f82d1c7cSQu Wenruo 		"invalid data ref offset, have %llu expect aligned to %u",
1422f82d1c7cSQu Wenruo 					   dref_offset, fs_info->sectorsize);
1423f82d1c7cSQu Wenruo 				return -EUCLEAN;
1424f82d1c7cSQu Wenruo 			}
1425f82d1c7cSQu Wenruo 			inline_refs += btrfs_extent_data_ref_count(leaf, dref);
1426f82d1c7cSQu Wenruo 			break;
1427f82d1c7cSQu Wenruo 		/* Contains parent bytenr and ref count */
1428f82d1c7cSQu Wenruo 		case BTRFS_SHARED_DATA_REF_KEY:
1429f82d1c7cSQu Wenruo 			sref = (struct btrfs_shared_data_ref *)(iref + 1);
1430c7c01a4aSDavid Sterba 			if (unlikely(!IS_ALIGNED(inline_offset,
1431c7c01a4aSDavid Sterba 						 fs_info->sectorsize))) {
1432f82d1c7cSQu Wenruo 				extent_err(leaf, slot,
1433f82d1c7cSQu Wenruo 		"invalid data parent bytenr, have %llu expect aligned to %u",
1434f82d1c7cSQu Wenruo 					   inline_offset, fs_info->sectorsize);
1435f82d1c7cSQu Wenruo 				return -EUCLEAN;
1436f82d1c7cSQu Wenruo 			}
1437f82d1c7cSQu Wenruo 			inline_refs += btrfs_shared_data_ref_count(leaf, sref);
1438f82d1c7cSQu Wenruo 			break;
1439f82d1c7cSQu Wenruo 		default:
1440f82d1c7cSQu Wenruo 			extent_err(leaf, slot, "unknown inline ref type: %u",
1441f82d1c7cSQu Wenruo 				   inline_type);
1442f82d1c7cSQu Wenruo 			return -EUCLEAN;
1443f82d1c7cSQu Wenruo 		}
1444f82d1c7cSQu Wenruo 		ptr += btrfs_extent_inline_ref_size(inline_type);
1445f82d1c7cSQu Wenruo 	}
1446f82d1c7cSQu Wenruo 	/* No padding is allowed */
1447c7c01a4aSDavid Sterba 	if (unlikely(ptr != end)) {
1448f82d1c7cSQu Wenruo 		extent_err(leaf, slot,
1449f82d1c7cSQu Wenruo 			   "invalid extent item size, padding bytes found");
1450f82d1c7cSQu Wenruo 		return -EUCLEAN;
1451f82d1c7cSQu Wenruo 	}
1452f82d1c7cSQu Wenruo 
1453f82d1c7cSQu Wenruo 	/* Finally, check the inline refs against total refs */
1454c7c01a4aSDavid Sterba 	if (unlikely(inline_refs > total_refs)) {
1455f82d1c7cSQu Wenruo 		extent_err(leaf, slot,
1456f82d1c7cSQu Wenruo 			"invalid extent refs, have %llu expect >= inline %llu",
1457f82d1c7cSQu Wenruo 			   total_refs, inline_refs);
1458f82d1c7cSQu Wenruo 		return -EUCLEAN;
1459f82d1c7cSQu Wenruo 	}
1460899b7f69SJosef Bacik 
1461899b7f69SJosef Bacik 	if ((prev_key->type == BTRFS_EXTENT_ITEM_KEY) ||
1462899b7f69SJosef Bacik 	    (prev_key->type == BTRFS_METADATA_ITEM_KEY)) {
1463899b7f69SJosef Bacik 		u64 prev_end = prev_key->objectid;
1464899b7f69SJosef Bacik 
1465899b7f69SJosef Bacik 		if (prev_key->type == BTRFS_METADATA_ITEM_KEY)
1466899b7f69SJosef Bacik 			prev_end += fs_info->nodesize;
1467899b7f69SJosef Bacik 		else
1468899b7f69SJosef Bacik 			prev_end += prev_key->offset;
1469899b7f69SJosef Bacik 
1470899b7f69SJosef Bacik 		if (unlikely(prev_end > key->objectid)) {
1471899b7f69SJosef Bacik 			extent_err(leaf, slot,
1472899b7f69SJosef Bacik 	"previous extent [%llu %u %llu] overlaps current extent [%llu %u %llu]",
1473899b7f69SJosef Bacik 				   prev_key->objectid, prev_key->type,
1474899b7f69SJosef Bacik 				   prev_key->offset, key->objectid, key->type,
1475899b7f69SJosef Bacik 				   key->offset);
1476899b7f69SJosef Bacik 			return -EUCLEAN;
1477899b7f69SJosef Bacik 		}
1478899b7f69SJosef Bacik 	}
1479899b7f69SJosef Bacik 
1480f82d1c7cSQu Wenruo 	return 0;
1481f82d1c7cSQu Wenruo }
1482f82d1c7cSQu Wenruo 
1483e2406a6fSQu Wenruo static int check_simple_keyed_refs(struct extent_buffer *leaf,
1484e2406a6fSQu Wenruo 				   struct btrfs_key *key, int slot)
1485e2406a6fSQu Wenruo {
1486e2406a6fSQu Wenruo 	u32 expect_item_size = 0;
1487e2406a6fSQu Wenruo 
1488e2406a6fSQu Wenruo 	if (key->type == BTRFS_SHARED_DATA_REF_KEY)
1489e2406a6fSQu Wenruo 		expect_item_size = sizeof(struct btrfs_shared_data_ref);
1490e2406a6fSQu Wenruo 
14913212fa14SJosef Bacik 	if (unlikely(btrfs_item_size(leaf, slot) != expect_item_size)) {
1492e2406a6fSQu Wenruo 		generic_err(leaf, slot,
1493e2406a6fSQu Wenruo 		"invalid item size, have %u expect %u for key type %u",
14943212fa14SJosef Bacik 			    btrfs_item_size(leaf, slot),
1495e2406a6fSQu Wenruo 			    expect_item_size, key->type);
1496e2406a6fSQu Wenruo 		return -EUCLEAN;
1497e2406a6fSQu Wenruo 	}
1498c7c01a4aSDavid Sterba 	if (unlikely(!IS_ALIGNED(key->objectid, leaf->fs_info->sectorsize))) {
1499e2406a6fSQu Wenruo 		generic_err(leaf, slot,
1500e2406a6fSQu Wenruo "invalid key objectid for shared block ref, have %llu expect aligned to %u",
1501e2406a6fSQu Wenruo 			    key->objectid, leaf->fs_info->sectorsize);
1502e2406a6fSQu Wenruo 		return -EUCLEAN;
1503e2406a6fSQu Wenruo 	}
1504c7c01a4aSDavid Sterba 	if (unlikely(key->type != BTRFS_TREE_BLOCK_REF_KEY &&
1505c7c01a4aSDavid Sterba 		     !IS_ALIGNED(key->offset, leaf->fs_info->sectorsize))) {
1506e2406a6fSQu Wenruo 		extent_err(leaf, slot,
1507e2406a6fSQu Wenruo 		"invalid tree parent bytenr, have %llu expect aligned to %u",
1508e2406a6fSQu Wenruo 			   key->offset, leaf->fs_info->sectorsize);
1509e2406a6fSQu Wenruo 		return -EUCLEAN;
1510e2406a6fSQu Wenruo 	}
1511e2406a6fSQu Wenruo 	return 0;
1512e2406a6fSQu Wenruo }
1513e2406a6fSQu Wenruo 
15140785a9aaSQu Wenruo static int check_extent_data_ref(struct extent_buffer *leaf,
15150785a9aaSQu Wenruo 				 struct btrfs_key *key, int slot)
15160785a9aaSQu Wenruo {
15170785a9aaSQu Wenruo 	struct btrfs_extent_data_ref *dref;
15180785a9aaSQu Wenruo 	unsigned long ptr = btrfs_item_ptr_offset(leaf, slot);
15193212fa14SJosef Bacik 	const unsigned long end = ptr + btrfs_item_size(leaf, slot);
15200785a9aaSQu Wenruo 
15213212fa14SJosef Bacik 	if (unlikely(btrfs_item_size(leaf, slot) % sizeof(*dref) != 0)) {
15220785a9aaSQu Wenruo 		generic_err(leaf, slot,
15230785a9aaSQu Wenruo 	"invalid item size, have %u expect aligned to %zu for key type %u",
15243212fa14SJosef Bacik 			    btrfs_item_size(leaf, slot),
15250785a9aaSQu Wenruo 			    sizeof(*dref), key->type);
15266d06b0adSDavid Sterba 		return -EUCLEAN;
15270785a9aaSQu Wenruo 	}
1528c7c01a4aSDavid Sterba 	if (unlikely(!IS_ALIGNED(key->objectid, leaf->fs_info->sectorsize))) {
15290785a9aaSQu Wenruo 		generic_err(leaf, slot,
15300785a9aaSQu Wenruo "invalid key objectid for shared block ref, have %llu expect aligned to %u",
15310785a9aaSQu Wenruo 			    key->objectid, leaf->fs_info->sectorsize);
15320785a9aaSQu Wenruo 		return -EUCLEAN;
15330785a9aaSQu Wenruo 	}
15340785a9aaSQu Wenruo 	for (; ptr < end; ptr += sizeof(*dref)) {
15350785a9aaSQu Wenruo 		u64 offset;
15360785a9aaSQu Wenruo 
15371119a72eSJosef Bacik 		/*
15381119a72eSJosef Bacik 		 * We cannot check the extent_data_ref hash due to possible
15391119a72eSJosef Bacik 		 * overflow from the leaf due to hash collisions.
15401119a72eSJosef Bacik 		 */
15410785a9aaSQu Wenruo 		dref = (struct btrfs_extent_data_ref *)ptr;
15420785a9aaSQu Wenruo 		offset = btrfs_extent_data_ref_offset(leaf, dref);
1543c7c01a4aSDavid Sterba 		if (unlikely(!IS_ALIGNED(offset, leaf->fs_info->sectorsize))) {
15440785a9aaSQu Wenruo 			extent_err(leaf, slot,
15450785a9aaSQu Wenruo 	"invalid extent data backref offset, have %llu expect aligned to %u",
15460785a9aaSQu Wenruo 				   offset, leaf->fs_info->sectorsize);
15476d06b0adSDavid Sterba 			return -EUCLEAN;
15480785a9aaSQu Wenruo 		}
15490785a9aaSQu Wenruo 	}
15500785a9aaSQu Wenruo 	return 0;
15510785a9aaSQu Wenruo }
15520785a9aaSQu Wenruo 
1553c3053ebbSQu Wenruo #define inode_ref_err(eb, slot, fmt, args...)			\
1554c3053ebbSQu Wenruo 	inode_item_err(eb, slot, fmt, ##args)
155571bf92a9SQu Wenruo static int check_inode_ref(struct extent_buffer *leaf,
155671bf92a9SQu Wenruo 			   struct btrfs_key *key, struct btrfs_key *prev_key,
155771bf92a9SQu Wenruo 			   int slot)
155871bf92a9SQu Wenruo {
155971bf92a9SQu Wenruo 	struct btrfs_inode_ref *iref;
156071bf92a9SQu Wenruo 	unsigned long ptr;
156171bf92a9SQu Wenruo 	unsigned long end;
156271bf92a9SQu Wenruo 
1563c7c01a4aSDavid Sterba 	if (unlikely(!check_prev_ino(leaf, key, slot, prev_key)))
156480d7fd1eSQu Wenruo 		return -EUCLEAN;
156571bf92a9SQu Wenruo 	/* namelen can't be 0, so item_size == sizeof() is also invalid */
15663212fa14SJosef Bacik 	if (unlikely(btrfs_item_size(leaf, slot) <= sizeof(*iref))) {
1567c3053ebbSQu Wenruo 		inode_ref_err(leaf, slot,
156871bf92a9SQu Wenruo 			"invalid item size, have %u expect (%zu, %u)",
15693212fa14SJosef Bacik 			btrfs_item_size(leaf, slot),
157071bf92a9SQu Wenruo 			sizeof(*iref), BTRFS_LEAF_DATA_SIZE(leaf->fs_info));
157171bf92a9SQu Wenruo 		return -EUCLEAN;
157271bf92a9SQu Wenruo 	}
157371bf92a9SQu Wenruo 
157471bf92a9SQu Wenruo 	ptr = btrfs_item_ptr_offset(leaf, slot);
15753212fa14SJosef Bacik 	end = ptr + btrfs_item_size(leaf, slot);
157671bf92a9SQu Wenruo 	while (ptr < end) {
157771bf92a9SQu Wenruo 		u16 namelen;
157871bf92a9SQu Wenruo 
1579c7c01a4aSDavid Sterba 		if (unlikely(ptr + sizeof(iref) > end)) {
1580c3053ebbSQu Wenruo 			inode_ref_err(leaf, slot,
158171bf92a9SQu Wenruo 			"inode ref overflow, ptr %lu end %lu inode_ref_size %zu",
158271bf92a9SQu Wenruo 				ptr, end, sizeof(iref));
158371bf92a9SQu Wenruo 			return -EUCLEAN;
158471bf92a9SQu Wenruo 		}
158571bf92a9SQu Wenruo 
158671bf92a9SQu Wenruo 		iref = (struct btrfs_inode_ref *)ptr;
158771bf92a9SQu Wenruo 		namelen = btrfs_inode_ref_name_len(leaf, iref);
1588c7c01a4aSDavid Sterba 		if (unlikely(ptr + sizeof(*iref) + namelen > end)) {
1589c3053ebbSQu Wenruo 			inode_ref_err(leaf, slot,
159071bf92a9SQu Wenruo 				"inode ref overflow, ptr %lu end %lu namelen %u",
159171bf92a9SQu Wenruo 				ptr, end, namelen);
159271bf92a9SQu Wenruo 			return -EUCLEAN;
159371bf92a9SQu Wenruo 		}
159471bf92a9SQu Wenruo 
159571bf92a9SQu Wenruo 		/*
159671bf92a9SQu Wenruo 		 * NOTE: In theory we should record all found index numbers
159771bf92a9SQu Wenruo 		 * to find any duplicated indexes, but that will be too time
159871bf92a9SQu Wenruo 		 * consuming for inodes with too many hard links.
159971bf92a9SQu Wenruo 		 */
160071bf92a9SQu Wenruo 		ptr += sizeof(*iref) + namelen;
160171bf92a9SQu Wenruo 	}
160271bf92a9SQu Wenruo 	return 0;
160371bf92a9SQu Wenruo }
160471bf92a9SQu Wenruo 
160582fc28fbSQu Wenruo /*
1606557ea5ddSQu Wenruo  * Common point to switch the item-specific validation.
1607557ea5ddSQu Wenruo  */
16080076bc89SDavid Sterba static int check_leaf_item(struct extent_buffer *leaf,
16094e9845efSFilipe Manana 			   struct btrfs_key *key, int slot,
16104e9845efSFilipe Manana 			   struct btrfs_key *prev_key)
1611557ea5ddSQu Wenruo {
1612557ea5ddSQu Wenruo 	int ret = 0;
1613075cb3c7SQu Wenruo 	struct btrfs_chunk *chunk;
1614557ea5ddSQu Wenruo 
1615557ea5ddSQu Wenruo 	switch (key->type) {
1616557ea5ddSQu Wenruo 	case BTRFS_EXTENT_DATA_KEY:
16174e9845efSFilipe Manana 		ret = check_extent_data_item(leaf, key, slot, prev_key);
1618557ea5ddSQu Wenruo 		break;
1619557ea5ddSQu Wenruo 	case BTRFS_EXTENT_CSUM_KEY:
1620ad1d8c43SFilipe Manana 		ret = check_csum_item(leaf, key, slot, prev_key);
1621557ea5ddSQu Wenruo 		break;
1622ad7b0368SQu Wenruo 	case BTRFS_DIR_ITEM_KEY:
1623ad7b0368SQu Wenruo 	case BTRFS_DIR_INDEX_KEY:
1624ad7b0368SQu Wenruo 	case BTRFS_XATTR_ITEM_KEY:
1625c18679ebSQu Wenruo 		ret = check_dir_item(leaf, key, prev_key, slot);
1626ad7b0368SQu Wenruo 		break;
162771bf92a9SQu Wenruo 	case BTRFS_INODE_REF_KEY:
162871bf92a9SQu Wenruo 		ret = check_inode_ref(leaf, key, prev_key, slot);
162971bf92a9SQu Wenruo 		break;
1630fce466eaSQu Wenruo 	case BTRFS_BLOCK_GROUP_ITEM_KEY:
1631af60ce2bSDavid Sterba 		ret = check_block_group_item(leaf, key, slot);
1632fce466eaSQu Wenruo 		break;
1633075cb3c7SQu Wenruo 	case BTRFS_CHUNK_ITEM_KEY:
1634075cb3c7SQu Wenruo 		chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
1635f6d2a5c2SQu Wenruo 		ret = check_leaf_chunk_item(leaf, chunk, key, slot);
1636075cb3c7SQu Wenruo 		break;
1637ab4ba2e1SQu Wenruo 	case BTRFS_DEV_ITEM_KEY:
1638412a2312SDavid Sterba 		ret = check_dev_item(leaf, key, slot);
1639ab4ba2e1SQu Wenruo 		break;
1640496245caSQu Wenruo 	case BTRFS_INODE_ITEM_KEY:
164139e57f49SDavid Sterba 		ret = check_inode_item(leaf, key, slot);
1642496245caSQu Wenruo 		break;
1643259ee775SQu Wenruo 	case BTRFS_ROOT_ITEM_KEY:
1644259ee775SQu Wenruo 		ret = check_root_item(leaf, key, slot);
1645259ee775SQu Wenruo 		break;
1646f82d1c7cSQu Wenruo 	case BTRFS_EXTENT_ITEM_KEY:
1647f82d1c7cSQu Wenruo 	case BTRFS_METADATA_ITEM_KEY:
1648899b7f69SJosef Bacik 		ret = check_extent_item(leaf, key, slot, prev_key);
1649f82d1c7cSQu Wenruo 		break;
1650e2406a6fSQu Wenruo 	case BTRFS_TREE_BLOCK_REF_KEY:
1651e2406a6fSQu Wenruo 	case BTRFS_SHARED_DATA_REF_KEY:
1652e2406a6fSQu Wenruo 	case BTRFS_SHARED_BLOCK_REF_KEY:
1653e2406a6fSQu Wenruo 		ret = check_simple_keyed_refs(leaf, key, slot);
1654e2406a6fSQu Wenruo 		break;
16550785a9aaSQu Wenruo 	case BTRFS_EXTENT_DATA_REF_KEY:
16560785a9aaSQu Wenruo 		ret = check_extent_data_ref(leaf, key, slot);
16570785a9aaSQu Wenruo 		break;
1658557ea5ddSQu Wenruo 	}
1659557ea5ddSQu Wenruo 	return ret;
1660557ea5ddSQu Wenruo }
1661557ea5ddSQu Wenruo 
1662e2ccd361SDavid Sterba static int check_leaf(struct extent_buffer *leaf, bool check_item_data)
1663557ea5ddSQu Wenruo {
1664e2ccd361SDavid Sterba 	struct btrfs_fs_info *fs_info = leaf->fs_info;
1665557ea5ddSQu Wenruo 	/* No valid key type is 0, so all key should be larger than this key */
1666557ea5ddSQu Wenruo 	struct btrfs_key prev_key = {0, 0, 0};
1667557ea5ddSQu Wenruo 	struct btrfs_key key;
1668557ea5ddSQu Wenruo 	u32 nritems = btrfs_header_nritems(leaf);
1669557ea5ddSQu Wenruo 	int slot;
1670557ea5ddSQu Wenruo 
1671c7c01a4aSDavid Sterba 	if (unlikely(btrfs_header_level(leaf) != 0)) {
167286a6be3aSDavid Sterba 		generic_err(leaf, 0,
1673f556faa4SQu Wenruo 			"invalid level for leaf, have %d expect 0",
1674f556faa4SQu Wenruo 			btrfs_header_level(leaf));
1675f556faa4SQu Wenruo 		return -EUCLEAN;
1676f556faa4SQu Wenruo 	}
1677f556faa4SQu Wenruo 
1678557ea5ddSQu Wenruo 	/*
1679557ea5ddSQu Wenruo 	 * Extent buffers from a relocation tree have a owner field that
1680557ea5ddSQu Wenruo 	 * corresponds to the subvolume tree they are based on. So just from an
1681557ea5ddSQu Wenruo 	 * extent buffer alone we can not find out what is the id of the
1682557ea5ddSQu Wenruo 	 * corresponding subvolume tree, so we can not figure out if the extent
1683557ea5ddSQu Wenruo 	 * buffer corresponds to the root of the relocation tree or not. So
1684557ea5ddSQu Wenruo 	 * skip this check for relocation trees.
1685557ea5ddSQu Wenruo 	 */
1686557ea5ddSQu Wenruo 	if (nritems == 0 && !btrfs_header_flag(leaf, BTRFS_HEADER_FLAG_RELOC)) {
1687ba480dd4SQu Wenruo 		u64 owner = btrfs_header_owner(leaf);
1688557ea5ddSQu Wenruo 
1689ba480dd4SQu Wenruo 		/* These trees must never be empty */
1690c7c01a4aSDavid Sterba 		if (unlikely(owner == BTRFS_ROOT_TREE_OBJECTID ||
1691ba480dd4SQu Wenruo 			     owner == BTRFS_CHUNK_TREE_OBJECTID ||
1692ba480dd4SQu Wenruo 			     owner == BTRFS_DEV_TREE_OBJECTID ||
1693ba480dd4SQu Wenruo 			     owner == BTRFS_FS_TREE_OBJECTID ||
1694c7c01a4aSDavid Sterba 			     owner == BTRFS_DATA_RELOC_TREE_OBJECTID)) {
169586a6be3aSDavid Sterba 			generic_err(leaf, 0,
1696ba480dd4SQu Wenruo 			"invalid root, root %llu must never be empty",
1697ba480dd4SQu Wenruo 				    owner);
1698ba480dd4SQu Wenruo 			return -EUCLEAN;
1699ba480dd4SQu Wenruo 		}
1700c2fa821cSJosef Bacik 
170162fdaa52SQu Wenruo 		/* Unknown tree */
1702c7c01a4aSDavid Sterba 		if (unlikely(owner == 0)) {
170362fdaa52SQu Wenruo 			generic_err(leaf, 0,
170462fdaa52SQu Wenruo 				"invalid owner, root 0 is not defined");
170562fdaa52SQu Wenruo 			return -EUCLEAN;
170662fdaa52SQu Wenruo 		}
1707c2fa821cSJosef Bacik 
1708c2fa821cSJosef Bacik 		/* EXTENT_TREE_V2 can have empty extent trees. */
1709c2fa821cSJosef Bacik 		if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2))
1710c2fa821cSJosef Bacik 			return 0;
1711c2fa821cSJosef Bacik 
1712c2fa821cSJosef Bacik 		if (unlikely(owner == BTRFS_EXTENT_TREE_OBJECTID)) {
1713c2fa821cSJosef Bacik 			generic_err(leaf, 0,
1714c2fa821cSJosef Bacik 			"invalid root, root %llu must never be empty",
1715c2fa821cSJosef Bacik 				    owner);
1716c2fa821cSJosef Bacik 			return -EUCLEAN;
1717c2fa821cSJosef Bacik 		}
1718c2fa821cSJosef Bacik 
1719557ea5ddSQu Wenruo 		return 0;
1720557ea5ddSQu Wenruo 	}
1721557ea5ddSQu Wenruo 
1722c7c01a4aSDavid Sterba 	if (unlikely(nritems == 0))
1723557ea5ddSQu Wenruo 		return 0;
1724557ea5ddSQu Wenruo 
1725557ea5ddSQu Wenruo 	/*
1726557ea5ddSQu Wenruo 	 * Check the following things to make sure this is a good leaf, and
1727557ea5ddSQu Wenruo 	 * leaf users won't need to bother with similar sanity checks:
1728557ea5ddSQu Wenruo 	 *
1729557ea5ddSQu Wenruo 	 * 1) key ordering
1730557ea5ddSQu Wenruo 	 * 2) item offset and size
1731557ea5ddSQu Wenruo 	 *    No overlap, no hole, all inside the leaf.
1732557ea5ddSQu Wenruo 	 * 3) item content
1733557ea5ddSQu Wenruo 	 *    If possible, do comprehensive sanity check.
1734557ea5ddSQu Wenruo 	 *    NOTE: All checks must only rely on the item data itself.
1735557ea5ddSQu Wenruo 	 */
1736557ea5ddSQu Wenruo 	for (slot = 0; slot < nritems; slot++) {
1737557ea5ddSQu Wenruo 		u32 item_end_expected;
1738a6ab66ebSSu Yue 		u64 item_data_end;
1739557ea5ddSQu Wenruo 		int ret;
1740557ea5ddSQu Wenruo 
1741557ea5ddSQu Wenruo 		btrfs_item_key_to_cpu(leaf, &key, slot);
1742557ea5ddSQu Wenruo 
1743557ea5ddSQu Wenruo 		/* Make sure the keys are in the right order */
1744c7c01a4aSDavid Sterba 		if (unlikely(btrfs_comp_cpu_keys(&prev_key, &key) >= 0)) {
174586a6be3aSDavid Sterba 			generic_err(leaf, slot,
1746478d01b3SQu Wenruo 	"bad key order, prev (%llu %u %llu) current (%llu %u %llu)",
1747478d01b3SQu Wenruo 				prev_key.objectid, prev_key.type,
1748478d01b3SQu Wenruo 				prev_key.offset, key.objectid, key.type,
1749478d01b3SQu Wenruo 				key.offset);
1750557ea5ddSQu Wenruo 			return -EUCLEAN;
1751557ea5ddSQu Wenruo 		}
1752557ea5ddSQu Wenruo 
1753a6ab66ebSSu Yue 		item_data_end = (u64)btrfs_item_offset(leaf, slot) +
1754a6ab66ebSSu Yue 				btrfs_item_size(leaf, slot);
1755557ea5ddSQu Wenruo 		/*
1756557ea5ddSQu Wenruo 		 * Make sure the offset and ends are right, remember that the
1757557ea5ddSQu Wenruo 		 * item data starts at the end of the leaf and grows towards the
1758557ea5ddSQu Wenruo 		 * front.
1759557ea5ddSQu Wenruo 		 */
1760557ea5ddSQu Wenruo 		if (slot == 0)
1761557ea5ddSQu Wenruo 			item_end_expected = BTRFS_LEAF_DATA_SIZE(fs_info);
1762557ea5ddSQu Wenruo 		else
17633212fa14SJosef Bacik 			item_end_expected = btrfs_item_offset(leaf,
1764557ea5ddSQu Wenruo 								 slot - 1);
1765a6ab66ebSSu Yue 		if (unlikely(item_data_end != item_end_expected)) {
176686a6be3aSDavid Sterba 			generic_err(leaf, slot,
1767a6ab66ebSSu Yue 				"unexpected item end, have %llu expect %u",
1768a6ab66ebSSu Yue 				item_data_end, item_end_expected);
1769557ea5ddSQu Wenruo 			return -EUCLEAN;
1770557ea5ddSQu Wenruo 		}
1771557ea5ddSQu Wenruo 
1772557ea5ddSQu Wenruo 		/*
1773557ea5ddSQu Wenruo 		 * Check to make sure that we don't point outside of the leaf,
1774557ea5ddSQu Wenruo 		 * just in case all the items are consistent to each other, but
1775557ea5ddSQu Wenruo 		 * all point outside of the leaf.
1776557ea5ddSQu Wenruo 		 */
1777a6ab66ebSSu Yue 		if (unlikely(item_data_end > BTRFS_LEAF_DATA_SIZE(fs_info))) {
177886a6be3aSDavid Sterba 			generic_err(leaf, slot,
1779a6ab66ebSSu Yue 			"slot end outside of leaf, have %llu expect range [0, %u]",
1780a6ab66ebSSu Yue 				item_data_end, BTRFS_LEAF_DATA_SIZE(fs_info));
1781557ea5ddSQu Wenruo 			return -EUCLEAN;
1782557ea5ddSQu Wenruo 		}
1783557ea5ddSQu Wenruo 
1784557ea5ddSQu Wenruo 		/* Also check if the item pointer overlaps with btrfs item. */
1785c7c01a4aSDavid Sterba 		if (unlikely(btrfs_item_ptr_offset(leaf, slot) <
1786c7c01a4aSDavid Sterba 			     btrfs_item_nr_offset(slot) + sizeof(struct btrfs_item))) {
178786a6be3aSDavid Sterba 			generic_err(leaf, slot,
1788478d01b3SQu Wenruo 		"slot overlaps with its data, item end %lu data start %lu",
1789478d01b3SQu Wenruo 				btrfs_item_nr_offset(slot) +
1790478d01b3SQu Wenruo 				sizeof(struct btrfs_item),
1791478d01b3SQu Wenruo 				btrfs_item_ptr_offset(leaf, slot));
1792557ea5ddSQu Wenruo 			return -EUCLEAN;
1793557ea5ddSQu Wenruo 		}
1794557ea5ddSQu Wenruo 
179569fc6cbbSQu Wenruo 		if (check_item_data) {
179669fc6cbbSQu Wenruo 			/*
179769fc6cbbSQu Wenruo 			 * Check if the item size and content meet other
179869fc6cbbSQu Wenruo 			 * criteria
179969fc6cbbSQu Wenruo 			 */
18004e9845efSFilipe Manana 			ret = check_leaf_item(leaf, &key, slot, &prev_key);
1801c7c01a4aSDavid Sterba 			if (unlikely(ret < 0))
1802557ea5ddSQu Wenruo 				return ret;
180369fc6cbbSQu Wenruo 		}
1804557ea5ddSQu Wenruo 
1805557ea5ddSQu Wenruo 		prev_key.objectid = key.objectid;
1806557ea5ddSQu Wenruo 		prev_key.type = key.type;
1807557ea5ddSQu Wenruo 		prev_key.offset = key.offset;
1808557ea5ddSQu Wenruo 	}
1809557ea5ddSQu Wenruo 
1810557ea5ddSQu Wenruo 	return 0;
1811557ea5ddSQu Wenruo }
1812557ea5ddSQu Wenruo 
18131c4360eeSDavid Sterba int btrfs_check_leaf_full(struct extent_buffer *leaf)
181469fc6cbbSQu Wenruo {
1815e2ccd361SDavid Sterba 	return check_leaf(leaf, true);
181669fc6cbbSQu Wenruo }
181702529d7aSQu Wenruo ALLOW_ERROR_INJECTION(btrfs_check_leaf_full, ERRNO);
181869fc6cbbSQu Wenruo 
1819cfdaad5eSDavid Sterba int btrfs_check_leaf_relaxed(struct extent_buffer *leaf)
18202f659546SQu Wenruo {
1821e2ccd361SDavid Sterba 	return check_leaf(leaf, false);
18222f659546SQu Wenruo }
18232f659546SQu Wenruo 
1824813fd1dcSDavid Sterba int btrfs_check_node(struct extent_buffer *node)
1825557ea5ddSQu Wenruo {
1826813fd1dcSDavid Sterba 	struct btrfs_fs_info *fs_info = node->fs_info;
1827557ea5ddSQu Wenruo 	unsigned long nr = btrfs_header_nritems(node);
1828557ea5ddSQu Wenruo 	struct btrfs_key key, next_key;
1829557ea5ddSQu Wenruo 	int slot;
1830f556faa4SQu Wenruo 	int level = btrfs_header_level(node);
1831557ea5ddSQu Wenruo 	u64 bytenr;
1832557ea5ddSQu Wenruo 	int ret = 0;
1833557ea5ddSQu Wenruo 
1834c7c01a4aSDavid Sterba 	if (unlikely(level <= 0 || level >= BTRFS_MAX_LEVEL)) {
183586a6be3aSDavid Sterba 		generic_err(node, 0,
1836f556faa4SQu Wenruo 			"invalid level for node, have %d expect [1, %d]",
1837f556faa4SQu Wenruo 			level, BTRFS_MAX_LEVEL - 1);
1838f556faa4SQu Wenruo 		return -EUCLEAN;
1839f556faa4SQu Wenruo 	}
1840c7c01a4aSDavid Sterba 	if (unlikely(nr == 0 || nr > BTRFS_NODEPTRS_PER_BLOCK(fs_info))) {
18412f659546SQu Wenruo 		btrfs_crit(fs_info,
1842bba4f298SQu Wenruo "corrupt node: root=%llu block=%llu, nritems too %s, have %lu expect range [1,%u]",
18432f659546SQu Wenruo 			   btrfs_header_owner(node), node->start,
1844bba4f298SQu Wenruo 			   nr == 0 ? "small" : "large", nr,
18452f659546SQu Wenruo 			   BTRFS_NODEPTRS_PER_BLOCK(fs_info));
1846bba4f298SQu Wenruo 		return -EUCLEAN;
1847557ea5ddSQu Wenruo 	}
1848557ea5ddSQu Wenruo 
1849557ea5ddSQu Wenruo 	for (slot = 0; slot < nr - 1; slot++) {
1850557ea5ddSQu Wenruo 		bytenr = btrfs_node_blockptr(node, slot);
1851557ea5ddSQu Wenruo 		btrfs_node_key_to_cpu(node, &key, slot);
1852557ea5ddSQu Wenruo 		btrfs_node_key_to_cpu(node, &next_key, slot + 1);
1853557ea5ddSQu Wenruo 
1854c7c01a4aSDavid Sterba 		if (unlikely(!bytenr)) {
185586a6be3aSDavid Sterba 			generic_err(node, slot,
1856bba4f298SQu Wenruo 				"invalid NULL node pointer");
1857bba4f298SQu Wenruo 			ret = -EUCLEAN;
1858bba4f298SQu Wenruo 			goto out;
1859bba4f298SQu Wenruo 		}
1860c7c01a4aSDavid Sterba 		if (unlikely(!IS_ALIGNED(bytenr, fs_info->sectorsize))) {
186186a6be3aSDavid Sterba 			generic_err(node, slot,
1862bba4f298SQu Wenruo 			"unaligned pointer, have %llu should be aligned to %u",
18632f659546SQu Wenruo 				bytenr, fs_info->sectorsize);
1864bba4f298SQu Wenruo 			ret = -EUCLEAN;
1865557ea5ddSQu Wenruo 			goto out;
1866557ea5ddSQu Wenruo 		}
1867557ea5ddSQu Wenruo 
1868c7c01a4aSDavid Sterba 		if (unlikely(btrfs_comp_cpu_keys(&key, &next_key) >= 0)) {
186986a6be3aSDavid Sterba 			generic_err(node, slot,
1870bba4f298SQu Wenruo 	"bad key order, current (%llu %u %llu) next (%llu %u %llu)",
1871bba4f298SQu Wenruo 				key.objectid, key.type, key.offset,
1872bba4f298SQu Wenruo 				next_key.objectid, next_key.type,
1873bba4f298SQu Wenruo 				next_key.offset);
1874bba4f298SQu Wenruo 			ret = -EUCLEAN;
1875557ea5ddSQu Wenruo 			goto out;
1876557ea5ddSQu Wenruo 		}
1877557ea5ddSQu Wenruo 	}
1878557ea5ddSQu Wenruo out:
1879557ea5ddSQu Wenruo 	return ret;
1880557ea5ddSQu Wenruo }
188102529d7aSQu Wenruo ALLOW_ERROR_INJECTION(btrfs_check_node, ERRNO);
188288c602abSQu Wenruo 
188388c602abSQu Wenruo int btrfs_check_eb_owner(const struct extent_buffer *eb, u64 root_owner)
188488c602abSQu Wenruo {
188588c602abSQu Wenruo 	const bool is_subvol = is_fstree(root_owner);
188688c602abSQu Wenruo 	const u64 eb_owner = btrfs_header_owner(eb);
188788c602abSQu Wenruo 
188888c602abSQu Wenruo 	/*
188988c602abSQu Wenruo 	 * Skip dummy fs, as selftests don't create unique ebs for each dummy
189088c602abSQu Wenruo 	 * root.
189188c602abSQu Wenruo 	 */
189288c602abSQu Wenruo 	if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &eb->fs_info->fs_state))
189388c602abSQu Wenruo 		return 0;
189488c602abSQu Wenruo 	/*
189588c602abSQu Wenruo 	 * There are several call sites (backref walking, qgroup, and data
189688c602abSQu Wenruo 	 * reloc) passing 0 as @root_owner, as they are not holding the
189788c602abSQu Wenruo 	 * tree root.  In that case, we can not do a reliable ownership check,
189888c602abSQu Wenruo 	 * so just exit.
189988c602abSQu Wenruo 	 */
190088c602abSQu Wenruo 	if (root_owner == 0)
190188c602abSQu Wenruo 		return 0;
190288c602abSQu Wenruo 	/*
190388c602abSQu Wenruo 	 * These trees use key.offset as their owner, our callers don't have
190488c602abSQu Wenruo 	 * the extra capacity to pass key.offset here.  So we just skip them.
190588c602abSQu Wenruo 	 */
190688c602abSQu Wenruo 	if (root_owner == BTRFS_TREE_LOG_OBJECTID ||
190788c602abSQu Wenruo 	    root_owner == BTRFS_TREE_RELOC_OBJECTID)
190888c602abSQu Wenruo 		return 0;
190988c602abSQu Wenruo 
191088c602abSQu Wenruo 	if (!is_subvol) {
191188c602abSQu Wenruo 		/* For non-subvolume trees, the eb owner should match root owner */
191288c602abSQu Wenruo 		if (unlikely(root_owner != eb_owner)) {
191388c602abSQu Wenruo 			btrfs_crit(eb->fs_info,
191488c602abSQu Wenruo "corrupted %s, root=%llu block=%llu owner mismatch, have %llu expect %llu",
191588c602abSQu Wenruo 				btrfs_header_level(eb) == 0 ? "leaf" : "node",
191688c602abSQu Wenruo 				root_owner, btrfs_header_bytenr(eb), eb_owner,
191788c602abSQu Wenruo 				root_owner);
191888c602abSQu Wenruo 			return -EUCLEAN;
191988c602abSQu Wenruo 		}
192088c602abSQu Wenruo 		return 0;
192188c602abSQu Wenruo 	}
192288c602abSQu Wenruo 
192388c602abSQu Wenruo 	/*
192488c602abSQu Wenruo 	 * For subvolume trees, owners can mismatch, but they should all belong
192588c602abSQu Wenruo 	 * to subvolume trees.
192688c602abSQu Wenruo 	 */
192788c602abSQu Wenruo 	if (unlikely(is_subvol != is_fstree(eb_owner))) {
192888c602abSQu Wenruo 		btrfs_crit(eb->fs_info,
192988c602abSQu Wenruo "corrupted %s, root=%llu block=%llu owner mismatch, have %llu expect [%llu, %llu]",
193088c602abSQu Wenruo 			btrfs_header_level(eb) == 0 ? "leaf" : "node",
193188c602abSQu Wenruo 			root_owner, btrfs_header_bytenr(eb), eb_owner,
193288c602abSQu Wenruo 			BTRFS_FIRST_FREE_OBJECTID, BTRFS_LAST_FREE_OBJECTID);
193388c602abSQu Wenruo 		return -EUCLEAN;
193488c602abSQu Wenruo 	}
193588c602abSQu Wenruo 	return 0;
193688c602abSQu Wenruo }
1937