]> git.ozlabs.org Git - ccan/commitdiff
tdb2: rework check.c internal functions to return enum TDB_ERROR.
authorRusty Russell <rusty@rustcorp.com.au>
Tue, 1 Mar 2011 12:49:19 +0000 (23:19 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Tue, 1 Mar 2011 12:49:19 +0000 (23:19 +1030)
Of course, we leave the API the same, but percolate error codes all the
way back to tdb_check().

ccan/tdb2/check.c
ccan/tdb2/private.h
ccan/tdb2/summary.c

index d4f1d343bf0268546aca47d72158bd6bab3b83eb..4f340a2e15e7faaa7112a90291fa2945faea510b 100644 (file)
@@ -30,7 +30,7 @@ static bool append(tdb_off_t **arr, size_t *num, tdb_off_t off)
        return true;
 }
 
-static bool check_header(struct tdb_context *tdb, tdb_off_t *recovery)
+static enum TDB_ERROR check_header(struct tdb_context *tdb, tdb_off_t *recovery)
 {
        uint64_t hash_test;
        struct tdb_header hdr;
@@ -38,8 +38,7 @@ static bool check_header(struct tdb_context *tdb, tdb_off_t *recovery)
 
        ecode = tdb_read_convert(tdb, 0, &hdr, sizeof(hdr));
        if (ecode != TDB_SUCCESS) {
-               tdb->ecode = ecode;
-               return false;
+               return ecode;
        }
        /* magic food should not be converted, so convert back. */
        tdb_convert(tdb, hdr.magic_food, sizeof(hdr.magic_food));
@@ -47,115 +46,111 @@ static bool check_header(struct tdb_context *tdb, tdb_off_t *recovery)
        hash_test = TDB_HASH_MAGIC;
        hash_test = tdb_hash(tdb, &hash_test, sizeof(hash_test));
        if (hdr.hash_test != hash_test) {
-               tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
-                          "check: hash test %llu should be %llu",
-                          (long long)hdr.hash_test,
-                          (long long)hash_test);
-               return false;
+               return tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
+                                 "check: hash test %llu should be %llu",
+                                 (long long)hdr.hash_test,
+                                 (long long)hash_test);
        }
 
        if (strcmp(hdr.magic_food, TDB_MAGIC_FOOD) != 0) {
-               tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
-                          "check: bad magic '%.*s'",
-                          (unsigned)sizeof(hdr.magic_food), hdr.magic_food);
-               return false;
+               return tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
+                                 "check: bad magic '%.*s'",
+                                 (unsigned)sizeof(hdr.magic_food),
+                                 hdr.magic_food);
        }
 
        *recovery = hdr.recovery;
        if (*recovery) {
                if (*recovery < sizeof(hdr) || *recovery > tdb->map_size) {
-                       tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
-                                "tdb_check: invalid recovery offset %zu",
-                                (size_t)*recovery);
-                       return false;
+                       return tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
+                                         "tdb_check:"
+                                         " invalid recovery offset %zu",
+                                         (size_t)*recovery);
                }
        }
 
        /* Don't check reserved: they *can* be used later. */
-       return true;
+       return TDB_SUCCESS;
 }
 
-static bool check_hash_tree(struct tdb_context *tdb,
-                           tdb_off_t off, unsigned int group_bits,
-                           uint64_t hprefix,
-                           unsigned hprefix_bits,
-                           tdb_off_t used[],
-                           size_t num_used,
-                           size_t *num_found,
-                           int (*check)(TDB_DATA, TDB_DATA, void *),
-                           void *private_data);
-
-static bool check_hash_chain(struct tdb_context *tdb,
-                            tdb_off_t off,
-                            uint64_t hash,
-                            tdb_off_t used[],
-                            size_t num_used,
-                            size_t *num_found,
-                            int (*check)(TDB_DATA, TDB_DATA, void *),
-                            void *private_data)
+static enum TDB_ERROR check_hash_tree(struct tdb_context *tdb,
+                                     tdb_off_t off, unsigned int group_bits,
+                                     uint64_t hprefix,
+                                     unsigned hprefix_bits,
+                                     tdb_off_t used[],
+                                     size_t num_used,
+                                     size_t *num_found,
+                                     int (*check)(TDB_DATA, TDB_DATA, void *),
+                                     void *private_data);
+
+static enum TDB_ERROR check_hash_chain(struct tdb_context *tdb,
+                                      tdb_off_t off,
+                                      uint64_t hash,
+                                      tdb_off_t used[],
+                                      size_t num_used,
+                                      size_t *num_found,
+                                      int (*check)(TDB_DATA, TDB_DATA, void *),
+                                      void *private_data)
 {
        struct tdb_used_record rec;
        enum TDB_ERROR ecode;
 
        ecode = tdb_read_convert(tdb, off, &rec, sizeof(rec));
        if (ecode != TDB_SUCCESS) {
-               tdb->ecode = ecode;
-               return false;
+               return ecode;
        }
 
        if (rec_magic(&rec) != TDB_CHAIN_MAGIC) {
-               tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
-                          "tdb_check: Bad hash chain magic %llu",
-                          (long long)rec_magic(&rec));
-               return false;
+               return tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
+                                 "tdb_check: Bad hash chain magic %llu",
+                                 (long long)rec_magic(&rec));
        }
 
        if (rec_data_length(&rec) != sizeof(struct tdb_chain)) {
-               tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
-                          "tdb_check: Bad hash chain length %llu vs %zu",
-                          (long long)rec_data_length(&rec),
-                          sizeof(struct tdb_chain));
-               return false;
+               return tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
+                                 "tdb_check:"
+                                 " Bad hash chain length %llu vs %zu",
+                                 (long long)rec_data_length(&rec),
+                                 sizeof(struct tdb_chain));
        }
        if (rec_key_length(&rec) != 0) {
-               tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
-                        "tdb_check: Bad hash chain key length %llu",
-                        (long long)rec_key_length(&rec));
-               return false;
+               return tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
+                                 "tdb_check: Bad hash chain key length %llu",
+                                 (long long)rec_key_length(&rec));
        }
        if (rec_hash(&rec) != 0) {
-               tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
-                        "tdb_check: Bad hash chain hash value %llu",
-                        (long long)rec_hash(&rec));
-               return false;
+               return tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
+                                 "tdb_check: Bad hash chain hash value %llu",
+                                 (long long)rec_hash(&rec));
        }
 
        off += sizeof(rec);
