free-space-tests.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2013 Fusion IO. All rights reserved.
  4. */
  5. #include <linux/slab.h>
  6. #include "btrfs-tests.h"
  7. #include "../ctree.h"
  8. #include "../disk-io.h"
  9. #include "../free-space-cache.h"
  10. #include "../block-group.h"
  11. #define BITS_PER_BITMAP (PAGE_SIZE * 8UL)
  12. /*
  13. * This test just does basic sanity checking, making sure we can add an extent
  14. * entry and remove space from either end and the middle, and make sure we can
  15. * remove space that covers adjacent extent entries.
  16. */
  17. static int test_extents(struct btrfs_block_group *cache)
  18. {
  19. int ret = 0;
  20. test_msg("running extent only tests");
  21. /* First just make sure we can remove an entire entry */
  22. ret = btrfs_add_free_space(cache, 0, SZ_4M);
  23. if (ret) {
  24. test_err("error adding initial extents %d", ret);
  25. return ret;
  26. }
  27. ret = btrfs_remove_free_space(cache, 0, SZ_4M);
  28. if (ret) {
  29. test_err("error removing extent %d", ret);
  30. return ret;
  31. }
  32. if (test_check_exists(cache, 0, SZ_4M)) {
  33. test_err("full remove left some lingering space");
  34. return -1;
  35. }
  36. /* Ok edge and middle cases now */
  37. ret = btrfs_add_free_space(cache, 0, SZ_4M);
  38. if (ret) {
  39. test_err("error adding half extent %d", ret);
  40. return ret;
  41. }
  42. ret = btrfs_remove_free_space(cache, 3 * SZ_1M, SZ_1M);
  43. if (ret) {
  44. test_err("error removing tail end %d", ret);
  45. return ret;
  46. }
  47. ret = btrfs_remove_free_space(cache, 0, SZ_1M);
  48. if (ret) {
  49. test_err("error removing front end %d", ret);
  50. return ret;
  51. }
  52. ret = btrfs_remove_free_space(cache, SZ_2M, 4096);
  53. if (ret) {
  54. test_err("error removing middle piece %d", ret);
  55. return ret;
  56. }
  57. if (test_check_exists(cache, 0, SZ_1M)) {
  58. test_err("still have space at the front");
  59. return -1;
  60. }
  61. if (test_check_exists(cache, SZ_2M, 4096)) {
  62. test_err("still have space in the middle");
  63. return -1;
  64. }
  65. if (test_check_exists(cache, 3 * SZ_1M, SZ_1M)) {
  66. test_err("still have space at the end");
  67. return -1;
  68. }
  69. /* Cleanup */
  70. btrfs_remove_free_space_cache(cache);
  71. return 0;
  72. }
  73. static int test_bitmaps(struct btrfs_block_group *cache, u32 sectorsize)
  74. {
  75. u64 next_bitmap_offset;
  76. int ret;
  77. test_msg("running bitmap only tests");
  78. ret = test_add_free_space_entry(cache, 0, SZ_4M, 1);
  79. if (ret) {
  80. test_err("couldn't create a bitmap entry %d", ret);
  81. return ret;
  82. }
  83. ret = btrfs_remove_free_space(cache, 0, SZ_4M);
  84. if (ret) {
  85. test_err("error removing bitmap full range %d", ret);
  86. return ret;
  87. }
  88. if (test_check_exists(cache, 0, SZ_4M)) {
  89. test_err("left some space in bitmap");
  90. return -1;
  91. }
  92. ret = test_add_free_space_entry(cache, 0, SZ_4M, 1);
  93. if (ret) {
  94. test_err("couldn't add to our bitmap entry %d", ret);
  95. return ret;
  96. }
  97. ret = btrfs_remove_free_space(cache, SZ_1M, SZ_2M);
  98. if (ret) {
  99. test_err("couldn't remove middle chunk %d", ret);
  100. return ret;
  101. }
  102. /*
  103. * The first bitmap we have starts at offset 0 so the next one is just
  104. * at the end of the first bitmap.
  105. */
  106. next_bitmap_offset = (u64)(BITS_PER_BITMAP * sectorsize);
  107. /* Test a bit straddling two bitmaps */
  108. ret = test_add_free_space_entry(cache, next_bitmap_offset - SZ_2M,
  109. SZ_4M, 1);
  110. if (ret) {
  111. test_err("couldn't add space that straddles two bitmaps %d",
  112. ret);
  113. return ret;
  114. }
  115. ret = btrfs_remove_free_space(cache, next_bitmap_offset - SZ_1M, SZ_2M);
  116. if (ret) {
  117. test_err("couldn't remove overlapping space %d", ret);
  118. return ret;
  119. }
  120. if (test_check_exists(cache, next_bitmap_offset - SZ_1M, SZ_2M)) {
  121. test_err("left some space when removing overlapping");
  122. return -1;
  123. }
  124. btrfs_remove_free_space_cache(cache);
  125. return 0;
  126. }
  127. /* This is the high grade jackassery */
  128. static int test_bitmaps_and_extents(struct btrfs_block_group *cache,
  129. u32 sectorsize)
  130. {
  131. u64 bitmap_offset = (u64)(BITS_PER_BITMAP * sectorsize);
  132. int ret;
  133. test_msg("running bitmap and extent tests");
  134. /*
  135. * First let's do something simple, an extent at the same offset as the
  136. * bitmap, but the free space completely in the extent and then
  137. * completely in the bitmap.
  138. */
  139. ret = test_add_free_space_entry(cache, SZ_4M, SZ_1M, 1);
  140. if (ret) {
  141. test_err("couldn't create bitmap entry %d", ret);
  142. return ret;
  143. }
  144. ret = test_add_free_space_entry(cache, 0, SZ_1M, 0);
  145. if (ret) {
  146. test_err("couldn't add extent entry %d", ret);
  147. return ret;
  148. }
  149. ret = btrfs_remove_free_space(cache, 0, SZ_1M);
  150. if (ret) {
  151. test_err("couldn't remove extent entry %d", ret);
  152. return ret;
  153. }
  154. if (test_check_exists(cache, 0, SZ_1M)) {
  155. test_err("left remnants after our remove");
  156. return -1;
  157. }
  158. /* Now to add back the extent entry and remove from the bitmap */
  159. ret = test_add_free_space_entry(cache, 0, SZ_1M, 0);
  160. if (ret) {
  161. test_err("couldn't re-add extent entry %d", ret);
  162. return ret;
  163. }
  164. ret = btrfs_remove_free_space(cache, SZ_4M, SZ_1M);
  165. if (ret) {
  166. test_err("couldn't remove from bitmap %d", ret);
  167. return ret;
  168. }
  169. if (test_check_exists(cache, SZ_4M, SZ_1M)) {
  170. test_err("left remnants in the bitmap");
  171. return -1;
  172. }
  173. /*
  174. * Ok so a little more evil, extent entry and bitmap at the same offset,
  175. * removing an overlapping chunk.
  176. */
  177. ret = test_add_free_space_entry(cache, SZ_1M, SZ_4M, 1);
  178. if (ret) {
  179. test_err("couldn't add to a bitmap %d", ret);
  180. return ret;
  181. }
  182. ret = btrfs_remove_free_space(cache, SZ_512K, 3 * SZ_1M);
  183. if (ret) {
  184. test_err("couldn't remove overlapping space %d", ret);
  185. return ret;
  186. }
  187. if (test_check_exists(cache, SZ_512K, 3 * SZ_1M)) {
  188. test_err("left over pieces after removing overlapping");
  189. return -1;
  190. }
  191. btrfs_remove_free_space_cache(cache);
  192. /* Now with the extent entry offset into the bitmap */
  193. ret = test_add_free_space_entry(cache, SZ_4M, SZ_4M, 1);
  194. if (ret) {
  195. test_err("couldn't add space to the bitmap %d", ret);
  196. return ret;
  197. }
  198. ret = test_add_free_space_entry(cache, SZ_2M, SZ_2M, 0);
  199. if (ret) {
  200. test_err("couldn't add extent to the cache %d", ret);
  201. return ret;
  202. }
  203. ret = btrfs_remove_free_space(cache, 3 * SZ_1M, SZ_4M);
  204. if (ret) {
  205. test_err("problem removing overlapping space %d", ret);
  206. return ret;
  207. }
  208. if (test_check_exists(cache, 3 * SZ_1M, SZ_4M)) {
  209. test_err("left something behind when removing space");
  210. return -1;
  211. }
  212. /*
  213. * This has blown up in the past, the extent entry starts before the
  214. * bitmap entry, but we're trying to remove an offset that falls
  215. * completely within the bitmap range and is in both the extent entry
  216. * and the bitmap entry, looks like this
  217. *
  218. * [ extent ]
  219. * [ bitmap ]
  220. * [ del ]
  221. */
  222. btrfs_remove_free_space_cache(cache);
  223. ret = test_add_free_space_entry(cache, bitmap_offset + SZ_4M, SZ_4M, 1);
  224. if (ret) {
  225. test_err("couldn't add bitmap %d", ret);
  226. return ret;
  227. }
  228. ret = test_add_free_space_entry(cache, bitmap_offset - SZ_1M,
  229. 5 * SZ_1M, 0);
  230. if (ret) {
  231. test_err("couldn't add extent entry %d", ret);
  232. return ret;
  233. }
  234. ret = btrfs_remove_free_space(cache, bitmap_offset + SZ_1M, 5 * SZ_1M);
  235. if (ret) {
  236. test_err("failed to free our space %d", ret);
  237. return ret;
  238. }
  239. if (test_check_exists(cache, bitmap_offset + SZ_1M, 5 * SZ_1M)) {
  240. test_err("left stuff over");
  241. return -1;
  242. }
  243. btrfs_remove_free_space_cache(cache);
  244. /*
  245. * This blew up before, we have part of the free space in a bitmap and
  246. * then the entirety of the rest of the space in an extent. This used
  247. * to return -EAGAIN back from btrfs_remove_extent, make sure this
  248. * doesn't happen.
  249. */
  250. ret = test_add_free_space_entry(cache, SZ_1M, SZ_2M, 1);
  251. if (ret) {
  252. test_err("couldn't add bitmap entry %d", ret);
  253. return ret;
  254. }
  255. ret = test_add_free_space_entry(cache, 3 * SZ_1M, SZ_1M, 0);
  256. if (ret) {
  257. test_err("couldn't add extent entry %d", ret);
  258. return ret;
  259. }
  260. ret = btrfs_remove_free_space(cache, SZ_1M, 3 * SZ_1M);
  261. if (ret) {
  262. test_err("error removing bitmap and extent overlapping %d", ret);
  263. return ret;
  264. }
  265. btrfs_remove_free_space_cache(cache);
  266. return 0;
  267. }
  268. /* Used by test_steal_space_from_bitmap_to_extent(). */
  269. static bool test_use_bitmap(struct btrfs_free_space_ctl *ctl,
  270. struct btrfs_free_space *info)
  271. {
  272. return ctl->free_extents > 0;
  273. }
  274. /* Used by test_steal_space_from_bitmap_to_extent(). */
  275. static int
  276. check_num_extents_and_bitmaps(const struct btrfs_block_group *cache,
  277. const int num_extents,
  278. const int num_bitmaps)
  279. {
  280. if (cache->free_space_ctl->free_extents != num_extents) {
  281. test_err(
  282. "incorrect # of extent entries in the cache: %d, expected %d",
  283. cache->free_space_ctl->free_extents, num_extents);
  284. return -EINVAL;
  285. }
  286. if (cache->free_space_ctl->total_bitmaps != num_bitmaps) {
  287. test_err(
  288. "incorrect # of extent entries in the cache: %d, expected %d",
  289. cache->free_space_ctl->total_bitmaps, num_bitmaps);
  290. return -EINVAL;
  291. }
  292. return 0;
  293. }
  294. /* Used by test_steal_space_from_bitmap_to_extent(). */
  295. static int check_cache_empty(struct btrfs_block_group *cache)
  296. {
  297. u64 offset;
  298. u64 max_extent_size;
  299. /*
  300. * Now lets confirm that there's absolutely no free space left to
  301. * allocate.
  302. */
  303. if (cache->free_space_ctl->free_space != 0) {
  304. test_err("cache free space is not 0");
  305. return -EINVAL;
  306. }
  307. /* And any allocation request, no matter how small, should fail now. */
  308. offset = btrfs_find_space_for_alloc(cache, 0, 4096, 0,
  309. &max_extent_size);
  310. if (offset != 0) {
  311. test_err("space allocation did not fail, returned offset: %llu",
  312. offset);
  313. return -EINVAL;
  314. }
  315. /* And no extent nor bitmap entries in the cache anymore. */
  316. return check_num_extents_and_bitmaps(cache, 0, 0);
  317. }
  318. /*
  319. * Before we were able to steal free space from a bitmap entry to an extent
  320. * entry, we could end up with 2 entries representing a contiguous free space.
  321. * One would be an extent entry and the other a bitmap entry. Since in order
  322. * to allocate space to a caller we use only 1 entry, we couldn't return that
  323. * whole range to the caller if it was requested. This forced the caller to
  324. * either assume ENOSPC or perform several smaller space allocations, which
  325. * wasn't optimal as they could be spread all over the block group while under
  326. * concurrency (extra overhead and fragmentation).
  327. *
  328. * This stealing approach is beneficial, since we always prefer to allocate
  329. * from extent entries, both for clustered and non-clustered allocation
  330. * requests.
  331. */
  332. static int
  333. test_steal_space_from_bitmap_to_extent(struct btrfs_block_group *cache,
  334. u32 sectorsize)
  335. {
  336. int ret;
  337. u64 offset;
  338. u64 max_extent_size;
  339. const struct btrfs_free_space_op test_free_space_ops = {
  340. .use_bitmap = test_use_bitmap,
  341. };
  342. const struct btrfs_free_space_op *orig_free_space_ops;
  343. test_msg("running space stealing from bitmap to extent tests");
  344. /*
  345. * For this test, we want to ensure we end up with an extent entry
  346. * immediately adjacent to a bitmap entry, where the bitmap starts
  347. * at an offset where the extent entry ends. We keep adding and
  348. * removing free space to reach into this state, but to get there
  349. * we need to reach a point where marking new free space doesn't
  350. * result in adding new extent entries or merging the new space
  351. * with existing extent entries - the space ends up being marked
  352. * in an existing bitmap that covers the new free space range.
  353. *
  354. * To get there, we need to reach the threshold defined set at
  355. * cache->free_space_ctl->extents_thresh, which currently is
  356. * 256 extents on a x86_64 system at least, and a few other
  357. * conditions (check free_space_cache.c). Instead of making the
  358. * test much longer and complicated, use a "use_bitmap" operation
  359. * that forces use of bitmaps as soon as we have at least 1
  360. * extent entry.
  361. */
  362. orig_free_space_ops = cache->free_space_ctl->op;
  363. cache->free_space_ctl->op = &test_free_space_ops;
  364. /*
  365. * Extent entry covering free space range [128Mb - 256Kb, 128Mb - 128Kb[
  366. */
  367. ret = test_add_free_space_entry(cache, SZ_128M - SZ_256K, SZ_128K, 0);
  368. if (ret) {
  369. test_err("couldn't add extent entry %d", ret);
  370. return ret;
  371. }
  372. /* Bitmap entry covering free space range [128Mb + 512Kb, 256Mb[ */
  373. ret = test_add_free_space_entry(cache, SZ_128M + SZ_512K,
  374. SZ_128M - SZ_512K, 1);
  375. if (ret) {
  376. test_err("couldn't add bitmap entry %d", ret);
  377. return ret;
  378. }
  379. ret = check_num_extents_and_bitmaps(cache, 2, 1);
  380. if (ret)
  381. return ret;
  382. /*
  383. * Now make only the first 256Kb of the bitmap marked as free, so that
  384. * we end up with only the following ranges marked as free space:
  385. *
  386. * [128Mb - 256Kb, 128Mb - 128Kb[
  387. * [128Mb + 512Kb, 128Mb + 768Kb[
  388. */
  389. ret = btrfs_remove_free_space(cache,
  390. SZ_128M + 768 * SZ_1K,
  391. SZ_128M - 768 * SZ_1K);
  392. if (ret) {
  393. test_err("failed to free part of bitmap space %d", ret);
  394. return ret;
  395. }
  396. /* Confirm that only those 2 ranges are marked as free. */
  397. if (!test_check_exists(cache, SZ_128M - SZ_256K, SZ_128K)) {
  398. test_err("free space range missing");
  399. return -ENOENT;
  400. }
  401. if (!test_check_exists(cache, SZ_128M + SZ_512K, SZ_256K)) {
  402. test_err("free space range missing");
  403. return -ENOENT;
  404. }
  405. /*
  406. * Confirm that the bitmap range [128Mb + 768Kb, 256Mb[ isn't marked
  407. * as free anymore.
  408. */
  409. if (test_check_exists(cache, SZ_128M + 768 * SZ_1K,
  410. SZ_128M - 768 * SZ_1K)) {
  411. test_err("bitmap region not removed from space cache");
  412. return -EINVAL;
  413. }
  414. /*
  415. * Confirm that the region [128Mb + 256Kb, 128Mb + 512Kb[, which is
  416. * covered by the bitmap, isn't marked as free.
  417. */
  418. if (test_check_exists(cache, SZ_128M + SZ_256K, SZ_256K)) {
  419. test_err("invalid bitmap region marked as free");
  420. return -EINVAL;
  421. }
  422. /*
  423. * Confirm that the region [128Mb, 128Mb + 256Kb[, which is covered
  424. * by the bitmap too, isn't marked as free either.
  425. */
  426. if (test_check_exists(cache, SZ_128M, SZ_256K)) {
  427. test_err("invalid bitmap region marked as free");
  428. return -EINVAL;
  429. }
  430. /*
  431. * Now lets mark the region [128Mb, 128Mb + 512Kb[ as free too. But,
  432. * lets make sure the free space cache marks it as free in the bitmap,
  433. * and doesn't insert a new extent entry to represent this region.
  434. */
  435. ret = btrfs_add_free_space(cache, SZ_128M, SZ_512K);
  436. if (ret) {
  437. test_err("error adding free space: %d", ret);
  438. return ret;
  439. }
  440. /* Confirm the region is marked as free. */
  441. if (!test_check_exists(cache, SZ_128M, SZ_512K)) {
  442. test_err("bitmap region not marked as free");
  443. return -ENOENT;
  444. }
  445. /*
  446. * Confirm that no new extent entries or bitmap entries were added to
  447. * the cache after adding that free space region.
  448. */
  449. ret = check_num_extents_and_bitmaps(cache, 2, 1);
  450. if (ret)
  451. return ret;
  452. /*
  453. * Now lets add a small free space region to the right of the previous
  454. * one, which is not contiguous with it and is part of the bitmap too.
  455. * The goal is to test that the bitmap entry space stealing doesn't
  456. * steal this space region.
  457. */
  458. ret = btrfs_add_free_space(cache, SZ_128M + SZ_16M, sectorsize);
  459. if (ret) {
  460. test_err("error adding free space: %d", ret);
  461. return ret;
  462. }
  463. /*
  464. * Confirm that no new extent entries or bitmap entries were added to
  465. * the cache after adding that free space region.
  466. */
  467. ret = check_num_extents_and_bitmaps(cache, 2, 1);
  468. if (ret)
  469. return ret;
  470. /*
  471. * Now mark the region [128Mb - 128Kb, 128Mb[ as free too. This will
  472. * expand the range covered by the existing extent entry that represents
  473. * the free space [128Mb - 256Kb, 128Mb - 128Kb[.
  474. */
  475. ret = btrfs_add_free_space(cache, SZ_128M - SZ_128K, SZ_128K);
  476. if (ret) {
  477. test_err("error adding free space: %d", ret);
  478. return ret;
  479. }
  480. /* Confirm the region is marked as free. */
  481. if (!test_check_exists(cache, SZ_128M - SZ_128K, SZ_128K)) {
  482. test_err("extent region not marked as free");
  483. return -ENOENT;
  484. }
  485. /*
  486. * Confirm that our extent entry didn't stole all free space from the
  487. * bitmap, because of the small 4Kb free space region.
  488. */
  489. ret = check_num_extents_and_bitmaps(cache, 2, 1);
  490. if (ret)
  491. return ret;
  492. /*
  493. * So now we have the range [128Mb - 256Kb, 128Mb + 768Kb[ as free
  494. * space. Without stealing bitmap free space into extent entry space,
  495. * we would have all this free space represented by 2 entries in the
  496. * cache:
  497. *
  498. * extent entry covering range: [128Mb - 256Kb, 128Mb[
  499. * bitmap entry covering range: [128Mb, 128Mb + 768Kb[
  500. *
  501. * Attempting to allocate the whole free space (1Mb) would fail, because
  502. * we can't allocate from multiple entries.
  503. * With the bitmap free space stealing, we get a single extent entry
  504. * that represents the 1Mb free space, and therefore we're able to
  505. * allocate the whole free space at once.
  506. */
  507. if (!test_check_exists(cache, SZ_128M - SZ_256K, SZ_1M)) {
  508. test_err("expected region not marked as free");
  509. return -ENOENT;
  510. }
  511. if (cache->free_space_ctl->free_space != (SZ_1M + sectorsize)) {
  512. test_err("cache free space is not 1Mb + %u", sectorsize);
  513. return -EINVAL;
  514. }
  515. offset = btrfs_find_space_for_alloc(cache,
  516. 0, SZ_1M, 0,
  517. &max_extent_size);
  518. if (offset != (SZ_128M - SZ_256K)) {
  519. test_err(
  520. "failed to allocate 1Mb from space cache, returned offset is: %llu",
  521. offset);
  522. return -EINVAL;
  523. }
  524. /*
  525. * All that remains is a sectorsize free space region in a bitmap.
  526. * Confirm.
  527. */
  528. ret = check_num_extents_and_bitmaps(cache, 1, 1);
  529. if (ret)
  530. return ret;
  531. if (cache->free_space_ctl->free_space != sectorsize) {
  532. test_err("cache free space is not %u", sectorsize);
  533. return -EINVAL;
  534. }
  535. offset = btrfs_find_space_for_alloc(cache,
  536. 0, sectorsize, 0,
  537. &max_extent_size);
  538. if (offset != (SZ_128M + SZ_16M)) {
  539. test_err("failed to allocate %u, returned offset : %llu",
  540. sectorsize, offset);
  541. return -EINVAL;
  542. }
  543. ret = check_cache_empty(cache);
  544. if (ret)
  545. return ret;
  546. btrfs_remove_free_space_cache(cache);
  547. /*
  548. * Now test a similar scenario, but where our extent entry is located
  549. * to the right of the bitmap entry, so that we can check that stealing
  550. * space from a bitmap to the front of an extent entry works.
  551. */
  552. /*
  553. * Extent entry covering free space range [128Mb + 128Kb, 128Mb + 256Kb[
  554. */
  555. ret = test_add_free_space_entry(cache, SZ_128M + SZ_128K, SZ_128K, 0);
  556. if (ret) {
  557. test_err("couldn't add extent entry %d", ret);
  558. return ret;
  559. }
  560. /* Bitmap entry covering free space range [0, 128Mb - 512Kb[ */
  561. ret = test_add_free_space_entry(cache, 0, SZ_128M - SZ_512K, 1);
  562. if (ret) {
  563. test_err("couldn't add bitmap entry %d", ret);
  564. return ret;
  565. }
  566. ret = check_num_extents_and_bitmaps(cache, 2, 1);
  567. if (ret)
  568. return ret;
  569. /*
  570. * Now make only the last 256Kb of the bitmap marked as free, so that
  571. * we end up with only the following ranges marked as free space:
  572. *
  573. * [128Mb + 128b, 128Mb + 256Kb[
  574. * [128Mb - 768Kb, 128Mb - 512Kb[
  575. */
  576. ret = btrfs_remove_free_space(cache, 0, SZ_128M - 768 * SZ_1K);
  577. if (ret) {
  578. test_err("failed to free part of bitmap space %d", ret);
  579. return ret;
  580. }
  581. /* Confirm that only those 2 ranges are marked as free. */
  582. if (!test_check_exists(cache, SZ_128M + SZ_128K, SZ_128K)) {
  583. test_err("free space range missing");
  584. return -ENOENT;
  585. }
  586. if (!test_check_exists(cache, SZ_128M - 768 * SZ_1K, SZ_256K)) {
  587. test_err("free space range missing");
  588. return -ENOENT;
  589. }
  590. /*
  591. * Confirm that the bitmap range [0, 128Mb - 768Kb[ isn't marked
  592. * as free anymore.
  593. */
  594. if (test_check_exists(cache, 0, SZ_128M - 768 * SZ_1K)) {
  595. test_err("bitmap region not removed from space cache");
  596. return -EINVAL;
  597. }
  598. /*
  599. * Confirm that the region [128Mb - 512Kb, 128Mb[, which is
  600. * covered by the bitmap, isn't marked as free.
  601. */
  602. if (test_check_exists(cache, SZ_128M - SZ_512K, SZ_512K)) {
  603. test_err("invalid bitmap region marked as free");
  604. return -EINVAL;
  605. }
  606. /*
  607. * Now lets mark the region [128Mb - 512Kb, 128Mb[ as free too. But,
  608. * lets make sure the free space cache marks it as free in the bitmap,
  609. * and doesn't insert a new extent entry to represent this region.
  610. */
  611. ret = btrfs_add_free_space(cache, SZ_128M - SZ_512K, SZ_512K);
  612. if (ret) {
  613. test_err("error adding free space: %d", ret);
  614. return ret;
  615. }
  616. /* Confirm the region is marked as free. */
  617. if (!test_check_exists(cache, SZ_128M - SZ_512K, SZ_512K)) {
  618. test_err("bitmap region not marked as free");
  619. return -ENOENT;
  620. }
  621. /*
  622. * Confirm that no new extent entries or bitmap entries were added to
  623. * the cache after adding that free space region.
  624. */
  625. ret = check_num_extents_and_bitmaps(cache, 2, 1);
  626. if (ret)
  627. return ret;
  628. /*
  629. * Now lets add a small free space region to the left of the previous
  630. * one, which is not contiguous with it and is part of the bitmap too.
  631. * The goal is to test that the bitmap entry space stealing doesn't
  632. * steal this space region.
  633. */
  634. ret = btrfs_add_free_space(cache, SZ_32M, 2 * sectorsize);
  635. if (ret) {
  636. test_err("error adding free space: %d", ret);
  637. return ret;
  638. }
  639. /*
  640. * Now mark the region [128Mb, 128Mb + 128Kb[ as free too. This will
  641. * expand the range covered by the existing extent entry that represents
  642. * the free space [128Mb + 128Kb, 128Mb + 256Kb[.
  643. */
  644. ret = btrfs_add_free_space(cache, SZ_128M, SZ_128K);
  645. if (ret) {
  646. test_err("error adding free space: %d", ret);
  647. return ret;
  648. }
  649. /* Confirm the region is marked as free. */
  650. if (!test_check_exists(cache, SZ_128M, SZ_128K)) {
  651. test_err("extent region not marked as free");
  652. return -ENOENT;
  653. }
  654. /*
  655. * Confirm that our extent entry didn't stole all free space from the
  656. * bitmap, because of the small 2 * sectorsize free space region.
  657. */
  658. ret = check_num_extents_and_bitmaps(cache, 2, 1);
  659. if (ret)
  660. return ret;
  661. /*
  662. * So now we have the range [128Mb - 768Kb, 128Mb + 256Kb[ as free
  663. * space. Without stealing bitmap free space into extent entry space,
  664. * we would have all this free space represented by 2 entries in the
  665. * cache:
  666. *
  667. * extent entry covering range: [128Mb, 128Mb + 256Kb[
  668. * bitmap entry covering range: [128Mb - 768Kb, 128Mb[
  669. *
  670. * Attempting to allocate the whole free space (1Mb) would fail, because
  671. * we can't allocate from multiple entries.
  672. * With the bitmap free space stealing, we get a single extent entry
  673. * that represents the 1Mb free space, and therefore we're able to
  674. * allocate the whole free space at once.
  675. */
  676. if (!test_check_exists(cache, SZ_128M - 768 * SZ_1K, SZ_1M)) {
  677. test_err("expected region not marked as free");
  678. return -ENOENT;
  679. }
  680. if (cache->free_space_ctl->free_space != (SZ_1M + 2 * sectorsize)) {
  681. test_err("cache free space is not 1Mb + %u", 2 * sectorsize);
  682. return -EINVAL;
  683. }
  684. offset = btrfs_find_space_for_alloc(cache, 0, SZ_1M, 0,
  685. &max_extent_size);
  686. if (offset != (SZ_128M - 768 * SZ_1K)) {
  687. test_err(
  688. "failed to allocate 1Mb from space cache, returned offset is: %llu",
  689. offset);
  690. return -EINVAL;
  691. }
  692. /*
  693. * All that remains is 2 * sectorsize free space region
  694. * in a bitmap. Confirm.
  695. */
  696. ret = check_num_extents_and_bitmaps(cache, 1, 1);
  697. if (ret)
  698. return ret;
  699. if (cache->free_space_ctl->free_space != 2 * sectorsize) {
  700. test_err("cache free space is not %u", 2 * sectorsize);
  701. return -EINVAL;
  702. }
  703. offset = btrfs_find_space_for_alloc(cache,
  704. 0, 2 * sectorsize, 0,
  705. &max_extent_size);
  706. if (offset != SZ_32M) {
  707. test_err("failed to allocate %u, offset: %llu",
  708. 2 * sectorsize, 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);
  716. return 0;
  717. }
  718. static bool bytes_index_use_bitmap(struct btrfs_free_space_ctl *ctl,
  719. struct btrfs_free_space *info)
  720. {
  721. return true;
  722. }
  723. static int test_bytes_index(struct btrfs_block_group *cache, u32 sectorsize)
  724. {
  725. const struct btrfs_free_space_op test_free_space_ops = {
  726. .use_bitmap = bytes_index_use_bitmap,
  727. };
  728. const struct btrfs_free_space_op *orig_free_space_ops;
  729. struct btrfs_free_space_ctl *ctl = cache->free_space_ctl;
  730. struct btrfs_free_space *entry;
  731. struct rb_node *node;
  732. u64 offset, max_extent_size, bytes;
  733. int ret, i;
  734. test_msg("running bytes index tests");
  735. /* First just validate that it does everything in order. */
  736. offset = 0;
  737. for (i = 0; i < 10; i++) {
  738. bytes = (i + 1) * SZ_1M;
  739. ret = test_add_free_space_entry(cache, offset, bytes, 0);
  740. if (ret) {
  741. test_err("couldn't add extent entry %d\n", ret);
  742. return ret;
  743. }
  744. offset += bytes + sectorsize;
  745. }
  746. for (node = rb_first_cached(&ctl->free_space_bytes), i = 9; node;
  747. node = rb_next(node), i--) {
  748. entry = rb_entry(node, struct btrfs_free_space, bytes_index);
  749. bytes = (i + 1) * SZ_1M;
  750. if (entry->bytes != bytes) {
  751. test_err("invalid bytes index order, found %llu expected %llu",
  752. entry->bytes, bytes);
  753. return -EINVAL;
  754. }
  755. }
  756. /* Now validate bitmaps do the correct thing. */
  757. btrfs_remove_free_space_cache(cache);
  758. for (i = 0; i < 2; i++) {
  759. offset = i * BITS_PER_BITMAP * sectorsize;
  760. bytes = (i + 1) * SZ_1M;
  761. ret = test_add_free_space_entry(cache, offset, bytes, 1);
  762. if (ret) {
  763. test_err("couldn't add bitmap entry");
  764. return ret;
  765. }
  766. }
  767. for (node = rb_first_cached(&ctl->free_space_bytes), i = 1; node;
  768. node = rb_next(node), i--) {
  769. entry = rb_entry(node, struct btrfs_free_space, bytes_index);
  770. bytes = (i + 1) * SZ_1M;
  771. if (entry->bytes != bytes) {
  772. test_err("invalid bytes index order, found %llu expected %llu",
  773. entry->bytes, bytes);
  774. return -EINVAL;
  775. }
  776. }
  777. /* Now validate bitmaps with different ->max_extent_size. */
  778. btrfs_remove_free_space_cache(cache);
  779. orig_free_space_ops = cache->free_space_ctl->op;
  780. cache->free_space_ctl->op = &test_free_space_ops;
  781. ret = test_add_free_space_entry(cache, 0, sectorsize, 1);
  782. if (ret) {
  783. test_err("couldn't add bitmap entry");
  784. return ret;
  785. }
  786. offset = BITS_PER_BITMAP * sectorsize;
  787. ret = test_add_free_space_entry(cache, offset, sectorsize, 1);
  788. if (ret) {
  789. test_err("couldn't add bitmap_entry");
  790. return ret;
  791. }
  792. /*
  793. * Now set a bunch of sectorsize extents in the first entry so it's
  794. * ->bytes is large.
  795. */
  796. for (i = 2; i < 20; i += 2) {
  797. offset = sectorsize * i;
  798. ret = btrfs_add_free_space(cache, offset, sectorsize);
  799. if (ret) {
  800. test_err("error populating sparse bitmap %d", ret);
  801. return ret;
  802. }
  803. }
  804. /*
  805. * Now set a contiguous extent in the second bitmap so its
  806. * ->max_extent_size is larger than the first bitmaps.
  807. */
  808. offset = (BITS_PER_BITMAP * sectorsize) + sectorsize;
  809. ret = btrfs_add_free_space(cache, offset, sectorsize);
  810. if (ret) {
  811. test_err("error adding contiguous extent %d", ret);
  812. return ret;
  813. }
  814. /*
  815. * Since we don't set ->max_extent_size unless we search everything
  816. * should be indexed on bytes.
  817. */
  818. entry = rb_entry(rb_first_cached(&ctl->free_space_bytes),
  819. struct btrfs_free_space, bytes_index);
  820. if (entry->bytes != (10 * sectorsize)) {
  821. test_err("error, wrong entry in the first slot in bytes_index");
  822. return -EINVAL;
  823. }
  824. max_extent_size = 0;
  825. offset = btrfs_find_space_for_alloc(cache, cache->start, sectorsize * 3,
  826. 0, &max_extent_size);
  827. if (offset != 0) {
  828. test_err("found space to alloc even though we don't have enough space");
  829. return -EINVAL;
  830. }
  831. if (max_extent_size != (2 * sectorsize)) {
  832. test_err("got the wrong max_extent size %llu expected %llu",
  833. max_extent_size, (unsigned long long)(2 * sectorsize));
  834. return -EINVAL;
  835. }
  836. /*
  837. * The search should have re-arranged the bytes index to use the
  838. * ->max_extent_size, validate it's now what we expect it to be.
  839. */
  840. entry = rb_entry(rb_first_cached(&ctl->free_space_bytes),
  841. struct btrfs_free_space, bytes_index);
  842. if (entry->bytes != (2 * sectorsize)) {
  843. test_err("error, the bytes index wasn't recalculated properly");
  844. return -EINVAL;
  845. }
  846. /* Add another sectorsize to re-arrange the tree back to ->bytes. */
  847. offset = (BITS_PER_BITMAP * sectorsize) - sectorsize;
  848. ret = btrfs_add_free_space(cache, offset, sectorsize);
  849. if (ret) {
  850. test_err("error adding extent to the sparse entry %d", ret);
  851. return ret;
  852. }
  853. entry = rb_entry(rb_first_cached(&ctl->free_space_bytes),
  854. struct btrfs_free_space, bytes_index);
  855. if (entry->bytes != (11 * sectorsize)) {
  856. test_err("error, wrong entry in the first slot in bytes_index");
  857. return -EINVAL;
  858. }
  859. /*
  860. * Now make sure we find our correct entry after searching that will
  861. * result in a re-arranging of the tree.
  862. */
  863. max_extent_size = 0;
  864. offset = btrfs_find_space_for_alloc(cache, cache->start, sectorsize * 2,
  865. 0, &max_extent_size);
  866. if (offset != (BITS_PER_BITMAP * sectorsize)) {
  867. test_err("error, found %llu instead of %llu for our alloc",
  868. offset,
  869. (unsigned long long)(BITS_PER_BITMAP * sectorsize));
  870. return -EINVAL;
  871. }
  872. cache->free_space_ctl->op = orig_free_space_ops;
  873. btrfs_remove_free_space_cache(cache);
  874. return 0;
  875. }
  876. int btrfs_test_free_space_cache(u32 sectorsize, u32 nodesize)
  877. {
  878. struct btrfs_fs_info *fs_info;
  879. struct btrfs_block_group *cache;
  880. struct btrfs_root *root = NULL;
  881. int ret = -ENOMEM;
  882. test_msg("running btrfs free space cache tests");
  883. fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
  884. if (!fs_info) {
  885. test_std_err(TEST_ALLOC_FS_INFO);
  886. return -ENOMEM;
  887. }
  888. /*
  889. * For ppc64 (with 64k page size), bytes per bitmap might be
  890. * larger than 1G. To make bitmap test available in ppc64,
  891. * alloc dummy block group whose size cross bitmaps.
  892. */
  893. cache = btrfs_alloc_dummy_block_group(fs_info,
  894. BITS_PER_BITMAP * sectorsize + PAGE_SIZE);
  895. if (!cache) {
  896. test_std_err(TEST_ALLOC_BLOCK_GROUP);
  897. btrfs_free_dummy_fs_info(fs_info);
  898. return 0;
  899. }
  900. root = btrfs_alloc_dummy_root(fs_info);
  901. if (IS_ERR(root)) {
  902. test_std_err(TEST_ALLOC_ROOT);
  903. ret = PTR_ERR(root);
  904. goto out;
  905. }
  906. root->root_key.objectid = BTRFS_EXTENT_TREE_OBJECTID;
  907. root->root_key.type = BTRFS_ROOT_ITEM_KEY;
  908. root->root_key.offset = 0;
  909. btrfs_global_root_insert(root);
  910. ret = test_extents(cache);
  911. if (ret)
  912. goto out;
  913. ret = test_bitmaps(cache, sectorsize);
  914. if (ret)
  915. goto out;
  916. ret = test_bitmaps_and_extents(cache, sectorsize);
  917. if (ret)
  918. goto out;
  919. ret = test_steal_space_from_bitmap_to_extent(cache, sectorsize);
  920. if (ret)
  921. goto out;
  922. ret = test_bytes_index(cache, sectorsize);
  923. out:
  924. btrfs_free_dummy_block_group(cache);
  925. btrfs_free_dummy_root(root);
  926. btrfs_free_dummy_fs_info(fs_info);
  927. return ret;
  928. }