free-space-tests.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /*
  2. * Copyright (C) 2013 Fusion IO. 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/slab.h>
  19. #include "btrfs-tests.h"
  20. #include "../ctree.h"
  21. #include "../free-space-cache.h"
  22. #define BITS_PER_BITMAP (PAGE_CACHE_SIZE * 8)
  23. static struct btrfs_block_group_cache *init_test_block_group(void)
  24. {
  25. struct btrfs_block_group_cache *cache;
  26. cache = kzalloc(sizeof(*cache), GFP_NOFS);
  27. if (!cache)
  28. return NULL;
  29. cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
  30. GFP_NOFS);
  31. if (!cache->free_space_ctl) {
  32. kfree(cache);
  33. return NULL;
  34. }
  35. cache->key.objectid = 0;
  36. cache->key.offset = 1024 * 1024 * 1024;
  37. cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
  38. cache->sectorsize = 4096;
  39. spin_lock_init(&cache->lock);
  40. INIT_LIST_HEAD(&cache->list);
  41. INIT_LIST_HEAD(&cache->cluster_list);
  42. INIT_LIST_HEAD(&cache->new_bg_list);
  43. btrfs_init_free_space_ctl(cache);
  44. return cache;
  45. }
  46. /*
  47. * This test just does basic sanity checking, making sure we can add an exten
  48. * entry and remove space from either end and the middle, and make sure we can
  49. * remove space that covers adjacent extent entries.
  50. */
  51. static int test_extents(struct btrfs_block_group_cache *cache)
  52. {
  53. int ret = 0;
  54. test_msg("Running extent only tests\n");
  55. /* First just make sure we can remove an entire entry */
  56. ret = btrfs_add_free_space(cache, 0, 4 * 1024 * 1024);
  57. if (ret) {
  58. test_msg("Error adding initial extents %d\n", ret);
  59. return ret;
  60. }
  61. ret = btrfs_remove_free_space(cache, 0, 4 * 1024 * 1024);
  62. if (ret) {
  63. test_msg("Error removing extent %d\n", ret);
  64. return ret;
  65. }
  66. if (test_check_exists(cache, 0, 4 * 1024 * 1024)) {
  67. test_msg("Full remove left some lingering space\n");
  68. return -1;
  69. }
  70. /* Ok edge and middle cases now */
  71. ret = btrfs_add_free_space(cache, 0, 4 * 1024 * 1024);
  72. if (ret) {
  73. test_msg("Error adding half extent %d\n", ret);
  74. return ret;
  75. }
  76. ret = btrfs_remove_free_space(cache, 3 * 1024 * 1024, 1 * 1024 * 1024);
  77. if (ret) {
  78. test_msg("Error removing tail end %d\n", ret);
  79. return ret;
  80. }
  81. ret = btrfs_remove_free_space(cache, 0, 1 * 1024 * 1024);
  82. if (ret) {
  83. test_msg("Error removing front end %d\n", ret);
  84. return ret;
  85. }
  86. ret = btrfs_remove_free_space(cache, 2 * 1024 * 1024, 4096);
  87. if (ret) {
  88. test_msg("Error removing middle peice %d\n", ret);
  89. return ret;
  90. }
  91. if (test_check_exists(cache, 0, 1 * 1024 * 1024)) {
  92. test_msg("Still have space at the front\n");
  93. return -1;
  94. }
  95. if (test_check_exists(cache, 2 * 1024 * 1024, 4096)) {
  96. test_msg("Still have space in the middle\n");
  97. return -1;
  98. }
  99. if (test_check_exists(cache, 3 * 1024 * 1024, 1 * 1024 * 1024)) {
  100. test_msg("Still have space at the end\n");
  101. return -1;
  102. }
  103. /* Cleanup */
  104. __btrfs_remove_free_space_cache(cache->free_space_ctl);
  105. return 0;
  106. }
  107. static int test_bitmaps(struct btrfs_block_group_cache *cache)
  108. {
  109. u64 next_bitmap_offset;
  110. int ret;
  111. test_msg("Running bitmap only tests\n");
  112. ret = test_add_free_space_entry(cache, 0, 4 * 1024 * 1024, 1);
  113. if (ret) {
  114. test_msg("Couldn't create a bitmap entry %d\n", ret);
  115. return ret;
  116. }
  117. ret = btrfs_remove_free_space(cache, 0, 4 * 1024 * 1024);
  118. if (ret) {
  119. test_msg("Error removing bitmap full range %d\n", ret);
  120. return ret;
  121. }
  122. if (test_check_exists(cache, 0, 4 * 1024 * 1024)) {
  123. test_msg("Left some space in bitmap\n");
  124. return -1;
  125. }
  126. ret = test_add_free_space_entry(cache, 0, 4 * 1024 * 1024, 1);
  127. if (ret) {
  128. test_msg("Couldn't add to our bitmap entry %d\n", ret);
  129. return ret;
  130. }
  131. ret = btrfs_remove_free_space(cache, 1 * 1024 * 1024, 2 * 1024 * 1024);
  132. if (ret) {
  133. test_msg("Couldn't remove middle chunk %d\n", ret);
  134. return ret;
  135. }
  136. /*
  137. * The first bitmap we have starts at offset 0 so the next one is just
  138. * at the end of the first bitmap.
  139. */
  140. next_bitmap_offset = (u64)(BITS_PER_BITMAP * 4096);
  141. /* Test a bit straddling two bitmaps */
  142. ret = test_add_free_space_entry(cache, next_bitmap_offset -
  143. (2 * 1024 * 1024), 4 * 1024 * 1024, 1);
  144. if (ret) {
  145. test_msg("Couldn't add space that straddles two bitmaps %d\n",
  146. ret);
  147. return ret;
  148. }
  149. ret = btrfs_remove_free_space(cache, next_bitmap_offset -
  150. (1 * 1024 * 1024), 2 * 1024 * 1024);
  151. if (ret) {
  152. test_msg("Couldn't remove overlapping space %d\n", ret);
  153. return ret;
  154. }
  155. if (test_check_exists(cache, next_bitmap_offset - (1 * 1024 * 1024),
  156. 2 * 1024 * 1024)) {
  157. test_msg("Left some space when removing overlapping\n");
  158. return -1;
  159. }
  160. __btrfs_remove_free_space_cache(cache->free_space_ctl);
  161. return 0;
  162. }
  163. /* This is the high grade jackassery */
  164. static int test_bitmaps_and_extents(struct btrfs_block_group_cache *cache)
  165. {
  166. u64 bitmap_offset = (u64)(BITS_PER_BITMAP * 4096);
  167. int ret;
  168. test_msg("Running bitmap and extent tests\n");
  169. /*
  170. * First let's do something simple, an extent at the same offset as the
  171. * bitmap, but the free space completely in the extent and then
  172. * completely in the bitmap.
  173. */
  174. ret = test_add_free_space_entry(cache, 4 * 1024 * 1024, 1 * 1024 * 1024, 1);
  175. if (ret) {
  176. test_msg("Couldn't create bitmap entry %d\n", ret);
  177. return ret;
  178. }
  179. ret = test_add_free_space_entry(cache, 0, 1 * 1024 * 1024, 0);
  180. if (ret) {
  181. test_msg("Couldn't add extent entry %d\n", ret);
  182. return ret;
  183. }
  184. ret = btrfs_remove_free_space(cache, 0, 1 * 1024 * 1024);
  185. if (ret) {
  186. test_msg("Couldn't remove extent entry %d\n", ret);
  187. return ret;
  188. }
  189. if (test_check_exists(cache, 0, 1 * 1024 * 1024)) {
  190. test_msg("Left remnants after our remove\n");
  191. return -1;
  192. }
  193. /* Now to add back the extent entry and remove from the bitmap */
  194. ret = test_add_free_space_entry(cache, 0, 1 * 1024 * 1024, 0);
  195. if (ret) {
  196. test_msg("Couldn't re-add extent entry %d\n", ret);
  197. return ret;
  198. }
  199. ret = btrfs_remove_free_space(cache, 4 * 1024 * 1024, 1 * 1024 * 1024);
  200. if (ret) {
  201. test_msg("Couldn't remove from bitmap %d\n", ret);
  202. return ret;
  203. }
  204. if (test_check_exists(cache, 4 * 1024 * 1024, 1 * 1024 * 1024)) {
  205. test_msg("Left remnants in the bitmap\n");
  206. return -1;
  207. }
  208. /*
  209. * Ok so a little more evil, extent entry and bitmap at the same offset,
  210. * removing an overlapping chunk.
  211. */
  212. ret = test_add_free_space_entry(cache, 1 * 1024 * 1024, 4 * 1024 * 1024, 1);
  213. if (ret) {
  214. test_msg("Couldn't add to a bitmap %d\n", ret);
  215. return ret;
  216. }
  217. ret = btrfs_remove_free_space(cache, 512 * 1024, 3 * 1024 * 1024);
  218. if (ret) {
  219. test_msg("Couldn't remove overlapping space %d\n", ret);
  220. return ret;
  221. }
  222. if (test_check_exists(cache, 512 * 1024, 3 * 1024 * 1024)) {
  223. test_msg("Left over peices after removing overlapping\n");
  224. return -1;
  225. }
  226. __btrfs_remove_free_space_cache(cache->free_space_ctl);
  227. /* Now with the extent entry offset into the bitmap */
  228. ret = test_add_free_space_entry(cache, 4 * 1024 * 1024, 4 * 1024 * 1024, 1);
  229. if (ret) {
  230. test_msg("Couldn't add space to the bitmap %d\n", ret);
  231. return ret;
  232. }
  233. ret = test_add_free_space_entry(cache, 2 * 1024 * 1024, 2 * 1024 * 1024, 0);
  234. if (ret) {
  235. test_msg("Couldn't add extent to the cache %d\n", ret);
  236. return ret;
  237. }
  238. ret = btrfs_remove_free_space(cache, 3 * 1024 * 1024, 4 * 1024 * 1024);
  239. if (ret) {
  240. test_msg("Problem removing overlapping space %d\n", ret);
  241. return ret;
  242. }
  243. if (test_check_exists(cache, 3 * 1024 * 1024, 4 * 1024 * 1024)) {
  244. test_msg("Left something behind when removing space");
  245. return -1;
  246. }
  247. /*
  248. * This has blown up in the past, the extent entry starts before the
  249. * bitmap entry, but we're trying to remove an offset that falls
  250. * completely within the bitmap range and is in both the extent entry
  251. * and the bitmap entry, looks like this
  252. *
  253. * [ extent ]
  254. * [ bitmap ]
  255. * [ del ]
  256. */
  257. __btrfs_remove_free_space_cache(cache->free_space_ctl);
  258. ret = test_add_free_space_entry(cache, bitmap_offset + 4 * 1024 * 1024,
  259. 4 * 1024 * 1024, 1);
  260. if (ret) {
  261. test_msg("Couldn't add bitmap %d\n", ret);
  262. return ret;
  263. }
  264. ret = test_add_free_space_entry(cache, bitmap_offset - 1 * 1024 * 1024,
  265. 5 * 1024 * 1024, 0);
  266. if (ret) {
  267. test_msg("Couldn't add extent entry %d\n", ret);
  268. return ret;
  269. }
  270. ret = btrfs_remove_free_space(cache, bitmap_offset + 1 * 1024 * 1024,
  271. 5 * 1024 * 1024);
  272. if (ret) {
  273. test_msg("Failed to free our space %d\n", ret);
  274. return ret;
  275. }
  276. if (test_check_exists(cache, bitmap_offset + 1 * 1024 * 1024,
  277. 5 * 1024 * 1024)) {
  278. test_msg("Left stuff over\n");
  279. return -1;
  280. }
  281. __btrfs_remove_free_space_cache(cache->free_space_ctl);
  282. /*
  283. * This blew up before, we have part of the free space in a bitmap and
  284. * then the entirety of the rest of the space in an extent. This used
  285. * to return -EAGAIN back from btrfs_remove_extent, make sure this
  286. * doesn't happen.
  287. */
  288. ret = test_add_free_space_entry(cache, 1 * 1024 * 1024, 2 * 1024 * 1024, 1);
  289. if (ret) {
  290. test_msg("Couldn't add bitmap entry %d\n", ret);
  291. return ret;
  292. }
  293. ret = test_add_free_space_entry(cache, 3 * 1024 * 1024, 1 * 1024 * 1024, 0);
  294. if (ret) {
  295. test_msg("Couldn't add extent entry %d\n", ret);
  296. return ret;
  297. }
  298. ret = btrfs_remove_free_space(cache, 1 * 1024 * 1024, 3 * 1024 * 1024);
  299. if (ret) {
  300. test_msg("Error removing bitmap and extent overlapping %d\n", ret);
  301. return ret;
  302. }
  303. __btrfs_remove_free_space_cache(cache->free_space_ctl);
  304. return 0;
  305. }
  306. int btrfs_test_free_space_cache(void)
  307. {
  308. struct btrfs_block_group_cache *cache;
  309. int ret;
  310. test_msg("Running btrfs free space cache tests\n");
  311. cache = init_test_block_group();
  312. if (!cache) {
  313. test_msg("Couldn't run the tests\n");
  314. return 0;
  315. }
  316. ret = test_extents(cache);
  317. if (ret)
  318. goto out;
  319. ret = test_bitmaps(cache);
  320. if (ret)
  321. goto out;
  322. ret = test_bitmaps_and_extents(cache);
  323. if (ret)
  324. goto out;
  325. out:
  326. __btrfs_remove_free_space_cache(cache->free_space_ctl);
  327. kfree(cache->free_space_ctl);
  328. kfree(cache);
  329. test_msg("Free space cache tests finished\n");
  330. return ret;
  331. }