Whamcloud - gitweb
e2fsck: allow deleting or zeroing shared blocks
[tools/e2fsprogs.git] / e2fsck / ea_refcount.c
index 6420c72..e66e636 100644 (file)
@@ -1,16 +1,20 @@
 /*
  * ea_refcount.c
- * 
+ *
  * Copyright (C) 2001 Theodore Ts'o.  This file may be
  * redistributed under the terms of the GNU Public License.
  */
 
+#include "config.h"
 #if HAVE_UNISTD_H
 #include <unistd.h>
 #endif
 #include <string.h>
 #include <stdio.h>
 
+#ifdef TEST_PROGRAM
+#undef ENABLE_NLS
+#endif
 #include "e2fsck.h"
 
 /*
@@ -18,7 +22,7 @@
  * follows.  We keep a sorted array of first EA blocks and its
  * reference counts.  Once the refcount has dropped to zero, it is
  * removed from the array to save memory space.  Once the EA block is
- * checked, its bit is set in the block_ea_map bitmap.  
+ * checked, its bit is set in the block_ea_map bitmap.
  */
 struct ea_refcount_el {
        blk_t   ea_blk;
@@ -118,7 +122,7 @@ static struct ea_refcount_el *insert_refcount_el(ext2_refcount_t refcount,
                new_size = refcount->size + 100;
 #ifdef DEBUG
                printf("Reallocating refcount %d entries...\n", new_size);
-#endif 
+#endif
                retval = ext2fs_resize_mem((size_t) refcount->size *
                                           sizeof(struct ea_refcount_el),
                                           (size_t) new_size *
@@ -151,9 +155,7 @@ static struct ea_refcount_el *insert_refcount_el(ext2_refcount_t refcount,
 static struct ea_refcount_el *get_refcount_el(ext2_refcount_t refcount,
                                              blk_t blk, int create)
 {
-       float   range;
        int     low, high, mid;
-       blk_t   lowval, highval;
 
        if (!refcount || !refcount->list)
                return 0;
@@ -170,7 +172,7 @@ retry:
        }
        if (refcount->count == 0)
                return 0;
-       
+
        if (refcount->cursor >= refcount->count)
                refcount->cursor = 0;
        if (blk == refcount->list[refcount->cursor].ea_blk)
@@ -179,26 +181,7 @@ retry:
        printf("Non-cursor get_refcount_el: %u\n", blk);
 #endif
        while (low <= high) {
-#if 0
                mid = (low+high)/2;
-#else
-               if (low == high)
-                       mid = low;
-               else {
-                       /* Interpolate for efficiency */
-                       lowval = refcount->list[low].ea_blk;
-                       highval = refcount->list[high].ea_blk;
-
-                       if (blk < lowval)
-                               range = 0;
-                       else if (blk > highval)
-                               range = 1;
-                       else 
-                               range = ((float) (blk - lowval)) /
-                                       (highval - lowval);
-                       mid = low + ((int) (range * (high-low)));
-               }
-#endif
                if (blk == refcount->list[mid].ea_blk) {
                        refcount->cursor = mid+1;
                        return &refcount->list[mid];
@@ -227,7 +210,7 @@ errcode_t ea_refcount_fetch(ext2_refcount_t refcount, blk_t blk,
                                int *ret)
 {
        struct ea_refcount_el   *el;
-       
+
        el = get_refcount_el(refcount, blk, 0);
        if (!el) {
                *ret = 0;
@@ -298,7 +281,7 @@ blk_t ea_refcount_intr_next(ext2_refcount_t refcount,
                                int *ret)
 {
        struct ea_refcount_el   *list;
-       
+
        while (1) {
                if (refcount->cursor >= refcount->count)
                        return 0;
@@ -320,7 +303,7 @@ errcode_t ea_refcount_validate(ext2_refcount_t refcount, FILE *out)
        errcode_t       ret = 0;
        int             i;
        const char *bad = "bad refcount";
-       
+
        if (refcount->count > refcount->size) {
                fprintf(out, "%s: count > size\n", bad);
                return EXT2_ET_INVALID_ARGUMENT;
@@ -398,8 +381,8 @@ int main(int argc, char **argv)
                        size = bcode_program[i++];
                        retval = ea_refcount_create(size, &refcount);
                        if (retval) {
-                               com_err("ea_refcount_create",
-                                       retval, "");
+                               com_err("ea_refcount_create", retval,
+                                       "while creating size %d", size);
                                exit(1);
                        } else
                                printf("Creating refcount with size %d\n",
@@ -413,35 +396,35 @@ int main(int argc, char **argv)
                case BCODE_STORE:
                        blk = (blk_t) bcode_program[i++];
                        arg = bcode_program[i++];
-                       retval = ea_refcount_store(refcount, blk, arg);
                        printf("Storing blk %u with value %d\n", blk, arg);
+                       retval = ea_refcount_store(refcount, blk, arg);
                        if (retval)
-                               com_err("ea_refcount_store", retval, "");
+                               com_err("ea_refcount_store", retval,
+                                       "while storing blk %u", blk);
                        break;
                case BCODE_FETCH:
                        blk = (blk_t) bcode_program[i++];
                        retval = ea_refcount_fetch(refcount, blk, &arg);
                        if (retval)
-                               com_err("ea_refcount_fetch", retval, "");
+                               com_err("ea_refcount_fetch", retval,
+                                       "while fetching blk %u", blk);
                        else
                                printf("bcode_fetch(%u) returns %d\n",
                                       blk, arg);
                        break;
                case BCODE_INCR:
                        blk = (blk_t) bcode_program[i++];
-                       retval = ea_refcount_increment(refcount, blk,
-                                                          &arg);
+                       retval = ea_refcount_increment(refcount, blk, &arg);
                        if (retval)
                                com_err("ea_refcount_increment", retval,
-                                       "");
+                                       "while incrementing blk %u", blk);
                        else
                                printf("bcode_increment(%u) returns %d\n",
                                       blk, arg);
                        break;
                case BCODE_DECR:
                        blk = (blk_t) bcode_program[i++];
-                       retval = ea_refcount_decrement(refcount, blk,
-                                                          &arg);
+                       retval = ea_refcount_decrement(refcount, blk, &arg);
                        if (retval)
                                com_err("ea_refcount_decrement", retval,
                                        "while decrementing blk %u", blk);
@@ -452,27 +435,25 @@ int main(int argc, char **argv)
                case BCODE_VALIDATE:
                        retval = ea_refcount_validate(refcount, stderr);
                        if (retval)
-                               com_err("ea_refcount_validate",
-                                       retval, "");
+                               com_err("ea_refcount_validate", retval,
+                                       "while validating");
                        else
                                printf("Refcount validation OK.\n");
                        break;
                case BCODE_LIST:
                        ea_refcount_intr_begin(refcount);
                        while (1) {
-                               blk = ea_refcount_intr_next(refcount, 
-                                                               &arg);
+                               blk = ea_refcount_intr_next(refcount, &arg);
                                if (!blk)
                                        break;
-                               printf("\tblk=%u, count=%d\n", blk,
-                                      arg);
+                               printf("\tblk=%u, count=%d\n", blk, arg);
                        }
                        break;
                case BCODE_COLLAPSE:
                        refcount_collapse(refcount);
                        break;
                }
-               
+
        }
 }