xref: /openbmc/linux/fs/btrfs/tree-checker.c (revision ad854a86)
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"
28c7f13d42SJosef Bacik #include "fs.h"
2907e81dc9SJosef Bacik #include "accessors.h"
306bfd0ffaSJosef Bacik #include "file-item.h"
31f541833cSJosef Bacik #include "inode-item.h"
32557ea5ddSQu Wenruo 
33bba4f298SQu Wenruo /*
34bba4f298SQu Wenruo  * Error message should follow the following format:
35bba4f298SQu Wenruo  * corrupt <type>: <identifier>, <reason>[, <bad_value>]
36bba4f298SQu Wenruo  *
37bba4f298SQu Wenruo  * @type:	leaf or node
38bba4f298SQu Wenruo  * @identifier:	the necessary info to locate the leaf/node.
3952042d8eSAndrea Gelmini  * 		It's recommended to decode key.objecitd/offset if it's
40bba4f298SQu Wenruo  * 		meaningful.
41bba4f298SQu Wenruo  * @reason:	describe the error
4252042d8eSAndrea Gelmini  * @bad_value:	optional, it's recommended to output bad value and its
43bba4f298SQu Wenruo  *		expected value (range).
44bba4f298SQu Wenruo  *
45bba4f298SQu Wenruo  * Since comma is used to separate the components, only space is allowed
46bba4f298SQu Wenruo  * inside each component.
47bba4f298SQu Wenruo  */
48bba4f298SQu Wenruo 
49bba4f298SQu Wenruo /*
50bba4f298SQu Wenruo  * Append generic "corrupt leaf/node root=%llu block=%llu slot=%d: " to @fmt.
51bba4f298SQu Wenruo  * Allows callers to customize the output.
52bba4f298SQu Wenruo  */
5386a6be3aSDavid Sterba __printf(3, 4)
54e67c718bSDavid Sterba __cold
generic_err(const struct extent_buffer * eb,int slot,const char * fmt,...)5586a6be3aSDavid Sterba static void generic_err(const struct extent_buffer *eb, int slot,
56bba4f298SQu Wenruo 			const char *fmt, ...)
57bba4f298SQu Wenruo {
5886a6be3aSDavid Sterba 	const struct btrfs_fs_info *fs_info = eb->fs_info;
59bba4f298SQu Wenruo 	struct va_format vaf;
60bba4f298SQu Wenruo 	va_list args;
61bba4f298SQu Wenruo 
62bba4f298SQu Wenruo 	va_start(args, fmt);
63bba4f298SQu Wenruo 
64bba4f298SQu Wenruo 	vaf.fmt = fmt;
65bba4f298SQu Wenruo 	vaf.va = &args;
66bba4f298SQu Wenruo 
672f659546SQu Wenruo 	btrfs_crit(fs_info,
68bba4f298SQu Wenruo 		"corrupt %s: root=%llu block=%llu slot=%d, %pV",
69bba4f298SQu Wenruo 		btrfs_header_level(eb) == 0 ? "leaf" : "node",
702f659546SQu Wenruo 		btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot, &vaf);
71bba4f298SQu Wenruo 	va_end(args);
72bba4f298SQu Wenruo }
73bba4f298SQu Wenruo 
748806d718SQu Wenruo /*
758806d718SQu Wenruo  * Customized reporter for extent data item, since its key objectid and
768806d718SQu Wenruo  * offset has its own meaning.
778806d718SQu Wenruo  */
781fd715ffSDavid Sterba __printf(3, 4)
79e67c718bSDavid Sterba __cold
file_extent_err(const struct extent_buffer * eb,int slot,const char * fmt,...)801fd715ffSDavid Sterba static void file_extent_err(const struct extent_buffer *eb, int slot,
818806d718SQu Wenruo 			    const char *fmt, ...)
828806d718SQu Wenruo {
831fd715ffSDavid Sterba 	const struct btrfs_fs_info *fs_info = eb->fs_info;
848806d718SQu Wenruo 	struct btrfs_key key;
858806d718SQu Wenruo 	struct va_format vaf;
868806d718SQu Wenruo 	va_list args;
878806d718SQu Wenruo 
888806d718SQu Wenruo 	btrfs_item_key_to_cpu(eb, &key, slot);
898806d718SQu Wenruo 	va_start(args, fmt);
908806d718SQu Wenruo 
918806d718SQu Wenruo 	vaf.fmt = fmt;
928806d718SQu Wenruo 	vaf.va = &args;
938806d718SQu Wenruo 
942f659546SQu Wenruo 	btrfs_crit(fs_info,
958806d718SQu Wenruo 	"corrupt %s: root=%llu block=%llu slot=%d ino=%llu file_offset=%llu, %pV",
962f659546SQu Wenruo 		btrfs_header_level(eb) == 0 ? "leaf" : "node",
972f659546SQu Wenruo 		btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot,
982f659546SQu Wenruo 		key.objectid, key.offset, &vaf);
998806d718SQu Wenruo 	va_end(args);
1008806d718SQu Wenruo }
1018806d718SQu Wenruo 
1028806d718SQu Wenruo /*
1038806d718SQu Wenruo  * Return 0 if the btrfs_file_extent_##name is aligned to @alignment
1048806d718SQu Wenruo  * Else return 1
1058806d718SQu Wenruo  */
106033774dcSDavid Sterba #define CHECK_FE_ALIGNED(leaf, slot, fi, name, alignment)		      \
1078806d718SQu Wenruo ({									      \
108c7c01a4aSDavid Sterba 	if (unlikely(!IS_ALIGNED(btrfs_file_extent_##name((leaf), (fi)),      \
109c7c01a4aSDavid Sterba 				 (alignment))))				      \
1101fd715ffSDavid Sterba 		file_extent_err((leaf), (slot),				      \
1118806d718SQu Wenruo 	"invalid %s for file extent, have %llu, should be aligned to %u",     \
1128806d718SQu Wenruo 			(#name), btrfs_file_extent_##name((leaf), (fi)),      \
1138806d718SQu Wenruo 			(alignment));					      \
1148806d718SQu Wenruo 	(!IS_ALIGNED(btrfs_file_extent_##name((leaf), (fi)), (alignment)));   \
1158806d718SQu Wenruo })
1168806d718SQu Wenruo 
file_extent_end(struct extent_buffer * leaf,struct btrfs_key * key,struct btrfs_file_extent_item * extent)1174e9845efSFilipe Manana static u64 file_extent_end(struct extent_buffer *leaf,
1184e9845efSFilipe Manana 			   struct btrfs_key *key,
1194e9845efSFilipe Manana 			   struct btrfs_file_extent_item *extent)
1204e9845efSFilipe Manana {
1214e9845efSFilipe Manana 	u64 end;
1224e9845efSFilipe Manana 	u64 len;
1234e9845efSFilipe Manana 
1244e9845efSFilipe Manana 	if (btrfs_file_extent_type(leaf, extent) == BTRFS_FILE_EXTENT_INLINE) {
1254e9845efSFilipe Manana 		len = btrfs_file_extent_ram_bytes(leaf, extent);
1264e9845efSFilipe Manana 		end = ALIGN(key->offset + len, leaf->fs_info->sectorsize);
1274e9845efSFilipe Manana 	} else {
1284e9845efSFilipe Manana 		len = btrfs_file_extent_num_bytes(leaf, extent);
1294e9845efSFilipe Manana 		end = key->offset + len;
1304e9845efSFilipe Manana 	}
1314e9845efSFilipe Manana 	return end;
1324e9845efSFilipe Manana }
1334e9845efSFilipe Manana 
13480d7fd1eSQu Wenruo /*
13580d7fd1eSQu Wenruo  * Customized report for dir_item, the only new important information is
13680d7fd1eSQu Wenruo  * key->objectid, which represents inode number
13780d7fd1eSQu Wenruo  */
13880d7fd1eSQu Wenruo __printf(3, 4)
13980d7fd1eSQu Wenruo __cold
dir_item_err(const struct extent_buffer * eb,int slot,const char * fmt,...)14080d7fd1eSQu Wenruo static void dir_item_err(const struct extent_buffer *eb, int slot,
14180d7fd1eSQu Wenruo 			 const char *fmt, ...)
14280d7fd1eSQu Wenruo {
14380d7fd1eSQu Wenruo 	const struct btrfs_fs_info *fs_info = eb->fs_info;
14480d7fd1eSQu Wenruo 	struct btrfs_key key;
14580d7fd1eSQu Wenruo 	struct va_format vaf;
14680d7fd1eSQu Wenruo 	va_list args;
14780d7fd1eSQu Wenruo 
14880d7fd1eSQu Wenruo 	btrfs_item_key_to_cpu(eb, &key, slot);
14980d7fd1eSQu Wenruo 	va_start(args, fmt);
15080d7fd1eSQu Wenruo 
15180d7fd1eSQu Wenruo 	vaf.fmt = fmt;
15280d7fd1eSQu Wenruo 	vaf.va = &args;
15380d7fd1eSQu Wenruo 
15480d7fd1eSQu Wenruo 	btrfs_crit(fs_info,
15580d7fd1eSQu Wenruo 		"corrupt %s: root=%llu block=%llu slot=%d ino=%llu, %pV",
15680d7fd1eSQu Wenruo 		btrfs_header_level(eb) == 0 ? "leaf" : "node",
15780d7fd1eSQu Wenruo 		btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot,
15880d7fd1eSQu Wenruo 		key.objectid, &vaf);
15980d7fd1eSQu Wenruo 	va_end(args);
16080d7fd1eSQu Wenruo }
16180d7fd1eSQu Wenruo 
16280d7fd1eSQu Wenruo /*
16380d7fd1eSQu Wenruo  * This functions checks prev_key->objectid, to ensure current key and prev_key
16480d7fd1eSQu Wenruo  * share the same objectid as inode number.
16580d7fd1eSQu Wenruo  *
16680d7fd1eSQu Wenruo  * This is to detect missing INODE_ITEM in subvolume trees.
16780d7fd1eSQu Wenruo  *
16880d7fd1eSQu Wenruo  * Return true if everything is OK or we don't need to check.
16980d7fd1eSQu Wenruo  * Return false if anything is wrong.
17080d7fd1eSQu Wenruo  */
check_prev_ino(struct extent_buffer * leaf,struct btrfs_key * key,int slot,struct btrfs_key * prev_key)17180d7fd1eSQu Wenruo static bool check_prev_ino(struct extent_buffer *leaf,
17280d7fd1eSQu Wenruo 			   struct btrfs_key *key, int slot,
17380d7fd1eSQu Wenruo 			   struct btrfs_key *prev_key)
17480d7fd1eSQu Wenruo {
17580d7fd1eSQu Wenruo 	/* No prev key, skip check */
17680d7fd1eSQu Wenruo 	if (slot == 0)
17780d7fd1eSQu Wenruo 		return true;
17880d7fd1eSQu Wenruo 
17980d7fd1eSQu Wenruo 	/* Only these key->types needs to be checked */
18080d7fd1eSQu Wenruo 	ASSERT(key->type == BTRFS_XATTR_ITEM_KEY ||
18180d7fd1eSQu Wenruo 	       key->type == BTRFS_INODE_REF_KEY ||
18280d7fd1eSQu Wenruo 	       key->type == BTRFS_DIR_INDEX_KEY ||
18380d7fd1eSQu Wenruo 	       key->type == BTRFS_DIR_ITEM_KEY ||
18480d7fd1eSQu Wenruo 	       key->type == BTRFS_EXTENT_DATA_KEY);
18580d7fd1eSQu Wenruo 
18680d7fd1eSQu Wenruo 	/*
18780d7fd1eSQu Wenruo 	 * Only subvolume trees along with their reloc trees need this check.
18880d7fd1eSQu Wenruo 	 * Things like log tree doesn't follow this ino requirement.
18980d7fd1eSQu Wenruo 	 */
19080d7fd1eSQu Wenruo 	if (!is_fstree(btrfs_header_owner(leaf)))
19180d7fd1eSQu Wenruo 		return true;
19280d7fd1eSQu Wenruo 
19380d7fd1eSQu Wenruo 	if (key->objectid == prev_key->objectid)
19480d7fd1eSQu Wenruo 		return true;
19580d7fd1eSQu Wenruo 
19680d7fd1eSQu Wenruo 	/* Error found */
19780d7fd1eSQu Wenruo 	dir_item_err(leaf, slot,
19880d7fd1eSQu Wenruo 		"invalid previous key objectid, have %llu expect %llu",
19980d7fd1eSQu Wenruo 		prev_key->objectid, key->objectid);
20080d7fd1eSQu Wenruo 	return false;
20180d7fd1eSQu Wenruo }
check_extent_data_item(struct extent_buffer * leaf,struct btrfs_key * key,int slot,struct btrfs_key * prev_key)202ae2a19d8SDavid Sterba static int check_extent_data_item(struct extent_buffer *leaf,
2034e9845efSFilipe Manana 				  struct btrfs_key *key, int slot,
2044e9845efSFilipe Manana 				  struct btrfs_key *prev_key)
205557ea5ddSQu Wenruo {
206ae2a19d8SDavid Sterba 	struct btrfs_fs_info *fs_info = leaf->fs_info;
207557ea5ddSQu Wenruo 	struct btrfs_file_extent_item *fi;
2082f659546SQu Wenruo 	u32 sectorsize = fs_info->sectorsize;
2093212fa14SJosef Bacik 	u32 item_size = btrfs_item_size(leaf, slot);
2104c094c33SQu Wenruo 	u64 extent_end;
211557ea5ddSQu Wenruo 
212c7c01a4aSDavid Sterba 	if (unlikely(!IS_ALIGNED(key->offset, sectorsize))) {
2131fd715ffSDavid Sterba 		file_extent_err(leaf, slot,
2148806d718SQu Wenruo "unaligned file_offset for file extent, have %llu should be aligned to %u",
2158806d718SQu Wenruo 			key->offset, sectorsize);
216557ea5ddSQu Wenruo 		return -EUCLEAN;
217557ea5ddSQu Wenruo 	}
218557ea5ddSQu Wenruo 
219c18679ebSQu Wenruo 	/*
220c18679ebSQu Wenruo 	 * Previous key must have the same key->objectid (ino).
221c18679ebSQu Wenruo 	 * It can be XATTR_ITEM, INODE_ITEM or just another EXTENT_DATA.
222c18679ebSQu Wenruo 	 * But if objectids mismatch, it means we have a missing
223c18679ebSQu Wenruo 	 * INODE_ITEM.
224c18679ebSQu Wenruo 	 */
225c7c01a4aSDavid Sterba 	if (unlikely(!check_prev_ino(leaf, key, slot, prev_key)))
226c18679ebSQu Wenruo 		return -EUCLEAN;
227c18679ebSQu Wenruo 
228557ea5ddSQu Wenruo 	fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
229557ea5ddSQu Wenruo 
230153a6d29SQu Wenruo 	/*
231153a6d29SQu Wenruo 	 * Make sure the item contains at least inline header, so the file
232153a6d29SQu Wenruo 	 * extent type is not some garbage.
233153a6d29SQu Wenruo 	 */
234c7c01a4aSDavid Sterba 	if (unlikely(item_size < BTRFS_FILE_EXTENT_INLINE_DATA_START)) {
235153a6d29SQu Wenruo 		file_extent_err(leaf, slot,
236994bf9cdSAndreas Färber 				"invalid item size, have %u expect [%zu, %u)",
237153a6d29SQu Wenruo 				item_size, BTRFS_FILE_EXTENT_INLINE_DATA_START,
238153a6d29SQu Wenruo 				SZ_4K);
239153a6d29SQu Wenruo 		return -EUCLEAN;
240153a6d29SQu Wenruo 	}
241c7c01a4aSDavid Sterba 	if (unlikely(btrfs_file_extent_type(leaf, fi) >=
242c7c01a4aSDavid Sterba 		     BTRFS_NR_FILE_EXTENT_TYPES)) {
2431fd715ffSDavid Sterba 		file_extent_err(leaf, slot,
2448806d718SQu Wenruo 		"invalid type for file extent, have %u expect range [0, %u]",
2458806d718SQu Wenruo 			btrfs_file_extent_type(leaf, fi),
246b9b1a53eSChengguang Xu 			BTRFS_NR_FILE_EXTENT_TYPES - 1);
247557ea5ddSQu Wenruo 		return -EUCLEAN;
248557ea5ddSQu Wenruo 	}
249557ea5ddSQu Wenruo 
250557ea5ddSQu Wenruo 	/*
25152042d8eSAndrea Gelmini 	 * Support for new compression/encryption must introduce incompat flag,
252557ea5ddSQu Wenruo 	 * and must be caught in open_ctree().
253557ea5ddSQu Wenruo 	 */
254c7c01a4aSDavid Sterba 	if (unlikely(btrfs_file_extent_compression(leaf, fi) >=
255c7c01a4aSDavid Sterba 		     BTRFS_NR_COMPRESS_TYPES)) {
2561fd715ffSDavid Sterba 		file_extent_err(leaf, slot,
2578806d718SQu Wenruo 	"invalid compression for file extent, have %u expect range [0, %u]",
2588806d718SQu Wenruo 			btrfs_file_extent_compression(leaf, fi),
259ce96b7ffSChengguang Xu 			BTRFS_NR_COMPRESS_TYPES - 1);
260557ea5ddSQu Wenruo 		return -EUCLEAN;
261557ea5ddSQu Wenruo 	}
262c7c01a4aSDavid Sterba 	if (unlikely(btrfs_file_extent_encryption(leaf, fi))) {
2631fd715ffSDavid Sterba 		file_extent_err(leaf, slot,
2648806d718SQu Wenruo 			"invalid encryption for file extent, have %u expect 0",
2658806d718SQu Wenruo 			btrfs_file_extent_encryption(leaf, fi));
266557ea5ddSQu Wenruo 		return -EUCLEAN;
267557ea5ddSQu Wenruo 	}
268557ea5ddSQu Wenruo 	if (btrfs_file_extent_type(leaf, fi) == BTRFS_FILE_EXTENT_INLINE) {
269557ea5ddSQu Wenruo 		/* Inline extent must have 0 as key offset */
270c7c01a4aSDavid Sterba 		if (unlikely(key->offset)) {
2711fd715ffSDavid Sterba 			file_extent_err(leaf, slot,
2728806d718SQu Wenruo 		"invalid file_offset for inline file extent, have %llu expect 0",
2738806d718SQu Wenruo 				key->offset);
274557ea5ddSQu Wenruo 			return -EUCLEAN;
275557ea5ddSQu Wenruo 		}
276557ea5ddSQu Wenruo 
277557ea5ddSQu Wenruo 		/* Compressed inline extent has no on-disk size, skip it */
278557ea5ddSQu Wenruo 		if (btrfs_file_extent_compression(leaf, fi) !=
279557ea5ddSQu Wenruo 		    BTRFS_COMPRESS_NONE)
280557ea5ddSQu Wenruo 			return 0;
281557ea5ddSQu Wenruo 
282557ea5ddSQu Wenruo 		/* Uncompressed inline extent size must match item size */
283c7c01a4aSDavid Sterba 		if (unlikely(item_size != BTRFS_FILE_EXTENT_INLINE_DATA_START +
284c7c01a4aSDavid Sterba 					  btrfs_file_extent_ram_bytes(leaf, fi))) {
2851fd715ffSDavid Sterba 			file_extent_err(leaf, slot,
2868806d718SQu Wenruo 	"invalid ram_bytes for uncompressed inline extent, have %u expect %llu",
2878806d718SQu Wenruo 				item_size, BTRFS_FILE_EXTENT_INLINE_DATA_START +
2888806d718SQu Wenruo 				btrfs_file_extent_ram_bytes(leaf, fi));
289557ea5ddSQu Wenruo 			return -EUCLEAN;
290557ea5ddSQu Wenruo 		}
291557ea5ddSQu Wenruo 		return 0;
292557ea5ddSQu Wenruo 	}
293557ea5ddSQu Wenruo 
294557ea5ddSQu Wenruo 	/* Regular or preallocated extent has fixed item size */
295c7c01a4aSDavid Sterba 	if (unlikely(item_size != sizeof(*fi))) {
2961fd715ffSDavid Sterba 		file_extent_err(leaf, slot,
297709a95c3SArnd Bergmann 	"invalid item size for reg/prealloc file extent, have %u expect %zu",
2988806d718SQu Wenruo 			item_size, sizeof(*fi));
299557ea5ddSQu Wenruo 		return -EUCLEAN;
300557ea5ddSQu Wenruo 	}
301c7c01a4aSDavid Sterba 	if (unlikely(CHECK_FE_ALIGNED(leaf, slot, fi, ram_bytes, sectorsize) ||
302033774dcSDavid Sterba 		     CHECK_FE_ALIGNED(leaf, slot, fi, disk_bytenr, sectorsize) ||
303033774dcSDavid Sterba 		     CHECK_FE_ALIGNED(leaf, slot, fi, disk_num_bytes, sectorsize) ||
304033774dcSDavid Sterba 		     CHECK_FE_ALIGNED(leaf, slot, fi, offset, sectorsize) ||
305c7c01a4aSDavid Sterba 		     CHECK_FE_ALIGNED(leaf, slot, fi, num_bytes, sectorsize)))
306557ea5ddSQu Wenruo 		return -EUCLEAN;
3074e9845efSFilipe Manana 
3084c094c33SQu Wenruo 	/* Catch extent end overflow */
309c7c01a4aSDavid Sterba 	if (unlikely(check_add_overflow(btrfs_file_extent_num_bytes(leaf, fi),
310c7c01a4aSDavid Sterba 					key->offset, &extent_end))) {
3114c094c33SQu Wenruo 		file_extent_err(leaf, slot,
3124c094c33SQu Wenruo 	"extent end overflow, have file offset %llu extent num bytes %llu",
3134c094c33SQu Wenruo 				key->offset,
3144c094c33SQu Wenruo 				btrfs_file_extent_num_bytes(leaf, fi));
3154c094c33SQu Wenruo 		return -EUCLEAN;
3164c094c33SQu Wenruo 	}
3174c094c33SQu Wenruo 
3184e9845efSFilipe Manana 	/*
3194e9845efSFilipe Manana 	 * Check that no two consecutive file extent items, in the same leaf,
3204e9845efSFilipe Manana 	 * present ranges that overlap each other.
3214e9845efSFilipe Manana 	 */
3224e9845efSFilipe Manana 	if (slot > 0 &&
3234e9845efSFilipe Manana 	    prev_key->objectid == key->objectid &&
3244e9845efSFilipe Manana 	    prev_key->type == BTRFS_EXTENT_DATA_KEY) {
3254e9845efSFilipe Manana 		struct btrfs_file_extent_item *prev_fi;
3264e9845efSFilipe Manana 		u64 prev_end;
3274e9845efSFilipe Manana 
3284e9845efSFilipe Manana 		prev_fi = btrfs_item_ptr(leaf, slot - 1,
3294e9845efSFilipe Manana 					 struct btrfs_file_extent_item);
3304e9845efSFilipe Manana 		prev_end = file_extent_end(leaf, prev_key, prev_fi);
331c7c01a4aSDavid Sterba 		if (unlikely(prev_end > key->offset)) {
3324e9845efSFilipe Manana 			file_extent_err(leaf, slot - 1,
3334e9845efSFilipe Manana "file extent end range (%llu) goes beyond start offset (%llu) of the next file extent",
3344e9845efSFilipe Manana 					prev_end, key->offset);
3354e9845efSFilipe Manana 			return -EUCLEAN;
3364e9845efSFilipe Manana 		}
3374e9845efSFilipe Manana 	}
3384e9845efSFilipe Manana 
339557ea5ddSQu Wenruo 	return 0;
340557ea5ddSQu Wenruo }
341557ea5ddSQu Wenruo 
check_csum_item(struct extent_buffer * leaf,struct btrfs_key * key,int slot,struct btrfs_key * prev_key)34268128ce7SDavid Sterba static int check_csum_item(struct extent_buffer *leaf, struct btrfs_key *key,
343ad1d8c43SFilipe Manana 			   int slot, struct btrfs_key *prev_key)
344557ea5ddSQu Wenruo {
34568128ce7SDavid Sterba 	struct btrfs_fs_info *fs_info = leaf->fs_info;
3462f659546SQu Wenruo 	u32 sectorsize = fs_info->sectorsize;
347223486c2SDavid Sterba 	const u32 csumsize = fs_info->csum_size;
348557ea5ddSQu Wenruo 
349c7c01a4aSDavid Sterba 	if (unlikely(key->objectid != BTRFS_EXTENT_CSUM_OBJECTID)) {
35086a6be3aSDavid Sterba 		generic_err(leaf, slot,
351d508c5f0SQu Wenruo 		"invalid key objectid for csum item, have %llu expect %llu",
352d508c5f0SQu Wenruo 			key->objectid, BTRFS_EXTENT_CSUM_OBJECTID);
353557ea5ddSQu Wenruo 		return -EUCLEAN;
354557ea5ddSQu Wenruo 	}
355c7c01a4aSDavid Sterba 	if (unlikely(!IS_ALIGNED(key->offset, sectorsize))) {
35686a6be3aSDavid Sterba 		generic_err(leaf, slot,
357d508c5f0SQu Wenruo 	"unaligned key offset for csum item, have %llu should be aligned to %u",
358d508c5f0SQu Wenruo 			key->offset, sectorsize);
359557ea5ddSQu Wenruo 		return -EUCLEAN;
360557ea5ddSQu Wenruo 	}
3613212fa14SJosef Bacik 	if (unlikely(!IS_ALIGNED(btrfs_item_size(leaf, slot), csumsize))) {
36286a6be3aSDavid Sterba 		generic_err(leaf, slot,
363d508c5f0SQu Wenruo 	"unaligned item size for csum item, have %u should be aligned to %u",
3643212fa14SJosef Bacik 			btrfs_item_size(leaf, slot), csumsize);
365557ea5ddSQu Wenruo 		return -EUCLEAN;
366557ea5ddSQu Wenruo 	}
367ad1d8c43SFilipe Manana 	if (slot > 0 && prev_key->type == BTRFS_EXTENT_CSUM_KEY) {
368ad1d8c43SFilipe Manana 		u64 prev_csum_end;
369ad1d8c43SFilipe Manana 		u32 prev_item_size;
370ad1d8c43SFilipe Manana 
3713212fa14SJosef Bacik 		prev_item_size = btrfs_item_size(leaf, slot - 1);
372ad1d8c43SFilipe Manana 		prev_csum_end = (prev_item_size / csumsize) * sectorsize;
373ad1d8c43SFilipe Manana 		prev_csum_end += prev_key->offset;
374c7c01a4aSDavid Sterba 		if (unlikely(prev_csum_end > key->offset)) {
375ad1d8c43SFilipe Manana 			generic_err(leaf, slot - 1,
376ad1d8c43SFilipe Manana "csum end range (%llu) goes beyond the start range (%llu) of the next csum item",
377ad1d8c43SFilipe Manana 				    prev_csum_end, key->offset);
378ad1d8c43SFilipe Manana 			return -EUCLEAN;
379ad1d8c43SFilipe Manana 		}
380ad1d8c43SFilipe Manana 	}
381557ea5ddSQu Wenruo 	return 0;
382557ea5ddSQu Wenruo }
383557ea5ddSQu Wenruo 
384c23c77b0SQu Wenruo /* Inode item error output has the same format as dir_item_err() */
385c23c77b0SQu Wenruo #define inode_item_err(eb, slot, fmt, ...)			\
386c23c77b0SQu Wenruo 	dir_item_err(eb, slot, fmt, __VA_ARGS__)
387c23c77b0SQu Wenruo 
check_inode_key(struct extent_buffer * leaf,struct btrfs_key * key,int slot)388c23c77b0SQu Wenruo static int check_inode_key(struct extent_buffer *leaf, struct btrfs_key *key,
389c23c77b0SQu Wenruo 			   int slot)
390c23c77b0SQu Wenruo {
391c23c77b0SQu Wenruo 	struct btrfs_key item_key;
392c23c77b0SQu Wenruo 	bool is_inode_item;
393c23c77b0SQu Wenruo 
394c23c77b0SQu Wenruo 	btrfs_item_key_to_cpu(leaf, &item_key, slot);
395c23c77b0SQu Wenruo 	is_inode_item = (item_key.type == BTRFS_INODE_ITEM_KEY);
396c23c77b0SQu Wenruo 
397c23c77b0SQu Wenruo 	/* For XATTR_ITEM, location key should be all 0 */
398c23c77b0SQu Wenruo 	if (item_key.type == BTRFS_XATTR_ITEM_KEY) {
399c7c01a4aSDavid Sterba 		if (unlikely(key->objectid != 0 || key->type != 0 ||
400c7c01a4aSDavid Sterba 			     key->offset != 0))
401c23c77b0SQu Wenruo 			return -EUCLEAN;
402c23c77b0SQu Wenruo 		return 0;
403c23c77b0SQu Wenruo 	}
404c23c77b0SQu Wenruo 
405c7c01a4aSDavid Sterba 	if (unlikely((key->objectid < BTRFS_FIRST_FREE_OBJECTID ||
406c23c77b0SQu Wenruo 		      key->objectid > BTRFS_LAST_FREE_OBJECTID) &&
407c23c77b0SQu Wenruo 		     key->objectid != BTRFS_ROOT_TREE_DIR_OBJECTID &&
408c7c01a4aSDavid Sterba 		     key->objectid != BTRFS_FREE_INO_OBJECTID)) {
409c23c77b0SQu Wenruo 		if (is_inode_item) {
410c23c77b0SQu Wenruo 			generic_err(leaf, slot,
411c23c77b0SQu Wenruo 	"invalid key objectid: has %llu expect %llu or [%llu, %llu] or %llu",
412c23c77b0SQu Wenruo 				key->objectid, BTRFS_ROOT_TREE_DIR_OBJECTID,
413c23c77b0SQu Wenruo 				BTRFS_FIRST_FREE_OBJECTID,
414c23c77b0SQu Wenruo 				BTRFS_LAST_FREE_OBJECTID,
415c23c77b0SQu Wenruo 				BTRFS_FREE_INO_OBJECTID);
416c23c77b0SQu Wenruo 		} else {
417c23c77b0SQu Wenruo 			dir_item_err(leaf, slot,
418c23c77b0SQu Wenruo "invalid location key objectid: has %llu expect %llu or [%llu, %llu] or %llu",
419c23c77b0SQu Wenruo 				key->objectid, BTRFS_ROOT_TREE_DIR_OBJECTID,
420c23c77b0SQu Wenruo 				BTRFS_FIRST_FREE_OBJECTID,
421c23c77b0SQu Wenruo 				BTRFS_LAST_FREE_OBJECTID,
422c23c77b0SQu Wenruo 				BTRFS_FREE_INO_OBJECTID);
423c23c77b0SQu Wenruo 		}
424c23c77b0SQu Wenruo 		return -EUCLEAN;
425c23c77b0SQu Wenruo 	}
426c7c01a4aSDavid Sterba 	if (unlikely(key->offset != 0)) {
427c23c77b0SQu Wenruo 		if (is_inode_item)
428c23c77b0SQu Wenruo 			inode_item_err(leaf, slot,
429c23c77b0SQu Wenruo 				       "invalid key offset: has %llu expect 0",
430c23c77b0SQu Wenruo 				       key->offset);
431c23c77b0SQu Wenruo 		else
432c23c77b0SQu Wenruo 			dir_item_err(leaf, slot,
433c23c77b0SQu Wenruo 				"invalid location key offset:has %llu expect 0",
434c23c77b0SQu Wenruo 				key->offset);
435c23c77b0SQu Wenruo 		return -EUCLEAN;
436c23c77b0SQu Wenruo 	}
437c23c77b0SQu Wenruo 	return 0;
438c23c77b0SQu Wenruo }
439c23c77b0SQu Wenruo 
check_root_key(struct extent_buffer * leaf,struct btrfs_key * key,int slot)44057a0e674SQu Wenruo static int check_root_key(struct extent_buffer *leaf, struct btrfs_key *key,
44157a0e674SQu Wenruo 			  int slot)
44257a0e674SQu Wenruo {
44357a0e674SQu Wenruo 	struct btrfs_key item_key;
44457a0e674SQu Wenruo 	bool is_root_item;
44557a0e674SQu Wenruo 
44657a0e674SQu Wenruo 	btrfs_item_key_to_cpu(leaf, &item_key, slot);
44757a0e674SQu Wenruo 	is_root_item = (item_key.type == BTRFS_ROOT_ITEM_KEY);
44857a0e674SQu Wenruo 
4496ebcd021SQu Wenruo 	/*
4506ebcd021SQu Wenruo 	 * Bad rootid for reloc trees.
4516ebcd021SQu Wenruo 	 *
4526ebcd021SQu Wenruo 	 * Reloc trees are only for subvolume trees, other trees only need
4536ebcd021SQu Wenruo 	 * to be COWed to be relocated.
4546ebcd021SQu Wenruo 	 */
4556ebcd021SQu Wenruo 	if (unlikely(is_root_item && key->objectid == BTRFS_TREE_RELOC_OBJECTID &&
4566ebcd021SQu Wenruo 		     !is_fstree(key->offset))) {
4576ebcd021SQu Wenruo 		generic_err(leaf, slot,
4586ebcd021SQu Wenruo 		"invalid reloc tree for root %lld, root id is not a subvolume tree",
4596ebcd021SQu Wenruo 			    key->offset);
4606ebcd021SQu Wenruo 		return -EUCLEAN;
4616ebcd021SQu Wenruo 	}
4626ebcd021SQu Wenruo 
46357a0e674SQu Wenruo 	/* No such tree id */
464c7c01a4aSDavid Sterba 	if (unlikely(key->objectid == 0)) {
46557a0e674SQu Wenruo 		if (is_root_item)
46657a0e674SQu Wenruo 			generic_err(leaf, slot, "invalid root id 0");
46757a0e674SQu Wenruo 		else
46857a0e674SQu Wenruo 			dir_item_err(leaf, slot,
46957a0e674SQu Wenruo 				     "invalid location key root id 0");
47057a0e674SQu Wenruo 		return -EUCLEAN;
47157a0e674SQu Wenruo 	}
47257a0e674SQu Wenruo 
47357a0e674SQu Wenruo 	/* DIR_ITEM/INDEX/INODE_REF is not allowed to point to non-fs trees */
474c7c01a4aSDavid Sterba 	if (unlikely(!is_fstree(key->objectid) && !is_root_item)) {
47557a0e674SQu Wenruo 		dir_item_err(leaf, slot,
47657a0e674SQu Wenruo 		"invalid location key objectid, have %llu expect [%llu, %llu]",
47757a0e674SQu Wenruo 				key->objectid, BTRFS_FIRST_FREE_OBJECTID,
47857a0e674SQu Wenruo 				BTRFS_LAST_FREE_OBJECTID);
47957a0e674SQu Wenruo 		return -EUCLEAN;
48057a0e674SQu Wenruo 	}
48157a0e674SQu Wenruo 
48257a0e674SQu Wenruo 	/*
48357a0e674SQu Wenruo 	 * ROOT_ITEM with non-zero offset means this is a snapshot, created at
48457a0e674SQu Wenruo 	 * @offset transid.
48557a0e674SQu Wenruo 	 * Furthermore, for location key in DIR_ITEM, its offset is always -1.
48657a0e674SQu Wenruo 	 *
48757a0e674SQu Wenruo 	 * So here we only check offset for reloc tree whose key->offset must
48857a0e674SQu Wenruo 	 * be a valid tree.
48957a0e674SQu Wenruo 	 */
490c7c01a4aSDavid Sterba 	if (unlikely(key->objectid == BTRFS_TREE_RELOC_OBJECTID &&
491c7c01a4aSDavid Sterba 		     key->offset == 0)) {
49257a0e674SQu Wenruo 		generic_err(leaf, slot, "invalid root id 0 for reloc tree");
49357a0e674SQu Wenruo 		return -EUCLEAN;
49457a0e674SQu Wenruo 	}
49557a0e674SQu Wenruo 	return 0;
49657a0e674SQu Wenruo }
49757a0e674SQu Wenruo 
check_dir_item(struct extent_buffer * leaf,struct btrfs_key * key,struct btrfs_key * prev_key,int slot)498ce4252c0SDavid Sterba static int check_dir_item(struct extent_buffer *leaf,
499c18679ebSQu Wenruo 			  struct btrfs_key *key, struct btrfs_key *prev_key,
500c18679ebSQu Wenruo 			  int slot)
501ad7b0368SQu Wenruo {
502ce4252c0SDavid Sterba 	struct btrfs_fs_info *fs_info = leaf->fs_info;
503ad7b0368SQu Wenruo 	struct btrfs_dir_item *di;
5043212fa14SJosef Bacik 	u32 item_size = btrfs_item_size(leaf, slot);
505ad7b0368SQu Wenruo 	u32 cur = 0;
506ad7b0368SQu Wenruo 
507c7c01a4aSDavid Sterba 	if (unlikely(!check_prev_ino(leaf, key, slot, prev_key)))
508c18679ebSQu Wenruo 		return -EUCLEAN;
509c7c01a4aSDavid Sterba 
510ad7b0368SQu Wenruo 	di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
511ad7b0368SQu Wenruo 	while (cur < item_size) {
512147a097cSQu Wenruo 		struct btrfs_key location_key;
513ad7b0368SQu Wenruo 		u32 name_len;
514ad7b0368SQu Wenruo 		u32 data_len;
515ad7b0368SQu Wenruo 		u32 max_name_len;
516ad7b0368SQu Wenruo 		u32 total_size;
517ad7b0368SQu Wenruo 		u32 name_hash;
518ad7b0368SQu Wenruo 		u8 dir_type;
519147a097cSQu Wenruo 		int ret;
520ad7b0368SQu Wenruo 
521ad7b0368SQu Wenruo 		/* header itself should not cross item boundary */
522c7c01a4aSDavid Sterba 		if (unlikely(cur + sizeof(*di) > item_size)) {
523d98ced68SDavid Sterba 			dir_item_err(leaf, slot,
5247cfad652SArnd Bergmann 		"dir item header crosses item boundary, have %zu boundary %u",
525ad7b0368SQu Wenruo 				cur + sizeof(*di), item_size);
526ad7b0368SQu Wenruo 			return -EUCLEAN;
527ad7b0368SQu Wenruo 		}
528ad7b0368SQu Wenruo 
529147a097cSQu Wenruo 		/* Location key check */
530147a097cSQu Wenruo 		btrfs_dir_item_key_to_cpu(leaf, di, &location_key);
531147a097cSQu Wenruo 		if (location_key.type == BTRFS_ROOT_ITEM_KEY) {
532147a097cSQu Wenruo 			ret = check_root_key(leaf, &location_key, slot);
533c7c01a4aSDavid Sterba 			if (unlikely(ret < 0))
534147a097cSQu Wenruo 				return ret;
535147a097cSQu Wenruo 		} else if (location_key.type == BTRFS_INODE_ITEM_KEY ||
536147a097cSQu Wenruo 			   location_key.type == 0) {
537147a097cSQu Wenruo 			ret = check_inode_key(leaf, &location_key, slot);
538c7c01a4aSDavid Sterba 			if (unlikely(ret < 0))
539147a097cSQu Wenruo 				return ret;
540147a097cSQu Wenruo 		} else {
541147a097cSQu Wenruo 			dir_item_err(leaf, slot,
542147a097cSQu Wenruo 			"invalid location key type, have %u, expect %u or %u",
543147a097cSQu Wenruo 				     location_key.type, BTRFS_ROOT_ITEM_KEY,
544147a097cSQu Wenruo 				     BTRFS_INODE_ITEM_KEY);
545147a097cSQu Wenruo 			return -EUCLEAN;
546147a097cSQu Wenruo 		}
547147a097cSQu Wenruo 
548ad7b0368SQu Wenruo 		/* dir type check */
54994a48aefSOmar Sandoval 		dir_type = btrfs_dir_ftype(leaf, di);
550c7c01a4aSDavid Sterba 		if (unlikely(dir_type >= BTRFS_FT_MAX)) {
551d98ced68SDavid Sterba 			dir_item_err(leaf, slot,
552ad7b0368SQu Wenruo 			"invalid dir item type, have %u expect [0, %u)",
553ad7b0368SQu Wenruo 				dir_type, BTRFS_FT_MAX);
554ad7b0368SQu Wenruo 			return -EUCLEAN;
555ad7b0368SQu Wenruo 		}
556ad7b0368SQu Wenruo 
557c7c01a4aSDavid Sterba 		if (unlikely(key->type == BTRFS_XATTR_ITEM_KEY &&
558c7c01a4aSDavid Sterba 			     dir_type != BTRFS_FT_XATTR)) {
559d98ced68SDavid Sterba 			dir_item_err(leaf, slot,
560ad7b0368SQu Wenruo 		"invalid dir item type for XATTR key, have %u expect %u",
561ad7b0368SQu Wenruo 				dir_type, BTRFS_FT_XATTR);
562ad7b0368SQu Wenruo 			return -EUCLEAN;
563ad7b0368SQu Wenruo 		}
564c7c01a4aSDavid Sterba 		if (unlikely(dir_type == BTRFS_FT_XATTR &&
565c7c01a4aSDavid Sterba 			     key->type != BTRFS_XATTR_ITEM_KEY)) {
566d98ced68SDavid Sterba 			dir_item_err(leaf, slot,
567ad7b0368SQu Wenruo 			"xattr dir type found for non-XATTR key");
568ad7b0368SQu Wenruo 			return -EUCLEAN;
569ad7b0368SQu Wenruo 		}
570ad7b0368SQu Wenruo 		if (dir_type == BTRFS_FT_XATTR)
571ad7b0368SQu Wenruo 			max_name_len = XATTR_NAME_MAX;
572ad7b0368SQu Wenruo 		else
573ad7b0368SQu Wenruo 			max_name_len = BTRFS_NAME_LEN;
574ad7b0368SQu Wenruo 
575ad7b0368SQu Wenruo 		/* Name/data length check */
576ad7b0368SQu Wenruo 		name_len = btrfs_dir_name_len(leaf, di);
577ad7b0368SQu Wenruo 		data_len = btrfs_dir_data_len(leaf, di);
578c7c01a4aSDavid Sterba 		if (unlikely(name_len > max_name_len)) {
579d98ced68SDavid Sterba 			dir_item_err(leaf, slot,
580ad7b0368SQu Wenruo 			"dir item name len too long, have %u max %u",
581ad7b0368SQu Wenruo 				name_len, max_name_len);
582ad7b0368SQu Wenruo 			return -EUCLEAN;
583ad7b0368SQu Wenruo 		}
584c7c01a4aSDavid Sterba 		if (unlikely(name_len + data_len > BTRFS_MAX_XATTR_SIZE(fs_info))) {
585d98ced68SDavid Sterba 			dir_item_err(leaf, slot,
586ad7b0368SQu Wenruo 			"dir item name and data len too long, have %u max %u",
587ad7b0368SQu Wenruo 				name_len + data_len,
5882f659546SQu Wenruo 				BTRFS_MAX_XATTR_SIZE(fs_info));
589ad7b0368SQu Wenruo 			return -EUCLEAN;
590ad7b0368SQu Wenruo 		}
591ad7b0368SQu Wenruo 
592c7c01a4aSDavid Sterba 		if (unlikely(data_len && dir_type != BTRFS_FT_XATTR)) {
593d98ced68SDavid Sterba 			dir_item_err(leaf, slot,
594ad7b0368SQu Wenruo 			"dir item with invalid data len, have %u expect 0",
595ad7b0368SQu Wenruo 				data_len);
596ad7b0368SQu Wenruo 			return -EUCLEAN;
597ad7b0368SQu Wenruo 		}
598ad7b0368SQu Wenruo 
599ad7b0368SQu Wenruo 		total_size = sizeof(*di) + name_len + data_len;
600ad7b0368SQu Wenruo 
601ad7b0368SQu Wenruo 		/* header and name/data should not cross item boundary */
602c7c01a4aSDavid Sterba 		if (unlikely(cur + total_size > item_size)) {
603d98ced68SDavid Sterba 			dir_item_err(leaf, slot,
604ad7b0368SQu Wenruo 		"dir item data crosses item boundary, have %u boundary %u",
605ad7b0368SQu Wenruo 				cur + total_size, item_size);
606ad7b0368SQu Wenruo 			return -EUCLEAN;
607ad7b0368SQu Wenruo 		}
608ad7b0368SQu Wenruo 
609ad7b0368SQu Wenruo 		/*
610ad7b0368SQu Wenruo 		 * Special check for XATTR/DIR_ITEM, as key->offset is name
611ad7b0368SQu Wenruo 		 * hash, should match its name
612ad7b0368SQu Wenruo 		 */
613ad7b0368SQu Wenruo 		if (key->type == BTRFS_DIR_ITEM_KEY ||
614ad7b0368SQu Wenruo 		    key->type == BTRFS_XATTR_ITEM_KEY) {
615e2683fc9SDavid Sterba 			char namebuf[max(BTRFS_NAME_LEN, XATTR_NAME_MAX)];
616e2683fc9SDavid Sterba 
617ad7b0368SQu Wenruo 			read_extent_buffer(leaf, namebuf,
618ad7b0368SQu Wenruo 					(unsigned long)(di + 1), name_len);
619ad7b0368SQu Wenruo 			name_hash = btrfs_name_hash(namebuf, name_len);
620c7c01a4aSDavid Sterba 			if (unlikely(key->offset != name_hash)) {
621d98ced68SDavid Sterba 				dir_item_err(leaf, slot,
622ad7b0368SQu Wenruo 		"name hash mismatch with key, have 0x%016x expect 0x%016llx",
623ad7b0368SQu Wenruo 					name_hash, key->offset);
624ad7b0368SQu Wenruo 				return -EUCLEAN;
625ad7b0368SQu Wenruo 			}
626ad7b0368SQu Wenruo 		}
627ad7b0368SQu Wenruo 		cur += total_size;
628ad7b0368SQu Wenruo 		di = (struct btrfs_dir_item *)((void *)di + total_size);
629ad7b0368SQu Wenruo 	}
630ad7b0368SQu Wenruo 	return 0;
631ad7b0368SQu Wenruo }
632ad7b0368SQu Wenruo 
6334806bd88SDavid Sterba __printf(3, 4)
634fce466eaSQu Wenruo __cold
block_group_err(const struct extent_buffer * eb,int slot,const char * fmt,...)6354806bd88SDavid Sterba static void block_group_err(const struct extent_buffer *eb, int slot,
636fce466eaSQu Wenruo 			    const char *fmt, ...)
637fce466eaSQu Wenruo {
6384806bd88SDavid Sterba 	const struct btrfs_fs_info *fs_info = eb->fs_info;
639fce466eaSQu Wenruo 	struct btrfs_key key;
640fce466eaSQu Wenruo 	struct va_format vaf;
641fce466eaSQu Wenruo 	va_list args;
642fce466eaSQu Wenruo 
643fce466eaSQu Wenruo 	btrfs_item_key_to_cpu(eb, &key, slot);
644fce466eaSQu Wenruo 	va_start(args, fmt);
645fce466eaSQu Wenruo 
646fce466eaSQu Wenruo 	vaf.fmt = fmt;
647fce466eaSQu Wenruo 	vaf.va = &args;
648fce466eaSQu Wenruo 
649fce466eaSQu Wenruo 	btrfs_crit(fs_info,
650fce466eaSQu Wenruo 	"corrupt %s: root=%llu block=%llu slot=%d bg_start=%llu bg_len=%llu, %pV",
651fce466eaSQu Wenruo 		btrfs_header_level(eb) == 0 ? "leaf" : "node",
652fce466eaSQu Wenruo 		btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot,
653fce466eaSQu Wenruo 		key.objectid, key.offset, &vaf);
654fce466eaSQu Wenruo 	va_end(args);
655fce466eaSQu Wenruo }
656fce466eaSQu Wenruo 
check_block_group_item(struct extent_buffer * leaf,struct btrfs_key * key,int slot)657af60ce2bSDavid Sterba static int check_block_group_item(struct extent_buffer *leaf,
658fce466eaSQu Wenruo 				  struct btrfs_key *key, int slot)
659fce466eaSQu Wenruo {
660f7238e50SJosef Bacik 	struct btrfs_fs_info *fs_info = leaf->fs_info;
661fce466eaSQu Wenruo 	struct btrfs_block_group_item bgi;
6623212fa14SJosef Bacik 	u32 item_size = btrfs_item_size(leaf, slot);
663f7238e50SJosef Bacik 	u64 chunk_objectid;
664fce466eaSQu Wenruo 	u64 flags;
665fce466eaSQu Wenruo 	u64 type;
666fce466eaSQu Wenruo 
667fce466eaSQu Wenruo 	/*
668fce466eaSQu Wenruo 	 * Here we don't really care about alignment since extent allocator can
66910950929SQu Wenruo 	 * handle it.  We care more about the size.
670fce466eaSQu Wenruo 	 */
671c7c01a4aSDavid Sterba 	if (unlikely(key->offset == 0)) {
6724806bd88SDavid Sterba 		block_group_err(leaf, slot,
67310950929SQu Wenruo 				"invalid block group size 0");
674fce466eaSQu Wenruo 		return -EUCLEAN;
675fce466eaSQu Wenruo 	}
676fce466eaSQu Wenruo 
677c7c01a4aSDavid Sterba 	if (unlikely(item_size != sizeof(bgi))) {
6784806bd88SDavid Sterba 		block_group_err(leaf, slot,
679fce466eaSQu Wenruo 			"invalid item size, have %u expect %zu",
680fce466eaSQu Wenruo 				item_size, sizeof(bgi));
681fce466eaSQu Wenruo 		return -EUCLEAN;
682fce466eaSQu Wenruo 	}
683fce466eaSQu Wenruo 
684fce466eaSQu Wenruo 	read_extent_buffer(leaf, &bgi, btrfs_item_ptr_offset(leaf, slot),
685fce466eaSQu Wenruo 			   sizeof(bgi));
686f7238e50SJosef Bacik 	chunk_objectid = btrfs_stack_block_group_chunk_objectid(&bgi);
687f7238e50SJosef Bacik 	if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) {
688f7238e50SJosef Bacik 		/*
689f7238e50SJosef Bacik 		 * We don't init the nr_global_roots until we load the global
690f7238e50SJosef Bacik 		 * roots, so this could be 0 at mount time.  If it's 0 we'll
691f7238e50SJosef Bacik 		 * just assume we're fine, and later we'll check against our
692f7238e50SJosef Bacik 		 * actual value.
693f7238e50SJosef Bacik 		 */
694f7238e50SJosef Bacik 		if (unlikely(fs_info->nr_global_roots &&
695f7238e50SJosef Bacik 			     chunk_objectid >= fs_info->nr_global_roots)) {
696f7238e50SJosef Bacik 			block_group_err(leaf, slot,
697f7238e50SJosef Bacik 	"invalid block group global root id, have %llu, needs to be <= %llu",
698f7238e50SJosef Bacik 					chunk_objectid,
699f7238e50SJosef Bacik 					fs_info->nr_global_roots);
700f7238e50SJosef Bacik 			return -EUCLEAN;
701f7238e50SJosef Bacik 		}
702f7238e50SJosef Bacik 	} else if (unlikely(chunk_objectid != BTRFS_FIRST_CHUNK_TREE_OBJECTID)) {
7034806bd88SDavid Sterba 		block_group_err(leaf, slot,
704fce466eaSQu Wenruo 		"invalid block group chunk objectid, have %llu expect %llu",
705de0dc456SDavid Sterba 				btrfs_stack_block_group_chunk_objectid(&bgi),
706fce466eaSQu Wenruo 				BTRFS_FIRST_CHUNK_TREE_OBJECTID);
707fce466eaSQu Wenruo 		return -EUCLEAN;
708fce466eaSQu Wenruo 	}
709fce466eaSQu Wenruo 
710c7c01a4aSDavid Sterba 	if (unlikely(btrfs_stack_block_group_used(&bgi) > key->offset)) {
7114806bd88SDavid Sterba 		block_group_err(leaf, slot,
712fce466eaSQu Wenruo 			"invalid block group used, have %llu expect [0, %llu)",
713de0dc456SDavid Sterba 				btrfs_stack_block_group_used(&bgi), key->offset);
714fce466eaSQu Wenruo 		return -EUCLEAN;
715fce466eaSQu Wenruo 	}
716fce466eaSQu Wenruo 
717de0dc456SDavid Sterba 	flags = btrfs_stack_block_group_flags(&bgi);
718c7c01a4aSDavid Sterba 	if (unlikely(hweight64(flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) > 1)) {
7194806bd88SDavid Sterba 		block_group_err(leaf, slot,
720fce466eaSQu Wenruo "invalid profile flags, have 0x%llx (%lu bits set) expect no more than 1 bit set",
721fce466eaSQu Wenruo 			flags & BTRFS_BLOCK_GROUP_PROFILE_MASK,
722fce466eaSQu Wenruo 			hweight64(flags & BTRFS_BLOCK_GROUP_PROFILE_MASK));
723fce466eaSQu Wenruo 		return -EUCLEAN;
724fce466eaSQu Wenruo 	}
725fce466eaSQu Wenruo 
726fce466eaSQu Wenruo 	type = flags & BTRFS_BLOCK_GROUP_TYPE_MASK;
727c7c01a4aSDavid Sterba 	if (unlikely(type != BTRFS_BLOCK_GROUP_DATA &&
728fce466eaSQu Wenruo 		     type != BTRFS_BLOCK_GROUP_METADATA &&
729fce466eaSQu Wenruo 		     type != BTRFS_BLOCK_GROUP_SYSTEM &&
730fce466eaSQu Wenruo 		     type != (BTRFS_BLOCK_GROUP_METADATA |
731c7c01a4aSDavid Sterba 			      BTRFS_BLOCK_GROUP_DATA))) {
7324806bd88SDavid Sterba 		block_group_err(leaf, slot,
733761333f2SShaokun Zhang "invalid type, have 0x%llx (%lu bits set) expect either 0x%llx, 0x%llx, 0x%llx or 0x%llx",
734fce466eaSQu Wenruo 			type, hweight64(type),
735fce466eaSQu Wenruo 			BTRFS_BLOCK_GROUP_DATA, BTRFS_BLOCK_GROUP_METADATA,
736fce466eaSQu Wenruo 			BTRFS_BLOCK_GROUP_SYSTEM,
737fce466eaSQu Wenruo 			BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA);
738fce466eaSQu Wenruo 		return -EUCLEAN;
739fce466eaSQu Wenruo 	}
740fce466eaSQu Wenruo 	return 0;
741fce466eaSQu Wenruo }
742fce466eaSQu Wenruo 
743d001e4a3SDavid Sterba __printf(4, 5)
744f1140243SQu Wenruo __cold
chunk_err(const struct extent_buffer * leaf,const struct btrfs_chunk * chunk,u64 logical,const char * fmt,...)745d001e4a3SDavid Sterba static void chunk_err(const struct extent_buffer *leaf,
746f1140243SQu Wenruo 		      const struct btrfs_chunk *chunk, u64 logical,
747f1140243SQu Wenruo 		      const char *fmt, ...)
748f1140243SQu Wenruo {
749d001e4a3SDavid Sterba 	const struct btrfs_fs_info *fs_info = leaf->fs_info;
750f1140243SQu Wenruo 	bool is_sb;
751f1140243SQu Wenruo 	struct va_format vaf;
752f1140243SQu Wenruo 	va_list args;
753f1140243SQu Wenruo 	int i;
754f1140243SQu Wenruo 	int slot = -1;
755f1140243SQu Wenruo 
756f1140243SQu Wenruo 	/* Only superblock eb is able to have such small offset */
757f1140243SQu Wenruo 	is_sb = (leaf->start == BTRFS_SUPER_INFO_OFFSET);
758f1140243SQu Wenruo 
759f1140243SQu Wenruo 	if (!is_sb) {
760f1140243SQu Wenruo 		/*
761f1140243SQu Wenruo 		 * Get the slot number by iterating through all slots, this
762f1140243SQu Wenruo 		 * would provide better readability.
763f1140243SQu Wenruo 		 */
764f1140243SQu Wenruo 		for (i = 0; i < btrfs_header_nritems(leaf); i++) {
765f1140243SQu Wenruo 			if (btrfs_item_ptr_offset(leaf, i) ==
766f1140243SQu Wenruo 					(unsigned long)chunk) {
767f1140243SQu Wenruo 				slot = i;
768f1140243SQu Wenruo 				break;
769f1140243SQu Wenruo 			}
770f1140243SQu Wenruo 		}
771f1140243SQu Wenruo 	}
772f1140243SQu Wenruo 	va_start(args, fmt);
773f1140243SQu Wenruo 	vaf.fmt = fmt;
774f1140243SQu Wenruo 	vaf.va = &args;
775f1140243SQu Wenruo 
776f1140243SQu Wenruo 	if (is_sb)
777f1140243SQu Wenruo 		btrfs_crit(fs_info,
778f1140243SQu Wenruo 		"corrupt superblock syschunk array: chunk_start=%llu, %pV",
779f1140243SQu Wenruo 			   logical, &vaf);
780f1140243SQu Wenruo 	else
781f1140243SQu Wenruo 		btrfs_crit(fs_info,
782f1140243SQu Wenruo 	"corrupt leaf: root=%llu block=%llu slot=%d chunk_start=%llu, %pV",
783f1140243SQu Wenruo 			   BTRFS_CHUNK_TREE_OBJECTID, leaf->start, slot,
784f1140243SQu Wenruo 			   logical, &vaf);
785f1140243SQu Wenruo 	va_end(args);
786f1140243SQu Wenruo }
787f1140243SQu Wenruo 
788ad7b0368SQu Wenruo /*
78982fc28fbSQu Wenruo  * The common chunk check which could also work on super block sys chunk array.
79082fc28fbSQu Wenruo  *
791bf871c3bSQu Wenruo  * Return -EUCLEAN if anything is corrupted.
79282fc28fbSQu Wenruo  * Return 0 if everything is OK.
79382fc28fbSQu Wenruo  */
btrfs_check_chunk_valid(struct extent_buffer * leaf,struct btrfs_chunk * chunk,u64 logical)794ddaf1d5aSDavid Sterba int btrfs_check_chunk_valid(struct extent_buffer *leaf,
79582fc28fbSQu Wenruo 			    struct btrfs_chunk *chunk, u64 logical)
79682fc28fbSQu Wenruo {
797ddaf1d5aSDavid Sterba 	struct btrfs_fs_info *fs_info = leaf->fs_info;
79882fc28fbSQu Wenruo 	u64 length;
799347fb0cfSSu Yue 	u64 chunk_end;
80082fc28fbSQu Wenruo 	u64 stripe_len;
80182fc28fbSQu Wenruo 	u16 num_stripes;
80282fc28fbSQu Wenruo 	u16 sub_stripes;
80382fc28fbSQu Wenruo 	u64 type;
80482fc28fbSQu Wenruo 	u64 features;
80582fc28fbSQu Wenruo 	bool mixed = false;
80685d07fbeSDaniel Xu 	int raid_index;
80785d07fbeSDaniel Xu 	int nparity;
80885d07fbeSDaniel Xu 	int ncopies;
80982fc28fbSQu Wenruo 
81082fc28fbSQu Wenruo 	length = btrfs_chunk_length(leaf, chunk);
81182fc28fbSQu Wenruo 	stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
81282fc28fbSQu Wenruo 	num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
81382fc28fbSQu Wenruo 	sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
81482fc28fbSQu Wenruo 	type = btrfs_chunk_type(leaf, chunk);
81585d07fbeSDaniel Xu 	raid_index = btrfs_bg_flags_to_raid_index(type);
81685d07fbeSDaniel Xu 	ncopies = btrfs_raid_array[raid_index].ncopies;
81785d07fbeSDaniel Xu 	nparity = btrfs_raid_array[raid_index].nparity;
81882fc28fbSQu Wenruo 
819c7c01a4aSDavid Sterba 	if (unlikely(!num_stripes)) {
820d001e4a3SDavid Sterba 		chunk_err(leaf, chunk, logical,
821f1140243SQu Wenruo 			  "invalid chunk num_stripes, have %u", num_stripes);
822bf871c3bSQu Wenruo 		return -EUCLEAN;
82382fc28fbSQu Wenruo 	}
824c7c01a4aSDavid Sterba 	if (unlikely(num_stripes < ncopies)) {
82585d07fbeSDaniel Xu 		chunk_err(leaf, chunk, logical,
82685d07fbeSDaniel Xu 			  "invalid chunk num_stripes < ncopies, have %u < %d",
82785d07fbeSDaniel Xu 			  num_stripes, ncopies);
82885d07fbeSDaniel Xu 		return -EUCLEAN;
82985d07fbeSDaniel Xu 	}
830c7c01a4aSDavid Sterba 	if (unlikely(nparity && num_stripes == nparity)) {
83185d07fbeSDaniel Xu 		chunk_err(leaf, chunk, logical,
83285d07fbeSDaniel Xu 			  "invalid chunk num_stripes == nparity, have %u == %d",
83385d07fbeSDaniel Xu 			  num_stripes, nparity);
83485d07fbeSDaniel Xu 		return -EUCLEAN;
83585d07fbeSDaniel Xu 	}
836c7c01a4aSDavid Sterba 	if (unlikely(!IS_ALIGNED(logical, fs_info->sectorsize))) {
837d001e4a3SDavid Sterba 		chunk_err(leaf, chunk, logical,
838f1140243SQu Wenruo 		"invalid chunk logical, have %llu should aligned to %u",
839f1140243SQu Wenruo 			  logical, fs_info->sectorsize);
840bf871c3bSQu Wenruo 		return -EUCLEAN;
84182fc28fbSQu Wenruo 	}
842c7c01a4aSDavid Sterba 	if (unlikely(btrfs_chunk_sector_size(leaf, chunk) != fs_info->sectorsize)) {
843d001e4a3SDavid Sterba 		chunk_err(leaf, chunk, logical,
844f1140243SQu Wenruo 			  "invalid chunk sectorsize, have %u expect %u",
845f1140243SQu Wenruo 			  btrfs_chunk_sector_size(leaf, chunk),
846f1140243SQu Wenruo 			  fs_info->sectorsize);
847bf871c3bSQu Wenruo 		return -EUCLEAN;
84882fc28fbSQu Wenruo 	}
849c7c01a4aSDavid Sterba 	if (unlikely(!length || !IS_ALIGNED(length, fs_info->sectorsize))) {
850d001e4a3SDavid Sterba 		chunk_err(leaf, chunk, logical,
851f1140243SQu Wenruo 			  "invalid chunk length, have %llu", length);
852bf871c3bSQu Wenruo 		return -EUCLEAN;
85382fc28fbSQu Wenruo 	}
854347fb0cfSSu Yue 	if (unlikely(check_add_overflow(logical, length, &chunk_end))) {
855347fb0cfSSu Yue 		chunk_err(leaf, chunk, logical,
856347fb0cfSSu Yue "invalid chunk logical start and length, have logical start %llu length %llu",
857347fb0cfSSu Yue 			  logical, length);
858347fb0cfSSu Yue 		return -EUCLEAN;
859347fb0cfSSu Yue 	}
860c7c01a4aSDavid Sterba 	if (unlikely(!is_power_of_2(stripe_len) || stripe_len != BTRFS_STRIPE_LEN)) {
861d001e4a3SDavid Sterba 		chunk_err(leaf, chunk, logical,
862f1140243SQu Wenruo 			  "invalid chunk stripe length: %llu",
86382fc28fbSQu Wenruo 			  stripe_len);
864bf871c3bSQu Wenruo 		return -EUCLEAN;
86582fc28fbSQu Wenruo 	}
8666ded22c1SQu Wenruo 	/*
8676ded22c1SQu Wenruo 	 * We artificially limit the chunk size, so that the number of stripes
8686ded22c1SQu Wenruo 	 * inside a chunk can be fit into a U32.  The current limit (256G) is
8696ded22c1SQu Wenruo 	 * way too large for real world usage anyway, and it's also much larger
8706ded22c1SQu Wenruo 	 * than our existing limit (10G).
8716ded22c1SQu Wenruo 	 *
8726ded22c1SQu Wenruo 	 * Thus it should be a good way to catch obvious bitflips.
8736ded22c1SQu Wenruo 	 */
874cb091225SQu Wenruo 	if (unlikely(length >= btrfs_stripe_nr_to_offset(U32_MAX))) {
8756ded22c1SQu Wenruo 		chunk_err(leaf, chunk, logical,
8766ded22c1SQu Wenruo 			  "chunk length too large: have %llu limit %llu",
877cb091225SQu Wenruo 			  length, btrfs_stripe_nr_to_offset(U32_MAX));
8786ded22c1SQu Wenruo 		return -EUCLEAN;
8796ded22c1SQu Wenruo 	}
880c7c01a4aSDavid Sterba 	if (unlikely(type & ~(BTRFS_BLOCK_GROUP_TYPE_MASK |
881c7c01a4aSDavid Sterba 			      BTRFS_BLOCK_GROUP_PROFILE_MASK))) {
882d001e4a3SDavid Sterba 		chunk_err(leaf, chunk, logical,
883f1140243SQu Wenruo 			  "unrecognized chunk type: 0x%llx",
88482fc28fbSQu Wenruo 			  ~(BTRFS_BLOCK_GROUP_TYPE_MASK |
88582fc28fbSQu Wenruo 			    BTRFS_BLOCK_GROUP_PROFILE_MASK) &
88682fc28fbSQu Wenruo 			  btrfs_chunk_type(leaf, chunk));
887bf871c3bSQu Wenruo 		return -EUCLEAN;
88882fc28fbSQu Wenruo 	}
88982fc28fbSQu Wenruo 
890c7c01a4aSDavid Sterba 	if (unlikely(!has_single_bit_set(type & BTRFS_BLOCK_GROUP_PROFILE_MASK) &&
891c7c01a4aSDavid Sterba 		     (type & BTRFS_BLOCK_GROUP_PROFILE_MASK) != 0)) {
892d001e4a3SDavid Sterba 		chunk_err(leaf, chunk, logical,
89380e46cf2SQu Wenruo 		"invalid chunk profile flag: 0x%llx, expect 0 or 1 bit set",
89480e46cf2SQu Wenruo 			  type & BTRFS_BLOCK_GROUP_PROFILE_MASK);
89580e46cf2SQu Wenruo 		return -EUCLEAN;
89680e46cf2SQu Wenruo 	}
897c7c01a4aSDavid Sterba 	if (unlikely((type & BTRFS_BLOCK_GROUP_TYPE_MASK) == 0)) {
898d001e4a3SDavid Sterba 		chunk_err(leaf, chunk, logical,
899f1140243SQu Wenruo 	"missing chunk type flag, have 0x%llx one bit must be set in 0x%llx",
900f1140243SQu Wenruo 			  type, BTRFS_BLOCK_GROUP_TYPE_MASK);
901bf871c3bSQu Wenruo 		return -EUCLEAN;
90282fc28fbSQu Wenruo 	}
90382fc28fbSQu Wenruo 
904c7c01a4aSDavid Sterba 	if (unlikely((type & BTRFS_BLOCK_GROUP_SYSTEM) &&
905c7c01a4aSDavid Sterba 		     (type & (BTRFS_BLOCK_GROUP_METADATA |
906c7c01a4aSDavid Sterba 			      BTRFS_BLOCK_GROUP_DATA)))) {
907d001e4a3SDavid Sterba 		chunk_err(leaf, chunk, logical,
908f1140243SQu Wenruo 			  "system chunk with data or metadata type: 0x%llx",
909f1140243SQu Wenruo 			  type);
910bf871c3bSQu Wenruo 		return -EUCLEAN;
91182fc28fbSQu Wenruo 	}
91282fc28fbSQu Wenruo 
91382fc28fbSQu Wenruo 	features = btrfs_super_incompat_flags(fs_info->super_copy);
91482fc28fbSQu Wenruo 	if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
91582fc28fbSQu Wenruo 		mixed = true;
91682fc28fbSQu Wenruo 
91782fc28fbSQu Wenruo 	if (!mixed) {
918c7c01a4aSDavid Sterba 		if (unlikely((type & BTRFS_BLOCK_GROUP_METADATA) &&
919c7c01a4aSDavid Sterba 			     (type & BTRFS_BLOCK_GROUP_DATA))) {
920d001e4a3SDavid Sterba 			chunk_err(leaf, chunk, logical,
92182fc28fbSQu Wenruo 			"mixed chunk type in non-mixed mode: 0x%llx", type);
922bf871c3bSQu Wenruo 			return -EUCLEAN;
92382fc28fbSQu Wenruo 		}
92482fc28fbSQu Wenruo 	}
92582fc28fbSQu Wenruo 
9260ac6e06bSDavid Sterba 	if (unlikely((type & BTRFS_BLOCK_GROUP_RAID10 &&
9270ac6e06bSDavid Sterba 		      sub_stripes != btrfs_raid_array[BTRFS_RAID_RAID10].sub_stripes) ||
9280ac6e06bSDavid Sterba 		     (type & BTRFS_BLOCK_GROUP_RAID1 &&
9290ac6e06bSDavid Sterba 		      num_stripes != btrfs_raid_array[BTRFS_RAID_RAID1].devs_min) ||
9306c154ba4SDavid Sterba 		     (type & BTRFS_BLOCK_GROUP_RAID1C3 &&
9316c154ba4SDavid Sterba 		      num_stripes != btrfs_raid_array[BTRFS_RAID_RAID1C3].devs_min) ||
9326c154ba4SDavid Sterba 		     (type & BTRFS_BLOCK_GROUP_RAID1C4 &&
9336c154ba4SDavid Sterba 		      num_stripes != btrfs_raid_array[BTRFS_RAID_RAID1C4].devs_min) ||
9340ac6e06bSDavid Sterba 		     (type & BTRFS_BLOCK_GROUP_RAID5 &&
9350ac6e06bSDavid Sterba 		      num_stripes < btrfs_raid_array[BTRFS_RAID_RAID5].devs_min) ||
9360ac6e06bSDavid Sterba 		     (type & BTRFS_BLOCK_GROUP_RAID6 &&
9370ac6e06bSDavid Sterba 		      num_stripes < btrfs_raid_array[BTRFS_RAID_RAID6].devs_min) ||
9380ac6e06bSDavid Sterba 		     (type & BTRFS_BLOCK_GROUP_DUP &&
9390ac6e06bSDavid Sterba 		      num_stripes != btrfs_raid_array[BTRFS_RAID_DUP].dev_stripes) ||
940c7c01a4aSDavid Sterba 		     ((type & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 &&
9410ac6e06bSDavid Sterba 		      num_stripes != btrfs_raid_array[BTRFS_RAID_SINGLE].dev_stripes))) {
942d001e4a3SDavid Sterba 		chunk_err(leaf, chunk, logical,
94382fc28fbSQu Wenruo 			"invalid num_stripes:sub_stripes %u:%u for profile %llu",
94482fc28fbSQu Wenruo 			num_stripes, sub_stripes,
94582fc28fbSQu Wenruo 			type & BTRFS_BLOCK_GROUP_PROFILE_MASK);
946bf871c3bSQu Wenruo 		return -EUCLEAN;
94782fc28fbSQu Wenruo 	}
94882fc28fbSQu Wenruo 
94982fc28fbSQu Wenruo 	return 0;
95082fc28fbSQu Wenruo }
95182fc28fbSQu Wenruo 
952f6d2a5c2SQu Wenruo /*
953f6d2a5c2SQu Wenruo  * Enhanced version of chunk item checker.
954f6d2a5c2SQu Wenruo  *
955f6d2a5c2SQu Wenruo  * The common btrfs_check_chunk_valid() doesn't check item size since it needs
956f6d2a5c2SQu Wenruo  * to work on super block sys_chunk_array which doesn't have full item ptr.
957f6d2a5c2SQu Wenruo  */
check_leaf_chunk_item(struct extent_buffer * leaf,struct btrfs_chunk * chunk,struct btrfs_key * key,int slot)958f6d2a5c2SQu Wenruo static int check_leaf_chunk_item(struct extent_buffer *leaf,
959f6d2a5c2SQu Wenruo 				 struct btrfs_chunk *chunk,
960f6d2a5c2SQu Wenruo 				 struct btrfs_key *key, int slot)
961f6d2a5c2SQu Wenruo {
962f6d2a5c2SQu Wenruo 	int num_stripes;
963f6d2a5c2SQu Wenruo 
9643212fa14SJosef Bacik 	if (unlikely(btrfs_item_size(leaf, slot) < sizeof(struct btrfs_chunk))) {
965f6d2a5c2SQu Wenruo 		chunk_err(leaf, chunk, key->offset,
966f6d2a5c2SQu Wenruo 			"invalid chunk item size: have %u expect [%zu, %u)",
9673212fa14SJosef Bacik 			btrfs_item_size(leaf, slot),
968f6d2a5c2SQu Wenruo 			sizeof(struct btrfs_chunk),
969f6d2a5c2SQu Wenruo 			BTRFS_LEAF_DATA_SIZE(leaf->fs_info));
970f6d2a5c2SQu Wenruo 		return -EUCLEAN;
971f6d2a5c2SQu Wenruo 	}
972f6d2a5c2SQu Wenruo 
973f6d2a5c2SQu Wenruo 	num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
974f6d2a5c2SQu Wenruo 	/* Let btrfs_check_chunk_valid() handle this error type */
975f6d2a5c2SQu Wenruo 	if (num_stripes == 0)
976f6d2a5c2SQu Wenruo 		goto out;
977f6d2a5c2SQu Wenruo 
978c7c01a4aSDavid Sterba 	if (unlikely(btrfs_chunk_item_size(num_stripes) !=
9793212fa14SJosef Bacik 		     btrfs_item_size(leaf, slot))) {
980f6d2a5c2SQu Wenruo 		chunk_err(leaf, chunk, key->offset,
981f6d2a5c2SQu Wenruo 			"invalid chunk item size: have %u expect %lu",
9823212fa14SJosef Bacik 			btrfs_item_size(leaf, slot),
983f6d2a5c2SQu Wenruo 			btrfs_chunk_item_size(num_stripes));
984f6d2a5c2SQu Wenruo 		return -EUCLEAN;
985f6d2a5c2SQu Wenruo 	}
986f6d2a5c2SQu Wenruo out:
987f6d2a5c2SQu Wenruo 	return btrfs_check_chunk_valid(leaf, chunk, key->offset);
988f6d2a5c2SQu Wenruo }
989f6d2a5c2SQu Wenruo 
9905617ed80SDavid Sterba __printf(3, 4)
991ab4ba2e1SQu Wenruo __cold
dev_item_err(const struct extent_buffer * eb,int slot,const char * fmt,...)9925617ed80SDavid Sterba static void dev_item_err(const struct extent_buffer *eb, int slot,
993ab4ba2e1SQu Wenruo 			 const char *fmt, ...)
994ab4ba2e1SQu Wenruo {
995ab4ba2e1SQu Wenruo 	struct btrfs_key key;
996ab4ba2e1SQu Wenruo 	struct va_format vaf;
997ab4ba2e1SQu Wenruo 	va_list args;
998ab4ba2e1SQu Wenruo 
999ab4ba2e1SQu Wenruo 	btrfs_item_key_to_cpu(eb, &key, slot);
1000ab4ba2e1SQu Wenruo 	va_start(args, fmt);
1001ab4ba2e1SQu Wenruo 
1002ab4ba2e1SQu Wenruo 	vaf.fmt = fmt;
1003ab4ba2e1SQu Wenruo 	vaf.va = &args;
1004ab4ba2e1SQu Wenruo 
10055617ed80SDavid Sterba 	btrfs_crit(eb->fs_info,
1006ab4ba2e1SQu Wenruo 	"corrupt %s: root=%llu block=%llu slot=%d devid=%llu %pV",
1007ab4ba2e1SQu Wenruo 		btrfs_header_level(eb) == 0 ? "leaf" : "node",
1008ab4ba2e1SQu Wenruo 		btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot,
1009ab4ba2e1SQu Wenruo 		key.objectid, &vaf);
1010ab4ba2e1SQu Wenruo 	va_end(args);
1011ab4ba2e1SQu Wenruo }
1012ab4ba2e1SQu Wenruo 
check_dev_item(struct extent_buffer * leaf,struct btrfs_key * key,int slot)1013412a2312SDavid Sterba static int check_dev_item(struct extent_buffer *leaf,
1014ab4ba2e1SQu Wenruo 			  struct btrfs_key *key, int slot)
1015ab4ba2e1SQu Wenruo {
1016ab4ba2e1SQu Wenruo 	struct btrfs_dev_item *ditem;
1017ea1d1ca4SSu Yue 	const u32 item_size = btrfs_item_size(leaf, slot);
1018ab4ba2e1SQu Wenruo 
1019c7c01a4aSDavid Sterba 	if (unlikely(key->objectid != BTRFS_DEV_ITEMS_OBJECTID)) {
10205617ed80SDavid Sterba 		dev_item_err(leaf, slot,
1021ab4ba2e1SQu Wenruo 			     "invalid objectid: has=%llu expect=%llu",
1022ab4ba2e1SQu Wenruo 			     key->objectid, BTRFS_DEV_ITEMS_OBJECTID);
1023ab4ba2e1SQu Wenruo 		return -EUCLEAN;
1024ab4ba2e1SQu Wenruo 	}
1025ea1d1ca4SSu Yue 
1026ea1d1ca4SSu Yue 	if (unlikely(item_size != sizeof(*ditem))) {
1027ea1d1ca4SSu Yue 		dev_item_err(leaf, slot, "invalid item size: has %u expect %zu",
1028ea1d1ca4SSu Yue 			     item_size, sizeof(*ditem));
1029ea1d1ca4SSu Yue 		return -EUCLEAN;
1030ea1d1ca4SSu Yue 	}
1031ea1d1ca4SSu Yue 
1032ab4ba2e1SQu Wenruo 	ditem = btrfs_item_ptr(leaf, slot, struct btrfs_dev_item);
1033c7c01a4aSDavid Sterba 	if (unlikely(btrfs_device_id(leaf, ditem) != key->offset)) {
10345617ed80SDavid Sterba 		dev_item_err(leaf, slot,
1035ab4ba2e1SQu Wenruo 			     "devid mismatch: key has=%llu item has=%llu",
1036ab4ba2e1SQu Wenruo 			     key->offset, btrfs_device_id(leaf, ditem));
1037ab4ba2e1SQu Wenruo 		return -EUCLEAN;
1038ab4ba2e1SQu Wenruo 	}
1039ab4ba2e1SQu Wenruo 
1040ab4ba2e1SQu Wenruo 	/*
1041ab4ba2e1SQu Wenruo 	 * For device total_bytes, we don't have reliable way to check it, as
1042ab4ba2e1SQu Wenruo 	 * it can be 0 for device removal. Device size check can only be done
1043ab4ba2e1SQu Wenruo 	 * by dev extents check.
1044ab4ba2e1SQu Wenruo 	 */
1045c7c01a4aSDavid Sterba 	if (unlikely(btrfs_device_bytes_used(leaf, ditem) >
1046c7c01a4aSDavid Sterba 		     btrfs_device_total_bytes(leaf, ditem))) {
10475617ed80SDavid Sterba 		dev_item_err(leaf, slot,
1048ab4ba2e1SQu Wenruo 			     "invalid bytes used: have %llu expect [0, %llu]",
1049ab4ba2e1SQu Wenruo 			     btrfs_device_bytes_used(leaf, ditem),
1050ab4ba2e1SQu Wenruo 			     btrfs_device_total_bytes(leaf, ditem));
1051ab4ba2e1SQu Wenruo 		return -EUCLEAN;
1052ab4ba2e1SQu Wenruo 	}
1053ab4ba2e1SQu Wenruo 	/*
1054ab4ba2e1SQu Wenruo 	 * Remaining members like io_align/type/gen/dev_group aren't really
1055ab4ba2e1SQu Wenruo 	 * utilized.  Skip them to make later usage of them easier.
1056ab4ba2e1SQu Wenruo 	 */
1057ab4ba2e1SQu Wenruo 	return 0;
1058ab4ba2e1SQu Wenruo }
1059ab4ba2e1SQu Wenruo 
check_inode_item(struct extent_buffer * leaf,struct btrfs_key * key,int slot)106039e57f49SDavid Sterba static int check_inode_item(struct extent_buffer *leaf,
1061496245caSQu Wenruo 			    struct btrfs_key *key, int slot)
1062496245caSQu Wenruo {
106339e57f49SDavid Sterba 	struct btrfs_fs_info *fs_info = leaf->fs_info;
1064496245caSQu Wenruo 	struct btrfs_inode_item *iitem;
1065496245caSQu Wenruo 	u64 super_gen = btrfs_super_generation(fs_info->super_copy);
1066496245caSQu Wenruo 	u32 valid_mask = (S_IFMT | S_ISUID | S_ISGID | S_ISVTX | 0777);
10670c982944SSu Yue 	const u32 item_size = btrfs_item_size(leaf, slot);
1068496245caSQu Wenruo 	u32 mode;
1069c23c77b0SQu Wenruo 	int ret;
107077eea05eSBoris Burkov 	u32 flags;
107177eea05eSBoris Burkov 	u32 ro_flags;
1072496245caSQu Wenruo 
1073c23c77b0SQu Wenruo 	ret = check_inode_key(leaf, key, slot);
1074c7c01a4aSDavid Sterba 	if (unlikely(ret < 0))
1075c23c77b0SQu Wenruo 		return ret;
1076c23c77b0SQu Wenruo 
10770c982944SSu Yue 	if (unlikely(item_size != sizeof(*iitem))) {
10780c982944SSu Yue 		generic_err(leaf, slot, "invalid item size: has %u expect %zu",
10790c982944SSu Yue 			    item_size, sizeof(*iitem));
10800c982944SSu Yue 		return -EUCLEAN;
10810c982944SSu Yue 	}
10820c982944SSu Yue 
1083496245caSQu Wenruo 	iitem = btrfs_item_ptr(leaf, slot, struct btrfs_inode_item);
1084496245caSQu Wenruo 
1085496245caSQu Wenruo 	/* Here we use super block generation + 1 to handle log tree */
1086c7c01a4aSDavid Sterba 	if (unlikely(btrfs_inode_generation(leaf, iitem) > super_gen + 1)) {
1087c3053ebbSQu Wenruo 		inode_item_err(leaf, slot,
1088496245caSQu Wenruo 			"invalid inode generation: has %llu expect (0, %llu]",
1089496245caSQu Wenruo 			       btrfs_inode_generation(leaf, iitem),
1090496245caSQu Wenruo 			       super_gen + 1);
1091496245caSQu Wenruo 		return -EUCLEAN;
1092496245caSQu Wenruo 	}
1093496245caSQu Wenruo 	/* Note for ROOT_TREE_DIR_ITEM, mkfs could set its transid 0 */
1094c7c01a4aSDavid Sterba 	if (unlikely(btrfs_inode_transid(leaf, iitem) > super_gen + 1)) {
1095c3053ebbSQu Wenruo 		inode_item_err(leaf, slot,
1096f96d6960SQu Wenruo 			"invalid inode transid: has %llu expect [0, %llu]",
1097496245caSQu Wenruo 			       btrfs_inode_transid(leaf, iitem), super_gen + 1);
1098496245caSQu Wenruo 		return -EUCLEAN;
1099496245caSQu Wenruo 	}
1100496245caSQu Wenruo 
1101496245caSQu Wenruo 	/*
1102496245caSQu Wenruo 	 * For size and nbytes it's better not to be too strict, as for dir
1103496245caSQu Wenruo 	 * item its size/nbytes can easily get wrong, but doesn't affect
1104496245caSQu Wenruo 	 * anything in the fs. So here we skip the check.
1105496245caSQu Wenruo 	 */
1106496245caSQu Wenruo 	mode = btrfs_inode_mode(leaf, iitem);
1107c7c01a4aSDavid Sterba 	if (unlikely(mode & ~valid_mask)) {
1108c3053ebbSQu Wenruo 		inode_item_err(leaf, slot,
1109496245caSQu Wenruo 			       "unknown mode bit detected: 0x%x",
1110496245caSQu Wenruo 			       mode & ~valid_mask);
1111496245caSQu Wenruo 		return -EUCLEAN;
1112496245caSQu Wenruo 	}
1113496245caSQu Wenruo 
1114496245caSQu Wenruo 	/*
1115c1499166SDavid Sterba 	 * S_IFMT is not bit mapped so we can't completely rely on
1116c1499166SDavid Sterba 	 * is_power_of_2/has_single_bit_set, but it can save us from checking
1117c1499166SDavid Sterba 	 * FIFO/CHR/DIR/REG.  Only needs to check BLK, LNK and SOCKS
1118496245caSQu Wenruo 	 */
1119c1499166SDavid Sterba 	if (!has_single_bit_set(mode & S_IFMT)) {
1120c7c01a4aSDavid Sterba 		if (unlikely(!S_ISLNK(mode) && !S_ISBLK(mode) && !S_ISSOCK(mode))) {
1121c3053ebbSQu Wenruo 			inode_item_err(leaf, slot,
1122496245caSQu Wenruo 			"invalid mode: has 0%o expect valid S_IF* bit(s)",
1123496245caSQu Wenruo 				       mode & S_IFMT);
1124496245caSQu Wenruo 			return -EUCLEAN;
1125496245caSQu Wenruo 		}
1126496245caSQu Wenruo 	}
1127c7c01a4aSDavid Sterba 	if (unlikely(S_ISDIR(mode) && btrfs_inode_nlink(leaf, iitem) > 1)) {
1128c3053ebbSQu Wenruo 		inode_item_err(leaf, slot,
1129496245caSQu Wenruo 		       "invalid nlink: has %u expect no more than 1 for dir",
1130496245caSQu Wenruo 			btrfs_inode_nlink(leaf, iitem));
1131496245caSQu Wenruo 		return -EUCLEAN;
1132496245caSQu Wenruo 	}
113377eea05eSBoris Burkov 	btrfs_inode_split_flags(btrfs_inode_flags(leaf, iitem), &flags, &ro_flags);
113477eea05eSBoris Burkov 	if (unlikely(flags & ~BTRFS_INODE_FLAG_MASK)) {
1135c3053ebbSQu Wenruo 		inode_item_err(leaf, slot,
113677eea05eSBoris Burkov 			       "unknown incompat flags detected: 0x%x", flags);
113777eea05eSBoris Burkov 		return -EUCLEAN;
113877eea05eSBoris Burkov 	}
113977eea05eSBoris Burkov 	if (unlikely(!sb_rdonly(fs_info->sb) &&
114077eea05eSBoris Burkov 		     (ro_flags & ~BTRFS_INODE_RO_FLAG_MASK))) {
114177eea05eSBoris Burkov 		inode_item_err(leaf, slot,
114277eea05eSBoris Burkov 			"unknown ro-compat flags detected on writeable mount: 0x%x",
114377eea05eSBoris Burkov 			ro_flags);
1144496245caSQu Wenruo 		return -EUCLEAN;
1145496245caSQu Wenruo 	}
1146496245caSQu Wenruo 	return 0;
1147496245caSQu Wenruo }
1148496245caSQu Wenruo 
check_root_item(struct extent_buffer * leaf,struct btrfs_key * key,int slot)1149259ee775SQu Wenruo static int check_root_item(struct extent_buffer *leaf, struct btrfs_key *key,
1150259ee775SQu Wenruo 			   int slot)
1151259ee775SQu Wenruo {
1152259ee775SQu Wenruo 	struct btrfs_fs_info *fs_info = leaf->fs_info;
11531465af12SQu Wenruo 	struct btrfs_root_item ri = { 0 };
1154259ee775SQu Wenruo 	const u64 valid_root_flags = BTRFS_ROOT_SUBVOL_RDONLY |
1155259ee775SQu Wenruo 				     BTRFS_ROOT_SUBVOL_DEAD;
115657a0e674SQu Wenruo 	int ret;
1157259ee775SQu Wenruo 
115857a0e674SQu Wenruo 	ret = check_root_key(leaf, key, slot);
1159c7c01a4aSDavid Sterba 	if (unlikely(ret < 0))
116057a0e674SQu Wenruo 		return ret;
1161259ee775SQu Wenruo 
11623212fa14SJosef Bacik 	if (unlikely(btrfs_item_size(leaf, slot) != sizeof(ri) &&
11633212fa14SJosef Bacik 		     btrfs_item_size(leaf, slot) !=
1164c7c01a4aSDavid Sterba 		     btrfs_legacy_root_item_size())) {
1165259ee775SQu Wenruo 		generic_err(leaf, slot,
11661465af12SQu Wenruo 			    "invalid root item size, have %u expect %zu or %u",
11673212fa14SJosef Bacik 			    btrfs_item_size(leaf, slot), sizeof(ri),
11681465af12SQu Wenruo 			    btrfs_legacy_root_item_size());
11691a49a97dSDaniel Xu 		return -EUCLEAN;
1170259ee775SQu Wenruo 	}
1171259ee775SQu Wenruo 
11721465af12SQu Wenruo 	/*
11731465af12SQu Wenruo 	 * For legacy root item, the members starting at generation_v2 will be
11741465af12SQu Wenruo 	 * all filled with 0.
11751465af12SQu Wenruo 	 * And since we allow geneartion_v2 as 0, it will still pass the check.
11761465af12SQu Wenruo 	 */
1177259ee775SQu Wenruo 	read_extent_buffer(leaf, &ri, btrfs_item_ptr_offset(leaf, slot),
11783212fa14SJosef Bacik 			   btrfs_item_size(leaf, slot));
1179259ee775SQu Wenruo 
1180259ee775SQu Wenruo 	/* Generation related */
1181c7c01a4aSDavid Sterba 	if (unlikely(btrfs_root_generation(&ri) >
1182c7c01a4aSDavid Sterba 		     btrfs_super_generation(fs_info->super_copy) + 1)) {
1183259ee775SQu Wenruo 		generic_err(leaf, slot,
1184259ee775SQu Wenruo 			"invalid root generation, have %llu expect (0, %llu]",
1185259ee775SQu Wenruo 			    btrfs_root_generation(&ri),
1186259ee775SQu Wenruo 			    btrfs_super_generation(fs_info->super_copy) + 1);
1187259ee775SQu Wenruo 		return -EUCLEAN;
1188259ee775SQu Wenruo 	}
1189c7c01a4aSDavid Sterba 	if (unlikely(btrfs_root_generation_v2(&ri) >
1190c7c01a4aSDavid Sterba 		     btrfs_super_generation(fs_info->super_copy) + 1)) {
1191259ee775SQu Wenruo 		generic_err(leaf, slot,
1192259ee775SQu Wenruo 		"invalid root v2 generation, have %llu expect (0, %llu]",
1193259ee775SQu Wenruo 			    btrfs_root_generation_v2(&ri),
1194259ee775SQu Wenruo 			    btrfs_super_generation(fs_info->super_copy) + 1);
1195259ee775SQu Wenruo 		return -EUCLEAN;
1196259ee775SQu Wenruo 	}
1197c7c01a4aSDavid Sterba 	if (unlikely(btrfs_root_last_snapshot(&ri) >
1198c7c01a4aSDavid Sterba 		     btrfs_super_generation(fs_info->super_copy) + 1)) {
1199259ee775SQu Wenruo 		generic_err(leaf, slot,
1200259ee775SQu Wenruo 		"invalid root last_snapshot, have %llu expect (0, %llu]",
1201259ee775SQu Wenruo 			    btrfs_root_last_snapshot(&ri),
1202259ee775SQu Wenruo 			    btrfs_super_generation(fs_info->super_copy) + 1);
1203259ee775SQu Wenruo 		return -EUCLEAN;
1204259ee775SQu Wenruo 	}
1205259ee775SQu Wenruo 
1206259ee775SQu Wenruo 	/* Alignment and level check */
1207c7c01a4aSDavid Sterba 	if (unlikely(!IS_ALIGNED(btrfs_root_bytenr(&ri), fs_info->sectorsize))) {
1208259ee775SQu Wenruo 		generic_err(leaf, slot,
1209259ee775SQu Wenruo 		"invalid root bytenr, have %llu expect to be aligned to %u",
1210259ee775SQu Wenruo 			    btrfs_root_bytenr(&ri), fs_info->sectorsize);
1211259ee775SQu Wenruo 		return -EUCLEAN;
1212259ee775SQu Wenruo 	}
1213c7c01a4aSDavid Sterba 	if (unlikely(btrfs_root_level(&ri) >= BTRFS_MAX_LEVEL)) {
1214259ee775SQu Wenruo 		generic_err(leaf, slot,
1215259ee775SQu Wenruo 			    "invalid root level, have %u expect [0, %u]",
1216259ee775SQu Wenruo 			    btrfs_root_level(&ri), BTRFS_MAX_LEVEL - 1);
1217259ee775SQu Wenruo 		return -EUCLEAN;
1218259ee775SQu Wenruo 	}
1219c7c01a4aSDavid Sterba 	if (unlikely(btrfs_root_drop_level(&ri) >= BTRFS_MAX_LEVEL)) {
1220259ee775SQu Wenruo 		generic_err(leaf, slot,
1221259ee775SQu Wenruo 			    "invalid root level, have %u expect [0, %u]",
1222c8422684SDavid Sterba 			    btrfs_root_drop_level(&ri), BTRFS_MAX_LEVEL - 1);
1223259ee775SQu Wenruo 		return -EUCLEAN;
1224259ee775SQu Wenruo 	}
1225259ee775SQu Wenruo 
1226259ee775SQu Wenruo 	/* Flags check */
1227c7c01a4aSDavid Sterba 	if (unlikely(btrfs_root_flags(&ri) & ~valid_root_flags)) {
1228259ee775SQu Wenruo 		generic_err(leaf, slot,
1229259ee775SQu Wenruo 			    "invalid root flags, have 0x%llx expect mask 0x%llx",
1230259ee775SQu Wenruo 			    btrfs_root_flags(&ri), valid_root_flags);
1231259ee775SQu Wenruo 		return -EUCLEAN;
1232259ee775SQu Wenruo 	}
1233259ee775SQu Wenruo 	return 0;
1234259ee775SQu Wenruo }
1235259ee775SQu Wenruo 
1236f82d1c7cSQu Wenruo __printf(3,4)
1237f82d1c7cSQu Wenruo __cold
extent_err(const struct extent_buffer * eb,int slot,const char * fmt,...)1238f82d1c7cSQu Wenruo static void extent_err(const struct extent_buffer *eb, int slot,
1239f82d1c7cSQu Wenruo 		       const char *fmt, ...)
1240f82d1c7cSQu Wenruo {
1241f82d1c7cSQu Wenruo 	struct btrfs_key key;
1242f82d1c7cSQu Wenruo 	struct va_format vaf;
1243f82d1c7cSQu Wenruo 	va_list args;
1244f82d1c7cSQu Wenruo 	u64 bytenr;
1245f82d1c7cSQu Wenruo 	u64 len;
1246f82d1c7cSQu Wenruo 
1247f82d1c7cSQu Wenruo 	btrfs_item_key_to_cpu(eb, &key, slot);
1248f82d1c7cSQu Wenruo 	bytenr = key.objectid;
1249e2406a6fSQu Wenruo 	if (key.type == BTRFS_METADATA_ITEM_KEY ||
1250e2406a6fSQu Wenruo 	    key.type == BTRFS_TREE_BLOCK_REF_KEY ||
1251e2406a6fSQu Wenruo 	    key.type == BTRFS_SHARED_BLOCK_REF_KEY)
1252f82d1c7cSQu Wenruo 		len = eb->fs_info->nodesize;
1253f82d1c7cSQu Wenruo 	else
1254f82d1c7cSQu Wenruo 		len = key.offset;
1255f82d1c7cSQu Wenruo 	va_start(args, fmt);
1256f82d1c7cSQu Wenruo 
1257f82d1c7cSQu Wenruo 	vaf.fmt = fmt;
1258f82d1c7cSQu Wenruo 	vaf.va = &args;
1259f82d1c7cSQu Wenruo 
1260f82d1c7cSQu Wenruo 	btrfs_crit(eb->fs_info,
1261f82d1c7cSQu Wenruo 	"corrupt %s: block=%llu slot=%d extent bytenr=%llu len=%llu %pV",
1262f82d1c7cSQu Wenruo 		btrfs_header_level(eb) == 0 ? "leaf" : "node",
1263f82d1c7cSQu Wenruo 		eb->start, slot, bytenr, len, &vaf);
1264f82d1c7cSQu Wenruo 	va_end(args);
1265f82d1c7cSQu Wenruo }
1266f82d1c7cSQu Wenruo 
check_extent_item(struct extent_buffer * leaf,struct btrfs_key * key,int slot,struct btrfs_key * prev_key)1267f82d1c7cSQu Wenruo static int check_extent_item(struct extent_buffer *leaf,
1268899b7f69SJosef Bacik 			     struct btrfs_key *key, int slot,
1269899b7f69SJosef Bacik 			     struct btrfs_key *prev_key)
1270f82d1c7cSQu Wenruo {
1271f82d1c7cSQu Wenruo 	struct btrfs_fs_info *fs_info = leaf->fs_info;
1272f82d1c7cSQu Wenruo 	struct btrfs_extent_item *ei;
1273f82d1c7cSQu Wenruo 	bool is_tree_block = false;
1274f82d1c7cSQu Wenruo 	unsigned long ptr;	/* Current pointer inside inline refs */
1275f82d1c7cSQu Wenruo 	unsigned long end;	/* Extent item end */
12763212fa14SJosef Bacik 	const u32 item_size = btrfs_item_size(leaf, slot);
1277f82d1c7cSQu Wenruo 	u64 flags;
1278f82d1c7cSQu Wenruo 	u64 generation;
1279f82d1c7cSQu Wenruo 	u64 total_refs;		/* Total refs in btrfs_extent_item */
1280f82d1c7cSQu Wenruo 	u64 inline_refs = 0;	/* found total inline refs */
1281f82d1c7cSQu Wenruo 
1282c7c01a4aSDavid Sterba 	if (unlikely(key->type == BTRFS_METADATA_ITEM_KEY &&
1283c7c01a4aSDavid Sterba 		     !btrfs_fs_incompat(fs_info, SKINNY_METADATA))) {
1284f82d1c7cSQu Wenruo 		generic_err(leaf, slot,
1285f82d1c7cSQu Wenruo "invalid key type, METADATA_ITEM type invalid when SKINNY_METADATA feature disabled");
1286f82d1c7cSQu Wenruo 		return -EUCLEAN;
1287f82d1c7cSQu Wenruo 	}
1288f82d1c7cSQu Wenruo 	/* key->objectid is the bytenr for both key types */
1289c7c01a4aSDavid Sterba 	if (unlikely(!IS_ALIGNED(key->objectid, fs_info->sectorsize))) {
1290f82d1c7cSQu Wenruo 		generic_err(leaf, slot,
1291f82d1c7cSQu Wenruo 		"invalid key objectid, have %llu expect to be aligned to %u",
1292f82d1c7cSQu Wenruo 			   key->objectid, fs_info->sectorsize);
1293f82d1c7cSQu Wenruo 		return -EUCLEAN;
1294f82d1c7cSQu Wenruo 	}
1295f82d1c7cSQu Wenruo 
1296f82d1c7cSQu Wenruo 	/* key->offset is tree level for METADATA_ITEM_KEY */
1297c7c01a4aSDavid Sterba 	if (unlikely(key->type == BTRFS_METADATA_ITEM_KEY &&
1298c7c01a4aSDavid Sterba 		     key->offset >= BTRFS_MAX_LEVEL)) {
1299f82d1c7cSQu Wenruo 		extent_err(leaf, slot,
1300f82d1c7cSQu Wenruo 			   "invalid tree level, have %llu expect [0, %u]",
1301f82d1c7cSQu Wenruo 			   key->offset, BTRFS_MAX_LEVEL - 1);
1302f82d1c7cSQu Wenruo 		return -EUCLEAN;
1303f82d1c7cSQu Wenruo 	}
1304f82d1c7cSQu Wenruo 
1305f82d1c7cSQu Wenruo 	/*
1306f82d1c7cSQu Wenruo 	 * EXTENT/METADATA_ITEM consists of:
1307f82d1c7cSQu Wenruo 	 * 1) One btrfs_extent_item
1308f82d1c7cSQu Wenruo 	 *    Records the total refs, type and generation of the extent.
1309f82d1c7cSQu Wenruo 	 *
1310f82d1c7cSQu Wenruo 	 * 2) One btrfs_tree_block_info (for EXTENT_ITEM and tree backref only)
1311f82d1c7cSQu Wenruo 	 *    Records the first key and level of the tree block.
1312f82d1c7cSQu Wenruo 	 *
1313f82d1c7cSQu Wenruo 	 * 2) Zero or more btrfs_extent_inline_ref(s)
1314f82d1c7cSQu Wenruo 	 *    Each inline ref has one btrfs_extent_inline_ref shows:
1315f82d1c7cSQu Wenruo 	 *    2.1) The ref type, one of the 4
1316f82d1c7cSQu Wenruo 	 *         TREE_BLOCK_REF	Tree block only
1317f82d1c7cSQu Wenruo 	 *         SHARED_BLOCK_REF	Tree block only
1318f82d1c7cSQu Wenruo 	 *         EXTENT_DATA_REF	Data only
1319f82d1c7cSQu Wenruo 	 *         SHARED_DATA_REF	Data only
1320f82d1c7cSQu Wenruo 	 *    2.2) Ref type specific data
1321f82d1c7cSQu Wenruo 	 *         Either using btrfs_extent_inline_ref::offset, or specific
1322f82d1c7cSQu Wenruo 	 *         data structure.
1323f82d1c7cSQu Wenruo 	 */
1324c7c01a4aSDavid Sterba 	if (unlikely(item_size < sizeof(*ei))) {
1325f82d1c7cSQu Wenruo 		extent_err(leaf, slot,
1326f82d1c7cSQu Wenruo 			   "invalid item size, have %u expect [%zu, %u)",
1327f82d1c7cSQu Wenruo 			   item_size, sizeof(*ei),
1328f82d1c7cSQu Wenruo 			   BTRFS_LEAF_DATA_SIZE(fs_info));
1329f82d1c7cSQu Wenruo 		return -EUCLEAN;
1330f82d1c7cSQu Wenruo 	}
1331f82d1c7cSQu Wenruo 	end = item_size + btrfs_item_ptr_offset(leaf, slot);
1332f82d1c7cSQu Wenruo 
1333f82d1c7cSQu Wenruo 	/* Checks against extent_item */
1334f82d1c7cSQu Wenruo 	ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
1335f82d1c7cSQu Wenruo 	flags = btrfs_extent_flags(leaf, ei);
1336f82d1c7cSQu Wenruo 	total_refs = btrfs_extent_refs(leaf, ei);
1337f82d1c7cSQu Wenruo 	generation = btrfs_extent_generation(leaf, ei);
1338c7c01a4aSDavid Sterba 	if (unlikely(generation >
1339c7c01a4aSDavid Sterba 		     btrfs_super_generation(fs_info->super_copy) + 1)) {
1340f82d1c7cSQu Wenruo 		extent_err(leaf, slot,
1341f82d1c7cSQu Wenruo 			   "invalid generation, have %llu expect (0, %llu]",
1342f82d1c7cSQu Wenruo 			   generation,
1343f82d1c7cSQu Wenruo 			   btrfs_super_generation(fs_info->super_copy) + 1);
1344f82d1c7cSQu Wenruo 		return -EUCLEAN;
1345f82d1c7cSQu Wenruo 	}
1346c7c01a4aSDavid Sterba 	if (unlikely(!has_single_bit_set(flags & (BTRFS_EXTENT_FLAG_DATA |
1347c7c01a4aSDavid Sterba 						  BTRFS_EXTENT_FLAG_TREE_BLOCK)))) {
1348f82d1c7cSQu Wenruo 		extent_err(leaf, slot,
1349f82d1c7cSQu Wenruo 		"invalid extent flag, have 0x%llx expect 1 bit set in 0x%llx",
1350f82d1c7cSQu Wenruo 			flags, BTRFS_EXTENT_FLAG_DATA |
1351f82d1c7cSQu Wenruo 			BTRFS_EXTENT_FLAG_TREE_BLOCK);
1352f82d1c7cSQu Wenruo 		return -EUCLEAN;
1353f82d1c7cSQu Wenruo 	}
1354f82d1c7cSQu Wenruo 	is_tree_block = !!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK);
1355f82d1c7cSQu Wenruo 	if (is_tree_block) {
1356c7c01a4aSDavid Sterba 		if (unlikely(key->type == BTRFS_EXTENT_ITEM_KEY &&
1357c7c01a4aSDavid Sterba 			     key->offset != fs_info->nodesize)) {
1358f82d1c7cSQu Wenruo 			extent_err(leaf, slot,
1359f82d1c7cSQu Wenruo 				   "invalid extent length, have %llu expect %u",
1360f82d1c7cSQu Wenruo 				   key->offset, fs_info->nodesize);
1361f82d1c7cSQu Wenruo 			return -EUCLEAN;
1362f82d1c7cSQu Wenruo 		}
1363f82d1c7cSQu Wenruo 	} else {
1364c7c01a4aSDavid Sterba 		if (unlikely(key->type != BTRFS_EXTENT_ITEM_KEY)) {
1365f82d1c7cSQu Wenruo 			extent_err(leaf, slot,
1366f82d1c7cSQu Wenruo 			"invalid key type, have %u expect %u for data backref",
1367f82d1c7cSQu Wenruo 				   key->type, BTRFS_EXTENT_ITEM_KEY);
1368f82d1c7cSQu Wenruo 			return -EUCLEAN;
1369f82d1c7cSQu Wenruo 		}
1370c7c01a4aSDavid Sterba 		if (unlikely(!IS_ALIGNED(key->offset, fs_info->sectorsize))) {
1371f82d1c7cSQu Wenruo 			extent_err(leaf, slot,
1372f82d1c7cSQu Wenruo 			"invalid extent length, have %llu expect aligned to %u",
1373f82d1c7cSQu Wenruo 				   key->offset, fs_info->sectorsize);
1374f82d1c7cSQu Wenruo 			return -EUCLEAN;
1375f82d1c7cSQu Wenruo 		}
13760ebb6bbbSJosef Bacik 		if (unlikely(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
13770ebb6bbbSJosef Bacik 			extent_err(leaf, slot,
13780ebb6bbbSJosef Bacik 			"invalid extent flag, data has full backref set");
13790ebb6bbbSJosef Bacik 			return -EUCLEAN;
13800ebb6bbbSJosef Bacik 		}
1381f82d1c7cSQu Wenruo 	}
1382f82d1c7cSQu Wenruo 	ptr = (unsigned long)(struct btrfs_extent_item *)(ei + 1);
1383f82d1c7cSQu Wenruo 
1384f82d1c7cSQu Wenruo 	/* Check the special case of btrfs_tree_block_info */
1385f82d1c7cSQu Wenruo 	if (is_tree_block && key->type != BTRFS_METADATA_ITEM_KEY) {
1386f82d1c7cSQu Wenruo 		struct btrfs_tree_block_info *info;
1387f82d1c7cSQu Wenruo 
1388f82d1c7cSQu Wenruo 		info = (struct btrfs_tree_block_info *)ptr;
1389c7c01a4aSDavid Sterba 		if (unlikely(btrfs_tree_block_level(leaf, info) >= BTRFS_MAX_LEVEL)) {
1390f82d1c7cSQu Wenruo 			extent_err(leaf, slot,
1391f82d1c7cSQu Wenruo 			"invalid tree block info level, have %u expect [0, %u]",
1392f82d1c7cSQu Wenruo 				   btrfs_tree_block_level(leaf, info),
1393f82d1c7cSQu Wenruo 				   BTRFS_MAX_LEVEL - 1);
1394f82d1c7cSQu Wenruo 			return -EUCLEAN;
1395f82d1c7cSQu Wenruo 		}
1396f82d1c7cSQu Wenruo 		ptr = (unsigned long)(struct btrfs_tree_block_info *)(info + 1);
1397f82d1c7cSQu Wenruo 	}
1398f82d1c7cSQu Wenruo 
1399f82d1c7cSQu Wenruo 	/* Check inline refs */
1400f82d1c7cSQu Wenruo 	while (ptr < end) {
1401f82d1c7cSQu Wenruo 		struct btrfs_extent_inline_ref *iref;
1402f82d1c7cSQu Wenruo 		struct btrfs_extent_data_ref *dref;
1403f82d1c7cSQu Wenruo 		struct btrfs_shared_data_ref *sref;
1404f82d1c7cSQu Wenruo 		u64 dref_offset;
1405f82d1c7cSQu Wenruo 		u64 inline_offset;
1406f82d1c7cSQu Wenruo 		u8 inline_type;
1407f82d1c7cSQu Wenruo 
1408c7c01a4aSDavid Sterba 		if (unlikely(ptr + sizeof(*iref) > end)) {
1409f82d1c7cSQu Wenruo 			extent_err(leaf, slot,
1410f82d1c7cSQu Wenruo "inline ref item overflows extent item, ptr %lu iref size %zu end %lu",
1411f82d1c7cSQu Wenruo 				   ptr, sizeof(*iref), end);
1412f82d1c7cSQu Wenruo 			return -EUCLEAN;
1413f82d1c7cSQu Wenruo 		}
1414f82d1c7cSQu Wenruo 		iref = (struct btrfs_extent_inline_ref *)ptr;
1415f82d1c7cSQu Wenruo 		inline_type = btrfs_extent_inline_ref_type(leaf, iref);
1416f82d1c7cSQu Wenruo 		inline_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1417c7c01a4aSDavid Sterba 		if (unlikely(ptr + btrfs_extent_inline_ref_size(inline_type) > end)) {
1418f82d1c7cSQu Wenruo 			extent_err(leaf, slot,
1419f82d1c7cSQu Wenruo "inline ref item overflows extent item, ptr %lu iref size %u end %lu",
1420*ad854a86SChung-Chiang Cheng 				   ptr, btrfs_extent_inline_ref_size(inline_type), end);
1421f82d1c7cSQu Wenruo 			return -EUCLEAN;
1422f82d1c7cSQu Wenruo 		}
1423f82d1c7cSQu Wenruo 
1424f82d1c7cSQu Wenruo 		switch (inline_type) {
1425f82d1c7cSQu Wenruo 		/* inline_offset is subvolid of the owner, no need to check */
1426f82d1c7cSQu Wenruo 		case BTRFS_TREE_BLOCK_REF_KEY:
1427f82d1c7cSQu Wenruo 			inline_refs++;
1428f82d1c7cSQu Wenruo 			break;
1429f82d1c7cSQu Wenruo 		/* Contains parent bytenr */
1430f82d1c7cSQu Wenruo 		case BTRFS_SHARED_BLOCK_REF_KEY:
1431c7c01a4aSDavid Sterba 			if (unlikely(!IS_ALIGNED(inline_offset,
1432c7c01a4aSDavid Sterba 						 fs_info->sectorsize))) {
1433f82d1c7cSQu Wenruo 				extent_err(leaf, slot,
1434f82d1c7cSQu Wenruo 		"invalid tree parent bytenr, have %llu expect aligned to %u",
1435f82d1c7cSQu Wenruo 					   inline_offset, fs_info->sectorsize);
1436f82d1c7cSQu Wenruo 				return -EUCLEAN;
1437f82d1c7cSQu Wenruo 			}
1438f82d1c7cSQu Wenruo 			inline_refs++;
1439f82d1c7cSQu Wenruo 			break;
1440f82d1c7cSQu Wenruo 		/*
1441f82d1c7cSQu Wenruo 		 * Contains owner subvolid, owner key objectid, adjusted offset.
1442f82d1c7cSQu Wenruo 		 * The only obvious corruption can happen in that offset.
1443f82d1c7cSQu Wenruo 		 */
1444f82d1c7cSQu Wenruo 		case BTRFS_EXTENT_DATA_REF_KEY:
1445f82d1c7cSQu Wenruo 			dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1446f82d1c7cSQu Wenruo 			dref_offset = btrfs_extent_data_ref_offset(leaf, dref);
1447c7c01a4aSDavid Sterba 			if (unlikely(!IS_ALIGNED(dref_offset,
1448c7c01a4aSDavid Sterba 						 fs_info->sectorsize))) {
1449f82d1c7cSQu Wenruo 				extent_err(leaf, slot,
1450f82d1c7cSQu Wenruo 		"invalid data ref offset, have %llu expect aligned to %u",
1451f82d1c7cSQu Wenruo 					   dref_offset, fs_info->sectorsize);
1452f82d1c7cSQu Wenruo 				return -EUCLEAN;
1453f82d1c7cSQu Wenruo 			}
1454f82d1c7cSQu Wenruo 			inline_refs += btrfs_extent_data_ref_count(leaf, dref);
1455f82d1c7cSQu Wenruo 			break;
1456f82d1c7cSQu Wenruo 		/* Contains parent bytenr and ref count */
1457f82d1c7cSQu Wenruo 		case BTRFS_SHARED_DATA_REF_KEY:
1458f82d1c7cSQu Wenruo 			sref = (struct btrfs_shared_data_ref *)(iref + 1);
1459c7c01a4aSDavid Sterba 			if (unlikely(!IS_ALIGNED(inline_offset,
1460c7c01a4aSDavid Sterba 						 fs_info->sectorsize))) {
1461f82d1c7cSQu Wenruo 				extent_err(leaf, slot,
1462f82d1c7cSQu Wenruo 		"invalid data parent bytenr, have %llu expect aligned to %u",
1463f82d1c7cSQu Wenruo 					   inline_offset, fs_info->sectorsize);
1464f82d1c7cSQu Wenruo 				return -EUCLEAN;
1465f82d1c7cSQu Wenruo 			}
1466f82d1c7cSQu Wenruo 			inline_refs += btrfs_shared_data_ref_count(leaf, sref);
1467f82d1c7cSQu Wenruo 			break;
1468f82d1c7cSQu Wenruo 		default:
1469f82d1c7cSQu Wenruo 			extent_err(leaf, slot, "unknown inline ref type: %u",
1470f82d1c7cSQu Wenruo 				   inline_type);
1471f82d1c7cSQu Wenruo 			return -EUCLEAN;
1472f82d1c7cSQu Wenruo 		}
1473f82d1c7cSQu Wenruo 		ptr += btrfs_extent_inline_ref_size(inline_type);
1474f82d1c7cSQu Wenruo 	}
1475f82d1c7cSQu Wenruo 	/* No padding is allowed */
1476c7c01a4aSDavid Sterba 	if (unlikely(ptr != end)) {
1477f82d1c7cSQu Wenruo 		extent_err(leaf, slot,
1478f82d1c7cSQu Wenruo 			   "invalid extent item size, padding bytes found");
1479f82d1c7cSQu Wenruo 		return -EUCLEAN;
1480f82d1c7cSQu Wenruo 	}
1481f82d1c7cSQu Wenruo 
1482f82d1c7cSQu Wenruo 	/* Finally, check the inline refs against total refs */
1483c7c01a4aSDavid Sterba 	if (unlikely(inline_refs > total_refs)) {
1484f82d1c7cSQu Wenruo 		extent_err(leaf, slot,
1485f82d1c7cSQu Wenruo 			"invalid extent refs, have %llu expect >= inline %llu",
1486f82d1c7cSQu Wenruo 			   total_refs, inline_refs);
1487f82d1c7cSQu Wenruo 		return -EUCLEAN;
1488f82d1c7cSQu Wenruo 	}
1489899b7f69SJosef Bacik 
1490899b7f69SJosef Bacik 	if ((prev_key->type == BTRFS_EXTENT_ITEM_KEY) ||
1491899b7f69SJosef Bacik 	    (prev_key->type == BTRFS_METADATA_ITEM_KEY)) {
1492899b7f69SJosef Bacik 		u64 prev_end = prev_key->objectid;
1493899b7f69SJosef Bacik 
1494899b7f69SJosef Bacik 		if (prev_key->type == BTRFS_METADATA_ITEM_KEY)
1495899b7f69SJosef Bacik 			prev_end += fs_info->nodesize;
1496899b7f69SJosef Bacik 		else
1497899b7f69SJosef Bacik 			prev_end += prev_key->offset;
1498899b7f69SJosef Bacik 
1499899b7f69SJosef Bacik 		if (unlikely(prev_end > key->objectid)) {
1500899b7f69SJosef Bacik 			extent_err(leaf, slot,
1501899b7f69SJosef Bacik 	"previous extent [%llu %u %llu] overlaps current extent [%llu %u %llu]",
1502899b7f69SJosef Bacik 				   prev_key->objectid, prev_key->type,
1503899b7f69SJosef Bacik 				   prev_key->offset, key->objectid, key->type,
1504899b7f69SJosef Bacik 				   key->offset);
1505899b7f69SJosef Bacik 			return -EUCLEAN;
1506899b7f69SJosef Bacik 		}
1507899b7f69SJosef Bacik 	}
1508899b7f69SJosef Bacik 
1509f82d1c7cSQu Wenruo 	return 0;
1510f82d1c7cSQu Wenruo }
1511f82d1c7cSQu Wenruo 
check_simple_keyed_refs(struct extent_buffer * leaf,struct btrfs_key * key,int slot)1512e2406a6fSQu Wenruo static int check_simple_keyed_refs(struct extent_buffer *leaf,
1513e2406a6fSQu Wenruo 				   struct btrfs_key *key, int slot)
1514e2406a6fSQu Wenruo {
1515e2406a6fSQu Wenruo 	u32 expect_item_size = 0;
1516e2406a6fSQu Wenruo 
1517e2406a6fSQu Wenruo 	if (key->type == BTRFS_SHARED_DATA_REF_KEY)
1518e2406a6fSQu Wenruo 		expect_item_size = sizeof(struct btrfs_shared_data_ref);
1519e2406a6fSQu Wenruo 
15203212fa14SJosef Bacik 	if (unlikely(btrfs_item_size(leaf, slot) != expect_item_size)) {
1521e2406a6fSQu Wenruo 		generic_err(leaf, slot,
1522e2406a6fSQu Wenruo 		"invalid item size, have %u expect %u for key type %u",
15233212fa14SJosef Bacik 			    btrfs_item_size(leaf, slot),
1524e2406a6fSQu Wenruo 			    expect_item_size, key->type);
1525e2406a6fSQu Wenruo 		return -EUCLEAN;
1526e2406a6fSQu Wenruo 	}
1527c7c01a4aSDavid Sterba 	if (unlikely(!IS_ALIGNED(key->objectid, leaf->fs_info->sectorsize))) {
1528e2406a6fSQu Wenruo 		generic_err(leaf, slot,
1529e2406a6fSQu Wenruo "invalid key objectid for shared block ref, have %llu expect aligned to %u",
1530e2406a6fSQu Wenruo 			    key->objectid, leaf->fs_info->sectorsize);
1531e2406a6fSQu Wenruo 		return -EUCLEAN;
1532e2406a6fSQu Wenruo 	}
1533c7c01a4aSDavid Sterba 	if (unlikely(key->type != BTRFS_TREE_BLOCK_REF_KEY &&
1534c7c01a4aSDavid Sterba 		     !IS_ALIGNED(key->offset, leaf->fs_info->sectorsize))) {
1535e2406a6fSQu Wenruo 		extent_err(leaf, slot,
1536e2406a6fSQu Wenruo 		"invalid tree parent bytenr, have %llu expect aligned to %u",
1537e2406a6fSQu Wenruo 			   key->offset, leaf->fs_info->sectorsize);
1538e2406a6fSQu Wenruo 		return -EUCLEAN;
1539e2406a6fSQu Wenruo 	}
1540e2406a6fSQu Wenruo 	return 0;
1541e2406a6fSQu Wenruo }
1542e2406a6fSQu Wenruo 
check_extent_data_ref(struct extent_buffer * leaf,struct btrfs_key * key,int slot)15430785a9aaSQu Wenruo static int check_extent_data_ref(struct extent_buffer *leaf,
15440785a9aaSQu Wenruo 				 struct btrfs_key *key, int slot)
15450785a9aaSQu Wenruo {
15460785a9aaSQu Wenruo 	struct btrfs_extent_data_ref *dref;
15470785a9aaSQu Wenruo 	unsigned long ptr = btrfs_item_ptr_offset(leaf, slot);
15483212fa14SJosef Bacik 	const unsigned long end = ptr + btrfs_item_size(leaf, slot);
15490785a9aaSQu Wenruo 
15503212fa14SJosef Bacik 	if (unlikely(btrfs_item_size(leaf, slot) % sizeof(*dref) != 0)) {
15510785a9aaSQu Wenruo 		generic_err(leaf, slot,
15520785a9aaSQu Wenruo 	"invalid item size, have %u expect aligned to %zu for key type %u",
15533212fa14SJosef Bacik 			    btrfs_item_size(leaf, slot),
15540785a9aaSQu Wenruo 			    sizeof(*dref), key->type);
15556d06b0adSDavid Sterba 		return -EUCLEAN;
15560785a9aaSQu Wenruo 	}
1557c7c01a4aSDavid Sterba 	if (unlikely(!IS_ALIGNED(key->objectid, leaf->fs_info->sectorsize))) {
15580785a9aaSQu Wenruo 		generic_err(leaf, slot,
15590785a9aaSQu Wenruo "invalid key objectid for shared block ref, have %llu expect aligned to %u",
15600785a9aaSQu Wenruo 			    key->objectid, leaf->fs_info->sectorsize);
15610785a9aaSQu Wenruo 		return -EUCLEAN;
15620785a9aaSQu Wenruo 	}
15630785a9aaSQu Wenruo 	for (; ptr < end; ptr += sizeof(*dref)) {
15640785a9aaSQu Wenruo 		u64 offset;
15650785a9aaSQu Wenruo 
15661119a72eSJosef Bacik 		/*
15671119a72eSJosef Bacik 		 * We cannot check the extent_data_ref hash due to possible
15681119a72eSJosef Bacik 		 * overflow from the leaf due to hash collisions.
15691119a72eSJosef Bacik 		 */
15700785a9aaSQu Wenruo 		dref = (struct btrfs_extent_data_ref *)ptr;
15710785a9aaSQu Wenruo 		offset = btrfs_extent_data_ref_offset(leaf, dref);
1572c7c01a4aSDavid Sterba 		if (unlikely(!IS_ALIGNED(offset, leaf->fs_info->sectorsize))) {
15730785a9aaSQu Wenruo 			extent_err(leaf, slot,
15740785a9aaSQu Wenruo 	"invalid extent data backref offset, have %llu expect aligned to %u",
15750785a9aaSQu Wenruo 				   offset, leaf->fs_info->sectorsize);
15766d06b0adSDavid Sterba 			return -EUCLEAN;
15770785a9aaSQu Wenruo 		}
15780785a9aaSQu Wenruo 	}
15790785a9aaSQu Wenruo 	return 0;
15800785a9aaSQu Wenruo }
15810785a9aaSQu Wenruo 
1582c3053ebbSQu Wenruo #define inode_ref_err(eb, slot, fmt, args...)			\
1583c3053ebbSQu Wenruo 	inode_item_err(eb, slot, fmt, ##args)
check_inode_ref(struct extent_buffer * leaf,struct btrfs_key * key,struct btrfs_key * prev_key,int slot)158471bf92a9SQu Wenruo static int check_inode_ref(struct extent_buffer *leaf,
158571bf92a9SQu Wenruo 			   struct btrfs_key *key, struct btrfs_key *prev_key,
158671bf92a9SQu Wenruo 			   int slot)
158771bf92a9SQu Wenruo {
158871bf92a9SQu Wenruo 	struct btrfs_inode_ref *iref;
158971bf92a9SQu Wenruo 	unsigned long ptr;
159071bf92a9SQu Wenruo 	unsigned long end;
159171bf92a9SQu Wenruo 
1592c7c01a4aSDavid Sterba 	if (unlikely(!check_prev_ino(leaf, key, slot, prev_key)))
159380d7fd1eSQu Wenruo 		return -EUCLEAN;
159471bf92a9SQu Wenruo 	/* namelen can't be 0, so item_size == sizeof() is also invalid */
15953212fa14SJosef Bacik 	if (unlikely(btrfs_item_size(leaf, slot) <= sizeof(*iref))) {
1596c3053ebbSQu Wenruo 		inode_ref_err(leaf, slot,
159771bf92a9SQu Wenruo 			"invalid item size, have %u expect (%zu, %u)",
15983212fa14SJosef Bacik 			btrfs_item_size(leaf, slot),
159971bf92a9SQu Wenruo 			sizeof(*iref), BTRFS_LEAF_DATA_SIZE(leaf->fs_info));
160071bf92a9SQu Wenruo 		return -EUCLEAN;
160171bf92a9SQu Wenruo 	}
160271bf92a9SQu Wenruo 
160371bf92a9SQu Wenruo 	ptr = btrfs_item_ptr_offset(leaf, slot);
16043212fa14SJosef Bacik 	end = ptr + btrfs_item_size(leaf, slot);
160571bf92a9SQu Wenruo 	while (ptr < end) {
160671bf92a9SQu Wenruo 		u16 namelen;
160771bf92a9SQu Wenruo 
1608c7c01a4aSDavid Sterba 		if (unlikely(ptr + sizeof(iref) > end)) {
1609c3053ebbSQu Wenruo 			inode_ref_err(leaf, slot,
161071bf92a9SQu Wenruo 			"inode ref overflow, ptr %lu end %lu inode_ref_size %zu",
161171bf92a9SQu Wenruo 				ptr, end, sizeof(iref));
161271bf92a9SQu Wenruo 			return -EUCLEAN;
161371bf92a9SQu Wenruo 		}
161471bf92a9SQu Wenruo 
161571bf92a9SQu Wenruo 		iref = (struct btrfs_inode_ref *)ptr;
161671bf92a9SQu Wenruo 		namelen = btrfs_inode_ref_name_len(leaf, iref);
1617c7c01a4aSDavid Sterba 		if (unlikely(ptr + sizeof(*iref) + namelen > end)) {
1618c3053ebbSQu Wenruo 			inode_ref_err(leaf, slot,
161971bf92a9SQu Wenruo 				"inode ref overflow, ptr %lu end %lu namelen %u",
162071bf92a9SQu Wenruo 				ptr, end, namelen);
162171bf92a9SQu Wenruo 			return -EUCLEAN;
162271bf92a9SQu Wenruo 		}
162371bf92a9SQu Wenruo 
162471bf92a9SQu Wenruo 		/*
162571bf92a9SQu Wenruo 		 * NOTE: In theory we should record all found index numbers
162671bf92a9SQu Wenruo 		 * to find any duplicated indexes, but that will be too time
162771bf92a9SQu Wenruo 		 * consuming for inodes with too many hard links.
162871bf92a9SQu Wenruo 		 */
162971bf92a9SQu Wenruo 		ptr += sizeof(*iref) + namelen;
163071bf92a9SQu Wenruo 	}
163171bf92a9SQu Wenruo 	return 0;
163271bf92a9SQu Wenruo }
163371bf92a9SQu Wenruo 
163482fc28fbSQu Wenruo /*
1635557ea5ddSQu Wenruo  * Common point to switch the item-specific validation.
1636557ea5ddSQu Wenruo  */
check_leaf_item(struct extent_buffer * leaf,struct btrfs_key * key,int slot,struct btrfs_key * prev_key)1637c8d54215SJosef Bacik static enum btrfs_tree_block_status check_leaf_item(struct extent_buffer *leaf,
1638c8d54215SJosef Bacik 						    struct btrfs_key *key,
1639c8d54215SJosef Bacik 						    int slot,
16404e9845efSFilipe Manana 						    struct btrfs_key *prev_key)
1641557ea5ddSQu Wenruo {
1642557ea5ddSQu Wenruo 	int ret = 0;
1643075cb3c7SQu Wenruo 	struct btrfs_chunk *chunk;
1644557ea5ddSQu Wenruo 
1645557ea5ddSQu Wenruo 	switch (key->type) {
1646557ea5ddSQu Wenruo 	case BTRFS_EXTENT_DATA_KEY:
16474e9845efSFilipe Manana 		ret = check_extent_data_item(leaf, key, slot, prev_key);
1648557ea5ddSQu Wenruo 		break;
1649557ea5ddSQu Wenruo 	case BTRFS_EXTENT_CSUM_KEY:
1650ad1d8c43SFilipe Manana 		ret = check_csum_item(leaf, key, slot, prev_key);
1651557ea5ddSQu Wenruo 		break;
1652ad7b0368SQu Wenruo 	case BTRFS_DIR_ITEM_KEY:
1653ad7b0368SQu Wenruo 	case BTRFS_DIR_INDEX_KEY:
1654ad7b0368SQu Wenruo 	case BTRFS_XATTR_ITEM_KEY:
1655c18679ebSQu Wenruo 		ret = check_dir_item(leaf, key, prev_key, slot);
1656ad7b0368SQu Wenruo 		break;
165771bf92a9SQu Wenruo 	case BTRFS_INODE_REF_KEY:
165871bf92a9SQu Wenruo 		ret = check_inode_ref(leaf, key, prev_key, slot);
165971bf92a9SQu Wenruo 		break;
1660fce466eaSQu Wenruo 	case BTRFS_BLOCK_GROUP_ITEM_KEY:
1661af60ce2bSDavid Sterba 		ret = check_block_group_item(leaf, key, slot);
1662fce466eaSQu Wenruo 		break;
1663075cb3c7SQu Wenruo 	case BTRFS_CHUNK_ITEM_KEY:
1664075cb3c7SQu Wenruo 		chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
1665f6d2a5c2SQu Wenruo 		ret = check_leaf_chunk_item(leaf, chunk, key, slot);
1666075cb3c7SQu Wenruo 		break;
1667ab4ba2e1SQu Wenruo 	case BTRFS_DEV_ITEM_KEY:
1668412a2312SDavid Sterba 		ret = check_dev_item(leaf, key, slot);
1669ab4ba2e1SQu Wenruo 		break;
1670496245caSQu Wenruo 	case BTRFS_INODE_ITEM_KEY:
167139e57f49SDavid Sterba 		ret = check_inode_item(leaf, key, slot);
1672496245caSQu Wenruo 		break;
1673259ee775SQu Wenruo 	case BTRFS_ROOT_ITEM_KEY:
1674259ee775SQu Wenruo 		ret = check_root_item(leaf, key, slot);
1675259ee775SQu Wenruo 		break;
1676f82d1c7cSQu Wenruo 	case BTRFS_EXTENT_ITEM_KEY:
1677f82d1c7cSQu Wenruo 	case BTRFS_METADATA_ITEM_KEY:
1678899b7f69SJosef Bacik 		ret = check_extent_item(leaf, key, slot, prev_key);
1679f82d1c7cSQu Wenruo 		break;
1680e2406a6fSQu Wenruo 	case BTRFS_TREE_BLOCK_REF_KEY:
1681e2406a6fSQu Wenruo 	case BTRFS_SHARED_DATA_REF_KEY:
1682e2406a6fSQu Wenruo 	case BTRFS_SHARED_BLOCK_REF_KEY:
1683e2406a6fSQu Wenruo 		ret = check_simple_keyed_refs(leaf, key, slot);
1684e2406a6fSQu Wenruo 		break;
16850785a9aaSQu Wenruo 	case BTRFS_EXTENT_DATA_REF_KEY:
16860785a9aaSQu Wenruo 		ret = check_extent_data_ref(leaf, key, slot);
16870785a9aaSQu Wenruo 		break;
1688557ea5ddSQu Wenruo 	}
1689c8d54215SJosef Bacik 
1690c8d54215SJosef Bacik 	if (ret)
1691c8d54215SJosef Bacik 		return BTRFS_TREE_BLOCK_INVALID_ITEM;
1692c8d54215SJosef Bacik 	return BTRFS_TREE_BLOCK_CLEAN;
1693557ea5ddSQu Wenruo }
1694557ea5ddSQu Wenruo 
__btrfs_check_leaf(struct extent_buffer * leaf)1695924452c8SJosef Bacik enum btrfs_tree_block_status __btrfs_check_leaf(struct extent_buffer *leaf)
1696557ea5ddSQu Wenruo {
1697e2ccd361SDavid Sterba 	struct btrfs_fs_info *fs_info = leaf->fs_info;
1698557ea5ddSQu Wenruo 	/* No valid key type is 0, so all key should be larger than this key */
1699557ea5ddSQu Wenruo 	struct btrfs_key prev_key = {0, 0, 0};
1700557ea5ddSQu Wenruo 	struct btrfs_key key;
1701557ea5ddSQu Wenruo 	u32 nritems = btrfs_header_nritems(leaf);
1702557ea5ddSQu Wenruo 	int slot;
1703557ea5ddSQu Wenruo 
1704c7c01a4aSDavid Sterba 	if (unlikely(btrfs_header_level(leaf) != 0)) {
170586a6be3aSDavid Sterba 		generic_err(leaf, 0,
1706f556faa4SQu Wenruo 			"invalid level for leaf, have %d expect 0",
1707f556faa4SQu Wenruo 			btrfs_header_level(leaf));
1708924452c8SJosef Bacik 		return BTRFS_TREE_BLOCK_INVALID_LEVEL;
1709f556faa4SQu Wenruo 	}
1710f556faa4SQu Wenruo 
1711557ea5ddSQu Wenruo 	/*
1712557ea5ddSQu Wenruo 	 * Extent buffers from a relocation tree have a owner field that
1713557ea5ddSQu Wenruo 	 * corresponds to the subvolume tree they are based on. So just from an
1714557ea5ddSQu Wenruo 	 * extent buffer alone we can not find out what is the id of the
1715557ea5ddSQu Wenruo 	 * corresponding subvolume tree, so we can not figure out if the extent
1716557ea5ddSQu Wenruo 	 * buffer corresponds to the root of the relocation tree or not. So
1717557ea5ddSQu Wenruo 	 * skip this check for relocation trees.
1718557ea5ddSQu Wenruo 	 */
1719557ea5ddSQu Wenruo 	if (nritems == 0 && !btrfs_header_flag(leaf, BTRFS_HEADER_FLAG_RELOC)) {
1720ba480dd4SQu Wenruo 		u64 owner = btrfs_header_owner(leaf);
1721557ea5ddSQu Wenruo 
1722ba480dd4SQu Wenruo 		/* These trees must never be empty */
1723c7c01a4aSDavid Sterba 		if (unlikely(owner == BTRFS_ROOT_TREE_OBJECTID ||
1724ba480dd4SQu Wenruo 			     owner == BTRFS_CHUNK_TREE_OBJECTID ||
1725ba480dd4SQu Wenruo 			     owner == BTRFS_DEV_TREE_OBJECTID ||
1726ba480dd4SQu Wenruo 			     owner == BTRFS_FS_TREE_OBJECTID ||
1727c7c01a4aSDavid Sterba 			     owner == BTRFS_DATA_RELOC_TREE_OBJECTID)) {
172886a6be3aSDavid Sterba 			generic_err(leaf, 0,
1729ba480dd4SQu Wenruo 			"invalid root, root %llu must never be empty",
1730ba480dd4SQu Wenruo 				    owner);
1731924452c8SJosef Bacik 			return BTRFS_TREE_BLOCK_INVALID_NRITEMS;
1732ba480dd4SQu Wenruo 		}
1733c2fa821cSJosef Bacik 
173462fdaa52SQu Wenruo 		/* Unknown tree */
1735c7c01a4aSDavid Sterba 		if (unlikely(owner == 0)) {
173662fdaa52SQu Wenruo 			generic_err(leaf, 0,
173762fdaa52SQu Wenruo 				"invalid owner, root 0 is not defined");
1738924452c8SJosef Bacik 			return BTRFS_TREE_BLOCK_INVALID_OWNER;
173962fdaa52SQu Wenruo 		}
1740c2fa821cSJosef Bacik 
1741c2fa821cSJosef Bacik 		/* EXTENT_TREE_V2 can have empty extent trees. */
1742c2fa821cSJosef Bacik 		if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2))
1743924452c8SJosef Bacik 			return BTRFS_TREE_BLOCK_CLEAN;
1744c2fa821cSJosef Bacik 
1745c2fa821cSJosef Bacik 		if (unlikely(owner == BTRFS_EXTENT_TREE_OBJECTID)) {
1746c2fa821cSJosef Bacik 			generic_err(leaf, 0,
1747c2fa821cSJosef Bacik 			"invalid root, root %llu must never be empty",
1748c2fa821cSJosef Bacik 				    owner);
1749924452c8SJosef Bacik 			return BTRFS_TREE_BLOCK_INVALID_NRITEMS;
1750c2fa821cSJosef Bacik 		}
1751c2fa821cSJosef Bacik 
1752924452c8SJosef Bacik 		return BTRFS_TREE_BLOCK_CLEAN;
1753557ea5ddSQu Wenruo 	}
1754557ea5ddSQu Wenruo 
1755c7c01a4aSDavid Sterba 	if (unlikely(nritems == 0))
1756924452c8SJosef Bacik 		return BTRFS_TREE_BLOCK_CLEAN;
1757557ea5ddSQu Wenruo 
1758557ea5ddSQu Wenruo 	/*
1759557ea5ddSQu Wenruo 	 * Check the following things to make sure this is a good leaf, and
1760557ea5ddSQu Wenruo 	 * leaf users won't need to bother with similar sanity checks:
1761557ea5ddSQu Wenruo 	 *
1762557ea5ddSQu Wenruo 	 * 1) key ordering
1763557ea5ddSQu Wenruo 	 * 2) item offset and size
1764557ea5ddSQu Wenruo 	 *    No overlap, no hole, all inside the leaf.
1765557ea5ddSQu Wenruo 	 * 3) item content
1766557ea5ddSQu Wenruo 	 *    If possible, do comprehensive sanity check.
1767557ea5ddSQu Wenruo 	 *    NOTE: All checks must only rely on the item data itself.
1768557ea5ddSQu Wenruo 	 */
1769557ea5ddSQu Wenruo 	for (slot = 0; slot < nritems; slot++) {
1770557ea5ddSQu Wenruo 		u32 item_end_expected;
1771a6ab66ebSSu Yue 		u64 item_data_end;
1772557ea5ddSQu Wenruo 
1773557ea5ddSQu Wenruo 		btrfs_item_key_to_cpu(leaf, &key, slot);
1774557ea5ddSQu Wenruo 
1775557ea5ddSQu Wenruo 		/* Make sure the keys are in the right order */
1776c7c01a4aSDavid Sterba 		if (unlikely(btrfs_comp_cpu_keys(&prev_key, &key) >= 0)) {
177786a6be3aSDavid Sterba 			generic_err(leaf, slot,
1778478d01b3SQu Wenruo 	"bad key order, prev (%llu %u %llu) current (%llu %u %llu)",
1779478d01b3SQu Wenruo 				prev_key.objectid, prev_key.type,
1780478d01b3SQu Wenruo 				prev_key.offset, key.objectid, key.type,
1781478d01b3SQu Wenruo 				key.offset);
1782924452c8SJosef Bacik 			return BTRFS_TREE_BLOCK_BAD_KEY_ORDER;
1783557ea5ddSQu Wenruo 		}
1784557ea5ddSQu Wenruo 
1785a6ab66ebSSu Yue 		item_data_end = (u64)btrfs_item_offset(leaf, slot) +
1786a6ab66ebSSu Yue 				btrfs_item_size(leaf, slot);
1787557ea5ddSQu Wenruo 		/*
1788557ea5ddSQu Wenruo 		 * Make sure the offset and ends are right, remember that the
1789557ea5ddSQu Wenruo 		 * item data starts at the end of the leaf and grows towards the
1790557ea5ddSQu Wenruo 		 * front.
1791557ea5ddSQu Wenruo 		 */
1792557ea5ddSQu Wenruo 		if (slot == 0)
1793557ea5ddSQu Wenruo 			item_end_expected = BTRFS_LEAF_DATA_SIZE(fs_info);
1794557ea5ddSQu Wenruo 		else
17953212fa14SJosef Bacik 			item_end_expected = btrfs_item_offset(leaf,
1796557ea5ddSQu Wenruo 								 slot - 1);
1797a6ab66ebSSu Yue 		if (unlikely(item_data_end != item_end_expected)) {
179886a6be3aSDavid Sterba 			generic_err(leaf, slot,
1799a6ab66ebSSu Yue 				"unexpected item end, have %llu expect %u",
1800a6ab66ebSSu Yue 				item_data_end, item_end_expected);
1801924452c8SJosef Bacik 			return BTRFS_TREE_BLOCK_INVALID_OFFSETS;
1802557ea5ddSQu Wenruo 		}
1803557ea5ddSQu Wenruo 
1804557ea5ddSQu Wenruo 		/*
1805557ea5ddSQu Wenruo 		 * Check to make sure that we don't point outside of the leaf,
1806557ea5ddSQu Wenruo 		 * just in case all the items are consistent to each other, but
1807557ea5ddSQu Wenruo 		 * all point outside of the leaf.
1808557ea5ddSQu Wenruo 		 */
1809a6ab66ebSSu Yue 		if (unlikely(item_data_end > BTRFS_LEAF_DATA_SIZE(fs_info))) {
181086a6be3aSDavid Sterba 			generic_err(leaf, slot,
1811a6ab66ebSSu Yue 			"slot end outside of leaf, have %llu expect range [0, %u]",
1812a6ab66ebSSu Yue 				item_data_end, BTRFS_LEAF_DATA_SIZE(fs_info));
1813924452c8SJosef Bacik 			return BTRFS_TREE_BLOCK_INVALID_OFFSETS;
1814557ea5ddSQu Wenruo 		}
1815557ea5ddSQu Wenruo 
1816557ea5ddSQu Wenruo 		/* Also check if the item pointer overlaps with btrfs item. */
1817c7c01a4aSDavid Sterba 		if (unlikely(btrfs_item_ptr_offset(leaf, slot) <
181842c9419aSJosef Bacik 			     btrfs_item_nr_offset(leaf, slot) + sizeof(struct btrfs_item))) {
181986a6be3aSDavid Sterba 			generic_err(leaf, slot,
1820478d01b3SQu Wenruo 		"slot overlaps with its data, item end %lu data start %lu",
182142c9419aSJosef Bacik 				btrfs_item_nr_offset(leaf, slot) +
1822478d01b3SQu Wenruo 				sizeof(struct btrfs_item),
1823478d01b3SQu Wenruo 				btrfs_item_ptr_offset(leaf, slot));
1824924452c8SJosef Bacik 			return BTRFS_TREE_BLOCK_INVALID_OFFSETS;
1825557ea5ddSQu Wenruo 		}
1826557ea5ddSQu Wenruo 
182785d8a826SJosef Bacik 		/*
182885d8a826SJosef Bacik 		 * We only want to do this if WRITTEN is set, otherwise the leaf
182985d8a826SJosef Bacik 		 * may be in some intermediate state and won't appear valid.
183085d8a826SJosef Bacik 		 */
183185d8a826SJosef Bacik 		if (btrfs_header_flag(leaf, BTRFS_HEADER_FLAG_WRITTEN)) {
1832c8d54215SJosef Bacik 			enum btrfs_tree_block_status ret;
1833c8d54215SJosef Bacik 
183469fc6cbbSQu Wenruo 			/*
183569fc6cbbSQu Wenruo 			 * Check if the item size and content meet other
183669fc6cbbSQu Wenruo 			 * criteria
183769fc6cbbSQu Wenruo 			 */
18384e9845efSFilipe Manana 			ret = check_leaf_item(leaf, &key, slot, &prev_key);
1839c8d54215SJosef Bacik 			if (unlikely(ret != BTRFS_TREE_BLOCK_CLEAN))
1840557ea5ddSQu Wenruo 				return ret;
184169fc6cbbSQu Wenruo 		}
1842557ea5ddSQu Wenruo 
1843557ea5ddSQu Wenruo 		prev_key.objectid = key.objectid;
1844557ea5ddSQu Wenruo 		prev_key.type = key.type;
1845557ea5ddSQu Wenruo 		prev_key.offset = key.offset;
1846557ea5ddSQu Wenruo 	}
1847557ea5ddSQu Wenruo 
1848924452c8SJosef Bacik 	return BTRFS_TREE_BLOCK_CLEAN;
1849924452c8SJosef Bacik }
1850924452c8SJosef Bacik 
btrfs_check_leaf(struct extent_buffer * leaf)1851924452c8SJosef Bacik int btrfs_check_leaf(struct extent_buffer *leaf)
1852924452c8SJosef Bacik {
1853924452c8SJosef Bacik 	enum btrfs_tree_block_status ret;
1854924452c8SJosef Bacik 
1855924452c8SJosef Bacik 	ret = __btrfs_check_leaf(leaf);
1856924452c8SJosef Bacik 	if (unlikely(ret != BTRFS_TREE_BLOCK_CLEAN))
1857924452c8SJosef Bacik 		return -EUCLEAN;
1858557ea5ddSQu Wenruo 	return 0;
1859557ea5ddSQu Wenruo }
186085d8a826SJosef Bacik ALLOW_ERROR_INJECTION(btrfs_check_leaf, ERRNO);
1861557ea5ddSQu Wenruo 
__btrfs_check_node(struct extent_buffer * node)1862c26fa931SJosef Bacik enum btrfs_tree_block_status __btrfs_check_node(struct extent_buffer *node)
1863557ea5ddSQu Wenruo {
1864813fd1dcSDavid Sterba 	struct btrfs_fs_info *fs_info = node->fs_info;
1865557ea5ddSQu Wenruo 	unsigned long nr = btrfs_header_nritems(node);
1866557ea5ddSQu Wenruo 	struct btrfs_key key, next_key;
1867557ea5ddSQu Wenruo 	int slot;
1868f556faa4SQu Wenruo 	int level = btrfs_header_level(node);
1869557ea5ddSQu Wenruo 	u64 bytenr;
1870557ea5ddSQu Wenruo 
1871c7c01a4aSDavid Sterba 	if (unlikely(level <= 0 || level >= BTRFS_MAX_LEVEL)) {
187286a6be3aSDavid Sterba 		generic_err(node, 0,
1873f556faa4SQu Wenruo 			"invalid level for node, have %d expect [1, %d]",
1874f556faa4SQu Wenruo 			level, BTRFS_MAX_LEVEL - 1);
1875c26fa931SJosef Bacik 		return BTRFS_TREE_BLOCK_INVALID_LEVEL;
1876f556faa4SQu Wenruo 	}
1877c7c01a4aSDavid Sterba 	if (unlikely(nr == 0 || nr > BTRFS_NODEPTRS_PER_BLOCK(fs_info))) {
18782f659546SQu Wenruo 		btrfs_crit(fs_info,
1879bba4f298SQu Wenruo "corrupt node: root=%llu block=%llu, nritems too %s, have %lu expect range [1,%u]",
18802f659546SQu Wenruo 			   btrfs_header_owner(node), node->start,
1881bba4f298SQu Wenruo 			   nr == 0 ? "small" : "large", nr,
18822f659546SQu Wenruo 			   BTRFS_NODEPTRS_PER_BLOCK(fs_info));
1883c26fa931SJosef Bacik 		return BTRFS_TREE_BLOCK_INVALID_NRITEMS;
1884557ea5ddSQu Wenruo 	}
1885557ea5ddSQu Wenruo 
1886557ea5ddSQu Wenruo 	for (slot = 0; slot < nr - 1; slot++) {
1887557ea5ddSQu Wenruo 		bytenr = btrfs_node_blockptr(node, slot);
1888557ea5ddSQu Wenruo 		btrfs_node_key_to_cpu(node, &key, slot);
1889557ea5ddSQu Wenruo 		btrfs_node_key_to_cpu(node, &next_key, slot + 1);
1890557ea5ddSQu Wenruo 
1891c7c01a4aSDavid Sterba 		if (unlikely(!bytenr)) {
189286a6be3aSDavid Sterba 			generic_err(node, slot,
1893bba4f298SQu Wenruo 				"invalid NULL node pointer");
1894c26fa931SJosef Bacik 			return BTRFS_TREE_BLOCK_INVALID_BLOCKPTR;
1895bba4f298SQu Wenruo 		}
1896c7c01a4aSDavid Sterba 		if (unlikely(!IS_ALIGNED(bytenr, fs_info->sectorsize))) {
189786a6be3aSDavid Sterba 			generic_err(node, slot,
1898bba4f298SQu Wenruo 			"unaligned pointer, have %llu should be aligned to %u",
18992f659546SQu Wenruo 				bytenr, fs_info->sectorsize);
1900c26fa931SJosef Bacik 			return BTRFS_TREE_BLOCK_INVALID_BLOCKPTR;
1901557ea5ddSQu Wenruo 		}
1902557ea5ddSQu Wenruo 
1903c7c01a4aSDavid Sterba 		if (unlikely(btrfs_comp_cpu_keys(&key, &next_key) >= 0)) {
190486a6be3aSDavid Sterba 			generic_err(node, slot,
1905bba4f298SQu Wenruo 	"bad key order, current (%llu %u %llu) next (%llu %u %llu)",
1906bba4f298SQu Wenruo 				key.objectid, key.type, key.offset,
1907bba4f298SQu Wenruo 				next_key.objectid, next_key.type,
1908bba4f298SQu Wenruo 				next_key.offset);
1909c26fa931SJosef Bacik 			return BTRFS_TREE_BLOCK_BAD_KEY_ORDER;
1910557ea5ddSQu Wenruo 		}
1911557ea5ddSQu Wenruo 	}
1912c26fa931SJosef Bacik 	return BTRFS_TREE_BLOCK_CLEAN;
1913c26fa931SJosef Bacik }
1914c26fa931SJosef Bacik 
btrfs_check_node(struct extent_buffer * node)1915c26fa931SJosef Bacik int btrfs_check_node(struct extent_buffer *node)
1916c26fa931SJosef Bacik {
1917c26fa931SJosef Bacik 	enum btrfs_tree_block_status ret;
1918c26fa931SJosef Bacik 
1919c26fa931SJosef Bacik 	ret = __btrfs_check_node(node);
1920c26fa931SJosef Bacik 	if (unlikely(ret != BTRFS_TREE_BLOCK_CLEAN))
1921c26fa931SJosef Bacik 		return -EUCLEAN;
1922c26fa931SJosef Bacik 	return 0;
1923557ea5ddSQu Wenruo }
192402529d7aSQu Wenruo ALLOW_ERROR_INJECTION(btrfs_check_node, ERRNO);
192588c602abSQu Wenruo 
btrfs_check_eb_owner(const struct extent_buffer * eb,u64 root_owner)192688c602abSQu Wenruo int btrfs_check_eb_owner(const struct extent_buffer *eb, u64 root_owner)
192788c602abSQu Wenruo {
192888c602abSQu Wenruo 	const bool is_subvol = is_fstree(root_owner);
192988c602abSQu Wenruo 	const u64 eb_owner = btrfs_header_owner(eb);
193088c602abSQu Wenruo 
193188c602abSQu Wenruo 	/*
193288c602abSQu Wenruo 	 * Skip dummy fs, as selftests don't create unique ebs for each dummy
193388c602abSQu Wenruo 	 * root.
193488c602abSQu Wenruo 	 */
193588c602abSQu Wenruo 	if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &eb->fs_info->fs_state))
193688c602abSQu Wenruo 		return 0;
193788c602abSQu Wenruo 	/*
193888c602abSQu Wenruo 	 * There are several call sites (backref walking, qgroup, and data
193988c602abSQu Wenruo 	 * reloc) passing 0 as @root_owner, as they are not holding the
194088c602abSQu Wenruo 	 * tree root.  In that case, we can not do a reliable ownership check,
194188c602abSQu Wenruo 	 * so just exit.
194288c602abSQu Wenruo 	 */
194388c602abSQu Wenruo 	if (root_owner == 0)
194488c602abSQu Wenruo 		return 0;
194588c602abSQu Wenruo 	/*
194688c602abSQu Wenruo 	 * These trees use key.offset as their owner, our callers don't have
194788c602abSQu Wenruo 	 * the extra capacity to pass key.offset here.  So we just skip them.
194888c602abSQu Wenruo 	 */
194988c602abSQu Wenruo 	if (root_owner == BTRFS_TREE_LOG_OBJECTID ||
195088c602abSQu Wenruo 	    root_owner == BTRFS_TREE_RELOC_OBJECTID)
195188c602abSQu Wenruo 		return 0;
195288c602abSQu Wenruo 
195388c602abSQu Wenruo 	if (!is_subvol) {
195488c602abSQu Wenruo 		/* For non-subvolume trees, the eb owner should match root owner */
195588c602abSQu Wenruo 		if (unlikely(root_owner != eb_owner)) {
195688c602abSQu Wenruo 			btrfs_crit(eb->fs_info,
195788c602abSQu Wenruo "corrupted %s, root=%llu block=%llu owner mismatch, have %llu expect %llu",
195888c602abSQu Wenruo 				btrfs_header_level(eb) == 0 ? "leaf" : "node",
195988c602abSQu Wenruo 				root_owner, btrfs_header_bytenr(eb), eb_owner,
196088c602abSQu Wenruo 				root_owner);
196188c602abSQu Wenruo 			return -EUCLEAN;
196288c602abSQu Wenruo 		}
196388c602abSQu Wenruo 		return 0;
196488c602abSQu Wenruo 	}
196588c602abSQu Wenruo 
196688c602abSQu Wenruo 	/*
196788c602abSQu Wenruo 	 * For subvolume trees, owners can mismatch, but they should all belong
196888c602abSQu Wenruo 	 * to subvolume trees.
196988c602abSQu Wenruo 	 */
197088c602abSQu Wenruo 	if (unlikely(is_subvol != is_fstree(eb_owner))) {
197188c602abSQu Wenruo 		btrfs_crit(eb->fs_info,
197288c602abSQu Wenruo "corrupted %s, root=%llu block=%llu owner mismatch, have %llu expect [%llu, %llu]",
197388c602abSQu Wenruo 			btrfs_header_level(eb) == 0 ? "leaf" : "node",
197488c602abSQu Wenruo 			root_owner, btrfs_header_bytenr(eb), eb_owner,
197588c602abSQu Wenruo 			BTRFS_FIRST_FREE_OBJECTID, BTRFS_LAST_FREE_OBJECTID);
197688c602abSQu Wenruo 		return -EUCLEAN;
197788c602abSQu Wenruo 	}
197888c602abSQu Wenruo 	return 0;
197988c602abSQu Wenruo }
19802cac5af1SJosef Bacik 
btrfs_verify_level_key(struct extent_buffer * eb,int level,struct btrfs_key * first_key,u64 parent_transid)19812cac5af1SJosef Bacik int btrfs_verify_level_key(struct extent_buffer *eb, int level,
19822cac5af1SJosef Bacik 			   struct btrfs_key *first_key, u64 parent_transid)
19832cac5af1SJosef Bacik {
19842cac5af1SJosef Bacik 	struct btrfs_fs_info *fs_info = eb->fs_info;
19852cac5af1SJosef Bacik 	int found_level;
19862cac5af1SJosef Bacik 	struct btrfs_key found_key;
19872cac5af1SJosef Bacik 	int ret;
19882cac5af1SJosef Bacik 
19892cac5af1SJosef Bacik 	found_level = btrfs_header_level(eb);
19902cac5af1SJosef Bacik 	if (found_level != level) {
19912cac5af1SJosef Bacik 		WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG),
19922cac5af1SJosef Bacik 		     KERN_ERR "BTRFS: tree level check failed\n");
19932cac5af1SJosef Bacik 		btrfs_err(fs_info,
19942cac5af1SJosef Bacik "tree level mismatch detected, bytenr=%llu level expected=%u has=%u",
19952cac5af1SJosef Bacik 			  eb->start, level, found_level);
19962cac5af1SJosef Bacik 		return -EIO;
19972cac5af1SJosef Bacik 	}
19982cac5af1SJosef Bacik 
19992cac5af1SJosef Bacik 	if (!first_key)
20002cac5af1SJosef Bacik 		return 0;
20012cac5af1SJosef Bacik 
20022cac5af1SJosef Bacik 	/*
20032cac5af1SJosef Bacik 	 * For live tree block (new tree blocks in current transaction),
20042cac5af1SJosef Bacik 	 * we need proper lock context to avoid race, which is impossible here.
20052cac5af1SJosef Bacik 	 * So we only checks tree blocks which is read from disk, whose
20062cac5af1SJosef Bacik 	 * generation <= fs_info->last_trans_committed.
20072cac5af1SJosef Bacik 	 */
20082cac5af1SJosef Bacik 	if (btrfs_header_generation(eb) > fs_info->last_trans_committed)
20092cac5af1SJosef Bacik 		return 0;
20102cac5af1SJosef Bacik 
20112cac5af1SJosef Bacik 	/* We have @first_key, so this @eb must have at least one item */
20122cac5af1SJosef Bacik 	if (btrfs_header_nritems(eb) == 0) {
20132cac5af1SJosef Bacik 		btrfs_err(fs_info,
20142cac5af1SJosef Bacik 		"invalid tree nritems, bytenr=%llu nritems=0 expect >0",
20152cac5af1SJosef Bacik 			  eb->start);
20162cac5af1SJosef Bacik 		WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG));
20172cac5af1SJosef Bacik 		return -EUCLEAN;
20182cac5af1SJosef Bacik 	}
20192cac5af1SJosef Bacik 
20202cac5af1SJosef Bacik 	if (found_level)
20212cac5af1SJosef Bacik 		btrfs_node_key_to_cpu(eb, &found_key, 0);
20222cac5af1SJosef Bacik 	else
20232cac5af1SJosef Bacik 		btrfs_item_key_to_cpu(eb, &found_key, 0);
20242cac5af1SJosef Bacik 	ret = btrfs_comp_cpu_keys(first_key, &found_key);
20252cac5af1SJosef Bacik 
20262cac5af1SJosef Bacik 	if (ret) {
20272cac5af1SJosef Bacik 		WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG),
20282cac5af1SJosef Bacik 		     KERN_ERR "BTRFS: tree first key check failed\n");
20292cac5af1SJosef Bacik 		btrfs_err(fs_info,
20302cac5af1SJosef Bacik "tree first key mismatch detected, bytenr=%llu parent_transid=%llu key expected=(%llu,%u,%llu) has=(%llu,%u,%llu)",
20312cac5af1SJosef Bacik 			  eb->start, parent_transid, first_key->objectid,
20322cac5af1SJosef Bacik 			  first_key->type, first_key->offset,
20332cac5af1SJosef Bacik 			  found_key.objectid, found_key.type,
20342cac5af1SJosef Bacik 			  found_key.offset);
20352cac5af1SJosef Bacik 	}
20362cac5af1SJosef Bacik 	return ret;
20372cac5af1SJosef Bacik }
2038