-       if (!check_hash_tree(tdb, off, 0, hash, 64,
-                            used, num_used, num_found, check, private_data))
+       ecode = check_hash_tree(tdb, off, 0, hash, 64,
+                               used, num_used, num_found, check, private_data);
+       if (ecode != TDB_SUCCESS) {
                return false;
+       }
 
        off = tdb_read_off(tdb, off + offsetof(struct tdb_chain, next));
        if (TDB_OFF_IS_ERR(off)) {
-               tdb->ecode = off;
-               return false;
+               return off;
        }
        if (off == 0)
-               return true;
+               return TDB_SUCCESS;
        (*num_found)++;
        return check_hash_chain(tdb, off, hash, used, num_used, num_found,
                                check, private_data);
 }
 
-static bool check_hash_record(struct tdb_context *tdb,
-                             tdb_off_t off,
-                             uint64_t hprefix,
-                             unsigned hprefix_bits,
-                             tdb_off_t used[],
-                             size_t num_used,
-                             size_t *num_found,
-                             int (*check)(TDB_DATA, TDB_DATA, void *),
-                             void *private_data)
+static enum TDB_ERROR check_hash_record(struct tdb_context *tdb,
+                                       tdb_off_t off,
+                                       uint64_t hprefix,
+                                       unsigned hprefix_bits,
+                                       tdb_off_t used[],
+                                       size_t num_used,
+                                       size_t *num_found,
+                                       int (*check)(TDB_DATA, TDB_DATA, void*),
+                                       void *private_data)
 {
        struct tdb_used_record rec;
        enum TDB_ERROR ecode;
@@ -166,36 +161,32 @@ static bool check_hash_record(struct tdb_context *tdb,
 
        ecode = tdb_read_convert(tdb, off, &rec, sizeof(rec));
        if (ecode != TDB_SUCCESS) {
-               tdb->ecode = ecode;
-               return false;
+               return ecode;
        }
 
        if (rec_magic(&rec) != TDB_HTABLE_MAGIC) {
-               tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
-                          "tdb_check: Bad hash table magic %llu",
-                          (long long)rec_magic(&rec));
-               return false;
+               return tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
+                                 "tdb_check: Bad hash table magic %llu",
+                                 (long long)rec_magic(&rec));
        }
        if (rec_data_length(&rec)
            != sizeof(tdb_off_t) << TDB_SUBLEVEL_HASH_BITS) {
-               tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
-                          "tdb_check: Bad hash table length %llu vs %llu",
-                          (long long)rec_data_length(&rec),
-                          (long long)sizeof(tdb_off_t)
-                          << TDB_SUBLEVEL_HASH_BITS);
-               return false;
+               return tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
+                                 "tdb_check:"
+                                 " Bad hash table length %llu vs %llu",
+                                 (long long)rec_data_length(&rec),
+                                 (long long)sizeof(tdb_off_t)
+                                 << TDB_SUBLEVEL_HASH_BITS);
        }
        if (rec_key_length(&rec) != 0) {
-               tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
-                        "tdb_check: Bad hash table key length %llu",
-                        (long long)rec_key_length(&rec));
-               return false;
+               return tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
+                                 "tdb_check: Bad hash table key length %llu",
+                                 (long long)rec_key_length(&rec));
        }
        if (rec_hash(&rec) != 0) {
-               tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
-                        "tdb_check: Bad hash table hash value %llu",
-                        (long long)rec_hash(&rec));
-               return false;
+               return tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
+                                 "tdb_check: Bad hash table hash value %llu",
+                                 (long long)rec_hash(&rec));
        }
 
        off += sizeof(rec);
