This source file includes following definitions.
- test_extents
- test_bitmaps
- test_bitmaps_and_extents
- test_use_bitmap
- check_num_extents_and_bitmaps
- check_cache_empty
- test_steal_space_from_bitmap_to_extent
- btrfs_test_free_space_cache
1
2
3
4
5
6 #include <linux/slab.h>
7 #include "btrfs-tests.h"
8 #include "../ctree.h"
9 #include "../disk-io.h"
10 #include "../free-space-cache.h"
11 #include "../block-group.h"
12
13 #define BITS_PER_BITMAP (PAGE_SIZE * 8UL)
14
15
16
17
18
19
20 static int test_extents(struct btrfs_block_group_cache *cache)
21 {
22 int ret = 0;
23
24 test_msg("running extent only tests");
25
26
27 ret = btrfs_add_free_space(cache, 0, SZ_4M);
28 if (ret) {
29 test_err("error adding initial extents %d", ret);
30 return ret;
31 }
32
33 ret = btrfs_remove_free_space(cache, 0, SZ_4M);
34 if (ret) {
35 test_err("error removing extent %d", ret);
36 return ret;
37 }
38
39 if (test_check_exists(cache, 0, SZ_4M)) {
40 test_err("full remove left some lingering space");
41 return -1;
42 }
43
44
45 ret = btrfs_add_free_space(cache, 0, SZ_4M);
46 if (ret) {
47 test_err("error adding half extent %d", ret);
48 return ret;
49 }
50
51 ret = btrfs_remove_free_space(cache, 3 * SZ_1M, SZ_1M);
52 if (ret) {
53 test_err("error removing tail end %d", ret);
54 return ret;
55 }
56
57 ret = btrfs_remove_free_space(cache, 0, SZ_1M);
58 if (ret) {
59 test_err("error removing front end %d", ret);
60 return ret;
61 }
62
63 ret = btrfs_remove_free_space(cache, SZ_2M, 4096);
64 if (ret) {
65 test_err("error removing middle piece %d", ret);
66 return ret;
67 }
68
69 if (test_check_exists(cache, 0, SZ_1M)) {
70 test_err("still have space at the front");
71 return -1;
72 }
73
74 if (test_check_exists(cache, SZ_2M, 4096)) {
75 test_err("still have space in the middle");
76 return -1;
77 }
78
79 if (test_check_exists(cache, 3 * SZ_1M, SZ_1M)) {
80 test_err("still have space at the end");
81 return -1;
82 }
83
84
85 __btrfs_remove_free_space_cache(cache->free_space_ctl);
86
87 return 0;
88 }
89
90 static int test_bitmaps(struct btrfs_block_group_cache *cache,
91 u32 sectorsize)
92 {
93 u64 next_bitmap_offset;
94 int ret;
95
96 test_msg("running bitmap only tests");
97
98 ret = test_add_free_space_entry(cache, 0, SZ_4M, 1);
99 if (ret) {
100 test_err("couldn't create a bitmap entry %d", ret);
101 return ret;
102 }
103
104 ret = btrfs_remove_free_space(cache, 0, SZ_4M);
105 if (ret) {
106 test_err("error removing bitmap full range %d", ret);
107 return ret;
108 }
109
110 if (test_check_exists(cache, 0, SZ_4M)) {
111 test_err("left some space in bitmap");
112 return -1;
113 }
114
115 ret = test_add_free_space_entry(cache, 0, SZ_4M, 1);
116 if (ret) {
117 test_err("couldn't add to our bitmap entry %d", ret);
118 return ret;
119 }
120
121 ret = btrfs_remove_free_space(cache, SZ_1M, SZ_2M);
122 if (ret) {
123 test_err("couldn't remove middle chunk %d", ret);
124 return ret;
125 }
126
127
128
129
130
131 next_bitmap_offset = (u64)(BITS_PER_BITMAP * sectorsize);
132
133
134 ret = test_add_free_space_entry(cache, next_bitmap_offset - SZ_2M,
135 SZ_4M, 1);
136 if (ret) {
137 test_err("couldn't add space that straddles two bitmaps %d",
138 ret);
139 return ret;
140 }
141
142 ret = btrfs_remove_free_space(cache, next_bitmap_offset - SZ_1M, SZ_2M);
143 if (ret) {
144 test_err("couldn't remove overlapping space %d", ret);
145 return ret;
146 }
147
148 if (test_check_exists(cache, next_bitmap_offset - SZ_1M, SZ_2M)) {
149 test_err("left some space when removing overlapping");
150 return -1;
151 }
152
153 __btrfs_remove_free_space_cache(cache->free_space_ctl);
154
155 return 0;
156 }
157
158
159 static int test_bitmaps_and_extents(struct btrfs_block_group_cache *cache,
160 u32 sectorsize)
161 {
162 u64 bitmap_offset = (u64)(BITS_PER_BITMAP * sectorsize);
163 int ret;
164
165 test_msg("running bitmap and extent tests");
166
167
168
169
170
171
172 ret = test_add_free_space_entry(cache, SZ_4M, SZ_1M, 1);
173 if (ret) {
174 test_err("couldn't create bitmap entry %d", ret);
175 return ret;
176 }
177
178 ret = test_add_free_space_entry(cache, 0, SZ_1M, 0);
179 if (ret) {
180 test_err("couldn't add extent entry %d", ret);
181 return ret;
182 }
183
184 ret = btrfs_remove_free_space(cache, 0, SZ_1M);
185 if (ret) {
186 test_err("couldn't remove extent entry %d", ret);
187 return ret;
188 }
189
190 if (test_check_exists(cache, 0, SZ_1M)) {
191 test_err("left remnants after our remove");
192 return -1;
193 }
194
195
196 ret = test_add_free_space_entry(cache, 0, SZ_1M, 0);
197 if (ret) {
198 test_err("couldn't re-add extent entry %d", ret);
199 return ret;
200 }
201
202 ret = btrfs_remove_free_space(cache, SZ_4M, SZ_1M);
203 if (ret) {
204 test_err("couldn't remove from bitmap %d", ret);
205 return ret;
206 }
207
208 if (test_check_exists(cache, SZ_4M, SZ_1M)) {
209 test_err("left remnants in the bitmap");
210 return -1;
211 }
212
213
214
215
216
217 ret = test_add_free_space_entry(cache, SZ_1M, SZ_4M, 1);
218 if (ret) {
219 test_err("couldn't add to a bitmap %d", ret);
220 return ret;
221 }
222
223 ret = btrfs_remove_free_space(cache, SZ_512K, 3 * SZ_1M);
224 if (ret) {
225 test_err("couldn't remove overlapping space %d", ret);
226 return ret;
227 }
228
229 if (test_check_exists(cache, SZ_512K, 3 * SZ_1M)) {
230 test_err("left over pieces after removing overlapping");
231 return -1;
232 }
233
234 __btrfs_remove_free_space_cache(cache->free_space_ctl);
235
236
237 ret = test_add_free_space_entry(cache, SZ_4M, SZ_4M, 1);
238 if (ret) {
239 test_err("couldn't add space to the bitmap %d", ret);
240 return ret;
241 }
242
243 ret = test_add_free_space_entry(cache, SZ_2M, SZ_2M, 0);
244 if (ret) {
245 test_err("couldn't add extent to the cache %d", ret);
246 return ret;
247 }
248
249 ret = btrfs_remove_free_space(cache, 3 * SZ_1M, SZ_4M);
250 if (ret) {
251 test_err("problem removing overlapping space %d", ret);
252 return ret;
253 }
254
255 if (test_check_exists(cache, 3 * SZ_1M, SZ_4M)) {
256 test_err("left something behind when removing space");
257 return -1;
258 }
259
260
261
262
263
264
265
266
267
268
269
270 __btrfs_remove_free_space_cache(cache->free_space_ctl);
271 ret = test_add_free_space_entry(cache, bitmap_offset + SZ_4M, SZ_4M, 1);
272 if (ret) {
273 test_err("couldn't add bitmap %d", ret);
274 return ret;
275 }
276
277 ret = test_add_free_space_entry(cache, bitmap_offset - SZ_1M,
278 5 * SZ_1M, 0);
279 if (ret) {
280 test_err("couldn't add extent entry %d", ret);
281 return ret;
282 }
283
284 ret = btrfs_remove_free_space(cache, bitmap_offset + SZ_1M, 5 * SZ_1M);
285 if (ret) {
286 test_err("failed to free our space %d", ret);
287 return ret;
288 }
289
290 if (test_check_exists(cache, bitmap_offset + SZ_1M, 5 * SZ_1M)) {
291 test_err("left stuff over");
292 return -1;
293 }
294
295 __btrfs_remove_free_space_cache(cache->free_space_ctl);
296
297
298
299
300
301
302
303 ret = test_add_free_space_entry(cache, SZ_1M, SZ_2M, 1);
304 if (ret) {
305 test_err("couldn't add bitmap entry %d", ret);
306 return ret;
307 }
308
309 ret = test_add_free_space_entry(cache, 3 * SZ_1M, SZ_1M, 0);
310 if (ret) {
311 test_err("couldn't add extent entry %d", ret);
312 return ret;
313 }
314
315 ret = btrfs_remove_free_space(cache, SZ_1M, 3 * SZ_1M);
316 if (ret) {
317 test_err("error removing bitmap and extent overlapping %d", ret);
318 return ret;
319 }
320
321 __btrfs_remove_free_space_cache(cache->free_space_ctl);
322 return 0;
323 }
324
325
326 static bool test_use_bitmap(struct btrfs_free_space_ctl *ctl,
327 struct btrfs_free_space *info)
328 {
329 return ctl->free_extents > 0;
330 }
331
332
333 static int
334 check_num_extents_and_bitmaps(const struct btrfs_block_group_cache *cache,
335 const int num_extents,
336 const int num_bitmaps)
337 {
338 if (cache->free_space_ctl->free_extents != num_extents) {
339 test_err(
340 "incorrect # of extent entries in the cache: %d, expected %d",
341 cache->free_space_ctl->free_extents, num_extents);
342 return -EINVAL;
343 }
344 if (cache->free_space_ctl->total_bitmaps != num_bitmaps) {
345 test_err(
346 "incorrect # of extent entries in the cache: %d, expected %d",
347 cache->free_space_ctl->total_bitmaps, num_bitmaps);
348 return -EINVAL;
349 }
350 return 0;
351 }
352
353
354 static int check_cache_empty(struct btrfs_block_group_cache *cache)
355 {
356 u64 offset;
357 u64 max_extent_size;
358
359
360
361
362
363 if (cache->free_space_ctl->free_space != 0) {
364 test_err("cache free space is not 0");
365 return -EINVAL;
366 }
367
368
369 offset = btrfs_find_space_for_alloc(cache, 0, 4096, 0,
370 &max_extent_size);
371 if (offset != 0) {
372 test_err("space allocation did not fail, returned offset: %llu",
373 offset);
374 return -EINVAL;
375 }
376
377
378 return check_num_extents_and_bitmaps(cache, 0, 0);
379 }
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395 static int
396 test_steal_space_from_bitmap_to_extent(struct btrfs_block_group_cache *cache,
397 u32 sectorsize)
398 {
399 int ret;
400 u64 offset;
401 u64 max_extent_size;
402 const struct btrfs_free_space_op test_free_space_ops = {
403 .recalc_thresholds = cache->free_space_ctl->op->recalc_thresholds,
404 .use_bitmap = test_use_bitmap,
405 };
406 const struct btrfs_free_space_op *orig_free_space_ops;
407
408 test_msg("running space stealing from bitmap to extent tests");
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428 orig_free_space_ops = cache->free_space_ctl->op;
429 cache->free_space_ctl->op = &test_free_space_ops;
430
431
432
433
434 ret = test_add_free_space_entry(cache, SZ_128M - SZ_256K, SZ_128K, 0);
435 if (ret) {
436 test_err("couldn't add extent entry %d", ret);
437 return ret;
438 }
439
440
441 ret = test_add_free_space_entry(cache, SZ_128M + SZ_512K,
442 SZ_128M - SZ_512K, 1);
443 if (ret) {
444 test_err("couldn't add bitmap entry %d", ret);
445 return ret;
446 }
447
448 ret = check_num_extents_and_bitmaps(cache, 2, 1);
449 if (ret)
450 return ret;
451
452
453
454
455
456
457
458
459 ret = btrfs_remove_free_space(cache,
460 SZ_128M + 768 * SZ_1K,
461 SZ_128M - 768 * SZ_1K);
462 if (ret) {
463 test_err("failed to free part of bitmap space %d", ret);
464 return ret;
465 }
466
467
468 if (!test_check_exists(cache, SZ_128M - SZ_256K, SZ_128K)) {
469 test_err("free space range missing");
470 return -ENOENT;
471 }
472 if (!test_check_exists(cache, SZ_128M + SZ_512K, SZ_256K)) {
473 test_err("free space range missing");
474 return -ENOENT;
475 }
476
477
478
479
480
481 if (test_check_exists(cache, SZ_128M + 768 * SZ_1K,
482 SZ_128M - 768 * SZ_1K)) {
483 test_err("bitmap region not removed from space cache");
484 return -EINVAL;
485 }
486
487
488
489
490
491 if (test_check_exists(cache, SZ_128M + SZ_256K, SZ_256K)) {
492 test_err("invalid bitmap region marked as free");
493 return -EINVAL;
494 }
495
496
497
498
499
500 if (test_check_exists(cache, SZ_128M, SZ_256K)) {
501 test_err("invalid bitmap region marked as free");
502 return -EINVAL;
503 }
504
505
506
507
508
509
510 ret = btrfs_add_free_space(cache, SZ_128M, SZ_512K);
511 if (ret) {
512 test_err("error adding free space: %d", ret);
513 return ret;
514 }
515
516 if (!test_check_exists(cache, SZ_128M, SZ_512K)) {
517 test_err("bitmap region not marked as free");
518 return -ENOENT;
519 }
520
521
522
523
524
525 ret = check_num_extents_and_bitmaps(cache, 2, 1);
526 if (ret)
527 return ret;
528
529
530
531
532
533
534
535 ret = btrfs_add_free_space(cache, SZ_128M + SZ_16M, sectorsize);
536 if (ret) {
537 test_err("error adding free space: %d", ret);
538 return ret;
539 }
540
541
542
543
544
545 ret = check_num_extents_and_bitmaps(cache, 2, 1);
546 if (ret)
547 return ret;
548
549
550
551
552
553
554 ret = btrfs_add_free_space(cache, SZ_128M - SZ_128K, SZ_128K);
555 if (ret) {
556 test_err("error adding free space: %d", ret);
557 return ret;
558 }
559
560 if (!test_check_exists(cache, SZ_128M - SZ_128K, SZ_128K)) {
561 test_err("extent region not marked as free");
562 return -ENOENT;
563 }
564
565
566
567
568
569 ret = check_num_extents_and_bitmaps(cache, 2, 1);
570 if (ret)
571 return ret;
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588 if (!test_check_exists(cache, SZ_128M - SZ_256K, SZ_1M)) {
589 test_err("expected region not marked as free");
590 return -ENOENT;
591 }
592
593 if (cache->free_space_ctl->free_space != (SZ_1M + sectorsize)) {
594 test_err("cache free space is not 1Mb + %u", sectorsize);
595 return -EINVAL;
596 }
597
598 offset = btrfs_find_space_for_alloc(cache,
599 0, SZ_1M, 0,
600 &max_extent_size);
601 if (offset != (SZ_128M - SZ_256K)) {
602 test_err(
603 "failed to allocate 1Mb from space cache, returned offset is: %llu",
604 offset);
605 return -EINVAL;
606 }
607
608
609
610
611
612 ret = check_num_extents_and_bitmaps(cache, 1, 1);
613 if (ret)
614 return ret;
615
616 if (cache->free_space_ctl->free_space != sectorsize) {
617 test_err("cache free space is not %u", sectorsize);
618 return -EINVAL;
619 }
620
621 offset = btrfs_find_space_for_alloc(cache,
622 0, sectorsize, 0,
623 &max_extent_size);
624 if (offset != (SZ_128M + SZ_16M)) {
625 test_err("failed to allocate %u, returned offset : %llu",
626 sectorsize, offset);
627 return -EINVAL;
628 }
629
630 ret = check_cache_empty(cache);
631 if (ret)
632 return ret;
633
634 __btrfs_remove_free_space_cache(cache->free_space_ctl);
635
636
637
638
639
640
641
642
643
644
645 ret = test_add_free_space_entry(cache, SZ_128M + SZ_128K, SZ_128K, 0);
646 if (ret) {
647 test_err("couldn't add extent entry %d", ret);
648 return ret;
649 }
650
651
652 ret = test_add_free_space_entry(cache, 0, SZ_128M - SZ_512K, 1);
653 if (ret) {
654 test_err("couldn't add bitmap entry %d", ret);
655 return ret;
656 }
657
658 ret = check_num_extents_and_bitmaps(cache, 2, 1);
659 if (ret)
660 return ret;
661
662
663
664
665
666
667
668
669 ret = btrfs_remove_free_space(cache, 0, SZ_128M - 768 * SZ_1K);
670 if (ret) {
671 test_err("failed to free part of bitmap space %d", ret);
672 return ret;
673 }
674
675
676 if (!test_check_exists(cache, SZ_128M + SZ_128K, SZ_128K)) {
677 test_err("free space range missing");
678 return -ENOENT;
679 }
680 if (!test_check_exists(cache, SZ_128M - 768 * SZ_1K, SZ_256K)) {
681 test_err("free space range missing");
682 return -ENOENT;
683 }
684
685
686
687
688
689 if (test_check_exists(cache, 0, SZ_128M - 768 * SZ_1K)) {
690 test_err("bitmap region not removed from space cache");
691 return -EINVAL;
692 }
693
694
695
696
697
698 if (test_check_exists(cache, SZ_128M - SZ_512K, SZ_512K)) {
699 test_err("invalid bitmap region marked as free");
700 return -EINVAL;
701 }
702
703
704
705
706
707
708 ret = btrfs_add_free_space(cache, SZ_128M - SZ_512K, SZ_512K);
709 if (ret) {
710 test_err("error adding free space: %d", ret);
711 return ret;
712 }
713
714 if (!test_check_exists(cache, SZ_128M - SZ_512K, SZ_512K)) {
715 test_err("bitmap region not marked as free");
716 return -ENOENT;
717 }
718
719
720
721
722
723 ret = check_num_extents_and_bitmaps(cache, 2, 1);
724 if (ret)
725 return ret;
726
727
728
729
730
731
732
733 ret = btrfs_add_free_space(cache, SZ_32M, 2 * sectorsize);
734 if (ret) {
735 test_err("error adding free space: %d", ret);
736 return ret;
737 }
738
739
740
741
742
743
744 ret = btrfs_add_free_space(cache, SZ_128M, SZ_128K);
745 if (ret) {
746 test_err("error adding free space: %d", ret);
747 return ret;
748 }
749
750 if (!test_check_exists(cache, SZ_128M, SZ_128K)) {
751 test_err("extent region not marked as free");
752 return -ENOENT;
753 }
754
755
756
757
758
759 ret = check_num_extents_and_bitmaps(cache, 2, 1);
760 if (ret)
761 return ret;
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778 if (!test_check_exists(cache, SZ_128M - 768 * SZ_1K, SZ_1M)) {
779 test_err("expected region not marked as free");
780 return -ENOENT;
781 }
782
783 if (cache->free_space_ctl->free_space != (SZ_1M + 2 * sectorsize)) {
784 test_err("cache free space is not 1Mb + %u", 2 * sectorsize);
785 return -EINVAL;
786 }
787
788 offset = btrfs_find_space_for_alloc(cache, 0, SZ_1M, 0,
789 &max_extent_size);
790 if (offset != (SZ_128M - 768 * SZ_1K)) {
791 test_err(
792 "failed to allocate 1Mb from space cache, returned offset is: %llu",
793 offset);
794 return -EINVAL;
795 }
796
797
798
799
800
801 ret = check_num_extents_and_bitmaps(cache, 1, 1);
802 if (ret)
803 return ret;
804
805 if (cache->free_space_ctl->free_space != 2 * sectorsize) {
806 test_err("cache free space is not %u", 2 * sectorsize);
807 return -EINVAL;
808 }
809
810 offset = btrfs_find_space_for_alloc(cache,
811 0, 2 * sectorsize, 0,
812 &max_extent_size);
813 if (offset != SZ_32M) {
814 test_err("failed to allocate %u, offset: %llu",
815 2 * sectorsize, offset);
816 return -EINVAL;
817 }
818
819 ret = check_cache_empty(cache);
820 if (ret)
821 return ret;
822
823 cache->free_space_ctl->op = orig_free_space_ops;
824 __btrfs_remove_free_space_cache(cache->free_space_ctl);
825
826 return 0;
827 }
828
829 int btrfs_test_free_space_cache(u32 sectorsize, u32 nodesize)
830 {
831 struct btrfs_fs_info *fs_info;
832 struct btrfs_block_group_cache *cache;
833 struct btrfs_root *root = NULL;
834 int ret = -ENOMEM;
835
836 test_msg("running btrfs free space cache tests");
837 fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
838 if (!fs_info) {
839 test_std_err(TEST_ALLOC_FS_INFO);
840 return -ENOMEM;
841 }
842
843
844
845
846
847
848 cache = btrfs_alloc_dummy_block_group(fs_info,
849 BITS_PER_BITMAP * sectorsize + PAGE_SIZE);
850 if (!cache) {
851 test_std_err(TEST_ALLOC_BLOCK_GROUP);
852 btrfs_free_dummy_fs_info(fs_info);
853 return 0;
854 }
855
856 root = btrfs_alloc_dummy_root(fs_info);
857 if (IS_ERR(root)) {
858 test_std_err(TEST_ALLOC_ROOT);
859 ret = PTR_ERR(root);
860 goto out;
861 }
862
863 root->fs_info->extent_root = root;
864
865 ret = test_extents(cache);
866 if (ret)
867 goto out;
868 ret = test_bitmaps(cache, sectorsize);
869 if (ret)
870 goto out;
871 ret = test_bitmaps_and_extents(cache, sectorsize);
872 if (ret)
873 goto out;
874
875 ret = test_steal_space_from_bitmap_to_extent(cache, sectorsize);
876 out:
877 btrfs_free_dummy_block_group(cache);
878 btrfs_free_dummy_root(root);
879 btrfs_free_dummy_fs_info(fs_info);
880 return ret;
881 }