free-space-tests.c 26 KB

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