Whamcloud - gitweb
Fix typos in code comments and developer docs
[tools/e2fsprogs.git] / lib / ext2fs / blkmap64_rb.c
index 28820cb..6fc245f 100644 (file)
@@ -9,6 +9,7 @@
  * %End-Header%
  */
 
+#include "config.h"
 #include <stdio.h>
 #include <string.h>
 #if HAVE_UNISTD_H
@@ -83,21 +84,21 @@ static void print_tree(struct rb_root *root)
 
 static void check_tree(struct rb_root *root, const char *msg)
 {
-       struct rb_node *new_node, *node, *next;
+       struct rb_node *node;
        struct bmap_rb_extent *ext, *old = NULL;
 
        for (node = ext2fs_rb_first(root); node;
             node = ext2fs_rb_next(node)) {
                ext = node_to_extent(node);
-               if (ext->count <= 0) {
-                       printf("Tree Error: count is crazy\n");
-                       printf("extent: %llu -> %llu (%u)\n", ext->start,
+               if (ext->count == 0) {
+                       printf("Tree Error: count is zero\n");
+                       printf("extent: %llu -> %llu (%llu)\n", ext->start,
                                ext->start + ext->count, ext->count);
                        goto err_out;
                }
-               if (ext->start < 0) {
-                       printf("Tree Error: start is crazy\n");
-                       printf("extent: %llu -> %llu (%u)\n", ext->start,
+               if (ext->start + ext->count < ext->start) {
+                       printf("Tree Error: start or count is crazy\n");
+                       printf("extent: %llu -> %llu (%llu)\n", ext->start,
                                ext->start + ext->count, ext->count);
                        goto err_out;
                }
@@ -105,20 +106,20 @@ static void check_tree(struct rb_root *root, const char *msg)
                if (old) {
                        if (old->start > ext->start) {
                                printf("Tree Error: start is crazy\n");
-                               printf("extent: %llu -> %llu (%u)\n",
+                               printf("extent: %llu -> %llu (%llu)\n",
                                        old->start, old->start + old->count,
                                        old->count);
-                               printf("extent next: %llu -> %llu (%u)\n",
+                               printf("extent next: %llu -> %llu (%llu)\n",
                                        ext->start, ext->start + ext->count,
                                        ext->count);
                                goto err_out;
                        }
                        if ((old->start + old->count) >= ext->start) {
                                printf("Tree Error: extent is crazy\n");
-                               printf("extent: %llu -> %llu (%u)\n",
+                               printf("extent: %llu -> %llu (%llu)\n",
                                        old->start, old->start + old->count,
                                        old->count);
-                               printf("extent next: %llu -> %llu (%u)\n",
+                               printf("extent next: %llu -> %llu (%llu)\n",
                                        ext->start, ext->start + ext->count,
                                        ext->count);
                                goto err_out;
@@ -297,21 +298,19 @@ static errcode_t rb_resize_bmap(ext2fs_generic_bitmap bmap,
 {
        struct ext2fs_rb_private *bp;
 
-       if (new_real_end >= bmap->real_end) {
-               bmap->end = new_end;
-               bmap->real_end = new_real_end;
-               return 0;
-       }
-
        bp = (struct ext2fs_rb_private *) bmap->private;
        bp->rcursor = NULL;
        bp->wcursor = NULL;
 
-       /* truncate tree to new_real_end size */
-       rb_truncate(new_real_end, &bp->root);
+       rb_truncate(((new_end < bmap->end) ? new_end : bmap->end) - bmap->start,
+                   &bp->root);
 
        bmap->end = new_end;
        bmap->real_end = new_real_end;
+
+       if (bmap->end < bmap->real_end)
+               rb_insert_extent(bmap->end + 1 - bmap->start,
+                                bmap->real_end - bmap->end, bp);
        return 0;
 
 }
@@ -490,7 +489,7 @@ static int rb_remove_extent(__u64 start, __u64 count,
        __u64 new_start, new_count;
        int retval = 0;
 
-       if (EXT2FS_RB_EMPTY_ROOT(root))
+       if (ext2fs_rb_empty_root(root))
                return 0;
 
        while (*n) {
@@ -553,7 +552,7 @@ static int rb_remove_extent(__u64 start, __u64 count,
                        retval = 1;
                        continue;
                } else {
-                       /* modify the last extent in reigon to be removed */
+                       /* modify the last extent in region to be removed */
                        ext->count -= ((start + count) - ext->start);
                        ext->start = start + count;
                        retval = 1;
@@ -639,7 +638,7 @@ static int rb_test_clear_bmap_extent(ext2fs_generic_bitmap bitmap,
        n = &bp->root.rb_node;
        start -= bitmap->start;
 
-       if ((len == 0) || EXT2FS_RB_EMPTY_ROOT(&bp->root))
+       if (len == 0 || ext2fs_rb_empty_root(&bp->root))
                return 1;
 
        /*
@@ -735,14 +734,13 @@ static errcode_t rb_get_bmap_range(ext2fs_generic_bitmap bitmap,
        struct rb_node *parent = NULL, *next, **n;
        struct ext2fs_rb_private *bp;
        struct bmap_rb_extent *ext;
-       int count;
-       __u64 pos;
+       __u64 count, pos;
 
        bp = (struct ext2fs_rb_private *) bitmap->private;
        n = &bp->root.rb_node;
        start -= bitmap->start;
 
-       if (EXT2FS_RB_EMPTY_ROOT(&bp->root))
+       if (ext2fs_rb_empty_root(&bp->root))
                return 0;
 
        while (*n) {
@@ -767,9 +765,9 @@ static errcode_t rb_get_bmap_range(ext2fs_generic_bitmap bitmap,
                if (pos >= start + num)
                        break;
                if (pos < start) {
-                       count -= start - pos;
-                       if (count < 0)
+                       if (pos + count <  start)
                                continue;
+                       count -= start - pos;
                        pos = start;
                }
                if (pos + count > start + num)
@@ -811,10 +809,8 @@ static errcode_t rb_find_first_zero(ext2fs_generic_bitmap bitmap,
                                   __u64 start, __u64 end, __u64 *out)
 {
        struct rb_node *parent = NULL, **n;
-       struct rb_node *node, *next;
        struct ext2fs_rb_private *bp;
        struct bmap_rb_extent *ext;
-       int retval = 1;
 
        bp = (struct ext2fs_rb_private *) bitmap->private;
        n = &bp->root.rb_node;
@@ -824,7 +820,7 @@ static errcode_t rb_find_first_zero(ext2fs_generic_bitmap bitmap,
        if (start > end)
                return EINVAL;
 
-       if (EXT2FS_RB_EMPTY_ROOT(&bp->root))
+       if (ext2fs_rb_empty_root(&bp->root))
                return ENOENT;
 
        while (*n) {
@@ -849,10 +845,9 @@ static errcode_t rb_find_first_set(ext2fs_generic_bitmap bitmap,
                                   __u64 start, __u64 end, __u64 *out)
 {
        struct rb_node *parent = NULL, **n;
-       struct rb_node *node, *next;
+       struct rb_node *node;
        struct ext2fs_rb_private *bp;
        struct bmap_rb_extent *ext;
-       int retval = 1;
 
        bp = (struct ext2fs_rb_private *) bitmap->private;
        n = &bp->root.rb_node;
@@ -862,7 +857,7 @@ static errcode_t rb_find_first_set(ext2fs_generic_bitmap bitmap,
        if (start > end)
                return EINVAL;
 
-       if (EXT2FS_RB_EMPTY_ROOT(&bp->root))
+       if (ext2fs_rb_empty_root(&bp->root))
                return ENOENT;
 
        while (*n) {