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
24 #include <sys/types.h>
26 #if HAVE_LINUX_TYPES_H
27 #include <linux/types.h>
37 struct bmap_rb_extent {
43 struct ext2fs_rb_private {
45 struct bmap_rb_extent *wcursor;
46 struct bmap_rb_extent *rcursor;
47 struct bmap_rb_extent *rcursor_next;
48 #ifdef ENABLE_BMAP_STATS_OPS
54 inline static struct bmap_rb_extent *node_to_extent(struct rb_node *node)
57 * This depends on the fact the struct rb_node is at the
58 * beginning of the bmap_rb_extent structure. We use this
59 * instead of the ext2fs_rb_entry macro because it causes gcc
60 * -Wall to generate a huge amount of noise.
62 return (struct bmap_rb_extent *) node;
65 static int rb_insert_extent(__u64 start, __u64 count,
66 struct ext2fs_rb_private *);
67 static void rb_get_new_extent(struct bmap_rb_extent **, __u64, __u64);
69 /* #define DEBUG_RB */
72 static void print_tree(struct rb_root *root)
74 struct rb_node *node = NULL;
75 struct bmap_rb_extent *ext;
77 fprintf(stderr, "\t\t\t=================================\n");
78 node = ext2fs_rb_first(root);
79 for (node = ext2fs_rb_first(root); node != NULL;
80 node = ext2fs_rb_next(node)) {
81 ext = node_to_extent(node);
82 fprintf(stderr, "\t\t\t--> (%llu -> %llu)\n",
83 (unsigned long long) ext->start,
84 (unsigned long long) ext->start + ext->count);
86 fprintf(stderr, "\t\t\t=================================\n");
89 static void check_tree(struct rb_root *root, const char *msg)
92 struct bmap_rb_extent *ext, *old = NULL;
94 for (node = ext2fs_rb_first(root); node;
95 node = ext2fs_rb_next(node)) {
96 ext = node_to_extent(node);
97 if (ext->count == 0) {
98 fprintf(stderr, "Tree Error: count is zero\n");
99 fprintf(stderr, "extent: %llu -> %llu (%llu)\n",
100 (unsigned long long) ext->start,
101 (unsigned long long) ext->start + ext->count,
102 (unsigned long long) ext->count);
105 if (ext->start + ext->count < ext->start) {
107 "Tree Error: start or count is crazy\n");
108 fprintf(stderr, "extent: %llu -> %llu (%llu)\n",
109 (unsigned long long) ext->start,
110 (unsigned long long) ext->start + ext->count,
111 (unsigned long long) ext->count);
116 if (old->start > ext->start) {
117 fprintf(stderr, "Tree Error: start is crazy\n");
118 fprintf(stderr, "extent: %llu -> %llu (%llu)\n",
119 (unsigned long long) old->start,
120 (unsigned long long) old->start + old->count,
121 (unsigned long long) old->count);
123 "extent next: %llu -> %llu (%llu)\n",
124 (unsigned long long) ext->start,
125 (unsigned long long) ext->start + ext->count,
126 (unsigned long long) ext->count);
129 if ((old->start + old->count) >= ext->start) {
131 "Tree Error: extent is crazy\n");
132 fprintf(stderr, "extent: %llu -> %llu (%llu)\n",
133 (unsigned long long) old->start,
134 (unsigned long long) old->start + old->count,
135 (unsigned long long) old->count);
137 "extent next: %llu -> %llu (%llu)\n",
138 (unsigned long long) ext->start,
139 (unsigned long long) ext->start + ext->count,
140 (unsigned long long) ext->count);
149 fprintf(stderr, "%s\n", msg);
154 #define check_tree(root, msg) do {} while (0)
155 #define print_tree(root) do {} while (0)
158 static void rb_get_new_extent(struct bmap_rb_extent **ext, __u64 start,
161 struct bmap_rb_extent *new_ext;
164 retval = ext2fs_get_mem(sizeof (struct bmap_rb_extent),
169 new_ext->start = start;
170 new_ext->count = count;
175 static void rb_free_extent(struct ext2fs_rb_private *bp,
176 struct bmap_rb_extent *ext)
178 if (bp->wcursor == ext)
180 if (bp->rcursor == ext)
182 if (bp->rcursor_next == ext)
183 bp->rcursor_next = NULL;
184 ext2fs_free_mem(&ext);
187 static errcode_t rb_alloc_private_data (ext2fs_generic_bitmap_64 bitmap)
189 struct ext2fs_rb_private *bp;
192 retval = ext2fs_get_mem(sizeof (struct ext2fs_rb_private), &bp);
198 bp->rcursor_next = NULL;
201 #ifdef ENABLE_BMAP_STATS_OPS
206 bitmap->private = (void *) bp;
210 static errcode_t rb_new_bmap(ext2_filsys fs EXT2FS_ATTR((unused)),
211 ext2fs_generic_bitmap_64 bitmap)
215 retval = rb_alloc_private_data (bitmap);
222 static void rb_free_tree(struct rb_root *root)
224 struct bmap_rb_extent *ext;
225 struct rb_node *node, *next;
227 for (node = ext2fs_rb_first(root); node; node = next) {
228 next = ext2fs_rb_next(node);
229 ext = node_to_extent(node);
230 ext2fs_rb_erase(node, root);
231 ext2fs_free_mem(&ext);
235 static void rb_free_bmap(ext2fs_generic_bitmap_64 bitmap)
237 struct ext2fs_rb_private *bp;
239 bp = (struct ext2fs_rb_private *) bitmap->private;
241 rb_free_tree(&bp->root);
242 ext2fs_free_mem(&bp);
246 static errcode_t rb_copy_bmap(ext2fs_generic_bitmap_64 src,
247 ext2fs_generic_bitmap_64 dest)
249 struct ext2fs_rb_private *src_bp, *dest_bp;
250 struct bmap_rb_extent *src_ext, *dest_ext;
251 struct rb_node *dest_node, *src_node, *dest_last, **n;
252 errcode_t retval = 0;
254 retval = rb_alloc_private_data (dest);
258 src_bp = (struct ext2fs_rb_private *) src->private;
259 dest_bp = (struct ext2fs_rb_private *) dest->private;
260 src_bp->rcursor = NULL;
261 dest_bp->rcursor = NULL;
263 src_node = ext2fs_rb_first(&src_bp->root);
265 src_ext = node_to_extent(src_node);
266 retval = ext2fs_get_mem(sizeof (struct bmap_rb_extent),
271 memcpy(dest_ext, src_ext, sizeof(struct bmap_rb_extent));
273 dest_node = &dest_ext->node;
274 n = &dest_bp->root.rb_node;
278 dest_last = ext2fs_rb_last(&dest_bp->root);
279 n = &(dest_last)->rb_right;
282 ext2fs_rb_link_node(dest_node, dest_last, n);
283 ext2fs_rb_insert_color(dest_node, &dest_bp->root);
285 src_node = ext2fs_rb_next(src_node);
291 static void rb_truncate(__u64 new_max, struct rb_root *root)
293 struct bmap_rb_extent *ext;
294 struct rb_node *node;
296 node = ext2fs_rb_last(root);
298 ext = node_to_extent(node);
300 if ((ext->start + ext->count - 1) <= new_max)
302 else if (ext->start > new_max) {
303 ext2fs_rb_erase(node, root);
304 ext2fs_free_mem(&ext);
305 node = ext2fs_rb_last(root);
308 ext->count = new_max - ext->start + 1;
312 static errcode_t rb_resize_bmap(ext2fs_generic_bitmap_64 bmap,
313 __u64 new_end, __u64 new_real_end)
315 struct ext2fs_rb_private *bp;
317 bp = (struct ext2fs_rb_private *) bmap->private;
321 rb_truncate(((new_end < bmap->end) ? new_end : bmap->end) - bmap->start,
325 bmap->real_end = new_real_end;
327 if (bmap->end < bmap->real_end)
328 rb_insert_extent(bmap->end + 1 - bmap->start,
329 bmap->real_end - bmap->end, bp);
335 rb_test_bit(struct ext2fs_rb_private *bp, __u64 bit)
337 struct bmap_rb_extent *rcursor, *next_ext = NULL;
338 struct rb_node *parent = NULL, *next;
339 struct rb_node **n = &bp->root.rb_node;
340 struct bmap_rb_extent *ext;
342 rcursor = bp->rcursor;
346 if (bit >= rcursor->start && bit < rcursor->start + rcursor->count) {
347 #ifdef ENABLE_BMAP_STATS_OPS
353 next_ext = bp->rcursor_next;
355 next = ext2fs_rb_next(&rcursor->node);
357 next_ext = node_to_extent(next);
358 bp->rcursor_next = next_ext;
361 if ((bit >= rcursor->start + rcursor->count) &&
362 (bit < next_ext->start)) {
363 #ifdef BMAP_STATS_OPS
370 bp->rcursor_next = NULL;
372 rcursor = bp->wcursor;
376 if (bit >= rcursor->start && bit < rcursor->start + rcursor->count)
383 ext = node_to_extent(parent);
384 if (bit < ext->start)
386 else if (bit >= (ext->start + ext->count))
390 bp->rcursor_next = NULL;
397 static int rb_insert_extent(__u64 start, __u64 count,
398 struct ext2fs_rb_private *bp)
400 struct rb_root *root = &bp->root;
401 struct rb_node *parent = NULL, **n = &root->rb_node;
402 struct rb_node *new_node, *node, *next;
403 struct bmap_rb_extent *new_ext;
404 struct bmap_rb_extent *ext;
410 bp->rcursor_next = NULL;
413 if (start >= ext->start &&
414 start <= (ext->start + ext->count)) {
415 #ifdef ENABLE_BMAP_STATS_OPS
424 ext = node_to_extent(parent);
426 if (start < ext->start) {
428 } else if (start > (ext->start + ext->count)) {
432 if ((start + count) <= (ext->start + ext->count))
435 if ((ext->start + ext->count) == start)
440 count += (start - ext->start);
443 new_node = &ext->node;
449 rb_get_new_extent(&new_ext, start, count);
451 new_node = &new_ext->node;
452 ext2fs_rb_link_node(new_node, parent, n);
453 ext2fs_rb_insert_color(new_node, root);
454 bp->wcursor = new_ext;
456 node = ext2fs_rb_prev(new_node);
458 ext = node_to_extent(node);
459 if ((ext->start + ext->count) == start) {
462 ext2fs_rb_erase(node, root);
463 rb_free_extent(bp, ext);
468 /* See if we can merge extent to the right */
469 for (node = ext2fs_rb_next(new_node); node != NULL; node = next) {
470 next = ext2fs_rb_next(node);
471 ext = node_to_extent(node);
473 if ((ext->start + ext->count) <= start)
476 /* No more merging */
477 if ((start + count) < ext->start)
480 /* ext is embedded in new_ext interval */
481 if ((start + count) >= (ext->start + ext->count)) {
482 ext2fs_rb_erase(node, root);
483 rb_free_extent(bp, ext);
486 /* merge ext with new_ext */
487 count += ((ext->start + ext->count) -
489 ext2fs_rb_erase(node, root);
490 rb_free_extent(bp, ext);
495 new_ext->start = start;
496 new_ext->count = count;
501 static int rb_remove_extent(__u64 start, __u64 count,
502 struct ext2fs_rb_private *bp)
504 struct rb_root *root = &bp->root;
505 struct rb_node *parent = NULL, **n = &root->rb_node;
506 struct rb_node *node;
507 struct bmap_rb_extent *ext;
508 __u64 new_start, new_count;
511 if (ext2fs_rb_empty_root(root))
516 ext = node_to_extent(parent);
517 if (start < ext->start) {
520 } else if (start >= (ext->start + ext->count)) {
525 if ((start > ext->start) &&
526 (start + count) < (ext->start + ext->count)) {
527 /* We have to split extent into two */
528 new_start = start + count;
529 new_count = (ext->start + ext->count) - new_start;
531 ext->count = start - ext->start;
533 rb_insert_extent(new_start, new_count, bp);
537 if ((start + count) >= (ext->start + ext->count)) {
538 ext->count = start - ext->start;
542 if (0 == ext->count) {
543 parent = ext2fs_rb_next(&ext->node);
544 ext2fs_rb_erase(&ext->node, root);
545 rb_free_extent(bp, ext);
549 if (start == ext->start) {
556 /* See if we should delete or truncate extent on the right */
557 for (; parent != NULL; parent = node) {
558 node = ext2fs_rb_next(parent);
559 ext = node_to_extent(parent);
560 if ((ext->start + ext->count) <= start)
563 /* No more extents to be removed/truncated */
564 if ((start + count) < ext->start)
567 /* The entire extent is within the region to be removed */
568 if ((start + count) >= (ext->start + ext->count)) {
569 ext2fs_rb_erase(parent, root);
570 rb_free_extent(bp, ext);
574 /* modify the last extent in region to be removed */
575 ext->count -= ((start + count) - ext->start);
576 ext->start = start + count;
585 static int rb_mark_bmap(ext2fs_generic_bitmap_64 bitmap, __u64 arg)
587 struct ext2fs_rb_private *bp;
590 bp = (struct ext2fs_rb_private *) bitmap->private;
591 arg -= bitmap->start;
593 retval = rb_insert_extent(arg, 1, bp);
594 check_tree(&bp->root, __func__);
598 static int rb_unmark_bmap(ext2fs_generic_bitmap_64 bitmap, __u64 arg)
600 struct ext2fs_rb_private *bp;
603 bp = (struct ext2fs_rb_private *) bitmap->private;
604 arg -= bitmap->start;
606 retval = rb_remove_extent(arg, 1, bp);
607 check_tree(&bp->root, __func__);
613 static int rb_test_bmap(ext2fs_generic_bitmap_64 bitmap, __u64 arg)
615 struct ext2fs_rb_private *bp;
617 bp = (struct ext2fs_rb_private *) bitmap->private;
618 arg -= bitmap->start;
620 return rb_test_bit(bp, arg);
623 static void rb_mark_bmap_extent(ext2fs_generic_bitmap_64 bitmap, __u64 arg,
626 struct ext2fs_rb_private *bp;
628 bp = (struct ext2fs_rb_private *) bitmap->private;
629 arg -= bitmap->start;
631 rb_insert_extent(arg, num, bp);
632 check_tree(&bp->root, __func__);
635 static void rb_unmark_bmap_extent(ext2fs_generic_bitmap_64 bitmap, __u64 arg,
638 struct ext2fs_rb_private *bp;
640 bp = (struct ext2fs_rb_private *) bitmap->private;
641 arg -= bitmap->start;
643 rb_remove_extent(arg, num, bp);
644 check_tree(&bp->root, __func__);
647 static int rb_test_clear_bmap_extent(ext2fs_generic_bitmap_64 bitmap,
648 __u64 start, unsigned int len)
650 struct rb_node *parent = NULL, **n;
651 struct rb_node *node, *next;
652 struct ext2fs_rb_private *bp;
653 struct bmap_rb_extent *ext;
656 bp = (struct ext2fs_rb_private *) bitmap->private;
657 n = &bp->root.rb_node;
658 start -= bitmap->start;
660 if (len == 0 || ext2fs_rb_empty_root(&bp->root))
664 * If we find nothing, we should examine whole extent, but
665 * when we find match, the extent is not clean, thus be return
670 ext = node_to_extent(parent);
671 if (start < ext->start) {
673 } else if (start >= (ext->start + ext->count)) {
677 * We found extent int the tree -> extent is not
686 next = ext2fs_rb_next(node);
687 ext = node_to_extent(node);
690 if ((ext->start + ext->count) <= start)
693 /* No more merging */
694 if ((start + len) <= ext->start)
703 static errcode_t rb_set_bmap_range(ext2fs_generic_bitmap_64 bitmap,
704 __u64 start, size_t num, void *in)
706 struct ext2fs_rb_private *bp;
707 unsigned char *cp = in;
711 bp = (struct ext2fs_rb_private *) bitmap->private;
713 for (i = 0; i < num; i++) {
715 unsigned char c = cp[i/8];
722 if ((c == 0x00) && (first_set == -1)) {
727 if (ext2fs_test_bit(i, in)) {
735 rb_insert_extent(start + first_set - bitmap->start,
737 check_tree(&bp->root, __func__);
740 if (first_set != -1) {
741 rb_insert_extent(start + first_set - bitmap->start,
742 num - first_set, bp);
743 check_tree(&bp->root, __func__);
749 static errcode_t rb_get_bmap_range(ext2fs_generic_bitmap_64 bitmap,
750 __u64 start, size_t num, void *out)
753 struct rb_node *parent = NULL, *next, **n;
754 struct ext2fs_rb_private *bp;
755 struct bmap_rb_extent *ext;
758 bp = (struct ext2fs_rb_private *) bitmap->private;
759 n = &bp->root.rb_node;
760 start -= bitmap->start;
762 if (ext2fs_rb_empty_root(&bp->root))
767 ext = node_to_extent(parent);
768 if (start < ext->start) {
770 } else if (start >= (ext->start + ext->count)) {
776 memset(out, 0, (num + 7) >> 3);
778 for (; parent != NULL; parent = next) {
779 next = ext2fs_rb_next(parent);
780 ext = node_to_extent(parent);
784 if (pos >= start + num)
787 if (pos + count < start)
789 count -= start - pos;
792 if (pos + count > start + num)
793 count = start + num - pos;
797 ((pos - start) % 8) == 0) {
798 int nbytes = count >> 3;
799 int offset = (pos - start) >> 3;
801 memset(((char *) out) + offset, 0xFF, nbytes);
803 count -= nbytes << 3;
806 ext2fs_fast_set_bit64((pos - start), out);
814 static void rb_clear_bmap(ext2fs_generic_bitmap_64 bitmap)
816 struct ext2fs_rb_private *bp;
818 bp = (struct ext2fs_rb_private *) bitmap->private;
820 rb_free_tree(&bp->root);
822 bp->rcursor_next = NULL;
824 check_tree(&bp->root, __func__);
827 static errcode_t rb_find_first_zero(ext2fs_generic_bitmap_64 bitmap,
828 __u64 start, __u64 end, __u64 *out)
830 struct rb_node *parent = NULL, **n;
831 struct ext2fs_rb_private *bp;
832 struct bmap_rb_extent *ext;
834 bp = (struct ext2fs_rb_private *) bitmap->private;
835 n = &bp->root.rb_node;
836 start -= bitmap->start;
837 end -= bitmap->start;
842 if (ext2fs_rb_empty_root(&bp->root))
847 ext = node_to_extent(parent);
848 if (start < ext->start) {
850 } else if (start >= (ext->start + ext->count)) {
852 } else if (ext->start + ext->count <= end) {
853 *out = ext->start + ext->count + bitmap->start;
859 *out = start + bitmap->start;
863 static errcode_t rb_find_first_set(ext2fs_generic_bitmap_64 bitmap,
864 __u64 start, __u64 end, __u64 *out)
866 struct rb_node *parent = NULL, **n;
867 struct rb_node *node;
868 struct ext2fs_rb_private *bp;
869 struct bmap_rb_extent *ext;
871 bp = (struct ext2fs_rb_private *) bitmap->private;
872 n = &bp->root.rb_node;
873 start -= bitmap->start;
874 end -= bitmap->start;
879 if (ext2fs_rb_empty_root(&bp->root))
884 ext = node_to_extent(parent);
885 if (start < ext->start) {
887 } else if (start >= (ext->start + ext->count)) {
890 /* The start bit is set */
891 *out = start + bitmap->start;
897 ext = node_to_extent(node);
898 if (ext->start < start) {
899 node = ext2fs_rb_next(node);
902 ext = node_to_extent(node);
904 if (ext->start <= end) {
905 *out = ext->start + bitmap->start;
911 #ifdef ENABLE_BMAP_STATS
912 static void rb_print_stats(ext2fs_generic_bitmap_64 bitmap)
914 struct ext2fs_rb_private *bp;
915 struct rb_node *node = NULL;
916 struct bmap_rb_extent *ext;
919 __u64 min_size = ULONG_MAX;
920 __u64 size = 0, avg_size = 0;
922 #ifdef ENABLE_BMAP_STATS_OPS
923 __u64 mark_all, test_all;
924 double m_hit = 0.0, t_hit = 0.0;
927 bp = (struct ext2fs_rb_private *) bitmap->private;
929 for (node = ext2fs_rb_first(&bp->root); node != NULL;
930 node = ext2fs_rb_next(node)) {
931 ext = node_to_extent(node);
933 if (ext->count > max_size)
934 max_size = ext->count;
935 if (ext->count < min_size)
936 min_size = ext->count;
941 avg_size = size / count;
942 if (min_size == ULONG_MAX)
944 eff = (double)((count * sizeof(struct bmap_rb_extent)) << 3) /
945 (bitmap->real_end - bitmap->start);
946 #ifdef ENABLE_BMAP_STATS_OPS
947 mark_all = bitmap->stats.mark_count + bitmap->stats.mark_ext_count;
948 test_all = bitmap->stats.test_count + bitmap->stats.test_ext_count;
950 m_hit = ((double)bp->mark_hit / mark_all) * 100;
952 t_hit = ((double)bp->test_hit / test_all) * 100;
954 fprintf(stderr, "%16llu cache hits on test (%.2f%%)\n"
955 "%16llu cache hits on mark (%.2f%%)\n",
956 bp->test_hit, t_hit, bp->mark_hit, m_hit);
958 fprintf(stderr, "%16llu extents (%llu bytes)\n",
959 (unsigned long long) count, (unsigned long long)
960 ((count * sizeof(struct bmap_rb_extent)) +
961 sizeof(struct ext2fs_rb_private)));
962 fprintf(stderr, "%16llu bits minimum size\n",
963 (unsigned long long) min_size);
964 fprintf(stderr, "%16llu bits maximum size\n"
965 "%16llu bits average size\n",
966 (unsigned long long) max_size, (unsigned long long) avg_size);
967 fprintf(stderr, "%16llu bits set in bitmap (out of %llu)\n",
968 (unsigned long long) size,
969 (unsigned long long) bitmap->real_end - bitmap->start);
971 "%16.4lf memory / bitmap bit memory ratio (bitarray = 1)\n",
975 static void rb_print_stats(ext2fs_generic_bitmap_64 bitmap EXT2FS_ATTR((unused)))
980 struct ext2_bitmap_ops ext2fs_blkmap64_rbtree = {
981 .type = EXT2FS_BMAP64_RBTREE,
982 .new_bmap = rb_new_bmap,
983 .free_bmap = rb_free_bmap,
984 .copy_bmap = rb_copy_bmap,
985 .resize_bmap = rb_resize_bmap,
986 .mark_bmap = rb_mark_bmap,
987 .unmark_bmap = rb_unmark_bmap,
988 .test_bmap = rb_test_bmap,
989 .test_clear_bmap_extent = rb_test_clear_bmap_extent,
990 .mark_bmap_extent = rb_mark_bmap_extent,
991 .unmark_bmap_extent = rb_unmark_bmap_extent,
992 .set_bmap_range = rb_set_bmap_range,
993 .get_bmap_range = rb_get_bmap_range,
994 .clear_bmap = rb_clear_bmap,
995 .print_stats = rb_print_stats,
996 .find_first_zero = rb_find_first_zero,
997 .find_first_set = rb_find_first_set,