tree-checker.c 20 KB

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