Commit | Line | Data |
---|---|---|
557ea5dd QW |
1 | /* |
2 | * Copyright (C) Qu Wenruo 2017. All rights reserved. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or | |
5 | * modify it under the terms of the GNU General Public | |
6 | * License v2 as published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, | |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
11 | * General Public License for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public | |
14 | * License along with this program. | |
15 | */ | |
16 | ||
17 | /* | |
18 | * The module is used to catch unexpected/corrupted tree block data. | |
19 | * Such behavior can be caused either by a fuzzed image or bugs. | |
20 | * | |
21 | * The objective is to do leaf/node validation checks when tree block is read | |
22 | * from disk, and check *every* possible member, so other code won't | |
23 | * need to checking them again. | |
24 | * | |
25 | * Due to the potential and unwanted damage, every checker needs to be | |
26 | * carefully reviewed otherwise so it does not prevent mount of valid images. | |
27 | */ | |
28 | ||
29 | #include "ctree.h" | |
30 | #include "tree-checker.h" | |
31 | #include "disk-io.h" | |
32 | #include "compression.h" | |
33 | ||
bba4f298 QW |
34 | /* |
35 | * Error message should follow the following format: | |
36 | * corrupt <type>: <identifier>, <reason>[, <bad_value>] | |
37 | * | |
38 | * @type: leaf or node | |
39 | * @identifier: the necessary info to locate the leaf/node. | |
40 | * It's recommened to decode key.objecitd/offset if it's | |
41 | * meaningful. | |
42 | * @reason: describe the error | |
43 | * @bad_value: optional, it's recommened to output bad value and its | |
44 | * expected value (range). | |
45 | * | |
46 | * Since comma is used to separate the components, only space is allowed | |
47 | * inside each component. | |
48 | */ | |
49 | ||
50 | /* | |
51 | * Append generic "corrupt leaf/node root=%llu block=%llu slot=%d: " to @fmt. | |
52 | * Allows callers to customize the output. | |
53 | */ | |
54 | __printf(4, 5) | |
2f659546 | 55 | static void generic_err(const struct btrfs_fs_info *fs_info, |
bba4f298 QW |
56 | const struct extent_buffer *eb, int slot, |
57 | const char *fmt, ...) | |
58 | { | |
59 | struct va_format vaf; | |
60 | va_list args; | |
61 | ||
62 | va_start(args, fmt); | |
63 | ||
64 | vaf.fmt = fmt; | |
65 | vaf.va = &args; | |
66 | ||
2f659546 | 67 | btrfs_crit(fs_info, |
bba4f298 QW |
68 | "corrupt %s: root=%llu block=%llu slot=%d, %pV", |
69 | btrfs_header_level(eb) == 0 ? "leaf" : "node", | |
2f659546 | 70 | btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot, &vaf); |
bba4f298 QW |
71 | va_end(args); |
72 | } | |
73 | ||
8806d718 QW |
74 | /* |
75 | * Customized reporter for extent data item, since its key objectid and | |
76 | * offset has its own meaning. | |
77 | */ | |
78 | __printf(4, 5) | |
2f659546 | 79 | static void file_extent_err(const struct btrfs_fs_info *fs_info, |
8806d718 QW |
80 | const struct extent_buffer *eb, int slot, |
81 | const char *fmt, ...) | |
82 | { | |
83 | struct btrfs_key key; | |
84 | struct va_format vaf; | |
85 | va_list args; | |
86 | ||
87 | btrfs_item_key_to_cpu(eb, &key, slot); | |
88 | va_start(args, fmt); | |
89 | ||
90 | vaf.fmt = fmt; | |
91 | vaf.va = &args; | |
92 | ||
2f659546 | 93 | btrfs_crit(fs_info, |
8806d718 | 94 | "corrupt %s: root=%llu block=%llu slot=%d ino=%llu file_offset=%llu, %pV", |
2f659546 QW |
95 | btrfs_header_level(eb) == 0 ? "leaf" : "node", |
96 | btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot, | |
97 | key.objectid, key.offset, &vaf); | |
8806d718 QW |
98 | va_end(args); |
99 | } | |
100 | ||
101 | /* | |
102 | * Return 0 if the btrfs_file_extent_##name is aligned to @alignment | |
103 | * Else return 1 | |
104 | */ | |
2f659546 | 105 | #define CHECK_FE_ALIGNED(fs_info, leaf, slot, fi, name, alignment) \ |
8806d718 QW |
106 | ({ \ |
107 | if (!IS_ALIGNED(btrfs_file_extent_##name((leaf), (fi)), (alignment))) \ | |
2f659546 | 108 | file_extent_err((fs_info), (leaf), (slot), \ |
8806d718 QW |
109 | "invalid %s for file extent, have %llu, should be aligned to %u", \ |
110 | (#name), btrfs_file_extent_##name((leaf), (fi)), \ | |
111 | (alignment)); \ | |
112 | (!IS_ALIGNED(btrfs_file_extent_##name((leaf), (fi)), (alignment))); \ | |
113 | }) | |
114 | ||
2f659546 | 115 | static int check_extent_data_item(struct btrfs_fs_info *fs_info, |
557ea5dd QW |
116 | struct extent_buffer *leaf, |
117 | struct btrfs_key *key, int slot) | |
118 | { | |
119 | struct btrfs_file_extent_item *fi; | |
2f659546 | 120 | u32 sectorsize = fs_info->sectorsize; |
557ea5dd QW |
121 | u32 item_size = btrfs_item_size_nr(leaf, slot); |
122 | ||
123 | if (!IS_ALIGNED(key->offset, sectorsize)) { | |
2f659546 | 124 | file_extent_err(fs_info, leaf, slot, |
8806d718 QW |
125 | "unaligned file_offset for file extent, have %llu should be aligned to %u", |
126 | key->offset, sectorsize); | |
557ea5dd QW |
127 | return -EUCLEAN; |
128 | } | |
129 | ||
130 | fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item); | |
131 | ||
132 | if (btrfs_file_extent_type(leaf, fi) > BTRFS_FILE_EXTENT_TYPES) { | |
2f659546 | 133 | file_extent_err(fs_info, leaf, slot, |
8806d718 QW |
134 | "invalid type for file extent, have %u expect range [0, %u]", |
135 | btrfs_file_extent_type(leaf, fi), | |
136 | BTRFS_FILE_EXTENT_TYPES); | |
557ea5dd QW |
137 | return -EUCLEAN; |
138 | } | |
139 | ||
140 | /* | |
141 | * Support for new compression/encrption must introduce incompat flag, | |
142 | * and must be caught in open_ctree(). | |
143 | */ | |
144 | if (btrfs_file_extent_compression(leaf, fi) > BTRFS_COMPRESS_TYPES) { | |
2f659546 | 145 | file_extent_err(fs_info, leaf, slot, |
8806d718 QW |
146 | "invalid compression for file extent, have %u expect range [0, %u]", |
147 | btrfs_file_extent_compression(leaf, fi), | |
148 | BTRFS_COMPRESS_TYPES); | |
557ea5dd QW |
149 | return -EUCLEAN; |
150 | } | |
151 | if (btrfs_file_extent_encryption(leaf, fi)) { | |
2f659546 | 152 | file_extent_err(fs_info, leaf, slot, |
8806d718 QW |
153 | "invalid encryption for file extent, have %u expect 0", |
154 | btrfs_file_extent_encryption(leaf, fi)); | |
557ea5dd QW |
155 | return -EUCLEAN; |
156 | } | |
157 | if (btrfs_file_extent_type(leaf, fi) == BTRFS_FILE_EXTENT_INLINE) { | |
158 | /* Inline extent must have 0 as key offset */ | |
159 | if (key->offset) { | |
2f659546 | 160 | file_extent_err(fs_info, leaf, slot, |
8806d718 QW |
161 | "invalid file_offset for inline file extent, have %llu expect 0", |
162 | key->offset); | |
557ea5dd QW |
163 | return -EUCLEAN; |
164 | } | |
165 | ||
166 | /* Compressed inline extent has no on-disk size, skip it */ | |
167 | if (btrfs_file_extent_compression(leaf, fi) != | |
168 | BTRFS_COMPRESS_NONE) | |
169 | return 0; | |
170 | ||
171 | /* Uncompressed inline extent size must match item size */ | |
172 | if (item_size != BTRFS_FILE_EXTENT_INLINE_DATA_START + | |
173 | btrfs_file_extent_ram_bytes(leaf, fi)) { | |
2f659546 | 174 | file_extent_err(fs_info, leaf, slot, |
8806d718 QW |
175 | "invalid ram_bytes for uncompressed inline extent, have %u expect %llu", |
176 | item_size, BTRFS_FILE_EXTENT_INLINE_DATA_START + | |
177 | btrfs_file_extent_ram_bytes(leaf, fi)); | |
557ea5dd QW |
178 | return -EUCLEAN; |
179 | } | |
180 | return 0; | |
181 | } | |
182 | ||
183 | /* Regular or preallocated extent has fixed item size */ | |
184 | if (item_size != sizeof(*fi)) { | |
2f659546 | 185 | file_extent_err(fs_info, leaf, slot, |
709a95c3 | 186 | "invalid item size for reg/prealloc file extent, have %u expect %zu", |
8806d718 | 187 | item_size, sizeof(*fi)); |
557ea5dd QW |
188 | return -EUCLEAN; |
189 | } | |
2f659546 QW |
190 | if (CHECK_FE_ALIGNED(fs_info, leaf, slot, fi, ram_bytes, sectorsize) || |
191 | CHECK_FE_ALIGNED(fs_info, leaf, slot, fi, disk_bytenr, sectorsize) || | |
192 | CHECK_FE_ALIGNED(fs_info, leaf, slot, fi, disk_num_bytes, sectorsize) || | |
193 | CHECK_FE_ALIGNED(fs_info, leaf, slot, fi, offset, sectorsize) || | |
194 | CHECK_FE_ALIGNED(fs_info, leaf, slot, fi, num_bytes, sectorsize)) | |
557ea5dd | 195 | return -EUCLEAN; |
557ea5dd QW |
196 | return 0; |
197 | } | |
198 | ||
2f659546 QW |
199 | static int check_csum_item(struct btrfs_fs_info *fs_info, |
200 | struct extent_buffer *leaf, struct btrfs_key *key, | |
201 | int slot) | |
557ea5dd | 202 | { |
2f659546 QW |
203 | u32 sectorsize = fs_info->sectorsize; |
204 | u32 csumsize = btrfs_super_csum_size(fs_info->super_copy); | |
557ea5dd QW |
205 | |
206 | if (key->objectid != BTRFS_EXTENT_CSUM_OBJECTID) { | |
2f659546 | 207 | generic_err(fs_info, leaf, slot, |
d508c5f0 QW |
208 | "invalid key objectid for csum item, have %llu expect %llu", |
209 | key->objectid, BTRFS_EXTENT_CSUM_OBJECTID); | |
557ea5dd QW |
210 | return -EUCLEAN; |
211 | } | |
212 | if (!IS_ALIGNED(key->offset, sectorsize)) { | |
2f659546 | 213 | generic_err(fs_info, leaf, slot, |
d508c5f0 QW |
214 | "unaligned key offset for csum item, have %llu should be aligned to %u", |
215 | key->offset, sectorsize); | |
557ea5dd QW |
216 | return -EUCLEAN; |
217 | } | |
218 | if (!IS_ALIGNED(btrfs_item_size_nr(leaf, slot), csumsize)) { | |
2f659546 | 219 | generic_err(fs_info, leaf, slot, |
d508c5f0 QW |
220 | "unaligned item size for csum item, have %u should be aligned to %u", |
221 | btrfs_item_size_nr(leaf, slot), csumsize); | |
557ea5dd QW |
222 | return -EUCLEAN; |
223 | } | |
224 | return 0; | |
225 | } | |
226 | ||
ad7b0368 QW |
227 | /* |
228 | * Customized reported for dir_item, only important new info is key->objectid, | |
229 | * which represents inode number | |
230 | */ | |
231 | __printf(4, 5) | |
2f659546 | 232 | static void dir_item_err(const struct btrfs_fs_info *fs_info, |
ad7b0368 QW |
233 | const struct extent_buffer *eb, int slot, |
234 | const char *fmt, ...) | |
235 | { | |
236 | struct btrfs_key key; | |
237 | struct va_format vaf; | |
238 | va_list args; | |
239 | ||
240 | btrfs_item_key_to_cpu(eb, &key, slot); | |
241 | va_start(args, fmt); | |
242 | ||
243 | vaf.fmt = fmt; | |
244 | vaf.va = &args; | |
245 | ||
2f659546 | 246 | btrfs_crit(fs_info, |
ad7b0368 | 247 | "corrupt %s: root=%llu block=%llu slot=%d ino=%llu, %pV", |
2f659546 QW |
248 | btrfs_header_level(eb) == 0 ? "leaf" : "node", |
249 | btrfs_header_owner(eb), btrfs_header_bytenr(eb), slot, | |
250 | key.objectid, &vaf); | |
ad7b0368 QW |
251 | va_end(args); |
252 | } | |
253 | ||
2f659546 | 254 | static int check_dir_item(struct btrfs_fs_info *fs_info, |
ad7b0368 QW |
255 | struct extent_buffer *leaf, |
256 | struct btrfs_key *key, int slot) | |
257 | { | |
258 | struct btrfs_dir_item *di; | |
259 | u32 item_size = btrfs_item_size_nr(leaf, slot); | |
260 | u32 cur = 0; | |
261 | ||
262 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); | |
263 | while (cur < item_size) { | |
ad7b0368 QW |
264 | u32 name_len; |
265 | u32 data_len; | |
266 | u32 max_name_len; | |
267 | u32 total_size; | |
268 | u32 name_hash; | |
269 | u8 dir_type; | |
270 | ||
271 | /* header itself should not cross item boundary */ | |
272 | if (cur + sizeof(*di) > item_size) { | |
2f659546 | 273 | dir_item_err(fs_info, leaf, slot, |
7cfad652 | 274 | "dir item header crosses item boundary, have %zu boundary %u", |
ad7b0368 QW |
275 | cur + sizeof(*di), item_size); |
276 | return -EUCLEAN; | |
277 | } | |
278 | ||
279 | /* dir type check */ | |
280 | dir_type = btrfs_dir_type(leaf, di); | |
281 | if (dir_type >= BTRFS_FT_MAX) { | |
2f659546 | 282 | dir_item_err(fs_info, leaf, slot, |
ad7b0368 QW |
283 | "invalid dir item type, have %u expect [0, %u)", |
284 | dir_type, BTRFS_FT_MAX); | |
285 | return -EUCLEAN; | |
286 | } | |
287 | ||
288 | if (key->type == BTRFS_XATTR_ITEM_KEY && | |
289 | dir_type != BTRFS_FT_XATTR) { | |
2f659546 | 290 | dir_item_err(fs_info, leaf, slot, |
ad7b0368 QW |
291 | "invalid dir item type for XATTR key, have %u expect %u", |
292 | dir_type, BTRFS_FT_XATTR); | |
293 | return -EUCLEAN; | |
294 | } | |
295 | if (dir_type == BTRFS_FT_XATTR && | |
296 | key->type != BTRFS_XATTR_ITEM_KEY) { | |
2f659546 | 297 | dir_item_err(fs_info, leaf, slot, |
ad7b0368 QW |
298 | "xattr dir type found for non-XATTR key"); |
299 | return -EUCLEAN; | |
300 | } | |
301 | if (dir_type == BTRFS_FT_XATTR) | |
302 | max_name_len = XATTR_NAME_MAX; | |
303 | else | |
304 | max_name_len = BTRFS_NAME_LEN; | |
305 | ||
306 | /* Name/data length check */ | |
307 | name_len = btrfs_dir_name_len(leaf, di); | |
308 | data_len = btrfs_dir_data_len(leaf, di); | |
309 | if (name_len > max_name_len) { | |
2f659546 | 310 | dir_item_err(fs_info, leaf, slot, |
ad7b0368 QW |
311 | "dir item name len too long, have %u max %u", |
312 | name_len, max_name_len); | |
313 | return -EUCLEAN; | |
314 | } | |
2f659546 QW |
315 | if (name_len + data_len > BTRFS_MAX_XATTR_SIZE(fs_info)) { |
316 | dir_item_err(fs_info, leaf, slot, | |
ad7b0368 QW |
317 | "dir item name and data len too long, have %u max %u", |
318 | name_len + data_len, | |
2f659546 | 319 | BTRFS_MAX_XATTR_SIZE(fs_info)); |
ad7b0368 QW |
320 | return -EUCLEAN; |
321 | } | |
322 | ||
323 | if (data_len && dir_type != BTRFS_FT_XATTR) { | |
2f659546 | 324 | dir_item_err(fs_info, leaf, slot, |
ad7b0368 QW |
325 | "dir item with invalid data len, have %u expect 0", |
326 | data_len); | |
327 | return -EUCLEAN; | |
328 | } | |
329 | ||
330 | total_size = sizeof(*di) + name_len + data_len; | |
331 | ||
332 | /* header and name/data should not cross item boundary */ | |
333 | if (cur + total_size > item_size) { | |
2f659546 | 334 | dir_item_err(fs_info, leaf, slot, |
ad7b0368 QW |
335 | "dir item data crosses item boundary, have %u boundary %u", |
336 | cur + total_size, item_size); | |
337 | return -EUCLEAN; | |
338 | } | |
339 | ||
340 | /* | |
341 | * Special check for XATTR/DIR_ITEM, as key->offset is name | |
342 | * hash, should match its name | |
343 | */ | |
344 | if (key->type == BTRFS_DIR_ITEM_KEY || | |
345 | key->type == BTRFS_XATTR_ITEM_KEY) { | |
e2683fc9 DS |
346 | char namebuf[max(BTRFS_NAME_LEN, XATTR_NAME_MAX)]; |
347 | ||
ad7b0368 QW |
348 | read_extent_buffer(leaf, namebuf, |
349 | (unsigned long)(di + 1), name_len); | |
350 | name_hash = btrfs_name_hash(namebuf, name_len); | |
351 | if (key->offset != name_hash) { | |
2f659546 | 352 | dir_item_err(fs_info, leaf, slot, |
ad7b0368 QW |
353 | "name hash mismatch with key, have 0x%016x expect 0x%016llx", |
354 | name_hash, key->offset); | |
355 | return -EUCLEAN; | |
356 | } | |
357 | } | |
358 | cur += total_size; | |
359 | di = (struct btrfs_dir_item *)((void *)di + total_size); | |
360 | } | |
361 | return 0; | |
362 | } | |
363 | ||
557ea5dd QW |
364 | /* |
365 | * Common point to switch the item-specific validation. | |
366 | */ | |
2f659546 | 367 | static int check_leaf_item(struct btrfs_fs_info *fs_info, |
557ea5dd QW |
368 | struct extent_buffer *leaf, |
369 | struct btrfs_key *key, int slot) | |
370 | { | |
371 | int ret = 0; | |
372 | ||
373 | switch (key->type) { | |
374 | case BTRFS_EXTENT_DATA_KEY: | |
2f659546 | 375 | ret = check_extent_data_item(fs_info, leaf, key, slot); |
557ea5dd QW |
376 | break; |
377 | case BTRFS_EXTENT_CSUM_KEY: | |
2f659546 | 378 | ret = check_csum_item(fs_info, leaf, key, slot); |
557ea5dd | 379 | break; |
ad7b0368 QW |
380 | case BTRFS_DIR_ITEM_KEY: |
381 | case BTRFS_DIR_INDEX_KEY: | |
382 | case BTRFS_XATTR_ITEM_KEY: | |
2f659546 | 383 | ret = check_dir_item(fs_info, leaf, key, slot); |
ad7b0368 | 384 | break; |
557ea5dd QW |
385 | } |
386 | return ret; | |
387 | } | |
388 | ||
2f659546 | 389 | static int check_leaf(struct btrfs_fs_info *fs_info, struct extent_buffer *leaf, |
69fc6cbb | 390 | bool check_item_data) |
557ea5dd | 391 | { |
557ea5dd QW |
392 | /* No valid key type is 0, so all key should be larger than this key */ |
393 | struct btrfs_key prev_key = {0, 0, 0}; | |
394 | struct btrfs_key key; | |
395 | u32 nritems = btrfs_header_nritems(leaf); | |
396 | int slot; | |
397 | ||
398 | /* | |
399 | * Extent buffers from a relocation tree have a owner field that | |
400 | * corresponds to the subvolume tree they are based on. So just from an | |
401 | * extent buffer alone we can not find out what is the id of the | |
402 | * corresponding subvolume tree, so we can not figure out if the extent | |
403 | * buffer corresponds to the root of the relocation tree or not. So | |
404 | * skip this check for relocation trees. | |
405 | */ | |
406 | if (nritems == 0 && !btrfs_header_flag(leaf, BTRFS_HEADER_FLAG_RELOC)) { | |
407 | struct btrfs_root *check_root; | |
408 | ||
409 | key.objectid = btrfs_header_owner(leaf); | |
410 | key.type = BTRFS_ROOT_ITEM_KEY; | |
411 | key.offset = (u64)-1; | |
412 | ||
413 | check_root = btrfs_get_fs_root(fs_info, &key, false); | |
414 | /* | |
415 | * The only reason we also check NULL here is that during | |
416 | * open_ctree() some roots has not yet been set up. | |
417 | */ | |
418 | if (!IS_ERR_OR_NULL(check_root)) { | |
419 | struct extent_buffer *eb; | |
420 | ||
421 | eb = btrfs_root_node(check_root); | |
422 | /* if leaf is the root, then it's fine */ | |
423 | if (leaf != eb) { | |
2f659546 | 424 | generic_err(fs_info, leaf, 0, |
478d01b3 QW |
425 | "invalid nritems, have %u should not be 0 for non-root leaf", |
426 | nritems); | |
557ea5dd QW |
427 | free_extent_buffer(eb); |
428 | return -EUCLEAN; | |
429 | } | |
430 | free_extent_buffer(eb); | |
431 | } | |
432 | return 0; | |
433 | } | |
434 | ||
435 | if (nritems == 0) | |
436 | return 0; | |
437 | ||
438 | /* | |
439 | * Check the following things to make sure this is a good leaf, and | |
440 | * leaf users won't need to bother with similar sanity checks: | |
441 | * | |
442 | * 1) key ordering | |
443 | * 2) item offset and size | |
444 | * No overlap, no hole, all inside the leaf. | |
445 | * 3) item content | |
446 | * If possible, do comprehensive sanity check. | |
447 | * NOTE: All checks must only rely on the item data itself. | |
448 | */ | |
449 | for (slot = 0; slot < nritems; slot++) { | |
450 | u32 item_end_expected; | |
451 | int ret; | |
452 | ||
453 | btrfs_item_key_to_cpu(leaf, &key, slot); | |
454 | ||
455 | /* Make sure the keys are in the right order */ | |
456 | if (btrfs_comp_cpu_keys(&prev_key, &key) >= 0) { | |
2f659546 | 457 | generic_err(fs_info, leaf, slot, |
478d01b3 QW |
458 | "bad key order, prev (%llu %u %llu) current (%llu %u %llu)", |
459 | prev_key.objectid, prev_key.type, | |
460 | prev_key.offset, key.objectid, key.type, | |
461 | key.offset); | |
557ea5dd QW |
462 | return -EUCLEAN; |
463 | } | |
464 | ||
465 | /* | |
466 | * Make sure the offset and ends are right, remember that the | |
467 | * item data starts at the end of the leaf and grows towards the | |
468 | * front. | |
469 | */ | |
470 | if (slot == 0) | |
471 | item_end_expected = BTRFS_LEAF_DATA_SIZE(fs_info); | |
472 | else | |
473 | item_end_expected = btrfs_item_offset_nr(leaf, | |
474 | slot - 1); | |
475 | if (btrfs_item_end_nr(leaf, slot) != item_end_expected) { | |
2f659546 | 476 | generic_err(fs_info, leaf, slot, |
478d01b3 QW |
477 | "unexpected item end, have %u expect %u", |
478 | btrfs_item_end_nr(leaf, slot), | |
479 | item_end_expected); | |
557ea5dd QW |
480 | return -EUCLEAN; |
481 | } | |
482 | ||
483 | /* | |
484 | * Check to make sure that we don't point outside of the leaf, | |
485 | * just in case all the items are consistent to each other, but | |
486 | * all point outside of the leaf. | |
487 | */ | |
488 | if (btrfs_item_end_nr(leaf, slot) > | |
489 | BTRFS_LEAF_DATA_SIZE(fs_info)) { | |
2f659546 | 490 | generic_err(fs_info, leaf, slot, |
478d01b3 QW |
491 | "slot end outside of leaf, have %u expect range [0, %u]", |
492 | btrfs_item_end_nr(leaf, slot), | |
493 | BTRFS_LEAF_DATA_SIZE(fs_info)); | |
557ea5dd QW |
494 | return -EUCLEAN; |
495 | } | |
496 | ||
497 | /* Also check if the item pointer overlaps with btrfs item. */ | |
498 | if (btrfs_item_nr_offset(slot) + sizeof(struct btrfs_item) > | |
499 | btrfs_item_ptr_offset(leaf, slot)) { | |
2f659546 | 500 | generic_err(fs_info, leaf, slot, |
478d01b3 QW |
501 | "slot overlaps with its data, item end %lu data start %lu", |
502 | btrfs_item_nr_offset(slot) + | |
503 | sizeof(struct btrfs_item), | |
504 | btrfs_item_ptr_offset(leaf, slot)); | |
557ea5dd QW |
505 | return -EUCLEAN; |
506 | } | |
507 | ||
69fc6cbb QW |
508 | if (check_item_data) { |
509 | /* | |
510 | * Check if the item size and content meet other | |
511 | * criteria | |
512 | */ | |
2f659546 | 513 | ret = check_leaf_item(fs_info, leaf, &key, slot); |
69fc6cbb QW |
514 | if (ret < 0) |
515 | return ret; | |
516 | } | |
557ea5dd QW |
517 | |
518 | prev_key.objectid = key.objectid; | |
519 | prev_key.type = key.type; | |
520 | prev_key.offset = key.offset; | |
521 | } | |
522 | ||
523 | return 0; | |
524 | } | |
525 | ||
2f659546 QW |
526 | int btrfs_check_leaf_full(struct btrfs_fs_info *fs_info, |
527 | struct extent_buffer *leaf) | |
69fc6cbb | 528 | { |
2f659546 | 529 | return check_leaf(fs_info, leaf, true); |
69fc6cbb QW |
530 | } |
531 | ||
2f659546 | 532 | int btrfs_check_leaf_relaxed(struct btrfs_fs_info *fs_info, |
69fc6cbb QW |
533 | struct extent_buffer *leaf) |
534 | { | |
2f659546 | 535 | return check_leaf(fs_info, leaf, false); |
69fc6cbb QW |
536 | } |
537 | ||
2f659546 | 538 | int btrfs_check_node(struct btrfs_fs_info *fs_info, struct extent_buffer *node) |
557ea5dd QW |
539 | { |
540 | unsigned long nr = btrfs_header_nritems(node); | |
541 | struct btrfs_key key, next_key; | |
542 | int slot; | |
543 | u64 bytenr; | |
544 | int ret = 0; | |
545 | ||
2f659546 QW |
546 | if (nr == 0 || nr > BTRFS_NODEPTRS_PER_BLOCK(fs_info)) { |
547 | btrfs_crit(fs_info, | |
bba4f298 | 548 | "corrupt node: root=%llu block=%llu, nritems too %s, have %lu expect range [1,%u]", |
2f659546 | 549 | btrfs_header_owner(node), node->start, |
bba4f298 | 550 | nr == 0 ? "small" : "large", nr, |
2f659546 | 551 | BTRFS_NODEPTRS_PER_BLOCK(fs_info)); |
bba4f298 | 552 | return -EUCLEAN; |
557ea5dd QW |
553 | } |
554 | ||
555 | for (slot = 0; slot < nr - 1; slot++) { | |
556 | bytenr = btrfs_node_blockptr(node, slot); | |
557 | btrfs_node_key_to_cpu(node, &key, slot); | |
558 | btrfs_node_key_to_cpu(node, &next_key, slot + 1); | |
559 | ||
560 | if (!bytenr) { | |
2f659546 | 561 | generic_err(fs_info, node, slot, |
bba4f298 QW |
562 | "invalid NULL node pointer"); |
563 | ret = -EUCLEAN; | |
564 | goto out; | |
565 | } | |
2f659546 QW |
566 | if (!IS_ALIGNED(bytenr, fs_info->sectorsize)) { |
567 | generic_err(fs_info, node, slot, | |
bba4f298 | 568 | "unaligned pointer, have %llu should be aligned to %u", |
2f659546 | 569 | bytenr, fs_info->sectorsize); |
bba4f298 | 570 | ret = -EUCLEAN; |
557ea5dd QW |
571 | goto out; |
572 | } | |
573 | ||
574 | if (btrfs_comp_cpu_keys(&key, &next_key) >= 0) { | |
2f659546 | 575 | generic_err(fs_info, node, slot, |
bba4f298 QW |
576 | "bad key order, current (%llu %u %llu) next (%llu %u %llu)", |
577 | key.objectid, key.type, key.offset, | |
578 | next_key.objectid, next_key.type, | |
579 | next_key.offset); | |
580 | ret = -EUCLEAN; | |
557ea5dd QW |
581 | goto out; |
582 | } | |
583 | } | |
584 | out: | |
585 | return ret; | |
586 | } |