xref: /openbmc/linux/fs/btrfs/tree-checker.c (revision bba4f29896c986c4cec17bc0f19f2ce644fceae1)
1557ea5ddSQu Wenruo /*
2557ea5ddSQu Wenruo  * Copyright (C) Qu Wenruo 2017.  All rights reserved.
3557ea5ddSQu Wenruo  *
4557ea5ddSQu Wenruo  * This program is free software; you can redistribute it and/or
5557ea5ddSQu Wenruo  * modify it under the terms of the GNU General Public
6557ea5ddSQu Wenruo  * License v2 as published by the Free Software Foundation.
7557ea5ddSQu Wenruo  *
8557ea5ddSQu Wenruo  * This program is distributed in the hope that it will be useful,
9557ea5ddSQu Wenruo  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10557ea5ddSQu Wenruo  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11557ea5ddSQu Wenruo  * General Public License for more details.
12557ea5ddSQu Wenruo  *
13557ea5ddSQu Wenruo  * You should have received a copy of the GNU General Public
14557ea5ddSQu Wenruo  * License along with this program.
15557ea5ddSQu Wenruo  */
16557ea5ddSQu Wenruo 
17557ea5ddSQu Wenruo /*
18557ea5ddSQu Wenruo  * The module is used to catch unexpected/corrupted tree block data.
19557ea5ddSQu Wenruo  * Such behavior can be caused either by a fuzzed image or bugs.
20557ea5ddSQu Wenruo  *
21557ea5ddSQu Wenruo  * The objective is to do leaf/node validation checks when tree block is read
22557ea5ddSQu Wenruo  * from disk, and check *every* possible member, so other code won't
23557ea5ddSQu Wenruo  * need to checking them again.
24557ea5ddSQu Wenruo  *
25557ea5ddSQu Wenruo  * Due to the potential and unwanted damage, every checker needs to be
26557ea5ddSQu Wenruo  * carefully reviewed otherwise so it does not prevent mount of valid images.
27557ea5ddSQu Wenruo  */
28557ea5ddSQu Wenruo 
29557ea5ddSQu Wenruo #include "ctree.h"
30557ea5ddSQu Wenruo #include "tree-checker.h"
31557ea5ddSQu Wenruo #include "disk-io.h"
32557ea5ddSQu Wenruo #include "compression.h"
33557ea5ddSQu Wenruo 
34557ea5ddSQu Wenruo #define CORRUPT(reason, eb, root, slot)					\
35557ea5ddSQu Wenruo 	btrfs_crit(root->fs_info,					\
36557ea5ddSQu Wenruo 		   "corrupt %s, %s: block=%llu, root=%llu, slot=%d",	\
37557ea5ddSQu Wenruo 		   btrfs_header_level(eb) == 0 ? "leaf" : "node",	\
38557ea5ddSQu Wenruo 		   reason, btrfs_header_bytenr(eb), root->objectid, slot)
39557ea5ddSQu Wenruo 
40*bba4f298SQu Wenruo /*
41*bba4f298SQu Wenruo  * Error message should follow the following format:
42*bba4f298SQu Wenruo  * corrupt <type>: <identifier>, <reason>[, <bad_value>]
43*bba4f298SQu Wenruo  *
44*bba4f298SQu Wenruo  * @type:	leaf or node
45*bba4f298SQu Wenruo  * @identifier:	the necessary info to locate the leaf/node.
46*bba4f298SQu Wenruo  * 		It's recommened to decode key.objecitd/offset if it's
47*bba4f298SQu Wenruo  * 		meaningful.
48*bba4f298SQu Wenruo  * @reason:	describe the error
49*bba4f298SQu Wenruo  * @bad_value:	optional, it's recommened to output bad value and its
50*bba4f298SQu Wenruo  *		expected value (range).
51*bba4f298SQu Wenruo  *
52*bba4f298SQu Wenruo  * Since comma is used to separate the components, only space is allowed
53*bba4f298SQu Wenruo  * inside each component.
54*bba4f298SQu Wenruo  */
55*bba4f298SQu Wenruo 
56*bba4f298SQu Wenruo /*
57*bba4f298SQu Wenruo  * Append generic "corrupt leaf/node root=%llu block=%llu slot=%d: " to @fmt.
58*bba4f298SQu Wenruo  * Allows callers to customize the output.
59*bba4f298SQu Wenruo  */
60*bba4f298SQu Wenruo __printf(4, 5)
61*bba4f298SQu Wenruo static void generic_err(const struct btrfs_root *root,
62*bba4f298SQu Wenruo 			const struct extent_buffer *eb, int slot,
63*bba4f298SQu Wenruo 			const char *fmt, ...)
64*bba4f298SQu Wenruo {
65*bba4f298SQu Wenruo 	struct va_format vaf;
66*bba4f298SQu Wenruo 	va_list args;
67*bba4f298SQu Wenruo 
68*bba4f298SQu Wenruo 	va_start(args, fmt);
69*bba4f298SQu Wenruo 
70*bba4f298SQu Wenruo 	vaf.fmt = fmt;
71*bba4f298SQu Wenruo 	vaf.va = &args;
72*bba4f298SQu Wenruo 
73*bba4f298SQu Wenruo 	btrfs_crit(root->fs_info,
74*bba4f298SQu Wenruo 		"corrupt %s: root=%llu block=%llu slot=%d, %pV",
75*bba4f298SQu Wenruo 		btrfs_header_level(eb) == 0 ? "leaf" : "node",
76*bba4f298SQu Wenruo 		root->objectid, btrfs_header_bytenr(eb), slot, &vaf);
77*bba4f298SQu Wenruo 	va_end(args);
78*bba4f298SQu Wenruo }
79*bba4f298SQu Wenruo 
80557ea5ddSQu Wenruo static int check_extent_data_item(struct btrfs_root *root,
81557ea5ddSQu Wenruo 				  struct extent_buffer *leaf,
82557ea5ddSQu Wenruo 				  struct btrfs_key *key, int slot)
83557ea5ddSQu Wenruo {
84557ea5ddSQu Wenruo 	struct btrfs_file_extent_item *fi;
85557ea5ddSQu Wenruo 	u32 sectorsize = root->fs_info->sectorsize;
86557ea5ddSQu Wenruo 	u32 item_size = btrfs_item_size_nr(leaf, slot);
87557ea5ddSQu Wenruo 
88557ea5ddSQu Wenruo 	if (!IS_ALIGNED(key->offset, sectorsize)) {
89557ea5ddSQu Wenruo 		CORRUPT("unaligned key offset for file extent",
90557ea5ddSQu Wenruo 			leaf, root, slot);
91557ea5ddSQu Wenruo 		return -EUCLEAN;
92557ea5ddSQu Wenruo 	}
93557ea5ddSQu Wenruo 
94557ea5ddSQu Wenruo 	fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
95557ea5ddSQu Wenruo 
96557ea5ddSQu Wenruo 	if (btrfs_file_extent_type(leaf, fi) > BTRFS_FILE_EXTENT_TYPES) {
97557ea5ddSQu Wenruo 		CORRUPT("invalid file extent type", leaf, root, slot);
98557ea5ddSQu Wenruo 		return -EUCLEAN;
99557ea5ddSQu Wenruo 	}
100557ea5ddSQu Wenruo 
101557ea5ddSQu Wenruo 	/*
102557ea5ddSQu Wenruo 	 * Support for new compression/encrption must introduce incompat flag,
103557ea5ddSQu Wenruo 	 * and must be caught in open_ctree().
104557ea5ddSQu Wenruo 	 */
105557ea5ddSQu Wenruo 	if (btrfs_file_extent_compression(leaf, fi) > BTRFS_COMPRESS_TYPES) {
106557ea5ddSQu Wenruo 		CORRUPT("invalid file extent compression", leaf, root, slot);
107557ea5ddSQu Wenruo 		return -EUCLEAN;
108557ea5ddSQu Wenruo 	}
109557ea5ddSQu Wenruo 	if (btrfs_file_extent_encryption(leaf, fi)) {
110557ea5ddSQu Wenruo 		CORRUPT("invalid file extent encryption", leaf, root, slot);
111557ea5ddSQu Wenruo 		return -EUCLEAN;
112557ea5ddSQu Wenruo 	}
113557ea5ddSQu Wenruo 	if (btrfs_file_extent_type(leaf, fi) == BTRFS_FILE_EXTENT_INLINE) {
114557ea5ddSQu Wenruo 		/* Inline extent must have 0 as key offset */
115557ea5ddSQu Wenruo 		if (key->offset) {
116557ea5ddSQu Wenruo 			CORRUPT("inline extent has non-zero key offset",
117557ea5ddSQu Wenruo 				leaf, root, slot);
118557ea5ddSQu Wenruo 			return -EUCLEAN;
119557ea5ddSQu Wenruo 		}
120557ea5ddSQu Wenruo 
121557ea5ddSQu Wenruo 		/* Compressed inline extent has no on-disk size, skip it */
122557ea5ddSQu Wenruo 		if (btrfs_file_extent_compression(leaf, fi) !=
123557ea5ddSQu Wenruo 		    BTRFS_COMPRESS_NONE)
124557ea5ddSQu Wenruo 			return 0;
125557ea5ddSQu Wenruo 
126557ea5ddSQu Wenruo 		/* Uncompressed inline extent size must match item size */
127557ea5ddSQu Wenruo 		if (item_size != BTRFS_FILE_EXTENT_INLINE_DATA_START +
128557ea5ddSQu Wenruo 		    btrfs_file_extent_ram_bytes(leaf, fi)) {
129557ea5ddSQu Wenruo 			CORRUPT("plaintext inline extent has invalid size",
130557ea5ddSQu Wenruo 				leaf, root, slot);
131557ea5ddSQu Wenruo 			return -EUCLEAN;
132557ea5ddSQu Wenruo 		}
133557ea5ddSQu Wenruo 		return 0;
134557ea5ddSQu Wenruo 	}
135557ea5ddSQu Wenruo 
136557ea5ddSQu Wenruo 	/* Regular or preallocated extent has fixed item size */
137557ea5ddSQu Wenruo 	if (item_size != sizeof(*fi)) {
138557ea5ddSQu Wenruo 		CORRUPT(
139557ea5ddSQu Wenruo 		"regluar or preallocated extent data item size is invalid",
140557ea5ddSQu Wenruo 			leaf, root, slot);
141557ea5ddSQu Wenruo 		return -EUCLEAN;
142557ea5ddSQu Wenruo 	}
143557ea5ddSQu Wenruo 	if (!IS_ALIGNED(btrfs_file_extent_ram_bytes(leaf, fi), sectorsize) ||
144557ea5ddSQu Wenruo 	    !IS_ALIGNED(btrfs_file_extent_disk_bytenr(leaf, fi), sectorsize) ||
145557ea5ddSQu Wenruo 	    !IS_ALIGNED(btrfs_file_extent_disk_num_bytes(leaf, fi), sectorsize) ||
146557ea5ddSQu Wenruo 	    !IS_ALIGNED(btrfs_file_extent_offset(leaf, fi), sectorsize) ||
147557ea5ddSQu Wenruo 	    !IS_ALIGNED(btrfs_file_extent_num_bytes(leaf, fi), sectorsize)) {
148557ea5ddSQu Wenruo 		CORRUPT(
149557ea5ddSQu Wenruo 		"regular or preallocated extent data item has unaligned value",
150557ea5ddSQu Wenruo 			leaf, root, slot);
151557ea5ddSQu Wenruo 		return -EUCLEAN;
152557ea5ddSQu Wenruo 	}
153557ea5ddSQu Wenruo 
154557ea5ddSQu Wenruo 	return 0;
155557ea5ddSQu Wenruo }
156557ea5ddSQu Wenruo 
157557ea5ddSQu Wenruo static int check_csum_item(struct btrfs_root *root, struct extent_buffer *leaf,
158557ea5ddSQu Wenruo 			   struct btrfs_key *key, int slot)
159557ea5ddSQu Wenruo {
160557ea5ddSQu Wenruo 	u32 sectorsize = root->fs_info->sectorsize;
161557ea5ddSQu Wenruo 	u32 csumsize = btrfs_super_csum_size(root->fs_info->super_copy);
162557ea5ddSQu Wenruo 
163557ea5ddSQu Wenruo 	if (key->objectid != BTRFS_EXTENT_CSUM_OBJECTID) {
164557ea5ddSQu Wenruo 		CORRUPT("invalid objectid for csum item", leaf, root, slot);
165557ea5ddSQu Wenruo 		return -EUCLEAN;
166557ea5ddSQu Wenruo 	}
167557ea5ddSQu Wenruo 	if (!IS_ALIGNED(key->offset, sectorsize)) {
168557ea5ddSQu Wenruo 		CORRUPT("unaligned key offset for csum item", leaf, root, slot);
169557ea5ddSQu Wenruo 		return -EUCLEAN;
170557ea5ddSQu Wenruo 	}
171557ea5ddSQu Wenruo 	if (!IS_ALIGNED(btrfs_item_size_nr(leaf, slot), csumsize)) {
172557ea5ddSQu Wenruo 		CORRUPT("unaligned csum item size", leaf, root, slot);
173557ea5ddSQu Wenruo 		return -EUCLEAN;
174557ea5ddSQu Wenruo 	}
175557ea5ddSQu Wenruo 	return 0;
176557ea5ddSQu Wenruo }
177557ea5ddSQu Wenruo 
178557ea5ddSQu Wenruo /*
179557ea5ddSQu Wenruo  * Common point to switch the item-specific validation.
180557ea5ddSQu Wenruo  */
181557ea5ddSQu Wenruo static int check_leaf_item(struct btrfs_root *root,
182557ea5ddSQu Wenruo 			   struct extent_buffer *leaf,
183557ea5ddSQu Wenruo 			   struct btrfs_key *key, int slot)
184557ea5ddSQu Wenruo {
185557ea5ddSQu Wenruo 	int ret = 0;
186557ea5ddSQu Wenruo 
187557ea5ddSQu Wenruo 	switch (key->type) {
188557ea5ddSQu Wenruo 	case BTRFS_EXTENT_DATA_KEY:
189557ea5ddSQu Wenruo 		ret = check_extent_data_item(root, leaf, key, slot);
190557ea5ddSQu Wenruo 		break;
191557ea5ddSQu Wenruo 	case BTRFS_EXTENT_CSUM_KEY:
192557ea5ddSQu Wenruo 		ret = check_csum_item(root, leaf, key, slot);
193557ea5ddSQu Wenruo 		break;
194557ea5ddSQu Wenruo 	}
195557ea5ddSQu Wenruo 	return ret;
196557ea5ddSQu Wenruo }
197557ea5ddSQu Wenruo 
198557ea5ddSQu Wenruo int btrfs_check_leaf(struct btrfs_root *root, struct extent_buffer *leaf)
199557ea5ddSQu Wenruo {
200557ea5ddSQu Wenruo 	struct btrfs_fs_info *fs_info = root->fs_info;
201557ea5ddSQu Wenruo 	/* No valid key type is 0, so all key should be larger than this key */
202557ea5ddSQu Wenruo 	struct btrfs_key prev_key = {0, 0, 0};
203557ea5ddSQu Wenruo 	struct btrfs_key key;
204557ea5ddSQu Wenruo 	u32 nritems = btrfs_header_nritems(leaf);
205557ea5ddSQu Wenruo 	int slot;
206557ea5ddSQu Wenruo 
207557ea5ddSQu Wenruo 	/*
208557ea5ddSQu Wenruo 	 * Extent buffers from a relocation tree have a owner field that
209557ea5ddSQu Wenruo 	 * corresponds to the subvolume tree they are based on. So just from an
210557ea5ddSQu Wenruo 	 * extent buffer alone we can not find out what is the id of the
211557ea5ddSQu Wenruo 	 * corresponding subvolume tree, so we can not figure out if the extent
212557ea5ddSQu Wenruo 	 * buffer corresponds to the root of the relocation tree or not. So
213557ea5ddSQu Wenruo 	 * skip this check for relocation trees.
214557ea5ddSQu Wenruo 	 */
215557ea5ddSQu Wenruo 	if (nritems == 0 && !btrfs_header_flag(leaf, BTRFS_HEADER_FLAG_RELOC)) {
216557ea5ddSQu Wenruo 		struct btrfs_root *check_root;
217557ea5ddSQu Wenruo 
218557ea5ddSQu Wenruo 		key.objectid = btrfs_header_owner(leaf);
219557ea5ddSQu Wenruo 		key.type = BTRFS_ROOT_ITEM_KEY;
220557ea5ddSQu Wenruo 		key.offset = (u64)-1;
221557ea5ddSQu Wenruo 
222557ea5ddSQu Wenruo 		check_root = btrfs_get_fs_root(fs_info, &key, false);
223557ea5ddSQu Wenruo 		/*
224557ea5ddSQu Wenruo 		 * The only reason we also check NULL here is that during
225557ea5ddSQu Wenruo 		 * open_ctree() some roots has not yet been set up.
226557ea5ddSQu Wenruo 		 */
227557ea5ddSQu Wenruo 		if (!IS_ERR_OR_NULL(check_root)) {
228557ea5ddSQu Wenruo 			struct extent_buffer *eb;
229557ea5ddSQu Wenruo 
230557ea5ddSQu Wenruo 			eb = btrfs_root_node(check_root);
231557ea5ddSQu Wenruo 			/* if leaf is the root, then it's fine */
232557ea5ddSQu Wenruo 			if (leaf != eb) {
233557ea5ddSQu Wenruo 				CORRUPT("non-root leaf's nritems is 0",
234557ea5ddSQu Wenruo 					leaf, check_root, 0);
235557ea5ddSQu Wenruo 				free_extent_buffer(eb);
236557ea5ddSQu Wenruo 				return -EUCLEAN;
237557ea5ddSQu Wenruo 			}
238557ea5ddSQu Wenruo 			free_extent_buffer(eb);
239557ea5ddSQu Wenruo 		}
240557ea5ddSQu Wenruo 		return 0;
241557ea5ddSQu Wenruo 	}
242557ea5ddSQu Wenruo 
243557ea5ddSQu Wenruo 	if (nritems == 0)
244557ea5ddSQu Wenruo 		return 0;
245557ea5ddSQu Wenruo 
246557ea5ddSQu Wenruo 	/*
247557ea5ddSQu Wenruo 	 * Check the following things to make sure this is a good leaf, and
248557ea5ddSQu Wenruo 	 * leaf users won't need to bother with similar sanity checks:
249557ea5ddSQu Wenruo 	 *
250557ea5ddSQu Wenruo 	 * 1) key ordering
251557ea5ddSQu Wenruo 	 * 2) item offset and size
252557ea5ddSQu Wenruo 	 *    No overlap, no hole, all inside the leaf.
253557ea5ddSQu Wenruo 	 * 3) item content
254557ea5ddSQu Wenruo 	 *    If possible, do comprehensive sanity check.
255557ea5ddSQu Wenruo 	 *    NOTE: All checks must only rely on the item data itself.
256557ea5ddSQu Wenruo 	 */
257557ea5ddSQu Wenruo 	for (slot = 0; slot < nritems; slot++) {
258557ea5ddSQu Wenruo 		u32 item_end_expected;
259557ea5ddSQu Wenruo 		int ret;
260557ea5ddSQu Wenruo 
261557ea5ddSQu Wenruo 		btrfs_item_key_to_cpu(leaf, &key, slot);
262557ea5ddSQu Wenruo 
263557ea5ddSQu Wenruo 		/* Make sure the keys are in the right order */
264557ea5ddSQu Wenruo 		if (btrfs_comp_cpu_keys(&prev_key, &key) >= 0) {
265557ea5ddSQu Wenruo 			CORRUPT("bad key order", leaf, root, slot);
266557ea5ddSQu Wenruo 			return -EUCLEAN;
267557ea5ddSQu Wenruo 		}
268557ea5ddSQu Wenruo 
269557ea5ddSQu Wenruo 		/*
270557ea5ddSQu Wenruo 		 * Make sure the offset and ends are right, remember that the
271557ea5ddSQu Wenruo 		 * item data starts at the end of the leaf and grows towards the
272557ea5ddSQu Wenruo 		 * front.
273557ea5ddSQu Wenruo 		 */
274557ea5ddSQu Wenruo 		if (slot == 0)
275557ea5ddSQu Wenruo 			item_end_expected = BTRFS_LEAF_DATA_SIZE(fs_info);
276557ea5ddSQu Wenruo 		else
277557ea5ddSQu Wenruo 			item_end_expected = btrfs_item_offset_nr(leaf,
278557ea5ddSQu Wenruo 								 slot - 1);
279557ea5ddSQu Wenruo 		if (btrfs_item_end_nr(leaf, slot) != item_end_expected) {
280557ea5ddSQu Wenruo 			CORRUPT("slot offset bad", leaf, root, slot);
281557ea5ddSQu Wenruo 			return -EUCLEAN;
282557ea5ddSQu Wenruo 		}
283557ea5ddSQu Wenruo 
284557ea5ddSQu Wenruo 		/*
285557ea5ddSQu Wenruo 		 * Check to make sure that we don't point outside of the leaf,
286557ea5ddSQu Wenruo 		 * just in case all the items are consistent to each other, but
287557ea5ddSQu Wenruo 		 * all point outside of the leaf.
288557ea5ddSQu Wenruo 		 */
289557ea5ddSQu Wenruo 		if (btrfs_item_end_nr(leaf, slot) >
290557ea5ddSQu Wenruo 		    BTRFS_LEAF_DATA_SIZE(fs_info)) {
291557ea5ddSQu Wenruo 			CORRUPT("slot end outside of leaf", leaf, root, slot);
292557ea5ddSQu Wenruo 			return -EUCLEAN;
293557ea5ddSQu Wenruo 		}
294557ea5ddSQu Wenruo 
295557ea5ddSQu Wenruo 		/* Also check if the item pointer overlaps with btrfs item. */
296557ea5ddSQu Wenruo 		if (btrfs_item_nr_offset(slot) + sizeof(struct btrfs_item) >
297557ea5ddSQu Wenruo 		    btrfs_item_ptr_offset(leaf, slot)) {
298557ea5ddSQu Wenruo 			CORRUPT("slot overlap with its data", leaf, root, slot);
299557ea5ddSQu Wenruo 			return -EUCLEAN;
300557ea5ddSQu Wenruo 		}
301557ea5ddSQu Wenruo 
302557ea5ddSQu Wenruo 		/* Check if the item size and content meet other criteria */
303557ea5ddSQu Wenruo 		ret = check_leaf_item(root, leaf, &key, slot);
304557ea5ddSQu Wenruo 		if (ret < 0)
305557ea5ddSQu Wenruo 			return ret;
306557ea5ddSQu Wenruo 
307557ea5ddSQu Wenruo 		prev_key.objectid = key.objectid;
308557ea5ddSQu Wenruo 		prev_key.type = key.type;
309557ea5ddSQu Wenruo 		prev_key.offset = key.offset;
310557ea5ddSQu Wenruo 	}
311557ea5ddSQu Wenruo 
312557ea5ddSQu Wenruo 	return 0;
313557ea5ddSQu Wenruo }
314557ea5ddSQu Wenruo 
315557ea5ddSQu Wenruo int btrfs_check_node(struct btrfs_root *root, struct extent_buffer *node)
316557ea5ddSQu Wenruo {
317557ea5ddSQu Wenruo 	unsigned long nr = btrfs_header_nritems(node);
318557ea5ddSQu Wenruo 	struct btrfs_key key, next_key;
319557ea5ddSQu Wenruo 	int slot;
320557ea5ddSQu Wenruo 	u64 bytenr;
321557ea5ddSQu Wenruo 	int ret = 0;
322557ea5ddSQu Wenruo 
323557ea5ddSQu Wenruo 	if (nr == 0 || nr > BTRFS_NODEPTRS_PER_BLOCK(root->fs_info)) {
324557ea5ddSQu Wenruo 		btrfs_crit(root->fs_info,
325*bba4f298SQu Wenruo "corrupt node: root=%llu block=%llu, nritems too %s, have %lu expect range [1,%u]",
326*bba4f298SQu Wenruo 			   root->objectid, node->start,
327*bba4f298SQu Wenruo 			   nr == 0 ? "small" : "large", nr,
328*bba4f298SQu Wenruo 			   BTRFS_NODEPTRS_PER_BLOCK(root->fs_info));
329*bba4f298SQu Wenruo 		return -EUCLEAN;
330557ea5ddSQu Wenruo 	}
331557ea5ddSQu Wenruo 
332557ea5ddSQu Wenruo 	for (slot = 0; slot < nr - 1; slot++) {
333557ea5ddSQu Wenruo 		bytenr = btrfs_node_blockptr(node, slot);
334557ea5ddSQu Wenruo 		btrfs_node_key_to_cpu(node, &key, slot);
335557ea5ddSQu Wenruo 		btrfs_node_key_to_cpu(node, &next_key, slot + 1);
336557ea5ddSQu Wenruo 
337557ea5ddSQu Wenruo 		if (!bytenr) {
338*bba4f298SQu Wenruo 			generic_err(root, node, slot,
339*bba4f298SQu Wenruo 				"invalid NULL node pointer");
340*bba4f298SQu Wenruo 			ret = -EUCLEAN;
341*bba4f298SQu Wenruo 			goto out;
342*bba4f298SQu Wenruo 		}
343*bba4f298SQu Wenruo 		if (!IS_ALIGNED(bytenr, root->fs_info->sectorsize)) {
344*bba4f298SQu Wenruo 			generic_err(root, node, slot,
345*bba4f298SQu Wenruo 			"unaligned pointer, have %llu should be aligned to %u",
346*bba4f298SQu Wenruo 				bytenr, root->fs_info->sectorsize);
347*bba4f298SQu Wenruo 			ret = -EUCLEAN;
348557ea5ddSQu Wenruo 			goto out;
349557ea5ddSQu Wenruo 		}
350557ea5ddSQu Wenruo 
351557ea5ddSQu Wenruo 		if (btrfs_comp_cpu_keys(&key, &next_key) >= 0) {
352*bba4f298SQu Wenruo 			generic_err(root, node, slot,
353*bba4f298SQu Wenruo 	"bad key order, current (%llu %u %llu) next (%llu %u %llu)",
354*bba4f298SQu Wenruo 				key.objectid, key.type, key.offset,
355*bba4f298SQu Wenruo 				next_key.objectid, next_key.type,
356*bba4f298SQu Wenruo 				next_key.offset);
357*bba4f298SQu Wenruo 			ret = -EUCLEAN;
358557ea5ddSQu Wenruo 			goto out;
359557ea5ddSQu Wenruo 		}
360557ea5ddSQu Wenruo 	}
361557ea5ddSQu Wenruo out:
362557ea5ddSQu Wenruo 	return ret;
363557ea5ddSQu Wenruo }
364