tree-checker.c 18 KB

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