free-space-tests.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  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. /*
  25. * This test just does basic sanity checking, making sure we can add an exten
  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 benefical, since we always prefer to allocate from
  338. * extent entries, both for clustered and non-clustered allocation requests.
  339. */
  340. static int
  341. test_steal_space_from_bitmap_to_extent(struct btrfs_block_group_cache *cache)
  342. {
  343. int ret;
  344. u64 offset;
  345. u64 max_extent_size;
  346. const struct btrfs_free_space_op test_free_space_ops = {
  347. .recalc_thresholds = cache->free_space_ctl->op->recalc_thresholds,
  348. .use_bitmap = test_use_bitmap,
  349. };
  350. const struct btrfs_free_space_op *orig_free_space_ops;
  351. test_msg("Running space stealing from bitmap to extent\n");
  352. /*
  353. * For this test, we want to ensure we end up with an extent entry
  354. * immediately adjacent to a bitmap entry, where the bitmap starts
  355. * at an offset where the extent entry ends. We keep adding and
  356. * removing free space to reach into this state, but to get there
  357. * we need to reach a point where marking new free space doesn't
  358. * result in adding new extent entries or merging the new space
  359. * with existing extent entries - the space ends up being marked
  360. * in an existing bitmap that covers the new free space range.
  361. *
  362. * To get there, we need to reach the threshold defined set at
  363. * cache->free_space_ctl->extents_thresh, which currently is
  364. * 256 extents on a x86_64 system at least, and a few other
  365. * conditions (check free_space_cache.c). Instead of making the
  366. * test much longer and complicated, use a "use_bitmap" operation
  367. * that forces use of bitmaps as soon as we have at least 1
  368. * extent entry.
  369. */
  370. orig_free_space_ops = cache->free_space_ctl->op;
  371. cache->free_space_ctl->op = &test_free_space_ops;
  372. /*
  373. * Extent entry covering free space range [128Mb - 256Kb, 128Mb - 128Kb[
  374. */
  375. ret = test_add_free_space_entry(cache, SZ_128M - SZ_256K, SZ_128K, 0);
  376. if (ret) {
  377. test_msg("Couldn't add extent entry %d\n", ret);
  378. return ret;
  379. }
  380. /* Bitmap entry covering free space range [128Mb + 512Kb, 256Mb[ */
  381. ret = test_add_free_space_entry(cache, SZ_128M + SZ_512K,
  382. SZ_128M - SZ_512K, 1);
  383. if (ret) {
  384. test_msg("Couldn't add bitmap entry %d\n", ret);
  385. return ret;
  386. }
  387. ret = check_num_extents_and_bitmaps(cache, 2, 1);
  388. if (ret)
  389. return ret;
  390. /*
  391. * Now make only the first 256Kb of the bitmap marked as free, so that
  392. * we end up with only the following ranges marked as free space:
  393. *
  394. * [128Mb - 256Kb, 128Mb - 128Kb[
  395. * [128Mb + 512Kb, 128Mb + 768Kb[
  396. */
  397. ret = btrfs_remove_free_space(cache,
  398. SZ_128M + 768 * SZ_1K,
  399. SZ_128M - 768 * SZ_1K);
  400. if (ret) {
  401. test_msg("Failed to free part of bitmap space %d\n", ret);
  402. return ret;
  403. }
  404. /* Confirm that only those 2 ranges are marked as free. */
  405. if (!test_check_exists(cache, SZ_128M - SZ_256K, SZ_128K)) {
  406. test_msg("Free space range missing\n");
  407. return -ENOENT;
  408. }
  409. if (!test_check_exists(cache, SZ_128M + SZ_512K, SZ_256K)) {
  410. test_msg("Free space range missing\n");
  411. return -ENOENT;
  412. }
  413. /*
  414. * Confirm that the bitmap range [128Mb + 768Kb, 256Mb[ isn't marked
  415. * as free anymore.
  416. */
  417. if (test_check_exists(cache, SZ_128M + 768 * SZ_1K,
  418. SZ_128M - 768 * SZ_1K)) {
  419. test_msg("Bitmap region not removed from space cache\n");
  420. return -EINVAL;
  421. }
  422. /*
  423. * Confirm that the region [128Mb + 256Kb, 128Mb + 512Kb[, which is
  424. * covered by the bitmap, isn't marked as free.
  425. */
  426. if (test_check_exists(cache, SZ_128M + SZ_256K, SZ_256K)) {
  427. test_msg("Invalid bitmap region marked as free\n");
  428. return -EINVAL;
  429. }
  430. /*
  431. * Confirm that the region [128Mb, 128Mb + 256Kb[, which is covered
  432. * by the bitmap too, isn't marked as free either.
  433. */
  434. if (test_check_exists(cache, SZ_128M, SZ_256K)) {
  435. test_msg("Invalid bitmap region marked as free\n");
  436. return -EINVAL;
  437. }
  438. /*
  439. * Now lets mark the region [128Mb, 128Mb + 512Kb[ as free too. But,
  440. * lets make sure the free space cache marks it as free in the bitmap,
  441. * and doesn't insert a new extent entry to represent this region.
  442. */
  443. ret = btrfs_add_free_space(cache, SZ_128M, SZ_512K);
  444. if (ret) {
  445. test_msg("Error adding free space: %d\n", ret);
  446. return ret;
  447. }
  448. /* Confirm the region is marked as free. */
  449. if (!test_check_exists(cache, SZ_128M, SZ_512K)) {
  450. test_msg("Bitmap region not marked as free\n");
  451. return -ENOENT;
  452. }
  453. /*
  454. * Confirm that no new extent entries or bitmap entries were added to
  455. * the cache after adding that free space region.
  456. */
  457. ret = check_num_extents_and_bitmaps(cache, 2, 1);
  458. if (ret)
  459. return ret;
  460. /*
  461. * Now lets add a small free space region to the right of the previous
  462. * one, which is not contiguous with it and is part of the bitmap too.
  463. * The goal is to test that the bitmap entry space stealing doesn't
  464. * steal this space region.
  465. */
  466. ret = btrfs_add_free_space(cache, SZ_128M + SZ_16M, 4096);
  467. if (ret) {
  468. test_msg("Error adding free space: %d\n", ret);
  469. return ret;
  470. }
  471. /*
  472. * Confirm that no new extent entries or bitmap entries were added to
  473. * the cache after adding that free space region.
  474. */
  475. ret = check_num_extents_and_bitmaps(cache, 2, 1);
  476. if (ret)
  477. return ret;
  478. /*
  479. * Now mark the region [128Mb - 128Kb, 128Mb[ as free too. This will
  480. * expand the range covered by the existing extent entry that represents
  481. * the free space [128Mb - 256Kb, 128Mb - 128Kb[.
  482. */
  483. ret = btrfs_add_free_space(cache, SZ_128M - SZ_128K, SZ_128K);
  484. if (ret) {
  485. test_msg("Error adding free space: %d\n", ret);
  486. return ret;
  487. }
  488. /* Confirm the region is marked as free. */
  489. if (!test_check_exists(cache, SZ_128M - SZ_128K, SZ_128K)) {
  490. test_msg("Extent region not marked as free\n");
  491. return -ENOENT;
  492. }
  493. /*
  494. * Confirm that our extent entry didn't stole all free space from the
  495. * bitmap, because of the small 4Kb free space region.
  496. */
  497. ret = check_num_extents_and_bitmaps(cache, 2, 1);
  498. if (ret)
  499. return ret;
  500. /*
  501. * So now we have the range [128Mb - 256Kb, 128Mb + 768Kb[ as free
  502. * space. Without stealing bitmap free space into extent entry space,
  503. * we would have all this free space represented by 2 entries in the
  504. * cache:
  505. *
  506. * extent entry covering range: [128Mb - 256Kb, 128Mb[
  507. * bitmap entry covering range: [128Mb, 128Mb + 768Kb[
  508. *
  509. * Attempting to allocate the whole free space (1Mb) would fail, because
  510. * we can't allocate from multiple entries.
  511. * With the bitmap free space stealing, we get a single extent entry
  512. * that represents the 1Mb free space, and therefore we're able to
  513. * allocate the whole free space at once.
  514. */
  515. if (!test_check_exists(cache, SZ_128M - SZ_256K, SZ_1M)) {
  516. test_msg("Expected region not marked as free\n");
  517. return -ENOENT;
  518. }
  519. if (cache->free_space_ctl->free_space != (SZ_1M + 4096)) {
  520. test_msg("Cache free space is not 1Mb + 4Kb\n");
  521. return -EINVAL;
  522. }
  523. offset = btrfs_find_space_for_alloc(cache,
  524. 0, SZ_1M, 0,
  525. &max_extent_size);
  526. if (offset != (SZ_128M - SZ_256K)) {
  527. test_msg("Failed to allocate 1Mb from space cache, returned offset is: %llu\n",
  528. offset);
  529. return -EINVAL;
  530. }
  531. /* All that remains is a 4Kb free space region in a bitmap. Confirm. */
  532. ret = check_num_extents_and_bitmaps(cache, 1, 1);
  533. if (ret)
  534. return ret;
  535. if (cache->free_space_ctl->free_space != 4096) {
  536. test_msg("Cache free space is not 4Kb\n");
  537. return -EINVAL;
  538. }
  539. offset = btrfs_find_space_for_alloc(cache,
  540. 0, 4096, 0,
  541. &max_extent_size);
  542. if (offset != (SZ_128M + SZ_16M)) {
  543. test_msg("Failed to allocate 4Kb from space cache, returned offset is: %llu\n",
  544. offset);
  545. return -EINVAL;
  546. }
  547. ret = check_cache_empty(cache);
  548. if (ret)
  549. return ret;
  550. __btrfs_remove_free_space_cache(cache->free_space_ctl);
  551. /*
  552. * Now test a similar scenario, but where our extent entry is located
  553. * to the right of the bitmap entry, so that we can check that stealing
  554. * space from a bitmap to the front of an extent entry works.
  555. */
  556. /*
  557. * Extent entry covering free space range [128Mb + 128Kb, 128Mb + 256Kb[
  558. */
  559. ret = test_add_free_space_entry(cache, SZ_128M + SZ_128K, SZ_128K, 0);
  560. if (ret) {
  561. test_msg("Couldn't add extent entry %d\n", ret);
  562. return ret;
  563. }
  564. /* Bitmap entry covering free space range [0, 128Mb - 512Kb[ */
  565. ret = test_add_free_space_entry(cache, 0, SZ_128M - SZ_512K, 1);
  566. if (ret) {
  567. test_msg("Couldn't add bitmap entry %d\n", ret);
  568. return ret;
  569. }
  570. ret = check_num_extents_and_bitmaps(cache, 2, 1);
  571. if (ret)
  572. return ret;
  573. /*
  574. * Now make only the last 256Kb of the bitmap marked as free, so that
  575. * we end up with only the following ranges marked as free space:
  576. *
  577. * [128Mb + 128b, 128Mb + 256Kb[
  578. * [128Mb - 768Kb, 128Mb - 512Kb[
  579. */
  580. ret = btrfs_remove_free_space(cache, 0, SZ_128M - 768 * SZ_1K);
  581. if (ret) {
  582. test_msg("Failed to free part of bitmap space %d\n", ret);
  583. return ret;
  584. }
  585. /* Confirm that only those 2 ranges are marked as free. */
  586. if (!test_check_exists(cache, SZ_128M + SZ_128K, SZ_128K)) {
  587. test_msg("Free space range missing\n");
  588. return -ENOENT;
  589. }
  590. if (!test_check_exists(cache, SZ_128M - 768 * SZ_1K, SZ_256K)) {
  591. test_msg("Free space range missing\n");
  592. return -ENOENT;
  593. }
  594. /*
  595. * Confirm that the bitmap range [0, 128Mb - 768Kb[ isn't marked
  596. * as free anymore.
  597. */
  598. if (test_check_exists(cache, 0, SZ_128M - 768 * SZ_1K)) {
  599. test_msg("Bitmap region not removed from space cache\n");
  600. return -EINVAL;
  601. }
  602. /*
  603. * Confirm that the region [128Mb - 512Kb, 128Mb[, which is
  604. * covered by the bitmap, isn't marked as free.
  605. */
  606. if (test_check_exists(cache, SZ_128M - SZ_512K, SZ_512K)) {
  607. test_msg("Invalid bitmap region marked as free\n");
  608. return -EINVAL;
  609. }
  610. /*
  611. * Now lets mark the region [128Mb - 512Kb, 128Mb[ as free too. But,
  612. * lets make sure the free space cache marks it as free in the bitmap,
  613. * and doesn't insert a new extent entry to represent this region.
  614. */
  615. ret = btrfs_add_free_space(cache, SZ_128M - SZ_512K, SZ_512K);
  616. if (ret) {
  617. test_msg("Error adding free space: %d\n", ret);
  618. return ret;
  619. }
  620. /* Confirm the region is marked as free. */
  621. if (!test_check_exists(cache, SZ_128M - SZ_512K, SZ_512K)) {
  622. test_msg("Bitmap region not marked as free\n");
  623. return -ENOENT;
  624. }
  625. /*
  626. * Confirm that no new extent entries or bitmap entries were added to
  627. * the cache after adding that free space region.
  628. */
  629. ret = check_num_extents_and_bitmaps(cache, 2, 1);
  630. if (ret)
  631. return ret;
  632. /*
  633. * Now lets add a small free space region to the left of the previous
  634. * one, which is not contiguous with it and is part of the bitmap too.
  635. * The goal is to test that the bitmap entry space stealing doesn't
  636. * steal this space region.
  637. */
  638. ret = btrfs_add_free_space(cache, SZ_32M, 8192);
  639. if (ret) {
  640. test_msg("Error adding free space: %d\n", ret);
  641. return ret;
  642. }
  643. /*
  644. * Now mark the region [128Mb, 128Mb + 128Kb[ as free too. This will
  645. * expand the range covered by the existing extent entry that represents
  646. * the free space [128Mb + 128Kb, 128Mb + 256Kb[.
  647. */
  648. ret = btrfs_add_free_space(cache, SZ_128M, SZ_128K);
  649. if (ret) {
  650. test_msg("Error adding free space: %d\n", ret);
  651. return ret;
  652. }
  653. /* Confirm the region is marked as free. */
  654. if (!test_check_exists(cache, SZ_128M, SZ_128K)) {
  655. test_msg("Extent region not marked as free\n");
  656. return -ENOENT;
  657. }
  658. /*
  659. * Confirm that our extent entry didn't stole all free space from the
  660. * bitmap, because of the small 8Kb free space region.
  661. */
  662. ret = check_num_extents_and_bitmaps(cache, 2, 1);
  663. if (ret)
  664. return ret;
  665. /*
  666. * So now we have the range [128Mb - 768Kb, 128Mb + 256Kb[ as free
  667. * space. Without stealing bitmap free space into extent entry space,
  668. * we would have all this free space represented by 2 entries in the
  669. * cache:
  670. *
  671. * extent entry covering range: [128Mb, 128Mb + 256Kb[
  672. * bitmap entry covering range: [128Mb - 768Kb, 128Mb[
  673. *
  674. * Attempting to allocate the whole free space (1Mb) would fail, because
  675. * we can't allocate from multiple entries.
  676. * With the bitmap free space stealing, we get a single extent entry
  677. * that represents the 1Mb free space, and therefore we're able to
  678. * allocate the whole free space at once.
  679. */
  680. if (!test_check_exists(cache, SZ_128M - 768 * SZ_1K, SZ_1M)) {
  681. test_msg("Expected region not marked as free\n");
  682. return -ENOENT;
  683. }
  684. if (cache->free_space_ctl->free_space != (SZ_1M + 8192)) {
  685. test_msg("Cache free space is not 1Mb + 8Kb\n");
  686. return -EINVAL;
  687. }
  688. offset = btrfs_find_space_for_alloc(cache, 0, SZ_1M, 0,
  689. &max_extent_size);
  690. if (offset != (SZ_128M - 768 * SZ_1K)) {
  691. test_msg("Failed to allocate 1Mb from space cache, returned offset is: %llu\n",
  692. offset);
  693. return -EINVAL;
  694. }
  695. /* All that remains is a 8Kb free space region in a bitmap. Confirm. */
  696. ret = check_num_extents_and_bitmaps(cache, 1, 1);
  697. if (ret)
  698. return ret;
  699. if (cache->free_space_ctl->free_space != 8192) {
  700. test_msg("Cache free space is not 8Kb\n");
  701. return -EINVAL;
  702. }
  703. offset = btrfs_find_space_for_alloc(cache,
  704. 0, 8192, 0,
  705. &max_extent_size);
  706. if (offset != SZ_32M) {
  707. test_msg("Failed to allocate 8Kb from space cache, returned offset is: %llu\n",
  708. offset);
  709. return -EINVAL;
  710. }
  711. ret = check_cache_empty(cache);
  712. if (ret)
  713. return ret;
  714. cache->free_space_ctl->op = orig_free_space_ops;
  715. __btrfs_remove_free_space_cache(cache->free_space_ctl);
  716. return 0;
  717. }
  718. int btrfs_test_free_space_cache(void)
  719. {
  720. struct btrfs_block_group_cache *cache;
  721. struct btrfs_root *root = NULL;
  722. int ret = -ENOMEM;
  723. test_msg("Running btrfs free space cache tests\n");
  724. cache = btrfs_alloc_dummy_block_group(1024 * 1024 * 1024);
  725. if (!cache) {
  726. test_msg("Couldn't run the tests\n");
  727. return 0;
  728. }
  729. root = btrfs_alloc_dummy_root();
  730. if (IS_ERR(root)) {
  731. ret = PTR_ERR(root);
  732. goto out;
  733. }
  734. root->fs_info = btrfs_alloc_dummy_fs_info();
  735. if (!root->fs_info)
  736. goto out;
  737. root->fs_info->extent_root = root;
  738. cache->fs_info = root->fs_info;
  739. ret = test_extents(cache);
  740. if (ret)
  741. goto out;
  742. ret = test_bitmaps(cache);
  743. if (ret)
  744. goto out;
  745. ret = test_bitmaps_and_extents(cache);
  746. if (ret)
  747. goto out;
  748. ret = test_steal_space_from_bitmap_to_extent(cache);
  749. out:
  750. btrfs_free_dummy_block_group(cache);
  751. btrfs_free_dummy_root(root);
  752. test_msg("Free space cache tests finished\n");
  753. return ret;
  754. }