X-Git-Url: http://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Ftdb2%2Ftdb1_freelist.c;h=af0129372173d3b12f048f4960c66cdfb14d844b;hp=ba93b5892cd3daa7b028c443ef4ff7a0f2ee9ff3;hb=818ed29730b030ce79855fc35c212b51adff3180;hpb=b929638e3cfe629285af3ecd0813e03eaeaa1133 diff --git a/ccan/tdb2/tdb1_freelist.c b/ccan/tdb2/tdb1_freelist.c index ba93b589..af012937 100644 --- a/ccan/tdb2/tdb1_freelist.c +++ b/ccan/tdb2/tdb1_freelist.c @@ -28,36 +28,36 @@ #include "tdb1_private.h" /* read a freelist record and check for simple errors */ -int tdb1_rec_free_read(struct tdb1_context *tdb, tdb1_off_t off, struct tdb1_record *rec) +int tdb1_rec_free_read(struct tdb_context *tdb, tdb1_off_t off, struct tdb1_record *rec) { - if (tdb->methods->tdb1_read(tdb, off, rec, sizeof(*rec),TDB1_DOCONV()) == -1) + if (tdb->tdb1.io->tdb1_read(tdb, off, rec, sizeof(*rec),TDB1_DOCONV()) == -1) return -1; if (rec->magic == TDB1_MAGIC) { /* this happens when a app is showdown while deleting a record - we should not completely fail when this happens */ - TDB1_LOG((tdb, TDB1_DEBUG_WARNING, "tdb1_rec_free_read non-free magic 0x%x at offset=%d - fixing\n", - rec->magic, off)); + tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_WARNING, + "tdb1_rec_free_read non-free magic 0x%x at offset=%d - fixing\n", + rec->magic, off); rec->magic = TDB1_FREE_MAGIC; - if (tdb->methods->tdb1_write(tdb, off, rec, sizeof(*rec)) == -1) + if (tdb->tdb1.io->tdb1_write(tdb, off, rec, sizeof(*rec)) == -1) return -1; } if (rec->magic != TDB1_FREE_MAGIC) { - /* Ensure ecode is set for log fn. */ - tdb->ecode = TDB1_ERR_CORRUPT; - TDB1_LOG((tdb, TDB1_DEBUG_WARNING, "tdb1_rec_free_read bad magic 0x%x at offset=%d\n", - rec->magic, off)); + tdb->last_error = tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR, + "tdb1_rec_free_read bad magic 0x%x at offset=%d\n", + rec->magic, off); return -1; } - if (tdb->methods->tdb1_oob(tdb, rec->next+sizeof(*rec), 0) != 0) + if (tdb->tdb1.io->tdb1_oob(tdb, rec->next+sizeof(*rec), 0) != 0) return -1; return 0; } /* update a record tailer (must hold allocation lock) */ -static int update_tailer(struct tdb1_context *tdb, tdb1_off_t offset, +static int update_tailer(struct tdb_context *tdb, tdb1_off_t offset, const struct tdb1_record *rec) { tdb1_off_t totalsize; @@ -70,7 +70,7 @@ static int update_tailer(struct tdb1_context *tdb, tdb1_off_t offset, /* Add an element into the freelist. Merge adjacent records if necessary. */ -int tdb1_free(struct tdb1_context *tdb, tdb1_off_t offset, struct tdb1_record *rec) +int tdb1_free(struct tdb_context *tdb, tdb1_off_t offset, struct tdb1_record *rec) { /* Allocation and tailer lock */ if (tdb1_lock(tdb, -1, F_WRLCK) != 0) @@ -78,19 +78,22 @@ int tdb1_free(struct tdb1_context *tdb, tdb1_off_t offset, struct tdb1_record *r /* set an initial tailer, so if we fail we don't leave a bogus record */ if (update_tailer(tdb, offset, rec) != 0) { - TDB1_LOG((tdb, TDB1_DEBUG_FATAL, "tdb1_free: update_tailer failed!\n")); + tdb_logerr(tdb, tdb->last_error, TDB_LOG_ERROR, + "tdb_free: update_tailer failed!\n"); goto fail; } + tdb->stats.alloc_coalesce_tried++; /* Look left */ - if (offset - sizeof(tdb1_off_t) > TDB1_DATA_START(tdb->header.hash_size)) { + if (offset - sizeof(tdb1_off_t) > TDB1_DATA_START(tdb->tdb1.header.hash_size)) { tdb1_off_t left = offset - sizeof(tdb1_off_t); struct tdb1_record l; tdb1_off_t leftsize; /* Read in tailer and jump back to header */ if (tdb1_ofs_read(tdb, left, &leftsize) == -1) { - TDB1_LOG((tdb, TDB1_DEBUG_FATAL, "tdb1_free: left offset read failed at %u\n", left)); + tdb_logerr(tdb, tdb->last_error, TDB_LOG_ERROR, + "tdb1_free: left offset read failed at %u", left); goto update; } @@ -102,13 +105,14 @@ int tdb1_free(struct tdb1_context *tdb, tdb1_off_t offset, struct tdb1_record *r left = offset - leftsize; if (leftsize > offset || - left < TDB1_DATA_START(tdb->header.hash_size)) { + left < TDB1_DATA_START(tdb->tdb1.header.hash_size)) { goto update; } /* Now read in the left record */ - if (tdb->methods->tdb1_read(tdb, left, &l, sizeof(l), TDB1_DOCONV()) == -1) { - TDB1_LOG((tdb, TDB1_DEBUG_FATAL, "tdb1_free: left read failed at %u (%u)\n", left, leftsize)); + if (tdb->tdb1.io->tdb1_read(tdb, left, &l, sizeof(l), TDB1_DOCONV()) == -1) { + tdb_logerr(tdb, tdb->last_error, TDB_LOG_ERROR, + "tdb1_free: left read failed at %u (%u)", left, leftsize); goto update; } @@ -119,13 +123,18 @@ int tdb1_free(struct tdb1_context *tdb, tdb1_off_t offset, struct tdb1_record *r prevents traverse from being O(n^2) after a lot of deletes */ l.rec_len += sizeof(*rec) + rec->rec_len; if (tdb1_rec_write(tdb, left, &l) == -1) { - TDB1_LOG((tdb, TDB1_DEBUG_FATAL, "tdb1_free: update_left failed at %u\n", left)); + tdb_logerr(tdb, tdb->last_error, TDB_LOG_ERROR, + "tdb1_free: update_left failed at %u", left); goto fail; } if (update_tailer(tdb, left, &l) == -1) { - TDB1_LOG((tdb, TDB1_DEBUG_FATAL, "tdb1_free: update_tailer failed at %u\n", offset)); + tdb_logerr(tdb, tdb->last_error, TDB_LOG_ERROR, + "tdb1_free: update_tailer failed at %u", offset); goto fail; } + tdb->stats.alloc_coalesce_succeeded++; + tdb->stats.alloc_coalesce_num_merged++; + tdb->stats.frees++; tdb1_unlock(tdb, -1, F_WRLCK); return 0; } @@ -139,11 +148,14 @@ update: if (tdb1_ofs_read(tdb, TDB1_FREELIST_TOP, &rec->next) == -1 || tdb1_rec_write(tdb, offset, rec) == -1 || tdb1_ofs_write(tdb, TDB1_FREELIST_TOP, &offset) == -1) { - TDB1_LOG((tdb, TDB1_DEBUG_FATAL, "tdb1_free record write failed at offset=%d\n", offset)); + tdb_logerr(tdb, tdb->last_error, TDB_LOG_ERROR, + "tdb1_free record write failed at offset=%d", + offset); goto fail; } /* And we're done. */ + tdb->stats.frees++; tdb1_unlock(tdb, -1, F_WRLCK); return 0; @@ -162,7 +174,7 @@ update: not the beginning. This is so the left merge in a free is more likely to be able to free up the record without fragmentation */ -static tdb1_off_t tdb1_allocate_ofs(struct tdb1_context *tdb, +static tdb1_off_t tdb1_allocate_ofs(struct tdb_context *tdb, tdb1_len_t length, tdb1_off_t rec_ptr, struct tdb1_record *rec, tdb1_off_t last_ptr) { @@ -181,6 +193,7 @@ static tdb1_off_t tdb1_allocate_ofs(struct tdb1_context *tdb, if (tdb1_rec_write(tdb, rec_ptr, rec) == -1) { return 0; } + tdb->stats.allocs++; return rec_ptr; } @@ -208,6 +221,8 @@ static tdb1_off_t tdb1_allocate_ofs(struct tdb1_context *tdb, return 0; } + tdb->stats.allocs++; + tdb->stats.alloc_leftover++; return rec_ptr; } @@ -217,7 +232,7 @@ static tdb1_off_t tdb1_allocate_ofs(struct tdb1_context *tdb, 0 is returned if the space could not be allocated */ -tdb1_off_t tdb1_allocate(struct tdb1_context *tdb, tdb1_len_t length, struct tdb1_record *rec) +tdb1_off_t tdb1_allocate(struct tdb_context *tdb, tdb1_len_t length, struct tdb1_record *rec) { tdb1_off_t rec_ptr, last_ptr, newrec_ptr; struct {