1c1d7c514SDavid Sterba // SPDX-License-Identifier: GPL-2.0
2557ea5ddSQu Wenruo /*
3557ea5ddSQu Wenruo * Copyright (C) Qu Wenruo 2017. All rights reserved.
4557ea5ddSQu Wenruo */
5557ea5ddSQu Wenruo
6557ea5ddSQu Wenruo /*
7557ea5ddSQu Wenruo * The module is used to catch unexpected/corrupted tree block data.
8557ea5ddSQu Wenruo * Such behavior can be caused either by a fuzzed image or bugs.
9557ea5ddSQu Wenruo *
10557ea5ddSQu Wenruo * The objective is to do leaf/node validation checks when tree block is read
11557ea5ddSQu Wenruo * from disk, and check *every* possible member, so other code won't
12557ea5ddSQu Wenruo * need to checking them again.
13557ea5ddSQu Wenruo *
14557ea5ddSQu Wenruo * Due to the potential and unwanted damage, every checker needs to be
15557ea5ddSQu Wenruo * carefully reviewed otherwise so it does not prevent mount of valid images.
16557ea5ddSQu Wenruo */
17557ea5ddSQu Wenruo
1802529d7aSQu Wenruo #include <linux/types.h>
1902529d7aSQu Wenruo #include <linux/stddef.h>
2002529d7aSQu Wenruo #include <linux/error-injection.h>
219b569ea0SJosef Bacik #include "messages.h"
22557ea5ddSQu Wenruo #include "ctree.h"
23557ea5ddSQu Wenruo #include "tree-checker.h"
24557ea5ddSQu Wenruo #include "disk-io.h"
25557ea5ddSQu Wenruo #include "compression.h"
26fce466eaSQu Wenruo #include "volumes.h"
27c1499166SDavid Sterba #include "misc.h"
28c7f13d42SJosef Bacik #include "fs.h"
2907e81dc9SJosef Bacik #include "accessors.h"
306bfd0ffaSJosef Bacik #include "file-item.h"
31f541833cSJosef Bacik #include "inode-item.h"
32c1bf973fSQu Wenruo #include "extent-tree.h"
33557ea5ddSQu Wenruo
34bba4f298SQu Wenruo /*
35bba4f298SQu Wenruo * Error message should follow the following format:
36bba4f298SQu Wenruo * corrupt <type>: <identifier>, <reason>[, <bad_value>]
37bba4f298SQu Wenruo *
38bba4f298SQu Wenruo * @type: leaf or node
39bba4f298SQu Wenruo * @identifier: the necessary info to locate the leaf/node.
4052042d8eSAndrea Gelmini * It's recommended to decode key.objecitd/offset if it's
41bba4f298SQu Wenruo * meaningful.
42bba4f298SQu Wenruo * @reason: describe the error
4352042d8eSAndrea Gelmini * @bad_value: optional, it's recommended to output bad value and its
44bba4f298SQu Wenruo * expected value (range).
45bba4f298SQu Wenruo *
46bba4f298SQu Wenruo * Since comma is used to separate the components, only space is allowed
47bba4f298SQu Wenruo * inside each component.
48bba4f298SQu Wenruo */
49bba4f298SQu Wenruo
50bba4f298SQu Wenruo /*
51bba4f298SQu Wenruo * Append generic "corrupt leaf/node root=%llu block=%llu slot=%d: " to @fmt.
52bba4f298SQu Wenruo * Allows callers to customize the output.
53bba4f298SQu Wenruo */
5486a6be3aSDavid Sterba __printf(3, 4)
55e67c718bSDavid Sterba __cold
generic_err(const struct extent_buffer * eb,int slot,const char * fmt,...)5686a6be3aSDavid Sterba static void generic_err(const struct extent_buffer *eb, int slot,
57bba4f298SQu Wenruo const char *fmt, ...)
58bba4f298SQu Wenruo {
5986a6be3aSDavid Sterba const struct btrfs_fs_info *fs_info = eb->fs_info;
60bba4f298SQu Wenruo struct va_format vaf;
61bba4f298SQu Wenruo va_list args;
62bba4f298SQu Wenruo
63bba4f298SQu Wenruo va_start(args, fmt);
64bba4f298SQu Wenruo
65bba4f298SQu Wenruo vaf.fmt = fmt;
66bba4f298SQu Wenruo vaf.va = &args;
67bba4f298SQu Wenruo
682f659546SQu Wenruo btrfs_crit(fs_info,
69bba4f298SQu Wenruo "corrupt %s: root=%llu block=%llu slot=%d, %pV",
70bba4f298SQu Wenruo btrfs_header_level(eb) == 0 ? "leaf" : "node",
712f659546SQu Wenruo btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot, &vaf);
72bba4f298SQu Wenruo va_end(args);
73bba4f298SQu Wenruo }
74bba4f298SQu Wenruo
758806d718SQu Wenruo /*
768806d718SQu Wenruo * Customized reporter for extent data item, since its key objectid and
778806d718SQu Wenruo * offset has its own meaning.
788806d718SQu Wenruo */
791fd715ffSDavid Sterba __printf(3, 4)
80e67c718bSDavid Sterba __cold
file_extent_err(const struct extent_buffer * eb,int slot,const char * fmt,...)811fd715ffSDavid Sterba static void file_extent_err(const struct extent_buffer *eb, int slot,
828806d718SQu Wenruo const char *fmt, ...)
838806d718SQu Wenruo {
841fd715ffSDavid Sterba const struct btrfs_fs_info *fs_info = eb->fs_info;
858806d718SQu Wenruo struct btrfs_key key;
868806d718SQu Wenruo struct va_format vaf;
878806d718SQu Wenruo va_list args;
888806d718SQu Wenruo
898806d718SQu Wenruo btrfs_item_key_to_cpu(eb, &key, slot);
908806d718SQu Wenruo va_start(args, fmt);
918806d718SQu Wenruo
928806d718SQu Wenruo vaf.fmt = fmt;
938806d718SQu Wenruo vaf.va = &args;
948806d718SQu Wenruo
952f659546SQu Wenruo btrfs_crit(fs_info,
968806d718SQu Wenruo "corrupt %s: root=%llu block=%llu slot=%d ino=%llu file_offset=%llu, %pV",
972f659546SQu Wenruo btrfs_header_level(eb) == 0 ? "leaf" : "node",
982f659546SQu Wenruo btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot,
992f659546SQu Wenruo key.objectid, key.offset, &vaf);
1008806d718SQu Wenruo va_end(args);
1018806d718SQu Wenruo }
1028806d718SQu Wenruo
1038806d718SQu Wenruo /*
1048806d718SQu Wenruo * Return 0 if the btrfs_file_extent_##name is aligned to @alignment
1058806d718SQu Wenruo * Else return 1
1068806d718SQu Wenruo */
107033774dcSDavid Sterba #define CHECK_FE_ALIGNED(leaf, slot, fi, name, alignment) \
1088806d718SQu Wenruo ({ \
109c7c01a4aSDavid Sterba if (unlikely(!IS_ALIGNED(btrfs_file_extent_##name((leaf), (fi)), \
110c7c01a4aSDavid Sterba (alignment)))) \
1111fd715ffSDavid Sterba file_extent_err((leaf), (slot), \
1128806d718SQu Wenruo "invalid %s for file extent, have %llu, should be aligned to %u", \
1138806d718SQu Wenruo (#name), btrfs_file_extent_##name((leaf), (fi)), \
1148806d718SQu Wenruo (alignment)); \
1158806d718SQu Wenruo (!IS_ALIGNED(btrfs_file_extent_##name((leaf), (fi)), (alignment))); \
1168806d718SQu Wenruo })
1178806d718SQu Wenruo
file_extent_end(struct extent_buffer * leaf,struct btrfs_key * key,struct btrfs_file_extent_item * extent)1184e9845efSFilipe Manana static u64 file_extent_end(struct extent_buffer *leaf,
1194e9845efSFilipe Manana struct btrfs_key *key,
1204e9845efSFilipe Manana struct btrfs_file_extent_item *extent)
1214e9845efSFilipe Manana {
1224e9845efSFilipe Manana u64 end;
1234e9845efSFilipe Manana u64 len;
1244e9845efSFilipe Manana
1254e9845efSFilipe Manana if (btrfs_file_extent_type(leaf, extent) == BTRFS_FILE_EXTENT_INLINE) {
1264e9845efSFilipe Manana len = btrfs_file_extent_ram_bytes(leaf, extent);
1274e9845efSFilipe Manana end = ALIGN(key->offset + len, leaf->fs_info->sectorsize);
1284e9845efSFilipe Manana } else {
1294e9845efSFilipe Manana len = btrfs_file_extent_num_bytes(leaf, extent);
1304e9845efSFilipe Manana end = key->offset + len;
1314e9845efSFilipe Manana }
1324e9845efSFilipe Manana return end;
1334e9845efSFilipe Manana }
1344e9845efSFilipe Manana
13580d7fd1eSQu Wenruo /*
13680d7fd1eSQu Wenruo * Customized report for dir_item, the only new important information is
13780d7fd1eSQu Wenruo * key->objectid, which represents inode number
13880d7fd1eSQu Wenruo */
13980d7fd1eSQu Wenruo __printf(3, 4)
14080d7fd1eSQu Wenruo __cold
dir_item_err(const struct extent_buffer * eb,int slot,const char * fmt,...)14180d7fd1eSQu Wenruo static void dir_item_err(const struct extent_buffer *eb, int slot,
14280d7fd1eSQu Wenruo const char *fmt, ...)
14380d7fd1eSQu Wenruo {
14480d7fd1eSQu Wenruo const struct btrfs_fs_info *fs_info = eb->fs_info;
14580d7fd1eSQu Wenruo struct btrfs_key key;
14680d7fd1eSQu Wenruo struct va_format vaf;
14780d7fd1eSQu Wenruo va_list args;
14880d7fd1eSQu Wenruo
14980d7fd1eSQu Wenruo btrfs_item_key_to_cpu(eb, &key, slot);
15080d7fd1eSQu Wenruo va_start(args, fmt);
15180d7fd1eSQu Wenruo
15280d7fd1eSQu Wenruo vaf.fmt = fmt;
15380d7fd1eSQu Wenruo vaf.va = &args;
15480d7fd1eSQu Wenruo
15580d7fd1eSQu Wenruo btrfs_crit(fs_info,
15680d7fd1eSQu Wenruo "corrupt %s: root=%llu block=%llu slot=%d ino=%llu, %pV",
15780d7fd1eSQu Wenruo btrfs_header_level(eb) == 0 ? "leaf" : "node",
15880d7fd1eSQu Wenruo btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot,
15980d7fd1eSQu Wenruo key.objectid, &vaf);
16080d7fd1eSQu Wenruo va_end(args);
16180d7fd1eSQu Wenruo }
16280d7fd1eSQu Wenruo
16380d7fd1eSQu Wenruo /*
16480d7fd1eSQu Wenruo * This functions checks prev_key->objectid, to ensure current key and prev_key
16580d7fd1eSQu Wenruo * share the same objectid as inode number.
16680d7fd1eSQu Wenruo *
16780d7fd1eSQu Wenruo * This is to detect missing INODE_ITEM in subvolume trees.
16880d7fd1eSQu Wenruo *
16980d7fd1eSQu Wenruo * Return true if everything is OK or we don't need to check.
17080d7fd1eSQu Wenruo * Return false if anything is wrong.
17180d7fd1eSQu Wenruo */
check_prev_ino(struct extent_buffer * leaf,struct btrfs_key * key,int slot,struct btrfs_key * prev_key)17280d7fd1eSQu Wenruo static bool check_prev_ino(struct extent_buffer *leaf,
17380d7fd1eSQu Wenruo struct btrfs_key *key, int slot,
17480d7fd1eSQu Wenruo struct btrfs_key *prev_key)
17580d7fd1eSQu Wenruo {
17680d7fd1eSQu Wenruo /* No prev key, skip check */
17780d7fd1eSQu Wenruo if (slot == 0)
17880d7fd1eSQu Wenruo return true;
17980d7fd1eSQu Wenruo
18080d7fd1eSQu Wenruo /* Only these key->types needs to be checked */
18180d7fd1eSQu Wenruo ASSERT(key->type == BTRFS_XATTR_ITEM_KEY ||
18280d7fd1eSQu Wenruo key->type == BTRFS_INODE_REF_KEY ||
18380d7fd1eSQu Wenruo key->type == BTRFS_DIR_INDEX_KEY ||
18480d7fd1eSQu Wenruo key->type == BTRFS_DIR_ITEM_KEY ||
18580d7fd1eSQu Wenruo key->type == BTRFS_EXTENT_DATA_KEY);
18680d7fd1eSQu Wenruo
18780d7fd1eSQu Wenruo /*
18880d7fd1eSQu Wenruo * Only subvolume trees along with their reloc trees need this check.
18980d7fd1eSQu Wenruo * Things like log tree doesn't follow this ino requirement.
19080d7fd1eSQu Wenruo */
19180d7fd1eSQu Wenruo if (!is_fstree(btrfs_header_owner(leaf)))
19280d7fd1eSQu Wenruo return true;
19380d7fd1eSQu Wenruo
19480d7fd1eSQu Wenruo if (key->objectid == prev_key->objectid)
19580d7fd1eSQu Wenruo return true;
19680d7fd1eSQu Wenruo
19780d7fd1eSQu Wenruo /* Error found */
19880d7fd1eSQu Wenruo dir_item_err(leaf, slot,
19980d7fd1eSQu Wenruo "invalid previous key objectid, have %llu expect %llu",
20080d7fd1eSQu Wenruo prev_key->objectid, key->objectid);
20180d7fd1eSQu Wenruo return false;
20280d7fd1eSQu Wenruo }
check_extent_data_item(struct extent_buffer * leaf,struct btrfs_key * key,int slot,struct btrfs_key * prev_key)203ae2a19d8SDavid Sterba static int check_extent_data_item(struct extent_buffer *leaf,
2044e9845efSFilipe Manana struct btrfs_key *key, int slot,
2054e9845efSFilipe Manana struct btrfs_key *prev_key)
206557ea5ddSQu Wenruo {
207ae2a19d8SDavid Sterba struct btrfs_fs_info *fs_info = leaf->fs_info;
208557ea5ddSQu Wenruo struct btrfs_file_extent_item *fi;
2092f659546SQu Wenruo u32 sectorsize = fs_info->sectorsize;
2103212fa14SJosef Bacik u32 item_size = btrfs_item_size(leaf, slot);
2114c094c33SQu Wenruo u64 extent_end;
212557ea5ddSQu Wenruo
213c7c01a4aSDavid Sterba if (unlikely(!IS_ALIGNED(key->offset, sectorsize))) {
2141fd715ffSDavid Sterba file_extent_err(leaf, slot,
2158806d718SQu Wenruo "unaligned file_offset for file extent, have %llu should be aligned to %u",
2168806d718SQu Wenruo key->offset, sectorsize);
217557ea5ddSQu Wenruo return -EUCLEAN;
218557ea5ddSQu Wenruo }
219557ea5ddSQu Wenruo
220c18679ebSQu Wenruo /*
221c18679ebSQu Wenruo * Previous key must have the same key->objectid (ino).
222c18679ebSQu Wenruo * It can be XATTR_ITEM, INODE_ITEM or just another EXTENT_DATA.
223c18679ebSQu Wenruo * But if objectids mismatch, it means we have a missing
224c18679ebSQu Wenruo * INODE_ITEM.
225c18679ebSQu Wenruo */
226c7c01a4aSDavid Sterba if (unlikely(!check_prev_ino(leaf, key, slot, prev_key)))
227c18679ebSQu Wenruo return -EUCLEAN;
228c18679ebSQu Wenruo
229557ea5ddSQu Wenruo fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
230557ea5ddSQu Wenruo
231153a6d29SQu Wenruo /*
232153a6d29SQu Wenruo * Make sure the item contains at least inline header, so the file
233153a6d29SQu Wenruo * extent type is not some garbage.
234153a6d29SQu Wenruo */
235c7c01a4aSDavid Sterba if (unlikely(item_size < BTRFS_FILE_EXTENT_INLINE_DATA_START)) {
236153a6d29SQu Wenruo file_extent_err(leaf, slot,
237994bf9cdSAndreas Färber "invalid item size, have %u expect [%zu, %u)",
238153a6d29SQu Wenruo item_size, BTRFS_FILE_EXTENT_INLINE_DATA_START,
239153a6d29SQu Wenruo SZ_4K);
240153a6d29SQu Wenruo return -EUCLEAN;
241153a6d29SQu Wenruo }
242c7c01a4aSDavid Sterba if (unlikely(btrfs_file_extent_type(leaf, fi) >=
243c7c01a4aSDavid Sterba BTRFS_NR_FILE_EXTENT_TYPES)) {
2441fd715ffSDavid Sterba file_extent_err(leaf, slot,
2458806d718SQu Wenruo "invalid type for file extent, have %u expect range [0, %u]",
2468806d718SQu Wenruo btrfs_file_extent_type(leaf, fi),
247b9b1a53eSChengguang Xu BTRFS_NR_FILE_EXTENT_TYPES - 1);
248557ea5ddSQu Wenruo return -EUCLEAN;
249557ea5ddSQu Wenruo }
250557ea5ddSQu Wenruo
251557ea5ddSQu Wenruo /*
25252042d8eSAndrea Gelmini * Support for new compression/encryption must introduce incompat flag,
253557ea5ddSQu Wenruo * and must be caught in open_ctree().
254557ea5ddSQu Wenruo */
255c7c01a4aSDavid Sterba if (unlikely(btrfs_file_extent_compression(leaf, fi) >=
256c7c01a4aSDavid Sterba BTRFS_NR_COMPRESS_TYPES)) {
2571fd715ffSDavid Sterba file_extent_err(leaf, slot,
2588806d718SQu Wenruo "invalid compression for file extent, have %u expect range [0, %u]",
2598806d718SQu Wenruo btrfs_file_extent_compression(leaf, fi),
260ce96b7ffSChengguang Xu BTRFS_NR_COMPRESS_TYPES - 1);
261557ea5ddSQu Wenruo return -EUCLEAN;
262557ea5ddSQu Wenruo }
263c7c01a4aSDavid Sterba if (unlikely(btrfs_file_extent_encryption(leaf, fi))) {
2641fd715ffSDavid Sterba file_extent_err(leaf, slot,
2658806d718SQu Wenruo "invalid encryption for file extent, have %u expect 0",
2668806d718SQu Wenruo btrfs_file_extent_encryption(leaf, fi));
267557ea5ddSQu Wenruo return -EUCLEAN;
268557ea5ddSQu Wenruo }
269557ea5ddSQu Wenruo if (btrfs_file_extent_type(leaf, fi) == BTRFS_FILE_EXTENT_INLINE) {
270557ea5ddSQu Wenruo /* Inline extent must have 0 as key offset */
271c7c01a4aSDavid Sterba if (unlikely(key->offset)) {
2721fd715ffSDavid Sterba file_extent_err(leaf, slot,
2738806d718SQu Wenruo "invalid file_offset for inline file extent, have %llu expect 0",
2748806d718SQu Wenruo key->offset);
275557ea5ddSQu Wenruo return -EUCLEAN;
276557ea5ddSQu Wenruo }
277557ea5ddSQu Wenruo
278557ea5ddSQu Wenruo /* Compressed inline extent has no on-disk size, skip it */
279557ea5ddSQu Wenruo if (btrfs_file_extent_compression(leaf, fi) !=
280557ea5ddSQu Wenruo BTRFS_COMPRESS_NONE)
281557ea5ddSQu Wenruo return 0;
282557ea5ddSQu Wenruo
283557ea5ddSQu Wenruo /* Uncompressed inline extent size must match item size */
284c7c01a4aSDavid Sterba if (unlikely(item_size != BTRFS_FILE_EXTENT_INLINE_DATA_START +
285c7c01a4aSDavid Sterba btrfs_file_extent_ram_bytes(leaf, fi))) {
2861fd715ffSDavid Sterba file_extent_err(leaf, slot,
2878806d718SQu Wenruo "invalid ram_bytes for uncompressed inline extent, have %u expect %llu",
2888806d718SQu Wenruo item_size, BTRFS_FILE_EXTENT_INLINE_DATA_START +
2898806d718SQu Wenruo btrfs_file_extent_ram_bytes(leaf, fi));
290557ea5ddSQu Wenruo return -EUCLEAN;
291557ea5ddSQu Wenruo }
292557ea5ddSQu Wenruo return 0;
293557ea5ddSQu Wenruo }
294557ea5ddSQu Wenruo
295557ea5ddSQu Wenruo /* Regular or preallocated extent has fixed item size */
296c7c01a4aSDavid Sterba if (unlikely(item_size != sizeof(*fi))) {
2971fd715ffSDavid Sterba file_extent_err(leaf, slot,
298709a95c3SArnd Bergmann "invalid item size for reg/prealloc file extent, have %u expect %zu",
2998806d718SQu Wenruo item_size, sizeof(*fi));
300557ea5ddSQu Wenruo return -EUCLEAN;
301557ea5ddSQu Wenruo }
302c7c01a4aSDavid Sterba if (unlikely(CHECK_FE_ALIGNED(leaf, slot, fi, ram_bytes, sectorsize) ||
303033774dcSDavid Sterba CHECK_FE_ALIGNED(leaf, slot, fi, disk_bytenr, sectorsize) ||
304033774dcSDavid Sterba CHECK_FE_ALIGNED(leaf, slot, fi, disk_num_bytes, sectorsize) ||
305033774dcSDavid Sterba CHECK_FE_ALIGNED(leaf, slot, fi, offset, sectorsize) ||
306c7c01a4aSDavid Sterba CHECK_FE_ALIGNED(leaf, slot, fi, num_bytes, sectorsize)))
307557ea5ddSQu Wenruo return -EUCLEAN;
3084e9845efSFilipe Manana
3094c094c33SQu Wenruo /* Catch extent end overflow */
310c7c01a4aSDavid Sterba if (unlikely(check_add_overflow(btrfs_file_extent_num_bytes(leaf, fi),
311c7c01a4aSDavid Sterba key->offset, &extent_end))) {
3124c094c33SQu Wenruo file_extent_err(leaf, slot,
3134c094c33SQu Wenruo "extent end overflow, have file offset %llu extent num bytes %llu",
3144c094c33SQu Wenruo key->offset,
3154c094c33SQu Wenruo btrfs_file_extent_num_bytes(leaf, fi));
3164c094c33SQu Wenruo return -EUCLEAN;
3174c094c33SQu Wenruo }
3184c094c33SQu Wenruo
3194e9845efSFilipe Manana /*
3204e9845efSFilipe Manana * Check that no two consecutive file extent items, in the same leaf,
3214e9845efSFilipe Manana * present ranges that overlap each other.
3224e9845efSFilipe Manana */
3234e9845efSFilipe Manana if (slot > 0 &&
3244e9845efSFilipe Manana prev_key->objectid == key->objectid &&
3254e9845efSFilipe Manana prev_key->type == BTRFS_EXTENT_DATA_KEY) {
3264e9845efSFilipe Manana struct btrfs_file_extent_item *prev_fi;
3274e9845efSFilipe Manana u64 prev_end;
3284e9845efSFilipe Manana
3294e9845efSFilipe Manana prev_fi = btrfs_item_ptr(leaf, slot - 1,
3304e9845efSFilipe Manana struct btrfs_file_extent_item);
3314e9845efSFilipe Manana prev_end = file_extent_end(leaf, prev_key, prev_fi);
332c7c01a4aSDavid Sterba if (unlikely(prev_end > key->offset)) {
3334e9845efSFilipe Manana file_extent_err(leaf, slot - 1,
3344e9845efSFilipe Manana "file extent end range (%llu) goes beyond start offset (%llu) of the next file extent",
3354e9845efSFilipe Manana prev_end, key->offset);
3364e9845efSFilipe Manana return -EUCLEAN;
3374e9845efSFilipe Manana }
3384e9845efSFilipe Manana }
3394e9845efSFilipe Manana
340557ea5ddSQu Wenruo return 0;
341557ea5ddSQu Wenruo }
342557ea5ddSQu Wenruo
check_csum_item(struct extent_buffer * leaf,struct btrfs_key * key,int slot,struct btrfs_key * prev_key)34368128ce7SDavid Sterba static int check_csum_item(struct extent_buffer *leaf, struct btrfs_key *key,
344ad1d8c43SFilipe Manana int slot, struct btrfs_key *prev_key)
345557ea5ddSQu Wenruo {
34668128ce7SDavid Sterba struct btrfs_fs_info *fs_info = leaf->fs_info;
3472f659546SQu Wenruo u32 sectorsize = fs_info->sectorsize;
348223486c2SDavid Sterba const u32 csumsize = fs_info->csum_size;
349557ea5ddSQu Wenruo
350c7c01a4aSDavid Sterba if (unlikely(key->objectid != BTRFS_EXTENT_CSUM_OBJECTID)) {
35186a6be3aSDavid Sterba generic_err(leaf, slot,
352d508c5f0SQu Wenruo "invalid key objectid for csum item, have %llu expect %llu",
353d508c5f0SQu Wenruo key->objectid, BTRFS_EXTENT_CSUM_OBJECTID);
354557ea5ddSQu Wenruo return -EUCLEAN;
355557ea5ddSQu Wenruo }
356c7c01a4aSDavid Sterba if (unlikely(!IS_ALIGNED(key->offset, sectorsize))) {
35786a6be3aSDavid Sterba generic_err(leaf, slot,
358d508c5f0SQu Wenruo "unaligned key offset for csum item, have %llu should be aligned to %u",
359d508c5f0SQu Wenruo key->offset, sectorsize);
360557ea5ddSQu Wenruo return -EUCLEAN;
361557ea5ddSQu Wenruo }
3623212fa14SJosef Bacik if (unlikely(!IS_ALIGNED(btrfs_item_size(leaf, slot), csumsize))) {
36386a6be3aSDavid Sterba generic_err(leaf, slot,
364d508c5f0SQu Wenruo "unaligned item size for csum item, have %u should be aligned to %u",
3653212fa14SJosef Bacik btrfs_item_size(leaf, slot), csumsize);
366557ea5ddSQu Wenruo return -EUCLEAN;
367557ea5ddSQu Wenruo }
368ad1d8c43SFilipe Manana if (slot > 0 && prev_key->type == BTRFS_EXTENT_CSUM_KEY) {
369ad1d8c43SFilipe Manana u64 prev_csum_end;
370ad1d8c43SFilipe Manana u32 prev_item_size;
371ad1d8c43SFilipe Manana
3723212fa14SJosef Bacik prev_item_size = btrfs_item_size(leaf, slot - 1);
373ad1d8c43SFilipe Manana prev_csum_end = (prev_item_size / csumsize) * sectorsize;
374ad1d8c43SFilipe Manana prev_csum_end += prev_key->offset;
375c7c01a4aSDavid Sterba if (unlikely(prev_csum_end > key->offset)) {
376ad1d8c43SFilipe Manana generic_err(leaf, slot - 1,
377ad1d8c43SFilipe Manana "csum end range (%llu) goes beyond the start range (%llu) of the next csum item",
378ad1d8c43SFilipe Manana prev_csum_end, key->offset);
379ad1d8c43SFilipe Manana return -EUCLEAN;
380ad1d8c43SFilipe Manana }
381ad1d8c43SFilipe Manana }
382557ea5ddSQu Wenruo return 0;
383557ea5ddSQu Wenruo }
384557ea5ddSQu Wenruo
385c23c77b0SQu Wenruo /* Inode item error output has the same format as dir_item_err() */
386c23c77b0SQu Wenruo #define inode_item_err(eb, slot, fmt, ...) \
387c23c77b0SQu Wenruo dir_item_err(eb, slot, fmt, __VA_ARGS__)
388c23c77b0SQu Wenruo
check_inode_key(struct extent_buffer * leaf,struct btrfs_key * key,int slot)389c23c77b0SQu Wenruo static int check_inode_key(struct extent_buffer *leaf, struct btrfs_key *key,
390c23c77b0SQu Wenruo int slot)
391c23c77b0SQu Wenruo {
392c23c77b0SQu Wenruo struct btrfs_key item_key;
393c23c77b0SQu Wenruo bool is_inode_item;
394c23c77b0SQu Wenruo
395c23c77b0SQu Wenruo btrfs_item_key_to_cpu(leaf, &item_key, slot);
396c23c77b0SQu Wenruo is_inode_item = (item_key.type == BTRFS_INODE_ITEM_KEY);
397c23c77b0SQu Wenruo
398c23c77b0SQu Wenruo /* For XATTR_ITEM, location key should be all 0 */
399c23c77b0SQu Wenruo if (item_key.type == BTRFS_XATTR_ITEM_KEY) {
400c7c01a4aSDavid Sterba if (unlikely(key->objectid != 0 || key->type != 0 ||
401c7c01a4aSDavid Sterba key->offset != 0))
402c23c77b0SQu Wenruo return -EUCLEAN;
403c23c77b0SQu Wenruo return 0;
404c23c77b0SQu Wenruo }
405c23c77b0SQu Wenruo
406c7c01a4aSDavid Sterba if (unlikely((key->objectid < BTRFS_FIRST_FREE_OBJECTID ||
407c23c77b0SQu Wenruo key->objectid > BTRFS_LAST_FREE_OBJECTID) &&
408c23c77b0SQu Wenruo key->objectid != BTRFS_ROOT_TREE_DIR_OBJECTID &&
409c7c01a4aSDavid Sterba key->objectid != BTRFS_FREE_INO_OBJECTID)) {
410c23c77b0SQu Wenruo if (is_inode_item) {
411c23c77b0SQu Wenruo generic_err(leaf, slot,
412c23c77b0SQu Wenruo "invalid key objectid: has %llu expect %llu or [%llu, %llu] or %llu",
413c23c77b0SQu Wenruo key->objectid, BTRFS_ROOT_TREE_DIR_OBJECTID,
414c23c77b0SQu Wenruo BTRFS_FIRST_FREE_OBJECTID,
415c23c77b0SQu Wenruo BTRFS_LAST_FREE_OBJECTID,
416c23c77b0SQu Wenruo BTRFS_FREE_INO_OBJECTID);
417c23c77b0SQu Wenruo } else {
418c23c77b0SQu Wenruo dir_item_err(leaf, slot,
419c23c77b0SQu Wenruo "invalid location key objectid: has %llu expect %llu or [%llu, %llu] or %llu",
420c23c77b0SQu Wenruo key->objectid, BTRFS_ROOT_TREE_DIR_OBJECTID,
421c23c77b0SQu Wenruo BTRFS_FIRST_FREE_OBJECTID,
422c23c77b0SQu Wenruo BTRFS_LAST_FREE_OBJECTID,
423c23c77b0SQu Wenruo BTRFS_FREE_INO_OBJECTID);
424c23c77b0SQu Wenruo }
425c23c77b0SQu Wenruo return -EUCLEAN;
426c23c77b0SQu Wenruo }
427c7c01a4aSDavid Sterba if (unlikely(key->offset != 0)) {
428c23c77b0SQu Wenruo if (is_inode_item)
429c23c77b0SQu Wenruo inode_item_err(leaf, slot,
430c23c77b0SQu Wenruo "invalid key offset: has %llu expect 0",
431c23c77b0SQu Wenruo key->offset);
432c23c77b0SQu Wenruo else
433c23c77b0SQu Wenruo dir_item_err(leaf, slot,
434c23c77b0SQu Wenruo "invalid location key offset:has %llu expect 0",
435c23c77b0SQu Wenruo key->offset);
436c23c77b0SQu Wenruo return -EUCLEAN;
437c23c77b0SQu Wenruo }
438c23c77b0SQu Wenruo return 0;
439c23c77b0SQu Wenruo }
440c23c77b0SQu Wenruo
check_root_key(struct extent_buffer * leaf,struct btrfs_key * key,int slot)44157a0e674SQu Wenruo static int check_root_key(struct extent_buffer *leaf, struct btrfs_key *key,
44257a0e674SQu Wenruo int slot)
44357a0e674SQu Wenruo {
44457a0e674SQu Wenruo struct btrfs_key item_key;
44557a0e674SQu Wenruo bool is_root_item;
44657a0e674SQu Wenruo
44757a0e674SQu Wenruo btrfs_item_key_to_cpu(leaf, &item_key, slot);
44857a0e674SQu Wenruo is_root_item = (item_key.type == BTRFS_ROOT_ITEM_KEY);
44957a0e674SQu Wenruo
4506ebcd021SQu Wenruo /*
4516ebcd021SQu Wenruo * Bad rootid for reloc trees.
4526ebcd021SQu Wenruo *
4536ebcd021SQu Wenruo * Reloc trees are only for subvolume trees, other trees only need
4546ebcd021SQu Wenruo * to be COWed to be relocated.
4556ebcd021SQu Wenruo */
4566ebcd021SQu Wenruo if (unlikely(is_root_item && key->objectid == BTRFS_TREE_RELOC_OBJECTID &&
4576ebcd021SQu Wenruo !is_fstree(key->offset))) {
4586ebcd021SQu Wenruo generic_err(leaf, slot,
4596ebcd021SQu Wenruo "invalid reloc tree for root %lld, root id is not a subvolume tree",
4606ebcd021SQu Wenruo key->offset);
4616ebcd021SQu Wenruo return -EUCLEAN;
4626ebcd021SQu Wenruo }
4636ebcd021SQu Wenruo
46457a0e674SQu Wenruo /* No such tree id */
465c7c01a4aSDavid Sterba if (unlikely(key->objectid == 0)) {
46657a0e674SQu Wenruo if (is_root_item)
46757a0e674SQu Wenruo generic_err(leaf, slot, "invalid root id 0");
46857a0e674SQu Wenruo else
46957a0e674SQu Wenruo dir_item_err(leaf, slot,
47057a0e674SQu Wenruo "invalid location key root id 0");
47157a0e674SQu Wenruo return -EUCLEAN;
47257a0e674SQu Wenruo }
47357a0e674SQu Wenruo
47457a0e674SQu Wenruo /* DIR_ITEM/INDEX/INODE_REF is not allowed to point to non-fs trees */
475c7c01a4aSDavid Sterba if (unlikely(!is_fstree(key->objectid) && !is_root_item)) {
47657a0e674SQu Wenruo dir_item_err(leaf, slot,
47757a0e674SQu Wenruo "invalid location key objectid, have %llu expect [%llu, %llu]",
47857a0e674SQu Wenruo key->objectid, BTRFS_FIRST_FREE_OBJECTID,
47957a0e674SQu Wenruo BTRFS_LAST_FREE_OBJECTID);
48057a0e674SQu Wenruo return -EUCLEAN;
48157a0e674SQu Wenruo }
48257a0e674SQu Wenruo
48357a0e674SQu Wenruo /*
48457a0e674SQu Wenruo * ROOT_ITEM with non-zero offset means this is a snapshot, created at
48557a0e674SQu Wenruo * @offset transid.
48657a0e674SQu Wenruo * Furthermore, for location key in DIR_ITEM, its offset is always -1.
48757a0e674SQu Wenruo *
48857a0e674SQu Wenruo * So here we only check offset for reloc tree whose key->offset must
48957a0e674SQu Wenruo * be a valid tree.
49057a0e674SQu Wenruo */
491c7c01a4aSDavid Sterba if (unlikely(key->objectid == BTRFS_TREE_RELOC_OBJECTID &&
492c7c01a4aSDavid Sterba key->offset == 0)) {
49357a0e674SQu Wenruo generic_err(leaf, slot, "invalid root id 0 for reloc tree");
49457a0e674SQu Wenruo return -EUCLEAN;
49557a0e674SQu Wenruo }
49657a0e674SQu Wenruo return 0;
49757a0e674SQu Wenruo }
49857a0e674SQu Wenruo
check_dir_item(struct extent_buffer * leaf,struct btrfs_key * key,struct btrfs_key * prev_key,int slot)499ce4252c0SDavid Sterba static int check_dir_item(struct extent_buffer *leaf,
500c18679ebSQu Wenruo struct btrfs_key *key, struct btrfs_key *prev_key,
501c18679ebSQu Wenruo int slot)
502ad7b0368SQu Wenruo {
503ce4252c0SDavid Sterba struct btrfs_fs_info *fs_info = leaf->fs_info;
504ad7b0368SQu Wenruo struct btrfs_dir_item *di;
5053212fa14SJosef Bacik u32 item_size = btrfs_item_size(leaf, slot);
506ad7b0368SQu Wenruo u32 cur = 0;
507ad7b0368SQu Wenruo
508c7c01a4aSDavid Sterba if (unlikely(!check_prev_ino(leaf, key, slot, prev_key)))
509c18679ebSQu Wenruo return -EUCLEAN;
510c7c01a4aSDavid Sterba
511ad7b0368SQu Wenruo di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
512ad7b0368SQu Wenruo while (cur < item_size) {
513147a097cSQu Wenruo struct btrfs_key location_key;
514ad7b0368SQu Wenruo u32 name_len;
515ad7b0368SQu Wenruo u32 data_len;
516ad7b0368SQu Wenruo u32 max_name_len;
517ad7b0368SQu Wenruo u32 total_size;
518ad7b0368SQu Wenruo u32 name_hash;
519ad7b0368SQu Wenruo u8 dir_type;
520147a097cSQu Wenruo int ret;
521ad7b0368SQu Wenruo
522ad7b0368SQu Wenruo /* header itself should not cross item boundary */
523c7c01a4aSDavid Sterba if (unlikely(cur + sizeof(*di) > item_size)) {
524d98ced68SDavid Sterba dir_item_err(leaf, slot,
5257cfad652SArnd Bergmann "dir item header crosses item boundary, have %zu boundary %u",
526ad7b0368SQu Wenruo cur + sizeof(*di), item_size);
527ad7b0368SQu Wenruo return -EUCLEAN;
528ad7b0368SQu Wenruo }
529ad7b0368SQu Wenruo
530147a097cSQu Wenruo /* Location key check */
531147a097cSQu Wenruo btrfs_dir_item_key_to_cpu(leaf, di, &location_key);
532147a097cSQu Wenruo if (location_key.type == BTRFS_ROOT_ITEM_KEY) {
533147a097cSQu Wenruo ret = check_root_key(leaf, &location_key, slot);
534c7c01a4aSDavid Sterba if (unlikely(ret < 0))
535147a097cSQu Wenruo return ret;
536147a097cSQu Wenruo } else if (location_key.type == BTRFS_INODE_ITEM_KEY ||
537147a097cSQu Wenruo location_key.type == 0) {
538147a097cSQu Wenruo ret = check_inode_key(leaf, &location_key, slot);
539c7c01a4aSDavid Sterba if (unlikely(ret < 0))
540147a097cSQu Wenruo return ret;
541147a097cSQu Wenruo } else {
542147a097cSQu Wenruo dir_item_err(leaf, slot,
543147a097cSQu Wenruo "invalid location key type, have %u, expect %u or %u",
544147a097cSQu Wenruo location_key.type, BTRFS_ROOT_ITEM_KEY,
545147a097cSQu Wenruo BTRFS_INODE_ITEM_KEY);
546147a097cSQu Wenruo return -EUCLEAN;
547147a097cSQu Wenruo }
548147a097cSQu Wenruo
549ad7b0368SQu Wenruo /* dir type check */
55094a48aefSOmar Sandoval dir_type = btrfs_dir_ftype(leaf, di);
551251508b9SQu Wenruo if (unlikely(dir_type <= BTRFS_FT_UNKNOWN ||
552251508b9SQu Wenruo dir_type >= BTRFS_FT_MAX)) {
553d98ced68SDavid Sterba dir_item_err(leaf, slot,
554251508b9SQu Wenruo "invalid dir item type, have %u expect (0, %u)",
555ad7b0368SQu Wenruo dir_type, BTRFS_FT_MAX);
556ad7b0368SQu Wenruo return -EUCLEAN;
557ad7b0368SQu Wenruo }
558ad7b0368SQu Wenruo
559c7c01a4aSDavid Sterba if (unlikely(key->type == BTRFS_XATTR_ITEM_KEY &&
560c7c01a4aSDavid Sterba dir_type != BTRFS_FT_XATTR)) {
561d98ced68SDavid Sterba dir_item_err(leaf, slot,
562ad7b0368SQu Wenruo "invalid dir item type for XATTR key, have %u expect %u",
563ad7b0368SQu Wenruo dir_type, BTRFS_FT_XATTR);
564ad7b0368SQu Wenruo return -EUCLEAN;
565ad7b0368SQu Wenruo }
566c7c01a4aSDavid Sterba if (unlikely(dir_type == BTRFS_FT_XATTR &&
567c7c01a4aSDavid Sterba key->type != BTRFS_XATTR_ITEM_KEY)) {
568d98ced68SDavid Sterba dir_item_err(leaf, slot,
569ad7b0368SQu Wenruo "xattr dir type found for non-XATTR key");
570ad7b0368SQu Wenruo return -EUCLEAN;
571ad7b0368SQu Wenruo }
572ad7b0368SQu Wenruo if (dir_type == BTRFS_FT_XATTR)
573ad7b0368SQu Wenruo max_name_len = XATTR_NAME_MAX;
574ad7b0368SQu Wenruo else
575ad7b0368SQu Wenruo max_name_len = BTRFS_NAME_LEN;
576ad7b0368SQu Wenruo
577ad7b0368SQu Wenruo /* Name/data length check */
578ad7b0368SQu Wenruo name_len = btrfs_dir_name_len(leaf, di);
579ad7b0368SQu Wenruo data_len = btrfs_dir_data_len(leaf, di);
580c7c01a4aSDavid Sterba if (unlikely(name_len > max_name_len)) {
581d98ced68SDavid Sterba dir_item_err(leaf, slot,
582ad7b0368SQu Wenruo "dir item name len too long, have %u max %u",
583ad7b0368SQu Wenruo name_len, max_name_len);
584ad7b0368SQu Wenruo return -EUCLEAN;
585ad7b0368SQu Wenruo }
586c7c01a4aSDavid Sterba if (unlikely(name_len + data_len > BTRFS_MAX_XATTR_SIZE(fs_info))) {
587d98ced68SDavid Sterba dir_item_err(leaf, slot,
588ad7b0368SQu Wenruo "dir item name and data len too long, have %u max %u",
589ad7b0368SQu Wenruo name_len + data_len,
5902f659546SQu Wenruo BTRFS_MAX_XATTR_SIZE(fs_info));
591ad7b0368SQu Wenruo return -EUCLEAN;
592ad7b0368SQu Wenruo }
593ad7b0368SQu Wenruo
594c7c01a4aSDavid Sterba if (unlikely(data_len && dir_type != BTRFS_FT_XATTR)) {
595d98ced68SDavid Sterba dir_item_err(leaf, slot,
596ad7b0368SQu Wenruo "dir item with invalid data len, have %u expect 0",
597ad7b0368SQu Wenruo data_len);
598ad7b0368SQu Wenruo return -EUCLEAN;
599ad7b0368SQu Wenruo }
600ad7b0368SQu Wenruo
601ad7b0368SQu Wenruo total_size = sizeof(*di) + name_len + data_len;
602ad7b0368SQu Wenruo
603ad7b0368SQu Wenruo /* header and name/data should not cross item boundary */
604c7c01a4aSDavid Sterba if (unlikely(cur + total_size > item_size)) {
605d98ced68SDavid Sterba dir_item_err(leaf, slot,
606ad7b0368SQu Wenruo "dir item data crosses item boundary, have %u boundary %u",
607ad7b0368SQu Wenruo cur + total_size, item_size);
608ad7b0368SQu Wenruo return -EUCLEAN;
609ad7b0368SQu Wenruo }
610ad7b0368SQu Wenruo
611ad7b0368SQu Wenruo /*
612ad7b0368SQu Wenruo * Special check for XATTR/DIR_ITEM, as key->offset is name
613ad7b0368SQu Wenruo * hash, should match its name
614ad7b0368SQu Wenruo */
615ad7b0368SQu Wenruo if (key->type == BTRFS_DIR_ITEM_KEY ||
616ad7b0368SQu Wenruo key->type == BTRFS_XATTR_ITEM_KEY) {
617e2683fc9SDavid Sterba char namebuf[max(BTRFS_NAME_LEN, XATTR_NAME_MAX)];
618e2683fc9SDavid Sterba
619ad7b0368SQu Wenruo read_extent_buffer(leaf, namebuf,
620ad7b0368SQu Wenruo (unsigned long)(di + 1), name_len);
621ad7b0368SQu Wenruo name_hash = btrfs_name_hash(namebuf, name_len);
622c7c01a4aSDavid Sterba if (unlikely(key->offset != name_hash)) {
623d98ced68SDavid Sterba dir_item_err(leaf, slot,
624ad7b0368SQu Wenruo "name hash mismatch with key, have 0x%016x expect 0x%016llx",
625ad7b0368SQu Wenruo name_hash, key->offset);
626ad7b0368SQu Wenruo return -EUCLEAN;
627ad7b0368SQu Wenruo }
628ad7b0368SQu Wenruo }
629ad7b0368SQu Wenruo cur += total_size;
630ad7b0368SQu Wenruo di = (struct btrfs_dir_item *)((void *)di + total_size);
631ad7b0368SQu Wenruo }
632ad7b0368SQu Wenruo return 0;
633ad7b0368SQu Wenruo }
634ad7b0368SQu Wenruo
6354806bd88SDavid Sterba __printf(3, 4)
636fce466eaSQu Wenruo __cold
block_group_err(const struct extent_buffer * eb,int slot,const char * fmt,...)6374806bd88SDavid Sterba static void block_group_err(const struct extent_buffer *eb, int slot,
638fce466eaSQu Wenruo const char *fmt, ...)
639fce466eaSQu Wenruo {
6404806bd88SDavid Sterba const struct btrfs_fs_info *fs_info = eb->fs_info;
641fce466eaSQu Wenruo struct btrfs_key key;
642fce466eaSQu Wenruo struct va_format vaf;
643fce466eaSQu Wenruo va_list args;
644fce466eaSQu Wenruo
645fce466eaSQu Wenruo btrfs_item_key_to_cpu(eb, &key, slot);
646fce466eaSQu Wenruo va_start(args, fmt);
647fce466eaSQu Wenruo
648fce466eaSQu Wenruo vaf.fmt = fmt;
649fce466eaSQu Wenruo vaf.va = &args;
650fce466eaSQu Wenruo
651fce466eaSQu Wenruo btrfs_crit(fs_info,
652fce466eaSQu Wenruo "corrupt %s: root=%llu block=%llu slot=%d bg_start=%llu bg_len=%llu, %pV",
653fce466eaSQu Wenruo btrfs_header_level(eb) == 0 ? "leaf" : "node",
654fce466eaSQu Wenruo btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot,
655fce466eaSQu Wenruo key.objectid, key.offset, &vaf);
656fce466eaSQu Wenruo va_end(args);
657fce466eaSQu Wenruo }
658fce466eaSQu Wenruo
check_block_group_item(struct extent_buffer * leaf,struct btrfs_key * key,int slot)659af60ce2bSDavid Sterba static int check_block_group_item(struct extent_buffer *leaf,
660fce466eaSQu Wenruo struct btrfs_key *key, int slot)
661fce466eaSQu Wenruo {
662f7238e50SJosef Bacik struct btrfs_fs_info *fs_info = leaf->fs_info;
663fce466eaSQu Wenruo struct btrfs_block_group_item bgi;
6643212fa14SJosef Bacik u32 item_size = btrfs_item_size(leaf, slot);
665f7238e50SJosef Bacik u64 chunk_objectid;
666fce466eaSQu Wenruo u64 flags;
667fce466eaSQu Wenruo u64 type;
668fce466eaSQu Wenruo
669fce466eaSQu Wenruo /*
670fce466eaSQu Wenruo * Here we don't really care about alignment since extent allocator can
67110950929SQu Wenruo * handle it. We care more about the size.
672fce466eaSQu Wenruo */
673c7c01a4aSDavid Sterba if (unlikely(key->offset == 0)) {
6744806bd88SDavid Sterba block_group_err(leaf, slot,
67510950929SQu Wenruo "invalid block group size 0");
676fce466eaSQu Wenruo return -EUCLEAN;
677fce466eaSQu Wenruo }
678fce466eaSQu Wenruo
679c7c01a4aSDavid Sterba if (unlikely(item_size != sizeof(bgi))) {
6804806bd88SDavid Sterba block_group_err(leaf, slot,
681fce466eaSQu Wenruo "invalid item size, have %u expect %zu",
682fce466eaSQu Wenruo item_size, sizeof(bgi));
683fce466eaSQu Wenruo return -EUCLEAN;
684fce466eaSQu Wenruo }
685fce466eaSQu Wenruo
686fce466eaSQu Wenruo read_extent_buffer(leaf, &bgi, btrfs_item_ptr_offset(leaf, slot),
687fce466eaSQu Wenruo sizeof(bgi));
688f7238e50SJosef Bacik chunk_objectid = btrfs_stack_block_group_chunk_objectid(&bgi);
689f7238e50SJosef Bacik if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2)) {
690f7238e50SJosef Bacik /*
691f7238e50SJosef Bacik * We don't init the nr_global_roots until we load the global
692f7238e50SJosef Bacik * roots, so this could be 0 at mount time. If it's 0 we'll
693f7238e50SJosef Bacik * just assume we're fine, and later we'll check against our
694f7238e50SJosef Bacik * actual value.
695f7238e50SJosef Bacik */
696f7238e50SJosef Bacik if (unlikely(fs_info->nr_global_roots &&
697f7238e50SJosef Bacik chunk_objectid >= fs_info->nr_global_roots)) {
698f7238e50SJosef Bacik block_group_err(leaf, slot,
699f7238e50SJosef Bacik "invalid block group global root id, have %llu, needs to be <= %llu",
700f7238e50SJosef Bacik chunk_objectid,
701f7238e50SJosef Bacik fs_info->nr_global_roots);
702f7238e50SJosef Bacik return -EUCLEAN;
703f7238e50SJosef Bacik }
704f7238e50SJosef Bacik } else if (unlikely(chunk_objectid != BTRFS_FIRST_CHUNK_TREE_OBJECTID)) {
7054806bd88SDavid Sterba block_group_err(leaf, slot,
706fce466eaSQu Wenruo "invalid block group chunk objectid, have %llu expect %llu",
707de0dc456SDavid Sterba btrfs_stack_block_group_chunk_objectid(&bgi),
708fce466eaSQu Wenruo BTRFS_FIRST_CHUNK_TREE_OBJECTID);
709fce466eaSQu Wenruo return -EUCLEAN;
710fce466eaSQu Wenruo }
711fce466eaSQu Wenruo
712c7c01a4aSDavid Sterba if (unlikely(btrfs_stack_block_group_used(&bgi) > key->offset)) {
7134806bd88SDavid Sterba block_group_err(leaf, slot,
714fce466eaSQu Wenruo "invalid block group used, have %llu expect [0, %llu)",
715de0dc456SDavid Sterba btrfs_stack_block_group_used(&bgi), key->offset);
716fce466eaSQu Wenruo return -EUCLEAN;
717fce466eaSQu Wenruo }
718fce466eaSQu Wenruo
719de0dc456SDavid Sterba flags = btrfs_stack_block_group_flags(&bgi);
720c7c01a4aSDavid Sterba if (unlikely(hweight64(flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) > 1)) {
7214806bd88SDavid Sterba block_group_err(leaf, slot,
722fce466eaSQu Wenruo "invalid profile flags, have 0x%llx (%lu bits set) expect no more than 1 bit set",
723fce466eaSQu Wenruo flags & BTRFS_BLOCK_GROUP_PROFILE_MASK,
724fce466eaSQu Wenruo hweight64(flags & BTRFS_BLOCK_GROUP_PROFILE_MASK));
725fce466eaSQu Wenruo return -EUCLEAN;
726fce466eaSQu Wenruo }
727fce466eaSQu Wenruo
728fce466eaSQu Wenruo type = flags & BTRFS_BLOCK_GROUP_TYPE_MASK;
729c7c01a4aSDavid Sterba if (unlikely(type != BTRFS_BLOCK_GROUP_DATA &&
730fce466eaSQu Wenruo type != BTRFS_BLOCK_GROUP_METADATA &&
731fce466eaSQu Wenruo type != BTRFS_BLOCK_GROUP_SYSTEM &&
732fce466eaSQu Wenruo type != (BTRFS_BLOCK_GROUP_METADATA |
733c7c01a4aSDavid Sterba BTRFS_BLOCK_GROUP_DATA))) {
7344806bd88SDavid Sterba block_group_err(leaf, slot,
735761333f2SShaokun Zhang "invalid type, have 0x%llx (%lu bits set) expect either 0x%llx, 0x%llx, 0x%llx or 0x%llx",
736fce466eaSQu Wenruo type, hweight64(type),
737fce466eaSQu Wenruo BTRFS_BLOCK_GROUP_DATA, BTRFS_BLOCK_GROUP_METADATA,
738fce466eaSQu Wenruo BTRFS_BLOCK_GROUP_SYSTEM,
739fce466eaSQu Wenruo BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA);
740fce466eaSQu Wenruo return -EUCLEAN;
741fce466eaSQu Wenruo }
742fce466eaSQu Wenruo return 0;
743fce466eaSQu Wenruo }
744fce466eaSQu Wenruo
745d001e4a3SDavid Sterba __printf(4, 5)
746f1140243SQu Wenruo __cold
chunk_err(const struct extent_buffer * leaf,const struct btrfs_chunk * chunk,u64 logical,const char * fmt,...)747d001e4a3SDavid Sterba static void chunk_err(const struct extent_buffer *leaf,
748f1140243SQu Wenruo const struct btrfs_chunk *chunk, u64 logical,
749f1140243SQu Wenruo const char *fmt, ...)
750f1140243SQu Wenruo {
751d001e4a3SDavid Sterba const struct btrfs_fs_info *fs_info = leaf->fs_info;
752f1140243SQu Wenruo bool is_sb;
753f1140243SQu Wenruo struct va_format vaf;
754f1140243SQu Wenruo va_list args;
755f1140243SQu Wenruo int i;
756f1140243SQu Wenruo int slot = -1;
757f1140243SQu Wenruo
758f1140243SQu Wenruo /* Only superblock eb is able to have such small offset */
759f1140243SQu Wenruo is_sb = (leaf->start == BTRFS_SUPER_INFO_OFFSET);
760f1140243SQu Wenruo
761f1140243SQu Wenruo if (!is_sb) {
762f1140243SQu Wenruo /*
763f1140243SQu Wenruo * Get the slot number by iterating through all slots, this
764f1140243SQu Wenruo * would provide better readability.
765f1140243SQu Wenruo */
766f1140243SQu Wenruo for (i = 0; i < btrfs_header_nritems(leaf); i++) {
767f1140243SQu Wenruo if (btrfs_item_ptr_offset(leaf, i) ==
768f1140243SQu Wenruo (unsigned long)chunk) {
769f1140243SQu Wenruo slot = i;
770f1140243SQu Wenruo break;
771f1140243SQu Wenruo }
772f1140243SQu Wenruo }
773f1140243SQu Wenruo }
774f1140243SQu Wenruo va_start(args, fmt);
775f1140243SQu Wenruo vaf.fmt = fmt;
776f1140243SQu Wenruo vaf.va = &args;
777f1140243SQu Wenruo
778f1140243SQu Wenruo if (is_sb)
779f1140243SQu Wenruo btrfs_crit(fs_info,
780f1140243SQu Wenruo "corrupt superblock syschunk array: chunk_start=%llu, %pV",
781f1140243SQu Wenruo logical, &vaf);
782f1140243SQu Wenruo else
783f1140243SQu Wenruo btrfs_crit(fs_info,
784f1140243SQu Wenruo "corrupt leaf: root=%llu block=%llu slot=%d chunk_start=%llu, %pV",
785f1140243SQu Wenruo BTRFS_CHUNK_TREE_OBJECTID, leaf->start, slot,
786f1140243SQu Wenruo logical, &vaf);
787f1140243SQu Wenruo va_end(args);
788f1140243SQu Wenruo }
789f1140243SQu Wenruo
790ad7b0368SQu Wenruo /*
79182fc28fbSQu Wenruo * The common chunk check which could also work on super block sys chunk array.
79282fc28fbSQu Wenruo *
793bf871c3bSQu Wenruo * Return -EUCLEAN if anything is corrupted.
79482fc28fbSQu Wenruo * Return 0 if everything is OK.
79582fc28fbSQu Wenruo */
btrfs_check_chunk_valid(struct extent_buffer * leaf,struct btrfs_chunk * chunk,u64 logical)796ddaf1d5aSDavid Sterba int btrfs_check_chunk_valid(struct extent_buffer *leaf,
79782fc28fbSQu Wenruo struct btrfs_chunk *chunk, u64 logical)
79882fc28fbSQu Wenruo {
799ddaf1d5aSDavid Sterba struct btrfs_fs_info *fs_info = leaf->fs_info;
80082fc28fbSQu Wenruo u64 length;
801347fb0cfSSu Yue u64 chunk_end;
80282fc28fbSQu Wenruo u64 stripe_len;
80382fc28fbSQu Wenruo u16 num_stripes;
80482fc28fbSQu Wenruo u16 sub_stripes;
80582fc28fbSQu Wenruo u64 type;
80682fc28fbSQu Wenruo u64 features;
80782fc28fbSQu Wenruo bool mixed = false;
80885d07fbeSDaniel Xu int raid_index;
80985d07fbeSDaniel Xu int nparity;
81085d07fbeSDaniel Xu int ncopies;
81182fc28fbSQu Wenruo
81282fc28fbSQu Wenruo length = btrfs_chunk_length(leaf, chunk);
81382fc28fbSQu Wenruo stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
81482fc28fbSQu Wenruo num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
81582fc28fbSQu Wenruo sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
81682fc28fbSQu Wenruo type = btrfs_chunk_type(leaf, chunk);
81785d07fbeSDaniel Xu raid_index = btrfs_bg_flags_to_raid_index(type);
81885d07fbeSDaniel Xu ncopies = btrfs_raid_array[raid_index].ncopies;
81985d07fbeSDaniel Xu nparity = btrfs_raid_array[raid_index].nparity;
82082fc28fbSQu Wenruo
821c7c01a4aSDavid Sterba if (unlikely(!num_stripes)) {
822d001e4a3SDavid Sterba chunk_err(leaf, chunk, logical,
823f1140243SQu Wenruo "invalid chunk num_stripes, have %u", num_stripes);
824bf871c3bSQu Wenruo return -EUCLEAN;
82582fc28fbSQu Wenruo }
826c7c01a4aSDavid Sterba if (unlikely(num_stripes < ncopies)) {
82785d07fbeSDaniel Xu chunk_err(leaf, chunk, logical,
82885d07fbeSDaniel Xu "invalid chunk num_stripes < ncopies, have %u < %d",
82985d07fbeSDaniel Xu num_stripes, ncopies);
83085d07fbeSDaniel Xu return -EUCLEAN;
83185d07fbeSDaniel Xu }
832c7c01a4aSDavid Sterba if (unlikely(nparity && num_stripes == nparity)) {
83385d07fbeSDaniel Xu chunk_err(leaf, chunk, logical,
83485d07fbeSDaniel Xu "invalid chunk num_stripes == nparity, have %u == %d",
83585d07fbeSDaniel Xu num_stripes, nparity);
83685d07fbeSDaniel Xu return -EUCLEAN;
83785d07fbeSDaniel Xu }
838c7c01a4aSDavid Sterba if (unlikely(!IS_ALIGNED(logical, fs_info->sectorsize))) {
839d001e4a3SDavid Sterba chunk_err(leaf, chunk, logical,
840f1140243SQu Wenruo "invalid chunk logical, have %llu should aligned to %u",
841f1140243SQu Wenruo logical, fs_info->sectorsize);
842bf871c3bSQu Wenruo return -EUCLEAN;
84382fc28fbSQu Wenruo }
844c7c01a4aSDavid Sterba if (unlikely(btrfs_chunk_sector_size(leaf, chunk) != fs_info->sectorsize)) {
845d001e4a3SDavid Sterba chunk_err(leaf, chunk, logical,
846f1140243SQu Wenruo "invalid chunk sectorsize, have %u expect %u",
847f1140243SQu Wenruo btrfs_chunk_sector_size(leaf, chunk),
848f1140243SQu Wenruo fs_info->sectorsize);
849bf871c3bSQu Wenruo return -EUCLEAN;
85082fc28fbSQu Wenruo }
851c7c01a4aSDavid Sterba if (unlikely(!length || !IS_ALIGNED(length, fs_info->sectorsize))) {
852d001e4a3SDavid Sterba chunk_err(leaf, chunk, logical,
853f1140243SQu Wenruo "invalid chunk length, have %llu", length);
854bf871c3bSQu Wenruo return -EUCLEAN;
85582fc28fbSQu Wenruo }
856347fb0cfSSu Yue if (unlikely(check_add_overflow(logical, length, &chunk_end))) {
857347fb0cfSSu Yue chunk_err(leaf, chunk, logical,
858347fb0cfSSu Yue "invalid chunk logical start and length, have logical start %llu length %llu",
859347fb0cfSSu Yue logical, length);
860347fb0cfSSu Yue return -EUCLEAN;
861347fb0cfSSu Yue }
862c7c01a4aSDavid Sterba if (unlikely(!is_power_of_2(stripe_len) || stripe_len != BTRFS_STRIPE_LEN)) {
863d001e4a3SDavid Sterba chunk_err(leaf, chunk, logical,
864f1140243SQu Wenruo "invalid chunk stripe length: %llu",
86582fc28fbSQu Wenruo stripe_len);
866bf871c3bSQu Wenruo return -EUCLEAN;
86782fc28fbSQu Wenruo }
8686ded22c1SQu Wenruo /*
8696ded22c1SQu Wenruo * We artificially limit the chunk size, so that the number of stripes
8706ded22c1SQu Wenruo * inside a chunk can be fit into a U32. The current limit (256G) is
8716ded22c1SQu Wenruo * way too large for real world usage anyway, and it's also much larger
8726ded22c1SQu Wenruo * than our existing limit (10G).
8736ded22c1SQu Wenruo *
8746ded22c1SQu Wenruo * Thus it should be a good way to catch obvious bitflips.
8756ded22c1SQu Wenruo */
876cb091225SQu Wenruo if (unlikely(length >= btrfs_stripe_nr_to_offset(U32_MAX))) {
8776ded22c1SQu Wenruo chunk_err(leaf, chunk, logical,
8786ded22c1SQu Wenruo "chunk length too large: have %llu limit %llu",
879cb091225SQu Wenruo length, btrfs_stripe_nr_to_offset(U32_MAX));
8806ded22c1SQu Wenruo return -EUCLEAN;
8816ded22c1SQu Wenruo }
882c7c01a4aSDavid Sterba if (unlikely(type & ~(BTRFS_BLOCK_GROUP_TYPE_MASK |
883c7c01a4aSDavid Sterba BTRFS_BLOCK_GROUP_PROFILE_MASK))) {
884d001e4a3SDavid Sterba chunk_err(leaf, chunk, logical,
885f1140243SQu Wenruo "unrecognized chunk type: 0x%llx",
88682fc28fbSQu Wenruo ~(BTRFS_BLOCK_GROUP_TYPE_MASK |
88782fc28fbSQu Wenruo BTRFS_BLOCK_GROUP_PROFILE_MASK) &
88882fc28fbSQu Wenruo btrfs_chunk_type(leaf, chunk));
889bf871c3bSQu Wenruo return -EUCLEAN;
89082fc28fbSQu Wenruo }
89182fc28fbSQu Wenruo
892c7c01a4aSDavid Sterba if (unlikely(!has_single_bit_set(type & BTRFS_BLOCK_GROUP_PROFILE_MASK) &&
893c7c01a4aSDavid Sterba (type & BTRFS_BLOCK_GROUP_PROFILE_MASK) != 0)) {
894d001e4a3SDavid Sterba chunk_err(leaf, chunk, logical,
89580e46cf2SQu Wenruo "invalid chunk profile flag: 0x%llx, expect 0 or 1 bit set",
89680e46cf2SQu Wenruo type & BTRFS_BLOCK_GROUP_PROFILE_MASK);
89780e46cf2SQu Wenruo return -EUCLEAN;
89880e46cf2SQu Wenruo }
899c7c01a4aSDavid Sterba if (unlikely((type & BTRFS_BLOCK_GROUP_TYPE_MASK) == 0)) {
900d001e4a3SDavid Sterba chunk_err(leaf, chunk, logical,
901f1140243SQu Wenruo "missing chunk type flag, have 0x%llx one bit must be set in 0x%llx",
902f1140243SQu Wenruo type, BTRFS_BLOCK_GROUP_TYPE_MASK);
903bf871c3bSQu Wenruo return -EUCLEAN;
90482fc28fbSQu Wenruo }
90582fc28fbSQu Wenruo
906c7c01a4aSDavid Sterba if (unlikely((type & BTRFS_BLOCK_GROUP_SYSTEM) &&
907c7c01a4aSDavid Sterba (type & (BTRFS_BLOCK_GROUP_METADATA |
908c7c01a4aSDavid Sterba BTRFS_BLOCK_GROUP_DATA)))) {
909d001e4a3SDavid Sterba chunk_err(leaf, chunk, logical,
910f1140243SQu Wenruo "system chunk with data or metadata type: 0x%llx",
911f1140243SQu Wenruo type);
912bf871c3bSQu Wenruo return -EUCLEAN;
91382fc28fbSQu Wenruo }
91482fc28fbSQu Wenruo
91582fc28fbSQu Wenruo features = btrfs_super_incompat_flags(fs_info->super_copy);
91682fc28fbSQu Wenruo if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
91782fc28fbSQu Wenruo mixed = true;
91882fc28fbSQu Wenruo
91982fc28fbSQu Wenruo if (!mixed) {
920c7c01a4aSDavid Sterba if (unlikely((type & BTRFS_BLOCK_GROUP_METADATA) &&
921c7c01a4aSDavid Sterba (type & BTRFS_BLOCK_GROUP_DATA))) {
922d001e4a3SDavid Sterba chunk_err(leaf, chunk, logical,
92382fc28fbSQu Wenruo "mixed chunk type in non-mixed mode: 0x%llx", type);
924bf871c3bSQu Wenruo return -EUCLEAN;
92582fc28fbSQu Wenruo }
92682fc28fbSQu Wenruo }
92782fc28fbSQu Wenruo
9280ac6e06bSDavid Sterba if (unlikely((type & BTRFS_BLOCK_GROUP_RAID10 &&
9290ac6e06bSDavid Sterba sub_stripes != btrfs_raid_array[BTRFS_RAID_RAID10].sub_stripes) ||
9300ac6e06bSDavid Sterba (type & BTRFS_BLOCK_GROUP_RAID1 &&
9310ac6e06bSDavid Sterba num_stripes != btrfs_raid_array[BTRFS_RAID_RAID1].devs_min) ||
9326c154ba4SDavid Sterba (type & BTRFS_BLOCK_GROUP_RAID1C3 &&
9336c154ba4SDavid Sterba num_stripes != btrfs_raid_array[BTRFS_RAID_RAID1C3].devs_min) ||
9346c154ba4SDavid Sterba (type & BTRFS_BLOCK_GROUP_RAID1C4 &&
9356c154ba4SDavid Sterba num_stripes != btrfs_raid_array[BTRFS_RAID_RAID1C4].devs_min) ||
9360ac6e06bSDavid Sterba (type & BTRFS_BLOCK_GROUP_RAID5 &&
9370ac6e06bSDavid Sterba num_stripes < btrfs_raid_array[BTRFS_RAID_RAID5].devs_min) ||
9380ac6e06bSDavid Sterba (type & BTRFS_BLOCK_GROUP_RAID6 &&
9390ac6e06bSDavid Sterba num_stripes < btrfs_raid_array[BTRFS_RAID_RAID6].devs_min) ||
9400ac6e06bSDavid Sterba (type & BTRFS_BLOCK_GROUP_DUP &&
9410ac6e06bSDavid Sterba num_stripes != btrfs_raid_array[BTRFS_RAID_DUP].dev_stripes) ||
942c7c01a4aSDavid Sterba ((type & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 &&
9430ac6e06bSDavid Sterba num_stripes != btrfs_raid_array[BTRFS_RAID_SINGLE].dev_stripes))) {
944d001e4a3SDavid Sterba chunk_err(leaf, chunk, logical,
94582fc28fbSQu Wenruo "invalid num_stripes:sub_stripes %u:%u for profile %llu",
94682fc28fbSQu Wenruo num_stripes, sub_stripes,
94782fc28fbSQu Wenruo type & BTRFS_BLOCK_GROUP_PROFILE_MASK);
948bf871c3bSQu Wenruo return -EUCLEAN;
94982fc28fbSQu Wenruo }
95082fc28fbSQu Wenruo
95182fc28fbSQu Wenruo return 0;
95282fc28fbSQu Wenruo }
95382fc28fbSQu Wenruo
954f6d2a5c2SQu Wenruo /*
955f6d2a5c2SQu Wenruo * Enhanced version of chunk item checker.
956f6d2a5c2SQu Wenruo *
957f6d2a5c2SQu Wenruo * The common btrfs_check_chunk_valid() doesn't check item size since it needs
958f6d2a5c2SQu Wenruo * to work on super block sys_chunk_array which doesn't have full item ptr.
959f6d2a5c2SQu Wenruo */
check_leaf_chunk_item(struct extent_buffer * leaf,struct btrfs_chunk * chunk,struct btrfs_key * key,int slot)960f6d2a5c2SQu Wenruo static int check_leaf_chunk_item(struct extent_buffer *leaf,
961f6d2a5c2SQu Wenruo struct btrfs_chunk *chunk,
962f6d2a5c2SQu Wenruo struct btrfs_key *key, int slot)
963f6d2a5c2SQu Wenruo {
964f6d2a5c2SQu Wenruo int num_stripes;
965f6d2a5c2SQu Wenruo
9663212fa14SJosef Bacik if (unlikely(btrfs_item_size(leaf, slot) < sizeof(struct btrfs_chunk))) {
967f6d2a5c2SQu Wenruo chunk_err(leaf, chunk, key->offset,
968f6d2a5c2SQu Wenruo "invalid chunk item size: have %u expect [%zu, %u)",
9693212fa14SJosef Bacik btrfs_item_size(leaf, slot),
970f6d2a5c2SQu Wenruo sizeof(struct btrfs_chunk),
971f6d2a5c2SQu Wenruo BTRFS_LEAF_DATA_SIZE(leaf->fs_info));
972f6d2a5c2SQu Wenruo return -EUCLEAN;
973f6d2a5c2SQu Wenruo }
974f6d2a5c2SQu Wenruo
975f6d2a5c2SQu Wenruo num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
976f6d2a5c2SQu Wenruo /* Let btrfs_check_chunk_valid() handle this error type */
977f6d2a5c2SQu Wenruo if (num_stripes == 0)
978f6d2a5c2SQu Wenruo goto out;
979f6d2a5c2SQu Wenruo
980c7c01a4aSDavid Sterba if (unlikely(btrfs_chunk_item_size(num_stripes) !=
9813212fa14SJosef Bacik btrfs_item_size(leaf, slot))) {
982f6d2a5c2SQu Wenruo chunk_err(leaf, chunk, key->offset,
983f6d2a5c2SQu Wenruo "invalid chunk item size: have %u expect %lu",
9843212fa14SJosef Bacik btrfs_item_size(leaf, slot),
985f6d2a5c2SQu Wenruo btrfs_chunk_item_size(num_stripes));
986f6d2a5c2SQu Wenruo return -EUCLEAN;
987f6d2a5c2SQu Wenruo }
988f6d2a5c2SQu Wenruo out:
989f6d2a5c2SQu Wenruo return btrfs_check_chunk_valid(leaf, chunk, key->offset);
990f6d2a5c2SQu Wenruo }
991f6d2a5c2SQu Wenruo
9925617ed80SDavid Sterba __printf(3, 4)
993ab4ba2e1SQu Wenruo __cold
dev_item_err(const struct extent_buffer * eb,int slot,const char * fmt,...)9945617ed80SDavid Sterba static void dev_item_err(const struct extent_buffer *eb, int slot,
995ab4ba2e1SQu Wenruo const char *fmt, ...)
996ab4ba2e1SQu Wenruo {
997ab4ba2e1SQu Wenruo struct btrfs_key key;
998ab4ba2e1SQu Wenruo struct va_format vaf;
999ab4ba2e1SQu Wenruo va_list args;
1000ab4ba2e1SQu Wenruo
1001ab4ba2e1SQu Wenruo btrfs_item_key_to_cpu(eb, &key, slot);
1002ab4ba2e1SQu Wenruo va_start(args, fmt);
1003ab4ba2e1SQu Wenruo
1004ab4ba2e1SQu Wenruo vaf.fmt = fmt;
1005ab4ba2e1SQu Wenruo vaf.va = &args;
1006ab4ba2e1SQu Wenruo
10075617ed80SDavid Sterba btrfs_crit(eb->fs_info,
1008ab4ba2e1SQu Wenruo "corrupt %s: root=%llu block=%llu slot=%d devid=%llu %pV",
1009ab4ba2e1SQu Wenruo btrfs_header_level(eb) == 0 ? "leaf" : "node",
1010ab4ba2e1SQu Wenruo btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot,
1011ab4ba2e1SQu Wenruo key.objectid, &vaf);
1012ab4ba2e1SQu Wenruo va_end(args);
1013ab4ba2e1SQu Wenruo }
1014ab4ba2e1SQu Wenruo
check_dev_item(struct extent_buffer * leaf,struct btrfs_key * key,int slot)1015412a2312SDavid Sterba static int check_dev_item(struct extent_buffer *leaf,
1016ab4ba2e1SQu Wenruo struct btrfs_key *key, int slot)
1017ab4ba2e1SQu Wenruo {
1018ab4ba2e1SQu Wenruo struct btrfs_dev_item *ditem;
1019ea1d1ca4SSu Yue const u32 item_size = btrfs_item_size(leaf, slot);
1020ab4ba2e1SQu Wenruo
1021c7c01a4aSDavid Sterba if (unlikely(key->objectid != BTRFS_DEV_ITEMS_OBJECTID)) {
10225617ed80SDavid Sterba dev_item_err(leaf, slot,
1023ab4ba2e1SQu Wenruo "invalid objectid: has=%llu expect=%llu",
1024ab4ba2e1SQu Wenruo key->objectid, BTRFS_DEV_ITEMS_OBJECTID);
1025ab4ba2e1SQu Wenruo return -EUCLEAN;
1026ab4ba2e1SQu Wenruo }
1027ea1d1ca4SSu Yue
1028ea1d1ca4SSu Yue if (unlikely(item_size != sizeof(*ditem))) {
1029ea1d1ca4SSu Yue dev_item_err(leaf, slot, "invalid item size: has %u expect %zu",
1030ea1d1ca4SSu Yue item_size, sizeof(*ditem));
1031ea1d1ca4SSu Yue return -EUCLEAN;
1032ea1d1ca4SSu Yue }
1033ea1d1ca4SSu Yue
1034ab4ba2e1SQu Wenruo ditem = btrfs_item_ptr(leaf, slot, struct btrfs_dev_item);
1035c7c01a4aSDavid Sterba if (unlikely(btrfs_device_id(leaf, ditem) != key->offset)) {
10365617ed80SDavid Sterba dev_item_err(leaf, slot,
1037ab4ba2e1SQu Wenruo "devid mismatch: key has=%llu item has=%llu",
1038ab4ba2e1SQu Wenruo key->offset, btrfs_device_id(leaf, ditem));
1039ab4ba2e1SQu Wenruo return -EUCLEAN;
1040ab4ba2e1SQu Wenruo }
1041ab4ba2e1SQu Wenruo
1042ab4ba2e1SQu Wenruo /*
1043ab4ba2e1SQu Wenruo * For device total_bytes, we don't have reliable way to check it, as
1044ab4ba2e1SQu Wenruo * it can be 0 for device removal. Device size check can only be done
1045ab4ba2e1SQu Wenruo * by dev extents check.
1046ab4ba2e1SQu Wenruo */
1047c7c01a4aSDavid Sterba if (unlikely(btrfs_device_bytes_used(leaf, ditem) >
1048c7c01a4aSDavid Sterba btrfs_device_total_bytes(leaf, ditem))) {
10495617ed80SDavid Sterba dev_item_err(leaf, slot,
1050ab4ba2e1SQu Wenruo "invalid bytes used: have %llu expect [0, %llu]",
1051ab4ba2e1SQu Wenruo btrfs_device_bytes_used(leaf, ditem),
1052ab4ba2e1SQu Wenruo btrfs_device_total_bytes(leaf, ditem));
1053ab4ba2e1SQu Wenruo return -EUCLEAN;
1054ab4ba2e1SQu Wenruo }
1055ab4ba2e1SQu Wenruo /*
1056ab4ba2e1SQu Wenruo * Remaining members like io_align/type/gen/dev_group aren't really
1057ab4ba2e1SQu Wenruo * utilized. Skip them to make later usage of them easier.
1058ab4ba2e1SQu Wenruo */
1059ab4ba2e1SQu Wenruo return 0;
1060ab4ba2e1SQu Wenruo }
1061ab4ba2e1SQu Wenruo
check_inode_item(struct extent_buffer * leaf,struct btrfs_key * key,int slot)106239e57f49SDavid Sterba static int check_inode_item(struct extent_buffer *leaf,
1063496245caSQu Wenruo struct btrfs_key *key, int slot)
1064496245caSQu Wenruo {
106539e57f49SDavid Sterba struct btrfs_fs_info *fs_info = leaf->fs_info;
1066496245caSQu Wenruo struct btrfs_inode_item *iitem;
1067496245caSQu Wenruo u64 super_gen = btrfs_super_generation(fs_info->super_copy);
1068496245caSQu Wenruo u32 valid_mask = (S_IFMT | S_ISUID | S_ISGID | S_ISVTX | 0777);
10690c982944SSu Yue const u32 item_size = btrfs_item_size(leaf, slot);
1070496245caSQu Wenruo u32 mode;
1071c23c77b0SQu Wenruo int ret;
107277eea05eSBoris Burkov u32 flags;
107377eea05eSBoris Burkov u32 ro_flags;
1074496245caSQu Wenruo
1075c23c77b0SQu Wenruo ret = check_inode_key(leaf, key, slot);
1076c7c01a4aSDavid Sterba if (unlikely(ret < 0))
1077c23c77b0SQu Wenruo return ret;
1078c23c77b0SQu Wenruo
10790c982944SSu Yue if (unlikely(item_size != sizeof(*iitem))) {
10800c982944SSu Yue generic_err(leaf, slot, "invalid item size: has %u expect %zu",
10810c982944SSu Yue item_size, sizeof(*iitem));
10820c982944SSu Yue return -EUCLEAN;
10830c982944SSu Yue }
10840c982944SSu Yue
1085496245caSQu Wenruo iitem = btrfs_item_ptr(leaf, slot, struct btrfs_inode_item);
1086496245caSQu Wenruo
1087496245caSQu Wenruo /* Here we use super block generation + 1 to handle log tree */
1088c7c01a4aSDavid Sterba if (unlikely(btrfs_inode_generation(leaf, iitem) > super_gen + 1)) {
1089c3053ebbSQu Wenruo inode_item_err(leaf, slot,
1090496245caSQu Wenruo "invalid inode generation: has %llu expect (0, %llu]",
1091496245caSQu Wenruo btrfs_inode_generation(leaf, iitem),
1092496245caSQu Wenruo super_gen + 1);
1093496245caSQu Wenruo return -EUCLEAN;
1094496245caSQu Wenruo }
1095496245caSQu Wenruo /* Note for ROOT_TREE_DIR_ITEM, mkfs could set its transid 0 */
1096c7c01a4aSDavid Sterba if (unlikely(btrfs_inode_transid(leaf, iitem) > super_gen + 1)) {
1097c3053ebbSQu Wenruo inode_item_err(leaf, slot,
1098f96d6960SQu Wenruo "invalid inode transid: has %llu expect [0, %llu]",
1099496245caSQu Wenruo btrfs_inode_transid(leaf, iitem), super_gen + 1);
1100496245caSQu Wenruo return -EUCLEAN;
1101496245caSQu Wenruo }
1102496245caSQu Wenruo
1103496245caSQu Wenruo /*
1104496245caSQu Wenruo * For size and nbytes it's better not to be too strict, as for dir
1105496245caSQu Wenruo * item its size/nbytes can easily get wrong, but doesn't affect
1106496245caSQu Wenruo * anything in the fs. So here we skip the check.
1107496245caSQu Wenruo */
1108496245caSQu Wenruo mode = btrfs_inode_mode(leaf, iitem);
1109c7c01a4aSDavid Sterba if (unlikely(mode & ~valid_mask)) {
1110c3053ebbSQu Wenruo inode_item_err(leaf, slot,
1111496245caSQu Wenruo "unknown mode bit detected: 0x%x",
1112496245caSQu Wenruo mode & ~valid_mask);
1113496245caSQu Wenruo return -EUCLEAN;
1114496245caSQu Wenruo }
1115496245caSQu Wenruo
1116496245caSQu Wenruo /*
1117c1499166SDavid Sterba * S_IFMT is not bit mapped so we can't completely rely on
1118c1499166SDavid Sterba * is_power_of_2/has_single_bit_set, but it can save us from checking
1119c1499166SDavid Sterba * FIFO/CHR/DIR/REG. Only needs to check BLK, LNK and SOCKS
1120496245caSQu Wenruo */
1121c1499166SDavid Sterba if (!has_single_bit_set(mode & S_IFMT)) {
1122c7c01a4aSDavid Sterba if (unlikely(!S_ISLNK(mode) && !S_ISBLK(mode) && !S_ISSOCK(mode))) {
1123c3053ebbSQu Wenruo inode_item_err(leaf, slot,
1124496245caSQu Wenruo "invalid mode: has 0%o expect valid S_IF* bit(s)",
1125496245caSQu Wenruo mode & S_IFMT);
1126496245caSQu Wenruo return -EUCLEAN;
1127496245caSQu Wenruo }
1128496245caSQu Wenruo }
1129c7c01a4aSDavid Sterba if (unlikely(S_ISDIR(mode) && btrfs_inode_nlink(leaf, iitem) > 1)) {
1130c3053ebbSQu Wenruo inode_item_err(leaf, slot,
1131496245caSQu Wenruo "invalid nlink: has %u expect no more than 1 for dir",
1132496245caSQu Wenruo btrfs_inode_nlink(leaf, iitem));
1133496245caSQu Wenruo return -EUCLEAN;
1134496245caSQu Wenruo }
113577eea05eSBoris Burkov btrfs_inode_split_flags(btrfs_inode_flags(leaf, iitem), &flags, &ro_flags);
113677eea05eSBoris Burkov if (unlikely(flags & ~BTRFS_INODE_FLAG_MASK)) {
1137c3053ebbSQu Wenruo inode_item_err(leaf, slot,
113877eea05eSBoris Burkov "unknown incompat flags detected: 0x%x", flags);
113977eea05eSBoris Burkov return -EUCLEAN;
114077eea05eSBoris Burkov }
114177eea05eSBoris Burkov if (unlikely(!sb_rdonly(fs_info->sb) &&
114277eea05eSBoris Burkov (ro_flags & ~BTRFS_INODE_RO_FLAG_MASK))) {
114377eea05eSBoris Burkov inode_item_err(leaf, slot,
114477eea05eSBoris Burkov "unknown ro-compat flags detected on writeable mount: 0x%x",
114577eea05eSBoris Burkov ro_flags);
1146496245caSQu Wenruo return -EUCLEAN;
1147496245caSQu Wenruo }
1148496245caSQu Wenruo return 0;
1149496245caSQu Wenruo }
1150496245caSQu Wenruo
check_root_item(struct extent_buffer * leaf,struct btrfs_key * key,int slot)1151259ee775SQu Wenruo static int check_root_item(struct extent_buffer *leaf, struct btrfs_key *key,
1152259ee775SQu Wenruo int slot)
1153259ee775SQu Wenruo {
1154259ee775SQu Wenruo struct btrfs_fs_info *fs_info = leaf->fs_info;
11551465af12SQu Wenruo struct btrfs_root_item ri = { 0 };
1156259ee775SQu Wenruo const u64 valid_root_flags = BTRFS_ROOT_SUBVOL_RDONLY |
1157259ee775SQu Wenruo BTRFS_ROOT_SUBVOL_DEAD;
115857a0e674SQu Wenruo int ret;
1159259ee775SQu Wenruo
116057a0e674SQu Wenruo ret = check_root_key(leaf, key, slot);
1161c7c01a4aSDavid Sterba if (unlikely(ret < 0))
116257a0e674SQu Wenruo return ret;
1163259ee775SQu Wenruo
11643212fa14SJosef Bacik if (unlikely(btrfs_item_size(leaf, slot) != sizeof(ri) &&
11653212fa14SJosef Bacik btrfs_item_size(leaf, slot) !=
1166c7c01a4aSDavid Sterba btrfs_legacy_root_item_size())) {
1167259ee775SQu Wenruo generic_err(leaf, slot,
11681465af12SQu Wenruo "invalid root item size, have %u expect %zu or %u",
11693212fa14SJosef Bacik btrfs_item_size(leaf, slot), sizeof(ri),
11701465af12SQu Wenruo btrfs_legacy_root_item_size());
11711a49a97dSDaniel Xu return -EUCLEAN;
1172259ee775SQu Wenruo }
1173259ee775SQu Wenruo
11741465af12SQu Wenruo /*
11751465af12SQu Wenruo * For legacy root item, the members starting at generation_v2 will be
11761465af12SQu Wenruo * all filled with 0.
11771465af12SQu Wenruo * And since we allow geneartion_v2 as 0, it will still pass the check.
11781465af12SQu Wenruo */
1179259ee775SQu Wenruo read_extent_buffer(leaf, &ri, btrfs_item_ptr_offset(leaf, slot),
11803212fa14SJosef Bacik btrfs_item_size(leaf, slot));
1181259ee775SQu Wenruo
1182259ee775SQu Wenruo /* Generation related */
1183c7c01a4aSDavid Sterba if (unlikely(btrfs_root_generation(&ri) >
1184c7c01a4aSDavid Sterba btrfs_super_generation(fs_info->super_copy) + 1)) {
1185259ee775SQu Wenruo generic_err(leaf, slot,
1186259ee775SQu Wenruo "invalid root generation, have %llu expect (0, %llu]",
1187259ee775SQu Wenruo btrfs_root_generation(&ri),
1188259ee775SQu Wenruo btrfs_super_generation(fs_info->super_copy) + 1);
1189259ee775SQu Wenruo return -EUCLEAN;
1190259ee775SQu Wenruo }
1191c7c01a4aSDavid Sterba if (unlikely(btrfs_root_generation_v2(&ri) >
1192c7c01a4aSDavid Sterba btrfs_super_generation(fs_info->super_copy) + 1)) {
1193259ee775SQu Wenruo generic_err(leaf, slot,
1194259ee775SQu Wenruo "invalid root v2 generation, have %llu expect (0, %llu]",
1195259ee775SQu Wenruo btrfs_root_generation_v2(&ri),
1196259ee775SQu Wenruo btrfs_super_generation(fs_info->super_copy) + 1);
1197259ee775SQu Wenruo return -EUCLEAN;
1198259ee775SQu Wenruo }
1199c7c01a4aSDavid Sterba if (unlikely(btrfs_root_last_snapshot(&ri) >
1200c7c01a4aSDavid Sterba btrfs_super_generation(fs_info->super_copy) + 1)) {
1201259ee775SQu Wenruo generic_err(leaf, slot,
1202259ee775SQu Wenruo "invalid root last_snapshot, have %llu expect (0, %llu]",
1203259ee775SQu Wenruo btrfs_root_last_snapshot(&ri),
1204259ee775SQu Wenruo btrfs_super_generation(fs_info->super_copy) + 1);
1205259ee775SQu Wenruo return -EUCLEAN;
1206259ee775SQu Wenruo }
1207259ee775SQu Wenruo
1208259ee775SQu Wenruo /* Alignment and level check */
1209c7c01a4aSDavid Sterba if (unlikely(!IS_ALIGNED(btrfs_root_bytenr(&ri), fs_info->sectorsize))) {
1210259ee775SQu Wenruo generic_err(leaf, slot,
1211259ee775SQu Wenruo "invalid root bytenr, have %llu expect to be aligned to %u",
1212259ee775SQu Wenruo btrfs_root_bytenr(&ri), fs_info->sectorsize);
1213259ee775SQu Wenruo return -EUCLEAN;
1214259ee775SQu Wenruo }
1215c7c01a4aSDavid Sterba if (unlikely(btrfs_root_level(&ri) >= BTRFS_MAX_LEVEL)) {
1216259ee775SQu Wenruo generic_err(leaf, slot,
1217259ee775SQu Wenruo "invalid root level, have %u expect [0, %u]",
1218259ee775SQu Wenruo btrfs_root_level(&ri), BTRFS_MAX_LEVEL - 1);
1219259ee775SQu Wenruo return -EUCLEAN;
1220259ee775SQu Wenruo }
1221c7c01a4aSDavid Sterba if (unlikely(btrfs_root_drop_level(&ri) >= BTRFS_MAX_LEVEL)) {
1222259ee775SQu Wenruo generic_err(leaf, slot,
1223259ee775SQu Wenruo "invalid root level, have %u expect [0, %u]",
1224c8422684SDavid Sterba btrfs_root_drop_level(&ri), BTRFS_MAX_LEVEL - 1);
1225259ee775SQu Wenruo return -EUCLEAN;
1226259ee775SQu Wenruo }
1227259ee775SQu Wenruo
1228259ee775SQu Wenruo /* Flags check */
1229c7c01a4aSDavid Sterba if (unlikely(btrfs_root_flags(&ri) & ~valid_root_flags)) {
1230259ee775SQu Wenruo generic_err(leaf, slot,
1231259ee775SQu Wenruo "invalid root flags, have 0x%llx expect mask 0x%llx",
1232259ee775SQu Wenruo btrfs_root_flags(&ri), valid_root_flags);
1233259ee775SQu Wenruo return -EUCLEAN;
1234259ee775SQu Wenruo }
1235259ee775SQu Wenruo return 0;
1236259ee775SQu Wenruo }
1237259ee775SQu Wenruo
1238f82d1c7cSQu Wenruo __printf(3,4)
1239f82d1c7cSQu Wenruo __cold
extent_err(const struct extent_buffer * eb,int slot,const char * fmt,...)1240f82d1c7cSQu Wenruo static void extent_err(const struct extent_buffer *eb, int slot,
1241f82d1c7cSQu Wenruo const char *fmt, ...)
1242f82d1c7cSQu Wenruo {
1243f82d1c7cSQu Wenruo struct btrfs_key key;
1244f82d1c7cSQu Wenruo struct va_format vaf;
1245f82d1c7cSQu Wenruo va_list args;
1246f82d1c7cSQu Wenruo u64 bytenr;
1247f82d1c7cSQu Wenruo u64 len;
1248f82d1c7cSQu Wenruo
1249f82d1c7cSQu Wenruo btrfs_item_key_to_cpu(eb, &key, slot);
1250f82d1c7cSQu Wenruo bytenr = key.objectid;
1251e2406a6fSQu Wenruo if (key.type == BTRFS_METADATA_ITEM_KEY ||
1252e2406a6fSQu Wenruo key.type == BTRFS_TREE_BLOCK_REF_KEY ||
1253e2406a6fSQu Wenruo key.type == BTRFS_SHARED_BLOCK_REF_KEY)
1254f82d1c7cSQu Wenruo len = eb->fs_info->nodesize;
1255f82d1c7cSQu Wenruo else
1256f82d1c7cSQu Wenruo len = key.offset;
1257f82d1c7cSQu Wenruo va_start(args, fmt);
1258f82d1c7cSQu Wenruo
1259f82d1c7cSQu Wenruo vaf.fmt = fmt;
1260f82d1c7cSQu Wenruo vaf.va = &args;
1261f82d1c7cSQu Wenruo
1262f82d1c7cSQu Wenruo btrfs_crit(eb->fs_info,
1263f82d1c7cSQu Wenruo "corrupt %s: block=%llu slot=%d extent bytenr=%llu len=%llu %pV",
1264f82d1c7cSQu Wenruo btrfs_header_level(eb) == 0 ? "leaf" : "node",
1265f82d1c7cSQu Wenruo eb->start, slot, bytenr, len, &vaf);
1266f82d1c7cSQu Wenruo va_end(args);
1267f82d1c7cSQu Wenruo }
1268f82d1c7cSQu Wenruo
is_valid_dref_root(u64 rootid)12699b090ccdSQu Wenruo static bool is_valid_dref_root(u64 rootid)
12709b090ccdSQu Wenruo {
12719b090ccdSQu Wenruo /*
12729b090ccdSQu Wenruo * The following tree root objectids are allowed to have a data backref:
12739b090ccdSQu Wenruo * - subvolume trees
12749b090ccdSQu Wenruo * - data reloc tree
12759b090ccdSQu Wenruo * - tree root
12769b090ccdSQu Wenruo * For v1 space cache
12779b090ccdSQu Wenruo */
12789b090ccdSQu Wenruo return is_fstree(rootid) || rootid == BTRFS_DATA_RELOC_TREE_OBJECTID ||
12799b090ccdSQu Wenruo rootid == BTRFS_ROOT_TREE_OBJECTID;
12809b090ccdSQu Wenruo }
12819b090ccdSQu Wenruo
check_extent_item(struct extent_buffer * leaf,struct btrfs_key * key,int slot,struct btrfs_key * prev_key)1282f82d1c7cSQu Wenruo static int check_extent_item(struct extent_buffer *leaf,
1283899b7f69SJosef Bacik struct btrfs_key *key, int slot,
1284899b7f69SJosef Bacik struct btrfs_key *prev_key)
1285f82d1c7cSQu Wenruo {
1286f82d1c7cSQu Wenruo struct btrfs_fs_info *fs_info = leaf->fs_info;
1287f82d1c7cSQu Wenruo struct btrfs_extent_item *ei;
1288f82d1c7cSQu Wenruo bool is_tree_block = false;
1289f82d1c7cSQu Wenruo unsigned long ptr; /* Current pointer inside inline refs */
1290f82d1c7cSQu Wenruo unsigned long end; /* Extent item end */
12913212fa14SJosef Bacik const u32 item_size = btrfs_item_size(leaf, slot);
1292c1bf973fSQu Wenruo u8 last_type = 0;
1293c1bf973fSQu Wenruo u64 last_seq = U64_MAX;
1294f82d1c7cSQu Wenruo u64 flags;
1295f82d1c7cSQu Wenruo u64 generation;
1296f82d1c7cSQu Wenruo u64 total_refs; /* Total refs in btrfs_extent_item */
1297f82d1c7cSQu Wenruo u64 inline_refs = 0; /* found total inline refs */
1298f82d1c7cSQu Wenruo
1299c7c01a4aSDavid Sterba if (unlikely(key->type == BTRFS_METADATA_ITEM_KEY &&
1300c7c01a4aSDavid Sterba !btrfs_fs_incompat(fs_info, SKINNY_METADATA))) {
1301f82d1c7cSQu Wenruo generic_err(leaf, slot,
1302f82d1c7cSQu Wenruo "invalid key type, METADATA_ITEM type invalid when SKINNY_METADATA feature disabled");
1303f82d1c7cSQu Wenruo return -EUCLEAN;
1304f82d1c7cSQu Wenruo }
1305f82d1c7cSQu Wenruo /* key->objectid is the bytenr for both key types */
1306c7c01a4aSDavid Sterba if (unlikely(!IS_ALIGNED(key->objectid, fs_info->sectorsize))) {
1307f82d1c7cSQu Wenruo generic_err(leaf, slot,
1308f82d1c7cSQu Wenruo "invalid key objectid, have %llu expect to be aligned to %u",
1309f82d1c7cSQu Wenruo key->objectid, fs_info->sectorsize);
1310f82d1c7cSQu Wenruo return -EUCLEAN;
1311f82d1c7cSQu Wenruo }
1312f82d1c7cSQu Wenruo
1313f82d1c7cSQu Wenruo /* key->offset is tree level for METADATA_ITEM_KEY */
1314c7c01a4aSDavid Sterba if (unlikely(key->type == BTRFS_METADATA_ITEM_KEY &&
1315c7c01a4aSDavid Sterba key->offset >= BTRFS_MAX_LEVEL)) {
1316f82d1c7cSQu Wenruo extent_err(leaf, slot,
1317f82d1c7cSQu Wenruo "invalid tree level, have %llu expect [0, %u]",
1318f82d1c7cSQu Wenruo key->offset, BTRFS_MAX_LEVEL - 1);
1319f82d1c7cSQu Wenruo return -EUCLEAN;
1320f82d1c7cSQu Wenruo }
1321f82d1c7cSQu Wenruo
1322f82d1c7cSQu Wenruo /*
1323f82d1c7cSQu Wenruo * EXTENT/METADATA_ITEM consists of:
1324f82d1c7cSQu Wenruo * 1) One btrfs_extent_item
1325f82d1c7cSQu Wenruo * Records the total refs, type and generation of the extent.
1326f82d1c7cSQu Wenruo *
1327f82d1c7cSQu Wenruo * 2) One btrfs_tree_block_info (for EXTENT_ITEM and tree backref only)
1328f82d1c7cSQu Wenruo * Records the first key and level of the tree block.
1329f82d1c7cSQu Wenruo *
1330f82d1c7cSQu Wenruo * 2) Zero or more btrfs_extent_inline_ref(s)
1331f82d1c7cSQu Wenruo * Each inline ref has one btrfs_extent_inline_ref shows:
1332f82d1c7cSQu Wenruo * 2.1) The ref type, one of the 4
1333f82d1c7cSQu Wenruo * TREE_BLOCK_REF Tree block only
1334f82d1c7cSQu Wenruo * SHARED_BLOCK_REF Tree block only
1335f82d1c7cSQu Wenruo * EXTENT_DATA_REF Data only
1336f82d1c7cSQu Wenruo * SHARED_DATA_REF Data only
1337f82d1c7cSQu Wenruo * 2.2) Ref type specific data
1338f82d1c7cSQu Wenruo * Either using btrfs_extent_inline_ref::offset, or specific
1339f82d1c7cSQu Wenruo * data structure.
1340c1bf973fSQu Wenruo *
1341c1bf973fSQu Wenruo * All above inline items should follow the order:
1342c1bf973fSQu Wenruo *
1343c1bf973fSQu Wenruo * - All btrfs_extent_inline_ref::type should be in an ascending
1344c1bf973fSQu Wenruo * order
1345c1bf973fSQu Wenruo *
1346c1bf973fSQu Wenruo * - Within the same type, the items should follow a descending
1347c1bf973fSQu Wenruo * order by their sequence number. The sequence number is
1348c1bf973fSQu Wenruo * determined by:
1349c1bf973fSQu Wenruo * * btrfs_extent_inline_ref::offset for all types other than
1350c1bf973fSQu Wenruo * EXTENT_DATA_REF
1351c1bf973fSQu Wenruo * * hash_extent_data_ref() for EXTENT_DATA_REF
1352f82d1c7cSQu Wenruo */
1353c7c01a4aSDavid Sterba if (unlikely(item_size < sizeof(*ei))) {
1354f82d1c7cSQu Wenruo extent_err(leaf, slot,
1355f82d1c7cSQu Wenruo "invalid item size, have %u expect [%zu, %u)",
1356f82d1c7cSQu Wenruo item_size, sizeof(*ei),
1357f82d1c7cSQu Wenruo BTRFS_LEAF_DATA_SIZE(fs_info));
1358f82d1c7cSQu Wenruo return -EUCLEAN;
1359f82d1c7cSQu Wenruo }
1360f82d1c7cSQu Wenruo end = item_size + btrfs_item_ptr_offset(leaf, slot);
1361f82d1c7cSQu Wenruo
1362f82d1c7cSQu Wenruo /* Checks against extent_item */
1363f82d1c7cSQu Wenruo ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
1364f82d1c7cSQu Wenruo flags = btrfs_extent_flags(leaf, ei);
1365f82d1c7cSQu Wenruo total_refs = btrfs_extent_refs(leaf, ei);
1366f82d1c7cSQu Wenruo generation = btrfs_extent_generation(leaf, ei);
1367c7c01a4aSDavid Sterba if (unlikely(generation >
1368c7c01a4aSDavid Sterba btrfs_super_generation(fs_info->super_copy) + 1)) {
1369f82d1c7cSQu Wenruo extent_err(leaf, slot,
1370f82d1c7cSQu Wenruo "invalid generation, have %llu expect (0, %llu]",
1371f82d1c7cSQu Wenruo generation,
1372f82d1c7cSQu Wenruo btrfs_super_generation(fs_info->super_copy) + 1);
1373f82d1c7cSQu Wenruo return -EUCLEAN;
1374f82d1c7cSQu Wenruo }
1375c7c01a4aSDavid Sterba if (unlikely(!has_single_bit_set(flags & (BTRFS_EXTENT_FLAG_DATA |
1376c7c01a4aSDavid Sterba BTRFS_EXTENT_FLAG_TREE_BLOCK)))) {
1377f82d1c7cSQu Wenruo extent_err(leaf, slot,
1378f82d1c7cSQu Wenruo "invalid extent flag, have 0x%llx expect 1 bit set in 0x%llx",
1379f82d1c7cSQu Wenruo flags, BTRFS_EXTENT_FLAG_DATA |
1380f82d1c7cSQu Wenruo BTRFS_EXTENT_FLAG_TREE_BLOCK);
1381f82d1c7cSQu Wenruo return -EUCLEAN;
1382f82d1c7cSQu Wenruo }
1383f82d1c7cSQu Wenruo is_tree_block = !!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK);
1384f82d1c7cSQu Wenruo if (is_tree_block) {
1385c7c01a4aSDavid Sterba if (unlikely(key->type == BTRFS_EXTENT_ITEM_KEY &&
1386c7c01a4aSDavid Sterba key->offset != fs_info->nodesize)) {
1387f82d1c7cSQu Wenruo extent_err(leaf, slot,
1388f82d1c7cSQu Wenruo "invalid extent length, have %llu expect %u",
1389f82d1c7cSQu Wenruo key->offset, fs_info->nodesize);
1390f82d1c7cSQu Wenruo return -EUCLEAN;
1391f82d1c7cSQu Wenruo }
1392f82d1c7cSQu Wenruo } else {
1393c7c01a4aSDavid Sterba if (unlikely(key->type != BTRFS_EXTENT_ITEM_KEY)) {
1394f82d1c7cSQu Wenruo extent_err(leaf, slot,
1395f82d1c7cSQu Wenruo "invalid key type, have %u expect %u for data backref",
1396f82d1c7cSQu Wenruo key->type, BTRFS_EXTENT_ITEM_KEY);
1397f82d1c7cSQu Wenruo return -EUCLEAN;
1398f82d1c7cSQu Wenruo }
1399c7c01a4aSDavid Sterba if (unlikely(!IS_ALIGNED(key->offset, fs_info->sectorsize))) {
1400f82d1c7cSQu Wenruo extent_err(leaf, slot,
1401f82d1c7cSQu Wenruo "invalid extent length, have %llu expect aligned to %u",
1402f82d1c7cSQu Wenruo key->offset, fs_info->sectorsize);
1403f82d1c7cSQu Wenruo return -EUCLEAN;
1404f82d1c7cSQu Wenruo }
14050ebb6bbbSJosef Bacik if (unlikely(flags & BTRFS_BLOCK_FLAG_FULL_BACKREF)) {
14060ebb6bbbSJosef Bacik extent_err(leaf, slot,
14070ebb6bbbSJosef Bacik "invalid extent flag, data has full backref set");
14080ebb6bbbSJosef Bacik return -EUCLEAN;
14090ebb6bbbSJosef Bacik }
1410f82d1c7cSQu Wenruo }
1411f82d1c7cSQu Wenruo ptr = (unsigned long)(struct btrfs_extent_item *)(ei + 1);
1412f82d1c7cSQu Wenruo
1413f82d1c7cSQu Wenruo /* Check the special case of btrfs_tree_block_info */
1414f82d1c7cSQu Wenruo if (is_tree_block && key->type != BTRFS_METADATA_ITEM_KEY) {
1415f82d1c7cSQu Wenruo struct btrfs_tree_block_info *info;
1416f82d1c7cSQu Wenruo
1417f82d1c7cSQu Wenruo info = (struct btrfs_tree_block_info *)ptr;
1418c7c01a4aSDavid Sterba if (unlikely(btrfs_tree_block_level(leaf, info) >= BTRFS_MAX_LEVEL)) {
1419f82d1c7cSQu Wenruo extent_err(leaf, slot,
1420f82d1c7cSQu Wenruo "invalid tree block info level, have %u expect [0, %u]",
1421f82d1c7cSQu Wenruo btrfs_tree_block_level(leaf, info),
1422f82d1c7cSQu Wenruo BTRFS_MAX_LEVEL - 1);
1423f82d1c7cSQu Wenruo return -EUCLEAN;
1424f82d1c7cSQu Wenruo }
1425f82d1c7cSQu Wenruo ptr = (unsigned long)(struct btrfs_tree_block_info *)(info + 1);
1426f82d1c7cSQu Wenruo }
1427f82d1c7cSQu Wenruo
1428f82d1c7cSQu Wenruo /* Check inline refs */
1429f82d1c7cSQu Wenruo while (ptr < end) {
1430f82d1c7cSQu Wenruo struct btrfs_extent_inline_ref *iref;
1431f82d1c7cSQu Wenruo struct btrfs_extent_data_ref *dref;
1432f82d1c7cSQu Wenruo struct btrfs_shared_data_ref *sref;
1433c1bf973fSQu Wenruo u64 seq;
14349b090ccdSQu Wenruo u64 dref_root;
14359b090ccdSQu Wenruo u64 dref_objectid;
1436f82d1c7cSQu Wenruo u64 dref_offset;
1437f82d1c7cSQu Wenruo u64 inline_offset;
1438f82d1c7cSQu Wenruo u8 inline_type;
1439f82d1c7cSQu Wenruo
1440c7c01a4aSDavid Sterba if (unlikely(ptr + sizeof(*iref) > end)) {
1441f82d1c7cSQu Wenruo extent_err(leaf, slot,
1442f82d1c7cSQu Wenruo "inline ref item overflows extent item, ptr %lu iref size %zu end %lu",
1443f82d1c7cSQu Wenruo ptr, sizeof(*iref), end);
1444f82d1c7cSQu Wenruo return -EUCLEAN;
1445f82d1c7cSQu Wenruo }
1446f82d1c7cSQu Wenruo iref = (struct btrfs_extent_inline_ref *)ptr;
1447f82d1c7cSQu Wenruo inline_type = btrfs_extent_inline_ref_type(leaf, iref);
1448f82d1c7cSQu Wenruo inline_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1449c1bf973fSQu Wenruo seq = inline_offset;
1450c7c01a4aSDavid Sterba if (unlikely(ptr + btrfs_extent_inline_ref_size(inline_type) > end)) {
1451f82d1c7cSQu Wenruo extent_err(leaf, slot,
1452f82d1c7cSQu Wenruo "inline ref item overflows extent item, ptr %lu iref size %u end %lu",
1453ad854a86SChung-Chiang Cheng ptr, btrfs_extent_inline_ref_size(inline_type), end);
1454f82d1c7cSQu Wenruo return -EUCLEAN;
1455f82d1c7cSQu Wenruo }
1456f82d1c7cSQu Wenruo
1457f82d1c7cSQu Wenruo switch (inline_type) {
1458f82d1c7cSQu Wenruo /* inline_offset is subvolid of the owner, no need to check */
1459f82d1c7cSQu Wenruo case BTRFS_TREE_BLOCK_REF_KEY:
1460f82d1c7cSQu Wenruo inline_refs++;
1461f82d1c7cSQu Wenruo break;
1462f82d1c7cSQu Wenruo /* Contains parent bytenr */
1463f82d1c7cSQu Wenruo case BTRFS_SHARED_BLOCK_REF_KEY:
1464c7c01a4aSDavid Sterba if (unlikely(!IS_ALIGNED(inline_offset,
1465c7c01a4aSDavid Sterba fs_info->sectorsize))) {
1466f82d1c7cSQu Wenruo extent_err(leaf, slot,
1467f82d1c7cSQu Wenruo "invalid tree parent bytenr, have %llu expect aligned to %u",
1468f82d1c7cSQu Wenruo inline_offset, fs_info->sectorsize);
1469f82d1c7cSQu Wenruo return -EUCLEAN;
1470f82d1c7cSQu Wenruo }
1471f82d1c7cSQu Wenruo inline_refs++;
1472f82d1c7cSQu Wenruo break;
1473f82d1c7cSQu Wenruo /*
1474f82d1c7cSQu Wenruo * Contains owner subvolid, owner key objectid, adjusted offset.
1475f82d1c7cSQu Wenruo * The only obvious corruption can happen in that offset.
1476f82d1c7cSQu Wenruo */
1477f82d1c7cSQu Wenruo case BTRFS_EXTENT_DATA_REF_KEY:
1478f82d1c7cSQu Wenruo dref = (struct btrfs_extent_data_ref *)(&iref->offset);
14799b090ccdSQu Wenruo dref_root = btrfs_extent_data_ref_root(leaf, dref);
14809b090ccdSQu Wenruo dref_objectid = btrfs_extent_data_ref_objectid(leaf, dref);
1481f82d1c7cSQu Wenruo dref_offset = btrfs_extent_data_ref_offset(leaf, dref);
1482c1bf973fSQu Wenruo seq = hash_extent_data_ref(
1483c1bf973fSQu Wenruo btrfs_extent_data_ref_root(leaf, dref),
1484c1bf973fSQu Wenruo btrfs_extent_data_ref_objectid(leaf, dref),
1485c1bf973fSQu Wenruo btrfs_extent_data_ref_offset(leaf, dref));
14869b090ccdSQu Wenruo if (unlikely(!is_valid_dref_root(dref_root))) {
14879b090ccdSQu Wenruo extent_err(leaf, slot,
14889b090ccdSQu Wenruo "invalid data ref root value %llu",
14899b090ccdSQu Wenruo dref_root);
14909b090ccdSQu Wenruo return -EUCLEAN;
14919b090ccdSQu Wenruo }
14929b090ccdSQu Wenruo if (unlikely(dref_objectid < BTRFS_FIRST_FREE_OBJECTID ||
14939b090ccdSQu Wenruo dref_objectid > BTRFS_LAST_FREE_OBJECTID)) {
14949b090ccdSQu Wenruo extent_err(leaf, slot,
14959b090ccdSQu Wenruo "invalid data ref objectid value %llu",
14966a6a5751SQu Wenruo dref_objectid);
14979b090ccdSQu Wenruo return -EUCLEAN;
14989b090ccdSQu Wenruo }
1499c7c01a4aSDavid Sterba if (unlikely(!IS_ALIGNED(dref_offset,
1500c7c01a4aSDavid Sterba fs_info->sectorsize))) {
1501f82d1c7cSQu Wenruo extent_err(leaf, slot,
1502f82d1c7cSQu Wenruo "invalid data ref offset, have %llu expect aligned to %u",
1503f82d1c7cSQu Wenruo dref_offset, fs_info->sectorsize);
1504f82d1c7cSQu Wenruo return -EUCLEAN;
1505f82d1c7cSQu Wenruo }
1506*a41ee016SQu Wenruo if (unlikely(btrfs_extent_data_ref_count(leaf, dref) == 0)) {
1507*a41ee016SQu Wenruo extent_err(leaf, slot,
1508*a41ee016SQu Wenruo "invalid data ref count, should have non-zero value");
1509*a41ee016SQu Wenruo return -EUCLEAN;
1510*a41ee016SQu Wenruo }
1511f82d1c7cSQu Wenruo inline_refs += btrfs_extent_data_ref_count(leaf, dref);
1512f82d1c7cSQu Wenruo break;
1513f82d1c7cSQu Wenruo /* Contains parent bytenr and ref count */
1514f82d1c7cSQu Wenruo case BTRFS_SHARED_DATA_REF_KEY:
1515f82d1c7cSQu Wenruo sref = (struct btrfs_shared_data_ref *)(iref + 1);
1516c7c01a4aSDavid Sterba if (unlikely(!IS_ALIGNED(inline_offset,
1517c7c01a4aSDavid Sterba fs_info->sectorsize))) {
1518f82d1c7cSQu Wenruo extent_err(leaf, slot,
1519f82d1c7cSQu Wenruo "invalid data parent bytenr, have %llu expect aligned to %u",
1520f82d1c7cSQu Wenruo inline_offset, fs_info->sectorsize);
1521f82d1c7cSQu Wenruo return -EUCLEAN;
1522f82d1c7cSQu Wenruo }
1523*a41ee016SQu Wenruo if (unlikely(btrfs_shared_data_ref_count(leaf, sref) == 0)) {
1524*a41ee016SQu Wenruo extent_err(leaf, slot,
1525*a41ee016SQu Wenruo "invalid shared data ref count, should have non-zero value");
1526*a41ee016SQu Wenruo return -EUCLEAN;
1527*a41ee016SQu Wenruo }
1528f82d1c7cSQu Wenruo inline_refs += btrfs_shared_data_ref_count(leaf, sref);
1529f82d1c7cSQu Wenruo break;
1530f82d1c7cSQu Wenruo default:
1531f82d1c7cSQu Wenruo extent_err(leaf, slot, "unknown inline ref type: %u",
1532f82d1c7cSQu Wenruo inline_type);
1533f82d1c7cSQu Wenruo return -EUCLEAN;
1534f82d1c7cSQu Wenruo }
1535c1bf973fSQu Wenruo if (inline_type < last_type) {
1536c1bf973fSQu Wenruo extent_err(leaf, slot,
1537c1bf973fSQu Wenruo "inline ref out-of-order: has type %u, prev type %u",
1538c1bf973fSQu Wenruo inline_type, last_type);
1539c1bf973fSQu Wenruo return -EUCLEAN;
1540c1bf973fSQu Wenruo }
1541c1bf973fSQu Wenruo /* Type changed, allow the sequence starts from U64_MAX again. */
1542c1bf973fSQu Wenruo if (inline_type > last_type)
1543c1bf973fSQu Wenruo last_seq = U64_MAX;
1544c1bf973fSQu Wenruo if (seq > last_seq) {
1545c1bf973fSQu Wenruo extent_err(leaf, slot,
1546c1bf973fSQu Wenruo "inline ref out-of-order: has type %u offset %llu seq 0x%llx, prev type %u seq 0x%llx",
1547c1bf973fSQu Wenruo inline_type, inline_offset, seq,
1548c1bf973fSQu Wenruo last_type, last_seq);
1549c1bf973fSQu Wenruo return -EUCLEAN;
1550c1bf973fSQu Wenruo }
1551c1bf973fSQu Wenruo last_type = inline_type;
1552c1bf973fSQu Wenruo last_seq = seq;
1553f82d1c7cSQu Wenruo ptr += btrfs_extent_inline_ref_size(inline_type);
1554f82d1c7cSQu Wenruo }
1555f82d1c7cSQu Wenruo /* No padding is allowed */
1556c7c01a4aSDavid Sterba if (unlikely(ptr != end)) {
1557f82d1c7cSQu Wenruo extent_err(leaf, slot,
1558f82d1c7cSQu Wenruo "invalid extent item size, padding bytes found");
1559f82d1c7cSQu Wenruo return -EUCLEAN;
1560f82d1c7cSQu Wenruo }
1561f82d1c7cSQu Wenruo
1562f82d1c7cSQu Wenruo /* Finally, check the inline refs against total refs */
1563c7c01a4aSDavid Sterba if (unlikely(inline_refs > total_refs)) {
1564f82d1c7cSQu Wenruo extent_err(leaf, slot,
1565f82d1c7cSQu Wenruo "invalid extent refs, have %llu expect >= inline %llu",
1566f82d1c7cSQu Wenruo total_refs, inline_refs);
1567f82d1c7cSQu Wenruo return -EUCLEAN;
1568f82d1c7cSQu Wenruo }
1569899b7f69SJosef Bacik
1570899b7f69SJosef Bacik if ((prev_key->type == BTRFS_EXTENT_ITEM_KEY) ||
1571899b7f69SJosef Bacik (prev_key->type == BTRFS_METADATA_ITEM_KEY)) {
1572899b7f69SJosef Bacik u64 prev_end = prev_key->objectid;
1573899b7f69SJosef Bacik
1574899b7f69SJosef Bacik if (prev_key->type == BTRFS_METADATA_ITEM_KEY)
1575899b7f69SJosef Bacik prev_end += fs_info->nodesize;
1576899b7f69SJosef Bacik else
1577899b7f69SJosef Bacik prev_end += prev_key->offset;
1578899b7f69SJosef Bacik
1579899b7f69SJosef Bacik if (unlikely(prev_end > key->objectid)) {
1580899b7f69SJosef Bacik extent_err(leaf, slot,
1581899b7f69SJosef Bacik "previous extent [%llu %u %llu] overlaps current extent [%llu %u %llu]",
1582899b7f69SJosef Bacik prev_key->objectid, prev_key->type,
1583899b7f69SJosef Bacik prev_key->offset, key->objectid, key->type,
1584899b7f69SJosef Bacik key->offset);
1585899b7f69SJosef Bacik return -EUCLEAN;
1586899b7f69SJosef Bacik }
1587899b7f69SJosef Bacik }
1588899b7f69SJosef Bacik
1589f82d1c7cSQu Wenruo return 0;
1590f82d1c7cSQu Wenruo }
1591f82d1c7cSQu Wenruo
check_simple_keyed_refs(struct extent_buffer * leaf,struct btrfs_key * key,int slot)1592e2406a6fSQu Wenruo static int check_simple_keyed_refs(struct extent_buffer *leaf,
1593e2406a6fSQu Wenruo struct btrfs_key *key, int slot)
1594e2406a6fSQu Wenruo {
1595e2406a6fSQu Wenruo u32 expect_item_size = 0;
1596e2406a6fSQu Wenruo
1597*a41ee016SQu Wenruo if (key->type == BTRFS_SHARED_DATA_REF_KEY) {
1598*a41ee016SQu Wenruo struct btrfs_shared_data_ref *sref;
1599*a41ee016SQu Wenruo
1600*a41ee016SQu Wenruo sref = btrfs_item_ptr(leaf, slot, struct btrfs_shared_data_ref);
1601*a41ee016SQu Wenruo if (unlikely(btrfs_shared_data_ref_count(leaf, sref) == 0)) {
1602*a41ee016SQu Wenruo extent_err(leaf, slot,
1603*a41ee016SQu Wenruo "invalid shared data backref count, should have non-zero value");
1604*a41ee016SQu Wenruo return -EUCLEAN;
1605*a41ee016SQu Wenruo }
1606*a41ee016SQu Wenruo
1607e2406a6fSQu Wenruo expect_item_size = sizeof(struct btrfs_shared_data_ref);
1608*a41ee016SQu Wenruo }
1609e2406a6fSQu Wenruo
16103212fa14SJosef Bacik if (unlikely(btrfs_item_size(leaf, slot) != expect_item_size)) {
1611e2406a6fSQu Wenruo generic_err(leaf, slot,
1612e2406a6fSQu Wenruo "invalid item size, have %u expect %u for key type %u",
16133212fa14SJosef Bacik btrfs_item_size(leaf, slot),
1614e2406a6fSQu Wenruo expect_item_size, key->type);
1615e2406a6fSQu Wenruo return -EUCLEAN;
1616e2406a6fSQu Wenruo }
1617c7c01a4aSDavid Sterba if (unlikely(!IS_ALIGNED(key->objectid, leaf->fs_info->sectorsize))) {
1618e2406a6fSQu Wenruo generic_err(leaf, slot,
1619e2406a6fSQu Wenruo "invalid key objectid for shared block ref, have %llu expect aligned to %u",
1620e2406a6fSQu Wenruo key->objectid, leaf->fs_info->sectorsize);
1621e2406a6fSQu Wenruo return -EUCLEAN;
1622e2406a6fSQu Wenruo }
1623c7c01a4aSDavid Sterba if (unlikely(key->type != BTRFS_TREE_BLOCK_REF_KEY &&
1624c7c01a4aSDavid Sterba !IS_ALIGNED(key->offset, leaf->fs_info->sectorsize))) {
1625e2406a6fSQu Wenruo extent_err(leaf, slot,
1626e2406a6fSQu Wenruo "invalid tree parent bytenr, have %llu expect aligned to %u",
1627e2406a6fSQu Wenruo key->offset, leaf->fs_info->sectorsize);
1628e2406a6fSQu Wenruo return -EUCLEAN;
1629e2406a6fSQu Wenruo }
1630e2406a6fSQu Wenruo return 0;
1631e2406a6fSQu Wenruo }
1632e2406a6fSQu Wenruo
check_extent_data_ref(struct extent_buffer * leaf,struct btrfs_key * key,int slot)16330785a9aaSQu Wenruo static int check_extent_data_ref(struct extent_buffer *leaf,
16340785a9aaSQu Wenruo struct btrfs_key *key, int slot)
16350785a9aaSQu Wenruo {
16360785a9aaSQu Wenruo struct btrfs_extent_data_ref *dref;
16370785a9aaSQu Wenruo unsigned long ptr = btrfs_item_ptr_offset(leaf, slot);
16383212fa14SJosef Bacik const unsigned long end = ptr + btrfs_item_size(leaf, slot);
16390785a9aaSQu Wenruo
16403212fa14SJosef Bacik if (unlikely(btrfs_item_size(leaf, slot) % sizeof(*dref) != 0)) {
16410785a9aaSQu Wenruo generic_err(leaf, slot,
16420785a9aaSQu Wenruo "invalid item size, have %u expect aligned to %zu for key type %u",
16433212fa14SJosef Bacik btrfs_item_size(leaf, slot),
16440785a9aaSQu Wenruo sizeof(*dref), key->type);
16456d06b0adSDavid Sterba return -EUCLEAN;
16460785a9aaSQu Wenruo }
1647c7c01a4aSDavid Sterba if (unlikely(!IS_ALIGNED(key->objectid, leaf->fs_info->sectorsize))) {
16480785a9aaSQu Wenruo generic_err(leaf, slot,
16490785a9aaSQu Wenruo "invalid key objectid for shared block ref, have %llu expect aligned to %u",
16500785a9aaSQu Wenruo key->objectid, leaf->fs_info->sectorsize);
16510785a9aaSQu Wenruo return -EUCLEAN;
16520785a9aaSQu Wenruo }
16530785a9aaSQu Wenruo for (; ptr < end; ptr += sizeof(*dref)) {
16549b090ccdSQu Wenruo u64 root;
16559b090ccdSQu Wenruo u64 objectid;
16560785a9aaSQu Wenruo u64 offset;
16570785a9aaSQu Wenruo
16581119a72eSJosef Bacik /*
16591119a72eSJosef Bacik * We cannot check the extent_data_ref hash due to possible
16601119a72eSJosef Bacik * overflow from the leaf due to hash collisions.
16611119a72eSJosef Bacik */
16620785a9aaSQu Wenruo dref = (struct btrfs_extent_data_ref *)ptr;
16639b090ccdSQu Wenruo root = btrfs_extent_data_ref_root(leaf, dref);
16649b090ccdSQu Wenruo objectid = btrfs_extent_data_ref_objectid(leaf, dref);
16650785a9aaSQu Wenruo offset = btrfs_extent_data_ref_offset(leaf, dref);
16669b090ccdSQu Wenruo if (unlikely(!is_valid_dref_root(root))) {
16679b090ccdSQu Wenruo extent_err(leaf, slot,
16689b090ccdSQu Wenruo "invalid extent data backref root value %llu",
16699b090ccdSQu Wenruo root);
16709b090ccdSQu Wenruo return -EUCLEAN;
16719b090ccdSQu Wenruo }
16729b090ccdSQu Wenruo if (unlikely(objectid < BTRFS_FIRST_FREE_OBJECTID ||
16739b090ccdSQu Wenruo objectid > BTRFS_LAST_FREE_OBJECTID)) {
16749b090ccdSQu Wenruo extent_err(leaf, slot,
16759b090ccdSQu Wenruo "invalid extent data backref objectid value %llu",
16769b090ccdSQu Wenruo root);
16779b090ccdSQu Wenruo return -EUCLEAN;
16789b090ccdSQu Wenruo }
1679c7c01a4aSDavid Sterba if (unlikely(!IS_ALIGNED(offset, leaf->fs_info->sectorsize))) {
16800785a9aaSQu Wenruo extent_err(leaf, slot,
16810785a9aaSQu Wenruo "invalid extent data backref offset, have %llu expect aligned to %u",
16820785a9aaSQu Wenruo offset, leaf->fs_info->sectorsize);
16836d06b0adSDavid Sterba return -EUCLEAN;
16840785a9aaSQu Wenruo }
1685*a41ee016SQu Wenruo if (unlikely(btrfs_extent_data_ref_count(leaf, dref) == 0)) {
1686*a41ee016SQu Wenruo extent_err(leaf, slot,
1687*a41ee016SQu Wenruo "invalid extent data backref count, should have non-zero value");
1688*a41ee016SQu Wenruo return -EUCLEAN;
1689*a41ee016SQu Wenruo }
16900785a9aaSQu Wenruo }
16910785a9aaSQu Wenruo return 0;
16920785a9aaSQu Wenruo }
16930785a9aaSQu Wenruo
1694c3053ebbSQu Wenruo #define inode_ref_err(eb, slot, fmt, args...) \
1695c3053ebbSQu Wenruo 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)169671bf92a9SQu Wenruo static int check_inode_ref(struct extent_buffer *leaf,
169771bf92a9SQu Wenruo struct btrfs_key *key, struct btrfs_key *prev_key,
169871bf92a9SQu Wenruo int slot)
169971bf92a9SQu Wenruo {
170071bf92a9SQu Wenruo struct btrfs_inode_ref *iref;
170171bf92a9SQu Wenruo unsigned long ptr;
170271bf92a9SQu Wenruo unsigned long end;
170371bf92a9SQu Wenruo
1704c7c01a4aSDavid Sterba if (unlikely(!check_prev_ino(leaf, key, slot, prev_key)))
170580d7fd1eSQu Wenruo return -EUCLEAN;
170671bf92a9SQu Wenruo /* namelen can't be 0, so item_size == sizeof() is also invalid */
17073212fa14SJosef Bacik if (unlikely(btrfs_item_size(leaf, slot) <= sizeof(*iref))) {
1708c3053ebbSQu Wenruo inode_ref_err(leaf, slot,
170971bf92a9SQu Wenruo "invalid item size, have %u expect (%zu, %u)",
17103212fa14SJosef Bacik btrfs_item_size(leaf, slot),
171171bf92a9SQu Wenruo sizeof(*iref), BTRFS_LEAF_DATA_SIZE(leaf->fs_info));
171271bf92a9SQu Wenruo return -EUCLEAN;
171371bf92a9SQu Wenruo }
171471bf92a9SQu Wenruo
171571bf92a9SQu Wenruo ptr = btrfs_item_ptr_offset(leaf, slot);
17163212fa14SJosef Bacik end = ptr + btrfs_item_size(leaf, slot);
171771bf92a9SQu Wenruo while (ptr < end) {
171871bf92a9SQu Wenruo u16 namelen;
171971bf92a9SQu Wenruo
1720c7c01a4aSDavid Sterba if (unlikely(ptr + sizeof(iref) > end)) {
1721c3053ebbSQu Wenruo inode_ref_err(leaf, slot,
172271bf92a9SQu Wenruo "inode ref overflow, ptr %lu end %lu inode_ref_size %zu",
172371bf92a9SQu Wenruo ptr, end, sizeof(iref));
172471bf92a9SQu Wenruo return -EUCLEAN;
172571bf92a9SQu Wenruo }
172671bf92a9SQu Wenruo
172771bf92a9SQu Wenruo iref = (struct btrfs_inode_ref *)ptr;
172871bf92a9SQu Wenruo namelen = btrfs_inode_ref_name_len(leaf, iref);
1729c7c01a4aSDavid Sterba if (unlikely(ptr + sizeof(*iref) + namelen > end)) {
1730c3053ebbSQu Wenruo inode_ref_err(leaf, slot,
173171bf92a9SQu Wenruo "inode ref overflow, ptr %lu end %lu namelen %u",
173271bf92a9SQu Wenruo ptr, end, namelen);
173371bf92a9SQu Wenruo return -EUCLEAN;
173471bf92a9SQu Wenruo }
173571bf92a9SQu Wenruo
173671bf92a9SQu Wenruo /*
173771bf92a9SQu Wenruo * NOTE: In theory we should record all found index numbers
173871bf92a9SQu Wenruo * to find any duplicated indexes, but that will be too time
173971bf92a9SQu Wenruo * consuming for inodes with too many hard links.
174071bf92a9SQu Wenruo */
174171bf92a9SQu Wenruo ptr += sizeof(*iref) + namelen;
174271bf92a9SQu Wenruo }
174371bf92a9SQu Wenruo return 0;
174471bf92a9SQu Wenruo }
174571bf92a9SQu Wenruo
check_dev_extent_item(const struct extent_buffer * leaf,const struct btrfs_key * key,int slot,struct btrfs_key * prev_key)1746d3ba98ceSQu Wenruo static int check_dev_extent_item(const struct extent_buffer *leaf,
1747d3ba98ceSQu Wenruo const struct btrfs_key *key,
1748d3ba98ceSQu Wenruo int slot,
1749d3ba98ceSQu Wenruo struct btrfs_key *prev_key)
1750d3ba98ceSQu Wenruo {
1751d3ba98ceSQu Wenruo struct btrfs_dev_extent *de;
1752d3ba98ceSQu Wenruo const u32 sectorsize = leaf->fs_info->sectorsize;
1753d3ba98ceSQu Wenruo
1754d3ba98ceSQu Wenruo de = btrfs_item_ptr(leaf, slot, struct btrfs_dev_extent);
1755d3ba98ceSQu Wenruo /* Basic fixed member checks. */
1756d3ba98ceSQu Wenruo if (unlikely(btrfs_dev_extent_chunk_tree(leaf, de) !=
1757d3ba98ceSQu Wenruo BTRFS_CHUNK_TREE_OBJECTID)) {
1758d3ba98ceSQu Wenruo generic_err(leaf, slot,
1759d3ba98ceSQu Wenruo "invalid dev extent chunk tree id, has %llu expect %llu",
1760d3ba98ceSQu Wenruo btrfs_dev_extent_chunk_tree(leaf, de),
1761d3ba98ceSQu Wenruo BTRFS_CHUNK_TREE_OBJECTID);
1762d3ba98ceSQu Wenruo return -EUCLEAN;
1763d3ba98ceSQu Wenruo }
1764d3ba98ceSQu Wenruo if (unlikely(btrfs_dev_extent_chunk_objectid(leaf, de) !=
1765d3ba98ceSQu Wenruo BTRFS_FIRST_CHUNK_TREE_OBJECTID)) {
1766d3ba98ceSQu Wenruo generic_err(leaf, slot,
1767d3ba98ceSQu Wenruo "invalid dev extent chunk objectid, has %llu expect %llu",
1768d3ba98ceSQu Wenruo btrfs_dev_extent_chunk_objectid(leaf, de),
1769d3ba98ceSQu Wenruo BTRFS_FIRST_CHUNK_TREE_OBJECTID);
1770d3ba98ceSQu Wenruo return -EUCLEAN;
1771d3ba98ceSQu Wenruo }
1772d3ba98ceSQu Wenruo /* Alignment check. */
1773d3ba98ceSQu Wenruo if (unlikely(!IS_ALIGNED(key->offset, sectorsize))) {
1774d3ba98ceSQu Wenruo generic_err(leaf, slot,
1775d3ba98ceSQu Wenruo "invalid dev extent key.offset, has %llu not aligned to %u",
1776d3ba98ceSQu Wenruo key->offset, sectorsize);
1777d3ba98ceSQu Wenruo return -EUCLEAN;
1778d3ba98ceSQu Wenruo }
1779d3ba98ceSQu Wenruo if (unlikely(!IS_ALIGNED(btrfs_dev_extent_chunk_offset(leaf, de),
1780d3ba98ceSQu Wenruo sectorsize))) {
1781d3ba98ceSQu Wenruo generic_err(leaf, slot,
1782d3ba98ceSQu Wenruo "invalid dev extent chunk offset, has %llu not aligned to %u",
1783d3ba98ceSQu Wenruo btrfs_dev_extent_chunk_objectid(leaf, de),
1784d3ba98ceSQu Wenruo sectorsize);
1785d3ba98ceSQu Wenruo return -EUCLEAN;
1786d3ba98ceSQu Wenruo }
1787d3ba98ceSQu Wenruo if (unlikely(!IS_ALIGNED(btrfs_dev_extent_length(leaf, de),
1788d3ba98ceSQu Wenruo sectorsize))) {
1789d3ba98ceSQu Wenruo generic_err(leaf, slot,
1790d3ba98ceSQu Wenruo "invalid dev extent length, has %llu not aligned to %u",
1791d3ba98ceSQu Wenruo btrfs_dev_extent_length(leaf, de), sectorsize);
1792d3ba98ceSQu Wenruo return -EUCLEAN;
1793d3ba98ceSQu Wenruo }
1794d3ba98ceSQu Wenruo /* Overlap check with previous dev extent. */
1795d3ba98ceSQu Wenruo if (slot && prev_key->objectid == key->objectid &&
1796d3ba98ceSQu Wenruo prev_key->type == key->type) {
1797d3ba98ceSQu Wenruo struct btrfs_dev_extent *prev_de;
1798d3ba98ceSQu Wenruo u64 prev_len;
1799d3ba98ceSQu Wenruo
1800d3ba98ceSQu Wenruo prev_de = btrfs_item_ptr(leaf, slot - 1, struct btrfs_dev_extent);
1801d3ba98ceSQu Wenruo prev_len = btrfs_dev_extent_length(leaf, prev_de);
1802d3ba98ceSQu Wenruo if (unlikely(prev_key->offset + prev_len > key->offset)) {
1803d3ba98ceSQu Wenruo generic_err(leaf, slot,
1804d3ba98ceSQu Wenruo "dev extent overlap, prev offset %llu len %llu current offset %llu",
1805d3ba98ceSQu Wenruo prev_key->objectid, prev_len, key->offset);
1806d3ba98ceSQu Wenruo return -EUCLEAN;
1807d3ba98ceSQu Wenruo }
1808d3ba98ceSQu Wenruo }
1809d3ba98ceSQu Wenruo return 0;
1810d3ba98ceSQu Wenruo }
1811d3ba98ceSQu Wenruo
181282fc28fbSQu Wenruo /*
1813557ea5ddSQu Wenruo * Common point to switch the item-specific validation.
1814557ea5ddSQu Wenruo */
check_leaf_item(struct extent_buffer * leaf,struct btrfs_key * key,int slot,struct btrfs_key * prev_key)1815c8d54215SJosef Bacik static enum btrfs_tree_block_status check_leaf_item(struct extent_buffer *leaf,
1816c8d54215SJosef Bacik struct btrfs_key *key,
1817c8d54215SJosef Bacik int slot,
18184e9845efSFilipe Manana struct btrfs_key *prev_key)
1819557ea5ddSQu Wenruo {
1820557ea5ddSQu Wenruo int ret = 0;
1821075cb3c7SQu Wenruo struct btrfs_chunk *chunk;
1822557ea5ddSQu Wenruo
1823557ea5ddSQu Wenruo switch (key->type) {
1824557ea5ddSQu Wenruo case BTRFS_EXTENT_DATA_KEY:
18254e9845efSFilipe Manana ret = check_extent_data_item(leaf, key, slot, prev_key);
1826557ea5ddSQu Wenruo break;
1827557ea5ddSQu Wenruo case BTRFS_EXTENT_CSUM_KEY:
1828ad1d8c43SFilipe Manana ret = check_csum_item(leaf, key, slot, prev_key);
1829557ea5ddSQu Wenruo break;
1830ad7b0368SQu Wenruo case BTRFS_DIR_ITEM_KEY:
1831ad7b0368SQu Wenruo case BTRFS_DIR_INDEX_KEY:
1832ad7b0368SQu Wenruo case BTRFS_XATTR_ITEM_KEY:
1833c18679ebSQu Wenruo ret = check_dir_item(leaf, key, prev_key, slot);
1834ad7b0368SQu Wenruo break;
183571bf92a9SQu Wenruo case BTRFS_INODE_REF_KEY:
183671bf92a9SQu Wenruo ret = check_inode_ref(leaf, key, prev_key, slot);
183771bf92a9SQu Wenruo break;
1838fce466eaSQu Wenruo case BTRFS_BLOCK_GROUP_ITEM_KEY:
1839af60ce2bSDavid Sterba ret = check_block_group_item(leaf, key, slot);
1840fce466eaSQu Wenruo break;
1841075cb3c7SQu Wenruo case BTRFS_CHUNK_ITEM_KEY:
1842075cb3c7SQu Wenruo chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
1843f6d2a5c2SQu Wenruo ret = check_leaf_chunk_item(leaf, chunk, key, slot);
1844075cb3c7SQu Wenruo break;
1845ab4ba2e1SQu Wenruo case BTRFS_DEV_ITEM_KEY:
1846412a2312SDavid Sterba ret = check_dev_item(leaf, key, slot);
1847ab4ba2e1SQu Wenruo break;
1848d3ba98ceSQu Wenruo case BTRFS_DEV_EXTENT_KEY:
1849d3ba98ceSQu Wenruo ret = check_dev_extent_item(leaf, key, slot, prev_key);
1850d3ba98ceSQu Wenruo break;
1851496245caSQu Wenruo case BTRFS_INODE_ITEM_KEY:
185239e57f49SDavid Sterba ret = check_inode_item(leaf, key, slot);
1853496245caSQu Wenruo break;
1854259ee775SQu Wenruo case BTRFS_ROOT_ITEM_KEY:
1855259ee775SQu Wenruo ret = check_root_item(leaf, key, slot);
1856259ee775SQu Wenruo break;
1857f82d1c7cSQu Wenruo case BTRFS_EXTENT_ITEM_KEY:
1858f82d1c7cSQu Wenruo case BTRFS_METADATA_ITEM_KEY:
1859899b7f69SJosef Bacik ret = check_extent_item(leaf, key, slot, prev_key);
1860f82d1c7cSQu Wenruo break;
1861e2406a6fSQu Wenruo case BTRFS_TREE_BLOCK_REF_KEY:
1862e2406a6fSQu Wenruo case BTRFS_SHARED_DATA_REF_KEY:
1863e2406a6fSQu Wenruo case BTRFS_SHARED_BLOCK_REF_KEY:
1864e2406a6fSQu Wenruo ret = check_simple_keyed_refs(leaf, key, slot);
1865e2406a6fSQu Wenruo break;
18660785a9aaSQu Wenruo case BTRFS_EXTENT_DATA_REF_KEY:
18670785a9aaSQu Wenruo ret = check_extent_data_ref(leaf, key, slot);
18680785a9aaSQu Wenruo break;
1869557ea5ddSQu Wenruo }
1870c8d54215SJosef Bacik
1871c8d54215SJosef Bacik if (ret)
1872c8d54215SJosef Bacik return BTRFS_TREE_BLOCK_INVALID_ITEM;
1873c8d54215SJosef Bacik return BTRFS_TREE_BLOCK_CLEAN;
1874557ea5ddSQu Wenruo }
1875557ea5ddSQu Wenruo
__btrfs_check_leaf(struct extent_buffer * leaf)1876924452c8SJosef Bacik enum btrfs_tree_block_status __btrfs_check_leaf(struct extent_buffer *leaf)
1877557ea5ddSQu Wenruo {
1878e2ccd361SDavid Sterba struct btrfs_fs_info *fs_info = leaf->fs_info;
1879557ea5ddSQu Wenruo /* No valid key type is 0, so all key should be larger than this key */
1880557ea5ddSQu Wenruo struct btrfs_key prev_key = {0, 0, 0};
1881557ea5ddSQu Wenruo struct btrfs_key key;
1882557ea5ddSQu Wenruo u32 nritems = btrfs_header_nritems(leaf);
1883557ea5ddSQu Wenruo int slot;
1884557ea5ddSQu Wenruo
1885c7c01a4aSDavid Sterba if (unlikely(btrfs_header_level(leaf) != 0)) {
188686a6be3aSDavid Sterba generic_err(leaf, 0,
1887f556faa4SQu Wenruo "invalid level for leaf, have %d expect 0",
1888f556faa4SQu Wenruo btrfs_header_level(leaf));
1889924452c8SJosef Bacik return BTRFS_TREE_BLOCK_INVALID_LEVEL;
1890f556faa4SQu Wenruo }
1891f556faa4SQu Wenruo
1892557ea5ddSQu Wenruo /*
1893557ea5ddSQu Wenruo * Extent buffers from a relocation tree have a owner field that
1894557ea5ddSQu Wenruo * corresponds to the subvolume tree they are based on. So just from an
1895557ea5ddSQu Wenruo * extent buffer alone we can not find out what is the id of the
1896557ea5ddSQu Wenruo * corresponding subvolume tree, so we can not figure out if the extent
1897557ea5ddSQu Wenruo * buffer corresponds to the root of the relocation tree or not. So
1898557ea5ddSQu Wenruo * skip this check for relocation trees.
1899557ea5ddSQu Wenruo */
1900557ea5ddSQu Wenruo if (nritems == 0 && !btrfs_header_flag(leaf, BTRFS_HEADER_FLAG_RELOC)) {
1901ba480dd4SQu Wenruo u64 owner = btrfs_header_owner(leaf);
1902557ea5ddSQu Wenruo
1903ba480dd4SQu Wenruo /* These trees must never be empty */
1904c7c01a4aSDavid Sterba if (unlikely(owner == BTRFS_ROOT_TREE_OBJECTID ||
1905ba480dd4SQu Wenruo owner == BTRFS_CHUNK_TREE_OBJECTID ||
1906ba480dd4SQu Wenruo owner == BTRFS_DEV_TREE_OBJECTID ||
1907ba480dd4SQu Wenruo owner == BTRFS_FS_TREE_OBJECTID ||
1908c7c01a4aSDavid Sterba owner == BTRFS_DATA_RELOC_TREE_OBJECTID)) {
190986a6be3aSDavid Sterba generic_err(leaf, 0,
1910ba480dd4SQu Wenruo "invalid root, root %llu must never be empty",
1911ba480dd4SQu Wenruo owner);
1912924452c8SJosef Bacik return BTRFS_TREE_BLOCK_INVALID_NRITEMS;
1913ba480dd4SQu Wenruo }
1914c2fa821cSJosef Bacik
191562fdaa52SQu Wenruo /* Unknown tree */
1916c7c01a4aSDavid Sterba if (unlikely(owner == 0)) {
191762fdaa52SQu Wenruo generic_err(leaf, 0,
191862fdaa52SQu Wenruo "invalid owner, root 0 is not defined");
1919924452c8SJosef Bacik return BTRFS_TREE_BLOCK_INVALID_OWNER;
192062fdaa52SQu Wenruo }
1921c2fa821cSJosef Bacik
1922c2fa821cSJosef Bacik /* EXTENT_TREE_V2 can have empty extent trees. */
1923c2fa821cSJosef Bacik if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2))
1924924452c8SJosef Bacik return BTRFS_TREE_BLOCK_CLEAN;
1925c2fa821cSJosef Bacik
1926c2fa821cSJosef Bacik if (unlikely(owner == BTRFS_EXTENT_TREE_OBJECTID)) {
1927c2fa821cSJosef Bacik generic_err(leaf, 0,
1928c2fa821cSJosef Bacik "invalid root, root %llu must never be empty",
1929c2fa821cSJosef Bacik owner);
1930924452c8SJosef Bacik return BTRFS_TREE_BLOCK_INVALID_NRITEMS;
1931c2fa821cSJosef Bacik }
1932c2fa821cSJosef Bacik
1933924452c8SJosef Bacik return BTRFS_TREE_BLOCK_CLEAN;
1934557ea5ddSQu Wenruo }
1935557ea5ddSQu Wenruo
1936c7c01a4aSDavid Sterba if (unlikely(nritems == 0))
1937924452c8SJosef Bacik return BTRFS_TREE_BLOCK_CLEAN;
1938557ea5ddSQu Wenruo
1939557ea5ddSQu Wenruo /*
1940557ea5ddSQu Wenruo * Check the following things to make sure this is a good leaf, and
1941557ea5ddSQu Wenruo * leaf users won't need to bother with similar sanity checks:
1942557ea5ddSQu Wenruo *
1943557ea5ddSQu Wenruo * 1) key ordering
1944557ea5ddSQu Wenruo * 2) item offset and size
1945557ea5ddSQu Wenruo * No overlap, no hole, all inside the leaf.
1946557ea5ddSQu Wenruo * 3) item content
1947557ea5ddSQu Wenruo * If possible, do comprehensive sanity check.
1948557ea5ddSQu Wenruo * NOTE: All checks must only rely on the item data itself.
1949557ea5ddSQu Wenruo */
1950557ea5ddSQu Wenruo for (slot = 0; slot < nritems; slot++) {
1951557ea5ddSQu Wenruo u32 item_end_expected;
1952a6ab66ebSSu Yue u64 item_data_end;
1953557ea5ddSQu Wenruo
1954557ea5ddSQu Wenruo btrfs_item_key_to_cpu(leaf, &key, slot);
1955557ea5ddSQu Wenruo
1956557ea5ddSQu Wenruo /* Make sure the keys are in the right order */
1957c7c01a4aSDavid Sterba if (unlikely(btrfs_comp_cpu_keys(&prev_key, &key) >= 0)) {
195886a6be3aSDavid Sterba generic_err(leaf, slot,
1959478d01b3SQu Wenruo "bad key order, prev (%llu %u %llu) current (%llu %u %llu)",
1960478d01b3SQu Wenruo prev_key.objectid, prev_key.type,
1961478d01b3SQu Wenruo prev_key.offset, key.objectid, key.type,
1962478d01b3SQu Wenruo key.offset);
1963924452c8SJosef Bacik return BTRFS_TREE_BLOCK_BAD_KEY_ORDER;
1964557ea5ddSQu Wenruo }
1965557ea5ddSQu Wenruo
1966a6ab66ebSSu Yue item_data_end = (u64)btrfs_item_offset(leaf, slot) +
1967a6ab66ebSSu Yue btrfs_item_size(leaf, slot);
1968557ea5ddSQu Wenruo /*
1969557ea5ddSQu Wenruo * Make sure the offset and ends are right, remember that the
1970557ea5ddSQu Wenruo * item data starts at the end of the leaf and grows towards the
1971557ea5ddSQu Wenruo * front.
1972557ea5ddSQu Wenruo */
1973557ea5ddSQu Wenruo if (slot == 0)
1974557ea5ddSQu Wenruo item_end_expected = BTRFS_LEAF_DATA_SIZE(fs_info);
1975557ea5ddSQu Wenruo else
19763212fa14SJosef Bacik item_end_expected = btrfs_item_offset(leaf,
1977557ea5ddSQu Wenruo slot - 1);
1978a6ab66ebSSu Yue if (unlikely(item_data_end != item_end_expected)) {
197986a6be3aSDavid Sterba generic_err(leaf, slot,
1980a6ab66ebSSu Yue "unexpected item end, have %llu expect %u",
1981a6ab66ebSSu Yue item_data_end, item_end_expected);
1982924452c8SJosef Bacik return BTRFS_TREE_BLOCK_INVALID_OFFSETS;
1983557ea5ddSQu Wenruo }
1984557ea5ddSQu Wenruo
1985557ea5ddSQu Wenruo /*
1986557ea5ddSQu Wenruo * Check to make sure that we don't point outside of the leaf,
1987557ea5ddSQu Wenruo * just in case all the items are consistent to each other, but
1988557ea5ddSQu Wenruo * all point outside of the leaf.
1989557ea5ddSQu Wenruo */
1990a6ab66ebSSu Yue if (unlikely(item_data_end > BTRFS_LEAF_DATA_SIZE(fs_info))) {
199186a6be3aSDavid Sterba generic_err(leaf, slot,
1992a6ab66ebSSu Yue "slot end outside of leaf, have %llu expect range [0, %u]",
1993a6ab66ebSSu Yue item_data_end, BTRFS_LEAF_DATA_SIZE(fs_info));
1994924452c8SJosef Bacik return BTRFS_TREE_BLOCK_INVALID_OFFSETS;
1995557ea5ddSQu Wenruo }
1996557ea5ddSQu Wenruo
1997557ea5ddSQu Wenruo /* Also check if the item pointer overlaps with btrfs item. */
1998c7c01a4aSDavid Sterba if (unlikely(btrfs_item_ptr_offset(leaf, slot) <
199942c9419aSJosef Bacik btrfs_item_nr_offset(leaf, slot) + sizeof(struct btrfs_item))) {
200086a6be3aSDavid Sterba generic_err(leaf, slot,
2001478d01b3SQu Wenruo "slot overlaps with its data, item end %lu data start %lu",
200242c9419aSJosef Bacik btrfs_item_nr_offset(leaf, slot) +
2003478d01b3SQu Wenruo sizeof(struct btrfs_item),
2004478d01b3SQu Wenruo btrfs_item_ptr_offset(leaf, slot));
2005924452c8SJosef Bacik return BTRFS_TREE_BLOCK_INVALID_OFFSETS;
2006557ea5ddSQu Wenruo }
2007557ea5ddSQu Wenruo
200885d8a826SJosef Bacik /*
200985d8a826SJosef Bacik * We only want to do this if WRITTEN is set, otherwise the leaf
201085d8a826SJosef Bacik * may be in some intermediate state and won't appear valid.
201185d8a826SJosef Bacik */
201285d8a826SJosef Bacik if (btrfs_header_flag(leaf, BTRFS_HEADER_FLAG_WRITTEN)) {
2013c8d54215SJosef Bacik enum btrfs_tree_block_status ret;
2014c8d54215SJosef Bacik
201569fc6cbbSQu Wenruo /*
201669fc6cbbSQu Wenruo * Check if the item size and content meet other
201769fc6cbbSQu Wenruo * criteria
201869fc6cbbSQu Wenruo */
20194e9845efSFilipe Manana ret = check_leaf_item(leaf, &key, slot, &prev_key);
2020c8d54215SJosef Bacik if (unlikely(ret != BTRFS_TREE_BLOCK_CLEAN))
2021557ea5ddSQu Wenruo return ret;
202269fc6cbbSQu Wenruo }
2023557ea5ddSQu Wenruo
2024557ea5ddSQu Wenruo prev_key.objectid = key.objectid;
2025557ea5ddSQu Wenruo prev_key.type = key.type;
2026557ea5ddSQu Wenruo prev_key.offset = key.offset;
2027557ea5ddSQu Wenruo }
2028557ea5ddSQu Wenruo
2029924452c8SJosef Bacik return BTRFS_TREE_BLOCK_CLEAN;
2030924452c8SJosef Bacik }
2031924452c8SJosef Bacik
btrfs_check_leaf(struct extent_buffer * leaf)2032924452c8SJosef Bacik int btrfs_check_leaf(struct extent_buffer *leaf)
2033924452c8SJosef Bacik {
2034924452c8SJosef Bacik enum btrfs_tree_block_status ret;
2035924452c8SJosef Bacik
2036924452c8SJosef Bacik ret = __btrfs_check_leaf(leaf);
2037924452c8SJosef Bacik if (unlikely(ret != BTRFS_TREE_BLOCK_CLEAN))
2038924452c8SJosef Bacik return -EUCLEAN;
2039557ea5ddSQu Wenruo return 0;
2040557ea5ddSQu Wenruo }
204185d8a826SJosef Bacik ALLOW_ERROR_INJECTION(btrfs_check_leaf, ERRNO);
2042557ea5ddSQu Wenruo
__btrfs_check_node(struct extent_buffer * node)2043c26fa931SJosef Bacik enum btrfs_tree_block_status __btrfs_check_node(struct extent_buffer *node)
2044557ea5ddSQu Wenruo {
2045813fd1dcSDavid Sterba struct btrfs_fs_info *fs_info = node->fs_info;
2046557ea5ddSQu Wenruo unsigned long nr = btrfs_header_nritems(node);
2047557ea5ddSQu Wenruo struct btrfs_key key, next_key;
2048557ea5ddSQu Wenruo int slot;
2049f556faa4SQu Wenruo int level = btrfs_header_level(node);
2050557ea5ddSQu Wenruo u64 bytenr;
2051557ea5ddSQu Wenruo
2052c7c01a4aSDavid Sterba if (unlikely(level <= 0 || level >= BTRFS_MAX_LEVEL)) {
205386a6be3aSDavid Sterba generic_err(node, 0,
2054f556faa4SQu Wenruo "invalid level for node, have %d expect [1, %d]",
2055f556faa4SQu Wenruo level, BTRFS_MAX_LEVEL - 1);
2056c26fa931SJosef Bacik return BTRFS_TREE_BLOCK_INVALID_LEVEL;
2057f556faa4SQu Wenruo }
2058c7c01a4aSDavid Sterba if (unlikely(nr == 0 || nr > BTRFS_NODEPTRS_PER_BLOCK(fs_info))) {
20592f659546SQu Wenruo btrfs_crit(fs_info,
2060bba4f298SQu Wenruo "corrupt node: root=%llu block=%llu, nritems too %s, have %lu expect range [1,%u]",
20612f659546SQu Wenruo btrfs_header_owner(node), node->start,
2062bba4f298SQu Wenruo nr == 0 ? "small" : "large", nr,
20632f659546SQu Wenruo BTRFS_NODEPTRS_PER_BLOCK(fs_info));
2064c26fa931SJosef Bacik return BTRFS_TREE_BLOCK_INVALID_NRITEMS;
2065557ea5ddSQu Wenruo }
2066557ea5ddSQu Wenruo
2067557ea5ddSQu Wenruo for (slot = 0; slot < nr - 1; slot++) {
2068557ea5ddSQu Wenruo bytenr = btrfs_node_blockptr(node, slot);
2069557ea5ddSQu Wenruo btrfs_node_key_to_cpu(node, &key, slot);
2070557ea5ddSQu Wenruo btrfs_node_key_to_cpu(node, &next_key, slot + 1);
2071557ea5ddSQu Wenruo
2072c7c01a4aSDavid Sterba if (unlikely(!bytenr)) {
207386a6be3aSDavid Sterba generic_err(node, slot,
2074bba4f298SQu Wenruo "invalid NULL node pointer");
2075c26fa931SJosef Bacik return BTRFS_TREE_BLOCK_INVALID_BLOCKPTR;
2076bba4f298SQu Wenruo }
2077c7c01a4aSDavid Sterba if (unlikely(!IS_ALIGNED(bytenr, fs_info->sectorsize))) {
207886a6be3aSDavid Sterba generic_err(node, slot,
2079bba4f298SQu Wenruo "unaligned pointer, have %llu should be aligned to %u",
20802f659546SQu Wenruo bytenr, fs_info->sectorsize);
2081c26fa931SJosef Bacik return BTRFS_TREE_BLOCK_INVALID_BLOCKPTR;
2082557ea5ddSQu Wenruo }
2083557ea5ddSQu Wenruo
2084c7c01a4aSDavid Sterba if (unlikely(btrfs_comp_cpu_keys(&key, &next_key) >= 0)) {
208586a6be3aSDavid Sterba generic_err(node, slot,
2086bba4f298SQu Wenruo "bad key order, current (%llu %u %llu) next (%llu %u %llu)",
2087bba4f298SQu Wenruo key.objectid, key.type, key.offset,
2088bba4f298SQu Wenruo next_key.objectid, next_key.type,
2089bba4f298SQu Wenruo next_key.offset);
2090c26fa931SJosef Bacik return BTRFS_TREE_BLOCK_BAD_KEY_ORDER;
2091557ea5ddSQu Wenruo }
2092557ea5ddSQu Wenruo }
2093c26fa931SJosef Bacik return BTRFS_TREE_BLOCK_CLEAN;
2094c26fa931SJosef Bacik }
2095c26fa931SJosef Bacik
btrfs_check_node(struct extent_buffer * node)2096c26fa931SJosef Bacik int btrfs_check_node(struct extent_buffer *node)
2097c26fa931SJosef Bacik {
2098c26fa931SJosef Bacik enum btrfs_tree_block_status ret;
2099c26fa931SJosef Bacik
2100c26fa931SJosef Bacik ret = __btrfs_check_node(node);
2101c26fa931SJosef Bacik if (unlikely(ret != BTRFS_TREE_BLOCK_CLEAN))
2102c26fa931SJosef Bacik return -EUCLEAN;
2103c26fa931SJosef Bacik return 0;
2104557ea5ddSQu Wenruo }
210502529d7aSQu Wenruo ALLOW_ERROR_INJECTION(btrfs_check_node, ERRNO);
210688c602abSQu Wenruo
btrfs_check_eb_owner(const struct extent_buffer * eb,u64 root_owner)210788c602abSQu Wenruo int btrfs_check_eb_owner(const struct extent_buffer *eb, u64 root_owner)
210888c602abSQu Wenruo {
210988c602abSQu Wenruo const bool is_subvol = is_fstree(root_owner);
211088c602abSQu Wenruo const u64 eb_owner = btrfs_header_owner(eb);
211188c602abSQu Wenruo
211288c602abSQu Wenruo /*
211388c602abSQu Wenruo * Skip dummy fs, as selftests don't create unique ebs for each dummy
211488c602abSQu Wenruo * root.
211588c602abSQu Wenruo */
211688c602abSQu Wenruo if (test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &eb->fs_info->fs_state))
211788c602abSQu Wenruo return 0;
211888c602abSQu Wenruo /*
211988c602abSQu Wenruo * There are several call sites (backref walking, qgroup, and data
212088c602abSQu Wenruo * reloc) passing 0 as @root_owner, as they are not holding the
212188c602abSQu Wenruo * tree root. In that case, we can not do a reliable ownership check,
212288c602abSQu Wenruo * so just exit.
212388c602abSQu Wenruo */
212488c602abSQu Wenruo if (root_owner == 0)
212588c602abSQu Wenruo return 0;
212688c602abSQu Wenruo /*
212788c602abSQu Wenruo * These trees use key.offset as their owner, our callers don't have
212888c602abSQu Wenruo * the extra capacity to pass key.offset here. So we just skip them.
212988c602abSQu Wenruo */
213088c602abSQu Wenruo if (root_owner == BTRFS_TREE_LOG_OBJECTID ||
213188c602abSQu Wenruo root_owner == BTRFS_TREE_RELOC_OBJECTID)
213288c602abSQu Wenruo return 0;
213388c602abSQu Wenruo
213488c602abSQu Wenruo if (!is_subvol) {
213588c602abSQu Wenruo /* For non-subvolume trees, the eb owner should match root owner */
213688c602abSQu Wenruo if (unlikely(root_owner != eb_owner)) {
213788c602abSQu Wenruo btrfs_crit(eb->fs_info,
213888c602abSQu Wenruo "corrupted %s, root=%llu block=%llu owner mismatch, have %llu expect %llu",
213988c602abSQu Wenruo btrfs_header_level(eb) == 0 ? "leaf" : "node",
214088c602abSQu Wenruo root_owner, btrfs_header_bytenr(eb), eb_owner,
214188c602abSQu Wenruo root_owner);
214288c602abSQu Wenruo return -EUCLEAN;
214388c602abSQu Wenruo }
214488c602abSQu Wenruo return 0;
214588c602abSQu Wenruo }
214688c602abSQu Wenruo
214788c602abSQu Wenruo /*
214888c602abSQu Wenruo * For subvolume trees, owners can mismatch, but they should all belong
214988c602abSQu Wenruo * to subvolume trees.
215088c602abSQu Wenruo */
215188c602abSQu Wenruo if (unlikely(is_subvol != is_fstree(eb_owner))) {
215288c602abSQu Wenruo btrfs_crit(eb->fs_info,
215388c602abSQu Wenruo "corrupted %s, root=%llu block=%llu owner mismatch, have %llu expect [%llu, %llu]",
215488c602abSQu Wenruo btrfs_header_level(eb) == 0 ? "leaf" : "node",
215588c602abSQu Wenruo root_owner, btrfs_header_bytenr(eb), eb_owner,
215688c602abSQu Wenruo BTRFS_FIRST_FREE_OBJECTID, BTRFS_LAST_FREE_OBJECTID);
215788c602abSQu Wenruo return -EUCLEAN;
215888c602abSQu Wenruo }
215988c602abSQu Wenruo return 0;
216088c602abSQu Wenruo }
21612cac5af1SJosef Bacik
btrfs_verify_level_key(struct extent_buffer * eb,int level,struct btrfs_key * first_key,u64 parent_transid)21622cac5af1SJosef Bacik int btrfs_verify_level_key(struct extent_buffer *eb, int level,
21632cac5af1SJosef Bacik struct btrfs_key *first_key, u64 parent_transid)
21642cac5af1SJosef Bacik {
21652cac5af1SJosef Bacik struct btrfs_fs_info *fs_info = eb->fs_info;
21662cac5af1SJosef Bacik int found_level;
21672cac5af1SJosef Bacik struct btrfs_key found_key;
21682cac5af1SJosef Bacik int ret;
21692cac5af1SJosef Bacik
21702cac5af1SJosef Bacik found_level = btrfs_header_level(eb);
21712cac5af1SJosef Bacik if (found_level != level) {
21722cac5af1SJosef Bacik WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG),
21732cac5af1SJosef Bacik KERN_ERR "BTRFS: tree level check failed\n");
21742cac5af1SJosef Bacik btrfs_err(fs_info,
21752cac5af1SJosef Bacik "tree level mismatch detected, bytenr=%llu level expected=%u has=%u",
21762cac5af1SJosef Bacik eb->start, level, found_level);
21772cac5af1SJosef Bacik return -EIO;
21782cac5af1SJosef Bacik }
21792cac5af1SJosef Bacik
21802cac5af1SJosef Bacik if (!first_key)
21812cac5af1SJosef Bacik return 0;
21822cac5af1SJosef Bacik
21832cac5af1SJosef Bacik /*
21842cac5af1SJosef Bacik * For live tree block (new tree blocks in current transaction),
21852cac5af1SJosef Bacik * we need proper lock context to avoid race, which is impossible here.
21862cac5af1SJosef Bacik * So we only checks tree blocks which is read from disk, whose
21872cac5af1SJosef Bacik * generation <= fs_info->last_trans_committed.
21882cac5af1SJosef Bacik */
21892cac5af1SJosef Bacik if (btrfs_header_generation(eb) > fs_info->last_trans_committed)
21902cac5af1SJosef Bacik return 0;
21912cac5af1SJosef Bacik
21922cac5af1SJosef Bacik /* We have @first_key, so this @eb must have at least one item */
21932cac5af1SJosef Bacik if (btrfs_header_nritems(eb) == 0) {
21942cac5af1SJosef Bacik btrfs_err(fs_info,
21952cac5af1SJosef Bacik "invalid tree nritems, bytenr=%llu nritems=0 expect >0",
21962cac5af1SJosef Bacik eb->start);
21972cac5af1SJosef Bacik WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG));
21982cac5af1SJosef Bacik return -EUCLEAN;
21992cac5af1SJosef Bacik }
22002cac5af1SJosef Bacik
22012cac5af1SJosef Bacik if (found_level)
22022cac5af1SJosef Bacik btrfs_node_key_to_cpu(eb, &found_key, 0);
22032cac5af1SJosef Bacik else
22042cac5af1SJosef Bacik btrfs_item_key_to_cpu(eb, &found_key, 0);
22052cac5af1SJosef Bacik ret = btrfs_comp_cpu_keys(first_key, &found_key);
22062cac5af1SJosef Bacik
22072cac5af1SJosef Bacik if (ret) {
22082cac5af1SJosef Bacik WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG),
22092cac5af1SJosef Bacik KERN_ERR "BTRFS: tree first key check failed\n");
22102cac5af1SJosef Bacik btrfs_err(fs_info,
22112cac5af1SJosef Bacik "tree first key mismatch detected, bytenr=%llu parent_transid=%llu key expected=(%llu,%u,%llu) has=(%llu,%u,%llu)",
22122cac5af1SJosef Bacik eb->start, parent_transid, first_key->objectid,
22132cac5af1SJosef Bacik first_key->type, first_key->offset,
22142cac5af1SJosef Bacik found_key.objectid, found_key.type,
22152cac5af1SJosef Bacik found_key.offset);
22162cac5af1SJosef Bacik }
22172cac5af1SJosef Bacik return ret;
22182cac5af1SJosef Bacik }
2219