blob: 1dac957eaf358481f0d4f8bf42c60a892c2a34e8 [file] [log] [blame]
David Sterbac1d7c512018-04-03 19:23:33 +02001// SPDX-License-Identifier: GPL-2.0
Qu Wenruo557ea5d2017-10-09 01:51:02 +00002/*
3 * Copyright (C) Qu Wenruo 2017. All rights reserved.
Qu Wenruo557ea5d2017-10-09 01:51:02 +00004 */
5
6/*
7 * The module is used to catch unexpected/corrupted tree block data.
8 * Such behavior can be caused either by a fuzzed image or bugs.
9 *
10 * The objective is to do leaf/node validation checks when tree block is read
11 * from disk, and check *every* possible member, so other code won't
12 * need to checking them again.
13 *
14 * Due to the potential and unwanted damage, every checker needs to be
15 * carefully reviewed otherwise so it does not prevent mount of valid images.
16 */
17
18#include "ctree.h"
19#include "tree-checker.h"
20#include "disk-io.h"
21#include "compression.h"
Qu Wenruofce466e2018-07-03 17:10:05 +080022#include "volumes.h"
Qu Wenruo557ea5d2017-10-09 01:51:02 +000023
Qu Wenruobba4f292017-10-09 01:51:03 +000024/*
25 * Error message should follow the following format:
26 * corrupt <type>: <identifier>, <reason>[, <bad_value>]
27 *
28 * @type: leaf or node
29 * @identifier: the necessary info to locate the leaf/node.
Andrea Gelmini52042d82018-11-28 12:05:13 +010030 * It's recommended to decode key.objecitd/offset if it's
Qu Wenruobba4f292017-10-09 01:51:03 +000031 * meaningful.
32 * @reason: describe the error
Andrea Gelmini52042d82018-11-28 12:05:13 +010033 * @bad_value: optional, it's recommended to output bad value and its
Qu Wenruobba4f292017-10-09 01:51:03 +000034 * expected value (range).
35 *
36 * Since comma is used to separate the components, only space is allowed
37 * inside each component.
38 */
39
40/*
41 * Append generic "corrupt leaf/node root=%llu block=%llu slot=%d: " to @fmt.
42 * Allows callers to customize the output.
43 */
David Sterba86a6be32019-03-20 15:31:28 +010044__printf(3, 4)
David Sterbae67c7182018-02-19 17:24:18 +010045__cold
David Sterba86a6be32019-03-20 15:31:28 +010046static void generic_err(const struct extent_buffer *eb, int slot,
Qu Wenruobba4f292017-10-09 01:51:03 +000047 const char *fmt, ...)
48{
David Sterba86a6be32019-03-20 15:31:28 +010049 const struct btrfs_fs_info *fs_info = eb->fs_info;
Qu Wenruobba4f292017-10-09 01:51:03 +000050 struct va_format vaf;
51 va_list args;
52
53 va_start(args, fmt);
54
55 vaf.fmt = fmt;
56 vaf.va = &args;
57
Qu Wenruo2f659542018-01-25 14:56:18 +080058 btrfs_crit(fs_info,
Qu Wenruobba4f292017-10-09 01:51:03 +000059 "corrupt %s: root=%llu block=%llu slot=%d, %pV",
60 btrfs_header_level(eb) == 0 ? "leaf" : "node",
Qu Wenruo2f659542018-01-25 14:56:18 +080061 btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot, &vaf);
Qu Wenruobba4f292017-10-09 01:51:03 +000062 va_end(args);
63}
64
Qu Wenruo8806d712017-10-09 01:51:06 +000065/*
66 * Customized reporter for extent data item, since its key objectid and
67 * offset has its own meaning.
68 */
David Sterba1fd715f2019-03-20 15:32:46 +010069__printf(3, 4)
David Sterbae67c7182018-02-19 17:24:18 +010070__cold
David Sterba1fd715f2019-03-20 15:32:46 +010071static void file_extent_err(const struct extent_buffer *eb, int slot,
Qu Wenruo8806d712017-10-09 01:51:06 +000072 const char *fmt, ...)
73{
David Sterba1fd715f2019-03-20 15:32:46 +010074 const struct btrfs_fs_info *fs_info = eb->fs_info;
Qu Wenruo8806d712017-10-09 01:51:06 +000075 struct btrfs_key key;
76 struct va_format vaf;
77 va_list args;
78
79 btrfs_item_key_to_cpu(eb, &key, slot);
80 va_start(args, fmt);
81
82 vaf.fmt = fmt;
83 vaf.va = &args;
84
Qu Wenruo2f659542018-01-25 14:56:18 +080085 btrfs_crit(fs_info,
Qu Wenruo8806d712017-10-09 01:51:06 +000086 "corrupt %s: root=%llu block=%llu slot=%d ino=%llu file_offset=%llu, %pV",
Qu Wenruo2f659542018-01-25 14:56:18 +080087 btrfs_header_level(eb) == 0 ? "leaf" : "node",
88 btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot,
89 key.objectid, key.offset, &vaf);
Qu Wenruo8806d712017-10-09 01:51:06 +000090 va_end(args);
91}
92
93/*
94 * Return 0 if the btrfs_file_extent_##name is aligned to @alignment
95 * Else return 1
96 */
Qu Wenruo2f659542018-01-25 14:56:18 +080097#define CHECK_FE_ALIGNED(fs_info, leaf, slot, fi, name, alignment) \
Qu Wenruo8806d712017-10-09 01:51:06 +000098({ \
99 if (!IS_ALIGNED(btrfs_file_extent_##name((leaf), (fi)), (alignment))) \
David Sterba1fd715f2019-03-20 15:32:46 +0100100 file_extent_err((leaf), (slot), \
Qu Wenruo8806d712017-10-09 01:51:06 +0000101 "invalid %s for file extent, have %llu, should be aligned to %u", \
102 (#name), btrfs_file_extent_##name((leaf), (fi)), \
103 (alignment)); \
104 (!IS_ALIGNED(btrfs_file_extent_##name((leaf), (fi)), (alignment))); \
105})
106
David Sterbaae2a19d2019-03-20 16:21:10 +0100107static int check_extent_data_item(struct extent_buffer *leaf,
Qu Wenruo557ea5d2017-10-09 01:51:02 +0000108 struct btrfs_key *key, int slot)
109{
David Sterbaae2a19d2019-03-20 16:21:10 +0100110 struct btrfs_fs_info *fs_info = leaf->fs_info;
Qu Wenruo557ea5d2017-10-09 01:51:02 +0000111 struct btrfs_file_extent_item *fi;
Qu Wenruo2f659542018-01-25 14:56:18 +0800112 u32 sectorsize = fs_info->sectorsize;
Qu Wenruo557ea5d2017-10-09 01:51:02 +0000113 u32 item_size = btrfs_item_size_nr(leaf, slot);
114
115 if (!IS_ALIGNED(key->offset, sectorsize)) {
David Sterba1fd715f2019-03-20 15:32:46 +0100116 file_extent_err(leaf, slot,
Qu Wenruo8806d712017-10-09 01:51:06 +0000117"unaligned file_offset for file extent, have %llu should be aligned to %u",
118 key->offset, sectorsize);
Qu Wenruo557ea5d2017-10-09 01:51:02 +0000119 return -EUCLEAN;
120 }
121
122 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
123
124 if (btrfs_file_extent_type(leaf, fi) > BTRFS_FILE_EXTENT_TYPES) {
David Sterba1fd715f2019-03-20 15:32:46 +0100125 file_extent_err(leaf, slot,
Qu Wenruo8806d712017-10-09 01:51:06 +0000126 "invalid type for file extent, have %u expect range [0, %u]",
127 btrfs_file_extent_type(leaf, fi),
128 BTRFS_FILE_EXTENT_TYPES);
Qu Wenruo557ea5d2017-10-09 01:51:02 +0000129 return -EUCLEAN;
130 }
131
132 /*
Andrea Gelmini52042d82018-11-28 12:05:13 +0100133 * Support for new compression/encryption must introduce incompat flag,
Qu Wenruo557ea5d2017-10-09 01:51:02 +0000134 * and must be caught in open_ctree().
135 */
136 if (btrfs_file_extent_compression(leaf, fi) > BTRFS_COMPRESS_TYPES) {
David Sterba1fd715f2019-03-20 15:32:46 +0100137 file_extent_err(leaf, slot,
Qu Wenruo8806d712017-10-09 01:51:06 +0000138 "invalid compression for file extent, have %u expect range [0, %u]",
139 btrfs_file_extent_compression(leaf, fi),
140 BTRFS_COMPRESS_TYPES);
Qu Wenruo557ea5d2017-10-09 01:51:02 +0000141 return -EUCLEAN;
142 }
143 if (btrfs_file_extent_encryption(leaf, fi)) {
David Sterba1fd715f2019-03-20 15:32:46 +0100144 file_extent_err(leaf, slot,
Qu Wenruo8806d712017-10-09 01:51:06 +0000145 "invalid encryption for file extent, have %u expect 0",
146 btrfs_file_extent_encryption(leaf, fi));
Qu Wenruo557ea5d2017-10-09 01:51:02 +0000147 return -EUCLEAN;
148 }
149 if (btrfs_file_extent_type(leaf, fi) == BTRFS_FILE_EXTENT_INLINE) {
150 /* Inline extent must have 0 as key offset */
151 if (key->offset) {
David Sterba1fd715f2019-03-20 15:32:46 +0100152 file_extent_err(leaf, slot,
Qu Wenruo8806d712017-10-09 01:51:06 +0000153 "invalid file_offset for inline file extent, have %llu expect 0",
154 key->offset);
Qu Wenruo557ea5d2017-10-09 01:51:02 +0000155 return -EUCLEAN;
156 }
157
158 /* Compressed inline extent has no on-disk size, skip it */
159 if (btrfs_file_extent_compression(leaf, fi) !=
160 BTRFS_COMPRESS_NONE)
161 return 0;
162
163 /* Uncompressed inline extent size must match item size */
164 if (item_size != BTRFS_FILE_EXTENT_INLINE_DATA_START +
165 btrfs_file_extent_ram_bytes(leaf, fi)) {
David Sterba1fd715f2019-03-20 15:32:46 +0100166 file_extent_err(leaf, slot,
Qu Wenruo8806d712017-10-09 01:51:06 +0000167 "invalid ram_bytes for uncompressed inline extent, have %u expect %llu",
168 item_size, BTRFS_FILE_EXTENT_INLINE_DATA_START +
169 btrfs_file_extent_ram_bytes(leaf, fi));
Qu Wenruo557ea5d2017-10-09 01:51:02 +0000170 return -EUCLEAN;
171 }
172 return 0;
173 }
174
175 /* Regular or preallocated extent has fixed item size */
176 if (item_size != sizeof(*fi)) {
David Sterba1fd715f2019-03-20 15:32:46 +0100177 file_extent_err(leaf, slot,
Arnd Bergmann709a95c2017-10-13 11:27:35 +0200178 "invalid item size for reg/prealloc file extent, have %u expect %zu",
Qu Wenruo8806d712017-10-09 01:51:06 +0000179 item_size, sizeof(*fi));
Qu Wenruo557ea5d2017-10-09 01:51:02 +0000180 return -EUCLEAN;
181 }
Qu Wenruo2f659542018-01-25 14:56:18 +0800182 if (CHECK_FE_ALIGNED(fs_info, leaf, slot, fi, ram_bytes, sectorsize) ||
183 CHECK_FE_ALIGNED(fs_info, leaf, slot, fi, disk_bytenr, sectorsize) ||
184 CHECK_FE_ALIGNED(fs_info, leaf, slot, fi, disk_num_bytes, sectorsize) ||
185 CHECK_FE_ALIGNED(fs_info, leaf, slot, fi, offset, sectorsize) ||
186 CHECK_FE_ALIGNED(fs_info, leaf, slot, fi, num_bytes, sectorsize))
Qu Wenruo557ea5d2017-10-09 01:51:02 +0000187 return -EUCLEAN;
Qu Wenruo557ea5d2017-10-09 01:51:02 +0000188 return 0;
189}
190
David Sterba68128ce2019-03-20 16:02:56 +0100191static int check_csum_item(struct extent_buffer *leaf, struct btrfs_key *key,
Qu Wenruo2f659542018-01-25 14:56:18 +0800192 int slot)
Qu Wenruo557ea5d2017-10-09 01:51:02 +0000193{
David Sterba68128ce2019-03-20 16:02:56 +0100194 struct btrfs_fs_info *fs_info = leaf->fs_info;
Qu Wenruo2f659542018-01-25 14:56:18 +0800195 u32 sectorsize = fs_info->sectorsize;
196 u32 csumsize = btrfs_super_csum_size(fs_info->super_copy);
Qu Wenruo557ea5d2017-10-09 01:51:02 +0000197
198 if (key->objectid != BTRFS_EXTENT_CSUM_OBJECTID) {
David Sterba86a6be32019-03-20 15:31:28 +0100199 generic_err(leaf, slot,
Qu Wenruod508c5f2017-10-09 01:51:05 +0000200 "invalid key objectid for csum item, have %llu expect %llu",
201 key->objectid, BTRFS_EXTENT_CSUM_OBJECTID);
Qu Wenruo557ea5d2017-10-09 01:51:02 +0000202 return -EUCLEAN;
203 }
204 if (!IS_ALIGNED(key->offset, sectorsize)) {
David Sterba86a6be32019-03-20 15:31:28 +0100205 generic_err(leaf, slot,
Qu Wenruod508c5f2017-10-09 01:51:05 +0000206 "unaligned key offset for csum item, have %llu should be aligned to %u",
207 key->offset, sectorsize);
Qu Wenruo557ea5d2017-10-09 01:51:02 +0000208 return -EUCLEAN;
209 }
210 if (!IS_ALIGNED(btrfs_item_size_nr(leaf, slot), csumsize)) {
David Sterba86a6be32019-03-20 15:31:28 +0100211 generic_err(leaf, slot,
Qu Wenruod508c5f2017-10-09 01:51:05 +0000212 "unaligned item size for csum item, have %u should be aligned to %u",
213 btrfs_item_size_nr(leaf, slot), csumsize);
Qu Wenruo557ea5d2017-10-09 01:51:02 +0000214 return -EUCLEAN;
215 }
216 return 0;
217}
218
219/*
Qu Wenruoad7b03682017-11-08 08:54:25 +0800220 * Customized reported for dir_item, only important new info is key->objectid,
221 * which represents inode number
222 */
David Sterbad98ced62019-03-20 16:07:27 +0100223__printf(3, 4)
David Sterbae67c7182018-02-19 17:24:18 +0100224__cold
David Sterbad98ced62019-03-20 16:07:27 +0100225static void dir_item_err(const struct extent_buffer *eb, int slot,
Qu Wenruoad7b03682017-11-08 08:54:25 +0800226 const char *fmt, ...)
227{
David Sterbad98ced62019-03-20 16:07:27 +0100228 const struct btrfs_fs_info *fs_info = eb->fs_info;
Qu Wenruoad7b03682017-11-08 08:54:25 +0800229 struct btrfs_key key;
230 struct va_format vaf;
231 va_list args;
232
233 btrfs_item_key_to_cpu(eb, &key, slot);
234 va_start(args, fmt);
235
236 vaf.fmt = fmt;
237 vaf.va = &args;
238
Qu Wenruo2f659542018-01-25 14:56:18 +0800239 btrfs_crit(fs_info,
Qu Wenruoad7b03682017-11-08 08:54:25 +0800240 "corrupt %s: root=%llu block=%llu slot=%d ino=%llu, %pV",
Qu Wenruo2f659542018-01-25 14:56:18 +0800241 btrfs_header_level(eb) == 0 ? "leaf" : "node",
242 btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot,
243 key.objectid, &vaf);
Qu Wenruoad7b03682017-11-08 08:54:25 +0800244 va_end(args);
245}
246
David Sterbace4252c2019-03-20 16:17:46 +0100247static int check_dir_item(struct extent_buffer *leaf,
Qu Wenruoad7b03682017-11-08 08:54:25 +0800248 struct btrfs_key *key, int slot)
249{
David Sterbace4252c2019-03-20 16:17:46 +0100250 struct btrfs_fs_info *fs_info = leaf->fs_info;
Qu Wenruoad7b03682017-11-08 08:54:25 +0800251 struct btrfs_dir_item *di;
252 u32 item_size = btrfs_item_size_nr(leaf, slot);
253 u32 cur = 0;
254
255 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
256 while (cur < item_size) {
Qu Wenruoad7b03682017-11-08 08:54:25 +0800257 u32 name_len;
258 u32 data_len;
259 u32 max_name_len;
260 u32 total_size;
261 u32 name_hash;
262 u8 dir_type;
263
264 /* header itself should not cross item boundary */
265 if (cur + sizeof(*di) > item_size) {
David Sterbad98ced62019-03-20 16:07:27 +0100266 dir_item_err(leaf, slot,
Arnd Bergmann7cfad652017-12-06 15:18:14 +0100267 "dir item header crosses item boundary, have %zu boundary %u",
Qu Wenruoad7b03682017-11-08 08:54:25 +0800268 cur + sizeof(*di), item_size);
269 return -EUCLEAN;
270 }
271
272 /* dir type check */
273 dir_type = btrfs_dir_type(leaf, di);
274 if (dir_type >= BTRFS_FT_MAX) {
David Sterbad98ced62019-03-20 16:07:27 +0100275 dir_item_err(leaf, slot,
Qu Wenruoad7b03682017-11-08 08:54:25 +0800276 "invalid dir item type, have %u expect [0, %u)",
277 dir_type, BTRFS_FT_MAX);
278 return -EUCLEAN;
279 }
280
281 if (key->type == BTRFS_XATTR_ITEM_KEY &&
282 dir_type != BTRFS_FT_XATTR) {
David Sterbad98ced62019-03-20 16:07:27 +0100283 dir_item_err(leaf, slot,
Qu Wenruoad7b03682017-11-08 08:54:25 +0800284 "invalid dir item type for XATTR key, have %u expect %u",
285 dir_type, BTRFS_FT_XATTR);
286 return -EUCLEAN;
287 }
288 if (dir_type == BTRFS_FT_XATTR &&
289 key->type != BTRFS_XATTR_ITEM_KEY) {
David Sterbad98ced62019-03-20 16:07:27 +0100290 dir_item_err(leaf, slot,
Qu Wenruoad7b03682017-11-08 08:54:25 +0800291 "xattr dir type found for non-XATTR key");
292 return -EUCLEAN;
293 }
294 if (dir_type == BTRFS_FT_XATTR)
295 max_name_len = XATTR_NAME_MAX;
296 else
297 max_name_len = BTRFS_NAME_LEN;
298
299 /* Name/data length check */
300 name_len = btrfs_dir_name_len(leaf, di);
301 data_len = btrfs_dir_data_len(leaf, di);
302 if (name_len > max_name_len) {
David Sterbad98ced62019-03-20 16:07:27 +0100303 dir_item_err(leaf, slot,
Qu Wenruoad7b03682017-11-08 08:54:25 +0800304 "dir item name len too long, have %u max %u",
305 name_len, max_name_len);
306 return -EUCLEAN;
307 }
Qu Wenruo2f659542018-01-25 14:56:18 +0800308 if (name_len + data_len > BTRFS_MAX_XATTR_SIZE(fs_info)) {
David Sterbad98ced62019-03-20 16:07:27 +0100309 dir_item_err(leaf, slot,
Qu Wenruoad7b03682017-11-08 08:54:25 +0800310 "dir item name and data len too long, have %u max %u",
311 name_len + data_len,
Qu Wenruo2f659542018-01-25 14:56:18 +0800312 BTRFS_MAX_XATTR_SIZE(fs_info));
Qu Wenruoad7b03682017-11-08 08:54:25 +0800313 return -EUCLEAN;
314 }
315
316 if (data_len && dir_type != BTRFS_FT_XATTR) {
David Sterbad98ced62019-03-20 16:07:27 +0100317 dir_item_err(leaf, slot,
Qu Wenruoad7b03682017-11-08 08:54:25 +0800318 "dir item with invalid data len, have %u expect 0",
319 data_len);
320 return -EUCLEAN;
321 }
322
323 total_size = sizeof(*di) + name_len + data_len;
324
325 /* header and name/data should not cross item boundary */
326 if (cur + total_size > item_size) {
David Sterbad98ced62019-03-20 16:07:27 +0100327 dir_item_err(leaf, slot,
Qu Wenruoad7b03682017-11-08 08:54:25 +0800328 "dir item data crosses item boundary, have %u boundary %u",
329 cur + total_size, item_size);
330 return -EUCLEAN;
331 }
332
333 /*
334 * Special check for XATTR/DIR_ITEM, as key->offset is name
335 * hash, should match its name
336 */
337 if (key->type == BTRFS_DIR_ITEM_KEY ||
338 key->type == BTRFS_XATTR_ITEM_KEY) {
David Sterbae2683fc2018-01-10 15:13:07 +0100339 char namebuf[max(BTRFS_NAME_LEN, XATTR_NAME_MAX)];
340
Qu Wenruoad7b03682017-11-08 08:54:25 +0800341 read_extent_buffer(leaf, namebuf,
342 (unsigned long)(di + 1), name_len);
343 name_hash = btrfs_name_hash(namebuf, name_len);
344 if (key->offset != name_hash) {
David Sterbad98ced62019-03-20 16:07:27 +0100345 dir_item_err(leaf, slot,
Qu Wenruoad7b03682017-11-08 08:54:25 +0800346 "name hash mismatch with key, have 0x%016x expect 0x%016llx",
347 name_hash, key->offset);
348 return -EUCLEAN;
349 }
350 }
351 cur += total_size;
352 di = (struct btrfs_dir_item *)((void *)di + total_size);
353 }
354 return 0;
355}
356
David Sterba4806bd82019-03-20 16:18:57 +0100357__printf(3, 4)
Qu Wenruofce466e2018-07-03 17:10:05 +0800358__cold
David Sterba4806bd82019-03-20 16:18:57 +0100359static void block_group_err(const struct extent_buffer *eb, int slot,
Qu Wenruofce466e2018-07-03 17:10:05 +0800360 const char *fmt, ...)
361{
David Sterba4806bd82019-03-20 16:18:57 +0100362 const struct btrfs_fs_info *fs_info = eb->fs_info;
Qu Wenruofce466e2018-07-03 17:10:05 +0800363 struct btrfs_key key;
364 struct va_format vaf;
365 va_list args;
366
367 btrfs_item_key_to_cpu(eb, &key, slot);
368 va_start(args, fmt);
369
370 vaf.fmt = fmt;
371 vaf.va = &args;
372
373 btrfs_crit(fs_info,
374 "corrupt %s: root=%llu block=%llu slot=%d bg_start=%llu bg_len=%llu, %pV",
375 btrfs_header_level(eb) == 0 ? "leaf" : "node",
376 btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot,
377 key.objectid, key.offset, &vaf);
378 va_end(args);
379}
380
David Sterbaaf60ce22019-03-20 16:19:31 +0100381static int check_block_group_item(struct extent_buffer *leaf,
Qu Wenruofce466e2018-07-03 17:10:05 +0800382 struct btrfs_key *key, int slot)
383{
384 struct btrfs_block_group_item bgi;
385 u32 item_size = btrfs_item_size_nr(leaf, slot);
386 u64 flags;
387 u64 type;
388
389 /*
390 * Here we don't really care about alignment since extent allocator can
Qu Wenruo10950922018-11-23 09:06:36 +0800391 * handle it. We care more about the size.
Qu Wenruofce466e2018-07-03 17:10:05 +0800392 */
Qu Wenruo10950922018-11-23 09:06:36 +0800393 if (key->offset == 0) {
David Sterba4806bd82019-03-20 16:18:57 +0100394 block_group_err(leaf, slot,
Qu Wenruo10950922018-11-23 09:06:36 +0800395 "invalid block group size 0");
Qu Wenruofce466e2018-07-03 17:10:05 +0800396 return -EUCLEAN;
397 }
398
399 if (item_size != sizeof(bgi)) {
David Sterba4806bd82019-03-20 16:18:57 +0100400 block_group_err(leaf, slot,
Qu Wenruofce466e2018-07-03 17:10:05 +0800401 "invalid item size, have %u expect %zu",
402 item_size, sizeof(bgi));
403 return -EUCLEAN;
404 }
405
406 read_extent_buffer(leaf, &bgi, btrfs_item_ptr_offset(leaf, slot),
407 sizeof(bgi));
408 if (btrfs_block_group_chunk_objectid(&bgi) !=
409 BTRFS_FIRST_CHUNK_TREE_OBJECTID) {
David Sterba4806bd82019-03-20 16:18:57 +0100410 block_group_err(leaf, slot,
Qu Wenruofce466e2018-07-03 17:10:05 +0800411 "invalid block group chunk objectid, have %llu expect %llu",
412 btrfs_block_group_chunk_objectid(&bgi),
413 BTRFS_FIRST_CHUNK_TREE_OBJECTID);
414 return -EUCLEAN;
415 }
416
417 if (btrfs_block_group_used(&bgi) > key->offset) {
David Sterba4806bd82019-03-20 16:18:57 +0100418 block_group_err(leaf, slot,
Qu Wenruofce466e2018-07-03 17:10:05 +0800419 "invalid block group used, have %llu expect [0, %llu)",
420 btrfs_block_group_used(&bgi), key->offset);
421 return -EUCLEAN;
422 }
423
424 flags = btrfs_block_group_flags(&bgi);
425 if (hweight64(flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) > 1) {
David Sterba4806bd82019-03-20 16:18:57 +0100426 block_group_err(leaf, slot,
Qu Wenruofce466e2018-07-03 17:10:05 +0800427"invalid profile flags, have 0x%llx (%lu bits set) expect no more than 1 bit set",
428 flags & BTRFS_BLOCK_GROUP_PROFILE_MASK,
429 hweight64(flags & BTRFS_BLOCK_GROUP_PROFILE_MASK));
430 return -EUCLEAN;
431 }
432
433 type = flags & BTRFS_BLOCK_GROUP_TYPE_MASK;
434 if (type != BTRFS_BLOCK_GROUP_DATA &&
435 type != BTRFS_BLOCK_GROUP_METADATA &&
436 type != BTRFS_BLOCK_GROUP_SYSTEM &&
437 type != (BTRFS_BLOCK_GROUP_METADATA |
438 BTRFS_BLOCK_GROUP_DATA)) {
David Sterba4806bd82019-03-20 16:18:57 +0100439 block_group_err(leaf, slot,
Shaokun Zhang761333f2018-11-05 18:49:09 +0800440"invalid type, have 0x%llx (%lu bits set) expect either 0x%llx, 0x%llx, 0x%llx or 0x%llx",
Qu Wenruofce466e2018-07-03 17:10:05 +0800441 type, hweight64(type),
442 BTRFS_BLOCK_GROUP_DATA, BTRFS_BLOCK_GROUP_METADATA,
443 BTRFS_BLOCK_GROUP_SYSTEM,
444 BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA);
445 return -EUCLEAN;
446 }
447 return 0;
448}
449
David Sterbad001e4a2019-03-20 16:22:58 +0100450__printf(4, 5)
Qu Wenruof1140242019-03-20 13:36:06 +0800451__cold
David Sterbad001e4a2019-03-20 16:22:58 +0100452static void chunk_err(const struct extent_buffer *leaf,
Qu Wenruof1140242019-03-20 13:36:06 +0800453 const struct btrfs_chunk *chunk, u64 logical,
454 const char *fmt, ...)
455{
David Sterbad001e4a2019-03-20 16:22:58 +0100456 const struct btrfs_fs_info *fs_info = leaf->fs_info;
Qu Wenruof1140242019-03-20 13:36:06 +0800457 bool is_sb;
458 struct va_format vaf;
459 va_list args;
460 int i;
461 int slot = -1;
462
463 /* Only superblock eb is able to have such small offset */
464 is_sb = (leaf->start == BTRFS_SUPER_INFO_OFFSET);
465
466 if (!is_sb) {
467 /*
468 * Get the slot number by iterating through all slots, this
469 * would provide better readability.
470 */
471 for (i = 0; i < btrfs_header_nritems(leaf); i++) {
472 if (btrfs_item_ptr_offset(leaf, i) ==
473 (unsigned long)chunk) {
474 slot = i;
475 break;
476 }
477 }
478 }
479 va_start(args, fmt);
480 vaf.fmt = fmt;
481 vaf.va = &args;
482
483 if (is_sb)
484 btrfs_crit(fs_info,
485 "corrupt superblock syschunk array: chunk_start=%llu, %pV",
486 logical, &vaf);
487 else
488 btrfs_crit(fs_info,
489 "corrupt leaf: root=%llu block=%llu slot=%d chunk_start=%llu, %pV",
490 BTRFS_CHUNK_TREE_OBJECTID, leaf->start, slot,
491 logical, &vaf);
492 va_end(args);
493}
494
Qu Wenruoad7b03682017-11-08 08:54:25 +0800495/*
Qu Wenruo82fc28f2019-03-20 13:16:42 +0800496 * The common chunk check which could also work on super block sys chunk array.
497 *
Qu Wenruobf871c32019-03-20 13:39:14 +0800498 * Return -EUCLEAN if anything is corrupted.
Qu Wenruo82fc28f2019-03-20 13:16:42 +0800499 * Return 0 if everything is OK.
500 */
501int btrfs_check_chunk_valid(struct btrfs_fs_info *fs_info,
502 struct extent_buffer *leaf,
503 struct btrfs_chunk *chunk, u64 logical)
504{
505 u64 length;
506 u64 stripe_len;
507 u16 num_stripes;
508 u16 sub_stripes;
509 u64 type;
510 u64 features;
511 bool mixed = false;
512
513 length = btrfs_chunk_length(leaf, chunk);
514 stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
515 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
516 sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
517 type = btrfs_chunk_type(leaf, chunk);
518
519 if (!num_stripes) {
David Sterbad001e4a2019-03-20 16:22:58 +0100520 chunk_err(leaf, chunk, logical,
Qu Wenruof1140242019-03-20 13:36:06 +0800521 "invalid chunk num_stripes, have %u", num_stripes);
Qu Wenruobf871c32019-03-20 13:39:14 +0800522 return -EUCLEAN;
Qu Wenruo82fc28f2019-03-20 13:16:42 +0800523 }
524 if (!IS_ALIGNED(logical, fs_info->sectorsize)) {
David Sterbad001e4a2019-03-20 16:22:58 +0100525 chunk_err(leaf, chunk, logical,
Qu Wenruof1140242019-03-20 13:36:06 +0800526 "invalid chunk logical, have %llu should aligned to %u",
527 logical, fs_info->sectorsize);
Qu Wenruobf871c32019-03-20 13:39:14 +0800528 return -EUCLEAN;
Qu Wenruo82fc28f2019-03-20 13:16:42 +0800529 }
530 if (btrfs_chunk_sector_size(leaf, chunk) != fs_info->sectorsize) {
David Sterbad001e4a2019-03-20 16:22:58 +0100531 chunk_err(leaf, chunk, logical,
Qu Wenruof1140242019-03-20 13:36:06 +0800532 "invalid chunk sectorsize, have %u expect %u",
533 btrfs_chunk_sector_size(leaf, chunk),
534 fs_info->sectorsize);
Qu Wenruobf871c32019-03-20 13:39:14 +0800535 return -EUCLEAN;
Qu Wenruo82fc28f2019-03-20 13:16:42 +0800536 }
537 if (!length || !IS_ALIGNED(length, fs_info->sectorsize)) {
David Sterbad001e4a2019-03-20 16:22:58 +0100538 chunk_err(leaf, chunk, logical,
Qu Wenruof1140242019-03-20 13:36:06 +0800539 "invalid chunk length, have %llu", length);
Qu Wenruobf871c32019-03-20 13:39:14 +0800540 return -EUCLEAN;
Qu Wenruo82fc28f2019-03-20 13:16:42 +0800541 }
542 if (!is_power_of_2(stripe_len) || stripe_len != BTRFS_STRIPE_LEN) {
David Sterbad001e4a2019-03-20 16:22:58 +0100543 chunk_err(leaf, chunk, logical,
Qu Wenruof1140242019-03-20 13:36:06 +0800544 "invalid chunk stripe length: %llu",
Qu Wenruo82fc28f2019-03-20 13:16:42 +0800545 stripe_len);
Qu Wenruobf871c32019-03-20 13:39:14 +0800546 return -EUCLEAN;
Qu Wenruo82fc28f2019-03-20 13:16:42 +0800547 }
548 if (~(BTRFS_BLOCK_GROUP_TYPE_MASK | BTRFS_BLOCK_GROUP_PROFILE_MASK) &
549 type) {
David Sterbad001e4a2019-03-20 16:22:58 +0100550 chunk_err(leaf, chunk, logical,
Qu Wenruof1140242019-03-20 13:36:06 +0800551 "unrecognized chunk type: 0x%llx",
Qu Wenruo82fc28f2019-03-20 13:16:42 +0800552 ~(BTRFS_BLOCK_GROUP_TYPE_MASK |
553 BTRFS_BLOCK_GROUP_PROFILE_MASK) &
554 btrfs_chunk_type(leaf, chunk));
Qu Wenruobf871c32019-03-20 13:39:14 +0800555 return -EUCLEAN;
Qu Wenruo82fc28f2019-03-20 13:16:42 +0800556 }
557
Qu Wenruo80e46cf2019-03-13 12:17:50 +0800558 if (!is_power_of_2(type & BTRFS_BLOCK_GROUP_PROFILE_MASK) &&
559 (type & BTRFS_BLOCK_GROUP_PROFILE_MASK) != 0) {
David Sterbad001e4a2019-03-20 16:22:58 +0100560 chunk_err(leaf, chunk, logical,
Qu Wenruo80e46cf2019-03-13 12:17:50 +0800561 "invalid chunk profile flag: 0x%llx, expect 0 or 1 bit set",
562 type & BTRFS_BLOCK_GROUP_PROFILE_MASK);
563 return -EUCLEAN;
564 }
Qu Wenruo82fc28f2019-03-20 13:16:42 +0800565 if ((type & BTRFS_BLOCK_GROUP_TYPE_MASK) == 0) {
David Sterbad001e4a2019-03-20 16:22:58 +0100566 chunk_err(leaf, chunk, logical,
Qu Wenruof1140242019-03-20 13:36:06 +0800567 "missing chunk type flag, have 0x%llx one bit must be set in 0x%llx",
568 type, BTRFS_BLOCK_GROUP_TYPE_MASK);
Qu Wenruobf871c32019-03-20 13:39:14 +0800569 return -EUCLEAN;
Qu Wenruo82fc28f2019-03-20 13:16:42 +0800570 }
571
572 if ((type & BTRFS_BLOCK_GROUP_SYSTEM) &&
573 (type & (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA))) {
David Sterbad001e4a2019-03-20 16:22:58 +0100574 chunk_err(leaf, chunk, logical,
Qu Wenruof1140242019-03-20 13:36:06 +0800575 "system chunk with data or metadata type: 0x%llx",
576 type);
Qu Wenruobf871c32019-03-20 13:39:14 +0800577 return -EUCLEAN;
Qu Wenruo82fc28f2019-03-20 13:16:42 +0800578 }
579
580 features = btrfs_super_incompat_flags(fs_info->super_copy);
581 if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
582 mixed = true;
583
584 if (!mixed) {
585 if ((type & BTRFS_BLOCK_GROUP_METADATA) &&
586 (type & BTRFS_BLOCK_GROUP_DATA)) {
David Sterbad001e4a2019-03-20 16:22:58 +0100587 chunk_err(leaf, chunk, logical,
Qu Wenruo82fc28f2019-03-20 13:16:42 +0800588 "mixed chunk type in non-mixed mode: 0x%llx", type);
Qu Wenruobf871c32019-03-20 13:39:14 +0800589 return -EUCLEAN;
Qu Wenruo82fc28f2019-03-20 13:16:42 +0800590 }
591 }
592
593 if ((type & BTRFS_BLOCK_GROUP_RAID10 && sub_stripes != 2) ||
594 (type & BTRFS_BLOCK_GROUP_RAID1 && num_stripes != 2) ||
595 (type & BTRFS_BLOCK_GROUP_RAID5 && num_stripes < 2) ||
596 (type & BTRFS_BLOCK_GROUP_RAID6 && num_stripes < 3) ||
597 (type & BTRFS_BLOCK_GROUP_DUP && num_stripes != 2) ||
598 ((type & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 && num_stripes != 1)) {
David Sterbad001e4a2019-03-20 16:22:58 +0100599 chunk_err(leaf, chunk, logical,
Qu Wenruo82fc28f2019-03-20 13:16:42 +0800600 "invalid num_stripes:sub_stripes %u:%u for profile %llu",
601 num_stripes, sub_stripes,
602 type & BTRFS_BLOCK_GROUP_PROFILE_MASK);
Qu Wenruobf871c32019-03-20 13:39:14 +0800603 return -EUCLEAN;
Qu Wenruo82fc28f2019-03-20 13:16:42 +0800604 }
605
606 return 0;
607}
608
Qu Wenruoab4ba2e2019-03-08 14:20:03 +0800609__printf(4, 5)
610__cold
611static void dev_item_err(const struct btrfs_fs_info *fs_info,
612 const struct extent_buffer *eb, int slot,
613 const char *fmt, ...)
614{
615 struct btrfs_key key;
616 struct va_format vaf;
617 va_list args;
618
619 btrfs_item_key_to_cpu(eb, &key, slot);
620 va_start(args, fmt);
621
622 vaf.fmt = fmt;
623 vaf.va = &args;
624
625 btrfs_crit(fs_info,
626 "corrupt %s: root=%llu block=%llu slot=%d devid=%llu %pV",
627 btrfs_header_level(eb) == 0 ? "leaf" : "node",
628 btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot,
629 key.objectid, &vaf);
630 va_end(args);
631}
632
633static int check_dev_item(struct btrfs_fs_info *fs_info,
634 struct extent_buffer *leaf,
635 struct btrfs_key *key, int slot)
636{
637 struct btrfs_dev_item *ditem;
638 u64 max_devid = max(BTRFS_MAX_DEVS(fs_info), BTRFS_MAX_DEVS_SYS_CHUNK);
639
640 if (key->objectid != BTRFS_DEV_ITEMS_OBJECTID) {
641 dev_item_err(fs_info, leaf, slot,
642 "invalid objectid: has=%llu expect=%llu",
643 key->objectid, BTRFS_DEV_ITEMS_OBJECTID);
644 return -EUCLEAN;
645 }
646 if (key->offset > max_devid) {
647 dev_item_err(fs_info, leaf, slot,
648 "invalid devid: has=%llu expect=[0, %llu]",
649 key->offset, max_devid);
650 return -EUCLEAN;
651 }
652 ditem = btrfs_item_ptr(leaf, slot, struct btrfs_dev_item);
653 if (btrfs_device_id(leaf, ditem) != key->offset) {
654 dev_item_err(fs_info, leaf, slot,
655 "devid mismatch: key has=%llu item has=%llu",
656 key->offset, btrfs_device_id(leaf, ditem));
657 return -EUCLEAN;
658 }
659
660 /*
661 * For device total_bytes, we don't have reliable way to check it, as
662 * it can be 0 for device removal. Device size check can only be done
663 * by dev extents check.
664 */
665 if (btrfs_device_bytes_used(leaf, ditem) >
666 btrfs_device_total_bytes(leaf, ditem)) {
667 dev_item_err(fs_info, leaf, slot,
668 "invalid bytes used: have %llu expect [0, %llu]",
669 btrfs_device_bytes_used(leaf, ditem),
670 btrfs_device_total_bytes(leaf, ditem));
671 return -EUCLEAN;
672 }
673 /*
674 * Remaining members like io_align/type/gen/dev_group aren't really
675 * utilized. Skip them to make later usage of them easier.
676 */
677 return 0;
678}
679
Qu Wenruo496245c2019-03-13 14:31:35 +0800680/* Inode item error output has the same format as dir_item_err() */
681#define inode_item_err(fs_info, eb, slot, fmt, ...) \
David Sterbad98ced62019-03-20 16:07:27 +0100682 dir_item_err(eb, slot, fmt, __VA_ARGS__)
Qu Wenruo496245c2019-03-13 14:31:35 +0800683
684static int check_inode_item(struct btrfs_fs_info *fs_info,
685 struct extent_buffer *leaf,
686 struct btrfs_key *key, int slot)
687{
688 struct btrfs_inode_item *iitem;
689 u64 super_gen = btrfs_super_generation(fs_info->super_copy);
690 u32 valid_mask = (S_IFMT | S_ISUID | S_ISGID | S_ISVTX | 0777);
691 u32 mode;
692
693 if ((key->objectid < BTRFS_FIRST_FREE_OBJECTID ||
694 key->objectid > BTRFS_LAST_FREE_OBJECTID) &&
695 key->objectid != BTRFS_ROOT_TREE_DIR_OBJECTID &&
696 key->objectid != BTRFS_FREE_INO_OBJECTID) {
David Sterba86a6be32019-03-20 15:31:28 +0100697 generic_err(leaf, slot,
Qu Wenruo496245c2019-03-13 14:31:35 +0800698 "invalid key objectid: has %llu expect %llu or [%llu, %llu] or %llu",
699 key->objectid, BTRFS_ROOT_TREE_DIR_OBJECTID,
700 BTRFS_FIRST_FREE_OBJECTID,
701 BTRFS_LAST_FREE_OBJECTID,
702 BTRFS_FREE_INO_OBJECTID);
703 return -EUCLEAN;
704 }
705 if (key->offset != 0) {
706 inode_item_err(fs_info, leaf, slot,
707 "invalid key offset: has %llu expect 0",
708 key->offset);
709 return -EUCLEAN;
710 }
711 iitem = btrfs_item_ptr(leaf, slot, struct btrfs_inode_item);
712
713 /* Here we use super block generation + 1 to handle log tree */
714 if (btrfs_inode_generation(leaf, iitem) > super_gen + 1) {
715 inode_item_err(fs_info, leaf, slot,
716 "invalid inode generation: has %llu expect (0, %llu]",
717 btrfs_inode_generation(leaf, iitem),
718 super_gen + 1);
719 return -EUCLEAN;
720 }
721 /* Note for ROOT_TREE_DIR_ITEM, mkfs could set its transid 0 */
722 if (btrfs_inode_transid(leaf, iitem) > super_gen + 1) {
723 inode_item_err(fs_info, leaf, slot,
724 "invalid inode generation: has %llu expect [0, %llu]",
725 btrfs_inode_transid(leaf, iitem), super_gen + 1);
726 return -EUCLEAN;
727 }
728
729 /*
730 * For size and nbytes it's better not to be too strict, as for dir
731 * item its size/nbytes can easily get wrong, but doesn't affect
732 * anything in the fs. So here we skip the check.
733 */
734 mode = btrfs_inode_mode(leaf, iitem);
735 if (mode & ~valid_mask) {
736 inode_item_err(fs_info, leaf, slot,
737 "unknown mode bit detected: 0x%x",
738 mode & ~valid_mask);
739 return -EUCLEAN;
740 }
741
742 /*
743 * S_IFMT is not bit mapped so we can't completely rely on is_power_of_2,
744 * but is_power_of_2() can save us from checking FIFO/CHR/DIR/REG.
745 * Only needs to check BLK, LNK and SOCKS
746 */
747 if (!is_power_of_2(mode & S_IFMT)) {
748 if (!S_ISLNK(mode) && !S_ISBLK(mode) && !S_ISSOCK(mode)) {
749 inode_item_err(fs_info, leaf, slot,
750 "invalid mode: has 0%o expect valid S_IF* bit(s)",
751 mode & S_IFMT);
752 return -EUCLEAN;
753 }
754 }
755 if (S_ISDIR(mode) && btrfs_inode_nlink(leaf, iitem) > 1) {
756 inode_item_err(fs_info, leaf, slot,
757 "invalid nlink: has %u expect no more than 1 for dir",
758 btrfs_inode_nlink(leaf, iitem));
759 return -EUCLEAN;
760 }
761 if (btrfs_inode_flags(leaf, iitem) & ~BTRFS_INODE_FLAG_MASK) {
762 inode_item_err(fs_info, leaf, slot,
763 "unknown flags detected: 0x%llx",
764 btrfs_inode_flags(leaf, iitem) &
765 ~BTRFS_INODE_FLAG_MASK);
766 return -EUCLEAN;
767 }
768 return 0;
769}
770
Qu Wenruo82fc28f2019-03-20 13:16:42 +0800771/*
Qu Wenruo557ea5d2017-10-09 01:51:02 +0000772 * Common point to switch the item-specific validation.
773 */
David Sterba0076bc892019-03-20 16:22:00 +0100774static int check_leaf_item(struct extent_buffer *leaf,
Qu Wenruo557ea5d2017-10-09 01:51:02 +0000775 struct btrfs_key *key, int slot)
776{
777 int ret = 0;
Qu Wenruo075cb3c2019-03-20 13:42:33 +0800778 struct btrfs_chunk *chunk;
Qu Wenruo557ea5d2017-10-09 01:51:02 +0000779
780 switch (key->type) {
781 case BTRFS_EXTENT_DATA_KEY:
David Sterbaae2a19d2019-03-20 16:21:10 +0100782 ret = check_extent_data_item(leaf, key, slot);
Qu Wenruo557ea5d2017-10-09 01:51:02 +0000783 break;
784 case BTRFS_EXTENT_CSUM_KEY:
David Sterba68128ce2019-03-20 16:02:56 +0100785 ret = check_csum_item(leaf, key, slot);
Qu Wenruo557ea5d2017-10-09 01:51:02 +0000786 break;
Qu Wenruoad7b03682017-11-08 08:54:25 +0800787 case BTRFS_DIR_ITEM_KEY:
788 case BTRFS_DIR_INDEX_KEY:
789 case BTRFS_XATTR_ITEM_KEY:
David Sterbace4252c2019-03-20 16:17:46 +0100790 ret = check_dir_item(leaf, key, slot);
Qu Wenruoad7b03682017-11-08 08:54:25 +0800791 break;
Qu Wenruofce466e2018-07-03 17:10:05 +0800792 case BTRFS_BLOCK_GROUP_ITEM_KEY:
David Sterbaaf60ce22019-03-20 16:19:31 +0100793 ret = check_block_group_item(leaf, key, slot);
Qu Wenruofce466e2018-07-03 17:10:05 +0800794 break;
Qu Wenruo075cb3c2019-03-20 13:42:33 +0800795 case BTRFS_CHUNK_ITEM_KEY:
796 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
David Sterba0076bc892019-03-20 16:22:00 +0100797 ret = btrfs_check_chunk_valid(leaf->fs_info, leaf, chunk,
Qu Wenruo075cb3c2019-03-20 13:42:33 +0800798 key->offset);
799 break;
Qu Wenruoab4ba2e2019-03-08 14:20:03 +0800800 case BTRFS_DEV_ITEM_KEY:
David Sterba0076bc892019-03-20 16:22:00 +0100801 ret = check_dev_item(leaf->fs_info, leaf, key, slot);
Qu Wenruoab4ba2e2019-03-08 14:20:03 +0800802 break;
Qu Wenruo496245c2019-03-13 14:31:35 +0800803 case BTRFS_INODE_ITEM_KEY:
David Sterba0076bc892019-03-20 16:22:00 +0100804 ret = check_inode_item(leaf->fs_info, leaf, key, slot);
Qu Wenruo496245c2019-03-13 14:31:35 +0800805 break;
Qu Wenruo557ea5d2017-10-09 01:51:02 +0000806 }
807 return ret;
808}
809
David Sterbae2ccd362019-03-20 16:22:58 +0100810static int check_leaf(struct extent_buffer *leaf, bool check_item_data)
Qu Wenruo557ea5d2017-10-09 01:51:02 +0000811{
David Sterbae2ccd362019-03-20 16:22:58 +0100812 struct btrfs_fs_info *fs_info = leaf->fs_info;
Qu Wenruo557ea5d2017-10-09 01:51:02 +0000813 /* No valid key type is 0, so all key should be larger than this key */
814 struct btrfs_key prev_key = {0, 0, 0};
815 struct btrfs_key key;
816 u32 nritems = btrfs_header_nritems(leaf);
817 int slot;
818
Qu Wenruof556faa2018-09-28 07:59:34 +0800819 if (btrfs_header_level(leaf) != 0) {
David Sterba86a6be32019-03-20 15:31:28 +0100820 generic_err(leaf, 0,
Qu Wenruof556faa2018-09-28 07:59:34 +0800821 "invalid level for leaf, have %d expect 0",
822 btrfs_header_level(leaf));
823 return -EUCLEAN;
824 }
825
Qu Wenruo557ea5d2017-10-09 01:51:02 +0000826 /*
827 * Extent buffers from a relocation tree have a owner field that
828 * corresponds to the subvolume tree they are based on. So just from an
829 * extent buffer alone we can not find out what is the id of the
830 * corresponding subvolume tree, so we can not figure out if the extent
831 * buffer corresponds to the root of the relocation tree or not. So
832 * skip this check for relocation trees.
833 */
834 if (nritems == 0 && !btrfs_header_flag(leaf, BTRFS_HEADER_FLAG_RELOC)) {
Qu Wenruoba480dd2018-07-03 17:10:06 +0800835 u64 owner = btrfs_header_owner(leaf);
Qu Wenruo557ea5d2017-10-09 01:51:02 +0000836 struct btrfs_root *check_root;
837
Qu Wenruoba480dd2018-07-03 17:10:06 +0800838 /* These trees must never be empty */
839 if (owner == BTRFS_ROOT_TREE_OBJECTID ||
840 owner == BTRFS_CHUNK_TREE_OBJECTID ||
841 owner == BTRFS_EXTENT_TREE_OBJECTID ||
842 owner == BTRFS_DEV_TREE_OBJECTID ||
843 owner == BTRFS_FS_TREE_OBJECTID ||
844 owner == BTRFS_DATA_RELOC_TREE_OBJECTID) {
David Sterba86a6be32019-03-20 15:31:28 +0100845 generic_err(leaf, 0,
Qu Wenruoba480dd2018-07-03 17:10:06 +0800846 "invalid root, root %llu must never be empty",
847 owner);
848 return -EUCLEAN;
849 }
850 key.objectid = owner;
Qu Wenruo557ea5d2017-10-09 01:51:02 +0000851 key.type = BTRFS_ROOT_ITEM_KEY;
852 key.offset = (u64)-1;
853
854 check_root = btrfs_get_fs_root(fs_info, &key, false);
855 /*
856 * The only reason we also check NULL here is that during
857 * open_ctree() some roots has not yet been set up.
858 */
859 if (!IS_ERR_OR_NULL(check_root)) {
860 struct extent_buffer *eb;
861
862 eb = btrfs_root_node(check_root);
863 /* if leaf is the root, then it's fine */
864 if (leaf != eb) {
David Sterba86a6be32019-03-20 15:31:28 +0100865 generic_err(leaf, 0,
Qu Wenruo478d01b2017-10-09 01:51:04 +0000866 "invalid nritems, have %u should not be 0 for non-root leaf",
867 nritems);
Qu Wenruo557ea5d2017-10-09 01:51:02 +0000868 free_extent_buffer(eb);
869 return -EUCLEAN;
870 }
871 free_extent_buffer(eb);
872 }
873 return 0;
874 }
875
876 if (nritems == 0)
877 return 0;
878
879 /*
880 * Check the following things to make sure this is a good leaf, and
881 * leaf users won't need to bother with similar sanity checks:
882 *
883 * 1) key ordering
884 * 2) item offset and size
885 * No overlap, no hole, all inside the leaf.
886 * 3) item content
887 * If possible, do comprehensive sanity check.
888 * NOTE: All checks must only rely on the item data itself.
889 */
890 for (slot = 0; slot < nritems; slot++) {
891 u32 item_end_expected;
892 int ret;
893
894 btrfs_item_key_to_cpu(leaf, &key, slot);
895
896 /* Make sure the keys are in the right order */
897 if (btrfs_comp_cpu_keys(&prev_key, &key) >= 0) {
David Sterba86a6be32019-03-20 15:31:28 +0100898 generic_err(leaf, slot,
Qu Wenruo478d01b2017-10-09 01:51:04 +0000899 "bad key order, prev (%llu %u %llu) current (%llu %u %llu)",
900 prev_key.objectid, prev_key.type,
901 prev_key.offset, key.objectid, key.type,
902 key.offset);
Qu Wenruo557ea5d2017-10-09 01:51:02 +0000903 return -EUCLEAN;
904 }
905
906 /*
907 * Make sure the offset and ends are right, remember that the
908 * item data starts at the end of the leaf and grows towards the
909 * front.
910 */
911 if (slot == 0)
912 item_end_expected = BTRFS_LEAF_DATA_SIZE(fs_info);
913 else
914 item_end_expected = btrfs_item_offset_nr(leaf,
915 slot - 1);
916 if (btrfs_item_end_nr(leaf, slot) != item_end_expected) {
David Sterba86a6be32019-03-20 15:31:28 +0100917 generic_err(leaf, slot,
Qu Wenruo478d01b2017-10-09 01:51:04 +0000918 "unexpected item end, have %u expect %u",
919 btrfs_item_end_nr(leaf, slot),
920 item_end_expected);
Qu Wenruo557ea5d2017-10-09 01:51:02 +0000921 return -EUCLEAN;
922 }
923
924 /*
925 * Check to make sure that we don't point outside of the leaf,
926 * just in case all the items are consistent to each other, but
927 * all point outside of the leaf.
928 */
929 if (btrfs_item_end_nr(leaf, slot) >
930 BTRFS_LEAF_DATA_SIZE(fs_info)) {
David Sterba86a6be32019-03-20 15:31:28 +0100931 generic_err(leaf, slot,
Qu Wenruo478d01b2017-10-09 01:51:04 +0000932 "slot end outside of leaf, have %u expect range [0, %u]",
933 btrfs_item_end_nr(leaf, slot),
934 BTRFS_LEAF_DATA_SIZE(fs_info));
Qu Wenruo557ea5d2017-10-09 01:51:02 +0000935 return -EUCLEAN;
936 }
937
938 /* Also check if the item pointer overlaps with btrfs item. */
939 if (btrfs_item_nr_offset(slot) + sizeof(struct btrfs_item) >
940 btrfs_item_ptr_offset(leaf, slot)) {
David Sterba86a6be32019-03-20 15:31:28 +0100941 generic_err(leaf, slot,
Qu Wenruo478d01b2017-10-09 01:51:04 +0000942 "slot overlaps with its data, item end %lu data start %lu",
943 btrfs_item_nr_offset(slot) +
944 sizeof(struct btrfs_item),
945 btrfs_item_ptr_offset(leaf, slot));
Qu Wenruo557ea5d2017-10-09 01:51:02 +0000946 return -EUCLEAN;
947 }
948
Qu Wenruo69fc6cb2017-11-08 08:54:24 +0800949 if (check_item_data) {
950 /*
951 * Check if the item size and content meet other
952 * criteria
953 */
David Sterba0076bc892019-03-20 16:22:00 +0100954 ret = check_leaf_item(leaf, &key, slot);
Qu Wenruo69fc6cb2017-11-08 08:54:24 +0800955 if (ret < 0)
956 return ret;
957 }
Qu Wenruo557ea5d2017-10-09 01:51:02 +0000958
959 prev_key.objectid = key.objectid;
960 prev_key.type = key.type;
961 prev_key.offset = key.offset;
962 }
963
964 return 0;
965}
966
Qu Wenruo2f659542018-01-25 14:56:18 +0800967int btrfs_check_leaf_full(struct btrfs_fs_info *fs_info,
968 struct extent_buffer *leaf)
Qu Wenruo69fc6cb2017-11-08 08:54:24 +0800969{
David Sterbae2ccd362019-03-20 16:22:58 +0100970 return check_leaf(leaf, true);
Qu Wenruo69fc6cb2017-11-08 08:54:24 +0800971}
972
Qu Wenruo2f659542018-01-25 14:56:18 +0800973int btrfs_check_leaf_relaxed(struct btrfs_fs_info *fs_info,
Qu Wenruo69fc6cb2017-11-08 08:54:24 +0800974 struct extent_buffer *leaf)
975{
David Sterbae2ccd362019-03-20 16:22:58 +0100976 return check_leaf(leaf, false);
Qu Wenruo69fc6cb2017-11-08 08:54:24 +0800977}
978
Qu Wenruo2f659542018-01-25 14:56:18 +0800979int btrfs_check_node(struct btrfs_fs_info *fs_info, struct extent_buffer *node)
Qu Wenruo557ea5d2017-10-09 01:51:02 +0000980{
981 unsigned long nr = btrfs_header_nritems(node);
982 struct btrfs_key key, next_key;
983 int slot;
Qu Wenruof556faa2018-09-28 07:59:34 +0800984 int level = btrfs_header_level(node);
Qu Wenruo557ea5d2017-10-09 01:51:02 +0000985 u64 bytenr;
986 int ret = 0;
987
Qu Wenruof556faa2018-09-28 07:59:34 +0800988 if (level <= 0 || level >= BTRFS_MAX_LEVEL) {
David Sterba86a6be32019-03-20 15:31:28 +0100989 generic_err(node, 0,
Qu Wenruof556faa2018-09-28 07:59:34 +0800990 "invalid level for node, have %d expect [1, %d]",
991 level, BTRFS_MAX_LEVEL - 1);
992 return -EUCLEAN;
993 }
Qu Wenruo2f659542018-01-25 14:56:18 +0800994 if (nr == 0 || nr > BTRFS_NODEPTRS_PER_BLOCK(fs_info)) {
995 btrfs_crit(fs_info,
Qu Wenruobba4f292017-10-09 01:51:03 +0000996"corrupt node: root=%llu block=%llu, nritems too %s, have %lu expect range [1,%u]",
Qu Wenruo2f659542018-01-25 14:56:18 +0800997 btrfs_header_owner(node), node->start,
Qu Wenruobba4f292017-10-09 01:51:03 +0000998 nr == 0 ? "small" : "large", nr,
Qu Wenruo2f659542018-01-25 14:56:18 +0800999 BTRFS_NODEPTRS_PER_BLOCK(fs_info));
Qu Wenruobba4f292017-10-09 01:51:03 +00001000 return -EUCLEAN;
Qu Wenruo557ea5d2017-10-09 01:51:02 +00001001 }
1002
1003 for (slot = 0; slot < nr - 1; slot++) {
1004 bytenr = btrfs_node_blockptr(node, slot);
1005 btrfs_node_key_to_cpu(node, &key, slot);
1006 btrfs_node_key_to_cpu(node, &next_key, slot + 1);
1007
1008 if (!bytenr) {
David Sterba86a6be32019-03-20 15:31:28 +01001009 generic_err(node, slot,
Qu Wenruobba4f292017-10-09 01:51:03 +00001010 "invalid NULL node pointer");
1011 ret = -EUCLEAN;
1012 goto out;
1013 }
Qu Wenruo2f659542018-01-25 14:56:18 +08001014 if (!IS_ALIGNED(bytenr, fs_info->sectorsize)) {
David Sterba86a6be32019-03-20 15:31:28 +01001015 generic_err(node, slot,
Qu Wenruobba4f292017-10-09 01:51:03 +00001016 "unaligned pointer, have %llu should be aligned to %u",
Qu Wenruo2f659542018-01-25 14:56:18 +08001017 bytenr, fs_info->sectorsize);
Qu Wenruobba4f292017-10-09 01:51:03 +00001018 ret = -EUCLEAN;
Qu Wenruo557ea5d2017-10-09 01:51:02 +00001019 goto out;
1020 }
1021
1022 if (btrfs_comp_cpu_keys(&key, &next_key) >= 0) {
David Sterba86a6be32019-03-20 15:31:28 +01001023 generic_err(node, slot,
Qu Wenruobba4f292017-10-09 01:51:03 +00001024 "bad key order, current (%llu %u %llu) next (%llu %u %llu)",
1025 key.objectid, key.type, key.offset,
1026 next_key.objectid, next_key.type,
1027 next_key.offset);
1028 ret = -EUCLEAN;
Qu Wenruo557ea5d2017-10-09 01:51:02 +00001029 goto out;
1030 }
1031 }
1032out:
1033 return ret;
1034}