X-Git-Url: https://git.ozlabs.org/?a=blobdiff_plain;f=ccan%2Ftdb2%2Ftdb1_freelist.c;h=cf2eeb7febac18e790bb1e20726de77d986bc363;hb=2b50be90a23893a06e0f1436a31d18b97ad0e11d;hp=9c40bc9e331aab7b155a5a638a863e453da21c0b;hpb=19e6c1a250ade1e7204ada17163294855585e825;p=ccan diff --git a/ccan/tdb2/tdb1_freelist.c b/ccan/tdb2/tdb1_freelist.c index 9c40bc9e..cf2eeb7f 100644 --- a/ccan/tdb2/tdb1_freelist.c +++ b/ccan/tdb2/tdb1_freelist.c @@ -27,12 +27,6 @@ #include "tdb1_private.h" -/* 'right' merges can involve O(n^2) cost when combined with a - traverse, so they are disabled until we find a way to do them in - O(1) time -*/ -#define USE_RIGHT_MERGES 0 - /* 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) { @@ -42,18 +36,18 @@ int tdb1_rec_free_read(struct tdb1_context *tdb, tdb1_off_t off, struct tdb1_rec 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) 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) @@ -62,29 +56,6 @@ int tdb1_rec_free_read(struct tdb1_context *tdb, tdb1_off_t off, struct tdb1_rec } -#if USE_RIGHT_MERGES -/* Remove an element from the freelist. Must have alloc lock. */ -static int remove_from_freelist(struct tdb1_context *tdb, tdb1_off_t off, tdb1_off_t next) -{ - tdb1_off_t last_ptr, i; - - /* read in the freelist top */ - last_ptr = TDB1_FREELIST_TOP; - while (tdb1_ofs_read(tdb, last_ptr, &i) != -1 && i != 0) { - if (i == off) { - /* We've found it! */ - return tdb1_ofs_write(tdb, last_ptr, &next); - } - /* Follow chain (next offset is at start of record) */ - last_ptr = i; - } - tdb->ecode = TDB1_ERR_CORRUPT; - TDB1_LOG((tdb, TDB1_DEBUG_FATAL,"remove_from_freelist: not on list at off=%d\n", off)); - return -1; -} -#endif - - /* update a record tailer (must hold allocation lock) */ static int update_tailer(struct tdb1_context *tdb, tdb1_off_t offset, const struct tdb1_record *rec) @@ -107,37 +78,11 @@ 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; } -#if USE_RIGHT_MERGES - /* Look right first (I'm an Australian, dammit) */ - if (offset + sizeof(*rec) + rec->rec_len + sizeof(*rec) <= tdb->map_size) { - tdb1_off_t right = offset + sizeof(*rec) + rec->rec_len; - struct tdb1_record r; - - if (tdb->methods->tdb1_read(tdb, right, &r, sizeof(r), TDB1_DOCONV()) == -1) { - TDB1_LOG((tdb, TDB1_DEBUG_FATAL, "tdb1_free: right read failed at %u\n", right)); - goto left; - } - - /* If it's free, expand to include it. */ - if (r.magic == TDB1_FREE_MAGIC) { - if (remove_from_freelist(tdb, right, r.next) == -1) { - TDB1_LOG((tdb, TDB1_DEBUG_FATAL, "tdb1_free: right free failed at %u\n", right)); - goto left; - } - rec->rec_len += sizeof(r) + r.rec_len; - if (update_tailer(tdb, offset, rec) == -1) { - TDB1_LOG((tdb, TDB1_DEBUG_FATAL, "tdb1_free: update_tailer failed at %u\n", offset)); - goto fail; - } - } - } -left: -#endif - /* Look left */ if (offset - sizeof(tdb1_off_t) > TDB1_DATA_START(tdb->header.hash_size)) { tdb1_off_t left = offset - sizeof(tdb1_off_t); @@ -146,7 +91,8 @@ left: /* 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; } @@ -164,7 +110,8 @@ left: /* 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)); + tdb_logerr(tdb, tdb->last_error, TDB_LOG_ERROR, + "tdb1_free: left read failed at %u (%u)", left, leftsize); goto update; } @@ -175,11 +122,13 @@ left: 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; } tdb1_unlock(tdb, -1, F_WRLCK); @@ -195,7 +144,9 @@ 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; } @@ -361,26 +312,3 @@ tdb1_off_t tdb1_allocate(struct tdb1_context *tdb, tdb1_len_t length, struct tdb tdb1_unlock(tdb, -1, F_WRLCK); return 0; } - - - -/* - return the size of the freelist - used to decide if we should repack -*/ -_PUBLIC_ int tdb1_freelist_size(struct tdb1_context *tdb) -{ - tdb1_off_t ptr; - int count=0; - - if (tdb1_lock(tdb, -1, F_RDLCK) == -1) { - return -1; - } - - ptr = TDB1_FREELIST_TOP; - while (tdb1_ofs_read(tdb, ptr, &ptr) == 0 && ptr != 0) { - count++; - } - - tdb1_unlock(tdb, -1, F_RDLCK); - return count; -}