Whamcloud - gitweb
31fc3934b850327f6562b568a9c6cce444825f18
[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         __u32 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 };
44
45 static int rb_insert_extent(__u64 start, __u64 count,
46                             struct ext2fs_rb_private *);
47 static void rb_get_new_extent(struct bmap_rb_extent **, __u64, __u64);
48
49 /* #define DEBUG_RB */
50
51 #ifdef DEBUG_RB
52 static void print_tree(struct rb_root *root)
53 {
54         struct rb_node *node = NULL;
55         struct bmap_rb_extent *ext;
56
57         printf("\t\t\t=================================\n");
58         node = ext2fs_rb_first(root);
59         for (node = ext2fs_rb_first(root); node != NULL; 
60              node = ext2fs_rb_next(node)) {
61                 ext = ext2fs_rb_entry(node, struct bmap_rb_extent, node);
62                 printf("\t\t\t--> (%llu -> %llu)\n",
63                         ext->start, ext->start + ext->count);
64         }
65         printf("\t\t\t=================================\n");
66 }
67
68 static int check_tree(struct rb_root *root, const char *msg)
69 {
70         struct rb_node *new_node, *node, *next;
71         struct bmap_rb_extent *ext, *old = NULL;
72
73         for (node = ext2fs_rb_first(root); node;
74              node = ext2fs_rb_next(node)) {
75                 ext = ext2fs_rb_entry(node, struct bmap_rb_extent, node);
76                 if (ext->count <= 0) {
77                         printf("Tree Error: count is crazy\n");
78                         printf("extent: %llu -> %llu (%u)\n", ext->start,
79                                 ext->start + ext->count, ext->count);
80                         goto err_out;
81                 }
82                 if (ext->start < 0) {
83                         printf("Tree Error: start is crazy\n");
84                         printf("extent: %llu -> %llu (%u)\n", ext->start,
85                                 ext->start + ext->count, ext->count);
86                         goto err_out;
87                 }
88
89                 if (old) {
90                         if (old->start > ext->start) {
91                                 printf("Tree Error: start is crazy\n");
92                                 printf("extent: %llu -> %llu (%u)\n",
93                                         old->start, old->start + old->count,
94                                         old->count);
95                                 printf("extent next: %llu -> %llu (%u)\n",
96                                         ext->start, ext->start + ext->count,
97                                         ext->count);
98                                 goto err_out;
99                         }
100                         if ((old->start + old->count) >= ext->start) {
101                                 printf("Tree Error: extent is crazy\n");
102                                 printf("extent: %llu -> %llu (%u)\n",
103                                         old->start, old->start + old->count,
104                                         old->count);
105                                 printf("extent next: %llu -> %llu (%u)\n",
106                                         ext->start, ext->start + ext->count,
107                                         ext->count);
108                                 goto err_out;
109                         }
110                 }
111                 old = ext;
112         }
113         return 0;
114
115 err_out:
116         printf("%s\n", msg);
117         print_tree(root);
118         exit(1);
119 }
120 #else
121 #define check_tree(root, msg) 0
122 #define print_tree(root, msg) 0
123 #endif
124
125 static void rb_get_new_extent(struct bmap_rb_extent **ext, __u64 start,
126                              __u64 count)
127 {
128         struct bmap_rb_extent *new_ext;
129         int retval;
130
131         retval = ext2fs_get_mem(sizeof (struct bmap_rb_extent),
132                                 &new_ext);
133         if (retval) {
134                 perror("ext2fs_get_mem");
135                 exit(1);
136         }
137
138         new_ext->start = start;
139         new_ext->count = count;
140         *ext = new_ext;
141 }
142
143 inline
144 static void rb_free_extent(struct ext2fs_rb_private *bp,
145                            struct bmap_rb_extent *ext)
146 {
147         if (*bp->wcursor == ext)
148                 *bp->wcursor = NULL;
149         if (*bp->rcursor == ext)
150                 *bp->rcursor = NULL;
151         ext2fs_free_mem(&ext);
152 }
153
154 static errcode_t rb_alloc_private_data (ext2fs_generic_bitmap bitmap)
155 {
156         struct ext2fs_rb_private *bp;
157         errcode_t       retval;
158
159         retval = ext2fs_get_mem(sizeof (struct ext2fs_rb_private), &bp);
160         if (retval)
161                 return retval;
162
163         bp->root = RB_ROOT;
164         retval = ext2fs_get_mem(sizeof(struct bmap_rb_extent *), &bp->rcursor);
165         if (retval)
166                 return retval;
167         retval = ext2fs_get_mem(sizeof(struct bmap_rb_extent *), &bp->wcursor);
168         if (retval)
169                 return retval;
170         *bp->rcursor = NULL;
171         *bp->wcursor = NULL;
172
173         bitmap->private = (void *) bp;
174         return 0;
175 }
176
177 static errcode_t rb_new_bmap(ext2_filsys fs EXT2FS_ATTR((unused)),
178                              ext2fs_generic_bitmap bitmap)
179 {
180         errcode_t       retval;
181
182         retval = rb_alloc_private_data (bitmap);
183         if (retval)
184                 return retval;
185
186         return 0;
187 }
188
189 static void rb_free_tree(struct rb_root *root)
190 {
191         struct bmap_rb_extent *ext;
192         struct rb_node *node, *next;
193
194         for (node = ext2fs_rb_first(root); node; node = next) {
195                 next = ext2fs_rb_next(node);
196                 ext = ext2fs_rb_entry(node, struct bmap_rb_extent, node);
197                 ext2fs_rb_erase(node, root);
198                 ext2fs_free_mem(&ext);
199         }
200 }
201
202 static void rb_free_bmap(ext2fs_generic_bitmap bitmap)
203 {
204         struct ext2fs_rb_private *bp;
205
206         bp = (struct ext2fs_rb_private *) bitmap->private;
207
208         rb_free_tree(&bp->root);
209         ext2fs_free_mem(&bp->rcursor);
210         ext2fs_free_mem(&bp->wcursor);
211         ext2fs_free_mem(&bp);
212         bp = 0;
213 }
214
215 static errcode_t rb_copy_bmap(ext2fs_generic_bitmap src,
216                               ext2fs_generic_bitmap dest)
217 {
218         struct ext2fs_rb_private *src_bp, *dest_bp;
219         struct bmap_rb_extent *src_ext, *dest_ext;
220         struct rb_node *dest_node, *src_node, *dest_last, **n;
221         errcode_t retval = 0;
222
223         retval = rb_alloc_private_data (dest);
224         if (retval)
225                 return retval;
226
227         src_bp = (struct ext2fs_rb_private *) src->private;
228         dest_bp = (struct ext2fs_rb_private *) dest->private;
229         *src_bp->rcursor = NULL;
230         *dest_bp->rcursor = NULL;
231
232         src_node = ext2fs_rb_first(&src_bp->root);
233         while (src_node) {
234                 src_ext = ext2fs_rb_entry(src_node, struct bmap_rb_extent, node);
235                 retval = ext2fs_get_mem(sizeof (struct bmap_rb_extent),
236                                         &dest_ext);
237                 if (retval)
238                         break;
239
240                 memcpy(dest_ext, src_ext, sizeof(struct bmap_rb_extent));
241
242                 dest_node = &dest_ext->node;
243                 n = &dest_bp->root.rb_node;
244
245                 dest_last = NULL;
246                 if (*n) {
247                         dest_last = ext2fs_rb_last(&dest_bp->root);
248                         n = &(dest_last)->rb_right;
249                 }
250
251                 ext2fs_rb_link_node(dest_node, dest_last, n);
252                 ext2fs_rb_insert_color(dest_node, &dest_bp->root);
253
254                 src_node = ext2fs_rb_next(src_node);
255         }
256
257         return retval;
258 }
259
260 static void rb_truncate(__u64 new_max, struct rb_root *root)
261 {
262         struct bmap_rb_extent *ext;
263         struct rb_node *node;
264
265         node = ext2fs_rb_last(root);
266         while (node) {
267                 ext = ext2fs_rb_entry(node, struct bmap_rb_extent, node);
268
269                 if ((ext->start + ext->count - 1) <= new_max)
270                         break;
271                 else if (ext->start > new_max) {
272                         ext2fs_rb_erase(node, root);
273                         ext2fs_free_mem(&ext);
274                         node = ext2fs_rb_last(root);
275                         continue;
276                 } else
277                         ext->count = new_max - ext->start + 1;
278         }
279 }
280
281 static errcode_t rb_resize_bmap(ext2fs_generic_bitmap bmap,
282                                 __u64 new_end, __u64 new_real_end)
283 {
284         struct ext2fs_rb_private *bp;
285
286         if (new_real_end >= bmap->real_end) {
287                 bmap->end = new_end;
288                 bmap->real_end = new_real_end;
289                 return 0;
290         }
291
292         bp = (struct ext2fs_rb_private *) bmap->private;
293         *bp->rcursor = NULL;
294         *bp->wcursor = NULL;
295
296         /* truncate tree to new_real_end size */
297         rb_truncate(new_real_end, &bp->root);
298
299         bmap->end = new_end;
300         bmap->real_end = new_real_end;
301         return 0;
302
303 }
304
305 inline static int
306 rb_test_bit(struct ext2fs_rb_private *bp, __u64 bit)
307 {
308         struct bmap_rb_extent *rcursor;
309         struct rb_node *parent = NULL;
310         struct rb_node **n = &bp->root.rb_node;
311         struct bmap_rb_extent *ext;
312         int i=0;
313
314         rcursor = *bp->rcursor;
315         if (!rcursor)
316                 goto search_tree;
317
318         if (bit >= rcursor->start && bit < rcursor->start + rcursor->count)
319                 return 1;
320
321         rcursor = *bp->wcursor;
322         if (!rcursor)
323                 goto search_tree;
324
325         if (bit >= rcursor->start && bit < rcursor->start + rcursor->count)
326                 return 1;
327
328 search_tree:
329
330         while (*n) {
331                 parent = *n;
332                 ext = ext2fs_rb_entry(parent, struct bmap_rb_extent, node);
333                 if (bit < ext->start)
334                         n = &(*n)->rb_left;
335                 else if (bit >= (ext->start + ext->count))
336                         n = &(*n)->rb_right;
337                 else {
338                         *bp->rcursor = ext;
339                         return 1;
340                 }
341         }
342         return 0;
343 }
344
345 static int rb_insert_extent(__u64 start, __u64 count,
346                             struct ext2fs_rb_private *bp)
347 {
348         struct rb_root *root = &bp->root;
349         struct rb_node *parent = NULL, **n = &root->rb_node;
350         struct rb_node *new_node, *node, *next;
351         struct bmap_rb_extent *new_ext;
352         struct bmap_rb_extent *ext;
353         int retval = 0;
354
355         ext = *bp->wcursor;
356         if (ext) {
357                 if (start >= ext->start &&
358                     start <= (ext->start + ext->count))
359                         goto got_extent;
360         }
361
362         while (*n) {
363                 parent = *n;
364                 ext = ext2fs_rb_entry(parent, struct bmap_rb_extent, node);
365
366                 if (start < ext->start) {
367                         n = &(*n)->rb_left;
368                 } else if (start > (ext->start + ext->count)) {
369                         n = &(*n)->rb_right;
370                 } else {
371 got_extent:
372                         if ((start + count) <= (ext->start + ext->count))
373                                 return 1;
374
375                         if ((ext->start + ext->count) == start)
376                                 retval = 0;
377                         else
378                                 retval = 1;
379
380                         count += (start - ext->start);
381                         start = ext->start;
382                         new_ext = ext;
383                         new_node = &ext->node;
384
385                         goto skip_insert;
386                 }
387         }
388
389         rb_get_new_extent(&new_ext, start, count);
390
391         new_node = &new_ext->node;
392         ext2fs_rb_link_node(new_node, parent, n);
393         ext2fs_rb_insert_color(new_node, root);
394         *bp->wcursor = new_ext;
395
396         node = ext2fs_rb_prev(new_node);
397         if (node) {
398                 ext = ext2fs_rb_entry(node, struct bmap_rb_extent, node);
399                 if ((ext->start + ext->count) == start) {
400                         start = ext->start;
401                         count += ext->count;
402                         ext2fs_rb_erase(node, root);
403                         rb_free_extent(bp, ext);
404                 }
405         }
406
407 skip_insert:
408         /* See if we can merge extent to the right */
409         for (node = ext2fs_rb_next(new_node); node != NULL; node = next) {
410                 next = ext2fs_rb_next(node);
411                 ext = ext2fs_rb_entry(node, struct bmap_rb_extent, node);
412
413                 if ((ext->start + ext->count) <= start)
414                         continue;
415
416                 /* No more merging */
417                 if ((start + count) < ext->start)
418                         break;
419
420                 /* ext is embedded in new_ext interval */
421                 if ((start + count) >= (ext->start + ext->count)) {
422                         ext2fs_rb_erase(node, root);
423                         rb_free_extent(bp, ext);
424                         continue;
425                 } else {
426                 /* merge ext with new_ext */
427                         count += ((ext->start + ext->count) -
428                                   (start + count));
429                         ext2fs_rb_erase(node, root);
430                         rb_free_extent(bp, ext);
431                         break;
432                 }
433         }
434
435         new_ext->start = start;
436         new_ext->count = count;
437
438         return retval;
439 }
440
441 static int rb_remove_extent(__u64 start, __u64 count,
442                             struct ext2fs_rb_private *bp)
443 {
444         struct rb_root *root = &bp->root;
445         struct rb_node *parent = NULL, **n = &root->rb_node;
446         struct rb_node *node;
447         struct bmap_rb_extent *ext;
448         __u64 new_start, new_count;
449         int retval = 0;
450
451         if (EXT2FS_RB_EMPTY_ROOT(root))
452                 return 0;
453
454         while (*n) {
455                 parent = *n;
456                 ext = ext2fs_rb_entry(parent, struct bmap_rb_extent, node);
457                 if (start < ext->start) {
458                         n = &(*n)->rb_left;
459                         continue;
460                 } else if (start >= (ext->start + ext->count)) {
461                         n = &(*n)->rb_right;
462                         continue;
463                 }
464
465                 if ((start > ext->start) &&
466                     (start + count) < (ext->start + ext->count)) {
467                         /* We have to split extent into two */
468                         new_start = start + count;
469                         new_count = (ext->start + ext->count) - new_start;
470
471                         ext->count = start - ext->start;
472
473                         rb_insert_extent(new_start, new_count, bp);
474                         return 1;
475                 }
476
477                 if ((start + count) >= (ext->start + ext->count)) {
478                         ext->count = start - ext->start;
479                         retval = 1;
480                 }
481
482                 if (0 == ext->count) {
483                         parent = ext2fs_rb_next(&ext->node);
484                         ext2fs_rb_erase(&ext->node, root);
485                         rb_free_extent(bp, ext);
486                         break;
487                 }
488
489                 if (start == ext->start) {
490                         ext->start += count;
491                         ext->count -= count;
492                         return 1;
493                 }
494         }
495
496         /* See if we should delete or truncate extent on the right */
497         for (; parent != NULL; parent = node) {
498                 node = ext2fs_rb_next(parent);
499                 ext = ext2fs_rb_entry(parent, struct bmap_rb_extent, node);
500                 if ((ext->start + ext->count) <= start)
501                         continue;
502
503                 /* No more extents to be removed/truncated */
504                 if ((start + count) < ext->start)
505                         break;
506
507                 /* The entire extent is within the region to be removed */
508                 if ((start + count) >= (ext->start + ext->count)) {
509                         ext2fs_rb_erase(parent, root);
510                         rb_free_extent(bp, ext);
511                         retval = 1;
512                         continue;
513                 } else {
514                         /* modify the last extent in reigon to be removed */
515                         ext->count -= ((start + count) - ext->start);
516                         ext->start = start + count;
517                         retval = 1;
518                         break;
519                 }
520         }
521
522         return retval;
523 }
524
525 static int rb_mark_bmap(ext2fs_generic_bitmap bitmap, __u64 arg)
526 {
527         struct ext2fs_rb_private *bp;
528         int i;
529
530
531         bp = (struct ext2fs_rb_private *) bitmap->private;
532         arg -= bitmap->start;
533
534         return rb_insert_extent(arg, 1, bp);
535 }
536
537 static int rb_unmark_bmap(ext2fs_generic_bitmap bitmap, __u64 arg)
538 {
539         struct ext2fs_rb_private *bp;
540         int retval;
541
542         bp = (struct ext2fs_rb_private *) bitmap->private;
543         arg -= bitmap->start;
544
545         retval = rb_remove_extent(arg, 1, bp);
546         check_tree(&bp->root, __func__);
547
548         return retval;
549 }
550
551 inline
552 static int rb_test_bmap(ext2fs_generic_bitmap bitmap, __u64 arg)
553 {
554         struct ext2fs_rb_private *bp;
555
556         bp = (struct ext2fs_rb_private *) bitmap->private;
557         arg -= bitmap->start;
558
559         return rb_test_bit(bp, arg);
560 }
561
562 static void rb_mark_bmap_extent(ext2fs_generic_bitmap bitmap, __u64 arg,
563                                 unsigned int num)
564 {
565         struct ext2fs_rb_private *bp;
566         struct bmap_rb_extent *new_ext;
567
568         bp = (struct ext2fs_rb_private *) bitmap->private;
569         arg -= bitmap->start;
570
571         rb_insert_extent(arg, num, bp);
572 }
573
574 static void rb_unmark_bmap_extent(ext2fs_generic_bitmap bitmap, __u64 arg,
575                                   unsigned int num)
576 {
577         struct ext2fs_rb_private *bp;
578         int ret;
579
580         bp = (struct ext2fs_rb_private *) bitmap->private;
581         arg -= bitmap->start;
582
583         rb_remove_extent(arg, num, bp);
584         check_tree(&bp->root, __func__);
585 }
586
587 static int rb_test_clear_bmap_extent(ext2fs_generic_bitmap bitmap,
588                                      __u64 start, unsigned int len)
589 {
590         struct rb_node *parent = NULL, **n;
591         struct rb_node *node, *next;
592         struct ext2fs_rb_private *bp;
593         struct bmap_rb_extent *ext;
594         int retval = 1;
595
596         bp = (struct ext2fs_rb_private *) bitmap->private;
597         n = &bp->root.rb_node;
598         start -= bitmap->start;
599
600         if ((len == 0) || EXT2FS_RB_EMPTY_ROOT(&bp->root))
601                 return 1;
602
603         /*
604          * If we find nothing, we should examine whole extent, but
605          * when we find match, the extent is not clean, thus be return
606          * false.
607          */
608         while (*n) {
609                 parent = *n;
610                 ext = ext2fs_rb_entry(parent, struct bmap_rb_extent, node);
611                 if (start < ext->start) {
612                         n = &(*n)->rb_left;
613                 } else if (start >= (ext->start + ext->count)) {
614                         n = &(*n)->rb_right;
615                 } else {
616                         /*
617                          * We found extent int the tree -> extent is not
618                          * clean
619                          */
620                         return 0;
621                 }
622         }
623
624         node = parent;
625         while (node) {
626                 next = ext2fs_rb_next(node);
627                 ext = ext2fs_rb_entry(node, struct bmap_rb_extent, node);
628                 node = next;
629
630                 if ((ext->start + ext->count) <= start)
631                         continue;
632
633                 /* No more merging */
634                 if ((start + len) <= ext->start)
635                         break;
636
637                 retval = 0;
638                 break;
639         }
640         return retval;
641 }
642
643 static errcode_t rb_set_bmap_range(ext2fs_generic_bitmap bitmap,
644                                      __u64 start, size_t num, void *in)
645 {
646         struct ext2fs_rb_private *bp;
647         size_t i;
648         int ret;
649
650         bp = (struct ext2fs_rb_private *) bitmap->private;
651
652         for (i = 0; i < num; i++) {
653                 ret = ext2fs_test_bit(i, in);
654                 if (ret)
655                         rb_insert_extent(start + i - bitmap->start, 1, bp);
656         }
657
658         return 0;
659 }
660
661 static errcode_t rb_get_bmap_range(ext2fs_generic_bitmap bitmap,
662                                      __u64 start, size_t num, void *out)
663 {
664
665         struct rb_node *parent = NULL, *next, **n;
666         struct ext2fs_rb_private *bp;
667         struct bmap_rb_extent *ext;
668         __u64 pos;
669
670         bp = (struct ext2fs_rb_private *) bitmap->private;
671         n = &bp->root.rb_node;
672         start -= bitmap->start;
673
674         if (EXT2FS_RB_EMPTY_ROOT(&bp->root))
675                 return 0;
676
677         while (*n) {
678                 parent = *n;
679                 ext = ext2fs_rb_entry(parent, struct bmap_rb_extent, node);
680                 if (start < ext->start) {
681                         n = &(*n)->rb_left;
682                 } else if (start >= (ext->start + ext->count)) {
683                         n = &(*n)->rb_right;
684                 } else
685                         break;
686         }
687
688         pos = start;
689         for (; parent != NULL; parent = next) {
690                 next = ext2fs_rb_next(parent);
691                 ext = ext2fs_rb_entry(parent, struct bmap_rb_extent, node);
692
693                 while (((pos - start) < num) &&
694                         (pos < ext->start)) {
695                         ext2fs_fast_clear_bit64((pos - start), out);
696                         pos++;
697                 }
698
699                 if ((pos - start) >= num)
700                         return 0;
701
702                 while (((pos - start) < num) &&
703                         (pos < (ext->start + ext->count))) {
704                         ext2fs_fast_set_bit64((pos - start), out);
705                         pos++;
706                 }
707         }
708
709         while ((pos - start) < num) {
710                 ext2fs_fast_clear_bit64((pos - start), out);
711                 pos++;
712         }
713
714         return 0;
715 }
716
717 static void rb_clear_bmap(ext2fs_generic_bitmap bitmap)
718 {
719         struct ext2fs_rb_private *bp;
720
721         bp = (struct ext2fs_rb_private *) bitmap->private;
722
723         rb_free_tree(&bp->root);
724         *bp->rcursor = NULL;
725         *bp->wcursor = NULL;
726 }
727
728 struct ext2_bitmap_ops ext2fs_blkmap64_rbtree = {
729         .type = EXT2FS_BMAP64_RBTREE,
730         .new_bmap = rb_new_bmap,
731         .free_bmap = rb_free_bmap,
732         .copy_bmap = rb_copy_bmap,
733         .resize_bmap = rb_resize_bmap,
734         .mark_bmap = rb_mark_bmap,
735         .unmark_bmap = rb_unmark_bmap,
736         .test_bmap = rb_test_bmap,
737         .test_clear_bmap_extent = rb_test_clear_bmap_extent,
738         .mark_bmap_extent = rb_mark_bmap_extent,
739         .unmark_bmap_extent = rb_unmark_bmap_extent,
740         .set_bmap_range = rb_set_bmap_range,
741         .get_bmap_range = rb_get_bmap_range,
742         .clear_bmap = rb_clear_bmap,
743 };