X-Git-Url: https://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Ftdb2%2Fcheck.c;h=230eaee8e367d7d13045f4fcdc68101a10d20c40;hp=077fa856e4cee2981659ecd406e06c3b0ca500e6;hb=e01d795c8964b791def1e9f68c386b350b3a2a84;hpb=41e027e11e0b15100e50c14e49aaa4c2b418f431;ds=sidebyside diff --git a/ccan/tdb2/check.c b/ccan/tdb2/check.c index 077fa856..230eaee8 100644 --- a/ccan/tdb2/check.c +++ b/ccan/tdb2/check.c @@ -94,7 +94,7 @@ static enum TDB_ERROR check_hash_tree(struct tdb_context *tdb, size_t *num_found, enum TDB_ERROR (*check)(TDB_DATA, TDB_DATA, void *), - void *private_data); + void *data); static enum TDB_ERROR check_hash_chain(struct tdb_context *tdb, tdb_off_t off, @@ -105,7 +105,7 @@ static enum TDB_ERROR check_hash_chain(struct tdb_context *tdb, enum TDB_ERROR (*check)(TDB_DATA, TDB_DATA, void *), - void *private_data) + void *data) { struct tdb_used_record rec; enum TDB_ERROR ecode; @@ -141,20 +141,20 @@ static enum TDB_ERROR check_hash_chain(struct tdb_context *tdb, off += sizeof(rec); ecode = check_hash_tree(tdb, off, 0, hash, 64, - used, num_used, num_found, check, private_data); + used, num_used, num_found, check, data); if (ecode != TDB_SUCCESS) { return ecode; } off = tdb_read_off(tdb, off + offsetof(struct tdb_chain, next)); if (TDB_OFF_IS_ERR(off)) { - return off; + return TDB_OFF_TO_ERR(off); } if (off == 0) return TDB_SUCCESS; (*num_found)++; return check_hash_chain(tdb, off, hash, used, num_used, num_found, - check, private_data); + check, data); } static enum TDB_ERROR check_hash_record(struct tdb_context *tdb, @@ -167,14 +167,14 @@ static enum TDB_ERROR check_hash_record(struct tdb_context *tdb, enum TDB_ERROR (*check)(TDB_DATA, TDB_DATA, void *), - void *private_data) + void *data) { struct tdb_used_record rec; enum TDB_ERROR ecode; if (hprefix_bits >= 64) return check_hash_chain(tdb, off, hprefix, used, num_used, - num_found, check, private_data); + num_found, check, data); ecode = tdb_read_convert(tdb, off, &rec, sizeof(rec)); if (ecode != TDB_SUCCESS) { @@ -210,7 +210,7 @@ static enum TDB_ERROR check_hash_record(struct tdb_context *tdb, return check_hash_tree(tdb, off, TDB_SUBLEVEL_HASH_BITS-TDB_HASH_GROUP_BITS, hprefix, hprefix_bits, - used, num_used, num_found, check, private_data); + used, num_used, num_found, check, data); } static int off_cmp(const tdb_off_t *a, const tdb_off_t *b) @@ -237,7 +237,7 @@ static enum TDB_ERROR check_hash_tree(struct tdb_context *tdb, size_t *num_found, enum TDB_ERROR (*check)(TDB_DATA, TDB_DATA, void *), - void *private_data) + void *data) { unsigned int g, b; const tdb_off_t *hash; @@ -318,7 +318,7 @@ static enum TDB_ERROR check_hash_tree(struct tdb_context *tdb, + group_bits + TDB_HASH_GROUP_BITS, used, num_used, num_found, - check, private_data); + check, data); if (ecode != TDB_SUCCESS) { goto fail; } @@ -401,23 +401,27 @@ static enum TDB_ERROR check_hash_tree(struct tdb_context *tdb, check: if (check) { - TDB_DATA key, data; - key.dsize = rec_key_length(&rec); - data.dsize = rec_data_length(&rec); - key.dptr = (void *)tdb_access_read(tdb, - off + sizeof(rec), - key.dsize + data.dsize, - false); - if (TDB_PTR_IS_ERR(key.dptr)) { - ecode = TDB_PTR_ERR(key.dptr); + TDB_DATA k, d; + const unsigned char *kptr; + + kptr = tdb_access_read(tdb, + off + sizeof(rec), + rec_key_length(&rec) + + rec_data_length(&rec), + false); + if (TDB_PTR_IS_ERR(kptr)) { + ecode = TDB_PTR_ERR(kptr); goto fail; } - data.dptr = key.dptr + key.dsize; - ecode = check(key, data, private_data); + + k = tdb_mkdata(kptr, rec_key_length(&rec)); + d = tdb_mkdata(kptr + k.dsize, + rec_data_length(&rec)); + ecode = check(k, d, data); + tdb_access_release(tdb, kptr); if (ecode != TDB_SUCCESS) { goto fail; } - tdb_access_release(tdb, key.dptr); } } } @@ -432,8 +436,8 @@ fail: 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) + enum TDB_ERROR (*check)(TDB_DATA, TDB_DATA, void *), + void *data) { /* Free tables also show up as used. */ size_t num_found = num_ftables; @@ -442,7 +446,7 @@ static enum TDB_ERROR check_hash(struct tdb_context *tdb, 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); + check, data); if (ecode == TDB_SUCCESS) { if (num_found != num_used) { ecode = tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR, @@ -474,7 +478,7 @@ static enum TDB_ERROR check_free(struct tdb_context *tdb, } - ecode = tdb->methods->oob(tdb, off + ecode = tdb->tdb2.io->oob(tdb, off + frec_len(frec) + sizeof(struct tdb_used_record), false); @@ -488,7 +492,7 @@ static enum TDB_ERROR check_free(struct tdb_context *tdb, (long long)off, bucket, size_to_bucket(frec_len(frec))); } - if (prev != frec_prev(frec)) { + if (prev && prev != frec_prev(frec)) { return tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR, "tdb_check: offset %llu bad prev" " (%llu vs %llu)", @@ -524,13 +528,17 @@ static enum TDB_ERROR check_free_table(struct tdb_context *tdb, } for (i = 0; i < TDB_FREE_BUCKETS; i++) { - tdb_off_t off, prev = 0, *p; + tdb_off_t off, prev = 0, *p, first = 0; struct tdb_free_record f; h = bucket_off(ftable_off, i); for (off = tdb_read_off(tdb, h); off; off = f.next) { if (TDB_OFF_IS_ERR(off)) { - return off; + return TDB_OFF_TO_ERR(off); + } + if (!first) { + off &= TDB_OFF_MASK; + first = off; } ecode = tdb_read_convert(tdb, off, &f, sizeof(f)); if (ecode != TDB_SUCCESS) { @@ -555,6 +563,18 @@ static enum TDB_ERROR check_free_table(struct tdb_context *tdb, (*num_found)++; prev = off; } + + if (first) { + /* Now we can check first back pointer. */ + ecode = tdb_read_convert(tdb, first, &f, sizeof(f)); + if (ecode != TDB_SUCCESS) { + return ecode; + } + ecode = check_free(tdb, first, &f, prev, ftable_num, i); + if (ecode != TDB_SUCCESS) { + return ecode; + } + } } return TDB_SUCCESS; } @@ -567,9 +587,9 @@ tdb_off_t dead_space(struct tdb_context *tdb, tdb_off_t off) for (len = 0; off + len < tdb->file->map_size; len++) { char c; - ecode = tdb->methods->tread(tdb, off, &c, 1); + ecode = tdb->tdb2.io->tread(tdb, off, &c, 1); if (ecode != TDB_SUCCESS) { - return ecode; + return TDB_ERR_TO_OFF(ecode); } if (c != 0 && c != 0x43) break; @@ -614,7 +634,7 @@ static enum TDB_ERROR check_linear(struct tdb_context *tdb, } else { len = dead_space(tdb, off); if (TDB_OFF_IS_ERR(len)) { - return len; + return TDB_OFF_TO_ERR(len); } if (len < sizeof(rec.r)) { return tdb_logerr(tdb, TDB_ERR_CORRUPT, @@ -754,24 +774,35 @@ static enum TDB_ERROR check_linear(struct tdb_context *tdb, } enum TDB_ERROR tdb_check_(struct tdb_context *tdb, - enum TDB_ERROR (*check)(TDB_DATA key, TDB_DATA data, - void *private), - void *private) + enum TDB_ERROR (*check)(TDB_DATA, TDB_DATA, void *), + void *data) { tdb_off_t *fr = NULL, *used = NULL, ft, recovery; size_t num_free = 0, num_used = 0, num_found = 0, num_ftables = 0; uint64_t features; enum TDB_ERROR ecode; + if (tdb->flags & TDB_CANT_CHECK) { + return tdb_logerr(tdb, TDB_SUCCESS, TDB_LOG_WARNING, + "tdb_check: database has unknown features," + " cannot check."); + } + + if (tdb->flags & TDB_VERSION1) { + if (tdb1_check(tdb, check, data) == -1) + return tdb->last_error; + return TDB_SUCCESS; + } + ecode = tdb_allrecord_lock(tdb, F_RDLCK, TDB_LOCK_WAIT, false); if (ecode != TDB_SUCCESS) { - return ecode; + return tdb->last_error = ecode; } ecode = tdb_lock_expand(tdb, F_RDLCK); if (ecode != TDB_SUCCESS) { tdb_allrecord_unlock(tdb, F_RDLCK); - return ecode; + return tdb->last_error = ecode; } ecode = check_header(tdb, &recovery, &features); @@ -786,7 +817,7 @@ enum TDB_ERROR tdb_check_(struct tdb_context *tdb, for (ft = first_ftable(tdb); ft; ft = next_ftable(tdb, ft)) { if (TDB_OFF_IS_ERR(ft)) { - ecode = ft; + ecode = TDB_OFF_TO_ERR(ft); goto out; } ecode = check_free_table(tdb, ft, num_ftables, fr, num_free, @@ -797,7 +828,7 @@ enum TDB_ERROR tdb_check_(struct tdb_context *tdb, } /* FIXME: Check key uniqueness? */ - ecode = check_hash(tdb, used, num_used, num_ftables, check, private); + ecode = check_hash(tdb, used, num_used, num_ftables, check, data); if (ecode != TDB_SUCCESS) goto out; @@ -812,5 +843,5 @@ out: tdb_unlock_expand(tdb, F_RDLCK); free(fr); free(used); - return ecode; + return tdb->last_error = ecode; }