free-space-tree-tests.c 15 KB

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