tree-checker.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  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, as if one block group is
  350. * larger than maximum size, it's must be some obvious corruption.
  351. */
  352. if (key->offset > BTRFS_MAX_DATA_CHUNK_SIZE || key->offset == 0) {
  353. block_group_err(fs_info, leaf, slot,
  354. "invalid block group size, have %llu expect (0, %llu]",
  355. key->offset, BTRFS_MAX_DATA_CHUNK_SIZE);
  356. return -EUCLEAN;
  357. }
  358. if (item_size != sizeof(bgi)) {
  359. block_group_err(fs_info, leaf, slot,
  360. "invalid item size, have %u expect %zu",
  361. item_size, sizeof(bgi));
  362. return -EUCLEAN;
  363. }
  364. read_extent_buffer(leaf, &bgi, btrfs_item_ptr_offset(leaf, slot),
  365. sizeof(bgi));
  366. if (btrfs_block_group_chunk_objectid(&bgi) !=
  367. BTRFS_FIRST_CHUNK_TREE_OBJECTID) {
  368. block_group_err(fs_info, leaf, slot,
  369. "invalid block group chunk objectid, have %llu expect %llu",
  370. btrfs_block_group_chunk_objectid(&bgi),
  371. BTRFS_FIRST_CHUNK_TREE_OBJECTID);
  372. return -EUCLEAN;
  373. }
  374. if (btrfs_block_group_used(&bgi) > key->offset) {
  375. block_group_err(fs_info, leaf, slot,
  376. "invalid block group used, have %llu expect [0, %llu)",
  377. btrfs_block_group_used(&bgi), key->offset);
  378. return -EUCLEAN;
  379. }
  380. flags = btrfs_block_group_flags(&bgi);
  381. if (hweight64(flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) > 1) {
  382. block_group_err(fs_info, leaf, slot,
  383. "invalid profile flags, have 0x%llx (%lu bits set) expect no more than 1 bit set",
  384. flags & BTRFS_BLOCK_GROUP_PROFILE_MASK,
  385. hweight64(flags & BTRFS_BLOCK_GROUP_PROFILE_MASK));
  386. return -EUCLEAN;
  387. }
  388. type = flags & BTRFS_BLOCK_GROUP_TYPE_MASK;
  389. if (type != BTRFS_BLOCK_GROUP_DATA &&
  390. type != BTRFS_BLOCK_GROUP_METADATA &&
  391. type != BTRFS_BLOCK_GROUP_SYSTEM &&
  392. type != (BTRFS_BLOCK_GROUP_METADATA |
  393. BTRFS_BLOCK_GROUP_DATA)) {
  394. block_group_err(fs_info, leaf, slot,
  395. "invalid type, have 0x%llx (%lu bits set) expect either 0x%llx, 0x%llx, 0x%llx or 0x%llx",
  396. type, hweight64(type),
  397. BTRFS_BLOCK_GROUP_DATA, BTRFS_BLOCK_GROUP_METADATA,
  398. BTRFS_BLOCK_GROUP_SYSTEM,
  399. BTRFS_BLOCK_GROUP_METADATA | BTRFS_BLOCK_GROUP_DATA);
  400. return -EUCLEAN;
  401. }
  402. return 0;
  403. }
  404. /*
  405. * Common point to switch the item-specific validation.
  406. */
  407. static int check_leaf_item(struct btrfs_fs_info *fs_info,
  408. struct extent_buffer *leaf,
  409. struct btrfs_key *key, int slot)
  410. {
  411. int ret = 0;
  412. switch (key->type) {
  413. case BTRFS_EXTENT_DATA_KEY:
  414. ret = check_extent_data_item(fs_info, leaf, key, slot);
  415. break;
  416. case BTRFS_EXTENT_CSUM_KEY:
  417. ret = check_csum_item(fs_info, leaf, key, slot);
  418. break;
  419. case BTRFS_DIR_ITEM_KEY:
  420. case BTRFS_DIR_INDEX_KEY:
  421. case BTRFS_XATTR_ITEM_KEY:
  422. ret = check_dir_item(fs_info, leaf, key, slot);
  423. break;
  424. case BTRFS_BLOCK_GROUP_ITEM_KEY:
  425. ret = check_block_group_item(fs_info, leaf, key, slot);
  426. break;
  427. }
  428. return ret;
  429. }
  430. static int check_leaf(struct btrfs_fs_info *fs_info, struct extent_buffer *leaf,
  431. bool check_item_data)
  432. {
  433. /* No valid key type is 0, so all key should be larger than this key */
  434. struct btrfs_key prev_key = {0, 0, 0};
  435. struct btrfs_key key;
  436. u32 nritems = btrfs_header_nritems(leaf);
  437. int slot;
  438. if (btrfs_header_level(leaf) != 0) {
  439. generic_err(fs_info, leaf, 0,
  440. "invalid level for leaf, have %d expect 0",
  441. btrfs_header_level(leaf));
  442. return -EUCLEAN;
  443. }
  444. /*
  445. * Extent buffers from a relocation tree have a owner field that
  446. * corresponds to the subvolume tree they are based on. So just from an
  447. * extent buffer alone we can not find out what is the id of the
  448. * corresponding subvolume tree, so we can not figure out if the extent
  449. * buffer corresponds to the root of the relocation tree or not. So
  450. * skip this check for relocation trees.
  451. */
  452. if (nritems == 0 && !btrfs_header_flag(leaf, BTRFS_HEADER_FLAG_RELOC)) {
  453. u64 owner = btrfs_header_owner(leaf);
  454. struct btrfs_root *check_root;
  455. /* These trees must never be empty */
  456. if (owner == BTRFS_ROOT_TREE_OBJECTID ||
  457. owner == BTRFS_CHUNK_TREE_OBJECTID ||
  458. owner == BTRFS_EXTENT_TREE_OBJECTID ||
  459. owner == BTRFS_DEV_TREE_OBJECTID ||
  460. owner == BTRFS_FS_TREE_OBJECTID ||
  461. owner == BTRFS_DATA_RELOC_TREE_OBJECTID) {
  462. generic_err(fs_info, leaf, 0,
  463. "invalid root, root %llu must never be empty",
  464. owner);
  465. return -EUCLEAN;
  466. }
  467. key.objectid = owner;
  468. key.type = BTRFS_ROOT_ITEM_KEY;
  469. key.offset = (u64)-1;
  470. check_root = btrfs_get_fs_root(fs_info, &key, false);
  471. /*
  472. * The only reason we also check NULL here is that during
  473. * open_ctree() some roots has not yet been set up.
  474. */
  475. if (!IS_ERR_OR_NULL(check_root)) {
  476. struct extent_buffer *eb;
  477. eb = btrfs_root_node(check_root);
  478. /* if leaf is the root, then it's fine */
  479. if (leaf != eb) {
  480. generic_err(fs_info, leaf, 0,
  481. "invalid nritems, have %u should not be 0 for non-root leaf",
  482. nritems);
  483. free_extent_buffer(eb);
  484. return -EUCLEAN;
  485. }
  486. free_extent_buffer(eb);
  487. }
  488. return 0;
  489. }
  490. if (nritems == 0)
  491. return 0;
  492. /*
  493. * Check the following things to make sure this is a good leaf, and
  494. * leaf users won't need to bother with similar sanity checks:
  495. *
  496. * 1) key ordering
  497. * 2) item offset and size
  498. * No overlap, no hole, all inside the leaf.
  499. * 3) item content
  500. * If possible, do comprehensive sanity check.
  501. * NOTE: All checks must only rely on the item data itself.
  502. */
  503. for (slot = 0; slot < nritems; slot++) {
  504. u32 item_end_expected;
  505. int ret;
  506. btrfs_item_key_to_cpu(leaf, &key, slot);
  507. /* Make sure the keys are in the right order */
  508. if (btrfs_comp_cpu_keys(&prev_key, &key) >= 0) {
  509. generic_err(fs_info, leaf, slot,
  510. "bad key order, prev (%llu %u %llu) current (%llu %u %llu)",
  511. prev_key.objectid, prev_key.type,
  512. prev_key.offset, key.objectid, key.type,
  513. key.offset);
  514. return -EUCLEAN;
  515. }
  516. /*
  517. * Make sure the offset and ends are right, remember that the
  518. * item data starts at the end of the leaf and grows towards the
  519. * front.
  520. */
  521. if (slot == 0)
  522. item_end_expected = BTRFS_LEAF_DATA_SIZE(fs_info);
  523. else
  524. item_end_expected = btrfs_item_offset_nr(leaf,
  525. slot - 1);
  526. if (btrfs_item_end_nr(leaf, slot) != item_end_expected) {
  527. generic_err(fs_info, leaf, slot,
  528. "unexpected item end, have %u expect %u",
  529. btrfs_item_end_nr(leaf, slot),
  530. item_end_expected);
  531. return -EUCLEAN;
  532. }
  533. /*
  534. * Check to make sure that we don't point outside of the leaf,
  535. * just in case all the items are consistent to each other, but
  536. * all point outside of the leaf.
  537. */
  538. if (btrfs_item_end_nr(leaf, slot) >
  539. BTRFS_LEAF_DATA_SIZE(fs_info)) {
  540. generic_err(fs_info, leaf, slot,
  541. "slot end outside of leaf, have %u expect range [0, %u]",
  542. btrfs_item_end_nr(leaf, slot),
  543. BTRFS_LEAF_DATA_SIZE(fs_info));
  544. return -EUCLEAN;
  545. }
  546. /* Also check if the item pointer overlaps with btrfs item. */
  547. if (btrfs_item_nr_offset(slot) + sizeof(struct btrfs_item) >
  548. btrfs_item_ptr_offset(leaf, slot)) {
  549. generic_err(fs_info, leaf, slot,
  550. "slot overlaps with its data, item end %lu data start %lu",
  551. btrfs_item_nr_offset(slot) +
  552. sizeof(struct btrfs_item),
  553. btrfs_item_ptr_offset(leaf, slot));
  554. return -EUCLEAN;
  555. }
  556. if (check_item_data) {
  557. /*
  558. * Check if the item size and content meet other
  559. * criteria
  560. */
  561. ret = check_leaf_item(fs_info, leaf, &key, slot);
  562. if (ret < 0)
  563. return ret;
  564. }
  565. prev_key.objectid = key.objectid;
  566. prev_key.type = key.type;
  567. prev_key.offset = key.offset;
  568. }
  569. return 0;
  570. }
  571. int btrfs_check_leaf_full(struct btrfs_fs_info *fs_info,
  572. struct extent_buffer *leaf)
  573. {
  574. return check_leaf(fs_info, leaf, true);
  575. }
  576. int btrfs_check_leaf_relaxed(struct btrfs_fs_info *fs_info,
  577. struct extent_buffer *leaf)
  578. {
  579. return check_leaf(fs_info, leaf, false);
  580. }
  581. int btrfs_check_node(struct btrfs_fs_info *fs_info, struct extent_buffer *node)
  582. {
  583. unsigned long nr = btrfs_header_nritems(node);
  584. struct btrfs_key key, next_key;
  585. int slot;
  586. int level = btrfs_header_level(node);
  587. u64 bytenr;
  588. int ret = 0;
  589. if (level <= 0 || level >= BTRFS_MAX_LEVEL) {
  590. generic_err(fs_info, node, 0,
  591. "invalid level for node, have %d expect [1, %d]",
  592. level, BTRFS_MAX_LEVEL - 1);
  593. return -EUCLEAN;
  594. }
  595. if (nr == 0 || nr > BTRFS_NODEPTRS_PER_BLOCK(fs_info)) {
  596. btrfs_crit(fs_info,
  597. "corrupt node: root=%llu block=%llu, nritems too %s, have %lu expect range [1,%u]",
  598. btrfs_header_owner(node), node->start,
  599. nr == 0 ? "small" : "large", nr,
  600. BTRFS_NODEPTRS_PER_BLOCK(fs_info));
  601. return -EUCLEAN;
  602. }
  603. for (slot = 0; slot < nr - 1; slot++) {
  604. bytenr = btrfs_node_blockptr(node, slot);
  605. btrfs_node_key_to_cpu(node, &key, slot);
  606. btrfs_node_key_to_cpu(node, &next_key, slot + 1);
  607. if (!bytenr) {
  608. generic_err(fs_info, node, slot,
  609. "invalid NULL node pointer");
  610. ret = -EUCLEAN;
  611. goto out;
  612. }
  613. if (!IS_ALIGNED(bytenr, fs_info->sectorsize)) {
  614. generic_err(fs_info, node, slot,
  615. "unaligned pointer, have %llu should be aligned to %u",
  616. bytenr, fs_info->sectorsize);
  617. ret = -EUCLEAN;
  618. goto out;
  619. }
  620. if (btrfs_comp_cpu_keys(&key, &next_key) >= 0) {
  621. generic_err(fs_info, node, slot,
  622. "bad key order, current (%llu %u %llu) next (%llu %u %llu)",
  623. key.objectid, key.type, key.offset,
  624. next_key.objectid, next_key.type,
  625. next_key.offset);
  626. ret = -EUCLEAN;
  627. goto out;
  628. }
  629. }
  630. out:
  631. return ret;
  632. }