free-space-tree-tests.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. /*
  2. * Copyright (C) 2015 Facebook. 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; if not, write to the
  15. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. * Boston, MA 021110-1307, USA.
  17. */
  18. #include <linux/types.h>
  19. #include "btrfs-tests.h"
  20. #include "../ctree.h"
  21. #include "../disk-io.h"
  22. #include "../free-space-tree.h"
  23. #include "../transaction.h"
  24. struct free_space_extent {
  25. u64 start, length;
  26. };
  27. /*
  28. * The test cases align their operations to this in order to hit some of the
  29. * edge cases in the bitmap code.
  30. */
  31. #define BITMAP_RANGE (BTRFS_FREE_SPACE_BITMAP_BITS * PAGE_SIZE)
  32. static int __check_free_space_extents(struct btrfs_trans_handle *trans,
  33. struct btrfs_fs_info *fs_info,
  34. struct btrfs_block_group_cache *cache,
  35. struct btrfs_path *path,
  36. struct free_space_extent *extents,
  37. unsigned int num_extents)
  38. {
  39. struct btrfs_free_space_info *info;
  40. struct btrfs_key key;
  41. int prev_bit = 0, bit;
  42. u64 extent_start = 0, offset, end;
  43. u32 flags, extent_count;
  44. unsigned int i;
  45. int ret;
  46. info = search_free_space_info(trans, fs_info, cache, path, 0);
  47. if (IS_ERR(info)) {
  48. test_msg("Could not find free space info\n");
  49. ret = PTR_ERR(info);
  50. goto out;
  51. }
  52. flags = btrfs_free_space_flags(path->nodes[0], info);
  53. extent_count = btrfs_free_space_extent_count(path->nodes[0], info);
  54. if (extent_count != num_extents) {
  55. test_msg("Extent count is wrong\n");
  56. ret = -EINVAL;
  57. goto out;
  58. }
  59. if (flags & BTRFS_FREE_SPACE_USING_BITMAPS) {
  60. if (path->slots[0] != 0)
  61. goto invalid;
  62. end = cache->key.objectid + cache->key.offset;
  63. i = 0;
  64. while (++path->slots[0] < btrfs_header_nritems(path->nodes[0])) {
  65. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  66. if (key.type != BTRFS_FREE_SPACE_BITMAP_KEY)
  67. goto invalid;
  68. offset = key.objectid;
  69. while (offset < key.objectid + key.offset) {
  70. bit = free_space_test_bit(cache, path, offset);
  71. if (prev_bit == 0 && bit == 1) {
  72. extent_start = offset;
  73. } else if (prev_bit == 1 && bit == 0) {
  74. if (i >= num_extents)
  75. goto invalid;
  76. if (i >= num_extents ||
  77. extent_start != extents[i].start ||
  78. offset - extent_start != extents[i].length)
  79. goto invalid;
  80. i++;
  81. }
  82. prev_bit = bit;
  83. offset += cache->sectorsize;
  84. }
  85. }
  86. if (prev_bit == 1) {
  87. if (i >= num_extents ||
  88. extent_start != extents[i].start ||
  89. end - extent_start != extents[i].length)
  90. goto invalid;
  91. i++;
  92. }
  93. if (i != num_extents)
  94. goto invalid;
  95. } else {
  96. if (btrfs_header_nritems(path->nodes[0]) != num_extents + 1 ||
  97. path->slots[0] != 0)
  98. goto invalid;
  99. for (i = 0; i < num_extents; i++) {
  100. path->slots[0]++;
  101. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  102. if (key.type != BTRFS_FREE_SPACE_EXTENT_KEY ||
  103. key.objectid != extents[i].start ||
  104. key.offset != extents[i].length)
  105. goto invalid;
  106. }
  107. }
  108. ret = 0;
  109. out:
  110. btrfs_release_path(path);
  111. return ret;
  112. invalid:
  113. test_msg("Free space tree is invalid\n");
  114. ret = -EINVAL;
  115. goto out;
  116. }
  117. static int check_free_space_extents(struct btrfs_trans_handle *trans,
  118. struct btrfs_fs_info *fs_info,
  119. struct btrfs_block_group_cache *cache,
  120. struct btrfs_path *path,
  121. struct free_space_extent *extents,
  122. unsigned int num_extents)
  123. {
  124. struct btrfs_free_space_info *info;
  125. u32 flags;
  126. int ret;
  127. info = search_free_space_info(trans, fs_info, cache, path, 0);
  128. if (IS_ERR(info)) {
  129. test_msg("Could not find free space info\n");
  130. btrfs_release_path(path);
  131. return PTR_ERR(info);
  132. }
  133. flags = btrfs_free_space_flags(path->nodes[0], info);
  134. btrfs_release_path(path);
  135. ret = __check_free_space_extents(trans, fs_info, cache, path, extents,
  136. num_extents);
  137. if (ret)
  138. return ret;
  139. /* Flip it to the other format and check that for good measure. */
  140. if (flags & BTRFS_FREE_SPACE_USING_BITMAPS) {
  141. ret = convert_free_space_to_extents(trans, fs_info, cache, path);
  142. if (ret) {
  143. test_msg("Could not convert to extents\n");
  144. return ret;
  145. }
  146. } else {
  147. ret = convert_free_space_to_bitmaps(trans, fs_info, cache, path);
  148. if (ret) {
  149. test_msg("Could not convert to bitmaps\n");
  150. return ret;
  151. }
  152. }
  153. return __check_free_space_extents(trans, fs_info, cache, path, extents,
  154. num_extents);
  155. }
  156. static int test_empty_block_group(struct btrfs_trans_handle *trans,
  157. struct btrfs_fs_info *fs_info,
  158. struct btrfs_block_group_cache *cache,
  159. struct btrfs_path *path)
  160. {
  161. struct free_space_extent extents[] = {
  162. {cache->key.objectid, cache->key.offset},
  163. };
  164. return check_free_space_extents(trans, fs_info, cache, path,
  165. extents, ARRAY_SIZE(extents));
  166. }
  167. static int test_remove_all(struct btrfs_trans_handle *trans,
  168. struct btrfs_fs_info *fs_info,
  169. struct btrfs_block_group_cache *cache,
  170. struct btrfs_path *path)
  171. {
  172. struct free_space_extent extents[] = {};
  173. int ret;
  174. ret = __remove_from_free_space_tree(trans, fs_info, cache, path,
  175. cache->key.objectid,
  176. cache->key.offset);
  177. if (ret) {
  178. test_msg("Could not remove free space\n");
  179. return ret;
  180. }
  181. return check_free_space_extents(trans, fs_info, cache, path,
  182. extents, ARRAY_SIZE(extents));
  183. }
  184. static int test_remove_beginning(struct btrfs_trans_handle *trans,
  185. struct btrfs_fs_info *fs_info,
  186. struct btrfs_block_group_cache *cache,
  187. struct btrfs_path *path)
  188. {
  189. struct free_space_extent extents[] = {
  190. {cache->key.objectid + BITMAP_RANGE,
  191. cache->key.offset - BITMAP_RANGE},
  192. };
  193. int ret;
  194. ret = __remove_from_free_space_tree(trans, fs_info, cache, path,
  195. cache->key.objectid, BITMAP_RANGE);
  196. if (ret) {
  197. test_msg("Could not remove free space\n");
  198. return ret;
  199. }
  200. return check_free_space_extents(trans, fs_info, cache, path,
  201. extents, ARRAY_SIZE(extents));
  202. }
  203. static int test_remove_end(struct btrfs_trans_handle *trans,
  204. struct btrfs_fs_info *fs_info,
  205. struct btrfs_block_group_cache *cache,
  206. struct btrfs_path *path)
  207. {
  208. struct free_space_extent extents[] = {
  209. {cache->key.objectid, cache->key.offset - BITMAP_RANGE},
  210. };
  211. int ret;
  212. ret = __remove_from_free_space_tree(trans, fs_info, cache, path,
  213. cache->key.objectid +
  214. cache->key.offset - BITMAP_RANGE,
  215. BITMAP_RANGE);
  216. if (ret) {
  217. test_msg("Could not remove free space\n");
  218. return ret;
  219. }
  220. return check_free_space_extents(trans, fs_info, cache, path,
  221. extents, ARRAY_SIZE(extents));
  222. }
  223. static int test_remove_middle(struct btrfs_trans_handle *trans,
  224. struct btrfs_fs_info *fs_info,
  225. struct btrfs_block_group_cache *cache,
  226. struct btrfs_path *path)
  227. {
  228. struct free_space_extent extents[] = {
  229. {cache->key.objectid, BITMAP_RANGE},
  230. {cache->key.objectid + 2 * BITMAP_RANGE,
  231. cache->key.offset - 2 * BITMAP_RANGE},
  232. };
  233. int ret;
  234. ret = __remove_from_free_space_tree(trans, fs_info, cache, path,
  235. cache->key.objectid + BITMAP_RANGE,
  236. BITMAP_RANGE);
  237. if (ret) {
  238. test_msg("Could not remove free space\n");
  239. return ret;
  240. }
  241. return check_free_space_extents(trans, fs_info, cache, path,
  242. extents, ARRAY_SIZE(extents));
  243. }
  244. static int test_merge_left(struct btrfs_trans_handle *trans,
  245. struct btrfs_fs_info *fs_info,
  246. struct btrfs_block_group_cache *cache,
  247. struct btrfs_path *path)
  248. {
  249. struct free_space_extent extents[] = {
  250. {cache->key.objectid, 2 * BITMAP_RANGE},
  251. };
  252. int ret;
  253. ret = __remove_from_free_space_tree(trans, fs_info, cache, path,
  254. cache->key.objectid,
  255. cache->key.offset);
  256. if (ret) {
  257. test_msg("Could not remove free space\n");
  258. return ret;
  259. }
  260. ret = __add_to_free_space_tree(trans, fs_info, cache, path,
  261. cache->key.objectid, BITMAP_RANGE);
  262. if (ret) {
  263. test_msg("Could not add free space\n");
  264. return ret;
  265. }
  266. ret = __add_to_free_space_tree(trans, fs_info, cache, path,
  267. cache->key.objectid + BITMAP_RANGE,
  268. BITMAP_RANGE);
  269. if (ret) {
  270. test_msg("Could not add free space\n");
  271. return ret;
  272. }
  273. return check_free_space_extents(trans, fs_info, cache, path,
  274. extents, ARRAY_SIZE(extents));
  275. }
  276. static int test_merge_right(struct btrfs_trans_handle *trans,
  277. struct btrfs_fs_info *fs_info,
  278. struct btrfs_block_group_cache *cache,
  279. struct btrfs_path *path)
  280. {
  281. struct free_space_extent extents[] = {
  282. {cache->key.objectid + BITMAP_RANGE, 2 * BITMAP_RANGE},
  283. };
  284. int ret;
  285. ret = __remove_from_free_space_tree(trans, fs_info, cache, path,
  286. cache->key.objectid,
  287. cache->key.offset);
  288. if (ret) {
  289. test_msg("Could not remove free space\n");
  290. return ret;
  291. }
  292. ret = __add_to_free_space_tree(trans, fs_info, cache, path,
  293. cache->key.objectid + 2 * BITMAP_RANGE,
  294. BITMAP_RANGE);
  295. if (ret) {
  296. test_msg("Could not add free space\n");
  297. return ret;
  298. }
  299. ret = __add_to_free_space_tree(trans, fs_info, cache, path,
  300. cache->key.objectid + BITMAP_RANGE,
  301. BITMAP_RANGE);
  302. if (ret) {
  303. test_msg("Could not add free space\n");
  304. return ret;
  305. }
  306. return check_free_space_extents(trans, fs_info, cache, path,
  307. extents, ARRAY_SIZE(extents));
  308. }
  309. static int test_merge_both(struct btrfs_trans_handle *trans,
  310. struct btrfs_fs_info *fs_info,
  311. struct btrfs_block_group_cache *cache,
  312. struct btrfs_path *path)
  313. {
  314. struct free_space_extent extents[] = {
  315. {cache->key.objectid, 3 * BITMAP_RANGE},
  316. };
  317. int ret;
  318. ret = __remove_from_free_space_tree(trans, fs_info, cache, path,
  319. cache->key.objectid,
  320. cache->key.offset);
  321. if (ret) {
  322. test_msg("Could not remove free space\n");
  323. return ret;
  324. }
  325. ret = __add_to_free_space_tree(trans, fs_info, cache, path,
  326. cache->key.objectid, BITMAP_RANGE);
  327. if (ret) {
  328. test_msg("Could not add free space\n");
  329. return ret;
  330. }
  331. ret = __add_to_free_space_tree(trans, fs_info, cache, path,
  332. cache->key.objectid + 2 * BITMAP_RANGE,
  333. BITMAP_RANGE);
  334. if (ret) {
  335. test_msg("Could not add free space\n");
  336. return ret;
  337. }
  338. ret = __add_to_free_space_tree(trans, fs_info, cache, path,
  339. cache->key.objectid + BITMAP_RANGE,
  340. BITMAP_RANGE);
  341. if (ret) {
  342. test_msg("Could not add free space\n");
  343. return ret;
  344. }
  345. return check_free_space_extents(trans, fs_info, cache, path,
  346. extents, ARRAY_SIZE(extents));
  347. }
  348. static int test_merge_none(struct btrfs_trans_handle *trans,
  349. struct btrfs_fs_info *fs_info,
  350. struct btrfs_block_group_cache *cache,
  351. struct btrfs_path *path)
  352. {
  353. struct free_space_extent extents[] = {
  354. {cache->key.objectid, BITMAP_RANGE},
  355. {cache->key.objectid + 2 * BITMAP_RANGE, BITMAP_RANGE},
  356. {cache->key.objectid + 4 * BITMAP_RANGE, BITMAP_RANGE},
  357. };
  358. int ret;
  359. ret = __remove_from_free_space_tree(trans, fs_info, cache, path,
  360. cache->key.objectid,
  361. cache->key.offset);
  362. if (ret) {
  363. test_msg("Could not remove free space\n");
  364. return ret;
  365. }
  366. ret = __add_to_free_space_tree(trans, fs_info, cache, path,
  367. cache->key.objectid, BITMAP_RANGE);
  368. if (ret) {
  369. test_msg("Could not add free space\n");
  370. return ret;
  371. }
  372. ret = __add_to_free_space_tree(trans, fs_info, cache, path,
  373. cache->key.objectid + 4 * BITMAP_RANGE,
  374. BITMAP_RANGE);
  375. if (ret) {
  376. test_msg("Could not add free space\n");
  377. return ret;
  378. }
  379. ret = __add_to_free_space_tree(trans, fs_info, cache, path,
  380. cache->key.objectid + 2 * BITMAP_RANGE,
  381. BITMAP_RANGE);
  382. if (ret) {
  383. test_msg("Could not add free space\n");
  384. return ret;
  385. }
  386. return check_free_space_extents(trans, fs_info, cache, path,
  387. extents, ARRAY_SIZE(extents));
  388. }
  389. typedef int (*test_func_t)(struct btrfs_trans_handle *,
  390. struct btrfs_fs_info *,
  391. struct btrfs_block_group_cache *,
  392. struct btrfs_path *);
  393. static int run_test(test_func_t test_func, int bitmaps,
  394. u32 sectorsize, u32 nodesize)
  395. {
  396. struct btrfs_fs_info *fs_info;
  397. struct btrfs_root *root = NULL;
  398. struct btrfs_block_group_cache *cache = NULL;
  399. struct btrfs_trans_handle trans;
  400. struct btrfs_path *path = NULL;
  401. int ret;
  402. fs_info = btrfs_alloc_dummy_fs_info();
  403. if (!fs_info) {
  404. test_msg("Couldn't allocate dummy fs info\n");
  405. ret = -ENOMEM;
  406. goto out;
  407. }
  408. root = btrfs_alloc_dummy_root(fs_info, sectorsize, nodesize);
  409. if (IS_ERR(root)) {
  410. test_msg("Couldn't allocate dummy root\n");
  411. ret = PTR_ERR(root);
  412. goto out;
  413. }
  414. btrfs_set_super_compat_ro_flags(root->fs_info->super_copy,
  415. BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE);
  416. root->fs_info->free_space_root = root;
  417. root->fs_info->tree_root = root;
  418. root->node = alloc_test_extent_buffer(root->fs_info,
  419. nodesize, nodesize);
  420. if (!root->node) {
  421. test_msg("Couldn't allocate dummy buffer\n");
  422. ret = -ENOMEM;
  423. goto out;
  424. }
  425. btrfs_set_header_level(root->node, 0);
  426. btrfs_set_header_nritems(root->node, 0);
  427. root->alloc_bytenr += 2 * nodesize;
  428. cache = btrfs_alloc_dummy_block_group(8 * BITMAP_RANGE, sectorsize);
  429. if (!cache) {
  430. test_msg("Couldn't allocate dummy block group cache\n");
  431. ret = -ENOMEM;
  432. goto out;
  433. }
  434. cache->bitmap_low_thresh = 0;
  435. cache->bitmap_high_thresh = (u32)-1;
  436. cache->needs_free_space = 1;
  437. cache->fs_info = root->fs_info;
  438. btrfs_init_dummy_trans(&trans);
  439. path = btrfs_alloc_path();
  440. if (!path) {
  441. test_msg("Couldn't allocate path\n");
  442. return -ENOMEM;
  443. }
  444. ret = add_block_group_free_space(&trans, root->fs_info, cache);
  445. if (ret) {
  446. test_msg("Could not add block group free space\n");
  447. goto out;
  448. }
  449. if (bitmaps) {
  450. ret = convert_free_space_to_bitmaps(&trans, root->fs_info,
  451. cache, path);
  452. if (ret) {
  453. test_msg("Could not convert block group to bitmaps\n");
  454. goto out;
  455. }
  456. }
  457. ret = test_func(&trans, root->fs_info, cache, path);
  458. if (ret)
  459. goto out;
  460. ret = remove_block_group_free_space(&trans, root->fs_info, cache);
  461. if (ret) {
  462. test_msg("Could not remove block group free space\n");
  463. goto out;
  464. }
  465. if (btrfs_header_nritems(root->node) != 0) {
  466. test_msg("Free space tree has leftover items\n");
  467. ret = -EINVAL;
  468. goto out;
  469. }
  470. ret = 0;
  471. out:
  472. btrfs_free_path(path);
  473. btrfs_free_dummy_block_group(cache);
  474. btrfs_free_dummy_root(root);
  475. btrfs_free_dummy_fs_info(fs_info);
  476. return ret;
  477. }
  478. static int run_test_both_formats(test_func_t test_func,
  479. u32 sectorsize, u32 nodesize)
  480. {
  481. int ret;
  482. ret = run_test(test_func, 0, sectorsize, nodesize);
  483. if (ret)
  484. return ret;
  485. return run_test(test_func, 1, sectorsize, nodesize);
  486. }
  487. int btrfs_test_free_space_tree(u32 sectorsize, u32 nodesize)
  488. {
  489. test_func_t tests[] = {
  490. test_empty_block_group,
  491. test_remove_all,
  492. test_remove_beginning,
  493. test_remove_end,
  494. test_remove_middle,
  495. test_merge_left,
  496. test_merge_right,
  497. test_merge_both,
  498. test_merge_none,
  499. };
  500. int i;
  501. test_msg("Running free space tree tests\n");
  502. for (i = 0; i < ARRAY_SIZE(tests); i++) {
  503. int ret = run_test_both_formats(tests[i], sectorsize,
  504. nodesize);
  505. if (ret) {
  506. test_msg("%pf : sectorsize %u failed\n",
  507. tests[i], sectorsize);
  508. return ret;
  509. }
  510. }
  511. return 0;
  512. }