#include "tdb_private.h"
int rec_free_read(struct tdb_context *tdb, tdb_off_t off, struct list_struct *rec)
{
if (tdb->methods->tdb_read(tdb, off, rec, sizeof(*rec),DOCONV()) == -1)
return -1;
if (rec->magic == TDB_MAGIC) {
TDB_LOG((tdb, TDB_DEBUG_WARNING, "rec_free_read non-free magic 0x%x at offset=%d - fixing\n",
rec->magic, off));
rec->magic = TDB_FREE_MAGIC;
if (tdb->methods->tdb_write(tdb, off, rec, sizeof(*rec)) == -1)
return -1;
}
if (rec->magic != TDB_FREE_MAGIC) {
tdb->ecode = TDB_ERR_CORRUPT;
TDB_LOG((tdb, TDB_DEBUG_WARNING, "rec_free_read bad magic 0x%x at offset=%d\n",
rec->magic, off));
return TDB_ERRCODE(TDB_ERR_CORRUPT, -1);
}
if (tdb->methods->tdb_oob(tdb, rec->next+sizeof(*rec), 0) != 0)
return -1;
return 0;
}
static int remove_from_freelist(struct tdb_context *tdb, tdb_off_t off, tdb_off_t next)
{
tdb_off_t last_ptr, i;
last_ptr = FREELIST_TOP;
while (tdb_ofs_read(tdb, last_ptr, &i) != -1 && i != 0) {
if (i == off) {
return tdb_ofs_write(tdb, last_ptr, &next);
}
last_ptr = i;
}
TDB_LOG((tdb, TDB_DEBUG_FATAL,"remove_from_freelist: not on list at off=%d\n", off));
return TDB_ERRCODE(TDB_ERR_CORRUPT, -1);
}
static int update_tailer(struct tdb_context *tdb, tdb_off_t offset,
const struct list_struct *rec)
{
tdb_off_t totalsize;
totalsize = sizeof(*rec) + rec->rec_len;
return tdb_ofs_write(tdb, offset + totalsize - sizeof(tdb_off_t),
&totalsize);
}
int tdb_free(struct tdb_context *tdb, tdb_off_t offset, struct list_struct *rec)
{
tdb_off_t right, left;
if (tdb_lock(tdb, -1, F_WRLCK) != 0)
return -1;
if (update_tailer(tdb, offset, rec) != 0) {
TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_free: update_tailer failed!\n"));
goto fail;
}
right = offset + sizeof(*rec) + rec->rec_len;
if (right + sizeof(*rec) <= tdb->map_size) {
struct list_struct r;
if (tdb->methods->tdb_read(tdb, right, &r, sizeof(r), DOCONV()) == -1) {
TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_free: right read failed at %u\n", right));
goto left;
}
if (r.magic == TDB_FREE_MAGIC) {
if (remove_from_freelist(tdb, right, r.next) == -1) {
TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_free: right free failed at %u\n", right));
goto left;
}
rec->rec_len += sizeof(r) + r.rec_len;
}
}
left:
left = offset - sizeof(tdb_off_t);
if (left > TDB_DATA_START(tdb->header.hash_size)) {
struct list_struct l;
tdb_off_t leftsize;
if (tdb_ofs_read(tdb, left, &leftsize) == -1) {
TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_free: left offset read failed at %u\n", left));
goto update;
}
if (leftsize == 0 || leftsize == TDB_PAD_U32) {
goto update;
}
left = offset - leftsize;
if (tdb->methods->tdb_read(tdb, left, &l, sizeof(l), DOCONV()) == -1) {
TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_free: left read failed at %u (%u)\n", left, leftsize));
goto update;
}
if (l.magic == TDB_FREE_MAGIC) {
if (remove_from_freelist(tdb, left, l.next) == -1) {
TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_free: left free failed at %u\n", left));
goto update;
} else {
offset = left;
rec->rec_len += leftsize;
}
}
}
update:
if (update_tailer(tdb, offset, rec) == -1) {
TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_free: update_tailer failed at %u\n", offset));
goto fail;
}
rec->magic = TDB_FREE_MAGIC;
if (tdb_ofs_read(tdb, FREELIST_TOP, &rec->next) == -1 ||
tdb_rec_write(tdb, offset, rec) == -1 ||
tdb_ofs_write(tdb, FREELIST_TOP, &offset) == -1) {
TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_free record write failed at offset=%d\n", offset));
goto fail;
}
tdb_unlock(tdb, -1, F_WRLCK);
return 0;
fail:
tdb_unlock(tdb, -1, F_WRLCK);
return -1;
}
static tdb_off_t tdb_allocate_ofs(struct tdb_context *tdb, tdb_len_t length, tdb_off_t rec_ptr,
struct list_struct *rec, tdb_off_t last_ptr)
{
struct list_struct newrec;
tdb_off_t newrec_ptr;
memset(&newrec, '\0', sizeof(newrec));
if (rec->rec_len > length + MIN_REC_SIZE) {
length = TDB_ALIGN(length, TDB_ALIGNMENT);
newrec.rec_len = rec->rec_len - (sizeof(*rec) + length);
newrec_ptr = rec_ptr + sizeof(*rec) + length;
rec->rec_len = length;
} else {
newrec_ptr = 0;
}
if (tdb_ofs_write(tdb, last_ptr, &rec->next) == -1) {
return 0;
}
rec->magic = TDB_MAGIC;
if (tdb_rec_write(tdb, rec_ptr, rec) == -1) {
return 0;
}
if (newrec_ptr) {
if (update_tailer(tdb, rec_ptr, rec) == -1) {
return 0;
}
if (tdb_free(tdb, newrec_ptr, &newrec) == -1) {
return 0;
}
}
return rec_ptr;
}
tdb_off_t tdb_allocate(struct tdb_context *tdb, tdb_len_t length, struct list_struct *rec)
{
tdb_off_t rec_ptr, last_ptr, newrec_ptr;
struct {
tdb_off_t rec_ptr, last_ptr;
tdb_len_t rec_len;
} bestfit;
if (tdb_lock(tdb, -1, F_WRLCK) == -1)
return 0;
length += sizeof(tdb_off_t);
again:
last_ptr = FREELIST_TOP;
if (tdb_ofs_read(tdb, FREELIST_TOP, &rec_ptr) == -1)
goto fail;
bestfit.rec_ptr = 0;
bestfit.last_ptr = 0;
bestfit.rec_len = 0;
while (rec_ptr) {
if (rec_free_read(tdb, rec_ptr, rec) == -1) {
goto fail;
}
if (rec->rec_len >= length) {
if (bestfit.rec_ptr == 0 ||
rec->rec_len < bestfit.rec_len) {
bestfit.rec_len = rec->rec_len;
bestfit.rec_ptr = rec_ptr;
bestfit.last_ptr = last_ptr;
if (bestfit.rec_len < 2*length) {
break;
}
}
}
last_ptr = rec_ptr;
rec_ptr = rec->next;
}
if (bestfit.rec_ptr != 0) {
if (rec_free_read(tdb, bestfit.rec_ptr, rec) == -1) {
goto fail;
}
newrec_ptr = tdb_allocate_ofs(tdb, length, bestfit.rec_ptr, rec, bestfit.last_ptr);
tdb_unlock(tdb, -1, F_WRLCK);
return newrec_ptr;
}
if (tdb_expand(tdb, length + sizeof(*rec)) == 0)
goto again;
fail:
tdb_unlock(tdb, -1, F_WRLCK);
return 0;
}