GNU Linux-libre 4.14.332-gnu1
[releases.git] / fs / btrfs / tree-checker.c
1 /*
2  * Copyright (C) Qu Wenruo 2017.  All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public
6  * License v2 as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public
14  * License along with this program.
15  */
16
17 /*
18  * The module is used to catch unexpected/corrupted tree block data.
19  * Such behavior can be caused either by a fuzzed image or bugs.
20  *
21  * The objective is to do leaf/node validation checks when tree block is read
22  * from disk, and check *every* possible member, so other code won't
23  * need to checking them again.
24  *
25  * Due to the potential and unwanted damage, every checker needs to be
26  * carefully reviewed otherwise so it does not prevent mount of valid images.
27  */
28
29 #include "ctree.h"
30 #include "tree-checker.h"
31 #include "disk-io.h"
32 #include "compression.h"
33 #include "hash.h"
34 #include "volumes.h"
35
36 #define CORRUPT(reason, eb, root, slot)                                 \
37         btrfs_crit(root->fs_info,                                       \
38                    "corrupt %s, %s: block=%llu, root=%llu, slot=%d",    \
39                    btrfs_header_level(eb) == 0 ? "leaf" : "node",       \
40                    reason, btrfs_header_bytenr(eb), root->objectid, slot)
41
42 /*
43  * Error message should follow the following format:
44  * corrupt <type>: <identifier>, <reason>[, <bad_value>]
45  *
46  * @type:       leaf or node
47  * @identifier: the necessary info to locate the leaf/node.
48  *              It's recommened to decode key.objecitd/offset if it's
49  *              meaningful.
50  * @reason:     describe the error
51  * @bad_value:  optional, it's recommened to output bad value and its
52  *              expected value (range).
53  *
54  * Since comma is used to separate the components, only space is allowed
55  * inside each component.
56  */
57
58 /*
59  * Append generic "corrupt leaf/node root=%llu block=%llu slot=%d: " to @fmt.
60  * Allows callers to customize the output.
61  */
62 __printf(4, 5)
63 static void generic_err(const struct btrfs_root *root,
64                         const struct extent_buffer *eb, int slot,
65                         const char *fmt, ...)
66 {
67         struct va_format vaf;
68         va_list args;
69
70         va_start(args, fmt);
71
72         vaf.fmt = fmt;
73         vaf.va = &args;
74
75         btrfs_crit(root->fs_info,
76                 "corrupt %s: root=%llu block=%llu slot=%d, %pV",
77                 btrfs_header_level(eb) == 0 ? "leaf" : "node",
78                 root->objectid, btrfs_header_bytenr(eb), slot, &vaf);
79         va_end(args);
80 }
81
82 static int check_extent_data_item(struct btrfs_root *root,
83                                   struct extent_buffer *leaf,
84                                   struct btrfs_key *key, int slot)
85 {
86         struct btrfs_file_extent_item *fi;
87         u32 sectorsize = root->fs_info->sectorsize;
88         u32 item_size = btrfs_item_size_nr(leaf, slot);
89
90         if (!IS_ALIGNED(key->offset, sectorsize)) {
91                 CORRUPT("unaligned key offset for file extent",
92                         leaf, root, slot);
93                 return -EUCLEAN;
94         }
95
96         fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
97
98         if (btrfs_file_extent_type(leaf, fi) > BTRFS_FILE_EXTENT_TYPES) {
99                 CORRUPT("invalid file extent type", leaf, root, slot);
100                 return -EUCLEAN;
101         }
102
103         /*
104          * Support for new compression/encrption must introduce incompat flag,
105          * and must be caught in open_ctree().
106          */
107         if (btrfs_file_extent_compression(leaf, fi) > BTRFS_COMPRESS_TYPES) {
108                 CORRUPT("invalid file extent compression", leaf, root, slot);
109                 return -EUCLEAN;
110         }
111         if (btrfs_file_extent_encryption(leaf, fi)) {
112                 CORRUPT("invalid file extent encryption", leaf, root, slot);
113                 return -EUCLEAN;
114         }
115         if (btrfs_file_extent_type(leaf, fi) == BTRFS_FILE_EXTENT_INLINE) {
116                 /* Inline extent must have 0 as key offset */
117                 if (key->offset) {
118                         CORRUPT("inline extent has non-zero key offset",
119                                 leaf, root, slot);
120                         return -EUCLEAN;
121                 }
122
123                 /* Compressed inline extent has no on-disk size, skip it */
124                 if (btrfs_file_extent_compression(leaf, fi) !=
125                     BTRFS_COMPRESS_NONE)
126                         return 0;
127
128                 /* Uncompressed inline extent size must match item size */
129                 if (item_size != BTRFS_FILE_EXTENT_INLINE_DATA_START +
130                     btrfs_file_extent_ram_bytes(leaf, fi)) {
131                         CORRUPT("plaintext inline extent has invalid size",
132                                 leaf, root, slot);
133                         return -EUCLEAN;
134                 }
135                 return 0;
136         }
137
138         /* Regular or preallocated extent has fixed item size */
139         if (item_size != sizeof(*fi)) {
140                 CORRUPT(
141                 "regluar or preallocated extent data item size is invalid",
142                         leaf, root, slot);
143                 return -EUCLEAN;
144         }
145         if (!IS_ALIGNED(btrfs_file_extent_ram_bytes(leaf, fi), sectorsize) ||
146             !IS_ALIGNED(btrfs_file_extent_disk_bytenr(leaf, fi), sectorsize) ||
147             !IS_ALIGNED(btrfs_file_extent_disk_num_bytes(leaf, fi), sectorsize) ||
148             !IS_ALIGNED(btrfs_file_extent_offset(leaf, fi), sectorsize) ||
149             !IS_ALIGNED(btrfs_file_extent_num_bytes(leaf, fi), sectorsize)) {
150                 CORRUPT(
151                 "regular or preallocated extent data item has unaligned value",
152                         leaf, root, slot);
153                 return -EUCLEAN;
154         }
155
156         return 0;
157 }
158
159 static int check_csum_item(struct btrfs_root *root, struct extent_buffer *leaf,
160                            struct btrfs_key *key, int slot)
161 {
162         u32 sectorsize = root->fs_info->sectorsize;
163         u32 csumsize = btrfs_super_csum_size(root->fs_info->super_copy);
164
165         if (key->objectid != BTRFS_EXTENT_CSUM_OBJECTID) {
166                 CORRUPT("invalid objectid for csum item", leaf, root, slot);
167                 return -EUCLEAN;
168         }
169         if (!IS_ALIGNED(key->offset, sectorsize)) {
170                 CORRUPT("unaligned key offset for csum item", leaf, root, slot);
171                 return -EUCLEAN;
172         }
173         if (!IS_ALIGNED(btrfs_item_size_nr(leaf, slot), csumsize)) {
174                 CORRUPT("unaligned csum item size", leaf, root, slot);
175                 return -EUCLEAN;
176         }
177         return 0;
178 }
179
180 /*
181  * Customized reported for dir_item, only important new info is key->objectid,
182  * which represents inode number
183  */
184 __printf(4, 5)
185 static void dir_item_err(const struct btrfs_root *root,
186                          const struct extent_buffer *eb, int slot,
187                          const char *fmt, ...)
188 {
189         struct btrfs_key key;
190         struct va_format vaf;
191         va_list args;
192
193         btrfs_item_key_to_cpu(eb, &key, slot);
194         va_start(args, fmt);
195
196         vaf.fmt = fmt;
197         vaf.va = &args;
198
199         btrfs_crit(root->fs_info,
200         "corrupt %s: root=%llu block=%llu slot=%d ino=%llu, %pV",
201                 btrfs_header_level(eb) == 0 ? "leaf" : "node", root->objectid,
202                 btrfs_header_bytenr(eb), slot, key.objectid, &vaf);
203         va_end(args);
204 }
205
206 static int check_dir_item(struct btrfs_root *root,
207                           struct extent_buffer *leaf,
208                           struct btrfs_key *key, int slot)
209 {
210         struct btrfs_dir_item *di;
211         u32 item_size = btrfs_item_size_nr(leaf, slot);
212         u32 cur = 0;
213
214         di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
215         while (cur < item_size) {
216                 u32 name_len;
217                 u32 data_len;
218                 u32 max_name_len;
219                 u32 total_size;
220                 u32 name_hash;
221                 u8 dir_type;
222
223                 /* header itself should not cross item boundary */
224                 if (cur + sizeof(*di) > item_size) {
225                         dir_item_err(root, leaf, slot,
226                 "dir item header crosses item boundary, have %zu boundary %u",
227                                 cur + sizeof(*di), item_size);
228                         return -EUCLEAN;
229                 }
230
231                 /* dir type check */
232                 dir_type = btrfs_dir_type(leaf, di);
233                 if (dir_type >= BTRFS_FT_MAX) {
234                         dir_item_err(root, leaf, slot,
235                         "invalid dir item type, have %u expect [0, %u)",
236                                 dir_type, BTRFS_FT_MAX);
237                         return -EUCLEAN;
238                 }
239
240                 if (key->type == BTRFS_XATTR_ITEM_KEY &&
241                     dir_type != BTRFS_FT_XATTR) {
242                         dir_item_err(root, leaf, slot,
243                 "invalid dir item type for XATTR key, have %u expect %u",
244                                 dir_type, BTRFS_FT_XATTR);
245                         return -EUCLEAN;
246                 }
247                 if (dir_type == BTRFS_FT_XATTR &&
248                     key->type != BTRFS_XATTR_ITEM_KEY) {
249                         dir_item_err(root, leaf, slot,
250                         "xattr dir type found for non-XATTR key");
251                         return -EUCLEAN;
252                 }
253                 if (dir_type == BTRFS_FT_XATTR)
254                         max_name_len = XATTR_NAME_MAX;
255                 else
256                         max_name_len = BTRFS_NAME_LEN;
257
258                 /* Name/data length check */
259                 name_len = btrfs_dir_name_len(leaf, di);
260                 data_len = btrfs_dir_data_len(leaf, di);
261                 if (name_len > max_name_len) {
262                         dir_item_err(root, leaf, slot,
263                         "dir item name len too long, have %u max %u",
264                                 name_len, max_name_len);
265                         return -EUCLEAN;
266                 }
267                 if (name_len + data_len > BTRFS_MAX_XATTR_SIZE(root->fs_info)) {
268                         dir_item_err(root, leaf, slot,
269                         "dir item name and data len too long, have %u max %u",
270                                 name_len + data_len,
271                                 BTRFS_MAX_XATTR_SIZE(root->fs_info));
272                         return -EUCLEAN;
273                 }
274
275                 if (data_len && dir_type != BTRFS_FT_XATTR) {
276                         dir_item_err(root, leaf, slot,
277                         "dir item with invalid data len, have %u expect 0",
278                                 data_len);
279                         return -EUCLEAN;
280                 }
281
282                 total_size = sizeof(*di) + name_len + data_len;
283
284                 /* header and name/data should not cross item boundary */
285                 if (cur + total_size > item_size) {
286                         dir_item_err(root, leaf, slot,
287                 "dir item data crosses item boundary, have %u boundary %u",
288                                 cur + total_size, item_size);
289                         return -EUCLEAN;
290                 }
291
292                 /*
293                  * Special check for XATTR/DIR_ITEM, as key->offset is name
294                  * hash, should match its name
295                  */
296                 if (key->type == BTRFS_DIR_ITEM_KEY ||
297                     key->type == BTRFS_XATTR_ITEM_KEY) {
298                         char namebuf[max(BTRFS_NAME_LEN, XATTR_NAME_MAX)];
299
300                         read_extent_buffer(leaf, namebuf,
301                                         (unsigned long)(di + 1), name_len);
302                         name_hash = btrfs_name_hash(namebuf, name_len);
303                         if (key->offset != name_hash) {
304                                 dir_item_err(root, leaf, slot,
305                 "name hash mismatch with key, have 0x%016x expect 0x%016llx",
306                                         name_hash, key->offset);
307                                 return -EUCLEAN;
308                         }
309                 }
310                 cur += total_size;
311                 di = (struct btrfs_dir_item *)((void *)di + total_size);
312         }
313         return 0;
314 }
315
316 __printf(4, 5)
317 __cold
318 static void block_group_err(const struct btrfs_fs_info *fs_info,
319                             const struct extent_buffer *eb, int slot,
320                             const char *fmt, ...)
321 {
322         struct btrfs_key key;
323         struct va_format vaf;
324         va_list args;
325
326         btrfs_item_key_to_cpu(eb, &key, slot);
327         va_start(args, fmt);
328
329         vaf.fmt = fmt;
330         vaf.va = &args;
331
332         btrfs_crit(fs_info,
333         "corrupt %s: root=%llu block=%llu slot=%d bg_start=%llu bg_len=%llu, %pV",
334                 btrfs_header_level(eb) == 0 ? "leaf" : "node",
335                 btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot,
336                 key.objectid, key.offset, &vaf);
337         va_end(args);
338 }
339
340 static int check_block_group_item(struct btrfs_fs_info *fs_info,
341                                   struct extent_buffer *leaf,
342                                   struct btrfs_key *key, int slot)
343 {
344         struct btrfs_block_group_item bgi;
345         u32 item_size = btrfs_item_size_nr(leaf, slot);
346         u64 flags;
347         u64 type;
348
349         /*
350          * Here we don't really care about alignment since extent allocator can
351          * handle it.  We care more about the size.
352          */
353         if (key->offset == 0) {
354                 block_group_err(fs_info, leaf, slot,
355                                 "invalid block group size 0");
356                 return -EUCLEAN;
357         }
358
359         if (item_size != sizeof(bgi)) {
360                 block_group_err(fs_info, leaf, slot,
361                         "invalid item size, have %u expect %zu",
362                                 item_size, sizeof(bgi));
363                 return -EUCLEAN;
364         }
365
366         read_extent_buffer(leaf, &bgi, btrfs_item_ptr_offset(leaf, slot),
367                            sizeof(bgi));
368         if (btrfs_block_group_chunk_objectid(&bgi) !=
369             BTRFS_FIRST_CHUNK_TREE_OBJECTID) {
370                 block_group_err(fs_info, leaf, slot,
371                 "invalid block group chunk objectid, have %llu expect %llu",
372                                 btrfs_block_group_chunk_objectid(&bgi),
373                                 BTRFS_FIRST_CHUNK_TREE_OBJECTID);
374                 return -EUCLEAN;
375         }
376
377         if (btrfs_block_group_used(&bgi) > key->offset) {
378                 block_group_err(fs_info, leaf, slot,
379                         "invalid block group used, have %llu expect [0, %llu)",
380                                 btrfs_block_group_used(&bgi), key->offset);
381                 return -EUCLEAN;
382         }
383
384         flags = btrfs_block_group_flags(&bgi);
385         if (hweight64(flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) > 1) {
386                 block_group_err(fs_info, leaf, slot,
387 "invalid profile flags, have 0x%llx (%lu bits set) expect no more than 1 bit set",
388                         flags & BTRFS_BLOCK_GROUP_PROFILE_MASK,
389                         hweight64(flags & BTRFS_BLOCK_GROUP_PROFILE_MASK));
390                 return -EUCLEAN;
391         }
392
393         type = flags & BTRFS_BLOCK_GROUP_TYPE_MASK;
394         if (type != BTRFS_BLOCK_GROUP_DATA &&
395             type != BTRFS_BLOCK_GROUP_METADATA &&
396             type != BTRFS_BLOCK_GROUP_SYSTEM &&
397             type != (BTRFS_BLOCK_GROUP_METADATA |
398                            BTRFS_BLOCK_GROUP_DATA)) {
399                 block_group_err(fs_info, leaf, slot,
400 "invalid type, have 0x%llx (%lu bits set) expect either 0x%llx, 0x%llx, 0x%llx or 0x%llx",
401                         type, hweight64(type),
402                         BTRFS_BLOCK_GROUP_DATA, BTRFS_BLOCK_GROUP_METADATA,
403                         BTRFS_BLOCK_GROUP_SYSTEM,
404                         BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA);
405                 return -EUCLEAN;
406         }
407         return 0;
408 }
409
410 /*
411  * Common point to switch the item-specific validation.
412  */
413 static int check_leaf_item(struct btrfs_root *root,
414                            struct extent_buffer *leaf,
415                            struct btrfs_key *key, int slot)
416 {
417         int ret = 0;
418
419         switch (key->type) {
420         case BTRFS_EXTENT_DATA_KEY:
421                 ret = check_extent_data_item(root, leaf, key, slot);
422                 break;
423         case BTRFS_EXTENT_CSUM_KEY:
424                 ret = check_csum_item(root, leaf, key, slot);
425                 break;
426         case BTRFS_DIR_ITEM_KEY:
427         case BTRFS_DIR_INDEX_KEY:
428         case BTRFS_XATTR_ITEM_KEY:
429                 ret = check_dir_item(root, leaf, key, slot);
430                 break;
431         case BTRFS_BLOCK_GROUP_ITEM_KEY:
432                 ret = check_block_group_item(root->fs_info, leaf, key, slot);
433                 break;
434         }
435         return ret;
436 }
437
438 static int check_leaf(struct btrfs_root *root, struct extent_buffer *leaf,
439                       bool check_item_data)
440 {
441         struct btrfs_fs_info *fs_info = root->fs_info;
442         /* No valid key type is 0, so all key should be larger than this key */
443         struct btrfs_key prev_key = {0, 0, 0};
444         struct btrfs_key key;
445         u32 nritems = btrfs_header_nritems(leaf);
446         int slot;
447
448         if (btrfs_header_level(leaf) != 0) {
449                 generic_err(root, leaf, 0,
450                         "invalid level for leaf, have %d expect 0",
451                         btrfs_header_level(leaf));
452                 return -EUCLEAN;
453         }
454
455         /*
456          * Extent buffers from a relocation tree have a owner field that
457          * corresponds to the subvolume tree they are based on. So just from an
458          * extent buffer alone we can not find out what is the id of the
459          * corresponding subvolume tree, so we can not figure out if the extent
460          * buffer corresponds to the root of the relocation tree or not. So
461          * skip this check for relocation trees.
462          */
463         if (nritems == 0 && !btrfs_header_flag(leaf, BTRFS_HEADER_FLAG_RELOC)) {
464                 u64 owner = btrfs_header_owner(leaf);
465                 struct btrfs_root *check_root;
466
467                 /* These trees must never be empty */
468                 if (owner == BTRFS_ROOT_TREE_OBJECTID ||
469                     owner == BTRFS_CHUNK_TREE_OBJECTID ||
470                     owner == BTRFS_EXTENT_TREE_OBJECTID ||
471                     owner == BTRFS_DEV_TREE_OBJECTID ||
472                     owner == BTRFS_FS_TREE_OBJECTID ||
473                     owner == BTRFS_DATA_RELOC_TREE_OBJECTID) {
474                         generic_err(root, leaf, 0,
475                         "invalid root, root %llu must never be empty",
476                                     owner);
477                         return -EUCLEAN;
478                 }
479                 key.objectid = owner;
480                 key.type = BTRFS_ROOT_ITEM_KEY;
481                 key.offset = (u64)-1;
482
483                 check_root = btrfs_get_fs_root(fs_info, &key, false);
484                 /*
485                  * The only reason we also check NULL here is that during
486                  * open_ctree() some roots has not yet been set up.
487                  */
488                 if (!IS_ERR_OR_NULL(check_root)) {
489                         struct extent_buffer *eb;
490
491                         eb = btrfs_root_node(check_root);
492                         /* if leaf is the root, then it's fine */
493                         if (leaf != eb) {
494                                 CORRUPT("non-root leaf's nritems is 0",
495                                         leaf, check_root, 0);
496                                 free_extent_buffer(eb);
497                                 return -EUCLEAN;
498                         }
499                         free_extent_buffer(eb);
500                 }
501                 return 0;
502         }
503
504         if (nritems == 0)
505                 return 0;
506
507         /*
508          * Check the following things to make sure this is a good leaf, and
509          * leaf users won't need to bother with similar sanity checks:
510          *
511          * 1) key ordering
512          * 2) item offset and size
513          *    No overlap, no hole, all inside the leaf.
514          * 3) item content
515          *    If possible, do comprehensive sanity check.
516          *    NOTE: All checks must only rely on the item data itself.
517          */
518         for (slot = 0; slot < nritems; slot++) {
519                 u32 item_end_expected;
520                 int ret;
521
522                 btrfs_item_key_to_cpu(leaf, &key, slot);
523
524                 /* Make sure the keys are in the right order */
525                 if (btrfs_comp_cpu_keys(&prev_key, &key) >= 0) {
526                         CORRUPT("bad key order", leaf, root, slot);
527                         return -EUCLEAN;
528                 }
529
530                 /*
531                  * Make sure the offset and ends are right, remember that the
532                  * item data starts at the end of the leaf and grows towards the
533                  * front.
534                  */
535                 if (slot == 0)
536                         item_end_expected = BTRFS_LEAF_DATA_SIZE(fs_info);
537                 else
538                         item_end_expected = btrfs_item_offset_nr(leaf,
539                                                                  slot - 1);
540                 if (btrfs_item_end_nr(leaf, slot) != item_end_expected) {
541                         CORRUPT("slot offset bad", leaf, root, slot);
542                         return -EUCLEAN;
543                 }
544
545                 /*
546                  * Check to make sure that we don't point outside of the leaf,
547                  * just in case all the items are consistent to each other, but
548                  * all point outside of the leaf.
549                  */
550                 if (btrfs_item_end_nr(leaf, slot) >
551                     BTRFS_LEAF_DATA_SIZE(fs_info)) {
552                         CORRUPT("slot end outside of leaf", leaf, root, slot);
553                         return -EUCLEAN;
554                 }
555
556                 /* Also check if the item pointer overlaps with btrfs item. */
557                 if (btrfs_item_nr_offset(slot) + sizeof(struct btrfs_item) >
558                     btrfs_item_ptr_offset(leaf, slot)) {
559                         CORRUPT("slot overlap with its data", leaf, root, slot);
560                         return -EUCLEAN;
561                 }
562
563                 if (check_item_data) {
564                         /*
565                          * Check if the item size and content meet other
566                          * criteria
567                          */
568                         ret = check_leaf_item(root, leaf, &key, slot);
569                         if (ret < 0)
570                                 return ret;
571                 }
572
573                 prev_key.objectid = key.objectid;
574                 prev_key.type = key.type;
575                 prev_key.offset = key.offset;
576         }
577
578         return 0;
579 }
580
581 int btrfs_check_leaf_full(struct btrfs_root *root, struct extent_buffer *leaf)
582 {
583         return check_leaf(root, leaf, true);
584 }
585
586 int btrfs_check_leaf_relaxed(struct btrfs_root *root,
587                              struct extent_buffer *leaf)
588 {
589         return check_leaf(root, leaf, false);
590 }
591
592 int btrfs_check_node(struct btrfs_root *root, struct extent_buffer *node)
593 {
594         unsigned long nr = btrfs_header_nritems(node);
595         struct btrfs_key key, next_key;
596         int slot;
597         int level = btrfs_header_level(node);
598         u64 bytenr;
599         int ret = 0;
600
601         if (level <= 0 || level >= BTRFS_MAX_LEVEL) {
602                 generic_err(root, node, 0,
603                         "invalid level for node, have %d expect [1, %d]",
604                         level, BTRFS_MAX_LEVEL - 1);
605                 return -EUCLEAN;
606         }
607         if (nr == 0 || nr > BTRFS_NODEPTRS_PER_BLOCK(root->fs_info)) {
608                 btrfs_crit(root->fs_info,
609 "corrupt node: root=%llu block=%llu, nritems too %s, have %lu expect range [1,%u]",
610                            root->objectid, node->start,
611                            nr == 0 ? "small" : "large", nr,
612                            BTRFS_NODEPTRS_PER_BLOCK(root->fs_info));
613                 return -EUCLEAN;
614         }
615
616         for (slot = 0; slot < nr - 1; slot++) {
617                 bytenr = btrfs_node_blockptr(node, slot);
618                 btrfs_node_key_to_cpu(node, &key, slot);
619                 btrfs_node_key_to_cpu(node, &next_key, slot + 1);
620
621                 if (!bytenr) {
622                         generic_err(root, node, slot,
623                                 "invalid NULL node pointer");
624                         ret = -EUCLEAN;
625                         goto out;
626                 }
627                 if (!IS_ALIGNED(bytenr, root->fs_info->sectorsize)) {
628                         generic_err(root, node, slot,
629                         "unaligned pointer, have %llu should be aligned to %u",
630                                 bytenr, root->fs_info->sectorsize);
631                         ret = -EUCLEAN;
632                         goto out;
633                 }
634
635                 if (btrfs_comp_cpu_keys(&key, &next_key) >= 0) {
636                         generic_err(root, node, slot,
637         "bad key order, current (%llu %u %llu) next (%llu %u %llu)",
638                                 key.objectid, key.type, key.offset,
639                                 next_key.objectid, next_key.type,
640                                 next_key.offset);
641                         ret = -EUCLEAN;
642                         goto out;
643                 }
644         }
645 out:
646         return ret;
647 }