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 tdb_off_t first_flist(struct tdb_context *tdb)
54 return tdb_read_off(tdb, offsetof(struct tdb_header, free_list));
57 tdb_off_t next_flist(struct tdb_context *tdb, tdb_off_t flist)
59 return tdb_read_off(tdb, flist + offsetof(struct tdb_freelist, next));
62 int tdb_flist_init(struct tdb_context *tdb)
64 /* Use reservoir sampling algorithm to select a free list at random. */
65 unsigned int rnd, max = 0;
68 tdb->flist_off = off = first_flist(tdb);
71 if (off == TDB_OFF_ERR)
80 off = next_flist(tdb, off);
85 /* Offset of a given bucket. */
86 tdb_off_t bucket_off(tdb_off_t flist_off, unsigned bucket)
88 return flist_off + offsetof(struct tdb_freelist, buckets)
89 + bucket * sizeof(tdb_off_t);
92 /* Returns free_buckets + 1, or list number to search. */
93 static tdb_off_t find_free_head(struct tdb_context *tdb,
97 /* Speculatively search for a non-zero bucket. */
98 return tdb_find_nonzero_off(tdb, bucket_off(flist_off, 0),
99 bucket, TDB_FREE_BUCKETS);
102 /* Remove from free bucket. */
103 static int remove_from_list(struct tdb_context *tdb,
104 tdb_off_t b_off, tdb_off_t r_off,
105 struct tdb_free_record *r)
113 off = r->prev + offsetof(struct tdb_free_record, next);
117 if (tdb_read_off(tdb, off) != r_off) {
118 tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
119 "remove_from_list: %llu bad prev in list %llu\n",
120 (long long)r_off, (long long)b_off);
125 /* r->prev->next = r->next */
126 if (tdb_write_off(tdb, off, r->next)) {
131 off = r->next + offsetof(struct tdb_free_record, prev);
132 /* r->next->prev = r->prev */
135 if (tdb_read_off(tdb, off) != r_off) {
136 tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
137 "remove_from_list: %llu bad list %llu\n",
138 (long long)r_off, (long long)b_off);
143 if (tdb_write_off(tdb, off, r->prev)) {
150 /* Enqueue in this free bucket. */
151 static int enqueue_in_free(struct tdb_context *tdb,
154 struct tdb_free_record *new)
157 /* new->next = head. */
158 new->next = tdb_read_off(tdb, b_off);
159 if (new->next == TDB_OFF_ERR)
164 if (tdb_read_off(tdb,
166 + offsetof(struct tdb_free_record, prev))
168 tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
169 "enqueue_in_free: %llu bad head prev %llu\n",
170 (long long)new->next, (long long)b_off);
174 /* next->prev = new. */
175 if (tdb_write_off(tdb, new->next
176 + offsetof(struct tdb_free_record, prev),
181 if (tdb_write_off(tdb, b_off, off) != 0)
184 return tdb_write_convert(tdb, off, new, sizeof(*new));
187 /* List need not be locked. */
188 int add_free_record(struct tdb_context *tdb,
189 tdb_off_t off, tdb_len_t len_with_header)
191 struct tdb_free_record new;
195 assert(len_with_header >= sizeof(new));
197 new.magic_and_meta = TDB_FREE_MAGIC << (64 - TDB_OFF_UPPER_STEAL)
199 new.data_len = len_with_header - sizeof(struct tdb_used_record);
201 b_off = bucket_off(tdb->flist_off, size_to_bucket(new.data_len));
202 if (tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) != 0)
205 ret = enqueue_in_free(tdb, b_off, off, &new);
206 tdb_unlock_free_bucket(tdb, b_off);
210 static size_t adjust_size(size_t keylen, size_t datalen)
212 size_t size = keylen + datalen;
214 if (size < TDB_MIN_DATA_LEN)
215 size = TDB_MIN_DATA_LEN;
217 /* Round to next uint64_t boundary. */
218 return (size + (sizeof(uint64_t) - 1ULL)) & ~(sizeof(uint64_t) - 1ULL);
221 /* If we have enough left over to be useful, split that off. */
222 static size_t record_leftover(size_t keylen, size_t datalen,
223 bool want_extra, size_t total_len)
228 datalen += datalen / 2;
229 leftover = total_len - adjust_size(keylen, datalen);
231 if (leftover < (ssize_t)sizeof(struct tdb_free_record))
237 /* Note: we unlock the current bucket if we coalesce or fail. */
238 static int coalesce(struct tdb_context *tdb,
239 tdb_off_t off, tdb_off_t b_off, tdb_len_t data_len)
241 struct tdb_free_record pad, *r;
244 end = off + sizeof(struct tdb_used_record) + data_len;
246 while (end < tdb->map_size) {
249 /* FIXME: do tdb_get here and below really win? */
250 r = tdb_get(tdb, end, &pad, sizeof(pad));
254 if (frec_magic(r) != TDB_FREE_MAGIC)
257 nb_off = bucket_off(frec_flist(r), size_to_bucket(r->data_len));
259 /* We may be violating lock order here, so best effort. */
260 if (tdb_lock_free_bucket(tdb, nb_off, TDB_LOCK_NOWAIT) == -1)
263 /* Now we have lock, re-check. */
264 r = tdb_get(tdb, end, &pad, sizeof(pad));
266 tdb_unlock_free_bucket(tdb, nb_off);
270 if (unlikely(frec_magic(r) != TDB_FREE_MAGIC)) {
271 tdb_unlock_free_bucket(tdb, nb_off);
275 if (unlikely(bucket_off(frec_flist(r),
276 size_to_bucket(r->data_len))
278 tdb_unlock_free_bucket(tdb, nb_off);
282 if (remove_from_list(tdb, nb_off, end, r) == -1) {
283 tdb_unlock_free_bucket(tdb, nb_off);
287 end += sizeof(struct tdb_used_record) + r->data_len;
288 tdb_unlock_free_bucket(tdb, nb_off);
291 /* Didn't find any adjacent free? */
292 if (end == off + sizeof(struct tdb_used_record) + data_len)
295 /* OK, expand record */
296 r = tdb_get(tdb, off, &pad, sizeof(pad));
300 if (r->data_len != data_len) {
301 tdb->ecode = TDB_ERR_CORRUPT;
302 tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
303 "coalesce: expected data len %llu not %llu\n",
304 (long long)data_len, (long long)r->data_len);
308 if (remove_from_list(tdb, b_off, off, r) == -1)
311 r = tdb_access_write(tdb, off, sizeof(*r), true);
315 /* We have to drop this to avoid deadlocks, so make sure record
316 * doesn't get coalesced by someone else! */
317 r->magic_and_meta = TDB_COALESCING_MAGIC << (64 - TDB_OFF_UPPER_STEAL);
318 r->data_len = end - off - sizeof(struct tdb_used_record);
319 if (tdb_access_commit(tdb, r) != 0)
322 tdb_unlock_free_bucket(tdb, b_off);
324 if (add_free_record(tdb, off, end - off) == -1)
329 /* To unify error paths, we *always* unlock bucket on error. */
330 tdb_unlock_free_bucket(tdb, b_off);
334 /* We need size bytes to put our key and data in. */
335 static tdb_off_t lock_and_alloc(struct tdb_context *tdb,
338 size_t keylen, size_t datalen,
342 tdb_off_t off, b_off,best_off;
343 struct tdb_free_record pad, best = { 0 }, *r;
345 size_t size = adjust_size(keylen, datalen);
348 b_off = bucket_off(flist_off, bucket);
350 /* FIXME: Try non-blocking wait first, to measure contention. */
351 /* Lock this bucket. */
352 if (tdb_lock_free_bucket(tdb, b_off, TDB_LOCK_WAIT) == -1) {
356 best.data_len = -1ULL;
359 /* Get slack if we're after extra. */
365 /* Walk the list to see if any are large enough, getting less fussy
367 off = tdb_read_off(tdb, b_off);
368 if (unlikely(off == TDB_OFF_ERR))
372 /* FIXME: Does tdb_get win anything here? */
373 r = tdb_get(tdb, off, &pad, sizeof(*r));
377 if (frec_magic(r) != TDB_FREE_MAGIC) {
378 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
379 "lock_and_alloc: %llu non-free 0x%llx\n",
380 (long long)off, (long long)r->magic_and_meta);
384 if (r->data_len >= size && r->data_len < best.data_len) {
389 if (best.data_len < size * multiplier && best_off)
394 /* Since we're going slow anyway, try coalescing here. */
395 switch (coalesce(tdb, off, b_off, r->data_len)) {
397 /* This has already unlocked on error. */
400 /* This has unlocked list, restart. */
406 /* If we found anything at all, use it. */
408 struct tdb_used_record rec;
411 /* We're happy with this size: take it. */
412 if (remove_from_list(tdb, b_off, best_off, &best) != 0)
415 leftover = record_leftover(keylen, datalen, want_extra,
418 assert(keylen + datalen + leftover <= best.data_len);
419 /* We need to mark non-free before we drop lock, otherwise
420 * coalesce() could try to merge it! */
421 if (set_header(tdb, &rec, keylen, datalen,
422 best.data_len - leftover,
426 if (tdb_write_convert(tdb, best_off, &rec, sizeof(rec)) != 0)
429 tdb_unlock_free_bucket(tdb, b_off);
432 if (add_free_record(tdb,
433 best_off + sizeof(rec)
434 + best.data_len - leftover,
441 tdb_unlock_free_bucket(tdb, b_off);
445 tdb_unlock_free_bucket(tdb, b_off);
449 /* Get a free block from current free list, or 0 if none. */
450 static tdb_off_t get_free(struct tdb_context *tdb,
451 size_t keylen, size_t datalen, bool want_extra,
454 tdb_off_t off, flist;
456 bool wrapped = false;
458 /* If they are growing, add 50% to get to higher bucket. */
460 start_b = size_to_bucket(adjust_size(keylen,
461 datalen + datalen / 2));
463 start_b = size_to_bucket(adjust_size(keylen, datalen));
465 flist = tdb->flist_off;
466 while (!wrapped || flist != tdb->flist_off) {
467 /* Start at exact size bucket, and search up... */
468 for (b = find_free_head(tdb, flist, start_b);
469 b < TDB_FREE_BUCKETS;
470 b = find_free_head(tdb, flist, b + 1)) {
471 /* Try getting one from list. */
472 off = lock_and_alloc(tdb, flist,
473 b, keylen, datalen, want_extra,
475 if (off == TDB_OFF_ERR)
478 /* Worked? Stay using this list. */
479 tdb->flist_off = flist;
482 /* Didn't work. Try next bucket. */
485 /* Hmm, try next list. */
486 flist = next_flist(tdb, flist);
489 flist = first_flist(tdb);
496 int set_header(struct tdb_context *tdb,
497 struct tdb_used_record *rec,
498 uint64_t keylen, uint64_t datalen,
499 uint64_t actuallen, unsigned hashlow)
501 uint64_t keybits = (fls64(keylen) + 1) / 2;
503 /* Use bottom bits of hash, so it's independent of hash table size. */
504 rec->magic_and_meta = (hashlow & ((1 << 11)-1))
505 | ((actuallen - (keylen + datalen)) << 11)
508 rec->key_and_data_len = (keylen | (datalen << (keybits*2)));
510 /* Encoding can fail on big values. */
511 if (rec_key_length(rec) != keylen
512 || rec_data_length(rec) != datalen
513 || rec_extra_padding(rec) != actuallen - (keylen + datalen)) {
514 tdb->ecode = TDB_ERR_IO;
515 tdb->log(tdb, TDB_DEBUG_ERROR, tdb->log_priv,
516 "Could not encode k=%llu,d=%llu,a=%llu\n",
517 (long long)keylen, (long long)datalen,
518 (long long)actuallen);
524 /* Expand the database. */
525 static int tdb_expand(struct tdb_context *tdb, tdb_len_t size)
530 /* We need room for the record header too. */
531 wanted = sizeof(struct tdb_used_record) + size;
533 /* Need to hold a hash lock to expand DB: transactions rely on it. */
534 if (!(tdb->flags & TDB_NOLOCK)
535 && !tdb->allrecord_lock.count && !tdb_has_hash_locks(tdb)) {
536 tdb->log(tdb, TDB_DEBUG_FATAL, tdb->log_priv,
537 "tdb_expand: must hold lock during expand\n");
541 /* Only one person can expand file at a time. */
542 if (tdb_lock_expand(tdb, F_WRLCK) != 0)
545 /* Someone else may have expanded the file, so retry. */
546 old_size = tdb->map_size;
547 tdb->methods->oob(tdb, tdb->map_size + 1, true);
548 if (tdb->map_size != old_size) {
549 tdb_unlock_expand(tdb, F_WRLCK);
553 if (tdb->methods->expand_file(tdb, wanted*TDB_EXTENSION_FACTOR) == -1) {
554 tdb_unlock_expand(tdb, F_WRLCK);
558 /* We need to drop this lock before adding free record. */
559 tdb_unlock_expand(tdb, F_WRLCK);
561 return add_free_record(tdb, old_size, wanted * TDB_EXTENSION_FACTOR);
564 /* This won't fail: it will expand the database if it has to. */
565 tdb_off_t alloc(struct tdb_context *tdb, size_t keylen, size_t datalen,
566 uint64_t hash, bool growing)
570 /* We can't hold pointers during this: we could unmap! */
571 assert(!tdb->direct_access);
574 off = get_free(tdb, keylen, datalen, growing, hash);
575 if (likely(off != 0))
578 if (tdb_expand(tdb, adjust_size(keylen, datalen)))