free-space-tests.c 24 KB

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