~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/btrfs/tree-checker.c

Version: ~ [ linux-6.11-rc3 ] ~ [ linux-6.10.4 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.45 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.104 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.164 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.223 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.281 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.319 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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 <linux/types.h>
 19 #include <linux/stddef.h>
 20 #include <linux/error-injection.h>
 21 #include "messages.h"
 22 #include "ctree.h"
 23 #include "tree-checker.h"
 24 #include "compression.h"
 25 #include "volumes.h"
 26 #include "misc.h"
 27 #include "fs.h"
 28 #include "accessors.h"
 29 #include "file-item.h"
 30 #include "inode-item.h"
 31 #include "dir-item.h"
 32 #include "extent-tree.h"
 33 
 34 /*
 35  * Error message should follow the following format:
 36  * corrupt <type>: <identifier>, <reason>[, <bad_value>]
 37  *
 38  * @type:       leaf or node
 39  * @identifier: the necessary info to locate the leaf/node.
 40  *              It's recommended to decode key.objecitd/offset if it's
 41  *              meaningful.
 42  * @reason:     describe the error
 43  * @bad_value:  optional, it's recommended to output bad value and its
 44  *              expected value (range).
 45  *
 46  * Since comma is used to separate the components, only space is allowed
 47  * inside each component.
 48  */
 49 
 50 /*
 51  * Append generic "corrupt leaf/node root=%llu block=%llu slot=%d: " to @fmt.
 52  * Allows callers to customize the output.
 53  */
 54 __printf(3, 4)
 55 __cold
 56 static void generic_err(const struct extent_buffer *eb, int slot,
 57                         const char *fmt, ...)
 58 {
 59         const struct btrfs_fs_info *fs_info = eb->fs_info;
 60         struct va_format vaf;
 61         va_list args;
 62 
 63         va_start(args, fmt);
 64 
 65         vaf.fmt = fmt;
 66         vaf.va = &args;
 67 
 68         dump_page(folio_page(eb->folios[0], 0), "eb page dump");
 69         btrfs_crit(fs_info,
 70                 "corrupt %s: root=%llu block=%llu slot=%d, %pV",
 71                 btrfs_header_level(eb) == 0 ? "leaf" : "node",
 72                 btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot, &vaf);
 73         va_end(args);
 74 }
 75 
 76 /*
 77  * Customized reporter for extent data item, since its key objectid and
 78  * offset has its own meaning.
 79  */
 80 __printf(3, 4)
 81 __cold
 82 static void file_extent_err(const struct extent_buffer *eb, int slot,
 83                             const char *fmt, ...)
 84 {
 85         const struct btrfs_fs_info *fs_info = eb->fs_info;
 86         struct btrfs_key key;
 87         struct va_format vaf;
 88         va_list args;
 89 
 90         btrfs_item_key_to_cpu(eb, &key, slot);
 91         va_start(args, fmt);
 92 
 93         vaf.fmt = fmt;
 94         vaf.va = &args;
 95 
 96         dump_page(folio_page(eb->folios[0], 0), "eb page dump");
 97         btrfs_crit(fs_info,
 98         "corrupt %s: root=%llu block=%llu slot=%d ino=%llu file_offset=%llu, %pV",
 99                 btrfs_header_level(eb) == 0 ? "leaf" : "node",
100                 btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot,
101                 key.objectid, key.offset, &vaf);
102         va_end(args);
103 }
104 
105 /*
106  * Return 0 if the btrfs_file_extent_##name is aligned to @alignment
107  * Else return 1
108  */
109 #define CHECK_FE_ALIGNED(leaf, slot, fi, name, alignment)                     \
110 ({                                                                            \
111         if (unlikely(!IS_ALIGNED(btrfs_file_extent_##name((leaf), (fi)),      \
112                                  (alignment))))                               \
113                 file_extent_err((leaf), (slot),                               \
114         "invalid %s for file extent, have %llu, should be aligned to %u",     \
115                         (#name), btrfs_file_extent_##name((leaf), (fi)),      \
116                         (alignment));                                         \
117         (!IS_ALIGNED(btrfs_file_extent_##name((leaf), (fi)), (alignment)));   \
118 })
119 
120 static u64 file_extent_end(struct extent_buffer *leaf,
121                            struct btrfs_key *key,
122                            struct btrfs_file_extent_item *extent)
123 {
124         u64 end;
125         u64 len;
126 
127         if (btrfs_file_extent_type(leaf, extent) == BTRFS_FILE_EXTENT_INLINE) {
128                 len = btrfs_file_extent_ram_bytes(leaf, extent);
129                 end = ALIGN(key->offset + len, leaf->fs_info->sectorsize);
130         } else {
131                 len = btrfs_file_extent_num_bytes(leaf, extent);
132                 end = key->offset + len;
133         }
134         return end;
135 }
136 
137 /*
138  * Customized report for dir_item, the only new important information is
139  * key->objectid, which represents inode number
140  */
141 __printf(3, 4)
142 __cold
143 static void dir_item_err(const struct extent_buffer *eb, int slot,
144                          const char *fmt, ...)
145 {
146         const struct btrfs_fs_info *fs_info = eb->fs_info;
147         struct btrfs_key key;
148         struct va_format vaf;
149         va_list args;
150 
151         btrfs_item_key_to_cpu(eb, &key, slot);
152         va_start(args, fmt);
153 
154         vaf.fmt = fmt;
155         vaf.va = &args;
156 
157         dump_page(folio_page(eb->folios[0], 0), "eb page dump");
158         btrfs_crit(fs_info,
159                 "corrupt %s: root=%llu block=%llu slot=%d ino=%llu, %pV",
160                 btrfs_header_level(eb) == 0 ? "leaf" : "node",
161                 btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot,
162                 key.objectid, &vaf);
163         va_end(args);
164 }
165 
166 /*
167  * This functions checks prev_key->objectid, to ensure current key and prev_key
168  * share the same objectid as inode number.
169  *
170  * This is to detect missing INODE_ITEM in subvolume trees.
171  *
172  * Return true if everything is OK or we don't need to check.
173  * Return false if anything is wrong.
174  */
175 static bool check_prev_ino(struct extent_buffer *leaf,
176                            struct btrfs_key *key, int slot,
177                            struct btrfs_key *prev_key)
178 {
179         /* No prev key, skip check */
180         if (slot == 0)
181                 return true;
182 
183         /* Only these key->types needs to be checked */
184         ASSERT(key->type == BTRFS_XATTR_ITEM_KEY ||
185                key->type == BTRFS_INODE_REF_KEY ||
186                key->type == BTRFS_DIR_INDEX_KEY ||
187                key->type == BTRFS_DIR_ITEM_KEY ||
188                key->type == BTRFS_EXTENT_DATA_KEY);
189 
190         /*
191          * Only subvolume trees along with their reloc trees need this check.
192          * Things like log tree doesn't follow this ino requirement.
193          */
194         if (!is_fstree(btrfs_header_owner(leaf)))
195                 return true;
196 
197         if (key->objectid == prev_key->objectid)
198                 return true;
199 
200         /* Error found */
201         dir_item_err(leaf, slot,
202                 "invalid previous key objectid, have %llu expect %llu",
203                 prev_key->objectid, key->objectid);
204         return false;
205 }
206 static int check_extent_data_item(struct extent_buffer *leaf,
207                                   struct btrfs_key *key, int slot,
208                                   struct btrfs_key *prev_key)
209 {
210         struct btrfs_fs_info *fs_info = leaf->fs_info;
211         struct btrfs_file_extent_item *fi;
212         u32 sectorsize = fs_info->sectorsize;
213         u32 item_size = btrfs_item_size(leaf, slot);
214         u64 extent_end;
215 
216         if (unlikely(!IS_ALIGNED(key->offset, sectorsize))) {
217                 file_extent_err(leaf, slot,
218 "unaligned file_offset for file extent, have %llu should be aligned to %u",
219                         key->offset, sectorsize);
220                 return -EUCLEAN;
221         }
222 
223         /*
224          * Previous key must have the same key->objectid (ino).
225          * It can be XATTR_ITEM, INODE_ITEM or just another EXTENT_DATA.
226          * But if objectids mismatch, it means we have a missing
227          * INODE_ITEM.
228          */
229         if (unlikely(!check_prev_ino(leaf, key, slot, prev_key)))
230                 return -EUCLEAN;
231 
232         fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
233 
234         /*
235          * Make sure the item contains at least inline header, so the file
236          * extent type is not some garbage.
237          */
238         if (unlikely(item_size < BTRFS_FILE_EXTENT_INLINE_DATA_START)) {
239                 file_extent_err(leaf, slot,
240                                 "invalid item size, have %u expect [%zu, %u)",
241                                 item_size, BTRFS_FILE_EXTENT_INLINE_DATA_START,
242                                 SZ_4K);
243                 return -EUCLEAN;
244         }
245         if (unlikely(btrfs_file_extent_type(leaf, fi) >=
246                      BTRFS_NR_FILE_EXTENT_TYPES)) {
247                 file_extent_err(leaf, slot,
248                 "invalid type for file extent, have %u expect range [0, %u]",
249                         btrfs_file_extent_type(leaf, fi),
250                         BTRFS_NR_FILE_EXTENT_TYPES - 1);
251                 return -EUCLEAN;
252         }
253 
254         /*
255          * Support for new compression/encryption must introduce incompat flag,
256          * and must be caught in open_ctree().
257          */
258         if (unlikely(btrfs_file_extent_compression(leaf, fi) >=
259                      BTRFS_NR_COMPRESS_TYPES)) {
260                 file_extent_err(leaf, slot,
261         "invalid compression for file extent, have %u expect range [0, %u]",
262                         btrfs_file_extent_compression(leaf, fi),
263                         BTRFS_NR_COMPRESS_TYPES - 1);
264                 return -EUCLEAN;
265         }
266         if (unlikely(btrfs_file_extent_encryption(leaf, fi))) {
267                 file_extent_err(leaf, slot,
268                         "invalid encryption for file extent, have %u expect 0",
269                         btrfs_file_extent_encryption(leaf, fi));
270                 return -EUCLEAN;
271         }
272         if (btrfs_file_extent_type(leaf, fi) == BTRFS_FILE_EXTENT_INLINE) {
273                 /* Inline extent must have 0 as key offset */
274                 if (unlikely(key->offset)) {
275                         file_extent_err(leaf, slot,
276                 "invalid file_offset for inline file extent, have %llu expect 0",
277                                 key->offset);
278                         return -EUCLEAN;
279                 }
280 
281                 /* Compressed inline extent has no on-disk size, skip it */
282                 if (btrfs_file_extent_compression(leaf, fi) !=
283                     BTRFS_COMPRESS_NONE)
284                         return 0;
285 
286                 /* Uncompressed inline extent size must match item size */
287                 if (unlikely(item_size != BTRFS_FILE_EXTENT_INLINE_DATA_START +
288                                           btrfs_file_extent_ram_bytes(leaf, fi))) {
289                         file_extent_err(leaf, slot,
290         "invalid ram_bytes for uncompressed inline extent, have %u expect %llu",
291                                 item_size, BTRFS_FILE_EXTENT_INLINE_DATA_START +
292                                 btrfs_file_extent_ram_bytes(leaf, fi));
293                         return -EUCLEAN;
294                 }
295                 return 0;
296         }
297 
298         /* Regular or preallocated extent has fixed item size */
299         if (unlikely(item_size != sizeof(*fi))) {
300                 file_extent_err(leaf, slot,
301         "invalid item size for reg/prealloc file extent, have %u expect %zu",
302                         item_size, sizeof(*fi));
303                 return -EUCLEAN;
304         }
305         if (unlikely(CHECK_FE_ALIGNED(leaf, slot, fi, ram_bytes, sectorsize) ||
306                      CHECK_FE_ALIGNED(leaf, slot, fi, disk_bytenr, sectorsize) ||
307                      CHECK_FE_ALIGNED(leaf, slot, fi, disk_num_bytes, sectorsize) ||
308                      CHECK_FE_ALIGNED(leaf, slot, fi, offset, sectorsize) ||
309                      CHECK_FE_ALIGNED(leaf, slot, fi, num_bytes, sectorsize)))
310                 return -EUCLEAN;
311 
312         /* Catch extent end overflow */
313         if (unlikely(check_add_overflow(btrfs_file_extent_num_bytes(leaf, fi),
314                                         key->offset, &extent_end))) {
315                 file_extent_err(leaf, slot,
316         "extent end overflow, have file offset %llu extent num bytes %llu",
317                                 key->offset,
318                                 btrfs_file_extent_num_bytes(leaf, fi));
319                 return -EUCLEAN;
320         }
321 
322         /*
323          * Check that no two consecutive file extent items, in the same leaf,
324          * present ranges that overlap each other.
325          */
326         if (slot > 0 &&
327             prev_key->objectid == key->objectid &&
328             prev_key->type == BTRFS_EXTENT_DATA_KEY) {
329                 struct btrfs_file_extent_item *prev_fi;
330                 u64 prev_end;
331 
332                 prev_fi = btrfs_item_ptr(leaf, slot - 1,
333                                          struct btrfs_file_extent_item);
334                 prev_end = file_extent_end(leaf, prev_key, prev_fi);
335                 if (unlikely(prev_end > key->offset)) {
336                         file_extent_err(leaf, slot - 1,
337 "file extent end range (%llu) goes beyond start offset (%llu) of the next file extent",
338                                         prev_end, key->offset);
339                         return -EUCLEAN;
340                 }
341         }
342 
343         /*
344          * For non-compressed data extents, ram_bytes should match its
345          * disk_num_bytes.
346          * However we do not really utilize ram_bytes in this case, so this check
347          * is only optional for DEBUG builds for developers to catch the
348          * unexpected behaviors.
349          */
350         if (IS_ENABLED(CONFIG_BTRFS_DEBUG) &&
351             btrfs_file_extent_compression(leaf, fi) == BTRFS_COMPRESS_NONE &&
352             btrfs_file_extent_disk_bytenr(leaf, fi)) {
353                 if (WARN_ON(btrfs_file_extent_ram_bytes(leaf, fi) !=
354                             btrfs_file_extent_disk_num_bytes(leaf, fi)))
355                         file_extent_err(leaf, slot,
356 "mismatch ram_bytes (%llu) and disk_num_bytes (%llu) for non-compressed extent",
357                                         btrfs_file_extent_ram_bytes(leaf, fi),
358                                         btrfs_file_extent_disk_num_bytes(leaf, fi));
359         }
360 
361         return 0;
362 }
363 
364 static int check_csum_item(struct extent_buffer *leaf, struct btrfs_key *key,
365                            int slot, struct btrfs_key *prev_key)
366 {
367         struct btrfs_fs_info *fs_info = leaf->fs_info;
368         u32 sectorsize = fs_info->sectorsize;
369         const u32 csumsize = fs_info->csum_size;
370 
371         if (unlikely(key->objectid != BTRFS_EXTENT_CSUM_OBJECTID)) {
372                 generic_err(leaf, slot,
373                 "invalid key objectid for csum item, have %llu expect %llu",
374                         key->objectid, BTRFS_EXTENT_CSUM_OBJECTID);
375                 return -EUCLEAN;
376         }
377         if (unlikely(!IS_ALIGNED(key->offset, sectorsize))) {
378                 generic_err(leaf, slot,
379         "unaligned key offset for csum item, have %llu should be aligned to %u",
380                         key->offset, sectorsize);
381                 return -EUCLEAN;
382         }
383         if (unlikely(!IS_ALIGNED(btrfs_item_size(leaf, slot), csumsize))) {
384                 generic_err(leaf, slot,
385         "unaligned item size for csum item, have %u should be aligned to %u",
386                         btrfs_item_size(leaf, slot), csumsize);
387                 return -EUCLEAN;
388         }
389         if (slot > 0 && prev_key->type == BTRFS_EXTENT_CSUM_KEY) {
390                 u64 prev_csum_end;
391                 u32 prev_item_size;
392 
393                 prev_item_size = btrfs_item_size(leaf, slot - 1);
394                 prev_csum_end = (prev_item_size / csumsize) * sectorsize;
395                 prev_csum_end += prev_key->offset;
396                 if (unlikely(prev_csum_end > key->offset)) {
397                         generic_err(leaf, slot - 1,
398 "csum end range (%llu) goes beyond the start range (%llu) of the next csum item",
399                                     prev_csum_end, key->offset);
400                         return -EUCLEAN;
401                 }
402         }
403         return 0;
404 }
405 
406 /* Inode item error output has the same format as dir_item_err() */
407 #define inode_item_err(eb, slot, fmt, ...)                      \
408         dir_item_err(eb, slot, fmt, __VA_ARGS__)
409 
410 static int check_inode_key(struct extent_buffer *leaf, struct btrfs_key *key,
411                            int slot)
412 {
413         struct btrfs_key item_key;
414         bool is_inode_item;
415 
416         btrfs_item_key_to_cpu(leaf, &item_key, slot);
417         is_inode_item = (item_key.type == BTRFS_INODE_ITEM_KEY);
418 
419         /* For XATTR_ITEM, location key should be all 0 */
420         if (item_key.type == BTRFS_XATTR_ITEM_KEY) {
421                 if (unlikely(key->objectid != 0 || key->type != 0 ||
422                              key->offset != 0))
423                         return -EUCLEAN;
424                 return 0;
425         }
426 
427         if (unlikely((key->objectid < BTRFS_FIRST_FREE_OBJECTID ||
428                       key->objectid > BTRFS_LAST_FREE_OBJECTID) &&
429                      key->objectid != BTRFS_ROOT_TREE_DIR_OBJECTID &&
430                      key->objectid != BTRFS_FREE_INO_OBJECTID)) {
431                 if (is_inode_item) {
432                         generic_err(leaf, slot,
433         "invalid key objectid: has %llu expect %llu or [%llu, %llu] or %llu",
434                                 key->objectid, BTRFS_ROOT_TREE_DIR_OBJECTID,
435                                 BTRFS_FIRST_FREE_OBJECTID,
436                                 BTRFS_LAST_FREE_OBJECTID,
437                                 BTRFS_FREE_INO_OBJECTID);
438                 } else {
439                         dir_item_err(leaf, slot,
440 "invalid location key objectid: has %llu expect %llu or [%llu, %llu] or %llu",
441                                 key->objectid, BTRFS_ROOT_TREE_DIR_OBJECTID,
442                                 BTRFS_FIRST_FREE_OBJECTID,
443                                 BTRFS_LAST_FREE_OBJECTID,
444                                 BTRFS_FREE_INO_OBJECTID);
445                 }
446                 return -EUCLEAN;
447         }
448         if (unlikely(key->offset != 0)) {
449                 if (is_inode_item)
450                         inode_item_err(leaf, slot,
451                                        "invalid key offset: has %llu expect 0",
452                                        key->offset);
453                 else
454                         dir_item_err(leaf, slot,
455                                 "invalid location key offset:has %llu expect 0",
456                                 key->offset);
457                 return -EUCLEAN;
458         }
459         return 0;
460 }
461 
462 static int check_root_key(struct extent_buffer *leaf, struct btrfs_key *key,
463                           int slot)
464 {
465         struct btrfs_key item_key;
466         bool is_root_item;
467 
468         btrfs_item_key_to_cpu(leaf, &item_key, slot);
469         is_root_item = (item_key.type == BTRFS_ROOT_ITEM_KEY);
470 
471         /*
472          * Bad rootid for reloc trees.
473          *
474          * Reloc trees are only for subvolume trees, other trees only need
475          * to be COWed to be relocated.
476          */
477         if (unlikely(is_root_item && key->objectid == BTRFS_TREE_RELOC_OBJECTID &&
478                      !is_fstree(key->offset))) {
479                 generic_err(leaf, slot,
480                 "invalid reloc tree for root %lld, root id is not a subvolume tree",
481                             key->offset);
482                 return -EUCLEAN;
483         }
484 
485         /* No such tree id */
486         if (unlikely(key->objectid == 0)) {
487                 if (is_root_item)
488                         generic_err(leaf, slot, "invalid root id 0");
489                 else
490                         dir_item_err(leaf, slot,
491                                      "invalid location key root id 0");
492                 return -EUCLEAN;
493         }
494 
495         /* DIR_ITEM/INDEX/INODE_REF is not allowed to point to non-fs trees */
496         if (unlikely(!is_fstree(key->objectid) && !is_root_item)) {
497                 dir_item_err(leaf, slot,
498                 "invalid location key objectid, have %llu expect [%llu, %llu]",
499                                 key->objectid, BTRFS_FIRST_FREE_OBJECTID,
500                                 BTRFS_LAST_FREE_OBJECTID);
501                 return -EUCLEAN;
502         }
503 
504         /*
505          * ROOT_ITEM with non-zero offset means this is a snapshot, created at
506          * @offset transid.
507          * Furthermore, for location key in DIR_ITEM, its offset is always -1.
508          *
509          * So here we only check offset for reloc tree whose key->offset must
510          * be a valid tree.
511          */
512         if (unlikely(key->objectid == BTRFS_TREE_RELOC_OBJECTID &&
513                      key->offset == 0)) {
514                 generic_err(leaf, slot, "invalid root id 0 for reloc tree");
515                 return -EUCLEAN;
516         }
517         return 0;
518 }
519 
520 static int check_dir_item(struct extent_buffer *leaf,
521                           struct btrfs_key *key, struct btrfs_key *prev_key,
522                           int slot)
523 {
524         struct btrfs_fs_info *fs_info = leaf->fs_info;
525         struct btrfs_dir_item *di;
526         u32 item_size = btrfs_item_size(leaf, slot);
527         u32 cur = 0;
528 
529         if (unlikely(!check_prev_ino(leaf, key, slot, prev_key)))
530                 return -EUCLEAN;
531 
532         di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
533         while (cur < item_size) {
534                 struct btrfs_key location_key;
535                 u32 name_len;
536                 u32 data_len;
537                 u32 max_name_len;
538                 u32 total_size;
539                 u32 name_hash;
540                 u8 dir_type;
541                 int ret;
542 
543                 /* header itself should not cross item boundary */
544                 if (unlikely(cur + sizeof(*di) > item_size)) {
545                         dir_item_err(leaf, slot,
546                 "dir item header crosses item boundary, have %zu boundary %u",
547                                 cur + sizeof(*di), item_size);
548                         return -EUCLEAN;
549                 }
550 
551                 /* Location key check */
552                 btrfs_dir_item_key_to_cpu(leaf, di, &location_key);
553                 if (location_key.type == BTRFS_ROOT_ITEM_KEY) {
554                         ret = check_root_key(leaf, &location_key, slot);
555                         if (unlikely(ret < 0))
556                                 return ret;
557                 } else if (location_key.type == BTRFS_INODE_ITEM_KEY ||
558                            location_key.type == 0) {
559                         ret = check_inode_key(leaf, &location_key, slot);
560                         if (unlikely(ret < 0))
561                                 return ret;
562                 } else {
563                         dir_item_err(leaf, slot,
564                         "invalid location key type, have %u, expect %u or %u",
565                                      location_key.type, BTRFS_ROOT_ITEM_KEY,
566                                      BTRFS_INODE_ITEM_KEY);
567                         return -EUCLEAN;
568                 }
569 
570                 /* dir type check */
571                 dir_type = btrfs_dir_ftype(leaf, di);
572                 if (unlikely(dir_type >= BTRFS_FT_MAX)) {
573                         dir_item_err(leaf, slot,
574                         "invalid dir item type, have %u expect [0, %u)",
575                                 dir_type, BTRFS_FT_MAX);
576                         return -EUCLEAN;
577                 }
578 
579                 if (unlikely(key->type == BTRFS_XATTR_ITEM_KEY &&
580                              dir_type != BTRFS_FT_XATTR)) {
581                         dir_item_err(leaf, slot,
582                 "invalid dir item type for XATTR key, have %u expect %u",
583                                 dir_type, BTRFS_FT_XATTR);
584                         return -EUCLEAN;
585                 }
586                 if (unlikely(dir_type == BTRFS_FT_XATTR &&
587                              key->type != BTRFS_XATTR_ITEM_KEY)) {
588                         dir_item_err(leaf, slot,
589                         "xattr dir type found for non-XATTR key");
590                         return -EUCLEAN;
591                 }
592                 if (dir_type == BTRFS_FT_XATTR)
593                         max_name_len = XATTR_NAME_MAX;
594                 else
595                         max_name_len = BTRFS_NAME_LEN;
596 
597                 /* Name/data length check */
598                 name_len = btrfs_dir_name_len(leaf, di);
599                 data_len = btrfs_dir_data_len(leaf, di);
600                 if (unlikely(name_len > max_name_len)) {
601                         dir_item_err(leaf, slot,
602                         "dir item name len too long, have %u max %u",
603                                 name_len, max_name_len);
604                         return -EUCLEAN;
605                 }
606                 if (unlikely(name_len + data_len > BTRFS_MAX_XATTR_SIZE(fs_info))) {
607                         dir_item_err(leaf, slot,
608                         "dir item name and data len too long, have %u max %u",
609                                 name_len + data_len,
610                                 BTRFS_MAX_XATTR_SIZE(fs_info));
611                         return -EUCLEAN;
612                 }
613 
614                 if (unlikely(data_len && dir_type != BTRFS_FT_XATTR)) {
615                         dir_item_err(leaf, slot,
616                         "dir item with invalid data len, have %u expect 0",
617                                 data_len);
618                         return -EUCLEAN;
619                 }
620 
621                 total_size = sizeof(*di) + name_len + data_len;
622 
623                 /* header and name/data should not cross item boundary */
624                 if (unlikely(cur + total_size > item_size)) {
625                         dir_item_err(leaf, slot,
626                 "dir item data crosses item boundary, have %u boundary %u",
627                                 cur + total_size, item_size);
628                         return -EUCLEAN;
629                 }
630 
631                 /*
632                  * Special check for XATTR/DIR_ITEM, as key->offset is name
633                  * hash, should match its name
634                  */
635                 if (key->type == BTRFS_DIR_ITEM_KEY ||
636                     key->type == BTRFS_XATTR_ITEM_KEY) {
637                         char namebuf[MAX(BTRFS_NAME_LEN, XATTR_NAME_MAX)];
638 
639                         read_extent_buffer(leaf, namebuf,
640                                         (unsigned long)(di + 1), name_len);
641                         name_hash = btrfs_name_hash(namebuf, name_len);
642                         if (unlikely(key->offset != name_hash)) {
643                                 dir_item_err(leaf, slot,
644                 "name hash mismatch with key, have 0x%016x expect 0x%016llx",
645                                         name_hash, key->offset);
646                                 return -EUCLEAN;
647                         }
648                 }
649                 cur += total_size;
650                 di = (struct btrfs_dir_item *)((void *)di + total_size);
651         }
652         return 0;
653 }
654 
655 __printf(3, 4)
656 __cold
657 static void block_group_err(const struct extent_buffer *eb, int slot,
658                             const char *fmt, ...)
659 {
660         const struct btrfs_fs_info *fs_info = eb->fs_info;
661         struct btrfs_key key;
662         struct va_format vaf;
663         va_list args;
664 
665         btrfs_item_key_to_cpu(eb, &key, slot);
666         va_start(args, fmt);
667 
668         vaf.fmt = fmt;
669         vaf.va = &args;
670 
671         dump_page(folio_page(eb->folios[0], 0), "eb page dump");
672         btrfs_crit(fs_info,
673         "corrupt %s: root=%llu block=%llu slot=%d bg_start=%llu bg_len=%llu, %pV",
674                 btrfs_header_level(eb) == 0 ? "leaf" : "node",
675                 btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot,
676                 key.objectid, key.offset, &vaf);
677         va_end(args);
678 }
679 
680 static int check_block_group_item(struct extent_buffer *leaf,
681                                   struct btrfs_key *key, int slot)
682 {
683         struct btrfs_fs_info *fs_info = leaf->fs_info;
684         struct btrfs_block_group_item bgi;
685         u32 item_size = btrfs_item_size(leaf, slot);
686         u64 chunk_objectid;
687         u64 flags;
688         u64 type;
689 
690         /*
691          * Here we don't really care about alignment since extent allocator can
692          * handle it.  We care more about the size.
693          */
694         if (unlikely(key->offset == 0)) {
695                 block_group_err(leaf, slot,
696                                 "invalid block group size 0");
697                 return -EUCLEAN;
698         }
699 
700         if (unlikely(item_size != sizeof(bgi))) {
701                 block_group_err(leaf, slot,
702                         "invalid item size, have %u expect %zu",
703                                 item_size, sizeof(bgi));
704                 return -EUCLEAN;
705         }
706 
707         read_extent_buffer(leaf, &bgi, btrfs_item_ptr_offset(leaf, slot),
708                            sizeof(bgi));
709         chunk_objectid = btrfs_stack_block_group_chunk_objectid(&bgi);
710         if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) {
711                 /*
712                  * We don't init the nr_global_roots until we load the global
713                  * roots, so this could be 0 at mount time.  If it's 0 we'll
714                  * just assume we're fine, and later we'll check against our
715                  * actual value.
716                  */
717                 if (unlikely(fs_info->nr_global_roots &&
718                              chunk_objectid >= fs_info->nr_global_roots)) {
719                         block_group_err(leaf, slot,
720         "invalid block group global root id, have %llu, needs to be <= %llu",
721                                         chunk_objectid,
722                                         fs_info->nr_global_roots);
723                         return -EUCLEAN;
724                 }
725         } else if (unlikely(chunk_objectid != BTRFS_FIRST_CHUNK_TREE_OBJECTID)) {
726                 block_group_err(leaf, slot,
727                 "invalid block group chunk objectid, have %llu expect %llu",
728                                 btrfs_stack_block_group_chunk_objectid(&bgi),
729                                 BTRFS_FIRST_CHUNK_TREE_OBJECTID);
730                 return -EUCLEAN;
731         }
732 
733         if (unlikely(btrfs_stack_block_group_used(&bgi) > key->offset)) {
734                 block_group_err(leaf, slot,
735                         "invalid block group used, have %llu expect [0, %llu)",
736                                 btrfs_stack_block_group_used(&bgi), key->offset);
737                 return -EUCLEAN;
738         }
739 
740         flags = btrfs_stack_block_group_flags(&bgi);
741         if (unlikely(hweight64(flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) > 1)) {
742                 block_group_err(leaf, slot,
743 "invalid profile flags, have 0x%llx (%lu bits set) expect no more than 1 bit set",
744                         flags & BTRFS_BLOCK_GROUP_PROFILE_MASK,
745                         hweight64(flags & BTRFS_BLOCK_GROUP_PROFILE_MASK));
746                 return -EUCLEAN;
747         }
748 
749         type = flags & BTRFS_BLOCK_GROUP_TYPE_MASK;
750         if (unlikely(type != BTRFS_BLOCK_GROUP_DATA &&
751                      type != BTRFS_BLOCK_GROUP_METADATA &&
752                      type != BTRFS_BLOCK_GROUP_SYSTEM &&
753                      type != (BTRFS_BLOCK_GROUP_METADATA |
754                               BTRFS_BLOCK_GROUP_DATA))) {
755                 block_group_err(leaf, slot,
756 "invalid type, have 0x%llx (%lu bits set) expect either 0x%llx, 0x%llx, 0x%llx or 0x%llx",
757                         type, hweight64(type),
758                         BTRFS_BLOCK_GROUP_DATA, BTRFS_BLOCK_GROUP_METADATA,
759                         BTRFS_BLOCK_GROUP_SYSTEM,
760                         BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA);
761                 return -EUCLEAN;
762         }
763         return 0;
764 }
765 
766 __printf(4, 5)
767 __cold
768 static void chunk_err(const struct extent_buffer *leaf,
769                       const struct btrfs_chunk *chunk, u64 logical,
770                       const char *fmt, ...)
771 {
772         const struct btrfs_fs_info *fs_info = leaf->fs_info;
773         bool is_sb;
774         struct va_format vaf;
775         va_list args;
776         int i;
777         int slot = -1;
778 
779         /* Only superblock eb is able to have such small offset */
780         is_sb = (leaf->start == BTRFS_SUPER_INFO_OFFSET);
781 
782         if (!is_sb) {
783                 /*
784                  * Get the slot number by iterating through all slots, this
785                  * would provide better readability.
786                  */
787                 for (i = 0; i < btrfs_header_nritems(leaf); i++) {
788                         if (btrfs_item_ptr_offset(leaf, i) ==
789                                         (unsigned long)chunk) {
790                                 slot = i;
791                                 break;
792                         }
793                 }
794         }
795         va_start(args, fmt);
796         vaf.fmt = fmt;
797         vaf.va = &args;
798 
799         if (is_sb)
800                 btrfs_crit(fs_info,
801                 "corrupt superblock syschunk array: chunk_start=%llu, %pV",
802                            logical, &vaf);
803         else
804                 btrfs_crit(fs_info,
805         "corrupt leaf: root=%llu block=%llu slot=%d chunk_start=%llu, %pV",
806                            BTRFS_CHUNK_TREE_OBJECTID, leaf->start, slot,
807                            logical, &vaf);
808         va_end(args);
809 }
810 
811 /*
812  * The common chunk check which could also work on super block sys chunk array.
813  *
814  * Return -EUCLEAN if anything is corrupted.
815  * Return 0 if everything is OK.
816  */
817 int btrfs_check_chunk_valid(struct extent_buffer *leaf,
818                             struct btrfs_chunk *chunk, u64 logical)
819 {
820         struct btrfs_fs_info *fs_info = leaf->fs_info;
821         u64 length;
822         u64 chunk_end;
823         u64 stripe_len;
824         u16 num_stripes;
825         u16 sub_stripes;
826         u64 type;
827         u64 features;
828         bool mixed = false;
829         int raid_index;
830         int nparity;
831         int ncopies;
832 
833         length = btrfs_chunk_length(leaf, chunk);
834         stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
835         num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
836         sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
837         type = btrfs_chunk_type(leaf, chunk);
838         raid_index = btrfs_bg_flags_to_raid_index(type);
839         ncopies = btrfs_raid_array[raid_index].ncopies;
840         nparity = btrfs_raid_array[raid_index].nparity;
841 
842         if (unlikely(!num_stripes)) {
843                 chunk_err(leaf, chunk, logical,
844                           "invalid chunk num_stripes, have %u", num_stripes);
845                 return -EUCLEAN;
846         }
847         if (unlikely(num_stripes < ncopies)) {
848                 chunk_err(leaf, chunk, logical,
849                           "invalid chunk num_stripes < ncopies, have %u < %d",
850                           num_stripes, ncopies);
851                 return -EUCLEAN;
852         }
853         if (unlikely(nparity && num_stripes == nparity)) {
854                 chunk_err(leaf, chunk, logical,
855                           "invalid chunk num_stripes == nparity, have %u == %d",
856                           num_stripes, nparity);
857                 return -EUCLEAN;
858         }
859         if (unlikely(!IS_ALIGNED(logical, fs_info->sectorsize))) {
860                 chunk_err(leaf, chunk, logical,
861                 "invalid chunk logical, have %llu should aligned to %u",
862                           logical, fs_info->sectorsize);
863                 return -EUCLEAN;
864         }
865         if (unlikely(btrfs_chunk_sector_size(leaf, chunk) != fs_info->sectorsize)) {
866                 chunk_err(leaf, chunk, logical,
867                           "invalid chunk sectorsize, have %u expect %u",
868                           btrfs_chunk_sector_size(leaf, chunk),
869                           fs_info->sectorsize);
870                 return -EUCLEAN;
871         }
872         if (unlikely(!length || !IS_ALIGNED(length, fs_info->sectorsize))) {
873                 chunk_err(leaf, chunk, logical,
874                           "invalid chunk length, have %llu", length);
875                 return -EUCLEAN;
876         }
877         if (unlikely(check_add_overflow(logical, length, &chunk_end))) {
878                 chunk_err(leaf, chunk, logical,
879 "invalid chunk logical start and length, have logical start %llu length %llu",
880                           logical, length);
881                 return -EUCLEAN;
882         }
883         if (unlikely(!is_power_of_2(stripe_len) || stripe_len != BTRFS_STRIPE_LEN)) {
884                 chunk_err(leaf, chunk, logical,
885                           "invalid chunk stripe length: %llu",
886                           stripe_len);
887                 return -EUCLEAN;
888         }
889         /*
890          * We artificially limit the chunk size, so that the number of stripes
891          * inside a chunk can be fit into a U32.  The current limit (256G) is
892          * way too large for real world usage anyway, and it's also much larger
893          * than our existing limit (10G).
894          *
895          * Thus it should be a good way to catch obvious bitflips.
896          */
897         if (unlikely(length >= btrfs_stripe_nr_to_offset(U32_MAX))) {
898                 chunk_err(leaf, chunk, logical,
899                           "chunk length too large: have %llu limit %llu",
900                           length, btrfs_stripe_nr_to_offset(U32_MAX));
901                 return -EUCLEAN;
902         }
903         if (unlikely(type & ~(BTRFS_BLOCK_GROUP_TYPE_MASK |
904                               BTRFS_BLOCK_GROUP_PROFILE_MASK))) {
905                 chunk_err(leaf, chunk, logical,
906                           "unrecognized chunk type: 0x%llx",
907                           ~(BTRFS_BLOCK_GROUP_TYPE_MASK |
908                             BTRFS_BLOCK_GROUP_PROFILE_MASK) &
909                           btrfs_chunk_type(leaf, chunk));
910                 return -EUCLEAN;
911         }
912 
913         if (unlikely(!has_single_bit_set(type & BTRFS_BLOCK_GROUP_PROFILE_MASK) &&
914                      (type & BTRFS_BLOCK_GROUP_PROFILE_MASK) != 0)) {
915                 chunk_err(leaf, chunk, logical,
916                 "invalid chunk profile flag: 0x%llx, expect 0 or 1 bit set",
917                           type & BTRFS_BLOCK_GROUP_PROFILE_MASK);
918                 return -EUCLEAN;
919         }
920         if (unlikely((type & BTRFS_BLOCK_GROUP_TYPE_MASK) == 0)) {
921                 chunk_err(leaf, chunk, logical,
922         "missing chunk type flag, have 0x%llx one bit must be set in 0x%llx",
923                           type, BTRFS_BLOCK_GROUP_TYPE_MASK);
924                 return -EUCLEAN;
925         }
926 
927         if (unlikely((type & BTRFS_BLOCK_GROUP_SYSTEM) &&
928                      (type & (BTRFS_BLOCK_GROUP_METADATA |
929                               BTRFS_BLOCK_GROUP_DATA)))) {
930                 chunk_err(leaf, chunk, logical,
931                           "system chunk with data or metadata type: 0x%llx",
932                           type);
933                 return -EUCLEAN;
934         }
935 
936         features = btrfs_super_incompat_flags(fs_info->super_copy);
937         if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
938                 mixed = true;
939 
940         if (!mixed) {
941                 if (unlikely((type & BTRFS_BLOCK_GROUP_METADATA) &&
942                              (type & BTRFS_BLOCK_GROUP_DATA))) {
943                         chunk_err(leaf, chunk, logical,
944                         "mixed chunk type in non-mixed mode: 0x%llx", type);
945                         return -EUCLEAN;
946                 }
947         }
948 
949         if (unlikely((type & BTRFS_BLOCK_GROUP_RAID10 &&
950                       sub_stripes != btrfs_raid_array[BTRFS_RAID_RAID10].sub_stripes) ||
951                      (type & BTRFS_BLOCK_GROUP_RAID1 &&
952                       num_stripes != btrfs_raid_array[BTRFS_RAID_RAID1].devs_min) ||
953                      (type & BTRFS_BLOCK_GROUP_RAID1C3 &&
954                       num_stripes != btrfs_raid_array[BTRFS_RAID_RAID1C3].devs_min) ||
955                      (type & BTRFS_BLOCK_GROUP_RAID1C4 &&
956                       num_stripes != btrfs_raid_array[BTRFS_RAID_RAID1C4].devs_min) ||
957                      (type & BTRFS_BLOCK_GROUP_RAID5 &&
958                       num_stripes < btrfs_raid_array[BTRFS_RAID_RAID5].devs_min) ||
959                      (type & BTRFS_BLOCK_GROUP_RAID6 &&
960                       num_stripes < btrfs_raid_array[BTRFS_RAID_RAID6].devs_min) ||
961                      (type & BTRFS_BLOCK_GROUP_DUP &&
962                       num_stripes != btrfs_raid_array[BTRFS_RAID_DUP].dev_stripes) ||
963                      ((type & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 &&
964                       num_stripes != btrfs_raid_array[BTRFS_RAID_SINGLE].dev_stripes))) {
965                 chunk_err(leaf, chunk, logical,
966                         "invalid num_stripes:sub_stripes %u:%u for profile %llu",
967                         num_stripes, sub_stripes,
968                         type & BTRFS_BLOCK_GROUP_PROFILE_MASK);
969                 return -EUCLEAN;
970         }
971 
972         return 0;
973 }
974 
975 /*
976  * Enhanced version of chunk item checker.
977  *
978  * The common btrfs_check_chunk_valid() doesn't check item size since it needs
979  * to work on super block sys_chunk_array which doesn't have full item ptr.
980  */
981 static int check_leaf_chunk_item(struct extent_buffer *leaf,
982                                  struct btrfs_chunk *chunk,
983                                  struct btrfs_key *key, int slot)
984 {
985         int num_stripes;
986 
987         if (unlikely(btrfs_item_size(leaf, slot) < sizeof(struct btrfs_chunk))) {
988                 chunk_err(leaf, chunk, key->offset,
989                         "invalid chunk item size: have %u expect [%zu, %u)",
990                         btrfs_item_size(leaf, slot),
991                         sizeof(struct btrfs_chunk),
992                         BTRFS_LEAF_DATA_SIZE(leaf->fs_info));
993                 return -EUCLEAN;
994         }
995 
996         num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
997         /* Let btrfs_check_chunk_valid() handle this error type */
998         if (num_stripes == 0)
999                 goto out;
1000 
1001         if (unlikely(btrfs_chunk_item_size(num_stripes) !=
1002                      btrfs_item_size(leaf, slot))) {
1003                 chunk_err(leaf, chunk, key->offset,
1004                         "invalid chunk item size: have %u expect %lu",
1005                         btrfs_item_size(leaf, slot),
1006                         btrfs_chunk_item_size(num_stripes));
1007                 return -EUCLEAN;
1008         }
1009 out:
1010         return btrfs_check_chunk_valid(leaf, chunk, key->offset);
1011 }
1012 
1013 __printf(3, 4)
1014 __cold
1015 static void dev_item_err(const struct extent_buffer *eb, int slot,
1016                          const char *fmt, ...)
1017 {
1018         struct btrfs_key key;
1019         struct va_format vaf;
1020         va_list args;
1021 
1022         btrfs_item_key_to_cpu(eb, &key, slot);
1023         va_start(args, fmt);
1024 
1025         vaf.fmt = fmt;
1026         vaf.va = &args;
1027 
1028         dump_page(folio_page(eb->folios[0], 0), "eb page dump");
1029         btrfs_crit(eb->fs_info,
1030         "corrupt %s: root=%llu block=%llu slot=%d devid=%llu %pV",
1031                 btrfs_header_level(eb) == 0 ? "leaf" : "node",
1032                 btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot,
1033                 key.objectid, &vaf);
1034         va_end(args);
1035 }
1036 
1037 static int check_dev_item(struct extent_buffer *leaf,
1038                           struct btrfs_key *key, int slot)
1039 {
1040         struct btrfs_dev_item *ditem;
1041         const u32 item_size = btrfs_item_size(leaf, slot);
1042 
1043         if (unlikely(key->objectid != BTRFS_DEV_ITEMS_OBJECTID)) {
1044                 dev_item_err(leaf, slot,
1045                              "invalid objectid: has=%llu expect=%llu",
1046                              key->objectid, BTRFS_DEV_ITEMS_OBJECTID);
1047                 return -EUCLEAN;
1048         }
1049 
1050         if (unlikely(item_size != sizeof(*ditem))) {
1051                 dev_item_err(leaf, slot, "invalid item size: has %u expect %zu",
1052                              item_size, sizeof(*ditem));
1053                 return -EUCLEAN;
1054         }
1055 
1056         ditem = btrfs_item_ptr(leaf, slot, struct btrfs_dev_item);
1057         if (unlikely(btrfs_device_id(leaf, ditem) != key->offset)) {
1058                 dev_item_err(leaf, slot,
1059                              "devid mismatch: key has=%llu item has=%llu",
1060                              key->offset, btrfs_device_id(leaf, ditem));
1061                 return -EUCLEAN;
1062         }
1063 
1064         /*
1065          * For device total_bytes, we don't have reliable way to check it, as
1066          * it can be 0 for device removal. Device size check can only be done
1067          * by dev extents check.
1068          */
1069         if (unlikely(btrfs_device_bytes_used(leaf, ditem) >
1070                      btrfs_device_total_bytes(leaf, ditem))) {
1071                 dev_item_err(leaf, slot,
1072                              "invalid bytes used: have %llu expect [0, %llu]",
1073                              btrfs_device_bytes_used(leaf, ditem),
1074                              btrfs_device_total_bytes(leaf, ditem));
1075                 return -EUCLEAN;
1076         }
1077         /*
1078          * Remaining members like io_align/type/gen/dev_group aren't really
1079          * utilized.  Skip them to make later usage of them easier.
1080          */
1081         return 0;
1082 }
1083 
1084 static int check_inode_item(struct extent_buffer *leaf,
1085                             struct btrfs_key *key, int slot)
1086 {
1087         struct btrfs_fs_info *fs_info = leaf->fs_info;
1088         struct btrfs_inode_item *iitem;
1089         u64 super_gen = btrfs_super_generation(fs_info->super_copy);
1090         u32 valid_mask = (S_IFMT | S_ISUID | S_ISGID | S_ISVTX | 0777);
1091         const u32 item_size = btrfs_item_size(leaf, slot);
1092         u32 mode;
1093         int ret;
1094         u32 flags;
1095         u32 ro_flags;
1096 
1097         ret = check_inode_key(leaf, key, slot);
1098         if (unlikely(ret < 0))
1099                 return ret;
1100 
1101         if (unlikely(item_size != sizeof(*iitem))) {
1102                 generic_err(leaf, slot, "invalid item size: has %u expect %zu",
1103                             item_size, sizeof(*iitem));
1104                 return -EUCLEAN;
1105         }
1106 
1107         iitem = btrfs_item_ptr(leaf, slot, struct btrfs_inode_item);
1108 
1109         /* Here we use super block generation + 1 to handle log tree */
1110         if (unlikely(btrfs_inode_generation(leaf, iitem) > super_gen + 1)) {
1111                 inode_item_err(leaf, slot,
1112                         "invalid inode generation: has %llu expect (0, %llu]",
1113                                btrfs_inode_generation(leaf, iitem),
1114                                super_gen + 1);
1115                 return -EUCLEAN;
1116         }
1117         /* Note for ROOT_TREE_DIR_ITEM, mkfs could set its transid 0 */
1118         if (unlikely(btrfs_inode_transid(leaf, iitem) > super_gen + 1)) {
1119                 inode_item_err(leaf, slot,
1120                         "invalid inode transid: has %llu expect [0, %llu]",
1121                                btrfs_inode_transid(leaf, iitem), super_gen + 1);
1122                 return -EUCLEAN;
1123         }
1124 
1125         /*
1126          * For size and nbytes it's better not to be too strict, as for dir
1127          * item its size/nbytes can easily get wrong, but doesn't affect
1128          * anything in the fs. So here we skip the check.
1129          */
1130         mode = btrfs_inode_mode(leaf, iitem);
1131         if (unlikely(mode & ~valid_mask)) {
1132                 inode_item_err(leaf, slot,
1133                                "unknown mode bit detected: 0x%x",
1134                                mode & ~valid_mask);
1135                 return -EUCLEAN;
1136         }
1137 
1138         /*
1139          * S_IFMT is not bit mapped so we can't completely rely on
1140          * is_power_of_2/has_single_bit_set, but it can save us from checking
1141          * FIFO/CHR/DIR/REG.  Only needs to check BLK, LNK and SOCKS
1142          */
1143         if (!has_single_bit_set(mode & S_IFMT)) {
1144                 if (unlikely(!S_ISLNK(mode) && !S_ISBLK(mode) && !S_ISSOCK(mode))) {
1145                         inode_item_err(leaf, slot,
1146                         "invalid mode: has 0%o expect valid S_IF* bit(s)",
1147                                        mode & S_IFMT);
1148                         return -EUCLEAN;
1149                 }
1150         }
1151         if (unlikely(S_ISDIR(mode) && btrfs_inode_nlink(leaf, iitem) > 1)) {
1152                 inode_item_err(leaf, slot,
1153                        "invalid nlink: has %u expect no more than 1 for dir",
1154                         btrfs_inode_nlink(leaf, iitem));
1155                 return -EUCLEAN;
1156         }
1157         btrfs_inode_split_flags(btrfs_inode_flags(leaf, iitem), &flags, &ro_flags);
1158         if (unlikely(flags & ~BTRFS_INODE_FLAG_MASK)) {
1159                 inode_item_err(leaf, slot,
1160                                "unknown incompat flags detected: 0x%x", flags);
1161                 return -EUCLEAN;
1162         }
1163         if (unlikely(!sb_rdonly(fs_info->sb) &&
1164                      (ro_flags & ~BTRFS_INODE_RO_FLAG_MASK))) {
1165                 inode_item_err(leaf, slot,
1166                         "unknown ro-compat flags detected on writeable mount: 0x%x",
1167                         ro_flags);
1168                 return -EUCLEAN;
1169         }
1170         return 0;
1171 }
1172 
1173 static int check_root_item(struct extent_buffer *leaf, struct btrfs_key *key,
1174                            int slot)
1175 {
1176         struct btrfs_fs_info *fs_info = leaf->fs_info;
1177         struct btrfs_root_item ri = { 0 };
1178         const u64 valid_root_flags = BTRFS_ROOT_SUBVOL_RDONLY |
1179                                      BTRFS_ROOT_SUBVOL_DEAD;
1180         int ret;
1181 
1182         ret = check_root_key(leaf, key, slot);
1183         if (unlikely(ret < 0))
1184                 return ret;
1185 
1186         if (unlikely(btrfs_item_size(leaf, slot) != sizeof(ri) &&
1187                      btrfs_item_size(leaf, slot) !=
1188                      btrfs_legacy_root_item_size())) {
1189                 generic_err(leaf, slot,
1190                             "invalid root item size, have %u expect %zu or %u",
1191                             btrfs_item_size(leaf, slot), sizeof(ri),
1192                             btrfs_legacy_root_item_size());
1193                 return -EUCLEAN;
1194         }
1195 
1196         /*
1197          * For legacy root item, the members starting at generation_v2 will be
1198          * all filled with 0.
1199          * And since we allow geneartion_v2 as 0, it will still pass the check.
1200          */
1201         read_extent_buffer(leaf, &ri, btrfs_item_ptr_offset(leaf, slot),
1202                            btrfs_item_size(leaf, slot));
1203 
1204         /* Generation related */
1205         if (unlikely(btrfs_root_generation(&ri) >
1206                      btrfs_super_generation(fs_info->super_copy) + 1)) {
1207                 generic_err(leaf, slot,
1208                         "invalid root generation, have %llu expect (0, %llu]",
1209                             btrfs_root_generation(&ri),
1210                             btrfs_super_generation(fs_info->super_copy) + 1);
1211                 return -EUCLEAN;
1212         }
1213         if (unlikely(btrfs_root_generation_v2(&ri) >
1214                      btrfs_super_generation(fs_info->super_copy) + 1)) {
1215                 generic_err(leaf, slot,
1216                 "invalid root v2 generation, have %llu expect (0, %llu]",
1217                             btrfs_root_generation_v2(&ri),
1218                             btrfs_super_generation(fs_info->super_copy) + 1);
1219                 return -EUCLEAN;
1220         }
1221         if (unlikely(btrfs_root_last_snapshot(&ri) >
1222                      btrfs_super_generation(fs_info->super_copy) + 1)) {
1223                 generic_err(leaf, slot,
1224                 "invalid root last_snapshot, have %llu expect (0, %llu]",
1225                             btrfs_root_last_snapshot(&ri),
1226                             btrfs_super_generation(fs_info->super_copy) + 1);
1227                 return -EUCLEAN;
1228         }
1229 
1230         /* Alignment and level check */
1231         if (unlikely(!IS_ALIGNED(btrfs_root_bytenr(&ri), fs_info->sectorsize))) {
1232                 generic_err(leaf, slot,
1233                 "invalid root bytenr, have %llu expect to be aligned to %u",
1234                             btrfs_root_bytenr(&ri), fs_info->sectorsize);
1235                 return -EUCLEAN;
1236         }
1237         if (unlikely(btrfs_root_level(&ri) >= BTRFS_MAX_LEVEL)) {
1238                 generic_err(leaf, slot,
1239                             "invalid root level, have %u expect [0, %u]",
1240                             btrfs_root_level(&ri), BTRFS_MAX_LEVEL - 1);
1241                 return -EUCLEAN;
1242         }
1243         if (unlikely(btrfs_root_drop_level(&ri) >= BTRFS_MAX_LEVEL)) {
1244                 generic_err(leaf, slot,
1245                             "invalid root level, have %u expect [0, %u]",
1246                             btrfs_root_drop_level(&ri), BTRFS_MAX_LEVEL - 1);
1247                 return -EUCLEAN;
1248         }
1249 
1250         /* Flags check */
1251         if (unlikely(btrfs_root_flags(&ri) & ~valid_root_flags)) {
1252                 generic_err(leaf, slot,
1253                             "invalid root flags, have 0x%llx expect mask 0x%llx",
1254                             btrfs_root_flags(&ri), valid_root_flags);
1255                 return -EUCLEAN;
1256         }
1257         return 0;
1258 }
1259 
1260 __printf(3,4)
1261 __cold
1262 static void extent_err(const struct extent_buffer *eb, int slot,
1263                        const char *fmt, ...)
1264 {
1265         struct btrfs_key key;
1266         struct va_format vaf;
1267         va_list args;
1268         u64 bytenr;
1269         u64 len;
1270 
1271         btrfs_item_key_to_cpu(eb, &key, slot);
1272         bytenr = key.objectid;
1273         if (key.type == BTRFS_METADATA_ITEM_KEY ||
1274             key.type == BTRFS_TREE_BLOCK_REF_KEY ||
1275             key.type == BTRFS_SHARED_BLOCK_REF_KEY)
1276                 len = eb->fs_info->nodesize;
1277         else
1278                 len = key.offset;
1279         va_start(args, fmt);
1280 
1281         vaf.fmt = fmt;
1282         vaf.va = &args;
1283 
1284         dump_page(folio_page(eb->folios[0], 0), "eb page dump");
1285         btrfs_crit(eb->fs_info,
1286         "corrupt %s: block=%llu slot=%d extent bytenr=%llu len=%llu %pV",
1287                 btrfs_header_level(eb) == 0 ? "leaf" : "node",
1288                 eb->start, slot, bytenr, len, &vaf);
1289         va_end(args);
1290 }
1291 
1292 static bool is_valid_dref_root(u64 rootid)
1293 {
1294         /*
1295          * The following tree root objectids are allowed to have a data backref:
1296          * - subvolume trees
1297          * - data reloc tree
1298          * - tree root
1299          *   For v1 space cache
1300          */
1301         return is_fstree(rootid) || rootid == BTRFS_DATA_RELOC_TREE_OBJECTID ||
1302                rootid == BTRFS_ROOT_TREE_OBJECTID;
1303 }
1304 
1305 static int check_extent_item(struct extent_buffer *leaf,
1306                              struct btrfs_key *key, int slot,
1307                              struct btrfs_key *prev_key)
1308 {
1309         struct btrfs_fs_info *fs_info = leaf->fs_info;
1310         struct btrfs_extent_item *ei;
1311         bool is_tree_block = false;
1312         unsigned long ptr;      /* Current pointer inside inline refs */
1313         unsigned long end;      /* Extent item end */
1314         const u32 item_size = btrfs_item_size(leaf, slot);
1315         u8 last_type = 0;
1316         u64 last_seq = U64_MAX;
1317         u64 flags;
1318         u64 generation;
1319         u64 total_refs;         /* Total refs in btrfs_extent_item */
1320         u64 inline_refs = 0;    /* found total inline refs */
1321 
1322         if (unlikely(key->type == BTRFS_METADATA_ITEM_KEY &&
1323                      !btrfs_fs_incompat(fs_info, SKINNY_METADATA))) {
1324                 generic_err(leaf, slot,
1325 "invalid key type, METADATA_ITEM type invalid when SKINNY_METADATA feature disabled");
1326                 return -EUCLEAN;
1327         }
1328         /* key->objectid is the bytenr for both key types */
1329         if (unlikely(!IS_ALIGNED(key->objectid, fs_info->sectorsize))) {
1330                 generic_err(leaf, slot,
1331                 "invalid key objectid, have %llu expect to be aligned to %u",
1332                            key->objectid, fs_info->sectorsize);
1333                 return -EUCLEAN;
1334         }
1335 
1336         /* key->offset is tree level for METADATA_ITEM_KEY */
1337         if (unlikely(key->type == BTRFS_METADATA_ITEM_KEY &&
1338                      key->offset >= BTRFS_MAX_LEVEL)) {
1339                 extent_err(leaf, slot,
1340                            "invalid tree level, have %llu expect [0, %u]",
1341                            key->offset, BTRFS_MAX_LEVEL - 1);
1342                 return -EUCLEAN;
1343         }
1344 
1345         /*
1346          * EXTENT/METADATA_ITEM consists of:
1347          * 1) One btrfs_extent_item
1348          *    Records the total refs, type and generation of the extent.
1349          *
1350          * 2) One btrfs_tree_block_info (for EXTENT_ITEM and tree backref only)
1351          *    Records the first key and level of the tree block.
1352          *
1353          * 2) Zero or more btrfs_extent_inline_ref(s)
1354          *    Each inline ref has one btrfs_extent_inline_ref shows:
1355          *    2.1) The ref type, one of the 4
1356          *         TREE_BLOCK_REF       Tree block only
1357          *         SHARED_BLOCK_REF     Tree block only
1358          *         EXTENT_DATA_REF      Data only
1359          *         SHARED_DATA_REF      Data only
1360          *    2.2) Ref type specific data
1361          *         Either using btrfs_extent_inline_ref::offset, or specific
1362          *         data structure.
1363          *
1364          *    All above inline items should follow the order:
1365          *
1366          *    - All btrfs_extent_inline_ref::type should be in an ascending
1367          *      order
1368          *
1369          *    - Within the same type, the items should follow a descending
1370          *      order by their sequence number. The sequence number is
1371          *      determined by:
1372          *      * btrfs_extent_inline_ref::offset for all types  other than
1373          *        EXTENT_DATA_REF
1374          *      * hash_extent_data_ref() for EXTENT_DATA_REF
1375          */
1376         if (unlikely(item_size < sizeof(*ei))) {
1377                 extent_err(leaf, slot,
1378                            "invalid item size, have %u expect [%zu, %u)",
1379                            item_size, sizeof(*ei),
1380                            BTRFS_LEAF_DATA_SIZE(fs_info));
1381                 return -EUCLEAN;
1382         }
1383         end = item_size + btrfs_item_ptr_offset(leaf, slot);
1384 
1385         /* Checks against extent_item */
1386         ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
1387         flags = btrfs_extent_flags(leaf, ei);
1388         total_refs = btrfs_extent_refs(leaf, ei);
1389         generation = btrfs_extent_generation(leaf, ei);
1390         if (unlikely(generation >
1391                      btrfs_super_generation(fs_info->super_copy) + 1)) {
1392                 extent_err(leaf, slot,
1393                            "invalid generation, have %llu expect (0, %llu]",
1394                            generation,
1395                            btrfs_super_generation(fs_info->super_copy) + 1);
1396                 return -EUCLEAN;
1397         }
1398         if (unlikely(!has_single_bit_set(flags & (BTRFS_EXTENT_FLAG_DATA |
1399                                                   BTRFS_EXTENT_FLAG_TREE_BLOCK)))) {
1400                 extent_err(leaf, slot,
1401                 "invalid extent flag, have 0x%llx expect 1 bit set in 0x%llx",
1402                         flags, BTRFS_EXTENT_FLAG_DATA |
1403                         BTRFS_EXTENT_FLAG_TREE_BLOCK);
1404                 return -EUCLEAN;
1405         }
1406         is_tree_block = !!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK);
1407         if (is_tree_block) {
1408                 if (unlikely(key->type == BTRFS_EXTENT_ITEM_KEY &&
1409                              key->offset != fs_info->nodesize)) {
1410                         extent_err(leaf, slot,
1411                                    "invalid extent length, have %llu expect %u",
1412                                    key->offset, fs_info->nodesize);
1413                         return -EUCLEAN;
1414                 }
1415         } else {
1416                 if (unlikely(key->type != BTRFS_EXTENT_ITEM_KEY)) {
1417                         extent_err(leaf, slot,
1418                         "invalid key type, have %u expect %u for data backref",
1419                                    key->type, BTRFS_EXTENT_ITEM_KEY);
1420                         return -EUCLEAN;
1421                 }
1422                 if (unlikely(!IS_ALIGNED(key->offset, fs_info->sectorsize))) {
1423                         extent_err(leaf, slot,
1424                         "invalid extent length, have %llu expect aligned to %u",
1425                                    key->offset, fs_info->sectorsize);
1426                         return -EUCLEAN;
1427                 }
1428                 if (unlikely(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
1429                         extent_err(leaf, slot,
1430                         "invalid extent flag, data has full backref set");
1431                         return -EUCLEAN;
1432                 }
1433         }
1434         ptr = (unsigned long)(struct btrfs_extent_item *)(ei + 1);
1435 
1436         /* Check the special case of btrfs_tree_block_info */
1437         if (is_tree_block && key->type != BTRFS_METADATA_ITEM_KEY) {
1438                 struct btrfs_tree_block_info *info;
1439 
1440                 info = (struct btrfs_tree_block_info *)ptr;
1441                 if (unlikely(btrfs_tree_block_level(leaf, info) >= BTRFS_MAX_LEVEL)) {
1442                         extent_err(leaf, slot,
1443                         "invalid tree block info level, have %u expect [0, %u]",
1444                                    btrfs_tree_block_level(leaf, info),
1445                                    BTRFS_MAX_LEVEL - 1);
1446                         return -EUCLEAN;
1447                 }
1448                 ptr = (unsigned long)(struct btrfs_tree_block_info *)(info + 1);
1449         }
1450 
1451         /* Check inline refs */
1452         while (ptr < end) {
1453                 struct btrfs_extent_inline_ref *iref;
1454                 struct btrfs_extent_data_ref *dref;
1455                 struct btrfs_shared_data_ref *sref;
1456                 u64 seq;
1457                 u64 dref_root;
1458                 u64 dref_objectid;
1459                 u64 dref_offset;
1460                 u64 inline_offset;
1461                 u8 inline_type;
1462 
1463                 if (unlikely(ptr + sizeof(*iref) > end)) {
1464                         extent_err(leaf, slot,
1465 "inline ref item overflows extent item, ptr %lu iref size %zu end %lu",
1466                                    ptr, sizeof(*iref), end);
1467                         return -EUCLEAN;
1468                 }
1469                 iref = (struct btrfs_extent_inline_ref *)ptr;
1470                 inline_type = btrfs_extent_inline_ref_type(leaf, iref);
1471                 inline_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1472                 seq = inline_offset;
1473                 if (unlikely(ptr + btrfs_extent_inline_ref_size(inline_type) > end)) {
1474                         extent_err(leaf, slot,
1475 "inline ref item overflows extent item, ptr %lu iref size %u end %lu",
1476                                    ptr, btrfs_extent_inline_ref_size(inline_type), end);
1477                         return -EUCLEAN;
1478                 }
1479 
1480                 switch (inline_type) {
1481                 /* inline_offset is subvolid of the owner, no need to check */
1482                 case BTRFS_TREE_BLOCK_REF_KEY:
1483                         inline_refs++;
1484                         break;
1485                 /* Contains parent bytenr */
1486                 case BTRFS_SHARED_BLOCK_REF_KEY:
1487                         if (unlikely(!IS_ALIGNED(inline_offset,
1488                                                  fs_info->sectorsize))) {
1489                                 extent_err(leaf, slot,
1490                 "invalid tree parent bytenr, have %llu expect aligned to %u",
1491                                            inline_offset, fs_info->sectorsize);
1492                                 return -EUCLEAN;
1493                         }
1494                         inline_refs++;
1495                         break;
1496                 /*
1497                  * Contains owner subvolid, owner key objectid, adjusted offset.
1498                  * The only obvious corruption can happen in that offset.
1499                  */
1500                 case BTRFS_EXTENT_DATA_REF_KEY:
1501                         dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1502                         dref_root = btrfs_extent_data_ref_root(leaf, dref);
1503                         dref_objectid = btrfs_extent_data_ref_objectid(leaf, dref);
1504                         dref_offset = btrfs_extent_data_ref_offset(leaf, dref);
1505                         seq = hash_extent_data_ref(
1506                                         btrfs_extent_data_ref_root(leaf, dref),
1507                                         btrfs_extent_data_ref_objectid(leaf, dref),
1508                                         btrfs_extent_data_ref_offset(leaf, dref));
1509                         if (unlikely(!is_valid_dref_root(dref_root))) {
1510                                 extent_err(leaf, slot,
1511                                            "invalid data ref root value %llu",
1512                                            dref_root);
1513                                 return -EUCLEAN;
1514                         }
1515                         if (unlikely(dref_objectid < BTRFS_FIRST_FREE_OBJECTID ||
1516                                      dref_objectid > BTRFS_LAST_FREE_OBJECTID)) {
1517                                 extent_err(leaf, slot,
1518                                            "invalid data ref objectid value %llu",
1519                                            dref_root);
1520                                 return -EUCLEAN;
1521                         }
1522                         if (unlikely(!IS_ALIGNED(dref_offset,
1523                                                  fs_info->sectorsize))) {
1524                                 extent_err(leaf, slot,
1525                 "invalid data ref offset, have %llu expect aligned to %u",
1526                                            dref_offset, fs_info->sectorsize);
1527                                 return -EUCLEAN;
1528                         }
1529                         inline_refs += btrfs_extent_data_ref_count(leaf, dref);
1530                         break;
1531                 /* Contains parent bytenr and ref count */
1532                 case BTRFS_SHARED_DATA_REF_KEY:
1533                         sref = (struct btrfs_shared_data_ref *)(iref + 1);
1534                         if (unlikely(!IS_ALIGNED(inline_offset,
1535                                                  fs_info->sectorsize))) {
1536                                 extent_err(leaf, slot,
1537                 "invalid data parent bytenr, have %llu expect aligned to %u",
1538                                            inline_offset, fs_info->sectorsize);
1539                                 return -EUCLEAN;
1540                         }
1541                         inline_refs += btrfs_shared_data_ref_count(leaf, sref);
1542                         break;
1543                 case BTRFS_EXTENT_OWNER_REF_KEY:
1544                         WARN_ON(!btrfs_fs_incompat(fs_info, SIMPLE_QUOTA));
1545                         break;
1546                 default:
1547                         extent_err(leaf, slot, "unknown inline ref type: %u",
1548                                    inline_type);
1549                         return -EUCLEAN;
1550                 }
1551                 if (inline_type < last_type) {
1552                         extent_err(leaf, slot,
1553                                    "inline ref out-of-order: has type %u, prev type %u",
1554                                    inline_type, last_type);
1555                         return -EUCLEAN;
1556                 }
1557                 /* Type changed, allow the sequence starts from U64_MAX again. */
1558                 if (inline_type > last_type)
1559                         last_seq = U64_MAX;
1560                 if (seq > last_seq) {
1561                         extent_err(leaf, slot,
1562 "inline ref out-of-order: has type %u offset %llu seq 0x%llx, prev type %u seq 0x%llx",
1563                                    inline_type, inline_offset, seq,
1564                                    last_type, last_seq);
1565                         return -EUCLEAN;
1566                 }
1567                 last_type = inline_type;
1568                 last_seq = seq;
1569                 ptr += btrfs_extent_inline_ref_size(inline_type);
1570         }
1571         /* No padding is allowed */
1572         if (unlikely(ptr != end)) {
1573                 extent_err(leaf, slot,
1574                            "invalid extent item size, padding bytes found");
1575                 return -EUCLEAN;
1576         }
1577 
1578         /* Finally, check the inline refs against total refs */
1579         if (unlikely(inline_refs > total_refs)) {
1580                 extent_err(leaf, slot,
1581                         "invalid extent refs, have %llu expect >= inline %llu",
1582                            total_refs, inline_refs);
1583                 return -EUCLEAN;
1584         }
1585 
1586         if ((prev_key->type == BTRFS_EXTENT_ITEM_KEY) ||
1587             (prev_key->type == BTRFS_METADATA_ITEM_KEY)) {
1588                 u64 prev_end = prev_key->objectid;
1589 
1590                 if (prev_key->type == BTRFS_METADATA_ITEM_KEY)
1591                         prev_end += fs_info->nodesize;
1592                 else
1593                         prev_end += prev_key->offset;
1594 
1595                 if (unlikely(prev_end > key->objectid)) {
1596                         extent_err(leaf, slot,
1597         "previous extent [%llu %u %llu] overlaps current extent [%llu %u %llu]",
1598                                    prev_key->objectid, prev_key->type,
1599                                    prev_key->offset, key->objectid, key->type,
1600                                    key->offset);
1601                         return -EUCLEAN;
1602                 }
1603         }
1604 
1605         return 0;
1606 }
1607 
1608 static int check_simple_keyed_refs(struct extent_buffer *leaf,
1609                                    struct btrfs_key *key, int slot)
1610 {
1611         u32 expect_item_size = 0;
1612 
1613         if (key->type == BTRFS_SHARED_DATA_REF_KEY)
1614                 expect_item_size = sizeof(struct btrfs_shared_data_ref);
1615 
1616         if (unlikely(btrfs_item_size(leaf, slot) != expect_item_size)) {
1617                 generic_err(leaf, slot,
1618                 "invalid item size, have %u expect %u for key type %u",
1619                             btrfs_item_size(leaf, slot),
1620                             expect_item_size, key->type);
1621                 return -EUCLEAN;
1622         }
1623         if (unlikely(!IS_ALIGNED(key->objectid, leaf->fs_info->sectorsize))) {
1624                 generic_err(leaf, slot,
1625 "invalid key objectid for shared block ref, have %llu expect aligned to %u",
1626                             key->objectid, leaf->fs_info->sectorsize);
1627                 return -EUCLEAN;
1628         }
1629         if (unlikely(key->type != BTRFS_TREE_BLOCK_REF_KEY &&
1630                      !IS_ALIGNED(key->offset, leaf->fs_info->sectorsize))) {
1631                 extent_err(leaf, slot,
1632                 "invalid tree parent bytenr, have %llu expect aligned to %u",
1633                            key->offset, leaf->fs_info->sectorsize);
1634                 return -EUCLEAN;
1635         }
1636         return 0;
1637 }
1638 
1639 static int check_extent_data_ref(struct extent_buffer *leaf,
1640                                  struct btrfs_key *key, int slot)
1641 {
1642         struct btrfs_extent_data_ref *dref;
1643         unsigned long ptr = btrfs_item_ptr_offset(leaf, slot);
1644         const unsigned long end = ptr + btrfs_item_size(leaf, slot);
1645 
1646         if (unlikely(btrfs_item_size(leaf, slot) % sizeof(*dref) != 0)) {
1647                 generic_err(leaf, slot,
1648         "invalid item size, have %u expect aligned to %zu for key type %u",
1649                             btrfs_item_size(leaf, slot),
1650                             sizeof(*dref), key->type);
1651                 return -EUCLEAN;
1652         }
1653         if (unlikely(!IS_ALIGNED(key->objectid, leaf->fs_info->sectorsize))) {
1654                 generic_err(leaf, slot,
1655 "invalid key objectid for shared block ref, have %llu expect aligned to %u",
1656                             key->objectid, leaf->fs_info->sectorsize);
1657                 return -EUCLEAN;
1658         }
1659         for (; ptr < end; ptr += sizeof(*dref)) {
1660                 u64 root;
1661                 u64 objectid;
1662                 u64 offset;
1663 
1664                 /*
1665                  * We cannot check the extent_data_ref hash due to possible
1666                  * overflow from the leaf due to hash collisions.
1667                  */
1668                 dref = (struct btrfs_extent_data_ref *)ptr;
1669                 root = btrfs_extent_data_ref_root(leaf, dref);
1670                 objectid = btrfs_extent_data_ref_objectid(leaf, dref);
1671                 offset = btrfs_extent_data_ref_offset(leaf, dref);
1672                 if (unlikely(!is_valid_dref_root(root))) {
1673                         extent_err(leaf, slot,
1674                                    "invalid extent data backref root value %llu",
1675                                    root);
1676                         return -EUCLEAN;
1677                 }
1678                 if (unlikely(objectid < BTRFS_FIRST_FREE_OBJECTID ||
1679                              objectid > BTRFS_LAST_FREE_OBJECTID)) {
1680                         extent_err(leaf, slot,
1681                                    "invalid extent data backref objectid value %llu",
1682                                    root);
1683                         return -EUCLEAN;
1684                 }
1685                 if (unlikely(!IS_ALIGNED(offset, leaf->fs_info->sectorsize))) {
1686                         extent_err(leaf, slot,
1687         "invalid extent data backref offset, have %llu expect aligned to %u",
1688                                    offset, leaf->fs_info->sectorsize);
1689                         return -EUCLEAN;
1690                 }
1691         }
1692         return 0;
1693 }
1694 
1695 #define inode_ref_err(eb, slot, fmt, args...)                   \
1696         inode_item_err(eb, slot, fmt, ##args)
1697 static int check_inode_ref(struct extent_buffer *leaf,
1698                            struct btrfs_key *key, struct btrfs_key *prev_key,
1699                            int slot)
1700 {
1701         struct btrfs_inode_ref *iref;
1702         unsigned long ptr;
1703         unsigned long end;
1704 
1705         if (unlikely(!check_prev_ino(leaf, key, slot, prev_key)))
1706                 return -EUCLEAN;
1707         /* namelen can't be 0, so item_size == sizeof() is also invalid */
1708         if (unlikely(btrfs_item_size(leaf, slot) <= sizeof(*iref))) {
1709                 inode_ref_err(leaf, slot,
1710                         "invalid item size, have %u expect (%zu, %u)",
1711                         btrfs_item_size(leaf, slot),
1712                         sizeof(*iref), BTRFS_LEAF_DATA_SIZE(leaf->fs_info));
1713                 return -EUCLEAN;
1714         }
1715 
1716         ptr = btrfs_item_ptr_offset(leaf, slot);
1717         end = ptr + btrfs_item_size(leaf, slot);
1718         while (ptr < end) {
1719                 u16 namelen;
1720 
1721                 if (unlikely(ptr + sizeof(iref) > end)) {
1722                         inode_ref_err(leaf, slot,
1723                         "inode ref overflow, ptr %lu end %lu inode_ref_size %zu",
1724                                 ptr, end, sizeof(iref));
1725                         return -EUCLEAN;
1726                 }
1727 
1728                 iref = (struct btrfs_inode_ref *)ptr;
1729                 namelen = btrfs_inode_ref_name_len(leaf, iref);
1730                 if (unlikely(ptr + sizeof(*iref) + namelen > end)) {
1731                         inode_ref_err(leaf, slot,
1732                                 "inode ref overflow, ptr %lu end %lu namelen %u",
1733                                 ptr, end, namelen);
1734                         return -EUCLEAN;
1735                 }
1736 
1737                 /*
1738                  * NOTE: In theory we should record all found index numbers
1739                  * to find any duplicated indexes, but that will be too time
1740                  * consuming for inodes with too many hard links.
1741                  */
1742                 ptr += sizeof(*iref) + namelen;
1743         }
1744         return 0;
1745 }
1746 
1747 static int check_raid_stripe_extent(const struct extent_buffer *leaf,
1748                                     const struct btrfs_key *key, int slot)
1749 {
1750         if (unlikely(!IS_ALIGNED(key->objectid, leaf->fs_info->sectorsize))) {
1751                 generic_err(leaf, slot,
1752 "invalid key objectid for raid stripe extent, have %llu expect aligned to %u",
1753                             key->objectid, leaf->fs_info->sectorsize);
1754                 return -EUCLEAN;
1755         }
1756 
1757         if (unlikely(!btrfs_fs_incompat(leaf->fs_info, RAID_STRIPE_TREE))) {
1758                 generic_err(leaf, slot,
1759         "RAID_STRIPE_EXTENT present but RAID_STRIPE_TREE incompat bit unset");
1760                 return -EUCLEAN;
1761         }
1762 
1763         return 0;
1764 }
1765 
1766 /*
1767  * Common point to switch the item-specific validation.
1768  */
1769 static enum btrfs_tree_block_status check_leaf_item(struct extent_buffer *leaf,
1770                                                     struct btrfs_key *key,
1771                                                     int slot,
1772                                                     struct btrfs_key *prev_key)
1773 {
1774         int ret = 0;
1775         struct btrfs_chunk *chunk;
1776 
1777         switch (key->type) {
1778         case BTRFS_EXTENT_DATA_KEY:
1779                 ret = check_extent_data_item(leaf, key, slot, prev_key);
1780                 break;
1781         case BTRFS_EXTENT_CSUM_KEY:
1782                 ret = check_csum_item(leaf, key, slot, prev_key);
1783                 break;
1784         case BTRFS_DIR_ITEM_KEY:
1785         case BTRFS_DIR_INDEX_KEY:
1786         case BTRFS_XATTR_ITEM_KEY:
1787                 ret = check_dir_item(leaf, key, prev_key, slot);
1788                 break;
1789         case BTRFS_INODE_REF_KEY:
1790                 ret = check_inode_ref(leaf, key, prev_key, slot);
1791                 break;
1792         case BTRFS_BLOCK_GROUP_ITEM_KEY:
1793                 ret = check_block_group_item(leaf, key, slot);
1794                 break;
1795         case BTRFS_CHUNK_ITEM_KEY:
1796                 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
1797                 ret = check_leaf_chunk_item(leaf, chunk, key, slot);
1798                 break;
1799         case BTRFS_DEV_ITEM_KEY:
1800                 ret = check_dev_item(leaf, key, slot);
1801                 break;
1802         case BTRFS_INODE_ITEM_KEY:
1803                 ret = check_inode_item(leaf, key, slot);
1804                 break;
1805         case BTRFS_ROOT_ITEM_KEY:
1806                 ret = check_root_item(leaf, key, slot);
1807                 break;
1808         case BTRFS_EXTENT_ITEM_KEY:
1809         case BTRFS_METADATA_ITEM_KEY:
1810                 ret = check_extent_item(leaf, key, slot, prev_key);
1811                 break;
1812         case BTRFS_TREE_BLOCK_REF_KEY:
1813         case BTRFS_SHARED_DATA_REF_KEY:
1814         case BTRFS_SHARED_BLOCK_REF_KEY:
1815                 ret = check_simple_keyed_refs(leaf, key, slot);
1816                 break;
1817         case BTRFS_EXTENT_DATA_REF_KEY:
1818                 ret = check_extent_data_ref(leaf, key, slot);
1819                 break;
1820         case BTRFS_RAID_STRIPE_KEY:
1821                 ret = check_raid_stripe_extent(leaf, key, slot);
1822                 break;
1823         }
1824 
1825         if (ret)
1826                 return BTRFS_TREE_BLOCK_INVALID_ITEM;
1827         return BTRFS_TREE_BLOCK_CLEAN;
1828 }
1829 
1830 enum btrfs_tree_block_status __btrfs_check_leaf(struct extent_buffer *leaf)
1831 {
1832         struct btrfs_fs_info *fs_info = leaf->fs_info;
1833         /* No valid key type is 0, so all key should be larger than this key */
1834         struct btrfs_key prev_key = {0, 0, 0};
1835         struct btrfs_key key;
1836         u32 nritems = btrfs_header_nritems(leaf);
1837         int slot;
1838 
1839         if (unlikely(btrfs_header_level(leaf) != 0)) {
1840                 generic_err(leaf, 0,
1841                         "invalid level for leaf, have %d expect 0",
1842                         btrfs_header_level(leaf));
1843                 return BTRFS_TREE_BLOCK_INVALID_LEVEL;
1844         }
1845 
1846         if (unlikely(!btrfs_header_flag(leaf, BTRFS_HEADER_FLAG_WRITTEN))) {
1847                 generic_err(leaf, 0, "invalid flag for leaf, WRITTEN not set");
1848                 return BTRFS_TREE_BLOCK_WRITTEN_NOT_SET;
1849         }
1850 
1851         /*
1852          * Extent buffers from a relocation tree have a owner field that
1853          * corresponds to the subvolume tree they are based on. So just from an
1854          * extent buffer alone we can not find out what is the id of the
1855          * corresponding subvolume tree, so we can not figure out if the extent
1856          * buffer corresponds to the root of the relocation tree or not. So
1857          * skip this check for relocation trees.
1858          */
1859         if (nritems == 0 && !btrfs_header_flag(leaf, BTRFS_HEADER_FLAG_RELOC)) {
1860                 u64 owner = btrfs_header_owner(leaf);
1861 
1862                 /* These trees must never be empty */
1863                 if (unlikely(owner == BTRFS_ROOT_TREE_OBJECTID ||
1864                              owner == BTRFS_CHUNK_TREE_OBJECTID ||
1865                              owner == BTRFS_DEV_TREE_OBJECTID ||
1866                              owner == BTRFS_FS_TREE_OBJECTID ||
1867                              owner == BTRFS_DATA_RELOC_TREE_OBJECTID)) {
1868                         generic_err(leaf, 0,
1869                         "invalid root, root %llu must never be empty",
1870                                     owner);
1871                         return BTRFS_TREE_BLOCK_INVALID_NRITEMS;
1872                 }
1873 
1874                 /* Unknown tree */
1875                 if (unlikely(owner == 0)) {
1876                         generic_err(leaf, 0,
1877                                 "invalid owner, root 0 is not defined");
1878                         return BTRFS_TREE_BLOCK_INVALID_OWNER;
1879                 }
1880 
1881                 /* EXTENT_TREE_V2 can have empty extent trees. */
1882                 if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2))
1883                         return BTRFS_TREE_BLOCK_CLEAN;
1884 
1885                 if (unlikely(owner == BTRFS_EXTENT_TREE_OBJECTID)) {
1886                         generic_err(leaf, 0,
1887                         "invalid root, root %llu must never be empty",
1888                                     owner);
1889                         return BTRFS_TREE_BLOCK_INVALID_NRITEMS;
1890                 }
1891 
1892                 return BTRFS_TREE_BLOCK_CLEAN;
1893         }
1894 
1895         if (unlikely(nritems == 0))
1896                 return BTRFS_TREE_BLOCK_CLEAN;
1897 
1898         /*
1899          * Check the following things to make sure this is a good leaf, and
1900          * leaf users won't need to bother with similar sanity checks:
1901          *
1902          * 1) key ordering
1903          * 2) item offset and size
1904          *    No overlap, no hole, all inside the leaf.
1905          * 3) item content
1906          *    If possible, do comprehensive sanity check.
1907          *    NOTE: All checks must only rely on the item data itself.
1908          */
1909         for (slot = 0; slot < nritems; slot++) {
1910                 u32 item_end_expected;
1911                 u64 item_data_end;
1912                 enum btrfs_tree_block_status ret;
1913 
1914                 btrfs_item_key_to_cpu(leaf, &key, slot);
1915 
1916                 /* Make sure the keys are in the right order */
1917                 if (unlikely(btrfs_comp_cpu_keys(&prev_key, &key) >= 0)) {
1918                         generic_err(leaf, slot,
1919         "bad key order, prev (%llu %u %llu) current (%llu %u %llu)",
1920                                 prev_key.objectid, prev_key.type,
1921                                 prev_key.offset, key.objectid, key.type,
1922                                 key.offset);
1923                         return BTRFS_TREE_BLOCK_BAD_KEY_ORDER;
1924                 }
1925 
1926                 item_data_end = (u64)btrfs_item_offset(leaf, slot) +
1927                                 btrfs_item_size(leaf, slot);
1928                 /*
1929                  * Make sure the offset and ends are right, remember that the
1930                  * item data starts at the end of the leaf and grows towards the
1931                  * front.
1932                  */
1933                 if (slot == 0)
1934                         item_end_expected = BTRFS_LEAF_DATA_SIZE(fs_info);
1935                 else
1936                         item_end_expected = btrfs_item_offset(leaf,
1937                                                                  slot - 1);
1938                 if (unlikely(item_data_end != item_end_expected)) {
1939                         generic_err(leaf, slot,
1940                                 "unexpected item end, have %llu expect %u",
1941                                 item_data_end, item_end_expected);
1942                         return BTRFS_TREE_BLOCK_INVALID_OFFSETS;
1943                 }
1944 
1945                 /*
1946                  * Check to make sure that we don't point outside of the leaf,
1947                  * just in case all the items are consistent to each other, but
1948                  * all point outside of the leaf.
1949                  */
1950                 if (unlikely(item_data_end > BTRFS_LEAF_DATA_SIZE(fs_info))) {
1951                         generic_err(leaf, slot,
1952                         "slot end outside of leaf, have %llu expect range [0, %u]",
1953                                 item_data_end, BTRFS_LEAF_DATA_SIZE(fs_info));
1954                         return BTRFS_TREE_BLOCK_INVALID_OFFSETS;
1955                 }
1956 
1957                 /* Also check if the item pointer overlaps with btrfs item. */
1958                 if (unlikely(btrfs_item_ptr_offset(leaf, slot) <
1959                              btrfs_item_nr_offset(leaf, slot) + sizeof(struct btrfs_item))) {
1960                         generic_err(leaf, slot,
1961                 "slot overlaps with its data, item end %lu data start %lu",
1962                                 btrfs_item_nr_offset(leaf, slot) +
1963                                 sizeof(struct btrfs_item),
1964                                 btrfs_item_ptr_offset(leaf, slot));
1965                         return BTRFS_TREE_BLOCK_INVALID_OFFSETS;
1966                 }
1967 
1968                 /* Check if the item size and content meet other criteria. */
1969                 ret = check_leaf_item(leaf, &key, slot, &prev_key);
1970                 if (unlikely(ret != BTRFS_TREE_BLOCK_CLEAN))
1971                         return ret;
1972 
1973                 prev_key.objectid = key.objectid;
1974                 prev_key.type = key.type;
1975                 prev_key.offset = key.offset;
1976         }
1977 
1978         return BTRFS_TREE_BLOCK_CLEAN;
1979 }
1980 
1981 int btrfs_check_leaf(struct extent_buffer *leaf)
1982 {
1983         enum btrfs_tree_block_status ret;
1984 
1985         ret = __btrfs_check_leaf(leaf);
1986         if (unlikely(ret != BTRFS_TREE_BLOCK_CLEAN))
1987                 return -EUCLEAN;
1988         return 0;
1989 }
1990 ALLOW_ERROR_INJECTION(btrfs_check_leaf, ERRNO);
1991 
1992 enum btrfs_tree_block_status __btrfs_check_node(struct extent_buffer *node)
1993 {
1994         struct btrfs_fs_info *fs_info = node->fs_info;
1995         unsigned long nr = btrfs_header_nritems(node);
1996         struct btrfs_key key, next_key;
1997         int slot;
1998         int level = btrfs_header_level(node);
1999         u64 bytenr;
2000 
2001         if (unlikely(!btrfs_header_flag(node, BTRFS_HEADER_FLAG_WRITTEN))) {
2002                 generic_err(node, 0, "invalid flag for node, WRITTEN not set");
2003                 return BTRFS_TREE_BLOCK_WRITTEN_NOT_SET;
2004         }
2005 
2006         if (unlikely(level <= 0 || level >= BTRFS_MAX_LEVEL)) {
2007                 generic_err(node, 0,
2008                         "invalid level for node, have %d expect [1, %d]",
2009                         level, BTRFS_MAX_LEVEL - 1);
2010                 return BTRFS_TREE_BLOCK_INVALID_LEVEL;
2011         }
2012         if (unlikely(nr == 0 || nr > BTRFS_NODEPTRS_PER_BLOCK(fs_info))) {
2013                 btrfs_crit(fs_info,
2014 "corrupt node: root=%llu block=%llu, nritems too %s, have %lu expect range [1,%u]",
2015                            btrfs_header_owner(node), node->start,
2016                            nr == 0 ? "small" : "large", nr,
2017                            BTRFS_NODEPTRS_PER_BLOCK(fs_info));
2018                 return BTRFS_TREE_BLOCK_INVALID_NRITEMS;
2019         }
2020 
2021         for (slot = 0; slot < nr - 1; slot++) {
2022                 bytenr = btrfs_node_blockptr(node, slot);
2023                 btrfs_node_key_to_cpu(node, &key, slot);
2024                 btrfs_node_key_to_cpu(node, &next_key, slot + 1);
2025 
2026                 if (unlikely(!bytenr)) {
2027                         generic_err(node, slot,
2028                                 "invalid NULL node pointer");
2029                         return BTRFS_TREE_BLOCK_INVALID_BLOCKPTR;
2030                 }
2031                 if (unlikely(!IS_ALIGNED(bytenr, fs_info->sectorsize))) {
2032                         generic_err(node, slot,
2033                         "unaligned pointer, have %llu should be aligned to %u",
2034                                 bytenr, fs_info->sectorsize);
2035                         return BTRFS_TREE_BLOCK_INVALID_BLOCKPTR;
2036                 }
2037 
2038                 if (unlikely(btrfs_comp_cpu_keys(&key, &next_key) >= 0)) {
2039                         generic_err(node, slot,
2040         "bad key order, current (%llu %u %llu) next (%llu %u %llu)",
2041                                 key.objectid, key.type, key.offset,
2042                                 next_key.objectid, next_key.type,
2043                                 next_key.offset);
2044                         return BTRFS_TREE_BLOCK_BAD_KEY_ORDER;
2045                 }
2046         }
2047         return BTRFS_TREE_BLOCK_CLEAN;
2048 }
2049 
2050 int btrfs_check_node(struct extent_buffer *node)
2051 {
2052         enum btrfs_tree_block_status ret;
2053 
2054         ret = __btrfs_check_node(node);
2055         if (unlikely(ret != BTRFS_TREE_BLOCK_CLEAN))
2056                 return -EUCLEAN;
2057         return 0;
2058 }
2059 ALLOW_ERROR_INJECTION(btrfs_check_node, ERRNO);
2060 
2061 int btrfs_check_eb_owner(const struct extent_buffer *eb, u64 root_owner)
2062 {
2063         const bool is_subvol = is_fstree(root_owner);
2064         const u64 eb_owner = btrfs_header_owner(eb);
2065 
2066         /*
2067          * Skip dummy fs, as selftests don't create unique ebs for each dummy
2068          * root.
2069          */
2070         if (btrfs_is_testing(eb->fs_info))
2071                 return 0;
2072         /*
2073          * There are several call sites (backref walking, qgroup, and data
2074          * reloc) passing 0 as @root_owner, as they are not holding the
2075          * tree root.  In that case, we can not do a reliable ownership check,
2076          * so just exit.
2077          */
2078         if (root_owner == 0)
2079                 return 0;
2080         /*
2081          * These trees use key.offset as their owner, our callers don't have
2082          * the extra capacity to pass key.offset here.  So we just skip them.
2083          */
2084         if (root_owner == BTRFS_TREE_LOG_OBJECTID ||
2085             root_owner == BTRFS_TREE_RELOC_OBJECTID)
2086                 return 0;
2087 
2088         if (!is_subvol) {
2089                 /* For non-subvolume trees, the eb owner should match root owner */
2090                 if (unlikely(root_owner != eb_owner)) {
2091                         btrfs_crit(eb->fs_info,
2092 "corrupted %s, root=%llu block=%llu owner mismatch, have %llu expect %llu",
2093                                 btrfs_header_level(eb) == 0 ? "leaf" : "node",
2094                                 root_owner, btrfs_header_bytenr(eb), eb_owner,
2095                                 root_owner);
2096                         return -EUCLEAN;
2097                 }
2098                 return 0;
2099         }
2100 
2101         /*
2102          * For subvolume trees, owners can mismatch, but they should all belong
2103          * to subvolume trees.
2104          */
2105         if (unlikely(is_subvol != is_fstree(eb_owner))) {
2106                 btrfs_crit(eb->fs_info,
2107 "corrupted %s, root=%llu block=%llu owner mismatch, have %llu expect [%llu, %llu]",
2108                         btrfs_header_level(eb) == 0 ? "leaf" : "node",
2109                         root_owner, btrfs_header_bytenr(eb), eb_owner,
2110                         BTRFS_FIRST_FREE_OBJECTID, BTRFS_LAST_FREE_OBJECTID);
2111                 return -EUCLEAN;
2112         }
2113         return 0;
2114 }
2115 
2116 int btrfs_verify_level_key(struct extent_buffer *eb, int level,
2117                            struct btrfs_key *first_key, u64 parent_transid)
2118 {
2119         struct btrfs_fs_info *fs_info = eb->fs_info;
2120         int found_level;
2121         struct btrfs_key found_key;
2122         int ret;
2123 
2124         found_level = btrfs_header_level(eb);
2125         if (found_level != level) {
2126                 WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG),
2127                      KERN_ERR "BTRFS: tree level check failed\n");
2128                 btrfs_err(fs_info,
2129 "tree level mismatch detected, bytenr=%llu level expected=%u has=%u",
2130                           eb->start, level, found_level);
2131                 return -EIO;
2132         }
2133 
2134         if (!first_key)
2135                 return 0;
2136 
2137         /*
2138          * For live tree block (new tree blocks in current transaction),
2139          * we need proper lock context to avoid race, which is impossible here.
2140          * So we only checks tree blocks which is read from disk, whose
2141          * generation <= fs_info->last_trans_committed.
2142          */
2143         if (btrfs_header_generation(eb) > btrfs_get_last_trans_committed(fs_info))
2144                 return 0;
2145 
2146         /* We have @first_key, so this @eb must have at least one item */
2147         if (btrfs_header_nritems(eb) == 0) {
2148                 btrfs_err(fs_info,
2149                 "invalid tree nritems, bytenr=%llu nritems=0 expect >0",
2150                           eb->start);
2151                 WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG));
2152                 return -EUCLEAN;
2153         }
2154 
2155         if (found_level)
2156                 btrfs_node_key_to_cpu(eb, &found_key, 0);
2157         else
2158                 btrfs_item_key_to_cpu(eb, &found_key, 0);
2159         ret = btrfs_comp_cpu_keys(first_key, &found_key);
2160 
2161         if (ret) {
2162                 WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG),
2163                      KERN_ERR "BTRFS: tree first key check failed\n");
2164                 btrfs_err(fs_info,
2165 "tree first key mismatch detected, bytenr=%llu parent_transid=%llu key expected=(%llu,%u,%llu) has=(%llu,%u,%llu)",
2166                           eb->start, parent_transid, first_key->objectid,
2167                           first_key->type, first_key->offset,
2168                           found_key.objectid, found_key.type,
2169                           found_key.offset);
2170         }
2171         return ret;
2172 }
2173 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php