2 Trivial Database 2: free list/block handling
3 Copyright (C) Rusty Russell 2010
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 3 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 #include <ccan/likely/likely.h>
20 #include <ccan/ilog/ilog.h>
25 static unsigned fls64(uint64_t val)
30 /* In which bucket would we find a particular record size? (ignoring header) */
31 unsigned int size_to_bucket(tdb_len_t data_len)
35 /* We can't have records smaller than this. */
36 assert(data_len >= TDB_MIN_DATA_LEN);
38 /* Ignoring the header... */
39 if (data_len - TDB_MIN_DATA_LEN <= 64) {
40 /* 0 in bucket 0, 8 in bucket 1... 64 in bucket 8. */
41 bucket = (data_len - TDB_MIN_DATA_LEN) / 8;
43 /* After that we go power of 2. */
44 bucket = fls64(data_len - TDB_MIN_DATA_LEN) + 2;
47 if (unlikely(bucket >= TDB_FREE_BUCKETS))
48 bucket = TDB_FREE_BUCKETS - 1;
52 int tdb_flist_init(struct tdb_context *tdb)
54 tdb->flist_off = tdb_read_off(tdb,
55 offsetof(struct tdb_header, free_list));
56 if (tdb->flist_off == TDB_OFF_ERR)
61 /* Offset of a given bucket. */
62 tdb_off_t bucket_off(tdb_off_t flist_off, unsigned bucket)
64 return flist_off + offsetof(struct tdb_freelist, buckets)
65 + bucket * sizeof(tdb_off_t);
68 /* Returns free_buckets + 1, or list number to search. */
69 static tdb_off_t find_free_head(struct tdb_context *tdb, tdb_off_t bucket)
71 /* Speculatively search for a non-zero bucket. */
72 return tdb_find_nonzero_off(tdb, bucket_off(tdb->flist_off, 0),
73 bucket, TDB_FREE_BUCKETS);
76 /* Remove from free bucket. */
77 static int remove_from_list(struct tdb_context *tdb,
78 tdb_off_t b_off, tdb_off_t r_off,
79 struct tdb_free_record *r)
87 off = r->prev + offsetof(struct tdb_free_record, next);
91 if (tdb_read_off(tdb, off) != r_off) {
92 tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
93 "remove_from_list: %llu bad prev in list %llu\n",
94 (long long)r_off, (long long)b_off);
99 /* r->prev->next = r->next */
100 if (tdb_write_off(tdb, off, r->next)) {
105 off = r->next + offsetof(struct tdb_free_record, prev);
106 /* r->next->prev = r->prev */
109 if (tdb_read_off(tdb, off) != r_off) {
110 tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
111 "remove_from_list: %llu bad list %llu\n",
112 (long long)r_off, (long long)b_off);
117 if (tdb_write_off(tdb, off, r->prev)) {
124 /* Enqueue in this free bucket. */
125 static int enqueue_in_free(struct tdb_context *tdb,
128 struct tdb_free_record *new)
131 /* new->next = head. */
132 new->next = tdb_read_off(tdb, b_off);
133 if (new->next == TDB_OFF_ERR)
138 if (tdb_read_off(tdb,
140 + offsetof(struct tdb_free_record, prev))
142 tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
143 "enqueue_in_free: %llu bad head prev %llu\n",
144 (long long)new->next, (long long)b_off);
148 /* next->prev = new. */
149 if (tdb_write_off(tdb, new->next
150 + offsetof(struct tdb_free_record, prev),
155 if (tdb_write_off(tdb, b_off, off) != 0)
158 return tdb_write_convert(tdb, off, new, sizeof(*new));
161 /* List need not be locked. */
162 int add_free_record(struct tdb_context *tdb,
163 tdb_off_t off, tdb_len_t len_with_header)
165 struct tdb_free_record new;
169 assert(len_with_header >= sizeof(new));
171 new.magic_and_meta = TDB_FREE_MAGIC;
172 new.data_len = len_with_header - sizeof(struct tdb_used_record);
174 b_off = bucket_off(tdb->flist_off, size_to_bucket(new.data_len));
175 if (tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) != 0)
178 ret = enqueue_in_free(tdb, b_off, off, &new);
179 tdb_unlock_free_bucket(tdb, b_off);
183 static size_t adjust_size(size_t keylen, size_t datalen)
185 size_t size = keylen + datalen;
187 if (size < TDB_MIN_DATA_LEN)
188 size = TDB_MIN_DATA_LEN;
190 /* Round to next uint64_t boundary. */
191 return (size + (sizeof(uint64_t) - 1ULL)) & ~(sizeof(uint64_t) - 1ULL);
194 /* If we have enough left over to be useful, split that off. */
195 static size_t record_leftover(size_t keylen, size_t datalen,
196 bool want_extra, size_t total_len)
201 datalen += datalen / 2;
202 leftover = total_len - adjust_size(keylen, datalen);
204 if (leftover < (ssize_t)sizeof(struct tdb_free_record))
210 /* Note: we unlock the current bucket if we coalesce or fail. */
211 static int coalesce(struct tdb_context *tdb,
212 tdb_off_t off, tdb_off_t b_off, tdb_len_t data_len)
214 struct tdb_free_record pad, *r;
217 end = off + sizeof(struct tdb_used_record) + data_len;
219 while (end < tdb->map_size) {
222 /* FIXME: do tdb_get here and below really win? */
223 r = tdb_get(tdb, end, &pad, sizeof(pad));
227 if (frec_magic(r) != TDB_FREE_MAGIC)
230 /* FIXME: Use flist from record */
231 nb_off = bucket_off(tdb->flist_off,size_to_bucket(r->data_len));
233 /* We may be violating lock order here, so best effort. */
234 if (tdb_lock_free_bucket(tdb, nb_off, TDB_LOCK_NOWAIT) == -1)
237 /* Now we have lock, re-check. */
238 r = tdb_get(tdb, end, &pad, sizeof(pad));
240 tdb_unlock_free_bucket(tdb, nb_off);
244 if (unlikely(frec_magic(r) != TDB_FREE_MAGIC)) {
245 tdb_unlock_free_bucket(tdb, nb_off);
249 if (unlikely(bucket_off(tdb->flist_off,
250 size_to_bucket(r->data_len))
252 tdb_unlock_free_bucket(tdb, nb_off);
256 if (remove_from_list(tdb, nb_off, end, r) == -1) {
257 tdb_unlock_free_bucket(tdb, nb_off);
261 end += sizeof(struct tdb_used_record) + r->data_len;
262 tdb_unlock_free_bucket(tdb, nb_off);
265 /* Didn't find any adjacent free? */
266 if (end == off + sizeof(struct tdb_used_record) + data_len)
269 /* OK, expand record */
270 r = tdb_get(tdb, off, &pad, sizeof(pad));
274 if (r->data_len != data_len) {
275 tdb->ecode = TDB_ERR_CORRUPT;
276 tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
277 "coalesce: expected data len %llu not %llu\n",
278 (long long)data_len, (long long)r->data_len);
282 if (remove_from_list(tdb, b_off, off, r) == -1)
285 r = tdb_access_write(tdb, off, sizeof(*r), true);
289 /* We have to drop this to avoid deadlocks, so make sure record
290 * doesn't get coalesced by someone else! */
291 r->magic_and_meta = TDB_COALESCING_MAGIC;
292 r->data_len = end - off - sizeof(struct tdb_used_record);
293 if (tdb_access_commit(tdb, r) != 0)
296 tdb_unlock_free_bucket(tdb, b_off);
298 if (add_free_record(tdb, off, end - off) == -1)
303 /* To unify error paths, we *always* unlock bucket on error. */
304 tdb_unlock_free_bucket(tdb, b_off);
308 /* We need size bytes to put our key and data in. */
309 static tdb_off_t lock_and_alloc(struct tdb_context *tdb,
312 size_t keylen, size_t datalen,
316 tdb_off_t off, b_off,best_off;
317 struct tdb_free_record pad, best = { 0 }, *r;
319 size_t size = adjust_size(keylen, datalen);
322 b_off = bucket_off(flist_off, bucket);
324 /* FIXME: Try non-blocking wait first, to measure contention. */
325 /* Lock this bucket. */
326 if (tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == -1) {
330 best.data_len = -1ULL;
333 /* Get slack if we're after extra. */
339 /* Walk the list to see if any are large enough, getting less fussy
341 off = tdb_read_off(tdb, b_off);
342 if (unlikely(off == TDB_OFF_ERR))
346 /* FIXME: Does tdb_get win anything here? */
347 r = tdb_get(tdb, off, &pad, sizeof(*r));
351 if (frec_magic(r) != TDB_FREE_MAGIC) {
352 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
353 "lock_and_alloc: %llu non-free 0x%llx\n",
354 (long long)off, (long long)r->magic_and_meta);
358 if (r->data_len >= size && r->data_len < best.data_len) {
363 if (best.data_len < size * multiplier && best_off)
368 /* Since we're going slow anyway, try coalescing here. */
369 switch (coalesce(tdb, off, b_off, r->data_len)) {
371 /* This has already unlocked on error. */
374 /* This has unlocked list, restart. */
380 /* If we found anything at all, use it. */
382 struct tdb_used_record rec;
385 /* We're happy with this size: take it. */
386 if (remove_from_list(tdb, b_off, best_off, &best) != 0)
389 leftover = record_leftover(keylen, datalen, want_extra,
392 assert(keylen + datalen + leftover <= best.data_len);
393 /* We need to mark non-free before we drop lock, otherwise
394 * coalesce() could try to merge it! */
395 if (set_header(tdb, &rec, keylen, datalen,
396 best.data_len - leftover,
400 if (tdb_write_convert(tdb, best_off, &rec, sizeof(rec)) != 0)
403 tdb_unlock_free_bucket(tdb, b_off);
406 if (add_free_record(tdb,
407 best_off + sizeof(rec)
408 + best.data_len - leftover,
415 tdb_unlock_free_bucket(tdb, b_off);
419 tdb_unlock_free_bucket(tdb, b_off);
423 /* Get a free block from current free list, or 0 if none. */
424 static tdb_off_t get_free(struct tdb_context *tdb,
425 size_t keylen, size_t datalen, bool want_extra,
431 /* If they are growing, add 50% to get to higher bucket. */
433 start_b = size_to_bucket(adjust_size(keylen,
434 datalen + datalen / 2));
436 start_b = size_to_bucket(adjust_size(keylen, datalen));
438 /* Start at exact size bucket, and search up... */
439 for (b = find_free_head(tdb, start_b);
440 b < TDB_FREE_BUCKETS;
441 b = find_free_head(tdb, b + 1)) {
442 /* Try getting one from list. */
443 off = lock_and_alloc(tdb, tdb->flist_off,
444 b, keylen, datalen, want_extra,
446 if (off == TDB_OFF_ERR)
450 /* Didn't work. Try next bucket. */
455 int set_header(struct tdb_context *tdb,
456 struct tdb_used_record *rec,
457 uint64_t keylen, uint64_t datalen,
458 uint64_t actuallen, unsigned hashlow)
460 uint64_t keybits = (fls64(keylen) + 1) / 2;
462 /* Use bottom bits of hash, so it's independent of hash table size. */
463 rec->magic_and_meta = (hashlow & ((1 << 11)-1))
464 | ((actuallen - (keylen + datalen)) << 11)
467 rec->key_and_data_len = (keylen | (datalen << (keybits*2)));
469 /* Encoding can fail on big values. */
470 if (rec_key_length(rec) != keylen
471 || rec_data_length(rec) != datalen
472 || rec_extra_padding(rec) != actuallen - (keylen + datalen)) {
473 tdb->ecode = TDB_ERR_IO;
474 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
475 "Could not encode k=%llu,d=%llu,a=%llu\n",
476 (long long)keylen, (long long)datalen,
477 (long long)actuallen);
483 /* Expand the database. */
484 static int tdb_expand(struct tdb_context *tdb, tdb_len_t size)
489 /* We need room for the record header too. */
490 wanted = sizeof(struct tdb_used_record) + size;
492 /* Only one person can expand file at a time. */
493 if (tdb_lock_expand(tdb, F_WRLCK) != 0)
496 /* Someone else may have expanded the file, so retry. */
497 old_size = tdb->map_size;
498 tdb->methods->oob(tdb, tdb->map_size + 1, true);
499 if (tdb->map_size != old_size) {
500 tdb_unlock_expand(tdb, F_WRLCK);
504 if (tdb->methods->expand_file(tdb, wanted*TDB_EXTENSION_FACTOR) == -1) {
505 tdb_unlock_expand(tdb, F_WRLCK);
509 /* We need to drop this lock before adding free record. */
510 tdb_unlock_expand(tdb, F_WRLCK);
512 return add_free_record(tdb, old_size, wanted * TDB_EXTENSION_FACTOR);
515 /* This won't fail: it will expand the database if it has to. */
516 tdb_off_t alloc(struct tdb_context *tdb, size_t keylen, size_t datalen,
517 uint64_t hash, bool growing)
521 /* We can't hold pointers during this: we could unmap! */
522 assert(!tdb->direct_access);
525 off = get_free(tdb, keylen, datalen, growing, hash);
526 if (likely(off != 0))
529 if (tdb_expand(tdb, adjust_size(keylen, datalen)))