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