@@ -220,15 +211,15 @@ static uint64_t get_bits(uint64_t h, unsigned num, unsigned *used)
        return (h >> (64 - *used)) & ((1U << num) - 1);
 }
 
-static bool check_hash_tree(struct tdb_context *tdb,
-                           tdb_off_t off, unsigned int group_bits,
-                           uint64_t hprefix,
-                           unsigned hprefix_bits,
-                           tdb_off_t used[],
-                           size_t num_used,
-                           size_t *num_found,
-                           int (*check)(TDB_DATA, TDB_DATA, void *),
-                           void *private_data)
+static enum TDB_ERROR check_hash_tree(struct tdb_context *tdb,
+                                     tdb_off_t off, unsigned int group_bits,
+                                     uint64_t hprefix,
+                                     unsigned hprefix_bits,
+                                     tdb_off_t used[],
+                                     size_t num_used,
+                                     size_t *num_found,
+                                     int (*check)(TDB_DATA, TDB_DATA, void *),
+                                     void *private_data)
 {
        unsigned int g, b;
        const tdb_off_t *hash;
@@ -240,8 +231,7 @@ static bool check_hash_tree(struct tdb_context *tdb,
                               << (group_bits + TDB_HASH_GROUP_BITS),
                               true);
        if (TDB_PTR_IS_ERR(hash)) {
-               tdb->ecode = TDB_PTR_ERR(hash);
-               return false;
+               return TDB_PTR_ERR(hash);
        }
 
        for (g = 0; g < (1 << group_bits); g++) {
@@ -256,10 +246,11 @@ static bool check_hash_tree(struct tdb_context *tdb,
                        off = group[b] & TDB_OFF_MASK;
                        p = asearch(&off, used, num_used, off_cmp);
                        if (!p) {
-                               tdb_logerr(tdb, TDB_ERR_CORRUPT,
-                                          TDB_LOG_ERROR,
-                                          "tdb_check: Invalid offset %llu "
-                                          "in hash", (long long)off);
+                               ecode = tdb_logerr(tdb, TDB_ERR_CORRUPT,
+                                                  TDB_LOG_ERROR,
+                                                  "tdb_check: Invalid offset"
+                                                  " %llu in hash",
+                                                  (long long)off);
                                goto fail;
                        }
                        /* Mark it invalid. */
@@ -269,7 +260,8 @@ static bool check_hash_tree(struct tdb_context *tdb,
                        if (hprefix_bits == 64) {
                                /* Chained entries are unordered. */
                                if (is_subhash(group[b])) {
-                                       tdb_logerr(tdb, TDB_ERR_CORRUPT,
+                                       ecode = TDB_ERR_CORRUPT;
+                                       tdb_logerr(tdb, ecode,
                                                   TDB_LOG_ERROR,
                                                   "tdb_check: Invalid chain"
                                                   " entry subhash");
@@ -277,7 +269,8 @@ static bool check_hash_tree(struct tdb_context *tdb,
                                }
                                h = hash_record(tdb, off);
                                if (h != hprefix) {
-                                       tdb_logerr(tdb, TDB_ERR_CORRUPT,
+                                       ecode = TDB_ERR_CORRUPT;
+                                       tdb_logerr(tdb, ecode,
                                                   TDB_LOG_ERROR,
                                                   "check: bad hash chain"
                                                   " placement"
@@ -289,7 +282,6 @@ static bool check_hash_tree(struct tdb_context *tdb,
                                ecode = tdb_read_convert(tdb, off, &rec,
                                                         sizeof(rec));
                                if (ecode != TDB_SUCCESS) {
-                                       tdb->ecode = ecode;
                                        goto fail;
                                }
                                goto check;
@@ -301,15 +293,17 @@ static bool check_hash_tree(struct tdb_context *tdb,
                                     << (group_bits + TDB_HASH_GROUP_BITS))
                                        + g * (1 << TDB_HASH_GROUP_BITS) + b;
 
-                               if (!check_hash_record(tdb,
+                               ecode = check_hash_record(tdb,
                                               group[b] & TDB_OFF_MASK,
                                               subprefix,
                                               hprefix_bits
                                                       + group_bits
                                                       + TDB_HASH_GROUP_BITS,
                                               used, num_used, num_found,
-                                              check, private_data))
+                                              check, private_data);
+                               if (ecode != TDB_SUCCESS) {
                                        goto fail;
+                               }
                                continue;
                        }
                        /* A normal entry */
@@ -319,20 +313,22 @@ static bool check_hash_tree(struct tdb_context *tdb,
                        used_bits = 0;
                        if (get_bits(h, hprefix_bits, &used_bits) != hprefix
                            && hprefix_bits) {
-                               tdb_logerr(tdb, TDB_ERR_CORRUPT,
-                                          TDB_LOG_ERROR,
-                                          "check: bad hash placement"
-                                          " 0x%llx vs 0x%llx",
-                                        (long long)h, (long long)hprefix);
+                               ecode = tdb_logerr(tdb, TDB_ERR_CORRUPT,
+                                                  TDB_LOG_ERROR,
+                                                  "check: bad hash placement"
+                                                  " 0x%llx vs 0x%llx",
+                                                  (long long)h,
+                                                  (long long)hprefix);
                                goto fail;
                        }
 
                        /* Does it belong in this group? */
                        if (get_bits(h, group_bits, &used_bits) != g) {
-                               tdb_logerr(tdb, TDB_ERR_CORRUPT,
-                                          TDB_LOG_ERROR,
-                                          "check: bad group %llu vs %u",
-                                          (long long)h, g);
+                               ecode = tdb_logerr(tdb, TDB_ERR_CORRUPT,
+                                                  TDB_LOG_ERROR,
+                                                  "check: bad group %llu"
+                                                  " vs %u",
+                                                  (long long)h, g);
                                goto fail;
                        }
 
@@ -341,13 +337,13 @@ static bool check_hash_tree(struct tdb_context *tdb,
                        if (get_bits(h, TDB_HASH_GROUP_BITS, &used_bits)
                            != bucket) {
                                used_bits -= TDB_HASH_GROUP_BITS;
-                               tdb_logerr(tdb, TDB_ERR_CORRUPT,
-                                          TDB_LOG_ERROR,
-                                        "check: bad bucket %u vs %u",
-                                        (unsigned)get_bits(h,
+                               ecode = tdb_logerr(tdb, TDB_ERR_CORRUPT,
+                                                  TDB_LOG_ERROR,
+                                                  "check: bad bucket %u vs %u",
+                                                  (unsigned)get_bits(h,
                                                        TDB_HASH_GROUP_BITS,
                                                        &used_bits),
-                                        bucket);
+                                                  bucket);
                                goto fail;
                        }
 
@@ -357,7 +353,8 @@ static bool check_hash_tree(struct tdb_context *tdb,
                             i != b;
                             i = (i + 1) % (1 << TDB_HASH_GROUP_BITS)) {
                                if (group[i] == 0) {
-                                       tdb_logerr(tdb, TDB_ERR_CORRUPT,
+                                       ecode = TDB_ERR_CORRUPT;
+                                       tdb_logerr(tdb, ecode,
                                                   TDB_LOG_ERROR,
                                                   "check: bad group placement"
                                                   " %u vs %u",
@@ -368,19 +365,19 @@ static bool check_hash_tree(struct tdb_context *tdb,
 
                        ecode = tdb_read_convert(tdb, off, &rec, sizeof(rec));
                        if (ecode != TDB_SUCCESS) {
-                               tdb->ecode = ecode;
                                goto fail;
                        }
 
                        /* Bottom bits must match header. */
                        if ((h & ((1 << 11)-1)) != rec_hash(&rec)) {
-                               tdb_logerr(tdb, TDB_ERR_CORRUPT,
-                                          TDB_LOG_ERROR,
-                                          "tdb_check: Bad hash magic at"
-                                          " offset %llu (0x%llx vs 0x%llx)",
-                                          (long long)off,
-                                          (long long)h,
-                                          (long long)rec_hash(&rec));
+                               ecode = tdb_logerr(tdb, TDB_ERR_CORRUPT,
+                                                  TDB_LOG_ERROR,
+                                                  "tdb_check: Bad hash magic"
+                                                  " at offset %llu"
+                                                  " (0x%llx vs 0x%llx)",
+                                                  (long long)off,
+                                                  (long long)h,
+                                                  (long long)rec_hash(&rec));
                                goto fail;
                        }
 
@@ -394,66 +391,69 @@ static bool check_hash_tree(struct tdb_context *tdb,
                                                   key.dsize + data.dsize,
                                                   false);
                                if (TDB_PTR_IS_ERR(key.dptr)) {
-                                       tdb->ecode = TDB_PTR_ERR(key.dptr);
+                                       ecode = TDB_PTR_ERR(key.dptr);
                                        goto fail;
                                }
                                data.dptr = key.dptr + key.dsize;
-                               if (check(key, data, private_data) != 0)
+                               if (check(key, data, private_data) != 0) {
+                                       ecode = TDB_ERR_CORRUPT;
                                        goto fail;
+                               }
                                tdb_access_release(tdb, key.dptr);
                        }
                }
        }
        tdb_access_release(tdb, hash);
-       return true;
+       return TDB_SUCCESS;
 
 fail:
        tdb_access_release(tdb, hash);
-       return false;
+       return ecode;
 }
 
-static bool check_hash(struct tdb_context *tdb,
-                      tdb_off_t used[],
-                      size_t num_used, size_t num_ftables,
-                      int (*check)(TDB_DATA, TDB_DATA, void *),
-                      void *private_data)
+static enum TDB_ERROR check_hash(struct tdb_context *tdb,
+                                tdb_off_t used[],
+                                size_t num_used, size_t num_ftables,
+                                int (*check)(TDB_DATA, TDB_DATA, void *),
+                                void *private_data)
 {
        /* Free tables also show up as used. */
        size_t num_found = num_ftables;
+       enum TDB_ERROR ecode;
 
-       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,
-                            check, private_data))
-               return false;
-
-       if (num_found != num_used) {
-               tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
-                          "tdb_check: Not all entries are in hash");
-               return false;
+       ecode = check_hash_tree(tdb, offsetof(struct tdb_header, hashtable),
+                               TDB_TOPLEVEL_HASH_BITS-TDB_HASH_GROUP_BITS,
+                               0, 0, used, num_used, &num_found,
+                               check, private_data);
+       if (ecode == TDB_SUCCESS) {
+               if (num_found != num_used) {
+                       ecode = tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
+                                          "tdb_check: Not all entries"
+                                          " are in hash");
+               }
        }
-       return true;
+       return ecode;
 }
 
-static bool check_free(struct tdb_context *tdb,
-                      tdb_off_t off,
-                      const struct tdb_free_record *frec,
-                      tdb_off_t prev, unsigned int ftable,
-                      unsigned int bucket)
+static enum TDB_ERROR check_free(struct tdb_context *tdb,
+                                tdb_off_t off,
+                                const struct tdb_free_record *frec,
+                                tdb_off_t prev, unsigned int ftable,
+                                unsigned int bucket)
 {
        enum TDB_ERROR ecode;
 
        if (frec_magic(frec) != TDB_FREE_MAGIC) {
-               tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
-                          "tdb_check: offset %llu bad magic 0x%llx",
-                          (long long)off, (long long)frec->magic_and_prev);
-               return false;
+               return tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
+                                 "tdb_check: offset %llu bad magic 0x%llx",
+                                 (long long)off,
+                                 (long long)frec->magic_and_prev);
        }
        if (frec_ftable(frec) != ftable) {
-               tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
-                          "tdb_check: offset %llu bad freetable %u",
-                          (long long)off, frec_ftable(frec));
-               return false;
+               return tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
+                                 "tdb_check: offset %llu bad freetable %u",
+                                 (long long)off, frec_ftable(frec));
+
        }
 
        ecode = tdb->methods->oob(tdb, off
@@ -461,32 +461,31 @@ static bool check_free(struct tdb_context *tdb,
                                  + sizeof(struct tdb_used_record),
                                  false);
        if (ecode != TDB_SUCCESS) {
-               tdb->ecode = ecode;
-               return false;
+               return ecode;
        }
        if (size_to_bucket(frec_len(frec)) != bucket) {
-               tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
-                          "tdb_check: offset %llu in wrong bucket %u vs %u",
-                          (long long)off,
-                          bucket, size_to_bucket(frec_len(frec)));
-               return false;
+               return tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
+                                 "tdb_check: offset %llu in wrong bucket"
+                                 " (%u vs %u)",
+                                 (long long)off,
+                                 bucket, size_to_bucket(frec_len(frec)));
        }
        if (prev != frec_prev(frec)) {
-               tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
-                          "tdb_check: offset %llu bad prev %llu vs %llu",
-                          (long long)off,
-                          (long long)prev, (long long)frec_len(frec));
-               return false;
+               return tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
+                                 "tdb_check: offset %llu bad prev"
+                                 " (%llu vs %llu)",
+                                 (long long)off,
+                                 (long long)prev, (long long)frec_len(frec));
        }
-       return true;
+       return TDB_SUCCESS;
 }
 
-static bool check_free_table(struct tdb_context *tdb,
-                            tdb_off_t ftable_off,
-                            unsigned ftable_num,
-                            tdb_off_t fr[],
-                            size_t num_free,
-                            size_t *num_found)
+static enum TDB_ERROR check_free_table(struct tdb_context *tdb,
+                                      tdb_off_t ftable_off,
+                                      unsigned ftable_num,
+                                      tdb_off_t fr[],
+                                      size_t num_free,
+                                      size_t *num_found)
 {
        struct tdb_freetable ft;
        tdb_off_t h;
@@ -495,17 +494,15 @@ static bool check_free_table(struct tdb_context *tdb,
 
        ecode = tdb_read_convert(tdb, ftable_off, &ft, sizeof(ft));
        if (ecode != TDB_SUCCESS) {
-               tdb->ecode = ecode;
-               return false;
+               return ecode;
        }
 
        if (rec_magic(&ft.hdr) != TDB_FTABLE_MAGIC
            || rec_key_length(&ft.hdr) != 0
            || rec_data_length(&ft.hdr) != sizeof(ft) - sizeof(ft.hdr)
            || rec_hash(&ft.hdr) != 0) {
-               tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
-                          "tdb_check: Invalid header on free table");
-               return false;
+               return tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
+                                 "tdb_check: Invalid header on free table");
        }
 
        for (i = 0; i < TDB_FREE_BUCKETS; i++) {
@@ -515,26 +512,25 @@ static bool check_free_table(struct tdb_context *tdb,
                h = bucket_off(ftable_off, i);
                for (off = tdb_read_off(tdb, h); off; off = f.next) {
                        if (TDB_OFF_IS_ERR(off)) {
-                               tdb->ecode = off;
-                               return false;
+                               return off;
                        }
                        ecode = tdb_read_convert(tdb, off, &f, sizeof(f));
                        if (ecode != TDB_SUCCESS) {
-                               tdb->ecode = ecode;
-                               return false;
+                               return ecode;
                        }
-                       if (!check_free(tdb, off, &f, prev, ftable_num, i))
+                       ecode = check_free(tdb, off, &f, prev, ftable_num, i);
+                       if (ecode != TDB_SUCCESS) {
                                return false;
+                       }
 
                        /* FIXME: Check hash bits */
                        p = asearch(&off, fr, num_free, off_cmp);
                        if (!p) {
-                               tdb_logerr(tdb, TDB_ERR_CORRUPT,
-                                          TDB_LOG_ERROR,
-                                          "tdb_check: Invalid offset"
-                                          " %llu in free table",
-                                          (long long)off);
-                               return false;
+                               return tdb_logerr(tdb, TDB_ERR_CORRUPT,
+                                                 TDB_LOG_ERROR,
+                                                 "tdb_check: Invalid offset"
+                                                 " %llu in free table",
+                                                 (long long)off);
                        }
                        /* Mark it invalid. */
                        *p ^= 1;
@@ -542,11 +538,11 @@ static bool check_free_table(struct tdb_context *tdb,
                        prev = off;
                }
        }
-       return true;
+       return TDB_SUCCESS;
 }
 
 /* Slow, but should be very rare. */
-size_t dead_space(struct tdb_context *tdb, tdb_off_t off)
+tdb_off_t dead_space(struct tdb_context *tdb, tdb_off_t off)
 {
        size_t len;
        enum TDB_ERROR ecode;
@@ -555,8 +551,7 @@ size_t dead_space(struct tdb_context *tdb, tdb_off_t off)
                char c;
                ecode = tdb->methods->tread(tdb, off, &c, 1);
                if (ecode != TDB_SUCCESS) {
-                       tdb->ecode = ecode;
-                       return 0;
+                       return ecode;
                }
                if (c != 0 && c != 0x43)
                        break;
@@ -564,10 +559,10 @@ size_t dead_space(struct tdb_context *tdb, tdb_off_t off)
        return len;
 }
 
-static bool check_linear(struct tdb_context *tdb,
-                        tdb_off_t **used, size_t *num_used,
-                        tdb_off_t **fr, size_t *num_free,
-                        tdb_off_t recovery)
+static enum TDB_ERROR check_linear(struct tdb_context *tdb,
+                                  tdb_off_t **used, size_t *num_used,
+                                  tdb_off_t **fr, size_t *num_free,
+                                  tdb_off_t recovery)
 {
        tdb_off_t off;
        tdb_len_t len;
@@ -583,8 +578,7 @@ static bool check_linear(struct tdb_context *tdb,
                /* r is larger: only get that if we need to. */
                ecode = tdb_read_convert(tdb, off, &rec, sizeof(rec.f));
                if (ecode != TDB_SUCCESS) {
-                       tdb->ecode = ecode;
-                       return false;
+                       return ecode;
                }
 
                /* If we crash after ftruncate, we can get zeroes or fill. */
@@ -592,21 +586,22 @@ static bool check_linear(struct tdb_context *tdb,
                    || rec.r.magic ==  0x4343434343434343ULL) {
                        ecode = tdb_read_convert(tdb, off, &rec, sizeof(rec.r));
                        if (ecode != TDB_SUCCESS) {
-                               tdb->ecode = ecode;
-                               return false;
+                               return ecode;
                        }
                        if (recovery == off) {
                                found_recovery = true;
                                len = sizeof(rec.r) + rec.r.max_len;
                        } else {
                                len = dead_space(tdb, off);
+                               if (TDB_OFF_IS_ERR(len)) {
+                                       return len;
+                               }
                                if (len < sizeof(rec.r)) {
-                                       tdb_logerr(tdb, TDB_ERR_CORRUPT,
-                                                  TDB_LOG_ERROR,
-                                                  "tdb_check: invalid dead"
-                                                  " space at %zu",
-                                                  (size_t)off);
-                                       return false;
+                                       return tdb_logerr(tdb, TDB_ERR_CORRUPT,
+                                                         TDB_LOG_ERROR,
+                                                         "tdb_check: invalid"
+                                                         " dead space at %zu",
+                                                         (size_t)off);
                                }
 
                                tdb_logerr(tdb, TDB_SUCCESS, TDB_LOG_WARNING,
@@ -617,51 +612,48 @@ static bool check_linear(struct tdb_context *tdb,
                } else if (rec.r.magic == TDB_RECOVERY_MAGIC) {
                        ecode = tdb_read_convert(tdb, off, &rec, sizeof(rec.r));
                        if (ecode != TDB_SUCCESS) {
-                               tdb->ecode = ecode;
-                               return false;
+                               return ecode;
                        }
                        if (recovery != off) {
-                               tdb_logerr(tdb, TDB_ERR_CORRUPT,
-                                          TDB_LOG_ERROR,
-                                          "tdb_check: unexpected recovery"
-                                          " record at offset %zu",
-                                          (size_t)off);
-                               return false;
+                               return tdb_logerr(tdb, TDB_ERR_CORRUPT,
+                                                 TDB_LOG_ERROR,
+                                                 "tdb_check: unexpected"
+                                                 " recovery record at offset"
+                                                 " %zu",
+                                                 (size_t)off);
                        }
                        if (rec.r.len > rec.r.max_len) {
-                               tdb_logerr(tdb, TDB_ERR_CORRUPT,
-                                          TDB_LOG_ERROR,
-                                          "tdb_check: invalid recovery length"
-                                          " %zu", (size_t)rec.r.len);
-                               return false;
+                               return tdb_logerr(tdb, TDB_ERR_CORRUPT,
+                                                 TDB_LOG_ERROR,
+                                                 "tdb_check: invalid recovery"
+                                                 " length %zu",
+                                                 (size_t)rec.r.len);
                        }
                        if (rec.r.eof > tdb->map_size) {
-                               tdb_logerr(tdb, TDB_ERR_CORRUPT,
-                                          TDB_LOG_ERROR,
-                                          "tdb_check: invalid old EOF"
-                                          " %zu", (size_t)rec.r.eof);
-                               return false;
+                               return tdb_logerr(tdb, TDB_ERR_CORRUPT,
+                                                 TDB_LOG_ERROR,
+                                                 "tdb_check: invalid old EOF"
+                                                 " %zu", (size_t)rec.r.eof);
                        }
                        found_recovery = true;
                        len = sizeof(rec.r) + rec.r.max_len;
                } else if (frec_magic(&rec.f) == TDB_FREE_MAGIC) {
                        len = sizeof(rec.u) + frec_len(&rec.f);
                        if (off + len > tdb->map_size) {
-                               tdb_logerr(tdb, TDB_ERR_CORRUPT,
-                                          TDB_LOG_ERROR,
-                                          "tdb_check: free overlength %llu"
-                                          " at offset %llu",
-                                          (long long)len, (long long)off);
-                               return false;
+                               return tdb_logerr(tdb, TDB_ERR_CORRUPT,
+                                                 TDB_LOG_ERROR,
+                                                 "tdb_check: free overlength"
+                                                 " %llu at offset %llu",
+                                                 (long long)len,
+                                                 (long long)off);
                        }
                        /* This record should be in free lists. */
                        if (frec_ftable(&rec.f) != TDB_FTABLE_NONE
                            && !append(fr, num_free, off)) {
-                               tdb_logerr(tdb, TDB_ERR_OOM,
-                                          TDB_LOG_ERROR,
-                                          "tdb_check: tracking %zu'th"
-                                          " free record.", *num_free);
-                               return false;
+                               return tdb_logerr(tdb, TDB_ERR_OOM,
+                                                 TDB_LOG_ERROR,
+                                                 "tdb_check: tracking %zu'th"
+                                                 " free record.", *num_free);
                        }
                } else if (rec_magic(&rec.u) == TDB_USED_MAGIC
                           || rec_magic(&rec.u) == TDB_CHAIN_MAGIC
@@ -671,11 +663,10 @@ static bool check_linear(struct tdb_context *tdb,
 
                        /* This record is used! */
                        if (!append(used, num_used, off)) {
-                               tdb_logerr(tdb, TDB_ERR_OOM,
-                                          TDB_LOG_ERROR,
-                                          "tdb_check: tracking %zu'th"
-                                          " used record.", *num_used);
-                               return false;
+                               return tdb_logerr(tdb, TDB_ERR_OOM,
+                                                 TDB_LOG_ERROR,
+                                                 "tdb_check: tracking %zu'th"
+                                                 " used record.", *num_used);
                        }
 
                        klen = rec_key_length(&rec.u);
@@ -684,40 +675,40 @@ static bool check_linear(struct tdb_context *tdb,
 
                        len = sizeof(rec.u) + klen + dlen + extra;
                        if (off + len > tdb->map_size) {
-                               tdb_logerr(tdb, TDB_ERR_CORRUPT,
-                                          TDB_LOG_ERROR,
-                                          "tdb_check: used overlength %llu"
-                                          " at offset %llu",
-                                          (long long)len, (long long)off);
-                               return false;
+                               return tdb_logerr(tdb, TDB_ERR_CORRUPT,
+                                                 TDB_LOG_ERROR,
+                                                 "tdb_check: used overlength"
+                                                 " %llu at offset %llu",
+                                                 (long long)len,
+                                                 (long long)off);
                        }
 
                        if (len < sizeof(rec.f)) {
-                               tdb_logerr(tdb, TDB_ERR_CORRUPT,
-                                          TDB_LOG_ERROR,
-                                          "tdb_check: too short record %llu"
-                                          " at %llu",
-                                          (long long)len, (long long)off);
-                               return false;
+                               return tdb_logerr(tdb, TDB_ERR_CORRUPT,
+                                                 TDB_LOG_ERROR,
+                                                 "tdb_check: too short record"
+                                                 " %llu at %llu",
+                                                 (long long)len,
+                                                 (long long)off);
                        }
                } else {
-                       tdb_logerr(tdb, TDB_ERR_CORRUPT,
-                                  TDB_LOG_ERROR,
-                                  "tdb_check: Bad magic 0x%llx at offset %zu",
-                                  (long long)rec_magic(&rec.u), (size_t)off);
-                       return false;
+                       return tdb_logerr(tdb, TDB_ERR_CORRUPT,
+                                         TDB_LOG_ERROR,
+                                         "tdb_check: Bad magic 0x%llx"
+                                         " at offset %zu",
+                                         (long long)rec_magic(&rec.u),
+                                         (size_t)off);
                }
        }
 
        /* We must have found recovery area if there was one. */
        if (recovery != 0 && !found_recovery) {
-               tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
-                          "tdb_check: expected a recovery area at %zu",
-                          (size_t)recovery);
-               return false;
+               return tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
+                                 "tdb_check: expected a recovery area at %zu",
+                                 (size_t)recovery);
        }
 
-       return true;
+       return TDB_SUCCESS;
 }
 
 int tdb_check(struct tdb_context *tdb,
@@ -741,44 +732,47 @@ int tdb_check(struct tdb_context *tdb,
                return -1;
        }
 
-       if (!check_header(tdb, &recovery))
-               goto fail;
+       ecode = check_header(tdb, &recovery);
+       if (ecode != TDB_SUCCESS)
+               goto out;
 
        /* First we do a linear scan, checking all records. */
-       if (!check_linear(tdb, &used, &num_used, &fr, &num_free, recovery))
-               goto fail;
+       ecode = check_linear(tdb, &used, &num_used, &fr, &num_free, recovery);
+       if (ecode != TDB_SUCCESS)
+               goto out;
 
        for (ft = first_ftable(tdb); ft; ft = next_ftable(tdb, ft)) {
                if (TDB_OFF_IS_ERR(ft)) {
                        tdb->ecode = ft;
-                       goto fail;
+                       goto out;
                }
-               if (!check_free_table(tdb, ft, num_ftables, fr, num_free,
-                                     &num_found))
-                       goto fail;
+               ecode = check_free_table(tdb, ft, num_ftables, fr, num_free,
+                                        &num_found);
+               if (ecode != TDB_SUCCESS)
+                       goto out;
                num_ftables++;
        }
 
        /* FIXME: Check key uniqueness? */
-       if (!check_hash(tdb, used, num_used, num_ftables, check, private_data))
-               goto fail;
+       ecode = check_hash(tdb, used, num_used, num_ftables, check,
+                          private_data);
+       if (ecode != TDB_SUCCESS)
+               goto out;
 
        if (num_found != num_free) {
-               tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
-                          "tdb_check: Not all entries are in free table");
-               return -1;
+               ecode = tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR,
+                                  "tdb_check: Not all entries are in"
+                                  " free table");
        }
 
+out:
        tdb_allrecord_unlock(tdb, F_RDLCK);
        tdb_unlock_expand(tdb, F_RDLCK);
        free(fr);
        free(used);
+       if (ecode != TDB_SUCCESS) {
+               tdb->ecode = ecode;
+               return -1;
+       }
        return 0;
-
-fail:
-       free(fr);
-       free(used);
-       tdb_allrecord_unlock(tdb, F_RDLCK);
-       tdb_unlock_expand(tdb, F_RDLCK);
-       return -1;
 }
index f313a1e704f0f6003bad73ced6e3d8db92b19686..c57b3ba9283bc54d907e4ddd886945a1d880435d 100644 (file)
@@ -453,7 +453,7 @@ unsigned int size_to_bucket(tdb_len_t data_len);
 tdb_off_t bucket_off(tdb_off_t ftable_off, unsigned bucket);
 
 /* Used by tdb_summary */
-size_t dead_space(struct tdb_context *tdb, tdb_off_t off);
+tdb_off_t dead_space(struct tdb_context *tdb, tdb_off_t off);
 
 /* io.c: */
 /* Initialize tdb->methods. */
index 61d7689749caa6a37b8883439d4ba7c4bcbc524a..8c611c3a04a8c14966d188ec047d68a152f0e1a3 100644 (file)
@@ -113,8 +113,12 @@ static bool summarize(struct tdb_context *tdb,
                                + rec_extra_padding(&p->u);
                        tally_add(chains, 1);
                        tally_add(extra, rec_extra_padding(&p->u));
-               } else
+               } else {
                        len = dead_space(tdb, off);
+                       if (TDB_OFF_IS_ERR(len)) {
+                               return false;
+                       }
+               }
                tdb_access_release(tdb, p);
        }
        if (unc)