tree-checker.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. /*
  32. * Error message should follow the following format:
  33. * corrupt <type>: <identifier>, <reason>[, <bad_value>]
  34. *
  35. * @type: leaf or node
  36. * @identifier: the necessary info to locate the leaf/node.
  37. * It's recommened to decode key.objecitd/offset if it's
  38. * meaningful.
  39. * @reason: describe the error
  40. * @bad_value: optional, it's recommened to output bad value and its
  41. * expected value (range).
  42. *
  43. * Since comma is used to separate the components, only space is allowed
  44. * inside each component.
  45. */
  46. /*
  47. * Append generic "corrupt leaf/node root=%llu block=%llu slot=%d: " to @fmt.
  48. * Allows callers to customize the output.
  49. */
  50. __printf(4, 5)
  51. static void generic_err(const struct btrfs_root *root,
  52. const struct extent_buffer *eb, int slot,
  53. const char *fmt, ...)
  54. {
  55. struct va_format vaf;
  56. va_list args;
  57. va_start(args, fmt);
  58. vaf.fmt = fmt;
  59. vaf.va = &args;
  60. btrfs_crit(root->fs_info,
  61. "corrupt %s: root=%llu block=%llu slot=%d, %pV",
  62. btrfs_header_level(eb) == 0 ? "leaf" : "node",
  63. root->objectid, btrfs_header_bytenr(eb), slot, &vaf);
  64. va_end(args);
  65. }
  66. /*
  67. * Customized reporter for extent data item, since its key objectid and
  68. * offset has its own meaning.
  69. */
  70. __printf(4, 5)
  71. static void file_extent_err(const struct btrfs_root *root,
  72. const struct extent_buffer *eb, int slot,
  73. const char *fmt, ...)
  74. {
  75. struct btrfs_key key;
  76. struct va_format vaf;
  77. va_list args;
  78. btrfs_item_key_to_cpu(eb, &key, slot);
  79. va_start(args, fmt);
  80. vaf.fmt = fmt;
  81. vaf.va = &args;
  82. btrfs_crit(root->fs_info,
  83. "corrupt %s: root=%llu block=%llu slot=%d ino=%llu file_offset=%llu, %pV",
  84. btrfs_header_level(eb) == 0 ? "leaf" : "node", root->objectid,
  85. btrfs_header_bytenr(eb), slot, key.objectid, key.offset, &vaf);
  86. va_end(args);
  87. }
  88. /*
  89. * Return 0 if the btrfs_file_extent_##name is aligned to @alignment
  90. * Else return 1
  91. */
  92. #define CHECK_FE_ALIGNED(root, leaf, slot, fi, name, alignment) \
  93. ({ \
  94. if (!IS_ALIGNED(btrfs_file_extent_##name((leaf), (fi)), (alignment))) \
  95. file_extent_err((root), (leaf), (slot), \
  96. "invalid %s for file extent, have %llu, should be aligned to %u", \
  97. (#name), btrfs_file_extent_##name((leaf), (fi)), \
  98. (alignment)); \
  99. (!IS_ALIGNED(btrfs_file_extent_##name((leaf), (fi)), (alignment))); \
  100. })
  101. static int check_extent_data_item(struct btrfs_root *root,
  102. struct extent_buffer *leaf,
  103. struct btrfs_key *key, int slot)
  104. {
  105. struct btrfs_file_extent_item *fi;
  106. u32 sectorsize = root->fs_info->sectorsize;
  107. u32 item_size = btrfs_item_size_nr(leaf, slot);
  108. if (!IS_ALIGNED(key->offset, sectorsize)) {
  109. file_extent_err(root, leaf, slot,
  110. "unaligned file_offset for file extent, have %llu should be aligned to %u",
  111. key->offset, sectorsize);
  112. return -EUCLEAN;
  113. }
  114. fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
  115. if (btrfs_file_extent_type(leaf, fi) > BTRFS_FILE_EXTENT_TYPES) {
  116. file_extent_err(root, leaf, slot,
  117. "invalid type for file extent, have %u expect range [0, %u]",
  118. btrfs_file_extent_type(leaf, fi),
  119. BTRFS_FILE_EXTENT_TYPES);
  120. return -EUCLEAN;
  121. }
  122. /*
  123. * Support for new compression/encrption must introduce incompat flag,
  124. * and must be caught in open_ctree().
  125. */
  126. if (btrfs_file_extent_compression(leaf, fi) > BTRFS_COMPRESS_TYPES) {
  127. file_extent_err(root, leaf, slot,
  128. "invalid compression for file extent, have %u expect range [0, %u]",
  129. btrfs_file_extent_compression(leaf, fi),
  130. BTRFS_COMPRESS_TYPES);
  131. return -EUCLEAN;
  132. }
  133. if (btrfs_file_extent_encryption(leaf, fi)) {
  134. file_extent_err(root, leaf, slot,
  135. "invalid encryption for file extent, have %u expect 0",
  136. btrfs_file_extent_encryption(leaf, fi));
  137. return -EUCLEAN;
  138. }
  139. if (btrfs_file_extent_type(leaf, fi) == BTRFS_FILE_EXTENT_INLINE) {
  140. /* Inline extent must have 0 as key offset */
  141. if (key->offset) {
  142. file_extent_err(root, leaf, slot,
  143. "invalid file_offset for inline file extent, have %llu expect 0",
  144. key->offset);
  145. return -EUCLEAN;
  146. }
  147. /* Compressed inline extent has no on-disk size, skip it */
  148. if (btrfs_file_extent_compression(leaf, fi) !=
  149. BTRFS_COMPRESS_NONE)
  150. return 0;
  151. /* Uncompressed inline extent size must match item size */
  152. if (item_size != BTRFS_FILE_EXTENT_INLINE_DATA_START +
  153. btrfs_file_extent_ram_bytes(leaf, fi)) {
  154. file_extent_err(root, leaf, slot,
  155. "invalid ram_bytes for uncompressed inline extent, have %u expect %llu",
  156. item_size, BTRFS_FILE_EXTENT_INLINE_DATA_START +
  157. btrfs_file_extent_ram_bytes(leaf, fi));
  158. return -EUCLEAN;
  159. }
  160. return 0;
  161. }
  162. /* Regular or preallocated extent has fixed item size */
  163. if (item_size != sizeof(*fi)) {
  164. file_extent_err(root, leaf, slot,
  165. "invalid item size for reg/prealloc file extent, have %u expect %zu",
  166. item_size, sizeof(*fi));
  167. return -EUCLEAN;
  168. }
  169. if (CHECK_FE_ALIGNED(root, leaf, slot, fi, ram_bytes, sectorsize) ||
  170. CHECK_FE_ALIGNED(root, leaf, slot, fi, disk_bytenr, sectorsize) ||
  171. CHECK_FE_ALIGNED(root, leaf, slot, fi, disk_num_bytes, sectorsize) ||
  172. CHECK_FE_ALIGNED(root, leaf, slot, fi, offset, sectorsize) ||
  173. CHECK_FE_ALIGNED(root, leaf, slot, fi, num_bytes, sectorsize))
  174. return -EUCLEAN;
  175. return 0;
  176. }
  177. static int check_csum_item(struct btrfs_root *root, struct extent_buffer *leaf,
  178. struct btrfs_key *key, int slot)
  179. {
  180. u32 sectorsize = root->fs_info->sectorsize;
  181. u32 csumsize = btrfs_super_csum_size(root->fs_info->super_copy);
  182. if (key->objectid != BTRFS_EXTENT_CSUM_OBJECTID) {
  183. generic_err(root, leaf, slot,
  184. "invalid key objectid for csum item, have %llu expect %llu",
  185. key->objectid, BTRFS_EXTENT_CSUM_OBJECTID);
  186. return -EUCLEAN;
  187. }
  188. if (!IS_ALIGNED(key->offset, sectorsize)) {
  189. generic_err(root, leaf, slot,
  190. "unaligned key offset for csum item, have %llu should be aligned to %u",
  191. key->offset, sectorsize);
  192. return -EUCLEAN;
  193. }
  194. if (!IS_ALIGNED(btrfs_item_size_nr(leaf, slot), csumsize)) {
  195. generic_err(root, leaf, slot,
  196. "unaligned item size for csum item, have %u should be aligned to %u",
  197. btrfs_item_size_nr(leaf, slot), csumsize);
  198. return -EUCLEAN;
  199. }
  200. return 0;
  201. }
  202. /*
  203. * Common point to switch the item-specific validation.
  204. */
  205. static int check_leaf_item(struct btrfs_root *root,
  206. struct extent_buffer *leaf,
  207. struct btrfs_key *key, int slot)
  208. {
  209. int ret = 0;
  210. switch (key->type) {
  211. case BTRFS_EXTENT_DATA_KEY:
  212. ret = check_extent_data_item(root, leaf, key, slot);
  213. break;
  214. case BTRFS_EXTENT_CSUM_KEY:
  215. ret = check_csum_item(root, leaf, key, slot);
  216. break;
  217. }
  218. return ret;
  219. }
  220. static int check_leaf(struct btrfs_root *root, struct extent_buffer *leaf,
  221. bool check_item_data)
  222. {
  223. struct btrfs_fs_info *fs_info = root->fs_info;
  224. /* No valid key type is 0, so all key should be larger than this key */
  225. struct btrfs_key prev_key = {0, 0, 0};
  226. struct btrfs_key key;
  227. u32 nritems = btrfs_header_nritems(leaf);
  228. int slot;
  229. /*
  230. * Extent buffers from a relocation tree have a owner field that
  231. * corresponds to the subvolume tree they are based on. So just from an
  232. * extent buffer alone we can not find out what is the id of the
  233. * corresponding subvolume tree, so we can not figure out if the extent
  234. * buffer corresponds to the root of the relocation tree or not. So
  235. * skip this check for relocation trees.
  236. */
  237. if (nritems == 0 && !btrfs_header_flag(leaf, BTRFS_HEADER_FLAG_RELOC)) {
  238. struct btrfs_root *check_root;
  239. key.objectid = btrfs_header_owner(leaf);
  240. key.type = BTRFS_ROOT_ITEM_KEY;
  241. key.offset = (u64)-1;
  242. check_root = btrfs_get_fs_root(fs_info, &key, false);
  243. /*
  244. * The only reason we also check NULL here is that during
  245. * open_ctree() some roots has not yet been set up.
  246. */
  247. if (!IS_ERR_OR_NULL(check_root)) {
  248. struct extent_buffer *eb;
  249. eb = btrfs_root_node(check_root);
  250. /* if leaf is the root, then it's fine */
  251. if (leaf != eb) {
  252. generic_err(check_root, leaf, 0,
  253. "invalid nritems, have %u should not be 0 for non-root leaf",
  254. nritems);
  255. free_extent_buffer(eb);
  256. return -EUCLEAN;
  257. }
  258. free_extent_buffer(eb);
  259. }
  260. return 0;
  261. }
  262. if (nritems == 0)
  263. return 0;
  264. /*
  265. * Check the following things to make sure this is a good leaf, and
  266. * leaf users won't need to bother with similar sanity checks:
  267. *
  268. * 1) key ordering
  269. * 2) item offset and size
  270. * No overlap, no hole, all inside the leaf.
  271. * 3) item content
  272. * If possible, do comprehensive sanity check.
  273. * NOTE: All checks must only rely on the item data itself.
  274. */
  275. for (slot = 0; slot < nritems; slot++) {
  276. u32 item_end_expected;
  277. int ret;
  278. btrfs_item_key_to_cpu(leaf, &key, slot);
  279. /* Make sure the keys are in the right order */
  280. if (btrfs_comp_cpu_keys(&prev_key, &key) >= 0) {
  281. generic_err(root, leaf, slot,
  282. "bad key order, prev (%llu %u %llu) current (%llu %u %llu)",
  283. prev_key.objectid, prev_key.type,
  284. prev_key.offset, key.objectid, key.type,
  285. key.offset);
  286. return -EUCLEAN;
  287. }
  288. /*
  289. * Make sure the offset and ends are right, remember that the
  290. * item data starts at the end of the leaf and grows towards the
  291. * front.
  292. */
  293. if (slot == 0)
  294. item_end_expected = BTRFS_LEAF_DATA_SIZE(fs_info);
  295. else
  296. item_end_expected = btrfs_item_offset_nr(leaf,
  297. slot - 1);
  298. if (btrfs_item_end_nr(leaf, slot) != item_end_expected) {
  299. generic_err(root, leaf, slot,
  300. "unexpected item end, have %u expect %u",
  301. btrfs_item_end_nr(leaf, slot),
  302. item_end_expected);
  303. return -EUCLEAN;
  304. }
  305. /*
  306. * Check to make sure that we don't point outside of the leaf,
  307. * just in case all the items are consistent to each other, but
  308. * all point outside of the leaf.
  309. */
  310. if (btrfs_item_end_nr(leaf, slot) >
  311. BTRFS_LEAF_DATA_SIZE(fs_info)) {
  312. generic_err(root, leaf, slot,
  313. "slot end outside of leaf, have %u expect range [0, %u]",
  314. btrfs_item_end_nr(leaf, slot),
  315. BTRFS_LEAF_DATA_SIZE(fs_info));
  316. return -EUCLEAN;
  317. }
  318. /* Also check if the item pointer overlaps with btrfs item. */
  319. if (btrfs_item_nr_offset(slot) + sizeof(struct btrfs_item) >
  320. btrfs_item_ptr_offset(leaf, slot)) {
  321. generic_err(root, leaf, slot,
  322. "slot overlaps with its data, item end %lu data start %lu",
  323. btrfs_item_nr_offset(slot) +
  324. sizeof(struct btrfs_item),
  325. btrfs_item_ptr_offset(leaf, slot));
  326. return -EUCLEAN;
  327. }
  328. if (check_item_data) {
  329. /*
  330. * Check if the item size and content meet other
  331. * criteria
  332. */
  333. ret = check_leaf_item(root, leaf, &key, slot);
  334. if (ret < 0)
  335. return ret;
  336. }
  337. prev_key.objectid = key.objectid;
  338. prev_key.type = key.type;
  339. prev_key.offset = key.offset;
  340. }
  341. return 0;
  342. }
  343. int btrfs_check_leaf_full(struct btrfs_root *root, struct extent_buffer *leaf)
  344. {
  345. return check_leaf(root, leaf, true);
  346. }
  347. int btrfs_check_leaf_relaxed(struct btrfs_root *root,
  348. struct extent_buffer *leaf)
  349. {
  350. return check_leaf(root, leaf, false);
  351. }
  352. int btrfs_check_node(struct btrfs_root *root, struct extent_buffer *node)
  353. {
  354. unsigned long nr = btrfs_header_nritems(node);
  355. struct btrfs_key key, next_key;
  356. int slot;
  357. u64 bytenr;
  358. int ret = 0;
  359. if (nr == 0 || nr > BTRFS_NODEPTRS_PER_BLOCK(root->fs_info)) {
  360. btrfs_crit(root->fs_info,
  361. "corrupt node: root=%llu block=%llu, nritems too %s, have %lu expect range [1,%u]",
  362. root->objectid, node->start,
  363. nr == 0 ? "small" : "large", nr,
  364. BTRFS_NODEPTRS_PER_BLOCK(root->fs_info));
  365. return -EUCLEAN;
  366. }
  367. for (slot = 0; slot < nr - 1; slot++) {
  368. bytenr = btrfs_node_blockptr(node, slot);
  369. btrfs_node_key_to_cpu(node, &key, slot);
  370. btrfs_node_key_to_cpu(node, &next_key, slot + 1);
  371. if (!bytenr) {
  372. generic_err(root, node, slot,
  373. "invalid NULL node pointer");
  374. ret = -EUCLEAN;
  375. goto out;
  376. }
  377. if (!IS_ALIGNED(bytenr, root->fs_info->sectorsize)) {
  378. generic_err(root, node, slot,
  379. "unaligned pointer, have %llu should be aligned to %u",
  380. bytenr, root->fs_info->sectorsize);
  381. ret = -EUCLEAN;
  382. goto out;
  383. }
  384. if (btrfs_comp_cpu_keys(&key, &next_key) >= 0) {
  385. generic_err(root, node, slot,
  386. "bad key order, current (%llu %u %llu) next (%llu %u %llu)",
  387. key.objectid, key.type, key.offset,
  388. next_key.objectid, next_key.type,
  389. next_key.offset);
  390. ret = -EUCLEAN;
  391. goto out;
  392. }
  393. }
  394. out:
  395. return ret;
  396. }