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