2 * blkmap64_rb.c --- Simple rb-tree implementation for bitmaps
4 * (C)2010 Red Hat, Inc., Lukas Czerner <lczerner@redhat.com>
7 * This file may be redistributed under the terms of the GNU Public
23 #include <sys/types.h>
33 struct bmap_rb_extent {
39 struct ext2fs_rb_private {
41 struct bmap_rb_extent *wcursor;
42 struct bmap_rb_extent *rcursor;
43 struct bmap_rb_extent *rcursor_next;
50 static int rb_insert_extent(__u64 start, __u64 count,
51 struct ext2fs_rb_private *);
52 static void rb_get_new_extent(struct bmap_rb_extent **, __u64, __u64);
54 /* #define DEBUG_RB */
57 static void print_tree(struct rb_root *root)
59 struct rb_node *node = NULL;
60 struct bmap_rb_extent *ext;
62 printf("\t\t\t=================================\n");
63 node = ext2fs_rb_first(root);
64 for (node = ext2fs_rb_first(root); node != NULL;
65 node = ext2fs_rb_next(node)) {
66 ext = ext2fs_rb_entry(node, struct bmap_rb_extent, node);
67 printf("\t\t\t--> (%llu -> %llu)\n",
68 ext->start, ext->start + ext->count);
70 printf("\t\t\t=================================\n");
73 static int check_tree(struct rb_root *root, const char *msg)
75 struct rb_node *new_node, *node, *next;
76 struct bmap_rb_extent *ext, *old = NULL;
78 for (node = ext2fs_rb_first(root); node;
79 node = ext2fs_rb_next(node)) {
80 ext = ext2fs_rb_entry(node, struct bmap_rb_extent, node);
81 if (ext->count <= 0) {
82 printf("Tree Error: count is crazy\n");
83 printf("extent: %llu -> %llu (%u)\n", ext->start,
84 ext->start + ext->count, ext->count);
88 printf("Tree Error: start is crazy\n");
89 printf("extent: %llu -> %llu (%u)\n", ext->start,
90 ext->start + ext->count, ext->count);
95 if (old->start > ext->start) {
96 printf("Tree Error: start is crazy\n");
97 printf("extent: %llu -> %llu (%u)\n",
98 old->start, old->start + old->count,
100 printf("extent next: %llu -> %llu (%u)\n",
101 ext->start, ext->start + ext->count,
105 if ((old->start + old->count) >= ext->start) {
106 printf("Tree Error: extent is crazy\n");
107 printf("extent: %llu -> %llu (%u)\n",
108 old->start, old->start + old->count,
110 printf("extent next: %llu -> %llu (%u)\n",
111 ext->start, ext->start + ext->count,
126 #define check_tree(root, msg) 0
127 #define print_tree(root, msg) 0
130 static void rb_get_new_extent(struct bmap_rb_extent **ext, __u64 start,
133 struct bmap_rb_extent *new_ext;
136 retval = ext2fs_get_mem(sizeof (struct bmap_rb_extent),
139 perror("ext2fs_get_mem");
143 new_ext->start = start;
144 new_ext->count = count;
149 static void rb_free_extent(struct ext2fs_rb_private *bp,
150 struct bmap_rb_extent *ext)
152 if (bp->wcursor == ext)
154 if (bp->rcursor == ext)
156 if (bp->rcursor_next == ext)
157 bp->rcursor_next = NULL;
158 ext2fs_free_mem(&ext);
161 static errcode_t rb_alloc_private_data (ext2fs_generic_bitmap bitmap)
163 struct ext2fs_rb_private *bp;
166 retval = ext2fs_get_mem(sizeof (struct ext2fs_rb_private), &bp);
172 bp->rcursor_next = NULL;
175 #ifdef BMAP_STATS_OPS
180 bitmap->private = (void *) bp;
184 static errcode_t rb_new_bmap(ext2_filsys fs EXT2FS_ATTR((unused)),
185 ext2fs_generic_bitmap bitmap)
189 retval = rb_alloc_private_data (bitmap);
196 static void rb_free_tree(struct rb_root *root)
198 struct bmap_rb_extent *ext;
199 struct rb_node *node, *next;
201 for (node = ext2fs_rb_first(root); node; node = next) {
202 next = ext2fs_rb_next(node);
203 ext = ext2fs_rb_entry(node, struct bmap_rb_extent, node);
204 ext2fs_rb_erase(node, root);
205 ext2fs_free_mem(&ext);
209 static void rb_free_bmap(ext2fs_generic_bitmap bitmap)
211 struct ext2fs_rb_private *bp;
213 bp = (struct ext2fs_rb_private *) bitmap->private;
215 rb_free_tree(&bp->root);
216 ext2fs_free_mem(&bp);
220 static errcode_t rb_copy_bmap(ext2fs_generic_bitmap src,
221 ext2fs_generic_bitmap dest)
223 struct ext2fs_rb_private *src_bp, *dest_bp;
224 struct bmap_rb_extent *src_ext, *dest_ext;
225 struct rb_node *dest_node, *src_node, *dest_last, **n;
226 errcode_t retval = 0;
228 retval = rb_alloc_private_data (dest);
232 src_bp = (struct ext2fs_rb_private *) src->private;
233 dest_bp = (struct ext2fs_rb_private *) dest->private;
234 src_bp->rcursor = NULL;
235 dest_bp->rcursor = NULL;
237 src_node = ext2fs_rb_first(&src_bp->root);
239 src_ext = ext2fs_rb_entry(src_node, struct bmap_rb_extent, node);
240 retval = ext2fs_get_mem(sizeof (struct bmap_rb_extent),
245 memcpy(dest_ext, src_ext, sizeof(struct bmap_rb_extent));
247 dest_node = &dest_ext->node;
248 n = &dest_bp->root.rb_node;
252 dest_last = ext2fs_rb_last(&dest_bp->root);
253 n = &(dest_last)->rb_right;
256 ext2fs_rb_link_node(dest_node, dest_last, n);
257 ext2fs_rb_insert_color(dest_node, &dest_bp->root);
259 src_node = ext2fs_rb_next(src_node);
265 static void rb_truncate(__u64 new_max, struct rb_root *root)
267 struct bmap_rb_extent *ext;
268 struct rb_node *node;
270 node = ext2fs_rb_last(root);
272 ext = ext2fs_rb_entry(node, struct bmap_rb_extent, node);
274 if ((ext->start + ext->count - 1) <= new_max)
276 else if (ext->start > new_max) {
277 ext2fs_rb_erase(node, root);
278 ext2fs_free_mem(&ext);
279 node = ext2fs_rb_last(root);
282 ext->count = new_max - ext->start + 1;
286 static errcode_t rb_resize_bmap(ext2fs_generic_bitmap bmap,
287 __u64 new_end, __u64 new_real_end)
289 struct ext2fs_rb_private *bp;
291 if (new_real_end >= bmap->real_end) {
293 bmap->real_end = new_real_end;
297 bp = (struct ext2fs_rb_private *) bmap->private;
301 /* truncate tree to new_real_end size */
302 rb_truncate(new_real_end, &bp->root);
305 bmap->real_end = new_real_end;
311 rb_test_bit(struct ext2fs_rb_private *bp, __u64 bit)
313 struct bmap_rb_extent *rcursor, *next_ext = NULL;
314 struct rb_node *parent = NULL, *next;
315 struct rb_node **n = &bp->root.rb_node;
316 struct bmap_rb_extent *ext;
318 rcursor = bp->rcursor;
322 if (bit >= rcursor->start && bit < rcursor->start + rcursor->count) {
323 #ifdef BMAP_STATS_OPS
329 next_ext = bp->rcursor_next;
331 next = ext2fs_rb_next(&rcursor->node);
333 next_ext = ext2fs_rb_entry(next, struct bmap_rb_extent,
335 bp->rcursor_next = next_ext;
338 if ((bit >= rcursor->start + rcursor->count) &&
339 (bit < next_ext->start)) {
340 #ifdef BMAP_STATS_OPS
347 bp->rcursor_next = NULL;
349 rcursor = bp->wcursor;
353 if (bit >= rcursor->start && bit < rcursor->start + rcursor->count)
360 ext = ext2fs_rb_entry(parent, struct bmap_rb_extent, node);
361 if (bit < ext->start)
363 else if (bit >= (ext->start + ext->count))
367 bp->rcursor_next = NULL;
374 static int rb_insert_extent(__u64 start, __u64 count,
375 struct ext2fs_rb_private *bp)
377 struct rb_root *root = &bp->root;
378 struct rb_node *parent = NULL, **n = &root->rb_node;
379 struct rb_node *new_node, *node, *next;
380 struct bmap_rb_extent *new_ext;
381 struct bmap_rb_extent *ext;
384 bp->rcursor_next = NULL;
387 if (start >= ext->start &&
388 start <= (ext->start + ext->count)) {
389 #ifdef BMAP_STATS_OPS
398 ext = ext2fs_rb_entry(parent, struct bmap_rb_extent, node);
400 if (start < ext->start) {
402 } else if (start > (ext->start + ext->count)) {
406 if ((start + count) <= (ext->start + ext->count))
409 if ((ext->start + ext->count) == start)
414 count += (start - ext->start);
417 new_node = &ext->node;
423 rb_get_new_extent(&new_ext, start, count);
425 new_node = &new_ext->node;
426 ext2fs_rb_link_node(new_node, parent, n);
427 ext2fs_rb_insert_color(new_node, root);
428 bp->wcursor = new_ext;
430 node = ext2fs_rb_prev(new_node);
432 ext = ext2fs_rb_entry(node, struct bmap_rb_extent, node);
433 if ((ext->start + ext->count) == start) {
436 ext2fs_rb_erase(node, root);
437 rb_free_extent(bp, ext);
442 /* See if we can merge extent to the right */
443 for (node = ext2fs_rb_next(new_node); node != NULL; node = next) {
444 next = ext2fs_rb_next(node);
445 ext = ext2fs_rb_entry(node, struct bmap_rb_extent, node);
447 if ((ext->start + ext->count) <= start)
450 /* No more merging */
451 if ((start + count) < ext->start)
454 /* ext is embedded in new_ext interval */
455 if ((start + count) >= (ext->start + ext->count)) {
456 ext2fs_rb_erase(node, root);
457 rb_free_extent(bp, ext);
460 /* merge ext with new_ext */
461 count += ((ext->start + ext->count) -
463 ext2fs_rb_erase(node, root);
464 rb_free_extent(bp, ext);
469 new_ext->start = start;
470 new_ext->count = count;
475 static int rb_remove_extent(__u64 start, __u64 count,
476 struct ext2fs_rb_private *bp)
478 struct rb_root *root = &bp->root;
479 struct rb_node *parent = NULL, **n = &root->rb_node;
480 struct rb_node *node;
481 struct bmap_rb_extent *ext;
482 __u64 new_start, new_count;
485 if (EXT2FS_RB_EMPTY_ROOT(root))
490 ext = ext2fs_rb_entry(parent, struct bmap_rb_extent, node);
491 if (start < ext->start) {
494 } else if (start >= (ext->start + ext->count)) {
499 if ((start > ext->start) &&
500 (start + count) < (ext->start + ext->count)) {
501 /* We have to split extent into two */
502 new_start = start + count;
503 new_count = (ext->start + ext->count) - new_start;
505 ext->count = start - ext->start;
507 rb_insert_extent(new_start, new_count, bp);
511 if ((start + count) >= (ext->start + ext->count)) {
512 ext->count = start - ext->start;
516 if (0 == ext->count) {
517 parent = ext2fs_rb_next(&ext->node);
518 ext2fs_rb_erase(&ext->node, root);
519 rb_free_extent(bp, ext);
523 if (start == ext->start) {
530 /* See if we should delete or truncate extent on the right */
531 for (; parent != NULL; parent = node) {
532 node = ext2fs_rb_next(parent);
533 ext = ext2fs_rb_entry(parent, struct bmap_rb_extent, node);
534 if ((ext->start + ext->count) <= start)
537 /* No more extents to be removed/truncated */
538 if ((start + count) < ext->start)
541 /* The entire extent is within the region to be removed */
542 if ((start + count) >= (ext->start + ext->count)) {
543 ext2fs_rb_erase(parent, root);
544 rb_free_extent(bp, ext);
548 /* modify the last extent in reigon to be removed */
549 ext->count -= ((start + count) - ext->start);
550 ext->start = start + count;
559 static int rb_mark_bmap(ext2fs_generic_bitmap bitmap, __u64 arg)
561 struct ext2fs_rb_private *bp;
563 bp = (struct ext2fs_rb_private *) bitmap->private;
564 arg -= bitmap->start;
566 return rb_insert_extent(arg, 1, bp);
569 static int rb_unmark_bmap(ext2fs_generic_bitmap bitmap, __u64 arg)
571 struct ext2fs_rb_private *bp;
574 bp = (struct ext2fs_rb_private *) bitmap->private;
575 arg -= bitmap->start;
577 retval = rb_remove_extent(arg, 1, bp);
578 check_tree(&bp->root, __func__);
584 static int rb_test_bmap(ext2fs_generic_bitmap bitmap, __u64 arg)
586 struct ext2fs_rb_private *bp;
588 bp = (struct ext2fs_rb_private *) bitmap->private;
589 arg -= bitmap->start;
591 return rb_test_bit(bp, arg);
594 static void rb_mark_bmap_extent(ext2fs_generic_bitmap bitmap, __u64 arg,
597 struct ext2fs_rb_private *bp;
599 bp = (struct ext2fs_rb_private *) bitmap->private;
600 arg -= bitmap->start;
602 rb_insert_extent(arg, num, bp);
605 static void rb_unmark_bmap_extent(ext2fs_generic_bitmap bitmap, __u64 arg,
608 struct ext2fs_rb_private *bp;
610 bp = (struct ext2fs_rb_private *) bitmap->private;
611 arg -= bitmap->start;
613 rb_remove_extent(arg, num, bp);
614 check_tree(&bp->root, __func__);
617 static int rb_test_clear_bmap_extent(ext2fs_generic_bitmap bitmap,
618 __u64 start, unsigned int len)
620 struct rb_node *parent = NULL, **n;
621 struct rb_node *node, *next;
622 struct ext2fs_rb_private *bp;
623 struct bmap_rb_extent *ext;
626 bp = (struct ext2fs_rb_private *) bitmap->private;
627 n = &bp->root.rb_node;
628 start -= bitmap->start;
630 if ((len == 0) || EXT2FS_RB_EMPTY_ROOT(&bp->root))
634 * If we find nothing, we should examine whole extent, but
635 * when we find match, the extent is not clean, thus be return
640 ext = ext2fs_rb_entry(parent, struct bmap_rb_extent, node);
641 if (start < ext->start) {
643 } else if (start >= (ext->start + ext->count)) {
647 * We found extent int the tree -> extent is not
656 next = ext2fs_rb_next(node);
657 ext = ext2fs_rb_entry(node, struct bmap_rb_extent, node);
660 if ((ext->start + ext->count) <= start)
663 /* No more merging */
664 if ((start + len) <= ext->start)
673 static errcode_t rb_set_bmap_range(ext2fs_generic_bitmap bitmap,
674 __u64 start, size_t num, void *in)
676 struct ext2fs_rb_private *bp;
677 unsigned char *cp = in;
682 bp = (struct ext2fs_rb_private *) bitmap->private;
684 for (i = 0; i < num; i++) {
686 unsigned char c = cp[i/8];
693 if ((c == 0x00) && (first_set == -1)) {
698 if (ext2fs_test_bit(i, in)) {
706 rb_insert_extent(start + first_set - bitmap->start,
711 rb_insert_extent(start + first_set - bitmap->start,
712 num - first_set, bp);
717 static errcode_t rb_get_bmap_range(ext2fs_generic_bitmap bitmap,
718 __u64 start, size_t num, void *out)
721 struct rb_node *parent = NULL, *next, **n;
722 struct ext2fs_rb_private *bp;
723 struct bmap_rb_extent *ext;
726 bp = (struct ext2fs_rb_private *) bitmap->private;
727 n = &bp->root.rb_node;
728 start -= bitmap->start;
730 if (EXT2FS_RB_EMPTY_ROOT(&bp->root))
735 ext = ext2fs_rb_entry(parent, struct bmap_rb_extent, node);
736 if (start < ext->start) {
738 } else if (start >= (ext->start + ext->count)) {
744 memset(out, 0, (num + 7) >> 3);
746 for (; parent != NULL; parent = next) {
747 next = ext2fs_rb_next(parent);
748 ext = ext2fs_rb_entry(parent, struct bmap_rb_extent, node);
754 while (pos < (ext->start + ext->count)) {
755 if ((pos - start) >= num)
757 ext2fs_fast_set_bit64((pos - start), out);
764 static void rb_clear_bmap(ext2fs_generic_bitmap bitmap)
766 struct ext2fs_rb_private *bp;
768 bp = (struct ext2fs_rb_private *) bitmap->private;
770 rb_free_tree(&bp->root);
772 bp->rcursor_next = NULL;
777 static void rb_print_stats(ext2fs_generic_bitmap bitmap)
779 struct ext2fs_rb_private *bp;
780 struct rb_node *node = NULL;
781 struct bmap_rb_extent *ext;
784 __u64 min_size = ULONG_MAX;
785 __u64 size = 0, avg_size = 0;
787 #ifdef BMAP_STATS_OPS
788 __u64 mark_all, test_all;
789 double m_hit = 0.0, t_hit = 0.0;
792 bp = (struct ext2fs_rb_private *) bitmap->private;
794 node = ext2fs_rb_first(&bp->root);
795 for (node = ext2fs_rb_first(&bp->root); node != NULL;
796 node = ext2fs_rb_next(node)) {
797 ext = ext2fs_rb_entry(node, struct bmap_rb_extent, node);
799 if (ext->count > max_size)
800 max_size = ext->count;
801 if (ext->count < min_size)
802 min_size = ext->count;
807 avg_size = size / count;
808 if (min_size == ULONG_MAX)
810 eff = (double)((count * sizeof(struct bmap_rb_extent)) << 3) /
811 (bitmap->real_end - bitmap->start);
812 #ifdef BMAP_STATS_OPS
813 mark_all = bitmap->stats.mark_count + bitmap->stats.mark_ext_count;
814 test_all = bitmap->stats.test_count + bitmap->stats.test_ext_count;
816 m_hit = ((double)bp->mark_hit / mark_all) * 100;
818 t_hit = ((double)bp->test_hit / test_all) * 100;
820 fprintf(stderr, "%16llu cache hits on test (%.2f%%)\n"
821 "%16llu cache hits on mark (%.2f%%)\n",
822 bp->test_hit, t_hit, bp->mark_hit, m_hit);
824 fprintf(stderr, "%16llu extents (%llu bytes)\n",
825 count, ((count * sizeof(struct bmap_rb_extent)) +
826 sizeof(struct ext2fs_rb_private)));
827 fprintf(stderr, "%16llu bits minimum size\n",
829 fprintf(stderr, "%16llu bits maximum size\n"
830 "%16llu bits average size\n",
832 fprintf(stderr, "%16llu bits set in bitmap (out of %llu)\n", size,
833 bitmap->real_end - bitmap->start);
835 "%16.4lf memory / bitmap bit memory ratio (bitarray = 1)\n",
839 static void rb_print_stats(ext2fs_generic_bitmap bitmap){}
842 struct ext2_bitmap_ops ext2fs_blkmap64_rbtree = {
843 .type = EXT2FS_BMAP64_RBTREE,
844 .new_bmap = rb_new_bmap,
845 .free_bmap = rb_free_bmap,
846 .copy_bmap = rb_copy_bmap,
847 .resize_bmap = rb_resize_bmap,
848 .mark_bmap = rb_mark_bmap,
849 .unmark_bmap = rb_unmark_bmap,
850 .test_bmap = rb_test_bmap,
851 .test_clear_bmap_extent = rb_test_clear_bmap_extent,
852 .mark_bmap_extent = rb_mark_bmap_extent,
853 .unmark_bmap_extent = rb_unmark_bmap_extent,
854 .set_bmap_range = rb_set_bmap_range,
855 .get_bmap_range = rb_get_bmap_range,
856 .clear_bmap = rb_clear_bmap,
857 .print_stats = rb_print_stats,