free-space-tests.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  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 "../disk-io.h"
  22. #include "../free-space-cache.h"
  23. #define BITS_PER_BITMAP (PAGE_CACHE_SIZE * 8)
  24. static struct btrfs_block_group_cache *init_test_block_group(void)
  25. {
  26. struct btrfs_block_group_cache *cache;
  27. cache = kzalloc(sizeof(*cache), GFP_NOFS);
  28. if (!cache)
  29. return NULL;
  30. cache->free_space_ctl = kzalloc(sizeof(*cache->free_space_ctl),
  31. GFP_NOFS);
  32. if (!cache->free_space_ctl) {
  33. kfree(cache);
  34. return NULL;
  35. }
  36. cache->fs_info = btrfs_alloc_dummy_fs_info();
  37. if (!cache->fs_info) {
  38. kfree(cache->free_space_ctl);
  39. kfree(cache);
  40. return NULL;
  41. }
  42. cache->key.objectid = 0;
  43. cache->key.offset = 1024 * 1024 * 1024;
  44. cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
  45. cache->sectorsize = 4096;
  46. cache->full_stripe_len = 4096;
  47. spin_lock_init(&cache->lock);
  48. INIT_LIST_HEAD(&cache->list);
  49. INIT_LIST_HEAD(&cache->cluster_list);
  50. INIT_LIST_HEAD(&cache->bg_list);
  51. btrfs_init_free_space_ctl(cache);
  52. return cache;
  53. }
  54. /*
  55. * This test just does basic sanity checking, making sure we can add an exten
  56. * entry and remove space from either end and the middle, and make sure we can
  57. * remove space that covers adjacent extent entries.
  58. */
  59. static int test_extents(struct btrfs_block_group_cache *cache)
  60. {
  61. int ret = 0;
  62. test_msg("Running extent only tests\n");
  63. /* First just make sure we can remove an entire entry */
  64. ret = btrfs_add_free_space(cache, 0, 4 * 1024 * 1024);
  65. if (ret) {
  66. test_msg("Error adding initial extents %d\n", ret);
  67. return ret;
  68. }
  69. ret = btrfs_remove_free_space(cache, 0, 4 * 1024 * 1024);
  70. if (ret) {
  71. test_msg("Error removing extent %d\n", ret);
  72. return ret;
  73. }
  74. if (test_check_exists(cache, 0, 4 * 1024 * 1024)) {
  75. test_msg("Full remove left some lingering space\n");
  76. return -1;
  77. }
  78. /* Ok edge and middle cases now */
  79. ret = btrfs_add_free_space(cache, 0, 4 * 1024 * 1024);
  80. if (ret) {
  81. test_msg("Error adding half extent %d\n", ret);
  82. return ret;
  83. }
  84. ret = btrfs_remove_free_space(cache, 3 * 1024 * 1024, 1 * 1024 * 1024);
  85. if (ret) {
  86. test_msg("Error removing tail end %d\n", ret);
  87. return ret;
  88. }
  89. ret = btrfs_remove_free_space(cache, 0, 1 * 1024 * 1024);
  90. if (ret) {
  91. test_msg("Error removing front end %d\n", ret);
  92. return ret;
  93. }
  94. ret = btrfs_remove_free_space(cache, 2 * 1024 * 1024, 4096);
  95. if (ret) {
  96. test_msg("Error removing middle piece %d\n", ret);
  97. return ret;
  98. }
  99. if (test_check_exists(cache, 0, 1 * 1024 * 1024)) {
  100. test_msg("Still have space at the front\n");
  101. return -1;
  102. }
  103. if (test_check_exists(cache, 2 * 1024 * 1024, 4096)) {
  104. test_msg("Still have space in the middle\n");
  105. return -1;
  106. }
  107. if (test_check_exists(cache, 3 * 1024 * 1024, 1 * 1024 * 1024)) {
  108. test_msg("Still have space at the end\n");
  109. return -1;
  110. }
  111. /* Cleanup */
  112. __btrfs_remove_free_space_cache(cache->free_space_ctl);
  113. return 0;
  114. }
  115. static int test_bitmaps(struct btrfs_block_group_cache *cache)
  116. {
  117. u64 next_bitmap_offset;
  118. int ret;
  119. test_msg("Running bitmap only tests\n");
  120. ret = test_add_free_space_entry(cache, 0, 4 * 1024 * 1024, 1);
  121. if (ret) {
  122. test_msg("Couldn't create a bitmap entry %d\n", ret);
  123. return ret;
  124. }
  125. ret = btrfs_remove_free_space(cache, 0, 4 * 1024 * 1024);
  126. if (ret) {
  127. test_msg("Error removing bitmap full range %d\n", ret);
  128. return ret;
  129. }
  130. if (test_check_exists(cache, 0, 4 * 1024 * 1024)) {
  131. test_msg("Left some space in bitmap\n");
  132. return -1;
  133. }
  134. ret = test_add_free_space_entry(cache, 0, 4 * 1024 * 1024, 1);
  135. if (ret) {
  136. test_msg("Couldn't add to our bitmap entry %d\n", ret);
  137. return ret;
  138. }
  139. ret = btrfs_remove_free_space(cache, 1 * 1024 * 1024, 2 * 1024 * 1024);
  140. if (ret) {
  141. test_msg("Couldn't remove middle chunk %d\n", ret);
  142. return ret;
  143. }
  144. /*
  145. * The first bitmap we have starts at offset 0 so the next one is just
  146. * at the end of the first bitmap.
  147. */
  148. next_bitmap_offset = (u64)(BITS_PER_BITMAP * 4096);
  149. /* Test a bit straddling two bitmaps */
  150. ret = test_add_free_space_entry(cache, next_bitmap_offset -
  151. (2 * 1024 * 1024), 4 * 1024 * 1024, 1);
  152. if (ret) {
  153. test_msg("Couldn't add space that straddles two bitmaps %d\n",
  154. ret);
  155. return ret;
  156. }
  157. ret = btrfs_remove_free_space(cache, next_bitmap_offset -
  158. (1 * 1024 * 1024), 2 * 1024 * 1024);
  159. if (ret) {
  160. test_msg("Couldn't remove overlapping space %d\n", ret);
  161. return ret;
  162. }
  163. if (test_check_exists(cache, next_bitmap_offset - (1 * 1024 * 1024),
  164. 2 * 1024 * 1024)) {
  165. test_msg("Left some space when removing overlapping\n");
  166. return -1;
  167. }
  168. __btrfs_remove_free_space_cache(cache->free_space_ctl);
  169. return 0;
  170. }
  171. /* This is the high grade jackassery */
  172. static int test_bitmaps_and_extents(struct btrfs_block_group_cache *cache)
  173. {
  174. u64 bitmap_offset = (u64)(BITS_PER_BITMAP * 4096);
  175. int ret;
  176. test_msg("Running bitmap and extent tests\n");
  177. /*
  178. * First let's do something simple, an extent at the same offset as the
  179. * bitmap, but the free space completely in the extent and then
  180. * completely in the bitmap.
  181. */
  182. ret = test_add_free_space_entry(cache, 4 * 1024 * 1024, 1 * 1024 * 1024, 1);
  183. if (ret) {
  184. test_msg("Couldn't create bitmap entry %d\n", ret);
  185. return ret;
  186. }
  187. ret = test_add_free_space_entry(cache, 0, 1 * 1024 * 1024, 0);
  188. if (ret) {
  189. test_msg("Couldn't add extent entry %d\n", ret);
  190. return ret;
  191. }
  192. ret = btrfs_remove_free_space(cache, 0, 1 * 1024 * 1024);
  193. if (ret) {
  194. test_msg("Couldn't remove extent entry %d\n", ret);
  195. return ret;
  196. }
  197. if (test_check_exists(cache, 0, 1 * 1024 * 1024)) {
  198. test_msg("Left remnants after our remove\n");
  199. return -1;
  200. }
  201. /* Now to add back the extent entry and remove from the bitmap */
  202. ret = test_add_free_space_entry(cache, 0, 1 * 1024 * 1024, 0);
  203. if (ret) {
  204. test_msg("Couldn't re-add extent entry %d\n", ret);
  205. return ret;
  206. }
  207. ret = btrfs_remove_free_space(cache, 4 * 1024 * 1024, 1 * 1024 * 1024);
  208. if (ret) {
  209. test_msg("Couldn't remove from bitmap %d\n", ret);
  210. return ret;
  211. }
  212. if (test_check_exists(cache, 4 * 1024 * 1024, 1 * 1024 * 1024)) {
  213. test_msg("Left remnants in the bitmap\n");
  214. return -1;
  215. }
  216. /*
  217. * Ok so a little more evil, extent entry and bitmap at the same offset,
  218. * removing an overlapping chunk.
  219. */
  220. ret = test_add_free_space_entry(cache, 1 * 1024 * 1024, 4 * 1024 * 1024, 1);
  221. if (ret) {
  222. test_msg("Couldn't add to a bitmap %d\n", ret);
  223. return ret;
  224. }
  225. ret = btrfs_remove_free_space(cache, 512 * 1024, 3 * 1024 * 1024);
  226. if (ret) {
  227. test_msg("Couldn't remove overlapping space %d\n", ret);
  228. return ret;
  229. }
  230. if (test_check_exists(cache, 512 * 1024, 3 * 1024 * 1024)) {
  231. test_msg("Left over pieces after removing overlapping\n");
  232. return -1;
  233. }
  234. __btrfs_remove_free_space_cache(cache->free_space_ctl);
  235. /* Now with the extent entry offset into the bitmap */
  236. ret = test_add_free_space_entry(cache, 4 * 1024 * 1024, 4 * 1024 * 1024, 1);
  237. if (ret) {
  238. test_msg("Couldn't add space to the bitmap %d\n", ret);
  239. return ret;
  240. }
  241. ret = test_add_free_space_entry(cache, 2 * 1024 * 1024, 2 * 1024 * 1024, 0);
  242. if (ret) {
  243. test_msg("Couldn't add extent to the cache %d\n", ret);
  244. return ret;
  245. }
  246. ret = btrfs_remove_free_space(cache, 3 * 1024 * 1024, 4 * 1024 * 1024);
  247. if (ret) {
  248. test_msg("Problem removing overlapping space %d\n", ret);
  249. return ret;
  250. }
  251. if (test_check_exists(cache, 3 * 1024 * 1024, 4 * 1024 * 1024)) {
  252. test_msg("Left something behind when removing space");
  253. return -1;
  254. }
  255. /*
  256. * This has blown up in the past, the extent entry starts before the
  257. * bitmap entry, but we're trying to remove an offset that falls
  258. * completely within the bitmap range and is in both the extent entry
  259. * and the bitmap entry, looks like this
  260. *
  261. * [ extent ]
  262. * [ bitmap ]
  263. * [ del ]
  264. */
  265. __btrfs_remove_free_space_cache(cache->free_space_ctl);
  266. ret = test_add_free_space_entry(cache, bitmap_offset + 4 * 1024 * 1024,
  267. 4 * 1024 * 1024, 1);
  268. if (ret) {
  269. test_msg("Couldn't add bitmap %d\n", ret);
  270. return ret;
  271. }
  272. ret = test_add_free_space_entry(cache, bitmap_offset - 1 * 1024 * 1024,
  273. 5 * 1024 * 1024, 0);
  274. if (ret) {
  275. test_msg("Couldn't add extent entry %d\n", ret);
  276. return ret;
  277. }
  278. ret = btrfs_remove_free_space(cache, bitmap_offset + 1 * 1024 * 1024,
  279. 5 * 1024 * 1024);
  280. if (ret) {
  281. test_msg("Failed to free our space %d\n", ret);
  282. return ret;
  283. }
  284. if (test_check_exists(cache, bitmap_offset + 1 * 1024 * 1024,
  285. 5 * 1024 * 1024)) {
  286. test_msg("Left stuff over\n");
  287. return -1;
  288. }
  289. __btrfs_remove_free_space_cache(cache->free_space_ctl);
  290. /*
  291. * This blew up before, we have part of the free space in a bitmap and
  292. * then the entirety of the rest of the space in an extent. This used
  293. * to return -EAGAIN back from btrfs_remove_extent, make sure this
  294. * doesn't happen.
  295. */
  296. ret = test_add_free_space_entry(cache, 1 * 1024 * 1024, 2 * 1024 * 1024, 1);
  297. if (ret) {
  298. test_msg("Couldn't add bitmap entry %d\n", ret);
  299. return ret;
  300. }
  301. ret = test_add_free_space_entry(cache, 3 * 1024 * 1024, 1 * 1024 * 1024, 0);
  302. if (ret) {
  303. test_msg("Couldn't add extent entry %d\n", ret);
  304. return ret;
  305. }
  306. ret = btrfs_remove_free_space(cache, 1 * 1024 * 1024, 3 * 1024 * 1024);
  307. if (ret) {
  308. test_msg("Error removing bitmap and extent overlapping %d\n", ret);
  309. return ret;
  310. }
  311. __btrfs_remove_free_space_cache(cache->free_space_ctl);
  312. return 0;
  313. }
  314. /* Used by test_steal_space_from_bitmap_to_extent(). */
  315. static bool test_use_bitmap(struct btrfs_free_space_ctl *ctl,
  316. struct btrfs_free_space *info)
  317. {
  318. return ctl->free_extents > 0;
  319. }
  320. /* Used by test_steal_space_from_bitmap_to_extent(). */
  321. static int
  322. check_num_extents_and_bitmaps(const struct btrfs_block_group_cache *cache,
  323. const int num_extents,
  324. const int num_bitmaps)
  325. {
  326. if (cache->free_space_ctl->free_extents != num_extents) {
  327. test_msg("Incorrect # of extent entries in the cache: %d, expected %d\n",
  328. cache->free_space_ctl->free_extents, num_extents);
  329. return -EINVAL;
  330. }
  331. if (cache->free_space_ctl->total_bitmaps != num_bitmaps) {
  332. test_msg("Incorrect # of extent entries in the cache: %d, expected %d\n",
  333. cache->free_space_ctl->total_bitmaps, num_bitmaps);
  334. return -EINVAL;
  335. }
  336. return 0;
  337. }
  338. /* Used by test_steal_space_from_bitmap_to_extent(). */
  339. static int check_cache_empty(struct btrfs_block_group_cache *cache)
  340. {
  341. u64 offset;
  342. u64 max_extent_size;
  343. /*
  344. * Now lets confirm that there's absolutely no free space left to
  345. * allocate.
  346. */
  347. if (cache->free_space_ctl->free_space != 0) {
  348. test_msg("Cache free space is not 0\n");
  349. return -EINVAL;
  350. }
  351. /* And any allocation request, no matter how small, should fail now. */
  352. offset = btrfs_find_space_for_alloc(cache, 0, 4096, 0,
  353. &max_extent_size);
  354. if (offset != 0) {
  355. test_msg("Space allocation did not fail, returned offset: %llu",
  356. offset);
  357. return -EINVAL;
  358. }
  359. /* And no extent nor bitmap entries in the cache anymore. */
  360. return check_num_extents_and_bitmaps(cache, 0, 0);
  361. }
  362. /*
  363. * Before we were able to steal free space from a bitmap entry to an extent
  364. * entry, we could end up with 2 entries representing a contiguous free space.
  365. * One would be an extent entry and the other a bitmap entry. Since in order
  366. * to allocate space to a caller we use only 1 entry, we couldn't return that
  367. * whole range to the caller if it was requested. This forced the caller to
  368. * either assume ENOSPC or perform several smaller space allocations, which
  369. * wasn't optimal as they could be spread all over the block group while under
  370. * concurrency (extra overhead and fragmentation).
  371. *
  372. * This stealing approach is benefical, since we always prefer to allocate from
  373. * extent entries, both for clustered and non-clustered allocation requests.
  374. */
  375. static int
  376. test_steal_space_from_bitmap_to_extent(struct btrfs_block_group_cache *cache)
  377. {
  378. int ret;
  379. u64 offset;
  380. u64 max_extent_size;
  381. bool (*use_bitmap_op)(struct btrfs_free_space_ctl *,
  382. struct btrfs_free_space *);
  383. test_msg("Running space stealing from bitmap to extent\n");
  384. /*
  385. * For this test, we want to ensure we end up with an extent entry
  386. * immediately adjacent to a bitmap entry, where the bitmap starts
  387. * at an offset where the extent entry ends. We keep adding and
  388. * removing free space to reach into this state, but to get there
  389. * we need to reach a point where marking new free space doesn't
  390. * result in adding new extent entries or merging the new space
  391. * with existing extent entries - the space ends up being marked
  392. * in an existing bitmap that covers the new free space range.
  393. *
  394. * To get there, we need to reach the threshold defined set at
  395. * cache->free_space_ctl->extents_thresh, which currently is
  396. * 256 extents on a x86_64 system at least, and a few other
  397. * conditions (check free_space_cache.c). Instead of making the
  398. * test much longer and complicated, use a "use_bitmap" operation
  399. * that forces use of bitmaps as soon as we have at least 1
  400. * extent entry.
  401. */
  402. use_bitmap_op = cache->free_space_ctl->op->use_bitmap;
  403. cache->free_space_ctl->op->use_bitmap = test_use_bitmap;
  404. /*
  405. * Extent entry covering free space range [128Mb - 256Kb, 128Mb - 128Kb[
  406. */
  407. ret = test_add_free_space_entry(cache, 128 * 1024 * 1024 - 256 * 1024,
  408. 128 * 1024, 0);
  409. if (ret) {
  410. test_msg("Couldn't add extent entry %d\n", ret);
  411. return ret;
  412. }
  413. /* Bitmap entry covering free space range [128Mb + 512Kb, 256Mb[ */
  414. ret = test_add_free_space_entry(cache, 128 * 1024 * 1024 + 512 * 1024,
  415. 128 * 1024 * 1024 - 512 * 1024, 1);
  416. if (ret) {
  417. test_msg("Couldn't add bitmap entry %d\n", ret);
  418. return ret;
  419. }
  420. ret = check_num_extents_and_bitmaps(cache, 2, 1);
  421. if (ret)
  422. return ret;
  423. /*
  424. * Now make only the first 256Kb of the bitmap marked as free, so that
  425. * we end up with only the following ranges marked as free space:
  426. *
  427. * [128Mb - 256Kb, 128Mb - 128Kb[
  428. * [128Mb + 512Kb, 128Mb + 768Kb[
  429. */
  430. ret = btrfs_remove_free_space(cache,
  431. 128 * 1024 * 1024 + 768 * 1024,
  432. 128 * 1024 * 1024 - 768 * 1024);
  433. if (ret) {
  434. test_msg("Failed to free part of bitmap space %d\n", ret);
  435. return ret;
  436. }
  437. /* Confirm that only those 2 ranges are marked as free. */
  438. if (!test_check_exists(cache, 128 * 1024 * 1024 - 256 * 1024,
  439. 128 * 1024)) {
  440. test_msg("Free space range missing\n");
  441. return -ENOENT;
  442. }
  443. if (!test_check_exists(cache, 128 * 1024 * 1024 + 512 * 1024,
  444. 256 * 1024)) {
  445. test_msg("Free space range missing\n");
  446. return -ENOENT;
  447. }
  448. /*
  449. * Confirm that the bitmap range [128Mb + 768Kb, 256Mb[ isn't marked
  450. * as free anymore.
  451. */
  452. if (test_check_exists(cache, 128 * 1024 * 1024 + 768 * 1024,
  453. 128 * 1024 * 1024 - 768 * 1024)) {
  454. test_msg("Bitmap region not removed from space cache\n");
  455. return -EINVAL;
  456. }
  457. /*
  458. * Confirm that the region [128Mb + 256Kb, 128Mb + 512Kb[, which is
  459. * covered by the bitmap, isn't marked as free.
  460. */
  461. if (test_check_exists(cache, 128 * 1024 * 1024 + 256 * 1024,
  462. 256 * 1024)) {
  463. test_msg("Invalid bitmap region marked as free\n");
  464. return -EINVAL;
  465. }
  466. /*
  467. * Confirm that the region [128Mb, 128Mb + 256Kb[, which is covered
  468. * by the bitmap too, isn't marked as free either.
  469. */
  470. if (test_check_exists(cache, 128 * 1024 * 1024,
  471. 256 * 1024)) {
  472. test_msg("Invalid bitmap region marked as free\n");
  473. return -EINVAL;
  474. }
  475. /*
  476. * Now lets mark the region [128Mb, 128Mb + 512Kb[ as free too. But,
  477. * lets make sure the free space cache marks it as free in the bitmap,
  478. * and doesn't insert a new extent entry to represent this region.
  479. */
  480. ret = btrfs_add_free_space(cache, 128 * 1024 * 1024, 512 * 1024);
  481. if (ret) {
  482. test_msg("Error adding free space: %d\n", ret);
  483. return ret;
  484. }
  485. /* Confirm the region is marked as free. */
  486. if (!test_check_exists(cache, 128 * 1024 * 1024, 512 * 1024)) {
  487. test_msg("Bitmap region not marked as free\n");
  488. return -ENOENT;
  489. }
  490. /*
  491. * Confirm that no new extent entries or bitmap entries were added to
  492. * the cache after adding that free space region.
  493. */
  494. ret = check_num_extents_and_bitmaps(cache, 2, 1);
  495. if (ret)
  496. return ret;
  497. /*
  498. * Now lets add a small free space region to the right of the previous
  499. * one, which is not contiguous with it and is part of the bitmap too.
  500. * The goal is to test that the bitmap entry space stealing doesn't
  501. * steal this space region.
  502. */
  503. ret = btrfs_add_free_space(cache, 128 * 1024 * 1024 + 16 * 1024 * 1024,
  504. 4096);
  505. if (ret) {
  506. test_msg("Error adding free space: %d\n", ret);
  507. return ret;
  508. }
  509. /*
  510. * Confirm that no new extent entries or bitmap entries were added to
  511. * the cache after adding that free space region.
  512. */
  513. ret = check_num_extents_and_bitmaps(cache, 2, 1);
  514. if (ret)
  515. return ret;
  516. /*
  517. * Now mark the region [128Mb - 128Kb, 128Mb[ as free too. This will
  518. * expand the range covered by the existing extent entry that represents
  519. * the free space [128Mb - 256Kb, 128Mb - 128Kb[.
  520. */
  521. ret = btrfs_add_free_space(cache, 128 * 1024 * 1024 - 128 * 1024,
  522. 128 * 1024);
  523. if (ret) {
  524. test_msg("Error adding free space: %d\n", ret);
  525. return ret;
  526. }
  527. /* Confirm the region is marked as free. */
  528. if (!test_check_exists(cache, 128 * 1024 * 1024 - 128 * 1024,
  529. 128 * 1024)) {
  530. test_msg("Extent region not marked as free\n");
  531. return -ENOENT;
  532. }
  533. /*
  534. * Confirm that our extent entry didn't stole all free space from the
  535. * bitmap, because of the small 4Kb free space region.
  536. */
  537. ret = check_num_extents_and_bitmaps(cache, 2, 1);
  538. if (ret)
  539. return ret;
  540. /*
  541. * So now we have the range [128Mb - 256Kb, 128Mb + 768Kb[ as free
  542. * space. Without stealing bitmap free space into extent entry space,
  543. * we would have all this free space represented by 2 entries in the
  544. * cache:
  545. *
  546. * extent entry covering range: [128Mb - 256Kb, 128Mb[
  547. * bitmap entry covering range: [128Mb, 128Mb + 768Kb[
  548. *
  549. * Attempting to allocate the whole free space (1Mb) would fail, because
  550. * we can't allocate from multiple entries.
  551. * With the bitmap free space stealing, we get a single extent entry
  552. * that represents the 1Mb free space, and therefore we're able to
  553. * allocate the whole free space at once.
  554. */
  555. if (!test_check_exists(cache, 128 * 1024 * 1024 - 256 * 1024,
  556. 1 * 1024 * 1024)) {
  557. test_msg("Expected region not marked as free\n");
  558. return -ENOENT;
  559. }
  560. if (cache->free_space_ctl->free_space != (1 * 1024 * 1024 + 4096)) {
  561. test_msg("Cache free space is not 1Mb + 4Kb\n");
  562. return -EINVAL;
  563. }
  564. offset = btrfs_find_space_for_alloc(cache,
  565. 0, 1 * 1024 * 1024, 0,
  566. &max_extent_size);
  567. if (offset != (128 * 1024 * 1024 - 256 * 1024)) {
  568. test_msg("Failed to allocate 1Mb from space cache, returned offset is: %llu\n",
  569. offset);
  570. return -EINVAL;
  571. }
  572. /* All that remains is a 4Kb free space region in a bitmap. Confirm. */
  573. ret = check_num_extents_and_bitmaps(cache, 1, 1);
  574. if (ret)
  575. return ret;
  576. if (cache->free_space_ctl->free_space != 4096) {
  577. test_msg("Cache free space is not 4Kb\n");
  578. return -EINVAL;
  579. }
  580. offset = btrfs_find_space_for_alloc(cache,
  581. 0, 4096, 0,
  582. &max_extent_size);
  583. if (offset != (128 * 1024 * 1024 + 16 * 1024 * 1024)) {
  584. test_msg("Failed to allocate 4Kb from space cache, returned offset is: %llu\n",
  585. offset);
  586. return -EINVAL;
  587. }
  588. ret = check_cache_empty(cache);
  589. if (ret)
  590. return ret;
  591. __btrfs_remove_free_space_cache(cache->free_space_ctl);
  592. /*
  593. * Now test a similar scenario, but where our extent entry is located
  594. * to the right of the bitmap entry, so that we can check that stealing
  595. * space from a bitmap to the front of an extent entry works.
  596. */
  597. /*
  598. * Extent entry covering free space range [128Mb + 128Kb, 128Mb + 256Kb[
  599. */
  600. ret = test_add_free_space_entry(cache, 128 * 1024 * 1024 + 128 * 1024,
  601. 128 * 1024, 0);
  602. if (ret) {
  603. test_msg("Couldn't add extent entry %d\n", ret);
  604. return ret;
  605. }
  606. /* Bitmap entry covering free space range [0, 128Mb - 512Kb[ */
  607. ret = test_add_free_space_entry(cache, 0,
  608. 128 * 1024 * 1024 - 512 * 1024, 1);
  609. if (ret) {
  610. test_msg("Couldn't add bitmap entry %d\n", ret);
  611. return ret;
  612. }
  613. ret = check_num_extents_and_bitmaps(cache, 2, 1);
  614. if (ret)
  615. return ret;
  616. /*
  617. * Now make only the last 256Kb of the bitmap marked as free, so that
  618. * we end up with only the following ranges marked as free space:
  619. *
  620. * [128Mb + 128b, 128Mb + 256Kb[
  621. * [128Mb - 768Kb, 128Mb - 512Kb[
  622. */
  623. ret = btrfs_remove_free_space(cache,
  624. 0,
  625. 128 * 1024 * 1024 - 768 * 1024);
  626. if (ret) {
  627. test_msg("Failed to free part of bitmap space %d\n", ret);
  628. return ret;
  629. }
  630. /* Confirm that only those 2 ranges are marked as free. */
  631. if (!test_check_exists(cache, 128 * 1024 * 1024 + 128 * 1024,
  632. 128 * 1024)) {
  633. test_msg("Free space range missing\n");
  634. return -ENOENT;
  635. }
  636. if (!test_check_exists(cache, 128 * 1024 * 1024 - 768 * 1024,
  637. 256 * 1024)) {
  638. test_msg("Free space range missing\n");
  639. return -ENOENT;
  640. }
  641. /*
  642. * Confirm that the bitmap range [0, 128Mb - 768Kb[ isn't marked
  643. * as free anymore.
  644. */
  645. if (test_check_exists(cache, 0,
  646. 128 * 1024 * 1024 - 768 * 1024)) {
  647. test_msg("Bitmap region not removed from space cache\n");
  648. return -EINVAL;
  649. }
  650. /*
  651. * Confirm that the region [128Mb - 512Kb, 128Mb[, which is
  652. * covered by the bitmap, isn't marked as free.
  653. */
  654. if (test_check_exists(cache, 128 * 1024 * 1024 - 512 * 1024,
  655. 512 * 1024)) {
  656. test_msg("Invalid bitmap region marked as free\n");
  657. return -EINVAL;
  658. }
  659. /*
  660. * Now lets mark the region [128Mb - 512Kb, 128Mb[ as free too. But,
  661. * lets make sure the free space cache marks it as free in the bitmap,
  662. * and doesn't insert a new extent entry to represent this region.
  663. */
  664. ret = btrfs_add_free_space(cache, 128 * 1024 * 1024 - 512 * 1024,
  665. 512 * 1024);
  666. if (ret) {
  667. test_msg("Error adding free space: %d\n", ret);
  668. return ret;
  669. }
  670. /* Confirm the region is marked as free. */
  671. if (!test_check_exists(cache, 128 * 1024 * 1024 - 512 * 1024,
  672. 512 * 1024)) {
  673. test_msg("Bitmap region not marked as free\n");
  674. return -ENOENT;
  675. }
  676. /*
  677. * Confirm that no new extent entries or bitmap entries were added to
  678. * the cache after adding that free space region.
  679. */
  680. ret = check_num_extents_and_bitmaps(cache, 2, 1);
  681. if (ret)
  682. return ret;
  683. /*
  684. * Now lets add a small free space region to the left of the previous
  685. * one, which is not contiguous with it and is part of the bitmap too.
  686. * The goal is to test that the bitmap entry space stealing doesn't
  687. * steal this space region.
  688. */
  689. ret = btrfs_add_free_space(cache, 32 * 1024 * 1024, 8192);
  690. if (ret) {
  691. test_msg("Error adding free space: %d\n", ret);
  692. return ret;
  693. }
  694. /*
  695. * Now mark the region [128Mb, 128Mb + 128Kb[ as free too. This will
  696. * expand the range covered by the existing extent entry that represents
  697. * the free space [128Mb + 128Kb, 128Mb + 256Kb[.
  698. */
  699. ret = btrfs_add_free_space(cache, 128 * 1024 * 1024, 128 * 1024);
  700. if (ret) {
  701. test_msg("Error adding free space: %d\n", ret);
  702. return ret;
  703. }
  704. /* Confirm the region is marked as free. */
  705. if (!test_check_exists(cache, 128 * 1024 * 1024, 128 * 1024)) {
  706. test_msg("Extent region not marked as free\n");
  707. return -ENOENT;
  708. }
  709. /*
  710. * Confirm that our extent entry didn't stole all free space from the
  711. * bitmap, because of the small 8Kb free space region.
  712. */
  713. ret = check_num_extents_and_bitmaps(cache, 2, 1);
  714. if (ret)
  715. return ret;
  716. /*
  717. * So now we have the range [128Mb - 768Kb, 128Mb + 256Kb[ as free
  718. * space. Without stealing bitmap free space into extent entry space,
  719. * we would have all this free space represented by 2 entries in the
  720. * cache:
  721. *
  722. * extent entry covering range: [128Mb, 128Mb + 256Kb[
  723. * bitmap entry covering range: [128Mb - 768Kb, 128Mb[
  724. *
  725. * Attempting to allocate the whole free space (1Mb) would fail, because
  726. * we can't allocate from multiple entries.
  727. * With the bitmap free space stealing, we get a single extent entry
  728. * that represents the 1Mb free space, and therefore we're able to
  729. * allocate the whole free space at once.
  730. */
  731. if (!test_check_exists(cache, 128 * 1024 * 1024 - 768 * 1024,
  732. 1 * 1024 * 1024)) {
  733. test_msg("Expected region not marked as free\n");
  734. return -ENOENT;
  735. }
  736. if (cache->free_space_ctl->free_space != (1 * 1024 * 1024 + 8192)) {
  737. test_msg("Cache free space is not 1Mb + 8Kb\n");
  738. return -EINVAL;
  739. }
  740. offset = btrfs_find_space_for_alloc(cache,
  741. 0, 1 * 1024 * 1024, 0,
  742. &max_extent_size);
  743. if (offset != (128 * 1024 * 1024 - 768 * 1024)) {
  744. test_msg("Failed to allocate 1Mb from space cache, returned offset is: %llu\n",
  745. offset);
  746. return -EINVAL;
  747. }
  748. /* All that remains is a 8Kb free space region in a bitmap. Confirm. */
  749. ret = check_num_extents_and_bitmaps(cache, 1, 1);
  750. if (ret)
  751. return ret;
  752. if (cache->free_space_ctl->free_space != 8192) {
  753. test_msg("Cache free space is not 8Kb\n");
  754. return -EINVAL;
  755. }
  756. offset = btrfs_find_space_for_alloc(cache,
  757. 0, 8192, 0,
  758. &max_extent_size);
  759. if (offset != (32 * 1024 * 1024)) {
  760. test_msg("Failed to allocate 8Kb from space cache, returned offset is: %llu\n",
  761. offset);
  762. return -EINVAL;
  763. }
  764. ret = check_cache_empty(cache);
  765. if (ret)
  766. return ret;
  767. cache->free_space_ctl->op->use_bitmap = use_bitmap_op;
  768. __btrfs_remove_free_space_cache(cache->free_space_ctl);
  769. return 0;
  770. }
  771. int btrfs_test_free_space_cache(void)
  772. {
  773. struct btrfs_block_group_cache *cache;
  774. struct btrfs_root *root = NULL;
  775. int ret = -ENOMEM;
  776. test_msg("Running btrfs free space cache tests\n");
  777. cache = init_test_block_group();
  778. if (!cache) {
  779. test_msg("Couldn't run the tests\n");
  780. return 0;
  781. }
  782. root = btrfs_alloc_dummy_root();
  783. if (IS_ERR(root)) {
  784. ret = PTR_ERR(root);
  785. goto out;
  786. }
  787. root->fs_info = btrfs_alloc_dummy_fs_info();
  788. if (!root->fs_info)
  789. goto out;
  790. root->fs_info->extent_root = root;
  791. cache->fs_info = root->fs_info;
  792. ret = test_extents(cache);
  793. if (ret)
  794. goto out;
  795. ret = test_bitmaps(cache);
  796. if (ret)
  797. goto out;
  798. ret = test_bitmaps_and_extents(cache);
  799. if (ret)
  800. goto out;
  801. ret = test_steal_space_from_bitmap_to_extent(cache);
  802. out:
  803. __btrfs_remove_free_space_cache(cache->free_space_ctl);
  804. kfree(cache->free_space_ctl);
  805. kfree(cache);
  806. btrfs_free_dummy_root(root);
  807. test_msg("Free space cache tests finished\n");
  808. return ret;
  809. }