Whamcloud - gitweb
Merge branch 'maint' into next
[tools/e2fsprogs.git] / lib / ext2fs / blkmap64_rb.c
1 /*
2  * blkmap64_rb.c --- Simple rb-tree implementation for bitmaps
3  *
4  * (C)2010 Red Hat, Inc., Lukas Czerner <lczerner@redhat.com>
5  *
6  * %Begin-Header%
7  * This file may be redistributed under the terms of the GNU Public
8  * License.
9  * %End-Header%
10  */
11
12 #include <stdio.h>
13 #include <string.h>
14 #if HAVE_UNISTD_H
15 #include <unistd.h>
16 #endif
17 #include <fcntl.h>
18 #include <time.h>
19 #if HAVE_SYS_STAT_H
20 #include <sys/stat.h>
21 #endif
22 #if HAVE_SYS_TYPES_H
23 #include <sys/types.h>
24 #endif
25
26 #include "ext2_fs.h"
27 #include "ext2fsP.h"
28 #include "bmap64.h"
29 #include "rbtree.h"
30
31 #include <limits.h>
32
33 struct bmap_rb_extent {
34         struct rb_node node;
35         __u64 start;
36         __u64 count;
37 };
38
39 struct ext2fs_rb_private {
40         struct rb_root root;
41         struct bmap_rb_extent *wcursor;
42         struct bmap_rb_extent *rcursor;
43         struct bmap_rb_extent *rcursor_next;
44 #ifdef ENABLE_BMAP_STATS_OPS
45         __u64 mark_hit;
46         __u64 test_hit;
47 #endif
48 };
49
50 inline static struct bmap_rb_extent *node_to_extent(struct rb_node *node)
51 {
52         /*
53          * This depends on the fact the struct rb_node is at the
54          * beginning of the bmap_rb_extent structure.  We use this
55          * instead of the ext2fs_rb_entry macro because it causes gcc
56          * -Wall to generate a huge amount of noise.
57          */
58         return (struct bmap_rb_extent *) node;
59 }
60
61 static int rb_insert_extent(__u64 start, __u64 count,
62                             struct ext2fs_rb_private *);
63 static void rb_get_new_extent(struct bmap_rb_extent **, __u64, __u64);
64
65 /* #define DEBUG_RB */
66
67 #ifdef DEBUG_RB
68 static void print_tree(struct rb_root *root)
69 {
70         struct rb_node *node = NULL;
71         struct bmap_rb_extent *ext;
72
73         printf("\t\t\t=================================\n");
74         node = ext2fs_rb_first(root);
75         for (node = ext2fs_rb_first(root); node != NULL; 
76              node = ext2fs_rb_next(node)) {
77                 ext = node_to_extent(node);
78                 printf("\t\t\t--> (%llu -> %llu)\n",
79                         ext->start, ext->start + ext->count);
80         }
81         printf("\t\t\t=================================\n");
82 }
83
84 static void check_tree(struct rb_root *root, const char *msg)
85 {
86         struct rb_node *node;
87         struct bmap_rb_extent *ext, *old = NULL;
88
89         for (node = ext2fs_rb_first(root); node;
90              node = ext2fs_rb_next(node)) {
91                 ext = node_to_extent(node);
92                 if (ext->count == 0) {
93                         printf("Tree Error: count is zero\n");
94                         printf("extent: %llu -> %llu (%llu)\n", ext->start,
95                                 ext->start + ext->count, ext->count);
96                         goto err_out;
97                 }
98                 if (ext->start + ext->count < ext->start) {
99                         printf("Tree Error: start or count is crazy\n");
100                         printf("extent: %llu -> %llu (%llu)\n", ext->start,
101                                 ext->start + ext->count, ext->count);
102                         goto err_out;
103                 }
104
105                 if (old) {
106                         if (old->start > ext->start) {
107                                 printf("Tree Error: start is crazy\n");
108                                 printf("extent: %llu -> %llu (%llu)\n",
109                                         old->start, old->start + old->count,
110                                         old->count);
111                                 printf("extent next: %llu -> %llu (%llu)\n",
112                                         ext->start, ext->start + ext->count,
113                                         ext->count);
114                                 goto err_out;
115                         }
116                         if ((old->start + old->count) >= ext->start) {
117                                 printf("Tree Error: extent is crazy\n");
118                                 printf("extent: %llu -> %llu (%llu)\n",
119                                         old->start, old->start + old->count,
120                                         old->count);
121                                 printf("extent next: %llu -> %llu (%llu)\n",
122                                         ext->start, ext->start + ext->count,
123                                         ext->count);
124                                 goto err_out;
125                         }
126                 }
127                 old = ext;
128         }
129         return;
130
131 err_out:
132         printf("%s\n", msg);
133         print_tree(root);
134         exit(1);
135 }
136 #else
137 #define check_tree(root, msg) do {} while (0)
138 #define print_tree(root) do {} while (0)
139 #endif
140
141 static void rb_get_new_extent(struct bmap_rb_extent **ext, __u64 start,
142                              __u64 count)
143 {
144         struct bmap_rb_extent *new_ext;
145         int retval;
146
147         retval = ext2fs_get_mem(sizeof (struct bmap_rb_extent),
148                                 &new_ext);
149         if (retval)
150                 abort();
151
152         new_ext->start = start;
153         new_ext->count = count;
154         *ext = new_ext;
155 }
156
157 inline
158 static void rb_free_extent(struct ext2fs_rb_private *bp,
159                            struct bmap_rb_extent *ext)
160 {
161         if (bp->wcursor == ext)
162                 bp->wcursor = NULL;
163         if (bp->rcursor == ext)
164                 bp->rcursor = NULL;
165         if (bp->rcursor_next == ext)
166                 bp->rcursor_next = NULL;
167         ext2fs_free_mem(&ext);
168 }
169
170 static errcode_t rb_alloc_private_data (ext2fs_generic_bitmap bitmap)
171 {
172         struct ext2fs_rb_private *bp;
173         errcode_t       retval;
174
175         retval = ext2fs_get_mem(sizeof (struct ext2fs_rb_private), &bp);
176         if (retval)
177                 return retval;
178
179         bp->root = RB_ROOT;
180         bp->rcursor = NULL;
181         bp->rcursor_next = NULL;
182         bp->wcursor = NULL;
183
184 #ifdef ENABLE_BMAP_STATS_OPS
185         bp->test_hit = 0;
186         bp->mark_hit = 0;
187 #endif
188
189         bitmap->private = (void *) bp;
190         return 0;
191 }
192
193 static errcode_t rb_new_bmap(ext2_filsys fs EXT2FS_ATTR((unused)),
194                              ext2fs_generic_bitmap bitmap)
195 {
196         errcode_t       retval;
197
198         retval = rb_alloc_private_data (bitmap);
199         if (retval)
200                 return retval;
201
202         return 0;
203 }
204
205 static void rb_free_tree(struct rb_root *root)
206 {
207         struct bmap_rb_extent *ext;
208         struct rb_node *node, *next;
209
210         for (node = ext2fs_rb_first(root); node; node = next) {
211                 next = ext2fs_rb_next(node);
212                 ext = node_to_extent(node);
213                 ext2fs_rb_erase(node, root);
214                 ext2fs_free_mem(&ext);
215         }
216 }
217
218 static void rb_free_bmap(ext2fs_generic_bitmap bitmap)
219 {
220         struct ext2fs_rb_private *bp;
221
222         bp = (struct ext2fs_rb_private *) bitmap->private;
223
224         rb_free_tree(&bp->root);
225         ext2fs_free_mem(&bp);
226         bp = 0;
227 }
228
229 static errcode_t rb_copy_bmap(ext2fs_generic_bitmap src,
230                               ext2fs_generic_bitmap dest)
231 {
232         struct ext2fs_rb_private *src_bp, *dest_bp;
233         struct bmap_rb_extent *src_ext, *dest_ext;
234         struct rb_node *dest_node, *src_node, *dest_last, **n;
235         errcode_t retval = 0;
236
237         retval = rb_alloc_private_data (dest);
238         if (retval)
239                 return retval;
240
241         src_bp = (struct ext2fs_rb_private *) src->private;
242         dest_bp = (struct ext2fs_rb_private *) dest->private;
243         src_bp->rcursor = NULL;
244         dest_bp->rcursor = NULL;
245
246         src_node = ext2fs_rb_first(&src_bp->root);
247         while (src_node) {
248                 src_ext = node_to_extent(src_node);
249                 retval = ext2fs_get_mem(sizeof (struct bmap_rb_extent),
250                                         &dest_ext);
251                 if (retval)
252                         break;
253
254                 memcpy(dest_ext, src_ext, sizeof(struct bmap_rb_extent));
255
256                 dest_node = &dest_ext->node;
257                 n = &dest_bp->root.rb_node;
258
259                 dest_last = NULL;
260                 if (*n) {
261                         dest_last = ext2fs_rb_last(&dest_bp->root);
262                         n = &(dest_last)->rb_right;
263                 }
264
265                 ext2fs_rb_link_node(dest_node, dest_last, n);
266                 ext2fs_rb_insert_color(dest_node, &dest_bp->root);
267
268                 src_node = ext2fs_rb_next(src_node);
269         }
270
271         return retval;
272 }
273
274 static void rb_truncate(__u64 new_max, struct rb_root *root)
275 {
276         struct bmap_rb_extent *ext;
277         struct rb_node *node;
278
279         node = ext2fs_rb_last(root);
280         while (node) {
281                 ext = node_to_extent(node);
282
283                 if ((ext->start + ext->count - 1) <= new_max)
284                         break;
285                 else if (ext->start > new_max) {
286                         ext2fs_rb_erase(node, root);
287                         ext2fs_free_mem(&ext);
288                         node = ext2fs_rb_last(root);
289                         continue;
290                 } else
291                         ext->count = new_max - ext->start + 1;
292         }
293 }
294
295 static errcode_t rb_resize_bmap(ext2fs_generic_bitmap bmap,
296                                 __u64 new_end, __u64 new_real_end)
297 {
298         struct ext2fs_rb_private *bp;
299
300         bp = (struct ext2fs_rb_private *) bmap->private;
301         bp->rcursor = NULL;
302         bp->wcursor = NULL;
303
304         rb_truncate(((new_end < bmap->end) ? new_end : bmap->end) - bmap->start,
305                     &bp->root);
306
307         bmap->end = new_end;
308         bmap->real_end = new_real_end;
309
310         if (bmap->end < bmap->real_end)
311                 rb_insert_extent(bmap->end + 1 - bmap->start,
312                                  bmap->real_end - bmap->end, bp);
313         return 0;
314
315 }
316
317 inline static int
318 rb_test_bit(struct ext2fs_rb_private *bp, __u64 bit)
319 {
320         struct bmap_rb_extent *rcursor, *next_ext = NULL;
321         struct rb_node *parent = NULL, *next;
322         struct rb_node **n = &bp->root.rb_node;
323         struct bmap_rb_extent *ext;
324
325         rcursor = bp->rcursor;
326         if (!rcursor)
327                 goto search_tree;
328
329         if (bit >= rcursor->start && bit < rcursor->start + rcursor->count) {
330 #ifdef ENABLE_BMAP_STATS_OPS
331                 bp->test_hit++;
332 #endif
333                 return 1;
334         }
335
336         next_ext = bp->rcursor_next;
337         if (!next_ext) {
338                 next = ext2fs_rb_next(&rcursor->node);
339                 if (next)
340                         next_ext = node_to_extent(next);
341                 bp->rcursor_next = next_ext;
342         }
343         if (next_ext) {
344                 if ((bit >= rcursor->start + rcursor->count) &&
345                     (bit < next_ext->start)) {
346 #ifdef BMAP_STATS_OPS
347                         bp->test_hit++;
348 #endif
349                         return 0;
350                 }
351         }
352         bp->rcursor = NULL;
353         bp->rcursor_next = NULL;
354
355         rcursor = bp->wcursor;
356         if (!rcursor)
357                 goto search_tree;
358
359         if (bit >= rcursor->start && bit < rcursor->start + rcursor->count)
360                 return 1;
361
362 search_tree:
363
364         while (*n) {
365                 parent = *n;
366                 ext = node_to_extent(parent);
367                 if (bit < ext->start)
368                         n = &(*n)->rb_left;
369                 else if (bit >= (ext->start + ext->count))
370                         n = &(*n)->rb_right;
371                 else {
372                         bp->rcursor = ext;
373                         bp->rcursor_next = NULL;
374                         return 1;
375                 }
376         }
377         return 0;
378 }
379
380 static int rb_insert_extent(__u64 start, __u64 count,
381                             struct ext2fs_rb_private *bp)
382 {
383         struct rb_root *root = &bp->root;
384         struct rb_node *parent = NULL, **n = &root->rb_node;
385         struct rb_node *new_node, *node, *next;
386         struct bmap_rb_extent *new_ext;
387         struct bmap_rb_extent *ext;
388         int retval = 0;
389
390         bp->rcursor_next = NULL;
391         ext = bp->wcursor;
392         if (ext) {
393                 if (start >= ext->start &&
394                     start <= (ext->start + ext->count)) {
395 #ifdef ENABLE_BMAP_STATS_OPS
396                         bp->mark_hit++;
397 #endif
398                         goto got_extent;
399                 }
400         }
401
402         while (*n) {
403                 parent = *n;
404                 ext = node_to_extent(parent);
405
406                 if (start < ext->start) {
407                         n = &(*n)->rb_left;
408                 } else if (start > (ext->start + ext->count)) {
409                         n = &(*n)->rb_right;
410                 } else {
411 got_extent:
412                         if ((start + count) <= (ext->start + ext->count))
413                                 return 1;
414
415                         if ((ext->start + ext->count) == start)
416                                 retval = 0;
417                         else
418                                 retval = 1;
419
420                         count += (start - ext->start);
421                         start = ext->start;
422                         new_ext = ext;
423                         new_node = &ext->node;
424
425                         goto skip_insert;
426                 }
427         }
428
429         rb_get_new_extent(&new_ext, start, count);
430
431         new_node = &new_ext->node;
432         ext2fs_rb_link_node(new_node, parent, n);
433         ext2fs_rb_insert_color(new_node, root);
434         bp->wcursor = new_ext;
435
436         node = ext2fs_rb_prev(new_node);
437         if (node) {
438                 ext = node_to_extent(node);
439                 if ((ext->start + ext->count) == start) {
440                         start = ext->start;
441                         count += ext->count;
442                         ext2fs_rb_erase(node, root);
443                         rb_free_extent(bp, ext);
444                 }
445         }
446
447 skip_insert:
448         /* See if we can merge extent to the right */
449         for (node = ext2fs_rb_next(new_node); node != NULL; node = next) {
450                 next = ext2fs_rb_next(node);
451                 ext = node_to_extent(node);
452
453                 if ((ext->start + ext->count) <= start)
454                         continue;
455
456                 /* No more merging */
457                 if ((start + count) < ext->start)
458                         break;
459
460                 /* ext is embedded in new_ext interval */
461                 if ((start + count) >= (ext->start + ext->count)) {
462                         ext2fs_rb_erase(node, root);
463                         rb_free_extent(bp, ext);
464                         continue;
465                 } else {
466                 /* merge ext with new_ext */
467                         count += ((ext->start + ext->count) -
468                                   (start + count));
469                         ext2fs_rb_erase(node, root);
470                         rb_free_extent(bp, ext);
471                         break;
472                 }
473         }
474
475         new_ext->start = start;
476         new_ext->count = count;
477
478         return retval;
479 }
480
481 static int rb_remove_extent(__u64 start, __u64 count,
482                             struct ext2fs_rb_private *bp)
483 {
484         struct rb_root *root = &bp->root;
485         struct rb_node *parent = NULL, **n = &root->rb_node;
486         struct rb_node *node;
487         struct bmap_rb_extent *ext;
488         __u64 new_start, new_count;
489         int retval = 0;
490
491         if (ext2fs_rb_empty_root(root))
492                 return 0;
493
494         while (*n) {
495                 parent = *n;
496                 ext = node_to_extent(parent);
497                 if (start < ext->start) {
498                         n = &(*n)->rb_left;
499                         continue;
500                 } else if (start >= (ext->start + ext->count)) {
501                         n = &(*n)->rb_right;
502                         continue;
503                 }
504
505                 if ((start > ext->start) &&
506                     (start + count) < (ext->start + ext->count)) {
507                         /* We have to split extent into two */
508                         new_start = start + count;
509                         new_count = (ext->start + ext->count) - new_start;
510
511                         ext->count = start - ext->start;
512
513                         rb_insert_extent(new_start, new_count, bp);
514                         return 1;
515                 }
516
517                 if ((start + count) >= (ext->start + ext->count)) {
518                         ext->count = start - ext->start;
519                         retval = 1;
520                 }
521
522                 if (0 == ext->count) {
523                         parent = ext2fs_rb_next(&ext->node);
524                         ext2fs_rb_erase(&ext->node, root);
525                         rb_free_extent(bp, ext);
526                         break;
527                 }
528
529                 if (start == ext->start) {
530                         ext->start += count;
531                         ext->count -= count;
532                         return 1;
533                 }
534         }
535
536         /* See if we should delete or truncate extent on the right */
537         for (; parent != NULL; parent = node) {
538                 node = ext2fs_rb_next(parent);
539                 ext = node_to_extent(parent);
540                 if ((ext->start + ext->count) <= start)
541                         continue;
542
543                 /* No more extents to be removed/truncated */
544                 if ((start + count) < ext->start)
545                         break;
546
547                 /* The entire extent is within the region to be removed */
548                 if ((start + count) >= (ext->start + ext->count)) {
549                         ext2fs_rb_erase(parent, root);
550                         rb_free_extent(bp, ext);
551                         retval = 1;
552                         continue;
553                 } else {
554                         /* modify the last extent in reigon to be removed */
555                         ext->count -= ((start + count) - ext->start);
556                         ext->start = start + count;
557                         retval = 1;
558                         break;
559                 }
560         }
561
562         return retval;
563 }
564
565 static int rb_mark_bmap(ext2fs_generic_bitmap bitmap, __u64 arg)
566 {
567         struct ext2fs_rb_private *bp;
568         int retval;
569
570         bp = (struct ext2fs_rb_private *) bitmap->private;
571         arg -= bitmap->start;
572
573         retval = rb_insert_extent(arg, 1, bp);
574         check_tree(&bp->root, __func__);
575         return retval;
576 }
577
578 static int rb_unmark_bmap(ext2fs_generic_bitmap bitmap, __u64 arg)
579 {
580         struct ext2fs_rb_private *bp;
581         int retval;
582
583         bp = (struct ext2fs_rb_private *) bitmap->private;
584         arg -= bitmap->start;
585
586         retval = rb_remove_extent(arg, 1, bp);
587         check_tree(&bp->root, __func__);
588
589         return retval;
590 }
591
592 inline
593 static int rb_test_bmap(ext2fs_generic_bitmap bitmap, __u64 arg)
594 {
595         struct ext2fs_rb_private *bp;
596
597         bp = (struct ext2fs_rb_private *) bitmap->private;
598         arg -= bitmap->start;
599
600         return rb_test_bit(bp, arg);
601 }
602
603 static void rb_mark_bmap_extent(ext2fs_generic_bitmap bitmap, __u64 arg,
604                                 unsigned int num)
605 {
606         struct ext2fs_rb_private *bp;
607
608         bp = (struct ext2fs_rb_private *) bitmap->private;
609         arg -= bitmap->start;
610
611         rb_insert_extent(arg, num, bp);
612         check_tree(&bp->root, __func__);
613 }
614
615 static void rb_unmark_bmap_extent(ext2fs_generic_bitmap bitmap, __u64 arg,
616                                   unsigned int num)
617 {
618         struct ext2fs_rb_private *bp;
619
620         bp = (struct ext2fs_rb_private *) bitmap->private;
621         arg -= bitmap->start;
622
623         rb_remove_extent(arg, num, bp);
624         check_tree(&bp->root, __func__);
625 }
626
627 static int rb_test_clear_bmap_extent(ext2fs_generic_bitmap bitmap,
628                                      __u64 start, unsigned int len)
629 {
630         struct rb_node *parent = NULL, **n;
631         struct rb_node *node, *next;
632         struct ext2fs_rb_private *bp;
633         struct bmap_rb_extent *ext;
634         int retval = 1;
635
636         bp = (struct ext2fs_rb_private *) bitmap->private;
637         n = &bp->root.rb_node;
638         start -= bitmap->start;
639
640         if (len == 0 || ext2fs_rb_empty_root(&bp->root))
641                 return 1;
642
643         /*
644          * If we find nothing, we should examine whole extent, but
645          * when we find match, the extent is not clean, thus be return
646          * false.
647          */
648         while (*n) {
649                 parent = *n;
650                 ext = node_to_extent(parent);
651                 if (start < ext->start) {
652                         n = &(*n)->rb_left;
653                 } else if (start >= (ext->start + ext->count)) {
654                         n = &(*n)->rb_right;
655                 } else {
656                         /*
657                          * We found extent int the tree -> extent is not
658                          * clean
659                          */
660                         return 0;
661                 }
662         }
663
664         node = parent;
665         while (node) {
666                 next = ext2fs_rb_next(node);
667                 ext = node_to_extent(node);
668                 node = next;
669
670                 if ((ext->start + ext->count) <= start)
671                         continue;
672
673                 /* No more merging */
674                 if ((start + len) <= ext->start)
675                         break;
676
677                 retval = 0;
678                 break;
679         }
680         return retval;
681 }
682
683 static errcode_t rb_set_bmap_range(ext2fs_generic_bitmap bitmap,
684                                      __u64 start, size_t num, void *in)
685 {
686         struct ext2fs_rb_private *bp;
687         unsigned char *cp = in;
688         size_t i;
689         int first_set = -1;
690
691         bp = (struct ext2fs_rb_private *) bitmap->private;
692
693         for (i = 0; i < num; i++) {
694                 if ((i & 7) == 0) {
695                         unsigned char c = cp[i/8];
696                         if (c == 0xFF) {
697                                 if (first_set == -1)
698                                         first_set = i;
699                                 i += 7;
700                                 continue;
701                         }
702                         if ((c == 0x00) && (first_set == -1)) {
703                                 i += 7;
704                                 continue;
705                         }
706                 }
707                 if (ext2fs_test_bit(i, in)) {
708                         if (first_set == -1)
709                                 first_set = i;
710                         continue;
711                 }
712                 if (first_set == -1)
713                         continue;
714
715                 rb_insert_extent(start + first_set - bitmap->start,
716                                  i - first_set, bp);
717                 check_tree(&bp->root, __func__);
718                 first_set = -1;
719         }
720         if (first_set != -1) {
721                 rb_insert_extent(start + first_set - bitmap->start,
722                                  num - first_set, bp);
723                 check_tree(&bp->root, __func__);
724         }
725
726         return 0;
727 }
728
729 static errcode_t rb_get_bmap_range(ext2fs_generic_bitmap bitmap,
730                                      __u64 start, size_t num, void *out)
731 {
732
733         struct rb_node *parent = NULL, *next, **n;
734         struct ext2fs_rb_private *bp;
735         struct bmap_rb_extent *ext;
736         __u64 count, pos;
737
738         bp = (struct ext2fs_rb_private *) bitmap->private;
739         n = &bp->root.rb_node;
740         start -= bitmap->start;
741
742         if (ext2fs_rb_empty_root(&bp->root))
743                 return 0;
744
745         while (*n) {
746                 parent = *n;
747                 ext = node_to_extent(parent);
748                 if (start < ext->start) {
749                         n = &(*n)->rb_left;
750                 } else if (start >= (ext->start + ext->count)) {
751                         n = &(*n)->rb_right;
752                 } else
753                         break;
754         }
755
756         memset(out, 0, (num + 7) >> 3);
757
758         for (; parent != NULL; parent = next) {
759                 next = ext2fs_rb_next(parent);
760                 ext = node_to_extent(parent);
761
762                 pos = ext->start;
763                 count = ext->count;
764                 if (pos >= start + num)
765                         break;
766                 if (pos < start) {
767                         if (pos + count <  start)
768                                 continue;
769                         count -= start - pos;
770                         pos = start;
771                 }
772                 if (pos + count > start + num)
773                         count = start + num - pos;
774
775                 while (count > 0) {
776                         if ((count >= 8) &&
777                             ((pos - start) % 8) == 0) {
778                                 int nbytes = count >> 3;
779                                 int offset = (pos - start) >> 3;
780
781                                 memset(((char *) out) + offset, 0xFF, nbytes);
782                                 pos += nbytes << 3;
783                                 count -= nbytes << 3;
784                                 continue;
785                         }
786                         ext2fs_fast_set_bit64((pos - start), out);
787                         pos++;
788                         count--;
789                 }
790         }
791         return 0;
792 }
793
794 static void rb_clear_bmap(ext2fs_generic_bitmap bitmap)
795 {
796         struct ext2fs_rb_private *bp;
797
798         bp = (struct ext2fs_rb_private *) bitmap->private;
799
800         rb_free_tree(&bp->root);
801         bp->rcursor = NULL;
802         bp->rcursor_next = NULL;
803         bp->wcursor = NULL;
804         check_tree(&bp->root, __func__);
805 }
806
807 static errcode_t rb_find_first_zero(ext2fs_generic_bitmap bitmap,
808                                    __u64 start, __u64 end, __u64 *out)
809 {
810         struct rb_node *parent = NULL, **n;
811         struct ext2fs_rb_private *bp;
812         struct bmap_rb_extent *ext;
813
814         bp = (struct ext2fs_rb_private *) bitmap->private;
815         n = &bp->root.rb_node;
816         start -= bitmap->start;
817         end -= bitmap->start;
818
819         if (start > end)
820                 return EINVAL;
821
822         if (ext2fs_rb_empty_root(&bp->root))
823                 return ENOENT;
824
825         while (*n) {
826                 parent = *n;
827                 ext = node_to_extent(parent);
828                 if (start < ext->start) {
829                         n = &(*n)->rb_left;
830                 } else if (start >= (ext->start + ext->count)) {
831                         n = &(*n)->rb_right;
832                 } else if (ext->start + ext->count <= end) {
833                         *out = ext->start + ext->count + bitmap->start;
834                         return 0;
835                 } else
836                         return ENOENT;
837         }
838
839         *out = start + bitmap->start;
840         return 0;
841 }
842
843 static errcode_t rb_find_first_set(ext2fs_generic_bitmap bitmap,
844                                    __u64 start, __u64 end, __u64 *out)
845 {
846         struct rb_node *parent = NULL, **n;
847         struct rb_node *node;
848         struct ext2fs_rb_private *bp;
849         struct bmap_rb_extent *ext;
850
851         bp = (struct ext2fs_rb_private *) bitmap->private;
852         n = &bp->root.rb_node;
853         start -= bitmap->start;
854         end -= bitmap->start;
855
856         if (start > end)
857                 return EINVAL;
858
859         if (ext2fs_rb_empty_root(&bp->root))
860                 return ENOENT;
861
862         while (*n) {
863                 parent = *n;
864                 ext = node_to_extent(parent);
865                 if (start < ext->start) {
866                         n = &(*n)->rb_left;
867                 } else if (start >= (ext->start + ext->count)) {
868                         n = &(*n)->rb_right;
869                 } else {
870                         /* The start bit is set */
871                         *out = start + bitmap->start;
872                         return 0;
873                 }
874         }
875
876         node = parent;
877         ext = node_to_extent(node);
878         if (ext->start < start) {
879                 node = ext2fs_rb_next(node);
880                 if (node == NULL)
881                         return ENOENT;
882                 ext = node_to_extent(node);
883         }
884         if (ext->start <= end) {
885                 *out = ext->start + bitmap->start;
886                 return 0;
887         }
888         return ENOENT;
889 }
890
891 #ifdef ENABLE_BMAP_STATS
892 static void rb_print_stats(ext2fs_generic_bitmap bitmap)
893 {
894         struct ext2fs_rb_private *bp;
895         struct rb_node *node = NULL;
896         struct bmap_rb_extent *ext;
897         __u64 count = 0;
898         __u64 max_size = 0;
899         __u64 min_size = ULONG_MAX;
900         __u64 size = 0, avg_size = 0;
901         double eff;
902 #ifdef ENABLE_BMAP_STATS_OPS
903         __u64 mark_all, test_all;
904         double m_hit = 0.0, t_hit = 0.0;
905 #endif
906
907         bp = (struct ext2fs_rb_private *) bitmap->private;
908
909         for (node = ext2fs_rb_first(&bp->root); node != NULL;
910              node = ext2fs_rb_next(node)) {
911                 ext = node_to_extent(node);
912                 count++;
913                 if (ext->count > max_size)
914                         max_size = ext->count;
915                 if (ext->count < min_size)
916                         min_size = ext->count;
917                 size += ext->count;
918         }
919
920         if (count)
921                 avg_size = size / count;
922         if (min_size == ULONG_MAX)
923                 min_size = 0;
924         eff = (double)((count * sizeof(struct bmap_rb_extent)) << 3) /
925               (bitmap->real_end - bitmap->start);
926 #ifdef ENABLE_BMAP_STATS_OPS
927         mark_all = bitmap->stats.mark_count + bitmap->stats.mark_ext_count;
928         test_all = bitmap->stats.test_count + bitmap->stats.test_ext_count;
929         if (mark_all)
930                 m_hit = ((double)bp->mark_hit / mark_all) * 100;
931         if (test_all)
932                 t_hit = ((double)bp->test_hit / test_all) * 100;
933
934         fprintf(stderr, "%16llu cache hits on test (%.2f%%)\n"
935                 "%16llu cache hits on mark (%.2f%%)\n",
936                 bp->test_hit, t_hit, bp->mark_hit, m_hit);
937 #endif
938         fprintf(stderr, "%16llu extents (%llu bytes)\n",
939                 count, ((count * sizeof(struct bmap_rb_extent)) +
940                         sizeof(struct ext2fs_rb_private)));
941         fprintf(stderr, "%16llu bits minimum size\n",
942                 min_size);
943         fprintf(stderr, "%16llu bits maximum size\n"
944                 "%16llu bits average size\n",
945                 max_size, avg_size);
946         fprintf(stderr, "%16llu bits set in bitmap (out of %llu)\n", size,
947                 bitmap->real_end - bitmap->start);
948         fprintf(stderr,
949                 "%16.4lf memory / bitmap bit memory ratio (bitarray = 1)\n",
950                 eff);
951 }
952 #else
953 static void rb_print_stats(ext2fs_generic_bitmap bitmap EXT2FS_ATTR((unused)))
954 {
955 }
956 #endif
957
958 struct ext2_bitmap_ops ext2fs_blkmap64_rbtree = {
959         .type = EXT2FS_BMAP64_RBTREE,
960         .new_bmap = rb_new_bmap,
961         .free_bmap = rb_free_bmap,
962         .copy_bmap = rb_copy_bmap,
963         .resize_bmap = rb_resize_bmap,
964         .mark_bmap = rb_mark_bmap,
965         .unmark_bmap = rb_unmark_bmap,
966         .test_bmap = rb_test_bmap,
967         .test_clear_bmap_extent = rb_test_clear_bmap_extent,
968         .mark_bmap_extent = rb_mark_bmap_extent,
969         .unmark_bmap_extent = rb_unmark_bmap_extent,
970         .set_bmap_range = rb_set_bmap_range,
971         .get_bmap_range = rb_get_bmap_range,
972         .clear_bmap = rb_clear_bmap,
973         .print_stats = rb_print_stats,
974         .find_first_zero = rb_find_first_zero,
975         .find_first_set = rb_find_first_set,
976 };