xref: /openbmc/linux/fs/btrfs/tree-checker.c (revision 478d01b3)
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 
40bba4f298SQu Wenruo /*
41bba4f298SQu Wenruo  * Error message should follow the following format:
42bba4f298SQu Wenruo  * corrupt <type>: <identifier>, <reason>[, <bad_value>]
43bba4f298SQu Wenruo  *
44bba4f298SQu Wenruo  * @type:	leaf or node
45bba4f298SQu Wenruo  * @identifier:	the necessary info to locate the leaf/node.
46bba4f298SQu Wenruo  * 		It's recommened to decode key.objecitd/offset if it's
47bba4f298SQu Wenruo  * 		meaningful.
48bba4f298SQu Wenruo  * @reason:	describe the error
49bba4f298SQu Wenruo  * @bad_value:	optional, it's recommened to output bad value and its
50bba4f298SQu Wenruo  *		expected value (range).
51bba4f298SQu Wenruo  *
52bba4f298SQu Wenruo  * Since comma is used to separate the components, only space is allowed
53bba4f298SQu Wenruo  * inside each component.
54bba4f298SQu Wenruo  */
55bba4f298SQu Wenruo 
56bba4f298SQu Wenruo /*
57bba4f298SQu Wenruo  * Append generic "corrupt leaf/node root=%llu block=%llu slot=%d: " to @fmt.
58bba4f298SQu Wenruo  * Allows callers to customize the output.
59bba4f298SQu Wenruo  */
60bba4f298SQu Wenruo __printf(4, 5)
61bba4f298SQu Wenruo static void generic_err(const struct btrfs_root *root,
62bba4f298SQu Wenruo 			const struct extent_buffer *eb, int slot,
63bba4f298SQu Wenruo 			const char *fmt, ...)
64bba4f298SQu Wenruo {
65bba4f298SQu Wenruo 	struct va_format vaf;
66bba4f298SQu Wenruo 	va_list args;
67bba4f298SQu Wenruo 
68bba4f298SQu Wenruo 	va_start(args, fmt);
69bba4f298SQu Wenruo 
70bba4f298SQu Wenruo 	vaf.fmt = fmt;
71bba4f298SQu Wenruo 	vaf.va = &args;
72bba4f298SQu Wenruo 
73bba4f298SQu Wenruo 	btrfs_crit(root->fs_info,
74bba4f298SQu Wenruo 		"corrupt %s: root=%llu block=%llu slot=%d, %pV",
75bba4f298SQu Wenruo 		btrfs_header_level(eb) == 0 ? "leaf" : "node",
76bba4f298SQu Wenruo 		root->objectid, btrfs_header_bytenr(eb), slot, &vaf);
77bba4f298SQu Wenruo 	va_end(args);
78bba4f298SQu Wenruo }
79bba4f298SQu 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) {
233478d01b3SQu Wenruo 				generic_err(check_root, leaf, 0,
234478d01b3SQu Wenruo 		"invalid nritems, have %u should not be 0 for non-root leaf",
235478d01b3SQu Wenruo 					nritems);
236557ea5ddSQu Wenruo 				free_extent_buffer(eb);
237557ea5ddSQu Wenruo 				return -EUCLEAN;
238557ea5ddSQu Wenruo 			}
239557ea5ddSQu Wenruo 			free_extent_buffer(eb);
240557ea5ddSQu Wenruo 		}
241557ea5ddSQu Wenruo 		return 0;
242557ea5ddSQu Wenruo 	}
243557ea5ddSQu Wenruo 
244557ea5ddSQu Wenruo 	if (nritems == 0)
245557ea5ddSQu Wenruo 		return 0;
246557ea5ddSQu Wenruo 
247557ea5ddSQu Wenruo 	/*
248557ea5ddSQu Wenruo 	 * Check the following things to make sure this is a good leaf, and
249557ea5ddSQu Wenruo 	 * leaf users won't need to bother with similar sanity checks:
250557ea5ddSQu Wenruo 	 *
251557ea5ddSQu Wenruo 	 * 1) key ordering
252557ea5ddSQu Wenruo 	 * 2) item offset and size
253557ea5ddSQu Wenruo 	 *    No overlap, no hole, all inside the leaf.
254557ea5ddSQu Wenruo 	 * 3) item content
255557ea5ddSQu Wenruo 	 *    If possible, do comprehensive sanity check.
256557ea5ddSQu Wenruo 	 *    NOTE: All checks must only rely on the item data itself.
257557ea5ddSQu Wenruo 	 */
258557ea5ddSQu Wenruo 	for (slot = 0; slot < nritems; slot++) {
259557ea5ddSQu Wenruo 		u32 item_end_expected;
260557ea5ddSQu Wenruo 		int ret;
261557ea5ddSQu Wenruo 
262557ea5ddSQu Wenruo 		btrfs_item_key_to_cpu(leaf, &key, slot);
263557ea5ddSQu Wenruo 
264557ea5ddSQu Wenruo 		/* Make sure the keys are in the right order */
265557ea5ddSQu Wenruo 		if (btrfs_comp_cpu_keys(&prev_key, &key) >= 0) {
266478d01b3SQu Wenruo 			generic_err(root, leaf, slot,
267478d01b3SQu Wenruo 	"bad key order, prev (%llu %u %llu) current (%llu %u %llu)",
268478d01b3SQu Wenruo 				prev_key.objectid, prev_key.type,
269478d01b3SQu Wenruo 				prev_key.offset, key.objectid, key.type,
270478d01b3SQu Wenruo 				key.offset);
271557ea5ddSQu Wenruo 			return -EUCLEAN;
272557ea5ddSQu Wenruo 		}
273557ea5ddSQu Wenruo 
274557ea5ddSQu Wenruo 		/*
275557ea5ddSQu Wenruo 		 * Make sure the offset and ends are right, remember that the
276557ea5ddSQu Wenruo 		 * item data starts at the end of the leaf and grows towards the
277557ea5ddSQu Wenruo 		 * front.
278557ea5ddSQu Wenruo 		 */
279557ea5ddSQu Wenruo 		if (slot == 0)
280557ea5ddSQu Wenruo 			item_end_expected = BTRFS_LEAF_DATA_SIZE(fs_info);
281557ea5ddSQu Wenruo 		else
282557ea5ddSQu Wenruo 			item_end_expected = btrfs_item_offset_nr(leaf,
283557ea5ddSQu Wenruo 								 slot - 1);
284557ea5ddSQu Wenruo 		if (btrfs_item_end_nr(leaf, slot) != item_end_expected) {
285478d01b3SQu Wenruo 			generic_err(root, leaf, slot,
286478d01b3SQu Wenruo 				"unexpected item end, have %u expect %u",
287478d01b3SQu Wenruo 				btrfs_item_end_nr(leaf, slot),
288478d01b3SQu Wenruo 				item_end_expected);
289557ea5ddSQu Wenruo 			return -EUCLEAN;
290557ea5ddSQu Wenruo 		}
291557ea5ddSQu Wenruo 
292557ea5ddSQu Wenruo 		/*
293557ea5ddSQu Wenruo 		 * Check to make sure that we don't point outside of the leaf,
294557ea5ddSQu Wenruo 		 * just in case all the items are consistent to each other, but
295557ea5ddSQu Wenruo 		 * all point outside of the leaf.
296557ea5ddSQu Wenruo 		 */
297557ea5ddSQu Wenruo 		if (btrfs_item_end_nr(leaf, slot) >
298557ea5ddSQu Wenruo 		    BTRFS_LEAF_DATA_SIZE(fs_info)) {
299478d01b3SQu Wenruo 			generic_err(root, leaf, slot,
300478d01b3SQu Wenruo 			"slot end outside of leaf, have %u expect range [0, %u]",
301478d01b3SQu Wenruo 				btrfs_item_end_nr(leaf, slot),
302478d01b3SQu Wenruo 				BTRFS_LEAF_DATA_SIZE(fs_info));
303557ea5ddSQu Wenruo 			return -EUCLEAN;
304557ea5ddSQu Wenruo 		}
305557ea5ddSQu Wenruo 
306557ea5ddSQu Wenruo 		/* Also check if the item pointer overlaps with btrfs item. */
307557ea5ddSQu Wenruo 		if (btrfs_item_nr_offset(slot) + sizeof(struct btrfs_item) >
308557ea5ddSQu Wenruo 		    btrfs_item_ptr_offset(leaf, slot)) {
309478d01b3SQu Wenruo 			generic_err(root, leaf, slot,
310478d01b3SQu Wenruo 		"slot overlaps with its data, item end %lu data start %lu",
311478d01b3SQu Wenruo 				btrfs_item_nr_offset(slot) +
312478d01b3SQu Wenruo 				sizeof(struct btrfs_item),
313478d01b3SQu Wenruo 				btrfs_item_ptr_offset(leaf, slot));
314557ea5ddSQu Wenruo 			return -EUCLEAN;
315557ea5ddSQu Wenruo 		}
316557ea5ddSQu Wenruo 
317557ea5ddSQu Wenruo 		/* Check if the item size and content meet other criteria */
318557ea5ddSQu Wenruo 		ret = check_leaf_item(root, leaf, &key, slot);
319557ea5ddSQu Wenruo 		if (ret < 0)
320557ea5ddSQu Wenruo 			return ret;
321557ea5ddSQu Wenruo 
322557ea5ddSQu Wenruo 		prev_key.objectid = key.objectid;
323557ea5ddSQu Wenruo 		prev_key.type = key.type;
324557ea5ddSQu Wenruo 		prev_key.offset = key.offset;
325557ea5ddSQu Wenruo 	}
326557ea5ddSQu Wenruo 
327557ea5ddSQu Wenruo 	return 0;
328557ea5ddSQu Wenruo }
329557ea5ddSQu Wenruo 
330557ea5ddSQu Wenruo int btrfs_check_node(struct btrfs_root *root, struct extent_buffer *node)
331557ea5ddSQu Wenruo {
332557ea5ddSQu Wenruo 	unsigned long nr = btrfs_header_nritems(node);
333557ea5ddSQu Wenruo 	struct btrfs_key key, next_key;
334557ea5ddSQu Wenruo 	int slot;
335557ea5ddSQu Wenruo 	u64 bytenr;
336557ea5ddSQu Wenruo 	int ret = 0;
337557ea5ddSQu Wenruo 
338557ea5ddSQu Wenruo 	if (nr == 0 || nr > BTRFS_NODEPTRS_PER_BLOCK(root->fs_info)) {
339557ea5ddSQu Wenruo 		btrfs_crit(root->fs_info,
340bba4f298SQu Wenruo "corrupt node: root=%llu block=%llu, nritems too %s, have %lu expect range [1,%u]",
341bba4f298SQu Wenruo 			   root->objectid, node->start,
342bba4f298SQu Wenruo 			   nr == 0 ? "small" : "large", nr,
343bba4f298SQu Wenruo 			   BTRFS_NODEPTRS_PER_BLOCK(root->fs_info));
344bba4f298SQu Wenruo 		return -EUCLEAN;
345557ea5ddSQu Wenruo 	}
346557ea5ddSQu Wenruo 
347557ea5ddSQu Wenruo 	for (slot = 0; slot < nr - 1; slot++) {
348557ea5ddSQu Wenruo 		bytenr = btrfs_node_blockptr(node, slot);
349557ea5ddSQu Wenruo 		btrfs_node_key_to_cpu(node, &key, slot);
350557ea5ddSQu Wenruo 		btrfs_node_key_to_cpu(node, &next_key, slot + 1);
351557ea5ddSQu Wenruo 
352557ea5ddSQu Wenruo 		if (!bytenr) {
353bba4f298SQu Wenruo 			generic_err(root, node, slot,
354bba4f298SQu Wenruo 				"invalid NULL node pointer");
355bba4f298SQu Wenruo 			ret = -EUCLEAN;
356bba4f298SQu Wenruo 			goto out;
357bba4f298SQu Wenruo 		}
358bba4f298SQu Wenruo 		if (!IS_ALIGNED(bytenr, root->fs_info->sectorsize)) {
359bba4f298SQu Wenruo 			generic_err(root, node, slot,
360bba4f298SQu Wenruo 			"unaligned pointer, have %llu should be aligned to %u",
361bba4f298SQu Wenruo 				bytenr, root->fs_info->sectorsize);
362bba4f298SQu Wenruo 			ret = -EUCLEAN;
363557ea5ddSQu Wenruo 			goto out;
364557ea5ddSQu Wenruo 		}
365557ea5ddSQu Wenruo 
366557ea5ddSQu Wenruo 		if (btrfs_comp_cpu_keys(&key, &next_key) >= 0) {
367bba4f298SQu Wenruo 			generic_err(root, node, slot,
368bba4f298SQu Wenruo 	"bad key order, current (%llu %u %llu) next (%llu %u %llu)",
369bba4f298SQu Wenruo 				key.objectid, key.type, key.offset,
370bba4f298SQu Wenruo 				next_key.objectid, next_key.type,
371bba4f298SQu Wenruo 				next_key.offset);
372bba4f298SQu Wenruo 			ret = -EUCLEAN;
373557ea5ddSQu Wenruo 			goto out;
374557ea5ddSQu Wenruo 		}
375557ea5ddSQu Wenruo 	}
376557ea5ddSQu Wenruo out:
377557ea5ddSQu Wenruo 	return ret;
378557ea5ddSQu Wenruo }
379