]> git.ozlabs.org Git - ccan/blobdiff - ccan/tdb2/check.c
tdb2: make summary command handle recovery "dead zone"
[ccan] / ccan / tdb2 / check.c
index bd7679c91b81ccc1fef0a1e51725957495ad5bcc..54f96bb0ea8283583faf91693decd8d467970423 100644 (file)
@@ -30,7 +30,7 @@ static bool append(tdb_off_t **arr, size_t *num, tdb_off_t off)
        return true;
 }
 
        return true;
 }
 
-static bool check_header(struct tdb_context *tdb)
+static bool check_header(struct tdb_context *tdb, tdb_off_t *recovery)
 {
        uint64_t hash_test;
        struct tdb_header hdr;
 {
        uint64_t hash_test;
        struct tdb_header hdr;
@@ -57,6 +57,16 @@ static bool check_header(struct tdb_context *tdb)
                return false;
        }
 
                return false;
        }
 
+       *recovery = hdr.recovery;
+       if (*recovery) {
+               if (*recovery < sizeof(hdr) || *recovery > tdb->map_size) {
+                       tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
+                                "tdb_check: invalid recovery offset %zu\n",
+                                (size_t)*recovery);
+                       return false;
+               }
+       }
+
        /* Don't check reserved: they *can* be used later. */
        return true;
 }
        /* Don't check reserved: they *can* be used later. */
        return true;
 }
@@ -258,17 +268,17 @@ fail:
 
 static bool check_hash(struct tdb_context *tdb,
                       tdb_off_t used[],
 
 static bool check_hash(struct tdb_context *tdb,
                       tdb_off_t used[],
-                      size_t num_used)
+                      size_t num_used, size_t num_flists)
 {
 {
-       size_t num_found = 0;
+       /* Free lists also show up as used. */
+       size_t num_found = num_flists;
 
        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))
                return false;
 
 
        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))
                return false;
 
-       /* 1 is for the free list. */
-       if (num_found != num_used - 1) {
+       if (num_found != num_used) {
                tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
                         "tdb_check: Not all entries are in hash\n");
                return false;
                tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
                         "tdb_check: Not all entries are in hash\n");
                return false;
@@ -279,7 +289,7 @@ static bool check_hash(struct tdb_context *tdb,
 static bool check_free(struct tdb_context *tdb,
                       tdb_off_t off,
                       const struct tdb_free_record *frec,
 static bool check_free(struct tdb_context *tdb,
                       tdb_off_t off,
                       const struct tdb_free_record *frec,
-                      tdb_off_t prev, unsigned int bucket)
+                      tdb_off_t prev, tdb_off_t flist_off, unsigned int bucket)
 {
        if (frec_magic(frec) != TDB_FREE_MAGIC) {
                tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
 {
        if (frec_magic(frec) != TDB_FREE_MAGIC) {
                tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
@@ -287,6 +297,13 @@ static bool check_free(struct tdb_context *tdb,
                         (long long)off, (long long)frec->magic_and_meta);
                return false;
        }
                         (long long)off, (long long)frec->magic_and_meta);
                return false;
        }
+       if (frec_flist(frec) != flist_off) {
+               tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
+                        "tdb_check: offset %llu bad freelist 0x%llx\n",
+                        (long long)off, (long long)frec_flist(frec));
+               return false;
+       }
+
        if (tdb->methods->oob(tdb, off
                              + frec->data_len+sizeof(struct tdb_used_record),
                              false))
        if (tdb->methods->oob(tdb, off
                              + frec->data_len+sizeof(struct tdb_used_record),
                              false))
@@ -341,7 +358,7 @@ static bool check_free_list(struct tdb_context *tdb,
                                return false;
                        if (tdb_read_convert(tdb, off, &f, sizeof(f)))
                                return false;
                                return false;
                        if (tdb_read_convert(tdb, off, &f, sizeof(f)))
                                return false;
-                       if (!check_free(tdb, off, &f, prev, i))
+                       if (!check_free(tdb, off, &f, prev, flist_off, i))
                                return false;
 
                        /* FIXME: Check hash bits */
                                return false;
 
                        /* FIXME: Check hash bits */
@@ -363,23 +380,73 @@ static bool check_free_list(struct tdb_context *tdb,
        return true;
 }
 
        return true;
 }
 
+/* Slow, but should be very rare. */
+size_t dead_space(struct tdb_context *tdb, tdb_off_t off)
+{
+       size_t len;
+
+       for (len = 0; off + len < tdb->map_size; len++) {
+               char c;
+               if (tdb->methods->read(tdb, off, &c, 1))
+                       return 0;
+               if (c != 0 && c != 0x43)
+                       break;
+       }
+       return len;
+}
+
 static bool check_linear(struct tdb_context *tdb,
                         tdb_off_t **used, size_t *num_used,
 static bool check_linear(struct tdb_context *tdb,
                         tdb_off_t **used, size_t *num_used,
-                        tdb_off_t **free, size_t *num_free)
+                        tdb_off_t **free, size_t *num_free,
+                        tdb_off_t recovery)
 {
        tdb_off_t off;
        tdb_len_t len;
 {
        tdb_off_t off;
        tdb_len_t len;
+       bool found_recovery = false;
 
        for (off = sizeof(struct tdb_header); off < tdb->map_size; off += len) {
                union {
                        struct tdb_used_record u;
                        struct tdb_free_record f;
 
        for (off = sizeof(struct tdb_header); off < tdb->map_size; off += len) {
                union {
                        struct tdb_used_record u;
                        struct tdb_free_record f;
+                       struct tdb_recovery_record r;
                } pad, *p;
                p = tdb_get(tdb, off, &pad, sizeof(pad));
                if (!p)
                        return false;
                } pad, *p;
                p = tdb_get(tdb, off, &pad, sizeof(pad));
                if (!p)
                        return false;
-               if (frec_magic(&p->f) == TDB_FREE_MAGIC
-                   || frec_magic(&p->f) == TDB_COALESCING_MAGIC) {
+
+               /* If we crash after ftruncate, we can get zeroes or fill. */
+               if (p->r.magic == TDB_RECOVERY_INVALID_MAGIC
+                   || p->r.magic ==  0x4343434343434343ULL) {
+                       if (recovery == off) {
+                               found_recovery = true;
+                               len = sizeof(p->r) + p->r.max_len;
+                       } else {
+                               len = dead_space(tdb, off);
+                               if (len < sizeof(p->r)) {
+                                       tdb->log(tdb, TDB_DEBUG_ERROR,
+                                                tdb->log_priv,
+                                                "tdb_check: invalid dead space"
+                                                " at %zu\n", (size_t)off);
+                                       return false;
+                               }
+
+                               tdb->log(tdb, TDB_DEBUG_WARNING, tdb->log_priv,
+                                        "Dead space at %zu-%zu (of %zu)\n",
+                                        (size_t)off, (size_t)(off + len),
+                                        (size_t)tdb->map_size);
+                       }
+               } else if (p->r.magic == TDB_RECOVERY_MAGIC) {
+                       if (recovery != off) {
+                               tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
+                                        "tdb_check: unexpected recovery"
+                                        " record at offset %zu\n",
+                                        (size_t)off);
+                               return false;
+                       }
+                       found_recovery = true;
+                       len = sizeof(p->r) + p->r.max_len;
+               } else if (frec_magic(&p->f) == TDB_FREE_MAGIC
+                          || frec_magic(&p->f) == TDB_COALESCING_MAGIC) {
                        len = sizeof(p->u) + p->f.data_len;
                        if (off + len > tdb->map_size) {
                                tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
                        len = sizeof(p->u) + p->f.data_len;
                        if (off + len > tdb->map_size) {
                                tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
@@ -430,6 +497,15 @@ static bool check_linear(struct tdb_context *tdb,
                        }
                }
        }
                        }
                }
        }
+
+       /* We must have found recovery area if there was one. */
+       if (recovery != 0 && !found_recovery) {
+               tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
+                        "tdb_check: expected a recovery area at %zu\n",
+                        (size_t)recovery);
+               return false;
+       }
+
        return true;
 }
 
        return true;
 }
 
