blob: 9699c91c958eabf64d7b34e405d2038729e546e4 [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
Qu Wenruo02529d72019-04-24 15:22:53 +080018#include <linux/types.h>
19#include <linux/stddef.h>
20#include <linux/error-injection.h>
Qu Wenruo557ea5d2017-10-09 01:51:02 +000021#include "ctree.h"
22#include "tree-checker.h"
23#include "disk-io.h"
24#include "compression.h"
Qu Wenruofce466e2018-07-03 17:10:05 +080025#include "volumes.h"
David Sterbac1499162019-10-01 19:44:42 +020026#include "misc.h"
Qu Wenruo557ea5d2017-10-09 01:51:02 +000027
Qu Wenruobba4f292017-10-09 01:51:03 +000028/*
29 * Error message should follow the following format:
30 * corrupt <type>: <identifier>, <reason>[, <bad_value>]
31 *
32 * @type: leaf or node
33 * @identifier: the necessary info to locate the leaf/node.
Andrea Gelmini52042d82018-11-28 12:05:13 +010034 * It's recommended to decode key.objecitd/offset if it's
Qu Wenruobba4f292017-10-09 01:51:03 +000035 * meaningful.
36 * @reason: describe the error
Andrea Gelmini52042d82018-11-28 12:05:13 +010037 * @bad_value: optional, it's recommended to output bad value and its
Qu Wenruobba4f292017-10-09 01:51:03 +000038 * expected value (range).
39 *
40 * Since comma is used to separate the components, only space is allowed
41 * inside each component.
42 */
43
44/*
45 * Append generic "corrupt leaf/node root=%llu block=%llu slot=%d: " to @fmt.
46 * Allows callers to customize the output.
47 */
David Sterba86a6be32019-03-20 15:31:28 +010048__printf(3, 4)
David Sterbae67c7182018-02-19 17:24:18 +010049__cold
David Sterba86a6be32019-03-20 15:31:28 +010050static void generic_err(const struct extent_buffer *eb, int slot,
Qu Wenruobba4f292017-10-09 01:51:03 +000051 const char *fmt, ...)
52{
David Sterba86a6be32019-03-20 15:31:28 +010053 const struct btrfs_fs_info *fs_info = eb->fs_info;
Qu Wenruobba4f292017-10-09 01:51:03 +000054 struct va_format vaf;
55 va_list args;
56
57 va_start(args, fmt);
58
59 vaf.fmt = fmt;
60 vaf.va = &args;
61
Qu Wenruo2f659542018-01-25 14:56:18 +080062 btrfs_crit(fs_info,
Qu Wenruobba4f292017-10-09 01:51:03 +000063 "corrupt %s: root=%llu block=%llu slot=%d, %pV",
64 btrfs_header_level(eb) == 0 ? "leaf" : "node",
Qu Wenruo2f659542018-01-25 14:56:18 +080065 btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot, &vaf);
Qu Wenruobba4f292017-10-09 01:51:03 +000066 va_end(args);
67}
68
Qu Wenruo8806d712017-10-09 01:51:06 +000069/*
70 * Customized reporter for extent data item, since its key objectid and
71 * offset has its own meaning.
72 */
David Sterba1fd715f2019-03-20 15:32:46 +010073__printf(3, 4)
David Sterbae67c7182018-02-19 17:24:18 +010074__cold
David Sterba1fd715f2019-03-20 15:32:46 +010075static void file_extent_err(const struct extent_buffer *eb, int slot,
Qu Wenruo8806d712017-10-09 01:51:06 +000076 const char *fmt, ...)
77{
David Sterba1fd715f2019-03-20 15:32:46 +010078 const struct btrfs_fs_info *fs_info = eb->fs_info;
Qu Wenruo8806d712017-10-09 01:51:06 +000079 struct btrfs_key key;
80 struct va_format vaf;
81 va_list args;
82
83 btrfs_item_key_to_cpu(eb, &key, slot);
84 va_start(args, fmt);
85
86 vaf.fmt = fmt;
87 vaf.va = &args;
88
Qu Wenruo2f659542018-01-25 14:56:18 +080089 btrfs_crit(fs_info,
Qu Wenruo8806d712017-10-09 01:51:06 +000090 "corrupt %s: root=%llu block=%llu slot=%d ino=%llu file_offset=%llu, %pV",
Qu Wenruo2f659542018-01-25 14:56:18 +080091 btrfs_header_level(eb) == 0 ? "leaf" : "node",
92 btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot,
93 key.objectid, key.offset, &vaf);
Qu Wenruo8806d712017-10-09 01:51:06 +000094 va_end(args);
95}
96
97/*
98 * Return 0 if the btrfs_file_extent_##name is aligned to @alignment
99 * Else return 1
100 */
David Sterba033774d2019-03-20 15:59:22 +0100101#define CHECK_FE_ALIGNED(leaf, slot, fi, name, alignment) \
Qu Wenruo8806d712017-10-09 01:51:06 +0000102({ \
103 if (!IS_ALIGNED(btrfs_file_extent_##name((leaf), (fi)), (alignment))) \
David Sterba1fd715f2019-03-20 15:32:46 +0100104 file_extent_err((leaf), (slot), \
Qu Wenruo8806d712017-10-09 01:51:06 +0000105 "invalid %s for file extent, have %llu, should be aligned to %u", \
106 (#name), btrfs_file_extent_##name((leaf), (fi)), \
107 (alignment)); \
108 (!IS_ALIGNED(btrfs_file_extent_##name((leaf), (fi)), (alignment))); \
109})
110
Filipe Manana4e9845e2019-05-06 16:44:12 +0100111static u64 file_extent_end(struct extent_buffer *leaf,
112 struct btrfs_key *key,
113 struct btrfs_file_extent_item *extent)
114{
115 u64 end;
116 u64 len;
117
118 if (btrfs_file_extent_type(leaf, extent) == BTRFS_FILE_EXTENT_INLINE) {
119 len = btrfs_file_extent_ram_bytes(leaf, extent);
120 end = ALIGN(key->offset + len, leaf->fs_info->sectorsize);
121 } else {
122 len = btrfs_file_extent_num_bytes(leaf, extent);
123 end = key->offset + len;
124 }
125 return end;
126}
127
Qu Wenruo80d7fd12019-10-04 17:31:32 +0800128/*
129 * Customized report for dir_item, the only new important information is
130 * key->objectid, which represents inode number
131 */
132__printf(3, 4)
133__cold
134static void dir_item_err(const struct extent_buffer *eb, int slot,
135 const char *fmt, ...)
136{
137 const struct btrfs_fs_info *fs_info = eb->fs_info;
138 struct btrfs_key key;
139 struct va_format vaf;
140 va_list args;
141
142 btrfs_item_key_to_cpu(eb, &key, slot);
143 va_start(args, fmt);
144
145 vaf.fmt = fmt;
146 vaf.va = &args;
147
148 btrfs_crit(fs_info,
149 "corrupt %s: root=%llu block=%llu slot=%d ino=%llu, %pV",
150 btrfs_header_level(eb) == 0 ? "leaf" : "node",
151 btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot,
152 key.objectid, &vaf);
153 va_end(args);
154}
155
156/*
157 * This functions checks prev_key->objectid, to ensure current key and prev_key
158 * share the same objectid as inode number.
159 *
160 * This is to detect missing INODE_ITEM in subvolume trees.
161 *
162 * Return true if everything is OK or we don't need to check.
163 * Return false if anything is wrong.
164 */
165static bool check_prev_ino(struct extent_buffer *leaf,
166 struct btrfs_key *key, int slot,
167 struct btrfs_key *prev_key)
168{
169 /* No prev key, skip check */
170 if (slot == 0)
171 return true;
172
173 /* Only these key->types needs to be checked */
174 ASSERT(key->type == BTRFS_XATTR_ITEM_KEY ||
175 key->type == BTRFS_INODE_REF_KEY ||
176 key->type == BTRFS_DIR_INDEX_KEY ||
177 key->type == BTRFS_DIR_ITEM_KEY ||
178 key->type == BTRFS_EXTENT_DATA_KEY);
179
180 /*
181 * Only subvolume trees along with their reloc trees need this check.
182 * Things like log tree doesn't follow this ino requirement.
183 */
184 if (!is_fstree(btrfs_header_owner(leaf)))
185 return true;
186
187 if (key->objectid == prev_key->objectid)
188 return true;
189
190 /* Error found */
191 dir_item_err(leaf, slot,
192 "invalid previous key objectid, have %llu expect %llu",
193 prev_key->objectid, key->objectid);
194 return false;
195}
David Sterbaae2a19d2019-03-20 16:21:10 +0100196static int check_extent_data_item(struct extent_buffer *leaf,
Filipe Manana4e9845e2019-05-06 16:44:12 +0100197 struct btrfs_key *key, int slot,
198 struct btrfs_key *prev_key)
Qu Wenruo557ea5d2017-10-09 01:51:02 +0000199{
David Sterbaae2a19d2019-03-20 16:21:10 +0100200 struct btrfs_fs_info *fs_info = leaf->fs_info;
Qu Wenruo557ea5d2017-10-09 01:51:02 +0000201 struct btrfs_file_extent_item *fi;
Qu Wenruo2f659542018-01-25 14:56:18 +0800202 u32 sectorsize = fs_info->sectorsize;
Qu Wenruo557ea5d2017-10-09 01:51:02 +0000203 u32 item_size = btrfs_item_size_nr(leaf, slot);
Qu Wenruo4c094c32019-05-03 08:30:54 +0800204 u64 extent_end;
Qu Wenruo557ea5d2017-10-09 01:51:02 +0000205
206 if (!IS_ALIGNED(key->offset, sectorsize)) {
David Sterba1fd715f2019-03-20 15:32:46 +0100207 file_extent_err(leaf, slot,
Qu Wenruo8806d712017-10-09 01:51:06 +0000208"unaligned file_offset for file extent, have %llu should be aligned to %u",
209 key->offset, sectorsize);
Qu Wenruo557ea5d2017-10-09 01:51:02 +0000210 return -EUCLEAN;
211 }
212
Qu Wenruoc18679e2019-08-26 15:40:38 +0800213 /*
214 * Previous key must have the same key->objectid (ino).
215 * It can be XATTR_ITEM, INODE_ITEM or just another EXTENT_DATA.
216 * But if objectids mismatch, it means we have a missing
217 * INODE_ITEM.
218 */
Qu Wenruo80d7fd12019-10-04 17:31:32 +0800219 if (!check_prev_ino(leaf, key, slot, prev_key))
Qu Wenruoc18679e2019-08-26 15:40:38 +0800220 return -EUCLEAN;
Qu Wenruoc18679e2019-08-26 15:40:38 +0800221
Qu Wenruo557ea5d2017-10-09 01:51:02 +0000222 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
223
Chengguang Xub9b1a532019-10-10 15:59:58 +0800224 if (btrfs_file_extent_type(leaf, fi) >= BTRFS_NR_FILE_EXTENT_TYPES) {
David Sterba1fd715f2019-03-20 15:32:46 +0100225 file_extent_err(leaf, slot,
Qu Wenruo8806d712017-10-09 01:51:06 +0000226 "invalid type for file extent, have %u expect range [0, %u]",
227 btrfs_file_extent_type(leaf, fi),
Chengguang Xub9b1a532019-10-10 15:59:58 +0800228 BTRFS_NR_FILE_EXTENT_TYPES - 1);
Qu Wenruo557ea5d2017-10-09 01:51:02 +0000229 return -EUCLEAN;
230 }
231
232 /*
Andrea Gelmini52042d82018-11-28 12:05:13 +0100233 * Support for new compression/encryption must introduce incompat flag,
Qu Wenruo557ea5d2017-10-09 01:51:02 +0000234 * and must be caught in open_ctree().
235 */
236 if (btrfs_file_extent_compression(leaf, fi) > BTRFS_COMPRESS_TYPES) {
David Sterba1fd715f2019-03-20 15:32:46 +0100237 file_extent_err(leaf, slot,
Qu Wenruo8806d712017-10-09 01:51:06 +0000238 "invalid compression for file extent, have %u expect range [0, %u]",
239 btrfs_file_extent_compression(leaf, fi),
240 BTRFS_COMPRESS_TYPES);
Qu Wenruo557ea5d2017-10-09 01:51:02 +0000241 return -EUCLEAN;
242 }
243 if (btrfs_file_extent_encryption(leaf, fi)) {
David Sterba1fd715f2019-03-20 15:32:46 +0100244 file_extent_err(leaf, slot,
Qu Wenruo8806d712017-10-09 01:51:06 +0000245 "invalid encryption for file extent, have %u expect 0",
246 btrfs_file_extent_encryption(leaf, fi));
Qu Wenruo557ea5d2017-10-09 01:51:02 +0000247 return -EUCLEAN;
248 }
249 if (btrfs_file_extent_type(leaf, fi) == BTRFS_FILE_EXTENT_INLINE) {
250 /* Inline extent must have 0 as key offset */
251 if (key->offset) {
David Sterba1fd715f2019-03-20 15:32:46 +0100252 file_extent_err(leaf, slot,
Qu Wenruo8806d712017-10-09 01:51:06 +0000253 "invalid file_offset for inline file extent, have %llu expect 0",
254 key->offset);
Qu Wenruo557ea5d2017-10-09 01:51:02 +0000255 return -EUCLEAN;
256 }
257
258 /* Compressed inline extent has no on-disk size, skip it */
259 if (btrfs_file_extent_compression(leaf, fi) !=
260 BTRFS_COMPRESS_NONE)
261 return 0;
262
263 /* Uncompressed inline extent size must match item size */
264 if (item_size != BTRFS_FILE_EXTENT_INLINE_DATA_START +
265 btrfs_file_extent_ram_bytes(leaf, fi)) {
David Sterba1fd715f2019-03-20 15:32:46 +0100266 file_extent_err(leaf, slot,
Qu Wenruo8806d712017-10-09 01:51:06 +0000267 "invalid ram_bytes for uncompressed inline extent, have %u expect %llu",
268 item_size, BTRFS_FILE_EXTENT_INLINE_DATA_START +
269 btrfs_file_extent_ram_bytes(leaf, fi));
Qu Wenruo557ea5d2017-10-09 01:51:02 +0000270 return -EUCLEAN;
271 }
272 return 0;
273 }
274
275 /* Regular or preallocated extent has fixed item size */
276 if (item_size != sizeof(*fi)) {
David Sterba1fd715f2019-03-20 15:32:46 +0100277 file_extent_err(leaf, slot,
Arnd Bergmann709a95c2017-10-13 11:27:35 +0200278 "invalid item size for reg/prealloc file extent, have %u expect %zu",
Qu Wenruo8806d712017-10-09 01:51:06 +0000279 item_size, sizeof(*fi));
Qu Wenruo557ea5d2017-10-09 01:51:02 +0000280 return -EUCLEAN;
281 }
David Sterba033774d2019-03-20 15:59:22 +0100282 if (CHECK_FE_ALIGNED(leaf, slot, fi, ram_bytes, sectorsize) ||
283 CHECK_FE_ALIGNED(leaf, slot, fi, disk_bytenr, sectorsize) ||
284 CHECK_FE_ALIGNED(leaf, slot, fi, disk_num_bytes, sectorsize) ||
285 CHECK_FE_ALIGNED(leaf, slot, fi, offset, sectorsize) ||
286 CHECK_FE_ALIGNED(leaf, slot, fi, num_bytes, sectorsize))
Qu Wenruo557ea5d2017-10-09 01:51:02 +0000287 return -EUCLEAN;
Filipe Manana4e9845e2019-05-06 16:44:12 +0100288
Qu Wenruo4c094c32019-05-03 08:30:54 +0800289 /* Catch extent end overflow */
290 if (check_add_overflow(btrfs_file_extent_num_bytes(leaf, fi),
291 key->offset, &extent_end)) {
292 file_extent_err(leaf, slot,
293 "extent end overflow, have file offset %llu extent num bytes %llu",
294 key->offset,
295 btrfs_file_extent_num_bytes(leaf, fi));
296 return -EUCLEAN;
297 }
298
Filipe Manana4e9845e2019-05-06 16:44:12 +0100299 /*
300 * Check that no two consecutive file extent items, in the same leaf,
301 * present ranges that overlap each other.
302 */
303 if (slot > 0 &&
304 prev_key->objectid == key->objectid &&
305 prev_key->type == BTRFS_EXTENT_DATA_KEY) {
306 struct btrfs_file_extent_item *prev_fi;
307 u64 prev_end;
308
309 prev_fi = btrfs_item_ptr(leaf, slot - 1,
310 struct btrfs_file_extent_item);
311 prev_end = file_extent_end(leaf, prev_key, prev_fi);
312 if (prev_end > key->offset) {
313 file_extent_err(leaf, slot - 1,
314"file extent end range (%llu) goes beyond start offset (%llu) of the next file extent",
315 prev_end, key->offset);
316 return -EUCLEAN;
317 }
318 }
319
Qu Wenruo557ea5d2017-10-09 01:51:02 +0000320 return 0;
321}
322
David Sterba68128ce2019-03-20 16:02:56 +0100323static int check_csum_item(struct extent_buffer *leaf, struct btrfs_key *key,
Qu Wenruo2f659542018-01-25 14:56:18 +0800324 int slot)
Qu Wenruo557ea5d2017-10-09 01:51:02 +0000325{
David Sterba68128ce2019-03-20 16:02:56 +0100326 struct btrfs_fs_info *fs_info = leaf->fs_info;
Qu Wenruo2f659542018-01-25 14:56:18 +0800327 u32 sectorsize = fs_info->sectorsize;
328 u32 csumsize = btrfs_super_csum_size(fs_info->super_copy);
Qu Wenruo557ea5d2017-10-09 01:51:02 +0000329
330 if (key->objectid != BTRFS_EXTENT_CSUM_OBJECTID) {
David Sterba86a6be32019-03-20 15:31:28 +0100331 generic_err(leaf, slot,
Qu Wenruod508c5f2017-10-09 01:51:05 +0000332 "invalid key objectid for csum item, have %llu expect %llu",
333 key->objectid, BTRFS_EXTENT_CSUM_OBJECTID);
Qu Wenruo557ea5d2017-10-09 01:51:02 +0000334 return -EUCLEAN;
335 }
336 if (!IS_ALIGNED(key->offset, sectorsize)) {
David Sterba86a6be32019-03-20 15:31:28 +0100337 generic_err(leaf, slot,
Qu Wenruod508c5f2017-10-09 01:51:05 +0000338 "unaligned key offset for csum item, have %llu should be aligned to %u",
339 key->offset, sectorsize);
Qu Wenruo557ea5d2017-10-09 01:51:02 +0000340 return -EUCLEAN;
341 }
342 if (!IS_ALIGNED(btrfs_item_size_nr(leaf, slot), csumsize)) {
David Sterba86a6be32019-03-20 15:31:28 +0100343 generic_err(leaf, slot,
Qu Wenruod508c5f2017-10-09 01:51:05 +0000344 "unaligned item size for csum item, have %u should be aligned to %u",
345 btrfs_item_size_nr(leaf, slot), csumsize);
Qu Wenruo557ea5d2017-10-09 01:51:02 +0000346 return -EUCLEAN;
347 }
348 return 0;
349}
350
David Sterbace4252c2019-03-20 16:17:46 +0100351static int check_dir_item(struct extent_buffer *leaf,
Qu Wenruoc18679e2019-08-26 15:40:38 +0800352 struct btrfs_key *key, struct btrfs_key *prev_key,
353 int slot)
Qu Wenruoad7b03682017-11-08 08:54:25 +0800354{
David Sterbace4252c2019-03-20 16:17:46 +0100355 struct btrfs_fs_info *fs_info = leaf->fs_info;
Qu Wenruoad7b03682017-11-08 08:54:25 +0800356 struct btrfs_dir_item *di;
357 u32 item_size = btrfs_item_size_nr(leaf, slot);
358 u32 cur = 0;
359
Qu Wenruo80d7fd12019-10-04 17:31:32 +0800360 if (!check_prev_ino(leaf, key, slot, prev_key))
Qu Wenruoc18679e2019-08-26 15:40:38 +0800361 return -EUCLEAN;
Qu Wenruoad7b03682017-11-08 08:54:25 +0800362 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
363 while (cur < item_size) {
Qu Wenruoad7b03682017-11-08 08:54:25 +0800364 u32 name_len;
365 u32 data_len;
366 u32 max_name_len;
367 u32 total_size;
368 u32 name_hash;
369 u8 dir_type;
370
371 /* header itself should not cross item boundary */
372 if (cur + sizeof(*di) > item_size) {
David Sterbad98ced62019-03-20 16:07:27 +0100373 dir_item_err(leaf, slot,
Arnd Bergmann7cfad652017-12-06 15:18:14 +0100374 "dir item header crosses item boundary, have %zu boundary %u",
Qu Wenruoad7b03682017-11-08 08:54:25 +0800375 cur + sizeof(*di), item_size);
376 return -EUCLEAN;
377 }
378
379 /* dir type check */
380 dir_type = btrfs_dir_type(leaf, di);
381 if (dir_type >= BTRFS_FT_MAX) {
David Sterbad98ced62019-03-20 16:07:27 +0100382 dir_item_err(leaf, slot,
Qu Wenruoad7b03682017-11-08 08:54:25 +0800383 "invalid dir item type, have %u expect [0, %u)",
384 dir_type, BTRFS_FT_MAX);
385 return -EUCLEAN;
386 }
387
388 if (key->type == BTRFS_XATTR_ITEM_KEY &&
389 dir_type != BTRFS_FT_XATTR) {
David Sterbad98ced62019-03-20 16:07:27 +0100390 dir_item_err(leaf, slot,
Qu Wenruoad7b03682017-11-08 08:54:25 +0800391 "invalid dir item type for XATTR key, have %u expect %u",
392 dir_type, BTRFS_FT_XATTR);
393 return -EUCLEAN;
394 }
395 if (dir_type == BTRFS_FT_XATTR &&
396 key->type != BTRFS_XATTR_ITEM_KEY) {
David Sterbad98ced62019-03-20 16:07:27 +0100397 dir_item_err(leaf, slot,
Qu Wenruoad7b03682017-11-08 08:54:25 +0800398 "xattr dir type found for non-XATTR key");
399 return -EUCLEAN;
400 }
401 if (dir_type == BTRFS_FT_XATTR)
402 max_name_len = XATTR_NAME_MAX;
403 else
404 max_name_len = BTRFS_NAME_LEN;
405
406 /* Name/data length check */
407 name_len = btrfs_dir_name_len(leaf, di);
408 data_len = btrfs_dir_data_len(leaf, di);
409 if (name_len > max_name_len) {
David Sterbad98ced62019-03-20 16:07:27 +0100410 dir_item_err(leaf, slot,
Qu Wenruoad7b03682017-11-08 08:54:25 +0800411 "dir item name len too long, have %u max %u",
412 name_len, max_name_len);
413 return -EUCLEAN;
414 }
Qu Wenruo2f659542018-01-25 14:56:18 +0800415 if (name_len + data_len > BTRFS_MAX_XATTR_SIZE(fs_info)) {
David Sterbad98ced62019-03-20 16:07:27 +0100416 dir_item_err(leaf, slot,
Qu Wenruoad7b03682017-11-08 08:54:25 +0800417 "dir item name and data len too long, have %u max %u",
418 name_len + data_len,
Qu Wenruo2f659542018-01-25 14:56:18 +0800419 BTRFS_MAX_XATTR_SIZE(fs_info));
Qu Wenruoad7b03682017-11-08 08:54:25 +0800420 return -EUCLEAN;
421 }
422
423 if (data_len && dir_type != BTRFS_FT_XATTR) {
David Sterbad98ced62019-03-20 16:07:27 +0100424 dir_item_err(leaf, slot,
Qu Wenruoad7b03682017-11-08 08:54:25 +0800425 "dir item with invalid data len, have %u expect 0",
426 data_len);
427 return -EUCLEAN;
428 }
429
430 total_size = sizeof(*di) + name_len + data_len;
431
432 /* header and name/data should not cross item boundary */
433 if (cur + total_size > item_size) {
David Sterbad98ced62019-03-20 16:07:27 +0100434 dir_item_err(leaf, slot,
Qu Wenruoad7b03682017-11-08 08:54:25 +0800435 "dir item data crosses item boundary, have %u boundary %u",
436 cur + total_size, item_size);
437 return -EUCLEAN;
438 }
439
440 /*
441 * Special check for XATTR/DIR_ITEM, as key->offset is name
442 * hash, should match its name
443 */
444 if (key->type == BTRFS_DIR_ITEM_KEY ||
445 key->type == BTRFS_XATTR_ITEM_KEY) {
David Sterbae2683fc2018-01-10 15:13:07 +0100446 char namebuf[max(BTRFS_NAME_LEN, XATTR_NAME_MAX)];
447
Qu Wenruoad7b03682017-11-08 08:54:25 +0800448 read_extent_buffer(leaf, namebuf,
449 (unsigned long)(di + 1), name_len);
450 name_hash = btrfs_name_hash(namebuf, name_len);
451 if (key->offset != name_hash) {
David Sterbad98ced62019-03-20 16:07:27 +0100452 dir_item_err(leaf, slot,
Qu Wenruoad7b03682017-11-08 08:54:25 +0800453 "name hash mismatch with key, have 0x%016x expect 0x%016llx",
454 name_hash, key->offset);
455 return -EUCLEAN;
456 }
457 }
458 cur += total_size;
459 di = (struct btrfs_dir_item *)((void *)di + total_size);
460 }
461 return 0;
462}
463
David Sterba4806bd82019-03-20 16:18:57 +0100464__printf(3, 4)
Qu Wenruofce466e2018-07-03 17:10:05 +0800465__cold
David Sterba4806bd82019-03-20 16:18:57 +0100466static void block_group_err(const struct extent_buffer *eb, int slot,
Qu Wenruofce466e2018-07-03 17:10:05 +0800467 const char *fmt, ...)
468{
David Sterba4806bd82019-03-20 16:18:57 +0100469 const struct btrfs_fs_info *fs_info = eb->fs_info;
Qu Wenruofce466e2018-07-03 17:10:05 +0800470 struct btrfs_key key;
471 struct va_format vaf;
472 va_list args;
473
474 btrfs_item_key_to_cpu(eb, &key, slot);
475 va_start(args, fmt);
476
477 vaf.fmt = fmt;
478 vaf.va = &args;
479
480 btrfs_crit(fs_info,
481 "corrupt %s: root=%llu block=%llu slot=%d bg_start=%llu bg_len=%llu, %pV",
482 btrfs_header_level(eb) == 0 ? "leaf" : "node",
483 btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot,
484 key.objectid, key.offset, &vaf);
485 va_end(args);
486}
487
David Sterbaaf60ce22019-03-20 16:19:31 +0100488static int check_block_group_item(struct extent_buffer *leaf,
Qu Wenruofce466e2018-07-03 17:10:05 +0800489 struct btrfs_key *key, int slot)
490{
491 struct btrfs_block_group_item bgi;
492 u32 item_size = btrfs_item_size_nr(leaf, slot);
493 u64 flags;
494 u64 type;
495
496 /*
497 * Here we don't really care about alignment since extent allocator can
Qu Wenruo10950922018-11-23 09:06:36 +0800498 * handle it. We care more about the size.
Qu Wenruofce466e2018-07-03 17:10:05 +0800499 */
Qu Wenruo10950922018-11-23 09:06:36 +0800500 if (key->offset == 0) {
David Sterba4806bd82019-03-20 16:18:57 +0100501 block_group_err(leaf, slot,
Qu Wenruo10950922018-11-23 09:06:36 +0800502 "invalid block group size 0");
Qu Wenruofce466e2018-07-03 17:10:05 +0800503 return -EUCLEAN;
504 }
505
506 if (item_size != sizeof(bgi)) {
David Sterba4806bd82019-03-20 16:18:57 +0100507 block_group_err(leaf, slot,
Qu Wenruofce466e2018-07-03 17:10:05 +0800508 "invalid item size, have %u expect %zu",
509 item_size, sizeof(bgi));
510 return -EUCLEAN;
511 }
512
513 read_extent_buffer(leaf, &bgi, btrfs_item_ptr_offset(leaf, slot),
514 sizeof(bgi));
515 if (btrfs_block_group_chunk_objectid(&bgi) !=
516 BTRFS_FIRST_CHUNK_TREE_OBJECTID) {
David Sterba4806bd82019-03-20 16:18:57 +0100517 block_group_err(leaf, slot,
Qu Wenruofce466e2018-07-03 17:10:05 +0800518 "invalid block group chunk objectid, have %llu expect %llu",
519 btrfs_block_group_chunk_objectid(&bgi),
520 BTRFS_FIRST_CHUNK_TREE_OBJECTID);
521 return -EUCLEAN;
522 }
523
524 if (btrfs_block_group_used(&bgi) > key->offset) {
David Sterba4806bd82019-03-20 16:18:57 +0100525 block_group_err(leaf, slot,
Qu Wenruofce466e2018-07-03 17:10:05 +0800526 "invalid block group used, have %llu expect [0, %llu)",
527 btrfs_block_group_used(&bgi), key->offset);
528 return -EUCLEAN;
529 }
530
531 flags = btrfs_block_group_flags(&bgi);
532 if (hweight64(flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) > 1) {
David Sterba4806bd82019-03-20 16:18:57 +0100533 block_group_err(leaf, slot,
Qu Wenruofce466e2018-07-03 17:10:05 +0800534"invalid profile flags, have 0x%llx (%lu bits set) expect no more than 1 bit set",
535 flags & BTRFS_BLOCK_GROUP_PROFILE_MASK,
536 hweight64(flags & BTRFS_BLOCK_GROUP_PROFILE_MASK));
537 return -EUCLEAN;
538 }
539
540 type = flags & BTRFS_BLOCK_GROUP_TYPE_MASK;
541 if (type != BTRFS_BLOCK_GROUP_DATA &&
542 type != BTRFS_BLOCK_GROUP_METADATA &&
543 type != BTRFS_BLOCK_GROUP_SYSTEM &&
544 type != (BTRFS_BLOCK_GROUP_METADATA |
545 BTRFS_BLOCK_GROUP_DATA)) {
David Sterba4806bd82019-03-20 16:18:57 +0100546 block_group_err(leaf, slot,
Shaokun Zhang761333f2018-11-05 18:49:09 +0800547"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 +0800548 type, hweight64(type),
549 BTRFS_BLOCK_GROUP_DATA, BTRFS_BLOCK_GROUP_METADATA,
550 BTRFS_BLOCK_GROUP_SYSTEM,
551 BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA);
552 return -EUCLEAN;
553 }
554 return 0;
555}
556
David Sterbad001e4a2019-03-20 16:22:58 +0100557__printf(4, 5)
Qu Wenruof1140242019-03-20 13:36:06 +0800558__cold
David Sterbad001e4a2019-03-20 16:22:58 +0100559static void chunk_err(const struct extent_buffer *leaf,
Qu Wenruof1140242019-03-20 13:36:06 +0800560 const struct btrfs_chunk *chunk, u64 logical,
561 const char *fmt, ...)
562{
David Sterbad001e4a2019-03-20 16:22:58 +0100563 const struct btrfs_fs_info *fs_info = leaf->fs_info;
Qu Wenruof1140242019-03-20 13:36:06 +0800564 bool is_sb;
565 struct va_format vaf;
566 va_list args;
567 int i;
568 int slot = -1;
569
570 /* Only superblock eb is able to have such small offset */
571 is_sb = (leaf->start == BTRFS_SUPER_INFO_OFFSET);
572
573 if (!is_sb) {
574 /*
575 * Get the slot number by iterating through all slots, this
576 * would provide better readability.
577 */
578 for (i = 0; i < btrfs_header_nritems(leaf); i++) {
579 if (btrfs_item_ptr_offset(leaf, i) ==
580 (unsigned long)chunk) {
581 slot = i;
582 break;
583 }
584 }
585 }
586 va_start(args, fmt);
587 vaf.fmt = fmt;
588 vaf.va = &args;
589
590 if (is_sb)
591 btrfs_crit(fs_info,
592 "corrupt superblock syschunk array: chunk_start=%llu, %pV",
593 logical, &vaf);
594 else
595 btrfs_crit(fs_info,
596 "corrupt leaf: root=%llu block=%llu slot=%d chunk_start=%llu, %pV",
597 BTRFS_CHUNK_TREE_OBJECTID, leaf->start, slot,
598 logical, &vaf);
599 va_end(args);
600}
601
Qu Wenruoad7b03682017-11-08 08:54:25 +0800602/*
Qu Wenruo82fc28f2019-03-20 13:16:42 +0800603 * The common chunk check which could also work on super block sys chunk array.
604 *
Qu Wenruobf871c32019-03-20 13:39:14 +0800605 * Return -EUCLEAN if anything is corrupted.
Qu Wenruo82fc28f2019-03-20 13:16:42 +0800606 * Return 0 if everything is OK.
607 */
David Sterbaddaf1d52019-03-20 16:40:48 +0100608int btrfs_check_chunk_valid(struct extent_buffer *leaf,
Qu Wenruo82fc28f2019-03-20 13:16:42 +0800609 struct btrfs_chunk *chunk, u64 logical)
610{
David Sterbaddaf1d52019-03-20 16:40:48 +0100611 struct btrfs_fs_info *fs_info = leaf->fs_info;
Qu Wenruo82fc28f2019-03-20 13:16:42 +0800612 u64 length;
613 u64 stripe_len;
614 u16 num_stripes;
615 u16 sub_stripes;
616 u64 type;
617 u64 features;
618 bool mixed = false;
619
620 length = btrfs_chunk_length(leaf, chunk);
621 stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
622 num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
623 sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
624 type = btrfs_chunk_type(leaf, chunk);
625
626 if (!num_stripes) {
David Sterbad001e4a2019-03-20 16:22:58 +0100627 chunk_err(leaf, chunk, logical,
Qu Wenruof1140242019-03-20 13:36:06 +0800628 "invalid chunk num_stripes, have %u", num_stripes);
Qu Wenruobf871c32019-03-20 13:39:14 +0800629 return -EUCLEAN;
Qu Wenruo82fc28f2019-03-20 13:16:42 +0800630 }
631 if (!IS_ALIGNED(logical, fs_info->sectorsize)) {
David Sterbad001e4a2019-03-20 16:22:58 +0100632 chunk_err(leaf, chunk, logical,
Qu Wenruof1140242019-03-20 13:36:06 +0800633 "invalid chunk logical, have %llu should aligned to %u",
634 logical, fs_info->sectorsize);
Qu Wenruobf871c32019-03-20 13:39:14 +0800635 return -EUCLEAN;
Qu Wenruo82fc28f2019-03-20 13:16:42 +0800636 }
637 if (btrfs_chunk_sector_size(leaf, chunk) != fs_info->sectorsize) {
David Sterbad001e4a2019-03-20 16:22:58 +0100638 chunk_err(leaf, chunk, logical,
Qu Wenruof1140242019-03-20 13:36:06 +0800639 "invalid chunk sectorsize, have %u expect %u",
640 btrfs_chunk_sector_size(leaf, chunk),
641 fs_info->sectorsize);
Qu Wenruobf871c32019-03-20 13:39:14 +0800642 return -EUCLEAN;
Qu Wenruo82fc28f2019-03-20 13:16:42 +0800643 }
644 if (!length || !IS_ALIGNED(length, fs_info->sectorsize)) {
David Sterbad001e4a2019-03-20 16:22:58 +0100645 chunk_err(leaf, chunk, logical,
Qu Wenruof1140242019-03-20 13:36:06 +0800646 "invalid chunk length, have %llu", length);
Qu Wenruobf871c32019-03-20 13:39:14 +0800647 return -EUCLEAN;
Qu Wenruo82fc28f2019-03-20 13:16:42 +0800648 }
649 if (!is_power_of_2(stripe_len) || stripe_len != BTRFS_STRIPE_LEN) {
David Sterbad001e4a2019-03-20 16:22:58 +0100650 chunk_err(leaf, chunk, logical,
Qu Wenruof1140242019-03-20 13:36:06 +0800651 "invalid chunk stripe length: %llu",
Qu Wenruo82fc28f2019-03-20 13:16:42 +0800652 stripe_len);
Qu Wenruobf871c32019-03-20 13:39:14 +0800653 return -EUCLEAN;
Qu Wenruo82fc28f2019-03-20 13:16:42 +0800654 }
655 if (~(BTRFS_BLOCK_GROUP_TYPE_MASK | BTRFS_BLOCK_GROUP_PROFILE_MASK) &
656 type) {
David Sterbad001e4a2019-03-20 16:22:58 +0100657 chunk_err(leaf, chunk, logical,
Qu Wenruof1140242019-03-20 13:36:06 +0800658 "unrecognized chunk type: 0x%llx",
Qu Wenruo82fc28f2019-03-20 13:16:42 +0800659 ~(BTRFS_BLOCK_GROUP_TYPE_MASK |
660 BTRFS_BLOCK_GROUP_PROFILE_MASK) &
661 btrfs_chunk_type(leaf, chunk));
Qu Wenruobf871c32019-03-20 13:39:14 +0800662 return -EUCLEAN;
Qu Wenruo82fc28f2019-03-20 13:16:42 +0800663 }
664
David Sterbac1499162019-10-01 19:44:42 +0200665 if (!has_single_bit_set(type & BTRFS_BLOCK_GROUP_PROFILE_MASK) &&
Qu Wenruo80e46cf2019-03-13 12:17:50 +0800666 (type & BTRFS_BLOCK_GROUP_PROFILE_MASK) != 0) {
David Sterbad001e4a2019-03-20 16:22:58 +0100667 chunk_err(leaf, chunk, logical,
Qu Wenruo80e46cf2019-03-13 12:17:50 +0800668 "invalid chunk profile flag: 0x%llx, expect 0 or 1 bit set",
669 type & BTRFS_BLOCK_GROUP_PROFILE_MASK);
670 return -EUCLEAN;
671 }
Qu Wenruo82fc28f2019-03-20 13:16:42 +0800672 if ((type & BTRFS_BLOCK_GROUP_TYPE_MASK) == 0) {
David Sterbad001e4a2019-03-20 16:22:58 +0100673 chunk_err(leaf, chunk, logical,
Qu Wenruof1140242019-03-20 13:36:06 +0800674 "missing chunk type flag, have 0x%llx one bit must be set in 0x%llx",
675 type, BTRFS_BLOCK_GROUP_TYPE_MASK);
Qu Wenruobf871c32019-03-20 13:39:14 +0800676 return -EUCLEAN;
Qu Wenruo82fc28f2019-03-20 13:16:42 +0800677 }
678
679 if ((type & BTRFS_BLOCK_GROUP_SYSTEM) &&
680 (type & (BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA))) {
David Sterbad001e4a2019-03-20 16:22:58 +0100681 chunk_err(leaf, chunk, logical,
Qu Wenruof1140242019-03-20 13:36:06 +0800682 "system chunk with data or metadata type: 0x%llx",
683 type);
Qu Wenruobf871c32019-03-20 13:39:14 +0800684 return -EUCLEAN;
Qu Wenruo82fc28f2019-03-20 13:16:42 +0800685 }
686
687 features = btrfs_super_incompat_flags(fs_info->super_copy);
688 if (features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS)
689 mixed = true;
690
691 if (!mixed) {
692 if ((type & BTRFS_BLOCK_GROUP_METADATA) &&
693 (type & BTRFS_BLOCK_GROUP_DATA)) {
David Sterbad001e4a2019-03-20 16:22:58 +0100694 chunk_err(leaf, chunk, logical,
Qu Wenruo82fc28f2019-03-20 13:16:42 +0800695 "mixed chunk type in non-mixed mode: 0x%llx", type);
Qu Wenruobf871c32019-03-20 13:39:14 +0800696 return -EUCLEAN;
Qu Wenruo82fc28f2019-03-20 13:16:42 +0800697 }
698 }
699
700 if ((type & BTRFS_BLOCK_GROUP_RAID10 && sub_stripes != 2) ||
701 (type & BTRFS_BLOCK_GROUP_RAID1 && num_stripes != 2) ||
702 (type & BTRFS_BLOCK_GROUP_RAID5 && num_stripes < 2) ||
703 (type & BTRFS_BLOCK_GROUP_RAID6 && num_stripes < 3) ||
704 (type & BTRFS_BLOCK_GROUP_DUP && num_stripes != 2) ||
705 ((type & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 && num_stripes != 1)) {
David Sterbad001e4a2019-03-20 16:22:58 +0100706 chunk_err(leaf, chunk, logical,
Qu Wenruo82fc28f2019-03-20 13:16:42 +0800707 "invalid num_stripes:sub_stripes %u:%u for profile %llu",
708 num_stripes, sub_stripes,
709 type & BTRFS_BLOCK_GROUP_PROFILE_MASK);
Qu Wenruobf871c32019-03-20 13:39:14 +0800710 return -EUCLEAN;
Qu Wenruo82fc28f2019-03-20 13:16:42 +0800711 }
712
713 return 0;
714}
715
David Sterba5617ed82019-03-20 16:22:58 +0100716__printf(3, 4)
Qu Wenruoab4ba2e2019-03-08 14:20:03 +0800717__cold
David Sterba5617ed82019-03-20 16:22:58 +0100718static void dev_item_err(const struct extent_buffer *eb, int slot,
Qu Wenruoab4ba2e2019-03-08 14:20:03 +0800719 const char *fmt, ...)
720{
721 struct btrfs_key key;
722 struct va_format vaf;
723 va_list args;
724
725 btrfs_item_key_to_cpu(eb, &key, slot);
726 va_start(args, fmt);
727
728 vaf.fmt = fmt;
729 vaf.va = &args;
730
David Sterba5617ed82019-03-20 16:22:58 +0100731 btrfs_crit(eb->fs_info,
Qu Wenruoab4ba2e2019-03-08 14:20:03 +0800732 "corrupt %s: root=%llu block=%llu slot=%d devid=%llu %pV",
733 btrfs_header_level(eb) == 0 ? "leaf" : "node",
734 btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot,
735 key.objectid, &vaf);
736 va_end(args);
737}
738
David Sterba412a2312019-03-20 16:22:58 +0100739static int check_dev_item(struct extent_buffer *leaf,
Qu Wenruoab4ba2e2019-03-08 14:20:03 +0800740 struct btrfs_key *key, int slot)
741{
742 struct btrfs_dev_item *ditem;
Qu Wenruoab4ba2e2019-03-08 14:20:03 +0800743
744 if (key->objectid != BTRFS_DEV_ITEMS_OBJECTID) {
David Sterba5617ed82019-03-20 16:22:58 +0100745 dev_item_err(leaf, slot,
Qu Wenruoab4ba2e2019-03-08 14:20:03 +0800746 "invalid objectid: has=%llu expect=%llu",
747 key->objectid, BTRFS_DEV_ITEMS_OBJECTID);
748 return -EUCLEAN;
749 }
Qu Wenruoab4ba2e2019-03-08 14:20:03 +0800750 ditem = btrfs_item_ptr(leaf, slot, struct btrfs_dev_item);
751 if (btrfs_device_id(leaf, ditem) != key->offset) {
David Sterba5617ed82019-03-20 16:22:58 +0100752 dev_item_err(leaf, slot,
Qu Wenruoab4ba2e2019-03-08 14:20:03 +0800753 "devid mismatch: key has=%llu item has=%llu",
754 key->offset, btrfs_device_id(leaf, ditem));
755 return -EUCLEAN;
756 }
757
758 /*
759 * For device total_bytes, we don't have reliable way to check it, as
760 * it can be 0 for device removal. Device size check can only be done
761 * by dev extents check.
762 */
763 if (btrfs_device_bytes_used(leaf, ditem) >
764 btrfs_device_total_bytes(leaf, ditem)) {
David Sterba5617ed82019-03-20 16:22:58 +0100765 dev_item_err(leaf, slot,
Qu Wenruoab4ba2e2019-03-08 14:20:03 +0800766 "invalid bytes used: have %llu expect [0, %llu]",
767 btrfs_device_bytes_used(leaf, ditem),
768 btrfs_device_total_bytes(leaf, ditem));
769 return -EUCLEAN;
770 }
771 /*
772 * Remaining members like io_align/type/gen/dev_group aren't really
773 * utilized. Skip them to make later usage of them easier.
774 */
775 return 0;
776}
777
Qu Wenruo496245c2019-03-13 14:31:35 +0800778/* Inode item error output has the same format as dir_item_err() */
779#define inode_item_err(fs_info, eb, slot, fmt, ...) \
David Sterbad98ced62019-03-20 16:07:27 +0100780 dir_item_err(eb, slot, fmt, __VA_ARGS__)
Qu Wenruo496245c2019-03-13 14:31:35 +0800781
David Sterba39e57f42019-03-20 16:22:58 +0100782static int check_inode_item(struct extent_buffer *leaf,
Qu Wenruo496245c2019-03-13 14:31:35 +0800783 struct btrfs_key *key, int slot)
784{
David Sterba39e57f42019-03-20 16:22:58 +0100785 struct btrfs_fs_info *fs_info = leaf->fs_info;
Qu Wenruo496245c2019-03-13 14:31:35 +0800786 struct btrfs_inode_item *iitem;
787 u64 super_gen = btrfs_super_generation(fs_info->super_copy);
788 u32 valid_mask = (S_IFMT | S_ISUID | S_ISGID | S_ISVTX | 0777);
789 u32 mode;
790
791 if ((key->objectid < BTRFS_FIRST_FREE_OBJECTID ||
792 key->objectid > BTRFS_LAST_FREE_OBJECTID) &&
793 key->objectid != BTRFS_ROOT_TREE_DIR_OBJECTID &&
794 key->objectid != BTRFS_FREE_INO_OBJECTID) {
David Sterba86a6be32019-03-20 15:31:28 +0100795 generic_err(leaf, slot,
Qu Wenruo496245c2019-03-13 14:31:35 +0800796 "invalid key objectid: has %llu expect %llu or [%llu, %llu] or %llu",
797 key->objectid, BTRFS_ROOT_TREE_DIR_OBJECTID,
798 BTRFS_FIRST_FREE_OBJECTID,
799 BTRFS_LAST_FREE_OBJECTID,
800 BTRFS_FREE_INO_OBJECTID);
801 return -EUCLEAN;
802 }
803 if (key->offset != 0) {
804 inode_item_err(fs_info, leaf, slot,
805 "invalid key offset: has %llu expect 0",
806 key->offset);
807 return -EUCLEAN;
808 }
809 iitem = btrfs_item_ptr(leaf, slot, struct btrfs_inode_item);
810
811 /* Here we use super block generation + 1 to handle log tree */
812 if (btrfs_inode_generation(leaf, iitem) > super_gen + 1) {
813 inode_item_err(fs_info, leaf, slot,
814 "invalid inode generation: has %llu expect (0, %llu]",
815 btrfs_inode_generation(leaf, iitem),
816 super_gen + 1);
817 return -EUCLEAN;
818 }
819 /* Note for ROOT_TREE_DIR_ITEM, mkfs could set its transid 0 */
820 if (btrfs_inode_transid(leaf, iitem) > super_gen + 1) {
821 inode_item_err(fs_info, leaf, slot,
822 "invalid inode generation: has %llu expect [0, %llu]",
823 btrfs_inode_transid(leaf, iitem), super_gen + 1);
824 return -EUCLEAN;
825 }
826
827 /*
828 * For size and nbytes it's better not to be too strict, as for dir
829 * item its size/nbytes can easily get wrong, but doesn't affect
830 * anything in the fs. So here we skip the check.
831 */
832 mode = btrfs_inode_mode(leaf, iitem);
833 if (mode & ~valid_mask) {
834 inode_item_err(fs_info, leaf, slot,
835 "unknown mode bit detected: 0x%x",
836 mode & ~valid_mask);
837 return -EUCLEAN;
838 }
839
840 /*
David Sterbac1499162019-10-01 19:44:42 +0200841 * S_IFMT is not bit mapped so we can't completely rely on
842 * is_power_of_2/has_single_bit_set, but it can save us from checking
843 * FIFO/CHR/DIR/REG. Only needs to check BLK, LNK and SOCKS
Qu Wenruo496245c2019-03-13 14:31:35 +0800844 */
David Sterbac1499162019-10-01 19:44:42 +0200845 if (!has_single_bit_set(mode & S_IFMT)) {
Qu Wenruo496245c2019-03-13 14:31:35 +0800846 if (!S_ISLNK(mode) && !S_ISBLK(mode) && !S_ISSOCK(mode)) {
847 inode_item_err(fs_info, leaf, slot,
848 "invalid mode: has 0%o expect valid S_IF* bit(s)",
849 mode & S_IFMT);
850 return -EUCLEAN;
851 }
852 }
853 if (S_ISDIR(mode) && btrfs_inode_nlink(leaf, iitem) > 1) {
854 inode_item_err(fs_info, leaf, slot,
855 "invalid nlink: has %u expect no more than 1 for dir",
856 btrfs_inode_nlink(leaf, iitem));
857 return -EUCLEAN;
858 }
859 if (btrfs_inode_flags(leaf, iitem) & ~BTRFS_INODE_FLAG_MASK) {
860 inode_item_err(fs_info, leaf, slot,
861 "unknown flags detected: 0x%llx",
862 btrfs_inode_flags(leaf, iitem) &
863 ~BTRFS_INODE_FLAG_MASK);
864 return -EUCLEAN;
865 }
866 return 0;
867}
868
Qu Wenruo259ee772019-07-16 17:00:34 +0800869static int check_root_item(struct extent_buffer *leaf, struct btrfs_key *key,
870 int slot)
871{
872 struct btrfs_fs_info *fs_info = leaf->fs_info;
873 struct btrfs_root_item ri;
874 const u64 valid_root_flags = BTRFS_ROOT_SUBVOL_RDONLY |
875 BTRFS_ROOT_SUBVOL_DEAD;
876
877 /* No such tree id */
878 if (key->objectid == 0) {
879 generic_err(leaf, slot, "invalid root id 0");
880 return -EUCLEAN;
881 }
882
883 /*
884 * Some older kernel may create ROOT_ITEM with non-zero offset, so here
885 * we only check offset for reloc tree whose key->offset must be a
886 * valid tree.
887 */
888 if (key->objectid == BTRFS_TREE_RELOC_OBJECTID && key->offset == 0) {
889 generic_err(leaf, slot, "invalid root id 0 for reloc tree");
890 return -EUCLEAN;
891 }
892
893 if (btrfs_item_size_nr(leaf, slot) != sizeof(ri)) {
894 generic_err(leaf, slot,
895 "invalid root item size, have %u expect %zu",
896 btrfs_item_size_nr(leaf, slot), sizeof(ri));
897 }
898
899 read_extent_buffer(leaf, &ri, btrfs_item_ptr_offset(leaf, slot),
900 sizeof(ri));
901
902 /* Generation related */
903 if (btrfs_root_generation(&ri) >
904 btrfs_super_generation(fs_info->super_copy) + 1) {
905 generic_err(leaf, slot,
906 "invalid root generation, have %llu expect (0, %llu]",
907 btrfs_root_generation(&ri),
908 btrfs_super_generation(fs_info->super_copy) + 1);
909 return -EUCLEAN;
910 }
911 if (btrfs_root_generation_v2(&ri) >
912 btrfs_super_generation(fs_info->super_copy) + 1) {
913 generic_err(leaf, slot,
914 "invalid root v2 generation, have %llu expect (0, %llu]",
915 btrfs_root_generation_v2(&ri),
916 btrfs_super_generation(fs_info->super_copy) + 1);
917 return -EUCLEAN;
918 }
919 if (btrfs_root_last_snapshot(&ri) >
920 btrfs_super_generation(fs_info->super_copy) + 1) {
921 generic_err(leaf, slot,
922 "invalid root last_snapshot, have %llu expect (0, %llu]",
923 btrfs_root_last_snapshot(&ri),
924 btrfs_super_generation(fs_info->super_copy) + 1);
925 return -EUCLEAN;
926 }
927
928 /* Alignment and level check */
929 if (!IS_ALIGNED(btrfs_root_bytenr(&ri), fs_info->sectorsize)) {
930 generic_err(leaf, slot,
931 "invalid root bytenr, have %llu expect to be aligned to %u",
932 btrfs_root_bytenr(&ri), fs_info->sectorsize);
933 return -EUCLEAN;
934 }
935 if (btrfs_root_level(&ri) >= BTRFS_MAX_LEVEL) {
936 generic_err(leaf, slot,
937 "invalid root level, have %u expect [0, %u]",
938 btrfs_root_level(&ri), BTRFS_MAX_LEVEL - 1);
939 return -EUCLEAN;
940 }
941 if (ri.drop_level >= BTRFS_MAX_LEVEL) {
942 generic_err(leaf, slot,
943 "invalid root level, have %u expect [0, %u]",
944 ri.drop_level, BTRFS_MAX_LEVEL - 1);
945 return -EUCLEAN;
946 }
947
948 /* Flags check */
949 if (btrfs_root_flags(&ri) & ~valid_root_flags) {
950 generic_err(leaf, slot,
951 "invalid root flags, have 0x%llx expect mask 0x%llx",
952 btrfs_root_flags(&ri), valid_root_flags);
953 return -EUCLEAN;
954 }
955 return 0;
956}
957
Qu Wenruof82d1c7c2019-08-09 09:24:22 +0800958__printf(3,4)
959__cold
960static void extent_err(const struct extent_buffer *eb, int slot,
961 const char *fmt, ...)
962{
963 struct btrfs_key key;
964 struct va_format vaf;
965 va_list args;
966 u64 bytenr;
967 u64 len;
968
969 btrfs_item_key_to_cpu(eb, &key, slot);
970 bytenr = key.objectid;
Qu Wenruoe2406a62019-08-09 09:24:23 +0800971 if (key.type == BTRFS_METADATA_ITEM_KEY ||
972 key.type == BTRFS_TREE_BLOCK_REF_KEY ||
973 key.type == BTRFS_SHARED_BLOCK_REF_KEY)
Qu Wenruof82d1c7c2019-08-09 09:24:22 +0800974 len = eb->fs_info->nodesize;
975 else
976 len = key.offset;
977 va_start(args, fmt);
978
979 vaf.fmt = fmt;
980 vaf.va = &args;
981
982 btrfs_crit(eb->fs_info,
983 "corrupt %s: block=%llu slot=%d extent bytenr=%llu len=%llu %pV",
984 btrfs_header_level(eb) == 0 ? "leaf" : "node",
985 eb->start, slot, bytenr, len, &vaf);
986 va_end(args);
987}
988
989static int check_extent_item(struct extent_buffer *leaf,
990 struct btrfs_key *key, int slot)
991{
992 struct btrfs_fs_info *fs_info = leaf->fs_info;
993 struct btrfs_extent_item *ei;
994 bool is_tree_block = false;
995 unsigned long ptr; /* Current pointer inside inline refs */
996 unsigned long end; /* Extent item end */
997 const u32 item_size = btrfs_item_size_nr(leaf, slot);
998 u64 flags;
999 u64 generation;
1000 u64 total_refs; /* Total refs in btrfs_extent_item */
1001 u64 inline_refs = 0; /* found total inline refs */
1002
1003 if (key->type == BTRFS_METADATA_ITEM_KEY &&
1004 !btrfs_fs_incompat(fs_info, SKINNY_METADATA)) {
1005 generic_err(leaf, slot,
1006"invalid key type, METADATA_ITEM type invalid when SKINNY_METADATA feature disabled");
1007 return -EUCLEAN;
1008 }
1009 /* key->objectid is the bytenr for both key types */
1010 if (!IS_ALIGNED(key->objectid, fs_info->sectorsize)) {
1011 generic_err(leaf, slot,
1012 "invalid key objectid, have %llu expect to be aligned to %u",
1013 key->objectid, fs_info->sectorsize);
1014 return -EUCLEAN;
1015 }
1016
1017 /* key->offset is tree level for METADATA_ITEM_KEY */
1018 if (key->type == BTRFS_METADATA_ITEM_KEY &&
1019 key->offset >= BTRFS_MAX_LEVEL) {
1020 extent_err(leaf, slot,
1021 "invalid tree level, have %llu expect [0, %u]",
1022 key->offset, BTRFS_MAX_LEVEL - 1);
1023 return -EUCLEAN;
1024 }
1025
1026 /*
1027 * EXTENT/METADATA_ITEM consists of:
1028 * 1) One btrfs_extent_item
1029 * Records the total refs, type and generation of the extent.
1030 *
1031 * 2) One btrfs_tree_block_info (for EXTENT_ITEM and tree backref only)
1032 * Records the first key and level of the tree block.
1033 *
1034 * 2) Zero or more btrfs_extent_inline_ref(s)
1035 * Each inline ref has one btrfs_extent_inline_ref shows:
1036 * 2.1) The ref type, one of the 4
1037 * TREE_BLOCK_REF Tree block only
1038 * SHARED_BLOCK_REF Tree block only
1039 * EXTENT_DATA_REF Data only
1040 * SHARED_DATA_REF Data only
1041 * 2.2) Ref type specific data
1042 * Either using btrfs_extent_inline_ref::offset, or specific
1043 * data structure.
1044 */
1045 if (item_size < sizeof(*ei)) {
1046 extent_err(leaf, slot,
1047 "invalid item size, have %u expect [%zu, %u)",
1048 item_size, sizeof(*ei),
1049 BTRFS_LEAF_DATA_SIZE(fs_info));
1050 return -EUCLEAN;
1051 }
1052 end = item_size + btrfs_item_ptr_offset(leaf, slot);
1053
1054 /* Checks against extent_item */
1055 ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item);
1056 flags = btrfs_extent_flags(leaf, ei);
1057 total_refs = btrfs_extent_refs(leaf, ei);
1058 generation = btrfs_extent_generation(leaf, ei);
1059 if (generation > btrfs_super_generation(fs_info->super_copy) + 1) {
1060 extent_err(leaf, slot,
1061 "invalid generation, have %llu expect (0, %llu]",
1062 generation,
1063 btrfs_super_generation(fs_info->super_copy) + 1);
1064 return -EUCLEAN;
1065 }
David Sterbac1499162019-10-01 19:44:42 +02001066 if (!has_single_bit_set(flags & (BTRFS_EXTENT_FLAG_DATA |
1067 BTRFS_EXTENT_FLAG_TREE_BLOCK))) {
Qu Wenruof82d1c7c2019-08-09 09:24:22 +08001068 extent_err(leaf, slot,
1069 "invalid extent flag, have 0x%llx expect 1 bit set in 0x%llx",
1070 flags, BTRFS_EXTENT_FLAG_DATA |
1071 BTRFS_EXTENT_FLAG_TREE_BLOCK);
1072 return -EUCLEAN;
1073 }
1074 is_tree_block = !!(flags & BTRFS_EXTENT_FLAG_TREE_BLOCK);
1075 if (is_tree_block) {
1076 if (key->type == BTRFS_EXTENT_ITEM_KEY &&
1077 key->offset != fs_info->nodesize) {
1078 extent_err(leaf, slot,
1079 "invalid extent length, have %llu expect %u",
1080 key->offset, fs_info->nodesize);
1081 return -EUCLEAN;
1082 }
1083 } else {
1084 if (key->type != BTRFS_EXTENT_ITEM_KEY) {
1085 extent_err(leaf, slot,
1086 "invalid key type, have %u expect %u for data backref",
1087 key->type, BTRFS_EXTENT_ITEM_KEY);
1088 return -EUCLEAN;
1089 }
1090 if (!IS_ALIGNED(key->offset, fs_info->sectorsize)) {
1091 extent_err(leaf, slot,
1092 "invalid extent length, have %llu expect aligned to %u",
1093 key->offset, fs_info->sectorsize);
1094 return -EUCLEAN;
1095 }
1096 }
1097 ptr = (unsigned long)(struct btrfs_extent_item *)(ei + 1);
1098
1099 /* Check the special case of btrfs_tree_block_info */
1100 if (is_tree_block && key->type != BTRFS_METADATA_ITEM_KEY) {
1101 struct btrfs_tree_block_info *info;
1102
1103 info = (struct btrfs_tree_block_info *)ptr;
1104 if (btrfs_tree_block_level(leaf, info) >= BTRFS_MAX_LEVEL) {
1105 extent_err(leaf, slot,
1106 "invalid tree block info level, have %u expect [0, %u]",
1107 btrfs_tree_block_level(leaf, info),
1108 BTRFS_MAX_LEVEL - 1);
1109 return -EUCLEAN;
1110 }
1111 ptr = (unsigned long)(struct btrfs_tree_block_info *)(info + 1);
1112 }
1113
1114 /* Check inline refs */
1115 while (ptr < end) {
1116 struct btrfs_extent_inline_ref *iref;
1117 struct btrfs_extent_data_ref *dref;
1118 struct btrfs_shared_data_ref *sref;
1119 u64 dref_offset;
1120 u64 inline_offset;
1121 u8 inline_type;
1122
1123 if (ptr + sizeof(*iref) > end) {
1124 extent_err(leaf, slot,
1125"inline ref item overflows extent item, ptr %lu iref size %zu end %lu",
1126 ptr, sizeof(*iref), end);
1127 return -EUCLEAN;
1128 }
1129 iref = (struct btrfs_extent_inline_ref *)ptr;
1130 inline_type = btrfs_extent_inline_ref_type(leaf, iref);
1131 inline_offset = btrfs_extent_inline_ref_offset(leaf, iref);
1132 if (ptr + btrfs_extent_inline_ref_size(inline_type) > end) {
1133 extent_err(leaf, slot,
1134"inline ref item overflows extent item, ptr %lu iref size %u end %lu",
1135 ptr, inline_type, end);
1136 return -EUCLEAN;
1137 }
1138
1139 switch (inline_type) {
1140 /* inline_offset is subvolid of the owner, no need to check */
1141 case BTRFS_TREE_BLOCK_REF_KEY:
1142 inline_refs++;
1143 break;
1144 /* Contains parent bytenr */
1145 case BTRFS_SHARED_BLOCK_REF_KEY:
1146 if (!IS_ALIGNED(inline_offset, fs_info->sectorsize)) {
1147 extent_err(leaf, slot,
1148 "invalid tree parent bytenr, have %llu expect aligned to %u",
1149 inline_offset, fs_info->sectorsize);
1150 return -EUCLEAN;
1151 }
1152 inline_refs++;
1153 break;
1154 /*
1155 * Contains owner subvolid, owner key objectid, adjusted offset.
1156 * The only obvious corruption can happen in that offset.
1157 */
1158 case BTRFS_EXTENT_DATA_REF_KEY:
1159 dref = (struct btrfs_extent_data_ref *)(&iref->offset);
1160 dref_offset = btrfs_extent_data_ref_offset(leaf, dref);
1161 if (!IS_ALIGNED(dref_offset, fs_info->sectorsize)) {
1162 extent_err(leaf, slot,
1163 "invalid data ref offset, have %llu expect aligned to %u",
1164 dref_offset, fs_info->sectorsize);
1165 return -EUCLEAN;
1166 }
1167 inline_refs += btrfs_extent_data_ref_count(leaf, dref);
1168 break;
1169 /* Contains parent bytenr and ref count */
1170 case BTRFS_SHARED_DATA_REF_KEY:
1171 sref = (struct btrfs_shared_data_ref *)(iref + 1);
1172 if (!IS_ALIGNED(inline_offset, fs_info->sectorsize)) {
1173 extent_err(leaf, slot,
1174 "invalid data parent bytenr, have %llu expect aligned to %u",
1175 inline_offset, fs_info->sectorsize);
1176 return -EUCLEAN;
1177 }
1178 inline_refs += btrfs_shared_data_ref_count(leaf, sref);
1179 break;
1180 default:
1181 extent_err(leaf, slot, "unknown inline ref type: %u",
1182 inline_type);
1183 return -EUCLEAN;
1184 }
1185 ptr += btrfs_extent_inline_ref_size(inline_type);
1186 }
1187 /* No padding is allowed */
1188 if (ptr != end) {
1189 extent_err(leaf, slot,
1190 "invalid extent item size, padding bytes found");
1191 return -EUCLEAN;
1192 }
1193
1194 /* Finally, check the inline refs against total refs */
1195 if (inline_refs > total_refs) {
1196 extent_err(leaf, slot,
1197 "invalid extent refs, have %llu expect >= inline %llu",
1198 total_refs, inline_refs);
1199 return -EUCLEAN;
1200 }
1201 return 0;
1202}
1203
Qu Wenruoe2406a62019-08-09 09:24:23 +08001204static int check_simple_keyed_refs(struct extent_buffer *leaf,
1205 struct btrfs_key *key, int slot)
1206{
1207 u32 expect_item_size = 0;
1208
1209 if (key->type == BTRFS_SHARED_DATA_REF_KEY)
1210 expect_item_size = sizeof(struct btrfs_shared_data_ref);
1211
1212 if (btrfs_item_size_nr(leaf, slot) != expect_item_size) {
1213 generic_err(leaf, slot,
1214 "invalid item size, have %u expect %u for key type %u",
1215 btrfs_item_size_nr(leaf, slot),
1216 expect_item_size, key->type);
1217 return -EUCLEAN;
1218 }
1219 if (!IS_ALIGNED(key->objectid, leaf->fs_info->sectorsize)) {
1220 generic_err(leaf, slot,
1221"invalid key objectid for shared block ref, have %llu expect aligned to %u",
1222 key->objectid, leaf->fs_info->sectorsize);
1223 return -EUCLEAN;
1224 }
1225 if (key->type != BTRFS_TREE_BLOCK_REF_KEY &&
1226 !IS_ALIGNED(key->offset, leaf->fs_info->sectorsize)) {
1227 extent_err(leaf, slot,
1228 "invalid tree parent bytenr, have %llu expect aligned to %u",
1229 key->offset, leaf->fs_info->sectorsize);
1230 return -EUCLEAN;
1231 }
1232 return 0;
1233}
1234
Qu Wenruo0785a9a2019-08-09 09:24:24 +08001235static int check_extent_data_ref(struct extent_buffer *leaf,
1236 struct btrfs_key *key, int slot)
1237{
1238 struct btrfs_extent_data_ref *dref;
1239 unsigned long ptr = btrfs_item_ptr_offset(leaf, slot);
1240 const unsigned long end = ptr + btrfs_item_size_nr(leaf, slot);
1241
1242 if (btrfs_item_size_nr(leaf, slot) % sizeof(*dref) != 0) {
1243 generic_err(leaf, slot,
1244 "invalid item size, have %u expect aligned to %zu for key type %u",
1245 btrfs_item_size_nr(leaf, slot),
1246 sizeof(*dref), key->type);
1247 }
1248 if (!IS_ALIGNED(key->objectid, leaf->fs_info->sectorsize)) {
1249 generic_err(leaf, slot,
1250"invalid key objectid for shared block ref, have %llu expect aligned to %u",
1251 key->objectid, leaf->fs_info->sectorsize);
1252 return -EUCLEAN;
1253 }
1254 for (; ptr < end; ptr += sizeof(*dref)) {
1255 u64 root_objectid;
1256 u64 owner;
1257 u64 offset;
1258 u64 hash;
1259
1260 dref = (struct btrfs_extent_data_ref *)ptr;
1261 root_objectid = btrfs_extent_data_ref_root(leaf, dref);
1262 owner = btrfs_extent_data_ref_objectid(leaf, dref);
1263 offset = btrfs_extent_data_ref_offset(leaf, dref);
1264 hash = hash_extent_data_ref(root_objectid, owner, offset);
1265 if (hash != key->offset) {
1266 extent_err(leaf, slot,
1267 "invalid extent data ref hash, item has 0x%016llx key has 0x%016llx",
1268 hash, key->offset);
1269 return -EUCLEAN;
1270 }
1271 if (!IS_ALIGNED(offset, leaf->fs_info->sectorsize)) {
1272 extent_err(leaf, slot,
1273 "invalid extent data backref offset, have %llu expect aligned to %u",
1274 offset, leaf->fs_info->sectorsize);
1275 }
1276 }
1277 return 0;
1278}
1279
Qu Wenruo71bf92a92019-08-26 15:40:39 +08001280#define inode_ref_err(fs_info, eb, slot, fmt, args...) \
1281 inode_item_err(fs_info, eb, slot, fmt, ##args)
1282static int check_inode_ref(struct extent_buffer *leaf,
1283 struct btrfs_key *key, struct btrfs_key *prev_key,
1284 int slot)
1285{
1286 struct btrfs_inode_ref *iref;
1287 unsigned long ptr;
1288 unsigned long end;
1289
Qu Wenruo80d7fd12019-10-04 17:31:32 +08001290 if (!check_prev_ino(leaf, key, slot, prev_key))
1291 return -EUCLEAN;
Qu Wenruo71bf92a92019-08-26 15:40:39 +08001292 /* namelen can't be 0, so item_size == sizeof() is also invalid */
1293 if (btrfs_item_size_nr(leaf, slot) <= sizeof(*iref)) {
1294 inode_ref_err(fs_info, leaf, slot,
1295 "invalid item size, have %u expect (%zu, %u)",
1296 btrfs_item_size_nr(leaf, slot),
1297 sizeof(*iref), BTRFS_LEAF_DATA_SIZE(leaf->fs_info));
1298 return -EUCLEAN;
1299 }
1300
1301 ptr = btrfs_item_ptr_offset(leaf, slot);
1302 end = ptr + btrfs_item_size_nr(leaf, slot);
1303 while (ptr < end) {
1304 u16 namelen;
1305
1306 if (ptr + sizeof(iref) > end) {
1307 inode_ref_err(fs_info, leaf, slot,
1308 "inode ref overflow, ptr %lu end %lu inode_ref_size %zu",
1309 ptr, end, sizeof(iref));
1310 return -EUCLEAN;
1311 }
1312
1313 iref = (struct btrfs_inode_ref *)ptr;
1314 namelen = btrfs_inode_ref_name_len(leaf, iref);
1315 if (ptr + sizeof(*iref) + namelen > end) {
1316 inode_ref_err(fs_info, leaf, slot,
1317 "inode ref overflow, ptr %lu end %lu namelen %u",
1318 ptr, end, namelen);
1319 return -EUCLEAN;
1320 }
1321
1322 /*
1323 * NOTE: In theory we should record all found index numbers
1324 * to find any duplicated indexes, but that will be too time
1325 * consuming for inodes with too many hard links.
1326 */
1327 ptr += sizeof(*iref) + namelen;
1328 }
1329 return 0;
1330}
1331
Qu Wenruo82fc28f2019-03-20 13:16:42 +08001332/*
Qu Wenruo557ea5d2017-10-09 01:51:02 +00001333 * Common point to switch the item-specific validation.
1334 */
David Sterba0076bc892019-03-20 16:22:00 +01001335static int check_leaf_item(struct extent_buffer *leaf,
Filipe Manana4e9845e2019-05-06 16:44:12 +01001336 struct btrfs_key *key, int slot,
1337 struct btrfs_key *prev_key)
Qu Wenruo557ea5d2017-10-09 01:51:02 +00001338{
1339 int ret = 0;
Qu Wenruo075cb3c2019-03-20 13:42:33 +08001340 struct btrfs_chunk *chunk;
Qu Wenruo557ea5d2017-10-09 01:51:02 +00001341
1342 switch (key->type) {
1343 case BTRFS_EXTENT_DATA_KEY:
Filipe Manana4e9845e2019-05-06 16:44:12 +01001344 ret = check_extent_data_item(leaf, key, slot, prev_key);
Qu Wenruo557ea5d2017-10-09 01:51:02 +00001345 break;
1346 case BTRFS_EXTENT_CSUM_KEY:
David Sterba68128ce2019-03-20 16:02:56 +01001347 ret = check_csum_item(leaf, key, slot);
Qu Wenruo557ea5d2017-10-09 01:51:02 +00001348 break;
Qu Wenruoad7b03682017-11-08 08:54:25 +08001349 case BTRFS_DIR_ITEM_KEY:
1350 case BTRFS_DIR_INDEX_KEY:
1351 case BTRFS_XATTR_ITEM_KEY:
Qu Wenruoc18679e2019-08-26 15:40:38 +08001352 ret = check_dir_item(leaf, key, prev_key, slot);
Qu Wenruoad7b03682017-11-08 08:54:25 +08001353 break;
Qu Wenruo71bf92a92019-08-26 15:40:39 +08001354 case BTRFS_INODE_REF_KEY:
1355 ret = check_inode_ref(leaf, key, prev_key, slot);
1356 break;
Qu Wenruofce466e2018-07-03 17:10:05 +08001357 case BTRFS_BLOCK_GROUP_ITEM_KEY:
David Sterbaaf60ce22019-03-20 16:19:31 +01001358 ret = check_block_group_item(leaf, key, slot);
Qu Wenruofce466e2018-07-03 17:10:05 +08001359 break;
Qu Wenruo075cb3c2019-03-20 13:42:33 +08001360 case BTRFS_CHUNK_ITEM_KEY:
1361 chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
David Sterbaddaf1d52019-03-20 16:40:48 +01001362 ret = btrfs_check_chunk_valid(leaf, chunk, key->offset);
Qu Wenruo075cb3c2019-03-20 13:42:33 +08001363 break;
Qu Wenruoab4ba2e2019-03-08 14:20:03 +08001364 case BTRFS_DEV_ITEM_KEY:
David Sterba412a2312019-03-20 16:22:58 +01001365 ret = check_dev_item(leaf, key, slot);
Qu Wenruoab4ba2e2019-03-08 14:20:03 +08001366 break;
Qu Wenruo496245c2019-03-13 14:31:35 +08001367 case BTRFS_INODE_ITEM_KEY:
David Sterba39e57f42019-03-20 16:22:58 +01001368 ret = check_inode_item(leaf, key, slot);
Qu Wenruo496245c2019-03-13 14:31:35 +08001369 break;
Qu Wenruo259ee772019-07-16 17:00:34 +08001370 case BTRFS_ROOT_ITEM_KEY:
1371 ret = check_root_item(leaf, key, slot);
1372 break;
Qu Wenruof82d1c7c2019-08-09 09:24:22 +08001373 case BTRFS_EXTENT_ITEM_KEY:
1374 case BTRFS_METADATA_ITEM_KEY:
1375 ret = check_extent_item(leaf, key, slot);
1376 break;
Qu Wenruoe2406a62019-08-09 09:24:23 +08001377 case BTRFS_TREE_BLOCK_REF_KEY:
1378 case BTRFS_SHARED_DATA_REF_KEY:
1379 case BTRFS_SHARED_BLOCK_REF_KEY:
1380 ret = check_simple_keyed_refs(leaf, key, slot);
1381 break;
Qu Wenruo0785a9a2019-08-09 09:24:24 +08001382 case BTRFS_EXTENT_DATA_REF_KEY:
1383 ret = check_extent_data_ref(leaf, key, slot);
1384 break;
Qu Wenruo557ea5d2017-10-09 01:51:02 +00001385 }
1386 return ret;
1387}
1388
David Sterbae2ccd362019-03-20 16:22:58 +01001389static int check_leaf(struct extent_buffer *leaf, bool check_item_data)
Qu Wenruo557ea5d2017-10-09 01:51:02 +00001390{
David Sterbae2ccd362019-03-20 16:22:58 +01001391 struct btrfs_fs_info *fs_info = leaf->fs_info;
Qu Wenruo557ea5d2017-10-09 01:51:02 +00001392 /* No valid key type is 0, so all key should be larger than this key */
1393 struct btrfs_key prev_key = {0, 0, 0};
1394 struct btrfs_key key;
1395 u32 nritems = btrfs_header_nritems(leaf);
1396 int slot;
1397
Qu Wenruof556faa2018-09-28 07:59:34 +08001398 if (btrfs_header_level(leaf) != 0) {
David Sterba86a6be32019-03-20 15:31:28 +01001399 generic_err(leaf, 0,
Qu Wenruof556faa2018-09-28 07:59:34 +08001400 "invalid level for leaf, have %d expect 0",
1401 btrfs_header_level(leaf));
1402 return -EUCLEAN;
1403 }
1404
Qu Wenruo557ea5d2017-10-09 01:51:02 +00001405 /*
1406 * Extent buffers from a relocation tree have a owner field that
1407 * corresponds to the subvolume tree they are based on. So just from an
1408 * extent buffer alone we can not find out what is the id of the
1409 * corresponding subvolume tree, so we can not figure out if the extent
1410 * buffer corresponds to the root of the relocation tree or not. So
1411 * skip this check for relocation trees.
1412 */
1413 if (nritems == 0 && !btrfs_header_flag(leaf, BTRFS_HEADER_FLAG_RELOC)) {
Qu Wenruoba480dd2018-07-03 17:10:06 +08001414 u64 owner = btrfs_header_owner(leaf);
Qu Wenruo557ea5d2017-10-09 01:51:02 +00001415
Qu Wenruoba480dd2018-07-03 17:10:06 +08001416 /* These trees must never be empty */
1417 if (owner == BTRFS_ROOT_TREE_OBJECTID ||
1418 owner == BTRFS_CHUNK_TREE_OBJECTID ||
1419 owner == BTRFS_EXTENT_TREE_OBJECTID ||
1420 owner == BTRFS_DEV_TREE_OBJECTID ||
1421 owner == BTRFS_FS_TREE_OBJECTID ||
1422 owner == BTRFS_DATA_RELOC_TREE_OBJECTID) {
David Sterba86a6be32019-03-20 15:31:28 +01001423 generic_err(leaf, 0,
Qu Wenruoba480dd2018-07-03 17:10:06 +08001424 "invalid root, root %llu must never be empty",
1425 owner);
1426 return -EUCLEAN;
1427 }
Qu Wenruo62fdaa52019-08-22 10:14:15 +08001428 /* Unknown tree */
1429 if (owner == 0) {
1430 generic_err(leaf, 0,
1431 "invalid owner, root 0 is not defined");
1432 return -EUCLEAN;
1433 }
Qu Wenruo557ea5d2017-10-09 01:51:02 +00001434 return 0;
1435 }
1436
1437 if (nritems == 0)
1438 return 0;
1439
1440 /*
1441 * Check the following things to make sure this is a good leaf, and
1442 * leaf users won't need to bother with similar sanity checks:
1443 *
1444 * 1) key ordering
1445 * 2) item offset and size
1446 * No overlap, no hole, all inside the leaf.
1447 * 3) item content
1448 * If possible, do comprehensive sanity check.
1449 * NOTE: All checks must only rely on the item data itself.
1450 */
1451 for (slot = 0; slot < nritems; slot++) {
1452 u32 item_end_expected;
1453 int ret;
1454
1455 btrfs_item_key_to_cpu(leaf, &key, slot);
1456
1457 /* Make sure the keys are in the right order */
1458 if (btrfs_comp_cpu_keys(&prev_key, &key) >= 0) {
David Sterba86a6be32019-03-20 15:31:28 +01001459 generic_err(leaf, slot,
Qu Wenruo478d01b2017-10-09 01:51:04 +00001460 "bad key order, prev (%llu %u %llu) current (%llu %u %llu)",
1461 prev_key.objectid, prev_key.type,
1462 prev_key.offset, key.objectid, key.type,
1463 key.offset);
Qu Wenruo557ea5d2017-10-09 01:51:02 +00001464 return -EUCLEAN;
1465 }
1466
1467 /*
1468 * Make sure the offset and ends are right, remember that the
1469 * item data starts at the end of the leaf and grows towards the
1470 * front.
1471 */
1472 if (slot == 0)
1473 item_end_expected = BTRFS_LEAF_DATA_SIZE(fs_info);
1474 else
1475 item_end_expected = btrfs_item_offset_nr(leaf,
1476 slot - 1);
1477 if (btrfs_item_end_nr(leaf, slot) != item_end_expected) {
David Sterba86a6be32019-03-20 15:31:28 +01001478 generic_err(leaf, slot,
Qu Wenruo478d01b2017-10-09 01:51:04 +00001479 "unexpected item end, have %u expect %u",
1480 btrfs_item_end_nr(leaf, slot),
1481 item_end_expected);
Qu Wenruo557ea5d2017-10-09 01:51:02 +00001482 return -EUCLEAN;
1483 }
1484
1485 /*
1486 * Check to make sure that we don't point outside of the leaf,
1487 * just in case all the items are consistent to each other, but
1488 * all point outside of the leaf.
1489 */
1490 if (btrfs_item_end_nr(leaf, slot) >
1491 BTRFS_LEAF_DATA_SIZE(fs_info)) {
David Sterba86a6be32019-03-20 15:31:28 +01001492 generic_err(leaf, slot,
Qu Wenruo478d01b2017-10-09 01:51:04 +00001493 "slot end outside of leaf, have %u expect range [0, %u]",
1494 btrfs_item_end_nr(leaf, slot),
1495 BTRFS_LEAF_DATA_SIZE(fs_info));
Qu Wenruo557ea5d2017-10-09 01:51:02 +00001496 return -EUCLEAN;
1497 }
1498
1499 /* Also check if the item pointer overlaps with btrfs item. */
1500 if (btrfs_item_nr_offset(slot) + sizeof(struct btrfs_item) >
1501 btrfs_item_ptr_offset(leaf, slot)) {
David Sterba86a6be32019-03-20 15:31:28 +01001502 generic_err(leaf, slot,
Qu Wenruo478d01b2017-10-09 01:51:04 +00001503 "slot overlaps with its data, item end %lu data start %lu",
1504 btrfs_item_nr_offset(slot) +
1505 sizeof(struct btrfs_item),
1506 btrfs_item_ptr_offset(leaf, slot));
Qu Wenruo557ea5d2017-10-09 01:51:02 +00001507 return -EUCLEAN;
1508 }
1509
Qu Wenruo69fc6cb2017-11-08 08:54:24 +08001510 if (check_item_data) {
1511 /*
1512 * Check if the item size and content meet other
1513 * criteria
1514 */
Filipe Manana4e9845e2019-05-06 16:44:12 +01001515 ret = check_leaf_item(leaf, &key, slot, &prev_key);
Qu Wenruo69fc6cb2017-11-08 08:54:24 +08001516 if (ret < 0)
1517 return ret;
1518 }
Qu Wenruo557ea5d2017-10-09 01:51:02 +00001519
1520 prev_key.objectid = key.objectid;
1521 prev_key.type = key.type;
1522 prev_key.offset = key.offset;
1523 }
1524
1525 return 0;
1526}
1527
David Sterba1c4360e2019-03-20 16:23:29 +01001528int btrfs_check_leaf_full(struct extent_buffer *leaf)
Qu Wenruo69fc6cb2017-11-08 08:54:24 +08001529{
David Sterbae2ccd362019-03-20 16:22:58 +01001530 return check_leaf(leaf, true);
Qu Wenruo69fc6cb2017-11-08 08:54:24 +08001531}
Qu Wenruo02529d72019-04-24 15:22:53 +08001532ALLOW_ERROR_INJECTION(btrfs_check_leaf_full, ERRNO);
Qu Wenruo69fc6cb2017-11-08 08:54:24 +08001533
David Sterbacfdaad52019-03-20 16:24:18 +01001534int btrfs_check_leaf_relaxed(struct extent_buffer *leaf)
Qu Wenruo69fc6cb2017-11-08 08:54:24 +08001535{
David Sterbae2ccd362019-03-20 16:22:58 +01001536 return check_leaf(leaf, false);
Qu Wenruo69fc6cb2017-11-08 08:54:24 +08001537}
1538
David Sterba813fd1d2019-03-20 16:25:00 +01001539int btrfs_check_node(struct extent_buffer *node)
Qu Wenruo557ea5d2017-10-09 01:51:02 +00001540{
David Sterba813fd1d2019-03-20 16:25:00 +01001541 struct btrfs_fs_info *fs_info = node->fs_info;
Qu Wenruo557ea5d2017-10-09 01:51:02 +00001542 unsigned long nr = btrfs_header_nritems(node);
1543 struct btrfs_key key, next_key;
1544 int slot;
Qu Wenruof556faa2018-09-28 07:59:34 +08001545 int level = btrfs_header_level(node);
Qu Wenruo557ea5d2017-10-09 01:51:02 +00001546 u64 bytenr;
1547 int ret = 0;
1548
Qu Wenruof556faa2018-09-28 07:59:34 +08001549 if (level <= 0 || level >= BTRFS_MAX_LEVEL) {
David Sterba86a6be32019-03-20 15:31:28 +01001550 generic_err(node, 0,
Qu Wenruof556faa2018-09-28 07:59:34 +08001551 "invalid level for node, have %d expect [1, %d]",
1552 level, BTRFS_MAX_LEVEL - 1);
1553 return -EUCLEAN;
1554 }
Qu Wenruo2f659542018-01-25 14:56:18 +08001555 if (nr == 0 || nr > BTRFS_NODEPTRS_PER_BLOCK(fs_info)) {
1556 btrfs_crit(fs_info,
Qu Wenruobba4f292017-10-09 01:51:03 +00001557"corrupt node: root=%llu block=%llu, nritems too %s, have %lu expect range [1,%u]",
Qu Wenruo2f659542018-01-25 14:56:18 +08001558 btrfs_header_owner(node), node->start,
Qu Wenruobba4f292017-10-09 01:51:03 +00001559 nr == 0 ? "small" : "large", nr,
Qu Wenruo2f659542018-01-25 14:56:18 +08001560 BTRFS_NODEPTRS_PER_BLOCK(fs_info));
Qu Wenruobba4f292017-10-09 01:51:03 +00001561 return -EUCLEAN;
Qu Wenruo557ea5d2017-10-09 01:51:02 +00001562 }
1563
1564 for (slot = 0; slot < nr - 1; slot++) {
1565 bytenr = btrfs_node_blockptr(node, slot);
1566 btrfs_node_key_to_cpu(node, &key, slot);
1567 btrfs_node_key_to_cpu(node, &next_key, slot + 1);
1568
1569 if (!bytenr) {
David Sterba86a6be32019-03-20 15:31:28 +01001570 generic_err(node, slot,
Qu Wenruobba4f292017-10-09 01:51:03 +00001571 "invalid NULL node pointer");
1572 ret = -EUCLEAN;
1573 goto out;
1574 }
Qu Wenruo2f659542018-01-25 14:56:18 +08001575 if (!IS_ALIGNED(bytenr, fs_info->sectorsize)) {
David Sterba86a6be32019-03-20 15:31:28 +01001576 generic_err(node, slot,
Qu Wenruobba4f292017-10-09 01:51:03 +00001577 "unaligned pointer, have %llu should be aligned to %u",
Qu Wenruo2f659542018-01-25 14:56:18 +08001578 bytenr, fs_info->sectorsize);
Qu Wenruobba4f292017-10-09 01:51:03 +00001579 ret = -EUCLEAN;
Qu Wenruo557ea5d2017-10-09 01:51:02 +00001580 goto out;
1581 }
1582
1583 if (btrfs_comp_cpu_keys(&key, &next_key) >= 0) {
David Sterba86a6be32019-03-20 15:31:28 +01001584 generic_err(node, slot,
Qu Wenruobba4f292017-10-09 01:51:03 +00001585 "bad key order, current (%llu %u %llu) next (%llu %u %llu)",
1586 key.objectid, key.type, key.offset,
1587 next_key.objectid, next_key.type,
1588 next_key.offset);
1589 ret = -EUCLEAN;
Qu Wenruo557ea5d2017-10-09 01:51:02 +00001590 goto out;
1591 }
1592 }
1593out:
1594 return ret;
1595}
Qu Wenruo02529d72019-04-24 15:22:53 +08001596ALLOW_ERROR_INJECTION(btrfs_check_node, ERRNO);