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