]> git.ozlabs.org Git - ccan/blobdiff - ccan/tdb2/check.c
tdb2: remove tailer
[ccan] / ccan / tdb2 / check.c
index c2ca95d08bcddfa98444d3d1354987c8168ce19a..5a16dbf42798e0d351b6fe0f5c424e00703936a9 100644 (file)
@@ -45,14 +45,15 @@ static bool check_header(struct tdb_context *tdb)
        if (hdr.hash_test != hash_test) {
                tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
                         "check: hash test %llu should be %llu\n",
-                        hdr.hash_test, hash_test);
+                        (long long)hdr.hash_test,
+                        (long long)hash_test);
                return false;
        }
 
        if (strcmp(hdr.magic_food, TDB_MAGIC_FOOD) != 0) {
                tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
                         "check: bad magic '%.*s'\n",
-                        sizeof(hdr.magic_food), hdr.magic_food);
+                        (unsigned)sizeof(hdr.magic_food), hdr.magic_food);
                return false;
        }
 
@@ -263,7 +264,7 @@ static bool check_hash(struct tdb_context *tdb,
 
        if (!check_hash_tree(tdb, offsetof(struct tdb_header, hashtable),
                             TDB_TOPLEVEL_HASH_BITS-TDB_HASH_GROUP_BITS,
-                            0, 0,  used, num_used, &num_found))
+                            0, 0, used, num_used, &num_found))
                return false;
 
        if (num_found != num_used) {
@@ -367,7 +368,7 @@ static tdb_off_t check_zone(struct tdb_context *tdb, tdb_off_t zone_off,
                            unsigned int *max_zone_bits)
 {
        struct free_zone_header zhdr;
-       tdb_off_t off, hdrlen;
+       tdb_off_t off, hdrlen, end;
        tdb_len_t len;
 
        if (tdb_read_convert(tdb, zone_off, &zhdr, sizeof(zhdr)) == -1)
@@ -390,15 +391,18 @@ static tdb_off_t check_zone(struct tdb_context *tdb, tdb_off_t zone_off,
                return TDB_OFF_ERR;
        }
 
-       /* Zone must be within file! */
-       if (tdb->methods->oob(tdb, zone_off + (1ULL << zhdr.zone_bits), false))
-               return TDB_OFF_ERR;
-
+       /* Zone header must be within file! */
        hdrlen = sizeof(zhdr)
                + (BUCKETS_FOR_ZONE(zhdr.zone_bits) + 1) * sizeof(tdb_off_t);
-       for (off = zone_off + hdrlen;
-            off < zone_off + (1ULL << zhdr.zone_bits);
-            off += len) {
+
+       if (tdb->methods->oob(tdb, zone_off + hdrlen, true))
+               return TDB_OFF_ERR;
+
+       end = zone_off + (1ULL << zhdr.zone_bits);
+       if (end > tdb->map_size)
+               end = tdb->map_size;
+
+       for (off = zone_off + hdrlen; off < end; off += len) {
                union {
                        struct tdb_used_record u;
                        struct tdb_free_record f;
@@ -406,7 +410,8 @@ static tdb_off_t check_zone(struct tdb_context *tdb, tdb_off_t zone_off,
                p = tdb_get(tdb, off, &pad, sizeof(pad));
                if (!p)
                        return TDB_OFF_ERR;
-               if (frec_magic(&p->f) == TDB_FREE_MAGIC) {
+               if (frec_magic(&p->f) == TDB_FREE_MAGIC
+                   || frec_magic(&p->f) == TDB_COALESCING_MAGIC) {
                        if (frec_zone_bits(&p->f) != zhdr.zone_bits) {
                                tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
                                         "tdb_check: Bad free zone bits %u"
@@ -415,9 +420,6 @@ static tdb_off_t check_zone(struct tdb_context *tdb, tdb_off_t zone_off,
                                         (long long)off);
                                return TDB_OFF_ERR;
                        }
-                       /* This record is free! */
-                       if (!append(free, num_free, off))
-                               return TDB_OFF_ERR;
                        len = sizeof(p->u) + p->f.data_len;
                        if (off + len > zone_off + (1ULL << zhdr.zone_bits)) {
                                tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
@@ -426,6 +428,10 @@ static tdb_off_t check_zone(struct tdb_context *tdb, tdb_off_t zone_off,
                                         (long long)len, (long long)off);
                                return TDB_OFF_ERR;
                        }
+                       /* This record is free! */
+                       if (frec_magic(&p->f) == TDB_FREE_MAGIC
+                           && !append(free, num_free, off))
+                               return TDB_OFF_ERR;
                } else {
                        uint64_t klen, dlen, extra;
 
@@ -473,7 +479,7 @@ static tdb_off_t check_zone(struct tdb_context *tdb, tdb_off_t zone_off,
                        }
                }
        }
-       return 1ULL << zhdr.zone_bits;
+       return off - zone_off;
 }
 
 /* FIXME: call check() function. */
@@ -485,7 +491,6 @@ int tdb_check(struct tdb_context *tdb,
        tdb_len_t len;
        size_t num_free = 0, num_used = 0, num_found = 0;
        unsigned max_zone_bits = INITIAL_ZONE_BITS;
-       uint8_t tailer;
 
        if (tdb_allrecord_lock(tdb, F_RDLCK, TDB_LOCK_WAIT, false) != 0)
                return -1;
@@ -500,7 +505,7 @@ int tdb_check(struct tdb_context *tdb,
 
        /* First we do a linear scan, checking all records. */
        for (off = sizeof(struct tdb_header);
-            off < tdb->map_size - 1;
+            off < tdb->map_size;
             off += len) {
                len = check_zone(tdb, off, &used, &num_used, &free, &num_free,
                                 &max_zone_bits);
@@ -508,17 +513,6 @@ int tdb_check(struct tdb_context *tdb,
                        goto fail;
        }
 
-       /* Check tailer. */
-       if (tdb->methods->read(tdb, tdb->map_size - 1, &tailer, 1) == -1)
-               goto fail;
-       if (tailer != max_zone_bits) {
-               tdb->ecode = TDB_ERR_CORRUPT;
-               tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
-                        "tdb_check: Bad tailer value %u vs %u\n", tailer,
-                        max_zone_bits);
-               goto fail;
-       }
-
        /* FIXME: Check key uniqueness? */
        if (!check_hash(tdb, used, num_used))
                goto fail;