free-space-tests.c 25 KB

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