@@ -438,8 +514,8 @@ int tdb_check(struct tdb_context *tdb,
              int (*check)(TDB_DATA key, TDB_DATA data, void *private_data),
              void *private_data)
 {
              int (*check)(TDB_DATA key, TDB_DATA data, void *private_data),
              void *private_data)
 {
-       tdb_off_t *free = NULL, *used = NULL;
-       size_t num_free = 0, num_used = 0, num_found = 0;
+       tdb_off_t *free = NULL, *used = NULL, flist, recovery;
+       size_t num_free = 0, num_used = 0, num_found = 0, num_flists = 0;
 
        if (tdb_allrecord_lock(tdb, F_RDLCK, TDB_LOCK_WAIT, false) != 0)
                return -1;
 
        if (tdb_allrecord_lock(tdb, F_RDLCK, TDB_LOCK_WAIT, false) != 0)
                return -1;
@@ -449,18 +525,23 @@ int tdb_check(struct tdb_context *tdb,
                return -1;
        }
 
                return -1;
        }
 
-       if (!check_header(tdb))
+       if (!check_header(tdb, &recovery))
                goto fail;
 
        /* First we do a linear scan, checking all records. */
                goto fail;
 
        /* First we do a linear scan, checking all records. */
-       if (!check_linear(tdb, &used, &num_used, &free, &num_free))
+       if (!check_linear(tdb, &used, &num_used, &free, &num_free, recovery))
                goto fail;
 
                goto fail;
 
-       /* FIXME: Check key uniqueness? */
-       if (!check_hash(tdb, used, num_used))
-               goto fail;
+       for (flist = first_flist(tdb); flist; flist = next_flist(tdb, flist)) {
+               if (flist == TDB_OFF_ERR)
+                       goto fail;
+               if (!check_free_list(tdb, flist, free, num_free, &num_found))
+                       goto fail;
+               num_flists++;
+       }
 
 
-       if (!check_free_list(tdb, tdb->flist_off, free, num_free, &num_found))
+       /* FIXME: Check key uniqueness? */
+       if (!check_hash(tdb, used, num_used, num_flists))
                goto fail;
 
        if (num_found != num_free) {
                goto fail;
 
        if (num_found != num_free) {