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